SDL_clipboard.h 9.7 KB

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