aboutsummaryrefslogtreecommitdiffstats
path: root/modules/desktop/eww/activate-windows.py
blob: 5773d64c2d3bee1215eaded265d460b5967d363f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python3
"""active-windows: emit icons for windows on the currently active
niri workspace, driven by niri's event-stream instead of polling."""
import json
import subprocess
import gi
from gi.repository import Gio, Gtk

gi.require_version("Gtk", "3.0")

icon_theme = Gtk.IconTheme.get_default()
_icon_cache: dict[str, str] = {}


def resolve_icon_path(app_id: str) -> str:
    if not app_id:
        return ""
    if app_id in _icon_cache:
        return _icon_cache[app_id]

    app_info = Gio.DesktopAppInfo.new(f"{app_id}.desktop") \
        or Gio.DesktopAppInfo.new(f"{app_id.lower()}.desktop")

    # fall back: scan installed .desktop files for a matching StartupWMClass
    if app_info is None:
        for info in Gio.AppInfo.get_all():
            if isinstance(info, Gio.DesktopAppInfo):
                wm_class = info.get_startup_wm_class()
                if wm_class and wm_class.lower() == app_id.lower():
                    app_info = info
                    break

    icon_path = ""
    gicon = app_info.get_icon() if app_info else None
    if gicon is not None:
        lookup = icon_theme.lookup_by_gicon(gicon,
                                            32,
                                            Gtk.IconLookupFlags.FORCE_SIZE)
        if lookup:
            icon_path = lookup.get_filename() or ""

    if not icon_path:
        # last resort: treat app_id itself as an icon-theme name
        lookup = icon_theme.lookup_icon(app_id,
                                        32,
                                        Gtk.IconLookupFlags.FORCE_SIZE)
        if lookup:
            icon_path = lookup.get_filename() or ""

    _icon_cache[app_id] = icon_path
    return icon_path


def niri_json(*args):
    out = subprocess.run(["niri", "msg", "-j", *args],
                         capture_output=True,
                         text=True,
                         check=True)
    return json.loads(out.stdout)


def emit_active_windows():
    workspaces = niri_json("workspaces")
    windows = niri_json("windows")

    active_ws = next((w for w in workspaces if w.get("is_active")), None)
    active_id = active_ws["id"] if active_ws else None

    on_workspace = [w for w in windows if w.get("workspace_id") == active_id]

    def sort_key(w):
        pos = (w.get("layout") or {}).get("pos_in_scrolling_layout")
        if pos is not None:
            return (0, pos[0], pos[1])
        # floating / positionless windows: push to the end, stable by id
        return (1, w["id"], 0)

    on_workspace.sort(key=sort_key)

    result = [
        {
            "id": w["id"],
            "title": w.get("title", ""),
            "app_id": w.get("app_id") or "",
            "is_focused": w.get("is_focused", False),
            "icon": resolve_icon_path(w.get("app_id") or ""),
        }
        for w in on_workspace
    ]
    print(json.dumps(result), flush=True)


RELEVANT_EVENTS = {
    "WindowOpenedOrChanged", "WindowClosed", "WindowFocusChanged",
    "WorkspaceActivated", "WorkspacesChanged", "WindowsChanged",
}


def main():
    emit_active_windows()
    proc = subprocess.Popen(
        ["niri", "msg", "-j", "event-stream"],
        stdout=subprocess.PIPE, text=True, bufsize=1,
    )
    for line in proc.stdout:
        try:
            key = next(iter(json.loads(line)))
        except (json.JSONDecodeError, StopIteration):
            continue
        if key in RELEVANT_EVENTS:
            try:
                emit_active_windows()
            except subprocess.CalledProcessError:
                pass


if __name__ == "__main__":
    main()