Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_clipboard.h File Reference

System clipboard: full X11 selection support on Linux, Win32 clipboard on Windows/MinGW. More...

+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define N_CLIPBOARD_CLIPBOARD   0
 the Ctrl+C / Ctrl+V clipboard selection
 
#define N_CLIPBOARD_PRIMARY   1
 the X11 PRIMARY selection (mouse select-to-copy / middle-click paste); a no-op on Windows, which has no PRIMARY selection
 

Functions

int n_clipboard_available (void)
 Whether a real clipboard backend (X11 or Win32) is active on this platform.
 
void n_clipboard_destroy (void)
 Stop the backend thread and release the display connection.
 
char * n_clipboard_get (int which)
 Fetch the current text of which selection from its owner.
 
int n_clipboard_init (void)
 Start the clipboard backend (idempotent; also started lazily on first use).
 
int n_clipboard_set (int which, const char *text)
 Own which selection and serve text to any app that requests it.
 

Detailed Description

System clipboard: full X11 selection support on Linux, Win32 clipboard on Windows/MinGW.

On Linux/X11 a small backend owns the X11 CLIPBOARD and PRIMARY selections itself and answers SelectionRequest with the full target set (UTF8_STRING, STRING, TEXT, text/plain, text/plain;charset=utf-8, and TARGETS), so strict consumers such as Chromium (which query TARGETS and want text/plain;charset= utf-8) can paste text this process copies - which a UTF8_STRING-only backend fails to satisfy. It also exposes the X11 PRIMARY selection (select-to-copy / middle-click paste) that the ordinary clipboard API cannot reach. A background thread on a dedicated X display connection serves requests promptly even while the GUI is idle.

On Windows (MinGW) N_CLIPBOARD_CLIPBOARD maps to the Win32 system clipboard (CF_UNICODETEXT, UTF-8 <-> UTF-16 internally); Windows has no PRIMARY selection, so N_CLIPBOARD_PRIMARY is a graceful no-op there (set does nothing, get returns NULL). Text always crosses this API as UTF-8.

On any other platform the backend is unavailable (n_clipboard_available() returns 0) and the caller should fall back to its toolkit's own clipboard.

Author
Castagnier Mickael
Version
1.0
Date
20/07/2026

Definition in file n_clipboard.h.

Macro Definition Documentation

◆ N_CLIPBOARD_CLIPBOARD

#define N_CLIPBOARD_CLIPBOARD   0

the Ctrl+C / Ctrl+V clipboard selection

Examples
ex_gui_multiwin.c.

Definition at line 56 of file n_clipboard.h.

◆ N_CLIPBOARD_PRIMARY

#define N_CLIPBOARD_PRIMARY   1

the X11 PRIMARY selection (mouse select-to-copy / middle-click paste); a no-op on Windows, which has no PRIMARY selection

Definition at line 59 of file n_clipboard.h.

Function Documentation

◆ n_clipboard_available()

int n_clipboard_available ( void  )

Whether a real clipboard backend (X11 or Win32) is active on this platform.

Returns
1 if available, 0 otherwise (caller should use its toolkit clipboard).

Definition at line 436 of file n_clipboard.c.

Referenced by _ngui_clip_get(), _ngui_clip_set(), _ngui_publish_primary_selection(), main(), and main().

+ Here is the caller graph for this function:

◆ n_clipboard_destroy()

void n_clipboard_destroy ( void  )

Stop the backend thread and release the display connection.

Definition at line 448 of file n_clipboard.c.

Referenced by main(), and n_gui_destroy_ctx().

+ Here is the caller graph for this function:

◆ n_clipboard_get()

char * n_clipboard_get ( int  which)

Fetch the current text of which selection from its owner.

Parameters
whichN_CLIPBOARD_CLIPBOARD or N_CLIPBOARD_PRIMARY.
Returns
A newly allocated UTF-8 string (free with free()), or NULL when empty, unavailable, or the owner did not answer in time.

Definition at line 444 of file n_clipboard.c.

Referenced by _ngui_clip_get(), main(), and main().

+ Here is the caller graph for this function:

◆ n_clipboard_init()

int n_clipboard_init ( void  )

Start the clipboard backend (idempotent; also started lazily on first use).

Returns
0 on success, -1 when no backend is available on this platform.

Definition at line 433 of file n_clipboard.c.

Referenced by main().

+ Here is the caller graph for this function:

◆ n_clipboard_set()

int n_clipboard_set ( int  which,
const char *  text 
)

Own which selection and serve text to any app that requests it.

Parameters
whichN_CLIPBOARD_CLIPBOARD or N_CLIPBOARD_PRIMARY.
textThe UTF-8 text to publish (copied). NULL/empty relinquishes it.
Returns
0 on success, -1 on failure / no backend.

Definition at line 439 of file n_clipboard.c.

Referenced by _ngui_clip_set(), and main().

+ Here is the caller graph for this function: