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 SDL_TRUE on success or SDL_FALSE on failure; call SDL_GetError()
50 * for more 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 SDL_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 SDL_TRUE if the clipboard has text, or SDL_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 SDL_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 SDL_TRUE on success or SDL_FALSE on failure; call SDL_GetError()
93 * for more 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 SDL_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 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 SDL_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 SDL_TRUE on success or SDL_FALSE on failure; call SDL_GetError()
189 * 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 SDL_DECLSPEC SDL_bool 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 SDL_TRUE on success or SDL_FALSE on failure; call SDL_GetError()
203 * for more information.
204 *
205 * \since This function is available since SDL 3.0.0.
206 *
207 * \sa SDL_SetClipboardData
208 */
209extern SDL_DECLSPEC SDL_bool 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. This should be freed with SDL_free() when it
221 * is no longer needed.
222 *
223 * \since This function is available since SDL 3.0.0.
224 *
225 * \sa SDL_HasClipboardData
226 * \sa SDL_SetClipboardData
227 */
228extern SDL_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 SDL_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)
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_SetClipboardText(const char *text)
SDL_bool SDL_HasClipboardText(void)
void(* SDL_ClipboardCleanupCallback)(void *userdata)
SDL_bool SDL_HasPrimarySelectionText(void)
SDL_bool SDL_SetClipboardData(SDL_ClipboardDataCallback callback, SDL_ClipboardCleanupCallback cleanup, void *userdata, const char **mime_types, size_t num_mime_types)
SDL_bool SDL_ClearClipboardData(void)
SDL_bool SDL_SetPrimarySelectionText(const char *text)
void * SDL_GetClipboardData(const char *mime_type, size_t *size)
SDL_MALLOC size_t size
Definition SDL_stdinc.h:535
bool SDL_bool
Definition SDL_stdinc.h:216