From 91952b8fd95da215a6404cffb6f96b85d18fe4a2 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Wed, 3 Nov 2021 09:45:36 +0100 Subject: Xsession: show notifications on screen brightness changes --- bin/brightness_monitor | 40 ++++++++++++++++++++++++++++++++++++++++ desktop.conf.yaml | 1 + dotfiles/Xsession | 5 +++++ 3 files changed, 46 insertions(+) create mode 100755 bin/brightness_monitor 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/...//\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() diff --git a/desktop.conf.yaml b/desktop.conf.yaml index fa2eb13..81ade61 100644 --- a/desktop.conf.yaml +++ b/desktop.conf.yaml @@ -4,6 +4,7 @@ - link: ~/.config/mpv/config: dotfiles/mpv + ~/.local/bin/brightness_monitor: bin/brightness_monitor ~/.local/bin/ssh_sk_clean: bin/ssh_sk_clean ~/.local/bin/udev_match: bin/udev_match ~/.urxvt/ext/reselect: urxvt/reselect/reselect diff --git a/dotfiles/Xsession b/dotfiles/Xsession index 72065a5..3d2a716 100644 --- a/dotfiles/Xsession +++ b/dotfiles/Xsession @@ -20,6 +20,11 @@ udev_match --subsystem=hidraw --match=ACTION=remove --match=ID_FIDO_TOKEN=1 ssh_ udev_match --subsystem=hidraw --match=ACTION=add --match=ID_FIDO_TOKEN=1 \ "ssh-add -K || notify-send -u critical 'Error adding FIDO2 keys to SSH agent' 'Wrong PIN?'" & +# show notifications on brightness changes +for d in /sys/class/backlight/*; do + brightness_monitor "$d" & +done + # source the machine-local configuration [ -r "$HOME/.config/Xsession_local" ] && . "$HOME/.config/Xsession_local" -- cgit v1.2.3