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 * \file SDL_clipboard.h
24 *
25 * Include file for SDL clipboard handling
26 */
27
28#ifndef SDL_clipboard_h_
29#define SDL_clipboard_h_
30
31#include <SDL3/SDL_stdinc.h>
32#include <SDL3/SDL_error.h>
33
34#include <SDL3/SDL_begin_code.h>
35/* Set up for C function definitions, even when using C++ */
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40/* Function prototypes */
41
42/**
43 * Put UTF-8 text into the clipboard.
44 *
45 * \param text the text to store in the clipboard
46 * \returns 0 on success or a negative error code on failure; call
47 * SDL_GetError() for more information.
48 *
49 * \since This function is available since SDL 3.0.0.
50 *
51 * \sa SDL_GetClipboardText
52 * \sa SDL_HasClipboardText
53 */
54extern DECLSPEC int SDLCALL SDL_SetClipboardText(const char *text);
55
56/**
57 * Get UTF-8 text from the clipboard, which must be freed with SDL_free().
58 *
59 * This functions returns empty string if there was not enough memory left for
60 * a copy of the clipboard's content.
61 *
62 * \returns the clipboard text on success or an empty string on failure; call
63 * SDL_GetError() for more information. Caller must call SDL_free()
64 * on the returned pointer when done with it (even if there was an
65 * error).
66 *
67 * \since This function is available since SDL 3.0.0.
68 *
69 * \sa SDL_HasClipboardText
70 * \sa SDL_SetClipboardText
71 */
72extern DECLSPEC char * SDLCALL SDL_GetClipboardText(void);
73
74/**
75 * Query whether the clipboard exists and contains a non-empty text string.
76 *
77 * \returns SDL_TRUE if the clipboard has text, or SDL_FALSE if it does not.
78 *
79 * \since This function is available since SDL 3.0.0.
80 *
81 * \sa SDL_GetClipboardText
82 * \sa SDL_SetClipboardText
83 */
84extern DECLSPEC SDL_bool SDLCALL SDL_HasClipboardText(void);
85
86/**
87 * Put UTF-8 text into the primary selection.
88 *
89 * \param text the text to store in the primary selection
90 * \returns 0 on success or a negative error code on failure; call
91 * SDL_GetError() for more information.
92 *
93 * \since This function is available since SDL 3.0.0.
94 *
95 * \sa SDL_GetPrimarySelectionText
96 * \sa SDL_HasPrimarySelectionText
97 */
98extern DECLSPEC int SDLCALL SDL_SetPrimarySelectionText(const char *text);
99
100/**
101 * Get UTF-8 text from the primary selection, which must be freed with
102 * SDL_free().
103 *
104 * This functions returns empty string if there was not enough memory left for
105 * a copy of the primary selection's content.
106 *
107 * \returns the primary selection text on success or an empty string on
108 * failure; call SDL_GetError() for more information. Caller must
109 * call SDL_free() on the returned pointer when done with it (even if
110 * there was an error).
111 *
112 * \since This function is available since SDL 3.0.0.
113 *
114 * \sa SDL_HasPrimarySelectionText
115 * \sa SDL_SetPrimarySelectionText
116 */
117extern 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 SDL_TRUE if the primary selection has text, or SDL_FALSE if it
124 * does not.
125 *
126 * \since This function is available since SDL 3.0.0.
127 *
128 * \sa SDL_GetPrimarySelectionText
129 * \sa SDL_SetPrimarySelectionText
130 */
131extern DECLSPEC SDL_bool SDLCALL SDL_HasPrimarySelectionText(void);
132
133/**
134 * Callback function that will be called when data for the specified mime-type
135 * is requested by the OS.
136 *
137 * The callback function is called with NULL as the mime_type when the
138 * clipboard is cleared or new data is set. The clipboard is automatically
139 * cleared in SDL_Quit().
140 *
141 * \param userdata A pointer to provided user data
142 * \param mime_type The requested mime-type
143 * \param size A pointer filled in with the length of the returned data
144 * \returns a pointer to the data for the provided mime-type. Returning NULL
145 * or setting length to 0 will cause no data to be sent to the
146 * "receiver". It is up to the receiver to handle this. Essentially
147 * returning no data is more or less undefined behavior and may cause
148 * breakage in receiving applications. The returned data will not be
149 * freed so it needs to be retained and dealt with internally.
150 *
151 * \since This function is available since SDL 3.0.0.
152 *
153 * \sa SDL_SetClipboardData
154 */
155typedef const void *(SDLCALL *SDL_ClipboardDataCallback)(void *userdata, const char *mime_type, size_t *size);
156
157/**
158 * Callback function that will be called when the clipboard is cleared, or new
159 * data is set.
160 *
161 * \param userdata A pointer to provided user data
162 *
163 * \since This function is available since SDL 3.0.0.
164 *
165 * \sa SDL_SetClipboardData
166 */
167typedef void (SDLCALL *SDL_ClipboardCleanupCallback)(void *userdata);
168
169/**
170 * Offer clipboard data to the OS.
171 *
172 * Tell the operating system that the application is offering clipboard data
173 * for each of the proivded mime-types. Once another application requests the
174 * data the callback function will be called allowing it to generate and
175 * respond with the data for the requested mime-type.
176 *
177 * The size of text data does not include any terminator, and the text does
178 * not need to be null terminated (e.g. you can directly copy a portion of a
179 * document)
180 *
181 * \param callback A function pointer to the function that provides the
182 * clipboard data
183 * \param cleanup A function pointer to the function that cleans up the
184 * clipboard data
185 * \param userdata An opaque pointer that will be forwarded to the callbacks
186 * \param mime_types A list of mime-types that are being offered
187 * \param num_mime_types The number of mime-types in the mime_types list
188 * \returns 0 on success or a negative error code on failure; call
189 * SDL_GetError() for more information.
190 *
191 * \since This function is available since SDL 3.0.0.
192 *
193 * \sa SDL_ClearClipboardData
194 * \sa SDL_GetClipboardData
195 * \sa SDL_HasClipboardData
196 */
197extern DECLSPEC int SDLCALL SDL_SetClipboardData(SDL_ClipboardDataCallback callback, SDL_ClipboardCleanupCallback cleanup, void *userdata, const char **mime_types, size_t num_mime_types);
198
199/**
200 * Clear the clipboard data.
201 *
202 * \returns 0 on success or a negative error code on failure; call
203 * SDL_GetError() for more information.
204 *
205 * \since This function is available since SDL 3.0.0.
206 *
207 * \sa SDL_SetClipboardData
208 */
209extern DECLSPEC int SDLCALL SDL_ClearClipboardData(void);
210
211/**
212 * Get the data from clipboard for a given mime type.
213 *
214 * The size of text data does not include the terminator, but the text is
215 * guaranteed to be null terminated.
216 *
217 * \param mime_type The mime type to read from the clipboard
218 * \param size A pointer filled in with the length of the returned data
219 * \returns the retrieved data buffer or NULL on failure; call SDL_GetError()
220 * for more information. Caller must call SDL_free() on the returned
221 * pointer when done with it.
222 *
223 * \since This function is available since SDL 3.0.0.
224 *
225 * \sa SDL_HasClipboardData
226 * \sa SDL_SetClipboardData
227 */
228extern DECLSPEC void *SDLCALL SDL_GetClipboardData(const char *mime_type, size_t *size);
229
230/**
231 * Query whether there is data in the clipboard for the provided mime type.
232 *
233 * \param mime_type The mime type to check for data for
234 * \returns SDL_TRUE if there exists data in clipboard for the provided mime
235 * type, SDL_FALSE if it does not.
236 *
237 * \since This function is available since SDL 3.0.0.
238 *
239 * \sa SDL_SetClipboardData
240 * \sa SDL_GetClipboardData
241 */
242extern DECLSPEC SDL_bool SDLCALL SDL_HasClipboardData(const char *mime_type);
243
244/* Ends C function definitions when using C++ */
245#ifdef __cplusplus
246}
247#endif
248#include <SDL3/SDL_close_code.h>
249
250#endif /* SDL_clipboard_h_ */
char * SDL_GetPrimarySelectionText(void)
int SDL_SetPrimarySelectionText(const char *text)
SDL_bool SDL_HasClipboardData(const char *mime_type)
char * SDL_GetClipboardText(void)
const void *(* SDL_ClipboardDataCallback)(void *userdata, const char *mime_type, size_t *size)
SDL_bool SDL_HasClipboardText(void)
void(* SDL_ClipboardCleanupCallback)(void *userdata)
int SDL_SetClipboardText(const char *text)
int SDL_SetClipboardData(SDL_ClipboardDataCallback callback, SDL_ClipboardCleanupCallback cleanup, void *userdata, const char **mime_types, size_t num_mime_types)
SDL_bool SDL_HasPrimarySelectionText(void)
int SDL_ClearClipboardData(void)
void * SDL_GetClipboardData(const char *mime_type, size_t *size)
SDL_MALLOC size_t size
Definition SDL_stdinc.h:469
int SDL_bool
Definition SDL_stdinc.h:170