From f416907a72738adbbf5bd877e1fa9d91826de6ac Mon Sep 17 00:00:00 2001 From: Jose A Ortega Ruiz Date: Tue, 30 Mar 2010 21:31:29 +0200 Subject: New (and optional) MPD monitor. Ignore-this: 405d2dcf63efe90ea243df1c99db8ead darcs-hash:20100330193129-748be-5af6dc8af59ccaf3b71a250b02c76900716008dc.gz --- Plugins/Monitors.hs | 16 ++++++++- Plugins/Monitors/MPD.hs | 92 +++++++++++++++++++++++++++++++++++++++++++++++++ README | 22 ++++++++++++ xmobar.cabal | 10 +++++- 4 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 Plugins/Monitors/MPD.hs diff --git a/Plugins/Monitors.hs b/Plugins/Monitors.hs index 158a990..dd01ce3 100644 --- a/Plugins/Monitors.hs +++ b/Plugins/Monitors.hs @@ -34,6 +34,9 @@ import Plugins.Monitors.Top #ifdef IWLIB import Plugins.Monitors.Wireless #endif +#ifdef LIBMPD +import Plugins.Monitors.MPD +#endif data Monitors = Weather Station Args Rate | Network Interface Args Rate @@ -52,6 +55,9 @@ data Monitors = Weather Station Args Rate | TopMem Args Rate #ifdef IWLIB | Wireless Interface Args Rate +#endif +#ifdef LIBMPD + | MPD Args Rate #endif deriving (Show,Read,Eq) @@ -82,7 +88,9 @@ instance Exec Monitors where alias (DiskIO _ _ _) = "diskio" #ifdef IWLIB alias (Wireless i _ _) = i ++ "wi" - start (Wireless i a r) = runM (a ++ [i]) wirelessConfig runWireless r +#endif +#ifdef LIBMPD + alias (MPD _ _) = "mpd" #endif start (Weather s a r) = runM (a ++ [s]) weatherConfig runWeather r start (Network i a r) = runM (a ++ [i]) netConfig runNet r @@ -99,3 +107,9 @@ instance Exec Monitors where start (DiskIO s a r) = runM a diskIOConfig (runDiskIO s) r start (TopMem a r) = runM a topMemConfig runTopMem r start (TopProc a r) = startTop a r +#ifdef IWLIB + start (Wireless i a r) = runM (a ++ [i]) wirelessConfig runWireless r +#endif +#ifdef LIBMPD + start (MPD a r) = runM a mpdConfig runMPD r +#endif diff --git a/Plugins/Monitors/MPD.hs b/Plugins/Monitors/MPD.hs new file mode 100644 index 0000000..410662d --- /dev/null +++ b/Plugins/Monitors/MPD.hs @@ -0,0 +1,92 @@ +----------------------------------------------------------------------------- +-- | +-- Module : Plugins.Monitors.MPD +-- Copyright : (c) Jose A Ortega Ruiz +-- License : BSD-style (see LICENSE) +-- +-- Maintainer : Jose A Ortega Ruiz +-- Stability : unstable +-- Portability : unportable +-- +-- MPD status and song +-- +----------------------------------------------------------------------------- + +module Plugins.Monitors.MPD ( mpdConfig, runMPD ) where + +import Plugins.Monitors.Common +import System.Console.GetOpt +import qualified Network.MPD as M + +mpdConfig :: IO MConfig +mpdConfig = mkMConfig "MPD: " + [ "bar", "state", "statei", "volume", "length" + , "lapsed", "plength" + , "name", "artist", "composer", "performer" + , "album", "title", "track", "trackno", "file", "genre" + ] + +data MOpts = MOpts {mPlaying :: String, mStopped :: String, mPaused :: String} + +defaultOpts :: MOpts +defaultOpts = MOpts { mPlaying = ">>", mStopped = "><", mPaused = "||" } + +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 }) "") "" + ] + +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) + +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) + where s = M.stState st + ss = show s + si = stateGlyph s opts + vol = int2Str $ M.stVolume st + (lap, len) = (showTime p, showTime t) + (p, t) = M.stTime st + b = if t > 0 then fromIntegral p / fromIntegral t else 0 + plen = int2Str $ M.stPlaylistLength st + sf = parseSong song + +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 (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 + +showTime :: Integer -> String +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 diff --git a/README b/README index f0eb521..5d0a8ba 100644 --- a/README +++ b/README @@ -443,6 +443,28 @@ Monitors have default aliases. - This monitor requires coretemp module to be loaded in kernel - Example: `Run CoreTemp ["-t","Temp:\|\C","-L","40","-H","60","-l","lightblue","-n","gray90","-h","red"] 50` + +`MPD Args RefreshRate` + +- aliases to `mpd` +- Args: the argument list (see below). In addition you can provide + `-P`, `-S` and `-Z`, with an string argument, to represent the + playing, stopped and paused states in the `statei` template field. +- Variables that can be used with the `-t`/`--template` argument: + `bar`, `state`, `statei`, `volume`, `length` + `lapsed`, `plength` + `name`, `artist`, `composer`, `performer` + `album`, `title`, `track`, `file`, `genre` +- Default template: `MPD: ` +- Example: + Run MPD ["-t", + " (<album>) <track>/<plength> <statei> ", + "--", "-P", ">>", "-Z", "|", "-S", "><"] 10 + Note that you need "--" to separate regular monitor options from + MPD's specific ones. +- This monitor will only be compiled if you ask for it using the + `with_mpd` flag. It needs libmpd 4.1 or later (available on Hackage). + `Mail Args` - aliases to `Mail` diff --git a/xmobar.cabal b/xmobar.cabal index e4d0cb5..bad2b9a 100644 --- a/xmobar.cabal +++ b/xmobar.cabal @@ -35,6 +35,10 @@ flag with_iwlib description: wireless info support. Required for the Wireless plugin, needs iwlib installed. default: False +flag with_mpd + description: mpd support. Needs libmpd installed. + default: False + executable xmobar main-is: Main.hs other-modules: Xmobar, Config, Parsers, Commands, XUtil, StatFS, Runnable, Plugins @@ -72,4 +76,8 @@ executable xmobar if flag(with_iwlib) extra-libraries: iw other-modules: IWlib - cpp-options: -DIWLIB \ No newline at end of file + cpp-options: -DIWLIB + + if flag(with_mpd) + build-depends: libmpd > 0.4 + cpp-options: -DLIBMPD -- cgit v1.2.3