Skip to content

Instantly share code, notes, and snippets.

@mtao
Created December 15, 2020 03:52
Show Gist options
  • Save mtao/eefef1d6a15c9bc8112c72d2ccc34bfe to your computer and use it in GitHub Desktop.
Save mtao/eefef1d6a15c9bc8112c72d2ccc34bfe to your computer and use it in GitHub Desktop.
Updates the brightness of a screen using xrandr.
#!/bin/bash
# Changes brightness as a float value in [0,1], using +N -N induces a relative
# change in brightness
# Usage:
# brightness.sh .8 # set brightness to .8
# brightness.sh -.1 # decrement brightness by .1
# brightness.sh +.1 # increment brightness by .1
# helper to do floating point evaluation
evaluate() {
echo $@ | bc
}
# sometimes the laptop screen is eDP-1 or eDP-1-1, just gotta detect the case
if [ -n "$( xrandr | grep eDP-1-1 )" ]
then
laptop="eDP-1-1"
else
laptop="eDP-1"
fi
# brightness is the second token of the 5th entry after the device identifier
CUR_BRIGHT=$( xrandr --verbose --current | grep ^"$laptop" -A5 | tail -n 1 | awk '{print $2}' )
# if no arguments are provided then just print the current brightness.
# otherwise just use the first one
if [ "$#" -lt 1 ]
then
echo $CUR_BRIGHT
exit 1
fi
# check for a sign in the first argument (+/-) use a relative offset, o.w just
# set the first arg to be brightness
ARGSGN=${1::1}
ARGREST=${1:1}
if [ $ARGSGN = '+' ]
then
NEW_BRIGHT=$( evaluate $CUR_BRIGHT + $ARGREST )
elif [ $ARGSGN = '-' ]
then
NEW_BRIGHT=$( evaluate $CUR_BRIGHT - $ARGREST )
else
NEW_BRIGHT=$1
fi
# clamp the brightness between 0 and 1
if [ $( evaluate "$NEW_BRIGHT < 0" ) -eq 1 ]
then
NEW_BRIGHT=0
elif [ $( evaluate "$NEW_BRIGHT > 1" ) -eq 1 ]
then
NEW_BRIGHT=1
fi
# finally use xrandr to change brightness
xrandr --output $laptop --brightness $NEW_BRIGHT
@mtao
Copy link
Author

mtao commented Dec 15, 2020

This was implemented in allow for brightness controls on my Dell XPS 15 7590, which has an OLED panel (which doesn't work with xbrightness for me).

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