#!/usr/bin/env python3 """Send an XTest keycode to the runner's private X server, without remapping it.""" import argparse import ctypes as c import os from pathlib import Path p = argparse.ArgumentParser(description=__doc__) p.add_argument('keycode', type=int) p.add_argument('--shift', action='store_true') a = p.parse_args() runtime = Path(os.environ.get('XDG_RUNTIME_DIR', '/')) if os.environ.get('DOCVIEW_PRIVATE_IME') != '1' or not runtime.name.startswith('docview-ime-'): p.error('Only the dedicated private test session is permitted') if not 8 <= a.keycode <= 255: p.error('X11 keycode is out of range') x = c.CDLL('libX11.so.6'); t = c.CDLL('libXtst.so.6') x.XOpenDisplay.argtypes = [c.c_char_p]; x.XOpenDisplay.restype = c.c_void_p x.XSync.argtypes = [c.c_void_p, c.c_int]; x.XCloseDisplay.argtypes = [c.c_void_p] t.XTestFakeKeyEvent.argtypes = [c.c_void_p, c.c_uint, c.c_int, c.c_ulong] d = x.XOpenDisplay(None) if not d: raise SystemExit('Private display is unavailable') try: if a.shift: assert t.XTestFakeKeyEvent(d, 50, 1, 0) assert t.XTestFakeKeyEvent(d, a.keycode, 1, 0) assert t.XTestFakeKeyEvent(d, a.keycode, 0, 0) if a.shift: assert t.XTestFakeKeyEvent(d, 50, 0, 0) x.XSync(d, 0) finally: x.XCloseDisplay(d)