"""Persistent cursor requests, the application cursor and real SDL bitmap cursors (premultiplied, cached, steady) on private Xvfb, then the same requests as CSS cursors in Canvas and DOM pages in a private headless Chromium.""" import ctypes as c import os from pathlib import Path import shutil import subprocess import sys import time from PIL import Image from toolchain import UI_MODULE_ARGS, ROOT, ZIRAN_ROOT, ZIRAN OUTPUT = ROOT / 'build/test/cursor-api' ENV = dict(os.environ) for name in ('DISPLAY','WAYLAND_DISPLAY','XAUTHORITY','GDK_DISPLAY','DBUS_SESSION_BUS_ADDRESS', 'SESSION_MANAGER','DESKTOP_SESSION','XDG_SESSION_ID','XDG_SESSION_TYPE', 'XDG_CURRENT_DESKTOP','XDG_SESSION_DESKTOP','XDG_RUNTIME_DIR', 'GNOME_DESKTOP_SESSION_ID','KDE_FULL_SESSION', 'KRYON_CAPTURE_PATH','KRYON_TEST_DRIVER'): ENV.pop(name, None) ENV.update(YUE_DESKTOP_RECOVERY='0', TMPDIR=str(OUTPUT), SDL_RENDER_DRIVER='software') ENV['KRYON_APP_DATA'] = str(OUTPUT / 'portable-data') ENV.setdefault('EM_CACHE', str(ROOT / 'build/emscripten-cache')) def run(args, cwd=ROOT, **kw): try: return subprocess.run(list(map(str,args)),cwd=cwd,env=ENV,check=True,**kw) except subprocess.CalledProcessError as failure: print(f'Cursor local test command failed ({failure.returncode}): {args[0]} in {cwd}', file=sys.stderr) raise SystemExit(failure.returncode) def wait(check, message): end = time.monotonic() + 20 while time.monotonic() < end: if check(): return time.sleep(.04) raise AssertionError(message) class CursorImage(c.Structure): _fields_ = [('x',c.c_short),('y',c.c_short),('width',c.c_ushort),('height',c.c_ushort), ('xhot',c.c_ushort),('yhot',c.c_ushort),('serial',c.c_ulong), ('pixels',c.POINTER(c.c_ulong)),('atom',c.c_ulong),('name',c.c_char_p)] def drive(mode): assert os.environ.get('KRYON_PRIVATE_XVFB') == '1' display = os.environ['DISPLAY'] assert display != ':0' x = c.CDLL('libX11.so.6'); fix = c.CDLL('libXfixes.so.3') x.XOpenDisplay.argtypes=[c.c_char_p]; x.XOpenDisplay.restype=c.c_void_p x.XFree.argtypes=[c.c_void_p]; x.XSync.argtypes=[c.c_void_p,c.c_int] x.XCloseDisplay.argtypes=[c.c_void_p] x.XDefaultRootWindow.argtypes=[c.c_void_p]; x.XDefaultRootWindow.restype=c.c_ulong x.XCreateSimpleWindow.argtypes=[c.c_void_p,c.c_ulong,c.c_int,c.c_int,c.c_uint,c.c_uint,c.c_uint,c.c_ulong,c.c_ulong] x.XCreateSimpleWindow.restype=c.c_ulong x.XMapWindow.argtypes=[c.c_void_p,c.c_ulong] x.XSetInputFocus.argtypes=[c.c_void_p,c.c_ulong,c.c_int,c.c_ulong] x.XDestroyWindow.argtypes=[c.c_void_p,c.c_ulong] fix.XFixesGetCursorImage.argtypes=[c.c_void_p]; fix.XFixesGetCursorImage.restype=c.POINTER(CursorImage) connection=x.XOpenDisplay(display.encode()); assert connection class Data(c.Union): _fields_=[('values',c.c_long*5),('pad',c.c_char*20)] class Message(c.Structure): _fields_=[('type',c.c_int),('serial',c.c_ulong),('send_event',c.c_int), ('display',c.c_void_p),('window',c.c_ulong),('message_type',c.c_ulong), ('format',c.c_int),('data',Data)] x.XInternAtom.argtypes=[c.c_void_p,c.c_char_p,c.c_int]; x.XInternAtom.restype=c.c_ulong x.XSendEvent.argtypes=[c.c_void_p,c.c_ulong,c.c_int,c.c_long,c.c_void_p] def close_window(window): event=Message(type=33,send_event=1,display=connection,window=int(window),format=32) event.message_type=x.XInternAtom(connection,b'WM_PROTOCOLS',0) event.data.values[0]=x.XInternAtom(connection,b'WM_DELETE_WINDOW',0) x.XSendEvent(connection,int(window),0,0,c.byref(event)); x.XSync(connection,0) def cursor(): x.XSync(connection,0) p=fix.XFixesGetCursorImage(connection); assert p v=p.contents result=(v.width,v.height,v.xhot,v.yhot,tuple(v.pixels[i] & 0xffffffff for i in range(v.width*v.height)),v.serial) x.XFree(p) return result outside=cursor() command=[OUTPUT/'build/cursor_probe-desktop'] if mode=='native' else [ROOT/'build/bin/zx',OUTPUT/'build/cursor_probe-portable.zx'] with (OUTPUT/f'{mode}.log').open('w') as log: child=subprocess.Popen(list(map(str,command)),cwd=OUTPUT,stdout=log,stderr=subprocess.STDOUT) window=None def locate(): nonlocal window assert child.poll() is None, f'{mode} exited; inspect {OUTPUT / (mode+".log")}' found=subprocess.run(['xdotool','search','--onlyvisible','--pid',str(child.pid)],capture_output=True,text=True) if found.returncode==0 and found.stdout.strip(): window=found.stdout.splitlines()[0]; return True return False def xdo(*args): subprocess.run(['xdotool',*map(str,args)],check=True,timeout=5) def key(n): xdo('key','--window',window,str(n)) def bitmap(colour,size,hot): value=cursor() return value[:4]==(*size,*hot) and any((p&0xffffff)==colour and p>>24==255 for p in value[4]) def hand(hot=(3,5)): return bitmap(0xe02040,(32,24),hot) def tool(): return bitmap(0x20a040,(32,24),(4,20)) def app(): return bitmap(0x2040e0,(32,32),(7,1)) def hidden(): return not any(cursor()[4]) def premultiplied(): # Xcursor composites premultiplied ARGB; a colour channel above # its alpha shows as a light fringe over dark pictures. pixels=cursor()[4] semi=[p for p in pixels if 0 < p>>24 < 255] assert semi, 'Cursor lost its antialiased edge' bad=[hex(p) for p in pixels if max((p>>16)&255,(p>>8)&255,p&255) > p>>24] assert not bad, f'Cursor pixels are not premultiplied (light fringe): {bad[:6]}' def steady(seconds=.6): # A repeated request must not re-create or re-select the cursor. first=cursor(); end=time.monotonic()+seconds while time.monotonic()