Skip to content

Instantly share code, notes, and snippets.

@Rudxain
Last active October 5, 2023 05:31
Show Gist options
  • Save Rudxain/3b070d4f17de31987e2052cf3eea0690 to your computer and use it in GitHub Desktop.
Save Rudxain/3b070d4f17de31987e2052cf3eea0690 to your computer and use it in GitHub Desktop.
exponential backlight for Linux on Dell Inspiron 15R
#!/bin/sh
set -euf
# `dell_backlight` is exp (percieved lin),
# `intel_backlight` is lin (percieved log),
# but Intel's `0` is dimmer than Dell's,
# so I prefer `acpi_backlight` set to "intel", not "vendor".
BL=/sys/class/backlight/intel_backlight/brightness
[ -w "$BL" ] || sudo chmod a+w "$BL"
# BL API has built-in input-validation,
# so there's no need to worry about security
# (unless you run `chmod a+x`).
# 15 for Dell.
MAX=4882 #./max_brightness
BITS=7 #bit_len MAX - 6
B2=152 #$((MAX >> (BITS - 2)))
#B1=0
# ChromeOS behavior
B0() {
# "b" can't change while off,
# and we want a "smooth" transition to pure-black
echo 0 > "$BL"
# X11
xset dpms force off 2>/dev/null \
|| \ # GNOME Wayland fallback
dbus-send \
--session \
--dest=org.gnome.ScreenSaver \
--type=method_call \
/org/gnome/ScreenSaver \
org.gnome.ScreenSaver.SetActive \
boolean:true
}
b="$1"
if [ "_$b" = _+ ]
then
b="$(cat "$BL")"
if [ "$b" -lt "$B2" ]
then
b="$B2"
else
b="$((b << 1))"
if [ "$b" -gt "$MAX" ]
then
b="$MAX"
fi
fi
echo "$b" > "$BL"
elif [ "_$b" = _- ]
then
b="$(cat "$BL")"
if [ "$b" -le 0 ]
then
B0
elif [ "$b" -le "$B2" ]
then
echo 0 > "$BL"
else
echo "$((b >> 1))" > "$BL"
fi
else
b="$((b))" # safety parse
if [ "$b" -le 1 ]
then
if [ "$b" -le 0 ]
then
B0
else
echo 0 > "$BL"
fi
else
if [ "$b" -ge "$BITS" ]
then
b=0
else
b="$((BITS - b))"
fi
echo "$((MAX >> b))" > "$BL"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment