Last active
July 16, 2024 08:21
-
-
Save murar8/8b528dcd07f2d12626ef36ca7858881b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# vim: ft=systemd.unit | |
# | |
# Systemd service file for the brightness-daemon script. | |
# | |
# Author: Lorenzo Murarotto <lnzmrr@gmail.com> | |
# License: MIT | |
[Unit] | |
Description=Daemon to sync the brightness level of external monitors. | |
After=dbus.socket | |
[Service] | |
Type=simple | |
ExecStart=%h/.local/bin/brightness-daemon | |
Restart=always | |
[Install] | |
WantedBy=default.target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# vim: ft=bash | |
# | |
# Keep the external monitor brightness in sync with the DBUS brightness. | |
# | |
# Author: Lorenzo Murarotto <lnzmrr@gmail.com> | |
# License: MIT | |
# Dependencies: ddcutil | |
set -o errexit # Exit on error. | |
set -o nounset # Trigger error when expanding unset variables. | |
set -o pipefail # Use last non-zero exit code in a pipeline. | |
DBUS_PATH="/org/gnome/SettingsDaemon/Power" | |
DBUS_TYPE="signal" | |
DBUS_MEMBER="PropertiesChanged" | |
DBUS_INTERFACE="org.freedesktop.DBus.Properties" | |
DBUS_GET_METHOD="org.freedesktop.DBus.Properties.Get" | |
DBUS_ARG0="org.gnome.SettingsDaemon.Power.Screen" | |
# Wait time in seconds before propagating the brightness change. | |
# On my system the ddcutil command takes a while to execute, so if the | |
# brightness changes in quick succession, it will start lagging behind. | |
WAIT_TIME_SECONDS=1 | |
function update_brightness() { | |
echo "Setting external monitor brightness to $1" | |
ddcutil setvcp 10 "$1" | |
} | |
function schedule_brightness_update() { | |
sleep $WAIT_TIME_SECONDS | |
update_brightness "$1" | |
} | |
update_brightness "$( | |
dbus-send \ | |
--session \ | |
--dest=org.gnome.SettingsDaemon.Power \ | |
--type=method_call \ | |
--print-reply \ | |
$DBUS_PATH \ | |
$DBUS_GET_METHOD \ | |
string:"$DBUS_ARG0" \ | |
string:"Brightness" | | |
grep -oP '(?<=variant int32 )\d+' | |
)" | |
dbus-monitor --monitor "type=${DBUS_TYPE},interface=${DBUS_INTERFACE},member=${DBUS_MEMBER},arg0=${DBUS_ARG0}" | | |
while read -r line; do | |
if [[ "$line" =~ variant[[:space:]]+int32[[:space:]]+([[:digit:]]+) ]]; then | |
if [[ -n "${last_pid:-}" ]]; then kill "$last_pid" 2>/dev/null || true; fi | |
schedule_brightness_update "${BASH_REMATCH[1]}" & | |
last_pid=$! | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tested on Ubuntu 24.04 LTS
Save
brightness-daemon.service
to$HOME/.config/systemd/user/brightness-daemon.service
Save
brightness-daemon.sh
to$HOME/.local/bin/brightness-daemon
(note nosh
extension)Exec
systemctl --user enable --now brightness-daemon.service