How to change the Xorg gamma/brightness?

Silly me! I have xflux with fluxgui activated, each time I would like to modify the settings xflux will be in my way. All commands worked, just that xflux would revert it back.

Those who want to change their gamma/brightness:

Use xrandr to list your outputs:

$ xrandr
Screen 0: minimum 320 x 200, current 1024 x 768, maximum 8192 x 8192
DVI-0 connected 1024x768+0+0 (normal left inverted right x axis y axis) 304mm x 228mm

As you can see my output is DVI-0 to change the brightness:

xrandr --output DVI-0 --brightness 2

To change the gamma:

xrandr --output DVI-0 --gamma 2:2:1

I was hoping there was some interactive program for adjusting xrandr's settings (gamma/brightness in particular), but couldn't find anything.

So I wrote this shell script which allows some interactive adjustment of brightness/gamma, as well as saving/restoring the settings.

Save the file to irandr.sh, do a chmod u+x irandr.sh to make it executable, and run as ./irandr.sh <outputname>.

The d/f/D/F/j/k/J/K keys adjust the brightness (d/f/D/F) or gamma (j/k/J/K) settings by steps of 5 (lower case) or 1 (upper case.)

  • s saves the settings to the dotfile
  • l loads the settings from the dotfile
  • r resets brightness/gamma to the defaults (1.0)
  • q quits.

The dotfile for a given output is ~/irandr-<outputname>.dat. If you run without an argument, it will list the valid xrandr outputs.

You can append a -setonly argument to the command line, which will read the settings for the given display's dotfile, update the display, and exit. (Useful, perhaps, in a ~/.bashrc file to automatically set a gamma/brightness.)

#!/bin/bash
# irandr.sh, by Dale Gass ([email protected])
# Wed Apr 10 16:43:22 EDT 2019

# Process arguments
if [ "$1" != "" ]
then
    output="$1"
else
    echo "Usage: irandr.sh <outputname> [-setonly]"
    echo "(Settings saved to ~/.xrandr-<outputname>.dat)"
    echo
    echo "Valid outputs:"
    xrandr | egrep -v '^( |Screen)'
    exit 1
fi
setonly=0
if [ "$2" = "-setonly" ]; then setonly=1; fi

# Initialize variables, read for dotfile if exists
cmdhelp="d/f/D/F=brightness j/k/J/K=gamma r=reset s=save l=load q=quit"
brightness=100
gamma=100
dotfile=~/.irandr-"$output".dat
if [ -s "$dotfile"  ]; then read brightness gamma <"$dotfile"; fi
if [ $setonly -eq 0 ]; then 
    echo $cmdhelp
    stty -echo raw intr $'\000' # Allow single character input
fi

# Main loop for setting adjustment
echo 'Bright Gamma'
while :
do
    b=$(bc <<< "scale=2; $brightness/100")  # Make 0.0-1.0
    g=$(bc <<< "scale=2; $gamma/100")
    xrandr --output "$output" --brightness "$b" --gamma "$g:$g:$g"
    printf "\r%4d %4d " $brightness $gamma
    if [ $setonly -eq 1 ]; then echo; exit 0; fi

    read -n1 ch     # Get input character from user
    case $ch in
    d) let brightness=brightness-5;; D) let brightness=brightness-1;;
    f) let brightness=brightness+5;; F) let brightness=brightness+1;;
    j) let gamma=gamma-5;;           J) let gamma=gamma-1;;
    k) let gamma=gamma+5;;           K) let gamma=gamma+1;;
    r) brightness=100; gamma=100;;
    s) echo "$brightness    $gamma" >"$dotfile" && echo -e "Saved\r";;
    l) read brightness gamma <"$dotfile"     && echo -e "Loaded\r";;
    q|$'\003') break;;
    *) echo -e "$cmdhelp\r";;
    esac
done

stty echo -raw intr $'\003' # Undo single character input