From 4e718217ada0367b220f0e2134dbf6cbdcb28977 Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Thu, 24 Mar 2022 10:38:05 -0600 Subject: Add more mouse motions. Add bindings for navigation to button15+<> --- src/Internal/Keys.hs | 48 +++++++++++++++++++++++++++++++++++--------- src/Internal/ScreenRotate.hs | 19 ++++++++++++++++++ 2 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 src/Internal/ScreenRotate.hs (limited to 'src/Internal') diff --git a/src/Internal/Keys.hs b/src/Internal/Keys.hs index 195e12f..1118788 100644 --- a/src/Internal/Keys.hs +++ b/src/Internal/Keys.hs @@ -45,6 +45,7 @@ import Internal.Lib import Internal.DMenu import Internal.PassMenu import Internal.Logger +import Internal.ScreenRotate (screenRotateForward, screenRotateBackward) type KeyMap l = XConfig l -> Map (KeyMask, KeySym) (X ()) type ButtonsMap l = XConfig l -> Map (KeyMask, Button) (Window -> X ()) @@ -376,6 +377,17 @@ mouseMap = runButtons $ do Map.lookup button (mouseMap config) subMouse = submapButtonsWithKey defaultButtons . flip runButtons config + + let continuous :: [(Button, X ())] -> Button -> Window -> X () + continuous actions button w = do + case find ((==button) . fst) actions of + Just (_, action) -> action + Nothing -> return () + + (subMouse $ + forM_ (map fst actions) $ \b -> + bind b $ noMod $ \w -> continuous actions b w) w + bind button1 $ do justMod $ \w -> focus w >> mouseMoveWindow w >> windows W.shiftMaster @@ -406,6 +418,9 @@ mouseMap = runButtons $ do bind button14 $ do noMod $ \_ -> click >> sendMessage ToggleZoom + bind button15 $ do + noMod $ \_ -> spawn "pavucontrol" + let mediaButtons = [ (button4, increaseVolume), (button5, decreaseVolume), @@ -416,19 +431,32 @@ mouseMap = runButtons $ do (button7, mediaNext) ] + forM_ (map fst mediaButtons) $ \b -> + bind b $ noMod $ continuous mediaButtons b - let continuous :: [(Button, X ())] -> Button -> Window -> X () - continuous actions button w = do - case find ((==button) . fst) actions of - Just (_, action) -> action - Nothing -> return () + bind button15 $ do - (subMouse $ - forM_ (map fst mediaButtons) $ \b -> - bind b $ noMod $ \w -> continuous actions b w) w + noMod $ subMouse $ do + + bind button15 $ do + noMod $ \_ -> jumpToLast - forM_ (map fst mediaButtons) $ \b -> - bind b $ noMod $ continuous mediaButtons b + + let workspaceButtons = [ + (button2, swapMaster), + + (button9, relativeWorkspaceShift next), + (button8, relativeWorkspaceShift prev), + + (button4, windows W.focusUp), + (button5, windows W.focusDown), + + (button7, windows screenRotateForward), + (button6, windows screenRotateBackward) + ] + + forM_ (map fst workspaceButtons) $ \b -> + bind b $ noMod $ continuous workspaceButtons b applyKeys :: XConfig l -> IO (XConfig l) applyKeys config = diff --git a/src/Internal/ScreenRotate.hs b/src/Internal/ScreenRotate.hs new file mode 100644 index 0000000..ff6417c --- /dev/null +++ b/src/Internal/ScreenRotate.hs @@ -0,0 +1,19 @@ +module Internal.ScreenRotate where + +import XMonad.StackSet as W + +screenRotateForward :: W.StackSet i l a sid sd -> W.StackSet i l a sid sd +screenRotateForward (W.StackSet current visible others floating) = do + let screens = current : visible + workspaces = tail $ cycle $ map W.workspace screens + (current':visible') = zipWith (\s w -> s {workspace = w} ) screens workspaces + in W.StackSet current' visible' others floating + +screenRotateBackward :: W.StackSet i l a sid sd -> W.StackSet i l a sid sd +screenRotateBackward (W.StackSet current visible others floating) = do + let screens = current : visible + workspaces = rcycle $ map W.workspace screens + (current':visible') = zipWith (\s w -> s {workspace = w} ) screens workspaces + in W.StackSet current' visible' others floating + + where rcycle l = last l : l -- cgit From e5f6b5109aab58b5d066ada7c542d0ecb991cafb Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Fri, 25 Mar 2022 15:32:06 -0600 Subject: Added a way to do per-window bindings in XMonad. This is particularly great for Chrome where one cannot remap the built-in bindings and some built-in bindings are really dumb (looking at you Ctrl+w!!). --- src/Internal/Intercept.hs | 157 ++++++++++++++++++++++++++++++++++++++++++++++ src/Internal/Keys.hs | 12 +++- 2 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 src/Internal/Intercept.hs (limited to 'src/Internal') diff --git a/src/Internal/Intercept.hs b/src/Internal/Intercept.hs new file mode 100644 index 0000000..987733b --- /dev/null +++ b/src/Internal/Intercept.hs @@ -0,0 +1,157 @@ +module Internal.Intercept where + +import XMonad + +import Text.Printf +import Data.Monoid (Endo(..)) +import Control.Monad.Trans.Class (lift) +import Control.Monad (forM, forM_) +import Data.Default (Default, def) +import Data.Map (Map) +import qualified Data.Map as Map +import qualified XMonad.Util.ExtensibleState as XS +import Data.Monoid (All(..)) + +import Internal.Logger + +type WindowHook = Query () + +newtype NoPersist a = NoPersist a + deriving (Typeable) + +instance Show (NoPersist a) where + show (NoPersist _) = show () + +instance (Default a) => Read (NoPersist a) where + readsPrec i s = map (\(_, s) -> (NoPersist def, s)) (readsPrec i s :: [((), String)]) + +instance (Default a) => Default (NoPersist a) where + def = NoPersist def + +instance (Default a, Typeable a) => ExtensionClass (NoPersist a) where + initialValue = NoPersist def + + +data InterceptState = InterceptState (NoPersist (Map (KeyMask, KeySym) (X ()))) + +data RemapState = RemapState (NoPersist (Map (Window, (KeyMask, KeySym)) (X ()))) + +instance ExtensionClass InterceptState where + initialValue = InterceptState def + +instance ExtensionClass RemapState where + initialValue = RemapState def + +remapHook :: Event -> X All +remapHook event = do + RemapState (NoPersist map) <- XS.get + + case event of + KeyEvent { ev_window = win, ev_event_type = typ, ev_keycode = code, ev_state = m } + | typ == keyPress-> do + XConf {display = dpy, theRoot = rootw} <- ask + keysym <- io $ keycodeToKeysym dpy code 0 + + case Map.lookup (win, (m, keysym)) map of + + Just xdo -> do + xdo + return (All False) + + Nothing -> return (All True) + + _ -> return (All True) + +interceptHook :: Event -> X All +interceptHook event = do + InterceptState (NoPersist map) <- XS.get + case event of + KeyEvent { ev_event_type = typ, ev_keycode = code, ev_state = m } + | typ == keyPress-> do + XConf {display = dpy, theRoot = rootw} <- ask + keysym <- io $ keycodeToKeysym dpy code 0 + + case Map.lookup (m, keysym) map of + + Just xdo -> do + xdo + mapM_ (\m' -> io $ ungrabKey dpy code (m .|. m') rootw) + =<< extraModifiers + return (All False) + + Nothing -> return (All True) + + _ -> return (All True) + +setIntercept :: (KeyMask, KeySym) -> X () -> X () +setIntercept (keyMask, keysym) action = do + XS.modify $ \(InterceptState (NoPersist m)) -> InterceptState $ NoPersist $ + Map.insert (keyMask, keysym) action m + XConf { display = dpy, theRoot = rootw } <- ask + + doGrab dpy rootw (keyMask, keysym) + + +getKeyCodesForKeysym :: Display -> KeySym -> IO [KeyCode] +getKeyCodesForKeysym dpy keysym = do + let (minCode, maxCode) = displayKeycodes dpy + allCodes = [fromIntegral minCode .. fromIntegral maxCode] + + syms <- forM allCodes $ \code -> keycodeToKeysym dpy code 0 + let keysymMap' = Map.fromListWith (++) (zip syms [[code] | code <- allCodes]) + + -- keycodeToKeysym returns noSymbol for all unbound keycodes, and we don't + -- want to grab those whenever someone accidentally uses def :: KeySym + let keysymMap = Map.delete noSymbol keysymMap' + let keysymToKeycodes sym = Map.findWithDefault [] keysym keysymMap + + return $ keysymToKeycodes keysym + + +doGrab :: Display -> Window -> (KeyMask, KeySym) -> X () +doGrab dpy win (keyMask, keysym) = do + let grab kc m = io $ grabKey dpy kc m win True grabModeAsync grabModeAsync + (minCode, maxCode) = displayKeycodes dpy + allCodes = [fromIntegral minCode .. fromIntegral maxCode] + + -- build a map from keysyms to lists of keysyms (doing what + -- XGetKeyboardMapping would do if the X11 package bound it) + syms <- forM allCodes $ \code -> io (keycodeToKeysym dpy code 0) + let keysymMap' = Map.fromListWith (++) (zip syms [[code] | code <- allCodes]) + -- keycodeToKeysym returns noSymbol for all unbound keycodes, and we don't + -- want to grab those whenever someone accidentally uses def :: KeySym + let keysymMap = Map.delete noSymbol keysymMap' + let keysymToKeycodes sym = Map.findWithDefault [] keysym keysymMap + + forM_ (keysymToKeycodes keysym) $ \kc -> + mapM_ (grab kc . (keyMask .|.)) =<< extraModifiers + +disableKey :: (KeyMask, KeySym) -> WindowHook +disableKey key = remapKey key (return ()) + +remapKey :: (KeyMask, KeySym) -> X () -> WindowHook +remapKey keyFrom action = do + window <- ask + Query $ lift $ do + XConf { display = disp, theRoot = rootw } <- ask + doGrab disp window keyFrom + + XS.modify $ \(RemapState (NoPersist keyMap)) -> RemapState $ NoPersist $ + Map.insert (window, keyFrom) action keyMap + +rebindKey :: (KeyMask, KeySym) -> (KeyMask, KeySym) -> WindowHook +rebindKey keyFrom keyTo = do + window <- ask + remapKey keyFrom $ do + XConf { display = disp, theRoot = rootw } <- ask + codes <- io $ getKeyCodesForKeysym disp (snd keyTo) + case codes of + (keyCode:_) -> do + io $ allocaXEvent $ \xEv -> do + setEventType xEv keyPress + setKeyEvent xEv window rootw none (fst keyTo) keyCode True + sendEvent disp window True keyPressMask xEv + setEventType xEv keyRelease + sendEvent disp window True keyPressMask xEv + + _ -> return () diff --git a/src/Internal/Keys.hs b/src/Internal/Keys.hs index 1118788..c40c346 100644 --- a/src/Internal/Keys.hs +++ b/src/Internal/Keys.hs @@ -1,6 +1,7 @@ {-# LANGUAGE RankNTypes #-} module Internal.Keys (applyKeys) where +import Internal.Intercept (setIntercept) import Control.Monad.Loops (iterateWhile) import Control.Monad.Fix (fix) import Graphics.X11.ExtraTypes.XF86; @@ -246,6 +247,12 @@ keymap = runKeys $ do then mediaPrev else mediaNext + bind xK_t $ + + (justMod -|- noMod) $ + setIntercept (modMask config, xK_i) $ + logs $ "Intercepted!" + bind xK_r $ do justMod runDMenu shiftMod $ sendMessage DoRotate @@ -470,4 +477,7 @@ click = do modifyWindowBorder :: Integer -> SpacingModifier modifyWindowBorder i = ModifyWindowBorder $ \(Border a b c d) -> - Border (a + i) (b + i) (c + i) (d + i) + Border (clip $ a + i) (clip $ b + i) (clip $ c + i) (clip $ d + i) + + where clip i | i < 0 = 0 + clip i | otherwise = i -- cgit From a7129b68fb7fa4f7cea52513fad7223dcbba9801 Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Fri, 25 Mar 2022 15:55:54 -0600 Subject: Another Chrome binding added. There's a bug where it seems the mappings are applied inconsistently after a restart. Not sure what causes that. Seems bouncing XMonad again fixes it. --- src/Internal/Intercept.hs | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) (limited to 'src/Internal') diff --git a/src/Internal/Intercept.hs b/src/Internal/Intercept.hs index 987733b..c99ce09 100644 --- a/src/Internal/Intercept.hs +++ b/src/Internal/Intercept.hs @@ -1,3 +1,7 @@ + +-- Module for intercepting key presses not explicity mapped in the key bindings. +-- This uses some deep magic with grabKey and windows and everything else, but +-- it makes window-specific key bindings awesome! module Internal.Intercept where import XMonad @@ -111,19 +115,10 @@ getKeyCodesForKeysym dpy keysym = do doGrab :: Display -> Window -> (KeyMask, KeySym) -> X () doGrab dpy win (keyMask, keysym) = do let grab kc m = io $ grabKey dpy kc m win True grabModeAsync grabModeAsync - (minCode, maxCode) = displayKeycodes dpy - allCodes = [fromIntegral minCode .. fromIntegral maxCode] - -- build a map from keysyms to lists of keysyms (doing what - -- XGetKeyboardMapping would do if the X11 package bound it) - syms <- forM allCodes $ \code -> io (keycodeToKeysym dpy code 0) - let keysymMap' = Map.fromListWith (++) (zip syms [[code] | code <- allCodes]) - -- keycodeToKeysym returns noSymbol for all unbound keycodes, and we don't - -- want to grab those whenever someone accidentally uses def :: KeySym - let keysymMap = Map.delete noSymbol keysymMap' - let keysymToKeycodes sym = Map.findWithDefault [] keysym keysymMap + codes <- io $ getKeyCodesForKeysym dpy keysym - forM_ (keysymToKeycodes keysym) $ \kc -> + forM_ codes $ \kc -> mapM_ (grab kc . (keyMask .|.)) =<< extraModifiers disableKey :: (KeyMask, KeySym) -> WindowHook -- cgit