Skip to content

Commit 9e9ac8a

Browse files
swenzeclaude
andcommitted
fix(clipboard): xclip detected but unusable without X11 display
Root cause: `which xclip` succeeds on the remote Termux server, but `xclip -selection clipboard -o` fails with "Can't open display: (null)" because there's no X11 display. The error was silently swallowed. Fix: After detecting xclip exists, verify it actually works by running a test command. If it fails with display errors, mark backend as NONE instead of XCLIP. This allows fallback detection to work correctly. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 3586c4e commit 9e9ac8a

1 file changed

Lines changed: 30 additions & 3 deletions

File tree

app/src/main/java/com/termux/app/clipboard/ClipboardSyncManager.kt

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,16 +219,43 @@ class ClipboardSyncManager private constructor() {
219219
.observeOn(AndroidSchedulers.mainThread())
220220
.subscribe({ output ->
221221
if (output.contains("xclip")) {
222+
// xclip 存在,但需要验证它能否实际工作(需要 DISPLAY 环境变量)
223+
Logger.logInfo(LOG_TAG, "检测到xclip,验证是否可用...")
224+
verifyXclipWorks()
225+
} else {
226+
backend = ClipboardBackend.NONE
227+
Logger.logInfo(LOG_TAG, "未检测到xclip")
228+
onBackendDetected()
229+
}
230+
}, {
231+
backend = ClipboardBackend.NONE
232+
Logger.logInfo(LOG_TAG, "未检测到可用的剪贴板后端 (xclip/termux-clipboard)")
233+
onBackendDetected()
234+
})
235+
}
236+
237+
/**
238+
* 验证 xclip 是否能实际工作(需要 X11 display)
239+
*/
240+
private fun verifyXclipWorks() {
241+
// 尝试用 echo 管道写入 xclip,如果失败说明没有 display
242+
SFTPConnectionManager.getInstance().executeCommand("echo test | xclip -selection clipboard -i 2>&1")
243+
.subscribeOn(Schedulers.io())
244+
.observeOn(AndroidSchedulers.mainThread())
245+
.subscribe({ output ->
246+
if (output.contains("Can't open display") || output.contains("Error")) {
247+
Logger.logWarn(LOG_TAG, "xclip 存在但无法工作(无X11 display),跳过")
248+
backend = ClipboardBackend.NONE
249+
} else {
222250
backend = ClipboardBackend.XCLIP
223251
readCommand = "xclip -selection clipboard -o"
224252
writeCommand = "xclip -selection clipboard -i"
225-
} else {
226-
backend = ClipboardBackend.NONE
253+
Logger.logInfo(LOG_TAG, "xclip 验证通过,使用xclip后端")
227254
}
228255
onBackendDetected()
229256
}, {
257+
Logger.logWarn(LOG_TAG, "xclip 验证失败: ${it.message},跳过")
230258
backend = ClipboardBackend.NONE
231-
Logger.logInfo(LOG_TAG, "未检测到可用的剪贴板后端 (xclip/termux-clipboard)")
232259
onBackendDetected()
233260
})
234261
}

0 commit comments

Comments
 (0)