diff --git a/src/main/venmic.ts b/src/main/venmic.ts index b86e7f6..c4b71ae 100644 --- a/src/main/venmic.ts +++ b/src/main/venmic.ts @@ -4,17 +4,53 @@ * Copyright (c) 2023 Vendicated and Vencord contributors */ -import type { PatchBay } from "@vencord/venmic"; +import type { PatchBay as PatchBayType } from "@vencord/venmic"; import { app, ipcMain } from "electron"; import { join } from "path"; import { IpcEvents } from "shared/IpcEvents"; import { STATIC_DIR } from "shared/paths"; -type LinkData = Parameters[0]; +type LinkData = Parameters[0]; +let PatchBay: typeof PatchBayType | undefined; +let patchBayInstance: PatchBayType | undefined; + +let imported = false; let initialized = false; -let patchBay: import("@vencord/venmic").PatchBay | undefined; -let isGlibcxxToOld = false; + +let hasPipewirePulse = false; +let isGlibCxxOutdated = false; + +function importVenmic() { + if (imported) { + return; + } + + imported = true; + + try { + PatchBay = (require(join(STATIC_DIR, `dist/venmic-${process.arch}.node`)) as typeof import("@vencord/venmic")) + .PatchBay; + + hasPipewirePulse = PatchBay.hasPipeWire(); + } catch (e: any) { + console.error("Failed to import venmic", e); + isGlibCxxOutdated = (e?.stack || e?.message || "").toLowerCase().includes("glibc"); + } +} + +function obtainVenmic() { + if (!imported) { + importVenmic(); + } + + if (PatchBay && !initialized) { + initialized = true; + patchBayInstance = new PatchBay(); + } + + return patchBayInstance; +} function getRendererAudioServicePid() { return ( @@ -25,33 +61,17 @@ function getRendererAudioServicePid() { ); } -function obtainVenmic() { - if (!initialized) { - initialized = true; - try { - const { PatchBay } = require( - join(STATIC_DIR, `dist/venmic-${process.arch}.node`) - ) as typeof import("@vencord/venmic"); - patchBay = new PatchBay(); - } catch (e: any) { - console.error("Failed to initialise venmic. Make sure you're using pipewire", e); - isGlibcxxToOld = (e?.stack || e?.message || "").toLowerCase().includes("glibc"); - } - } - - return patchBay; -} - ipcMain.handle(IpcEvents.VIRT_MIC_LIST, () => { const audioPid = getRendererAudioServicePid(); + const list = obtainVenmic() ?.list() .filter(s => s["application.process.id"] !== audioPid) .map(s => s["application.name"]); - return list - ? { ok: true, targets: [...new Set(list)] } // Remove duplicates - : { ok: false, isGlibcxxToOld }; + const uniqueTargets = [...new Set(list)]; + + return list ? { ok: true, targets: uniqueTargets, hasPipewirePulse } : { ok: false, isGlibCxxOutdated }; }); ipcMain.handle(IpcEvents.VIRT_MIC_START, (_, targets: string[], workaround?: boolean) => { diff --git a/src/preload/VesktopNative.ts b/src/preload/VesktopNative.ts index 4fc068a..184b095 100644 --- a/src/preload/VesktopNative.ts +++ b/src/preload/VesktopNative.ts @@ -62,7 +62,9 @@ export const VesktopNative = { /** only available on Linux. */ virtmic: { list: () => - invoke<{ ok: false; isGlibcxxToOld: boolean } | { ok: true; targets: string[] }>(IpcEvents.VIRT_MIC_LIST), + invoke< + { ok: false; isGlibCxxOutdated: boolean } | { ok: true; targets: string[]; hasPipewirePulse: boolean } + >(IpcEvents.VIRT_MIC_LIST), start: (targets: string[], workaround?: boolean) => invoke(IpcEvents.VIRT_MIC_START, targets, workaround), startSystem: (workaround?: boolean, onlyDefaultSpeakers?: boolean) => invoke(IpcEvents.VIRT_MIC_START_SYSTEM, workaround, onlyDefaultSpeakers), diff --git a/src/renderer/components/ScreenSharePicker.tsx b/src/renderer/components/ScreenSharePicker.tsx index eddfd7a..6f95ae0 100644 --- a/src/renderer/components/ScreenSharePicker.tsx +++ b/src/renderer/components/ScreenSharePicker.tsx @@ -265,7 +265,7 @@ function AudioSourcePickerLinux({ setOnlyDefaultSpeakers(b: boolean): void; }) { const [sources, _, loading] = useAwaiter(() => VesktopNative.virtmic.list(), { - fallbackValue: { ok: true, targets: [] } + fallbackValue: { ok: true, targets: [], hasPipewirePulse: true } }); const allSources = sources.ok ? ["None", "Entire System", ...sources.targets] : null;