Skip to content
2 changes: 1 addition & 1 deletion bin/omarchy-restart-app
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
# omarchy:summary=Restart an application by killing it and relaunching via uwsm.
# omarchy:args=<application-name> [application-args...]

pkill -x -- "$1"
pkill -u "$UID" -x -- "$1"
setsid uwsm-app -- "$@" >/dev/null 2>&1 &
9 changes: 5 additions & 4 deletions bin/omarchy-toggle-nightlight
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ if [[ ${1:-} == "--status" ]]; then
exit 0
fi

# Ensure hyprsunset is running. One a few seconds old may still be booting,
# such as one started at login, so it counts as just started too.
# Ensure this user's hyprsunset is running. Another user's process controls a
# different Hyprland session. One a few seconds old may still be booting, such
# as one started at login, so it counts as just started too.
STARTING=false
if ! pgrep -x hyprsunset >/dev/null; then
if ! pgrep -x -u "$UID" hyprsunset >/dev/null; then
setsid uwsm-app -- hyprsunset &
STARTING=true
elif (( $(ps -o etimes= -p "$(pgrep -nx hyprsunset)" 2>/dev/null || echo 0) < 3 )); then
elif (( $(ps -o etimes= -p "$(pgrep -nx -u "$UID" hyprsunset)" 2>/dev/null || echo 0) < 3 )); then
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
STARTING=true
fi

Expand Down
12 changes: 11 additions & 1 deletion shell/plugins/services/nightlight/NightlightModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,20 @@ function isNightlight(temperature) {
return temperature !== null && temperature !== undefined && temperature < IDENTITY_TEMPERATURE
}

// Starts this user's hyprsunset if needed, then applies the temperature.
// Another user's hyprsunset controls a different Hyprland session.
function applyCommand(temperature, resetIdentity) {
return ["bash", "-lc",
"pgrep -x -u \"$UID\" hyprsunset >/dev/null || { setsid uwsm-app -- hyprsunset >/dev/null 2>&1 & sleep 1; }; " +
"hyprctl hyprsunset temperature " + Number(temperature) +
(resetIdentity ? " && hyprctl hyprsunset identity" : "")]
}

if (typeof module !== "undefined") {
module.exports = {
IDENTITY_TEMPERATURE: IDENTITY_TEMPERATURE,
temperatureFromOutput: temperatureFromOutput,
isNightlight: isNightlight
isNightlight: isNightlight,
applyCommand: applyCommand
}
}
7 changes: 2 additions & 5 deletions shell/plugins/services/nightlight/Service.qml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,8 @@ Item {
}

function runApply(temp) {
applyProcess.command = ["bash", "-lc",
"pgrep -x hyprsunset >/dev/null || { setsid uwsm-app -- hyprsunset >/dev/null 2>&1 & sleep 1; }; " +
"hyprctl hyprsunset temperature " + Number(temp) +
// Even daylight tints the screen, so off also restores the identity matrix
(temp === dayTemperature ? " && hyprctl hyprsunset identity" : "")]
// Even daylight tints the screen, so off also restores the identity matrix
applyProcess.command = NightlightModel.applyCommand(temp, temp === dayTemperature)
applyProcess.running = true
}

Expand Down
2 changes: 1 addition & 1 deletion test/shell.d/command-operand-boundaries-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ PATH="$mock_bin:$PATH" OMARCHY_TEST_PKILL_CALLS="$pkill_calls" \
"$ROOT/bin/omarchy-restart-app" "-9 kitty"

pkill_argv=$(<"$pkill_calls")
[[ $pkill_argv == $'<-x>\n<-->\n<-9 kitty>' ]] ||
[[ $pkill_argv == "<-u>"$'\n'"<$UID>"$'\n<-x>\n<-->\n<-9 kitty>' ]] ||
fail "restart-app treats the application name as one literal pkill pattern" "$pkill_argv"
pass "restart-app treats the application name as one literal pkill pattern"

Expand Down
77 changes: 72 additions & 5 deletions test/shell.d/nightlight-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"

run_node_test <<'JS'
const nightlight = requireFromRoot('shell/plugins/services/nightlight/NightlightModel.js')

assertEqual(nightlight.temperatureFromOutput('4000\n'), 4000, 'nightlight parses probe temperature')
assertEqual(nightlight.temperatureFromOutput("Couldn't connect to hyprsunset"), null, 'nightlight treats unreachable hyprsunset as unknown')
assertEqual(nightlight.isNightlight(4000), true, 'nightlight reports warm temperatures as enabled')
assertEqual(nightlight.isNightlight(5999), true, 'nightlight reports warmer-than-identity values as enabled')
assertEqual(nightlight.isNightlight(6000), false, 'nightlight reports identity temperature as disabled')
assertEqual(nightlight.isNightlight(null), false, 'nightlight reports unknown temperature as disabled')
assertEqual(nightlight.applyCommand(6500, true)[2].endsWith(' && hyprctl hyprsunset identity'), true, 'nightlight service resets to the identity matrix when turning off')
JS

TMPDIR=$(mktemp -d)
Expand Down Expand Up @@ -104,10 +104,6 @@ nightlight_cli >/dev/null
[[ $(<"$STATE") == 4000 ]] || fail "nightlight toggle warms the screen from hyprsunset's default"
pass "nightlight toggle warms the screen from hyprsunset's default"

rg -F '(temp === dayTemperature ? " && hyprctl hyprsunset identity" : "")' "$ROOT/shell/plugins/services/nightlight/Service.qml" >/dev/null ||
fail "nightlight service resets to the identity matrix when turning off"
pass "nightlight service resets to the identity matrix when turning off"

# hyprsunset reports its age through ps. A boot still in progress applies its
# default temperature on the first wait, overriding whatever was set before.
BOOT_PENDING="$TMPDIR/boot-pending"
Expand Down Expand Up @@ -144,3 +140,74 @@ chmod +x "$TMPDIR/bin/setsid"
toggle_from_daylight 100
[[ $(<"$STATE") == 4000 && ! -e $BOOT_PENDING ]] || fail "a freshly started hyprsunset can't undo the toggle with its default"
pass "a freshly started hyprsunset can't undo the toggle with its default"

# Another user's hyprsunset always runs here, for their own Hyprland session;
# this user's runs only when OWN_HYPRSUNSET=1. Limited by -u, pgrep finds a match
# only for this user's UID and only when that process exists.
LAUNCH_LOG="$TMPDIR/launches"
rm -f "$TMPDIR/bin/setsid"
cat >"$TMPDIR/bin/pgrep" <<'SH'
#!/bin/bash
while (( $# )); do
if [[ $1 == "-u" ]]; then
[[ $2 == "$TEST_UID" && ${OWN_HYPRSUNSET:-0} == 1 ]]
exit
fi
shift
done
exit 0
SH
cat >"$TMPDIR/bin/uwsm-app" <<'SH'
#!/bin/bash
printf '%s\n' "$*" >>"$LAUNCH_LOG"
SH
chmod +x "$TMPDIR/bin/pgrep" "$TMPDIR/bin/uwsm-app"

wait_for_launch() {
for _ in {1..20}; do
[[ -s $LAUNCH_LOG ]] && break
sleep 0.05
done
}

LAUNCH_LOG="$LAUNCH_LOG" TEST_UID="$UID" nightlight_cli >/dev/null
wait_for_launch
grep -Fqx -- '-- hyprsunset' "$LAUNCH_LOG" || fail "nightlight toggle starts this user's hyprsunset when only another user's runs"

rm -f "$LAUNCH_LOG"
LAUNCH_LOG="$LAUNCH_LOG" TEST_UID="$UID" OWN_HYPRSUNSET=1 nightlight_cli >/dev/null
sleep 0.2
[[ ! -e $LAUNCH_LOG ]] || fail "nightlight toggle leaves a running hyprsunset of this user's alone"
pass "nightlight toggle only counts this user's hyprsunset as running"

# The shell service runs its script in a login shell; run it without -l here, so
# no login profile can put the real commands ahead of these mocks.
mapfile -d '' -t apply_command < <(run_node_test <<'JS'
const nightlight = requireFromRoot('shell/plugins/services/nightlight/NightlightModel.js')
process.stdout.write(nightlight.applyCommand(4000).map(arg => arg + '\0').join(''))
JS
)
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
[[ ${apply_command[0]:-} == "bash" && ${apply_command[1]:-} == "-lc" ]] || fail "the nightlight service applies through a login shell" "${apply_command[*]}"

nightlight_service() {
PATH="$TMPDIR/bin:$PATH" \
HYPRSUNSET_STATE="$STATE" \
OMARCHY_SHELL_LOG="$SHELL_LOG" \
LAUNCH_LOG="$LAUNCH_LOG" \
TEST_UID="$UID" \
bash -c "${apply_command[2]}"
}

printf '6500\n' >"$STATE"
rm -f "$LAUNCH_LOG"
nightlight_service
wait_for_launch
grep -Fqx -- '-- hyprsunset' "$LAUNCH_LOG" || fail "nightlight service starts this user's hyprsunset when only another user's runs"
[[ $(<"$STATE") == 4000 ]] || fail "nightlight service applies the requested temperature"

printf '6500\n' >"$STATE"
rm -f "$LAUNCH_LOG"
OWN_HYPRSUNSET=1 nightlight_service
sleep 0.2
[[ ! -e $LAUNCH_LOG && $(<"$STATE") == 4000 ]] || fail "nightlight service leaves a running hyprsunset of this user's alone"
pass "nightlight service only counts this user's hyprsunset as running"
Loading