Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions bin/omarchy-launch-screensaver
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
# Exit early if screensaver is already running
pgrep -f '[o]rg.omarchy.screensaver' && exit 0

# The idle service passes --idle so a screensaver that never takes focus keeps
# the lock armed. Menu and branding launches do not.
screensaver_cmd=(omarchy-screensaver)
if [[ ${1:-} == --idle ]]; then
screensaver_cmd=(omarchy-screensaver --idle)
shift
fi

# Allow screensaver to be turned off but also force started
if omarchy-toggle-enabled screensaver-off && [[ $1 != "force" ]]; then
exit 1
Expand Down Expand Up @@ -54,16 +62,16 @@ for m in $(hyprctl monitors -j | jq -r '.[] | .name'); do

case $terminal in
*Alacritty*)
hypr_exec alacritty --class=org.omarchy.screensaver --config-file "$OMARCHY_PATH/default/alacritty/screensaver.toml" -e omarchy-screensaver
hypr_exec alacritty --class=org.omarchy.screensaver --config-file "$OMARCHY_PATH/default/alacritty/screensaver.toml" -e "${screensaver_cmd[@]}"
;;
*ghostty*)
hypr_exec ghostty --class=org.omarchy.screensaver --config-file="$OMARCHY_PATH/default/ghostty/screensaver" --font-size=18 -e omarchy-screensaver
hypr_exec ghostty --class=org.omarchy.screensaver --config-file="$OMARCHY_PATH/default/ghostty/screensaver" --font-size=18 -e "${screensaver_cmd[@]}"
;;
*foot*)
hypr_exec foot --app-id=org.omarchy.screensaver --config="$OMARCHY_PATH/default/foot/screensaver.ini" -e omarchy-screensaver
hypr_exec foot --app-id=org.omarchy.screensaver --config="$OMARCHY_PATH/default/foot/screensaver.ini" -e "${screensaver_cmd[@]}"
;;
*kitty*)
hypr_exec kitty --class=org.omarchy.screensaver --override font_size=18 --override window_padding_width=0 -e omarchy-screensaver
hypr_exec kitty --class=org.omarchy.screensaver --override font_size=18 --override window_padding_width=0 -e "${screensaver_cmd[@]}"
;;
esac

Expand Down
41 changes: 40 additions & 1 deletion bin/omarchy-screensaver
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,52 @@ screensaver_in_focus() {
hyprctl activewindow -j | jq -e '.class == "org.omarchy.screensaver"' >/dev/null 2>&1
}

# The screensaver may not be the active window when the first poll runs.
# Treat "not focused" as a dismissal only after it has been focused once.
# A launch that never receives focus gives up after a few polls. An idle-cycle
# launch (--idle) does not: the idle service would treat that exit as a
# dismissal and cancel the pending lock.
screensaver_seen_focus=0
screensaver_idle=0
screensaver_unfocused_polls=0
screensaver_unfocused_limit=8

screensaver_lost_focus() {
if screensaver_in_focus; then
screensaver_seen_focus=1
return 1
fi
if ((screensaver_seen_focus)); then
return 0
fi
screensaver_unfocused_polls=$((screensaver_unfocused_polls + 1))
if ((screensaver_idle == 0 && screensaver_unfocused_polls >= screensaver_unfocused_limit)); then
return 0
fi
return 1
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
}

exit_screensaver() {
hyprctl eval 'hl.config({ cursor = { invisible = false } })' &>/dev/null || hyprctl keyword cursor:invisible false &>/dev/null || true
pkill -x ttfx 2>/dev/null
pkill -f '[o]rg.omarchy.screensaver' 2>/dev/null
exit 0
}

# Above the source guard so a test can call it without starting ttfx.
screensaver_apply_args() {
local arg
for arg in "$@"; do
[[ $arg == --idle ]] && screensaver_idle=1
done
}

screensaver_apply_args "$@"

if [[ ${BASH_SOURCE[0]} != "$0" ]]; then
return 0
fi

# Exit the screensaver on signals and input from keyboard and mouse
trap exit_screensaver SIGINT SIGTERM SIGHUP SIGQUIT

Expand Down Expand Up @@ -41,7 +80,7 @@ while true; do
--random-effect --no-eol --no-restore-cursor &

while pgrep -t "${tty#/dev/}" -x ttfx >/dev/null; do
if read -n1 -t 1 || ! screensaver_in_focus; then
if read -n1 -t 1 || screensaver_lost_focus; then
exit_screensaver
fi
done
Expand Down
2 changes: 1 addition & 1 deletion shell/plugins/services/idle/Service.qml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Item {
function launchScreensaver() {
root.screensaverStartedThisCycle = true
screensaverLaunchGraceTimer.restart()
runProcess(screensaverProcess, "screensaver", "[[ $(omarchy-shell lock isLocked 2>/dev/null) == \"true\" ]] || omarchy-launch-screensaver")
runProcess(screensaverProcess, "screensaver", "[[ $(omarchy-shell lock isLocked 2>/dev/null) == \"true\" ]] || omarchy-launch-screensaver --idle")
}

function lockSystem(reason) {
Expand Down
68 changes: 68 additions & 0 deletions test/shell.d/launch-screensaver-idle-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/bin/bash

set -euo pipefail

source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"

require_command jq

test_tmp=$(mktemp -d)
trap 'rm -rf "$test_tmp"' EXIT
mock_bin="$test_tmp/bin"
call_log="$test_tmp/calls"
mkdir -p "$mock_bin" "$test_tmp/home" "$test_tmp/runtime"

cat >"$mock_bin/pgrep" <<'SH'
#!/bin/bash
exit 1
SH
cat >"$mock_bin/omarchy-toggle-enabled" <<'SH'
#!/bin/bash
exit 1
SH
cat >"$mock_bin/omarchy-hyprland-monitor-focused" <<'SH'
#!/bin/bash
printf 'DP-1\n'
SH
cat >"$mock_bin/xdg-terminal-exec" <<'SH'
#!/bin/bash
printf '%s\n' "$TEST_TERMINAL"
SH
cat >"$mock_bin/hyprctl" <<'SH'
#!/bin/bash
if [[ $1 == "monitors" ]]; then
printf '[{"name":"DP-1"}]\n'
else
printf '%s\n' "$*" >>"$CALL_LOG"
fi
SH
cat >"$mock_bin/socat" <<'SH'
#!/bin/bash
printf 'openwindow>>1,1,org.omarchy.screensaver,Screensaver\n'
SH
chmod +x "$mock_bin"/*

# Run the actual launcher with all compositor, process, and event-stream calls
# stubbed. Check the command it sends, rather than the array's source spelling.
for terminal in Alacritty ghostty foot kitty; do
for mode in manual idle; do
args=()
[[ $mode == "idle" ]] && args=(--idle)
: >"$call_log"
HOME="$test_tmp/home" XDG_RUNTIME_DIR="$test_tmp/runtime" \
HYPRLAND_INSTANCE_SIGNATURE=test OMARCHY_PATH="$ROOT" \
PATH="$mock_bin:$PATH" TEST_TERMINAL="$terminal" CALL_LOG="$call_log" \
bash "$ROOT/bin/omarchy-launch-screensaver" "${args[@]}"

mapfile -t launches < <(rg 'hl\.dsp\.exec_cmd' "$call_log")
(( ${#launches[@]} == 1 )) || fail "$terminal $mode launches one screensaver"
if [[ $mode == "idle" ]]; then
[[ ${launches[0]} == *"-e omarchy-screensaver --idle "* ]] ||
fail "$terminal forwards --idle to the screensaver" "${launches[0]}"
else
[[ ${launches[0]} == *"-e omarchy-screensaver "* && ${launches[0]} != *"--idle"* ]] ||
fail "$terminal manual launch stays on the manual path" "${launches[0]}"
fi
pass "$terminal $mode forwards the correct screensaver arguments"
done
done
112 changes: 112 additions & 0 deletions test/shell.d/screensaver-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#!/bin/bash

set -euo pipefail

source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"

require_command jq
require_command rg

if rg -q 'read -n1 -t 1 \|\| ! screensaver_in_focus' "$ROOT/bin/omarchy-screensaver"; then
fail "screensaver does not treat the first unfocused poll as dismissal"
fi
grep -Fq 'read -n1 -t 1 || screensaver_lost_focus' "$ROOT/bin/omarchy-screensaver" ||
fail "screensaver watchdog dismisses through screensaver_lost_focus"
grep -Fq 'omarchy-launch-screensaver --idle' "$ROOT/shell/plugins/services/idle/Service.qml" ||
fail "idle cycle launches the screensaver with --idle"
grep -Fq 'omarchy-screensaver --idle' "$ROOT/bin/omarchy-launch-screensaver" ||
fail "screensaver launcher forwards --idle"
pass "screensaver does not treat the first unfocused poll as dismissal"

test_tmp=$(mktemp -d)
trap 'rm -rf "$test_tmp"' EXIT

mock_bin="$test_tmp/bin"
window_file="$test_tmp/window.json"
mkdir -p "$mock_bin"

cat >"$mock_bin/hyprctl" <<'SH'
#!/bin/bash

if [[ $1 == activewindow && $2 == -j ]]; then
cat "$HYPRCTL_WINDOW"
exit 0
fi
exit 0
SH
chmod +x "$mock_bin/hyprctl"

write_window() {
printf '{"class":"%s"}\n' "$1" >"$window_file"
}

export PATH="$mock_bin:$PATH"
export HYPRCTL_WINDOW="$window_file"
source "$ROOT/bin/omarchy-screensaver"

write_window foot
screensaver_seen_focus=0
if screensaver_lost_focus; then
fail "screensaver stays up before it has been focused"
fi
if screensaver_lost_focus; then
fail "screensaver stays up across repeated unfocused polls before first focus"
fi
pass "screensaver stays up before it has been focused"

write_window org.omarchy.screensaver
if screensaver_lost_focus; then
fail "screensaver stays up once it is focused"
fi
(( screensaver_seen_focus == 1 )) || fail "screensaver records that it has been focused"
pass "screensaver stays up once it is focused"

write_window foot
if screensaver_lost_focus; then
pass "screensaver dismisses after losing focus"
else
fail "screensaver dismisses after losing focus"
fi

write_window foot
screensaver_seen_focus=0
screensaver_unfocused_polls=0
screensaver_idle=0
gave_up=0
for ((i = 0; i < screensaver_unfocused_limit; i++)); do
if screensaver_lost_focus; then
gave_up=1
break
fi
done
((gave_up == 1)) || fail "manual screensaver gives up when it never receives focus"
pass "manual screensaver gives up when it never receives focus"

write_window foot
screensaver_seen_focus=0
screensaver_unfocused_polls=0
screensaver_idle=1
for ((i = 0; i < screensaver_unfocused_limit + 2; i++)); do
if screensaver_lost_focus; then
fail "idle screensaver keeps waiting for focus"
fi
done
pass "idle screensaver keeps waiting for focus"

screensaver_idle=0
source "$ROOT/bin/omarchy-screensaver" --idle
((screensaver_idle == 1)) || fail "screensaver --idle is recorded before the watchdog runs"
for ((i = 0; i < screensaver_unfocused_limit + 2; i++)); do
if screensaver_lost_focus; then
fail "parsed --idle keeps an unfocused screensaver waiting"
fi
done
screensaver_idle=0
source "$ROOT/bin/omarchy-screensaver"
((screensaver_idle == 0)) || fail "screensaver without --idle stays on the manual path"
pass "screensaver parses --idle before the source guard and keeps waiting for focus"

if rg -n -- '(-e|--) omarchy-screensaver( |$)' "$ROOT/bin/omarchy-launch-screensaver"; then
fail "terminal launchers must forward the screensaver command including --idle"
fi
pass "screensaver launcher has no bare terminal command that drops --idle"