summaryrefslogtreecommitdiffhomepage
path: root/src/Plugins
diff options
context:
space:
mode:
authorJose Antonio Ortega Ruiz <jao@gnu.org>2011-11-08 14:40:59 +0100
committerJose Antonio Ortega Ruiz <jao@gnu.org>2011-11-08 14:40:59 +0100
commitb08c8830e1db1942dd0a1b00fea4e195e4c75a14 (patch)
treecef924ab15773835a120b001750020028f3175a9 /src/Plugins
parent2527948d197fbcb99bd19d3bbb7ec85440598060 (diff)
downloadxmobar-b08c8830e1db1942dd0a1b00fea4e195e4c75a14.tar.gz
xmobar-b08c8830e1db1942dd0a1b00fea4e195e4c75a14.tar.bz2
A new attempt at fixing battery monitor's bailouts on awake
Diffstat (limited to 'src/Plugins')
-rw-r--r--src/Plugins/Monitors/Batt.hs30
1 files changed, 18 insertions, 12 deletions
diff --git a/src/Plugins/Monitors/Batt.hs b/src/Plugins/Monitors/Batt.hs
index dfa8075..ccb75eb 100644
--- a/src/Plugins/Monitors/Batt.hs
+++ b/src/Plugins/Monitors/Batt.hs
@@ -13,11 +13,14 @@
--
-----------------------------------------------------------------------------
+{-# LANGUAGE BangPatterns #-}
+
module Plugins.Monitors.Batt ( battConfig, runBatt, runBatt' ) where
import Control.Exception (SomeException, handle)
import Plugins.Monitors.Common
import System.FilePath ((</>))
+import System.IO (IOMode(ReadMode), hGetLine, withFile)
import System.Posix.Files (fileExist)
import System.Console.GetOpt
@@ -85,17 +88,21 @@ data Files = Files
} | NoFiles
data Battery = Battery
- { full :: Float
- , now :: Float
- , voltage :: Float
- , current :: Float
+ { full :: !Float
+ , now :: !Float
+ , voltage :: !Float
+ , current :: !Float
}
+safeFileExist :: String -> IO Bool
+safeFileExist f = handle noErrors $ fileExist f
+ where noErrors = const (return False) :: SomeException -> IO Bool
+
batteryFiles :: String -> IO Files
batteryFiles bat =
- do is_charge <- fileExist $ prefix </> "charge_now"
- is_energy <- fileExist $ prefix </> "energy_now"
- is_current <- fileExist $ prefix </> "current_now"
+ do is_charge <- safeFileExist $ prefix </> "charge_now"
+ is_energy <- safeFileExist $ prefix </> "energy_now"
+ is_current <- safeFileExist $ prefix </> "current_now"
let cf = if is_current then "current_now" else "power_now"
return $ case (is_charge, is_energy) of
(True, _) -> files "charge" cf
@@ -108,10 +115,9 @@ batteryFiles bat =
, fVoltage = prefix </> "voltage_now" }
haveAc :: FilePath -> IO Bool
-haveAc f = do
- handle onError (fmap ((== "1\n") . B.unpack) (B.readFile ofile))
- where ofile = sysDir </> f
- onError = const (return False) :: SomeException -> IO Bool
+haveAc f =
+ handle onError $ withFile (sysDir </> f) ReadMode (fmap (== "1") . hGetLine)
+ where onError = const (return False) :: SomeException -> IO Bool
readBattery :: Files -> IO Battery
readBattery NoFiles = return $ Battery 0 0 0 0
@@ -125,7 +131,7 @@ readBattery files =
(c / 1000000) -- volts
(if c > 0 then (d / c) else -1) -- amperes
where grab f = handle onError (fmap (read . B.unpack) $ B.readFile f)
- onError = const (return 0) :: SomeException -> IO Float
+ onError = const (return (-1)) :: SomeException -> IO Float
readBatteries :: BattOpts -> [Files] -> IO Result
readBatteries opts bfs =