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

autoStart: add linux portal implementation #322

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions src/main/autoStart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Copyright (c) 2023 Vendicated and Vencord contributors
*/

import { spawnSync } from "child_process";
import { app } from "electron";
import { existsSync, mkdirSync, rmSync, writeFileSync } from "fs";
import { join } from "path";
Expand All @@ -14,7 +15,37 @@ interface AutoStart {
disable(): void;
}

function makeAutoStartLinux(): AutoStart {
function requestBackgroundPortal(autostart: boolean, commandline: string[]) {
const commandlineString = commandline.map(a => `'${a.replaceAll("'", "\\'")}'`).join(", ");

const { status } = spawnSync(
"gdbus",
[
"call",
"--session",
"--dest",
"org.freedesktop.portal.Desktop",
"--object-path",
"/org/freedesktop/portal/desktop",
"--method",
"org.freedesktop.portal.Background.RequestBackground",
"",
`{'autostart':<${autostart}>,'commandline':<[${commandlineString}]>}`
],
{ encoding: "utf-8", stdio: "inherit" }
);

return status === 0;
}

// todo: only apply start-minimized if setting is enabled
const LinuxAutoStartPortal = {
isEnabled: () => "dunno",
enable: () => requestBackgroundPortal(true, [process.execPath, "--start-minimized"]),
disable: () => requestBackgroundPortal(false, [])
};

const makeLinuxAutoStartDesktopFile = () => {
const configDir = process.env.XDG_CONFIG_HOME || join(process.env.HOME!, ".config");
const dir = join(configDir, "autostart");
const file = join(dir, "vencord.desktop");
Expand All @@ -28,7 +59,7 @@ Type=Application
Version=1.0
Name=Vencord
Comment=Vencord autostart script
Exec=${process.execPath}
Exec=${process.execPath} --start-minimized
Terminal=false
StartupNotify=false
`.trim();
Expand All @@ -38,6 +69,20 @@ StartupNotify=false
},
disable: () => rmSync(file, { force: true })
};
};

function makeAutoStartLinux(): AutoStart {
const autoStartDesktop = makeLinuxAutoStartDesktopFile();
const autoStartPortal = LinuxAutoStartPortal;

return {
isEnabled: () => autoStartDesktop.isEnabled(),
enable: () => autoStartPortal.enable() || autoStartDesktop.enable(),
disable: () => {
autoStartPortal.disable();
autoStartDesktop.disable();
}
};
}

const autoStartWindowsMac: AutoStart = {
Expand Down
Loading