qdesk 1.0.3 → 1.0.5

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.
@@ -1,4 +1,4 @@
1
- import { CallNextHookEx, decodeKeyboardHookLParam, DispatchMessageW, GetAsyncKeyState, GetMessageW, GetModuleHandleW, registerKeyboardHookCallback, SetWindowsHookExW, TranslateMessage, UnhookWindowsHookEx, unregisterCallback, } from "./koffi-utils.js";
1
+ import { CallNextHookEx, decodeKeyboardHookLParam, DispatchMessageW, GetAsyncKeyState, GetModuleHandleW, PeekMessageW, registerKeyboardHookCallback, SetWindowsHookExW, TranslateMessage, UnhookWindowsHookEx, unregisterCallback, } from "./koffi-utils.js";
2
2
  const process = globalThis.process;
3
3
  const MOD_ALT = 0x0001;
4
4
  const MOD_CONTROL = 0x0002;
@@ -14,6 +14,7 @@ const HC_ACTION = 0;
14
14
  const WH_KEYBOARD_LL = 13;
15
15
  const WM_KEYDOWN = 0x0100;
16
16
  const WM_SYSKEYDOWN = 0x0104;
17
+ const PM_REMOVE = 0x0001;
17
18
  export function normalizeChord(chord) {
18
19
  const parts = chord
19
20
  .split("+")
@@ -155,6 +156,8 @@ export function startListening(options) {
155
156
  }
156
157
  let keyboardHook = null;
157
158
  let keyboardHookProcPtr = null;
159
+ let messagePump;
160
+ let isShuttingDown = false;
158
161
  const callback = (nCode, wParam, lParam) => {
159
162
  const passThrough = () => BigInt(Number(CallNextHookEx(keyboardHook, nCode, wParam, lParam)));
160
163
  try {
@@ -206,7 +209,15 @@ export function startListening(options) {
206
209
  process.exit(1);
207
210
  }
208
211
  function shutdown() {
212
+ if (isShuttingDown) {
213
+ return;
214
+ }
215
+ isShuttingDown = true;
209
216
  console.log("\n[qdesk] Shutting down...");
217
+ if (messagePump) {
218
+ clearInterval(messagePump);
219
+ messagePump = undefined;
220
+ }
210
221
  if (keyboardHook) {
211
222
  UnhookWindowsHookEx(keyboardHook);
212
223
  keyboardHook = null;
@@ -222,18 +233,12 @@ export function startListening(options) {
222
233
  return {
223
234
  runMessageLoop: () => {
224
235
  const msg = {};
225
- while (true) {
226
- const ret = Number(GetMessageW(msg, null, 0, 0));
227
- if (ret === 0) {
228
- break;
236
+ messagePump = setInterval(() => {
237
+ while (Number(PeekMessageW(msg, null, 0, 0, PM_REMOVE)) !== 0) {
238
+ TranslateMessage(msg);
239
+ DispatchMessageW(msg);
229
240
  }
230
- if (ret === -1) {
231
- console.error("[error] GetMessageW returned -1");
232
- break;
233
- }
234
- TranslateMessage(msg);
235
- DispatchMessageW(msg);
236
- }
241
+ }, 8);
237
242
  },
238
243
  };
239
244
  }
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env node
2
+ import { readFileSync } from "node:fs";
2
3
  import process from "node:process";
3
4
  import { normalizeChord, startListening } from "./chord-utils.js";
4
5
  import { setScreenHue } from "./screen-hue.js";
@@ -6,6 +7,23 @@ import { activateWindowByHandle, getActiveWindowHandleAndName, } from "./window-
6
7
  // Configure this chord at the top of file.
7
8
  const DEFAULT_ADD_CHORD = "win+ctrl+a";
8
9
  const DEFAULT_DROP_CHORD = "win+ctrl+d";
10
+ function hasCliFlag(flag) {
11
+ return process.argv.slice(2).includes(flag);
12
+ }
13
+ function getPackageVersion() {
14
+ try {
15
+ const packageJson = readFileSync(new URL("../package.json", import.meta.url), "utf8");
16
+ const parsed = JSON.parse(packageJson);
17
+ return parsed.version ?? "unknown";
18
+ }
19
+ catch {
20
+ return "unknown";
21
+ }
22
+ }
23
+ if (hasCliFlag("--version") || hasCliFlag("-v")) {
24
+ console.log(getPackageVersion());
25
+ process.exit(0);
26
+ }
9
27
  function normalizeChordInput(chord) {
10
28
  return normalizeChord(chord.trim().toLowerCase());
11
29
  }
@@ -53,6 +53,13 @@ export const GetMessageW = user32.func("GetMessageW", "int", [
53
53
  UINT,
54
54
  UINT,
55
55
  ]);
56
+ export const PeekMessageW = user32.func("PeekMessageW", "int", [
57
+ koffi.out(koffi.pointer(MSG)),
58
+ HWND,
59
+ UINT,
60
+ UINT,
61
+ UINT,
62
+ ]);
56
63
  export const TranslateMessage = user32.func("TranslateMessage", "int", [
57
64
  koffi.pointer(MSG),
58
65
  ]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qdesk",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",