------------------------------------------------------------------------------
-- |
-- Module       :  Plugins.Monitors.ThermalZone
-- Copyright    :  (c) 2011, 2013 Jose Antonio Ortega Ruiz
-- License      :  BSD3-style (see LICENSE)
--
-- Maintainer   :  jao@gnu.org
-- Stability    :  unstable
-- Portability  :  portable
-- Created      :  Fri Feb 25, 2011 03:18
--
--
-- A thermal zone plugin based on the sysfs linux interface.
-- See http://kernel.org/doc/Documentation/thermal/sysfs-api.txt
--
------------------------------------------------------------------------------

module Xmobar.Plugins.Monitors.ThermalZone (thermalZoneConfig, runThermalZone) where

import Xmobar.Plugins.Monitors.Common

import System.Posix.Files (fileExist)
import Control.Exception (IOException, catch)
import qualified Data.ByteString.Char8 as B

-- | Default thermal configuration.
thermalZoneConfig :: IO MConfig
thermalZoneConfig :: IO MConfig
thermalZoneConfig = String -> [String] -> IO MConfig
mkMConfig String
"<temp>C" [String
"temp"]

-- | Retrieves thermal information. Argument is name of thermal
-- directory in \/sys\/clas\/thermal. Returns the monitor string
-- parsed according to template (either default or user specified).
runThermalZone :: [String] -> Monitor String
runThermalZone :: [String] -> Monitor String
runThermalZone [String]
args = do
    let zone :: String
zone = forall a. [a] -> a
head [String]
args
        file :: String
file = String
"/sys/class/thermal/thermal_zone" forall a. [a] -> [a] -> [a]
++ String
zone forall a. [a] -> [a] -> [a]
++ String
"/temp"
        handleIOError :: IOException -> IO (Maybe B.ByteString)
        handleIOError :: IOException -> IO (Maybe ByteString)
handleIOError IOException
_ = forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
        parse :: ByteString -> ReaderT MConfig IO Int
parse = forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Read a => String -> a
read :: String -> Int) forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> String
B.unpack
    Bool
exists <- forall a. IO a -> Monitor a
io forall a b. (a -> b) -> a -> b
$ String -> IO Bool
fileExist String
file
    if Bool
exists
      then do Maybe ByteString
contents <- forall a. IO a -> Monitor a
io forall a b. (a -> b) -> a -> b
$ forall e a. Exception e => IO a -> (e -> IO a) -> IO a
catch (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ String -> IO ByteString
B.readFile String
file) IOException -> IO (Maybe ByteString)
handleIOError
              case Maybe ByteString
contents of
                Just ByteString
d -> do
                  Int
mdegrees <- ByteString -> ReaderT MConfig IO Int
parse ByteString
d
                  String
temp <- forall a. (Num a, Ord a) => (a -> String) -> a -> Monitor String
showWithColors forall a. Show a => a -> String
show (Int
mdegrees forall a. Integral a => a -> a -> a
`quot` Int
1000)
                  [String] -> Monitor String
parseTemplate [ String
temp ]
                Maybe ByteString
Nothing -> forall a. Selector a -> Monitor a
getConfigValue MConfig -> IORef String
naString
      else forall a. Selector a -> Monitor a
getConfigValue MConfig -> IORef String
naString