Files
cthulhu/tests/gtk_web_notification.py

72 lines
2.3 KiB
Python

#!/usr/bin/env python3
"""Controlled AT-SPI toast provider; process names are fixture identities."""
import ctypes
import gi
import os
import sys
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
# PR_SET_NAME changes only this fixture's identity; no Steam process is used.
libc = ctypes.CDLL(None)
if libc.prctl(15, sys.argv[1].encode(), 0, 0, 0) != 0:
raise RuntimeError("Cannot set fixture process identity")
window = Gtk.Window(title="Web notification fixture")
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
window.add(box)
toast = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
toast.get_accessible().set_role(Atk.Role.ALERT)
toast.set_no_show_all(True)
username = Gtk.Label(label="Username")
body = Gtk.Label(label="Playing: Example Game")
toast.add(username)
toast.add(body)
box.add(toast)
window.connect("destroy", Gtk.main_quit)
window.show_all()
foreground = Gtk.Window(title="Foreground fixture")
foreground.set_default_size(300, 80)
foreground.move(400, 200)
foreground.add(Gtk.Entry())
foreground.show_all()
pendingInput = ""
def receive_command(source: object, condition: object) -> bool:
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 == "show":
toast.set_no_show_all(False)
toast.show_all()
elif command == "update":
body.set_text("Username is now online")
elif command == "destroy":
toast.destroy()
elif command == "add":
addedToast = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
addedToast.get_accessible().set_role(Atk.Role.ALERT)
addedToast.add(Gtk.Label(label="A new toast"))
box.add(addedToast)
addedToast.show_all()
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(foreground.get_window()), flush=True) or False)
Gtk.main()