SDL_x11clipboard.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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. #include "../../SDL_internal.h"
  19. #if SDL_VIDEO_DRIVER_X11
  20. #include <limits.h> /* For INT_MAX */
  21. #include "SDL_events.h"
  22. #include "SDL_x11video.h"
  23. #include "SDL_timer.h"
  24. #include "SDL_x11clipboard.h"
  25. /* Get any application owned window handle for clipboard association */
  26. static Window
  27. GetWindow(_THIS)
  28. {
  29. SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
  30. /* We create an unmapped window that exists just to manage the clipboard,
  31. since X11 selection data is tied to a specific window and dies with it.
  32. We create the window on demand, so apps that don't use the clipboard
  33. don't have to keep an unnecessary resource around. */
  34. if (data->clipboard_window == None) {
  35. Display *dpy = data->display;
  36. Window parent = RootWindow(dpy, DefaultScreen(dpy));
  37. XSetWindowAttributes xattr;
  38. data->clipboard_window = X11_XCreateWindow(dpy, parent, -10, -10, 1, 1, 0,
  39. CopyFromParent, InputOnly,
  40. CopyFromParent, 0, &xattr);
  41. X11_XFlush(data->display);
  42. }
  43. return data->clipboard_window;
  44. }
  45. /* We use our own cut-buffer for intermediate storage instead of
  46. XA_CUT_BUFFER0 because their use isn't really defined for holding UTF8. */
  47. Atom
  48. X11_GetSDLCutBufferClipboardType(Display *display, enum ESDLX11ClipboardMimeType mime_type,
  49. Atom selection_type)
  50. {
  51. switch (mime_type) {
  52. case SDL_X11_CLIPBOARD_MIME_TYPE_STRING:
  53. case SDL_X11_CLIPBOARD_MIME_TYPE_TEXT_PLAIN:
  54. #ifdef X_HAVE_UTF8_STRING
  55. case SDL_X11_CLIPBOARD_MIME_TYPE_TEXT_PLAIN_UTF8:
  56. #endif
  57. case SDL_X11_CLIPBOARD_MIME_TYPE_TEXT:
  58. return X11_XInternAtom(display, selection_type == XA_PRIMARY ?
  59. "SDL_CUTBUFFER_PRIMARY_SELECTION" : "SDL_CUTBUFFER",
  60. False);
  61. default:
  62. SDL_SetError("Can't find mime_type.");
  63. return XA_STRING;
  64. }
  65. }
  66. Atom
  67. X11_GetSDLCutBufferClipboardExternalFormat(Display *display, enum ESDLX11ClipboardMimeType mime_type)
  68. {
  69. switch (mime_type) {
  70. case SDL_X11_CLIPBOARD_MIME_TYPE_STRING:
  71. /* If you don't support UTF-8, you might use XA_STRING here */
  72. #ifdef X_HAVE_UTF8_STRING
  73. return X11_XInternAtom(display, "UTF8_STRING", False);
  74. #else
  75. return XA_STRING;
  76. #endif
  77. case SDL_X11_CLIPBOARD_MIME_TYPE_TEXT_PLAIN:
  78. return X11_XInternAtom(display, "text/plain", False);
  79. #ifdef X_HAVE_UTF8_STRING
  80. case SDL_X11_CLIPBOARD_MIME_TYPE_TEXT_PLAIN_UTF8:
  81. return X11_XInternAtom(display, "text/plain;charset=utf-8", False);
  82. #endif
  83. case SDL_X11_CLIPBOARD_MIME_TYPE_TEXT:
  84. return X11_XInternAtom(display, "TEXT", False);
  85. default:
  86. SDL_SetError("Can't find mime_type.");
  87. return XA_STRING;
  88. }
  89. }
  90. Atom
  91. X11_GetSDLCutBufferClipboardInternalFormat(Display *display, enum ESDLX11ClipboardMimeType mime_type)
  92. {
  93. switch (mime_type) {
  94. case SDL_X11_CLIPBOARD_MIME_TYPE_STRING:
  95. case SDL_X11_CLIPBOARD_MIME_TYPE_TEXT_PLAIN:
  96. #ifdef X_HAVE_UTF8_STRING
  97. case SDL_X11_CLIPBOARD_MIME_TYPE_TEXT_PLAIN_UTF8:
  98. #endif
  99. case SDL_X11_CLIPBOARD_MIME_TYPE_TEXT:
  100. /* If you don't support UTF-8, you might use XA_STRING here */
  101. #ifdef X_HAVE_UTF8_STRING
  102. return X11_XInternAtom(display, "UTF8_STRING", False);
  103. #else
  104. return XA_STRING;
  105. #endif
  106. default:
  107. SDL_SetError("Can't find mime_type.");
  108. return XA_STRING;
  109. }
  110. }
  111. static int
  112. SetSelectionText(_THIS, const char *text, Atom selection_type)
  113. {
  114. Display *display = ((SDL_VideoData *) _this->driverdata)->display;
  115. Window window;
  116. /* Get the SDL window that will own the selection */
  117. window = GetWindow(_this);
  118. if (window == None) {
  119. return SDL_SetError("Couldn't find a window to own the selection");
  120. }
  121. /* Save the selection on the root window */
  122. X11_XChangeProperty(display, DefaultRootWindow(display),
  123. X11_GetSDLCutBufferClipboardType(display, SDL_X11_CLIPBOARD_MIME_TYPE_STRING, selection_type),
  124. X11_GetSDLCutBufferClipboardInternalFormat(display, SDL_X11_CLIPBOARD_MIME_TYPE_STRING), 8, PropModeReplace,
  125. (const unsigned char *)text, SDL_strlen(text));
  126. if (X11_XGetSelectionOwner(display, selection_type) != window) {
  127. X11_XSetSelectionOwner(display, selection_type, window, CurrentTime);
  128. }
  129. return 0;
  130. }
  131. static char *
  132. GetSlectionText(_THIS, Atom selection_type)
  133. {
  134. SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
  135. Display *display = videodata->display;
  136. Atom format;
  137. Window window;
  138. Window owner;
  139. Atom selection;
  140. Atom seln_type;
  141. int seln_format;
  142. unsigned long nbytes;
  143. unsigned long overflow;
  144. unsigned char *src;
  145. char *text;
  146. Uint32 waitStart;
  147. Uint32 waitElapsed;
  148. text = NULL;
  149. /* Get the window that holds the selection */
  150. window = GetWindow(_this);
  151. format = X11_GetSDLCutBufferClipboardInternalFormat(display, SDL_X11_CLIPBOARD_MIME_TYPE_STRING);
  152. owner = X11_XGetSelectionOwner(display, selection_type);
  153. if (owner == None) {
  154. /* Fall back to ancient X10 cut-buffers which do not support UTF8 strings*/
  155. owner = DefaultRootWindow(display);
  156. selection = XA_CUT_BUFFER0;
  157. format = XA_STRING;
  158. } else if (owner == window) {
  159. owner = DefaultRootWindow(display);
  160. selection = X11_GetSDLCutBufferClipboardType(display, SDL_X11_CLIPBOARD_MIME_TYPE_STRING, selection_type);
  161. } else {
  162. /* Request that the selection owner copy the data to our window */
  163. owner = window;
  164. selection = X11_XInternAtom(display, "SDL_SELECTION", False);
  165. X11_XConvertSelection(display, selection_type, format, selection, owner,
  166. CurrentTime);
  167. /* When using synergy on Linux and when data has been put in the clipboard
  168. on the remote (Windows anyway) machine then selection_waiting may never
  169. be set to False. Time out after a while. */
  170. waitStart = SDL_GetTicks();
  171. videodata->selection_waiting = SDL_TRUE;
  172. while (videodata->selection_waiting) {
  173. SDL_PumpEvents();
  174. waitElapsed = SDL_GetTicks() - waitStart;
  175. /* Wait one second for a selection response. */
  176. if (waitElapsed > 1000) {
  177. videodata->selection_waiting = SDL_FALSE;
  178. SDL_SetError("Selection timeout");
  179. /* We need to set the selection text so that next time we won't
  180. timeout, otherwise we will hang on every call to this function. */
  181. SetSelectionText(_this, "", selection_type);
  182. return SDL_strdup("");
  183. }
  184. }
  185. }
  186. if (X11_XGetWindowProperty(display, owner, selection, 0, INT_MAX/4, False,
  187. format, &seln_type, &seln_format, &nbytes, &overflow, &src)
  188. == Success) {
  189. if (seln_type == format) {
  190. text = (char *)SDL_malloc(nbytes+1);
  191. if (text) {
  192. SDL_memcpy(text, src, nbytes);
  193. text[nbytes] = '\0';
  194. }
  195. }
  196. X11_XFree(src);
  197. }
  198. if (!text) {
  199. text = SDL_strdup("");
  200. }
  201. return text;
  202. }
  203. int
  204. X11_SetClipboardText(_THIS, const char *text)
  205. {
  206. SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
  207. Atom XA_CLIPBOARD = X11_XInternAtom(videodata->display, "CLIPBOARD", 0);
  208. if (XA_CLIPBOARD == None) {
  209. return SDL_SetError("Couldn't access X clipboard");
  210. }
  211. return SetSelectionText(_this, text, XA_CLIPBOARD);
  212. }
  213. int
  214. X11_SetPrimarySelectionText(_THIS, const char *text)
  215. {
  216. return SetSelectionText(_this, text, XA_PRIMARY);
  217. }
  218. char *
  219. X11_GetClipboardText(_THIS)
  220. {
  221. SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
  222. Atom XA_CLIPBOARD = X11_XInternAtom(videodata->display, "CLIPBOARD", 0);
  223. if (XA_CLIPBOARD == None) {
  224. SDL_SetError("Couldn't access X clipboard");
  225. return SDL_strdup("");
  226. }
  227. return GetSlectionText(_this, XA_CLIPBOARD);
  228. }
  229. char *
  230. X11_GetPrimarySelectionText(_THIS)
  231. {
  232. return GetSlectionText(_this, XA_PRIMARY);
  233. }
  234. SDL_bool
  235. X11_HasClipboardText(_THIS)
  236. {
  237. SDL_bool result = SDL_FALSE;
  238. char *text = X11_GetClipboardText(_this);
  239. if (text) {
  240. result = text[0] != '\0' ? SDL_TRUE : SDL_FALSE;
  241. SDL_free(text);
  242. }
  243. return result;
  244. }
  245. SDL_bool
  246. X11_HasPrimarySelectionText(_THIS)
  247. {
  248. SDL_bool result = SDL_FALSE;
  249. char *text = X11_GetPrimarySelectionText(_this);
  250. if (text) {
  251. result = text[0] != '\0' ? SDL_TRUE : SDL_FALSE;
  252. SDL_free(text);
  253. }
  254. return result;
  255. }
  256. #endif /* SDL_VIDEO_DRIVER_X11 */
  257. /* vi: set ts=4 sw=4 expandtab: */