{-# LANGUAGE LambdaCase #-}
module XMonad.Util.Grab
(
grabKP
, ungrabKP
, grabUngrab
, grab
, customRegrabEvHook
) where
import XMonad hiding (mkGrabs)
import Control.Monad ( when )
import Data.Bits ( setBit )
import Data.Foldable ( traverse_ )
import qualified Data.Map.Strict as M
import Data.Semigroup ( All(..) )
import Data.Traversable ( for )
grabKP :: KeyMask -> KeyCode -> X ()
grabKP :: KeyMask -> KeyCode -> X ()
grabKP KeyMask
mdfr KeyCode
kc = do
XConf { display :: XConf -> Display
display = Display
dpy, theRoot :: XConf -> Window
theRoot = Window
rootw } <- X XConf
forall r (m :: * -> *). MonadReader r m => m r
ask
IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (Display
-> KeyCode
-> KeyMask
-> Window
-> Bool
-> GrabMode
-> GrabMode
-> IO ()
grabKey Display
dpy KeyCode
kc KeyMask
mdfr Window
rootw Bool
True GrabMode
grabModeAsync GrabMode
grabModeAsync)
ungrabKP :: KeyMask -> KeyCode -> X ()
ungrabKP :: KeyMask -> KeyCode -> X ()
ungrabKP KeyMask
mdfr KeyCode
kc = do
XConf { display :: XConf -> Display
display = Display
dpy, theRoot :: XConf -> Window
theRoot = Window
rootw } <- X XConf
forall r (m :: * -> *). MonadReader r m => m r
ask
IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (Display -> KeyCode -> KeyMask -> Window -> IO ()
ungrabKey Display
dpy KeyCode
kc KeyMask
mdfr Window
rootw)
grabUngrab
:: [(KeyMask, KeySym)]
-> [(KeyMask, KeySym)]
-> X ()
grabUngrab :: [(KeyMask, Window)] -> [(KeyMask, Window)] -> X ()
grabUngrab [(KeyMask, Window)]
gr [(KeyMask, Window)]
ugr = do
[(KeyMask, Window)] -> [(KeyMask, KeyCode)]
f <- X ([(KeyMask, Window)] -> [(KeyMask, KeyCode)])
mkGrabs
((KeyMask, KeyCode) -> X ()) -> [(KeyMask, KeyCode)] -> X ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ ((KeyMask -> KeyCode -> X ()) -> (KeyMask, KeyCode) -> X ()
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry KeyMask -> KeyCode -> X ()
ungrabKP) ([(KeyMask, Window)] -> [(KeyMask, KeyCode)]
f [(KeyMask, Window)]
ugr)
((KeyMask, KeyCode) -> X ()) -> [(KeyMask, KeyCode)] -> X ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ ((KeyMask -> KeyCode -> X ()) -> (KeyMask, KeyCode) -> X ()
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry KeyMask -> KeyCode -> X ()
grabKP) ([(KeyMask, Window)] -> [(KeyMask, KeyCode)]
f [(KeyMask, Window)]
gr)
grab :: [(KeyMask, KeySym)] -> X ()
grab :: [(KeyMask, Window)] -> X ()
grab [(KeyMask, Window)]
ks = do
XConf { display :: XConf -> Display
display = Display
dpy, theRoot :: XConf -> Window
theRoot = Window
rootw } <- X XConf
forall r (m :: * -> *). MonadReader r m => m r
ask
IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (Display -> KeyCode -> KeyMask -> Window -> IO ()
ungrabKey Display
dpy KeyCode
anyKey KeyMask
anyModifier Window
rootw)
[(KeyMask, Window)] -> [(KeyMask, Window)] -> X ()
grabUngrab [(KeyMask, Window)]
ks []
customRegrabEvHook :: X () -> Event -> X All
customRegrabEvHook :: X () -> Event -> X All
customRegrabEvHook X ()
regr = \case
e :: Event
e@MappingNotifyEvent{} -> do
IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (Event -> IO ()
refreshKeyboardMapping Event
e)
Bool -> X () -> X ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Event -> GrabMode
ev_request Event
e GrabMode -> [GrabMode] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [GrabMode
mappingKeyboard, GrabMode
mappingModifier])
(X () -> X ()) -> X () -> X ()
forall a b. (a -> b) -> a -> b
$ X ()
setNumlockMask
X () -> X () -> X ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> X ()
regr
All -> X All
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool -> All
All Bool
False)
Event
_ -> All -> X All
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool -> All
All Bool
True)
setNumlockMask :: X ()
setNumlockMask :: X ()
setNumlockMask = (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
dpy -> do
[(KeyMask, [KeyCode])]
ms <- IO [(KeyMask, [KeyCode])] -> X [(KeyMask, [KeyCode])]
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (Display -> IO [(KeyMask, [KeyCode])]
getModifierMapping Display
dpy)
[KeyMask]
xs <- [X KeyMask] -> X [KeyMask]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence
[ do
Window
ks <- IO Window -> X Window
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (Display -> KeyCode -> GrabMode -> IO Window
keycodeToKeysym Display
dpy KeyCode
kc GrabMode
0)
KeyMask -> X KeyMask
forall (f :: * -> *) a. Applicative f => a -> f a
pure (KeyMask -> X KeyMask) -> KeyMask -> X KeyMask
forall a b. (a -> b) -> a -> b
$ if Window
ks Window -> Window -> Bool
forall a. Eq a => a -> a -> Bool
== Window
xK_Num_Lock
then KeyMask -> Int -> KeyMask
forall a. Bits a => a -> Int -> a
setBit KeyMask
0 (KeyMask -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral KeyMask
m)
else KeyMask
0 :: KeyMask
| (KeyMask
m, [KeyCode]
kcs) <- [(KeyMask, [KeyCode])]
ms
, KeyCode
kc <- [KeyCode]
kcs
, KeyCode
kc KeyCode -> KeyCode -> Bool
forall a. Eq a => a -> a -> Bool
/= KeyCode
0
]
(XState -> XState) -> X ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((XState -> XState) -> X ()) -> (XState -> XState) -> X ()
forall a b. (a -> b) -> a -> b
$ \XState
s -> XState
s { numberlockMask :: KeyMask
numberlockMask = (KeyMask -> KeyMask -> KeyMask) -> KeyMask -> [KeyMask] -> KeyMask
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr KeyMask -> KeyMask -> KeyMask
forall a. Bits a => a -> a -> a
(.|.) KeyMask
0 [KeyMask]
xs }
mkGrabs :: X ([(KeyMask, KeySym)] -> [(KeyMask, KeyCode)])
mkGrabs :: X ([(KeyMask, Window)] -> [(KeyMask, KeyCode)])
mkGrabs = (Display -> X ([(KeyMask, Window)] -> [(KeyMask, KeyCode)]))
-> X ([(KeyMask, Window)] -> [(KeyMask, KeyCode)])
forall a. (Display -> X a) -> X a
withDisplay ((Display -> X ([(KeyMask, Window)] -> [(KeyMask, KeyCode)]))
-> X ([(KeyMask, Window)] -> [(KeyMask, KeyCode)]))
-> (Display -> X ([(KeyMask, Window)] -> [(KeyMask, KeyCode)]))
-> X ([(KeyMask, Window)] -> [(KeyMask, KeyCode)])
forall a b. (a -> b) -> a -> b
$ \Display
dpy -> do
let (GrabMode
minCode, GrabMode
maxCode) = Display -> (GrabMode, GrabMode)
displayKeycodes Display
dpy
allCodes :: [KeyCode]
allCodes = [GrabMode -> KeyCode
forall a b. (Integral a, Num b) => a -> b
fromIntegral GrabMode
minCode .. GrabMode -> KeyCode
forall a b. (Integral a, Num b) => a -> b
fromIntegral GrabMode
maxCode]
[Window]
syms <- IO [Window] -> X [Window]
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO [Window] -> X [Window])
-> ((KeyCode -> IO Window) -> IO [Window])
-> (KeyCode -> IO Window)
-> X [Window]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [KeyCode] -> (KeyCode -> IO Window) -> IO [Window]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
for [KeyCode]
allCodes ((KeyCode -> IO Window) -> X [Window])
-> (KeyCode -> IO Window) -> X [Window]
forall a b. (a -> b) -> a -> b
$ \KeyCode
code -> Display -> KeyCode -> GrabMode -> IO Window
keycodeToKeysym Display
dpy KeyCode
code GrabMode
0
let keysymMap :: Map Window [KeyCode]
keysymMap = ([KeyCode] -> [KeyCode] -> [KeyCode])
-> [(Window, [KeyCode])] -> Map Window [KeyCode]
forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
M.fromListWith [KeyCode] -> [KeyCode] -> [KeyCode]
forall a. [a] -> [a] -> [a]
(++) ([Window] -> [[KeyCode]] -> [(Window, [KeyCode])]
forall a b. [a] -> [b] -> [(a, b)]
zip [Window]
syms ([[KeyCode]] -> [(Window, [KeyCode])])
-> [[KeyCode]] -> [(Window, [KeyCode])]
forall a b. (a -> b) -> a -> b
$ KeyCode -> [KeyCode]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (KeyCode -> [KeyCode]) -> [KeyCode] -> [[KeyCode]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [KeyCode]
allCodes)
keysymToKeycodes :: Window -> [KeyCode]
keysymToKeycodes Window
sym = [KeyCode] -> Window -> Map Window [KeyCode] -> [KeyCode]
forall k a. Ord k => a -> k -> Map k a -> a
M.findWithDefault [] Window
sym Map Window [KeyCode]
keysymMap
[KeyMask]
extraMods <- X [KeyMask]
extraModifiers
([(KeyMask, Window)] -> [(KeyMask, KeyCode)])
-> X ([(KeyMask, Window)] -> [(KeyMask, KeyCode)])
forall (f :: * -> *) a. Applicative f => a -> f a
pure (([(KeyMask, Window)] -> [(KeyMask, KeyCode)])
-> X ([(KeyMask, Window)] -> [(KeyMask, KeyCode)]))
-> ([(KeyMask, Window)] -> [(KeyMask, KeyCode)])
-> X ([(KeyMask, Window)] -> [(KeyMask, KeyCode)])
forall a b. (a -> b) -> a -> b
$ \[(KeyMask, Window)]
ks -> do
(KeyMask
mask, Window
sym) <- [(KeyMask, Window)]
ks
KeyCode
keycode <- Window -> [KeyCode]
keysymToKeycodes Window
sym
KeyMask
extraMod <- [KeyMask]
extraMods
(KeyMask, KeyCode) -> [(KeyMask, KeyCode)]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (KeyMask
mask KeyMask -> KeyMask -> KeyMask
forall a. Bits a => a -> a -> a
.|. KeyMask
extraMod, KeyCode
keycode)