From 0798e5f73ce50f342f30be422ac8aba17cdd6528 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sat, 14 May 2016 18:42:33 +0200 Subject: thinklight --- custom/thinklight.nix | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 custom/thinklight.nix (limited to 'custom') diff --git a/custom/thinklight.nix b/custom/thinklight.nix new file mode 100644 index 00000000..d3d4e7ca --- /dev/null +++ b/custom/thinklight.nix @@ -0,0 +1,58 @@ +{ stdenv, haskellPackages, thinklight ? "thinklight", makeWrapper }: + +stdenv.mkDerivation { + name = "thinklight-0.1"; + ghc = haskellPackages.ghcWithPackages (self: [self.strict]); + outputs = [ "out" ]; + builder = builtins.toFile "builder.sh" '' + source $stdenv/setup + mkdir -p $out/bin + $ghc/bin/ghc $src -o $out/bin/thinklight + wrapProgram $out/bin/thinklight --set THINKLIGHT ${thinklight} + ''; + src = builtins.toFile "source.hs" '' + import Control.Monad (sequence) + import System.IO.Error + import System.Environment + import System.FilePath + import Control.Concurrent (threadDelay) + import System.IO.Strict (readFile) + import Prelude hiding (readFile) + import Data.List (intersperse) + + data Mode = On | Off | Toggle | Blink deriving (Read, Show, Eq) + + main :: IO () + main = do + args <- getArgs + let mode = if length args >= 1 then read $ head args else Toggle + sequence $ map (\g -> catchIOError g (\e -> if isDoesNotExistError e then return () else ioError e)) [thinklight mode] + return () + + findBase :: IO FilePath + findBase = do + env <- fromMaybe "thinklight" <$> lookupEnv "THINKLIGHT" + return $ "/sys/class/leds/tpacpi::" ++ env + + readMax, readCurrent :: IO String + readMax = readFile =<< (( "max_brightness") <$> findBase) + readCurrent = readFile =<< (( "brightness") <$> findBase) + writeCurrent :: String -> IO () + writeCurrent str = flip writeFile str =<< (( "brightness") <$> findBase) + + thinklight :: Mode -> IO () + thinklight On = readMax >>= writeCurrent . show + thinklight Off = writeCurrent "0" + thinklight Toggle = do + c <- readCurrent + m <- readMax + let m' = read m :: Int + let c' = read c :: Int + writeCurrent $ show $ if c' /= 0 then 0 else m' + thinklight Blink = do + args <- getArgs + thinklight Toggle + sequence $ intersperse (thinklight Toggle) $ map (\i -> threadDelay $ (read i) * 10^3) (tail args) + thinklight Toggle + ''; +} -- cgit v1.2.3