You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UpdatePointer.hs 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. {-# LANGUAGE ScopedTypeVariables #-}
  2. -----------------------------------------------------------------------------
  3. -- |
  4. -- Module : XMonadContrib.UpdatePointer
  5. -- Copyright : (c) Robert Marlow <robreim@bobturf.org>, 2015 Evgeny Kurnevsky
  6. -- License : BSD3-style (see LICENSE)
  7. --
  8. -- Maintainer : Robert Marlow <robreim@bobturf.org>
  9. -- Stability : stable
  10. -- Portability : portable
  11. --
  12. -- Causes the pointer to follow whichever window focus changes to. Compliments
  13. -- the idea of switching focus as the mouse crosses window boundaries to
  14. -- keep the mouse near the currently focused window
  15. --
  16. -----------------------------------------------------------------------------
  17. module XMonad.Actions.UpdatePointer
  18. (
  19. -- * Usage
  20. -- $usage
  21. updatePointer
  22. )
  23. where
  24. import XMonad
  25. import XMonad.Util.XUtils (fi)
  26. import Control.Arrow
  27. import Control.Monad
  28. import XMonad.StackSet (member, peek, screenDetail, current)
  29. import Data.Maybe
  30. import Control.Exception
  31. -- $usage
  32. -- You can use this module with the following in your @~\/.xmonad\/xmonad.hs@:
  33. --
  34. -- > import XMonad
  35. -- > import XMonad.Actions.UpdatePointer
  36. --
  37. -- Enable it by including it in your logHook definition, e.g.:
  38. --
  39. -- > logHook = updatePointer (0.5, 0.5) (1, 1)
  40. --
  41. -- which will move the pointer to the nearest point of a newly focused
  42. -- window. The first argument establishes a reference point within the
  43. -- newly-focused window, while the second argument linearly interpolates
  44. -- between said reference point and the edges of the newly-focused window to
  45. -- obtain a bounding box for the pointer.
  46. --
  47. -- > logHook = updatePointer (0.5, 0.5) (0, 0) -- exact centre of window
  48. -- > logHook = updatePointer (0.25, 0.25) (0.25, 0.25) -- near the top-left
  49. -- > logHook = updatePointer (0.5, 0.5) (1.1, 1.1) -- within 110% of the edge
  50. --
  51. -- To use this with an existing logHook, use >> :
  52. --
  53. -- > logHook = dynamicLog
  54. -- > >> updatePointer (1, 1) (0, 0)
  55. --
  56. -- which moves the pointer to the bottom-right corner of the focused window.
  57. -- | Update the pointer's location to the currently focused
  58. -- window or empty screen unless it's already there, or unless the user was changing
  59. -- focus with the mouse
  60. updatePointer :: (Rational, Rational) -> (Rational, Rational) -> X ()
  61. updatePointer refPos ratio = do
  62. ws <- gets windowset
  63. dpy <- asks display
  64. let defaultRect = screenRect $ screenDetail $ current ws
  65. rect <- case peek ws of
  66. Nothing -> return defaultRect
  67. Just w -> do tryAttributes <- io $ try $ getWindowAttributes dpy w
  68. return $ case tryAttributes of
  69. Left (_ :: SomeException) -> defaultRect
  70. Right attributes -> windowAttributesToRectangle attributes
  71. root <- asks theRoot
  72. mouseIsMoving <- asks mouseFocused
  73. (_sameRoot,_,currentWindow,rootX,rootY,_,_,_) <- io $ queryPointer dpy root
  74. drag <- gets dragging
  75. unless (pointWithin (fi rootX) (fi rootY) rect
  76. || mouseIsMoving
  77. || isJust drag
  78. || not (currentWindow `member` ws || currentWindow == none)) $ let
  79. -- focused rectangle
  80. (rectX, rectY) = (rect_x &&& rect_y) rect
  81. (rectW, rectH) = (fi . rect_width &&& fi . rect_height) rect
  82. -- reference position, with (0,0) and (1,1) being top-left and bottom-right
  83. refX = lerp (fst refPos) rectX (rectX + rectW)
  84. refY = lerp (snd refPos) rectY (rectY + rectH)
  85. -- final pointer bounds, lerped *outwards* from reference position
  86. boundsX = join (***) (lerp (fst ratio) refX) (rectX, rectX + rectW)
  87. boundsY = join (***) (lerp (snd ratio) refY) (rectY, rectY + rectH)
  88. -- ideally we ought to move the pointer in a straight line towards the
  89. -- reference point until it is within the above bounds, but…
  90. in io $ warpPointer dpy none root 0 0 0 0
  91. (round . clip boundsX $ fi rootX)
  92. (round . clip boundsY $ fi rootY)
  93. windowAttributesToRectangle :: WindowAttributes -> Rectangle
  94. windowAttributesToRectangle wa = Rectangle (fi (wa_x wa))
  95. (fi (wa_y wa))
  96. (fi (wa_width wa + 2 * wa_border_width wa))
  97. (fi (wa_height wa + 2 * wa_border_width wa))
  98. lerp :: (RealFrac r, Real a, Real b) => r -> a -> b -> r
  99. lerp r a b = (1 - r) * realToFrac a + r * realToFrac b
  100. clip :: Ord a => (a, a) -> a -> a
  101. clip (lower, upper) x = if x < lower then lower
  102. else if x > upper then upper else x