From 0503eff71e51499183c5df768f41422ba2567fcb Mon Sep 17 00:00:00 2001 From: Tuxinal <24763016+tuxinal@users.noreply.github.com> Date: Sun, 7 Jan 2024 15:56:07 +0330 Subject: [PATCH 1/9] Add global shortcuts --- src/main/ipc.ts | 26 +++++++++++++++++- src/preload/VesktopNative.ts | 4 +++ src/renderer/index.ts | 2 ++ src/renderer/patches/index.ts | 1 + src/renderer/patches/keybinds.ts | 45 ++++++++++++++++++++++++++++++++ src/shared/IpcEvents.ts | 4 ++- 6 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 src/renderer/patches/keybinds.ts diff --git a/src/main/ipc.ts b/src/main/ipc.ts index e0bf1316..0c7b11c3 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -7,7 +7,17 @@ if (process.platform === "linux") import("./venmic"); import { execFile } from "child_process"; -import { app, BrowserWindow, clipboard, dialog, nativeImage, RelaunchOptions, session, shell } from "electron"; +import { + app, + BrowserWindow, + clipboard, + dialog, + globalShortcut, + nativeImage, + RelaunchOptions, + session, + shell +} from "electron"; import { mkdirSync, readFileSync, watch } from "fs"; import { open, readFile } from "fs/promises"; import { release } from "os"; @@ -130,6 +140,20 @@ handle(IpcEvents.CLIPBOARD_COPY_IMAGE, async (_, buf: ArrayBuffer, src: string) }); }); +const registered_keybinds = {}; + +handle(IpcEvents.KEYBIND_REGISTER, (_, id: string, shortcut: string) => { + globalShortcut.register(shortcut, () => { + // false here implies `keyup` + // electron's global shortcut system doesn't really register keyup or down as far as i can tell + mainWin.webContents.executeJavaScript(`Vesktop.keybindCallbacks["${id}"](false)`); + }); + registered_keybinds[id] = shortcut; +}); +handle(IpcEvents.KEYBIND_UNREGISTER, (_, id: string) => { + globalShortcut.unregister(registered_keybinds[id]); +}); + function readCss() { return readFile(VENCORD_QUICKCSS_FILE, "utf-8").catch(() => ""); } diff --git a/src/preload/VesktopNative.ts b/src/preload/VesktopNative.ts index 625b8c13..a755ae5c 100644 --- a/src/preload/VesktopNative.ts +++ b/src/preload/VesktopNative.ts @@ -75,5 +75,9 @@ export const VesktopNative = { clipboard: { copyImage: (imageBuffer: Uint8Array, imageSrc: string) => invoke(IpcEvents.CLIPBOARD_COPY_IMAGE, imageBuffer, imageSrc) + }, + keybind: { + register: (id: string, shortcut: string) => invoke(IpcEvents.KEYBIND_REGISTER, id, shortcut), + uregister: (id: string) => invoke(IpcEvents.KEYBIND_UNREGISTER, id) } }; diff --git a/src/renderer/index.ts b/src/renderer/index.ts index 1ccc2e41..a1c3de9c 100644 --- a/src/renderer/index.ts +++ b/src/renderer/index.ts @@ -21,6 +21,8 @@ export { Settings }; const InviteActions = findByPropsLazy("resolveInvite"); +export const keybindCallbacks: { [id: string]: Function } = {}; + export async function openInviteModal(code: string) { const { invite } = await InviteActions.resolveInvite(code, "Desktop Modal"); if (!invite) return false; diff --git a/src/renderer/patches/index.ts b/src/renderer/patches/index.ts index 7d4c4b3d..e09ff236 100644 --- a/src/renderer/patches/index.ts +++ b/src/renderer/patches/index.ts @@ -10,3 +10,4 @@ import "./platformClass"; import "./screenShareAudio"; import "./spellCheck"; import "./windowsTitleBar"; +import "./keybinds"; diff --git a/src/renderer/patches/keybinds.ts b/src/renderer/patches/keybinds.ts new file mode 100644 index 00000000..209b579e --- /dev/null +++ b/src/renderer/patches/keybinds.ts @@ -0,0 +1,45 @@ +/* + * SPDX-License-Identifier: GPL-3.0 + * Vesktop, a desktop app aiming to give you a snappier Discord Experience + * Copyright (c) 2023 Vendicated and Vencord contributors + */ + +import { keybindCallbacks } from "renderer"; + +import { addPatch } from "./shared"; + +addPatch({ + patches: [ + { + find: ".default.Messages.KEYBINDS,children:", + replacement: { + match: /.\.isPlatformEmbedded/, + replace: "true" + } + }, + { + find: "[kb store] KeybindStore", + replacement: { + match: /(inputEventRegister\(parseInt\((.{1,2})\),(.{1,2}),(.{1,2}),(.{1,2})\);else\{)([^;]*;[^;]*;.{1,2}\.keyup&&.{1,2}\.bindGlobal\(\(0,(.{1,2}\.toString)\))/, + replace: "$1$self.registerKeybind($2,$3,$4,$7);return;$6" + } + }, + { + find: "[kb store] KeybindStore", + replacement: { + // WHY IS THE RADIX SPEICIFIED + match: /(inputEventUnregister\(parseInt\((.{1,2}),10\)\);else\{)/, + replace: "$1$self.unregisterKeybind($2);return;" + } + } + ], + + registerKeybind: function (id, shortcut, callback, toString) { + keybindCallbacks[id] = callback; + VesktopNative.keybind.register(id, toString(shortcut)); + }, + unregisterKeybind: function (id) { + delete keybindCallbacks[id]; + VesktopNative.keybind.uregister(id); + } +}); diff --git a/src/shared/IpcEvents.ts b/src/shared/IpcEvents.ts index df64403e..05cbcc88 100644 --- a/src/shared/IpcEvents.ts +++ b/src/shared/IpcEvents.ts @@ -49,5 +49,7 @@ export const enum IpcEvents { ARRPC_ACTIVITY = "VCD_ARRPC_ACTIVITY", - CLIPBOARD_COPY_IMAGE = "VCD_CLIPBOARD_COPY_IMAGE" + CLIPBOARD_COPY_IMAGE = "VCD_CLIPBOARD_COPY_IMAGE", + KEYBIND_REGISTER = "VCD_KEYBIND_REGISTER", + KEYBIND_UNREGISTER = "VCD_KEYBIND_UNREGISTER" } From d222d4ffc1c846853b99b87c2287ed097c181f1b Mon Sep 17 00:00:00 2001 From: Tuxinal <24763016+tuxinal@users.noreply.github.com> Date: Sun, 7 Jan 2024 18:26:54 +0330 Subject: [PATCH 2/9] Minimize regex convert id to number enable desktop only shortcut actions --- src/main/ipc.ts | 6 ++--- src/preload/VesktopNative.ts | 5 ++-- src/renderer/index.ts | 2 +- src/renderer/patches/keybinds.ts | 46 +++++++++++++++++++------------- 4 files changed, 35 insertions(+), 24 deletions(-) diff --git a/src/main/ipc.ts b/src/main/ipc.ts index 0c7b11c3..71dd5120 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -142,15 +142,15 @@ handle(IpcEvents.CLIPBOARD_COPY_IMAGE, async (_, buf: ArrayBuffer, src: string) const registered_keybinds = {}; -handle(IpcEvents.KEYBIND_REGISTER, (_, id: string, shortcut: string) => { +handle(IpcEvents.KEYBIND_REGISTER, (_, id: number, shortcut: string, options: any) => { globalShortcut.register(shortcut, () => { // false here implies `keyup` // electron's global shortcut system doesn't really register keyup or down as far as i can tell - mainWin.webContents.executeJavaScript(`Vesktop.keybindCallbacks["${id}"](false)`); + mainWin.webContents.executeJavaScript(`Vesktop.keybindCallbacks[${id}](false)`); }); registered_keybinds[id] = shortcut; }); -handle(IpcEvents.KEYBIND_UNREGISTER, (_, id: string) => { +handle(IpcEvents.KEYBIND_UNREGISTER, (_, id: number) => { globalShortcut.unregister(registered_keybinds[id]); }); diff --git a/src/preload/VesktopNative.ts b/src/preload/VesktopNative.ts index a755ae5c..01af5cba 100644 --- a/src/preload/VesktopNative.ts +++ b/src/preload/VesktopNative.ts @@ -77,7 +77,8 @@ export const VesktopNative = { invoke(IpcEvents.CLIPBOARD_COPY_IMAGE, imageBuffer, imageSrc) }, keybind: { - register: (id: string, shortcut: string) => invoke(IpcEvents.KEYBIND_REGISTER, id, shortcut), - uregister: (id: string) => invoke(IpcEvents.KEYBIND_UNREGISTER, id) + register: (id: number, shortcut: string, options: any) => + invoke(IpcEvents.KEYBIND_REGISTER, id, shortcut), + uregister: (id: number) => invoke(IpcEvents.KEYBIND_UNREGISTER, id) } }; diff --git a/src/renderer/index.ts b/src/renderer/index.ts index a1c3de9c..cd0e00ca 100644 --- a/src/renderer/index.ts +++ b/src/renderer/index.ts @@ -21,7 +21,7 @@ export { Settings }; const InviteActions = findByPropsLazy("resolveInvite"); -export const keybindCallbacks: { [id: string]: Function } = {}; +export const keybindCallbacks: { [id: number]: Function } = {}; export async function openInviteModal(code: string) { const { invite } = await InviteActions.resolveInvite(code, "Desktop Modal"); diff --git a/src/renderer/patches/keybinds.ts b/src/renderer/patches/keybinds.ts index 209b579e..d159b57d 100644 --- a/src/renderer/patches/keybinds.ts +++ b/src/renderer/patches/keybinds.ts @@ -4,39 +4,49 @@ * Copyright (c) 2023 Vendicated and Vencord contributors */ +import { findByPropsLazy } from "@vencord/types/webpack"; import { keybindCallbacks } from "renderer"; import { addPatch } from "./shared"; +const { toString } = findByPropsLazy("keyToCode"); addPatch({ patches: [ { find: ".default.Messages.KEYBINDS,children:", - replacement: { - match: /.\.isPlatformEmbedded/, - replace: "true" - } + replacement: [ + { + // eslint-disable-next-line no-useless-escape + match: /\i\.isPlatformEmbedded/g, + replace: "true" + }, + { + // eslint-disable-next-line no-useless-escape + match: /\(0,\i.isDesktop\)\(\)/g, + replace: "true" + } + ] }, { find: "[kb store] KeybindStore", - replacement: { - match: /(inputEventRegister\(parseInt\((.{1,2})\),(.{1,2}),(.{1,2}),(.{1,2})\);else\{)([^;]*;[^;]*;.{1,2}\.keyup&&.{1,2}\.bindGlobal\(\(0,(.{1,2}\.toString)\))/, - replace: "$1$self.registerKeybind($2,$3,$4,$7);return;$6" - } - }, - { - find: "[kb store] KeybindStore", - replacement: { - // WHY IS THE RADIX SPEICIFIED - match: /(inputEventUnregister\(parseInt\((.{1,2}),10\)\);else\{)/, - replace: "$1$self.unregisterKeybind($2);return;" - } + replacement: [ + { + // eslint-disable-next-line no-useless-escape + match: /inputEventRegister\((parseInt\(\i\),\i,\i,\i)\);else\{/, + replace: "$&$self.registerKeybind($1);return;" + }, + { + // eslint-disable-next-line no-useless-escape + match: /inputEventUnregister\((parseInt\(\i,10\))\);else\{/, + replace: "$&$self.unregisterKeybind($1);return;" + } + ] } ], - registerKeybind: function (id, shortcut, callback, toString) { + registerKeybind: function (id, shortcut, callback, options) { keybindCallbacks[id] = callback; - VesktopNative.keybind.register(id, toString(shortcut)); + VesktopNative.keybind.register(id, toString(shortcut), options); }, unregisterKeybind: function (id) { delete keybindCallbacks[id]; From 8d3196c96ce03c4bdabf13a9f388eca580274016 Mon Sep 17 00:00:00 2001 From: Tuxinal <24763016+tuxinal@users.noreply.github.com> Date: Sat, 9 Mar 2024 01:33:48 +0330 Subject: [PATCH 3/9] cli based toggles Co-Authored-By: exhq --- src/main/index.ts | 29 +++++++++++++++++++++-------- src/main/ipc.ts | 20 +++----------------- src/preload/VesktopNative.ts | 2 +- src/renderer/patches/keybinds.ts | 2 +- 4 files changed, 26 insertions(+), 27 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index 9d6a52cc..60e536b6 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -47,8 +47,12 @@ function init() { // In the Flatpak on SteamOS the theme is detected as light, but SteamOS only has a dark mode, so we just override it if (isDeckGameMode) nativeTheme.themeSource = "dark"; - app.on("second-instance", (_event, _cmdLine, _cwd, data: any) => { - if (data.IS_DEV) app.quit(); + app.on("second-instance", (_event, cmdLine, _cwd, data: any) => { + const keybindIndex = cmdLine.indexOf("--keybind"); + + if (keybindIndex !== -1) { + mainWin.webContents.executeJavaScript(`Vesktop.keybindCallbacks[${cmdLine[keybindIndex + 1]}](false)`); + } else if (data.IS_DEV) app.quit(); else if (mainWin) { if (mainWin.isMinimized()) mainWin.restore(); if (!mainWin.isVisible()) mainWin.show(); @@ -72,15 +76,24 @@ function init() { } if (!app.requestSingleInstanceLock({ IS_DEV })) { - if (IS_DEV) { - console.log("Vesktop is already running. Quitting previous instance..."); - init(); - } else { - console.log("Vesktop is already running. Quitting..."); + if (process.argv.includes("--keybind")) { app.quit(); + } else { + if (IS_DEV) { + console.log("Vesktop is already running. Quitting previous instance..."); + init(); + } else { + console.log("Vesktop is already running. Quitting..."); + app.quit(); + } } } else { - init(); + if (process.argv.includes("--keybind")) { + console.error("No instances running! cannot issue a keybind!"); + app.quit(); + } else { + init(); + } } async function bootstrap() { diff --git a/src/main/ipc.ts b/src/main/ipc.ts index 71dd5120..fb6dd447 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -7,17 +7,7 @@ if (process.platform === "linux") import("./venmic"); import { execFile } from "child_process"; -import { - app, - BrowserWindow, - clipboard, - dialog, - globalShortcut, - nativeImage, - RelaunchOptions, - session, - shell -} from "electron"; +import { app, BrowserWindow, clipboard, dialog, nativeImage, RelaunchOptions, session, shell } from "electron"; import { mkdirSync, readFileSync, watch } from "fs"; import { open, readFile } from "fs/promises"; import { release } from "os"; @@ -143,15 +133,11 @@ handle(IpcEvents.CLIPBOARD_COPY_IMAGE, async (_, buf: ArrayBuffer, src: string) const registered_keybinds = {}; handle(IpcEvents.KEYBIND_REGISTER, (_, id: number, shortcut: string, options: any) => { - globalShortcut.register(shortcut, () => { - // false here implies `keyup` - // electron's global shortcut system doesn't really register keyup or down as far as i can tell - mainWin.webContents.executeJavaScript(`Vesktop.keybindCallbacks[${id}](false)`); - }); registered_keybinds[id] = shortcut; + console.log(registered_keybinds); }); handle(IpcEvents.KEYBIND_UNREGISTER, (_, id: number) => { - globalShortcut.unregister(registered_keybinds[id]); + delete registered_keybinds[id]; }); function readCss() { diff --git a/src/preload/VesktopNative.ts b/src/preload/VesktopNative.ts index 01af5cba..ba69f0d5 100644 --- a/src/preload/VesktopNative.ts +++ b/src/preload/VesktopNative.ts @@ -79,6 +79,6 @@ export const VesktopNative = { keybind: { register: (id: number, shortcut: string, options: any) => invoke(IpcEvents.KEYBIND_REGISTER, id, shortcut), - uregister: (id: number) => invoke(IpcEvents.KEYBIND_UNREGISTER, id) + unregister: (id: number) => invoke(IpcEvents.KEYBIND_UNREGISTER, id) } }; diff --git a/src/renderer/patches/keybinds.ts b/src/renderer/patches/keybinds.ts index d159b57d..717113b7 100644 --- a/src/renderer/patches/keybinds.ts +++ b/src/renderer/patches/keybinds.ts @@ -50,6 +50,6 @@ addPatch({ }, unregisterKeybind: function (id) { delete keybindCallbacks[id]; - VesktopNative.keybind.uregister(id); + VesktopNative.keybind.unregister(id); } }); From 9748a036b6d34fe5f0ea2849bdac4d35c5b16809 Mon Sep 17 00:00:00 2001 From: Tuxinal <24763016+tuxinal@users.noreply.github.com> Date: Sat, 11 May 2024 19:53:43 +0330 Subject: [PATCH 4/9] Add keyup/keydown support for the cli TEMPORARY: show keybind ID in the keybind page --- src/main/index.ts | 8 +++++++- src/main/ipc.ts | 1 - src/renderer/patches/keybinds.ts | 10 ++++++++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index bfca49a1..2569d402 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -63,7 +63,13 @@ function init() { const keybindIndex = cmdLine.indexOf("--keybind"); if (keybindIndex !== -1) { - mainWin.webContents.executeJavaScript(`Vesktop.keybindCallbacks[${cmdLine[keybindIndex + 1]}](false)`); + if (cmdLine[keybindIndex + 2] === "keyup" || cmdLine[keybindIndex + 2] === "keydown") { + mainWin.webContents.executeJavaScript( + `Vesktop.keybindCallbacks[${cmdLine[keybindIndex + 1]}](${cmdLine[keybindIndex + 2] === "keydown" ? "true" : "false"})` + ); + } else { + mainWin.webContents.executeJavaScript(`Vesktop.keybindCallbacks[${cmdLine[keybindIndex + 1]}](false)`); + } } else if (data.IS_DEV) app.quit(); else if (mainWin) { if (mainWin.isMinimized()) mainWin.restore(); diff --git a/src/main/ipc.ts b/src/main/ipc.ts index fb6dd447..4166c6da 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -134,7 +134,6 @@ const registered_keybinds = {}; handle(IpcEvents.KEYBIND_REGISTER, (_, id: number, shortcut: string, options: any) => { registered_keybinds[id] = shortcut; - console.log(registered_keybinds); }); handle(IpcEvents.KEYBIND_UNREGISTER, (_, id: number) => { delete registered_keybinds[id]; diff --git a/src/renderer/patches/keybinds.ts b/src/renderer/patches/keybinds.ts index 717113b7..6ba1f903 100644 --- a/src/renderer/patches/keybinds.ts +++ b/src/renderer/patches/keybinds.ts @@ -22,8 +22,14 @@ addPatch({ }, { // eslint-disable-next-line no-useless-escape - match: /\(0,\i.isDesktop\)\(\)/g, + match: /\(0,\i\.isDesktop\)\(\)/g, replace: "true" + }, + { + // THIS PATCH IS TEMPORARY + // eslint-disable-next-line no-useless-escape + match: /\.keybindGroup,\i.card\),children:\[/g, + replace: "$&`ID: ${this.props.keybind.id}`," } ] }, @@ -37,7 +43,7 @@ addPatch({ }, { // eslint-disable-next-line no-useless-escape - match: /inputEventUnregister\((parseInt\(\i,10\))\);else\{/, + match: /inputEventUnregister\((parseInt\(\i,10\))\);else if\(\i\[\i\]\)\{/, replace: "$&$self.unregisterKeybind($1);return;" } ] From 4fd335b68c16237b923a8490744639895b932cd1 Mon Sep 17 00:00:00 2001 From: Tuxinal <24763016+tuxinal@users.noreply.github.com> Date: Tue, 2 Jul 2024 00:22:51 +0330 Subject: [PATCH 5/9] update patch --- src/renderer/patches/keybinds.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/renderer/patches/keybinds.ts b/src/renderer/patches/keybinds.ts index 6ba1f903..e3520437 100644 --- a/src/renderer/patches/keybinds.ts +++ b/src/renderer/patches/keybinds.ts @@ -13,7 +13,7 @@ const { toString } = findByPropsLazy("keyToCode"); addPatch({ patches: [ { - find: ".default.Messages.KEYBINDS,children:", + find: "keybindActionTypes", replacement: [ { // eslint-disable-next-line no-useless-escape From 657b51b0da80becdcf17ba2a01fe71d1178fd1cf Mon Sep 17 00:00:00 2001 From: Tuxinal <24763016+tuxinal@users.noreply.github.com> Date: Tue, 16 Jul 2024 00:35:00 +0330 Subject: [PATCH 6/9] fix shortcut string function --- src/renderer/patches/keybinds.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/renderer/patches/keybinds.ts b/src/renderer/patches/keybinds.ts index e3520437..fc33f73b 100644 --- a/src/renderer/patches/keybinds.ts +++ b/src/renderer/patches/keybinds.ts @@ -4,11 +4,11 @@ * Copyright (c) 2023 Vendicated and Vencord contributors */ -import { findByPropsLazy } from "@vencord/types/webpack"; +import { findByCodeLazy } from "@vencord/types/webpack"; import { keybindCallbacks } from "renderer"; import { addPatch } from "./shared"; -const { toString } = findByPropsLazy("keyToCode"); +const toShortcutString = findByCodeLazy('return"gamepad".'); addPatch({ patches: [ @@ -52,7 +52,7 @@ addPatch({ registerKeybind: function (id, shortcut, callback, options) { keybindCallbacks[id] = callback; - VesktopNative.keybind.register(id, toString(shortcut), options); + VesktopNative.keybind.register(id, toShortcutString(shortcut), options); }, unregisterKeybind: function (id) { delete keybindCallbacks[id]; From f0919833e6ec278c9f5ad342b447f87c804642dd Mon Sep 17 00:00:00 2001 From: Tuxinal <24763016+tuxinal@users.noreply.github.com> Date: Sun, 18 Aug 2024 19:27:25 +0330 Subject: [PATCH 7/9] add venbind. currently only linux x64 on x11 --- package.json | 3 ++- pnpm-lock.yaml | 26 ++++++++++++------- scripts/build/build.mts | 4 +++ src/main/index.ts | 2 ++ src/main/ipc.ts | 9 ------- src/main/venbind.ts | 57 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 82 insertions(+), 19 deletions(-) create mode 100644 src/main/venbind.ts diff --git a/package.json b/package.json index ca5cf495..d8d3fedd 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,8 @@ }, "dependencies": { "arrpc": "github:OpenAsar/arrpc#c62ec6a04c8d870530aa6944257fe745f6c59a24", - "electron-updater": "^6.2.1" + "electron-updater": "^6.2.1", + "venbind": "^0.0.2" }, "optionalDependencies": { "@vencord/venmic": "^6.1.0" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e67ad18f..64b614cb 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -19,6 +19,9 @@ importers: electron-updater: specifier: ^6.2.1 version: 6.2.1 + venbind: + specifier: ^0.0.2 + version: 0.0.2 optionalDependencies: '@vencord/venmic': specifier: ^6.1.0 @@ -2784,6 +2787,9 @@ packages: util-deprecate@1.0.2: resolution: {integrity: sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==} + venbind@0.0.2: + resolution: {integrity: sha512-TlkghnnV43lmINTMOuZWvgRIIZJ3I5oNykXbGZPC22GE7gdoen9uPBWWEKLtN7BG4wjtT/dIAv2Xz9gHz9P4Gw==} + verror@1.10.1: resolution: {integrity: sha512-veufcmxri4e3XSrT0xwfUR7kguIkaxBeosDg00yDWhk49wdwkSUrvvsm7nc75e1PUyvIeZj6nS8VQRYz2/S4Xg==} engines: {node: '>=0.6.0'} @@ -3441,7 +3447,7 @@ snapshots: app-builder-bin@5.0.0-alpha.6: {} - app-builder-lib@24.13.3(dmg-builder@25.0.1(electron-builder-squirrel-windows@24.13.3(dmg-builder@25.0.1)))(electron-builder-squirrel-windows@24.13.3(dmg-builder@25.0.1)): + app-builder-lib@24.13.3(dmg-builder@25.0.1(electron-builder-squirrel-windows@24.13.3))(electron-builder-squirrel-windows@24.13.3(dmg-builder@25.0.1)): dependencies: '@develar/schema-utils': 2.6.5 '@electron/notarize': 2.2.1 @@ -3455,7 +3461,7 @@ snapshots: builder-util-runtime: 9.2.4 chromium-pickle-js: 0.2.0 debug: 4.3.5 - dmg-builder: 25.0.1(electron-builder-squirrel-windows@24.13.3(dmg-builder@25.0.1)) + dmg-builder: 25.0.1(electron-builder-squirrel-windows@24.13.3) ejs: 3.1.10 electron-builder-squirrel-windows: 24.13.3(dmg-builder@25.0.1) electron-publish: 24.13.1 @@ -3475,7 +3481,7 @@ snapshots: transitivePeerDependencies: - supports-color - app-builder-lib@25.0.1(dmg-builder@25.0.1(electron-builder-squirrel-windows@24.13.3(dmg-builder@25.0.1)))(electron-builder-squirrel-windows@24.13.3(dmg-builder@25.0.1)): + app-builder-lib@25.0.1(dmg-builder@25.0.1(electron-builder-squirrel-windows@24.13.3))(electron-builder-squirrel-windows@24.13.3(dmg-builder@25.0.1)): dependencies: '@develar/schema-utils': 2.6.5 '@electron/notarize': 2.3.2 @@ -3490,7 +3496,7 @@ snapshots: builder-util-runtime: 9.2.5 chromium-pickle-js: 0.2.0 debug: 4.3.5 - dmg-builder: 25.0.1(electron-builder-squirrel-windows@24.13.3(dmg-builder@25.0.1)) + dmg-builder: 25.0.1(electron-builder-squirrel-windows@24.13.3) ejs: 3.1.10 electron-builder-squirrel-windows: 24.13.3(dmg-builder@25.0.1) electron-publish: 25.0.1 @@ -4051,9 +4057,9 @@ snapshots: '@types/react': 17.0.2 moment: 2.30.1 - dmg-builder@25.0.1(electron-builder-squirrel-windows@24.13.3(dmg-builder@25.0.1)): + dmg-builder@25.0.1(electron-builder-squirrel-windows@24.13.3): dependencies: - app-builder-lib: 25.0.1(dmg-builder@25.0.1(electron-builder-squirrel-windows@24.13.3(dmg-builder@25.0.1)))(electron-builder-squirrel-windows@24.13.3(dmg-builder@25.0.1)) + app-builder-lib: 25.0.1(dmg-builder@25.0.1(electron-builder-squirrel-windows@24.13.3))(electron-builder-squirrel-windows@24.13.3(dmg-builder@25.0.1)) builder-util: 25.0.1 builder-util-runtime: 9.2.5 fs-extra: 10.1.0 @@ -4104,7 +4110,7 @@ snapshots: electron-builder-squirrel-windows@24.13.3(dmg-builder@25.0.1): dependencies: - app-builder-lib: 24.13.3(dmg-builder@25.0.1(electron-builder-squirrel-windows@24.13.3(dmg-builder@25.0.1)))(electron-builder-squirrel-windows@24.13.3(dmg-builder@25.0.1)) + app-builder-lib: 24.13.3(dmg-builder@25.0.1(electron-builder-squirrel-windows@24.13.3))(electron-builder-squirrel-windows@24.13.3(dmg-builder@25.0.1)) archiver: 5.3.2 builder-util: 24.13.1 fs-extra: 10.1.0 @@ -4114,11 +4120,11 @@ snapshots: electron-builder@25.0.1(electron-builder-squirrel-windows@24.13.3(dmg-builder@25.0.1)): dependencies: - app-builder-lib: 25.0.1(dmg-builder@25.0.1(electron-builder-squirrel-windows@24.13.3(dmg-builder@25.0.1)))(electron-builder-squirrel-windows@24.13.3(dmg-builder@25.0.1)) + app-builder-lib: 25.0.1(dmg-builder@25.0.1(electron-builder-squirrel-windows@24.13.3))(electron-builder-squirrel-windows@24.13.3(dmg-builder@25.0.1)) builder-util: 25.0.1 builder-util-runtime: 9.2.5 chalk: 4.1.2 - dmg-builder: 25.0.1(electron-builder-squirrel-windows@24.13.3(dmg-builder@25.0.1)) + dmg-builder: 25.0.1(electron-builder-squirrel-windows@24.13.3) fs-extra: 10.1.0 is-ci: 3.0.1 lazy-val: 1.0.5 @@ -5983,6 +5989,8 @@ snapshots: util-deprecate@1.0.2: {} + venbind@0.0.2: {} + verror@1.10.1: dependencies: assert-plus: 1.0.0 diff --git a/scripts/build/build.mts b/scripts/build/build.mts index 243381ba..fa832e35 100644 --- a/scripts/build/build.mts +++ b/scripts/build/build.mts @@ -45,6 +45,10 @@ async function copyVenmic() { copyFile( "./node_modules/@vencord/venmic/prebuilds/venmic-addon-linux-arm64/node-napi-v7.node", "./static/dist/venmic-arm64.node" + ), + copyFile( + "./node_modules/venbind/prebuilds/linux-x86_64/venbind-linux-x86_64.node", + "./static/dist/venbind-linux-x86_64.node" ) ]).catch(() => console.warn("Failed to copy venmic. Building without venmic support")); } diff --git a/src/main/index.ts b/src/main/index.ts index 175eff57..1fe2aeea 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -16,6 +16,7 @@ import { registerMediaPermissionsHandler } from "./mediaPermissions"; import { registerScreenShareHandler } from "./screenShare"; import { Settings, State } from "./settings"; import { isDeckGameMode } from "./utils/steamOS"; +import { startVenbind } from "./venbind"; if (IS_DEV) { require("source-map-support").install(); @@ -88,6 +89,7 @@ function init() { app.whenReady().then(async () => { if (process.platform === "win32") app.setAppUserModelId("dev.vencord.vesktop"); + startVenbind(); registerScreenShareHandler(); registerMediaPermissionsHandler(); diff --git a/src/main/ipc.ts b/src/main/ipc.ts index 6e040857..4fa662c3 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -135,15 +135,6 @@ handle(IpcEvents.CLIPBOARD_COPY_IMAGE, async (_, buf: ArrayBuffer, src: string) }); }); -const registered_keybinds = {}; - -handle(IpcEvents.KEYBIND_REGISTER, (_, id: number, shortcut: string, options: any) => { - registered_keybinds[id] = shortcut; -}); -handle(IpcEvents.KEYBIND_UNREGISTER, (_, id: number) => { - delete registered_keybinds[id]; -}); - function readCss() { return readFile(VENCORD_QUICKCSS_FILE, "utf-8").catch(() => ""); } diff --git a/src/main/venbind.ts b/src/main/venbind.ts new file mode 100644 index 00000000..617b27e5 --- /dev/null +++ b/src/main/venbind.ts @@ -0,0 +1,57 @@ +/* + * SPDX-License-Identifier: GPL-3.0 + * Vesktop, a desktop app aiming to give you a snappier Discord Experience + * Copyright (c) 2023 Vendicated and Vencord contributors + */ + +import { join } from "path"; +import { IpcEvents } from "shared/IpcEvents"; +import { STATIC_DIR } from "shared/paths"; +import type { Venbind as VenbindType } from "venbind"; + +import { mainWin } from "./mainWindow"; +import { handle } from "./utils/ipcWrappers"; + +let venbind: VenbindType | null = null; +export function obtainVenbind() { + // TODO?: make binary outputs consistant with node's apis + let os: string; + switch (process.platform) { + case "linux": + os = "linux"; + break; + // case "win32": + // os = "windows"; + // case "darwin": + // os = "darwin"; + default: + return null; + }; + let arch: string; + switch (process.arch) { + case "x64": + arch = "x86_64"; + break; + // case "arm64": + // arch = "aarch64"; + // break; + default: + return null; + }; + if (venbind == null) venbind = require(join(STATIC_DIR, `dist/venbind-${os}-${arch}.node`)); + return venbind; +} + +export function startVenbind() { + const venbind = obtainVenbind(); + venbind?.startKeybinds(null, x => { + mainWin.webContents.executeJavaScript(`Vesktop.keybindCallbacks[${x}](false)`); + }); +} + +handle(IpcEvents.KEYBIND_REGISTER, (_, id: number, shortcut: string, options: any) => { + obtainVenbind()?.registerKeybind(shortcut, id); +}); +handle(IpcEvents.KEYBIND_UNREGISTER, (_, id: number) => { + obtainVenbind()?.unregisterKeybind(id); +}); From 2fcf255f92eaad9cc4abb93458bcd35d1d4965f0 Mon Sep 17 00:00:00 2001 From: Tuxinal <24763016+tuxinal@users.noreply.github.com> Date: Sun, 18 Aug 2024 19:43:28 +0330 Subject: [PATCH 8/9] separate venbind copy function --- scripts/build/build.mts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/scripts/build/build.mts b/scripts/build/build.mts index fa832e35..4758d312 100644 --- a/scripts/build/build.mts +++ b/scripts/build/build.mts @@ -45,16 +45,22 @@ async function copyVenmic() { copyFile( "./node_modules/@vencord/venmic/prebuilds/venmic-addon-linux-arm64/node-napi-v7.node", "./static/dist/venmic-arm64.node" - ), + ) + ]).catch(() => console.warn("Failed to copy venmic. Building without venmic support")); +} + +async function copyVenbind() { + return Promise.all([ copyFile( "./node_modules/venbind/prebuilds/linux-x86_64/venbind-linux-x86_64.node", "./static/dist/venbind-linux-x86_64.node" ) - ]).catch(() => console.warn("Failed to copy venmic. Building without venmic support")); + ]).catch(() => console.warn("Failed to copy venbind. Building without venbind support")); } await Promise.all([ copyVenmic(), + copyVenbind(), createContext({ ...NodeCommonOpts, entryPoints: ["src/main/index.ts"], From 76d56583b7f914565eaff90a55767d8228c90b16 Mon Sep 17 00:00:00 2001 From: Tuxinal <24763016+tuxinal@users.noreply.github.com> Date: Sun, 18 Aug 2024 20:33:19 +0330 Subject: [PATCH 9/9] fix unregistering not working --- src/main/venbind.ts | 54 +++++++++++++++++--------------- src/renderer/patches/keybinds.ts | 4 +-- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/main/venbind.ts b/src/main/venbind.ts index 617b27e5..5a075f1a 100644 --- a/src/main/venbind.ts +++ b/src/main/venbind.ts @@ -14,31 +14,35 @@ import { handle } from "./utils/ipcWrappers"; let venbind: VenbindType | null = null; export function obtainVenbind() { - // TODO?: make binary outputs consistant with node's apis - let os: string; - switch (process.platform) { - case "linux": - os = "linux"; - break; - // case "win32": - // os = "windows"; - // case "darwin": - // os = "darwin"; - default: - return null; - }; - let arch: string; - switch (process.arch) { - case "x64": - arch = "x86_64"; - break; - // case "arm64": - // arch = "aarch64"; - // break; - default: - return null; - }; - if (venbind == null) venbind = require(join(STATIC_DIR, `dist/venbind-${os}-${arch}.node`)); + if (venbind == null) { + // TODO?: make binary outputs consistant with node's apis + let os: string; + let arch: string; + + switch (process.platform) { + case "linux": + os = "linux"; + break; + // case "win32": + // os = "windows"; + // case "darwin": + // os = "darwin"; + default: + return null; + } + switch (process.arch) { + case "x64": + arch = "x86_64"; + break; + // case "arm64": + // arch = "aarch64"; + // break; + default: + return null; + } + + venbind = require(join(STATIC_DIR, `dist/venbind-${os}-${arch}.node`)); + } return venbind; } diff --git a/src/renderer/patches/keybinds.ts b/src/renderer/patches/keybinds.ts index fc33f73b..b8550c53 100644 --- a/src/renderer/patches/keybinds.ts +++ b/src/renderer/patches/keybinds.ts @@ -43,8 +43,8 @@ addPatch({ }, { // eslint-disable-next-line no-useless-escape - match: /inputEventUnregister\((parseInt\(\i,10\))\);else if\(\i\[\i\]\)\{/, - replace: "$&$self.unregisterKeybind($1);return;" + match: /inputEventUnregister\((parseInt\(\i,10\))\);else/, + replace: "$&{$self.unregisterKeybind($1);return;}" } ] }