Files
cthulhu/tests/gtk_window_switcher.py

65 lines
2.1 KiB
Python

#!/usr/bin/env python3
# Copyright (c) 2026 Storm Dragon
# Authors: Storm Dragon, Angus Kola
# SPDX-License-Identifier: GPL-3.0-or-later
"""A non-focusable status container in a private Marco-shaped GTK provider."""
import ctypes
import os
import sys
import gi
gi.require_version("Gtk", "3.0")
gi.require_version("Atk", "1.0")
gi.require_version("GdkX11", "3.0")
from gi.repository import Atk, GdkX11, GLib, Gtk
providerName = sys.argv[1]
GLib.set_prgname(providerName)
# Match /proc/PID/comm as well as the AT-SPI name; a name alone must not
# enable the window-manager exception.
if providerName == "marco":
if ctypes.CDLL(None).prctl(15, b"marco", 0, 0, 0) != 0:
raise RuntimeError("cannot set fixture process name")
window = Gtk.Window(title="Switcher fixture " + providerName)
# Label updates must not resize the foreground window while the test compares
# its full X11 snapshot to check that the switcher has not taken ownership.
window.set_default_size(400, 100)
if providerName == "marco":
window.set_accept_focus(False)
window.set_focus_on_map(False)
label = Gtk.Label(label="Initial selection")
label.get_accessible().set_role(Atk.Role.STATUSBAR)
window.add(label)
window.show_all()
Atk.get_root().set_name(providerName)
pendingInput = ""
def receive_command(_source, _condition):
global pendingInput
content = os.read(sys.stdin.fileno(), 4096)
if not content:
Gtk.main_quit()
return False
pendingInput += content.decode()
while "\n" in pendingInput:
command, pendingInput = pendingInput.split("\n", 1)
if command.startswith("select "):
label.set_text(command.removeprefix("select "))
elif command == "showing":
label.get_accessible().notify_state_change(Atk.StateType.SHOWING, True)
elif command == "hide":
label.hide()
elif command == "quit":
Gtk.main_quit()
return False
return True
GLib.io_add_watch(sys.stdin, GLib.IO_IN | GLib.IO_HUP, receive_command)
GLib.idle_add(lambda: print("ready", GdkX11.X11Window.get_xid(window.get_window()), flush=True) or False)
Gtk.main()