Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add global shortcuts #326

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
26 changes: 17 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions scripts/build/build.mts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,18 @@ async function copyVenmic() {
]).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 venbind. Building without venbind support"));
}

await Promise.all([
copyVenmic(),
copyVenbind(),
createContext({
...NodeCommonOpts,
entryPoints: ["src/main/index.ts"],
Expand Down
37 changes: 29 additions & 8 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -66,8 +67,18 @@ 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) {
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();
if (!mainWin.isVisible()) mainWin.show();
Expand All @@ -78,6 +89,7 @@ function init() {
app.whenReady().then(async () => {
if (process.platform === "win32") app.setAppUserModelId("dev.vencord.vesktop");

startVenbind();
registerScreenShareHandler();
registerMediaPermissionsHandler();

Expand All @@ -90,15 +102,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() {
Expand Down
61 changes: 61 additions & 0 deletions src/main/venbind.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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() {
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;
}

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);
});
5 changes: 5 additions & 0 deletions src/preload/VesktopNative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,10 @@ export const VesktopNative = {
clipboard: {
copyImage: (imageBuffer: Uint8Array, imageSrc: string) =>
invoke<void>(IpcEvents.CLIPBOARD_COPY_IMAGE, imageBuffer, imageSrc)
},
keybind: {
register: (id: number, shortcut: string, options: any) =>
invoke<void>(IpcEvents.KEYBIND_REGISTER, id, shortcut),
unregister: (id: number) => invoke<void>(IpcEvents.KEYBIND_UNREGISTER, id)
}
};
2 changes: 2 additions & 0 deletions src/renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export { Settings };

const InviteActions = findByPropsLazy("resolveInvite");

export const keybindCallbacks: { [id: number]: Function } = {};

export async function openInviteModal(code: string) {
const { invite } = await InviteActions.resolveInvite(code, "Desktop Modal");
if (!invite) return false;
Expand Down
1 change: 1 addition & 0 deletions src/renderer/patches/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ import "./hideVenmicInput";
import "./screenShareFixes";
import "./spellCheck";
import "./windowsTitleBar";
import "./keybinds";
61 changes: 61 additions & 0 deletions src/renderer/patches/keybinds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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 { findByCodeLazy } from "@vencord/types/webpack";
import { keybindCallbacks } from "renderer";

import { addPatch } from "./shared";
const toShortcutString = findByCodeLazy('return"gamepad".');

addPatch({
patches: [
{
find: "keybindActionTypes",
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"
},
{
// THIS PATCH IS TEMPORARY
// eslint-disable-next-line no-useless-escape
match: /\.keybindGroup,\i.card\),children:\[/g,
replace: "$&`ID: ${this.props.keybind.id}`,"
}
]
},
{
find: "[kb store] KeybindStore",
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, options) {
keybindCallbacks[id] = callback;
VesktopNative.keybind.register(id, toShortcutString(shortcut), options);
},
unregisterKeybind: function (id) {
delete keybindCallbacks[id];
VesktopNative.keybind.unregister(id);
}
});
4 changes: 3 additions & 1 deletion src/shared/IpcEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,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"
}