diff --git a/wallpaper_changer/python/src/wallpaper_changer/monitor_manager.py b/wallpaper_changer/python/src/wallpaper_changer/monitor_manager.py index 3d0e4dc..5f2d624 100644 --- a/wallpaper_changer/python/src/wallpaper_changer/monitor_manager.py +++ b/wallpaper_changer/python/src/wallpaper_changer/monitor_manager.py @@ -1,3 +1,4 @@ +import os import re import subprocess import xml.etree.ElementTree as ET @@ -58,8 +59,19 @@ class MonitorManager: active_gnome_connectors = { drm_to_gnome(connector.name): connector for connector in connectors_drm } + session_type = os.environ.get("XDG_SESSION_TYPE", "").lower() + desktops = os.environ.get("XDG_CURRENT_DESKTOP", "").lower().split(":") + if session_type == "x11": + read_virtual_size = self._read_virtual_size_x11 + elif session_type == "wayland" and "gnome" in desktops: + read_virtual_size = self._read_virtual_size_gnome + else: + raise RuntimeError( + f"Unsupported graphical session: XDG_SESSION_TYPE={session_type!r}, " + f"XDG_CURRENT_DESKTOP={os.environ.get('XDG_CURRENT_DESKTOP', '')!r}" + ) active_sizes = { - name: self._read_virtual_size_gnome(connector) + name: read_virtual_size(connector) for name, connector in active_gnome_connectors.items() } selected: ScreenLayout | None = None @@ -150,6 +162,33 @@ class MonitorManager: connectors.append(connector) return connectors + @staticmethod + def _read_virtual_size_x11(connector: Path) -> tuple[int, int]: + output = subprocess.check_output(["xrandr", "--query"], text=True) + connector_name = drm_to_gnome(connector.name) + in_connector = False + for line in output.splitlines(): + if line and not line[0].isspace(): + fields = line.split() + if in_connector: + break + in_connector = ( + len(fields) >= 2 + and fields[0] == connector_name + and fields[1] == "connected" + ) + elif in_connector: + mode = re.match(r"\s+(\d+)x(\d+)\s+(.*)", line) + if mode and any("*" in rate for rate in mode.group(3).split()): + width, height = int(mode.group(1)), int(mode.group(2)) + if width <= 0 or height <= 0: + raise ValueError(f"Invalid current display mode for {connector_name}") + logger.debug( + f"Current display mode for connector {connector_name}: {width}x{height}" + ) + return width, height + raise RuntimeError(f"No current X11 display mode found for connector {connector_name}") + @staticmethod def _read_virtual_size_gnome(connector: Path) -> tuple[int, int]: monitors_info = subprocess.check_output(