128 lines
4.7 KiB
Diff
128 lines
4.7 KiB
Diff
|
--- Plugins/Monitors/MPD.hs 2010-12-01 21:35:40.602992136 -0500
|
||
|
+++ Plugins/Monitors/MPD.hs.libmpd-0.5 2010-11-18 17:24:03.000000000 -0500
|
||
|
@@ -19,74 +19,97 @@
|
||
|
import qualified Network.MPD as M
|
||
|
|
||
|
mpdConfig :: IO MConfig
|
||
|
mpdConfig = mkMConfig "MPD: <state>"
|
||
|
[ "bar", "state", "statei", "volume", "length"
|
||
|
- , "lapsed", "plength"
|
||
|
+ , "lapsed", "remaining", "plength", "ppos", "file"
|
||
|
, "name", "artist", "composer", "performer"
|
||
|
- , "album", "title", "track", "trackno", "file", "genre"
|
||
|
+ , "album", "title", "track", "genre"
|
||
|
]
|
||
|
|
||
|
-data MOpts = MOpts {mPlaying :: String, mStopped :: String, mPaused :: String}
|
||
|
+data MOpts = MOpts
|
||
|
+ { mPlaying :: String
|
||
|
+ , mStopped :: String
|
||
|
+ , mPaused :: String
|
||
|
+ , mHost :: String
|
||
|
+ , mPort :: Integer
|
||
|
+ , mPassword :: String
|
||
|
+ }
|
||
|
|
||
|
defaultOpts :: MOpts
|
||
|
-defaultOpts = MOpts { mPlaying = ">>", mStopped = "><", mPaused = "||" }
|
||
|
+defaultOpts = MOpts
|
||
|
+ { mPlaying = ">>"
|
||
|
+ , mStopped = "><"
|
||
|
+ , mPaused = "||"
|
||
|
+ , mHost = "127.0.0.1"
|
||
|
+ , mPort = 6600
|
||
|
+ , mPassword = ""
|
||
|
+ }
|
||
|
|
||
|
options :: [OptDescr (MOpts -> MOpts)]
|
||
|
options =
|
||
|
[ Option "P" ["playing"] (ReqArg (\x o -> o { mPlaying = x }) "") ""
|
||
|
, Option "S" ["stopped"] (ReqArg (\x o -> o { mStopped = x }) "") ""
|
||
|
, Option "Z" ["paused"] (ReqArg (\x o -> o { mPaused = x }) "") ""
|
||
|
+ , Option "h" ["host"] (ReqArg (\x o -> o { mHost = x }) "") ""
|
||
|
+ , Option "p" ["port"] (ReqArg (\x o -> o { mPort = read x }) "") ""
|
||
|
+ , Option "x" ["password"] (ReqArg (\x o -> o { mPassword = x }) "") ""
|
||
|
]
|
||
|
|
||
|
runMPD :: [String] -> Monitor String
|
||
|
runMPD args = do
|
||
|
- status <- io $ M.withMPD M.status
|
||
|
- song <- io $ M.withMPD M.currentSong
|
||
|
opts <- io $ mopts args
|
||
|
- let (b, s) = parseMPD status song opts
|
||
|
- bs <- showPercentBar (100 * b) b
|
||
|
- parseTemplate (bs:s)
|
||
|
+ let mpd = M.withMPDEx (mHost opts) (mPort opts) (mPassword opts)
|
||
|
+ status <- io $ mpd M.status
|
||
|
+ song <- io $ mpd M.currentSong
|
||
|
+ s <- parseMPD status song opts
|
||
|
+ parseTemplate s
|
||
|
|
||
|
mopts :: [String] -> IO MOpts
|
||
|
mopts argv =
|
||
|
case getOpt Permute options argv of
|
||
|
(o, _, []) -> return $ foldr id defaultOpts o
|
||
|
(_, _, errs) -> ioError . userError $ concat errs
|
||
|
|
||
|
parseMPD :: M.Response M.Status -> M.Response (Maybe M.Song) -> MOpts
|
||
|
- -> (Float, [String])
|
||
|
-parseMPD (Left e) _ _ = (0, show e:repeat "")
|
||
|
-parseMPD (Right st) song opts = (b, [ss, si, vol, len, lap, plen] ++ sf)
|
||
|
+ -> Monitor [String]
|
||
|
+parseMPD (Left e) _ _ = return $ show e:repeat ""
|
||
|
+parseMPD (Right st) song opts = do
|
||
|
+ songData <- parseSong song
|
||
|
+ bar <- showPercentBar (100 * b) b
|
||
|
+ return $ [bar, ss, si, vol, len, lap, remain, plen, ppos] ++ songData
|
||
|
where s = M.stState st
|
||
|
ss = show s
|
||
|
si = stateGlyph s opts
|
||
|
- vol = int2Str $ M.stVolume st
|
||
|
- (lap, len) = (showTime p, showTime t)
|
||
|
+ vol = int2str $ M.stVolume st
|
||
|
(p, t) = M.stTime st
|
||
|
- b = if t > 0 then fromIntegral p / fromIntegral t else 0
|
||
|
- plen = int2Str $ M.stPlaylistLength st
|
||
|
- sf = parseSong song
|
||
|
+ [lap, len, remain] = map showTime [floor p, t, max 0 (t - floor p)]
|
||
|
+ b = if t > 0 then realToFrac $ p / fromIntegral t else 0
|
||
|
+ plen = int2str $ M.stPlaylistLength st
|
||
|
+ ppos = maybe "" (int2str . (+1)) $ M.stSongPos st
|
||
|
|
||
|
stateGlyph :: M.State -> MOpts -> String
|
||
|
stateGlyph s o =
|
||
|
case s of
|
||
|
M.Playing -> mPlaying o
|
||
|
M.Paused -> mPaused o
|
||
|
M.Stopped -> mStopped o
|
||
|
|
||
|
-parseSong :: M.Response (Maybe M.Song) -> [String]
|
||
|
-parseSong (Left _) = repeat ""
|
||
|
-parseSong (Right Nothing) = repeat ""
|
||
|
+parseSong :: M.Response (Maybe M.Song) -> Monitor [String]
|
||
|
+parseSong (Left _) = return $ repeat ""
|
||
|
+parseSong (Right Nothing) = return $ repeat ""
|
||
|
parseSong (Right (Just s)) =
|
||
|
- [ M.sgName s, M.sgArtist s, M.sgComposer s, M.sgPerformer s
|
||
|
- , M.sgAlbum s, M.sgTitle s, track, trackno, M.sgFilePath s, M.sgGenre s]
|
||
|
- where (track, trackno) = (int2Str t, int2Str tn)
|
||
|
- (t, tn) = M.sgTrack s
|
||
|
+ let join [] = ""
|
||
|
+ join (x:xs) = foldl (\a o -> a ++ ", " ++ o) x xs
|
||
|
+ str sel = maybe "" join (M.sgGet sel s)
|
||
|
+ sels = [ M.Name, M.Artist, M.Composer, M.Performer
|
||
|
+ , M.Album, M.Title, M.Track, M.Genre ]
|
||
|
+ fields = M.sgFilePath s : map str sels
|
||
|
+ in mapM showWithPadding fields
|
||
|
|
||
|
showTime :: Integer -> String
|
||
|
-showTime t = int2Str minutes ++ ":" ++ int2Str seconds
|
||
|
+showTime t = int2str minutes ++ ":" ++ int2str seconds
|
||
|
where minutes = t `div` 60
|
||
|
seconds = t `mod` 60
|
||
|
|
||
|
-int2Str :: (Num a, Ord a) => a -> String
|
||
|
-int2Str x = if x < 10 then '0':sx else sx where sx = show x
|
||
|
+int2str :: (Num a, Ord a) => a -> String
|
||
|
+int2str x = if x < 10 then '0':sx else sx where sx = show x
|