Advertisement
ostankino_tower

Brighness change - Haskell

Nov 28th, 2020
1,213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# LANGUAGE OverloadedStrings #-}
  2. module Main (main) where
  3.  
  4. import System.IO (stdin, hReady, hSetEcho, hSetBuffering, BufferMode(NoBuffering))
  5. import Data.Text.Read (decimal)
  6. import qualified Data.Text as T
  7. import qualified Data.Text.IO as TIO
  8.  
  9. data Action = BIncrease | BDecrease | Noop
  10.  
  11. currentHwBacklightPath = "/sys/class/backlight/intel_backlight/brightness"
  12. maxHwBacklightPath = "/sys/class/backlight/intel_backlight/max_brightness"
  13. maxLgBrightness = 20 -- Logical brightness, number of steps
  14.  
  15. getKeyCode :: IO [Char]
  16. getKeyCode = reverse <$> getKeyCode' ""
  17.  where
  18.    getKeyCode' chars = do
  19.       char <- getChar
  20.       more <- hReady stdin
  21.       (if more then getKeyCode' else return) (char:chars)
  22.  
  23. getAction :: IO Action
  24. getAction = do
  25.  hSetBuffering stdin NoBuffering
  26.  hSetEcho stdin False
  27.  key <- getKeyCode
  28.  return $ case key of
  29.    "\ESC[A" -> BIncrease -- Up arrow
  30.    "\ESC[B" -> BDecrease -- Down arrow
  31.    "\ESC[C" -> BIncrease -- Right arrow
  32.    "\ESC[D" -> BDecrease -- Left arrow
  33.    _        -> Noop
  34.  
  35. readFileInt0 :: FilePath -> IO Int
  36. readFileInt0 path = textToInt0 <$> TIO.readFile path
  37.  where textToInt0 text = either (const 0) fst (decimal text)
  38.  
  39. writeFileInt :: FilePath -> Int -> IO ()
  40. writeFileInt path val = TIO.writeFile path (intToText val)
  41.  where intToText = T.pack . show
  42.  
  43. getBrightness :: IO Int
  44. getBrightness = do
  45.  maxHwBrightness <- readFileInt0 maxHwBacklightPath
  46.  curHwBrightness <- readFileInt0 currentHwBacklightPath
  47.  
  48.  let curLgBrightness = hwToLg maxHwBrightness curHwBrightness
  49.  return $ max 1 curLgBrightness -- never return 0
  50.  
  51. setBrightness :: Int -> IO ()
  52. setBrightness newLgBrightness = do
  53.  maxHwBrightness <- readFileInt0 maxHwBacklightPath
  54.  
  55.  let newHwBrightness = lgToHw maxHwBrightness newLgBrightness
  56.  if newLgBrightness >= 1 && newLgBrightness <= maxLgBrightness
  57.    then do writeFileInt currentHwBacklightPath newHwBrightness
  58.            putStrLn $ "New brightness: " ++ (show newLgBrightness)
  59.    else return ()
  60.  
  61. hwToLg = convert logBase
  62. lgToHw = convert (**)
  63.  
  64. -- op must be either logBase or (**)
  65. convert op maxHw val =
  66.  let
  67.    base = (fromIntegral maxHw) ** (1.0 / (fromIntegral  maxLgBrightness))
  68.  in
  69.    round $ op base (fromIntegral val)
  70.  
  71. main = do
  72.  action <- getAction
  73.  curBrightness <- getBrightness
  74.  case action of
  75.    BIncrease -> setBrightness (curBrightness + 1) >> main
  76.    BDecrease -> setBrightness (curBrightness - 1) >> main
  77.    Noop -> return ()
  78.  
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement