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 * \threadsafety You may only call this function from the main thread.
53 *
54 * \since This function is available since SDL 3.1.3.
55 *
56 * \sa SDL_GetClipboardText
57 * \sa SDL_HasClipboardText
58 */
59extern SDL_DECLSPEC bool SDLCALL SDL_SetClipboardText(const char *text);
60
61/**
62 * Get UTF-8 text from the clipboard.
63 *
64 * This functions returns an empty string if there was not enough memory left
65 * for a copy of the clipboard's content.
66 *
67 * \returns the clipboard text on success or an empty string on failure; call
68 * SDL_GetError() for more information. This should be freed with
69 * SDL_free() when it is no longer needed.
70 *
71 * \threadsafety You may only call this function from the main thread.
72 *
73 * \since This function is available since SDL 3.1.3.
74 *
75 * \sa SDL_HasClipboardText
76 * \sa SDL_SetClipboardText
77 */
78extern SDL_DECLSPEC char * SDLCALL SDL_GetClipboardText(void);
79
80/**
81 * Query whether the clipboard exists and contains a non-empty text string.
82 *
83 * \returns true if the clipboard has text, or false if it does not.
84 *
85 * \threadsafety You may only call this function from the main thread.
86 *
87 * \since This function is available since SDL 3.1.3.
88 *
89 * \sa SDL_GetClipboardText
90 * \sa SDL_SetClipboardText
91 */
92extern SDL_DECLSPEC bool SDLCALL SDL_HasClipboardText(void);
93
94/**
95 * Put UTF-8 text into the primary selection.
96 *
97 * \param text the text to store in the primary selection.
98 * \returns true on success or false on failure; call SDL_GetError() for more
99 * information.
100 *
101 * \threadsafety You may only call this function from the main thread.
102 *
103 * \since This function is available since SDL 3.1.3.
104 *
105 * \sa SDL_GetPrimarySelectionText
106 * \sa SDL_HasPrimarySelectionText
107 */
108extern SDL_DECLSPEC bool SDLCALL SDL_SetPrimarySelectionText(const char *text);
109
110/**
111 * Get UTF-8 text from the primary selection.
112 *
113 * This functions returns an empty string if there was not enough memory left
114 * for a copy of the primary selection's content.
115 *
116 * \returns the primary selection text on success or an empty string on
117 * failure; call SDL_GetError() for more information. This should be
118 * freed with SDL_free() when it is no longer needed.
119 *
120 * \threadsafety You may only call this function from the main thread.
121 *
122 * \since This function is available since SDL 3.1.3.
123 *
124 * \sa SDL_HasPrimarySelectionText
125 * \sa SDL_SetPrimarySelectionText
126 */
127extern SDL_DECLSPEC char * SDLCALL SDL_GetPrimarySelectionText(void);
128
129/**
130 * Query whether the primary selection exists and contains a non-empty text
131 * string.
132 *
133 * \returns true if the primary selection has text, or false if it does not.
134 *
135 * \threadsafety You may only call this function from the main thread.
136 *
137 * \since This function is available since SDL 3.1.3.
138 *
139 * \sa SDL_GetPrimarySelectionText
140 * \sa SDL_SetPrimarySelectionText
141 */
142extern SDL_DECLSPEC bool SDLCALL SDL_HasPrimarySelectionText(void);
143
144/**
145 * Callback function that will be called when data for the specified mime-type
146 * is requested by the OS.
147 *
148 * The callback function is called with NULL as the mime_type when the
149 * clipboard is cleared or new data is set. The clipboard is automatically
150 * cleared in SDL_Quit().
151 *
152 * \param userdata a pointer to provided user data.
153 * \param mime_type the requested mime-type.
154 * \param size a pointer filled in with the length of the returned data.
155 * \returns a pointer to the data for the provided mime-type. Returning NULL
156 * or setting length to 0 will cause no data to be sent to the
157 * "receiver". It is up to the receiver to handle this. Essentially
158 * returning no data is more or less undefined behavior and may cause
159 * breakage in receiving applications. The returned data will not be
160 * freed so it needs to be retained and dealt with internally.
161 *
162 * \since This function is available since SDL 3.1.3.
163 *
164 * \sa SDL_SetClipboardData
165 */
166typedef const void *(SDLCALL *SDL_ClipboardDataCallback)(void *userdata, const char *mime_type, size_t *size);
167
168/**
169 * Callback function that will be called when the clipboard is cleared, or new
170 * data is set.
171 *
172 * \param userdata a pointer to provided user data.
173 *
174 * \since This function is available since SDL 3.1.3.
175 *
176 * \sa SDL_SetClipboardData
177 */
178typedef void (SDLCALL *SDL_ClipboardCleanupCallback)(void *userdata);
179
180/**
181 * Offer clipboard data to the OS.
182 *
183 * Tell the operating system that the application is offering clipboard data
184 * for each of the proivded mime-types. Once another application requests the
185 * data the callback function will be called allowing it to generate and
186 * respond with the data for the requested mime-type.
187 *
188 * The size of text data does not include any terminator, and the text does
189 * not need to be null terminated (e.g. you can directly copy a portion of a
190 * document)
191 *
192 * \param callback a function pointer to the function that provides the
193 * clipboard data.
194 * \param cleanup a function pointer to the function that cleans up the
195 * clipboard data.
196 * \param userdata an opaque pointer that will be forwarded to the callbacks.
197 * \param mime_types a list of mime-types that are being offered.
198 * \param num_mime_types the number of mime-types in the mime_types list.
199 * \returns true on success or false on failure; call SDL_GetError() for more
200 * information.
201 *
202 * \threadsafety You may only call this function from the main thread.
203 *
204 * \since This function is available since SDL 3.1.3.
205 *
206 * \sa SDL_ClearClipboardData
207 * \sa SDL_GetClipboardData
208 * \sa SDL_HasClipboardData
209 */
210extern SDL_DECLSPEC bool SDLCALL SDL_SetClipboardData(SDL_ClipboardDataCallback callback, SDL_ClipboardCleanupCallback cleanup, void *userdata, const char **mime_types, size_t num_mime_types);
211
212/**
213 * Clear the clipboard data.
214 *
215 * \returns true on success or false on failure; call SDL_GetError() for more
216 * information.
217 *
218 * \threadsafety You may only call this function from the main thread.
219 *
220 * \since This function is available since SDL 3.1.3.
221 *
222 * \sa SDL_SetClipboardData
223 */
224extern SDL_DECLSPEC bool SDLCALL SDL_ClearClipboardData(void);
225
226/**
227 * Get the data from clipboard for a given mime type.
228 *
229 * The size of text data does not include the terminator, but the text is
230 * guaranteed to be null terminated.
231 *
232 * \param mime_type the mime type to read from the clipboard.
233 * \param size a pointer filled in with the length of the returned data.
234 * \returns the retrieved data buffer or NULL on failure; call SDL_GetError()
235 * for more information. This should be freed with SDL_free() when it
236 * is no longer needed.
237 *
238 * \threadsafety You may only call this function from the main thread.
239 *
240 * \since This function is available since SDL 3.1.3.
241 *
242 * \sa SDL_HasClipboardData
243 * \sa SDL_SetClipboardData
244 */
245extern SDL_DECLSPEC void * SDLCALL SDL_GetClipboardData(const char *mime_type, size_t *size);
246
247/**
248 * Query whether there is data in the clipboard for the provided mime type.
249 *
250 * \param mime_type the mime type to check for data for.
251 * \returns true if there exists data in clipboard for the provided mime type,
252 * false if it does not.
253 *
254 * \threadsafety You may only call this function from the main thread.
255 *
256 * \since This function is available since SDL 3.1.3.
257 *
258 * \sa SDL_SetClipboardData
259 * \sa SDL_GetClipboardData
260 */
261extern SDL_DECLSPEC bool SDLCALL SDL_HasClipboardData(const char *mime_type);
262
263/**
264 * Retrieve the list of mime types available in the clipboard.
265 *
266 * \param num_mime_types a pointer filled with the number of mime types, may
267 * be NULL.
268 * \returns a null terminated array of strings with mime types, or NULL on
269 * failure; call SDL_GetError() for more information. This should be
270 * freed with SDL_free() when it is no longer needed.
271 *
272 * \threadsafety You may only call this function from the main thread.
273 *
274 * \since This function is available since SDL 3.1.3.
275 *
276 * \sa SDL_SetClipboardData
277 */
278extern SDL_DECLSPEC char ** SDLCALL SDL_GetClipboardMimeTypes(size_t *num_mime_types);
279
280/* Ends C function definitions when using C++ */
281#ifdef __cplusplus
282}
283#endif
284#include <SDL3/SDL_close_code.h>
285
286#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:734