1// @effect-diagnostics globalTimers:off -- A plain Node child; the poll runs outside any Effect fiber.
2// Windows modifier-pair listener. Runs in a forked Node-mode child so a stuck or
3// crashed FFI call cannot take the main process with it. Mirrors the macOS
4// poller: sample both physical keys at 20 Hz, fire on the rising edge.
5import { SNAP_SHOT_MODIFIERS, type SnapShotModifier } from "@t3tools/contracts";
6
7import { loadWindowsForegroundApi } from "../electron/WindowsForeground.ts";
8import { WINDOWS_MODIFIER_PAIR_VIRTUAL_KEYS } from "./snapShot.ts";
9
10const POLL_INTERVAL_MS = 50;
11
12const requested = process.argv[2];
13if (!(SNAP_SHOT_MODIFIERS as readonly string[]).includes(requested ?? "")) {
14 process.exit(1);
15}
16const [left, right] = WINDOWS_MODIFIER_PAIR_VIRTUAL_KEYS[requested as SnapShotModifier];
17
18async function poll() {
19 const api = await loadWindowsForegroundApi();
20 let active = false;
21 process.send?.("ready");
22 const timer = setInterval(() => {
23 const pressed = api.isKeyDown(left) && api.isKeyDown(right);
24 if (pressed && !active) {
25 try {
26 process.send?.("trigger");
27 } catch {}
28 }
29 active = pressed;
30 }, POLL_INTERVAL_MS);
31 const shutdown = () => {
32 clearInterval(timer);
33 process.exit(0);
34 };
35 process.once("disconnect", shutdown);
36 process.once("SIGTERM", shutdown);
37}
38
39void poll().catch(() => process.exit(1));