From 9a2053ff3d0225507dde64205383a295f0a0ce69 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= <motiejus@jakstys.lt>
Date: Thu, 6 Mar 2025 20:36:40 +0000
Subject: [PATCH] brightness script

---
 modules/profiles/desktop/brightness  | 46 ++++++++++++++++++++++++++++
 modules/profiles/desktop/default.nix |  5 +++
 modules/profiles/desktop/rc.lua      | 10 ++++++
 3 files changed, 61 insertions(+)
 create mode 100755 modules/profiles/desktop/brightness

diff --git a/modules/profiles/desktop/brightness b/modules/profiles/desktop/brightness
new file mode 100755
index 0000000..585bb3c
--- /dev/null
+++ b/modules/profiles/desktop/brightness
@@ -0,0 +1,46 @@
+#!/usr/bin/env bash
+#
+# This is a shell "application", but doesn't have to be one, so
+# leaving the shebang in place.
+set -euo pipefail
+
+dir=$(find /sys/devices/ -name intel_backlight | head -1)
+if [[ -z "$dir" ]]; then
+    echo "intel_backlight not found"
+    exit 1
+fi
+
+max=$(cat "$dir/max_brightness")
+now=$(cat "$dir/brightness")
+step=$((max * 3 / 100))
+min=100
+
+clamp() {
+    if [[ $1 -gt $max ]]; then
+        echo "$max"
+    elif [[ $1 -lt $min ]]; then
+        echo "$min"
+    else
+        echo "$1"
+    fi
+}
+
+info() {
+    perc="$(echo "$1*100/$max" | bc -l)"
+    printf "%s of %s (%.2f%%)\n" "$1" "$max" "$perc"
+}
+
+new=
+case "${1:-}" in
+up)
+    new=$(clamp $((now + step)) ) ;;
+down)
+    new=$(clamp $((now - step)) ) ;;
+esac
+
+if [[ -n "$new" ]]; then
+    echo "$new" | sudo tee "$dir/brightness" > /dev/null
+    info "$new"
+else
+    info "$now"
+fi
diff --git a/modules/profiles/desktop/default.nix b/modules/profiles/desktop/default.nix
index 2ea9313..71127d2 100644
--- a/modules/profiles/desktop/default.nix
+++ b/modules/profiles/desktop/default.nix
@@ -8,6 +8,10 @@ let
   inherit (config.mj) username;
   firefox =
     if (pkgs.stdenv.hostPlatform.system == "x86_64-linux") then pkgs.firefox-bin else pkgs.firefox;
+  brightness = pkgs.writeShellApplication {
+    name = "brightness";
+    text = builtins.readFile ./brightness;
+  };
 in
 {
   imports = [ ../dev ];
@@ -134,6 +138,7 @@ in
         # packages defined here
         nicer
         tmuxbash
+        brightness
 
         f3 # flight-flash-fraud
         iw
diff --git a/modules/profiles/desktop/rc.lua b/modules/profiles/desktop/rc.lua
index 876e3a1..37b111f 100644
--- a/modules/profiles/desktop/rc.lua
+++ b/modules/profiles/desktop/rc.lua
@@ -278,6 +278,16 @@ globalkeys = gears.table.join(
                     xclip -selection clipboard -target image/png -i $f'
         ]])
     end),
+    awful.key({}, "XF86MonBrightnessDown", function()
+        awful.spawn.easy_async_with_shell("brightness down", function(stdout)
+            naughty.notify({text = stdout, timeout = 1})
+        end)
+    end),
+    awful.key({}, "XF86MonBrightnessUp", function()
+        awful.spawn.easy_async_with_shell("brightness up", function(stdout)
+            naughty.notify({text = stdout, timeout = 1})
+        end)
+    end),
     awful.key({ modkey,           }, "Left",   awful.tag.viewprev,
               {description = "view previous", group = "tag"}),
     awful.key({ modkey,           }, "Right",  awful.tag.viewnext,