[scan for device Evan Martin **20060216085150] { hunk ./MPDMate.hs 8 -file = "/dev/input/event3" - hunk ./MPDMate.hs 21 +main :: IO () hunk ./MPDMate.hs 23 - name <- PowerMate.getName file - putStrLn name + powermate_device <- PowerMate.searchForDevice + case powermate_device of + Nothing -> return () + Just pmd -> do + mpd <- MPD.connect "localhost" 6600 hunk ./MPDMate.hs 29 - mpd <- MPD.connect "localhost" 6600 - - handle_pm <- openBinaryFile file ReadWriteMode - loop mpd $ \mpd -> do - event <- PowerMate.readEventWithSkip handle_pm Nothing - (do maybe (return ()) (processEvent mpd) event - return mpd) - `catch` \e -> do - print $ ioeGetErrorString e - MPD.reconnect mpd + handle_pm <- openBinaryFile pmd ReadWriteMode + loop mpd $ \mpd -> do + event <- PowerMate.readEventWithSkip handle_pm Nothing + (do maybe (return ()) (processEvent mpd) event + return mpd) + `catch` \e -> do + print $ ioeGetErrorString e + MPD.reconnect mpd + return () hunk ./PowerMate.hsc 3 - getName, + getUSBName, + searchForDevice, hunk ./PowerMate.hsc 19 +import List (isPrefixOf, find) +import Control.Monad (filterM, liftM) +import System.Directory (getDirectoryContents) hunk ./PowerMate.hsc 36 -getName :: FilePath -> IO String -getName filename = do +getUSBName :: FilePath -> IO String +getUSBName filename = do hunk ./PowerMate.hsc 39 + +searchForDevice :: IO (Maybe FilePath) +searchForDevice = do + files <- getDirectoryContents basedir + let goodfiles = filter ("event" `isPrefixOf`) files + let paths = [basedir ++ "/" ++ file | file <- goodfiles] + -- There's this: find :: (a -> Bool) -> [a] -> Maybe a + -- but I want: (a -> m Bool) -> [a] -> m (Maybe a) + inputs <- filterM deviceIsGood paths + return $ case inputs of + [] -> Nothing + (x:_) -> Just x + where basedir = "/dev/input" + deviceIsGood path = do + putStr (path ++ ": ") + hFlush stdout + name <- getUSBName path + putStrLn name + return $ nameIsGood name + nameIsGood "Griffin PowerMate" = True + nameIsGood _ = False }