diff options
Diffstat (limited to 'pkgs/haskell')
| -rw-r--r-- | pkgs/haskell/xmonad-tv/src/SuperToRiver.hs | 95 | ||||
| -rw-r--r-- | pkgs/haskell/xmonad-tv/src/main.hs | 6 |
2 files changed, 100 insertions, 1 deletions
diff --git a/pkgs/haskell/xmonad-tv/src/SuperToRiver.hs b/pkgs/haskell/xmonad-tv/src/SuperToRiver.hs new file mode 100644 index 0000000..558aca5 --- /dev/null +++ b/pkgs/haskell/xmonad-tv/src/SuperToRiver.hs @@ -0,0 +1,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) diff --git a/pkgs/haskell/xmonad-tv/src/main.hs b/pkgs/haskell/xmonad-tv/src/main.hs index 05817af..606d03e 100644 --- a/pkgs/haskell/xmonad-tv/src/main.hs +++ b/pkgs/haskell/xmonad-tv/src/main.hs @@ -44,6 +44,7 @@ import Data.Ratio import XMonad.Hooks.Place (placeHook, smart) import XMonad.Actions.PerWorkspaceKeys (chooseAction) +import SuperToRiver qualified import Shutdown (shutdown, newShutdownEventHandler) import System.IO.Unsafe (unsafePerformIO) @@ -98,7 +99,9 @@ mainNoArgs = do { terminal = {-pkg:alacritty-tv-}"alacritty" , clientMask = clientMask def .|. focusChangeMask , modMask = mod4Mask - , keys = myKeys + , keys = \c -> + myKeys c + `Map.union` SuperToRiver.bindings , layoutHook = refocusLastLayoutHook $ gaps (zip [U,R,D,L] myScreenGaps) $ @@ -124,6 +127,7 @@ mainNoArgs = do , normalBorderColor = "#1c1c1c" , focusedBorderColor = "#f000b0" , handleEventHook = handleFocusChangeEvent <> handleShutdownEvent + , logHook = SuperToRiver.logHook } directories <- getDirectories launch config directories |
