X-Git-Url: https://disinclined.org/git/?a=blobdiff_plain;f=powermate.hs;h=91e24cfced7b6502b9ef3ea58731299bed946d99;hb=4bd7a3f471aa02515fb69d53e17bf1b99150c33c;hp=cdb4bed024dee8b4b2c70c58b1551b44cab7a608;hpb=161cec25784853bc27b48bd12ce4928ca5ec21e1;p=powermate.git diff --git a/powermate.hs b/powermate.hs index cdb4bed..91e24cf 100755 --- a/powermate.hs +++ b/powermate.hs @@ -3,13 +3,13 @@ module Main where import PowerMate import System.IO import System.Process -import Control.Monad import Text.Regex.Posix data State = State { - stPowerMate :: Handle, - stVolume :: Int, - stDir :: Int + stPowerMate :: Handle, + stVolume :: Int, + stPrevDir :: Int, + stPrevAction :: Int } processEvent :: State -> Event -> IO State @@ -20,8 +20,19 @@ processEvent state (Button False) = do return state processEvent state (Rotate dir) = do - state <- (if dir < 2 then volumeUp else volumeDown) state + state <- (if dir < 2 + && (stPrevDir state) == 1 + && (stPrevAction state) == 1 + then volumeUp + else return) state + state <- (if dir > 2 + && (stPrevDir state) == 0 + && (stPrevAction state) == 0 + then volumeDown + else return) state + state <- updatePrevState state (if dir < 2 then 1 else 0) updateBrightness state + state <- updatePrevAction state (if (stPrevAction state) == 1 then 0 else 1) return state processEvent state (StatusChange status) = do @@ -43,22 +54,44 @@ updateBrightness state = do writeStatus (stPowerMate state) $ statusInit { brightness=brightness } -volumeUp :: State -> IO State +volumeUp :: State -> IO State volumeUp state = do createProcess (proc "volume-up" []) state <- readState $ State { stPowerMate=(stPowerMate state), stVolume=(max 0 $ 1+(stVolume state)), - stDir=(stDir state) } + stPrevAction=(stPrevAction state), + stPrevDir=(stPrevDir state) } + state <- updatePrevAction state 1 return state -volumeDown :: State -> IO State +volumeDown :: State -> IO State volumeDown state = do createProcess (proc "volume-down" []) state <- readState $ State { stPowerMate=(stPowerMate state), stVolume=(max 0 $ (stVolume state)-1), - stDir=(stDir state) } + stPrevAction=(stPrevAction state), + stPrevDir=(stPrevDir state) } + state <- updatePrevAction state 0 + return state + +updatePrevState :: State -> Int -> IO State +updatePrevState state dir = do + state <- readState $ State { + stPowerMate=(stPowerMate state), + stVolume=(stVolume state), + stPrevAction=(stPrevAction state), + stPrevDir=dir } + return state + +updatePrevAction :: State -> Int -> IO State +updatePrevAction state action = do + state <- readState $ State { + stPowerMate=(stPowerMate state), + stVolume=(stVolume state), + stPrevAction=action, + stPrevDir=(stPrevDir state) } return state loop :: FilePath -> IO () @@ -74,7 +107,8 @@ loop devname = do state <- readState $ State { stPowerMate=powermate, stVolume=volume, - stDir=1 } + stPrevAction=0, + stPrevDir=0 } updateBrightness state next state $ \call -> do