You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

101 lines
3.0 KiB

#!/bin/bash
#----------------------------------------------------------------
# gammastep-adjust — Adjust gammastep color temperature
#
# Replaces the old ratflow redshift-adjust script.
# gammastep is the Wayland fork of redshift; it uses the same
# temperature range (1000K–25000K, default 6500K = daylight).
#
# Usage:
# gammastep-adjust -100 — make screen warmer (lower temperature by 100K)
# gammastep-adjust +100 — make screen cooler (raise temperature by 100K)
#
# How it works:
# gammastep doesn't support runtime temperature changes directly.
# This script writes the current temperature to a state file,
# kills any running gammastep, and launches a new one in one-shot
# mode (-O) with the new temperature.
#
# State file: /tmp/gammastep-current-temp
# PID file: /tmp/gammastep-pid
# Default: 6500 (neutral/daylight)
#
# Dependencies: gammastep
#----------------------------------------------------------------
set -x
STATE_FILE="/tmp/gammastep-current-temp"
PID_FILE="/tmp/gammastep-pid"
DEFAULT_TEMP=6500
MIN_TEMP=1000
MAX_TEMP=25000
# Read current temperature or use default
if [ -f "$STATE_FILE" ]; then
CURRENT=$(cat "$STATE_FILE")
else
CURRENT=$DEFAULT_TEMP
fi
# Parse argument
DELTA="${1:-0}"
# Strip the sign to get the absolute value
ABS_DELTA="${DELTA#+}"
ABS_DELTA="${ABS_DELTA#-}"
if [ -z "$ABS_DELTA" ]; then
echo "Usage: gammastep-adjust <+/-N>"
echo " Example: gammastep-adjust -100 (warmer)"
echo " Example: gammastep-adjust +100 (cooler)"
exit 1
fi
# Calculate new temperature
NEW_TEMP=$((CURRENT + DELTA))
# Clamp to valid range
if [ "$NEW_TEMP" -lt "$MIN_TEMP" ]; then
NEW_TEMP=$MIN_TEMP
fi
if [ "$NEW_TEMP" -gt "$MAX_TEMP" ]; then
NEW_TEMP=$MAX_TEMP
fi
# Save new state
echo "$NEW_TEMP" >"$STATE_FILE"
# Kill existing gammastep instance(s)
# Priority 1: use PID file if available (most precise)
if [ -f "$PID_FILE" ]; then
OLD_PID=$(cat "$PID_FILE")
if kill -0 "$OLD_PID" 2>/dev/null; then
kill "$OLD_PID" 2>/dev/null
# Give gammastep a moment to release the display and exit
sleep 0.2
fi
rm -f "$PID_FILE"
fi
# Priority 2: kill any stray gammastep processes
# -x (exact match) ensures we don't kill this script (gammastep-adjust)
pkill -x gammastep 2>/dev/null
sleep 0.1
# Launch gammastep with the new temperature.
#
# IMPORTANT: We use "setsid" (create a new session) so that gammastep is
# completely detached from the shell that Sway spawns for this keybinding.
# Without setsid, Sway kills gammastep when the keybinding shell exits,
# even with "&" — because the process is still in the same process group.
# setsid makes gammastep its own session leader, immune to cleanup.
# if [ "$NEW_TEMP" -ge "$DEFAULT_TEMP" ]; then
# # At or above neutral — reset to no tint
# setsid gammastep -x 2>/dev/null &
# echo $! >"$PID_FILE"
# echo "Temperature reset to neutral ($DEFAULT_TEMP K)"
# else
# Below neutral — apply one-shot manual temperature
setsid gammastep -O "$NEW_TEMP" 2>/dev/null &
echo $! >"$PID_FILE"
echo "Temperature set to $NEW_TEMP K"
# fi