summaryrefslogtreecommitdiffstats
path: root/pkgs/haskell/xmonad-tv/src/SuperToRiver.hs
blob: 558aca5613819ad64c9aba26e1668d186b3764ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
-- | Hand the Super keys to a nested river session.
--
-- xmonad grabs its key bindings on the root window, so a river session
-- running in an X11 window (its title is "river - X11-1") never sees
-- them.  While 'toggle' is on and such a window is focused, xmonad
-- releases every binding that involves mod4 or a Super key, and the
-- focused border is orange.
--
-- > import qualified SuperToRiver
-- >
-- >   , keys = \c -> myKeys c `Map.union` SuperToRiver.bindings
-- >   , logHook = SuperToRiver.logHook
module SuperToRiver
    ( bindings
    , logHook
    , toggle
    , toggleKeys
    , borderColor
    , wantsSuper
    )
  where

import Control.Monad (when)
import Data.Bits ((.&.))
import Data.Foldable (traverse_)
import Data.Map (Map)
import XMonad hiding (logHook)
import qualified Data.List
import qualified Data.Map as Map
import qualified XMonad.StackSet as W
import qualified XMonad.Util.ExtensibleState as XS


-- | Windows that can get the Super keys instead of xmonad.
wantsSuper :: Query Bool
wantsSuper = Data.List.isPrefixOf "river - X11-" <$> title

-- | Border of the focused window while the toggle is on.
borderColor :: String
borderColor = "#ff8000"

-- | Control_L, Super_L and Alt_L, pressed in any order.  The binding that
-- fires is the one for the last key, with the other two held.
toggleKeys :: [(KeyMask, KeySym)]
toggleKeys =
    [ (controlMask .|. mod1Mask, xK_Super_L)
    , (controlMask .|. mod4Mask, xK_Alt_L)
    , (mod4Mask .|. mod1Mask, xK_Control_L)
    ]

-- | 'toggleKeys' bound to 'toggle'.
bindings :: Map (KeyMask, KeySym) (X ())
bindings = Map.fromList [ (k, toggle) | k <- toggleKeys ]


-- Whether a focused river window gets the Super keys.  Off at start.
newtype Enabled = Enabled Bool

instance ExtensionClass Enabled where
    initialValue = Enabled False

-- Whether xmonad's Super grabs are currently released.
newtype Released = Released Bool

instance ExtensionClass Released where
    initialValue = Released False


-- | Switch handing the Super keys to river on or off.
toggle :: X ()
toggle = do
    XS.modify $ \(Enabled on) -> Enabled (not on)
    refresh  -- runs the logHook

-- | While the toggle is on and a river window is focused, grab only the
-- bindings that involve neither mod4 nor a Super key, apart from the
-- toggle itself.  While the toggle is on, the focused border is orange.
logHook :: X ()
logHook = do
    Enabled on <- XS.get
    focused <- gets (W.peek . windowset)
    river <- maybe (return False) (runQuery wantsSuper) focused
    let wanted = on && river
    Released released <- XS.get
    when (wanted /= released) $ do
      XConf { display = dpy, theRoot = rootw } <- ask
      let usesSuper (m, k) = m .&. mod4Mask /= 0 || k `elem` [xK_Super_L, xK_Super_R]
          keep b = not (wanted && usesSuper b) || b `elem` toggleKeys
          grab (km, kc) = io $ grabKey dpy kc km rootw True grabModeAsync grabModeAsync
      io $ ungrabKey dpy anyKey anyModifier rootw
      traverse_ grab =<< mkGrabs . filter keep =<< asks (Map.keys . keyActions)
      XS.put (Released wanted)
    when on $ whenJust focused $ \w -> withDisplay $ \dpy -> do
      pixel <- io (initColor dpy borderColor)
      whenJust pixel (setWindowBorderWithFallback dpy w borderColor)