From c9ca8953c6f4099e5c0504ce0e31b016d878989c Mon Sep 17 00:00:00 2001 From: Andrea Rossato Date: Wed, 10 Feb 2010 12:45:05 +0100 Subject: Batt: read energy_ if charge_ is not present Ignore-this: 76a24d0c8d89c239a2c9d8136f77f409ccb34f1a darcs-hash:20100210114505-d6583-ffe623e21b431c2d4d88b79dfb92a1f3af8ed9d8.gz --- Plugins/Monitors/Batt.hs | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/Plugins/Monitors/Batt.hs b/Plugins/Monitors/Batt.hs index 93fddf9..03becaa 100644 --- a/Plugins/Monitors/Batt.hs +++ b/Plugins/Monitors/Batt.hs @@ -26,25 +26,36 @@ battConfig = mkMConfig "Batt: " -- template ["left","status"] -- available replacements -file2batfile :: String -> (String, String, String) -file2batfile s = ( s' ++ "/charge_full" - , s' ++ "/charge_now" - , s' ++ "/status") +type File = (String, String) + +file2batfile :: String -> (File, File) +file2batfile s = ( (s' ++ "/charge_full", s' ++ "/energy_full") + , (s' ++ "/charge_now" , s' ++ "/energy_now" ) + ) where s' = "/sys/class/power_supply/" ++ s -readFileBatt :: (String, String, String) -> IO (String, String, String) -readFileBatt (f,n,s) = - do a <- rf f - b <- rf n - c <- rf s +readFileBatt :: (File, File) -> IO (String, String, String) +readFileBatt (f,n) = + do a <- rf f + b <- rf n + ac <- fileExist "/sys/class/power_supply/AC0/online" + c <- if not ac + then return [] + else do s <- B.unpack `fmap` catRead "/sys/class/power_supply/AC0/online" + return $ if s == "1\n" then "On" else"Off" return (a,b,c) where rf file = do - fe <- fileExist file - if fe then B.unpack `fmap` catRead file else return [] + fe <- fileExist (fst file) + if fe + then B.unpack `fmap` catRead (fst file) + else do fe' <- fileExist (snd file) + if fe' + then B.unpack `fmap` catRead (snd file) + else return [] -parseBATT :: [(String, String, String)] -> IO Batt +parseBATT :: [(File,File)] -> IO Batt parseBATT bfs = - do [(a0,b0,c0),(a1,b1,_),(a2,b2,_)] <- mapM readFileBatt (take 3 $ bfs ++ repeat ("","","")) + do [(a0,b0,c0),(a1,b1,_),(a2,b2,_)] <- mapM readFileBatt (take 3 $ bfs ++ repeat (("",""),("",""))) let read' s = if s == [] then 0 else read s left = (read' b0 + read' b1 + read' b2) / (read' a0 + read' a1 + read' a2) --present / full return $ if isNaN left then NA else Batt left c0 -- cgit v1.2.3