------------------------------------------------------------------------------
-- |
-- 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 = [String] -> String
forall a. HasCallStack => [a] -> a
head [String]
args
        file :: String
file = String
"/sys/class/thermal/thermal_zone" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
zone String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"/temp"
        handleIOError :: IOException -> IO (Maybe B.ByteString)
        handleIOError :: IOException -> IO (Maybe ByteString)
handleIOError IOException
_ = Maybe ByteString -> IO (Maybe ByteString)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe ByteString
forall a. Maybe a
Nothing
        parse :: ByteString -> ReaderT MConfig IO Int
parse = Int -> ReaderT MConfig IO Int
forall a. a -> ReaderT MConfig IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> ReaderT MConfig IO Int)
-> (ByteString -> Int) -> ByteString -> ReaderT MConfig IO Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> Int
forall a. Read a => String -> a
read :: String -> Int) (String -> Int) -> (ByteString -> String) -> ByteString -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> String
B.unpack
    exists <- IO Bool -> Monitor Bool
forall a. IO a -> Monitor a
io (IO Bool -> Monitor Bool) -> IO Bool -> Monitor Bool
forall a b. (a -> b) -> a -> b
$ String -> IO Bool
fileExist String
file
    if exists
      then do contents <- io $ catch (fmap Just $ B.readFile file) handleIOError
              case contents of
                Just ByteString
d -> do
                  mdegrees <- ByteString -> ReaderT MConfig IO Int
parse ByteString
d
                  temp <- showWithColors show (mdegrees `quot` 1000)
                  parseTemplate [ temp ]
                Maybe ByteString
Nothing -> Selector String -> Monitor String
forall a. Selector a -> Monitor a
getConfigValue Selector String
naString
      else getConfigValue naString