summaryrefslogtreecommitdiffhomepage
path: root/src/Plugins
diff options
context:
space:
mode:
authorDmitry Malikov <color404051h@gmail.com>2013-02-04 02:35:43 +0400
committerDmitry Malikov <color404051h@gmail.com>2013-02-04 02:56:50 +0400
commitfdc91a62c37bd475440bee275e6b0329833f6af4 (patch)
tree3960d435a9144e39a88d0a639cfa5c75d7056e26 /src/Plugins
parent1b1aa3a756aef66e5aa9b4d7b393a1565fa63737 (diff)
downloadxmobar-fdc91a62c37bd475440bee275e6b0329833f6af4.tar.gz
xmobar-fdc91a62c37bd475440bee275e6b0329833f6af4.tar.bz2
Remove unsafe fromJust from Volume plugin
Diffstat (limited to 'src/Plugins')
-rw-r--r--src/Plugins/Monitors/Volume.hs67
1 files changed, 49 insertions, 18 deletions
diff --git a/src/Plugins/Monitors/Volume.hs b/src/Plugins/Monitors/Volume.hs
index 8eef780..23dc348 100644
--- a/src/Plugins/Monitors/Volume.hs
+++ b/src/Plugins/Monitors/Volume.hs
@@ -14,9 +14,9 @@
module Plugins.Monitors.Volume (runVolume, volumeConfig) where
-import Prelude hiding ( catch )
-import Control.Monad ( liftM, mplus )
-import Data.Maybe
+import Control.Applicative ((<$>))
+import Control.Monad ( join, liftM2, liftM3, mplus )
+import Data.Traversable (sequenceA)
import Plugins.Monitors.Common
import Sound.ALSA.Mixer
import Sound.ALSA.Exception ( catch )
@@ -110,19 +110,50 @@ formatDb opts dbi = do
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 $ mplus (playback $ volume control)
- (common $ volume control)
- switchControl = fromJust $ mplus (playback $ switch control)
- (common $ switch control)
- maybeNA = maybe (return "N/A")
- (lo, hi) <- io $ getRange volumeControl
- val <- io $ getChannel FrontLeft $ value volumeControl
- db <- io $ catch (getChannel FrontLeft $ dB volumeControl)
- (\_ -> return $ Just 0)
- sw <- io $ getChannel FrontLeft switchControl
- p <- maybeNA (formatVol lo hi) val
- b <- maybeNA (formatVolBar lo hi) val
- d <- maybeNA (formatDb opts) db
- s <- maybeNA (formatSwitch opts) sw
+ control <- io $ getControlByName mixerName controlName
+ (lo, hi) <- io . liftMaybe $ getRange <$> volumeControl control
+ val <- getVal $ volumeControl control
+ db <- getDB $ volumeControl control
+ sw <- getSw $ switchControl control
+ p <- liftMonitor $ liftM3 formatVol lo hi val
+ b <- liftMonitor $ liftM3 formatVolBar lo hi val
+ d <- getFormatDB opts db
+ s <- getFormatSwitch opts sw
parseTemplate [p, b, d, s]
+
+ where
+
+ volumeControl :: Maybe Control -> Maybe Volume
+ volumeControl c = join $ (playback . volume <$> c) `mplus` (common . volume <$> c)
+
+ switchControl :: Maybe Control -> Maybe Switch
+ switchControl c = join $ (playback . switch <$> c) `mplus` (common . switch <$> c)
+
+ liftMaybe :: Maybe (IO (a,b)) -> IO (Maybe a, Maybe b)
+ liftMaybe = fmap (liftM2 (,) (fmap fst) (fmap snd)) . sequenceA
+
+ liftMonitor :: Maybe (Monitor String) -> Monitor String
+ liftMonitor Nothing = return unavailable
+ liftMonitor (Just m) = m
+
+ getDB :: Maybe Volume -> Monitor (Maybe Integer)
+ getDB Nothing = return Nothing
+ getDB (Just v) = io $ catch (getChannel FrontLeft $ dB v) (const $ return $ Just 0)
+
+ getVal :: Maybe Volume -> Monitor (Maybe Integer)
+ getVal Nothing = return Nothing
+ getVal (Just v) = io $ getChannel FrontLeft $ value v
+
+ getSw :: Maybe Switch -> Monitor (Maybe Bool)
+ getSw Nothing = return Nothing
+ getSw (Just s) = io $ getChannel FrontLeft s
+
+ getFormatDB :: VolumeOpts -> Maybe Integer -> Monitor String
+ getFormatDB _ Nothing = return unavailable
+ getFormatDB opts (Just d) = formatDb opts d
+
+ getFormatSwitch :: VolumeOpts -> Maybe Bool -> Monitor String
+ getFormatSwitch _ Nothing = return unavailable
+ getFormatSwitch opts (Just sw) = formatSwitch opts sw
+
+ unavailable = "N/A"