Skip to content

Instantly share code, notes, and snippets.

@joshbmarshall
Created April 11, 2023 13:30
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 joshbmarshall/30da334af63e8859c52f92e72068b246 to your computer and use it in GitHub Desktop.
Save joshbmarshall/30da334af63e8859c52f92e72068b246 to your computer and use it in GitHub Desktop.
Automatic keyboard backlight on/off on Thinkpad bash script
#!/bin/bash
startup_time="20:00"
end_time="08:00"
inactivity_time=10000
polling_interval=0.5
device=tpacpi::kbd_backlight
on_value=1
off_value=0
# Initial state
brightnessctl -d $device s $off_value
light_status=off
while true; do
idle=`xprintidle`
if [ "$light_status" == "off" ]; then
if [ $idle -lt $inactivity_time ]; then
current_time_epoch=$(date +%s)
startup_time_epoch=$(date +%s -d "today $startup_time")
end_time_epoch=$(date +%s -d "today $end_time")
if [ $current_time_epoch -ge $startup_time_epoch ] || [ $current_time_epoch -le $end_time_epoch ]; then
brightnessctl -d $device s $on_value
light_status=on
echo "Lights on"
else
sleep 10
fi
fi
fi
if [ "$light_status" == "on" ]; then
if [ $idle -gt $inactivity_time ]; then
brightnessctl -d $device s $off_value
light_status=off
echo "Lights off"
fi
fi
sleep $polling_interval
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment