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-hyprland-window-close
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash

# omarchy:summary=Close the focused Hyprland window, force-killing XWayland zombies
# omarchy:hidden=true

# Some apps (notably JUCE/XWayland clients like Surge XT) accept the close
# request and quit their process while leaving an empty shell mapped. Clear
# that leftover surface only after confirming the XWayland PID is gone (#13308).

active=$(hyprctl activewindow -j 2>/dev/null) || exit 0
addr=$(jq -r '.address // empty' <<<"$active")
pid=$(jq -r '.pid // 0' <<<"$active")

[[ -n $addr ]] || exit 0

hyprctl dispatch "hl.dsp.window.close({ window = \"address:$addr\" })" >/dev/null 2>&1 ||
hyprctl dispatch closewindow "address:$addr" >/dev/null 2>&1 || true

# Give clients a beat to finish a cooperative close. Remaining mapped can
# also mean the client is waiting for an answer to a save/discard dialog.
for _ in 1 2 3 4 5; do
sleep 0.05
if ! hyprctl clients -j 2>/dev/null | jq -e --arg addr "$addr" \
'any(.[]; .address == $addr)' >/dev/null 2>&1; then
exit 0
fi
Comment on lines +23 to +26

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Query errors look like closure If hyprctl clients -j fails or returns malformed JSON, the failed check is treated as though the window disappeared. The script exits without checking whether a still-mapped surface needs cleanup, and gives no indication that the query failed.

done

# Only a confirmed orphaned XWayland surface warrants cleanup. Live clients
# may still be handling the close request; never signal their entire process.
if ! jq -e '.xwayland == true' <<<"$active" >/dev/null 2>&1 ||
[[ ! $pid =~ ^[1-9][0-9]*$ ]] || [[ -e /proc/$pid ]]; then
exit 0
fi

if hyprctl clients -j 2>/dev/null | jq -e --arg addr "$addr" \
'any(.[]; .address == $addr)' >/dev/null 2>&1; then
hyprctl dispatch "hl.dsp.window.close({ window = \"address:$addr\" })" >/dev/null 2>&1 ||
hyprctl dispatch killwindow "address:$addr" >/dev/null 2>&1 ||
Comment on lines +38 to +39

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Force close never runs When an XWayland surface remains mapped after the first close and its PID is gone, this branch sends another cooperative close. That dispatch can succeed even though the surface stays mapped, so || skips killwindow and the empty window remains.

hyprctl dispatch closewindow "address:$addr" >/dev/null 2>&1 || true
fi
4 changes: 2 additions & 2 deletions default/hypr/bindings/tiling.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
o.bind("SUPER + W", "Close window", hl.dsp.window.close())
o.bind("SUPER + Q", "Close window", hl.dsp.window.close())
o.bind("SUPER + W", "Close window", "omarchy-hyprland-window-close")
o.bind("SUPER + Q", "Close window", "omarchy-hyprland-window-close")
o.bind("CTRL + ALT + DELETE", "Close all windows", "omarchy-hyprland-window-close-all")

o.bind("SUPER + J", "Toggle window split", hl.dsp.layout("togglesplit"))
Expand Down
147 changes: 147 additions & 0 deletions test/shell.d/hyprland-window-close-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#!/bin/bash

set -euo pipefail

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

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

mock_bin="$tmpdir/bin"
log_file="$tmpdir/log"
state_dir="$tmpdir/state"
mkdir -p "$mock_bin" "$state_dir"

: >"$log_file"
cat >"$mock_bin/hyprctl" <<'SH'
#!/bin/bash
printf '%s\n' "$*" >>"$TEST_LOG"
case $1 in
activewindow)
printf '{"address":"0xdead","pid":4242,"xwayland":true,"fullscreen":0}\n'
;;
clients)
if [[ -f $STATE_DIR/closed ]]; then
printf '[]\n'
else
printf '[{"address":"0xdead","pid":4242}]\n'
fi
;;
dispatch)
touch "$STATE_DIR/closed"
;;
esac
SH
cat >"$mock_bin/sleep" <<'SH'
#!/bin/bash
exit 0
SH
chmod +x "$mock_bin"/*

PATH="$mock_bin:$PATH" TEST_LOG="$log_file" STATE_DIR="$state_dir" \
bash "$ROOT/bin/omarchy-hyprland-window-close"

grep -q 'window.close({ window = "address:0xdead" })' "$log_file" ||
fail "window-close dispatches a cooperative close" "log: $(< "$log_file")"
closes=$(grep -c '^dispatch ' "$log_file" || true)
(( closes == 1 )) || fail "unmapped window receives only one close"
pass "window-close dispatches a cooperative close without force-kill"

# A mapped live client may be waiting for a save/discard answer.
cat >"$mock_bin/kill" <<'SH'
#!/bin/bash
printf '%s\n' "$*" >>"$SIGNAL_LOG"
SH
cat >"$mock_bin/hyprctl" <<'SH'
#!/bin/bash
printf '%s\n' "$*" >>"$TEST_LOG"
case $1 in
activewindow) printf '%s\n' "$ACTIVE_WINDOW" ;;
clients) printf '[{"address":"0xdead"}]\n' ;;
esac
SH
chmod +x "$mock_bin"/*
signal_log="$tmpdir/signals"

assert_cooperative_only() {
: >"$log_file"
: >"$signal_log"
PATH="$mock_bin:$PATH" TEST_LOG="$log_file" SIGNAL_LOG="$signal_log" ACTIVE_WINDOW="$1" \
bash -c 'enable -n kill; source "$1"' bash "$ROOT/bin/omarchy-hyprland-window-close"

[[ ! -s $signal_log ]] ||
fail "$2 does not signal the process" "signals: $(< "$signal_log")"
closes=$(grep -c '^dispatch ' "$log_file" || true)
(( closes == 1 )) ||
fail "$2 sends exactly one cooperative close" "log: $(< "$log_file")"
grep -q 'window.close({' "$log_file" ||
fail "$2 sends a cooperative close"
if grep -q 'killwindow' "$log_file"; then
fail "$2 does not force-clear the surface"
fi
pass "$2 remains cooperative"
}

for xwayland in false true; do
assert_cooperative_only \
"{\"address\":\"0xdead\",\"pid\":$$,\"xwayland\":$xwayland}" \
"live client (xwayland=$xwayland) awaiting a save/discard answer"
done

for pid_data in '' ',"pid":null' ',"pid":0' ',"pid":-1' ',"pid":"invalid"'; do
assert_cooperative_only \
"{\"address\":\"0xdead\",\"xwayland\":true$pid_data}" \
"XWayland client with missing or invalid PID ($pid_data)"
done

# Choose a PID beyond the kernel's PID range and verify it is absent.
missing_pid=$(< /proc/sys/kernel/pid_max)
(( missing_pid += 1 ))
[[ ! -e /proc/$missing_pid ]] || fail "orphan fixture PID is absent"
assert_cooperative_only \
"{\"address\":\"0xdead\",\"pid\":$missing_pid,\"xwayland\":false}" \
"native Wayland client with absent PID"

# Zombie path: window stays mapped; pid has no /proc entry.
: >"$log_file"
rm -f "$state_dir"/*
cat >"$mock_bin/hyprctl" <<'SH'
#!/bin/bash
printf '%s\n' "$*" >>"$TEST_LOG"
case $1 in
activewindow)
printf '{"address":"0xzombie","pid":%s,"xwayland":true,"fullscreen":0}\n' "$MISSING_PID"
;;
clients)
if [[ -f $STATE_DIR/force_closed ]]; then
printf '[]\n'
else
printf '[{"address":"0xzombie","pid":%s}]\n' "$MISSING_PID"
fi
;;
dispatch)
closes=0
[[ -f $STATE_DIR/closes ]] && closes=$(wc -l <"$STATE_DIR/closes")
echo x >>"$STATE_DIR/closes"
if (( closes >= 1 )); then
touch "$STATE_DIR/force_closed"
fi
;;
esac
SH
chmod +x "$mock_bin/hyprctl"

PATH="$mock_bin:$PATH" TEST_LOG="$log_file" STATE_DIR="$state_dir" MISSING_PID="$missing_pid" \
bash "$ROOT/bin/omarchy-hyprland-window-close"

grep -q 'window.close({ window = "address:0xzombie" })' "$log_file" ||
fail "zombie close still sends an initial close" "log: $(< "$log_file")"
# Second close / killwindow after the surface refused to leave.
closes=$(grep -c '0xzombie' "$log_file" || true)
(( closes >= 2 )) ||
fail "zombie close force-targets the stuck surface" "log: $(< "$log_file")"
Comment on lines +137 to +142

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Zombie test misses force close The mock marks the window closed after any second dispatch, while this assertion only counts uses of the window address. Two cooperative closes therefore pass the test without calling killwindow, leaving the force-clear behavior untested.

pass "window-close force-clears an XWayland zombie that ignores close"

grep -q 'omarchy-hyprland-window-close' "$ROOT/default/hypr/bindings/tiling.lua" ||
fail "tiling bindings use omarchy-hyprland-window-close"
pass "tiling bindings use omarchy-hyprland-window-close"