about summary refs log tree commit diff
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2017-05-31 05:16:12 +0200
committerProfpatsch <mail@profpatsch.de>2017-05-31 05:16:51 +0200
commitbef643c5a2e9d25fea4210e4b4d58a81ee0f025f (patch)
treee1c38912899cc029e2263040451669b56ea86ea0
parent3753365a719885f3b02aa8a61f8f37fdf7cb5849 (diff)
pkgs/profpatsch/backlight: init
-rw-r--r--machines/profpatsch/katara.nix1
-rwxr-xr-xpkgs/profpatsch/backlight/backlight.py38
-rw-r--r--pkgs/profpatsch/backlight/default.nix17
-rw-r--r--pkgs/profpatsch/default.nix4
4 files changed, 59 insertions, 1 deletions
diff --git a/machines/profpatsch/katara.nix b/machines/profpatsch/katara.nix
index 21103eba..d798a6c8 100644
--- a/machines/profpatsch/katara.nix
+++ b/machines/profpatsch/katara.nix
@@ -169,6 +169,7 @@ in {
       userScripts = with pkgs.vuizvui.profpatsch; [
         display-infos  # show time & battery
         show-qr-code   # display a QR code
+        backlight      # adjust laptop backlight
       ];
       mailPkgs = [
         elinks               # command line browser
diff --git a/pkgs/profpatsch/backlight/backlight.py b/pkgs/profpatsch/backlight/backlight.py
new file mode 100755
index 00000000..d580260a
--- /dev/null
+++ b/pkgs/profpatsch/backlight/backlight.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python3
+# xbacklight uses a linear percentage,
+# but the backlight intensity is actually logarithmic *facepalm*.
+# So we read the current "percentage" given by xbacklight
+# and calculate the next step, base 2.
+
+import subprocess as sub
+import math
+import sys
+
+xbacklight = "xbacklight"
+
+def usage():
+    print("usage: backlight [inc|dec]", file=sys.stderr)
+    sys.exit(1)
+
+# read current value
+current_backlight = float(sub.run(xbacklight, stdout=sub.PIPE).stdout.strip())
+# find the actual value, base 2
+current_val = round(math.sqrt(current_backlight))
+
+if len(sys.argv) == 1: usage()
+else:
+    mode = sys.argv[1]
+
+# modify actual value
+if mode == "inc":
+    new_val = current_val + 1
+elif mode == "dec":
+    new_val = current_val - 1
+else:
+    usage()
+
+# clamp value
+new_backlight = min(10, max(0, new_val))
+
+# pow it again and set
+sub.run([xbacklight, "-set", str(math.pow(new_backlight, 2))])
diff --git a/pkgs/profpatsch/backlight/default.nix b/pkgs/profpatsch/backlight/default.nix
new file mode 100644
index 00000000..d2f40be0
--- /dev/null
+++ b/pkgs/profpatsch/backlight/default.nix
@@ -0,0 +1,17 @@
+{ stdenv, python3, xbacklight}:
+
+stdenv.mkDerivation rec {
+  name = "backlight";
+
+  src = ./backlight.py;
+  phases = [ "installPhase" "fixupPhase" ];
+
+  buildInputs = [ python3 ];
+
+  installPhase = ''
+    install -D ${src} $out/bin/backlight
+    substituteInPlace $out/bin/backlight \
+      --replace '"xbacklight"' '"${xbacklight}/bin/xbacklight"'
+  '';
+
+}
diff --git a/pkgs/profpatsch/default.nix b/pkgs/profpatsch/default.nix
index 51927fba..62f96e27 100644
--- a/pkgs/profpatsch/default.nix
+++ b/pkgs/profpatsch/default.nix
@@ -1,4 +1,4 @@
-{ callPackage, haskellPackages, jmtpfs, libmtp, droopy, fetchFromGitHub }:
+{ pkgs, callPackage, haskellPackages, jmtpfs, libmtp, droopy, fetchFromGitHub }:
 
 {
   display-infos = callPackage ./display-infos {};
@@ -16,6 +16,8 @@
     });
   };
 
+  backlight = callPackage ./backlight { inherit (pkgs.xorg) xbacklight; };
+
   # patched version of droopy, with javascript user-enhancement
   droopy = droopy.overrideDerivation (old: {
     src = fetchFromGitHub {