Skip to content

Instantly share code, notes, and snippets.

@ar7eniyan
Last active November 30, 2024 12:24
Show Gist options
  • Save ar7eniyan/42567870ad2ce47143ffeb41754b4484 to your computer and use it in GitHub Desktop.
Save ar7eniyan/42567870ad2ce47143ffeb41754b4484 to your computer and use it in GitHub Desktop.
Waybar module for controlling brightness of an external display
// insert into ~/.config/waybar/config
"custom/ddc_brightness": {
// I don't even want to know why this works.
// Change it to the following for your custom icons,
// current format is a hack for Material Symbols to display normally:
// "format": "{icon} {percentage}%",
"format": "<span rise='-2pt' size='12pt'>{icon}</span> <span rise='1pt' size='10pt'>{percentage}%</span>",
"format-icons": [
// Icons from Google Material Symbols.
// Put your icons here.
"\ue3a6", "\ue3a7", "\ue3a8", "\ue3a9", "\ue3aa", "\ue3ab", "\ue3ac"
],
"exec": "~/.config/waybar/waybar-ddc-module.sh",
"return-type": "json",
"on-scroll-up": "echo '+' > /tmp/waybar-ddc-module-rx",
"on-scroll-down": "echo '-' > /tmp/waybar-ddc-module-rx",
"on-click": "echo 'min' > /tmp/waybar-ddc-module-rx",
"on-click-right": "echo 'max' > /tmp/waybar-ddc-module-rx",
"tooltip": false,
},
#!/usr/bin/env bash
receive_pipe="/tmp/waybar-ddc-module-rx"
step=5
ddcutil_fast() {
# adjust the bus number and the multiplier for your display
# multiplier should be chosen so that it both works reliably and fast enough
ddcutil --noverify --bus 5 --sleep-multiplier .03 "$@" 2>/dev/null
}
ddcutil_slow() {
ddcutil --maxtries 15,15,15 "$@" 2>/dev/null
}
# takes ddcutil commandline as arguments
print_brightness() {
if brightness=$("$@" -t getvcp 10); then
brightness=$(echo "$brightness" | cut -d ' ' -f 4)
else
brightness=-1
fi
echo '{ "percentage":' "$brightness" '}'
}
rm -rf $receive_pipe
mkfifo $receive_pipe
# in case waybar restarted the script after restarting/replugging a monitor
print_brightness ddcutil_slow
while true; do
read -r command < $receive_pipe
case $command in
+ | -)
ddcutil_fast setvcp 10 $command $step
;;
max)
ddcutil_fast setvcp 10 100
;;
min)
ddcutil_fast setvcp 10 0
;;
esac
print_brightness ddcutil_fast
done
@ar7eniyan
Copy link
Author

Dependencies:

  • ddcutil (any version)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment