diff options
author | Jose Antonio Ortega Ruiz <jao@gnu.org> | 2011-01-15 00:04:07 +0100 |
---|---|---|
committer | Jose Antonio Ortega Ruiz <jao@gnu.org> | 2011-01-15 00:04:07 +0100 |
commit | d1675af6f9749e12113d6bf957f920682e7fd91d (patch) | |
tree | 4c71d49ebb1c0260d6b127475a35622e1f3c9f66 /src/Plugins/Monitors | |
parent | 3c9b2b38bd216e2d308f3b00cf7baee52266b1be (diff) | |
parent | ce23b849f7aff963a1e6a84da5281b6a4aaff241 (diff) | |
download | xmobar-d1675af6f9749e12113d6bf957f920682e7fd91d.tar.gz xmobar-d1675af6f9749e12113d6bf957f920682e7fd91d.tar.bz2 |
Merge branch 'ttuegel-master'
Diffstat (limited to 'src/Plugins/Monitors')
-rw-r--r-- | src/Plugins/Monitors/Volume.hs | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/src/Plugins/Monitors/Volume.hs b/src/Plugins/Monitors/Volume.hs new file mode 100644 index 0000000..72a9b0e --- /dev/null +++ b/src/Plugins/Monitors/Volume.hs @@ -0,0 +1,122 @@ +----------------------------------------------------------------------------- +-- | +-- Module : Plugins.Monitors.Volume +-- Copyright : (c) 2011 Thomas Tuegel +-- License : BSD-style (see LICENSE) +-- +-- Maintainer : Jose A. Ortega Ruiz <jao@gnu.org> +-- Stability : unstable +-- Portability : unportable +-- +-- A monitor for ALSA soundcards +-- +----------------------------------------------------------------------------- + +module Plugins.Monitors.Volume (runVolume, volumeConfig) where + +import Control.Monad ( liftM ) +import Data.Maybe +import Plugins.Monitors.Common +import Sound.ALSA.Mixer +import System.Console.GetOpt + +data VolumeOpts = VolumeOpts + { onString :: String + , offString :: String + , onColor :: Maybe String + , offColor :: Maybe String + , highDbThresh :: Float + , lowDbThresh :: Float + } + +defaultOpts :: VolumeOpts +defaultOpts = VolumeOpts + { onString = "[on] " + , offString = "[off]" + , onColor = Just "green" + , offColor = Just "red" + , highDbThresh = -5.0 + , lowDbThresh = -30.0 + } + +options :: [OptDescr (VolumeOpts -> VolumeOpts)] +options = + [ Option "O" ["on"] (ReqArg (\x o -> o { onString = x }) "") "" + , Option "o" ["off"] (ReqArg (\x o -> o { offString = x }) "") "" + , Option "" ["lowd"] (ReqArg (\x o -> o { lowDbThresh = read x }) "") "" + , Option "" ["highd"] (ReqArg (\x o -> o { highDbThresh = read x }) "") "" + , Option "C" ["onc"] (ReqArg (\x o -> o { onColor = Just x }) "") "" + , Option "c" ["offc"] (ReqArg (\x o -> o { offColor = Just x }) "") "" + ] + +parseOpts :: [String] -> IO VolumeOpts +parseOpts argv = + case getOpt Permute options argv of + (o, _, []) -> return $ foldr id defaultOpts o + (_, _, errs) -> ioError . userError $ concat errs + +percent :: Integer -> Integer -> Integer -> Float +percent v' lo' hi' = (v - lo) / (hi - lo) + where v = fromIntegral v' + lo = fromIntegral lo' + hi = fromIntegral hi' + +volumeConfig :: IO MConfig +volumeConfig = mkMConfig "Vol: <volume>% <status>" + ["volume","dB","status"] + +formatVol :: Integer -> Integer -> Integer -> Monitor String +formatVol v lo hi = + showPercentWithColors $ percent v lo hi + +switchHelper :: VolumeOpts + -> (VolumeOpts -> Maybe String) + -> (VolumeOpts -> String) + -> Monitor String +switchHelper opts cHelp strHelp = return $ + colorHelper (cHelp opts) + ++ strHelp opts + ++ maybe "" (const "</fc>") (cHelp opts) + +formatSwitch :: VolumeOpts -> Bool -> Monitor String +formatSwitch opts True = switchHelper opts onColor onString +formatSwitch opts False = switchHelper opts offColor offString + +colorHelper :: Maybe String -> String +colorHelper = maybe "" (\c -> "<fc=" ++ c ++ ">") + +formatDb :: VolumeOpts -> Float -> Monitor String +formatDb opts db = do + h <- getConfigValue highColor + m <- getConfigValue normalColor + l <- getConfigValue lowColor + let digits = showDigits 0 db + startColor | db >= highDbThresh opts = colorHelper h + | db < lowDbThresh opts = colorHelper l + | otherwise = colorHelper m + stopColor | null startColor = "" + | otherwise = "</fc>" + return $ startColor ++ digits ++ stopColor + +runVolume :: String -> String -> [String] -> Monitor String +runVolume mixerName controlName argv = do + opts <- io $ parseOpts argv + control <- liftM fromJust $ io $ getControlByName mixerName controlName + let volumeControl = fromJust $ maybe (playback $ volume control) Just + (common $ volume control) + switchControl = fromJust $ maybe (playback $ switch control) Just + (common $ switch control) + (lo, hi) <- io $ getRange volumeControl + val <- io $ getChannel FrontLeft $ value volumeControl + db <- io $ getChannel FrontLeft $ dB volumeControl + sw <- io $ getChannel FrontLeft switchControl + p <- case val of + Just x -> formatVol x lo hi + Nothing -> formatVol hi lo hi + d <- case db of + Just x -> formatDb opts $ fromIntegral x / 100.0 + Nothing -> formatDb opts 0.0 + s <- case sw of + Just x -> formatSwitch opts x + Nothing -> formatSwitch opts True + parseTemplate [ p, d, s ] |