init
[powermate.git] / powermate.hs
1 module Main where
2
3 import PowerMate
4 import System.IO
5 import System.Process
6
7 data State = State {
8   stPowerMate  :: Handle,
9   stVolume     :: Int
10 }
11
12 processEvent :: State -> Event -> IO State
13 processEvent state (Button True) = do
14   createProcess (proc "music-toggle" [])
15   return state
16 processEvent state (Button False) = return state
17
18 processEvent state (Rotate dir) = do
19   if dir < 2 then createProcess (proc "volume-up" [])
20   else createProcess (proc "volume-down" [])
21   return state
22
23 processEvent state (StatusChange status) = do
24   return state
25
26 readState :: State -> IO State
27 readState state = do
28   return state
29
30 next :: a -> (a -> IO a) -> IO ()
31 next state func = do
32   newstate <- func state
33   next newstate func
34   return ()
35
36
37 loop :: FilePath -> IO ()
38 loop devname = do
39   powermate <- openDevice devname
40
41   state <- readState $ State { stPowerMate=powermate, stVolume=0 }
42
43   next state $ \call -> do
44     event <- readEventWithSkip powermate Nothing
45     case event of
46       Nothing -> return call
47       Just event -> processEvent call event
48
49 main :: IO ()
50 main = do
51   powermate <- searchForDevice
52   case powermate of
53     Nothing  -> return ()
54     Just work -> do
55       loop work