summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorThomas Tuegel <ttuegel@gmail.com>2011-01-13 10:34:09 -0600
committerThomas Tuegel <ttuegel@gmail.com>2011-01-13 10:36:41 -0600
commite3c9b33170f1064077aa8578342369657b9e059c (patch)
tree8478ba072b620a8da621828ce117139458c216d5
parentbfbbe753e83efa25bc3408e61fe86c6223e6c695 (diff)
downloadxmobar-e3c9b33170f1064077aa8578342369657b9e059c.tar.gz
xmobar-e3c9b33170f1064077aa8578342369657b9e059c.tar.bz2
Added volume monitor based on alsa-mixer.
-rw-r--r--src/Plugins/Monitors.hs12
-rw-r--r--src/Plugins/Monitors/Volume.hs36
-rw-r--r--xmobar.cabal8
3 files changed, 56 insertions, 0 deletions
diff --git a/src/Plugins/Monitors.hs b/src/Plugins/Monitors.hs
index 9887d74..14d97a2 100644
--- a/src/Plugins/Monitors.hs
+++ b/src/Plugins/Monitors.hs
@@ -38,6 +38,9 @@ import Plugins.Monitors.Wireless
#ifdef LIBMPD
import Plugins.Monitors.MPD
#endif
+#ifdef ALSA
+import Plugins.Monitors.Volume
+#endif
data Monitors = Weather Station Args Rate
| Network Interface Args Rate
@@ -61,6 +64,9 @@ data Monitors = Weather Station Args Rate
#ifdef LIBMPD
| MPD Args Rate
#endif
+#ifdef ALSA
+ | Volume String String Args Rate
+#endif
deriving (Show,Read,Eq)
type Args = [String]
@@ -95,6 +101,9 @@ instance Exec Monitors where
#ifdef LIBMPD
alias (MPD _ _) = "mpd"
#endif
+#ifdef ALSA
+ alias (Volume m c _ _) = m ++ ":" ++ c
+#endif
start (Weather s a r) = runM (a ++ [s]) weatherConfig runWeather r
start (Network i a r) = runM (a ++ [i]) netConfig runNet r
start (Thermal z a r) = runM (a ++ [z]) thermalConfig runThermal r
@@ -117,3 +126,6 @@ instance Exec Monitors where
#ifdef LIBMPD
start (MPD a r) = runM a mpdConfig runMPD r
#endif
+#ifdef ALSA
+ start (Volume m c a r) = runM a volumeConfig (runVolume m c) r
+#endif
diff --git a/src/Plugins/Monitors/Volume.hs b/src/Plugins/Monitors/Volume.hs
new file mode 100644
index 0000000..c7814a8
--- /dev/null
+++ b/src/Plugins/Monitors/Volume.hs
@@ -0,0 +1,36 @@
+module Plugins.Monitors.Volume (runVolume, volumeConfig) where
+
+import Control.Monad ( liftM )
+import Data.Maybe
+import Plugins.Monitors.Common
+import Sound.ALSA.Mixer
+
+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>% <fc=green><on></fc><fc=red><off></fc>"
+ ["volume","dB","on","off"]
+
+runVolume :: String -> String -> [String] -> Monitor String
+runVolume mixerName controlName _ = do
+ 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 <- liftM fromJust $ io $ getChannel FrontLeft $ value volumeControl
+ db <- liftM fromJust $ io $ getChannel FrontLeft $ dB volumeControl
+ sw <- liftM fromJust $ io $ getChannel FrontLeft $ switchControl
+ p <- showPercentsWithColors [ percent val lo hi ]
+ let d :: Double
+ d = fromIntegral db / 100.0
+ dStr = showDigits 2 d
+ parseTemplate $ p ++ [ dStr ]
+ ++ [ if sw then "[on] " else ""
+ , if sw then "" else "[off]"
+ ]
diff --git a/xmobar.cabal b/xmobar.cabal
index e5ee507..0eac0fa 100644
--- a/xmobar.cabal
+++ b/xmobar.cabal
@@ -53,6 +53,10 @@ flag all_extensions
description: Includes all optional extensions.
default: False
+flag with_alsa
+ description: Use alsa-mixer to get the volume from soundcards.
+ default: False
+
executable xmobar
hs-source-dirs: src
main-is: Main.hs
@@ -109,3 +113,7 @@ executable xmobar
build-depends: libmpd >= 0.5
other-modules: Plugins.Monitors.MPD
cpp-options: -DLIBMPD
+
+ if flag(with_alsa) || flag(all_extensions)
+ build-depends: alsa-mixer
+ cpp-options: -DALSA