Something went wrong. Try again.
Collection of macros
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899"""Internal helpers for Linux Wayland integration.
Everything here uses standard Wayland protocols, so it works across compositors(niri, wlroots-based ones like Sway/Hyprland, and any compositor that exposes``wl_output``). KDE Plasma and GNOME do not implement ``zwlr_screencopy`` andare detected separately so screen capture can use their own tools.
is_wayland() -> True when running under a Wayland session is_kde() -> True under KDE Plasma is_gnome() -> True under GNOME wayland_output_size() -> (width, height) of the primary output (wl_output)"""
import os
def is_wayland(): """Best-effort check for a Wayland session.""" if os.environ.get("WAYLAND_DISPLAY"): return True return os.environ.get("XDG_SESSION_TYPE") == "wayland"
def _desktop_names(): """The desktop/session identifiers advertised by the environment.""" return " ".join( [ os.environ.get("XDG_CURRENT_DESKTOP", ""), os.environ.get("XDG_SESSION_DESKTOP", ""), os.environ.get("DESKTOP_SESSION", ""), ] ).lower()
def is_kde(): """True under KDE Plasma (which has no ``wlr-screencopy``).""" return "kde" in _desktop_names() or os.environ.get("KDE_FULL_SESSION") == "true"
def is_gnome(): """True under GNOME (which has no ``wlr-screencopy``).""" return "gnome" in _desktop_names() or bool( os.environ.get("GNOME_DESKTOP_SESSION_ID") )
def wayland_output_size(): """Return (width, height) of the primary output via the ``wl_output`` protocol.
Falls back to ``None`` when pywayland is unavailable or the compositor can't be reached (e.g. not running inside a session). """ try: from pywayland.client import Display from pywayland.protocol.wayland import WlOutput except Exception: return None
display = Display() try: display.connect() except Exception: return None
registry = display.get_registry() outputs = []
def on_global(reg, name, interface, version): if interface != "wl_output": return out = {"modes": [], "scale": 1} proxy = reg.bind(name, WlOutput, version) proxy.user_data = out proxy.dispatcher["mode"] = ( lambda p, flags, w, h, r: p.user_data["modes"].append((w, h)) ) proxy.dispatcher["scale"] = ( lambda p, factor: p.user_data.__setitem__("scale", factor) ) outputs.append(out)
registry.dispatcher["global"] = on_global try: display.roundtrip() display.roundtrip() # flush the wl_output state after binding except Exception: return None finally: try: display.disconnect() except Exception: pass
if outputs and outputs[0]["modes"]: w, h = outputs[0]["modes"][0] scale = outputs[0]["scale"] or 1 return int(w // scale), int(h // scale) return None