6 import Text.Regex.Posix
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
24 && (stPrevDir state) == 1
25 && (stPrevAction state) == 1
29 && (stPrevDir state) == 0
30 && (stPrevAction state) == 0
33 state <- updatePrevState state (if dir < 2 then 1 else 0)
34 updateBrightness state
35 state <- updatePrevAction state (if (stPrevAction state) == 1 then 0 else 1)
38 processEvent state (StatusChange status) = do
41 readState :: State -> IO State
45 next :: a -> (a -> IO a) -> IO ()
47 newstate <- func state
51 updateBrightness :: State -> IO ()
52 updateBrightness state = do
53 let brightness = (stVolume state)
54 writeStatus (stPowerMate state) $
55 statusInit { brightness=brightness }
57 volumeUp :: State -> IO State
59 createProcess (proc "volume-up" [])
60 state <- readState $ State {
61 stPowerMate=(stPowerMate state),
62 stVolume=(max 0 $ 1+(stVolume state)),
63 stPrevAction=(stPrevAction state),
64 stPrevDir=(stPrevDir state) }
65 state <- updatePrevAction state 1
68 volumeDown :: State -> IO State
70 createProcess (proc "volume-down" [])
71 state <- readState $ State {
72 stPowerMate=(stPowerMate state),
73 stVolume=(max 0 $ (stVolume state)-1),
74 stPrevAction=(stPrevAction state),
75 stPrevDir=(stPrevDir state) }
76 state <- updatePrevAction state 0
79 updatePrevState :: State -> Int -> IO State
80 updatePrevState state dir = do
81 state <- readState $ State {
82 stPowerMate=(stPowerMate state),
83 stVolume=(stVolume state),
84 stPrevAction=(stPrevAction state),
88 updatePrevAction :: State -> Int -> IO State
89 updatePrevAction state action = do
90 state <- readState $ State {
91 stPowerMate=(stPowerMate state),
92 stVolume=(stVolume state),
94 stPrevDir=(stPrevDir state) }
97 loop :: FilePath -> IO ()
99 powermate <- openDevice devname
101 alsaMixers <- readProcess "amixer" ["get", "Master"] []
102 let alsaMaster = (alsaMixers =~ "\\[([0-9]{1,2})%\\]" :: String)
103 let volume = read (drop 1
106 (length alsaMaster)) alsaMaster)) :: Int
107 state <- readState $ State {
108 stPowerMate=powermate,
112 updateBrightness state
114 next state $ \call -> do
115 event <- readEventWithSkip powermate Nothing
117 Nothing -> return call
118 Just event -> processEvent call event
122 powermate <- searchForDevice