Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
41 changes: 41 additions & 0 deletions bin/omarchy-system-sleep-lock
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,25 @@ sync_clamshell() {
omarchy-hyprland-monitor-clamshell >/dev/null 2>&1 || true
}

# Undocking with the lid closed disables eDP-1 (clamshell) and then suspends
# before the internal panel is re-enabled, so the lock client has no real
# output for a surface (#13300). Wait for at least one enabled, named monitor
# before treating the session as ready to sleep.
has_active_output() {
hyprctl monitors -j 2>/dev/null | jq -e \
'[.[] | select(.disabled != true and (.name // "") != "" and .name != "FALLBACK")] | length > 0' \
>/dev/null 2>&1
}

wait_for_active_output() {
while (( $(remaining_ms) > 0 )); do
has_active_output && return 0
sync_clamshell
sleep "$poll_interval"
done
return 1
}

# logind suspends whether or not this wait succeeded, so a failure here means
# the machine slept with the session exposed. The notification is the only way
# anyone finds out, and it lands on the screen they unlock into.
Expand All @@ -109,9 +128,31 @@ report_unsecured() {
request_lock
sync_clamshell

# Re-enable the internal panel (and any other recoverable output) before we
# insist on a secure lock surface. Without an active output the shell refuses
# to create a lock surface and resume wakes to a black screen.
had_output=0
if has_active_output; then
had_output=1
elif ! wait_for_active_output; then
report_unsecured "no active display was available before suspend"
fi

# A lock requested while headless may never have produced a surface; ask again
# only when we had to wait for a real output to appear.
if (( ! had_output )); then
request_lock
fi

# The trailing sleep can overshoot the deadline by one interval, which is well
# inside the reserve derive_budget_ms already held back for logind.
while (( $(remaining_ms) > 0 )); do
if ! has_active_output; then
sync_clamshell
sleep "$poll_interval"
continue
fi

case $(lock_state) in
secure) exit 0 ;;
locking) ;;
Expand Down
117 changes: 115 additions & 2 deletions test/shell.d/sleep-lock-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ setup_scenario() {
printf '%s\n' "\$*" >>"$notify_log"
SH
chmod +x "$mock_bin/omarchy-notification-send"

# Sleep lock now refuses to finish without an enabled, named output so a
# clamshell undock cannot suspend headless. Default every scenario to one
# healthy internal panel; headless cases override this stub.
mock_monitors_active
}

mock_logind_window() {
Expand All @@ -54,6 +59,38 @@ SH
chmod +x "$mock_bin/omarchy-hyprland-monitor-clamshell"
}

mock_monitors_active() {
cat >"$mock_bin/hyprctl" <<'SH'
#!/bin/bash

printf 'hyprctl %s\n' "$*" >>"$CALL_LOG"
if [[ $1 == "monitors" ]]; then
printf '[{"name":"eDP-1","disabled":false}]\n'
exit 0
fi
exit 0
SH
chmod +x "$mock_bin/hyprctl"
}

mock_monitors_headless() {
cat >"$mock_bin/hyprctl" <<'SH'
#!/bin/bash

printf 'hyprctl %s\n' "$*" >>"$CALL_LOG"
if [[ $1 == "monitors" ]]; then
if [[ -f $STATE_DIR/output_ready ]]; then
printf '[{"name":"eDP-1","disabled":false}]\n'
else
printf '[{"name":"FALLBACK","disabled":false},{"name":"","disabled":false}]\n'
fi
exit 0
fi
exit 0
SH
chmod +x "$mock_bin/hyprctl"
}

# Called with no budget to exercise the value derived from logind's window.
run_sleep_lock() {
local args=()
Expand Down Expand Up @@ -95,8 +132,8 @@ pass "sleep lock succeeds once the session reports secure"
fail "sleep lock requests the session lock first" "first call: ${calls[0]}"
pass "sleep lock requests the session lock first"

[[ ${calls[1]} == "clamshell" && ${calls[2]} == "shell lock status" ]] ||
fail "sleep lock checks security after clamshell reconciliation"
[[ ${calls[*]} == *clamshell* && ${calls[*]} == *"shell lock status"* ]] ||
fail "sleep lock checks security after clamshell reconciliation" "calls: ${calls[*]}"
pass "sleep lock checks security after clamshell reconciliation"

(( elapsed_us < 1500000 )) ||
Expand Down Expand Up @@ -339,3 +376,79 @@ budget_cap_ms=$(sed -n 's/^budget_cap_ms=//p' "$sleep_lock")
fail "sleep lock cap leaves logind room to act" \
"cap: ${budget_cap_ms}ms window: ${inhibit_delay}s"
pass "sleep lock cap stays inside the shipped logind inhibitor window"

# Undocking with the lid closed briefly leaves only FALLBACK/empty outputs.
# Sleep lock must keep reconciling clamshell until a real panel appears, then
# secure the session — otherwise resume wakes to a black screen (#13300).
setup_scenario undock_headless_then_recover
cat >"$mock_bin/omarchy-shell" <<'SH'
#!/bin/bash

printf 'shell %s\n' "$*" >>"$CALL_LOG"
if [[ $* == "lock lock" ]]; then
printf 'ok\n'
elif [[ $* == "lock status" ]]; then
if [[ -f $STATE_DIR/output_ready ]]; then
printf '{"secure":true}\n'
else
printf '{"secure":false,"requested":true,"pending":true}\n'
fi
fi
SH
chmod +x "$mock_bin/omarchy-shell"
mock_monitors_headless
cat >"$mock_bin/omarchy-hyprland-monitor-clamshell" <<'SH'
#!/bin/bash
echo clamshell >>"$CALL_LOG"
# The second clamshell sync is what re-enables eDP after undock.
if [[ -f $STATE_DIR/clamshell_once ]]; then
touch "$STATE_DIR/output_ready"
else
touch "$STATE_DIR/clamshell_once"
fi
SH
chmod +x "$mock_bin/omarchy-hyprland-monitor-clamshell"

run_sleep_lock 4000

(( exit_status == 0 )) ||
fail "sleep lock recovers an active output before securing" "exit: $exit_status"
pass "sleep lock recovers an active output before securing"

[[ -f $state_dir/output_ready ]] ||
fail "sleep lock waited for clamshell to re-enable a real output"
pass "sleep lock waited for clamshell to re-enable a real output"

# A permanently headless suspend must fail closed inside the budget rather than
# reporting secure against a FALLBACK placeholder.
setup_scenario undock_headless_forever
cat >"$mock_bin/omarchy-shell" <<'SH'
#!/bin/bash

printf 'shell %s\n' "$*" >>"$CALL_LOG"
if [[ $* == "lock lock" ]]; then
printf 'ok\n'
elif [[ $* == "lock status" ]]; then
printf '{"secure":true}\n'
fi
SH
chmod +x "$mock_bin/omarchy-shell"
cat >"$mock_bin/hyprctl" <<'SH'
#!/bin/bash
printf 'hyprctl %s\n' "$*" >>"$CALL_LOG"
if [[ $1 == "monitors" ]]; then
printf '[{"name":"FALLBACK","disabled":false}]\n'
exit 0
fi
exit 0
SH
chmod +x "$mock_bin/hyprctl"
mock_clamshell

run_sleep_lock 1500

(( exit_status != 0 )) ||
fail "sleep lock fails when only FALLBACK outputs exist"
grep -qF "no active display was available before suspend" "$journal_log" ||
fail "sleep lock names the missing-display failure" "journal: $(< "$journal_log")"
pass "sleep lock fails closed when undock leaves no active output"