Changed semantics of UpdatePointer to move to nearest point

This commit is contained in:
robreim 2008-03-01 14:31:26 +00:00
parent 26de20d294
commit d44253f17f

View File

@ -10,7 +10,7 @@
-- --
-- Causes the pointer to follow whichever window focus changes to. Compliments -- Causes the pointer to follow whichever window focus changes to. Compliments
-- the idea of switching focus as the mouse crosses window boundaries to -- the idea of switching focus as the mouse crosses window boundaries to
-- keep the mouse near the currently focussed window -- keep the mouse near the currently focused window
-- --
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
@ -33,22 +33,30 @@ import Control.Monad
-- --
-- Enable it by including it in your logHook definition. Eg: -- Enable it by including it in your logHook definition. Eg:
-- --
-- > logHook = updatePointer (1%2) (1%2) -- > logHook = updatePointer
-- --
-- which will move the pointer to the middle of a newly focused window if the -- which will move the pointer to the nearest point of a newly focused window
-- focus moves away from the pointer
-- | Update the pointer's location to the currently focused window unless it's -- | Update the pointer's location to the nearest point of the currently focused
-- already there -- window unless it's already there
updatePointer :: Rational -> Rational -> X () updatePointer :: X ()
updatePointer h v = withFocused $ \w -> do updatePointer = withFocused $ \w -> do
dpy <- asks display dpy <- asks display
root <- asks theRoot root <- asks theRoot
wa <- io $ getWindowAttributes dpy w wa <- io $ getWindowAttributes dpy w
(sameRoot,_,w',_,_,_,_,_) <- io $ queryPointer dpy root (_sameRoot,_,w',rootx,rooty,_,_,_) <- io $ queryPointer dpy root
unless (sameRoot && w == w') $ -- Can sameRoot ever be false in this case? I'm going to assume not
io $ warpPointer dpy none w 0 0 0 0 unless (w == w') $ do
(fraction h (wa_width wa)) (fraction v (wa_height wa)) let x = moveWithin rootx (wa_x wa) ((wa_x wa) + (wa_width wa))
where fraction x y = floor (x * fromIntegral y) let y = moveWithin rooty (wa_y wa) ((wa_y wa) + (wa_height wa))
io $ warpPointer dpy none root 0 0 0 0 (fromIntegral x) (fromIntegral y)
moveWithin :: Integral a => a -> a -> a -> a
moveWithin current lower upper =
if current < lower
then lower
else if current > upper
then upper
else current