Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Detect explicit Linux input method backend arguments https://github.com/siyuan-note/siyuan/issues/20549

Daniel1d97758200f5
diff --git a/app/electron/linuxInputMethod.js b/app/electron/linuxInputMethod.js
index 1d7d8cdf65..e976da0181 100644
--- a/app/electron/linuxInputMethod.js
+++ b/app/electron/linuxInputMethod.js
@@ -33,18 +33,44 @@ const writeLinuxInputMethodSetting = (file, enabled) => {
}
};
-// 显式显示后端参数优先于本机兼容设置。
-const getLinuxInputMethodOverride = (commandLine) => {
- if (commandLine.hasSwitch("ozone-platform")) {
- return commandLine.getSwitchValue("ozone-platform") === "x11";
+// 读取内核保存的启动参数,排除 Chromium 和本机设置在进程内追加的参数。
+const readLinuxInputMethodArguments = () => {
+ const content = fs.readFileSync("/proc/self/cmdline", "utf8");
+ if (!content || !content.endsWith("\0")) {
+ throw new Error("Invalid Linux process command line");
}
- return null;
+ return content.slice(0, -1).split("\0");
};
-const configureLinuxInputMethod = (commandLine, enabled, platform) => {
- if (platform === "linux" && enabled && getLinuxInputMethodOverride(commandLine) === null) {
- commandLine.appendSwitch("ozone-platform", "x11");
+// 显式显示后端参数优先于本机兼容设置,重复参数采用最后一次指定的值。
+const getLinuxInputMethodBackend = (args) => {
+ let backend = null;
+ for (let i = 1; i < args.length && args[i] !== "--"; i++) {
+ if (args[i].startsWith("--ozone-platform=")) {
+ backend = args[i].slice("--ozone-platform=".length);
+ } else if (args[i] === "--ozone-platform") {
+ backend = args[i + 1] && !args[i + 1].startsWith("--") ? args[++i] : "";
+ }
+ }
+ return backend;
+};
+
+const getLinuxInputMethodOverride = (args) => {
+ const backend = getLinuxInputMethodBackend(args);
+ return backend === null ? null : backend === "x11";
+};
+
+const configureLinuxInputMethod = (commandLine, enabled, platform, args) => {
+ if (platform !== "linux") {
+ return;
+ }
+ const backend = getLinuxInputMethodBackend(args);
+ if (backend !== null || enabled) {
+ commandLine.appendSwitch("ozone-platform", backend ?? "x11");
}
};
-module.exports = {readLinuxInputMethodSetting, writeLinuxInputMethodSetting, getLinuxInputMethodOverride, configureLinuxInputMethod};
+module.exports = {
+ readLinuxInputMethodSetting, writeLinuxInputMethodSetting, readLinuxInputMethodArguments,
+ getLinuxInputMethodOverride, configureLinuxInputMethod,
+};
diff --git a/app/electron/linuxInputMethod.test.js b/app/electron/linuxInputMethod.test.js
index 35066fd2dc..94ae2acb3f 100644
--- a/app/electron/linuxInputMethod.test.js
+++ b/app/electron/linuxInputMethod.test.js
@@ -4,7 +4,8 @@ const os = require("node:os");
const path = require("node:path");
const {test} = require("node:test");
const {
- readLinuxInputMethodSetting, writeLinuxInputMethodSetting, getLinuxInputMethodOverride, configureLinuxInputMethod,
+ readLinuxInputMethodSetting, writeLinuxInputMethodSetting, readLinuxInputMethodArguments,
+ getLinuxInputMethodOverride, configureLinuxInputMethod,
} = require("./linuxInputMethod");
const commandLine = (platform) => {
@@ -24,7 +25,7 @@ test("compatibility mode persists across launches and can be disabled", (t) => {
for (const enabled of [true, false]) {
writeLinuxInputMethodSetting(file, enabled);
const launch = commandLine();
- configureLinuxInputMethod(launch, readLinuxInputMethodSetting(file), "linux");
+ configureLinuxInputMethod(launch, readLinuxInputMethodSetting(file), "linux", ["siyuan"]);
assert.equal(launch.getSwitchValue("ozone-platform"), enabled ? "x11" : undefined);
}
assert.deepEqual(fs.readdirSync(path.dirname(file)), ["linux-input-method.json"]);
@@ -34,12 +35,59 @@ test("explicit display backend overrides either saved choice", () => {
for (const backend of ["x11", "wayland", "auto", ""]) {
for (const enabled of [false, true]) {
const launch = commandLine(backend);
- assert.equal(getLinuxInputMethodOverride(launch), backend === "x11");
- configureLinuxInputMethod(launch, enabled, "linux");
+ const args = ["siyuan", "--ozone-platform=" + backend];
+ assert.equal(getLinuxInputMethodOverride(args), backend === "x11");
+ configureLinuxInputMethod(launch, enabled, "linux", args);
assert.equal(launch.getSwitchValue("ozone-platform"), backend);
}
}
- assert.equal(getLinuxInputMethodOverride(commandLine()), null);
+ assert.equal(getLinuxInputMethodOverride(["siyuan"]), null);
+});
+
+test("automatically selected backends do not lock the preference across launches", () => {
+ const args = ["siyuan"];
+ for (const backend of ["wayland", "x11"]) {
+ for (const enabled of [false, true]) {
+ const launch = commandLine(backend);
+ configureLinuxInputMethod(launch, enabled, "linux", args);
+ assert.equal(launch.getSwitchValue("ozone-platform"), enabled ? "x11" : backend);
+ assert.equal(getLinuxInputMethodOverride(args), null);
+ }
+ }
+});
+
+test("explicit backends support separate values, repeated switches and the argument terminator", () => {
+ for (const backend of ["wayland", "x11", "auto", ""]) {
+ const args = ["siyuan", "--ozone-platform", backend];
+ const launch = commandLine("");
+ configureLinuxInputMethod(launch, true, "linux", args);
+ assert.equal(getLinuxInputMethodOverride(args), backend === "x11");
+ assert.equal(launch.getSwitchValue("ozone-platform"), backend);
+ }
+ assert.equal(getLinuxInputMethodOverride(["siyuan", "--ozone-platform=x11", "--ozone-platform=wayland"]), false);
+ assert.equal(getLinuxInputMethodOverride(["siyuan", "--", "--ozone-platform=x11"]), null);
+ assert.equal(getLinuxInputMethodOverride(["siyuan", "--ozone-platform-hint=x11"]), null);
+ assert.equal(getLinuxInputMethodOverride(["siyuan", "--ozone-platform", "--other"]), false);
+});
+
+test("original process arguments exclude appended switches and preserve empty arguments", (t) => {
+ t.mock.method(fs, "readFileSync", (file, encoding) => {
+ assert.equal(file, "/proc/self/cmdline");
+ assert.equal(encoding, "utf8");
+ return "siyuan\0--workspace=/path with spaces\0\0";
+ });
+ const args = readLinuxInputMethodArguments();
+ assert.deepEqual(args, ["siyuan", "--workspace=/path with spaces", ""]);
+ assert.equal(getLinuxInputMethodOverride(args), null);
+});
+
+test("unavailable or invalid original arguments are reported instead of guessing an override", (t) => {
+ const read = t.mock.method(fs, "readFileSync", () => { throw new Error("Access denied"); });
+ assert.throws(() => readLinuxInputMethodArguments(), /Access denied/);
+ for (const content of ["", "siyuan"]) {
+ read.mock.mockImplementation(() => content);
+ assert.throws(() => readLinuxInputMethodArguments(), /Invalid Linux process command line/);
+ }
});
test("compatibility mode does not alter other platforms", () => {
diff --git a/app/electron/main.js b/app/electron/main.js
index e46c181211..ba2527458a 100644
--- a/app/electron/main.js
+++ b/app/electron/main.js
@@ -90,7 +90,8 @@ const {
} = require("./accessibility");
const {
- readLinuxInputMethodSetting, writeLinuxInputMethodSetting, getLinuxInputMethodOverride, configureLinuxInputMethod,
+ readLinuxInputMethodSetting, writeLinuxInputMethodSetting, readLinuxInputMethodArguments,
+ getLinuxInputMethodOverride, configureLinuxInputMethod,
} = require("./linuxInputMethod");
process.noAsar = true;
@@ -809,7 +810,8 @@ try {
configureAccessibility(app.commandLine, accessibilityEnabled);
// Linux 输入法兼容设置在窗口创建前应用,显式显示后端参数保持优先。
-const linuxInputMethodOverride = getLinuxInputMethodOverride(app.commandLine);
+let linuxInputMethodOverride = null;
+let linuxInputMethodArgumentsError = null;
let linuxInputMethodEnabled = false;
if (process.platform === "linux") {
try {
@@ -817,7 +819,14 @@ if (process.platform === "linux") {
} catch (error) {
writeLog("read Linux input method setting failed: " + error.message);
}
- configureLinuxInputMethod(app.commandLine, linuxInputMethodEnabled, process.platform);
+ try {
+ const args = readLinuxInputMethodArguments();
+ linuxInputMethodOverride = getLinuxInputMethodOverride(args);
+ configureLinuxInputMethod(app.commandLine, linuxInputMethodEnabled, process.platform, args);
+ } catch (error) {
+ linuxInputMethodArgumentsError = error;
+ writeLog("read Linux process command line failed: " + error.message);
+ }
}
try {
@@ -3349,6 +3358,9 @@ app.whenReady().then(() => {
!getWindowKernelTarget(event.sender.id) || event.senderFrame !== event.sender.mainFrame) {
throw new Error("Linux input method setting is unavailable");
}
+ if (linuxInputMethodArgumentsError) {
+ throw linuxInputMethodArgumentsError;
+ }
if (data.cmd === "setLinuxInputMethodSetting") {
writeLinuxInputMethodSetting(linuxInputMethodSettingPath, data.enabled);
linuxInputMethodEnabled = data.enabled;