diff --git a/bin/xmonad.hs b/bin/xmonad.hs index dab4fcd..5fd0f05 100644 --- a/bin/xmonad.hs +++ b/bin/xmonad.hs @@ -276,6 +276,24 @@ runHibernate = spawn "systemctl hibernate" runReboot :: X () 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 = mkXPrompt PowerPrompt theme comp executeAction where @@ -284,20 +302,19 @@ myPowerPrompt = mkXPrompt PowerPrompt theme comp executeAction keymap = M.fromList $ ((controlMask, xK_g), quit) : map (first $ (,) 0) - [ (xK_p, sendAction "p") - , (xK_s, sendAction "s") - , (xK_h, sendAction "h") - , (xK_r, sendAction "r") + [ (xK_p, sendAction Poweroff) + , (xK_s, sendAction Shutdown) + , (xK_h, sendAction Hibernate) + , (xK_r, sendAction Reboot) , (xK_Return, quit) , (xK_Escape, quit) ] - sendAction a = setInput a >> setSuccess True >> setDone True - executeAction a - | a == "p" = runPowerOff - | a == "s" = runScreenLock >> runSuspend - | a == "h" = runScreenLock >> runHibernate - | a == "r" = runReboot - | otherwise = return () -- should never happen + sendAction a = setInput (show $ fromEnum a) >> setSuccess True >> setDone True + executeAction a = case toEnum $ read a of + Poweroff -> runPowerOff + Shutdown -> runScreenLock >> runSuspend + Hibernate -> runScreenLock >> runHibernate + Reboot -> runReboot myQuitPrompt :: X () myQuitPrompt = confirmPrompt T.promptTheme "quit?" $ io exitSuccess diff --git a/lib/ACPI.hs b/lib/ACPI.hs index c04614d..0c2a67e 100644 --- a/lib/ACPI.hs +++ b/lib/ACPI.hs @@ -7,17 +7,17 @@ module ACPI , runPowermon ) where -import SendXMsg +import SendXMsg -import Control.Exception -import Control.Monad +import Control.Exception +import Control.Monad -import Data.ByteString hiding (readFile) -import Data.ByteString.Char8 as C hiding (readFile) -import Data.Connection +import Data.ByteString hiding (readFile) +import Data.ByteString.Char8 as C hiding (readFile) +import Data.Connection -import System.IO.Streams.Internal as S (read) -import System.IO.Streams.UnixSocket +import System.IO.Streams.Internal as S (read) +import System.IO.Streams.UnixSocket data ACPIEvent = Power | Sleep @@ -28,7 +28,7 @@ instance Enum ACPIEvent where toEnum 0 = Power toEnum 1 = Sleep toEnum 2 = LidClose - toEnum _ = errorWithoutStackTrace "ACPI.Enum.ACPIEvent.toEnum" + toEnum _ = errorWithoutStackTrace "ACPI.Enum.ACPIEvent.toEnum: bad argument" fromEnum Power = 0 fromEnum Sleep = 1