37#define CHECK(cond, msg) \
40 printf("ex_clipboard: FAIL: %s\n", msg); \
48#include <sys/select.h>
53static char* fetch_target(
const char* sel_name,
const char* target_name) {
54 Display* d = XOpenDisplay(NULL);
56 Atom sel, target, prop;
61 w = XCreateSimpleWindow(d, RootWindow(d, DefaultScreen(d)), 0, 0, 1, 1, 0, 0, 0);
62 sel = XInternAtom(d, sel_name, False);
63 target = XInternAtom(d, target_name, False);
64 prop = XInternAtom(d,
"EX_CLIP_PROP", False);
65 XConvertSelection(d, sel, target, prop, w, CurrentTime);
67 fd = ConnectionNumber(d);
68 for (tries = 0; tries < 20 && !out; tries++) {
75 select(fd + 1, &fds, NULL, NULL, &tv);
79 if (ev.type == SelectionNotify) {
80 if (ev.xselection.property != None) {
84 unsigned char* data = NULL;
85 if (XGetWindowProperty(d, w, prop, 0, (~0L), False, AnyPropertyType, &rt, &rf, &ni, &ba, &data) == Success && data) {
88 memcpy(out, data, ni);
108 printf(
"ex_clipboard: SKIP (no X11 display)\n");
117 CHECK(c && strcmp(c,
"hello clipboard") == 0,
"clipboard round trip");
123 CHECK(p && strcmp(p,
"hello primary") == 0,
"primary round trip");
127#if defined(__linux__)
131 char* c = fetch_target(
"CLIPBOARD",
"text/plain;charset=utf-8");
132 CHECK(c && strcmp(c,
"hello clipboard") == 0,
"serve text/plain;charset=utf-8 (Chromium)");
134 c = fetch_target(
"CLIPBOARD",
"UTF8_STRING");
135 CHECK(c && strcmp(c,
"hello clipboard") == 0,
"serve UTF8_STRING");
137 c = fetch_target(
"PRIMARY",
"STRING");
138 CHECK(c && strcmp(c,
"hello primary") == 0,
"serve STRING on PRIMARY");
141 c = fetch_target(
"CLIPBOARD",
"TARGETS");
142 CHECK(c != NULL,
"answer a TARGETS query");
149 printf(
"ex_clipboard: all checks passed\n");
151 printf(
"ex_clipboard: %d checks FAILED\n",
failures);
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
int n_clipboard_set(int which, const char *text)
Own which selection and serve text to any app that requests it.
int n_clipboard_available(void)
Whether a real clipboard backend (X11 or Win32) is active on this platform.
int n_clipboard_init(void)
Start the clipboard backend (idempotent; also started lazily on first use).
char * n_clipboard_get(int which)
Fetch the current text of which selection from its owner.
void n_clipboard_destroy(void)
Stop the backend thread and release the display connection.
System clipboard: full X11 selection support on Linux, Win32 clipboard on Windows/MinGW.
#define N_CLIPBOARD_PRIMARY
the X11 PRIMARY selection (mouse select-to-copy / middle-click paste); a no-op on Windows,...
#define N_CLIPBOARD_CLIPBOARD
the Ctrl+C / Ctrl+V clipboard selection