import ctypes as c, re, sys, time, os from pathlib import Path # Only the Xwayland display created by this specific hidden test process. log=Path(sys.argv[1]);assert log.parent.name.startswith('err-held-pages-') text=log.read_text(errors='replace');match=re.search(r'Starting Xwayland on (:\d+)',text);assert match name=match[1];assert name!=os.environ.get('DISPLAY') x=c.CDLL('libX11.so.6');t=c.CDLL('libXtst.so.6');P=c.c_void_p;U=c.c_ulong x.XOpenDisplay.argtypes=[c.c_char_p];x.XOpenDisplay.restype=P x.XKeysymToKeycode.argtypes=[P,U];x.XKeysymToKeycode.restype=c.c_uint x.XFlush.argtypes=[P];x.XCloseDisplay.argtypes=[P] t.XTestFakeKeyEvent.argtypes=[P,c.c_uint,c.c_int,U];t.XTestFakeKeyEvent.restype=c.c_int d=x.XOpenDisplay(name.encode());assert d x.XDefaultRootWindow.argtypes=[P];x.XDefaultRootWindow.restype=U x.XQueryTree.argtypes=[P,U,c.POINTER(U),c.POINTER(U),c.POINTER(c.POINTER(U)),c.POINTER(c.c_uint)] x.XFetchName.argtypes=[P,U,c.POINTER(c.c_char_p)] x.XSetInputFocus.argtypes=[P,U,c.c_int,U];x.XFree.argtypes=[P] root=x.XDefaultRootWindow(d);r=U();parent=U();children=c.POINTER(U)();n=c.c_uint() x.XQueryTree(d,root,c.byref(r),c.byref(parent),c.byref(children),c.byref(n)) for i in range(n.value): title=c.c_char_p();x.XFetchName(d,children[i],c.byref(title));caption=title.value.decode(errors='replace') if title.value else '' if 'World Editor' in caption or 'WorldEditor' in caption: x.XSetInputFocus(d,children[i],2,0);x.XFlush(d);print('Private test keyboard target:',caption,flush=True) if title:x.XFree(title) x.XFree(children) key={'Right':0xff53,'Down':0xff54,'m':ord('m')}[sys.argv[2]];code=x.XKeysymToKeycode(d,key);assert code for i in range(int(sys.argv[3]) if len(sys.argv)>3 else 1): assert t.XTestFakeKeyEvent(d,code,1,0);x.XFlush(d);time.sleep(.09) assert t.XTestFakeKeyEvent(d,code,0,0);x.XFlush(d);time.sleep(.14) x.XCloseDisplay(d)