summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2021-11-03 09:45:36 +0100
committerAnton Khirnov <anton@khirnov.net>2021-11-04 08:58:56 +0100
commit91952b8fd95da215a6404cffb6f96b85d18fe4a2 (patch)
tree445d6ae857340de053ac8a8f0b22d72ad0e37ba5 /bin
parent056c38ca063c19d6f4c119d75ea6ed6c2e4473a8 (diff)
Xsession: show notifications on screen brightness changes
Diffstat (limited to 'bin')
-rwxr-xr-xbin/brightness_monitor40
1 files changed, 40 insertions, 0 deletions
diff --git a/bin/brightness_monitor b/bin/brightness_monitor
new file mode 100755
index 0000000..a8617e0
--- /dev/null
+++ b/bin/brightness_monitor
@@ -0,0 +1,40 @@
+#!/usr/bin/python3
+
+import notify2
+import os
+import subprocess
+import sys
+
+if len(sys.argv) < 2:
+ sys.stdout.write('Usage: %s /sys/.../<backlight dir>/\n')
+ sys.exit(0)
+
+path = sys.argv[1]
+
+with open(os.path.join(path, 'max_brightness'), 'r') as f:
+ brightness_max = float(f.read().strip())
+
+notify2.init(os.path.basename(sys.argv[0]))
+
+notification = notify2.Notification('Brightness')
+
+inotify_cmd = ['inotifywait', '--monitor', '--event=close_write',
+ os.path.join(path, 'brightness')]
+
+child = subprocess.Popen(inotify_cmd, stdout = subprocess.PIPE, bufsize = 0)
+
+while True:
+ # wait for updates
+ child.stdout.read(4096)
+ if child.poll() is not None:
+ notification.message = 'inotify monitor has terminated'
+ notification.set_urgency(notify2.URGENCY_CRITICAL)
+ notification.show()
+ sys.stderr.write(notification.message + '\n')
+ break
+
+ with open(os.path.join(path, 'brightness'), 'r') as f:
+ brightness = float(f.read().strip())
+
+ notification.message = '%d%%' % (100 * brightness / brightness_max)
+ notification.show()