|
| 1 | +package io.github.kdroidfilter.nucleus.clipboard.linux |
| 2 | + |
| 3 | +import io.github.kdroidfilter.nucleus.clipboard.AccessBehavior |
| 4 | +import io.github.kdroidfilter.nucleus.clipboard.ClipboardFormat |
| 5 | +import io.github.kdroidfilter.nucleus.clipboard.internal.ClipboardBackend |
| 6 | +import io.github.kdroidfilter.nucleus.clipboard.internal.ClipboardWritePayload |
| 7 | +import io.github.kdroidfilter.nucleus.core.runtime.Platform |
| 8 | +import kotlinx.coroutines.Dispatchers |
| 9 | +import kotlinx.coroutines.withContext |
| 10 | +import java.nio.file.Path |
| 11 | +import java.nio.file.Paths |
| 12 | + |
| 13 | +/** |
| 14 | + * Linux [ClipboardBackend] with a two-backend strategy: |
| 15 | + * |
| 16 | + * - **X11** — native XCB + XFixes via JNI (`libnucleus_clipboard_linux`). |
| 17 | + * Supports the full format matrix (text, HTML, RTF, image/png, files) and |
| 18 | + * delivers change events through XFixes selection notifications. |
| 19 | + * - **Wayland** — delegates to `wl-copy` / `wl-paste` from the `wl-clipboard` |
| 20 | + * project. This already handles the three protocol variants |
| 21 | + * (`ext-data-control-v1`, `zwlr_data_control_v1`, and focus-stealing core) |
| 22 | + * plus GNOME quirks that a native implementation would have to replicate. |
| 23 | + * |
| 24 | + * Selection heuristic: |
| 25 | + * - Wayland (`WAYLAND_DISPLAY` or `XDG_SESSION_TYPE=wayland`) → Wayland delegate |
| 26 | + * if `wl-copy`/`wl-paste` are on `PATH`, otherwise fall back to the X11 |
| 27 | + * native path (Xwayland bridges CLIPBOARD for X11 clients). |
| 28 | + * - X11 → native XCB backend. |
| 29 | + */ |
| 30 | +@Suppress("TooManyFunctions") |
| 31 | +class LinuxClipboardBackend : ClipboardBackend { |
| 32 | + private enum class Mode { X11, WAYLAND, UNAVAILABLE } |
| 33 | + |
| 34 | + private val wayland = WaylandClipboardDelegate() |
| 35 | + |
| 36 | + private val mode: Mode by lazy { resolveMode() } |
| 37 | + |
| 38 | + override val name: String |
| 39 | + get() = |
| 40 | + when (mode) { |
| 41 | + Mode.X11 -> "Linux X11 (XCB+XFixes)" |
| 42 | + Mode.WAYLAND -> "Linux Wayland (wl-clipboard)" |
| 43 | + Mode.UNAVAILABLE -> "Linux unavailable" |
| 44 | + } |
| 45 | + |
| 46 | + override fun isAvailable(): Boolean = mode != Mode.UNAVAILABLE |
| 47 | + |
| 48 | + private fun resolveMode(): Mode { |
| 49 | + if (Platform.Current != Platform.Linux) return Mode.UNAVAILABLE |
| 50 | + if (Platform.isWayland && wayland.isAvailable) { |
| 51 | + wayland.startWatcher() |
| 52 | + return Mode.WAYLAND |
| 53 | + } |
| 54 | + if (NativeX11ClipboardBridge.isLoaded && NativeX11ClipboardBridge.nativeInit()) { |
| 55 | + return Mode.X11 |
| 56 | + } |
| 57 | + // Wayland session without wl-clipboard but with Xwayland available |
| 58 | + // (rare on modern distros — wl-clipboard is almost always shipped). |
| 59 | + return Mode.UNAVAILABLE |
| 60 | + } |
| 61 | + |
| 62 | + // --- Reads --- |
| 63 | + |
| 64 | + override suspend fun readText(): String? = |
| 65 | + withContext(Dispatchers.IO) { |
| 66 | + when (mode) { |
| 67 | + Mode.X11 -> NativeX11ClipboardBridge.nativeReadText() |
| 68 | + Mode.WAYLAND -> wayland.readText() |
| 69 | + Mode.UNAVAILABLE -> null |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + override suspend fun readHtml(): String? = |
| 74 | + withContext(Dispatchers.IO) { |
| 75 | + when (mode) { |
| 76 | + Mode.X11 -> NativeX11ClipboardBridge.nativeReadHtml() |
| 77 | + Mode.WAYLAND -> wayland.readHtml() |
| 78 | + Mode.UNAVAILABLE -> null |
| 79 | + }?.stripBom() |
| 80 | + } |
| 81 | + |
| 82 | + override suspend fun readRtf(): String? = |
| 83 | + withContext(Dispatchers.IO) { |
| 84 | + when (mode) { |
| 85 | + Mode.X11 -> NativeX11ClipboardBridge.nativeReadRtf() |
| 86 | + Mode.WAYLAND -> wayland.readRtf() |
| 87 | + Mode.UNAVAILABLE -> null |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + override suspend fun readImagePng(): ByteArray? = |
| 92 | + withContext(Dispatchers.IO) { |
| 93 | + when (mode) { |
| 94 | + Mode.X11 -> NativeX11ClipboardBridge.nativeReadImagePng() |
| 95 | + Mode.WAYLAND -> wayland.readImagePng() |
| 96 | + Mode.UNAVAILABLE -> null |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + override suspend fun readFiles(): List<Path> = |
| 101 | + withContext(Dispatchers.IO) { |
| 102 | + when (mode) { |
| 103 | + Mode.X11 -> { |
| 104 | + val entries = NativeX11ClipboardBridge.nativeReadFilePaths() |
| 105 | + // First entry carries the copy/cut marker ("c" or "m"); we |
| 106 | + // currently don't expose cut-vs-copy in the public API so |
| 107 | + // it is discarded. |
| 108 | + entries |
| 109 | + .drop(1) |
| 110 | + .map(Paths::get) |
| 111 | + } |
| 112 | + Mode.WAYLAND -> wayland.readFiles() |
| 113 | + Mode.UNAVAILABLE -> emptyList() |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + override suspend fun availableFormats(): Set<ClipboardFormat> = |
| 118 | + withContext(Dispatchers.IO) { |
| 119 | + val targets: List<String> = |
| 120 | + when (mode) { |
| 121 | + Mode.X11 -> NativeX11ClipboardBridge.nativeAvailableTargets().toList() |
| 122 | + Mode.WAYLAND -> wayland.listTypes() |
| 123 | + Mode.UNAVAILABLE -> emptyList() |
| 124 | + } |
| 125 | + targets.toClipboardFormats() |
| 126 | + } |
| 127 | + |
| 128 | + // --- Writes --- |
| 129 | + |
| 130 | + override suspend fun write(payload: ClipboardWritePayload): Boolean = |
| 131 | + withContext(Dispatchers.IO) { |
| 132 | + when (mode) { |
| 133 | + Mode.X11 -> { |
| 134 | + val paths = payload.files?.map { it.toAbsolutePath().toString() }?.toTypedArray() |
| 135 | + NativeX11ClipboardBridge.nativeWrite( |
| 136 | + payload.text, |
| 137 | + payload.html, |
| 138 | + payload.rtf, |
| 139 | + payload.imagePng, |
| 140 | + paths, |
| 141 | + false, |
| 142 | + ) |
| 143 | + } |
| 144 | + Mode.WAYLAND -> |
| 145 | + wayland.write( |
| 146 | + text = payload.text, |
| 147 | + html = payload.html, |
| 148 | + rtf = payload.rtf, |
| 149 | + imagePng = payload.imagePng, |
| 150 | + files = payload.files, |
| 151 | + ) |
| 152 | + Mode.UNAVAILABLE -> false |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + override suspend fun clear(): Boolean = |
| 157 | + withContext(Dispatchers.IO) { |
| 158 | + when (mode) { |
| 159 | + Mode.X11 -> NativeX11ClipboardBridge.nativeClear() |
| 160 | + Mode.WAYLAND -> wayland.clear() |
| 161 | + Mode.UNAVAILABLE -> false |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + override fun changeCount(): Long = |
| 166 | + when (mode) { |
| 167 | + Mode.X11 -> NativeX11ClipboardBridge.nativeChangeCount() |
| 168 | + Mode.WAYLAND -> wayland.changeCount() |
| 169 | + Mode.UNAVAILABLE -> 0L |
| 170 | + } |
| 171 | + |
| 172 | + // Linux has no OS-level pasteboard privacy toggle — callers are unrestricted. |
| 173 | + override fun setAccessBehavior(behavior: AccessBehavior) = Unit |
| 174 | + |
| 175 | + override fun accessBehavior(): AccessBehavior? = null |
| 176 | + |
| 177 | + override fun isAccessBehaviorSupported(): Boolean = false |
| 178 | + |
| 179 | + private fun String.stripBom(): String = |
| 180 | + when { |
| 181 | + isEmpty() -> this |
| 182 | + this[0] == '\uFEFF' -> substring(1) |
| 183 | + else -> this |
| 184 | + } |
| 185 | + |
| 186 | + private fun List<String>.toClipboardFormats(): Set<ClipboardFormat> = |
| 187 | + buildSet { |
| 188 | + for (t in this@toClipboardFormats) { |
| 189 | + when { |
| 190 | + t.equals("UTF8_STRING", ignoreCase = true) || |
| 191 | + t.equals("STRING", ignoreCase = true) || |
| 192 | + t.equals("TEXT", ignoreCase = true) || |
| 193 | + t.equals("COMPOUND_TEXT", ignoreCase = true) || |
| 194 | + t.startsWith("text/plain", ignoreCase = true) -> add(ClipboardFormat.Text) |
| 195 | + t.equals("text/html", ignoreCase = true) -> add(ClipboardFormat.Html) |
| 196 | + t.equals("text/rtf", ignoreCase = true) || |
| 197 | + t.equals("application/rtf", ignoreCase = true) -> add(ClipboardFormat.Rtf) |
| 198 | + t.startsWith("image/", ignoreCase = true) -> add(ClipboardFormat.Image) |
| 199 | + t.equals("text/uri-list", ignoreCase = true) || |
| 200 | + t.equals("x-special/gnome-copied-files", ignoreCase = true) || |
| 201 | + t.equals("application/x-kde-cutselection", ignoreCase = true) -> add(ClipboardFormat.Files) |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments