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
11 changes: 10 additions & 1 deletion shell/plugins/services/nightlight/NightlightModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,19 @@ 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) {
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)]
}

if (typeof module !== "undefined") {
module.exports = {
IDENTITY_TEMPERATURE: IDENTITY_TEMPERATURE,
temperatureFromOutput: temperatureFromOutput,
isNightlight: isNightlight
isNightlight: isNightlight,
applyCommand: applyCommand
}
}
4 changes: 1 addition & 3 deletions shell/plugins/services/nightlight/Service.qml
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ 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)]
applyProcess.command = NightlightModel.applyCommand(temp)
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
80 changes: 80 additions & 0 deletions test/shell.d/nightlight-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ set -euo pipefail
source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"

run_node_test <<'JS'
const fs = require('fs')
const nightlight = requireFromRoot('shell/plugins/services/nightlight/NightlightModel.js')
const serviceSource = fs.readFileSync(root + '/shell/plugins/services/nightlight/Service.qml', 'utf8')
const runApply = serviceSource.match(/function runApply\(temp\) \{([\s\S]*?)\n \}/)
const executableRunApply = runApply ? runApply[1].replace(/\/\*[\s\S]*?\*\//g, '').replace(/\/\/.*$/gm, '') : ''

assert(/^\s*applyProcess\.command\s*=\s*NightlightModel\.applyCommand\(temp\)\s*$/m.test(executableRunApply), 'nightlight service delegates its apply process to the tested model command')
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')
Expand Down Expand Up @@ -122,3 +127,78 @@ 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"
if rg -q 'omarchy.indicators' "$ROOT/bin/omarchy-toggle-nightlight"; then
fail "nightlight toggle leaves indicator refresh to the nightlight service"
fi
pass "nightlight toggle leaves indicator refresh to the nightlight service"
Comment thread
manuaudio marked this conversation as resolved.
Resolved

# 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