-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtray.py
More file actions
252 lines (224 loc) · 8.61 KB
/
Copy pathtray.py
File metadata and controls
252 lines (224 loc) · 8.61 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
"""System tray icon (KStatusNotifierItem via separate Qt process) for LinuxPop.
Spawns a lean Qt process that uses KStatusNotifierItem - native KDE/Wayland
protocol, no XWayland dependency, no GTK thread conflicts.
"""
from __future__ import annotations
import json
import os
import signal
import socket
import struct
import subprocess
import sys
import time
from pathlib import Path
from typing import Callable
from xdg_paths import CACHE_DIR as SOCKET_DIR
TRAY_SCRIPT = str(Path(__file__).resolve().parent / "tray_qt.py")
def _tray_preexec() -> None:
"""Runs in the tray child between fork and exec.
1. PR_SET_PDEATHSIG: ask the kernel to SIGTERM this process the instant
its parent (the main daemon) dies. Without this, killing or crashing
the daemon left the Qt tray subprocess orphaned (reparented to systemd)
and its tray icon lingered forever -- which is how two LinuxPop icons
could end up side by side after a few restarts.
2. setsid(): give the tray its own session so a Ctrl-C in the daemon's
terminal doesn't also tear it down (the original intent of the
start_new_session flag this replaces).
The death-signal is armed FIRST, then we re-check the parent is still
alive, to close the tiny fork-then-parent-exits race before setsid()."""
try:
import ctypes
PR_SET_PDEATHSIG = 1
ctypes.CDLL("libc.so.6", use_errno=True).prctl(
PR_SET_PDEATHSIG, signal.SIGTERM)
# If the parent vanished between fork and now, PDEATHSIG already
# missed its window -- exit rather than become the next orphan.
if os.getppid() == 1:
os._exit(0)
except Exception:
pass
try:
os.setsid()
except OSError:
pass
def _recv_exact(sock: socket.socket, n: int) -> bytes:
buf = b""
while len(buf) < n:
chunk = sock.recv(n - len(buf))
if not chunk:
raise ConnectionError("Socket closed")
buf += chunk
return buf
def _recv_message(sock: socket.socket) -> dict | None:
try:
raw_len = _recv_exact(sock, 4)
msg_len = struct.unpack("!I", raw_len)[0]
if msg_len > 1_000_000:
return None
raw = _recv_exact(sock, msg_len)
return json.loads(raw.decode("utf-8"))
except (ConnectionError, OSError, json.JSONDecodeError):
return None
def _send_message(sock: socket.socket, msg: dict) -> None:
raw = json.dumps(msg).encode("utf-8")
sock.sendall(struct.pack("!I", len(raw)) + raw)
class Tray:
"""Same API as the old GTK/Ayatana Tray, but backed by a Qt KSNI subprocess."""
def __init__(
self,
on_toggle_watcher: Callable[[bool], None],
get_watcher_active: Callable[[], bool],
on_show_popup_now: Callable[[], None],
on_open_settings: Callable[[], None],
on_open_plugins: Callable[[], None],
on_open_about: Callable[[], None],
on_quit: Callable[[], None],
on_open_support: Callable[[], None] | None = None,
) -> None:
self._on_toggle_watcher = on_toggle_watcher
self._get_watcher_active = get_watcher_active
self._on_show_popup_now = on_show_popup_now
self._on_open_settings = on_open_settings
self._on_open_plugins = on_open_plugins
self._on_open_about = on_open_about
self._on_quit = on_quit
self._on_open_support = on_open_support
self._proc: subprocess.Popen | None = None
self._sock: socket.socket | None = None
self._connected = False
if not os.path.isfile(TRAY_SCRIPT):
print("[tray] tray_qt.py not found - tray disabled")
return
self._start_process()
self._connect()
def _start_process(self) -> None:
"""Launch the Qt tray subprocess."""
SOCKET_DIR.mkdir(parents=True, exist_ok=True)
# Remove stale socket
try:
os.unlink(str(SOCKET_DIR / "tray.sock"))
except OSError:
pass
try:
self._proc = subprocess.Popen(
[sys.executable, TRAY_SCRIPT],
stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT,
preexec_fn=_tray_preexec, # die with parent + own session
)
print(f"[tray] spawned Qt tray process (pid={self._proc.pid})")
except OSError as exc:
print(f"[tray] failed to start tray process: {exc}")
self._proc = None
def _connect(self) -> None:
"""Connect to the tray subprocess's Unix socket with retries."""
socket_path = str(SOCKET_DIR / "tray.sock")
for attempt in range(30): # wait up to ~3 seconds
if not os.path.exists(socket_path):
time.sleep(0.1)
continue
try:
self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self._sock.settimeout(3)
self._sock.connect(socket_path)
self._sock.setblocking(True)
self._connected = True
# Sync initial watcher state
self.refresh()
print("[tray] connected to tray subprocess")
return
except (OSError, ConnectionRefusedError):
time.sleep(0.1)
print("[tray] WARNING: could not connect to tray subprocess")
def _reconnect(self) -> bool:
"""Try to reconnect if the socket was lost."""
if self._sock:
try:
self._sock.close()
except OSError:
pass
self._sock = None
self._connected = False
# If the subprocess died, restart it
if self._proc is None or self._proc.poll() is not None:
self._start_process()
self._connect()
return self._connected
def _dispatch(self, event: str, value: object) -> None:
"""Route an event from the tray subprocess to the right callback."""
if event == "toggle_watcher":
self._on_toggle_watcher(bool(value))
elif event == "show_popup":
self._on_show_popup_now()
elif event == "settings":
self._on_open_settings()
elif event == "plugins":
self._on_open_plugins()
elif event == "about":
self._on_open_about()
elif event == "support":
if self._on_open_support:
self._on_open_support()
elif event == "quit":
self._on_quit()
elif event == "pong":
pass # keepalive
def refresh(self) -> None:
"""Update tray state from app - called by main.py."""
if not self._connected or self._sock is None:
self._reconnect()
return
try:
active = self._get_watcher_active()
_send_message(self._sock, {"cmd": "set_watcher_active", "value": active})
except OSError:
self._connected = False
self._reconnect()
def reload_icon(self) -> None:
"""Ask the tray subprocess to re-render its icon - called when the
tray_icon_style setting changes, so it updates live (no restart)."""
if not self._connected or self._sock is None:
return
try:
_send_message(self._sock, {"cmd": "reload_icon"})
except OSError:
self._connected = False
def poll(self) -> None:
"""Read any pending messages from the tray subprocess.
Must be called periodically from the GTK main loop (via GLib.timeout_add).
"""
if not self._connected or self._sock is None:
self._reconnect()
return
try:
self._sock.setblocking(False)
while True:
try:
msg = _recv_message(self._sock)
if msg is None:
break
self._dispatch(msg.get("event", ""), msg.get("value"))
except BlockingIOError:
break
except (OSError, ConnectionError):
self._connected = False
self._reconnect()
def shutdown(self) -> None:
"""Tell tray subprocess to quit and wait for it."""
if self._connected and self._sock:
try:
_send_message(self._sock, {"cmd": "quit"})
except OSError:
pass
if self._sock:
try:
self._sock.close()
except OSError:
pass
if self._proc:
try:
self._proc.wait(timeout=3)
except subprocess.TimeoutExpired:
self._proc.kill()
self._proc = None