ENH use enums for power actions

This commit is contained in:
Nathan Dwarshuis 2020-03-22 23:46:56 -04:00
parent 84e3c4a0a1
commit 2f8a0eaf0a
2 changed files with 37 additions and 20 deletions

View File

@ -276,6 +276,24 @@ runHibernate = spawn "systemctl hibernate"
runReboot :: X () runReboot :: X ()
runReboot = spawn "systemctl reboot" runReboot = spawn "systemctl reboot"
data PowerAction = Poweroff
| Shutdown
| Hibernate
| Reboot
deriving (Eq)
instance Enum PowerAction where
toEnum 0 = Poweroff
toEnum 1 = Shutdown
toEnum 2 = Hibernate
toEnum 3 = Reboot
toEnum _ = errorWithoutStackTrace "Main.Enum.PowerAction.toEnum: bad argument"
fromEnum Poweroff = 0
fromEnum Shutdown = 1
fromEnum Hibernate = 2
fromEnum Reboot = 3
myPowerPrompt :: X () myPowerPrompt :: X ()
myPowerPrompt = mkXPrompt PowerPrompt theme comp executeAction myPowerPrompt = mkXPrompt PowerPrompt theme comp executeAction
where where
@ -284,20 +302,19 @@ myPowerPrompt = mkXPrompt PowerPrompt theme comp executeAction
keymap = M.fromList keymap = M.fromList
$ ((controlMask, xK_g), quit) : $ ((controlMask, xK_g), quit) :
map (first $ (,) 0) map (first $ (,) 0)
[ (xK_p, sendAction "p") [ (xK_p, sendAction Poweroff)
, (xK_s, sendAction "s") , (xK_s, sendAction Shutdown)
, (xK_h, sendAction "h") , (xK_h, sendAction Hibernate)
, (xK_r, sendAction "r") , (xK_r, sendAction Reboot)
, (xK_Return, quit) , (xK_Return, quit)
, (xK_Escape, quit) , (xK_Escape, quit)
] ]
sendAction a = setInput a >> setSuccess True >> setDone True sendAction a = setInput (show $ fromEnum a) >> setSuccess True >> setDone True
executeAction a executeAction a = case toEnum $ read a of
| a == "p" = runPowerOff Poweroff -> runPowerOff
| a == "s" = runScreenLock >> runSuspend Shutdown -> runScreenLock >> runSuspend
| a == "h" = runScreenLock >> runHibernate Hibernate -> runScreenLock >> runHibernate
| a == "r" = runReboot Reboot -> runReboot
| otherwise = return () -- should never happen
myQuitPrompt :: X () myQuitPrompt :: X ()
myQuitPrompt = confirmPrompt T.promptTheme "quit?" $ io exitSuccess myQuitPrompt = confirmPrompt T.promptTheme "quit?" $ io exitSuccess

View File

@ -28,7 +28,7 @@ instance Enum ACPIEvent where
toEnum 0 = Power toEnum 0 = Power
toEnum 1 = Sleep toEnum 1 = Sleep
toEnum 2 = LidClose toEnum 2 = LidClose
toEnum _ = errorWithoutStackTrace "ACPI.Enum.ACPIEvent.toEnum" toEnum _ = errorWithoutStackTrace "ACPI.Enum.ACPIEvent.toEnum: bad argument"
fromEnum Power = 0 fromEnum Power = 0
fromEnum Sleep = 1 fromEnum Sleep = 1