-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Actions.Warp
-- Description :  Warp the pointer to a given window or screen.
-- Copyright   :  (c) daniel@wagner-home.com
-- License     :  BSD3-style (see LICENSE)
--
-- Maintainer  :  daniel@wagner-home.com
-- Stability   :  unstable
-- Portability :  unportable
--
-- Warp the pointer to a given window or screen.
--
-----------------------------------------------------------------------------

module XMonad.Actions.Warp (
                           -- * Usage
                           -- $usage
                           banish,
                           banishScreen,
                           Corner(..),
                           warpToScreen,
                           warpToWindow
                          ) where

import XMonad.Prelude
import XMonad
import XMonad.StackSet as W

{- $usage
You can use this module with the following in your @xmonad.hs@:

> import XMonad.Actions.Warp

then add appropriate keybindings to warp the pointer; for example:

> , ((modm,   xK_z     ), warpToWindow (1%2) (1%2)) -- @@ Move pointer to currently focused window
>
>-- mod-ctrl-{w,e,r} @@ Move mouse pointer to screen 1, 2, or 3
>
>   [((modm .|. controlMask, key), warpToScreen sc (1%2) (1%2))
>       | (key, sc) <- zip [xK_w, xK_e, xK_r] [0..]]

Note that warping to a particular screen may change the focus.
-}

-- For detailed instructions on editing your key bindings, see
-- <https://xmonad.org/TUTORIAL.html#customizing-xmonad the tutorial>.


data Corner = UpperLeft | UpperRight | LowerLeft | LowerRight

{- | Move the mouse cursor to a corner of the focused window. Useful for
   uncluttering things.

   Internally, this uses numerical parameters. We parametrize on the 'Corner'
   type so the user need not see the violence inherent in
   the system.

   'warpToScreen' and 'warpToWindow' can be used in a variety of
   ways. Suppose you wanted to emulate Ratpoison's \'banish\' command,
   which moves the mouse pointer to a corner? warpToWindow can do that! -}
banish :: Corner -> X ()
banish :: Corner -> X ()
banish Corner
direction = case Corner
direction of
                     Corner
LowerRight -> Ratio Integer -> Ratio Integer -> X ()
warpToWindow Ratio Integer
1 Ratio Integer
1
                     Corner
LowerLeft  -> Ratio Integer -> Ratio Integer -> X ()
warpToWindow Ratio Integer
0 Ratio Integer
1
                     Corner
UpperLeft  -> Ratio Integer -> Ratio Integer -> X ()
warpToWindow Ratio Integer
0 Ratio Integer
0
                     Corner
UpperRight -> Ratio Integer -> Ratio Integer -> X ()
warpToWindow Ratio Integer
1 Ratio Integer
0

{- | Same as 'banish' but moves the mouse to the corner of the
   currently focused screen -}
banishScreen :: Corner -> X ()
banishScreen :: Corner -> X ()
banishScreen Corner
direction = case Corner
direction of
                           Corner
LowerRight -> Ratio Integer -> Ratio Integer -> X ()
warpToCurrentScreen Ratio Integer
1 Ratio Integer
1
                           Corner
LowerLeft  -> Ratio Integer -> Ratio Integer -> X ()
warpToCurrentScreen Ratio Integer
0 Ratio Integer
1
                           Corner
UpperLeft  -> Ratio Integer -> Ratio Integer -> X ()
warpToCurrentScreen Ratio Integer
0 Ratio Integer
0
                           Corner
UpperRight -> Ratio Integer -> Ratio Integer -> X ()
warpToCurrentScreen Ratio Integer
1 Ratio Integer
0
    where
      warpToCurrentScreen :: Ratio Integer -> Ratio Integer -> X ()
warpToCurrentScreen Ratio Integer
h Ratio Integer
v =
          do ws <- (XState
 -> StackSet
      WorkspaceId (Layout Window) Window ScreenId ScreenDetail)
-> X (StackSet
        WorkspaceId (Layout Window) Window ScreenId ScreenDetail)
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets XState
-> StackSet
     WorkspaceId (Layout Window) Window ScreenId ScreenDetail
windowset
             warpToScreen (W.screen $ current ws) h v
             windows (const ws)


fraction :: (Integral a, Integral b) => Rational -> a -> b
fraction :: forall a b. (Integral a, Integral b) => Ratio Integer -> a -> b
fraction Ratio Integer
f a
x = Ratio Integer -> b
forall b. Integral b => Ratio Integer -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor (Ratio Integer
f Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Num a => a -> a -> a
* a -> Ratio Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
x)

warp :: Window -> Position -> Position -> X ()
warp :: Window -> Int32 -> Int32 -> X ()
warp Window
w Int32
x Int32
y = (Display -> X ()) -> X ()
forall a. (Display -> X a) -> X a
withDisplay ((Display -> X ()) -> X ()) -> (Display -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \Display
d -> IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO () -> X ()) -> IO () -> X ()
forall a b. (a -> b) -> a -> b
$ Display
-> Window
-> Window
-> Int32
-> Int32
-> Word32
-> Word32
-> Int32
-> Int32
-> IO ()
warpPointer Display
d Window
none Window
w Int32
0 Int32
0 Word32
0 Word32
0 Int32
x Int32
y

-- | Warp the pointer to a given position relative to the currently
--   focused window.  Top left = (0,0), bottom right = (1,1).
warpToWindow :: Rational -> Rational -> X ()
warpToWindow :: Ratio Integer -> Ratio Integer -> X ()
warpToWindow Ratio Integer
h Ratio Integer
v = (Display -> X ()) -> X ()
forall a. (Display -> X a) -> X a
withDisplay ((Display -> X ()) -> X ()) -> (Display -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \Display
d -> (Window -> X ()) -> X ()
withFocused ((Window -> X ()) -> X ()) -> (Window -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \Window
w ->
  Display -> Window -> (WindowAttributes -> X ()) -> X ()
withWindowAttributes Display
d Window
w ((WindowAttributes -> X ()) -> X ())
-> (WindowAttributes -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \WindowAttributes
wa ->
    Window -> Int32 -> Int32 -> X ()
warp Window
w (Ratio Integer -> CInt -> Int32
forall a b. (Integral a, Integral b) => Ratio Integer -> a -> b
fraction Ratio Integer
h (WindowAttributes -> CInt
wa_width WindowAttributes
wa)) (Ratio Integer -> CInt -> Int32
forall a b. (Integral a, Integral b) => Ratio Integer -> a -> b
fraction Ratio Integer
v (WindowAttributes -> CInt
wa_height WindowAttributes
wa))

-- | Warp the pointer to the given position (top left = (0,0), bottom
--   right = (1,1)) on the given screen.
warpToScreen :: ScreenId -> Rational -> Rational -> X ()
warpToScreen :: ScreenId -> Ratio Integer -> Ratio Integer -> X ()
warpToScreen ScreenId
n Ratio Integer
h Ratio Integer
v = do
    root <- (XConf -> Window) -> X Window
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> Window
theRoot
    StackSet{current = x, visible = xs} <- gets windowset
    whenJust (fmap (screenRect . W.screenDetail) . find ((n==) . W.screen) $ x : xs)
        $ \Rectangle
r ->
            Window -> Int32 -> Int32 -> X ()
warp Window
root (Rectangle -> Int32
rect_x Rectangle
r Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Ratio Integer -> Word32 -> Int32
forall a b. (Integral a, Integral b) => Ratio Integer -> a -> b
fraction Ratio Integer
h (Rectangle -> Word32
rect_width  Rectangle
r))
                      (Rectangle -> Int32
rect_y Rectangle
r Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Ratio Integer -> Word32 -> Int32
forall a b. (Integral a, Integral b) => Ratio Integer -> a -> b
fraction Ratio Integer
v (Rectangle -> Word32
rect_height Rectangle
r))