wl4pgi_example.py
Python script, ASCII text executable
1from pywayland.client import Display, EventQueue 2import wl4pgi 3 4import gi 5from gi.repository import GLib, Gio 6 7# The display is NOT converted to GObject since they have a lot of special behaviour. Also 8# you still have to roundtrip, but only after creating GObject adapters. 9display = Display() 10display.connect() 11event_queue = EventQueue(display) 12 13 14def on_geometry(output, x, y, pw, ph, subpixel, make, model, transform): 15print(output, x, y, pw, ph, subpixel, make, model, transform) 16 17 18def on_geometry_1(output, x, y, pw, ph, subpixel, make, model, transform): 19print("Since it uses GObject signals, multiple handlers work without problems") 20print(f"Also, custom attributes work: {output.test}. There's always zero or one GObjects " 21f"per Wayland object.") 22 23 24def on_global(registry, name, interface, version): 25if interface == "wl_output": 26# GObjects adapters only represent a proxy. Thus, to get the interface class for use 27# in this signal, you must go into pywayland. 28output = registry.bind(name, wl4pgi.wayland.WlOutput.interface_type, version) 29output.test = 0 30# Multiple handlers, transparently and with a single display and a single registry. 31output.connect("geometry", on_geometry) 32output.connect("geometry", on_geometry_1) 33display.roundtrip() 34 35 36# Starting from the registry, operation is within PyGObject. 37registry = wl4pgi.wayland.WlRegistry(display.get_registry()) 38registry.connect("global", on_global) 39display.roundtrip() 40 41# The next parts would be removed for GTK integration. 42def on_display_event(self, source, condition): 43if condition & GLib.IO_IN: 44self.display.dispatch(queue=self.event_queue) 45return True 46 47 48fd = display.get_fd() 49GLib.io_add_watch(fd, GLib.IO_IN, on_display_event) 50 51main_loop = GLib.MainLoop() 52main_loop.run() 53