module XMonad.Actions.Submap (
submap,
visualSubmap,
visualSubmapSorted,
submapDefault,
submapDefaultWithKey,
subName,
) where
import Data.Bits
import qualified Data.Map as M
import XMonad hiding (keys)
import XMonad.Prelude (fix, fromMaybe, keyToString, cleanKeyMask)
import XMonad.Util.XUtils
submap :: M.Map (KeyMask, KeySym) (X ()) -> X ()
submap :: Map (KeyMask, EventMask) (X ()) -> X ()
submap = X () -> Map (KeyMask, EventMask) (X ()) -> X ()
submapDefault (() -> X ()
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return ())
visualSubmap :: WindowConfig
-> M.Map (KeyMask, KeySym) (String, X ())
-> X ()
visualSubmap :: WindowConfig -> Map (KeyMask, EventMask) (String, X ()) -> X ()
visualSubmap = ([((KeyMask, EventMask), String)]
-> [((KeyMask, EventMask), String)])
-> WindowConfig -> Map (KeyMask, EventMask) (String, X ()) -> X ()
visualSubmapSorted [((KeyMask, EventMask), String)]
-> [((KeyMask, EventMask), String)]
forall a. a -> a
id
visualSubmapSorted :: ([((KeyMask, KeySym), String)] -> [((KeyMask, KeySym), String)])
-> WindowConfig
-> M.Map (KeyMask, KeySym) (String, X ())
-> X ()
visualSubmapSorted :: ([((KeyMask, EventMask), String)]
-> [((KeyMask, EventMask), String)])
-> WindowConfig -> Map (KeyMask, EventMask) (String, X ()) -> X ()
visualSubmapSorted [((KeyMask, EventMask), String)]
-> [((KeyMask, EventMask), String)]
sorted WindowConfig
wc Map (KeyMask, EventMask) (String, X ())
keys =
WindowConfig
-> [String] -> X (KeyMask, EventMask) -> X (KeyMask, EventMask)
forall a. WindowConfig -> [String] -> X a -> X a
withSimpleWindow WindowConfig
wc [String]
descriptions X (KeyMask, EventMask)
waitForKeyPress X (KeyMask, EventMask) -> ((KeyMask, EventMask) -> X ()) -> X ()
forall a b. X a -> (a -> X b) -> X b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(KeyMask
m', EventMask
s) ->
X () -> ((String, X ()) -> X ()) -> Maybe (String, X ()) -> X ()
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (() -> X ()
forall a. a -> X a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()) (String, X ()) -> X ()
forall a b. (a, b) -> b
snd ((KeyMask, EventMask)
-> Map (KeyMask, EventMask) (String, X ()) -> Maybe (String, X ())
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup (KeyMask
m', EventMask
s) Map (KeyMask, EventMask) (String, X ())
keys)
where
descriptions :: [String]
descriptions :: [String]
descriptions =
(((KeyMask, EventMask), String) -> String)
-> [((KeyMask, EventMask), String)] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (\((KeyMask, EventMask)
key, String
desc) -> (KeyMask, EventMask) -> String
keyToString (KeyMask, EventMask)
key String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
": " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
desc)
([((KeyMask, EventMask), String)] -> [String])
-> ([((KeyMask, EventMask), String)]
-> [((KeyMask, EventMask), String)])
-> [((KeyMask, EventMask), String)]
-> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [((KeyMask, EventMask), String)]
-> [((KeyMask, EventMask), String)]
sorted
([((KeyMask, EventMask), String)] -> [String])
-> [((KeyMask, EventMask), String)] -> [String]
forall a b. (a -> b) -> a -> b
$ [(KeyMask, EventMask)]
-> [String] -> [((KeyMask, EventMask), String)]
forall a b. [a] -> [b] -> [(a, b)]
zip (Map (KeyMask, EventMask) (String, X ()) -> [(KeyMask, EventMask)]
forall k a. Map k a -> [k]
M.keys Map (KeyMask, EventMask) (String, X ())
keys) (((String, X ()) -> String) -> [(String, X ())] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (String, X ()) -> String
forall a b. (a, b) -> a
fst (Map (KeyMask, EventMask) (String, X ()) -> [(String, X ())]
forall k a. Map k a -> [a]
M.elems Map (KeyMask, EventMask) (String, X ())
keys))
subName :: String -> X () -> (String, X ())
subName :: String -> X () -> (String, X ())
subName = (,)
submapDefault :: X () -> M.Map (KeyMask, KeySym) (X ()) -> X ()
submapDefault :: X () -> Map (KeyMask, EventMask) (X ()) -> X ()
submapDefault = ((KeyMask, EventMask) -> X ())
-> Map (KeyMask, EventMask) (X ()) -> X ()
submapDefaultWithKey (((KeyMask, EventMask) -> X ())
-> Map (KeyMask, EventMask) (X ()) -> X ())
-> (X () -> (KeyMask, EventMask) -> X ())
-> X ()
-> Map (KeyMask, EventMask) (X ())
-> X ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. X () -> (KeyMask, EventMask) -> X ()
forall a b. a -> b -> a
const
submapDefaultWithKey :: ((KeyMask, KeySym) -> X ())
-> M.Map (KeyMask, KeySym) (X ())
-> X ()
submapDefaultWithKey :: ((KeyMask, EventMask) -> X ())
-> Map (KeyMask, EventMask) (X ()) -> X ()
submapDefaultWithKey (KeyMask, EventMask) -> X ()
defAction Map (KeyMask, EventMask) (X ())
keys = X (KeyMask, EventMask)
waitForKeyPress X (KeyMask, EventMask) -> ((KeyMask, EventMask) -> X ()) -> X ()
forall a b. X a -> (a -> X b) -> X b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
\(KeyMask
m', EventMask
s) -> X () -> Maybe (X ()) -> X ()
forall a. a -> Maybe a -> a
fromMaybe ((KeyMask, EventMask) -> X ()
defAction (KeyMask
m', EventMask
s)) ((KeyMask, EventMask)
-> Map (KeyMask, EventMask) (X ()) -> Maybe (X ())
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup (KeyMask
m', EventMask
s) Map (KeyMask, EventMask) (X ())
keys)
waitForKeyPress :: X (KeyMask, KeySym)
waitForKeyPress :: X (KeyMask, EventMask)
waitForKeyPress = do
XConf{ theRoot :: XConf -> EventMask
theRoot = EventMask
root, display :: XConf -> Display
display = Display
dpy } <- X XConf
forall r (m :: * -> *). MonadReader r m => m r
ask
IO CInt -> X CInt
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO CInt -> X CInt) -> IO CInt -> X CInt
forall a b. (a -> b) -> a -> b
$ do Display
-> EventMask -> Bool -> CInt -> CInt -> EventMask -> IO CInt
grabKeyboard Display
dpy EventMask
root Bool
False CInt
grabModeAsync CInt
grabModeAsync EventMask
currentTime
Display
-> EventMask
-> Bool
-> EventMask
-> CInt
-> CInt
-> EventMask
-> EventMask
-> EventMask
-> IO CInt
grabPointer Display
dpy EventMask
root Bool
False EventMask
buttonPressMask CInt
grabModeAsync CInt
grabModeAsync
EventMask
none EventMask
none EventMask
currentTime
(KeyMask
m, EventMask
s) <- IO (KeyMask, EventMask) -> X (KeyMask, EventMask)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO (KeyMask, EventMask) -> X (KeyMask, EventMask))
-> IO (KeyMask, EventMask) -> X (KeyMask, EventMask)
forall a b. (a -> b) -> a -> b
$ (XEventPtr -> IO (KeyMask, EventMask)) -> IO (KeyMask, EventMask)
forall a. (XEventPtr -> IO a) -> IO a
allocaXEvent ((XEventPtr -> IO (KeyMask, EventMask)) -> IO (KeyMask, EventMask))
-> (XEventPtr -> IO (KeyMask, EventMask))
-> IO (KeyMask, EventMask)
forall a b. (a -> b) -> a -> b
$ \XEventPtr
p -> (IO (KeyMask, EventMask) -> IO (KeyMask, EventMask))
-> IO (KeyMask, EventMask)
forall a. (a -> a) -> a
fix ((IO (KeyMask, EventMask) -> IO (KeyMask, EventMask))
-> IO (KeyMask, EventMask))
-> (IO (KeyMask, EventMask) -> IO (KeyMask, EventMask))
-> IO (KeyMask, EventMask)
forall a b. (a -> b) -> a -> b
$ \IO (KeyMask, EventMask)
nextkey -> do
Display -> EventMask -> XEventPtr -> IO ()
maskEvent Display
dpy (EventMask
keyPressMask EventMask -> EventMask -> EventMask
forall a. Bits a => a -> a -> a
.|. EventMask
buttonPressMask) XEventPtr
p
Event
ev <- XEventPtr -> IO Event
getEvent XEventPtr
p
case Event
ev of
KeyEvent { ev_keycode :: Event -> KeyCode
ev_keycode = KeyCode
code, ev_state :: Event -> KeyMask
ev_state = KeyMask
m } -> do
EventMask
keysym <- Display -> KeyCode -> CInt -> IO EventMask
keycodeToKeysym Display
dpy KeyCode
code CInt
0
if EventMask -> Bool
isModifierKey EventMask
keysym
then IO (KeyMask, EventMask)
nextkey
else (KeyMask, EventMask) -> IO (KeyMask, EventMask)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (KeyMask
m, EventMask
keysym)
Event
_ -> (KeyMask, EventMask) -> IO (KeyMask, EventMask)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (KeyMask
0, EventMask
0)
KeyMask
m' <- X (KeyMask -> KeyMask)
cleanKeyMask X (KeyMask -> KeyMask) -> X KeyMask -> X KeyMask
forall a b. X (a -> b) -> X a -> X b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMask -> X KeyMask
forall a. a -> X a
forall (f :: * -> *) a. Applicative f => a -> f a
pure KeyMask
m
IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO () -> X ()) -> IO () -> X ()
forall a b. (a -> b) -> a -> b
$ do Display -> EventMask -> IO ()
ungrabPointer Display
dpy EventMask
currentTime
Display -> EventMask -> IO ()
ungrabKeyboard Display
dpy EventMask
currentTime
Display -> Bool -> IO ()
sync Display
dpy Bool
False
(KeyMask, EventMask) -> X (KeyMask, EventMask)
forall a. a -> X a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (KeyMask
m', EventMask
s)