From 89bee7551a7845eb974bac2346a522348fcab6ec Mon Sep 17 00:00:00 2001 From: Martin Perner Date: Mon, 4 Apr 2011 21:33:22 +0200 Subject: Added brightness monitor --- README | 18 +++++++ src/Plugins/Monitors.hs | 4 ++ src/Plugins/Monitors/Bright.hs | 118 +++++++++++++++++++++++++++++++++++++++++ xmobar.cabal | 3 +- 4 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 src/Plugins/Monitors/Bright.hs diff --git a/README b/README index 925c1aa..9294586 100644 --- a/README +++ b/README @@ -677,6 +677,24 @@ Monitors have default aliases. distribution can be used to set the given property from the output of any other program or script. +`Brightness Args RefreshRate` + +- Aliases to `bright` +- Args: default monitor arguments (see below), plus the following specif ones: + - `-D`: directory in `/sys/class/backlight/` with files in it + (default: "acpi_video0") + - `-C`: file with the current brightness (default: + actual_brightness) + - `-M`: file with the maximum brightness (default: + max_brigtness) +- Variables that can be used with the `-t`/`--template` argument: + `hbar`, `percent`, `bar` +- Default template: `%` +- Example: + + Run Brightness ["-t", ""] 60 + + [samples/xmonadpropwrite.hs script]: https://github.com/jaor/xmobar/raw/master/samples/xmonadpropwrite.hs ## Monitor Plugins Commands Arguments diff --git a/src/Plugins/Monitors.hs b/src/Plugins/Monitors.hs index c4688a7..5e1cb62 100644 --- a/src/Plugins/Monitors.hs +++ b/src/Plugins/Monitors.hs @@ -27,6 +27,7 @@ import Plugins.Monitors.Swap import Plugins.Monitors.Cpu import Plugins.Monitors.MultiCpu import Plugins.Monitors.Batt +import Plugins.Monitors.Bright import Plugins.Monitors.Thermal import Plugins.Monitors.ThermalZone import Plugins.Monitors.CpuFreq @@ -56,6 +57,7 @@ data Monitors = Weather Station Args Rate | Cpu Args Rate | MultiCpu Args Rate | Battery Args Rate + | Brightness Args Rate | CpuFreq Args Rate | CoreTemp Args Rate | TopProc Args Rate @@ -93,6 +95,7 @@ instance Exec Monitors where alias (MultiCpu _ _) = "multicpu" alias (Battery _ _) = "battery" alias (BatteryP _ _ _)= "battery" + alias (Brightness _ _) = "bright" alias (CpuFreq _ _) = "cpufreq" alias (TopProc _ _) = "top" alias (TopMem _ _) = "topmem" @@ -122,6 +125,7 @@ instance Exec Monitors where start (Swap a r) = runM a swapConfig runSwap r start (Battery a r) = runM a battConfig runBatt r start (BatteryP s a r) = runM a battConfig (runBatt' s) r + start (Brightness a r) = runM a brightConfig runBright r start (CpuFreq a r) = runM a cpuFreqConfig runCpuFreq r start (CoreTemp a r) = runM a coreTempConfig runCoreTemp r start (DiskU s a r) = runM a diskUConfig (runDiskU s) r diff --git a/src/Plugins/Monitors/Bright.hs b/src/Plugins/Monitors/Bright.hs new file mode 100644 index 0000000..36009a6 --- /dev/null +++ b/src/Plugins/Monitors/Bright.hs @@ -0,0 +1,118 @@ +----------------------------------------------------------------------------- +---- | +---- Module : Plugins.Monitors.Birght +---- Copyright : (c) Martin Perner +---- License : BSD-style (see LICENSE) +---- +---- Maintainer : Martin Perner +---- Stability : unstable +---- Portability : unportable +---- +---- A screen brightness monitor for Xmobar +---- +------------------------------------------------------------------------------- + +module Plugins.Monitors.Bright (brightConfig, runBright) where + +import Plugins.Monitors.Common +import qualified Data.ByteString.Lazy.Char8 as B +import Data.Char +import System.FilePath (()) +import System.Posix.Files (fileExist) +import System.Console.GetOpt + +data BrightOpts = BrightOpts + { subDir :: String + , currBright :: String + , maxBright :: String + } + +defaultOpts :: BrightOpts +defaultOpts = BrightOpts + { subDir = "acpi_video0" + , currBright = "actual_brightness" + , maxBright = "max_brightness" + } + +options :: [OptDescr (BrightOpts -> BrightOpts)] +options = + [ Option "D" ["device"] (ReqArg (\x o -> o { subDir = x }) "") "" + , Option "C" ["curr"] (ReqArg (\x o -> o { currBright = x }) "") "" + , Option "M" ["max"] (ReqArg (\x o -> o { maxBright = x }) "") "" + ] + +-- from Batt.hs +parseOpts :: [String] -> IO BrightOpts +parseOpts argv = + case getOpt Permute options argv of + (o, _, []) -> return $ foldr id defaultOpts o + (_, _, errs) -> ioError . userError $ concat errs + +sysDir :: FilePath +sysDir = "/sys/class/backlight/" + +brightConfig :: IO MConfig +brightConfig = mkMConfig + "%" -- template + ["hbar", "percent", "bar"] -- replacements + +data Files = Files + { fCurr :: String + , fMax :: String + } | NoFiles + +brightFiles :: BrightOpts -> IO Files +brightFiles opts = + do + is_curr <- fileExist $ (fCurr files) + is_max <- fileExist $ (fCurr files) + if is_curr && is_max + then return files + else return NoFiles + where + prefix = sysDir (subDir opts) + files = Files { fCurr = prefix (currBright opts) + , fMax = prefix (maxBright opts) + } + + +runBright :: [String] -> Monitor String +runBright args = do + opts <- io $ parseOpts args + f <- io $ brightFiles opts + c <- io $ readBright f + case f of + NoFiles -> return "hurz" + _ -> do x <- fmtPercent c + parseTemplate (x) + where + fmtPercent :: Float -> Monitor [String] + fmtPercent c = do + r <- showHorizontalBar (100 * c) + s <- showPercentWithColors c + t <- showPercentBar (100 * c) c + return [r,s,t] + +readBright :: Files -> IO Float +readBright NoFiles = return 0 +readBright files = + do + currVal<- grab $ (fCurr files) + maxVal <- grab $ (fMax files) + return $ (currVal / maxVal) + where + grab = fmap (read . B.unpack) . B.readFile + + +showHorizontalBar :: Float -> Monitor String +showHorizontalBar x = do + return $ [convert x] + where + convert :: Float -> Char + convert val + | t <= 9600 = ' ' + | t > 9608 = chr 9608 + | otherwise = chr t + where + -- we scale from 0 to 100, we have 8 slots (9 elements), 100/8 = 12 + t = 9600 + ((round val) `div` 12) diff --git a/xmobar.cabal b/xmobar.cabal index 5484eb5..cc9e016 100644 --- a/xmobar.cabal +++ b/xmobar.cabal @@ -71,7 +71,8 @@ executable xmobar Plugins.Monitors.MultiCpu, Plugins.Monitors.Net, Plugins.Monitors.Swap, Plugins.Monitors.Thermal, Plugins.Monitors.ThermalZone, Plugins.Monitors.Top, - Plugins.Monitors.Uptime, Plugins.Monitors.Weather + Plugins.Monitors.Uptime, Plugins.Monitors.Weather, + Plugins.Monitors.Bright ghc-prof-options: -prof -auto-all -- cgit v1.2.3 From 41fa766599e061dec240aced32b2444e631d6b60 Mon Sep 17 00:00:00 2001 From: Martin Perner Date: Tue, 5 Apr 2011 23:54:02 +0200 Subject: Bright Monitor: Didn't made use of -S --- README | 2 +- src/Plugins/Monitors/Bright.hs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README b/README index 9294586..f8ecc83 100644 --- a/README +++ b/README @@ -689,7 +689,7 @@ Monitors have default aliases. max_brigtness) - Variables that can be used with the `-t`/`--template` argument: `hbar`, `percent`, `bar` -- Default template: `%` +- Default template: `` - Example: Run Brightness ["-t", ""] 60 diff --git a/src/Plugins/Monitors/Bright.hs b/src/Plugins/Monitors/Bright.hs index 36009a6..4cbbfa4 100644 --- a/src/Plugins/Monitors/Bright.hs +++ b/src/Plugins/Monitors/Bright.hs @@ -53,7 +53,7 @@ sysDir = "/sys/class/backlight/" brightConfig :: IO MConfig brightConfig = mkMConfig - "%" -- template + "" -- template ["hbar", "percent", "bar"] -- replacements data Files = Files -- cgit v1.2.3 From 63847f5b585e0fb5f19812ce9b5ddaa257b359b7 Mon Sep 17 00:00:00 2001 From: Martin Perner Date: Tue, 5 Apr 2011 23:56:29 +0200 Subject: Bright Monitor: Use 0 if reading the file fails Thx to mathstuf for the hint --- src/Plugins/Monitors/Bright.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugins/Monitors/Bright.hs b/src/Plugins/Monitors/Bright.hs index 4cbbfa4..499e5bc 100644 --- a/src/Plugins/Monitors/Bright.hs +++ b/src/Plugins/Monitors/Bright.hs @@ -101,7 +101,7 @@ readBright files = maxVal <- grab $ (fMax files) return $ (currVal / maxVal) where - grab = fmap (read . B.unpack) . B.readFile + grab f = catch (fmap (read . B.unpack) $ B.readFile f)(\_ -> return 0) showHorizontalBar :: Float -> Monitor String -- cgit v1.2.3