Hopefully fix ctrl W for desktop apps
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
// on linux the app takes chromium's reserved shortcuts (see shortcuts.ts) from the x server itself, with a passive grab
|
||||
// on its window through libX11. deno desktop's chromium always runs on x11, see linux_launcher.ts. a grabbed key goes
|
||||
// to the grabbing client until it's let go, so the grab is dropped right away and the key's release and everything
|
||||
// after it reaches chromium as usual. holding it repeats the press, which is grabbed again each time
|
||||
import { type Key, KEYS, send_key } from "./shortcuts.ts";
|
||||
|
||||
const SHIFT_MASK = 1 << 0;
|
||||
const LOCK_MASK = 1 << 1;
|
||||
const CONTROL_MASK = 1 << 2;
|
||||
// num lock on most setups. a grab only matches the exact modifiers, so every lock combination gets one
|
||||
const MOD2_MASK = 1 << 4;
|
||||
const KEY_PRESS = 2;
|
||||
const KEY_RELEASE = 3;
|
||||
const GRAB_MODE_ASYNC = 1;
|
||||
const CURRENT_TIME = 0n;
|
||||
// XEvent is a union of 24 longs
|
||||
const EVENT_SIZE = 24 * 8;
|
||||
// XKeyEvent's fields on 64 bit systems
|
||||
const EVENT_STATE = 80;
|
||||
const EVENT_KEYCODE = 84;
|
||||
// how often it looks for grabbed keys, in milliseconds
|
||||
const POLL_MS = 4;
|
||||
|
||||
const X11 = {
|
||||
XOpenDisplay: { parameters: ["pointer"], result: "pointer" },
|
||||
XSetErrorHandler: { parameters: ["function"], result: "pointer" },
|
||||
XKeysymToKeycode: { parameters: ["pointer", "u64"], result: "u8" },
|
||||
XGetInputFocus: { parameters: ["pointer", "buffer", "buffer"], result: "i32" },
|
||||
XQueryTree: { parameters: ["pointer", "u64", "buffer", "buffer", "buffer", "buffer"], result: "i32" },
|
||||
XFree: { parameters: ["pointer"], result: "i32" },
|
||||
XGrabKey: { parameters: ["pointer", "i32", "u32", "u64", "i32", "i32", "i32"], result: "i32" },
|
||||
XUngrabKeyboard: { parameters: ["pointer", "u64"], result: "i32" },
|
||||
XPending: { parameters: ["pointer"], result: "i32" },
|
||||
XNextEvent: { parameters: ["pointer", "buffer"], result: "i32" },
|
||||
XFlush: { parameters: ["pointer"], result: "i32" },
|
||||
XSync: { parameters: ["pointer", "i32"], result: "i32" },
|
||||
} as const;
|
||||
|
||||
// grabs the shortcuts on the window once it has focus, which is when x11 can tell which window is the app's. nothing
|
||||
// happens without libX11
|
||||
export function guard_shortcuts_x11(win: Deno.BrowserWindow) {
|
||||
let lib: Deno.DynamicLibrary<typeof X11>;
|
||||
try {
|
||||
lib = Deno.dlopen("libX11.so.6", X11);
|
||||
} catch (e) {
|
||||
console.error(`Not guarding shortcuts, no libX11: ${(e as Error).message}`);
|
||||
return;
|
||||
}
|
||||
const x = lib.symbols;
|
||||
const display = x.XOpenDisplay(null);
|
||||
if (!display) {
|
||||
console.error("Not guarding shortcuts, no X display");
|
||||
return;
|
||||
}
|
||||
// xlib's own handler exits the process, like when another app already grabbed one of the keys
|
||||
const on_error = new Deno.UnsafeCallback({ parameters: ["pointer", "pointer"], result: "i32" }, () => 0);
|
||||
x.XSetErrorHandler(on_error.pointer);
|
||||
|
||||
const by_keycode = new Map<number, Key>();
|
||||
for (const key of KEYS) {
|
||||
const keycode = x.XKeysymToKeycode(display, BigInt(key.keysym));
|
||||
if (keycode) by_keycode.set(keycode, key);
|
||||
}
|
||||
|
||||
let grabbed = false;
|
||||
win.addEventListener("focus", () => {
|
||||
if (grabbed) return;
|
||||
const window = top_level(x, display);
|
||||
if (!window) return;
|
||||
for (const keycode of by_keycode.keys()) {
|
||||
for (const shift of [0, SHIFT_MASK]) {
|
||||
for (const locks of [0, LOCK_MASK, MOD2_MASK, LOCK_MASK | MOD2_MASK]) {
|
||||
const modifiers = CONTROL_MASK | shift | locks;
|
||||
x.XGrabKey(display, keycode, modifiers, window, 0, GRAB_MODE_ASYNC, GRAB_MODE_ASYNC);
|
||||
}
|
||||
}
|
||||
}
|
||||
x.XSync(display, 0);
|
||||
grabbed = true;
|
||||
});
|
||||
|
||||
const event = new Uint8Array(EVENT_SIZE);
|
||||
const view = new DataView(event.buffer);
|
||||
setInterval(() => {
|
||||
while (x.XPending(display) > 0) {
|
||||
x.XNextEvent(display, event);
|
||||
const type = view.getInt32(0, true);
|
||||
if (type !== KEY_PRESS && type !== KEY_RELEASE) continue;
|
||||
if (type === KEY_PRESS) {
|
||||
x.XUngrabKeyboard(display, CURRENT_TIME);
|
||||
x.XFlush(display);
|
||||
}
|
||||
const key = by_keycode.get(view.getUint32(EVENT_KEYCODE, true));
|
||||
const state = view.getUint32(EVENT_STATE, true);
|
||||
if (key) {
|
||||
const ctrl = (state & CONTROL_MASK) !== 0;
|
||||
send_key(win, type === KEY_PRESS ? "keydown" : "keyup", key, ctrl, (state & SHIFT_MASK) !== 0);
|
||||
}
|
||||
}
|
||||
}, POLL_MS);
|
||||
}
|
||||
|
||||
// the window that has focus, up to the one right under the root, which is the app's own window and not one of
|
||||
// chromium's inside it
|
||||
function top_level(x: Deno.DynamicLibrary<typeof X11>["symbols"], display: Deno.PointerObject): bigint | undefined {
|
||||
const focus = new BigUint64Array(1);
|
||||
x.XGetInputFocus(display, focus, new Int32Array(1));
|
||||
let window = focus[0];
|
||||
// None and PointerRoot
|
||||
if (window <= 1n) return undefined;
|
||||
const root = new BigUint64Array(1);
|
||||
const parent = new BigUint64Array(1);
|
||||
const children = new BigUint64Array(1);
|
||||
const count = new Uint32Array(1);
|
||||
while (true) {
|
||||
if (!x.XQueryTree(display, window, root, parent, children, count)) return undefined;
|
||||
if (children[0]) x.XFree(Deno.UnsafePointer.create(children[0]));
|
||||
if (parent[0] === root[0]) return window;
|
||||
window = parent[0];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user