7 import Text.Regex.Posix
10 stPowerMate :: Handle,
16 processEvent :: State -> Event -> IO State
17 processEvent state (Button True) = do
18 createProcess (proc "music-toggle" [])
20 processEvent state (Button False) = do
23 processEvent state (Rotate dir) = do
25 && (stPrevDir state) == 1
26 && (stPrevAction state) == 1
30 && (stPrevDir state) == 0
31 && (stPrevAction state) == 0
34 state <- updatePrevState state (if dir < 2 then 1 else 0)
35 updateBrightness state
36 state <- updatePrevAction state (if (stPrevAction state) == 1 then 0 else 1)
39 processEvent state (StatusChange status) = do
42 readState :: State -> IO State
46 next :: a -> (a -> IO a) -> IO ()
48 newstate <- func state
52 updateBrightness :: State -> IO ()
53 updateBrightness state = do
54 let brightness = (stVolume state)
55 writeStatus (stPowerMate state) $
56 statusInit { brightness=brightness }
58 volumeUp :: State -> IO State
60 createProcess (proc "volume-up" [])
61 state <- readState $ State {
62 stPowerMate=(stPowerMate state),
63 stVolume=(max 0 $ 1+(stVolume state)),
64 stPrevAction=(stPrevAction state),
65 stPrevDir=(stPrevDir state) }
66 state <- updatePrevAction state 1
69 volumeDown :: State -> IO State
71 createProcess (proc "volume-down" [])
72 state <- readState $ State {
73 stPowerMate=(stPowerMate state),
74 stVolume=(max 0 $ (stVolume state)-1),
75 stPrevAction=(stPrevAction state),
76 stPrevDir=(stPrevDir state) }
77 state <- updatePrevAction state 0
80 updatePrevState :: State -> Int -> IO State
81 updatePrevState state dir = do
82 state <- readState $ State {
83 stPowerMate=(stPowerMate state),
84 stVolume=(stVolume state),
85 stPrevAction=(stPrevAction state),
89 updatePrevAction :: State -> Int -> IO State
90 updatePrevAction state action = do
91 state <- readState $ State {
92 stPowerMate=(stPowerMate state),
93 stVolume=(stVolume state),
95 stPrevDir=(stPrevDir state) }
98 loop :: FilePath -> IO ()
100 powermate <- openDevice devname
102 alsaMixers <- readProcess "amixer" ["get", "Master"] []
103 let alsaMaster = (alsaMixers =~ "\\[([0-9]{1,2})%\\]" :: String)
104 let volume = read (drop 1
107 (length alsaMaster)) alsaMaster)) :: Int
108 state <- readState $ State {
109 stPowerMate=powermate,
113 updateBrightness state
115 next state $ \call -> do
116 event <- readEventWithSkip powermate Nothing
118 Nothing -> return call
119 Just event -> processEvent call event
123 powermate <- searchForDevice