summaryrefslogtreecommitdiffhomepage
path: root/Monitors
diff options
context:
space:
mode:
authorAndrea Rossato <andrea.rossato@ing.unitn.it>2007-07-05 15:17:49 +0200
committerAndrea Rossato <andrea.rossato@ing.unitn.it>2007-07-05 15:17:49 +0200
commit8c558c537ca9a201c3aafbbde77e96128feaebdd (patch)
treecac1a3bf953440714d72455ba316b2ba29b3cc3e /Monitors
parent28e09ba6aa2b00df1470660f5cde2a91a26f9ce7 (diff)
downloadxmobar-8c558c537ca9a201c3aafbbde77e96128feaebdd.tar.gz
xmobar-8c558c537ca9a201c3aafbbde77e96128feaebdd.tar.bz2
added a battery monitor
darcs-hash:20070705131749-d6583-54986e62ce9f3afc6c5bf0324fbc458991c5d523.gz
Diffstat (limited to 'Monitors')
-rw-r--r--Monitors/Batt.hs85
1 files changed, 85 insertions, 0 deletions
diff --git a/Monitors/Batt.hs b/Monitors/Batt.hs
new file mode 100644
index 0000000..5e89f9a
--- /dev/null
+++ b/Monitors/Batt.hs
@@ -0,0 +1,85 @@
+-----------------------------------------------------------------------------
+-- |
+-- Module : Monitors.Batt
+-- Copyright : (c) Andrea Rossato
+-- License : BSD-style (see LICENSE)
+--
+-- Maintainer : Andrea Rossato <andrea.rossato@unibz.it>
+-- Stability : unstable
+-- Portability : unportable
+--
+-- A battery monitor for XMobar
+--
+-----------------------------------------------------------------------------
+
+module Main where
+
+import Data.IORef
+import qualified Data.ByteString.Lazy.Char8 as B
+import System.Posix.Files
+
+import Monitors.Common
+
+monitorConfig :: IO MConfig
+monitorConfig =
+ do lc <- newIORef "#FF0000"
+ l <- newIORef 25
+ nc <- newIORef "#FF0000"
+ h <- newIORef 75
+ hc <- newIORef "#00FF00"
+ t <- newIORef "Batt: <left>"
+ p <- newIORef package
+ u <- newIORef ""
+ a <- newIORef []
+ e <- newIORef ["left"]
+ return $ MC nc l lc h hc t p u a e
+
+fileB1 :: (String, String)
+fileB1 = ("/proc/acpi/battery/BAT1/info", "/proc/acpi/battery/BAT1/state")
+
+fileB2 :: (String, String)
+fileB2 = ("/proc/acpi/battery/BAT2/info", "/proc/acpi/battery/BAT2/state")
+
+checkFileBatt :: (String, String) -> IO Bool
+checkFileBatt (i,_) =
+ fileExist i
+
+readFileBatt :: (String, String) -> IO (B.ByteString, B.ByteString)
+readFileBatt (i,s) =
+ do a <- B.readFile i
+ b <- B.readFile s
+ return (a,b)
+
+parseBATT :: IO Float
+parseBATT =
+ do (a1,b1) <- readFileBatt fileB1
+ c <- checkFileBatt fileB2
+ let sp p s = read $ stringParser p s
+ (fu, pr) = (sp (3,2) a1, sp (2,4) b1)
+ case c of
+ True -> do (a2,b2) <- readFileBatt fileB1
+ let full = fu + (sp (3,2) a2)
+ present = pr + (sp (2,4) b2)
+ return $ present / full
+ _ -> return $ pr / fu
+
+
+formatBatt :: Float -> Monitor [String]
+formatBatt x =
+ do let f s = floatToPercent (s / 100)
+ l <- showWithColors f (x * 100)
+ return [l]
+
+package :: String
+package = "xmb-batt"
+
+runBatt :: [String] -> Monitor String
+runBatt _ =
+ do c <- io $ parseBATT
+ l <- formatBatt c
+ parseTemplate l
+
+main :: IO ()
+main =
+ do let af = runBatt []
+ runMonitor monitorConfig af runBatt