Skip to content

Commit

Permalink
feat(voiceglobalkeybinds): add PR Vencord#609
Browse files Browse the repository at this point in the history
  • Loading branch information
verysillycat committed Oct 14, 2024
1 parent 6a842b4 commit d376248
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ You can join our [discord server](https://discord.gg/5Xh2W87egW) for commits, ch
- Equicord preinstalled
- Custom Splash animations from [this PR](https://github.com/Vencord/Vesktop/pull/355)
- Tray Customization & Voice detection and Badge from [this PR](https://github.com/Vencord/Vesktop/pull/517)
- Global Keybind to Toggle voice status from [this PR](https://github.com/Vencord/Vesktop/pull/609)
- Remove (#) title prefix when Notification Badge option is toggled from [this PR](https://github.com/Vencord/Vesktop/pull/686)

**Not yet supported**:
Expand Down
3 changes: 3 additions & 0 deletions src/main/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,6 @@ export const enum MessageBoxChoice {
Default,
Cancel
}

export const isWayland =
process.platform === "linux" && (process.env.XDG_SESSION_TYPE === "wayland" || !!process.env.WAYLAND_DISPLAY);
80 changes: 80 additions & 0 deletions src/main/keybinds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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 { spawnSync } from "node:child_process";
import { constants, existsSync, open, unlinkSync } from "node:fs";
import { join } from "node:path";

import { Socket } from "net";
import { IpcEvents } from "shared/IpcEvents";

import { mainWin } from "./mainWindow";

const xdgRuntimeDir = process.env.XDG_RUNTIME_DIR || process.env.TMP || "/tmp";
const socketFile = join(xdgRuntimeDir, "vesktop-ipc");

const Actions = new Set([IpcEvents.TOGGLE_SELF_DEAF, IpcEvents.TOGGLE_SELF_MUTE]);

function createFIFO() {
if (existsSync(socketFile)) {
try {
unlinkSync(socketFile);
} catch (err) {
console.error("Failed to remove existing mkfifo file:", err);
return false;
}
}

try {
spawnSync("mkfifo", [socketFile]);
} catch (err) {
console.error("Failed to create mkfifo while initializing keybinds:", err);
return false;
}
return true;
}

function openFIFO() {
try {
open(socketFile, constants.O_RDONLY | constants.O_NONBLOCK, (err, fd) => {
if (err) {
console.error("Error opening pipe while initializing keybinds:", err);
return;
}

const pipe = new Socket({ fd });
pipe.on("data", data => {
const action = data.toString().trim();
if (Actions.has(action as IpcEvents)) {
mainWin.webContents.send(action);
}
});

pipe.on("end", () => {
pipe.destroy();
openFIFO();
});
});
} catch (err) {
console.error("Can't open socket file.", err);
}
}

function cleanup() {
try {
unlinkSync(socketFile);
} catch (err) {
console.error("Failed to remove mkfifo file:", err);
}
}

process.on("exit", cleanup);

export function initKeybinds() {
if (createFIFO()) {
openFIFO();
}
}
3 changes: 3 additions & 0 deletions src/main/mainWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ import {
DATA_DIR,
DEFAULT_HEIGHT,
DEFAULT_WIDTH,
isWayland,
MessageBoxChoice,
MIN_HEIGHT,
MIN_WIDTH,
VENCORD_DIR
} from "./constants";
import { initKeybinds } from "./keybinds";
import { Settings, State, VencordSettings } from "./settings";
import { addSplashLog, splash } from "./splash";
import { setTrayIcon } from "./tray";
Expand Down Expand Up @@ -527,4 +529,5 @@ export async function createWindows() {
});

initArRPC();
if (isWayland) initKeybinds();
}
4 changes: 1 addition & 3 deletions src/main/screenShare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import { desktopCapturer, session, Streams } from "electron";
import type { StreamPick } from "renderer/components/ScreenSharePicker";
import { IpcEvents } from "shared/IpcEvents";

import { isWayland } from "./constants";
import { handle } from "./utils/ipcWrappers";

const isWayland =
process.platform === "linux" && (process.env.XDG_SESSION_TYPE === "wayland" || !!process.env.WAYLAND_DISPLAY);

export function registerScreenShareHandler() {
handle(IpcEvents.CAPTURER_GET_LARGE_THUMBNAIL, async (_, id: string) => {
const sources = await desktopCapturer.getSources({
Expand Down
8 changes: 8 additions & 0 deletions src/preload/VesktopNative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ export const VesktopNative = {
copyImage: (imageBuffer: Uint8Array, imageSrc: string) =>
invoke<void>(IpcEvents.CLIPBOARD_COPY_IMAGE, imageBuffer, imageSrc)
},
voice: {
onToggleSelfMute: (listener: (...args: any[]) => void) => {
ipcRenderer.on(IpcEvents.TOGGLE_SELF_MUTE, listener);
},
onToggleSelfDeaf: (listener: (...args: any[]) => void) => {
ipcRenderer.on(IpcEvents.TOGGLE_SELF_DEAF, listener);
}
},
tray: {
setIcon: (iconURI: string) => invoke<void>(IpcEvents.SET_TRAY_ICON, iconURI),
getIcon: (iconName: string) => invoke<string>(IpcEvents.GET_TRAY_ICON, iconName),
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ VesktopNative.arrpc.onActivity(async data => {
arRPC.handleEvent(new MessageEvent("message", { data }));
});

const VoiceActions = findByPropsLazy("toggleSelfMute");
VesktopNative.voice.onToggleSelfMute(() => VoiceActions.toggleSelfMute());
VesktopNative.voice.onToggleSelfDeaf(() => VoiceActions.toggleSelfDeaf());

// TODO: remove soon
const vencordDir = "vencordDir" as keyof typeof Settings.store;
if (Settings.store[vencordDir]) {
Expand Down
2 changes: 2 additions & 0 deletions src/shared/IpcEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export const enum IpcEvents {
ARRPC_ACTIVITY = "VCD_ARRPC_ACTIVITY",

CLIPBOARD_COPY_IMAGE = "VCD_CLIPBOARD_COPY_IMAGE",
TOGGLE_SELF_MUTE = "VCD_TOGGLE_SELF_MUTE",
TOGGLE_SELF_DEAF = "VCD_TOGGLE_SELF_DEAF",

SET_TRAY_ICON = "VCD_SET_TRAY_ICON",
GET_TRAY_ICON = "VCD_GET_TRAY_ICON",
Expand Down

0 comments on commit d376248

Please sign in to comment.