clipboard.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. """Clipboard image extraction and text write for macOS, Windows, Linux, and WSL2.
  2. No Python deps — only OS-level CLI tools: macOS osascript (always present) / pngpaste (optional)
  3. plus a file-url fallback for Finder copies; Windows and WSL2 PowerShell via WinForms,
  4. Get-Clipboard, then a file-drop fallback; Linux wl-paste (Wayland), xclip (X11).
  5. """
  6. import base64
  7. import logging
  8. import os
  9. import shutil
  10. import subprocess
  11. import sys
  12. from pathlib import Path
  13. from hermes_constants import is_wsl as _is_wsl
  14. logger = logging.getLogger(__name__)
  15. _PNG_SIGNATURE = b"\x89PNG\r\n\x1a\n"
  16. _TEXT = dict(capture_output=True, text=True, encoding='utf-8', errors='replace', stdin=subprocess.DEVNULL)
  17. _PS_FLAGS = ("-NoProfile", "-NonInteractive")
  18. _FILE_IMAGE_EXTS = {".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".tiff", ".tif"}
  19. def _nonempty(path: Path) -> bool:
  20. return path.exists() and path.stat().st_size > 0
  21. def _probe(argv: list, timeout: int, ok, *, missing: str | None = None) -> bool:
  22. """Run a text-mode probe; True when it ran and ``ok(result)`` holds.
  23. A missing executable logs *missing* (when given); every other failure is silent."""
  24. try:
  25. return bool(ok(subprocess.run(argv, timeout=timeout, **_TEXT, check=False)))
  26. except FileNotFoundError:
  27. if missing:
  28. logger.debug(missing)
  29. except Exception:
  30. pass
  31. return False
  32. def _pipe_to_file(argv: list, dest: Path) -> bool:
  33. """Run *argv* with stdout redirected into *dest*; True when a non-empty file resulted."""
  34. with open(dest, "wb") as f:
  35. subprocess.run(argv, stdin=subprocess.DEVNULL, stdout=f, stderr=subprocess.DEVNULL, timeout=5, check=True)
  36. return _nonempty(dest)
  37. def _linux_backends():
  38. """(enabled, has_image, save) in Linux fallthrough order: WSL → Wayland → X11
  39. (a failed WSL probe falls through — WSLg might have wl-paste or xclip working)."""
  40. return (
  41. (_is_wsl(), _wsl_has_image, _wsl_save),
  42. (bool(os.environ.get("WAYLAND_DISPLAY")), _wayland_has_image, _wayland_save),
  43. (True, _xclip_has_image, _xclip_save))
  44. def save_clipboard_image(dest: Path) -> bool:
  45. """Save the clipboard image to *dest* as PNG; True when an image was found and written."""
  46. dest.parent.mkdir(parents=True, exist_ok=True)
  47. if sys.platform == "darwin":
  48. # pngpaste first (fast, handles more formats); osascript is the always-present fallback.
  49. return _macos_pngpaste(dest) or _macos_osascript(dest)
  50. return (_windows_save if sys.platform == "win32" else _linux_save)(dest)
  51. def has_clipboard_image() -> bool:
  52. """Quick check: does the clipboard currently contain an image?"""
  53. if sys.platform == "darwin":
  54. return _macos_has_image()
  55. if sys.platform == "win32":
  56. return _windows_has_image()
  57. return any(enabled and has() for enabled, has, _ in _linux_backends())
  58. # ── Text write (native tools, mirrors ui-tui/src/lib/clipboard.ts) ──────
  59. def _write_clipboard_commands(data: bytes) -> list:
  60. """(argv, run_kwargs) candidates for writing *data*, in platform fallback order."""
  61. # PowerShell decodes piped stdin with the system ANSI code page (e.g. CP936), not UTF-8, so
  62. # stdin-based writes mangle CJK/emoji. Base64 the UTF-8 bytes and decode inside PowerShell
  63. # instead (same approach as the TUI's writeClipboardText).
  64. b64 = base64.b64encode(data).decode("ascii")
  65. ps_argv = [*_PS_FLAGS, "-Command", "Set-Clipboard -Value ([System.Text.Encoding]::UTF8"
  66. f".GetString([System.Convert]::FromBase64String('{b64}')))"]
  67. ps_kw, pipe = {"stdin": subprocess.DEVNULL}, {"input": data}
  68. linux = sys.platform not in ("darwin", "win32")
  69. return [(argv, kw) for enabled, argv, kw in (
  70. (sys.platform == "darwin", ["pbcopy"], pipe),
  71. (sys.platform == "win32", ["powershell", *ps_argv], ps_kw),
  72. (linux and _is_wsl(), ["powershell.exe", *ps_argv], ps_kw),
  73. (linux and os.environ.get("WAYLAND_DISPLAY"), ["wl-copy", "--type", "text/plain"], pipe),
  74. (linux, ["xclip", "-selection", "clipboard", "-in"], pipe),
  75. (linux, ["xsel", "--clipboard", "--input"], pipe),
  76. ) if enabled]
  77. def is_remote_shell_session(env=None) -> bool:
  78. """True inside an SSH session (mirrors ui-tui/src/lib/terminalSetup.ts). Over SSH, native
  79. clipboard tools write the REMOTE machine's clipboard (or an X-forwarded one), which is almost
  80. never what the user wants — OSC 52 reaches the LOCAL terminal instead."""
  81. e = os.environ if env is None else env
  82. return bool(e.get("SSH_CONNECTION") or e.get("SSH_TTY") or e.get("SSH_CLIENT"))
  83. def write_clipboard_text(text: str) -> bool:
  84. """Write *text* to the clipboard via native tools; fallback order matches the TUI: pbcopy →
  85. Windows/WSL PowerShell Set-Clipboard → wl-copy → xclip → xsel. Returns True if any backend
  86. succeeded; callers fall back to OSC 52 on False."""
  87. for argv, kw in _write_clipboard_commands(text.encode("utf-8")):
  88. try:
  89. if subprocess.run(argv, timeout=10, stdout=subprocess.DEVNULL,
  90. stderr=subprocess.DEVNULL, **kw, check=False).returncode == 0:
  91. return True
  92. except (OSError, subprocess.SubprocessError):
  93. continue
  94. return False
  95. # ── macOS ────────────────────────────────────────────────────────────────
  96. def _osascript(expr: str, timeout: int = 3) -> str:
  97. """stdout of an osascript expression; "" on any failure."""
  98. try:
  99. r = subprocess.run(["osascript", "-e", expr], timeout=timeout, **_TEXT, check=False)
  100. return r.stdout if r.returncode == 0 else ""
  101. except Exception as e:
  102. logger.debug("osascript probe failed: %s", e)
  103. return ""
  104. def _macos_has_bitmap(info: str) -> bool:
  105. return "«class PNGf»" in info or "«class TIFF»" in info
  106. def _macos_clipboard_file_image() -> Path | None:
  107. """Local image path when the clipboard holds a Finder file-url: Cmd+C on a file puts
  108. «class furl» (no bitmap) on the pasteboard, yet other apps paste it as an image."""
  109. path = Path(_osascript("POSIX path of (the clipboard as «class furl»)").strip())
  110. return path if path.suffix.lower() in _FILE_IMAGE_EXTS and os.path.isfile(path) else None
  111. def _macos_has_image() -> bool:
  112. info = _osascript("clipboard info")
  113. return _macos_has_bitmap(info) or (
  114. "«class furl»" in info and _macos_clipboard_file_image() is not None)
  115. def _macos_save_file_image(dest: Path) -> bool:
  116. src = _macos_clipboard_file_image()
  117. if src is None:
  118. return False
  119. try:
  120. shutil.copyfile(src, dest)
  121. if _is_png_file(dest) or (_convert_to_png(dest) and _is_png_file(dest)):
  122. return True
  123. except OSError as e:
  124. logger.debug("clipboard file-url extract failed: %s", e)
  125. dest.unlink(missing_ok=True)
  126. return False
  127. def _macos_pngpaste(dest: Path) -> bool:
  128. """pngpaste (brew install pngpaste) — fastest, cleanest."""
  129. try:
  130. r = subprocess.run(["pngpaste", str(dest)], stdin=subprocess.DEVNULL, capture_output=True, timeout=3, check=False)
  131. return r.returncode == 0 and _nonempty(dest)
  132. except FileNotFoundError:
  133. pass # pngpaste not installed
  134. except Exception as e:
  135. logger.debug("pngpaste failed: %s", e)
  136. return False
  137. def _macos_osascript(dest: Path) -> bool:
  138. """osascript extraction (always available): bitmap PNGf, else a Finder file-url. One
  139. `clipboard info` listing gates both — the furl read is a pasteboard *content* access."""
  140. info = _osascript("clipboard info")
  141. if not _macos_has_bitmap(info):
  142. return "«class furl»" in info and _macos_save_file_image(dest)
  143. script = f'''try
  144. set imgData to the clipboard as «class PNGf»
  145. set f to open for access POSIX file "{dest}" with write permission
  146. write imgData to f
  147. close access f
  148. on error
  149. return "fail"
  150. end try
  151. '''
  152. try:
  153. r = subprocess.run(["osascript", "-e", script], timeout=5, **_TEXT, check=False)
  154. return r.returncode == 0 and "fail" not in r.stdout and _nonempty(dest)
  155. except Exception as e:
  156. logger.debug("osascript clipboard extract failed: %s", e)
  157. return False
  158. # ── PowerShell (native Windows powershell/pwsh + WSL2 powershell.exe) ─────
  159. _FILEDROP_IMAGE_EXTS = ",".join(f"'{e}'" for e in sorted(_FILE_IMAGE_EXTS))
  160. _PS_FILEDROP_HIT = (
  161. "try { "
  162. "$files = Get-Clipboard -Format FileDropList -ErrorAction Stop;"
  163. f"$exts = @({_FILEDROP_IMAGE_EXTS});"
  164. "$hit = $files | Where-Object { $exts -contains ([System.IO.Path]::GetExtension($_).ToLowerInvariant()) } | Select-Object -First 1;"
  165. )
  166. # (has_image, extract-as-base64-PNG) script pairs, tried in order.
  167. _PS_IMAGE_STRATEGIES = (
  168. ( # .NET System.Windows.Forms.Clipboard
  169. "Add-Type -AssemblyName System.Windows.Forms;"
  170. "[System.Windows.Forms.Clipboard]::ContainsImage()",
  171. "Add-Type -AssemblyName System.Windows.Forms;"
  172. "Add-Type -AssemblyName System.Drawing;"
  173. "$img = [System.Windows.Forms.Clipboard]::GetImage();"
  174. "if ($null -eq $img) { exit 1 }"
  175. "$ms = New-Object System.IO.MemoryStream;"
  176. "$img.Save($ms, [System.Drawing.Imaging.ImageFormat]::Png);"
  177. "[System.Convert]::ToBase64String($ms.ToArray())"),
  178. ( # Get-Clipboard -Format Image (System.Drawing.Image or WPF BitmapSource)
  179. "try { "
  180. "$img = Get-Clipboard -Format Image -ErrorAction Stop;"
  181. "if ($null -ne $img) { 'True' } else { 'False' }"
  182. "} catch { 'False' }",
  183. "try { "
  184. "Add-Type -AssemblyName System.Drawing;"
  185. "Add-Type -AssemblyName PresentationCore;"
  186. "Add-Type -AssemblyName WindowsBase;"
  187. "$img = Get-Clipboard -Format Image -ErrorAction Stop;"
  188. "if ($null -eq $img) { exit 1 }"
  189. "$ms = New-Object System.IO.MemoryStream;"
  190. "if ($img -is [System.Drawing.Image]) {"
  191. "$img.Save($ms, [System.Drawing.Imaging.ImageFormat]::Png)"
  192. "} elseif ($img -is [System.Windows.Media.Imaging.BitmapSource]) {"
  193. "$enc = New-Object System.Windows.Media.Imaging.PngBitmapEncoder;"
  194. "$enc.Frames.Add([System.Windows.Media.Imaging.BitmapFrame]::Create($img));"
  195. "$enc.Save($ms)"
  196. "} else { exit 2 }"
  197. "[System.Convert]::ToBase64String($ms.ToArray())"
  198. "} catch { exit 1 }"),
  199. ( # copied image *file* (Explorer file drop)
  200. _PS_FILEDROP_HIT
  201. + "if ($null -ne $hit) { 'True' } else { 'False' }"
  202. "} catch { 'False' }",
  203. _PS_FILEDROP_HIT
  204. + "if ($null -eq $hit) { exit 1 }"
  205. "[System.Convert]::ToBase64String([System.IO.File]::ReadAllBytes($hit))"
  206. "} catch { exit 1 }"))
  207. def _ps_clipboard(exe: str, timeout: int, label: str, dest: Path | None = None) -> bool:
  208. """Probe (*dest* None) or extract (base64 PNG → *dest*) the Windows clipboard image via *exe*.
  209. A missing *exe* ends the whole chain (every script needs the same binary); any other failure
  210. logs (and drops a partial *dest*) then tries the next strategy."""
  211. for check, extract in _PS_IMAGE_STRATEGIES:
  212. try:
  213. argv = [exe, *_PS_FLAGS, "-Command", check if dest is None else extract]
  214. r = subprocess.run(argv, timeout=timeout, **_TEXT, check=False)
  215. if dest is None:
  216. if r.returncode == 0 and "True" in r.stdout:
  217. return True
  218. elif r.returncode == 0 and r.stdout.strip():
  219. dest.write_bytes(base64.b64decode(r.stdout.strip(), validate=True))
  220. if _nonempty(dest):
  221. return True
  222. except FileNotFoundError:
  223. logger.debug("%s not found — clipboard unavailable", exe)
  224. return False
  225. except Exception as e:
  226. logger.debug("%s clipboard image %s failed: %s", label,
  227. "check" if dest is None else "extraction", e)
  228. if dest is not None:
  229. dest.unlink(missing_ok=True)
  230. return False
  231. _ps_exe: str | None | bool = False # resolved PowerShell executable; False = not yet checked
  232. def _get_ps_exe() -> str | None:
  233. """First working PowerShell — ``powershell`` (5.1, always present) or ``pwsh`` (7+,
  234. optional) — cached per process; None when neither runs."""
  235. global _ps_exe
  236. if _ps_exe is False:
  237. _ps_exe = next((name for name in ("powershell", "pwsh") if _probe(
  238. [name, *_PS_FLAGS, "-Command", "echo ok"], 5,
  239. lambda r: r.returncode == 0 and "ok" in r.stdout)), None)
  240. return _ps_exe
  241. def _windows_has_image() -> bool:
  242. ps = _get_ps_exe()
  243. return ps is not None and _ps_clipboard(ps, 5, "Windows")
  244. def _windows_save(dest: Path) -> bool:
  245. ps = _get_ps_exe()
  246. if ps is None:
  247. logger.debug("No PowerShell found — Windows clipboard image paste unavailable")
  248. return False
  249. return _ps_clipboard(ps, 15, "Windows", dest)
  250. # ── Linux: WSL (powershell.exe) → Wayland (wl-paste) → X11 (xclip) ───────
  251. def _linux_save(dest: Path) -> bool:
  252. return any(enabled and save(dest) for enabled, _, save in _linux_backends())
  253. def _wsl_has_image() -> bool:
  254. return _ps_clipboard("powershell.exe", 8, "WSL")
  255. def _wsl_save(dest: Path) -> bool:
  256. return _ps_clipboard("powershell.exe", 15, "WSL", dest)
  257. _WAYLAND_MIME_PREFERENCE = ("image/png", "image/jpeg", "image/bmp", "image/gif", "image/webp")
  258. _WL_LIST_TYPES = ["wl-paste", "--list-types"]
  259. _WL_MISSING = "wl-paste not installed — Wayland clipboard unavailable"
  260. def _wayland_has_image() -> bool:
  261. return _probe(_WL_LIST_TYPES, 3, lambda r: r.returncode == 0 and any(
  262. t.startswith("image/") for t in r.stdout.splitlines()), missing=_WL_MISSING)
  263. def _wayland_save(dest: Path) -> bool:
  264. try:
  265. types_r = subprocess.run(_WL_LIST_TYPES, timeout=3, **_TEXT, check=False)
  266. types = types_r.stdout.splitlines() if types_r.returncode == 0 else ()
  267. mime = next((m for m in _WAYLAND_MIME_PREFERENCE if m in types), None) # PNG preferred
  268. if not mime:
  269. return False
  270. # save_clipboard_image() promises a PNG. Wayland can offer JPEG/GIF/WebP/BMP payloads,
  271. # so every non-PNG result is normalized (and re-verified) before reporting success.
  272. if _pipe_to_file(["wl-paste", "--type", mime], dest) and (
  273. mime == "image/png" or (_convert_to_png(dest) and _is_png_file(dest))):
  274. return True
  275. dest.unlink(missing_ok=True)
  276. except FileNotFoundError:
  277. logger.debug(_WL_MISSING)
  278. except Exception as e:
  279. logger.debug("wl-paste clipboard extraction failed: %s", e)
  280. dest.unlink(missing_ok=True)
  281. return False
  282. def _convert_to_png(path: Path) -> bool:
  283. """Convert an image file to PNG in-place: Pillow first (likely in the venv), then ImageMagick.
  284. When neither works the file is left as-is — BMP is still usable for most APIs."""
  285. try:
  286. from PIL import Image
  287. Image.open(path).save(path, "PNG")
  288. return True
  289. except ImportError:
  290. pass
  291. except Exception as e:
  292. logger.debug("Pillow BMP→PNG conversion failed: %s", e)
  293. tmp = path.with_suffix(".bmp")
  294. try:
  295. path.rename(tmp)
  296. r = subprocess.run(["convert", str(tmp), "png:" + str(path)], stdin=subprocess.DEVNULL, capture_output=True,
  297. timeout=5, check=False)
  298. if r.returncode == 0 and _nonempty(path):
  299. tmp.unlink(missing_ok=True)
  300. return True
  301. tmp.rename(path) # convert failed — restore the original file
  302. except Exception as e:
  303. if isinstance(e, FileNotFoundError):
  304. logger.debug("ImageMagick not installed — cannot convert BMP to PNG")
  305. else:
  306. logger.debug("ImageMagick BMP→PNG conversion failed: %s", e)
  307. if tmp.exists() and not path.exists():
  308. tmp.rename(path)
  309. return _nonempty(path)
  310. def _is_png_file(path: Path) -> bool:
  311. try:
  312. with path.open("rb") as f:
  313. return f.read(len(_PNG_SIGNATURE)) == _PNG_SIGNATURE
  314. except OSError:
  315. return False
  316. _XCLIP_TARGETS = ["xclip", "-selection", "clipboard", "-t", "TARGETS", "-o"]
  317. def _xclip_has_image() -> bool:
  318. return _probe(_XCLIP_TARGETS, 3, lambda r: r.returncode == 0 and "image/png" in r.stdout)
  319. def _xclip_save(dest: Path) -> bool:
  320. if not _probe(_XCLIP_TARGETS, 3, lambda r: "image/png" in r.stdout,
  321. missing="xclip not installed — X11 clipboard image paste unavailable"):
  322. return False
  323. try:
  324. return _pipe_to_file(["xclip", "-selection", "clipboard", "-t", "image/png", "-o"], dest)
  325. except Exception as e:
  326. logger.debug("xclip image extraction failed: %s", e)
  327. dest.unlink(missing_ok=True)
  328. return False