fix(wm): handle WM_CHANGE_STATE and WM_STATE for Wine/DXVK focus recovery

- Track iconic state on hidden clients and reply to WM_CHANGE_STATE(IconicState)
  with IconicState while hidden on an inactive tag (or NormalState if visible),
  restoring NormalState in showhide() before WM_TAKE_FOCUS on workspace return.
- Re-assert _NET_WM_STATE PropertyNotify on no-op client messages and always
  emit ICCCM ConfigureNotify replies for hidden/fullscreen ConfigureRequests.
- Keep hidden fullscreen clients offscreen during root ConfigureNotify and run
  arrange()/restack() before focus().
- Add test_wine_dxvk_iconic_workspace_switch() in tests/test_x11_xvfb.py.
This commit is contained in:
Luxferre committed 2026-11-26 20:18:48 +02:00
1 parent 8da5c5d1cb
commit 08fd92f184
2 files changed
+387 -10

No files matched your search

+328
View File
@@ -1227,6 +1227,332 @@ def test_wine_take_focus_workspace_switch():
terminate(xvfb)
def test_wine_dxvk_iconic_workspace_switch():
if not shutil.which("xdotool"):
print("test_x11_xvfb: xdotool not installed (skipping Wine/DXVK iconic focus test)")
return
disp = pick_display()
with tempfile.TemporaryDirectory() as home:
env = dict(os.environ)
env["DISPLAY"] = disp
env["HOME"] = home
env["XDG_CONFIG_HOME"] = os.path.join(home, ".config")
env["ANODE_PREFIX"] = ROOT
env["ANODE_NO_SIGNAL"] = "1"
env["PATH"] = (
os.path.join(BUILD, "dwm") + ":" +
os.path.join(BUILD, "st") + ":" +
os.path.join(ROOT, "scripts") + ":" +
env.get("PATH", "")
)
subprocess.run([os.path.join(ROOT, "scripts", "anode-apply-scale"), "1.00", "--touch=0"], env=env, check=True)
xvfb = subprocess.Popen(
["Xvfb", disp, "-noreset", "-screen", "0", "1280x800x24", "-nolisten", "tcp"],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
)
dpy = None
dwm = None
wine_sim = None
tag2_client = None
try:
dpy = wait_until(lambda: X11.XOpenDisplay(disp.encode()))
assert dpy, f"Failed to open Xvfb {disp}"
dwm = subprocess.Popen([os.path.join(BUILD, "dwm", "dwm")], env=env)
tray_atom = X11.XInternAtom(dpy, b"_NET_SYSTEM_TRAY_S0", 0)
assert wait_until(lambda: X11.XGetSelectionOwner(dpy, tray_atom)), "DWM failed to start on Xvfb"
sim_code = (
"import ctypes, ctypes.util, json, time\n"
"X11 = ctypes.CDLL(ctypes.util.find_library('X11') or 'libX11.so.6')\n"
"X11.XOpenDisplay.restype = ctypes.c_void_p\n"
"X11.XDefaultRootWindow.argtypes = [ctypes.c_void_p]\n"
"X11.XDefaultRootWindow.restype = ctypes.c_ulong\n"
"X11.XDefaultScreen.argtypes = [ctypes.c_void_p]\n"
"X11.XDefaultScreen.restype = ctypes.c_int\n"
"X11.XCreateSimpleWindow.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.c_int, ctypes.c_int, ctypes.c_uint, ctypes.c_uint, ctypes.c_uint, ctypes.c_ulong, ctypes.c_ulong]\n"
"X11.XCreateSimpleWindow.restype = ctypes.c_ulong\n"
"X11.XInternAtom.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_int]\n"
"X11.XInternAtom.restype = ctypes.c_ulong\n"
"X11.XChangeProperty.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.c_ulong, ctypes.c_ulong, ctypes.c_int, ctypes.c_int, ctypes.c_void_p, ctypes.c_int]\n"
"X11.XGetWindowProperty.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.c_ulong, ctypes.c_long, ctypes.c_long, ctypes.c_int, ctypes.c_ulong, ctypes.POINTER(ctypes.c_ulong), ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_ulong), ctypes.POINTER(ctypes.c_ulong), ctypes.POINTER(ctypes.POINTER(ctypes.c_ubyte))]\n"
"X11.XGetWindowProperty.restype = ctypes.c_int\n"
"X11.XSetWMProtocols.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.POINTER(ctypes.c_ulong), ctypes.c_int]\n"
"class XWMHints(ctypes.Structure):\n"
" _fields_ = [('flags', ctypes.c_long), ('input', ctypes.c_int), ('initial_state', ctypes.c_int),\n"
" ('icon_pixmap', ctypes.c_ulong), ('icon_window', ctypes.c_ulong),\n"
" ('icon_x', ctypes.c_int), ('icon_y', ctypes.c_int),\n"
" ('icon_mask', ctypes.c_ulong), ('window_group', ctypes.c_ulong)]\n"
"class XFocusChangeEvent(ctypes.Structure):\n"
" _fields_ = [('type', ctypes.c_int), ('serial', ctypes.c_ulong), ('send_event', ctypes.c_int),\n"
" ('display', ctypes.c_void_p), ('window', ctypes.c_ulong), ('mode', ctypes.c_int), ('detail', ctypes.c_int)]\n"
"class XConfigureEvent(ctypes.Structure):\n"
" _fields_ = [('type', ctypes.c_int), ('serial', ctypes.c_ulong), ('send_event', ctypes.c_int),\n"
" ('display', ctypes.c_void_p), ('event', ctypes.c_ulong), ('window', ctypes.c_ulong),\n"
" ('x', ctypes.c_int), ('y', ctypes.c_int), ('width', ctypes.c_int), ('height', ctypes.c_int),\n"
" ('border_width', ctypes.c_int), ('above', ctypes.c_ulong), ('override_redirect', ctypes.c_int)]\n"
"class XPropertyEvent(ctypes.Structure):\n"
" _fields_ = [('type', ctypes.c_int), ('serial', ctypes.c_ulong), ('send_event', ctypes.c_int),\n"
" ('display', ctypes.c_void_p), ('window', ctypes.c_ulong), ('atom', ctypes.c_ulong),\n"
" ('time', ctypes.c_ulong), ('state', ctypes.c_int)]\n"
"class XClientMessageData(ctypes.Union):\n"
" _fields_ = [('b', ctypes.c_char * 20), ('s', ctypes.c_short * 10), ('l', ctypes.c_long * 5)]\n"
"class XClientMessageEvent(ctypes.Structure):\n"
" _fields_ = [('type', ctypes.c_int), ('serial', ctypes.c_ulong), ('send_event', ctypes.c_int),\n"
" ('display', ctypes.c_void_p), ('window', ctypes.c_ulong), ('message_type', ctypes.c_ulong),\n"
" ('format', ctypes.c_int), ('data', XClientMessageData)]\n"
"X11.XAllocWMHints.restype = ctypes.POINTER(XWMHints)\n"
"X11.XSetWMHints.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.POINTER(XWMHints)]\n"
"X11.XFree.argtypes = [ctypes.c_void_p]\n"
"X11.XSelectInput.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.c_long]\n"
"X11.XMapWindow.argtypes = [ctypes.c_void_p, ctypes.c_ulong]\n"
"X11.XIconifyWindow.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.c_int]\n"
"X11.XIconifyWindow.restype = ctypes.c_int\n"
"X11.XMoveResizeWindow.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.c_int, ctypes.c_int, ctypes.c_uint, ctypes.c_uint]\n"
"X11.XSendEvent.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.c_int, ctypes.c_long, ctypes.c_void_p]\n"
"X11.XFlush.argtypes = [ctypes.c_void_p]\n"
"X11.XPending.argtypes = [ctypes.c_void_p]\n"
"X11.XNextEvent.argtypes = [ctypes.c_void_p, ctypes.c_void_p]\n"
"X11.XSetInputFocus.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.c_int, ctypes.c_ulong]\n"
"X11.XLookupKeysym.argtypes = [ctypes.c_void_p, ctypes.c_int]\n"
"X11.XLookupKeysym.restype = ctypes.c_ulong\n"
"X11.XGrabPointer.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.c_int, ctypes.c_uint, ctypes.c_int, ctypes.c_int, ctypes.c_ulong, ctypes.c_ulong, ctypes.c_ulong]\n"
"X11.XGrabPointer.restype = ctypes.c_int\n"
"X11.XUngrabPointer.argtypes = [ctypes.c_void_p, ctypes.c_ulong]\n"
"X11.XTranslateCoordinates.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.c_ulong, ctypes.c_int, ctypes.c_int, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_ulong)]\n"
"dpy = X11.XOpenDisplay(None)\n"
"root = X11.XDefaultRootWindow(dpy)\n"
"scr = X11.XDefaultScreen(dpy)\n"
"win = X11.XCreateSimpleWindow(dpy, root, 0, 0, 1280, 800, 0, 0, 0x222222)\n"
"wmh = X11.XAllocWMHints()\n"
"wmh.contents.flags = 1\n"
"wmh.contents.input = 0\n"
"X11.XSetWMHints(dpy, win, wmh)\n"
"X11.XFree(wmh)\n"
"wm_proto = X11.XInternAtom(dpy, b'WM_PROTOCOLS', 0)\n"
"wm_del = X11.XInternAtom(dpy, b'WM_DELETE_WINDOW', 0)\n"
"wm_take = X11.XInternAtom(dpy, b'WM_TAKE_FOCUS', 0)\n"
"wm_state_atom = X11.XInternAtom(dpy, b'WM_STATE', 0)\n"
"protos = (ctypes.c_ulong * 2)(wm_del, wm_take)\n"
"X11.XSetWMProtocols(dpy, win, protos, 2)\n"
"wm_state = X11.XInternAtom(dpy, b'_NET_WM_STATE', 0)\n"
"wm_fs = X11.XInternAtom(dpy, b'_NET_WM_STATE_FULLSCREEN', 0)\n"
"states = (ctypes.c_ulong * 1)(wm_fs)\n"
"X11.XChangeProperty(dpy, win, wm_state, 4, 32, 0, states, 1)\n"
"mask = (1<<0)|(1<<2)|(1<<6)|(1<<4)|(1<<5)|(1<<17)|(1<<21)|(1<<22)\n"
"X11.XSelectInput(dpy, win, mask)\n"
"X11.XMapWindow(dpy, win)\n"
"X11.XFlush(dpy)\n"
"buf = (ctypes.c_long * 32)()\n"
"print(json.dumps({'ready': int(win)}), flush=True)\n"
"seen_focusin = False\n"
"deactivated = False\n"
"offscreen = False\n"
"triggered_deactivate = False\n"
"pending_wm_state = False\n"
"pending_net_wm_state = False\n"
"pending_config = False\n"
"def read_wm_state():\n"
" da, di = ctypes.c_ulong(), ctypes.c_int()\n"
" n, dl = ctypes.c_ulong(), ctypes.c_ulong()\n"
" prop = ctypes.POINTER(ctypes.c_ubyte)()\n"
" rc = X11.XGetWindowProperty(dpy, win, wm_state_atom, 0, 2, 0, wm_state_atom,\n"
" ctypes.byref(da), ctypes.byref(di), ctypes.byref(n), ctypes.byref(dl), ctypes.byref(prop))\n"
" val = None\n"
" if rc == 0 and prop:\n"
" if di.value == 32 and n.value >= 1:\n"
" val = int(ctypes.cast(prop, ctypes.POINTER(ctypes.c_ulong))[0])\n"
" X11.XFree(prop)\n"
" return val\n"
"def maybe_trigger():\n"
" global triggered_deactivate, pending_wm_state, pending_net_wm_state, pending_config\n"
" if seen_focusin and deactivated and offscreen and not triggered_deactivate:\n"
" triggered_deactivate = True\n"
" pending_wm_state = True\n"
" X11.XIconifyWindow(dpy, win, scr)\n"
" pending_net_wm_state = True\n"
" ev = XClientMessageEvent()\n"
" ev.type = 33\n"
" ev.display = dpy\n"
" ev.window = win\n"
" ev.message_type = wm_state\n"
" ev.format = 32\n"
" ev.data.l[0] = 1\n"
" ev.data.l[1] = wm_fs\n"
" ev.data.l[2] = 0\n"
" ev.data.l[3] = 1\n"
" X11.XSendEvent(dpy, root, 0, (1<<19)|(1<<20), ctypes.byref(ev))\n"
" pending_config = True\n"
" X11.XMoveResizeWindow(dpy, win, 0, 0, 1280, 800)\n"
" X11.XFlush(dpy)\n"
"t0 = time.monotonic()\n"
"while time.monotonic() - t0 < 10.0:\n"
" while X11.XPending(dpy):\n"
" X11.XNextEvent(dpy, buf)\n"
" et = ctypes.c_int.from_buffer(buf).value\n"
" if et == 33:\n"
" cm = XClientMessageEvent.from_buffer(buf)\n"
" if cm.message_type == wm_proto and cm.data.l[0] == wm_take:\n"
" ts = int(cm.data.l[1]) & 0xffffffff\n"
" if pending_wm_state:\n"
" print(json.dumps({'ev': 'WM_TAKE_FOCUS_IGNORED', 'time': ts}), flush=True)\n"
" else:\n"
" X11.XSetInputFocus(dpy, win, 1, ts)\n"
" X11.XFlush(dpy)\n"
" print(json.dumps({'ev': 'WM_TAKE_FOCUS', 'time': ts}), flush=True)\n"
" elif et == 28:\n"
" pe = XPropertyEvent.from_buffer(buf)\n"
" if pe.state == 0 and pe.atom == wm_state_atom:\n"
" val = read_wm_state()\n"
" if val in (1, 3):\n"
" pending_wm_state = False\n"
" print(json.dumps({'ev': 'WM_STATE', 'state': val, 'pending': pending_wm_state}), flush=True)\n"
" elif pe.state == 0 and pe.atom == wm_state:\n"
" if pending_net_wm_state:\n"
" pending_net_wm_state = False\n"
" print(json.dumps({'ev': 'NET_WM_STATE_ACK'}), flush=True)\n"
" elif et == 9:\n"
" seen_focusin = True\n"
" fe = XFocusChangeEvent.from_buffer(buf)\n"
" tx, ty, ch = ctypes.c_int(), ctypes.c_int(), ctypes.c_ulong()\n"
" X11.XTranslateCoordinates(dpy, win, root, 0, 0, ctypes.byref(tx), ctypes.byref(ty), ctypes.byref(ch))\n"
" grc = X11.XGrabPointer(dpy, win, 1, (1<<2)|(1<<3)|(1<<6), 1, 1, win, 0, 0)\n"
" print(json.dumps({'ev': 'FocusIn', 'mode': fe.mode, 'pos': [tx.value, ty.value], 'grab': grc}), flush=True)\n"
" elif et == 10:\n"
" fe = XFocusChangeEvent.from_buffer(buf)\n"
" X11.XUngrabPointer(dpy, 0)\n"
" print(json.dumps({'ev': 'FocusOut', 'mode': fe.mode}), flush=True)\n"
" if seen_focusin:\n"
" deactivated = True\n"
" maybe_trigger()\n"
" elif et == 22:\n"
" ce = XConfigureEvent.from_buffer(buf)\n"
" if ce.x < 0:\n"
" offscreen = True\n"
" if pending_config and ce.send_event:\n"
" pending_config = False\n"
" print(json.dumps({'ev': 'ConfigureRequestAck', 'pos': [ce.x, ce.y], 'size': [ce.width, ce.height]}), flush=True)\n"
" print(json.dumps({'ev': 'ConfigureNotify', 'send_event': bool(ce.send_event), 'pos': [ce.x, ce.y], 'size': [ce.width, ce.height]}), flush=True)\n"
" maybe_trigger()\n"
" elif et in (2, 4, 6):\n"
" names = {2: 'KeyPress', 4: 'ButtonPress', 6: 'MotionNotify'}\n"
" print(json.dumps({'ev': names[et]}), flush=True)\n"
" if et == 2 and X11.XLookupKeysym(buf, 0) == 0x62:\n"
" pending_wm_state = True\n"
" print(json.dumps({'ev': 'VISIBLE_ICONIFY_REQ'}), flush=True)\n"
" X11.XIconifyWindow(dpy, win, scr)\n"
" X11.XFlush(dpy)\n"
" time.sleep(0.005)\n"
)
wine_sim = subprocess.Popen(
[sys.executable, "-c", sim_code],
env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True,
)
import json
ready_info = json.loads(wine_sim.stdout.readline().strip())
win_id = ready_info["ready"]
time.sleep(0.4)
# Switch Tag 1 -> Tag 2 via Super_L + 2
subprocess.run(["xdotool", "keydown", "Super_L"], env=env, check=True)
time.sleep(0.06)
subprocess.run(["xdotool", "key", "2"], env=env, check=True)
time.sleep(0.08)
subprocess.run(["xdotool", "keyup", "Super_L"], env=env, check=True)
time.sleep(0.25)
tag2_client = subprocess.Popen(
[os.path.join(BUILD, "st", "st"), "-e", "sh", "-c", "sleep 6"],
env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
)
time.sleep(0.4)
subprocess.run(["xdotool", "mousemove", "400", "400", "click", "1"], env=env, check=True)
time.sleep(0.25)
# Switch Tag 2 -> Tag 1 via Super_L + 1
subprocess.run(["xdotool", "keydown", "Super_L"], env=env, check=True)
time.sleep(0.06)
subprocess.run(["xdotool", "key", "1"], env=env, check=True)
time.sleep(0.08)
subprocess.run(["xdotool", "keyup", "Super_L"], env=env, check=True)
time.sleep(0.3)
subprocess.run(["xdotool", "mousemove", "500", "500", "click", "1", "mousemove", "700", "300", "key", "a"],
env=env, check=True)
time.sleep(0.25)
# Verify visible XIconifyWindow on Tag 1 is rejected with NormalState
subprocess.run(["xdotool", "key", "b"], env=env, check=True)
time.sleep(0.25)
terminate(wine_sim)
out, _ = wine_sim.communicate(timeout=3)
events = [json.loads(line) for line in out.splitlines() if line.strip().startswith("{")]
off_idx = next((i for i, e in enumerate(events) if e["ev"] == "ConfigureNotify" and e["pos"][0] < 0), None)
assert off_idx is not None, f"Client never moved offscreen on Tag 1 -> Tag 2: {events}"
post_off = events[off_idx:]
return_idx = next(
(i for i, e in enumerate(post_off)
if e["ev"] == "ConfigureNotify" and not e["send_event"]
and e["pos"] == [0, 0] and e["size"] == [1280, 800]),
None,
)
assert return_idx is not None, f"Missing ConfigureNotify return to (0,0): {post_off}"
during_hide = post_off[:return_idx]
wm_iconics = [e for e in during_hide if e["ev"] == "WM_STATE" and e["state"] == 3 and not e["pending"]]
assert wm_iconics, f"Missing WM_STATE IconicState acceptance while hidden on Tag 2: {during_hide}"
wm_premature_normals = [e for e in during_hide if e["ev"] == "WM_STATE" and e["state"] == 1]
assert not wm_premature_normals, f"Premature WM_STATE NormalState while still hidden on Tag 2: {during_hide}"
assert any(e["ev"] == "NET_WM_STATE_ACK" for e in during_hide), (
f"Missing PropertyNotify(_NET_WM_STATE) ack for redundant _NET_WM_STATE_ADD: {during_hide}"
)
assert any(e["ev"] == "ConfigureRequestAck" for e in during_hide), (
f"Missing synthetic ConfigureNotify ack for hidden ConfigureRequest: {during_hide}"
)
assert not any(e["ev"] == "WM_TAKE_FOCUS_IGNORED" for e in events), (
f"WM_TAKE_FOCUS was dropped due to pending WM_STATE: {events}"
)
post_return = post_off[return_idx:]
norm_idx = next(
(i for i, e in enumerate(post_return) if e["ev"] == "WM_STATE" and e["state"] == 1),
None,
)
tf_idx = next((i for i, e in enumerate(post_return) if e["ev"] == "WM_TAKE_FOCUS"), None)
assert norm_idx is not None, f"Missing WM_STATE NormalState restoration on Tag 2 -> Tag 1 return: {post_return}"
assert tf_idx is not None, f"Missing WM_TAKE_FOCUS after Tag 2 -> Tag 1 return: {post_return}"
assert norm_idx < tf_idx, f"WM_STATE NormalState ({norm_idx}) must precede WM_TAKE_FOCUS ({tf_idx}): {post_return}"
fi = [e for e in post_return if e["ev"] == "FocusIn"]
assert fi, f"Missing FocusIn after Tag 2 -> Tag 1 return: {post_return}"
assert fi[0]["mode"] == 0 and fi[0]["pos"] == [0, 0] and fi[0]["grab"] == 0, (
f"Bad FocusIn/grab after Tag 2 -> Tag 1 return: {fi[0]}"
)
post_names = [e["ev"] for e in post_return]
for req in ("MotionNotify", "ButtonPress", "KeyPress"):
assert req in post_names, f"Missing {req} after return to Tag 1: {post_return}"
vis_req_idx = next((i for i, e in enumerate(events) if e["ev"] == "VISIBLE_ICONIFY_REQ"), None)
assert vis_req_idx is not None, f"Visible XIconifyWindow trigger never fired: {events}"
after_vis = events[vis_req_idx:]
assert any(e["ev"] == "WM_STATE" and e["state"] == 1 and not e["pending"] for e in after_vis), (
f"Visible XIconifyWindow was not rejected with WM_STATE NormalState: {after_vis}"
)
assert not any(e["ev"] == "WM_STATE" and e["state"] == 3 for e in after_vis), (
f"Visible XIconifyWindow unexpectedly accepted IconicState: {after_vis}"
)
finally:
terminate(tag2_client)
terminate(wine_sim)
terminate(dwm)
if dpy:
X11.XCloseDisplay(dpy)
terminate(xvfb)
def test_systray_xembed_dock_race():
disp = pick_display()
with tempfile.TemporaryDirectory() as home:
@@ -1372,6 +1698,8 @@ def main():
print("test_x11_xvfb: fullscreen workspace switch focus OK")
test_wine_take_focus_workspace_switch()
print("test_x11_xvfb: Wine WM_TAKE_FOCUS workspace switch OK")
test_wine_dxvk_iconic_workspace_switch()
print("test_x11_xvfb: Wine/DXVK WM_CHANGE_STATE iconic workspace switch OK")
test_systray_xembed_dock_race()
print("test_x11_xvfb: systray XEmbed dock/map race OK")
return 0