Files
cthulhu/tests/x11_focus_child.py

46 lines
1.5 KiB
Python

#!/usr/bin/env python3
"""Move X input focus into a child of the current test client window."""
import ctypes
import sys
def main() -> None:
parent = int(sys.argv[1])
x11 = ctypes.CDLL("libX11.so.6")
x11.XOpenDisplay.argtypes = [ctypes.c_char_p]
x11.XOpenDisplay.restype = ctypes.c_void_p
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,
]
x11.XCreateSimpleWindow.restype = ctypes.c_ulong
x11.XMapWindow.argtypes = [ctypes.c_void_p, ctypes.c_ulong]
x11.XSetInputFocus.argtypes = [
ctypes.c_void_p, ctypes.c_ulong, ctypes.c_int, ctypes.c_ulong,
]
x11.XSync.argtypes = [ctypes.c_void_p, ctypes.c_int]
x11.XCloseDisplay.argtypes = [ctypes.c_void_p]
display = x11.XOpenDisplay(None)
if not display:
raise RuntimeError("X11 display unavailable")
try:
child = x11.XCreateSimpleWindow(display, parent, 0, 0, 1, 1, 0, 0, 0)
if not child:
raise RuntimeError("could not create child window")
x11.XMapWindow(display, child)
x11.XSetInputFocus(display, child, 2, 0) # RevertToParent, CurrentTime
x11.XSync(display, 0)
print(child, flush=True)
sys.stdin.readline()
x11.XSetInputFocus(display, parent, 2, 0)
x11.XSync(display, 0)
finally:
x11.XCloseDisplay(display)
if __name__ == "__main__":
main()