Skip to content

Instantly share code, notes, and snippets.

@kevinlipe
Last active December 30, 2022 20:56
Show Gist options
  • Save kevinlipe/ff7a921a55dddb02b7fa70b6d3945ad5 to your computer and use it in GitHub Desktop.
Save kevinlipe/ff7a921a55dddb02b7fa70b6d3945ad5 to your computer and use it in GitHub Desktop.
#!/bin/bash
# for use with https://forums.linuxmint.com/viewtopic.php?t=319948
# NAME: redirect-brightness
# PATH: /usr/local/bin
# DESC: Redirect to correct driver when Ubuntu is adjusting the wrong
# /sys/class/DRIVER_NAME/brightness
# DATE: June 13, 2018. Modified June 14, 2018.
# NOTE: Written for Ubuntu question:
# https://askubuntu.com/q/1045624/307523
WatchDriver="/sys/class/backlight/panasonic"
PatchDriver="/sys/class/backlight/intel_backlight"
# Must be running as sudo
if [[ $(id -u) != 0 ]]; then
echo >&2 "Root access required. Use: 'sudo redirect-brightness'"
exit 1
fi
# inotifywait required
type inotifywait >/dev/null 2>&1 || \
{ echo >&2 "'inotifywait' required but it's not installed. Aborting."; \
echo >&2 "Use 'sudo apt install inotify-tools' to install it.'"; \
exit 1; }
# Was right watch driver directory name setup correctly?
if [[ ! -d $WatchDriver ]]; then
echo >&2 "Watch directory: '$WatchDriver'"; \
echo >&2 "does not exist. Did you spell it correctly? Aborting.'"; \
exit 1;
fi
# Was right patch driver directory name setup correctly?
if [[ ! -d $PatchDriver ]]; then
echo >&2 "Redirect to directory: '$PatchDriver'"; \
echo >&2 "does not exist. Did you spell it correctly? Aborting.'"; \
exit 1;
fi
#Get maximum brightness values
WatchMax=$(cat $WatchDriver/max_brightness)
PatchMax=$(cat $PatchDriver/max_brightness)
# PARM: 1="-l" or "--log-file" then write each step to log file.
fLogFile=false
if [[ $1 == "-l" ]] || [[ $1 == "--log-file" ]]; then
fLogFile=true
LogFile=/tmp/redirect-brightness.log
echo redirect-brightness LOG FILE > $LogFile
echo WatchMax: $WatchMax PatchMax: $PatchMax >> $LogFile
fi
SetBrightness () {
# Calculate watch current percentage
WatchAct=$(cat $WatchDriver/actual_brightness)
WatchPer=$(( WatchAct * 100 / WatchMax ))
[[ $fLogFile == true ]] && echo WatchAct: $WatchAct WatchPer: $WatchPer >> $LogFile
# Reverse engineer patch brightness to set
PatchAct=$(( PatchMax * WatchPer / 100 ))
echo $PatchAct | sudo tee $PatchDriver/brightness
[[ $fLogFile == true ]] && echo PatchAct: $PatchAct >> $LogFile
}
# When machine boots, set brightness to last saved value
SetBrightness
# Wait forever for user to press Fn keys adjusting brightness up/down.
while (true); do
inotifywait --event modify $WatchDriver/actual_brightness
[[ $fLogFile == true ]] && \
echo "Processing modify event in $WatchDriver/actual_brightness" >> $LogFile
SetBrightness
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment