1 |
/* -*- c-basic-offset: 8 -*- |
2 |
rdesktop: A Remote Desktop Protocol client. |
3 |
Protocol services - Clipboard functions |
4 |
Copyright (C) Erik Forsberg <forsberg@cendio.se> 2003 |
5 |
Copyright (C) Matthew Chapman 2003 |
6 |
|
7 |
This program is free software; you can redistribute it and/or modify |
8 |
it under the terms of the GNU General Public License as published by |
9 |
the Free Software Foundation; either version 2 of the License, or |
10 |
(at your option) any later version. |
11 |
|
12 |
This program is distributed in the hope that it will be useful, |
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
GNU General Public License for more details. |
16 |
|
17 |
You should have received a copy of the GNU General Public License |
18 |
along with this program; if not, write to the Free Software |
19 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 |
*/ |
21 |
|
22 |
#include <X11/Xlib.h> |
23 |
#include <X11/Xatom.h> |
24 |
#include "rdesktop.h" |
25 |
|
26 |
#define NUM_TARGETS 6 |
27 |
|
28 |
extern Display *display; |
29 |
extern Window wnd; |
30 |
extern Time last_gesturetime; |
31 |
|
32 |
static Atom clipboard_atom, primary_atom, targets_atom, timestamp_atom; |
33 |
static Atom rdesktop_clipboard_target_atom, rdesktop_clipboard_formats_atom, incr_atom; |
34 |
static XSelectionRequestEvent selection_request; |
35 |
static Atom targets[NUM_TARGETS]; |
36 |
static int have_primary = 0; |
37 |
static int rdesktop_is_selection_owner = 0; |
38 |
|
39 |
static void |
40 |
xclip_provide_selection(XSelectionRequestEvent * req, Atom type, unsigned int format, uint8 * data, |
41 |
uint32 length) |
42 |
{ |
43 |
XEvent xev; |
44 |
|
45 |
XChangeProperty(display, req->requestor, req->property, |
46 |
type, format, PropModeReplace, data, length); |
47 |
|
48 |
xev.xselection.type = SelectionNotify; |
49 |
xev.xselection.serial = 0; |
50 |
xev.xselection.send_event = True; |
51 |
xev.xselection.requestor = req->requestor; |
52 |
xev.xselection.selection = req->selection; |
53 |
xev.xselection.target = req->target; |
54 |
xev.xselection.property = req->property; |
55 |
xev.xselection.time = req->time; |
56 |
XSendEvent(display, req->requestor, False, NoEventMask, &xev); |
57 |
} |
58 |
|
59 |
void |
60 |
xclip_handle_SelectionNotify(XSelectionEvent * event) |
61 |
{ |
62 |
unsigned long nitems, bytes_left; |
63 |
Atom type, best_target, text_target; |
64 |
Atom *supported_targets; |
65 |
int res, i, format; |
66 |
uint8 *data; |
67 |
|
68 |
if (event->property == None) |
69 |
goto fail; |
70 |
|
71 |
DEBUG_CLIPBOARD(("xclip_handle_SelectionNotify: selection=%s, target=%s, property=%s\n", |
72 |
XGetAtomName(display, event->selection), |
73 |
XGetAtomName(display, event->target), |
74 |
XGetAtomName(display, event->property))); |
75 |
|
76 |
if (event->property == None) |
77 |
goto fail; |
78 |
|
79 |
res = XGetWindowProperty(display, wnd, rdesktop_clipboard_target_atom, |
80 |
0, XMaxRequestSize(display), True, AnyPropertyType, |
81 |
&type, &format, &nitems, &bytes_left, &data); |
82 |
|
83 |
if (res != Success) |
84 |
{ |
85 |
DEBUG_CLIPBOARD(("XGetWindowProperty failed!\n")); |
86 |
goto fail; |
87 |
} |
88 |
|
89 |
if (event->target == targets_atom) |
90 |
{ |
91 |
/* FIXME: We should choose format here based on what the server wanted */ |
92 |
best_target = XA_STRING; |
93 |
if (type != None) |
94 |
{ |
95 |
supported_targets = (Atom *) data; |
96 |
text_target = XInternAtom(display, "TEXT", False); |
97 |
for (i = 0; i < nitems; i++) |
98 |
{ |
99 |
DEBUG_CLIPBOARD(("Target %d: %s\n", i, |
100 |
XGetAtomName(display, supported_targets[i]))); |
101 |
if (supported_targets[i] == text_target) |
102 |
{ |
103 |
DEBUG_CLIPBOARD(("Other party supports TEXT, choosing that as best_target\n")); |
104 |
best_target = text_target; |
105 |
} |
106 |
} |
107 |
XFree(data); |
108 |
} |
109 |
|
110 |
XConvertSelection(display, primary_atom, best_target, |
111 |
rdesktop_clipboard_target_atom, wnd, event->time); |
112 |
return; |
113 |
} |
114 |
|
115 |
if (type == incr_atom) |
116 |
{ |
117 |
warning("We don't support INCR transfers at this time. Try cutting less data.\n"); |
118 |
goto fail; |
119 |
} |
120 |
|
121 |
cliprdr_send_data(data, nitems + 1); |
122 |
XFree(data); |
123 |
|
124 |
if (!rdesktop_is_selection_owner) |
125 |
cliprdr_send_text_format_announce(); |
126 |
return; |
127 |
|
128 |
fail: |
129 |
cliprdr_send_data(NULL, 0); |
130 |
} |
131 |
|
132 |
void |
133 |
xclip_handle_SelectionRequest(XSelectionRequestEvent * event) |
134 |
{ |
135 |
unsigned long nitems, bytes_left; |
136 |
uint32 *wanted_format; |
137 |
int format, res; |
138 |
Atom type; |
139 |
|
140 |
DEBUG_CLIPBOARD(("xclip_handle_SelectionRequest: selection=%s, target=%s, property=%s\n", |
141 |
XGetAtomName(display, event->selection), |
142 |
XGetAtomName(display, event->target), |
143 |
XGetAtomName(display, event->property))); |
144 |
|
145 |
if (event->target == targets_atom) |
146 |
{ |
147 |
xclip_provide_selection(event, XA_ATOM, 32, (uint8 *) & targets, NUM_TARGETS); |
148 |
return; |
149 |
} |
150 |
else if (event->target == timestamp_atom) |
151 |
{ |
152 |
xclip_provide_selection(event, XA_INTEGER, 32, (uint8 *) & last_gesturetime, 1); |
153 |
return; |
154 |
} |
155 |
else if (event->target == rdesktop_clipboard_formats_atom) |
156 |
{ |
157 |
res = XGetWindowProperty(display, event->requestor, |
158 |
rdesktop_clipboard_target_atom, 0, 1, True, XA_INTEGER, |
159 |
&type, &format, &nitems, &bytes_left, |
160 |
(unsigned char **) &wanted_format); |
161 |
format = (res == Success) ? *wanted_format : CF_TEXT; |
162 |
} |
163 |
else |
164 |
{ |
165 |
format = CF_TEXT; |
166 |
} |
167 |
|
168 |
cliprdr_send_data_request(format); |
169 |
selection_request = *event; |
170 |
/* wait for data */ |
171 |
} |
172 |
|
173 |
void |
174 |
xclip_handle_SelectionClear(void) |
175 |
{ |
176 |
DEBUG_CLIPBOARD(("xclip_handle_SelectionClear\n")); |
177 |
have_primary = 0; |
178 |
XDeleteProperty(display, DefaultRootWindow(display), rdesktop_clipboard_formats_atom); |
179 |
cliprdr_send_text_format_announce(); |
180 |
} |
181 |
|
182 |
void |
183 |
xclip_handle_PropertyNotify(XPropertyEvent * event) |
184 |
{ |
185 |
unsigned long nitems, bytes_left; |
186 |
int format, res; |
187 |
uint8 *data; |
188 |
Atom type; |
189 |
|
190 |
if (event->atom != rdesktop_clipboard_formats_atom) |
191 |
return; |
192 |
|
193 |
if (have_primary) /* from us */ |
194 |
return; |
195 |
|
196 |
if (event->state == PropertyNewValue) |
197 |
{ |
198 |
res = XGetWindowProperty(display, DefaultRootWindow(display), |
199 |
rdesktop_clipboard_formats_atom, 0, |
200 |
XMaxRequestSize(display), False, XA_STRING, &type, &format, |
201 |
&nitems, &bytes_left, &data); |
202 |
|
203 |
if ((res == Success) && (nitems > 0)) |
204 |
{ |
205 |
cliprdr_send_native_format_announce(data, nitems); |
206 |
rdesktop_is_selection_owner = 1; |
207 |
return; |
208 |
} |
209 |
} |
210 |
|
211 |
/* PropertyDelete, or XGetWindowProperty failed */ |
212 |
cliprdr_send_text_format_announce(); |
213 |
rdesktop_is_selection_owner = 0; |
214 |
} |
215 |
|
216 |
|
217 |
void |
218 |
ui_clip_format_announce(char *data, uint32 length) |
219 |
{ |
220 |
XSetSelectionOwner(display, primary_atom, wnd, last_gesturetime); |
221 |
if (XGetSelectionOwner(display, primary_atom) != wnd) |
222 |
{ |
223 |
warning("Failed to aquire ownership of PRIMARY clipboard\n"); |
224 |
return; |
225 |
} |
226 |
|
227 |
have_primary = 1; |
228 |
XChangeProperty(display, DefaultRootWindow(display), |
229 |
rdesktop_clipboard_formats_atom, XA_STRING, 8, PropModeReplace, data, |
230 |
length); |
231 |
|
232 |
XSetSelectionOwner(display, clipboard_atom, wnd, last_gesturetime); |
233 |
if (XGetSelectionOwner(display, clipboard_atom) != wnd) |
234 |
warning("Failed to aquire ownership of CLIPBOARD clipboard\n"); |
235 |
} |
236 |
|
237 |
|
238 |
void |
239 |
ui_clip_handle_data(char *data, uint32 length) |
240 |
{ |
241 |
xclip_provide_selection(&selection_request, XA_STRING, 8, data, length - 1); |
242 |
} |
243 |
|
244 |
void |
245 |
ui_clip_request_data(uint32 format) |
246 |
{ |
247 |
Window selectionowner; |
248 |
|
249 |
DEBUG_CLIPBOARD(("Request from server for format %d\n", format)); |
250 |
|
251 |
if (rdesktop_is_selection_owner) |
252 |
{ |
253 |
XChangeProperty(display, wnd, rdesktop_clipboard_target_atom, |
254 |
XA_INTEGER, 32, PropModeReplace, (unsigned char *) &format, 1); |
255 |
|
256 |
XConvertSelection(display, primary_atom, rdesktop_clipboard_formats_atom, |
257 |
rdesktop_clipboard_target_atom, wnd, CurrentTime); |
258 |
return; |
259 |
} |
260 |
|
261 |
selectionowner = XGetSelectionOwner(display, primary_atom); |
262 |
if (selectionowner != None) |
263 |
{ |
264 |
XConvertSelection(display, primary_atom, targets_atom, |
265 |
rdesktop_clipboard_target_atom, wnd, CurrentTime); |
266 |
return; |
267 |
} |
268 |
|
269 |
/* No PRIMARY, try CLIPBOARD */ |
270 |
selectionowner = XGetSelectionOwner(display, clipboard_atom); |
271 |
if (selectionowner != None) |
272 |
{ |
273 |
XConvertSelection(display, clipboard_atom, targets_atom, |
274 |
rdesktop_clipboard_target_atom, wnd, CurrentTime); |
275 |
return; |
276 |
} |
277 |
|
278 |
/* No data available */ |
279 |
cliprdr_send_data(NULL, 0); |
280 |
} |
281 |
|
282 |
void |
283 |
ui_clip_sync(void) |
284 |
{ |
285 |
cliprdr_send_text_format_announce(); |
286 |
} |
287 |
|
288 |
|
289 |
void |
290 |
xclip_init(void) |
291 |
{ |
292 |
if (!cliprdr_init()) |
293 |
return; |
294 |
|
295 |
primary_atom = XInternAtom(display, "PRIMARY", False); |
296 |
clipboard_atom = XInternAtom(display, "CLIPBOARD", False); |
297 |
targets_atom = XInternAtom(display, "TARGETS", False); |
298 |
timestamp_atom = XInternAtom(display, "TIMESTAMP", False); |
299 |
rdesktop_clipboard_target_atom = XInternAtom(display, "_RDESKTOP_CLIPBOARD_TARGET", False); |
300 |
incr_atom = XInternAtom(display, "INCR", False); |
301 |
targets[0] = targets_atom; |
302 |
targets[1] = XInternAtom(display, "TEXT", False); |
303 |
targets[2] = XInternAtom(display, "UTF8_STRING", False); |
304 |
targets[3] = XInternAtom(display, "text/unicode", False); |
305 |
targets[4] = XInternAtom(display, "TIMESTAMP", False); |
306 |
targets[5] = XA_STRING; |
307 |
|
308 |
/* rdesktop sets _RDESKTOP_CLIPBOARD_FORMATS on the root window when acquiring the clipboard. |
309 |
Other interested rdesktops can use this to notify their server of the available formats. */ |
310 |
rdesktop_clipboard_formats_atom = |
311 |
XInternAtom(display, "_RDESKTOP_CLIPBOARD_FORMATS", False); |
312 |
XSelectInput(display, DefaultRootWindow(display), PropertyChangeMask); |
313 |
} |