Watch
1
0
Fork
You've already forked dotfiles
0
dotfiles/.local/bin/night-light-make-cooler
2026-08-15 03:17:05 +02:00

88 lines
2.6 KiB
Shell
Executable file

#!/bin/sh
# Triggered by <Alt><Shift>Left
# as defined in [org/gnome/settings-daemon/plugins/media-keys]
# in ~/.config/gnome-settings/key-bindings.ini
#
# This script makes the screen a bit cooler than it currently is,
# unless it is currently in a ramping phase and not fully tinted
#
# The default warmth is restored at login by ~/.local/bin/restore-gnome
set -eu
# GNOME's GUI lets one choose between 1700 and 4700 only,
# and 6500 is the point where no color correction is applied at all
# while 1000 is the absolute minimum supported by the OS
_MIN_TEMPERATURE=1200
_MAX_TEMPERATURE=4700
notify() {
ID_FILE="${XDG_RUNTIME_DIR}/night-light-notification-id"
_id=$(cat "$ID_FILE" 2>/dev/null || true)
if [ -n "$_id" ]; then
notify-send -p -t 5000 --replace-id="$_id" "Night Light" "$1" > "$ID_FILE" || rm -f "$ID_FILE"
else
notify-send -p -t 5000 "Night Light" "$1" > "$ID_FILE" || rm -f "$ID_FILE"
fi
}
Schema=org.gnome.settings-daemon.plugins.color
active=$(gdbus call --session \
--dest org.gnome.SettingsDaemon.Color \
--object-path /org/gnome/SettingsDaemon/Color \
--method org.freedesktop.DBus.Properties.Get \
org.gnome.SettingsDaemon.Color NightLightActive)
applied=$(gdbus call --session \
--dest org.gnome.SettingsDaemon.Color \
--object-path /org/gnome/SettingsDaemon/Color \
--method org.freedesktop.DBus.Properties.Get \
org.gnome.SettingsDaemon.Color Temperature \
| sed -E 's/.*uint32 ([0-9]+).*/\1/')
automatic=$(gsettings get $Schema night-light-schedule-automatic)
current=$(gsettings get $Schema night-light-temperature | awk '{print $2}')
# Cooling never turns "Night Light" on
if [ "$active" = "(<false>,)" ]; then
notify "is not tinting the screen right now (target: ${current}K)"
exit 0
fi
# While the screen is not (fully) tinted,
# stepping the target has no visible effect
# => Only show what is going on
if [ "$automatic" = "true" ] && [ $((applied - current)) -gt 300 ]; then
notify "is still following the schedule's ramp (target: ${current}K)"
exit 0
fi
# Walking past the cool end switches the feature off
if [ "$current" -ge $_MAX_TEMPERATURE ]; then
gsettings set $Schema night-light-enabled false
notify "is now off (stepped past ${_MAX_TEMPERATURE}K)"
exit 0
fi
# 500 = (4700 - 1700 + 500) / 7
# => Seven steps spanning more than the range in the GUI,
# mainly because we want to allow a bit more warmth (i.e., +500)
new=$((current + 500))
if [ "$new" -gt $_MAX_TEMPERATURE ]; then
new=$_MAX_TEMPERATURE
fi
gsettings set $Schema night-light-temperature "uint32 $new"