SDL 3.0
SDL_clipboard.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22/**
23 * # CategoryClipboard
24 *
25 * SDL provides access to the system clipboard, both for reading information
26 * from other processes and publishing information of its own.
27 *
28 * This is not just text! SDL apps can access and publish data by mimetype.
29 */
30
31#ifndef SDL_clipboard_h_
32#define SDL_clipboard_h_
33
34#include <SDL3/SDL_stdinc.h>
35#include <SDL3/SDL_error.h>
36
37#include <SDL3/SDL_begin_code.h>
38/* Set up for C function definitions, even when using C++ */
39#ifdef __cplusplus
40extern "C" {
41#endif
42
43/* Function prototypes */
44
45/**
46 * Put UTF-8 text into the clipboard.
47 *
48 * \param text the text to store in the clipboard.
49 * \returns true on success or false on failure; call SDL_GetError() for more
50 * information.
51 *
52 * \since This function is available since SDL 3.0.0.
53 *
54 * \sa SDL_GetClipboardText
55 * \sa SDL_HasClipboardText
56 */
57extern SDL_DECLSPEC bool SDLCALL SDL_SetClipboardText(const char *text);
58
59/**
60 * Get UTF-8 text from the clipboard.
61 *
62 * This functions returns empty string if there was not enough memory left for
63 * a copy of the clipboard's content.
64 *
65 * \returns the clipboard text on success or an empty string on failure; call
66 * SDL_GetError() for more information. This should be freed with
67 * SDL_free() when it is no longer needed.
68 *
69 * \since This function is available since SDL 3.0.0.
70 *
71 * \sa SDL_HasClipboardText
72 * \sa SDL_SetClipboardText
73 */
74extern SDL_DECLSPEC char * SDLCALL SDL_GetClipboardText(void);
75
76/**
77 * Query whether the clipboard exists and contains a non-empty text string.
78 *
79 * \returns true if the clipboard has text, or false if it does not.
80 *
81 * \since This function is available since SDL 3.0.0.
82 *
83 * \sa SDL_GetClipboardText
84 * \sa SDL_SetClipboardText
85 */
86extern SDL_DECLSPEC bool SDLCALL SDL_HasClipboardText(void);
87
88/**
89 * Put UTF-8 text into the primary selection.
90 *
91 * \param text the text to store in the primary selection.
92 * \returns true on success or false on failure; call SDL_GetError() for more
93 * information.
94 *
95 * \since This function is available since SDL 3.0.0.
96 *
97 * \sa SDL_GetPrimarySelectionText
98 * \sa SDL_HasPrimarySelectionText
99 */
100extern SDL_DECLSPEC bool SDLCALL SDL_SetPrimarySelectionText(const char *text);
101
102/**
103 * Get UTF-8 text from the primary selection.
104 *
105 * This functions returns empty string if there was not enough memory left for
106 * a copy of the primary selection's content.
107 *
108 * \returns the primary selection text on success or an empty string on
109 * failure; call SDL_GetError() for more information. This should be
110 * freed with SDL_free() when it is no longer needed.
111 *
112 * \since This function is available since SDL 3.0.0.
113 *
114 * \sa SDL_HasPrimarySelectionText
115 * \sa SDL_SetPrimarySelectionText
116 */
117extern SDL_DECLSPEC char * SDLCALL SDL_GetPrimarySelectionText(void);
118
119/**
120 * Query whether the primary selection exists and contains a non-empty text
121 * string.
122 *
123 * \returns true if the primary selection has text, or false if it does not.
124 *
125 * \since This function is available since SDL 3.0.0.
126 *
127 * \sa SDL_GetPrimarySelectionText
128 * \sa SDL_SetPrimarySelectionText
129 */
130extern SDL_DECLSPEC bool SDLCALL SDL_HasPrimarySelectionText(void);
131
132/**
133 * Callback function that will be called when data for the specified mime-type
134 * is requested by the OS.
135 *
136 * The callback function is called with NULL as the mime_type when the
137 * clipboard is cleared or new data is set. The clipboard is automatically
138 * cleared in SDL_Quit().
139 *
140 * \param userdata a pointer to provided user data.
141 * \param mime_type the requested mime-type.
142 * \param size a pointer filled in with the length of the returned data.
143 * \returns a pointer to the data for the provided mime-type. Returning NULL
144 * or setting length to 0 will cause no data to be sent to the
145 * "receiver". It is up to the receiver to handle this. Essentially
146 * returning no data is more or less undefined behavior and may cause
147 * breakage in receiving applications. The returned data will not be
148 * freed so it needs to be retained and dealt with internally.
149 *
150 * \since This function is available since SDL 3.0.0.
151 *
152 * \sa SDL_SetClipboardData
153 */
154typedef const void *(SDLCALL *SDL_ClipboardDataCallback)(void *userdata, const char *mime_type, size_t *size);
155
156/**
157 * Callback function that will be called when the clipboard is cleared, or new
158 * data is set.
159 *
160 * \param userdata a pointer to provided user data.
161 *
162 * \since This function is available since SDL 3.0.0.
163 *
164 * \sa SDL_SetClipboardData
165 */
166typedef void (SDLCALL *SDL_ClipboardCleanupCallback)(void *userdata);
167
168/**
169 * Offer clipboard data to the OS.
170 *
171 * Tell the operating system that the application is offering clipboard data
172 * for each of the proivded mime-types. Once another application requests the
173 * data the callback function will be called allowing it to generate and
174 * respond with the data for the requested mime-type.
175 *
176 * The size of text data does not include any terminator, and the text does
177 * not need to be null terminated (e.g. you can directly copy a portion of a
178 * document)
179 *
180 * \param callback a function pointer to the function that provides the
181 * clipboard data.
182 * \param cleanup a function pointer to the function that cleans up the
183 * clipboard data.
184 * \param userdata an opaque pointer that will be forwarded to the callbacks.
185 * \param mime_types a list of mime-types that are being offered.
186 * \param num_mime_types the number of mime-types in the mime_types list.
187 * \returns true on success or false on failure; call SDL_GetError() for more
188 * information.
189 *
190 * \since This function is available since SDL 3.0.0.
191 *
192 * \sa SDL_ClearClipboardData
193 * \sa SDL_GetClipboardData
194 * \sa SDL_HasClipboardData
195 */
196extern SDL_DECLSPEC bool SDLCALL SDL_SetClipboardData(SDL_ClipboardDataCallback callback, SDL_ClipboardCleanupCallback cleanup, void *userdata, const char **mime_types, size_t num_mime_types);
197
198/**
199 * Clear the clipboard data.
200 *
201 * \returns true on success or false on failure; call SDL_GetError() for more
202 * information.
203 *
204 * \since This function is available since SDL 3.0.0.
205 *
206 * \sa SDL_SetClipboardData
207 */
208extern SDL_DECLSPEC bool SDLCALL SDL_ClearClipboardData(void);
209
210/**
211 * Get the data from clipboard for a given mime type.
212 *
213 * The size of text data does not include the terminator, but the text is
214 * guaranteed to be null terminated.
215 *
216 * \param mime_type the mime type to read from the clipboard.
217 * \param size a pointer filled in with the length of the returned data.
218 * \returns the retrieved data buffer or NULL on failure; call SDL_GetError()
219 * for more information. This should be freed with SDL_free() when it
220 * is no longer needed.
221 *
222 * \since This function is available since SDL 3.0.0.
223 *
224 * \sa SDL_HasClipboardData
225 * \sa SDL_SetClipboardData
226 */
227extern SDL_DECLSPEC void * SDLCALL SDL_GetClipboardData(const char *mime_type, size_t *size);
228
229/**
230 * Query whether there is data in the clipboard for the provided mime type.
231 *
232 * \param mime_type the mime type to check for data for.
233 * \returns true if there exists data in clipboard for the provided mime type,
234 * false if it does not.
235 *
236 * \since This function is available since SDL 3.0.0.
237 *
238 * \sa SDL_SetClipboardData
239 * \sa SDL_GetClipboardData
240 */
241extern SDL_DECLSPEC bool SDLCALL SDL_HasClipboardData(const char *mime_type);
242
243/**
244 * Retrieve the list of mime types available in the clipboard.
245 *
246 * \param num_mime_types a pointer filled with the number of mime types, may
247 * be NULL.
248 * \returns a null terminated array of strings with mime types, or NULL on
249 * failure; call SDL_GetError() for more information. This should be
250 * freed with SDL_free() when it is no longer needed.
251 *
252 * \since This function is available since SDL 3.0.0.
253 *
254 * \sa SDL_SetClipboardData
255 */
256extern SDL_DECLSPEC char ** SDLCALL SDL_GetClipboardMimeTypes(size_t *num_mime_types);
257
258/* Ends C function definitions when using C++ */
259#ifdef __cplusplus
260}
261#endif
262#include <SDL3/SDL_close_code.h>
263
264#endif /* SDL_clipboard_h_ */
char * SDL_GetPrimarySelectionText(void)
bool SDL_HasPrimarySelectionText(void)
char * SDL_GetClipboardText(void)
const void *(* SDL_ClipboardDataCallback)(void *userdata, const char *mime_type, size_t *size)
void(* SDL_ClipboardCleanupCallback)(void *userdata)
bool SDL_HasClipboardText(void)
char ** SDL_GetClipboardMimeTypes(size_t *num_mime_types)
bool SDL_SetPrimarySelectionText(const char *text)
bool SDL_ClearClipboardData(void)
bool SDL_SetClipboardText(const char *text)
bool SDL_HasClipboardData(const char *mime_type)
bool SDL_SetClipboardData(SDL_ClipboardDataCallback callback, SDL_ClipboardCleanupCallback cleanup, void *userdata, const char **mime_types, size_t num_mime_types)
void * SDL_GetClipboardData(const char *mime_type, size_t *size)
SDL_MALLOC size_t size
Definition SDL_stdinc.h:720