#!/usr/bin/env python3 """Headless Xvfb E2E integration tests across 1024x600, 1920x1080, and 3840x2160.""" import ctypes import ctypes.util import os import re import shutil import signal import socket import subprocess import sys import tempfile import time ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) BUILD = os.path.join(ROOT, "build") libx11_name = ctypes.util.find_library("X11") or "libX11.so.6" X11 = ctypes.CDLL(libx11_name) Window = ctypes.c_ulong Atom = ctypes.c_ulong Display_p = ctypes.c_void_p class XWindowAttributes(ctypes.Structure): _fields_ = [ ("x", ctypes.c_int), ("y", ctypes.c_int), ("width", ctypes.c_int), ("height", ctypes.c_int), ("border_width", ctypes.c_int), ("depth", ctypes.c_int), ("visual", ctypes.c_void_p), ("root", Window), ("class", ctypes.c_int), ("bit_gravity", ctypes.c_int), ("win_gravity", ctypes.c_int), ("backing_store", ctypes.c_int), ("backing_planes", ctypes.c_ulong), ("backing_pixel", ctypes.c_ulong), ("save_under", ctypes.c_int), ("colormap", ctypes.c_ulong), ("map_installed", ctypes.c_int), ("map_state", ctypes.c_int), ("all_event_masks", ctypes.c_long), ("your_event_mask", ctypes.c_long), ("do_not_propagate_mask", ctypes.c_long), ("override_redirect", ctypes.c_int), ("screen", ctypes.c_void_p), ] class XClientMessageData(ctypes.Union): _fields_ = [("b", ctypes.c_char * 20), ("s", ctypes.c_short * 10), ("l", ctypes.c_long * 5)] class XClientMessageEvent(ctypes.Structure): _fields_ = [ ("type", ctypes.c_int), ("serial", ctypes.c_ulong), ("send_event", ctypes.c_int), ("display", Display_p), ("window", Window), ("message_type", Atom), ("format", ctypes.c_int), ("data", XClientMessageData), ] class XButtonEvent(ctypes.Structure): _fields_ = [ ("type", ctypes.c_int), ("serial", ctypes.c_ulong), ("send_event", ctypes.c_int), ("display", Display_p), ("window", Window), ("root", Window), ("subwindow", Window), ("time", ctypes.c_ulong), ("x", ctypes.c_int), ("y", ctypes.c_int), ("x_root", ctypes.c_int), ("y_root", ctypes.c_int), ("state", ctypes.c_uint), ("button", ctypes.c_uint), ("same_screen", ctypes.c_int), ] class XWMHints(ctypes.Structure): _fields_ = [ ("flags", ctypes.c_long), ("input", ctypes.c_int), ("initial_state", ctypes.c_int), ("icon_pixmap", ctypes.c_ulong), ("icon_window", Window), ("icon_x", ctypes.c_int), ("icon_y", ctypes.c_int), ("icon_mask", ctypes.c_ulong), ("window_group", Window), ] class XSetWindowAttributes(ctypes.Structure): _fields_ = [ ("background_pixmap", ctypes.c_ulong), ("background_pixel", ctypes.c_ulong), ("border_pixmap", ctypes.c_ulong), ("border_pixel", ctypes.c_ulong), ("bit_gravity", ctypes.c_int), ("win_gravity", ctypes.c_int), ("backing_store", ctypes.c_int), ("backing_planes", ctypes.c_ulong), ("backing_pixel", ctypes.c_ulong), ("save_under", ctypes.c_int), ("event_mask", ctypes.c_long), ("do_not_propagate_mask", ctypes.c_long), ("override_redirect", ctypes.c_int), ("colormap", ctypes.c_ulong), ("cursor", ctypes.c_ulong), ] X11.XOpenDisplay.argtypes = [ctypes.c_char_p] X11.XOpenDisplay.restype = Display_p X11.XCloseDisplay.argtypes = [Display_p] X11.XDefaultRootWindow.argtypes = [Display_p] X11.XDefaultRootWindow.restype = Window X11.XInternAtom.argtypes = [Display_p, ctypes.c_char_p, ctypes.c_int] X11.XInternAtom.restype = Atom X11.XGetSelectionOwner.argtypes = [Display_p, Atom] X11.XGetSelectionOwner.restype = Window X11.XGetWindowProperty.argtypes = [ Display_p, Window, Atom, ctypes.c_long, ctypes.c_long, ctypes.c_int, Atom, ctypes.POINTER(Atom), ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_ulong), ctypes.POINTER(ctypes.c_ulong), ctypes.POINTER(ctypes.POINTER(ctypes.c_ubyte)), ] X11.XGetWindowProperty.restype = ctypes.c_int X11.XFree.argtypes = [ctypes.c_void_p] X11.XCreateSimpleWindow.argtypes = [ Display_p, Window, ctypes.c_int, ctypes.c_int, ctypes.c_uint, ctypes.c_uint, ctypes.c_uint, ctypes.c_ulong, ctypes.c_ulong, ] X11.XCreateSimpleWindow.restype = Window X11.XDestroyWindow.argtypes = [Display_p, Window] X11.XMapWindow.argtypes = [Display_p, Window] X11.XChangeWindowAttributes.argtypes = [Display_p, Window, ctypes.c_ulong, ctypes.POINTER(XSetWindowAttributes)] X11.XChangeProperty.argtypes = [ Display_p, Window, Atom, Atom, ctypes.c_int, ctypes.c_int, ctypes.c_void_p, ctypes.c_int ] X11.XSendEvent.argtypes = [Display_p, Window, ctypes.c_int, ctypes.c_long, ctypes.c_void_p] X11.XFlush.argtypes = [Display_p] X11.XSync.argtypes = [Display_p, ctypes.c_int] X11.XQueryTree.argtypes = [ Display_p, Window, ctypes.POINTER(Window), ctypes.POINTER(Window), ctypes.POINTER(ctypes.POINTER(Window)), ctypes.POINTER(ctypes.c_uint), ] X11.XQueryTree.restype = ctypes.c_int X11.XGetWindowAttributes.argtypes = [Display_p, Window, ctypes.POINTER(XWindowAttributes)] X11.XGetWindowAttributes.restype = ctypes.c_int X11.XGetWMHints.argtypes = [Display_p, Window] X11.XGetWMHints.restype = ctypes.POINTER(XWMHints) X11.XFetchName.argtypes = [Display_p, Window, ctypes.POINTER(ctypes.c_char_p)] X11.XFetchName.restype = ctypes.c_int X11.XkbLockGroup.argtypes = [Display_p, ctypes.c_uint, ctypes.c_uint] X11.XkbLockGroup.restype = ctypes.c_int def pick_display(): for d in range(70, 120): if not os.path.exists(f"/tmp/.X{d}-lock") and not os.path.exists(f"/tmp/.X11-unix/X{d}"): return f":{d}" return ":99" def get_cardinal_prop(dpy, win, atom_name): atom = X11.XInternAtom(dpy, atom_name.encode(), 0) da = Atom() di = ctypes.c_int() n = ctypes.c_ulong() dl = ctypes.c_ulong() prop = ctypes.POINTER(ctypes.c_ubyte)() rc = X11.XGetWindowProperty(dpy, win, atom, 0, 16, 0, 0, ctypes.byref(da), ctypes.byref(di), ctypes.byref(n), ctypes.byref(dl), ctypes.byref(prop)) if rc != 0 or not prop: return [] vals = [] if di.value == 32 and n.value > 0: arr = ctypes.cast(prop, ctypes.POINTER(ctypes.c_ulong)) vals = [int(arr[i]) for i in range(n.value)] X11.XFree(prop) return vals def query_children(dpy, parent): r = Window() p = Window() ch = ctypes.POINTER(Window)() n = ctypes.c_uint() if X11.XQueryTree(dpy, parent, ctypes.byref(r), ctypes.byref(p), ctypes.byref(ch), ctypes.byref(n)): out = [int(ch[i]) for i in range(n.value)] if ch: X11.XFree(ch) return out return [] def wait_until(pred, timeout=5.0): end = time.monotonic() + timeout while time.monotonic() < end: res = pred() if res: return res time.sleep(0.05) return None def terminate(proc): if proc and proc.poll() is None: proc.terminate() try: proc.wait(timeout=3) except subprocess.TimeoutExpired: proc.kill() proc.wait(timeout=2) def test_resolution(res_str, scale_str, width, height): 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["LD_LIBRARY_PATH"] = os.path.join(BUILD, "libgemui") + (":" + env["LD_LIBRARY_PATH"] if env.get("LD_LIBRARY_PATH") else "") env["PATH"] = ( os.path.join(BUILD, "dwm") + ":" + os.path.join(BUILD, "dmenu") + ":" + os.path.join(BUILD, "st") + ":" + os.path.join(BUILD, "apps") + ":" + os.path.join(BUILD, "daemons") + ":" + os.path.join(ROOT, "scripts") + ":" + env.get("PATH", "") ) # Configure theme + scale in sandbox subprocess.run([os.path.join(ROOT, "scripts", "anode-apply-scale"), scale_str, "--touch=1"], env=env, check=True) xvfb = subprocess.Popen( ["Xvfb", disp, "-screen", "0", f"{res_str}x24", "-nolisten", "tcp"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ) dwm = None dpy = None procs = [] try: dpy = wait_until(lambda: X11.XOpenDisplay(disp.encode())) assert dpy, f"Failed to open Xvfb {disp}" root = X11.XDefaultRootWindow(dpy) dwm = subprocess.Popen([os.path.join(BUILD, "dwm", "dwm")], env=env) procs.append(dwm) tray_atom = X11.XInternAtom(dpy, b"_NET_SYSTEM_TRAY_S0", 0) tray_win = wait_until(lambda: X11.XGetSelectionOwner(dpy, tray_atom)) assert tray_win, f"DWM failed to acquire _NET_SYSTEM_TRAY_S0 on {res_str}" # Check initial _NET_WORKAREA and empty systray must be unmapped wa0 = wait_until(lambda: get_cardinal_prop(dpy, root, "_NET_WORKAREA")) assert len(wa0) >= 4 and wa0[2] == width and wa0[3] < height, f"Bad initial workarea: {wa0}" tray_attr = XWindowAttributes() X11.XGetWindowAttributes(dpy, tray_win, ctypes.byref(tray_attr)) assert tray_attr.map_state == 0, "Empty systray should stay unmapped!" # Test XEmbed systray docking + _XEMBED_INFO unmap/remap + SIGUSR1 resize + undock icon = X11.XCreateSimpleWindow(dpy, root, 0, 0, 24, 24, 0, 0, 0xffffff) xembed_info = X11.XInternAtom(dpy, b"_XEMBED_INFO", 0) info_arr = (ctypes.c_ulong * 2)(0, 1) # version=0, XEMBED_MAPPED=1 X11.XChangeProperty(dpy, icon, xembed_info, xembed_info, 32, 0, info_arr, 2) op_atom = X11.XInternAtom(dpy, b"_NET_SYSTEM_TRAY_OPCODE", 0) ev = XClientMessageEvent() ev.type = 33 # ClientMessage ev.display = dpy ev.window = tray_win ev.message_type = op_atom ev.format = 32 ev.data.l[0] = 0 ev.data.l[1] = 0 # SYSTEM_TRAY_REQUEST_DOCK ev.data.l[2] = icon X11.XSendEvent(dpy, tray_win, 0, 0, ctypes.byref(ev)) X11.XSync(dpy, 0) assert wait_until(lambda: icon in query_children(dpy, tray_win)), "Icon was not reparented into systray!" X11.XGetWindowAttributes(dpy, tray_win, ctypes.byref(tray_attr)) assert tray_attr.map_state == 2, "Non-empty systray must be mapped (IsViewable)" # Toggle _XEMBED_INFO unmapped -> tray unmaps info_unmap = (ctypes.c_ulong * 2)(0, 0) X11.XChangeProperty(dpy, icon, xembed_info, xembed_info, 32, 0, info_unmap, 2) X11.XSync(dpy, 0) assert wait_until(lambda: (X11.XGetWindowAttributes(dpy, tray_win, ctypes.byref(tray_attr)) or True) and tray_attr.map_state == 0), "Tray did not unmap when icon withdrew!" # Remap via _XEMBED_INFO, reload scale via SIGUSR1, then destroy cleanly X11.XChangeProperty(dpy, icon, xembed_info, xembed_info, 32, 0, info_arr, 2) X11.XSync(dpy, 0) assert wait_until(lambda: (X11.XGetWindowAttributes(dpy, tray_win, ctypes.byref(tray_attr)) or True) and tray_attr.map_state == 2) pre_h = tray_attr.height subprocess.run([os.path.join(ROOT, "scripts", "anode-apply-scale"), "2.00", "--touch=1"], env=env, check=True) dwm.send_signal(signal.SIGUSR1) time.sleep(0.25) X11.XGetWindowAttributes(dpy, tray_win, ctypes.byref(tray_attr)) assert tray_attr.height >= pre_h, f"Tray height did not preserve/scale after SIGUSR1: {tray_attr.height} vs {pre_h}" # Stale exported ANODE_SCALE must not block live scale updates + SIGUSR1 reload stale_env = dict(env) stale_env["ANODE_SCALE"] = "2.00" stale_env["ANODE_TOUCH"] = "1" stale_env["ANODE_NO_DBUS"] = "1" subprocess.run([os.path.join(ROOT, "scripts", "anode-apply-scale"), "1.25", "--touch=0"], env=stale_env, check=True) if shutil.which("xrdb"): xq = subprocess.check_output(["xrdb", "-query"], env=env, text=True) assert "theme.scale:\t1.25" in xq or "theme.scale: 1.25" in xq, xq dwm.send_signal(signal.SIGUSR1) time.sleep(0.2) assert dwm.poll() is None, "dwm exited after SIGUSR1 live scale update" subprocess.run([os.path.join(ROOT, "scripts", "anode-apply-scale"), scale_str, "--touch=1"], env=env, check=True) dwm.send_signal(signal.SIGUSR1) time.sleep(0.2) X11.XDestroyWindow(dpy, icon) X11.XSync(dpy, 0) assert wait_until(lambda: icon not in query_children(dpy, tray_win)) # Refresh wa0 after scale restore before testing anode-vkbd dock strut wa0 = wait_until(lambda: get_cardinal_prop(dpy, root, "_NET_WORKAREA")) vkbd = subprocess.Popen([os.path.join(BUILD, "apps", "anode-vkbd")], env=env) procs.append(vkbd) wa_vk = wait_until(lambda: (lambda w: w if w and len(w) >= 4 and w[3] < wa0[3] else None)(get_cardinal_prop(dpy, root, "_NET_WORKAREA"))) assert wa_vk, f"anode-vkbd did not reserve workarea strut on {res_str} (wa0={wa0})" # Hide via SIGUSR1 -> workarea restores, then re-show via SIGUSR1 -> strut reserves again vkbd.send_signal(signal.SIGUSR1) assert wait_until(lambda: get_cardinal_prop(dpy, root, "_NET_WORKAREA") == wa0), "Workarea did not restore on vkbd hide" vkbd.send_signal(signal.SIGUSR1) assert wait_until(lambda: (lambda w: w if w and len(w) >= 4 and w[3] < wa0[3] else None)(get_cardinal_prop(dpy, root, "_NET_WORKAREA"))), "Workarea did not re-reserve on vkbd show" terminate(vkbd) # Test anode-statusd publishes CPU/RAM (never net/docker0) and lowercase XKB left of datetime sd = subprocess.Popen([os.path.join(BUILD, "daemons", "anode-statusd")], env=env) procs.append(sd) def get_status(): nm = ctypes.c_char_p() if X11.XFetchName(dpy, root, ctypes.byref(nm)) and nm.value: s = nm.value.decode("utf-8", errors="ignore") X11.XFree(nm) if "|" in s: return s return None def get_kbd_left_of_dt(): s = get_status() if not s: return None assert "NET " not in s and "docker" not in s.lower(), f"Unexpected network segment in status: {s!r}" parts = [p.strip() for p in s.split("|")] if len(parts) < 4: return None if not any(re.fullmatch(r"CPU \d+%", p) for p in parts): return None if not any(re.fullmatch(r"RAM \d+%", p) for p in parts): return None if not re.fullmatch(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}", parts[-1]): return None kbd = parts[-2] if not kbd or not kbd.isascii() or not kbd.isalnum() or kbd != kbd.lower(): return None return kbd init_kbd = wait_until(get_kbd_left_of_dt) assert init_kbd, f"anode-statusd missing lowercase XKB layout left of datetime: {get_status()!r}" if shutil.which("setxkbmap"): subprocess.run( ["setxkbmap", "-layout", "us,de,ru", "-variant", ",nodeadkeys,"], env=env, check=True, ) assert wait_until(lambda: get_kbd_left_of_dt() == "us"), f"Expected 'us': {get_status()!r}" X11.XkbLockGroup(dpy, 0x0100, 1) X11.XSync(dpy, 0) assert wait_until(lambda: get_kbd_left_of_dt() == "de"), f"Expected 'de': {get_status()!r}" X11.XkbLockGroup(dpy, 0x0100, 2) X11.XSync(dpy, 0) assert wait_until(lambda: get_kbd_left_of_dt() == "ru"), f"Expected 'ru': {get_status()!r}" X11.XkbLockGroup(dpy, 0x0100, 0) X11.XSync(dpy, 0) assert wait_until(lambda: get_kbd_left_of_dt() == "us"), f"Expected 'us' restore: {get_status()!r}" terminate(sd) # Test dmenu -l 0 (fills full screen height) vs dmenu -l 5 (exact list height) dm_full = subprocess.Popen( [os.path.join(BUILD, "dmenu", "dmenu"), "-l", "0"], env=env, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ) procs.append(dm_full) dm_full.stdin.write(b"\n".join(f"item{i}".encode() for i in range(60)) + b"\n") dm_full.stdin.close() def find_win_with_height(target_min_w, check_fn): for w in query_children(dpy, root): a = XWindowAttributes() if X11.XGetWindowAttributes(dpy, w, ctypes.byref(a)) and a.map_state == 2 and a.width >= target_min_w: if check_fn(a.height): return a.height return None h_full = wait_until(lambda: find_win_with_height(width - 10, lambda hh: hh == height)) assert h_full == height, f"dmenu -l 0 did not fill screen height {height}: got {h_full}" terminate(dm_full) dm_five = subprocess.Popen( [os.path.join(BUILD, "dmenu", "dmenu"), "-l", "5"], env=env, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ) procs.append(dm_five) dm_five.stdin.write(b"\n".join(f"item{i}".encode() for i in range(60)) + b"\n") dm_five.stdin.close() h_five = wait_until(lambda: find_win_with_height(width - 10, lambda hh: 100 <= hh < height)) assert h_five and h_five < height, f"dmenu -l 5 height invalid: {h_five} vs {height}" terminate(dm_five) # Test anode-checkpw rejects wrong password cleanly chk = subprocess.run( [os.path.join(BUILD, "daemons", "anode-checkpw")], input="definitely_wrong_pw_12345\n", env=env, capture_output=True, text=True, timeout=3, ) assert chk.returncode != 0 # Test core + GemUI utilities launch, map cleanly inside screen, and exit cleanly for bin_path, extra_stdin in [ (os.path.join(BUILD, "st", "st"), None), (os.path.join(BUILD, "dmenu", "dmenu"), b"alpha\nbeta\n"), (os.path.join(BUILD, "apps", "anode-themes"), None), (os.path.join(BUILD, "apps", "anode-power"), None), (os.path.join(BUILD, "apps", "anode-display"), None), (os.path.join(BUILD, "apps", "anode-audio"), None), (os.path.join(BUILD, "apps", "anode-datetime"), None), (os.path.join(BUILD, "apps", "anode-net"), None), ]: before = set(query_children(dpy, root)) if extra_stdin: p = subprocess.Popen([bin_path], env=env, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) p.stdin.write(extra_stdin) p.stdin.close() else: p = subprocess.Popen([bin_path], env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) procs.append(p) time.sleep(0.35) assert p.poll() is None, f"{os.path.basename(bin_path)} exited prematurely on {res_str} with code {p.returncode}" terminate(p) finally: for p in reversed(procs): terminate(p) if dpy: X11.XCloseDisplay(dpy) terminate(xvfb) def test_st_font_override_and_dunst(): 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["LD_LIBRARY_PATH"] = os.path.join(BUILD, "libgemui") + (":" + env["LD_LIBRARY_PATH"] if env.get("LD_LIBRARY_PATH") else "") env["PATH"] = ( os.path.join(BUILD, "dwm") + ":" + os.path.join(BUILD, "st") + ":" + os.path.join(ROOT, "scripts") + ":" + env.get("PATH", "") ) xvfb = subprocess.Popen( ["Xvfb", disp, "-screen", "0", "1920x1200x24", "-nolisten", "tcp"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ) dpy = None procs = [] try: dpy = wait_until(lambda: X11.XOpenDisplay(disp.encode())) assert dpy, f"Failed to open Xvfb {disp}" root = X11.XDefaultRootWindow(dpy) def launch_st_and_get_height_inc(xres_text): xr_path = os.path.join(home, ".Xresources") with open(xr_path, "w", encoding="utf-8") as f: f.write(xres_text) if shutil.which("xrdb"): subprocess.run(["xrdb", "-load", xr_path], env=env, check=True) before = set(query_children(dpy, root)) stp = subprocess.Popen( [os.path.join(BUILD, "st", "st"), "-e", "sh", "-c", "sleep 4"], env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ) procs.append(stp) def get_inc(): for w in query_children(dpy, root): if w in before: continue a = XWindowAttributes() if X11.XGetWindowAttributes(dpy, w, ctypes.byref(a)) and a.map_state == 2: out = subprocess.check_output(["xprop", "-id", hex(w), "WM_NORMAL_HINTS"], env=env, text=True) m = re.search(r"resize increment:\s*\d+\s+by\s+(\d+)", out) if m: return int(m.group(1)) return None inc = wait_until(get_inc) terminate(stp) procs.remove(stp) return inc if shutil.which("xprop"): inc_default = launch_st_and_get_height_inc( "Xft.dpi: 144\n" ) assert inc_default and inc_default >= 28, f"Expected default size=13 @ 144 DPI height_inc >= 28, got {inc_default}" inc_small = launch_st_and_get_height_inc( "Xft.dpi: 144\nst.font: Fira Code:pixelsize=15:style=Regular:antialias=true:autohint=true\n" ) assert inc_small and inc_small <= 22, f"Expected pixelsize=15 height_inc <= 22, got {inc_small}" inc_override = launch_st_and_get_height_inc( "Xft.dpi: 144\nst.font: Fira Code:pixelsize=15:style=Regular:antialias=true:autohint=true\n" "St.font: Fira Code:size=14:antialias=true:autohint=true\n" ) assert inc_override and inc_override >= 32, f"Expected St.font size=14 @ 144 DPI height_inc >= 32, got {inc_override}" subprocess.run([os.path.join(ROOT, "scripts", "anode-apply-scale"), "1.50", "--touch=0"], env=env, check=True) dunstrc = os.path.join(home, ".config", "dunst", "dunstrc") if shutil.which("dunst"): denv = dict(env) denv.pop("DBUS_SESSION_BUS_ADDRESS", None) dp = subprocess.Popen( ["dunst", "-conf", dunstrc, "-verbosity", "debug"], env=denv, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, ) time.sleep(0.4) terminate(dp) dout, derr = dp.communicate(timeout=3) combined = dout + "\n" + derr assert "Trying to set scale to 1" in combined, combined assert "Fira Code 23px" in combined, combined finally: for p in reversed(procs): terminate(p) if dpy: X11.XCloseDisplay(dpy) terminate(xvfb) def get_wm_class(dpy, win): atom = X11.XInternAtom(dpy, b"WM_CLASS", 0) da = Atom() di = ctypes.c_int() n = ctypes.c_ulong() dl = ctypes.c_ulong() prop = ctypes.POINTER(ctypes.c_ubyte)() rc = X11.XGetWindowProperty(dpy, win, atom, 0, 64, 0, 31, # XA_STRING=31 ctypes.byref(da), ctypes.byref(di), ctypes.byref(n), ctypes.byref(dl), ctypes.byref(prop)) if rc != 0 or not prop: return [] raw = bytes(prop[:n.value]) if n.value > 0 else b"" X11.XFree(prop) return [s for s in raw.decode("utf-8", errors="ignore").split("\0") if s] def test_lockscreen_overlaps_systray(): 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["LD_LIBRARY_PATH"] = os.path.join(BUILD, "libgemui") + (":" + env["LD_LIBRARY_PATH"] if env.get("LD_LIBRARY_PATH") else "") env["PATH"] = ( os.path.join(BUILD, "dwm") + ":" + os.path.join(BUILD, "apps") + ":" + 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, "-screen", "0", "1280x800x24", "-nolisten", "tcp"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ) dpy = None procs = [] try: dpy = wait_until(lambda: X11.XOpenDisplay(disp.encode())) assert dpy, f"Failed to open Xvfb {disp}" root = X11.XDefaultRootWindow(dpy) dwm = subprocess.Popen([os.path.join(BUILD, "dwm", "dwm")], env=env) procs.append(dwm) tray_atom = X11.XInternAtom(dpy, b"_NET_SYSTEM_TRAY_S0", 0) tray_win = wait_until(lambda: X11.XGetSelectionOwner(dpy, tray_atom)) assert tray_win, "DWM failed to acquire _NET_SYSTEM_TRAY_S0" def find_bar_win(): for w in query_children(dpy, root): if "anode-wm" in get_wm_class(dpy, w): return w return None bar_win = wait_until(find_bar_win) assert bar_win, "DWM barwin not found" xembed_info = X11.XInternAtom(dpy, b"_XEMBED_INFO", 0) op_atom = X11.XInternAtom(dpy, b"_NET_SYSTEM_TRAY_OPCODE", 0) info_arr = (ctypes.c_ulong * 2)(0, 1) def dock_dummy_icon(): ic = X11.XCreateSimpleWindow(dpy, root, 0, 0, 20, 20, 0, 0, 0xffffff) X11.XChangeProperty(dpy, ic, xembed_info, xembed_info, 32, 0, info_arr, 2) ev = XClientMessageEvent() ev.type = 33 ev.display = dpy ev.window = tray_win ev.message_type = op_atom ev.format = 32 ev.data.l[0] = 0 ev.data.l[1] = 0 ev.data.l[2] = ic X11.XSendEvent(dpy, tray_win, 0, 0, ctypes.byref(ev)) X11.XSync(dpy, 0) assert wait_until(lambda: ic in query_children(dpy, tray_win)), "Dummy icon failed to dock" return ic dock_dummy_icon() lockp = subprocess.Popen([os.path.join(BUILD, "apps", "anode-lock")], env=env) procs.append(lockp) def find_lock_win(): for w in query_children(dpy, root): if "anode-lock" in get_wm_class(dpy, w): a = XWindowAttributes() if X11.XGetWindowAttributes(dpy, w, ctypes.byref(a)) and a.map_state == 2: return w return None lock_win = wait_until(find_lock_win) assert lock_win, "anode-lock window did not map" # Trigger systray updates while locked: dock a 2nd icon and reload dwm via SIGUSR1 dock_dummy_icon() dwm.send_signal(signal.SIGUSR1) time.sleep(0.3) def lock_on_top(): stack = query_children(dpy, root) if lock_win not in stack or tray_win not in stack or bar_win not in stack: return False return stack.index(lock_win) > stack.index(tray_win) and stack.index(lock_win) > stack.index(bar_win) assert wait_until(lock_on_top), f"anode-lock did not overlap systray/bar: stack={query_children(dpy, root)}, lock={lock_win}, tray={tray_win}, bar={bar_win}" finally: for p in reversed(procs): terminate(p) if dpy: X11.XCloseDisplay(dpy) terminate(xvfb) def test_gtk_xsettings_no_double_scale(): 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["XDG_DATA_HOME"] = os.path.join(home, ".local", "share") env["ANODE_PREFIX"] = ROOT env["ANODE_NO_SIGNAL"] = "1" env["ANODE_NO_DBUS"] = "1" env["LD_LIBRARY_PATH"] = os.path.join(BUILD, "libgemui") + (":" + env["LD_LIBRARY_PATH"] if env.get("LD_LIBRARY_PATH") else "") env["PATH"] = ( os.path.join(BUILD, "dwm") + ":" + os.path.join(ROOT, "scripts") + ":" + env.get("PATH", "") ) xvfb = subprocess.Popen( ["Xvfb", disp, "-noreset", "-screen", "0", "1920x1200x24", "-nolisten", "tcp"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ) dpy = None xsd = None dwm = None try: dpy = wait_until(lambda: X11.XOpenDisplay(disp.encode())) assert dpy, f"Failed to open Xvfb {disp}" gtk3_probe = ( "import json\n" "try:\n" " import gi; gi.require_version('Gtk', '3.0'); from gi.repository import Gtk, Gdk\n" "except Exception:\n" " print('SKIP'); raise SystemExit(0)\n" "Gtk.init([])\n" "s = Gtk.Settings.get_default()\n" "d = Gdk.Display.get_default()\n" "m = d.get_primary_monitor() or d.get_monitor(0)\n" "print(json.dumps({'xft_dpi': s.get_property('gtk-xft-dpi'), 'mon_sc': m.get_scale_factor()}))\n" ) gtk4_probe = ( "import json\n" "try:\n" " import gi; gi.require_version('Gtk', '4.0'); from gi.repository import Gtk, Gdk\n" "except Exception:\n" " print('SKIP'); raise SystemExit(0)\n" "Gtk.init()\n" "s = Gtk.Settings.get_default()\n" "d = Gdk.Display.get_default()\n" "m = d.get_monitors().get_item(0)\n" "print(json.dumps({'xft_dpi': s.get_property('gtk-xft-dpi'), 'mon_sc': m.get_scale_factor()}))\n" ) import json has_xset = bool(shutil.which("xsettingsd")) if has_xset: os.makedirs(os.path.join(home, ".config", "xsettingsd"), exist_ok=True) subprocess.run([os.path.join(ROOT, "scripts", "anode-apply-scale"), "1.50", "--touch=0"], env=env, check=True) xsd = subprocess.Popen( ["xsettingsd", "-c", os.path.join(home, ".config", "xsettingsd", "xsettingsd.conf")], env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ) time.sleep(0.25) for sc_str, exp_wsf, exp_dpi in [("1.50", 1, 144), ("1.75", 2, 168), ("2.00", 2, 192)]: subprocess.run([os.path.join(ROOT, "scripts", "anode-apply-scale"), sc_str, "--touch=0"], env=env, check=True) if xsd: xsd.send_signal(signal.SIGHUP) time.sleep(0.2) dirty_env = dict(env) dirty_env["GDK_SCALE"] = "2" dirty_env["GDK_DPI_SCALE"] = "0.5" scale_env = os.path.join(home, ".config", "anode", "scale.env") for label, probe_code in [("gtk3", gtk3_probe), ("gtk4", gtk4_probe)]: res = subprocess.run( ["/bin/sh", "-c", '. "$1" && exec "$2" -c "$3"', "sh", scale_env, sys.executable, probe_code], env=dirty_env, capture_output=True, text=True, check=True, ) line = [l for l in res.stdout.splitlines() if l.strip()][-1].strip() if line == "SKIP": continue info = json.loads(line) wsf_used = exp_wsf if has_xset else 1 exp_xft = int((exp_dpi * 1024) / wsf_used + 0.5) assert info["mon_sc"] == wsf_used, f"{label} @ {sc_str}: {info} expected mon_sc={wsf_used}" assert abs(info["xft_dpi"] - exp_xft) <= 1024, f"{label} @ {sc_str}: {info} expected xft_dpi~{exp_xft}" eff_dpr = (info["xft_dpi"] / 1024.0 / 96.0) * info["mon_sc"] assert abs(eff_dpr - float(sc_str)) <= 0.02, f"{label} @ {sc_str} double-scaled: eff_dpr={eff_dpr} ({info})" fakebin = os.path.join(home, "fakebin") os.makedirs(fakebin, exist_ok=True) child_env_path = os.path.join(home, "child_env.txt") launcher_stub = os.path.join(fakebin, "anode-launcher") with open(launcher_stub, "w", encoding="utf-8") as f: f.write("#!/bin/sh\nenv > \"$HOME/child_env.tmp\" && mv -f \"$HOME/child_env.tmp\" \"$HOME/child_env.txt\"\n") os.chmod(launcher_stub, 0o755) dirty_dwm_env = dict(env) dirty_dwm_env["PATH"] = fakebin + ":" + dirty_dwm_env.get("PATH", "") dirty_dwm_env["GDK_SCALE"] = "2" dirty_dwm_env["GDK_DPI_SCALE"] = "0.5" dwm = subprocess.Popen([os.path.join(BUILD, "dwm", "dwm")], env=dirty_dwm_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" root = X11.XDefaultRootWindow(dpy) bar_win = wait_until(lambda: next((w for w in query_children(dpy, root) if "anode-wm" in get_wm_class(dpy, w)), None)) assert bar_win, "DWM barwin not found" bev = XButtonEvent() bev.type = 4 # ButtonPress bev.display = dpy bev.window = bar_win bev.root = root bev.x = 2 bev.y = 2 bev.x_root = 2 bev.y_root = 2 bev.state = 0 bev.button = 1 bev.same_screen = 1 X11.XSendEvent(dpy, bar_win, 0, 1 << 2, ctypes.byref(bev)) X11.XSync(dpy, 0) assert wait_until(lambda: os.path.isfile(child_env_path)), "DWM did not spawn launcher on bar click" with open(child_env_path, "r", encoding="utf-8", errors="ignore") as f: child_lines = [line.strip() for line in f.read().splitlines() if line.strip()] assert "ANODE_SCALE=2.00" in child_lines, child_lines assert not any(line.startswith(("GDK_SCALE=", "GDK_DPI_SCALE=")) for line in child_lines), child_lines finally: terminate(dwm) terminate(xsd) if dpy: X11.XCloseDisplay(dpy) terminate(xvfb) def test_fullscreen_workspace_switch_focus(): if not shutil.which("xdotool"): print("test_x11_xvfb: xdotool not installed (skipping fullscreen 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", "") ) xvfb = subprocess.Popen( ["Xvfb", disp, "-noreset", "-screen", "0", "1280x800x24", "-nolisten", "tcp"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ) dpy = None dwm = None game = 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" game_code = ( "import ctypes, ctypes.util, json, sys, 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.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.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.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.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" "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" "dpy = X11.XOpenDisplay(None)\n" "root = X11.XDefaultRootWindow(dpy)\n" "win = X11.XCreateSimpleWindow(dpy, root, 0, 0, 1280, 800, 0, 0, 0x222222)\n" "wm_state = X11.XInternAtom(dpy, b'_NET_WM_STATE', 0)\n" "wm_fs = X11.XInternAtom(dpy, b'_NET_WM_STATE_FULLSCREEN', 0)\n" "fs_val = ctypes.c_ulong(wm_fs)\n" "X11.XChangeProperty(dpy, win, wm_state, 4, 32, 0, ctypes.byref(fs_val), 1)\n" "mask = (1<<0)|(1<<2)|(1<<6)|(1<<4)|(1<<5)|(1<<17)|(1<<21)\n" "X11.XSelectInput(dpy, win, mask)\n" "X11.XMapWindow(dpy, win)\n" "X11.XFlush(dpy)\n" "buf = (ctypes.c_long * 32)()\n" "print('READY', flush=True)\n" "t0 = time.monotonic()\n" "while time.monotonic() - t0 < 8.0:\n" " while X11.XPending(dpy):\n" " X11.XNextEvent(dpy, buf)\n" " et = ctypes.c_int.from_buffer(buf).value\n" " if et == 9:\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" " elif et == 22:\n" " ce = XConfigureEvent.from_buffer(buf)\n" " print(json.dumps({'ev': 'ConfigureNotify', 'pos': [ce.x, ce.y], 'size': [ce.width, ce.height]}), flush=True)\n" " elif et in (2, 4, 6, 7, 8):\n" " names = {2: 'KeyPress', 4: 'ButtonPress', 6: 'MotionNotify', 7: 'EnterNotify', 8: 'LeaveNotify'}\n" " print(json.dumps({'ev': names[et]}), flush=True)\n" " time.sleep(0.005)\n" ) game = subprocess.Popen( [sys.executable, "-c", game_code], env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, ) assert game.stdout.readline().strip() == "READY" time.sleep(0.35) subprocess.run(["xdotool", "mousemove", "640", "400", "click", "1"], env=env, check=True) time.sleep(0.15) # Switch Tag 1 -> Tag 2 while holding Super_L subprocess.run(["xdotool", "keydown", "Super_L"], env=env, check=True) time.sleep(0.08) subprocess.run(["xdotool", "key", "2"], env=env, check=True) time.sleep(0.12) subprocess.run(["xdotool", "keyup", "Super_L"], env=env, check=True) time.sleep(0.25) # Open and focus a window on Tag 2 so Tag 2 takes X11 focus/pointer tag2_client = subprocess.Popen( [os.path.join(BUILD, "st", "st"), "-e", "sh", "-c", "sleep 4"], 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.2) # Switch Tag 2 -> Tag 1 while holding Super_L (this previously caused GrabFrozen & offscreen FocusIn) subprocess.run(["xdotool", "keydown", "Super_L"], env=env, check=True) time.sleep(0.08) subprocess.run(["xdotool", "key", "1"], env=env, check=True) time.sleep(0.12) subprocess.run(["xdotool", "keyup", "Super_L"], env=env, check=True) time.sleep(0.3) # Interact with game after returning to Tag 1 subprocess.run(["xdotool", "mousemove", "500", "500", "click", "1", "mousemove", "700", "300", "key", "a"], env=env, check=True) time.sleep(0.25) # Verify quick Super_L tap-latch still works under GrabModeAsync subprocess.run(["xdotool", "key", "Super_L"], env=env, check=True) time.sleep(0.08) subprocess.run(["xdotool", "key", "2"], env=env, check=True) time.sleep(0.25) terminate(game) out, err = game.communicate(timeout=3) import json events = [json.loads(line) for line in out.splitlines() if line.strip().startswith("{")] offscreen_idx = next((i for i, e in enumerate(events) if e["ev"] == "ConfigureNotify" and e["pos"][0] < 0), None) assert offscreen_idx is not None, f"Game never moved offscreen on tag switch: {events}" post_return = events[offscreen_idx:] onscreen_cfg_idx = next( (i for i, e in enumerate(post_return) if e["ev"] == "ConfigureNotify" and e["pos"] == [0, 0] and e["size"] == [1280, 800]), None, ) assert onscreen_cfg_idx is not None, f"Missing ConfigureNotify(0,0) on return to Tag 1: {post_return}" first_focusin_idx = next((i for i, e in enumerate(post_return) if e["ev"] == "FocusIn"), None) assert first_focusin_idx is not None, f"Missing FocusIn on return to Tag 1: {post_return}" assert onscreen_cfg_idx < first_focusin_idx, f"FocusIn arrived before ConfigureNotify(0,0): {post_return}" fi = post_return[first_focusin_idx] assert fi["pos"] == [0, 0], f"Game was offscreen at FocusIn: {fi}" assert fi["mode"] == 0, f"FocusIn was not NotifyNormal (Super_L grab still active?): {fi}" assert fi["grab"] == 0, f"XGrabPointer failed on FocusIn after return (GrabFrozen/etc): {fi} ({post_return})" # Ensure no FocusIn while holding Super_L failed grab AND mouse/key events worked after returning post_focus_names = [e["ev"] for e in post_return[first_focusin_idx:]] for req in ("MotionNotify", "ButtonPress", "KeyPress"): assert req in post_focus_names, f"Missing {req} after return to Tag 1: {post_return}" # Confirm tap-latch switched back away to Tag 2 at the end last_off = next((i for i, e in enumerate(post_return[first_focusin_idx:]) if e["ev"] == "ConfigureNotify" and e["pos"][0] < 0), None) assert last_off is not None, f"Super_L tap-latch did not switch back to Tag 2: {post_return}" finally: terminate(tag2_client) terminate(game) terminate(dwm) if dpy: X11.XCloseDisplay(dpy) terminate(xvfb) def test_wine_take_focus_workspace_switch(): if not shutil.which("xdotool"): print("test_x11_xvfb: xdotool not installed (skipping Wine WM_TAKE_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.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.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 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.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.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" "win = X11.XCreateSimpleWindow(dpy, root, 0, 0, 800, 600, 0, 0, 0x222222)\n" "wmh = X11.XAllocWMHints()\n" "wmh.contents.flags = 1\n" # InputHint "wmh.contents.input = 0\n" # globally-active Wine-style client "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" "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_maxh = X11.XInternAtom(dpy, b'_NET_WM_STATE_MAXIMIZED_HORZ', 0)\n" "wm_fs = X11.XInternAtom(dpy, b'_NET_WM_STATE_FULLSCREEN', 0)\n" "states = (ctypes.c_ulong * 2)(wm_maxh, wm_fs)\n" # fullscreen NOT at index 0 "X11.XChangeProperty(dpy, win, wm_state, 4, 32, 0, states, 2)\n" "mask = (1<<0)|(1<<2)|(1<<6)|(1<<4)|(1<<5)|(1<<17)|(1<<21)\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" "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" " 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 == 9:\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" " elif et == 22:\n" " ce = XConfigureEvent.from_buffer(buf)\n" " print(json.dumps({'ev': 'ConfigureNotify', 'pos': [ce.x, ce.y], 'size': [ce.width, ce.height]}), flush=True)\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" " 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) net_wm_state_atom = X11.XInternAtom(dpy, b"_NET_WM_STATE", 0) atom_maxh = X11.XInternAtom(dpy, b"_NET_WM_STATE_MAXIMIZED_HORZ", 0) atom_fs = X11.XInternAtom(dpy, b"_NET_WM_STATE_FULLSCREEN", 0) atom_foc = X11.XInternAtom(dpy, b"_NET_WM_STATE_FOCUSED", 0) def get_win_states(): return get_cardinal_prop(dpy, win_id, "_NET_WM_STATE") init_states = wait_until( lambda: (lambda s: s if atom_fs in s and atom_foc in s else None)(get_win_states()) ) assert init_states is not None, f"Missing expected _NET_WM_STATE atoms: {get_win_states()}" assert atom_maxh in init_states, f"_NET_WM_STATE_MAXIMIZED_HORZ was overwritten: {init_states}" a_init = XWindowAttributes() X11.XGetWindowAttributes(dpy, win_id, ctypes.byref(a_init)) assert (a_init.x, a_init.y, a_init.width, a_init.height) == (0, 0, 1280, 800), ( f"Multi-atom _NET_WM_STATE_FULLSCREEN not detected fullscreen: {(a_init.x, a_init.y, a_init.width, a_init.height)}" ) # Switch Tag 1 -> Tag 2 via Super_L + 2, spawn and click st on Tag 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.2) # 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) # Phase 2: switch away to Tag 2 via Super_L + 2, wait, then return via bar Tag 1 click (x=50, y=10) 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) subprocess.run(["xdotool", "mousemove", "400", "400", "click", "1"], env=env, check=True) time.sleep(0.2) subprocess.run(["xdotool", "mousemove", "50", "10", "click", "1"], env=env, check=True) time.sleep(0.3) subprocess.run(["xdotool", "mousemove", "520", "480", "click", "1", "mousemove", "720", "320", "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_indices = [i for i, e in enumerate(events) if e["ev"] == "ConfigureNotify" and e["pos"][0] < 0] assert len(off_indices) >= 2, f"Expected two workspace switches away from Tag 1: {events}" pre_switch = events[:off_indices[0]] init_tf = [e for e in pre_switch if e["ev"] == "WM_TAKE_FOCUS"] assert init_tf and init_tf[0]["time"] > 0, f"Initial WM_TAKE_FOCUS must have non-zero time: {pre_switch}" # Check Phase 1 (Super_L + 1 return) seg1 = events[off_indices[0]:off_indices[1]] tf1 = [e for e in seg1 if e["ev"] == "WM_TAKE_FOCUS"] assert len(tf1) == 1, f"Expected exactly 1 WM_TAKE_FOCUS on Super_L+1 return, got {len(tf1)}: {seg1}" assert tf1[0]["time"] > init_tf[-1]["time"], f"WM_TAKE_FOCUS timestamp did not advance: {tf1[0]} vs {init_tf[-1]}" fi1 = [e for e in seg1 if e["ev"] == "FocusIn"] assert fi1, f"Missing FocusIn on Super_L+1 return: {seg1}" assert fi1[0]["mode"] == 0 and fi1[0]["pos"] == [0, 0] and fi1[0]["grab"] == 0, f"Bad FocusIn/grab on Super_L+1 return: {fi1[0]}" seg1_names = [e["ev"] for e in seg1] for req in ("MotionNotify", "ButtonPress", "KeyPress"): assert req in seg1_names, f"Missing {req} after Super_L+1 return: {seg1}" # Check Phase 2 (bar Tag 1 click return) seg2 = events[off_indices[1]:] tf2 = [e for e in seg2 if e["ev"] == "WM_TAKE_FOCUS"] assert len(tf2) == 1, f"Expected exactly 1 WM_TAKE_FOCUS on bar click return, got {len(tf2)}: {seg2}" assert tf2[0]["time"] > tf1[0]["time"], f"WM_TAKE_FOCUS timestamp did not advance on bar click: {tf2[0]} vs {tf1[0]}" fi2 = [e for e in seg2 if e["ev"] == "FocusIn"] assert fi2, f"Missing FocusIn on bar click return: {seg2}" assert fi2[0]["mode"] == 0 and fi2[0]["pos"] == [0, 0] and fi2[0]["grab"] == 0, f"Bad FocusIn/grab on bar click return: {fi2[0]}" seg2_names = [e["ev"] for e in seg2] for req in ("MotionNotify", "ButtonPress", "KeyPress"): assert req in seg2_names, f"Missing {req} after bar click return: {seg2}" finally: terminate(tag2_client) terminate(wine_sim) terminate(dwm) if dpy: X11.XCloseDisplay(dpy) 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: 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(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 cdpy = None dwm = None try: dpy = wait_until(lambda: X11.XOpenDisplay(disp.encode())) assert dpy, f"Failed to open Xvfb {disp}" cdpy = wait_until(lambda: X11.XOpenDisplay(disp.encode())) assert cdpy, f"Failed to open client display on Xvfb {disp}" root = X11.XDefaultRootWindow(dpy) dwm = subprocess.Popen([os.path.join(BUILD, "dwm", "dwm")], env=env) tray_atom = X11.XInternAtom(dpy, b"_NET_SYSTEM_TRAY_S0", 0) tray_win = wait_until(lambda: X11.XGetSelectionOwner(dpy, tray_atom)) assert tray_win, "DWM failed to acquire _NET_SYSTEM_TRAY_S0" xembed_info = X11.XInternAtom(cdpy, b"_XEMBED_INFO", 0) op_atom = X11.XInternAtom(cdpy, b"_NET_SYSTEM_TRAY_OPCODE", 0) def set_xembed(ic, mapped): arr = (ctypes.c_ulong * 2)(1, 1 if mapped else 0) X11.XChangeProperty(cdpy, ic, xembed_info, xembed_info, 32, 0, arr, 2) def send_dock(ic): ev = XClientMessageEvent() ev.type = 33 ev.display = cdpy ev.window = tray_win ev.message_type = op_atom ev.format = 32 ev.data.l[0] = 0 ev.data.l[1] = 0 ev.data.l[2] = ic X11.XSendEvent(cdpy, tray_win, 0, 0, ctypes.byref(ev)) def assert_icon_viewable_resized(ic, desc): def check(): if ic not in query_children(dpy, tray_win): return None a = XWindowAttributes() if not X11.XGetWindowAttributes(dpy, ic, ctypes.byref(a)): return None if a.map_state == 2 and 8 <= a.width <= 48 and 8 <= a.height <= 48: return (a.width, a.height) return None res = wait_until(check, timeout=3.0) if not res: a = XWindowAttributes() X11.XGetWindowAttributes(dpy, ic, ctypes.byref(a)) raise AssertionError( f"{desc}: icon {hex(ic)} failed systray check " f"(in_tray={ic in query_children(dpy, tray_win)}, map_state={a.map_state}, size={a.width}x{a.height})" ) # Subtest A: GTK2/HexChat burst race (_XEMBED_INFO=(1,0) -> REQUEST_DOCK -> _XEMBED_INFO=(1,1)) for delay in (0.0, 0.0005, 0.002, 0.01): ic = X11.XCreateSimpleWindow(cdpy, root, 0, 0, 200, 200, 0, 0, 0xffffff) set_xembed(ic, False) send_dock(ic) X11.XFlush(cdpy) if delay > 0.0: time.sleep(delay) set_xembed(ic, True) X11.XFlush(cdpy) assert_icon_viewable_resized(ic, f"Subtest A (delay={delay})") # Subtest B: Already-mapped icon docking ic_mapped = X11.XCreateSimpleWindow(cdpy, root, 0, 0, 200, 200, 0, 0, 0xffffff) swa = XSetWindowAttributes() swa.override_redirect = 1 X11.XChangeWindowAttributes(cdpy, ic_mapped, 1 << 9, ctypes.byref(swa)) set_xembed(ic_mapped, True) X11.XMapWindow(cdpy, ic_mapped) X11.XSync(cdpy, 0) send_dock(ic_mapped) X11.XFlush(cdpy) assert_icon_viewable_resized(ic_mapped, "Subtest B (already-mapped)") # Subtest C: Toggle XEMBED_MAPPED 1 -> 0 -> 1 ic_toggle = X11.XCreateSimpleWindow(cdpy, root, 0, 0, 200, 200, 0, 0, 0xffffff) set_xembed(ic_toggle, True) send_dock(ic_toggle) X11.XFlush(cdpy) assert_icon_viewable_resized(ic_toggle, "Subtest C (initial map)") set_xembed(ic_toggle, False) X11.XFlush(cdpy) def is_unmapped(): a = XWindowAttributes() return X11.XGetWindowAttributes(dpy, ic_toggle, ctypes.byref(a)) and a.map_state == 0 assert wait_until(is_unmapped, timeout=3.0), "Subtest C: icon did not unmap on XEMBED_MAPPED=0" set_xembed(ic_toggle, True) X11.XFlush(cdpy) assert_icon_viewable_resized(ic_toggle, "Subtest C (remap)") finally: terminate(dwm) if cdpy: X11.XCloseDisplay(cdpy) if dpy: X11.XCloseDisplay(dpy) terminate(xvfb) def main(): if not shutil.which("Xvfb"): print("test_x11_xvfb: Xvfb not installed (skipping)") return 0 cases = [ ("1024x600", "1.00", 1024, 600), ("1920x1080", "1.25", 1920, 1080), ("3840x2160", "2.00", 3840, 2160), ] for res, sc, w, h in cases: test_resolution(res, sc, w, h) print(f"test_x11_xvfb: {res} @ {sc}x OK") test_st_font_override_and_dunst() print("test_x11_xvfb: St.font override & Dunst scale=1 OK") test_lockscreen_overlaps_systray() print("test_x11_xvfb: lockscreen overlaps systray OK") test_gtk_xsettings_no_double_scale() print("test_x11_xvfb: GTK/XSETTINGS non-double-scaling OK") test_fullscreen_workspace_switch_focus() 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 if __name__ == "__main__": sys.exit(main())