summaryrefslogtreecommitdiff
path: root/custom/thinklight.nix
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2016-05-14 18:42:33 +0200
committerGregor Kleen <gkleen@yggdrasil.li>2016-05-14 18:42:33 +0200
commit0798e5f73ce50f342f30be422ac8aba17cdd6528 (patch)
treeb08479cabbea5139b2436bad17c200de68186fa1 /custom/thinklight.nix
parent1fec6b4889ee02fe6002026f39f7d622351aed63 (diff)
downloadnixos-0798e5f73ce50f342f30be422ac8aba17cdd6528.tar
nixos-0798e5f73ce50f342f30be422ac8aba17cdd6528.tar.gz
nixos-0798e5f73ce50f342f30be422ac8aba17cdd6528.tar.bz2
nixos-0798e5f73ce50f342f30be422ac8aba17cdd6528.tar.xz
nixos-0798e5f73ce50f342f30be422ac8aba17cdd6528.zip
thinklight
Diffstat (limited to 'custom/thinklight.nix')
-rw-r--r--custom/thinklight.nix58
1 files changed, 58 insertions, 0 deletions
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 @@
1{ stdenv, haskellPackages, thinklight ? "thinklight", makeWrapper }:
2
3stdenv.mkDerivation {
4 name = "thinklight-0.1";
5 ghc = haskellPackages.ghcWithPackages (self: [self.strict]);
6 outputs = [ "out" ];
7 builder = builtins.toFile "builder.sh" ''
8 source $stdenv/setup
9 mkdir -p $out/bin
10 $ghc/bin/ghc $src -o $out/bin/thinklight
11 wrapProgram $out/bin/thinklight --set THINKLIGHT ${thinklight}
12 '';
13 src = builtins.toFile "source.hs" ''
14 import Control.Monad (sequence)
15 import System.IO.Error
16 import System.Environment
17 import System.FilePath
18 import Control.Concurrent (threadDelay)
19 import System.IO.Strict (readFile)
20 import Prelude hiding (readFile)
21 import Data.List (intersperse)
22
23 data Mode = On | Off | Toggle | Blink deriving (Read, Show, Eq)
24
25 main :: IO ()
26 main = do
27 args <- getArgs
28 let mode = if length args >= 1 then read $ head args else Toggle
29 sequence $ map (\g -> catchIOError g (\e -> if isDoesNotExistError e then return () else ioError e)) [thinklight mode]
30 return ()
31
32 findBase :: IO FilePath
33 findBase = do
34 env <- fromMaybe "thinklight" <$> lookupEnv "THINKLIGHT"
35 return $ "/sys/class/leds/tpacpi::" ++ env
36
37 readMax, readCurrent :: IO String
38 readMax = readFile =<< ((</> "max_brightness") <$> findBase)
39 readCurrent = readFile =<< ((</> "brightness") <$> findBase)
40 writeCurrent :: String -> IO ()
41 writeCurrent str = flip writeFile str =<< ((</> "brightness") <$> findBase)
42
43 thinklight :: Mode -> IO ()
44 thinklight On = readMax >>= writeCurrent . show
45 thinklight Off = writeCurrent "0"
46 thinklight Toggle = do
47 c <- readCurrent
48 m <- readMax
49 let m' = read m :: Int
50 let c' = read c :: Int
51 writeCurrent $ show $ if c' /= 0 then 0 else m'
52 thinklight Blink = do
53 args <- getArgs
54 thinklight Toggle
55 sequence $ intersperse (thinklight Toggle) $ map (\i -> threadDelay $ (read i) * 10^3) (tail args)
56 thinklight Toggle
57 '';
58}