    {-# LANGUAGE TupleSections #-}
    -----------------------------------------------------------------------------
    -- |
    -- Module      : System.Taffybar.Information.CPU2
    -- Copyright   : (c) José A. Romero L.
    -- License     : BSD3-style (see LICENSE)
    --
    -- Maintainer  : José A. Romero L. <escherdragon@gmail.com>
    -- Stability   : unstable
    -- Portability : unportable
    --
    -- Provides information about used CPU times, obtained from parsing the
    -- @\/proc\/stat@ file using some of the facilities included in the
    -- "System.Taffybar.Information.StreamInfo" module.
    -- And also provides information about the temperature of cores.
    -- (Now supports only physical cpu).
    --
    -----------------------------------------------------------------------------

    module System.Taffybar.Information.CPU2 where

    import Control.Monad
    import Data.List
    import Data.Maybe
    import Safe
    import System.Directory
    import System.FilePath
    import System.Taffybar.Information.StreamInfo

    -- | Returns a list of 5 to 7 elements containing all the values available for
    -- the given core (or all of them aggregated, if "cpu" is passed).
    getCPUInfo :: String -> IO [Int]
    getCPUInfo :: String -> IO [Int]
    getCPUInfo = String -> (String -> [(String, [Int])]) -> String -> IO [Int]
    forall a. String -> (String -> [(String, [a])]) -> String -> IO [a]
    getParsedInfo "/proc/stat" String -> [(String, [Int])]
    parse

    parse :: String -> [(String, [Int])]
    parse :: String -> [(String, [Int])]
    parse = (String -> Maybe (String, [Int])) -> [String] -> [(String, [Int])]
    forall a b. (a -> Maybe b) -> [a] -> [b]
    mapMaybe ([String] -> Maybe (String, [Int])
    tuplize ([String] -> Maybe (String, [Int]))
    -> (String -> [String]) -> String -> Maybe (String, [Int])
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . String -> [String]
    words) ([String] -> [(String, [Int])])
    -> (String -> [String]) -> String -> [(String, [Int])]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (String -> Bool) -> [String] -> [String]
    forall a. (a -> Bool) -> [a] -> [a]
    filter (\x :: String
    x -> Int -> String -> String
    forall a. Int -> [a] -> [a]
    take 3 String
    x String -> String -> Bool
    forall a. Eq a => a -> a -> Bool
    == "cpu") ([String] -> [String])
    -> (String -> [String]) -> String -> [String]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . String -> [String]
    lines

    tuplize :: [String] -> Maybe (String, [Int])
    tuplize :: [String] -> Maybe (String, [Int])
    tuplize s :: [String]
    s = do
      String
    cpu <- [String]
    s [String] -> Int -> Maybe String
    forall a. [a] -> Int -> Maybe a
    `atMay` 0
      (String, [Int]) -> Maybe (String, [Int])
    forall (m :: * -> *) a. Monad m => a -> m a
    return (String
    cpu, (String -> Int) -> [String] -> [Int]
    forall a b. (a -> b) -> [a] -> [b]
    map (Int -> String -> Int
    forall a. Read a => a -> String -> a
    readDef (-1)) ([String] -> [String]
    forall a. [a] -> [a]
    tailSafe [String]
    s))

    -- | Returns a two-element list containing relative system and user times
    -- calculated using two almost simultaneous samples of the @\/proc\/stat@ file
    -- for the given core (or all of them aggregated, if \"cpu\" is passed).
    getCPULoad :: String -> IO [Double]
    getCPULoad :: String -> IO [Double]
    getCPULoad cpu :: String
    cpu = do
      [Double]
    load <- Double -> IO [Int] -> IO [Double]
    forall a b. (Integral a, RealFloat b) => b -> IO [a] -> IO [b]
    getLoad 0.05 (IO [Int] -> IO [Double]) -> IO [Int] -> IO [Double]
    forall a b. (a -> b) -> a -> b
    $ String -> IO [Int]
    getCPUInfo String
    cpu
      case [Double]
    load of
        l0 :: Double
    l0:l1 :: Double
    l1:l2 :: Double
    l2:_ -> [Double] -> IO [Double]
    forall (m :: * -> *) a. Monad m => a -> m a
    return [ Double
    l0 Double -> Double -> Double
    forall a. Num a => a -> a -> a
    + Double
    l1, Double
    l2 ]
        _ -> [Double] -> IO [Double]
    forall (m :: * -> *) a. Monad m => a -> m a
    return []

    -- | Get the directory in which core temperature files are kept.
    getCPUTemperatureDirectory :: IO FilePath
    getCPUTemperatureDirectory :: IO String
    getCPUTemperatureDirectory =
      (String
    baseDir String -> String -> String
    </>) (String -> String) -> ([String] -> String) -> [String] -> String
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . String -> Maybe String -> String
    forall a. a -> Maybe a -> a
    fromMaybe "hwmon0" (Maybe String -> String)
    -> ([String] -> Maybe String) -> [String] -> String
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    .
      (String -> Bool) -> [String] -> Maybe String
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
    find (String -> String -> Bool
    forall a. Eq a => [a] -> [a] -> Bool
    isPrefixOf "hwmon")
      ([String] -> String) -> IO [String] -> IO String
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> String -> IO [String]
    listDirectory String
    baseDir
      where baseDir :: String
    baseDir =
              "/"  String -> String -> String
    </> "sys" String -> String -> String
    </> "bus" String -> String -> String
    </> "platform" String -> String -> String
    </>
              "devices" String -> String -> String
    </> "coretemp.0" String -> String -> String
    </> "hwmon"

    readCPUTempFile :: FilePath -> IO Double
    readCPUTempFile :: String -> IO Double
    readCPUTempFile cpuTempFilePath :: String
    cpuTempFilePath = (Double -> Double -> Double
    forall a. Fractional a => a -> a -> a
    / 1000) (Double -> Double) -> (String -> Double) -> String -> Double
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . String -> Double
    forall a. Read a => String -> a
    read (String -> Double) -> IO String -> IO Double
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> String -> IO String
    readFile String
    cpuTempFilePath

    getAllTemperatureFiles :: FilePath -> IO [FilePath]
    getAllTemperatureFiles :: String -> IO [String]
    getAllTemperatureFiles temperaturesDirectory :: String
    temperaturesDirectory =
      (String -> Bool) -> [String] -> [String]
    forall a. (a -> Bool) -> [a] -> [a]
    filter ((Bool -> Bool -> Bool)
    -> (String -> Bool) -> (String -> Bool) -> String -> Bool
    forall (m :: * -> *) a1 a2 r.
    Monad m =>
    (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
    liftM2 Bool -> Bool -> Bool
    (&&) (String -> String -> Bool
    forall a. Eq a => [a] -> [a] -> Bool
    isPrefixOf "temp") (String -> String -> Bool
    forall a. Eq a => [a] -> [a] -> Bool
    isSuffixOf "input")) ([String] -> [String]) -> IO [String] -> IO [String]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$>
             String -> IO [String]
    listDirectory String
    temperaturesDirectory

    getCPUTemperatures :: IO [(String, Double)]
    getCPUTemperatures :: IO [(String, Double)]
    getCPUTemperatures = do
      String
    dir <- IO String
    getCPUTemperatureDirectory
      let mkPair :: String -> IO (String, Double)
    mkPair filename :: String
    filename = (String
    filename,) (Double -> (String, Double)) -> IO Double -> IO (String, Double)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> String -> IO Double
    readCPUTempFile (String
    dir String -> String -> String
    </> String
    filename)
      String -> IO [String]
    getAllTemperatureFiles String
    dir IO [String]
    -> ([String] -> IO [(String, Double)]) -> IO [(String, Double)]
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= (String -> IO (String, Double))
    -> [String] -> IO [(String, Double)]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    mapM String -> IO (String, Double)
    mkPair