xmonad-config/lib/XMonad/Internal/Command/Desktop.hs

263 lines
7.5 KiB
Haskell
Raw Normal View History

2020-03-28 18:38:38 -04:00
--------------------------------------------------------------------------------
-- | General commands
2020-04-01 20:17:47 -04:00
module XMonad.Internal.Command.Desktop
( myTerm
, runTerm
2021-06-17 01:17:59 -04:00
, runTMux
2020-04-01 20:17:47 -04:00
, runCalc
, runBrowser
, runEditor
, runFileManager
, runTogglePlay
, runPrevTrack
, runNextTrack
, runStopPlay
, runVolumeDown
, runVolumeUp
, runVolumeMute
, runToggleBluetooth
2021-11-11 00:11:15 -05:00
-- , runToggleDPMS
2020-05-28 23:17:17 -04:00
, runToggleEthernet
2020-04-01 20:17:47 -04:00
, runRestart
, runRecompile
, runAreaCapture
, runScreenCapture
, runDesktopCapture
, runCaptureBrowser
2021-02-04 21:46:04 -05:00
, runStartISyncTimer
, runStartISyncService
2021-10-24 13:30:30 -04:00
, runNotificationClose
, runNotificationCloseAll
, runNotificationHistory
, runNotificationContext
2020-04-01 20:17:47 -04:00
) where
2021-11-11 00:11:15 -05:00
import Control.Monad (void)
2020-04-01 20:17:47 -04:00
import System.Directory
( createDirectoryIfMissing
, getHomeDirectory
)
import System.Environment
import System.FilePath
2020-03-28 18:38:38 -04:00
import XMonad.Actions.Volume
2021-11-11 00:11:15 -05:00
import XMonad.Core hiding (spawn)
-- import XMonad.Internal.DBus.Screensaver
import XMonad.Internal.Dependency
2020-04-01 20:17:47 -04:00
import XMonad.Internal.Notify
import XMonad.Internal.Process
2020-03-28 18:38:38 -04:00
import XMonad.Operations
--------------------------------------------------------------------------------
2021-06-19 00:17:47 -04:00
-- | My Executables
2020-03-28 18:38:38 -04:00
2020-04-01 20:17:47 -04:00
myTerm :: String
myTerm = "urxvt"
2021-06-19 00:17:47 -04:00
myBrowser :: String
myBrowser = "brave-accel"
myEditor :: String
myEditor = "emacsclient"
myMultimediaCtl :: String
myMultimediaCtl = "playerctl"
myBluetooth :: String
myBluetooth = "bluetoothctl"
myCapture :: String
myCapture = "flameshot"
myImageBrowser :: String
myImageBrowser = "feh"
2021-10-24 13:30:30 -04:00
myNotificationCtrl :: String
myNotificationCtrl = "dunstctl"
2021-06-19 00:17:47 -04:00
--------------------------------------------------------------------------------
-- | Misc constants
volumeChangeSound :: FilePath
volumeChangeSound = "smb_fireball.wav"
ethernetIface :: String
ethernetIface = "enp7s0f1"
2020-03-28 18:38:38 -04:00
2021-06-19 00:17:47 -04:00
--------------------------------------------------------------------------------
-- | Some nice apps
runTerm :: IO MaybeX
runTerm = spawnIfInstalled myTerm
runTMux :: IO MaybeX
2021-11-11 00:11:15 -05:00
runTMux = evalFeature $ Feature
{ ftrAction = cmd
, ftrSilent = False
, ftrChildren = [exe myTerm, exe "tmux", exe "bash"]
}
2021-06-17 01:17:59 -04:00
where
2021-06-19 00:17:47 -04:00
cmd = spawn
$ "tmux has-session"
#!&& fmtCmd myTerm ["-e", "bash", "-c", singleQuote c]
#!|| fmtNotifyCmd defNoteError { body = Just $ Text msg }
2021-06-17 01:17:59 -04:00
c = "exec tmux attach-session -d"
msg = "could not connect to tmux session"
2021-06-19 00:17:47 -04:00
runCalc :: IO MaybeX
runCalc = runIfInstalled [exe myTerm, exe "R"] $ spawnCmd myTerm ["-e", "R"]
2020-03-28 18:38:38 -04:00
2021-06-19 00:17:47 -04:00
runBrowser :: IO MaybeX
runBrowser = spawnIfInstalled myBrowser
2020-03-28 18:38:38 -04:00
2021-06-19 00:17:47 -04:00
runEditor :: IO MaybeX
runEditor = spawnCmdIfInstalled myEditor
2021-06-17 01:17:59 -04:00
["-c", "-e", doubleQuote "(select-frame-set-input-focus (selected-frame))"]
2020-03-28 18:38:38 -04:00
2021-06-19 00:17:47 -04:00
runFileManager :: IO MaybeX
runFileManager = spawnIfInstalled "pcmanfm"
2020-03-28 18:38:38 -04:00
--------------------------------------------------------------------------------
-- | Multimedia Commands
2021-06-19 00:17:47 -04:00
runMultimediaIfInstalled :: String -> IO MaybeX
runMultimediaIfInstalled cmd = spawnCmdIfInstalled myMultimediaCtl [cmd]
2020-03-28 18:38:38 -04:00
2021-06-19 00:17:47 -04:00
runTogglePlay :: IO MaybeX
runTogglePlay = runMultimediaIfInstalled "play-pause"
2020-03-28 18:38:38 -04:00
2021-06-19 00:17:47 -04:00
runPrevTrack :: IO MaybeX
runPrevTrack = runMultimediaIfInstalled "previous"
2020-03-28 18:38:38 -04:00
2021-06-19 00:17:47 -04:00
runNextTrack :: IO MaybeX
runNextTrack = runMultimediaIfInstalled "next"
2020-03-28 18:38:38 -04:00
2021-06-19 00:17:47 -04:00
runStopPlay :: IO MaybeX
runStopPlay = runMultimediaIfInstalled "stop"
2020-03-28 18:38:38 -04:00
2021-06-19 00:17:47 -04:00
runVolumeDown :: IO MaybeX
runVolumeDown = spawnSound volumeChangeSound (return ()) $ void (lowerVolume 2)
2020-05-31 20:56:57 -04:00
2021-06-19 00:17:47 -04:00
runVolumeUp :: IO MaybeX
runVolumeUp = spawnSound volumeChangeSound (return ()) $ void (raiseVolume 2)
2020-03-28 18:38:38 -04:00
2021-06-19 00:17:47 -04:00
runVolumeMute :: IO MaybeX
runVolumeMute = spawnSound volumeChangeSound (void toggleMute) $ return ()
2020-03-28 18:38:38 -04:00
2021-10-24 13:30:30 -04:00
--------------------------------------------------------------------------------
-- | Notification control
runNotificationCmd :: String -> IO MaybeX
runNotificationCmd cmd = spawnCmdIfInstalled myNotificationCtrl [cmd]
runNotificationClose :: IO MaybeX
runNotificationClose = runNotificationCmd "close"
runNotificationCloseAll :: IO MaybeX
runNotificationCloseAll = runNotificationCmd "close-all"
runNotificationHistory :: IO MaybeX
runNotificationHistory = runNotificationCmd "history-pop"
runNotificationContext :: IO MaybeX
runNotificationContext = runNotificationCmd "context"
2020-03-28 18:38:38 -04:00
--------------------------------------------------------------------------------
-- | System commands
2021-06-19 00:17:47 -04:00
runToggleBluetooth :: IO MaybeX
runToggleBluetooth = runIfInstalled [exe myBluetooth] $ spawn
2021-06-19 00:17:47 -04:00
$ myBluetooth ++ " show | grep -q \"Powered: no\""
2020-03-28 18:38:38 -04:00
#!&& "a=on"
#!|| "a=off"
2021-06-19 00:17:47 -04:00
#!>> fmtCmd myBluetooth ["power", "$a", ">", "/dev/null"]
2020-03-28 18:38:38 -04:00
#!&& fmtNotifyCmd defNoteInfo { body = Just $ Text "bluetooth powered $a" }
2021-11-11 00:11:15 -05:00
-- runToggleDPMS :: IO MaybeX
-- runToggleDPMS = io <$> evalFeature callToggle
2020-03-28 18:38:38 -04:00
2021-06-19 00:17:47 -04:00
runToggleEthernet :: IO MaybeX
runToggleEthernet = runIfInstalled [exe "nmcli"] $ spawn
2020-05-28 23:17:17 -04:00
$ "nmcli -g GENERAL.STATE device show " ++ ethernetIface ++ " | grep -q disconnected"
#!&& "a=connect"
#!|| "a=disconnect"
#!>> fmtCmd "nmcli" ["device", "$a", ethernetIface]
#!&& fmtNotifyCmd defNoteInfo { body = Just $ Text "ethernet \"$a\"ed" }
runStartISyncTimer :: IO MaybeX
runStartISyncTimer = runIfInstalled [userUnit "mbsync.timer"]
$ spawn
2021-02-04 21:46:04 -05:00
$ "systemctl --user start mbsync.timer"
#!&& fmtNotifyCmd defNoteInfo { body = Just $ Text "Isync timer started" }
#!|| fmtNotifyCmd defNoteError { body = Just $ Text "Isync timer failed to start" }
2021-02-04 21:46:04 -05:00
runStartISyncService :: IO MaybeX
runStartISyncService = runIfInstalled [userUnit "mbsync.service"]
$ spawn
2021-02-04 21:46:04 -05:00
$ "systemctl --user start mbsync.service"
#!&& fmtNotifyCmd defNoteInfo { body = Just $ Text "Isync completed" }
#!|| fmtNotifyCmd defNoteError { body = Just $ Text "Isync failed" }
2021-02-04 21:46:04 -05:00
2020-03-28 18:38:38 -04:00
--------------------------------------------------------------------------------
-- | Configuration commands
runRestart :: X ()
runRestart = restart "xmonad" True
runRecompile :: X ()
runRecompile = do
-- assume that the conf directory contains a valid stack project
confDir <- getXMonadDir
spawnAt confDir $ fmtCmd "stack" ["install"]
#!&& fmtNotifyCmd defNoteInfo { body = Just $ Text "compilation succeeded" }
#!|| fmtNotifyCmd defNoteError { body = Just $ Text "compilation failed" }
-- runRecompile :: X ()
-- runRecompile = do
-- -- assume that the conf directory contains a valid stack project
-- -- TODO this is hacky AF
-- confDir <- getXMonadDir
-- spawnCmdAt confDir "stack" ["install"]
2020-04-01 20:17:47 -04:00
--------------------------------------------------------------------------------
-- | Screen capture commands
getCaptureDir :: IO FilePath
getCaptureDir = do
e <- lookupEnv "XDG_DATA_HOME"
parent <- case e of
Nothing -> fallback
Just path
| isRelative path -> fallback
| otherwise -> return path
let fullpath = parent </> "screenshots"
createDirectoryIfMissing True fullpath
return fullpath
where
fallback = (</> ".local/share") <$> getHomeDirectory
2020-04-01 20:17:47 -04:00
2021-06-19 00:17:47 -04:00
runFlameshot :: String -> IO MaybeX
runFlameshot mode = runIfInstalled [exe myCapture] $ do
ssDir <- io getCaptureDir
2021-06-19 00:17:47 -04:00
spawnCmd myCapture $ mode : ["-p", ssDir]
2020-04-01 20:17:47 -04:00
-- TODO this will steal focus from the current window (and puts it
-- in the root window?) ...need to fix
2021-06-19 00:17:47 -04:00
runAreaCapture :: IO MaybeX
2020-04-01 20:17:47 -04:00
runAreaCapture = runFlameshot "gui"
-- myWindowCap = "screencap -w" --external script
2021-06-19 00:17:47 -04:00
runDesktopCapture :: IO MaybeX
2020-04-01 20:17:47 -04:00
runDesktopCapture = runFlameshot "full"
2021-06-19 00:17:47 -04:00
runScreenCapture :: IO MaybeX
runScreenCapture = runFlameshot "screen"
2021-06-19 00:54:01 -04:00
2021-06-19 00:17:47 -04:00
runCaptureBrowser :: IO MaybeX
runCaptureBrowser = runIfInstalled [exe myImageBrowser] $ do
dir <- io getCaptureDir
2021-06-19 00:17:47 -04:00
spawnCmd myImageBrowser [dir]