Last active
January 26, 2022 19:39
-
-
Save MawKKe/5e6af07558d4ba8150f5d12425406524 to your computer and use it in GitHub Desktop.
Ubuntu + i3-wm laptop display brightness control - a helper script that does not require sudo/root privileges
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 | |
set -eu | |
# Helper script for controlling laptop screen brightness WITHOUT sudo/root privileges. | |
# | |
# Tested on Lenovo Thinkpad x240 running Ubuntu 18.04. | |
# | |
# Author: Markus H (MawKKe) markus@mawkke.fi | |
# -------------------------------------------------------------------- | |
# Install instructions: | |
# 1) Add this file somewhere in your PATH and mark it excutable (chmod +x <...>) | |
# 2) Add the following in your i3-wm config: | |
# | |
# bindsym XF86MonBrightnessUp exec brightness.sh inc | |
# bindsym XF86MonBrightnessDown exec brightness.sh dec | |
# | |
# 3) Reload i3 (Mod-shift-r or something) | |
# | |
# ...and now your laptop keys should control the backlight! | |
# | |
# You may notice that this script does not require root privileges, | |
# unlike 'xbacklight' or all the crappy "echo $num > /sys/class.." solutions do :) | |
# | |
# -------------------------------------------------------------------- | |
# all changes happen via this PolicyKit helper program | |
# Unfortunately it does not support relative incrementation. | |
# Solution found at https://forum.xfce.org/viewtopic.php?id=11956 | |
cmd="pkexec /usr/lib/gnome-settings-daemon/gsd-backlight-helper" | |
# NOTE these may change depending on hardware. | |
# Run $cmd --get-max-brightness to get the maxv value; here we | |
# hardcode them to avoid reading the value on every keypress.. | |
# The lower limit (minv) is unlikely to change; replace maxv with value on your system. | |
minv=1 | |
maxv=851 | |
amount_default=100 | |
if [ $# -lt 1 ]; then | |
echo "Usage: $0 inc|dec|get [<delta:integer>]" | |
echo "By default changes brightness value by ${amount_default}. Override by specifying 'delta'." | |
echo "Current brightness limits: min=${minv} maxv=${maxv}" | |
exit 1 | |
fi | |
direction=$1 | |
# user value or 100 | |
amount=${2:-${amount_default}} | |
if ! [[ "$amount" =~ ^[0-9]+$ ]]; then | |
echo "Error: argument to inc/dec argument must be an integer" | |
exit 2 | |
fi | |
set_value(){ | |
val=$1 | |
if [ $val -gt $maxv ]; then | |
val=$maxv | |
fi | |
if [ $val -lt $minv ]; then | |
val=1 | |
fi | |
echo "Setting brightness to $val" | |
$cmd --set-brightness $val | |
} | |
current_value=$($cmd --get-brightness) | |
case $direction in | |
get) | |
echo "Current value is: $current_value" | |
;; | |
inc) | |
set_value $(($current_value + $amount)) | |
;; | |
dec) | |
set_value $(($current_value - $amount)) | |
;; | |
*) | |
echo "Argument not understood. Allowed: 'inc', 'dec', 'get'" | |
exit 2 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment