-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Actions.UpdateFocus
-- Description :  Updates the focus on mouse move in unfocused windows.
-- Copyright   :  (c) Daniel Schoepe
-- License     :  BSD3-style (see LICENSE)
--
-- Maintainer  :  Daniel Schoepe <asgaroth_@gmx.de>
-- Stability   :  unstable
-- Portability :  unportable
--
-- Updates the focus on mouse move in unfocused windows.
--
-----------------------------------------------------------------------------

module XMonad.Actions.UpdateFocus (
    -- * Usage
    -- $usage
    focusOnMouseMove,
    adjustEventInput,
    focusUnderPointer,
) where

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

-- $usage
-- To make the focus update on mouse movement within an unfocused window, add the
-- following to your @xmonad.hs@:
--
-- > import XMonad.Actions.UpdateFocus
-- > xmonad $ def {
-- >   ..
-- >   startupHook = adjustEventInput
-- >   handleEventHook = focusOnMouseMove
-- >   ..
-- > }
--
-- This module is probably only useful when focusFollowsMouse is set to True(default).

-- | Changes the focus if the mouse is moved within an unfocused window.
focusOnMouseMove :: Event -> X All
focusOnMouseMove :: Event -> X All
focusOnMouseMove MotionEvent{ ev_x :: Event -> CInt
ev_x = CInt
x, ev_y :: Event -> CInt
ev_y = CInt
y, ev_window :: Event -> Window
ev_window = Window
root } = do
    -- check only every 15 px to avoid excessive calls to translateCoordinates
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (CInt
x forall a. Integral a => a -> a -> a
`mod` CInt
15 forall a. Eq a => a -> a -> Bool
== CInt
0 Bool -> Bool -> Bool
|| CInt
y forall a. Integral a => a -> a -> a
`mod` CInt
15 forall a. Eq a => a -> a -> Bool
== CInt
0) forall a b. (a -> b) -> a -> b
$ do
      Display
dpy <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> Display
display
      Maybe Window
foc <- forall a. (WindowSet -> X a) -> X a
withWindowSet forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a s sd. StackSet i l a s sd -> Maybe a
W.peek
      -- get the window under the pointer:
      (Bool
_,Position
_,Position
_,Window
w) <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
io forall a b. (a -> b) -> a -> b
$ Display
-> Window
-> Window
-> Position
-> Position
-> IO (Bool, Position, Position, Window)
translateCoordinates Display
dpy Window
root Window
root (forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
x) (forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
y)
      forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Maybe Window
foc forall a. Eq a => a -> a -> Bool
/= forall a. a -> Maybe a
Just Window
w) forall a b. (a -> b) -> a -> b
$ Window -> X ()
focus Window
w
    forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> All
All Bool
True)
focusOnMouseMove Event
_ = forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> All
All Bool
True)

-- | Adjusts the event mask to pick up pointer movements.
adjustEventInput :: X ()
adjustEventInput :: X ()
adjustEventInput = forall a. (Display -> X a) -> X a
withDisplay forall a b. (a -> b) -> a -> b
$ \Display
dpy -> do
  Window
rootw <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> Window
theRoot
  forall (m :: * -> *) a. MonadIO m => IO a -> m a
io forall a b. (a -> b) -> a -> b
$ Display -> Window -> Window -> IO ()
selectInput Display
dpy Window
rootw forall a b. (a -> b) -> a -> b
$  Window
substructureRedirectMask forall a. Bits a => a -> a -> a
.|. Window
substructureNotifyMask
                                forall a. Bits a => a -> a -> a
.|. Window
enterWindowMask forall a. Bits a => a -> a -> a
.|. Window
leaveWindowMask forall a. Bits a => a -> a -> a
.|. Window
structureNotifyMask
                                forall a. Bits a => a -> a -> a
.|. Window
buttonPressMask forall a. Bits a => a -> a -> a
.|. Window
pointerMotionMask

-- | Focus the window under the mouse pointer, unless we're currently changing
-- focus with the mouse or dragging. This is the inverse to
-- "XMonad.Actions.UpdatePointer": instead of moving the mouse pointer to
-- match the focus, we change the focus to match the mouse pointer.
--
-- This is meant to be used together with
-- 'XMonad.Actions.UpdatePointer.updatePointer' in individual key bindings.
-- Bindings that change focus should invoke
-- 'XMonad.Actions.UpdatePointer.updatePointer' at the end, bindings that
-- switch workspaces or change layouts should call 'focusUnderPointer' at the
-- end. Neither should go to 'logHook', as that would override the other.
--
-- This is more finicky to set up than 'focusOnMouseMove', but ensures that
-- focus is updated immediately, without having to touch the mouse.
focusUnderPointer :: X ()
focusUnderPointer :: X ()
focusUnderPointer = X Bool -> X () -> X ()
whenX (Bool -> Bool
not forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> Bool
mouseFocused forall (m :: * -> *). Monad m => m Bool -> m Bool -> m Bool
<||> forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (forall a. Maybe a -> Bool
isJust forall b c a. (b -> c) -> (a -> b) -> a -> c
. XState -> Maybe (Position -> Position -> X (), X ())
dragging))) forall a b. (a -> b) -> a -> b
$ do
  Display
dpy <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> Display
display
  Window
root <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> Window
theRoot
  (Bool
_, Window
_, Window
w', CInt
_, CInt
_, 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 Window
w <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (forall i l a s sd. StackSet i l a s sd -> Maybe a
W.peek forall b c a. (b -> c) -> (a -> b) -> a -> c
. XState -> WindowSet
windowset)
  forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Window
w' forall a. Eq a => a -> a -> Bool
/= Window
none Bool -> Bool -> Bool
&& forall a. a -> Maybe a
Just Window
w' forall a. Eq a => a -> a -> Bool
/= Maybe Window
w) (Window -> X ()
focus Window
w')