2 Simple DirectMedia Layer
3 Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
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.
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:
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.
21 #include "../../SDL_internal.h"
23 #if SDL_VIDEO_DRIVER_X11
25 #include <limits.h> /* For INT_MAX */
27 #include "SDL_events.h"
28 #include "SDL_x11video.h"
29 #include "SDL_timer.h"
32 /* If you don't support UTF-8, you might use XA_STRING here */
33 #ifdef X_HAVE_UTF8_STRING
34 #define TEXT_FORMAT X11_XInternAtom(display, "UTF8_STRING", False)
36 #define TEXT_FORMAT XA_STRING
39 /* Get any application owned window handle for clipboard association */
45 window = _this->windows;
47 return ((SDL_WindowData *) window->driverdata)->xwindow;
52 /* We use our own cut-buffer for intermediate storage instead of
53 XA_CUT_BUFFER0 because their use isn't really defined for holding UTF8. */
55 X11_GetSDLCutBufferClipboardType(Display *display)
57 return X11_XInternAtom(display, "SDL_CUTBUFFER", False);
61 X11_SetClipboardText(_THIS, const char *text)
63 Display *display = ((SDL_VideoData *) _this->driverdata)->display;
66 Atom XA_CLIPBOARD = X11_XInternAtom(display, "CLIPBOARD", 0);
68 /* Get the SDL window that will own the selection */
69 window = GetWindow(_this);
71 return SDL_SetError("Couldn't find a window to own the selection");
74 /* Save the selection on the root window */
76 X11_XChangeProperty(display, DefaultRootWindow(display),
77 X11_GetSDLCutBufferClipboardType(display), format, 8, PropModeReplace,
78 (const unsigned char *)text, SDL_strlen(text));
80 if (XA_CLIPBOARD != None &&
81 X11_XGetSelectionOwner(display, XA_CLIPBOARD) != window) {
82 X11_XSetSelectionOwner(display, XA_CLIPBOARD, window, CurrentTime);
85 if (X11_XGetSelectionOwner(display, XA_PRIMARY) != window) {
86 X11_XSetSelectionOwner(display, XA_PRIMARY, window, CurrentTime);
92 X11_GetClipboardText(_THIS)
94 SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
95 Display *display = videodata->display;
102 unsigned long nbytes;
103 unsigned long overflow;
108 Atom XA_CLIPBOARD = X11_XInternAtom(display, "CLIPBOARD", 0);
109 if (XA_CLIPBOARD == None) {
110 SDL_SetError("Couldn't access X clipboard");
111 return SDL_strdup("");
116 /* Get the window that holds the selection */
117 window = GetWindow(_this);
118 format = TEXT_FORMAT;
119 owner = X11_XGetSelectionOwner(display, XA_CLIPBOARD);
121 /* Fall back to ancient X10 cut-buffers which do not support UTF8 strings*/
122 owner = DefaultRootWindow(display);
123 selection = XA_CUT_BUFFER0;
125 } else if (owner == window) {
126 owner = DefaultRootWindow(display);
127 selection = X11_GetSDLCutBufferClipboardType(display);
129 /* Request that the selection owner copy the data to our window */
131 selection = X11_XInternAtom(display, "SDL_SELECTION", False);
132 X11_XConvertSelection(display, XA_CLIPBOARD, format, selection, owner,
135 /* When using synergy on Linux and when data has been put in the clipboard
136 on the remote (Windows anyway) machine then selection_waiting may never
137 be set to False. Time out after a while. */
138 waitStart = SDL_GetTicks();
139 videodata->selection_waiting = SDL_TRUE;
140 while (videodata->selection_waiting) {
142 waitElapsed = SDL_GetTicks() - waitStart;
143 /* Wait one second for a clipboard response. */
144 if (waitElapsed > 1000) {
145 videodata->selection_waiting = SDL_FALSE;
146 SDL_SetError("Clipboard timeout");
147 /* We need to set the clipboard text so that next time we won't
148 timeout, otherwise we will hang on every call to this function. */
149 X11_SetClipboardText(_this, "");
150 return SDL_strdup("");
155 if (X11_XGetWindowProperty(display, owner, selection, 0, INT_MAX/4, False,
156 format, &seln_type, &seln_format, &nbytes, &overflow, &src)
158 if (seln_type == format) {
159 text = (char *)SDL_malloc(nbytes+1);
161 SDL_memcpy(text, src, nbytes);
169 text = SDL_strdup("");
176 X11_HasClipboardText(_THIS)
178 SDL_bool result = SDL_FALSE;
179 char *text = X11_GetClipboardText(_this);
181 result = text[0] != '\0' ? SDL_TRUE : SDL_FALSE;
187 #endif /* SDL_VIDEO_DRIVER_X11 */
189 /* vi: set ts=4 sw=4 expandtab: */