aboutsummaryrefslogtreecommitdiffstats
path: root/config/enableWaylandAutoDetection.cjs
diff options
context:
space:
mode:
Diffstat (limited to 'config/enableWaylandAutoDetection.cjs')
-rw-r--r--config/enableWaylandAutoDetection.cjs86
1 files changed, 86 insertions, 0 deletions
diff --git a/config/enableWaylandAutoDetection.cjs b/config/enableWaylandAutoDetection.cjs
new file mode 100644
index 0000000..393ec91
--- /dev/null
+++ b/config/enableWaylandAutoDetection.cjs
@@ -0,0 +1,86 @@
1/*
2 * Copyright (C) 2022 Kristóf Marussy <kristof@marussy.com>
3 *
4 * This file is part of Sophie.
5 *
6 * Sophie is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, version 3.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: AGPL-3.0-only
19 */
20
21const { readFile, rename, writeFile } = require('node:fs/promises');
22const path = require('node:path');
23
24const templatePath = path.join(__dirname, '../build-helpers/detect_wayland.sh');
25
26/**
27 * Replaces the `{{handlebars}}` in the template with the replacements.
28 *
29 * Each `{{VARIABLE}}` will be repalced by `value` in the replacement `['VARIABLE', value]`.
30 *
31 * @param {string} template The template.
32 * @param {[string, string][]} replacements The replacements to apply.
33 * @returns
34 */
35function executeTemplate(template, ...replacements) {
36 const replacementsMap = new Map(replacements);
37 return template.replaceAll(
38 /{{([^}]+)}}/g,
39 (_match, /** @type {string} */ variable) => {
40 const replacement = replacementsMap.get(variable);
41 if (replacement === undefined) {
42 throw new Error(`Unknown variable: ${variable}`);
43 }
44 return replacement;
45 },
46 );
47}
48
49/**
50 * Adds a wrapper scripts that detects in wayland is in use and enabled it in chromium.
51 *
52 * The script in `build-heleprs/detect_wayland.sh` uses the `WAYLAND_DISPLAY` environmental
53 * variable to detect whether wayland is in use.
54 *
55 * If wayland is in use, the script enables the wayland ozone backed for chromium
56 * and pipewire screen sharing. Otherwise, the x11 ozone backend will be used.
57 *
58 * @param {import('electron-builder').AfterPackContext} context The `electron-builder` context.
59 * @return {Promise<void>} The promise to add the wrapper script.
60 * @see https://stackoverflow.com/a/45537237
61 */
62module.exports = async function enableWaylandAutoDetection(context) {
63 const {
64 appOutDir,
65 packager: {
66 appInfo: { productName, productFilename },
67 },
68 } = context;
69
70 const electronBinaryPath = path.join(appOutDir, productFilename);
71 const newFilename = `${productFilename}-bin`;
72 const newElectronBinaryPath = path.join(appOutDir, newFilename);
73
74 await rename(electronBinaryPath, newElectronBinaryPath);
75
76 const wrapperScriptTempate = await readFile(templatePath, 'utf8');
77 const wrapperScript = executeTemplate(
78 wrapperScriptTempate,
79 ['PRODUCT_NAME', productName],
80 ['REAL_BINARY_NAME', newFilename],
81 );
82 await writeFile(electronBinaryPath, wrapperScript, {
83 encoding: 'utf8',
84 mode: 0o755,
85 });
86};