Skip to content

Instantly share code, notes, and snippets.

@tassaron
Last active May 8, 2024 08:58
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tassaron/a8ecfd6e30d1785f761a4124c8665b73 to your computer and use it in GitHub Desktop.
Save tassaron/a8ecfd6e30d1785f761a4124c8665b73 to your computer and use it in GitHub Desktop.
set KDE Plasma backlight brightness using dbus with a bash script
#!/bin/bash
# Set this to 1 to reset the backlight to 100% after execution
TEST_MODE=0
DBUS_SERVICE="local.org_kde_powerdevil"
DBUS_PATH="/org/kde/Solid/PowerManagement/Actions/BrightnessControl"
MAX_BRIGHTNESS=$(qdbus $DBUS_SERVICE $DBUS_PATH brightnessMax)
current_brightness() {
qdbus $DBUS_SERVICE $DBUS_PATH brightness
}
if [ -z "$1" ]; then
echo "set backlight using qdbus on KDE Plasma"
echo "usage: $0 50"
echo "usage: $0 up"
echo "usage: $0 down"
echo "usage: $0 -v"
exit 0
elif [ "$1" = "-v" ]; then
# Use bc for floating point division
printf %.0f $(echo "$(echo \"$(current_brightness) / $MAX_BRIGHTNESS\" | bc -l) * 100" | bc -l)
exit 0
else
# Attempt to convert first arg to a number
# bc returns 0 when given non-numbers
desired_percent=$(echo "$1" | bc -l)
if [ $desired_percent -gt 0 ] && [ $desired_percent -le 100 ]; then
val=$(echo "$(echo \"$desired_percent / 100\" | bc -l) * $MAX_BRIGHTNESS" | bc -l)
# use printf to strip decimals from bc's output, making it an integer again for dbus
qdbus $DBUS_SERVICE $DBUS_PATH setBrightness $(printf %.0f $val)
else
# Check if arg controls relative "steps" up and down
if [ "$1" = "up" ] || [ "$1" = "down" ]; then
CURR_BRIGHTNESS=$(current_brightness)
STEPS_BRIGHTNESS=$(qdbus $DBUS_SERVICE $DBUS_PATH brightnessSteps)
if [ "$1" = "up" ]; then
val=$(($CURR_BRIGHTNESS + $(($MAX_BRIGHTNESS / $STEPS_BRIGHTNESS))))
elif [ "$1" = "down" ]; then
val=$(($CURR_BRIGHTNESS - $(($MAX_BRIGHTNESS / $STEPS_BRIGHTNESS))))
fi
qdbus $DBUS_SERVICE $DBUS_PATH setBrightness $val
else
# Invalid argument
exit 1
fi
fi
fi
if [ $TEST_MODE = 1 ]; then
sleep 2s
qdbus $DBUS_SERVICE $DBUS_PATH setBrightness $MAX_BRIGHTNESS
echo "TEST_MODE is enabled"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment