Path: blob/master/thirdparty/sdl/include/SDL3/SDL_clipboard.h
9912 views
/*1Simple DirectMedia Layer2Copyright (C) 1997-2025 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/2021/**22* # CategoryClipboard23*24* SDL provides access to the system clipboard, both for reading information25* from other processes and publishing information of its own.26*27* This is not just text! SDL apps can access and publish data by mimetype.28*29* ## Basic use (text)30*31* Obtaining and publishing simple text to the system clipboard is as easy as32* calling SDL_GetClipboardText() and SDL_SetClipboardText(), respectively.33* These deal with C strings in UTF-8 encoding. Data transmission and encoding34* conversion is completely managed by SDL.35*36* ## Clipboard callbacks (data other than text)37*38* Things get more complicated when the clipboard contains something other39* than text. Not only can the system clipboard contain data of any type, in40* some cases it can contain the same data in different formats! For example,41* an image painting app might let the user copy a graphic to the clipboard,42* and offers it in .BMP, .JPG, or .PNG format for other apps to consume.43*44* Obtaining clipboard data ("pasting") like this is a matter of calling45* SDL_GetClipboardData() and telling it the mimetype of the data you want.46* But how does one know if that format is available? SDL_HasClipboardData()47* can report if a specific mimetype is offered, and48* SDL_GetClipboardMimeTypes() can provide the entire list of mimetypes49* available, so the app can decide what to do with the data and what formats50* it can support.51*52* Setting the clipboard ("copying") to arbitrary data is done with53* SDL_SetClipboardData. The app does not provide the data in this call, but54* rather the mimetypes it is willing to provide and a callback function.55* During the callback, the app will generate the data. This allows massive56* data sets to be provided to the clipboard, without any data being copied57* before it is explicitly requested. More specifically, it allows an app to58* offer data in multiple formats without providing a copy of all of them59* upfront. If the app has an image that it could provide in PNG or JPG60* format, it doesn't have to encode it to either of those unless and until61* something tries to paste it.62*63* ## Primary Selection64*65* The X11 and Wayland video targets have a concept of the "primary selection"66* in addition to the usual clipboard. This is generally highlighted (but not67* explicitly copied) text from various apps. SDL offers APIs for this through68* SDL_GetPrimarySelectionText() and SDL_SetPrimarySelectionText(). SDL offers69* these APIs on platforms without this concept, too, but only so far that it70* will keep a copy of a string that the app sets for later retrieval; the71* operating system will not ever attempt to change the string externally if72* it doesn't support a primary selection.73*/7475#ifndef SDL_clipboard_h_76#define SDL_clipboard_h_7778#include <SDL3/SDL_stdinc.h>79#include <SDL3/SDL_error.h>8081#include <SDL3/SDL_begin_code.h>82/* Set up for C function definitions, even when using C++ */83#ifdef __cplusplus84extern "C" {85#endif8687/* Function prototypes */8889/**90* Put UTF-8 text into the clipboard.91*92* \param text the text to store in the clipboard.93* \returns true on success or false on failure; call SDL_GetError() for more94* information.95*96* \threadsafety This function should only be called on the main thread.97*98* \since This function is available since SDL 3.2.0.99*100* \sa SDL_GetClipboardText101* \sa SDL_HasClipboardText102*/103extern SDL_DECLSPEC bool SDLCALL SDL_SetClipboardText(const char *text);104105/**106* Get UTF-8 text from the clipboard.107*108* This functions returns an empty string if there was not enough memory left109* for a copy of the clipboard's content.110*111* \returns the clipboard text on success or an empty string on failure; call112* SDL_GetError() for more information. This should be freed with113* SDL_free() when it is no longer needed.114*115* \threadsafety This function should only be called on the main thread.116*117* \since This function is available since SDL 3.2.0.118*119* \sa SDL_HasClipboardText120* \sa SDL_SetClipboardText121*/122extern SDL_DECLSPEC char * SDLCALL SDL_GetClipboardText(void);123124/**125* Query whether the clipboard exists and contains a non-empty text string.126*127* \returns true if the clipboard has text, or false if it does not.128*129* \threadsafety This function should only be called on the main thread.130*131* \since This function is available since SDL 3.2.0.132*133* \sa SDL_GetClipboardText134* \sa SDL_SetClipboardText135*/136extern SDL_DECLSPEC bool SDLCALL SDL_HasClipboardText(void);137138/**139* Put UTF-8 text into the primary selection.140*141* \param text the text to store in the primary selection.142* \returns true on success or false on failure; call SDL_GetError() for more143* information.144*145* \threadsafety This function should only be called on the main thread.146*147* \since This function is available since SDL 3.2.0.148*149* \sa SDL_GetPrimarySelectionText150* \sa SDL_HasPrimarySelectionText151*/152extern SDL_DECLSPEC bool SDLCALL SDL_SetPrimarySelectionText(const char *text);153154/**155* Get UTF-8 text from the primary selection.156*157* This functions returns an empty string if there was not enough memory left158* for a copy of the primary selection's content.159*160* \returns the primary selection text on success or an empty string on161* failure; call SDL_GetError() for more information. This should be162* freed with SDL_free() when it is no longer needed.163*164* \threadsafety This function should only be called on the main thread.165*166* \since This function is available since SDL 3.2.0.167*168* \sa SDL_HasPrimarySelectionText169* \sa SDL_SetPrimarySelectionText170*/171extern SDL_DECLSPEC char * SDLCALL SDL_GetPrimarySelectionText(void);172173/**174* Query whether the primary selection exists and contains a non-empty text175* string.176*177* \returns true if the primary selection has text, or false if it does not.178*179* \threadsafety This function should only be called on the main thread.180*181* \since This function is available since SDL 3.2.0.182*183* \sa SDL_GetPrimarySelectionText184* \sa SDL_SetPrimarySelectionText185*/186extern SDL_DECLSPEC bool SDLCALL SDL_HasPrimarySelectionText(void);187188/**189* Callback function that will be called when data for the specified mime-type190* is requested by the OS.191*192* The callback function is called with NULL as the mime_type when the193* clipboard is cleared or new data is set. The clipboard is automatically194* cleared in SDL_Quit().195*196* \param userdata a pointer to provided user data.197* \param mime_type the requested mime-type.198* \param size a pointer filled in with the length of the returned data.199* \returns a pointer to the data for the provided mime-type. Returning NULL200* or setting length to 0 will cause no data to be sent to the201* "receiver". It is up to the receiver to handle this. Essentially202* returning no data is more or less undefined behavior and may cause203* breakage in receiving applications. The returned data will not be204* freed so it needs to be retained and dealt with internally.205*206* \since This function is available since SDL 3.2.0.207*208* \sa SDL_SetClipboardData209*/210typedef const void *(SDLCALL *SDL_ClipboardDataCallback)(void *userdata, const char *mime_type, size_t *size);211212/**213* Callback function that will be called when the clipboard is cleared, or new214* data is set.215*216* \param userdata a pointer to provided user data.217*218* \since This function is available since SDL 3.2.0.219*220* \sa SDL_SetClipboardData221*/222typedef void (SDLCALL *SDL_ClipboardCleanupCallback)(void *userdata);223224/**225* Offer clipboard data to the OS.226*227* Tell the operating system that the application is offering clipboard data228* for each of the provided mime-types. Once another application requests the229* data the callback function will be called, allowing it to generate and230* respond with the data for the requested mime-type.231*232* The size of text data does not include any terminator, and the text does233* not need to be null terminated (e.g. you can directly copy a portion of a234* document).235*236* \param callback a function pointer to the function that provides the237* clipboard data.238* \param cleanup a function pointer to the function that cleans up the239* clipboard data.240* \param userdata an opaque pointer that will be forwarded to the callbacks.241* \param mime_types a list of mime-types that are being offered.242* \param num_mime_types the number of mime-types in the mime_types list.243* \returns true on success or false on failure; call SDL_GetError() for more244* information.245*246* \threadsafety This function should only be called on the main thread.247*248* \since This function is available since SDL 3.2.0.249*250* \sa SDL_ClearClipboardData251* \sa SDL_GetClipboardData252* \sa SDL_HasClipboardData253*/254extern SDL_DECLSPEC bool SDLCALL SDL_SetClipboardData(SDL_ClipboardDataCallback callback, SDL_ClipboardCleanupCallback cleanup, void *userdata, const char **mime_types, size_t num_mime_types);255256/**257* Clear the clipboard data.258*259* \returns true on success or false on failure; call SDL_GetError() for more260* information.261*262* \threadsafety This function should only be called on the main thread.263*264* \since This function is available since SDL 3.2.0.265*266* \sa SDL_SetClipboardData267*/268extern SDL_DECLSPEC bool SDLCALL SDL_ClearClipboardData(void);269270/**271* Get the data from clipboard for a given mime type.272*273* The size of text data does not include the terminator, but the text is274* guaranteed to be null terminated.275*276* \param mime_type the mime type to read from the clipboard.277* \param size a pointer filled in with the length of the returned data.278* \returns the retrieved data buffer or NULL on failure; call SDL_GetError()279* for more information. This should be freed with SDL_free() when it280* is no longer needed.281*282* \threadsafety This function should only be called on the main thread.283*284* \since This function is available since SDL 3.2.0.285*286* \sa SDL_HasClipboardData287* \sa SDL_SetClipboardData288*/289extern SDL_DECLSPEC void * SDLCALL SDL_GetClipboardData(const char *mime_type, size_t *size);290291/**292* Query whether there is data in the clipboard for the provided mime type.293*294* \param mime_type the mime type to check for data for.295* \returns true if there exists data in clipboard for the provided mime type,296* false if it does not.297*298* \threadsafety This function should only be called on the main thread.299*300* \since This function is available since SDL 3.2.0.301*302* \sa SDL_SetClipboardData303* \sa SDL_GetClipboardData304*/305extern SDL_DECLSPEC bool SDLCALL SDL_HasClipboardData(const char *mime_type);306307/**308* Retrieve the list of mime types available in the clipboard.309*310* \param num_mime_types a pointer filled with the number of mime types, may311* be NULL.312* \returns a null terminated array of strings with mime types, or NULL on313* failure; call SDL_GetError() for more information. This should be314* freed with SDL_free() when it is no longer needed.315*316* \threadsafety This function should only be called on the main thread.317*318* \since This function is available since SDL 3.2.0.319*320* \sa SDL_SetClipboardData321*/322extern SDL_DECLSPEC char ** SDLCALL SDL_GetClipboardMimeTypes(size_t *num_mime_types);323324/* Ends C function definitions when using C++ */325#ifdef __cplusplus326}327#endif328#include <SDL3/SDL_close_code.h>329330#endif /* SDL_clipboard_h_ */331332333