Hello, I am looking for a way to detect whether the display is using Wayland or X11 via PyGObject.
It seems GDK is providing macros to manage this in C…but this project is pure Python. I have a few different ideas but they seem a bit fragile since they involve environment variables or inspecting class names. Thank you.
When I try this with Gtk3, though, I get an error:
>>> import gi
>>> gi.require_version("Gdk", "3.0")
>>> gi.require_version("GdkWayland", "3.0")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/russell/.pyenv/versions/briefcase-3.10/lib/python3.10/site-packages/gi/__init__.py", line 125, in require_version
raise ValueError('Namespace %s not available for version %s' %
ValueError: Namespace GdkWayland not available for version 3.0
Maybe the inverse is a safe alternative:
>>> from gi.repository import Gdk, GdkX11
>>>
>>> is_wayland = not isinstance(Gdk.Display.get_default(), GdkX11.X11Display)
>>> is_wayland
False
Clarifications about my understanding are definitely welcome.
Ah, right: there’s no introspection data for GDK’s Wayland backend in GTK3. You’ll have to check the type name, for instance:
import gi
gi.require_versions({'Gdk': '3.0'})
from gi.repository import GObject, Gdk
Gdk.init([])
d = Gdk.Display.get_default()
print(GObject.type_name(d.__gtype__))
will print out GdkWaylandDisplay if the display connection is using Wayland, and GdkX11Display if the display connection is using X11.
It is; on Linux/Unix, GTK can only either use X11 or Wayland, so if the display instance is not GdkX11Display then GTK is using Wayland.