118 lines
4.1 KiB
TypeScript
118 lines
4.1 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// when the game lets go of the mouse to open a gui, chromium puts the cursor back where it was grabbed. like minecraft
|
|
// it goes to the middle of the window instead, moved there by the os: libX11 on linux, user32 on windows. the page
|
|
// asks for it, see desktop/main.ts and client/input_manager.ts. nothing happens without the library or on macs
|
|
|
|
const X11 = {
|
|
XOpenDisplay: { parameters: ["pointer"], result: "pointer" },
|
|
XSetErrorHandler: { parameters: ["function"], result: "pointer" },
|
|
XGetInputFocus: { parameters: ["pointer", "buffer", "buffer"], result: "i32" },
|
|
XGetGeometry: {
|
|
parameters: ["pointer", "u64", "buffer", "buffer", "buffer", "buffer", "buffer", "buffer", "buffer"],
|
|
result: "i32",
|
|
},
|
|
XWarpPointer: { parameters: ["pointer", "u64", "u64", "i32", "i32", "u32", "u32", "i32", "i32"], result: "i32" },
|
|
XSync: { parameters: ["pointer", "i32"], result: "i32" },
|
|
} as const;
|
|
|
|
const USER32 = {
|
|
GetForegroundWindow: { parameters: [], result: "pointer" },
|
|
GetWindowThreadProcessId: { parameters: ["pointer", "buffer"], result: "u32" },
|
|
GetClientRect: { parameters: ["pointer", "buffer"], result: "i32" },
|
|
ClientToScreen: { parameters: ["pointer", "buffer"], result: "i32" },
|
|
SetCursorPos: { parameters: ["i32", "i32"], result: "i32" },
|
|
} as const;
|
|
|
|
// moves the cursor to the middle of the app's window, if it's the one in front. set up on the first call
|
|
export function center_mouse() {
|
|
center ??= setup();
|
|
center();
|
|
}
|
|
|
|
let center: (() => void) | undefined;
|
|
|
|
function setup(): () => void {
|
|
switch (Deno.build.os) {
|
|
case "linux":
|
|
return setup_x11();
|
|
case "windows":
|
|
return setup_windows();
|
|
default:
|
|
return () => {};
|
|
}
|
|
}
|
|
|
|
function setup_x11(): () => void {
|
|
let lib: Deno.DynamicLibrary<typeof X11>;
|
|
try {
|
|
lib = Deno.dlopen("libX11.so.6", X11);
|
|
} catch (e) {
|
|
console.error(`Not centering the mouse, no libX11: ${(e as Error).message}`);
|
|
return () => {};
|
|
}
|
|
const x = lib.symbols;
|
|
const display = x.XOpenDisplay(null);
|
|
if (!display) {
|
|
console.error("Not centering the mouse, no X display");
|
|
return () => {};
|
|
}
|
|
// xlib's own handler exits the process, like when the window went away
|
|
const on_error = new Deno.UnsafeCallback({ parameters: ["pointer", "pointer"], result: "i32" }, () => 0);
|
|
x.XSetErrorHandler(on_error.pointer);
|
|
|
|
const focus = new BigUint64Array(1);
|
|
const revert_to = new Int32Array(1);
|
|
const root = new BigUint64Array(1);
|
|
const [left, top] = [new Int32Array(1), new Int32Array(1)];
|
|
const [width, height, border, depth] = [
|
|
new Uint32Array(1),
|
|
new Uint32Array(1),
|
|
new Uint32Array(1),
|
|
new Uint32Array(1),
|
|
];
|
|
return () => {
|
|
// the window with focus, chromium's own inside the app's frame, which has it while the game is asking
|
|
x.XGetInputFocus(display, focus, revert_to);
|
|
const window = focus[0];
|
|
// None and PointerRoot
|
|
if (window <= 1n) return;
|
|
if (!x.XGetGeometry(display, window, root, left, top, width, height, border, depth)) return;
|
|
// from no particular window, to the middle of the app's
|
|
x.XWarpPointer(display, 0n, window, 0, 0, 0, 0, width[0] >> 1, height[0] >> 1);
|
|
// done before the page hears back, it grabs the mouse again only after that
|
|
x.XSync(display, 0);
|
|
};
|
|
}
|
|
|
|
function setup_windows(): () => void {
|
|
let lib: Deno.DynamicLibrary<typeof USER32>;
|
|
try {
|
|
lib = Deno.dlopen("user32.dll", USER32);
|
|
} catch (e) {
|
|
console.error(`Not centering the mouse, no user32: ${(e as Error).message}`);
|
|
return () => {};
|
|
}
|
|
const user32 = lib.symbols;
|
|
|
|
const owner = new Uint32Array(1);
|
|
// RECT: left, top, right, bottom
|
|
const rect = new Int32Array(4);
|
|
// POINT: x, y
|
|
const point = new Int32Array(2);
|
|
return () => {
|
|
// chromium's windows are this process's own, like in shortcuts_windows.ts
|
|
const window = user32.GetForegroundWindow();
|
|
if (!window) return;
|
|
owner[0] = 0;
|
|
user32.GetWindowThreadProcessId(window, owner);
|
|
if (owner[0] !== Deno.pid) return;
|
|
if (!user32.GetClientRect(window, rect)) return;
|
|
point[0] = (rect[0] + rect[2]) >> 1;
|
|
point[1] = (rect[1] + rect[3]) >> 1;
|
|
if (!user32.ClientToScreen(window, point)) return;
|
|
user32.SetCursorPos(point[0], point[1]);
|
|
};
|
|
}
|