Skip to content

Instantly share code, notes, and snippets.

@PhrozenByte
Created March 30, 2019 14:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save PhrozenByte/d4ffab74d236651d21f0613d7c051cf7 to your computer and use it in GitHub Desktop.
Save PhrozenByte/d4ffab74d236651d21f0613d7c051cf7 to your computer and use it in GitHub Desktop.
Zenity dialog to set screen brightness using xrandr
#!/bin/bash
export LC_ALL=C
readarray -t SCREENS < <(xrandr --listmonitors | grep "^ [0-9]\+: " | cut -d ' ' -f 6)
[[ ${#SCREENS[@]} > 0 ]] || exit 1
CURBRIGHT="$(xrandr --current --verbose | awk 'on { if ($0 ~ /^\tBrightness: /) { print $2 } else if ($0 ~ /^\t/) { next } exit } /^[[:graph:]]+ connected primary/ { on=1 }')"
[ -n "$CURBRIGHT" ] && CURBRIGHT="$(bc 2> /dev/null <<< "scale=0; ($CURBRIGHT * 100)/1")" || CURBRIGHT=100
function setbright {
local BRIGHT="$(bc <<< "scale=1; $1 / 100")"
for SCREEN in "${SCREENS[@]}"; do
xrandr --output "$SCREEN" --brightness "$BRIGHT"
done
}
ICON_PATH=""
[ ! -x "$(which gtk-icon-path)" ] || ICON_PATH="$(gtk-icon-path display-brightness-symbolic 256 2> /dev/null)"
[ -n "$ICON_PATH" ] || ICON_PATH="question"
printf "Current brightness: %4d%%\n" "$CURBRIGHT"
printf "\rBrightness: %4d%%" "$CURBRIGHT"
setbright "$CURBRIGHT"
zenity --scale --print-partial \
--title="X RandR Brightness" \
--text="Set brightness to..." \
--window-icon="$ICON_PATH" \
--min-value=10 --max-value=1000 --step=10 \
--value="$CURBRIGHT" 2> /dev/null \
| while read BRIGHT; do
printf "\rBrightness: %4d%%" "$BRIGHT"
setbright "$BRIGHT"
done
if [ "${PIPESTATUS[0]}" -ne 0 ]; then
printf "\nReset brightness: %4d%%" "$CURBRIGHT"
setbright "$CURBRIGHT"
fi
echo
@liskin
Copy link

liskin commented Oct 26, 2020

This inspired me to create a similar script that uses ddcutil to adjust the actual brightness of an external monitor:
https://github.com/liskin/dotfiles/blob/bb9a2cbc91eabf23d386fb4c3cc912b231d955bb/bin/liskin-brightness
https://twitter.com/Liskni_si/status/1320698852970778627

@PhrozenByte
Copy link
Author

Great work @liskin ❤️

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