{-# LANGUAGE ScopedTypeVariables #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  XMonadContrib.UpdatePointer
-- Description :  Causes the pointer to follow whichever window focus changes to.
-- Copyright   :  (c) Robert Marlow <robreim@bobturf.org>, 2015 Evgeny Kurnevsky
-- License     :  BSD3-style (see LICENSE)
--
-- Maintainer  :  Robert Marlow <robreim@bobturf.org>
-- Stability   :  stable
-- Portability :  portable
--
-- Causes the pointer to follow whichever window focus changes to. Compliments
-- the idea of switching focus as the mouse crosses window boundaries to
-- keep the mouse near the currently focused window
--
-----------------------------------------------------------------------------

module XMonad.Actions.UpdatePointer
    (
     -- * Usage
     -- $usage
     updatePointer
    )
    where

import XMonad
import XMonad.Prelude
import XMonad.StackSet (member, peek, screenDetail, current)

import Control.Arrow ((&&&), (***))

-- $usage
-- You can use this module with the following in your @~\/.xmonad\/xmonad.hs@:
--
-- > import XMonad
-- > import XMonad.Actions.UpdatePointer
--
-- Enable it by including it in your logHook definition, e.g.:
--
-- > logHook = updatePointer (0.5, 0.5) (1, 1)
--
-- which will move the pointer to the nearest point of a newly focused
-- window. The first argument establishes a reference point within the
-- newly-focused window, while the second argument linearly interpolates
-- between said reference point and the edges of the newly-focused window to
-- obtain a bounding box for the pointer.
--
-- > logHook = updatePointer (0.5, 0.5) (0, 0) -- exact centre of window
-- > logHook = updatePointer (0.25, 0.25) (0.25, 0.25) -- near the top-left
-- > logHook = updatePointer (0.5, 0.5) (1.1, 1.1) -- within 110% of the edge
--
-- To use this with an existing logHook, use >> :
--
-- > logHook = dynamicLog
-- >           >> updatePointer (1, 1) (0, 0)
--
-- which moves the pointer to the bottom-right corner of the focused window.

-- | Update the pointer's location to the currently focused
-- window or empty screen unless it's already there, or unless the user was changing
-- focus with the mouse
--
-- See also 'XMonad.Actions.UpdateFocus.focusUnderPointer' for an inverse
-- operation that updates the focus instead. The two can be combined in a
-- single config if neither goes into 'logHook' but are invoked explicitly in
-- individual key bindings.
updatePointer :: (Rational, Rational) -> (Rational, Rational) -> X ()
updatePointer :: (Rational, Rational) -> (Rational, Rational) -> X ()
updatePointer (Rational, Rational)
refPos (Rational, Rational)
ratio = do
  WindowSet
ws <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets XState -> WindowSet
windowset
  Display
dpy <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> Display
display
  let defaultRect :: Rectangle
defaultRect = ScreenDetail -> Rectangle
screenRect forall a b. (a -> b) -> a -> b
$ forall i l a sid sd. Screen i l a sid sd -> sd
screenDetail forall a b. (a -> b) -> a -> b
$ forall i l a sid sd. StackSet i l a sid sd -> Screen i l a sid sd
current WindowSet
ws
  Rectangle
rect <- case forall i l a s sd. StackSet i l a s sd -> Maybe a
peek WindowSet
ws of
        Maybe Window
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return Rectangle
defaultRect
        Just Window
w  -> forall b a. b -> (a -> b) -> Maybe a -> b
maybe Rectangle
defaultRect WindowAttributes -> Rectangle
windowAttributesToRectangle
               forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Window -> X (Maybe WindowAttributes)
safeGetWindowAttributes Window
w

  Window
root <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> Window
theRoot
  Bool
mouseIsMoving <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> Bool
mouseFocused
  (Bool
_sameRoot,Window
_,Window
currentWindow,CInt
rootX,CInt
rootY,CInt
_,CInt
_,Modifier
_) <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
io forall a b. (a -> b) -> a -> b
$ Display
-> Window
-> IO (Bool, Window, Window, CInt, CInt, CInt, CInt, Modifier)
queryPointer Display
dpy Window
root
  Maybe (Position -> Position -> X (), X ())
drag <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets XState -> Maybe (Position -> Position -> X (), X ())
dragging
  forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Position -> Position -> Rectangle -> Bool
pointWithin (forall a b. (Integral a, Num b) => a -> b
fi CInt
rootX) (forall a b. (Integral a, Num b) => a -> b
fi CInt
rootY) Rectangle
rect
          Bool -> Bool -> Bool
|| Bool
mouseIsMoving
          Bool -> Bool -> Bool
|| forall a. Maybe a -> Bool
isJust Maybe (Position -> Position -> X (), X ())
drag
          Bool -> Bool -> Bool
|| Bool -> Bool
not (Window
currentWindow forall a i l s sd. Eq a => a -> StackSet i l a s sd -> Bool
`member` WindowSet
ws Bool -> Bool -> Bool
|| Window
currentWindow forall a. Eq a => a -> a -> Bool
== Window
none)) forall a b. (a -> b) -> a -> b
$ let
    -- focused rectangle
    (Position
rectX, Position
rectY) = (Rectangle -> Position
rect_x forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& Rectangle -> Position
rect_y) Rectangle
rect
    (Position
rectW, Position
rectH) = (forall a b. (Integral a, Num b) => a -> b
fi forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rectangle -> Dimension
rect_width forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& forall a b. (Integral a, Num b) => a -> b
fi forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rectangle -> Dimension
rect_height) Rectangle
rect
    -- reference position, with (0,0) and (1,1) being top-left and bottom-right
    refX :: Rational
refX = forall r a b. (RealFrac r, Real a, Real b) => r -> a -> b -> r
lerp (forall a b. (a, b) -> a
fst (Rational, Rational)
refPos) Position
rectX (Position
rectX forall a. Num a => a -> a -> a
+ Position
rectW)
    refY :: Rational
refY = forall r a b. (RealFrac r, Real a, Real b) => r -> a -> b -> r
lerp (forall a b. (a, b) -> b
snd (Rational, Rational)
refPos) Position
rectY (Position
rectY forall a. Num a => a -> a -> a
+ Position
rectH)
    -- final pointer bounds, lerped *outwards* from reference position
    boundsX :: (Rational, Rational)
boundsX = forall (m :: * -> *) a. Monad m => m (m a) -> m a
join forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
(***) (forall r a b. (RealFrac r, Real a, Real b) => r -> a -> b -> r
lerp (forall a b. (a, b) -> a
fst (Rational, Rational)
ratio) Rational
refX) (Position
rectX, Position
rectX forall a. Num a => a -> a -> a
+ Position
rectW)
    boundsY :: (Rational, Rational)
boundsY = forall (m :: * -> *) a. Monad m => m (m a) -> m a
join forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
(***) (forall r a b. (RealFrac r, Real a, Real b) => r -> a -> b -> r
lerp (forall a b. (a, b) -> b
snd (Rational, Rational)
ratio) Rational
refY) (Position
rectY, Position
rectY forall a. Num a => a -> a -> a
+ Position
rectH)
    -- ideally we ought to move the pointer in a straight line towards the
    -- reference point until it is within the above bounds, but…
    in forall (m :: * -> *) a. MonadIO m => IO a -> m a
io forall a b. (a -> b) -> a -> b
$ Display
-> Window
-> Window
-> Position
-> Position
-> Dimension
-> Dimension
-> Position
-> Position
-> IO ()
warpPointer Display
dpy Window
none Window
root Position
0 Position
0 Dimension
0 Dimension
0
        (forall a b. (RealFrac a, Integral b) => a -> b
round forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Ord a => (a, a) -> a -> a
clip (Rational, Rational)
boundsX forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fi CInt
rootX)
        (forall a b. (RealFrac a, Integral b) => a -> b
round forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Ord a => (a, a) -> a -> a
clip (Rational, Rational)
boundsY forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fi CInt
rootY)

windowAttributesToRectangle :: WindowAttributes -> Rectangle
windowAttributesToRectangle :: WindowAttributes -> Rectangle
windowAttributesToRectangle WindowAttributes
wa = Position -> Position -> Dimension -> Dimension -> Rectangle
Rectangle (forall a b. (Integral a, Num b) => a -> b
fi (WindowAttributes -> CInt
wa_x WindowAttributes
wa))
                                           (forall a b. (Integral a, Num b) => a -> b
fi (WindowAttributes -> CInt
wa_y WindowAttributes
wa))
                                           (forall a b. (Integral a, Num b) => a -> b
fi (WindowAttributes -> CInt
wa_width WindowAttributes
wa forall a. Num a => a -> a -> a
+ CInt
2 forall a. Num a => a -> a -> a
* WindowAttributes -> CInt
wa_border_width WindowAttributes
wa))
                                           (forall a b. (Integral a, Num b) => a -> b
fi (WindowAttributes -> CInt
wa_height WindowAttributes
wa forall a. Num a => a -> a -> a
+ CInt
2 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 forall a. Num a => a -> a -> a
- r
r) forall a. Num a => a -> a -> a
* forall a b. (Real a, Fractional b) => a -> b
realToFrac a
a forall a. Num a => a -> a -> a
+ r
r forall a. Num a => a -> a -> a
* 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 forall a. Ord a => a -> a -> Bool
< a
lower = a
lower
  | a
x forall a. Ord a => a -> a -> Bool
> a
upper = a
upper
  | Bool
otherwise = a
x