{-# LANGUAGE ImportQualifiedPost #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE PatternSynonyms #-} module Main (main) where import System.Exit (exitFailure) import XMonad.Hooks.EwmhDesktops (ewmh, ewmhFullscreen) import XMonad.Hooks.EwmhDesktops.Extra (ewmhExtra) import XMonad.Hooks.RefocusLast (refocusLastLayoutHook, toggleFocus) import Control.Exception (bracket) import Control.Monad (void, when) import Control.Monad.Extra (whenJustM) import Data.Aeson qualified import Data.Bits ((.&.)) import Data.ByteString.Char8 qualified import Data.Char (isDigit) import Data.Foldable (traverse_) import Data.List qualified import Data.Maybe qualified import Data.Monoid (All(All)) import Graphics.X11.ExtraTypes.XF86 import Graphics.X11.Xlib.Display.Extra (defaultDisplayName) import XMonad hiding (config, directories) import XMonad.Extra (isFloatingX, withAllFocused) import System.IO (hPutStrLn, stderr) import System.Environment (getArgs, getEnv, getEnvironment, lookupEnv) import System.Posix.Process (executeFile) import XMonad.Actions.DynamicWorkspaces (removeEmptyWorkspace) import XMonad.Actions.CycleWS (toggleWS) import XMonad.Layout.Gaps (Direction2D(U,R,D,L), gaps) import XMonad.Layout.FocusTracking (focusTracking) import XMonad.Layout.NoBorders (noBorders) import XMonad.Layout.ResizableTile (MirrorResize(MirrorExpand,MirrorShrink)) import XMonad.Layout.ResizableTile (ResizableTall(ResizableTall)) import XMonad.Util.ExtensibleState qualified as XS import qualified XMonad.StackSet as W import Data.Map (Map) import qualified Data.Map as Map import XMonad.Hooks.UrgencyHook ( BorderUrgencyHook(BorderUrgencyHook,urgencyBorderColor) , RemindWhen(Dont) , SuppressWhen(Never) , UrgencyConfig(UrgencyConfig,remindWhen,suppressWhen) , clearUrgents' , withUrgencyHookC ) import XMonad.Hooks.ManageDocks (avoidStruts, docks) import XMonad.Hooks.ManageHelpers (doCenterFloat, doRectFloat) import XMonad.ManageHook (doIgnore) import Data.Ratio import XMonad.Hooks.Place (placeHook, smart) import qualified System.Systemd.Daemon as Systemd import qualified Shortcuts import SuperToRiver qualified import Shutdown (shutdown, newShutdownEventHandler) import System.IO.Unsafe (unsafePerformIO) sliceEnvPath :: FilePath sliceEnvPath = unsafePerformIO (getEnv "XMONAD_CACHE_DIR") <> "/slice.env" main :: IO () main = getArgs >>= \case [] -> mainNoArgs ["--shutdown"] -> shutdown args -> hPutStrLn stderr ("bad arguments: " <> show args) >> exitFailure (=??) :: Query a -> (a -> Bool) -> Query Bool (=??) x p = fmap p x readEnv :: Data.Aeson.FromJSON b => String -> IO b readEnv name = readEnv' (error $ "could not get environment variable: " <> name) name readEnv' :: Data.Aeson.FromJSON b => b -> String -> IO b readEnv' defaultValue name = Data.Maybe.fromMaybe defaultValue . Data.Aeson.decodeStrict' . Data.ByteString.Char8.pack . Data.Maybe.fromMaybe mempty <$> lookupEnv name mainNoArgs :: IO () mainNoArgs = do getEnvironment >>= \env -> writeFile sliceEnvPath $ unlines [ k <> "=" <> v | (k, v) <- env] myMasterDelta <- readEnv' (1 / 20) "XMONAD_MASTER_DELTA" :: IO Rational myMasterWidth <- readEnv' (1 / 2) "XMONAD_MASTER_WIDTH" :: IO Rational myScreenGaps <- readEnv' [] "XMONAD_SCREEN_GAPS" :: IO [Int] myScreenWidth <- bracket (getEnv "DISPLAY" >>= openDisplay) closeDisplay (return . widthOfScreen . defaultScreenOfDisplay) shortcuts <- Shortcuts.new . Data.Maybe.fromMaybe mempty <$> (Data.Aeson.decodeFileStrict' =<< getEnv "XMONAD_SHORTCUTS") handleShutdownEvent <- newShutdownEventHandler config <- ewmhExtra $ ewmhFullscreen $ ewmh $ docks $ withUrgencyHookC BorderUrgencyHook { urgencyBorderColor = "#ff0000" } UrgencyConfig { remindWhen = Dont , suppressWhen = Never } $ def { clientMask = clientMask def .|. focusChangeMask , modMask = mod4Mask , keys = \c -> myKeys c `Map.union` SuperToRiver.bindings `Map.union` shortcuts c , layoutHook = avoidStruts $ refocusLastLayoutHook $ gaps (zip [U,R,D,L] myScreenGaps) $ ResizableTall 1 myMasterDelta (myMasterWidth + 2 * fromIntegral (borderWidth def) / fromIntegral myScreenWidth) [] ||| noBorders (focusTracking Full) , manageHook = composeAll [ appName =?? Data.List.isPrefixOf "pinentry" --> doCenterFloat , appName =?? Data.List.isInfixOf "Float" --> doCenterFloat , appName =?? Data.List.isInfixOf "Ignore" --> doIgnore , title =? "Upload to Imgur" --> doRectFloat (W.RationalRect 0 0 (1 % 8) (1 % 8)) , placeHook (smart (1,0)) ] , startupHook = do whenJustM (io (lookupEnv "XMONAD_STARTUP_HOOK")) (\path -> forkFile path [] Nothing) io (void Systemd.notifyReady) , normalBorderColor = "#1c1c1c" , focusedBorderColor = "#f000b0" , handleEventHook = handleFocusChangeEvent <> handleShutdownEvent , logHook = SuperToRiver.logHook } directories <- getDirectories launch config directories handleFocusChangeEvent :: Event -> X All handleFocusChangeEvent = \case FocusChangeEvent{ev_event_type,ev_window} | ev_event_type == focusIn -> do clearUrgents' [ev_window] return (All True) _ -> return (All True) forkFile :: FilePath -> [String] -> Maybe [(String, String)] -> X () forkFile path args env = void . xfork $ do environment <- getEnvironment executeFile path True args (env <> Just environment) forkFileInSlice :: String -> FilePath -> [String] -> X () forkFileInSlice sliceName path args = void . xfork $ executeFile {-pkg:systemd-}"systemd-run" True ( "--collect" : "--user" : "--slice=" <> sliceName : "--property=EnvironmentFile=" <> sliceEnvPath : path : args ) Nothing displayNumber :: Int displayNumber = read . takeWhile isDigit . dropWhile (==':') $ defaultDisplayName myKeys :: XConfig Layout -> Map (KeyMask, KeySym) (X ()) myKeys conf = Map.fromList $ [ ((_4 , xK_Escape ), forkFile {-notpkg-}"slock" [] Nothing) , ((0 , xF86XK_ScreenSaver ), forkFile {-notpkg-}"slock" [] Nothing) , ((_4S , xK_c ), kill) , ((_4 , xK_o ), forkFile {-pkg:fzmenu-}"otpmenu" [] Nothing) , ((_4C , xK_o ), forkFile {-pkg:fzmenu-}"otpmenu" ["--phase2-method=copy"] Nothing) , ((_4 , xK_p ), forkFile {-pkg:fzmenu-}"passmenu" [] Nothing) , ((_4C , xK_p ), forkFile {-pkg:fzmenu-}"passmenu" ["--phase2-method=copy"] Nothing) , ((_4 , xK_x ), forkFileInSlice "alacritty" {-pkg:alacritty-tv-}"alacritty" []) , ((_4C , xK_x ), forkFileInSlice "alacritty" {-pkg:alacritty-tv-}"alacritty" ["-e", {-pkg-}"su2", "user=root"] ) , ((0 , xK_Menu ), do focusedWindows <- withAllFocused forkFile {-pkg-}"desktop-pager" ("view" : map show focusedWindows) Nothing ) , ((_S , xK_Menu ), do focusedWindows <- withAllFocused forkFile {-pkg-}"desktop-pager" ("shift" : map show focusedWindows) Nothing ) , ((_CS , xK_Menu ) , do focusedWindows <- withAllFocused forkFile {-pkg-}"desktop-pager" ("shiftview" : map show focusedWindows) Nothing ) , ((_4 , xK_Menu ), toggleWS) , ((_M , xK_Menu ), toggleWS) , ((_4 , xK_e ), toggleWS) , ((_4 , xK_Delete ), removeEmptyWorkspace) , ((0 , xK_Super_R ), do focusedWindows <- withAllFocused forkFile {-pkg-}"desktop-pager" ("view" : map show focusedWindows) Nothing ) , ((_S , xK_Super_R ), do focusedWindows <- withAllFocused forkFile {-pkg-}"desktop-pager" ("shift" : map show focusedWindows) Nothing ) , ((_CS , xK_Super_R ) , do focusedWindows <- withAllFocused forkFile {-pkg-}"desktop-pager" ("shiftview" : map show focusedWindows) Nothing ) , ((_C , xK_Super_R ), toggleWS) , ((_M , xK_Super_R ), toggleWS) , ((_4 , xK_space ), withFocused $ \w -> ifM (isFloatingX w) xdeny $ sendMessage NextLayout) , ((_4M , xK_space ), withFocused $ \w -> ifM (isFloatingX w) xdeny $ resetLayout) , ((_4 , xK_l ), toggleFocus) , ((_4 , xK_h ), windows W.focusMaster) , ((_4 , xK_j ), windows W.focusDown) , ((_4 , xK_k ), windows W.focusUp) , ((_4S , xK_h ), windows W.swapMaster) , ((_4S , xK_j ), windows W.swapDown) , ((_4S , xK_k ), windows W.swapUp) , ((_4M , xK_h ), sendMessage Shrink) , ((_4M , xK_l ), sendMessage Expand) , ((_4M , xK_j ), sendMessage MirrorShrink) , ((_4M , xK_k ), sendMessage MirrorExpand) , ((_4 , xK_t ), withFocused $ windows . W.sink) , ((_4 , xK_comma ), sendMessage $ IncMasterN 1) , ((_4 , xK_period ), sendMessage $ IncMasterN (-1)) , ((0, xF86XK_MyComputer), forkFile {-notpkg-}"slock" [] Nothing) , ((0, xF86XK_AudioMute), audioMute) , ((0, xF86XK_AudioMicMute), audioMicMute) , ((_4, xF86XK_AudioMute), pwvucontrol []) , ((0, xK_Print), forkFile {-pkg:flameshot-once.profiles.tv-}"flameshot-once" [] Nothing) , ((_C, xF86XK_Forward), forkFile {-pkg:xdpytools-}"xdpychvt" ["next"] Nothing) , ((_C, xF86XK_Back), forkFile {-pkg:xdpytools-}"xdpychvt" ["prev"] Nothing) ] where _4 = mod4Mask _C = controlMask _CM = _C .|. _M _CS = _C .|. _S _S = shiftMask _M = mod1Mask _4C = _4 .|. _C _4S = _4 .|. _S _4M = _4 .|. _M _4CM = _4 .|. _C .|. _M _4SM = _4 .|. _S .|. _M amixer args = forkFile {-pkg:alsa-utils-}"amixer" args Nothing pwvucontrol args = forkFile {-pkg-}"pwvucontrol" args Nothing audioLowerVolume = amixer ["-q", "sset", "Master", "5%-"] audioRaiseVolume = amixer ["-q", "sset", "Master", "5%+"] audioMute = amixer ["-q", "sset", "Master", "toggle"] audioMicMute = amixer ["-q", "sset", "Capture", "toggle"] resetLayout = setLayout $ XMonad.layoutHook conf xdeny = forkFile {-pkg-}"xterm" [ "-geometry", "300x100" , "-name", "AlertFloat" , "-bg", "#E4002B" , "-e", "sleep", "0.05" ] Nothing