vdi-cloud/tools/inject-mouse-sweep.py

32 lines
1.2 KiB
Python

"""X11 benchmark only: 20 seconds of 125 Hz motion, no buttons or keys."""
import ctypes
import math
import time
x11 = ctypes.CDLL("libX11.so.6")
xtest = ctypes.CDLL("libXtst.so.6")
x11.XOpenDisplay.argtypes = [ctypes.c_char_p]
x11.XOpenDisplay.restype = ctypes.c_void_p
x11.XFlush.argtypes = [ctypes.c_void_p]
x11.XCloseDisplay.argtypes = [ctypes.c_void_p]
xtest.XTestFakeMotionEvent.argtypes = [ctypes.c_void_p, ctypes.c_int,
ctypes.c_int, ctypes.c_int, ctypes.c_ulong]
display = x11.XOpenDisplay(None)
if not display:
raise RuntimeError("X11 unavailable")
start = time.monotonic()
count = 0
try:
while time.monotonic() - start < 20:
elapsed = time.monotonic() - start
x = round(1280 + 1000 * math.sin(elapsed * 4))
y = round(720 + 450 * math.cos(elapsed * 3))
if not xtest.XTestFakeMotionEvent(display, -1, x, y, 0):
raise RuntimeError("Motion injection failed")
x11.XFlush(display)
count += 1
time.sleep(max(0, start + count / 125 - time.monotonic()))
finally:
x11.XCloseDisplay(display)
print(f"Injected {count} motion events in {time.monotonic()-start:.2f}s")