1 | /* |
2 | Simple DirectMedia Layer |
3 | Copyright (C) 1997-2025 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 | * ## Basic use (text) |
31 | * |
32 | * Obtaining and publishing simple text to the system clipboard is as easy as |
33 | * calling SDL_GetClipboardText() and SDL_SetClipboardText(), respectively. |
34 | * These deal with C strings in UTF-8 encoding. Data transmission and encoding |
35 | * conversion is completely managed by SDL. |
36 | * |
37 | * ## Clipboard callbacks (data other than text) |
38 | * |
39 | * Things get more complicated when the clipboard contains something other |
40 | * than text. Not only can the system clipboard contain data of any type, in |
41 | * some cases it can contain the same data in different formats! For example, |
42 | * an image painting app might let the user copy a graphic to the clipboard, |
43 | * and offers it in .BMP, .JPG, or .PNG format for other apps to consume. |
44 | * |
45 | * Obtaining clipboard data ("pasting") like this is a matter of calling |
46 | * SDL_GetClipboardData() and telling it the mimetype of the data you want. |
47 | * But how does one know if that format is available? SDL_HasClipboardData() |
48 | * can report if a specific mimetype is offered, and |
49 | * SDL_GetClipboardMimeTypes() can provide the entire list of mimetypes |
50 | * available, so the app can decide what to do with the data and what formats |
51 | * it can support. |
52 | * |
53 | * Setting the clipboard ("copying") to arbitrary data is done with |
54 | * SDL_SetClipboardData. The app does not provide the data in this call, but |
55 | * rather the mimetypes it is willing to provide and a callback function. |
56 | * During the callback, the app will generate the data. This allows massive |
57 | * data sets to be provided to the clipboard, without any data being copied |
58 | * before it is explicitly requested. More specifically, it allows an app to |
59 | * offer data in multiple formats without providing a copy of all of them |
60 | * upfront. If the app has an image that it could provide in PNG or JPG |
61 | * format, it doesn't have to encode it to either of those unless and until |
62 | * something tries to paste it. |
63 | * |
64 | * ## Primary Selection |
65 | * |
66 | * The X11 and Wayland video targets have a concept of the "primary selection" |
67 | * in addition to the usual clipboard. This is generally highlighted (but not |
68 | * explicitly copied) text from various apps. SDL offers APIs for this through |
69 | * SDL_GetPrimarySelectionText() and SDL_SetPrimarySelectionText(). SDL offers |
70 | * these APIs on platforms without this concept, too, but only so far that it |
71 | * will keep a copy of a string that the app sets for later retrieval; the |
72 | * operating system will not ever attempt to change the string externally if |
73 | * it doesn't support a primary selection. |
74 | */ |
75 | |
76 | #ifndef SDL_clipboard_h_ |
77 | #define SDL_clipboard_h_ |
78 | |
79 | #include <SDL3/SDL_stdinc.h> |
80 | #include <SDL3/SDL_error.h> |
81 | |
82 | #include <SDL3/SDL_begin_code.h> |
83 | /* Set up for C function definitions, even when using C++ */ |
84 | #ifdef __cplusplus |
85 | extern "C" { |
86 | #endif |
87 | |
88 | /* Function prototypes */ |
89 | |
90 | /** |
91 | * Put UTF-8 text into the clipboard. |
92 | * |
93 | * \param text the text to store in the clipboard. |
94 | * \returns true on success or false on failure; call SDL_GetError() for more |
95 | * information. |
96 | * |
97 | * \threadsafety This function should only be called on the main thread. |
98 | * |
99 | * \since This function is available since SDL 3.2.0. |
100 | * |
101 | * \sa SDL_GetClipboardText |
102 | * \sa SDL_HasClipboardText |
103 | */ |
104 | extern SDL_DECLSPEC bool SDLCALL SDL_SetClipboardText(const char *text); |
105 | |
106 | /** |
107 | * Get UTF-8 text from the clipboard. |
108 | * |
109 | * This functions returns an empty string if there was not enough memory left |
110 | * for a copy of the clipboard's content. |
111 | * |
112 | * \returns the clipboard text on success or an empty string on failure; call |
113 | * SDL_GetError() for more information. This should be freed with |
114 | * SDL_free() when it is no longer needed. |
115 | * |
116 | * \threadsafety This function should only be called on the main thread. |
117 | * |
118 | * \since This function is available since SDL 3.2.0. |
119 | * |
120 | * \sa SDL_HasClipboardText |
121 | * \sa SDL_SetClipboardText |
122 | */ |
123 | extern SDL_DECLSPEC char * SDLCALL SDL_GetClipboardText(void); |
124 | |
125 | /** |
126 | * Query whether the clipboard exists and contains a non-empty text string. |
127 | * |
128 | * \returns true if the clipboard has text, or false if it does not. |
129 | * |
130 | * \threadsafety This function should only be called on the main thread. |
131 | * |
132 | * \since This function is available since SDL 3.2.0. |
133 | * |
134 | * \sa SDL_GetClipboardText |
135 | * \sa SDL_SetClipboardText |
136 | */ |
137 | extern SDL_DECLSPEC bool SDLCALL SDL_HasClipboardText(void); |
138 | |
139 | /** |
140 | * Put UTF-8 text into the primary selection. |
141 | * |
142 | * \param text the text to store in the primary selection. |
143 | * \returns true on success or false on failure; call SDL_GetError() for more |
144 | * information. |
145 | * |
146 | * \threadsafety This function should only be called on the main thread. |
147 | * |
148 | * \since This function is available since SDL 3.2.0. |
149 | * |
150 | * \sa SDL_GetPrimarySelectionText |
151 | * \sa SDL_HasPrimarySelectionText |
152 | */ |
153 | extern SDL_DECLSPEC bool SDLCALL SDL_SetPrimarySelectionText(const char *text); |
154 | |
155 | /** |
156 | * Get UTF-8 text from the primary selection. |
157 | * |
158 | * This functions returns an empty string if there was not enough memory left |
159 | * for a copy of the primary selection's content. |
160 | * |
161 | * \returns the primary selection text on success or an empty string on |
162 | * failure; call SDL_GetError() for more information. This should be |
163 | * freed with SDL_free() when it is no longer needed. |
164 | * |
165 | * \threadsafety This function should only be called on the main thread. |
166 | * |
167 | * \since This function is available since SDL 3.2.0. |
168 | * |
169 | * \sa SDL_HasPrimarySelectionText |
170 | * \sa SDL_SetPrimarySelectionText |
171 | */ |
172 | extern SDL_DECLSPEC char * SDLCALL SDL_GetPrimarySelectionText(void); |
173 | |
174 | /** |
175 | * Query whether the primary selection exists and contains a non-empty text |
176 | * string. |
177 | * |
178 | * \returns true if the primary selection has text, or false if it does not. |
179 | * |
180 | * \threadsafety This function should only be called on the main thread. |
181 | * |
182 | * \since This function is available since SDL 3.2.0. |
183 | * |
184 | * \sa SDL_GetPrimarySelectionText |
185 | * \sa SDL_SetPrimarySelectionText |
186 | */ |
187 | extern SDL_DECLSPEC bool SDLCALL SDL_HasPrimarySelectionText(void); |
188 | |
189 | /** |
190 | * Callback function that will be called when data for the specified mime-type |
191 | * is requested by the OS. |
192 | * |
193 | * The callback function is called with NULL as the mime_type when the |
194 | * clipboard is cleared or new data is set. The clipboard is automatically |
195 | * cleared in SDL_Quit(). |
196 | * |
197 | * \param userdata a pointer to provided user data. |
198 | * \param mime_type the requested mime-type. |
199 | * \param size a pointer filled in with the length of the returned data. |
200 | * \returns a pointer to the data for the provided mime-type. Returning NULL |
201 | * or setting length to 0 will cause no data to be sent to the |
202 | * "receiver". It is up to the receiver to handle this. Essentially |
203 | * returning no data is more or less undefined behavior and may cause |
204 | * breakage in receiving applications. The returned data will not be |
205 | * freed so it needs to be retained and dealt with internally. |
206 | * |
207 | * \since This function is available since SDL 3.2.0. |
208 | * |
209 | * \sa SDL_SetClipboardData |
210 | */ |
211 | typedef const void *(SDLCALL *SDL_ClipboardDataCallback)(void *userdata, const char *mime_type, size_t *size); |
212 | |
213 | /** |
214 | * Callback function that will be called when the clipboard is cleared, or new |
215 | * data is set. |
216 | * |
217 | * \param userdata a pointer to provided user data. |
218 | * |
219 | * \since This function is available since SDL 3.2.0. |
220 | * |
221 | * \sa SDL_SetClipboardData |
222 | */ |
223 | typedef void (SDLCALL *SDL_ClipboardCleanupCallback)(void *userdata); |
224 | |
225 | /** |
226 | * Offer clipboard data to the OS. |
227 | * |
228 | * Tell the operating system that the application is offering clipboard data |
229 | * for each of the provided mime-types. Once another application requests the |
230 | * data the callback function will be called, allowing it to generate and |
231 | * respond with the data for the requested mime-type. |
232 | * |
233 | * The size of text data does not include any terminator, and the text does |
234 | * not need to be null terminated (e.g. you can directly copy a portion of a |
235 | * document). |
236 | * |
237 | * \param callback a function pointer to the function that provides the |
238 | * clipboard data. |
239 | * \param cleanup a function pointer to the function that cleans up the |
240 | * clipboard data. |
241 | * \param userdata an opaque pointer that will be forwarded to the callbacks. |
242 | * \param mime_types a list of mime-types that are being offered. |
243 | * \param num_mime_types the number of mime-types in the mime_types list. |
244 | * \returns true on success or false on failure; call SDL_GetError() for more |
245 | * information. |
246 | * |
247 | * \threadsafety This function should only be called on the main thread. |
248 | * |
249 | * \since This function is available since SDL 3.2.0. |
250 | * |
251 | * \sa SDL_ClearClipboardData |
252 | * \sa SDL_GetClipboardData |
253 | * \sa SDL_HasClipboardData |
254 | */ |
255 | extern SDL_DECLSPEC bool SDLCALL SDL_SetClipboardData(SDL_ClipboardDataCallback callback, SDL_ClipboardCleanupCallback cleanup, void *userdata, const char **mime_types, size_t num_mime_types); |
256 | |
257 | /** |
258 | * Clear the clipboard data. |
259 | * |
260 | * \returns true on success or false on failure; call SDL_GetError() for more |
261 | * information. |
262 | * |
263 | * \threadsafety This function should only be called on the main thread. |
264 | * |
265 | * \since This function is available since SDL 3.2.0. |
266 | * |
267 | * \sa SDL_SetClipboardData |
268 | */ |
269 | extern SDL_DECLSPEC bool SDLCALL SDL_ClearClipboardData(void); |
270 | |
271 | /** |
272 | * Get the data from clipboard for a given mime type. |
273 | * |
274 | * The size of text data does not include the terminator, but the text is |
275 | * guaranteed to be null terminated. |
276 | * |
277 | * \param mime_type the mime type to read from the clipboard. |
278 | * \param size a pointer filled in with the length of the returned data. |
279 | * \returns the retrieved data buffer or NULL on failure; call SDL_GetError() |
280 | * for more information. This should be freed with SDL_free() when it |
281 | * is no longer needed. |
282 | * |
283 | * \threadsafety This function should only be called on the main thread. |
284 | * |
285 | * \since This function is available since SDL 3.2.0. |
286 | * |
287 | * \sa SDL_HasClipboardData |
288 | * \sa SDL_SetClipboardData |
289 | */ |
290 | extern SDL_DECLSPEC void * SDLCALL SDL_GetClipboardData(const char *mime_type, size_t *size); |
291 | |
292 | /** |
293 | * Query whether there is data in the clipboard for the provided mime type. |
294 | * |
295 | * \param mime_type the mime type to check for data for. |
296 | * \returns true if there exists data in clipboard for the provided mime type, |
297 | * false if it does not. |
298 | * |
299 | * \threadsafety This function should only be called on the main thread. |
300 | * |
301 | * \since This function is available since SDL 3.2.0. |
302 | * |
303 | * \sa SDL_SetClipboardData |
304 | * \sa SDL_GetClipboardData |
305 | */ |
306 | extern SDL_DECLSPEC bool SDLCALL SDL_HasClipboardData(const char *mime_type); |
307 | |
308 | /** |
309 | * Retrieve the list of mime types available in the clipboard. |
310 | * |
311 | * \param num_mime_types a pointer filled with the number of mime types, may |
312 | * be NULL. |
313 | * \returns a null terminated array of strings with mime types, or NULL on |
314 | * failure; call SDL_GetError() for more information. This should be |
315 | * freed with SDL_free() when it is no longer needed. |
316 | * |
317 | * \threadsafety This function should only be called on the main thread. |
318 | * |
319 | * \since This function is available since SDL 3.2.0. |
320 | * |
321 | * \sa SDL_SetClipboardData |
322 | */ |
323 | extern SDL_DECLSPEC char ** SDLCALL SDL_GetClipboardMimeTypes(size_t *num_mime_types); |
324 | |
325 | /* Ends C function definitions when using C++ */ |
326 | #ifdef __cplusplus |
327 | } |
328 | #endif |
329 | #include <SDL3/SDL_close_code.h> |
330 | |
331 | #endif /* SDL_clipboard_h_ */ |
332 | |