Medlar/tools/lib/xresize.py
astar76 529c509c05 S003: screenshot and hidden-run tools, notes
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 00:28:08 -06:00

29 lines
1.4 KiB
Python

"""Resize every mapped top-level window of an X display (Xvfb has no window manager): xresize.py :N W H.
tools/nested-session.sh uses it to give niri's winit output the requested size. Plain ctypes over libX11.
"""
import ctypes
import sys
x = ctypes.CDLL("libX11.so.6")
x.XOpenDisplay.restype = ctypes.c_void_p
x.XOpenDisplay.argtypes = [ctypes.c_char_p]
x.XDefaultRootWindow.restype = ctypes.c_ulong
x.XDefaultRootWindow.argtypes = [ctypes.c_void_p]
x.XQueryTree.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.POINTER(ctypes.c_ulong), ctypes.POINTER(ctypes.c_ulong),
ctypes.POINTER(ctypes.POINTER(ctypes.c_ulong)), ctypes.POINTER(ctypes.c_uint)]
x.XResizeWindow.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.c_uint, ctypes.c_uint]
x.XSync.argtypes = [ctypes.c_void_p, ctypes.c_int]
x.XCloseDisplay.argtypes = [ctypes.c_void_p]
display, w, h = sys.argv[1], int(sys.argv[2]), int(sys.argv[3])
d = x.XOpenDisplay(display.encode())
if not d:
sys.exit(f"xresize: can't open {display}")
root, parent, children, n = ctypes.c_ulong(), ctypes.c_ulong(), ctypes.POINTER(ctypes.c_ulong)(), ctypes.c_uint()
x.XQueryTree(d, x.XDefaultRootWindow(d), ctypes.byref(root), ctypes.byref(parent), ctypes.byref(children), ctypes.byref(n))
for i in range(n.value):
x.XResizeWindow(d, children[i], w, h)
x.XSync(d, 0)
x.XCloseDisplay(d)
print(f"xresize: {n.value} window(s) to {w}x{h}")