Created
June 25, 2024 23:03
-
-
Save mathieu-b/f7a13a260380ac2ffe8e7808cca9c0a4 to your computer and use it in GitHub Desktop.
Control the brightness of external displays via ddcutil on Linux
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 | |
#============================================================================== | |
# Script for setting the brightness of two displays using ddcutil. | |
# You could store this script at ~/.local/bin/brightness | |
# (with execute permissions, and ensuring that ~/.local/bin is in your PATH) | |
# and then call it from a keybinding, launcher, or directly from CLI. | |
# Example usages: | |
# brightness +10 (increase brightness by 10%) | |
# brightness -10 (decrease brightness by 10%) | |
# brightness 50 (set brightness to 50%) | |
# brightness 100 (set brightness to 100%) | |
# | |
# Author: Mathieu Bosi, 2024/06/26 | |
# Based on: https://moverest.xyz/blog/control-display-with-ddc-ci/configure | |
#============================================================================== | |
set -eu | |
if ! command -v ddcutil &> /dev/null; then | |
echo "ddcutil is not installed. Please install it and configure your machine as explained here:" | |
echo "https://moverest.xyz/blog/control-display-with-ddc-ci/configure" | |
exit 1 | |
fi | |
if [[ $# -ne 1 ]]; then | |
echo "Usage: $0 [+,-]<brightness_percent>" | |
exit 1 | |
fi | |
brightness="$1" | |
# Ensure that there is a space between the sign and the number, as required by ddcutil: | |
brightness="${brightness//+/+ }" | |
brightness="${brightness//-/- }" | |
echo "Brightness parameter for ddcutil: '$brightness'" | |
readonly DDC_BRIGHTNESS_CONTROL_ID=10 | |
function set_display_brightness() { | |
local display_number="$1" | |
local brightness="$2" | |
local retries_left=2 | |
echo "Setting brightness for display $display_number to '${brightness}' ..." | |
until ddcutil --display "${display_number}" setvcp "${DDC_BRIGHTNESS_CONTROL_ID}" ${brightness} ; do | |
[[ $retries_left -le 0 ]] && break | |
echo "Error setting brightness for display $display_number, will retry $retries_left more times ..." | |
retries_left=$((retries_left - 1)) | |
sleep 0.1 | |
done | |
if [[ $retries_left -le 0 ]]; then | |
echo "Failed to set brightness for display $display_number" | |
else | |
echo "Brightness for display $display_number set to $brightness percent" | |
fi | |
} | |
for display_number in 1 2 ; do | |
set_display_brightness "${display_number}" "${brightness}" & | |
done | |
wait |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated version that auto-detects all connected displays that support the Brightness control: