2020-04-01 20:17:47 -04:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Commands for controlling power
|
|
|
|
|
|
|
|
module XMonad.Internal.Command.Power
|
2020-03-28 18:38:38 -04:00
|
|
|
( runHibernate
|
|
|
|
, runOptimusPrompt
|
|
|
|
, runPowerOff
|
|
|
|
, runPowerPrompt
|
|
|
|
, runReboot
|
|
|
|
, runScreenLock
|
|
|
|
, runSuspend
|
|
|
|
, runSuspendPrompt
|
|
|
|
, runQuitPrompt
|
2021-06-23 20:47:41 -04:00
|
|
|
, hasBattery
|
2022-06-26 20:07:25 -04:00
|
|
|
|
|
|
|
|
|
|
|
, powerPrompt
|
2020-04-01 20:17:47 -04:00
|
|
|
) where
|
2020-03-28 18:38:38 -04:00
|
|
|
|
|
|
|
import Control.Arrow (first)
|
|
|
|
|
2021-06-23 20:47:41 -04:00
|
|
|
import Data.Either
|
2020-03-28 18:38:38 -04:00
|
|
|
import qualified Data.Map as M
|
|
|
|
|
|
|
|
import Graphics.X11.Types
|
|
|
|
|
2020-04-01 20:17:47 -04:00
|
|
|
import System.Directory
|
|
|
|
import System.Exit
|
2021-06-23 20:47:41 -04:00
|
|
|
import System.FilePath.Posix
|
|
|
|
import System.IO.Error
|
2020-04-01 20:17:47 -04:00
|
|
|
|
2020-03-28 18:38:38 -04:00
|
|
|
import XMonad.Core
|
2021-11-07 13:35:08 -05:00
|
|
|
import XMonad.Internal.Dependency
|
2021-11-20 01:15:04 -05:00
|
|
|
import XMonad.Internal.Shell
|
2020-04-01 20:17:47 -04:00
|
|
|
import qualified XMonad.Internal.Theme as T
|
2020-03-28 18:38:38 -04:00
|
|
|
import XMonad.Prompt
|
|
|
|
import XMonad.Prompt.ConfirmPrompt
|
|
|
|
|
2021-06-20 01:01:36 -04:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Executables
|
|
|
|
|
|
|
|
myScreenlock :: String
|
|
|
|
myScreenlock = "screenlock"
|
|
|
|
|
|
|
|
myOptimusManager :: String
|
|
|
|
myOptimusManager = "optimus-manager"
|
|
|
|
|
2020-03-28 18:38:38 -04:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Core commands
|
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
runScreenLock :: SometimesX
|
2022-06-28 23:27:55 -04:00
|
|
|
runScreenLock = sometimesExe "screen locker" "i3lock script" True myScreenlock
|
2020-03-28 18:38:38 -04:00
|
|
|
|
|
|
|
runPowerOff :: X ()
|
|
|
|
runPowerOff = spawn "systemctl poweroff"
|
|
|
|
|
|
|
|
runSuspend :: X ()
|
|
|
|
runSuspend = spawn "systemctl suspend"
|
|
|
|
|
|
|
|
runHibernate :: X ()
|
|
|
|
runHibernate = spawn "systemctl hibernate"
|
|
|
|
|
|
|
|
runReboot :: X ()
|
|
|
|
runReboot = spawn "systemctl reboot"
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Confirm prompt wrappers
|
|
|
|
|
2021-11-20 01:15:04 -05:00
|
|
|
-- TODO doesn't this need to also lock the screen?
|
2020-03-28 18:38:38 -04:00
|
|
|
runSuspendPrompt :: X ()
|
|
|
|
runSuspendPrompt = confirmPrompt T.promptTheme "suspend?" runSuspend
|
|
|
|
|
|
|
|
runQuitPrompt :: X ()
|
|
|
|
runQuitPrompt = confirmPrompt T.promptTheme "quit?" $ io exitSuccess
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Nvidia Optimus
|
|
|
|
|
|
|
|
-- TODO for some reason the screen never wakes up after suspend when
|
|
|
|
-- the nvidia card is up, so block suspend if nvidia card is running
|
|
|
|
-- and warn user
|
|
|
|
isUsingNvidia :: IO Bool
|
|
|
|
isUsingNvidia = doesDirectoryExist "/sys/module/nvidia"
|
|
|
|
|
2021-11-19 00:35:54 -05:00
|
|
|
hasBattery :: IO (Maybe String)
|
2021-06-23 20:47:41 -04:00
|
|
|
hasBattery = do
|
|
|
|
ps <- fromRight [] <$> tryIOError (listDirectory syspath)
|
|
|
|
ts <- mapM readType ps
|
2021-11-19 00:35:54 -05:00
|
|
|
return $ if "Battery\n" `elem` ts then Nothing else Just "battery not found"
|
2021-06-23 20:47:41 -04:00
|
|
|
where
|
|
|
|
readType p = fromRight [] <$> tryIOError (readFile $ syspath </> p </> "type")
|
|
|
|
syspath = "/sys/class/power_supply"
|
2021-06-20 01:01:36 -04:00
|
|
|
|
|
|
|
runOptimusPrompt' :: X ()
|
|
|
|
runOptimusPrompt' = do
|
2020-03-28 18:38:38 -04:00
|
|
|
nvidiaOn <- io isUsingNvidia
|
2021-06-20 01:01:36 -04:00
|
|
|
switch $ if nvidiaOn then "integrated" else "nvidia"
|
2020-03-28 18:38:38 -04:00
|
|
|
where
|
|
|
|
switch mode = confirmPrompt T.promptTheme (prompt mode) (cmd mode)
|
|
|
|
prompt mode = "gpu switch to " ++ mode ++ "?"
|
2020-07-03 22:16:15 -04:00
|
|
|
cmd mode = spawn $
|
2021-06-20 01:01:36 -04:00
|
|
|
"prime-offload"
|
|
|
|
#!&& unwords [myOptimusManager, "--switch", mode, "--no-confirm"]
|
2020-07-03 22:16:15 -04:00
|
|
|
#!&& "killall xmonad"
|
2020-03-28 18:38:38 -04:00
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
runOptimusPrompt :: SometimesX
|
2022-06-28 23:27:55 -04:00
|
|
|
runOptimusPrompt = sometimesIO "graphics switcher" "optimus manager"
|
|
|
|
(Only_ $ localExe myOptimusManager) runOptimusPrompt'
|
2021-06-20 01:01:36 -04:00
|
|
|
|
2020-03-28 18:38:38 -04:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Universal power prompt
|
|
|
|
|
2021-11-21 10:26:28 -05:00
|
|
|
data PowerMaybeAction = Poweroff
|
2020-03-28 18:38:38 -04:00
|
|
|
| Shutdown
|
|
|
|
| Hibernate
|
|
|
|
| Reboot
|
|
|
|
deriving (Eq)
|
|
|
|
|
2021-11-21 10:26:28 -05:00
|
|
|
instance Enum PowerMaybeAction where
|
2020-03-28 18:38:38 -04:00
|
|
|
toEnum 0 = Poweroff
|
|
|
|
toEnum 1 = Shutdown
|
|
|
|
toEnum 2 = Hibernate
|
|
|
|
toEnum 3 = Reboot
|
2021-11-21 10:26:28 -05:00
|
|
|
toEnum _ = errorWithoutStackTrace "Main.Enum.PowerMaybeAction.toEnum: bad argument"
|
2020-03-28 18:38:38 -04:00
|
|
|
|
|
|
|
fromEnum Poweroff = 0
|
|
|
|
fromEnum Shutdown = 1
|
|
|
|
fromEnum Hibernate = 2
|
|
|
|
fromEnum Reboot = 3
|
|
|
|
|
|
|
|
data PowerPrompt = PowerPrompt
|
|
|
|
|
|
|
|
instance XPrompt PowerPrompt where
|
|
|
|
showXPrompt PowerPrompt = "(P)oweroff (S)uspend (H)ibernate (R)eboot:"
|
|
|
|
|
2022-06-26 20:07:25 -04:00
|
|
|
runPowerPrompt :: AlwaysX
|
2022-06-28 23:27:55 -04:00
|
|
|
runPowerPrompt = always1 "power prompt" "lock-enabled prompt" withLock powerPromptNoLock
|
2022-06-26 20:07:25 -04:00
|
|
|
where
|
|
|
|
withLock = IORoot powerPrompt (Only $ IOSometimes runScreenLock id)
|
|
|
|
|
|
|
|
powerPromptNoLock :: X ()
|
|
|
|
powerPromptNoLock = powerPrompt skip
|
|
|
|
|
|
|
|
powerPrompt :: X () -> X ()
|
|
|
|
powerPrompt lock = mkXPrompt PowerPrompt theme comp executeMaybeAction
|
2020-03-28 18:38:38 -04:00
|
|
|
where
|
2022-03-05 18:18:16 -05:00
|
|
|
comp = mkComplFunFromList theme []
|
2020-03-28 18:38:38 -04:00
|
|
|
theme = T.promptTheme { promptKeymap = keymap }
|
|
|
|
keymap = M.fromList
|
|
|
|
$ ((controlMask, xK_g), quit) :
|
|
|
|
map (first $ (,) 0)
|
2021-11-21 10:26:28 -05:00
|
|
|
[ (xK_p, sendMaybeAction Poweroff)
|
|
|
|
, (xK_s, sendMaybeAction Shutdown)
|
|
|
|
, (xK_h, sendMaybeAction Hibernate)
|
|
|
|
, (xK_r, sendMaybeAction Reboot)
|
2020-03-28 18:38:38 -04:00
|
|
|
, (xK_Return, quit)
|
|
|
|
, (xK_Escape, quit)
|
|
|
|
]
|
2021-11-21 10:26:28 -05:00
|
|
|
sendMaybeAction a = setInput (show $ fromEnum a) >> setSuccess True >> setDone True
|
|
|
|
executeMaybeAction a = case toEnum $ read a of
|
2020-03-28 18:38:38 -04:00
|
|
|
Poweroff -> runPowerOff
|
2021-11-19 22:42:19 -05:00
|
|
|
Shutdown -> lock >> runSuspend
|
|
|
|
Hibernate -> lock >> runHibernate
|
2020-03-28 18:38:38 -04:00
|
|
|
Reboot -> runReboot
|