From 4e76b411f1a50e4a3247ce02895993b5d044229d Mon Sep 17 00:00:00 2001 From: Franco Carabajal Date: Fri, 24 Apr 2026 12:10:42 -0300 Subject: [PATCH] feat: add hyprland support and leave space for other compositors The main thing is not using xdotool in a wayland environment. That's what the script change does. Then for function `get_active_pid()` used XDG variables to get the session type and desktop. First if session is wayland and then if desktop is hyprland. I made a match-case so others can include their own desktops. I have only hyprland and don't want to make cases that I'm not able to test myself. --- overlay_design.py | 30 ++++++++++++++++++++++++++++++ start_overlay.sh | 9 ++++++--- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/overlay_design.py b/overlay_design.py index c1962cc..57198a4 100644 --- a/overlay_design.py +++ b/overlay_design.py @@ -78,6 +78,36 @@ def get_active_pid(): global last_pid_error global last_pid_error_time + session_type = os.environ.get("XDG_SESSION_TYPE", "") + current_desktop = os.environ.get("XDG_CURRENT_DESKTOP", "").lower() + + # Wayland compositors + if session_type == "wayland": + match current_desktop: + case desktop if "hyprland" in desktop or os.environ.get("HYPRLAND_INSTANCE_SIGNATURE"): + try: + result = subprocess.check_output( + "hyprctl activewindow | grep -oP 'pid: \\K\\d+'", + shell=True, + stderr=subprocess.DEVNULL, + ) + return int(result.strip()) + except (subprocess.CalledProcessError, ValueError, FileNotFoundError) as exc: + now = time.time() + error_key = ("hyprctl", str(exc)) + if last_pid_error != error_key or now - last_pid_error_time > 5: + safe_log_error(f"could not get active window from hyprctl: {exc}") + last_pid_error = error_key + last_pid_error_time = now + return None + + case _: + if last_pid_error != "wayland_unsupported": + safe_log_error(f"Wayland compositor '{current_desktop}' not supported yet. Currently supported: Hyprland") + last_pid_error = "wayland_unsupported" + return None + + # X11 fallback try: result = subprocess.check_output( ["xdotool", "getwindowfocus", "getwindowpid"], diff --git a/start_overlay.sh b/start_overlay.sh index 9ae3c7b..bde2029 100755 --- a/start_overlay.sh +++ b/start_overlay.sh @@ -9,9 +9,12 @@ if [[ -x "$ROOT_DIR/venv/bin/python" ]]; then PYTHON_BIN="$ROOT_DIR/venv/bin/python" fi -if ! command -v xdotool >/dev/null 2>&1; then - echo "xdotool is required. Install it first and then run this script again." >&2 - exit 1 +# Only require xdotool on X11 (not on Wayland) +if [[ "${XDG_SESSION_TYPE:-}" != "wayland" ]]; then + if ! command -v xdotool >/dev/null 2>&1; then + echo "xdotool is required for X11. Install it first and then run this script again." >&2 + exit 1 + fi fi exec "$PYTHON_BIN" "$ROOT_DIR/overlay_design.py"