{-# LANGUAGE ScopedTypeVariables #-}
module XMonad.Actions.UpdatePointer
(
updatePointer
)
where
import XMonad
import XMonad.Prelude
import XMonad.StackSet (member, peek, screenDetail, current)
import Control.Arrow ((&&&), (***))
updatePointer :: (Rational, Rational) -> (Rational, Rational) -> X ()
updatePointer :: (Rational, Rational) -> (Rational, Rational) -> X ()
updatePointer (Rational, Rational)
refPos (Rational, Rational)
ratio = do
ws <- (XState -> WindowSet) -> X WindowSet
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets XState -> WindowSet
windowset
dpy <- asks display
let defaultRect = ScreenDetail -> Rectangle
screenRect (ScreenDetail -> Rectangle) -> ScreenDetail -> Rectangle
forall a b. (a -> b) -> a -> b
$ Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> ScreenDetail
forall i l a sid sd. Screen i l a sid sd -> sd
screenDetail (Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> ScreenDetail)
-> Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
-> ScreenDetail
forall a b. (a -> b) -> a -> b
$ WindowSet
-> Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
forall i l a sid sd. StackSet i l a sid sd -> Screen i l a sid sd
current WindowSet
ws
rect <- case peek ws of
Maybe Window
Nothing -> Rectangle -> X Rectangle
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return Rectangle
defaultRect
Just Window
w -> Rectangle
-> (WindowAttributes -> Rectangle)
-> Maybe WindowAttributes
-> Rectangle
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Rectangle
defaultRect WindowAttributes -> Rectangle
windowAttributesToRectangle
(Maybe WindowAttributes -> Rectangle)
-> X (Maybe WindowAttributes) -> X Rectangle
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Window -> X (Maybe WindowAttributes)
safeGetWindowAttributes Window
w
root <- asks theRoot
mouseIsMoving <- asks mouseFocused
(_sameRoot,_,currentWindow,rootX,rootY,_,_,_) <- io $ queryPointer dpy root
drag <- gets dragging
unless (pointWithin (fi rootX) (fi rootY) rect
|| mouseIsMoving
|| isJust drag
|| not (currentWindow `member` ws || currentWindow == none)) $ let
(rectX, rectY) = (rect_x &&& rect_y) rect
(rectW, rectH) = (fi . rect_width &&& fi . rect_height) rect
refX = Rational -> Position -> Position -> Rational
forall r a b. (RealFrac r, Real a, Real b) => r -> a -> b -> r
lerp ((Rational, Rational) -> Rational
forall a b. (a, b) -> a
fst (Rational, Rational)
refPos) Position
rectX (Position
rectX Position -> Position -> Position
forall a. Num a => a -> a -> a
+ Position
rectW)
refY = Rational -> Position -> Position -> Rational
forall r a b. (RealFrac r, Real a, Real b) => r -> a -> b -> r
lerp ((Rational, Rational) -> Rational
forall a b. (a, b) -> b
snd (Rational, Rational)
refPos) Position
rectY (Position
rectY Position -> Position -> Position
forall a. Num a => a -> a -> a
+ Position
rectH)
boundsX = ((Position -> Rational)
-> (Position -> Rational)
-> (Position, Position)
-> (Rational, Rational))
-> (Position -> Rational)
-> (Position, Position)
-> (Rational, Rational)
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Position -> Rational)
-> (Position -> Rational)
-> (Position, Position)
-> (Rational, Rational)
forall b c b' c'. (b -> c) -> (b' -> c') -> (b, b') -> (c, c')
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
(***) (Rational -> Rational -> Position -> Rational
forall r a b. (RealFrac r, Real a, Real b) => r -> a -> b -> r
lerp ((Rational, Rational) -> Rational
forall a b. (a, b) -> a
fst (Rational, Rational)
ratio) Rational
refX) (Position
rectX, Position
rectX Position -> Position -> Position
forall a. Num a => a -> a -> a
+ Position
rectW)
boundsY = ((Position -> Rational)
-> (Position -> Rational)
-> (Position, Position)
-> (Rational, Rational))
-> (Position -> Rational)
-> (Position, Position)
-> (Rational, Rational)
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Position -> Rational)
-> (Position -> Rational)
-> (Position, Position)
-> (Rational, Rational)
forall b c b' c'. (b -> c) -> (b' -> c') -> (b, b') -> (c, c')
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
(***) (Rational -> Rational -> Position -> Rational
forall r a b. (RealFrac r, Real a, Real b) => r -> a -> b -> r
lerp ((Rational, Rational) -> Rational
forall a b. (a, b) -> b
snd (Rational, Rational)
ratio) Rational
refY) (Position
rectY, Position
rectY Position -> Position -> Position
forall a. Num a => a -> a -> a
+ Position
rectH)
in io $ warpPointer dpy none root 0 0 0 0
(round . clip boundsX $ fi rootX)
(round . clip boundsY $ fi rootY)
windowAttributesToRectangle :: WindowAttributes -> Rectangle
windowAttributesToRectangle :: WindowAttributes -> Rectangle
windowAttributesToRectangle WindowAttributes
wa = Position -> Position -> Dimension -> Dimension -> Rectangle
Rectangle (CInt -> Position
forall a b. (Integral a, Num b) => a -> b
fi (WindowAttributes -> CInt
wa_x WindowAttributes
wa))
(CInt -> Position
forall a b. (Integral a, Num b) => a -> b
fi (WindowAttributes -> CInt
wa_y WindowAttributes
wa))
(CInt -> Dimension
forall a b. (Integral a, Num b) => a -> b
fi (WindowAttributes -> CInt
wa_width WindowAttributes
wa CInt -> CInt -> CInt
forall a. Num a => a -> a -> a
+ CInt
2 CInt -> CInt -> CInt
forall a. Num a => a -> a -> a
* WindowAttributes -> CInt
wa_border_width WindowAttributes
wa))
(CInt -> Dimension
forall a b. (Integral a, Num b) => a -> b
fi (WindowAttributes -> CInt
wa_height WindowAttributes
wa CInt -> CInt -> CInt
forall a. Num a => a -> a -> a
+ CInt
2 CInt -> CInt -> CInt
forall a. Num a => a -> a -> a
* WindowAttributes -> CInt
wa_border_width WindowAttributes
wa))
lerp :: (RealFrac r, Real a, Real b) => r -> a -> b -> r
lerp :: forall r a b. (RealFrac r, Real a, Real b) => r -> a -> b -> r
lerp r
r a
a b
b = (r
1 r -> r -> r
forall a. Num a => a -> a -> a
- r
r) r -> r -> r
forall a. Num a => a -> a -> a
* a -> r
forall a b. (Real a, Fractional b) => a -> b
realToFrac a
a r -> r -> r
forall a. Num a => a -> a -> a
+ r
r r -> r -> r
forall a. Num a => a -> a -> a
* b -> r
forall a b. (Real a, Fractional b) => a -> b
realToFrac b
b
clip :: Ord a => (a, a) -> a -> a
clip :: forall a. Ord a => (a, a) -> a -> a
clip (a
lower, a
upper) a
x
| a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
lower = a
lower
| a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
upper = a
upper
| Bool
otherwise = a
x