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

411 lines
12 KiB
Haskell
Raw Normal View History

2020-03-28 18:38:38 -04:00
--------------------------------------------------------------------------------
2022-12-30 14:58:23 -05:00
-- General commands
2020-03-28 18:38:38 -04:00
2020-04-01 20:17:47 -04:00
module XMonad.Internal.Command.Desktop
( myTerm
2022-07-03 01:11:32 -04:00
, playSound
-- commands
2020-04-01 20:17:47 -04:00
, 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
2024-03-17 11:15:03 -04:00
, runToggleNetworking
, runToggleWifi
2020-04-01 20:17:47 -04:00
, runRestart
, runRecompile
, runAreaCapture
, runScreenCapture
, runDesktopCapture
, runCaptureBrowser
2021-10-24 13:30:30 -04:00
, runNotificationClose
, runNotificationCloseAll
, runNotificationHistory
, runNotificationContext
2022-07-03 01:11:32 -04:00
-- daemons
, runNetAppDaemon
-- packages
, networkManagerPkgs
2022-12-30 14:58:23 -05:00
)
where
import DBus
import Data.Internal.DBus
2023-01-01 18:33:02 -05:00
import Data.Internal.XIO
2022-12-30 14:58:23 -05:00
import RIO
2022-12-31 19:47:02 -05:00
import RIO.Directory
2022-12-30 14:58:23 -05:00
import RIO.FilePath
import qualified RIO.Process as P
import qualified RIO.Text as T
import System.Posix.User
2022-12-31 19:16:44 -05:00
import UnliftIO.Environment
2022-12-30 14:58:23 -05:00
import XMonad.Actions.Volume
import XMonad.Core hiding (spawn)
import XMonad.Internal.DBus.Common
import XMonad.Internal.Notify
import XMonad.Internal.Shell as S
import XMonad.Operations
2020-03-28 18:38:38 -04:00
--------------------------------------------------------------------------------
2022-12-30 14:58:23 -05:00
-- My Executables
2020-03-28 18:38:38 -04:00
myTerm :: FilePath
2024-02-18 19:41:32 -05:00
myTerm = "alacritty"
2020-04-01 20:17:47 -04:00
myCalc :: FilePath
myCalc = "bc"
myBrowser :: FilePath
2024-07-07 10:28:24 -04:00
myBrowser = "firefox"
2021-06-19 00:17:47 -04:00
myEditor :: FilePath
2021-06-19 00:17:47 -04:00
myEditor = "emacsclient"
myEditorServer :: FilePath
myEditorServer = "emacs"
myMultimediaCtl :: FilePath
2021-06-19 00:17:47 -04:00
myMultimediaCtl = "playerctl"
myBluetooth :: FilePath
2021-06-19 00:17:47 -04:00
myBluetooth = "bluetoothctl"
myCapture :: FilePath
2021-06-19 00:17:47 -04:00
myCapture = "flameshot"
myImageBrowser :: FilePath
2021-06-19 00:17:47 -04:00
myImageBrowser = "feh"
myNotificationCtrl :: FilePath
2021-10-24 13:30:30 -04:00
myNotificationCtrl = "dunstctl"
--------------------------------------------------------------------------------
2022-12-30 14:58:23 -05:00
-- Packages
myTermPkgs :: [Fulfillment]
2022-12-30 14:58:23 -05:00
myTermPkgs =
2024-02-18 19:41:32 -05:00
[ Package Official "alacritty"
2022-12-30 14:58:23 -05:00
]
myEditorPkgs :: [Fulfillment]
myEditorPkgs = [Package Official "emacs-nativecomp"]
notifyPkgs :: [Fulfillment]
notifyPkgs = [Package Official "dunst"]
bluetoothPkgs :: [Fulfillment]
bluetoothPkgs = [Package Official "bluez-utils"]
networkManagerPkgs :: [Fulfillment]
networkManagerPkgs = [Package Official "networkmanager"]
2024-03-17 11:15:03 -04:00
nmcli :: IODependency_
nmcli = sysExe networkManagerPkgs "nmcli"
2021-06-19 00:17:47 -04:00
--------------------------------------------------------------------------------
2022-12-30 14:58:23 -05:00
-- Misc constants
2021-06-19 00:17:47 -04:00
volumeChangeSound :: FilePath
volumeChangeSound = "smb_fireball.wav"
--------------------------------------------------------------------------------
2022-12-30 14:58:23 -05:00
-- Some nice apps
2021-06-19 00:17:47 -04:00
2023-01-02 19:28:41 -05:00
runTerm :: MonadUnliftIO m => Sometimes (m ())
2024-02-18 19:41:32 -05:00
runTerm = sometimesExe "terminal" "alacritty" myTermPkgs True myTerm
2021-06-19 00:17:47 -04:00
2023-01-02 19:28:41 -05:00
runTMux :: MonadUnliftIO m => Sometimes (m ())
2022-07-02 17:09:21 -04:00
runTMux = sometimesIO_ "terminal multiplexer" "tmux" deps act
2021-06-17 01:17:59 -04:00
where
2022-12-30 14:58:23 -05:00
deps =
listToAnds (socketExists "tmux" [] socketName) $
fmap (sysExe myTermPkgs) [myTerm, "tmux", "bash"]
act =
S.spawn $
fmtCmd "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"
socketName = do
2022-12-31 19:04:37 -05:00
u <- liftIO getEffectiveUserID
t <- getTemporaryDirectory
return $ t </> "tmux-" ++ show u </> "default"
2023-01-02 19:28:41 -05:00
runCalc :: MonadUnliftIO m => Sometimes (m ())
2022-07-21 22:51:00 -04:00
runCalc = sometimesIO_ "calculator" "bc" deps act
where
deps = toAnd_ (sysExe myTermPkgs myTerm) (sysExe [Package Official "bc"] myCalc)
act = spawnCmd myTerm ["-e", T.pack myCalc, "-l"]
2020-03-28 18:38:38 -04:00
2023-01-02 19:28:41 -05:00
runBrowser :: MonadUnliftIO m => Sometimes (m ())
2022-12-30 14:58:23 -05:00
runBrowser =
sometimesExe
"web browser"
"brave"
[Package AUR "brave-bin"]
False
myBrowser
2020-03-28 18:38:38 -04:00
2023-01-02 19:28:41 -05:00
runEditor :: MonadUnliftIO m => Sometimes (m ())
runEditor = sometimesIO_ "text editor" "emacs" tree cmd
where
2022-12-30 14:58:23 -05:00
cmd =
spawnCmd
myEditor
["-c", "-e", doubleQuote "(select-frame-set-input-focus (selected-frame))"]
-- NOTE 1: we could test if the emacs socket exists, but it won't come up
-- before xmonad starts, so just check to see if the process has started
tree = toAnd_ (sysExe myEditorPkgs myEditor) $ process [] $ T.pack myEditorServer
2020-03-28 18:38:38 -04:00
2023-01-02 19:28:41 -05:00
runFileManager :: MonadUnliftIO m => Sometimes (m ())
2022-12-30 14:58:23 -05:00
runFileManager =
sometimesExe
"file browser"
"pcmanfm"
[Package Official "pcmanfm"]
True
"pcmanfm"
2020-03-28 18:38:38 -04:00
--------------------------------------------------------------------------------
2022-12-30 14:58:23 -05:00
-- Multimedia Commands
2020-03-28 18:38:38 -04:00
2023-01-02 19:28:41 -05:00
runMultimediaIfInstalled
:: MonadUnliftIO m
=> T.Text
-> T.Text
-> Sometimes (m ())
2022-12-30 14:58:23 -05:00
runMultimediaIfInstalled n cmd =
sometimesExeArgs
(T.append n " multimedia control")
"playerctl"
[Package Official "playerctl"]
True
myMultimediaCtl
[cmd]
2020-03-28 18:38:38 -04:00
2023-01-02 19:28:41 -05:00
runTogglePlay :: MonadUnliftIO m => Sometimes (m ())
runTogglePlay = runMultimediaIfInstalled "play/pause" "play-pause"
2020-03-28 18:38:38 -04:00
2023-01-02 19:28:41 -05:00
runPrevTrack :: MonadUnliftIO m => Sometimes (m ())
runPrevTrack = runMultimediaIfInstalled "previous track" "previous"
2020-03-28 18:38:38 -04:00
2023-01-02 19:28:41 -05:00
runNextTrack :: MonadUnliftIO m => Sometimes (m ())
runNextTrack = runMultimediaIfInstalled "next track" "next"
2020-03-28 18:38:38 -04:00
2023-01-02 19:28:41 -05:00
runStopPlay :: MonadUnliftIO m => Sometimes (m ())
runStopPlay = runMultimediaIfInstalled "stop playback" "stop"
2020-03-28 18:38:38 -04:00
2021-11-20 01:15:04 -05:00
--------------------------------------------------------------------------------
2022-12-30 14:58:23 -05:00
-- Volume Commands
2021-11-20 01:15:04 -05:00
soundDir :: FilePath
2024-01-07 09:52:44 -05:00
soundDir = "assets" </> "sound"
2021-11-20 01:15:04 -05:00
playSound :: MonadIO m => FilePath -> m ()
playSound file = do
2022-03-05 18:18:16 -05:00
-- manually look up directories to avoid the X monad
p <- io $ (</> soundDir </> file) . cfgDir <$> getDirectories
2021-11-20 01:15:04 -05:00
-- paplay seems to have less latency than aplay
spawnCmd "paplay" [T.pack p]
2020-05-31 20:56:57 -04:00
2023-01-02 19:28:41 -05:00
featureSound
:: MonadUnliftIO m
=> T.Text
-> FilePath
-> m ()
-> m ()
-> Sometimes (m ())
featureSound n file pre post =
2022-12-30 14:58:23 -05:00
sometimesIO_ (T.unwords ["volume", n, " control"]) "paplay" tree $
pre >> playSound file >> post
where
-- ASSUME pulseaudio pulls in libpulse as a dep; pulseaudio itself is needed
-- to play sound (duh) but libpulse is the package with the paplay binary
tree = Only_ $ sysExe [Package Official "pulseaudio"] "paplay"
2020-03-28 18:38:38 -04:00
2023-01-02 19:28:41 -05:00
runVolumeDown :: MonadUnliftIO m => Sometimes (m ())
runVolumeDown = featureSound "up" volumeChangeSound (return ()) $ void (lowerVolume 2)
2021-11-20 01:15:04 -05:00
2023-01-02 19:28:41 -05:00
runVolumeUp :: MonadUnliftIO m => Sometimes (m ())
runVolumeUp = featureSound "down" volumeChangeSound (return ()) $ void (raiseVolume 2)
2021-11-20 01:15:04 -05:00
2023-01-02 19:28:41 -05:00
runVolumeMute :: MonadUnliftIO m => Sometimes (m ())
runVolumeMute = featureSound "mute" volumeChangeSound (void toggleMute) $ return ()
2020-03-28 18:38:38 -04:00
2021-10-24 13:30:30 -04:00
--------------------------------------------------------------------------------
2022-12-30 14:58:23 -05:00
-- Notification control
2021-10-24 13:30:30 -04:00
2023-01-02 19:28:41 -05:00
runNotificationCmd
:: MonadUnliftIO m
=> T.Text
-> T.Text
2023-10-27 23:12:22 -04:00
-> Maybe NamedSesConnection
2023-01-02 19:28:41 -05:00
-> Sometimes (m ())
runNotificationCmd n arg cl =
sometimesDBus cl (T.unwords [n, "control"]) "dunstctl" tree cmd
2022-07-03 01:11:32 -04:00
where
cmd _ = spawnCmd myNotificationCtrl [arg]
2022-12-30 14:58:23 -05:00
tree =
toAnd_ (DBusIO $ sysExe notifyPkgs myNotificationCtrl) $
Endpoint [] notifyBus notifyPath (interfaceName_ "org.dunstproject.cmd0") $
Method_ $
memberName_ "NotificationAction"
2021-10-24 13:30:30 -04:00
2023-10-27 23:12:22 -04:00
runNotificationClose :: MonadUnliftIO m => Maybe NamedSesConnection -> Sometimes (m ())
runNotificationClose = runNotificationCmd "close notification" "close"
2021-10-24 13:30:30 -04:00
2023-10-27 23:12:22 -04:00
runNotificationCloseAll :: MonadUnliftIO m => Maybe NamedSesConnection -> Sometimes (m ())
runNotificationCloseAll =
runNotificationCmd "close all notifications" "close-all"
2021-10-24 13:30:30 -04:00
2023-10-27 23:12:22 -04:00
runNotificationHistory :: MonadUnliftIO m => Maybe NamedSesConnection -> Sometimes (m ())
runNotificationHistory =
runNotificationCmd "see notification history" "history-pop"
2021-10-24 13:30:30 -04:00
2023-10-27 23:12:22 -04:00
runNotificationContext :: MonadUnliftIO m => Maybe NamedSesConnection -> Sometimes (m ())
runNotificationContext =
runNotificationCmd "open notification context" "context"
2021-10-24 13:30:30 -04:00
2020-03-28 18:38:38 -04:00
--------------------------------------------------------------------------------
2022-12-30 14:58:23 -05:00
-- System commands
2020-03-28 18:38:38 -04:00
2024-03-30 17:58:24 -04:00
-- needed to lookup/prompt for passwords/keys for wifi connections and some VPNs
2023-10-27 23:12:22 -04:00
runNetAppDaemon :: Maybe NamedSysConnection -> Sometimes (XIO (P.Process () () ()))
2022-12-30 14:58:23 -05:00
runNetAppDaemon cl =
Sometimes
"network applet"
2024-03-30 17:58:24 -04:00
(\x -> xpfVPN x || xpfWireless x)
2022-12-30 14:58:23 -05:00
[Subfeature (DBusRoot_ cmd tree cl) "NM-applet"]
2022-07-03 01:11:32 -04:00
where
tree = toAnd_ app $ Bus networkManagerPkgs networkManagerBus
app = DBusIO $ sysExe [Package Official "network-manager-applet"] "nm-applet"
cmd _ = P.proc "nm-applet" [] (P.startProcess . P.setCreateGroup True)
2022-07-03 01:11:32 -04:00
2023-10-27 23:12:22 -04:00
runToggleBluetooth :: MonadUnliftIO m => Maybe NamedSysConnection -> Sometimes (m ())
2022-12-30 14:58:23 -05:00
runToggleBluetooth cl =
Sometimes
"bluetooth toggle"
xpfBluetooth
[Subfeature (DBusRoot_ cmd tree cl) "bluetoothctl"]
where
tree = And_ (Only_ $ DBusIO $ sysExe bluetoothPkgs myBluetooth) (Only_ $ Bus [] btBus)
2022-12-30 14:58:23 -05:00
cmd _ =
S.spawn $
fmtCmd myBluetooth ["show"]
#!| "grep -q \"Powered: no\""
#!&& "a=on"
#!|| "a=off"
#!>> fmtCmd myBluetooth ["power", "$a", ">", "/dev/null"]
#!&& fmtNotifyCmd defNoteInfo {body = Just $ Text "bluetooth powered $a"}
2020-03-28 18:38:38 -04:00
2024-03-17 11:15:03 -04:00
runToggleNetworking :: MonadUnliftIO m => Sometimes (m ())
runToggleNetworking =
2022-12-30 14:58:23 -05:00
Sometimes
2024-03-17 11:15:03 -04:00
"network toggle"
(\x -> xpfEthernet x || xpfWireless x)
2022-12-30 14:58:23 -05:00
[Subfeature root "nmcli"]
where
2024-03-17 11:15:03 -04:00
root = IORoot_ cmd $ Only_ nmcli
cmd =
S.spawn $
fmtCmd "nmcli" ["networking"]
#!| "grep -q enabled"
#!&& "a=off"
#!|| "a=on"
#!>> fmtCmd "nmcli" ["networking", "$a"]
#!&& fmtNotifyCmd defNoteInfo {body = Just $ Text "networking switched $a"}
runToggleWifi :: MonadUnliftIO m => Sometimes (m ())
runToggleWifi = Sometimes "wifi toggle" xpfWireless [Subfeature root "nmcli"]
where
root = IORoot_ cmd $ Only_ nmcli
cmd =
2022-12-30 14:58:23 -05:00
S.spawn $
2024-03-17 11:15:03 -04:00
fmtCmd "nmcli" ["radio", "wifi"]
#!| "grep -q enabled"
#!&& "a=off"
#!|| "a=on"
#!>> fmtCmd "nmcli" ["radio", "wifi", "$a"]
#!&& fmtNotifyCmd defNoteInfo {body = Just $ Text "wifi switched $a"}
2020-05-28 23:17:17 -04:00
2020-03-28 18:38:38 -04:00
--------------------------------------------------------------------------------
2022-12-30 14:58:23 -05:00
-- Configuration commands
2020-03-28 18:38:38 -04:00
runRestart :: X ()
runRestart = restart "xmonad" True
2023-01-02 19:28:41 -05:00
-- TODO use rio in here so I don't have to fill my xinit log with stack poop
2022-08-30 00:21:21 -04:00
-- TODO only recompile the VM binary if we have virtualbox enabled
2020-03-28 18:38:38 -04:00
runRecompile :: X ()
runRecompile = do
-- assume that the conf directory contains a valid stack project
2022-03-05 18:18:16 -05:00
confDir <- asks (cfgDir . directories)
2022-12-30 14:58:23 -05:00
spawn $
fmtCmd "cd" [T.pack confDir]
#!&& fmtCmd "stack" ["install"]
#!&& fmtNotifyCmd defNoteInfo {body = Just $ Text "compilation succeeded"}
#!|| fmtNotifyCmd defNoteError {body = Just $ Text "compilation failed"}
2020-04-01 20:17:47 -04:00
--------------------------------------------------------------------------------
2022-12-30 14:58:23 -05:00
-- Screen capture commands
2020-04-01 20:17:47 -04:00
2022-12-31 19:16:44 -05:00
getCaptureDir :: MonadIO m => m 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
2023-01-02 19:28:41 -05:00
runFlameshot
:: MonadUnliftIO m
=> T.Text
-> T.Text
2023-10-27 23:12:22 -04:00
-> Maybe NamedSesConnection
2023-01-02 19:28:41 -05:00
-> Sometimes (m ())
runFlameshot n mode cl = sometimesDBus cl n (T.pack myCapture) tree cmd
where
cmd _ = spawnCmd myCapture [mode]
2022-12-30 14:58:23 -05:00
tree =
toAnd_ (DBusIO $ sysExe [Package Official "flameshot"] myCapture) $
Bus [] $
busName_ "org.flameshot.Flameshot"
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
2023-10-27 23:12:22 -04:00
runAreaCapture :: MonadUnliftIO m => Maybe NamedSesConnection -> Sometimes (m ())
runAreaCapture = runFlameshot "screen area capture" "gui"
2020-04-01 20:17:47 -04:00
-- myWindowCap = "screencap -w" --external script
2023-10-27 23:12:22 -04:00
runDesktopCapture :: MonadUnliftIO m => Maybe NamedSesConnection -> Sometimes (m ())
runDesktopCapture = runFlameshot "fullscreen capture" "full"
2023-10-27 23:12:22 -04:00
runScreenCapture :: MonadUnliftIO m => Maybe NamedSesConnection -> Sometimes (m ())
runScreenCapture = runFlameshot "screen capture" "screen"
2021-06-19 00:54:01 -04:00
2023-01-02 19:28:41 -05:00
runCaptureBrowser :: MonadUnliftIO m => Sometimes (m ())
2022-12-30 14:58:23 -05:00
runCaptureBrowser = sometimesIO_
"screen capture browser"
"feh"
(Only_ $ sysExe [Package Official "feh"] myImageBrowser)
$ do
2022-12-31 19:47:02 -05:00
dir <- getCaptureDir
2022-12-30 14:58:23 -05:00
spawnCmd myImageBrowser [T.pack dir]