Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 44 additions & 0 deletions bin/omarchy-hyprland-window-close
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/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 UI while leaving an empty shell mapped. A short wait
# followed by a force-kill clears that leftover surface (#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

# Wayland-native clients usually unmap immediately. Give XWayland a beat to
# finish a cooperative close before deciding the surface is stuck.
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

# Still mapped: either the client ignored close, or it died and left a ghost.

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.

please keep ordinary close requests cooperative: a window can still be mapped after 250 ms because it is waiting for the user to answer a save/discard dialog, and this path sends its entire process SIGTERM then SIGKILL (including native wayland clients). gate force-killing on an explicit user action or a positively identified zombie, otherwise super+w can discard unsaved work and close other windows of the same app.

# Prefer /proc over the kill builtin so tests and exotic PATH layouts work.
if [[ $pid =~ ^[1-9][0-9]*$ ]] && [[ -d /proc/$pid ]]; then
command kill "$pid" 2>/dev/null || true
sleep 0.1
if [[ -d /proc/$pid ]]; then
command kill -9 "$pid" 2>/dev/null || true
fi
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
90 changes: 90 additions & 0 deletions test/shell.d/hyprland-window-close-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/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")"
pass "window-close dispatches a cooperative close without force-kill"

# 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":99999,"xwayland":true,"fullscreen":0}\n'
;;
clients)
if [[ -f $STATE_DIR/force_closed ]]; then
printf '[]\n'
else
printf '[{"address":"0xzombie","pid":99999}]\n'
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" \
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"