7 import Text.Regex.Posix
10 stPowerMate :: Handle,
15 processEvent :: State -> Event -> IO State
16 processEvent state (Button True) = do
17 createProcess (proc "music-toggle" [])
19 processEvent state (Button False) = do
22 processEvent state (Rotate dir) = do
23 state <- (if dir < 2 then volumeUp else volumeDown) state
24 updateBrightness state
27 processEvent state (StatusChange status) = do
30 readState :: State -> IO State
34 next :: a -> (a -> IO a) -> IO ()
36 newstate <- func state
40 updateBrightness :: State -> IO ()
41 updateBrightness state = do
42 let brightness = (stVolume state)
43 writeStatus (stPowerMate state) $
44 statusInit { brightness=brightness }
46 volumeUp :: State -> IO State
48 createProcess (proc "volume-up" [])
49 state <- readState $ State {
50 stPowerMate=(stPowerMate state),
51 stVolume=(max 0 $ 1+(stVolume state)),
55 volumeDown :: State -> IO State
57 createProcess (proc "volume-down" [])
58 state <- readState $ State {
59 stPowerMate=(stPowerMate state),
60 stVolume=(max 0 $ (stVolume state)-1),
64 loop :: FilePath -> IO ()
66 powermate <- openDevice devname
68 alsaMixers <- readProcess "amixer" ["get", "Master"] []
69 let alsaMaster = (alsaMixers =~ "\\[([0-9]{1,2})%\\]" :: String)
70 let volume = read (drop 1
73 (length alsaMaster)) alsaMaster)) :: Int
74 state <- readState $ State {
75 stPowerMate=powermate,
78 updateBrightness state
80 next state $ \call -> do
81 event <- readEventWithSkip powermate Nothing
83 Nothing -> return call
84 Just event -> processEvent call event
88 powermate <- searchForDevice