Replay browse keys when native focus leaves the document window

This commit is contained in:
Storm Dragon committed 2026-11-19 04:27:39 -05:00
1 parent 938847e6ae
commit 58d8f6bee1
7 files changed
+386 -2

No files matched your search

+1
View File
@@ -12,6 +12,7 @@ from native_test_session import run_private_session
BROWSER_PROOFS = {
"focus-routing": ("./internal/platform/xorg", "^TestNativeBrowseFocusRouting$", 20, 25),
"find": ("./internal/app", "^TestNativeBrowserFind$", 35, 40, "browser-find.html"),
"disclosure": ("./internal/app", "^TestNativeBrowserDisclosureFocus$", 35, 40, "browser-actions.html"),
"actions": ("./internal/app", "^TestNativeBrowserActionOwnership$", 35, 40, "browser-actions.html"),
+1 -1
View File
@@ -175,7 +175,7 @@ def run_inside(directory):
subprocess.run(['xdotool', 'windowfocus', window], check=True)
nativeTest = os.environ.get('CTHULHU_XRECORD_TEST')
if nativeTest:
native_tests = os.environ.get('CTHULHU_NATIVE_X11_TESTS', '^TestNative(ModifierPressRelease|CommandBypass|CommandGrabSuspensionAndRestoration|BrowseKeyTransitions|DispatchBeforeRelease|CustomBindingsUpdateAndRelease|TentacleBindingsUpdateAndRelease|ModifierConfigurationUpdate|LockStatesAndShiftCapsLock|XTermHandoffKeepsKeypadReleased|SuspendedQueuedKey|LearnModeCapturesKeysAndRestoresCommands|SecureInputDropsKeysAndRestoresGrabs)$')
native_tests = os.environ.get('CTHULHU_NATIVE_X11_TESTS', '^TestNative(ModifierPressRelease|CommandBypass|CommandGrabSuspensionAndRestoration|BrowseFocusRouting|BrowseKeyTransitions|DispatchBeforeRelease|CustomBindingsUpdateAndRelease|TentacleBindingsUpdateAndRelease|ModifierConfigurationUpdate|LockStatesAndShiftCapsLock|XTermHandoffKeepsKeypadReleased|SuspendedQueuedKey|LearnModeCapturesKeysAndRestoresCommands|SecureInputDropsKeysAndRestoresGrabs)$')
subprocess.run([nativeTest, '-test.run', native_tests, '-test.timeout=40s'], env=dict(os.environ, CTHULHU_NATIVE_X11='1', CTHULHU_NATIVE_BROWSER='1'), check=True, timeout=45, cwd=Path(__file__).resolve().parents[1] / 'internal/platform/xorg')
if nativeTest and os.environ.get('CTHULHU_NATIVE_LISTENER_ONLY') == '1':
print('PASS: selected native X11 command listener tests')
+124
View File
@@ -0,0 +1,124 @@
"""Record real XTest key delivery to windows of one private X11 client.
F12's release ends each batch: receiving it proves earlier keys have passed
through any synchronous reader grab. No timeout or quiet period counts as proof
that a key was consumed.
"""
import json
import os
import select
import sys
from Xlib import X, Xatom, XK, display
from Xlib.ext import xtest
def main():
connection = display.Display()
root = connection.screen().root
activeAtom = connection.intern_atom("_NET_ACTIVE_WINDOW")
stateAtom = connection.intern_atom("WM_STATE")
pidAtom = connection.intern_atom("_NET_WM_PID")
leaderAtom = connection.intern_atom("WM_CLIENT_LEADER")
oldActive = root.get_full_property(activeAtom, X.AnyPropertyType)
oldFocus = connection.get_input_focus()
windows = {}
for index, name in enumerate(("document", "other")):
window = root.create_window(
20 + index * 350, 20, 300, 200, 0,
connection.screen().root_depth, X.InputOutput, X.CopyFromParent,
event_mask=X.KeyPressMask | X.KeyReleaseMask,
)
window.set_wm_class("cthulhu-key-probe", "CthulhuKeyProbe")
window.change_property(pidAtom, Xatom.CARDINAL, 32, [os.getpid()])
window.change_property(leaderAtom, Xatom.WINDOW, 32, [window.id])
window.change_property(stateAtom, stateAtom, 32, [1, 0])
window.map()
windows[name] = window
child = window.create_window(
0, 0, 100, 100, 0, connection.screen().root_depth,
X.InputOutput, X.CopyFromParent,
event_mask=X.KeyPressMask | X.KeyReleaseMask,
)
child.map()
windows[name + "-child"] = child
popup = root.create_window(
20 + index * 350, 30, 100, 60, 0, connection.screen().root_depth,
X.InputOutput, X.CopyFromParent, override_redirect=True,
)
grab = root.create_window(
0, 0, 1, 1, 0, 0, X.InputOnly, X.CopyFromParent,
override_redirect=True, event_mask=X.KeyPressMask | X.KeyReleaseMask,
)
for member in (popup, grab):
member.change_property(pidAtom, Xatom.CARDINAL, 32, [os.getpid()])
member.change_property(leaderAtom, Xatom.WINDOW, 32, [window.id])
member.map()
popup.set_wm_transient_for(window)
popup.change_property(connection.intern_atom("_NET_WM_WINDOW_TYPE"), Xatom.ATOM, 32,
[connection.intern_atom("_NET_WM_WINDOW_TYPE_COMBO")])
windows[name + "-popup"] = popup
windows[name + "-grab"] = grab
names = {window.id: name for name, window in windows.items()}
keyNames = ("h", "k", "Right", "Down", "Return", "F12")
codes = {name: connection.keysym_to_keycode(XK.string_to_keysym(name)) for name in keyNames}
symbols = {code: name for name, code in codes.items()}
windows["document"].set_input_focus(X.RevertToParent, X.CurrentTime)
root.change_property(activeAtom, Xatom.WINDOW, 32, [windows["document"].id])
connection.sync()
print(json.dumps({"windows": {name: window.id for name, window in windows.items()}}), flush=True)
records = []
try:
while True:
while connection.pending_events():
event = connection.next_event()
if event.type not in (X.KeyPress, X.KeyRelease):
continue
name = symbols.get(event.detail, str(event.detail))
if name == "F12":
if event.type == X.KeyRelease:
print(json.dumps({"events": records}), flush=True)
records = []
else:
records.append({"window": names[event.window.id], "key": name,
"pressed": event.type == X.KeyPress})
ready, _, _ = select.select([sys.stdin, connection], [], [])
if sys.stdin not in ready:
continue
line = sys.stdin.readline()
if not line:
return
request = json.loads(line)
# Prevent the command owner from observing a halfway-completed
# focus transition before the first key has reached the server.
connection.grab_server()
try:
if "focus" in request:
windows[request["focus"]].set_input_focus(X.RevertToParent, X.CurrentTime)
if "active" in request:
if request["active"]:
root.change_property(activeAtom, Xatom.WINDOW, 32,
[windows[request["active"]].id])
else:
root.delete_property(activeAtom)
for key in request.get("keys", []):
xtest.fake_input(connection, X.KeyPress, codes[key])
xtest.fake_input(connection, X.KeyRelease, codes[key])
xtest.fake_input(connection, X.KeyPress, codes["F12"])
xtest.fake_input(connection, X.KeyRelease, codes["F12"])
finally:
connection.ungrab_server()
connection.flush()
finally:
if oldActive is None:
root.delete_property(activeAtom)
else:
root.change_property(activeAtom, oldActive.property_type, oldActive.format, oldActive.value)
connection.set_input_focus(oldFocus.focus, oldFocus.revert_to, X.CurrentTime)
connection.sync()
connection.close()
if __name__ == "__main__":
main()