4508
|
1 /*
|
|
2 SDL - Simple DirectMedia Layer
|
|
3 Copyright (C) 1997-2010 Sam Lantinga
|
|
4
|
|
5 This library is free software; you can redistribute it and/or
|
|
6 modify it under the terms of the GNU Lesser General Public
|
|
7 License as published by the Free Software Foundation; either
|
|
8 version 2.1 of the License, or (at your option) any later version.
|
|
9
|
|
10 This library is distributed in the hope that it will be useful,
|
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
13 Lesser General Public License for more details.
|
|
14
|
|
15 You should have received a copy of the GNU Lesser General Public
|
|
16 License along with this library; if not, write to the Free Software
|
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
18
|
|
19 Sam Lantinga
|
|
20 slouken@libsdl.org
|
|
21 */
|
|
22 #include "SDL_config.h"
|
|
23
|
|
24 #include <limits.h> /* For INT_MAX */
|
|
25
|
|
26 #include "SDL_events.h"
|
|
27 #include "SDL_x11video.h"
|
|
28
|
|
29
|
|
30 /* Get any application owned window handle for clipboard association */
|
|
31 static Window
|
|
32 GetWindow(_THIS)
|
|
33 {
|
|
34 SDL_VideoDisplay *display;
|
|
35 SDL_Window *window;
|
|
36
|
|
37 display = _this->displays;
|
|
38 if (display) {
|
|
39 window = display->windows;
|
|
40 if (window) {
|
|
41 return ((SDL_WindowData *) window->driverdata)->xwindow;
|
|
42 }
|
|
43 }
|
|
44 return None;
|
|
45 }
|
|
46
|
|
47 int
|
|
48 X11_SetClipboardText(_THIS, const char *text)
|
|
49 {
|
|
50 Display *display = ((SDL_VideoData *) _this->driverdata)->display;
|
|
51 Atom format;
|
|
52 Window window;
|
|
53
|
|
54 /* Get the SDL window that will own the selection */
|
|
55 window = GetWindow(_this);
|
|
56 if (window == None) {
|
|
57 SDL_SetError("Couldn't find a window to own the selection");
|
|
58 return -1;
|
|
59 }
|
|
60
|
|
61 /* If you don't support UTF-8, you might use XA_STRING here */
|
|
62 format = XInternAtom(display, "UTF8_STRING", False);
|
|
63 XChangeProperty(display, DefaultRootWindow(display),
|
|
64 XA_CUT_BUFFER0, format, 8, PropModeReplace,
|
|
65 (const unsigned char *)text, SDL_strlen(text));
|
|
66
|
|
67 if (XGetSelectionOwner(display, XA_PRIMARY) != window) {
|
|
68 XSetSelectionOwner(display, XA_PRIMARY, window, CurrentTime);
|
|
69 }
|
|
70 return 0;
|
|
71 }
|
|
72
|
|
73 char *
|
|
74 X11_GetClipboardText(_THIS)
|
|
75 {
|
|
76 SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
|
|
77 Display *display = videodata->display;
|
|
78 Atom format;
|
|
79 Window window;
|
|
80 Window owner;
|
|
81 Atom selection;
|
|
82 Atom seln_type;
|
|
83 int seln_format;
|
|
84 unsigned long nbytes;
|
|
85 unsigned long overflow;
|
|
86 unsigned char *src;
|
|
87 char *text;
|
|
88
|
|
89 text = NULL;
|
|
90
|
|
91 /* Get the SDL window that will own the selection */
|
|
92 window = GetWindow(_this);
|
|
93
|
|
94 /* If you don't support UTF-8, you might use XA_STRING here */
|
|
95 format = XInternAtom(display, "UTF8_STRING", False);
|
|
96
|
|
97 owner = XGetSelectionOwner(display, XA_PRIMARY);
|
|
98 if ((owner == None) || (owner == window)) {
|
|
99 owner = DefaultRootWindow(display);
|
|
100 selection = XA_CUT_BUFFER0;
|
|
101 } else {
|
|
102 /* Request that the selection owner copy the data to our window */
|
|
103 owner = window;
|
|
104 selection = XInternAtom(display, "SDL_SELECTION", False);
|
|
105 XConvertSelection(display, XA_PRIMARY, format, selection, owner,
|
|
106 CurrentTime);
|
|
107
|
|
108 /* FIXME: Should we have a timeout here? */
|
|
109 videodata->selection_waiting = SDL_TRUE;
|
|
110 while (videodata->selection_waiting) {
|
|
111 SDL_PumpEvents();
|
|
112 }
|
|
113 }
|
|
114
|
|
115 if (XGetWindowProperty(display, owner, selection, 0, INT_MAX/4, False,
|
|
116 format, &seln_type, &seln_format, &nbytes, &overflow, &src)
|
|
117 == Success) {
|
|
118 if (seln_type == format) {
|
|
119 text = (char *)SDL_malloc(nbytes+1);
|
|
120 if (text) {
|
|
121 SDL_memcpy(text, src, nbytes);
|
|
122 text[nbytes] = '\0';
|
|
123 }
|
|
124 }
|
|
125 XFree(src);
|
|
126 }
|
|
127
|
|
128 if (!text) {
|
|
129 text = SDL_strdup("");
|
|
130 }
|
|
131 return text;
|
|
132 }
|
|
133
|
|
134 SDL_bool
|
|
135 X11_HasClipboardText(_THIS)
|
|
136 {
|
|
137 /* Not an easy way to tell with X11, as far as I know... */
|
|
138 char *text;
|
|
139 SDL_bool retval;
|
|
140
|
|
141 text = X11_GetClipboardText(_this);
|
|
142 if (*text) {
|
|
143 retval = SDL_TRUE;
|
|
144 } else {
|
|
145 retval = SDL_FALSE;
|
|
146 }
|
|
147 SDL_free(text);
|
|
148
|
|
149 return retval;
|
|
150 }
|
|
151
|
|
152 /* vi: set ts=4 sw=4 expandtab: */
|