dots-of-war/xmonad/.xmonad/lib/Config.hs

782 lines
32 KiB
Haskell
Raw Normal View History

2020-06-21 10:41:26 +00:00
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE MultiParamTypeClasses, DeriveDataTypeable, TypeSynonymInstances, FlexibleInstances, FlexibleContexts, ScopedTypeVariables, LambdaCase #-}
2021-03-16 15:14:21 +00:00
{-# OPTIONS_GHC -fno-warn-missing-signatures -fno-warn-unused-binds -fno-warn-Wno-unused-top-binds #-}
2020-06-21 10:41:26 +00:00
-- Imports -------------------------------------------------------- {{{
module Config (main) where
import qualified Data.Map.Strict as M
import Control.Concurrent
import Control.Exception ( catch , SomeException)
2021-08-23 14:39:58 +00:00
import Control.Monad (join, filterM , when)
2020-06-21 10:41:26 +00:00
import Control.Arrow ( (>>>) )
2021-08-23 16:11:56 +00:00
import Data.List ( isPrefixOf, isInfixOf, find )
2020-11-14 22:32:12 +00:00
import qualified Data.List
2020-06-21 10:41:26 +00:00
import System.Exit (exitSuccess)
import qualified Rofi
import qualified TiledDragging
2020-08-05 12:55:11 +00:00
--import qualified WindowSwallowing
2020-06-21 10:41:26 +00:00
2020-08-05 12:55:11 +00:00
import XMonad.Hooks.WindowSwallowing as WindowSwallowing
2020-06-21 10:41:26 +00:00
2020-09-20 08:29:51 +00:00
2021-08-23 16:11:56 +00:00
import qualified XMonad.Util.Hacks as Hacks
2020-11-18 16:46:53 +00:00
--import XMonad.Util.ActionCycle
2020-06-21 10:41:26 +00:00
import Data.Foldable ( for_ )
import Data.Function ((&))
import XMonad hiding ((|||))
import XMonad.Actions.CopyWindow
import XMonad.Actions.SpawnOn
import XMonad.Hooks.DynamicLog
import XMonad.Hooks.ManageDocks
import XMonad.Hooks.SetWMName (setWMName)
import XMonad.Layout.LayoutCombinators ((|||))
import XMonad.Layout.LayoutHints
import XMonad.Layout.MouseResizableTile
import XMonad.Layout.NoBorders
import XMonad.Layout.Renamed (renamed, Rename(Replace))
import XMonad.Layout.ResizableTile
import XMonad.Layout.Simplest
import XMonad.Layout.Reflect
2021-08-23 16:11:56 +00:00
import XMonad.Layout.Spacing (spacingRaw, Border(..), incScreenWindowSpacing, decScreenWindowSpacing)
2020-06-21 10:41:26 +00:00
import XMonad.Layout.SubLayouts
import XMonad.Layout.Tabbed
import XMonad.Layout.WindowNavigation ( windowNavigation )
import XMonad.Layout.ZoomRow
import XMonad.Layout.ThreeColumns
import XMonad.Layout.ResizableThreeColumns
import XMonad.Layout.WindowSwitcherDecoration
import XMonad.Layout.DraggingVisualizer
import XMonad.Util.EZConfig ( additionalKeysP
, removeKeysP
, checkKeymap
)
2020-12-25 10:53:37 +00:00
2020-06-21 10:41:26 +00:00
import XMonad.Util.Run
import XMonad.Util.SpawnOnce (spawnOnce)
2021-08-23 16:11:56 +00:00
import XMonad.Util.WorkspaceCompare ( getSortByIndex)
2020-06-21 10:41:26 +00:00
import Data.Monoid ( Endo )
import Data.Semigroup ( All(..) )
import qualified System.IO as SysIO
import qualified XMonad.Actions.Navigation2D as Nav2d
import qualified XMonad.Config.Desktop as Desktop
import qualified XMonad.Hooks.EwmhDesktops as Ewmh
import qualified XMonad.Hooks.ManageHelpers as ManageHelpers
2020-08-30 17:00:09 +00:00
import XMonad.Hooks.DebugStack ( debugStackFullString
2020-06-21 10:41:26 +00:00
)
import qualified XMonad.Layout.BoringWindows as BoringWindows
import qualified XMonad.Layout.MultiToggle as MTog
import qualified XMonad.Layout.MultiToggle.Instances as MTog
import qualified XMonad.Layout.ToggleLayouts as ToggleLayouts
import qualified XMonad.StackSet as W
import qualified XMonad.Util.XSelection as XSel
import qualified XMonad.Layout.PerScreen as PerScreen
2020-11-14 22:32:12 +00:00
import Data.Maybe (catMaybes, maybeToList, fromMaybe)
import Data.Bifunctor
2020-11-18 16:46:53 +00:00
import GHC.IO.Unsafe (unsafePerformIO)
2020-12-25 16:48:15 +00:00
import XMonad.Layout.LayoutModifier
2021-01-02 16:33:37 +00:00
import qualified IndependentScreens as IS
2021-08-23 16:11:56 +00:00
import qualified XMonad.Actions.Sift as Sift
2020-06-21 10:41:26 +00:00
{-# ANN module "HLint: ignore Redundant $" #-}
{-# ANN module "HLint: ignore Redundant bracket" #-}
{-# ANN module "HLint: ignore Move brackets to avoid $" #-}
{-# ANN module "HLint: ignore Unused LANGUAGE pragma" #-}
-- }}}
-- Values -------------------- {{{
myModMask = mod4Mask
2020-08-16 21:05:10 +00:00
myLauncher = Rofi.asCommand def ["-show run"]
2021-05-14 09:24:19 +00:00
myTerminal = "alacritty"
screenIndexMain = 0
screenIndexVert = 1
screenIndexTop = 2
2020-06-21 10:41:26 +00:00
{-| adds the scripts-directory path to the filename of a script |-}
scriptFile :: String -> String
scriptFile script = "/home/leon/scripts/" ++ script
2020-11-14 22:32:12 +00:00
-- }}}
2020-06-21 10:41:26 +00:00
-- Colors ------ {{{
gray = "#888974"
red = "#fb4934"
purple = "#d3869b"
aqua = "#8ec07c"
-- }}}
-- Layout ---------------------------------------- {{{
2020-08-11 11:18:46 +00:00
myTabTheme :: Theme
2020-06-21 10:41:26 +00:00
myTabTheme = def -- defaultThemeWithButtons
2021-08-23 16:11:56 +00:00
{ activeColor = "#282828"
2020-09-20 08:29:51 +00:00
, inactiveColor = "#1d2021" --inactiveColor = "#282828"
2020-06-21 10:41:26 +00:00
, activeBorderColor = "#1d2021"
, inactiveBorderColor = "#282828"
, activeTextColor = "#fbf1c7"
, inactiveTextColor = "#fbf1c7"
2021-08-23 16:11:56 +00:00
, decoHeight = 20
2020-09-20 08:29:51 +00:00
, activeBorderWidth = 0
, inactiveBorderWidth = 0
, urgentBorderWidth = 0
2020-06-21 10:41:26 +00:00
, fontName = "-misc-cozettevector-*-*-*-*-10-*-*-*-*-*-*-*"
}
data EmptyShrinker = EmptyShrinker deriving (Show, Read)
instance Shrinker EmptyShrinker where
shrinkIt _ _ = [] :: [String]
2020-08-18 22:25:19 +00:00
2021-08-23 14:39:58 +00:00
myLayout = noBorders
2020-12-25 16:48:15 +00:00
. avoidStruts
. smartBorders
. MTog.mkToggle1 MTog.FULL
. ToggleLayouts.toggleLayouts tabbedTall
. MTog.mkToggle1 WINDOWDECORATION
. draggingVisualizer
. layoutHintsToCenter
2020-06-21 10:41:26 +00:00
$ layouts
where
-- | if the screen is wider than 1900px it's horizontal, so use horizontal layouts.
-- if it's not, it's vertical, so use layouts for vertical screens.
2022-01-13 10:05:31 +00:00
layouts = PerScreen.ifWider 1900
(PerScreen.ifWider 3000 chonkyScreenLayouts (MTog.mkToggle1 ONLYONTOPHALF horizScreenLayouts))
vertScreenLayouts
2020-06-21 10:41:26 +00:00
2021-08-23 16:11:56 +00:00
chonkyScreenLayouts = (rn "UltraTall" $ withGaps $ centeredIfSingle 0.6 resizableThreeCol)
||| horizScreenLayouts
2020-12-25 16:48:15 +00:00
2021-08-23 14:39:58 +00:00
horizScreenLayouts =
(rn "Tall" $ withGaps $ centeredIfSingle 0.7 mouseResizableTile {draggerType = BordersDragger})
||| (rn "Horizon" $ withGaps $ mouseResizableTileMirrored {draggerType = BordersDragger})
2020-12-25 16:48:15 +00:00
||| (rn "ThreeCol" $ mkTabbed $ withGaps $ resizableThreeCol)
||| (rn "TabbedRow" $ mkTabbed $ withGaps $ zoomRow)
-- ||| (rn "Colm" $ mkTabbed $ withGaps $ centeredIfSingle 0.7 (multiCol [1] 2 0.05))
2020-06-21 10:41:26 +00:00
vertScreenLayouts =
2020-12-25 16:48:15 +00:00
((rn "ThreeCol" $ mkTabbed $ withGaps $ Mirror $ reflectHoriz $ ThreeColMid 1 (3/100) (1/2))
||| (rn "Horizon" $ withGaps $ mouseResizableTileMirrored {draggerType = BordersDragger}))
2020-06-21 10:41:26 +00:00
2020-12-25 16:48:15 +00:00
-- | Simple tall layout with tab support
tabbedTall = rn "Tabbed" . mkTabbed . withGaps $ ResizableTall 1 (3/100) (1/2) []
-- | Specific instance of ResizableThreeCol
resizableThreeCol = ResizableThreeColMid 1 (3/100) (2/5) []
2020-08-18 22:25:19 +00:00
2020-12-25 16:48:15 +00:00
rn n = renamed [Replace n]
2020-08-18 22:25:19 +00:00
2020-12-25 16:48:15 +00:00
withGaps = spacingRaw False border True border True
where gap = 15
border = Border gap gap gap gap
2020-06-21 10:41:26 +00:00
-- | transform a layout into supporting tabs
2020-12-25 16:48:15 +00:00
mkTabbed layout = BoringWindows.boringWindows . windowNavigation . addTabs shrinkText myTabTheme $ subLayout [] Simplest $ layout
-- LayoutModifier and layout definitions ---------- {{{
2020-06-21 10:41:26 +00:00
-- | window decoration layout modifier. this needs you to add `dragginVisualizer` yourself
data WINDOWDECORATION = WINDOWDECORATION deriving (Read, Show, Eq, Typeable)
instance MTog.Transformer WINDOWDECORATION Window where
transform WINDOWDECORATION layout k = k (windowSwitcherDecoration EmptyShrinker myTabTheme layout) (const layout)
2020-06-21 10:41:26 +00:00
2020-09-20 08:29:51 +00:00
2020-12-25 16:48:15 +00:00
-- | Layout Modifier that places a window in the center of the screen,
-- leaving room on the left and right, if there is only a single window
newtype CenteredIfSingle a = CenteredIfSingle Double deriving (Show, Read)
instance LayoutModifier CenteredIfSingle Window where
pureModifier (CenteredIfSingle ratio) r _ [(onlyWindow, _)] = ([(onlyWindow, rectangleCenterPiece ratio r)], Nothing)
pureModifier _ _ _ winRects = (winRects, Nothing)
-- | Layout Modifier that places a window in the center of the screen,
-- leaving room on the left and right, if there is only a single window
centeredIfSingle :: Double -> l a -> ModifiedLayout CenteredIfSingle l a
centeredIfSingle ratio = ModifiedLayout (CenteredIfSingle ratio)
-- | Give the center piece of a rectangle by taking the given percentage
-- of the rectangle and taking that in the middle.
rectangleCenterPiece :: Double -> Rectangle -> Rectangle
rectangleCenterPiece ratio (Rectangle rx ry rw rh) = Rectangle start ry width rh
2021-08-23 14:39:58 +00:00
where
2020-12-25 16:48:15 +00:00
sides = floor $ ((fi rw) * (1.0 - ratio)) / 2
start = (fi rx) + sides
width = fi $ (fi rw) - (sides * 2)
-- Layout modifier that restricts windows to only display on the top part of the monitor
-- I use this because sometimes I have my bottom monitor obscure part of the top monitor,
-- thus having windows only be placed on the visible part of the screen is a good thing.
data ONLYONTOPHALF = ONLYONTOPHALF deriving (Read, Show, Eq, Typeable)
instance MTog.Transformer ONLYONTOPHALF Window where
2022-01-13 10:05:31 +00:00
transform ONLYONTOPHALF layout k = k (onlyOnTopHalf 0.62 layout) (const layout)
2022-01-13 10:05:31 +00:00
newtype OnlyOnTopHalf a = OnlyOnTopHalf Double deriving (Show, Read)
instance LayoutModifier OnlyOnTopHalf Window where
pureModifier (OnlyOnTopHalf ratio) _screenRect _ wins = (fmap (second (rectangleTopHalf ratio)) wins, Nothing)
onlyOnTopHalf :: Double -> l a -> ModifiedLayout OnlyOnTopHalf l a
onlyOnTopHalf ratio = ModifiedLayout (OnlyOnTopHalf ratio)
-- | Calculate the top part of a rectangle by a given ratio
rectangleTopHalf :: Double -> Rectangle -> Rectangle
rectangleTopHalf ratio (Rectangle rx ry rw rh) = Rectangle rx (floor $ (fi ry) * ratio) rw (floor $ (fi rh) * ratio)
2020-12-25 16:48:15 +00:00
fi :: (Integral a, Num b) => a -> b
fi = fromIntegral
-- }}}
2020-09-20 08:29:51 +00:00
2020-06-21 10:41:26 +00:00
-- }}}
-- Startuphook ----------------------------- {{{
myStartupHook :: X ()
myStartupHook = do
setWMName "LG3D" -- Java stuff hack
--spawnOnce "nm-applet &"
2021-05-23 13:16:47 +00:00
spawnOnce "udiskie -s &" -- Mount USB sticks automatically. -s is smart systray mode: systray icon if something is mounted
2020-06-21 10:41:26 +00:00
spawnOnce "xfce4-clipman &"
2020-12-25 10:53:37 +00:00
--spawnOnce "redshift -P -O 5000 &"
2020-06-21 10:41:26 +00:00
spawn "xset r rate 300 50 &" -- make key repeat quicker
2021-06-19 14:56:00 +00:00
--spawn "setxkbmap de nodeadkeys"
spawn "setxkbmap us -option compose:ralt"
2021-05-21 17:29:37 +00:00
spawn "arbtt-capture"
spawn "/home/leon/.screenlayout/dualscreen.sh"
--spawn "/home/leon/.screenlayout/tripplescreen-fixed.sh"
2020-08-24 13:57:25 +00:00
spawnOnce "xsetroot -cursor_name left_ptr"
spawnOnce "xrdb -merge ~/.Xresources"
2020-06-21 10:41:26 +00:00
io $ threadDelay $ 1000 * 100
2020-11-21 12:06:55 +00:00
--spawnOnce "/home/leon/Downloads/picom --config /home/leon/.config/picom.conf --experimental-backends --backend xrender" --no-fading-openclose"
2021-08-21 09:58:20 +00:00
spawnOnce "/home/leon/Downloads/picom-epic-animations --config /home/leon/.config/picom.conf --experimental-backends --animations --animation-stiffness 200 --animation-dampening 20" --backend glx" --no-fading-openclose"
2021-08-12 11:12:10 +00:00
--spawn "/home/leon/.config/polybar/launch.sh"
setupPolybarOn "HDMI-A-0"
2021-08-12 11:12:10 +00:00
spawnOnce "eww -c /home/leon/.config/eww-bar open-many bar_2 bar_1 &"
2021-08-11 10:43:34 +00:00
spawn "xsetroot -cursor_name left_ptr"
2020-06-21 10:41:26 +00:00
spawnOnce "nitrogen --restore"
spawnOnce "mailnag"
2021-03-16 15:14:21 +00:00
spawn "flashfocus"
2021-05-06 15:14:36 +00:00
spawnOnce "dunst"
2022-04-20 08:54:40 +00:00
-- This supposedly fixes slow startup of gtk applications???
spawn "dbus-update-activation-environment --systemd DBUS_SESSION_BUS_ADDRESS DISPLAY XAUTHORITY"
2020-06-21 10:41:26 +00:00
for_ ["led1", "led2"] $ \led -> safeSpawn "sudo" ["liquidctl", "set", led, "color", "fixed", "00ffff"]
2021-08-23 16:11:56 +00:00
setGtkFrameExtents
setGtkFrameExtents :: X ()
setGtkFrameExtents = withDisplay $ \dpy -> do
r <- asks theRoot
a <- getAtom "_NET_SUPPORTED"
c <- getAtom "ATOM"
f <- getAtom "_GTK_FRAME_EXTENTS"
io $ do
sup <- (join . maybeToList) <$> getWindowProperty32 dpy a r
when (fromIntegral f `notElem` sup) $ do
changeProperty32 dpy r a c propModeAppend [fromIntegral f]
2020-06-21 10:41:26 +00:00
-- }}}
-- Keymap --------------------------------------- {{{
myMouseBindings :: XConfig Layout -> M.Map (KeyMask, Button) (Window -> X ())
myMouseBindings (XConfig {XMonad.modMask = modMask'}) = M.fromList
[((modMask' .|. shiftMask, button1), TiledDragging.tiledDrag)]
-- Default mappings that need to be removed
removedKeys :: [String]
2021-08-23 16:11:56 +00:00
removedKeys = ["M-<Tab>", "M-S-c", "M-S-q", "M-h", "M-l", "M-j", "M-k", "M-S-<Return>", "M-S-j", "M-S-k"]
++ [key ++ show n | key <- ["M-", "M-S-", "M-C-"], n <- [1..9 :: Int]]
2020-06-21 10:41:26 +00:00
myKeys :: [(String, X ())]
myKeys = concat [ zoomRowBindings, tabbedBindings, multiMonitorBindings, programLaunchBindings, miscBindings, windowControlBindings, workspaceBindings ]
where
keyDirPairs = [("h", L), ("j", D), ("k", U), ("l", R)]
zoomRowBindings :: [(String, X ())]
zoomRowBindings =
[ ("M-+", sendMessage zoomIn)
, ("M--", sendMessage zoomOut)
, ("M-#", sendMessage zoomReset)
]
tabbedBindings :: [(String, X ())]
tabbedBindings =
[ ("M-j", ifLayoutName ("Tabbed" `isPrefixOf`) (BoringWindows.focusDown) (windows W.focusDown))
, ("M-k", ifLayoutName ("Tabbed" `isPrefixOf`) (BoringWindows.focusUp) (windows W.focusUp))
2021-08-23 16:11:56 +00:00
, ("M-S-j", windows Sift.siftDown)
, ("M-S-k", windows Sift.siftUp)
2020-06-21 10:41:26 +00:00
, ("M-C-S-h", sendMessage $ pullGroup L)
, ("M-C-S-j", sendMessage $ pullGroup D)
, ("M-C-S-k", sendMessage $ pullGroup U)
, ("M-C-S-l", sendMessage $ pullGroup R)
, ("M-S-C-m", withFocused (sendMessage . MergeAll))
, ("M-S-C-<Backspace>", withFocused (sendMessage . UnMerge))
, ("M-<Tab>", onGroup W.focusDown')
, ("M-C-<Tab>", onGroup W.focusUp')
, ("M-S-t", toggleTabbedLayout)
-- In tabbed mode, while focussing master pane, cycle tabs on the first slave
2021-08-23 16:11:56 +00:00
, ("M-S-<Tab>", windows W.focusMaster >> BoringWindows.focusDown >> onGroup W.focusDown' >> windows W.focusMaster)
2020-06-21 10:41:26 +00:00
]
multiMonitorBindings :: [(String, X ())]
multiMonitorBindings =
2022-09-12 08:43:24 +00:00
[ ("M-a", windows $ IS.focusScreen screenIndexVert)
, ("M-s", windows $ IS.focusScreen screenIndexTop)
, ("M-d", windows $ IS.focusScreen screenIndexMain)
, ("M-S-a", windows $ IS.withWspOnScreen screenIndexVert (\wsp -> W.view wsp . W.shift wsp))
, ("M-S-s", windows $ IS.withWspOnScreen screenIndexTop (\wsp -> W.view wsp . W.shift wsp))
, ("M-S-d", windows $ IS.withWspOnScreen screenIndexMain (\wsp -> W.view wsp . W.shift wsp))
2020-06-21 10:41:26 +00:00
, ("M-C-s", windows swapScreenContents)
]
programLaunchBindings :: [(String, X ())]
programLaunchBindings =
2021-08-23 16:11:56 +00:00
[ ("M-p", spawn myLauncher)
, ("M-S-p", Rofi.showNormal def "drun")
, ("M-S-e", Rofi.showNormal (def { Rofi.fuzzy = False }) "emoji")
, ("M-S-o", spawn $ scriptFile "rofi-open.sh")
, ("M-e", Rofi.promptRunCommand def specialCommands)
, ("M-o", Rofi.promptRunCommand def withSelectionCommands)
2021-04-23 08:18:51 +00:00
, ("M-b", safeSpawnProg "google-chrome-stable")
2020-11-21 12:06:55 +00:00
, ("M-S-<Return>", spawn myTerminal)
2021-08-23 16:11:56 +00:00
, ("M-C-S-s", spawn "flameshot gui")
, ("M-z", spawn $ scriptFile "copy-pasta.sh")
2020-11-18 16:46:53 +00:00
, ("M-S-h", fuckshit)
2020-06-21 10:41:26 +00:00
]
miscBindings :: [(String, X ())]
miscBindings =
[ ("M-f", do withFocused (windows . W.sink)
sendMessage (MTog.Toggle MTog.FULL)
sendMessage ToggleStruts)
, ("M-C-S-w", sendMessage $ MTog.Toggle WINDOWDECORATION)
, ("M-S-C-c", kill1)
, ("M-S-C-q", io exitSuccess)
2020-12-25 10:53:37 +00:00
-- useless binding simply to not accidentally quit firefox
, ("C-q", pure ())
2020-06-21 10:41:26 +00:00
-- Media
2021-08-14 09:06:01 +00:00
, ("<XF86AudioRaiseVolume>", spawn "amixer sset Master 5%+")
, ("<XF86AudioLowerVolume>", spawn "amixer sset Master 5%-")
2020-06-21 10:41:26 +00:00
, ("M-S-C-,", (notify "hi" (show $ map (\(a, _) -> show a) workspaceBindings)) >> (notify "ho" (show removedKeys)))
, ("M-<Backspace>", spawn "flash_window")
, ("M-g", incScreenWindowSpacing 5)
, ("M-S-g", decScreenWindowSpacing 5)
]
workspaceBindings :: [(String, X ())]
2021-08-23 16:11:56 +00:00
workspaceBindings = concat $
[ [ ("M-" ++ show wspNum, runActionOnWorkspace W.view wspNum)
, ("M-S-" ++ show wspNum, runActionOnWorkspace W.shift wspNum)
, ("M-C-" ++ show wspNum, runActionOnWorkspace copy wspNum)
]
| wspNum <- [1..9 :: Int]
]
2020-06-21 10:41:26 +00:00
where
2021-01-02 16:33:37 +00:00
runActionOnWorkspace :: (IS.VirtualWorkspace -> WindowSet -> WindowSet) -> Int -> X ()
2020-06-21 10:41:26 +00:00
runActionOnWorkspace action wspNum = do
2021-01-02 16:33:37 +00:00
desiredWsp <- IS.nthWorkspace (wspNum - 1)
case desiredWsp of
Just wsp -> windows $ IS.onCurrentScreen action wsp
Nothing -> pure ()
2021-08-23 14:39:58 +00:00
2020-06-21 10:41:26 +00:00
windowControlBindings :: [(String, X ())]
windowControlBindings = windowGoMappings ++ windowSwapMappings ++ resizeMappings
where
windowGoMappings = [ ("M-M1-" ++ key, Nav2d.windowGo dir False) | (key, dir) <- keyDirPairs ]
windowSwapMappings = [ ("M-S-M1-" ++ key, Nav2d.windowSwap dir False) | (key, dir) <- keyDirPairs ]
resizeMappings =
2021-08-23 16:11:56 +00:00
[ ("M-C-h", ifLayoutIs "Horizon" (sendMessage ShrinkSlave) (sendMessage Shrink))
, ("M-C-j", ifLayoutIs "Horizon" (sendMessage Expand) (sendMessage MirrorShrink >> sendMessage ExpandSlave))
, ("M-C-k", ifLayoutIs "Horizon" (sendMessage Shrink) (sendMessage MirrorExpand >> sendMessage ShrinkSlave))
, ("M-C-l", ifLayoutIs "Horizon" (sendMessage ExpandSlave) (sendMessage Expand))
2020-06-21 10:41:26 +00:00
]
-- | toggle tabbed Tall layout, merging all non-master windows
-- into a single tab group when initializing the tabbed layout.
toggleTabbedLayout :: X ()
toggleTabbedLayout = do
sendMessage $ ToggleLayouts.Toggle "Tabbed"
ifLayoutIs "Tabbed" (do BoringWindows.focusMaster
withFocused (sendMessage . MergeAll)
withFocused (sendMessage . UnMerge)
-- refresh the tabs, so they draw correctly
windows W.focusUp
windows W.focusDown)
(return ())
-- | launch a program by starting an instance in a hidden workspace,
-- and just raising an already running instance. This allows for super quick "startup" time.
-- For this to work, the window needs to have the `_NET_WM_PID` set and unique!
launchWithBackgroundInstance :: (Query Bool) -> String -> X ()
launchWithBackgroundInstance windowQuery commandToRun = withWindowSet $ \winSet -> do
fittingHiddenWindows <- (W.allWindows winSet) |> filter (\win -> Just "NSP" == W.findTag win winSet)
|> filterM (runQuery windowQuery)
case fittingHiddenWindows of
[] -> do spawnHere commandToRun
spawnOn "NSP" commandToRun
[winId] -> do windows $ W.shiftWin (W.currentTag winSet) winId
spawnOn "NSP" commandToRun
(winId:_) -> windows $ W.shiftWin (W.currentTag winSet) winId
swapScreenContents :: W.StackSet i l a sid sd -> W.StackSet i l a sid sd
swapScreenContents ws = if null (W.visible ws) then ws else
let
otherScreen = head $ W.visible ws
otherWsp = W.workspace otherScreen
currentScreen = W.current ws
currentWsp = W.workspace currentScreen
in
ws
{ W.current = currentScreen { W.workspace = otherWsp { W.tag = W.tag currentWsp } }
, W.visible = (otherScreen { W.workspace = currentWsp { W.tag = W.tag otherWsp } } : (tail $ W.visible ws))
}
2021-03-16 15:14:21 +00:00
swapCurrentWspContentsWith :: Eq i => i -> W.StackSet i l a sid sd -> W.StackSet i l a sid sd
2021-08-23 14:39:58 +00:00
swapCurrentWspContentsWith other ws =
2021-03-16 15:14:21 +00:00
case find ((other ==) . W.tag) $ W.workspaces ws of
Just otherWsp -> W.mapWorkspace (swapWith otherWsp) ws
Nothing -> ws
2021-08-23 14:39:58 +00:00
where
2021-03-16 15:14:21 +00:00
currentWsp = W.workspace $ W.current ws
2021-08-23 14:39:58 +00:00
swapWith otherWsp w
2021-03-16 15:14:21 +00:00
| W.tag w == other = currentWsp { W.tag = W.tag otherWsp }
| W.tag w == W.tag currentWsp = otherWsp { W.tag = W.tag currentWsp }
| otherwise = w
2020-06-21 10:41:26 +00:00
withSelectionCommands :: [(String, X ())]
withSelectionCommands =
2021-08-23 16:11:56 +00:00
[ ("Google", XSel.transformPromptSelection ("https://google.com/search?q=" ++) "google-chrome-stable")
, ("Hoogle", XSel.transformPromptSelection ("https://hoogle.haskell.org/?hoogle=" ++) "google-chrome-stable")
2020-06-21 10:41:26 +00:00
, ("Translate", XSel.getSelection >>= translateMenu)
]
translateMenu :: String -> X ()
translateMenu input = do
selectedLanguage <- Rofi.promptSimple def ["de", "en", "fr"]
translated <- runProcessWithInput "trans" [":" ++ selectedLanguage, input, "--no-ansi"] ""
notify "Translation" translated
specialCommands :: [(String, X ())]
specialCommands =
[ ("screenshot", spawn $ scriptFile "screenshot.sh")
, ("screenshot to file", spawn $ scriptFile "screenshot.sh --tofile")
, ("screenshot full to file", spawn $ scriptFile "screenshot.sh --tofile --fullscreen")
, ("screenvideo to file", spawn (scriptFile "screenvideo.sh") >> notify "video" "stop video-recording with M-S-C-g")
, ("screengif to file", spawn (scriptFile "screengif.sh") >> notify "gif" "stop gif-recording with M-S-C-g")
, ("Copy to all workspaces", windows copyToAll)
, ("Kill all other copies", killAllOtherCopies)
, ("get debug data", debugStackFullString >>= (\str -> safeSpawn "xmessage" [str]))
, ("get full stackset", withWindowSet (\ws -> spawn $ "echo '" ++ show (W.floating ws) ++ "\n" ++ show (W.current ws) ++ "' | xclip -in -selection clipboard"))
, ("toggle top screen halving", sendMessage $ MTog.Toggle ONLYONTOPHALF)
2020-06-21 10:41:26 +00:00
]
-- }}}
-- ManageHook -------------------------------{{{
myManageHook :: Query (Data.Monoid.Endo WindowSet)
myManageHook = composeAll
[ resource =? "Dialog" --> ManageHelpers.doCenterFloat
, appName =? "pavucontrol" --> ManageHelpers.doCenterFloat
, className =? "Myxer" --> ManageHelpers.doCenterFloat
2020-07-02 13:40:06 +00:00
--, className =? "mpv" --> ManageHelpers.doRectFloat (W.RationalRect 0.9 0.9 0.1 0.1)
2020-06-21 10:41:26 +00:00
, title =? "Something" --> doFloat
, className =? "termite_floating" --> ManageHelpers.doRectFloat(W.RationalRect 0.2 0.2 0.6 0.6)
, className =? "bar_system_status_indicator" --> ManageHelpers.doRectFloat (W.RationalRect 0.7 0.05 0.29 0.26)
2020-11-14 22:32:12 +00:00
, title =? "discord.com hat Ihren Bildschirm freigegeben" --> doShift "NSP"
2020-06-21 10:41:26 +00:00
, manageDocks
]
-- }}}
-- Main ------------------------------------ {{{
2021-08-23 14:39:58 +00:00
2021-08-12 11:12:10 +00:00
2020-06-21 10:41:26 +00:00
main :: IO ()
main = do
2021-01-02 16:33:37 +00:00
currentScreenCount :: Int <- IS.countScreens
2020-11-14 22:32:12 +00:00
2021-08-12 11:12:10 +00:00
-- set up the fifo for polybar
safeSpawn "mkfifo" ["/tmp/xmonad-state-bar" ++ show (fi screenIndexVert)]
2020-06-21 10:41:26 +00:00
let myConfig = flip additionalKeysP myKeys
$ flip removeKeysP removedKeys
$ Desktop.desktopConfig
{ terminal = myTerminal
2021-08-23 16:11:56 +00:00
, workspaces = (IS.withScreens (fromIntegral currentScreenCount) (map show [1..6 :: Int])) ++ ["NSP"]
2020-06-21 10:41:26 +00:00
, modMask = myModMask
2020-08-30 17:00:09 +00:00
, borderWidth = 0
2020-06-21 10:41:26 +00:00
, layoutHook = myLayout
, logHook = mconcat [ polybarLogHook (fi screenIndexVert)
2021-08-12 11:12:10 +00:00
, ewwLogHook
2021-10-21 11:24:42 +00:00
--, Ewmh.ewmhDesktopsLogHook
2020-08-30 17:00:09 +00:00
, logHook Desktop.desktopConfig
--, fadeInactiveLogHook 0.95
, logHook def]
2021-10-21 11:24:42 +00:00
, startupHook = mconcat [ --Ewmh.ewmhDesktopsStartup
--,
myStartupHook, checkKeymap myConfig myKeys]
2020-06-21 10:41:26 +00:00
, manageHook = mconcat [ manageSpawn, myManageHook, manageHook def]
, focusedBorderColor = "#427b58"
, normalBorderColor = "#282828"
, handleEventHook = mconcat [ mySwallowEventHook
, activateWindowEventHook
, handleEventHook Desktop.desktopConfig
2021-08-23 16:11:56 +00:00
, Hacks.windowedFullscreenFixEventHook
2021-10-21 11:24:42 +00:00
--, Ewmh.ewmhDesktopsEventHook
2020-06-21 10:41:26 +00:00
]
--, handleEventHook = minimizeEventHook <+> handleEventHook def <+> hintsEventHook -- <+> Ewmh.fullscreenEventHook
, mouseBindings = myMouseBindings <+> mouseBindings def
}
xmonad
$ Nav2d.withNavigation2DConfig def { Nav2d.defaultTiledNavigation = Nav2d.sideNavigation }
2021-10-21 11:24:42 +00:00
$ Ewmh.setEwmhActivateHook myActivateManageHook
$ Ewmh.ewmh
2020-06-21 10:41:26 +00:00
$ docks
$ myConfig
2021-10-21 11:24:42 +00:00
2020-06-21 10:41:26 +00:00
-- }}}
2022-01-13 10:05:31 +00:00
-- | for a list of queries, finds the first one that
-- matches the currently focused window and executes that X action,
-- or a fallback if no window is active.
perQueryAction :: X () -> [(Query Bool, X ())] -> X ()
perQueryAction fallback options = withWindowSet $ \ws -> case W.peek ws of
Nothing -> fallback
Just w -> do
matching <- findM (\(q, _) -> runQuery q w) options
maybe fallback snd matching
findM :: Monad m => (a -> m Bool) -> [a] -> m (Maybe a)
findM _ [] = pure Nothing
findM f (x:xs) = do b <- f x; if b then pure $ Just x else findM f xs
fooElseBar :: X ()
fooElseBar = perQueryAction
(spawn "notify-send no window")
[ ((className =? "Alacritty"), spawn "notify-send kek hehe")
, ((className =? "Google-chrome"), spawn "notify-send chrome user")
]
2021-10-21 11:24:42 +00:00
myActivateManageHook :: ManageHook
myActivateManageHook = pure mempty
2020-11-18 16:46:53 +00:00
2020-07-10 13:16:46 +00:00
mySwallowEventHook = WindowSwallowing.swallowEventHook
2020-08-05 12:55:11 +00:00
(className =? "Alacritty" <||> className =? "Termite" <||> className =? "NOPE Thunar")
2022-01-14 10:23:05 +00:00
(not <$> foldl1 (<||>) [ className =* "eww"
, className =? "Dragon"
, className =? "okular"
2022-01-20 12:41:11 +00:00
, className =? "Zathura"
2022-01-14 10:23:05 +00:00
, className =? "noswallow"
])
2020-06-21 10:41:26 +00:00
2020-10-19 15:40:50 +00:00
(=*) :: Query String -> String -> Query Bool
query =* value = (value `isInfixOf`) <$> query
2020-06-21 10:41:26 +00:00
activateWindowEventHook :: Event -> X All
activateWindowEventHook (ClientMessageEvent { ev_message_type = messageType, ev_window = window }) = withWindowSet $ \ws -> do
activateWindowAtom <- getAtom "_NET_ACTIVE_WINDOW"
2020-06-25 18:57:56 +00:00
2020-06-21 10:41:26 +00:00
when (messageType == activateWindowAtom) $
2021-01-31 10:55:50 +00:00
if window `elem` (W.integrate' $ W.stack $ W.workspace $ W.current ws)
2020-06-21 10:41:26 +00:00
then windows (W.focusWindow window)
else do
shouldRaise <- runQuery (className =? "discord" <||> className =? "web.whatsapp.com") window
if shouldRaise
then windows (W.shiftWin (W.currentTag ws) window)
2021-10-21 11:24:42 +00:00
else windows (IS.focusWindow' window)
2020-06-21 10:41:26 +00:00
return $ All True
activateWindowEventHook _ = return $ All True
2021-01-31 10:55:50 +00:00
-- | Focus a window, switching workspace on the correct Xinerama screen if neccessary.
2021-10-21 11:24:42 +00:00
-- focusWindow' :: Window -> WindowSet -> WindowSet
-- focusWindow' window ws
-- | Just window == W.peek ws = ws
-- | otherwise = case W.findTag window ws of
-- Just tag -> IS.focusScreen (IS.unmarshallS tag) ws
-- Nothing -> ws
2020-06-21 10:41:26 +00:00
2021-04-23 08:18:51 +00:00
2021-08-12 11:12:10 +00:00
-- Bar Kram -------------------------------------- {{{
-- | Loghook for eww on all monitors. Runs the eww-bar
ewwLogHook :: X ()
ewwLogHook = spawn "/home/leon/.config/eww-bar/update-workspaces.sh"
-- | Launch a polybar on the given monitor name
setupPolybarOn :: String -> X ()
setupPolybarOn name =
spawnOnce $ "MONITOR=" ++ name ++ " TRAY_POSITION=right polybar -r --config=/home/leon/.config/polybar/config.ini main &"
2020-06-21 10:41:26 +00:00
-- | Loghook for polybar on a given monitor.
-- Will write the polybar formatted string to /tmp/xmonad-state-bar${monitor}
polybarLogHook :: Int -> X ()
polybarLogHook monitor = do
barOut <- dynamicLogString $ polybarPP $ fromIntegral monitor
io $ SysIO.appendFile ("/tmp/xmonad-state-bar" ++ show monitor) (barOut ++ "\n")
-- swapping namedScratchpadFilterOutWorkspacePP and marshallPP will throw "Prelude.read no Parse" errors..... wtf
-- | create a polybar Pretty printer, marshalled for given monitor.
polybarPP :: ScreenId -> PP
2021-08-23 16:11:56 +00:00
polybarPP monitor = filterOutWsPP ["NSP"] . (IS.marshallPP $ fromIntegral monitor) $ def
2020-06-21 10:41:26 +00:00
{ ppCurrent = withFG aqua . withMargin . withFont 5 . const "__active__"
, ppVisible = withFG aqua . withMargin . withFont 5 . const "__active__"
, ppUrgent = withFG red . withMargin . withFont 5 . const "__urgent__"
, ppHidden = withFG gray . withMargin . withFont 5 . (`wrapClickableWorkspace` "__hidden__")
, ppHiddenNoWindows = withFG gray . withMargin . withFont 5 . (`wrapClickableWorkspace` "__empty__")
, ppWsSep = ""
, ppSep = ""
, ppLayout = removeWords ["DraggingVisualizer", "WindowSwitcherDeco", "Minimize", "Hinted", "Spacing", "Tall"]
>>> \l -> if l == "Tall" || l == "Horizon" || l == "" then ""
else (withFG gray " | ") ++ (withFG purple $ withMargin l)
, ppExtras = []
, ppTitle = const "" -- withFG aqua . (shorten 40)
2021-08-23 16:11:56 +00:00
, ppSort = onlyRelevantWspsSorter
2020-06-21 10:41:26 +00:00
}
where
withMargin = wrap " " " "
removeWords wrds = unwords . filter (`notElem` wrds). words
withFont fNum = wrap ("%{T" ++ show (fNum :: Int) ++ "}") "%{T}"
withBG col = wrap ("%{B" ++ col ++ "}") "%{B-}"
withFG col = wrap ("%{F" ++ col ++ "}") "%{F-}"
wrapOnClickCmd command = wrap ("%{A1:" ++ command ++ ":}") "%{A}"
wrapClickableWorkspace wsp = wrapOnClickCmd ("xdotool key super+" ++ wsp)
onlyRelevantWspsSorter = do
sortByIndex <- getSortByIndex
visibleWorkspaceTags <- getVisibleWorkspacesTagsOnMonitor monitor
let isEmptyAndNotOpened wsp = (null $ W.stack wsp) && (W.tag wsp) `notElem` visibleWorkspaceTags
return $ dropEndWhile isEmptyAndNotOpened . sortByIndex
-- }}}
-- Utilities --------------------------------------------------- {{{
(|>) :: a -> (a -> b) -> b
(|>) = (&)
infixl 1 |>
dropEndWhile :: (a -> Bool) -> [a] -> [a]
dropEndWhile _ [] = []
dropEndWhile test xs = if test $ last xs then dropEndWhile test (init xs) else xs
catchAndNotifyAny :: IO () -> IO ()
catchAndNotifyAny ioAction = catch ioAction (\(e :: SomeException) -> notify "Xmonad exception" (show e))
2021-01-02 16:33:37 +00:00
getVisibleWorkspacesTagsOnMonitor :: ScreenId -> X [IS.VirtualWorkspace]
2020-06-21 10:41:26 +00:00
getVisibleWorkspacesTagsOnMonitor monitor = do
ws <- gets windowset
return $ W.current ws : W.visible ws
|> map (W.tag . W.workspace)
2021-01-02 16:33:37 +00:00
|> filter (\tag -> monitor == fromIntegral (IS.unmarshallS tag))
|> map IS.unmarshallW
2020-06-21 10:41:26 +00:00
notify :: MonadIO m => String -> String -> m ()
notify notificationTitle notificationMsg = safeSpawn "notify-send" [notificationTitle, notificationMsg]
ifLayoutIs :: String -> X a -> X a -> X a
ifLayoutIs layoutAName = ifLayoutName (== layoutAName)
ifLayoutName :: (String -> Bool) -> X a -> X a -> X a
ifLayoutName check onLayoutA onLayoutB = do
layout <- getActiveLayoutDescription
if (check layout) then onLayoutA else onLayoutB
2020-11-18 16:46:53 +00:00
-- | Get the name of the active layout.
2020-06-21 10:41:26 +00:00
getActiveLayoutDescription :: X String
getActiveLayoutDescription = (description . W.layout . W.workspace . W.current) <$> gets windowset
2020-06-21 10:41:26 +00:00
-- }}}
2020-11-14 22:32:12 +00:00
2020-11-18 16:46:53 +00:00
unsafeGetXrdbValue :: String -> String
unsafeGetXrdbValue = unsafePerformIO . getXrdbValue
2020-11-14 22:32:12 +00:00
getXrdbValue :: String -> IO String
getXrdbValue key = fromMaybe "" . findValue key <$> runProcessWithInput "xrdb" ["-query"] ""
where
findValue :: String -> String -> Maybe String
findValue xresKey xres =
snd <$> ( Data.List.find ((== xresKey) . fst)
$ catMaybes
$ splitAtColon
<$> lines xres
)
splitAtColon :: String -> Maybe (String, String)
splitAtColon str = splitAtTrimming str <$> (Data.List.elemIndex ':' str)
splitAtTrimming :: String -> Int -> (String, String)
2021-08-12 11:12:10 +00:00
splitAtTrimming str idx = bimap trim (trim . tail) $ splitAt idx str
2020-11-14 22:32:12 +00:00
2020-11-18 16:46:53 +00:00
fuckshit = getActiveLayoutDescription >>= debugShit
debugShit :: MonadIO m => String -> m ()
debugShit x = spawn $ "notify-send 'Debug' '" ++ x ++ "'"
2021-03-16 15:14:21 +00:00
lastLayout :: X ()
lastLayout = lastLayout' 123
2021-08-23 14:39:58 +00:00
where
2021-03-16 15:14:21 +00:00
lastLayout' :: Int -> X ()
lastLayout' 0 = pure ()
lastLayout' n = sendMessage NextLayout >> lastLayout' (n - 1)
2021-08-12 11:12:10 +00:00
(!?) :: [a] -> Int -> Maybe a
(!?) [] _ = Nothing
(!?) (x:_) 0 = Just x
2021-08-23 14:39:58 +00:00
(!?) (_:xs) n
2021-08-12 11:12:10 +00:00
| n > 0 = xs !? (n - 1)
| otherwise = Nothing