23 |
#include <X11/Xatom.h> |
#include <X11/Xatom.h> |
24 |
#include "rdesktop.h" |
#include "rdesktop.h" |
25 |
|
|
26 |
|
/* |
27 |
|
To gain better understanding of this code, one could be assisted by the following documents: |
28 |
|
- Inter-Client Communication Conventions Manual (ICCCM) |
29 |
|
HTML: http://tronche.com/gui/x/icccm/ |
30 |
|
PDF: http://ftp.xfree86.org/pub/XFree86/4.5.0/doc/PDF/icccm.pdf |
31 |
|
- MSDN: Clipboard Formats |
32 |
|
http://msdn.microsoft.com/library/en-us/winui/winui/windowsuserinterface/dataexchange/clipboard/clipboardformats.asp |
33 |
|
*/ |
34 |
|
|
35 |
#define NUM_TARGETS 6 |
#define NUM_TARGETS 6 |
36 |
|
|
37 |
extern Display *g_display; |
extern Display *g_display; |
38 |
extern Window g_wnd; |
extern Window g_wnd; |
39 |
extern Time g_last_gesturetime; |
extern Time g_last_gesturetime; |
40 |
|
|
41 |
static Atom clipboard_atom, primary_atom, targets_atom, timestamp_atom; |
/* Atoms of the two X selections we're dealing with: CLIPBOARD (explicit-copy) and PRIMARY (selection-copy) */ |
42 |
static Atom rdesktop_clipboard_target_atom, rdesktop_clipboard_formats_atom, incr_atom; |
static Atom clipboard_atom, primary_atom; |
43 |
|
/* Atom of the TARGETS clipboard target */ |
44 |
|
static Atom targets_atom; |
45 |
|
/* Atom of the TIMESTAMP clipboard target */ |
46 |
|
static Atom timestamp_atom; |
47 |
|
/* Atom _RDESKTOP_CLIPBOARD_TARGET which has multiple uses: |
48 |
|
- The 'property' argument in XConvertSelection calls: This is the property of our |
49 |
|
window into which XConvertSelection will store the received clipboard data. |
50 |
|
- In a clipboard request of target _RDESKTOP_CLIPBOARD_FORMATS, an XA_INTEGER-typed |
51 |
|
property carrying the Windows native (CF_...) format desired by the requestor. |
52 |
|
Requestor set this property (e.g. requestor_wnd[_RDESKTOP_CLIPBOARD_TARGET] = CF_TEXT) |
53 |
|
before requesting clipboard data from a fellow rdesktop using |
54 |
|
the _RDESKTOP_CLIPBOARD_FORMATS target. */ |
55 |
|
static Atom rdesktop_clipboard_target_atom; |
56 |
|
/* Atom _RDESKTOP_CLIPBOARD_FORMATS which has multiple uses: |
57 |
|
- The clipboard target (X jargon for "clipboard format") for rdesktop-to-rdesktop interchange |
58 |
|
of Windows native clipboard data. |
59 |
|
This target cannot be used standalone; the requestor must keep the |
60 |
|
_RDESKTOP_CLIPBOARD_TARGET property on his window denoting |
61 |
|
the Windows native clipboard format being requested. |
62 |
|
- The root window property set by rdesktop when it owns the clipboard, |
63 |
|
denoting all Windows native clipboard formats it offers via |
64 |
|
requests of the _RDESKTOP_CLIPBOARD_FORMATS target. */ |
65 |
|
static Atom rdesktop_clipboard_formats_atom; |
66 |
|
/* Atom of the INCR clipboard type (see ICCCM on "INCR Properties") */ |
67 |
|
static Atom incr_atom; |
68 |
|
/* Stores the last "selection request" (= another X client requesting clipboard data from us). |
69 |
|
To satisfy such a request, we request the clipboard data from the RDP server. |
70 |
|
When we receive the response from the RDP server (asynchronously), this variable gives us |
71 |
|
the context to proceed. */ |
72 |
static XSelectionRequestEvent selection_request; |
static XSelectionRequestEvent selection_request; |
73 |
|
/* Array of offered clipboard targets that will be sent to fellow X clients upon a TARGETS request. */ |
74 |
static Atom targets[NUM_TARGETS]; |
static Atom targets[NUM_TARGETS]; |
75 |
|
/* Denotes that this client currently holds the PRIMARY selection. */ |
76 |
static int have_primary = 0; |
static int have_primary = 0; |
77 |
|
/* Denotes that an rdesktop (not this rdesktop) is owning the selection, |
78 |
|
allowing us to interchange Windows native clipboard data directly. */ |
79 |
static int rdesktop_is_selection_owner = 0; |
static int rdesktop_is_selection_owner = 0; |
80 |
|
|
81 |
|
/* Denotes that an INCR ("chunked") transfer is in progress. */ |
82 |
/* Replace CR-LF to LF (well, rather removing all CR:s) This is done |
static int g_waiting_for_INCR = 0; |
83 |
in-place. The length is updated. Handles embedded nulls */ |
/* Buffers an INCR transfer. */ |
84 |
|
static uint8 *g_clip_buffer = 0; |
85 |
|
/* Denotes the size of g_clip_buffer. */ |
86 |
|
static uint32 g_clip_buflen = 0; |
87 |
|
|
88 |
|
/* Translate LF to CR-LF. To do this, we must allocate more memory. |
89 |
|
The returned string is null-terminated, as required by CF_TEXT. |
90 |
|
Does not stop on embedded nulls. |
91 |
|
The length is updated. */ |
92 |
static void |
static void |
93 |
crlf2lf(uint8 * data, uint32 * length) |
crlf2lf(uint8 * data, uint32 * length) |
94 |
{ |
{ |
151 |
XSendEvent(g_display, req->requestor, False, NoEventMask, &xev); |
XSendEvent(g_display, req->requestor, False, NoEventMask, &xev); |
152 |
} |
} |
153 |
|
|
154 |
|
/* This function is called for SelectionNotify events. |
155 |
|
The SelectionNotify message is sent from the clipboard owner to the requestor |
156 |
|
after his request was satisfied. |
157 |
|
If this function is called, we're the requestor side. */ |
158 |
|
#ifndef MAKE_PROTO |
159 |
void |
void |
160 |
xclip_handle_SelectionNotify(XSelectionEvent * event) |
xclip_handle_SelectionNotify(XSelectionEvent * event) |
161 |
{ |
{ |
162 |
unsigned long nitems, bytes_left; |
unsigned long nitems, bytes_left; |
163 |
|
XWindowAttributes wa; |
164 |
Atom type, best_target, text_target; |
Atom type, best_target, text_target; |
165 |
Atom *supported_targets; |
Atom *supported_targets; |
166 |
int res, i, format; |
int res, i, format; |
178 |
goto fail; |
goto fail; |
179 |
|
|
180 |
res = XGetWindowProperty(g_display, g_wnd, rdesktop_clipboard_target_atom, |
res = XGetWindowProperty(g_display, g_wnd, rdesktop_clipboard_target_atom, |
181 |
0, XMaxRequestSize(g_display), True, AnyPropertyType, |
0, XMaxRequestSize(g_display), False, AnyPropertyType, |
182 |
&type, &format, &nitems, &bytes_left, &data); |
&type, &format, &nitems, &bytes_left, &data); |
183 |
|
|
184 |
if (res != Success) |
if (res != Success) |
187 |
goto fail; |
goto fail; |
188 |
} |
} |
189 |
|
|
190 |
|
|
191 |
|
if (type == incr_atom) |
192 |
|
{ |
193 |
|
DEBUG_CLIPBOARD(("Received INCR.\n")); |
194 |
|
|
195 |
|
XGetWindowAttributes(g_display, g_wnd, &wa); |
196 |
|
if ((wa.your_event_mask | PropertyChangeMask) != wa.your_event_mask) |
197 |
|
{ |
198 |
|
XSelectInput(g_display, g_wnd, (wa.your_event_mask | PropertyChangeMask)); |
199 |
|
} |
200 |
|
XDeleteProperty(g_display, g_wnd, rdesktop_clipboard_target_atom); |
201 |
|
XFree(data); |
202 |
|
g_waiting_for_INCR = 1; |
203 |
|
return; |
204 |
|
} |
205 |
|
|
206 |
|
XDeleteProperty(g_display, g_wnd, rdesktop_clipboard_target_atom); |
207 |
|
|
208 |
/* Negotiate target format */ |
/* Negotiate target format */ |
209 |
if (event->target == targets_atom) |
if (event->target == targets_atom) |
210 |
{ |
{ |
233 |
return; |
return; |
234 |
} |
} |
235 |
|
|
|
if (type == incr_atom) |
|
|
{ |
|
|
warning("We don't support INCR transfers at this time. Try cutting less data.\n"); |
|
|
goto fail; |
|
|
} |
|
|
|
|
236 |
/* Translate linebreaks, but only if not getting data from |
/* Translate linebreaks, but only if not getting data from |
237 |
other rdesktop instance */ |
other rdesktop instance */ |
238 |
if (event->target != rdesktop_clipboard_formats_atom) |
if (event->target != rdesktop_clipboard_formats_atom) |
252 |
XFree(data); |
XFree(data); |
253 |
|
|
254 |
if (!rdesktop_is_selection_owner) |
if (!rdesktop_is_selection_owner) |
255 |
cliprdr_send_text_format_announce(); |
cliprdr_send_simple_native_format_announce(CF_TEXT); |
256 |
return; |
return; |
257 |
|
|
258 |
fail: |
fail: |
259 |
|
XDeleteProperty(g_display, g_wnd, rdesktop_clipboard_target_atom); |
260 |
|
XFree(data); |
261 |
cliprdr_send_data(NULL, 0); |
cliprdr_send_data(NULL, 0); |
262 |
} |
} |
263 |
|
|
264 |
|
/* This function is called for SelectionRequest events. |
265 |
|
The SelectionRequest message is sent from the requestor to the clipboard owner |
266 |
|
to request clipboard data. |
267 |
|
*/ |
268 |
void |
void |
269 |
xclip_handle_SelectionRequest(XSelectionRequestEvent * event) |
xclip_handle_SelectionRequest(XSelectionRequestEvent * event) |
270 |
{ |
{ |
308 |
/* wait for data */ |
/* wait for data */ |
309 |
} |
} |
310 |
|
|
311 |
|
/* When this rdesktop holds ownership over the clipboard, it means the clipboard data |
312 |
|
is offered by the RDP server (and when its pasted inside RDP, there's no network |
313 |
|
roundtrip). This event symbolizes this rdesktop lost onwership of the clipboard |
314 |
|
to some other X client. We should find out what clipboard formats this other |
315 |
|
client offers and announce that to RDP. */ |
316 |
void |
void |
317 |
xclip_handle_SelectionClear(void) |
xclip_handle_SelectionClear(void) |
318 |
{ |
{ |
319 |
DEBUG_CLIPBOARD(("xclip_handle_SelectionClear\n")); |
DEBUG_CLIPBOARD(("xclip_handle_SelectionClear\n")); |
320 |
have_primary = 0; |
have_primary = 0; |
321 |
XDeleteProperty(g_display, DefaultRootWindow(g_display), rdesktop_clipboard_formats_atom); |
XDeleteProperty(g_display, DefaultRootWindow(g_display), rdesktop_clipboard_formats_atom); |
322 |
cliprdr_send_text_format_announce(); |
cliprdr_send_simple_native_format_announce(CF_TEXT); |
323 |
} |
} |
324 |
|
|
325 |
|
/* Called when any property changes in our window or the root window. */ |
326 |
void |
void |
327 |
xclip_handle_PropertyNotify(XPropertyEvent * event) |
xclip_handle_PropertyNotify(XPropertyEvent * event) |
328 |
{ |
{ |
329 |
unsigned long nitems, bytes_left; |
unsigned long nitems; |
330 |
|
unsigned long offset = 0; |
331 |
|
unsigned long bytes_left = 1; |
332 |
int format, res; |
int format, res; |
333 |
|
XWindowAttributes wa; |
334 |
uint8 *data; |
uint8 *data; |
335 |
Atom type; |
Atom type; |
336 |
|
|
337 |
|
if (event->state == PropertyNewValue && g_waiting_for_INCR) |
338 |
|
{ |
339 |
|
DEBUG_CLIPBOARD(("x_clip_handle_PropertyNotify: g_waiting_for_INCR != 0\n")); |
340 |
|
|
341 |
|
while (bytes_left > 0) |
342 |
|
{ |
343 |
|
if ((XGetWindowProperty |
344 |
|
(g_display, g_wnd, rdesktop_clipboard_target_atom, offset, 4096L, |
345 |
|
False, AnyPropertyType, &type, &format, &nitems, &bytes_left, |
346 |
|
&data) != Success)) |
347 |
|
{ |
348 |
|
XFree(data); |
349 |
|
return; |
350 |
|
} |
351 |
|
|
352 |
|
if (nitems == 0) |
353 |
|
{ |
354 |
|
XGetWindowAttributes(g_display, g_wnd, &wa); |
355 |
|
XSelectInput(g_display, g_wnd, |
356 |
|
(wa.your_event_mask ^ PropertyChangeMask)); |
357 |
|
XFree(data); |
358 |
|
g_waiting_for_INCR = 0; |
359 |
|
|
360 |
|
if (g_clip_buflen > 0) |
361 |
|
{ |
362 |
|
cliprdr_send_data(g_clip_buffer, g_clip_buflen + 1); |
363 |
|
|
364 |
|
if (!rdesktop_is_selection_owner) |
365 |
|
cliprdr_send_simple_native_format_announce(CF_TEXT); |
366 |
|
|
367 |
|
xfree(g_clip_buffer); |
368 |
|
g_clip_buffer = 0; |
369 |
|
g_clip_buflen = 0; |
370 |
|
} |
371 |
|
} |
372 |
|
else |
373 |
|
{ |
374 |
|
uint8 *translated_data; |
375 |
|
uint32 length = nitems; |
376 |
|
uint8 *tmp; |
377 |
|
|
378 |
|
offset += (length / 4); |
379 |
|
DEBUG_CLIPBOARD(("Translating linebreaks before sending data\n")); |
380 |
|
translated_data = lf2crlf(data, &length); |
381 |
|
|
382 |
|
tmp = xmalloc(length + g_clip_buflen); |
383 |
|
strncpy((char *) tmp, (char *) g_clip_buffer, g_clip_buflen); |
384 |
|
xfree(g_clip_buffer); |
385 |
|
|
386 |
|
strncpy((char *) (tmp + g_clip_buflen), (char *) translated_data, |
387 |
|
length); |
388 |
|
xfree(translated_data); |
389 |
|
|
390 |
|
g_clip_buffer = tmp; |
391 |
|
g_clip_buflen += length; |
392 |
|
|
393 |
|
XFree(data); |
394 |
|
} |
395 |
|
} |
396 |
|
XDeleteProperty(g_display, g_wnd, rdesktop_clipboard_target_atom); |
397 |
|
} |
398 |
|
|
399 |
if (event->atom != rdesktop_clipboard_formats_atom) |
if (event->atom != rdesktop_clipboard_formats_atom) |
400 |
return; |
return; |
401 |
|
|
418 |
} |
} |
419 |
|
|
420 |
/* PropertyDelete, or XGetWindowProperty failed */ |
/* PropertyDelete, or XGetWindowProperty failed */ |
421 |
cliprdr_send_text_format_announce(); |
cliprdr_send_simple_native_format_announce(CF_TEXT); |
422 |
rdesktop_is_selection_owner = 0; |
rdesktop_is_selection_owner = 0; |
423 |
} |
} |
424 |
|
#endif |
425 |
|
|
426 |
|
|
427 |
|
/* Called when the RDP server announces new clipboard data formats. |
428 |
|
In response, we: |
429 |
|
- take ownership over the clipboard |
430 |
|
- declare those formats in their Windows native form |
431 |
|
to other rdesktop instances on this X server */ |
432 |
void |
void |
433 |
ui_clip_format_announce(uint8 * data, uint32 length) |
ui_clip_format_announce(uint8 * data, uint32 length) |
434 |
{ |
{ |
461 |
crlf2lf(data, &length); |
crlf2lf(data, &length); |
462 |
|
|
463 |
/* Only send data up to null byte, if any */ |
/* Only send data up to null byte, if any */ |
464 |
firstnull = strchr(data, '\0'); |
firstnull = (uint8 *) strchr((char *) data, '\0'); |
465 |
if (firstnull) |
if (firstnull) |
466 |
{ |
{ |
467 |
length = firstnull - data + 1; |
length = firstnull - data + 1; |
512 |
void |
void |
513 |
ui_clip_sync(void) |
ui_clip_sync(void) |
514 |
{ |
{ |
515 |
cliprdr_send_text_format_announce(); |
cliprdr_send_simple_native_format_announce(CF_TEXT); |
516 |
} |
} |
517 |
|
|
518 |
|
|
531 |
incr_atom = XInternAtom(g_display, "INCR", False); |
incr_atom = XInternAtom(g_display, "INCR", False); |
532 |
targets[0] = targets_atom; |
targets[0] = targets_atom; |
533 |
targets[1] = XInternAtom(g_display, "TEXT", False); |
targets[1] = XInternAtom(g_display, "TEXT", False); |
534 |
targets[2] = XInternAtom(g_display, "UTF8_STRING", False); |
targets[2] = XInternAtom(g_display, "STRING", False); |
535 |
targets[3] = XInternAtom(g_display, "text/unicode", False); |
targets[3] = XInternAtom(g_display, "text/unicode", False); |
536 |
targets[4] = XInternAtom(g_display, "TIMESTAMP", False); |
targets[4] = XInternAtom(g_display, "TIMESTAMP", False); |
537 |
targets[5] = XA_STRING; |
targets[5] = XA_STRING; |