@sirenodeck/sirenodeck 0.2.4 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,158 @@
1
+ import { n as hasWaylandGnomeSession, r as shouldUseWaylandGnomeProvider, t as createWaylandGnomeProvider } from "./main.mjs";
2
+ //#region src/system/providers/active-app/linux.ts
3
+ const DEFAULT_POLL_MS = 1e3;
4
+ const parseDbusEvalResult = (raw) => {
5
+ if (typeof raw !== "string" || raw.length === 0) return null;
6
+ try {
7
+ const parsed = JSON.parse(raw);
8
+ const wmClass = typeof parsed.wm_class === "string" ? parsed.wm_class : "";
9
+ const title = typeof parsed.title === "string" ? parsed.title : "";
10
+ const pid = typeof parsed.pid === "number" && Number.isFinite(parsed.pid) ? parsed.pid : null;
11
+ if (wmClass.length === 0 && title.length === 0) return null;
12
+ return {
13
+ wmClass,
14
+ title,
15
+ pid
16
+ };
17
+ } catch {
18
+ return null;
19
+ }
20
+ };
21
+ const readProcName = async (executor) => {
22
+ const name = (await executor.run("sh", ["-c", "cat /proc/$$/comm 2>/dev/null || true"])).stdout.trim();
23
+ return name.length > 0 ? name : null;
24
+ };
25
+ const readForegroundProc = async (executor) => {
26
+ const winId = (await executor.run("sh", ["-c", "xdotool getactivewindow 2>/dev/null || xprop -root _NET_ACTIVE_WINDOW 2>/dev/null | awk '{print $5}' || true"])).stdout.trim();
27
+ if (winId.length === 0) {
28
+ const name = await readProcName(executor);
29
+ return name === null ? null : {
30
+ name,
31
+ pid: null
32
+ };
33
+ }
34
+ const pidResult = await executor.run("sh", ["-c", `xdotool getwindowpid ${winId} 2>/dev/null || true`]);
35
+ const pid = Number.parseInt(pidResult.stdout.trim(), 10);
36
+ if (!Number.isFinite(pid)) {
37
+ const name = await readProcName(executor);
38
+ return name === null ? null : {
39
+ name,
40
+ pid: null
41
+ };
42
+ }
43
+ const name = (await executor.run("sh", ["-c", `cat /proc/${pid}/comm 2>/dev/null || true`])).stdout.trim();
44
+ if (name.length === 0) return {
45
+ name: `pid:${pid}`,
46
+ pid
47
+ };
48
+ return {
49
+ name,
50
+ pid
51
+ };
52
+ };
53
+ const D_BUS_GNOME_SHELL = "org.gnome.Shell";
54
+ const D_BUS_GNOME_SHELL_PATH = "/org/gnome/Shell";
55
+ const dbusSnapshot = async (bus) => {
56
+ const iface = (await bus.getProxyObject(D_BUS_GNOME_SHELL, D_BUS_GNOME_SHELL_PATH)).getInterface("org.gnome.Shell");
57
+ if (iface.Eval === void 0) return null;
58
+ const result = await iface.Eval("global.display.focus_window && JSON.stringify({wm_class: global.display.focus_window.wm_class, title: global.display.focus_window.title, pid: global.display.focus_window.pid})");
59
+ return parseDbusEvalResult(result) === null ? null : (() => {
60
+ const parsed = parseDbusEvalResult(result);
61
+ return {
62
+ name: parsed.wmClass.length > 0 ? parsed.wmClass : parsed.title,
63
+ windowTitle: parsed.title.length > 0 ? parsed.title : null,
64
+ processId: parsed.pid
65
+ };
66
+ })();
67
+ };
68
+ const fallbackSnapshot = async (executor) => {
69
+ const fg = await readForegroundProc(executor);
70
+ if (fg === null) return null;
71
+ return {
72
+ name: fg.name,
73
+ windowTitle: null,
74
+ processId: fg.pid
75
+ };
76
+ };
77
+ const sameSnapshot = (a, b) => {
78
+ if (a === null && b === null) return true;
79
+ if (a === null || b === null) return false;
80
+ return a.name === b.name && a.windowTitle === b.windowTitle && a.processId === b.processId;
81
+ };
82
+ const createLinuxActiveAppProvider = async (deps) => {
83
+ if (shouldUseWaylandGnomeProvider(deps.env) || await hasWaylandGnomeSession({
84
+ env: deps.env,
85
+ executor: deps.executor
86
+ })) {
87
+ deps.logger.info("active-app: detected GNOME Wayland session, using 'Window Calls Extended' provider");
88
+ try {
89
+ return await createWaylandGnomeProvider({
90
+ ...deps.dbus !== void 0 ? { dbus: deps.dbus } : {},
91
+ logger: deps.logger,
92
+ ...deps.pollIntervalMs !== void 0 ? { pollIntervalMs: deps.pollIntervalMs } : {}
93
+ });
94
+ } catch (err) {
95
+ deps.logger.warn({ err }, "active-app: GNOME Wayland provider init failed, using null provider");
96
+ }
97
+ }
98
+ const pollMs = deps.pollIntervalMs ?? DEFAULT_POLL_MS;
99
+ const subscribers = /* @__PURE__ */ new Set();
100
+ let last = null;
101
+ let stopped = false;
102
+ const bus = deps.dbus ?? null;
103
+ let dbusFailed = false;
104
+ const snapshot = async () => {
105
+ if (bus && !dbusFailed) try {
106
+ const snap = await dbusSnapshot(bus);
107
+ if (snap !== null) return snap;
108
+ dbusFailed = true;
109
+ } catch (err) {
110
+ deps.logger.debug({ err }, "active-app: D-Bus snapshot failed, falling back to /proc");
111
+ dbusFailed = true;
112
+ }
113
+ try {
114
+ return await fallbackSnapshot(deps.executor);
115
+ } catch (err) {
116
+ deps.logger.warn({ err }, "active-app: /proc fallback failed");
117
+ return last;
118
+ }
119
+ };
120
+ let interval = null;
121
+ const start = () => {
122
+ if (interval !== null) return;
123
+ interval = setInterval(() => {
124
+ if (stopped) return;
125
+ snapshot().then((s) => {
126
+ if (stopped) return;
127
+ if (!sameSnapshot(last, s)) {
128
+ last = s;
129
+ for (const handler of subscribers) handler(s);
130
+ }
131
+ });
132
+ }, pollMs);
133
+ };
134
+ start();
135
+ return {
136
+ async getActive() {
137
+ return snapshot();
138
+ },
139
+ subscribe(handler) {
140
+ subscribers.add(handler);
141
+ return () => {
142
+ subscribers.delete(handler);
143
+ };
144
+ },
145
+ async stop() {
146
+ stopped = true;
147
+ if (interval !== null) {
148
+ clearInterval(interval);
149
+ interval = null;
150
+ }
151
+ try {
152
+ bus?.disconnect?.();
153
+ } catch {}
154
+ }
155
+ };
156
+ };
157
+ //#endregion
158
+ export { createLinuxActiveAppProvider };
@@ -0,0 +1,119 @@
1
+ import { i as createNullSessionProvider } from "./main.mjs";
2
+ import { sessionBus } from "dbus-next";
3
+ //#region src/system/providers/session/linux.ts
4
+ const SCREENSAVER_SERVICE = "org.gnome.ScreenSaver";
5
+ const SCREENSAVER_PATH = "/org/gnome/ScreenSaver";
6
+ const SCREENSAVER_IFACE = "org.gnome.ScreenSaver";
7
+ const IDLE_MONITOR_SERVICE = "org.gnome.Mutter.IdleMonitor";
8
+ const IDLE_MONITOR_PATH = "/org/gnome/Mutter/IdleMonitor/Core";
9
+ const IDLE_MONITOR_IFACE = "org.gnome.Mutter.IdleMonitor";
10
+ const toState = (locked) => locked ? "locked" : "unlocked";
11
+ const createLinuxSessionProvider = async (deps) => {
12
+ const idleMs = deps.idleMs ?? 3e5;
13
+ const listeners = /* @__PURE__ */ new Set();
14
+ let state = "unknown";
15
+ let stopped = false;
16
+ let idleSupported = false;
17
+ let bus = deps.dbus ?? null;
18
+ if (bus === null) try {
19
+ bus = await sessionBus();
20
+ } catch (err) {
21
+ deps.logger.debug({ err }, "session: dbus sessionBus unavailable");
22
+ return createNullSessionProvider(deps.logger);
23
+ }
24
+ let screensaverOk = false;
25
+ try {
26
+ const saver = (await bus.getProxyObject(SCREENSAVER_SERVICE, SCREENSAVER_PATH)).getInterface(SCREENSAVER_IFACE);
27
+ const initial = await saver.GetActive?.() ?? false;
28
+ state = toState(initial);
29
+ saver.on?.("ActiveChanged", (locked) => {
30
+ if (typeof locked === "boolean") {
31
+ const next = toState(locked);
32
+ if (next !== state) {
33
+ state = next;
34
+ for (const l of listeners) l(state);
35
+ }
36
+ }
37
+ });
38
+ screensaverOk = true;
39
+ } catch (err) {
40
+ deps.logger.debug({ err }, "session: ScreenSaver init failed");
41
+ }
42
+ try {
43
+ await (await bus.getProxyObject(IDLE_MONITOR_SERVICE, IDLE_MONITOR_PATH)).getInterface(IDLE_MONITOR_IFACE).GetIdletime?.();
44
+ idleSupported = true;
45
+ } catch (err) {
46
+ deps.logger.debug({ err }, "session: IdleMonitor init failed; idle polling disabled");
47
+ }
48
+ if (!screensaverOk && !idleSupported) {
49
+ deps.logger.debug("session: no lock source available, returning null");
50
+ return createNullSessionProvider(deps.logger);
51
+ }
52
+ let interval = null;
53
+ if (idleSupported) {
54
+ let cachedIdleIface = null;
55
+ const getIdleIface = async () => {
56
+ if (cachedIdleIface !== null) return cachedIdleIface;
57
+ cachedIdleIface = (await bus.getProxyObject(IDLE_MONITOR_SERVICE, IDLE_MONITOR_PATH)).getInterface(IDLE_MONITOR_IFACE);
58
+ return cachedIdleIface;
59
+ };
60
+ interval = setInterval(() => {
61
+ if (stopped) return;
62
+ getIdleIface().then((iface) => iface?.GetIdletime?.()).then((idleMsRaw) => {
63
+ deps.logger.debug({
64
+ idleMsRaw,
65
+ idleMs,
66
+ currentState: state
67
+ }, "session: idle monitor tick");
68
+ if (typeof idleMsRaw === "number") {
69
+ if (idleMsRaw > idleMs && state === "unlocked") {
70
+ state = "locked";
71
+ deps.logger.info({
72
+ idleMsRaw,
73
+ idleMs
74
+ }, "session: locked via idle monitor");
75
+ for (const l of listeners) l(state);
76
+ } else if (idleMsRaw <= idleMs && state === "locked") {
77
+ state = "unlocked";
78
+ deps.logger.info({
79
+ idleMsRaw,
80
+ idleMs
81
+ }, "session: unlocked via idle monitor");
82
+ for (const l of listeners) l(state);
83
+ }
84
+ }
85
+ }).catch(() => void 0);
86
+ }, 5e3);
87
+ }
88
+ return {
89
+ getState() {
90
+ return state;
91
+ },
92
+ subscribe(handler) {
93
+ let initialFired = false;
94
+ const wrappedHandler = (s) => {
95
+ initialFired = true;
96
+ handler(s);
97
+ };
98
+ listeners.add(wrappedHandler);
99
+ if (!initialFired && state !== "unknown") queueMicrotask(() => {
100
+ if (!initialFired) handler(state);
101
+ });
102
+ return () => {
103
+ listeners.delete(wrappedHandler);
104
+ };
105
+ },
106
+ async stop() {
107
+ stopped = true;
108
+ if (interval !== null) {
109
+ clearInterval(interval);
110
+ interval = null;
111
+ }
112
+ try {
113
+ bus?.disconnect?.();
114
+ } catch {}
115
+ }
116
+ };
117
+ };
118
+ //#endregion
119
+ export { createLinuxSessionProvider };
@@ -0,0 +1,51 @@
1
+ import { a as withTimeout } from "./main.mjs";
2
+ //#region src/system/providers/notification/linux.ts
3
+ const NOTIFY_SEND_TOOL = "notify-send";
4
+ const FFPLAY_TOOL = "ffplay";
5
+ const PAPLAY_TOOL = "paplay";
6
+ const DEFAULT_TIMEOUT_MS = 2e3;
7
+ const SOUND_TIMEOUT_MS = 5e3;
8
+ const probeTool = async (executor, tool, extraFsProbe) => {
9
+ const result = await executor.run("which", [tool]);
10
+ if (result.exitCode === 0 && result.stdout.trim().length > 0) return true;
11
+ return extraFsProbe?.(tool) === true;
12
+ };
13
+ const shellQuote = (value) => `'${value.replace(/'/g, "'\\''")}'`;
14
+ const createLinuxNotificationProvider = async (deps) => {
15
+ if (!await probeTool(deps.executor, NOTIFY_SEND_TOOL, deps.extraFsProbe)) {
16
+ deps.logger.warn({}, "linux notification: notify-send not found; notify() will be a no-op");
17
+ return { async notify() {} };
18
+ }
19
+ const ffplayOk = await probeTool(deps.executor, FFPLAY_TOOL, deps.extraFsProbe);
20
+ const paplayOk = !ffplayOk && await probeTool(deps.executor, PAPLAY_TOOL, deps.extraFsProbe);
21
+ const soundTool = ffplayOk ? FFPLAY_TOOL : paplayOk ? PAPLAY_TOOL : null;
22
+ const timeoutMs = deps.timeoutMs ?? DEFAULT_TIMEOUT_MS;
23
+ return { async notify(args) {
24
+ const sendArgs = [
25
+ "-t",
26
+ "5000",
27
+ shellQuote(args.title),
28
+ shellQuote(args.body)
29
+ ];
30
+ try {
31
+ const result = await withTimeout(deps.executor.run(NOTIFY_SEND_TOOL, sendArgs), timeoutMs);
32
+ if (result.exitCode !== 0) deps.logger.warn({ stderr: result.stderr.trim() }, "linux notification: notify-send returned non-zero");
33
+ } catch (err) {
34
+ deps.logger.warn({ err }, "linux notification: notify-send failed");
35
+ }
36
+ if (args.sound === true && deps.soundPath !== void 0 && soundTool !== null) try {
37
+ await withTimeout(deps.executor.run(soundTool, [
38
+ "-nodisp",
39
+ "-autoexit",
40
+ deps.soundPath
41
+ ]), SOUND_TIMEOUT_MS);
42
+ } catch (err) {
43
+ deps.logger.warn({
44
+ err,
45
+ tool: soundTool
46
+ }, "linux notification: sound playback failed");
47
+ }
48
+ } };
49
+ };
50
+ //#endregion
51
+ export { createLinuxNotificationProvider };