From fdc91a62c37bd475440bee275e6b0329833f6af4 Mon Sep 17 00:00:00 2001 From: Dmitry Malikov Date: Mon, 4 Feb 2013 02:35:43 +0400 Subject: Remove unsafe fromJust from Volume plugin --- src/Plugins/Monitors/Volume.hs | 67 ++++++++++++++++++++++++++++++------------ 1 file 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" -- cgit v1.2.3