Skip to content

Instantly share code, notes, and snippets.

@pas-calc
Created April 15, 2021 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pas-calc/b50bb7f59482d72aa85167e735a474bf to your computer and use it in GitHub Desktop.
Save pas-calc/b50bb7f59482d72aa85167e735a474bf to your computer and use it in GitHub Desktop.
Change brightness of internal Laptop display and external HDMI/VGA monitor
#!/bin/bash
# or use sh instead of bash?
# set to executable flag, chmod +x !
set -eu
# on error : do not resume, but cancel , see https://stackoverflow.com/questions/13468481/when-to-use-set-e/13478622
#debug
exec >> ~/.redshift-hooks.log 2>&1
echo ""
echo "begin"
# Set brightness via xbrightness when redshift status changes
# Set brightness values for each status.
# Range from 1 to 100 is valid
brightness_day=100 #85
brightness_transition=50
brightness_night=10
#0 #30
# Set fps for smoooooth transition
#fps=1000
# Adjust this grep to filter only the backlights you want to adjust
#backlights=($(xbacklight -list | grep ddcci*))
echo "started now"
# output date
date
maxbrightness=$(mate-power-backlight-helper --get-max-brightness)
echo "max Laptop brightness = ${maxbrightness}"
set_brightness() {
echo "set brightness to $1"
ddcutil setvcp 10 $1 --display 1
echo "return value was: $?"
#calculate https://www.shell-tips.com/bash/math-arithmetic-calculation/
laptopbrightness=$(expr $maxbrightness \* $1 / 100)
echo "set laptop brightness to ${laptopbrightness}"
#bypass security https://ubuntu-mate.community/t/keyboard-shortcut-for-display-brightness/11912/8 using pkexec
#https://askubuntu.com/questions/78352/when-to-use-pkexec-vs-gksu-gksudo
# alternative using sudo : https://askubuntu.com/a/469040
pkexec mate-power-backlight-helper --set-brightness $laptopbrightness
echo "return value was: $?"
#for backlight in "${backlights[@]}"
#do
# xbacklight -set $1 -fps $fps -ctrl $backlight &
#done
}
if [ "$1" = period-changed ]; then
echo "period changed : \"$2\" to \"$3\""
case $3 in
night)
set_brightness $brightness_night
;;
transition)
set_brightness $brightness_transition
;;
daytime)
set_brightness $brightness_day
;;
esac
fi
echo "end"
# apt install ddcutil
# add user to group i2c-tools
# https://github.com/jonls/redshift/pull/512#issuecomment-359946517
# https://github.com/jonls/redshift/issues/436
# https://askubuntu.com/questions/894465/changing-the-screen-brightness-of-the-external-screen
# https://github.com/qualiaa/redshift-hooks
## ----------------------------------
## Redshift hooks:
## http://jonls.dk/2015/01/redshift-1-10-released/#hooks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment