-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Actions.Submap
-- Description :  Create a sub-mapping of key bindings.
-- Copyright   :  (c) Jason Creighton <jcreigh@gmail.com>
-- License     :  BSD3-style (see LICENSE)
--
-- Maintainer  :  Jason Creighton <jcreigh@gmail.com>
-- Stability   :  unstable
-- Portability :  unportable
--
-- A module that allows the user to create a sub-mapping of key bindings.
--
-----------------------------------------------------------------------------

module XMonad.Actions.Submap (
                             -- * Usage
                             -- $usage
                             submap,
                             submapDefault,
                             submapDefaultWithKey
                            ) where
import Data.Bits
import XMonad.Prelude (fix, fromMaybe)
import XMonad hiding (keys)
import qualified Data.Map as M

{- $usage




First, import this module into your @~\/.xmonad\/xmonad.hs@:

> import XMonad.Actions.Submap

Allows you to create a sub-mapping of keys. Example:

>    , ((modm, xK_a), submap . M.fromList $
>        [ ((0, xK_n),     spawn "mpc next")
>        , ((0, xK_p),     spawn "mpc prev")
>        , ((0, xK_z),     spawn "mpc random")
>        , ((0, xK_space), spawn "mpc toggle")
>        ])

So, for example, to run 'spawn \"mpc next\"', you would hit mod-a (to
trigger the submapping) and then 'n' to run that action. (0 means \"no
modifier\"). You are, of course, free to use any combination of
modifiers in the submapping. However, anyModifier will not work,
because that is a special value passed to XGrabKey() and not an actual
modifier.

For detailed instructions on editing your key bindings, see
"XMonad.Doc.Extending#Editing_key_bindings".

-}

-- | Given a 'Data.Map.Map' from key bindings to X () actions, return
--   an action which waits for a user keypress and executes the
--   corresponding action, or does nothing if the key is not found in
--   the map.
submap :: M.Map (KeyMask, KeySym) (X ()) -> X ()
submap :: Map (KeyMask, EventMask) (X ()) -> X ()
submap = X () -> Map (KeyMask, EventMask) (X ()) -> X ()
submapDefault (() -> X ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())

-- | Like 'submap', but executes a default action if the key did not match.
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

-- | Like 'submapDefault', but sends the unmatched key to the default
-- action as argument.
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 = do
    XConf { theRoot :: XConf -> EventMask
theRoot = EventMask
root, display :: XConf -> Display
display = Display
d } <- 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
$ Display
-> EventMask -> Bool -> CInt -> CInt -> EventMask -> IO CInt
grabKeyboard Display
d EventMask
root Bool
False CInt
grabModeAsync CInt
grabModeAsync EventMask
currentTime
    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
$ Display
-> EventMask
-> Bool
-> EventMask
-> CInt
-> CInt
-> EventMask
-> EventMask
-> EventMask
-> IO CInt
grabPointer Display
d 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
d (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
d KeyCode
code CInt
0
            if EventMask -> Bool
isModifierKey EventMask
keysym
                then IO (KeyMask, EventMask)
nextkey
                else (KeyMask, EventMask) -> IO (KeyMask, EventMask)
forall (m :: * -> *) a. Monad m => a -> m a
return (KeyMask
m, EventMask
keysym)
          Event
_ -> (KeyMask, EventMask) -> IO (KeyMask, EventMask)
forall (m :: * -> *) a. Monad m => a -> m a
return (KeyMask
0, EventMask
0)
    -- Remove num lock mask and Xkb group state bits
    KeyMask
m' <- KeyMask -> X KeyMask
cleanMask (KeyMask -> X KeyMask) -> KeyMask -> X KeyMask
forall a b. (a -> b) -> a -> b
$ KeyMask
m KeyMask -> KeyMask -> KeyMask
forall a. Bits a => a -> a -> a
.&. ((KeyMask
1 KeyMask -> Int -> KeyMask
forall a. Bits a => a -> Int -> a
`shiftL` Int
12) KeyMask -> KeyMask -> KeyMask
forall a. Num a => a -> a -> a
- KeyMask
1)

    IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO () -> X ()) -> IO () -> X ()
forall a b. (a -> b) -> a -> b
$ Display -> EventMask -> IO ()
ungrabPointer Display
d EventMask
currentTime
    IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO () -> X ()) -> IO () -> X ()
forall a b. (a -> b) -> a -> b
$ Display -> EventMask -> IO ()
ungrabKeyboard Display
d EventMask
currentTime
    IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO () -> X ()) -> IO () -> X ()
forall a b. (a -> b) -> a -> b
$ Display -> Bool -> IO ()
sync Display
d Bool
False

    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)