SDL_clipboard.h 12 KB

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