xmonad-config/bin/powermon.hs

43 lines
1.1 KiB
Haskell
Raw Normal View History

2020-03-13 20:50:13 -04:00
{-# LANGUAGE OverloadedStrings #-}
import ACPI
2020-03-20 01:12:20 -04:00
import SendXMsg
2020-03-13 20:50:13 -04:00
import Control.Monad
import Data.ByteString
import Data.ByteString.Char8 as C
import Data.Connection
2020-03-20 01:12:20 -04:00
import System.IO.Streams.Internal as S (read)
2020-03-13 20:50:13 -04:00
import System.IO.Streams.UnixSocket
splitLine :: ByteString -> [ByteString]
splitLine = C.words . C.reverse . C.dropWhile (== '\n') . C.reverse
parseLine :: ByteString -> Maybe ACPIEvent
2020-03-20 01:12:20 -04:00
parseLine line =
2020-03-13 20:50:13 -04:00
-- TODO what if we don't have a list this long (we crash)
case (line' !! 1, line' !! 2) of
2020-03-20 01:12:20 -04:00
("PBTN", _) -> Just Power
("PWRF", _) -> Just Power
("SLPB", _) -> Just Sleep
("SBTN", _) -> Just Sleep
2020-03-13 20:50:13 -04:00
("LID", "close") -> Just LidClose
2020-03-20 01:12:20 -04:00
_ -> Nothing
2020-03-13 20:50:13 -04:00
where
line' = splitLine line
sendACPIEvent :: ACPIEvent -> IO ()
sendACPIEvent = sendXMsg acpiMagic . show
main :: IO ()
main = do
-- TODO barf when the socket doesn't exist
Connection { source = s } <- connect "/var/run/acpid.socket"
forever $ readStream s
where
readStream s = do
out <- (>>= parseLine) <$> S.read s
forM_ out sendACPIEvent