1 xtk_clipboard
liquibyte edited this page 2026-11-19 16:23:20 -06:00

xtk_clipboard.h

Copy and paste text through the X CLIPBOARD and PRIMARY selections.

X has no clipboard store: the program that copied keeps the text and hands it to each program that pastes. An XtkClipboard does both sides of that for one display. To copy, xtk_clipboard_set_text() keeps a copy of the text and makes a window of the program the owner of the selection; the clipboard then answers other programs' requests for it. To paste, xtk_clipboard_request_text() asks the owner for the text, and a callback gets it when it arrives.

Two selections are handled: CLIPBOARD (Ctrl+C and Ctrl+V) and PRIMARY (the text selected last, pasted with the middle mouse button). A program usually makes one XtkClipboard and gives it to its text widgets (for example xtk_entry_create() takes one).

A copy can also carry a second, richer form of the text under a name of the program's choice (xtk_clipboard_set_rich_text()), for example Markdown for a rich text editor. Only a program that asks for that exact name gets it; every other program gets the plain text.

Events. Copy and paste happen through X events, so the program's event loop must pass every event to xtk_clipboard_handle_event(). A paste callback is called from inside that call when the answer arrives (or at once, when the selection has no owner or the program owns it itself).

Example.

static void on_paste(const char *text, void *user_data) {
    if (text) printf("Pasted: %s\n", text);   // text is only valid in the callback
}

XtkClipboard *clipboard = xtk_clipboard_create(display);

// Copy:
xtk_clipboard_set_text(clipboard, window, "Hello", SELECTION_CLIPBOARD);
// Paste:
xtk_clipboard_request_text(clipboard, window, on_paste, NULL, SELECTION_CLIPBOARD);

for (;;) {
    XEvent event;
    XNextEvent(display, &event);
    if (xtk_clipboard_handle_event(clipboard, &event)) continue;
    // ... the program's other event handling ...
}

xtk_clipboard_destroy(clipboard);
 * 

Limitations.

  • Large text goes in pieces (the INCR protocol) both ways: a large copy is sent in pieces, and a large paste from another program is put together from its pieces before the callback gets it. While a paste arrives in pieces, its window also gets PropertyChangeMask; its own mask is put back at the end.
  • At most eight pastes can wait for an answer at once, and at most four large copies can be sent at once.
  • Text is offered as UTF8_STRING, as TEXT (answered in UTF-8) and as STRING (Latin-1, as the X conventions define it; a character Latin-1 lacks becomes ?). A paste asks for UTF8_STRING, and for TEXT when the owner refuses that; a STRING answer is turned from Latin-1 into UTF-8.
#include "xtk_clipboard.h"

Types: XtkClipboard, ClipboardCallback, SelectionType

Functions: xtk_clipboard_create, xtk_clipboard_destroy, xtk_clipboard_set_text, xtk_clipboard_request_text, xtk_clipboard_handle_event, xtk_clipboard_set_rich_text, xtk_clipboard_request_rich_text

Types

XtkClipboard

typedef struct XtkClipboard XtkClipboard;

A connection to the X selections (CLIPBOARD and PRIMARY); opaque, made by xtk_clipboard_create().

ClipboardCallback

typedef void (*ClipboardCallback)(const char *text, void *user_data);

Callback that gets the text of a paste.

Called once for each xtk_clipboard_request_text() or xtk_clipboard_request_rich_text().

SelectionType

Which X selection a call works on.

Name Value Description
SELECTION_CLIPBOARD CLIPBOARD: copy and paste with Ctrl+C and Ctrl+V.
SELECTION_PRIMARY PRIMARY: the text selected last, pasted with the middle mouse button.

Functions

xtk_clipboard_create

XtkClipboard *xtk_clipboard_create(Display *display);

Make a clipboard for a display.

One clipboard per display is usual; share it between the widgets.

Parameter Description
display The X connection

Returns: The new clipboard, or NULL if display is NULL or memory ran out

xtk_clipboard_destroy

void xtk_clipboard_destroy(XtkClipboard *clipboard);

Free a clipboard.

Frees the copied text it holds. Each paste still waiting for an answer has its callback called with NULL. The program's windows stay owners of the selections until they are destroyed, but nothing answers requests any more. Does nothing when clipboard is NULL.

Parameter Description
clipboard The clipboard to free, or NULL

xtk_clipboard_set_text

void xtk_clipboard_set_text(
    XtkClipboard *clipboard,
    Window window,
    const char *text,
    SelectionType type);

Copy text to a selection.

The clipboard keeps its own copy of the text and makes window the owner of the selection, so other programs can paste it. Any earlier text of that selection, and any rich form given with xtk_clipboard_set_rich_text(), is dropped. When another program copies later, the clipboard drops the text.

Parameter Description
clipboard The clipboard
window A window of the program; it becomes the selection's owner
text The UTF-8 text to copy (it is copied), or NULL to give up the selection
type SELECTION_CLIPBOARD or SELECTION_PRIMARY

xtk_clipboard_request_text

void xtk_clipboard_request_text(
    XtkClipboard *clipboard,
    Window window,
    ClipboardCallback callback,
    void *user_data,
    SelectionType type);

Ask for the text of a selection.

The answer comes later: callback is called from xtk_clipboard_handle_event() when the owner's answer arrives, so the program must keep passing events to it. When the selection has no owner, or the program owns it through this clipboard, callback is called at once, before this returns. When eight pastes are already waiting, callback is called at once with NULL.

Parameter Description
clipboard The clipboard
window A window of the program; the answer is delivered to it. Use a window that has no other paste waiting.
callback Called once with the text, or with NULL if there is none. When callback is NULL, nothing is asked.
user_data Passed to callback
type SELECTION_CLIPBOARD or SELECTION_PRIMARY

xtk_clipboard_handle_event

int xtk_clipboard_handle_event(XtkClipboard *clipboard, XEvent *event);

Handle the X events of copy and paste.

Pass every event from the program's event loop. It answers other programs' requests for the text this clipboard owns (SelectionRequest), delivers the answers to this clipboard's pastes (SelectionNotify), drops the text when another program takes the selection (SelectionClear), and sends large copies in pieces (PropertyNotify).

Parameter Description
clipboard The clipboard
event The X event

Returns: 1 if the event was meant for this clipboard and handled, 0 otherwise: a SelectionRequest for a selection it owns, a SelectionNotify that answers one of its pastes, a SelectionClear for a selection its window owned, a PropertyNotify of one of its transfers in pieces. Any other event, also one of these kinds meant for another window or a second clipboard, gives 0, so the program or the other clipboard can handle it.

xtk_clipboard_set_rich_text

void xtk_clipboard_set_rich_text(
    XtkClipboard *clipboard,
    Window window,
    const char *target_name,
    const char *plain_text,
    const char *rich_text,
    SelectionType type);

Copy text to a selection with a second, richer form of it.

Like xtk_clipboard_set_text(), plus a rich form offered under target_name. A program that asks for target_name gets rich_text; a program that asks for plain text gets plain_text. Both replace whatever the selection held, and a later xtk_clipboard_set_text() on the same selection drops the rich form again: there is always one current copy.

Parameter Description
clipboard The clipboard
window A window of the program; it becomes the selection's owner
target_name The name of the rich form, as an X atom name, for example "XTK_RICHTEXT_MARKDOWN". Use a name unlikely to be used by anything else: only a program that asks for this exact name gets rich_text. When NULL, no rich form is offered.
plain_text The UTF-8 plain text (it is copied), served to every program that asks for text. NULL gives up the selection, as xtk_clipboard_set_text() with NULL does, and rich_text is ignored.
rich_text The UTF-8 rich form (it is copied), or NULL to copy only the plain text
type SELECTION_CLIPBOARD or SELECTION_PRIMARY

xtk_clipboard_request_rich_text

void xtk_clipboard_request_rich_text(
    XtkClipboard *clipboard,
    Window window,
    const char *target_name,
    ClipboardCallback callback,
    void *user_data,
    SelectionType type);

Ask for the rich form of a selection, offered under target_name.

Works as xtk_clipboard_request_text(), but asks for target_name instead of plain text. When the callback gets NULL, ask for the plain text with xtk_clipboard_request_text() instead.

Parameter Description
clipboard The clipboard
window A window of the program; the answer is delivered to it
target_name The same name the copying program gave xtk_clipboard_set_rich_text(); it must match exactly
callback Called once with the rich text, or with NULL if the selection has no owner, the owner does not offer target_name, or the request failed. Check the text before using it: any program can own the selection and offer a form with that name.
user_data Passed to callback
type SELECTION_CLIPBOARD or SELECTION_PRIMARY