I have fixed this, and also added it to sanity - Microsoft Windows only, sadly.
Question: How to check if there is data on the Linux clipboard? (TSE SAL is written in Borland C)?
[ChatGPT]
On Linux there is not a single "clipboard API" like Windows. What you can do depends on **which display system you are running under**:
* **X11 (xterm/gnome-terminal on Xorg, or Xwayland):** you can query the clipboard selection owner via Xlib.
* **Wayland (native Wayland terminals):** there is *no global clipboard owner you can query via Xlib*; you typically talk to the compositor (Wayland protocols) or shell out to `wl-paste`.
* **Pure terminal clipboard (OSC 52):** that is a *terminal feature*, not an OS clipboard you can query reliably.
Below are the practical options.
---
## 1) X11: check if CLIPBOARD (or PRIMARY) has an owner
In X11 the "clipboard" is a **selection**. If it has **no owner**, there is nothing to paste.
`XGetSelectionOwner()` returns `None` if there is no owner for that selection. ([
tronche.com][1])
**Minimal C check (X11):**
```c
// returns 1 if CLIPBOARD likely has something, else 0
int isX11ClipboardAvailable(void)
{
Display *dpy = XOpenDisplay(NULL);
if (!dpy) return 0;
Atom clip = XInternAtom(dpy, "CLIPBOARD", False);
Window owner = XGetSelectionOwner(dpy, clip); // None => no owner
XCloseDisplay(dpy);
return (owner != None) ? 1 : 0;
}
// optional: PRIMARY selection (middle-click paste on X11)
int isX11PrimaryAvailable(void)
{
Display *dpy = XOpenDisplay(NULL);
if (!dpy) return 0;
Atom prim = XInternAtom(dpy, "PRIMARY", False);
Window owner = XGetSelectionOwner(dpy, prim);
XCloseDisplay(dpy);
return (owner != None) ? 1 : 0;
}
```
### Caveat (important)
An owner existing does not guarantee it offers **text**. For a closer "isWinClipAvailable"-style test, you should request the selection's supported **TARGETS** (via `XConvertSelection`) and check for `UTF8_STRING`, `TEXT`, `STRING`, etc. (X11 clipboard is *lazy*: the owner provides the data when asked.)
---
## 2) Wayland: you can not do the above; use wl-clipboard (or a Wayland client)
On Wayland, clipboard access is mediated by the compositor; Xlib selection-owner tricks do not apply.
The practical route for a terminal editor is to **shell out** to `wl-paste` (from `wl-clipboard`) and decide "available" based on whether it returns anything.
Also note: in `wl-paste --watch` mode, the tool explicitly distinguishes clipboard state (`data`, `nil`, `clear`). ([Debian Manpages][2])
(That "nil vs clear" distinction is Wayland-specific behavior you will see discussed a lot; clipboard persistence can depend on a clipboard manager. ([Hyprland][3]))
### A robust-ish Wayland check
Because `wl-paste` can block in some "clipboard empty" situations on some setups (there are reports of hangs), many programs do one of:
* run `wl-paste` with a small timeout, or
* run `wl-paste --list-types` and see if it prints anything (behavior can still vary by environment).
If you are implementing inside TSE's C core, the "safe" approach is:
* Try X11 owner check **if `$DISPLAY` is set** and XOpenDisplay works.
* Else, if `$WAYLAND_DISPLAY` is set, run `wl-paste` with a **short timeout** and treat "got any bytes" as "available".
(Your Linux clipboard support already likely shells out somewhere, so this fits your architecture.)
---
## 3) Recommended combined strategy (what editors typically do)
1. **If X11 available:** `XGetSelectionOwner(CLIPBOARD)` (and optionally PRIMARY) fast and non-blocking. ([
tronche.com][1])
2. **Else if Wayland:** shell out to `wl-paste` with a timeout (or `--list-types`), treat non-empty output as "available". ([Ubuntu Manpages][4])
3. **Else (no GUI clipboard):** return "not available" (or rely on terminal paste paths only).
If you tell me what your Linux clipboard support in TSE currently uses (Xlib selection code vs calling `xclip/xsel/wl-paste` vs OSC 52), I can sketch an `isLinClipAvailable()` that matches *exactly* that backend and would not hang your UI/menu logic.