36 |
static int have_primary = 0; |
static int have_primary = 0; |
37 |
static int rdesktop_is_selection_owner = 0; |
static int rdesktop_is_selection_owner = 0; |
38 |
|
|
39 |
|
|
40 |
|
/* Replace CR-LF to LF (well, rather removing all CR:s) This is done |
41 |
|
in-place. The length is updated. Handles embedded nulls */ |
42 |
|
static void |
43 |
|
crlf2lf(uint8 * data, uint32 * length) |
44 |
|
{ |
45 |
|
uint8 *dst, *src; |
46 |
|
src = dst = data; |
47 |
|
while (src < data + *length) |
48 |
|
{ |
49 |
|
if (*src != '\x0d') |
50 |
|
*dst++ = *src; |
51 |
|
src++; |
52 |
|
} |
53 |
|
*length = dst - data; |
54 |
|
} |
55 |
|
|
56 |
|
/* Translate LF to CR-LF. To do this, we must allocate more memory. |
57 |
|
The length is updated. */ |
58 |
|
static uint8 * |
59 |
|
lf2crlf(uint8 * data, uint32 * length) |
60 |
|
{ |
61 |
|
uint8 *result, *p, *o; |
62 |
|
|
63 |
|
/* Worst case: Every char is LF */ |
64 |
|
result = xmalloc(*length * 2); |
65 |
|
|
66 |
|
p = data; |
67 |
|
o = result; |
68 |
|
|
69 |
|
while (p < data + *length) |
70 |
|
{ |
71 |
|
if (*p == '\x0a') |
72 |
|
*o++ = '\x0d'; |
73 |
|
*o++ = *p++; |
74 |
|
} |
75 |
|
*length = o - result; |
76 |
|
|
77 |
|
/* Convenience */ |
78 |
|
*o++ = '\0'; |
79 |
|
|
80 |
|
return result; |
81 |
|
} |
82 |
|
|
83 |
|
|
84 |
static void |
static void |
85 |
xclip_provide_selection(XSelectionRequestEvent * req, Atom type, unsigned int format, uint8 * data, |
xclip_provide_selection(XSelectionRequestEvent * req, Atom type, unsigned int format, uint8 * data, |
86 |
uint32 length) |
uint32 length) |
131 |
goto fail; |
goto fail; |
132 |
} |
} |
133 |
|
|
134 |
|
/* Negotiate target format */ |
135 |
if (event->target == targets_atom) |
if (event->target == targets_atom) |
136 |
{ |
{ |
137 |
/* FIXME: We should choose format here based on what the server wanted */ |
/* FIXME: We should choose format here based on what the server wanted */ |
148 |
{ |
{ |
149 |
DEBUG_CLIPBOARD(("Other party supports TEXT, choosing that as best_target\n")); |
DEBUG_CLIPBOARD(("Other party supports TEXT, choosing that as best_target\n")); |
150 |
best_target = text_target; |
best_target = text_target; |
151 |
|
break; |
152 |
} |
} |
153 |
} |
} |
154 |
XFree(data); |
XFree(data); |
165 |
goto fail; |
goto fail; |
166 |
} |
} |
167 |
|
|
168 |
cliprdr_send_data(data, nitems + 1); |
/* Translate linebreaks, but only if not getting data from |
169 |
|
other rdesktop instance */ |
170 |
|
if (event->target != rdesktop_clipboard_formats_atom) |
171 |
|
{ |
172 |
|
uint8 *translated_data; |
173 |
|
uint32 length = nitems; |
174 |
|
|
175 |
|
DEBUG_CLIPBOARD(("Translating linebreaks before sending data\n")); |
176 |
|
translated_data = lf2crlf(data, &length); |
177 |
|
cliprdr_send_data(translated_data, length + 1); |
178 |
|
xfree(translated_data); /* Not the same thing as XFree! */ |
179 |
|
} |
180 |
|
else |
181 |
|
{ |
182 |
|
cliprdr_send_data(data, nitems + 1); |
183 |
|
} |
184 |
XFree(data); |
XFree(data); |
185 |
|
|
186 |
if (!rdesktop_is_selection_owner) |
if (!rdesktop_is_selection_owner) |
195 |
xclip_handle_SelectionRequest(XSelectionRequestEvent * event) |
xclip_handle_SelectionRequest(XSelectionRequestEvent * event) |
196 |
{ |
{ |
197 |
unsigned long nitems, bytes_left; |
unsigned long nitems, bytes_left; |
198 |
|
unsigned char *prop_return; |
199 |
uint32 *wanted_format; |
uint32 *wanted_format; |
200 |
int format, res; |
int format, res; |
201 |
Atom type; |
Atom type; |
219 |
{ |
{ |
220 |
res = XGetWindowProperty(g_display, event->requestor, |
res = XGetWindowProperty(g_display, event->requestor, |
221 |
rdesktop_clipboard_target_atom, 0, 1, True, XA_INTEGER, |
rdesktop_clipboard_target_atom, 0, 1, True, XA_INTEGER, |
222 |
&type, &format, &nitems, &bytes_left, |
&type, &format, &nitems, &bytes_left, &prop_return); |
223 |
(unsigned char **) &wanted_format); |
wanted_format = (uint32 *) prop_return; |
224 |
format = (res == Success) ? *wanted_format : CF_TEXT; |
format = (res == Success) ? *wanted_format : CF_TEXT; |
225 |
|
/* FIXME: Need to free returned data? */ |
226 |
} |
} |
227 |
else |
else |
228 |
{ |
{ |
261 |
{ |
{ |
262 |
res = XGetWindowProperty(g_display, DefaultRootWindow(g_display), |
res = XGetWindowProperty(g_display, DefaultRootWindow(g_display), |
263 |
rdesktop_clipboard_formats_atom, 0, |
rdesktop_clipboard_formats_atom, 0, |
264 |
XMaxRequestSize(g_display), False, XA_STRING, &type, &format, |
XMaxRequestSize(g_display), False, XA_STRING, &type, |
265 |
&nitems, &bytes_left, &data); |
&format, &nitems, &bytes_left, &data); |
266 |
|
|
267 |
if ((res == Success) && (nitems > 0)) |
if ((res == Success) && (nitems > 0)) |
268 |
{ |
{ |
279 |
|
|
280 |
|
|
281 |
void |
void |
282 |
ui_clip_format_announce(char *data, uint32 length) |
ui_clip_format_announce(uint8 * data, uint32 length) |
283 |
{ |
{ |
284 |
XSetSelectionOwner(g_display, primary_atom, g_wnd, g_last_gesturetime); |
XSetSelectionOwner(g_display, primary_atom, g_wnd, g_last_gesturetime); |
285 |
if (XGetSelectionOwner(g_display, primary_atom) != g_wnd) |
if (XGetSelectionOwner(g_display, primary_atom) != g_wnd) |
300 |
|
|
301 |
|
|
302 |
void |
void |
303 |
ui_clip_handle_data(char *data, uint32 length) |
ui_clip_handle_data(uint8 * data, uint32 length) |
304 |
{ |
{ |
305 |
|
if (selection_request.target != rdesktop_clipboard_formats_atom) |
306 |
|
{ |
307 |
|
uint8 *firstnull; |
308 |
|
|
309 |
|
/* translate linebreaks */ |
310 |
|
crlf2lf(data, &length); |
311 |
|
|
312 |
|
/* Only send data up to null byte, if any */ |
313 |
|
firstnull = (uint8 *)strchr((char*)data, '\0'); |
314 |
|
if (firstnull) |
315 |
|
{ |
316 |
|
length = firstnull - data + 1; |
317 |
|
} |
318 |
|
} |
319 |
|
|
320 |
xclip_provide_selection(&selection_request, XA_STRING, 8, data, length - 1); |
xclip_provide_selection(&selection_request, XA_STRING, 8, data, length - 1); |
321 |
} |
} |
322 |
|
|
375 |
clipboard_atom = XInternAtom(g_display, "CLIPBOARD", False); |
clipboard_atom = XInternAtom(g_display, "CLIPBOARD", False); |
376 |
targets_atom = XInternAtom(g_display, "TARGETS", False); |
targets_atom = XInternAtom(g_display, "TARGETS", False); |
377 |
timestamp_atom = XInternAtom(g_display, "TIMESTAMP", False); |
timestamp_atom = XInternAtom(g_display, "TIMESTAMP", False); |
378 |
rdesktop_clipboard_target_atom = XInternAtom(g_display, "_RDESKTOP_CLIPBOARD_TARGET", False); |
rdesktop_clipboard_target_atom = |
379 |
|
XInternAtom(g_display, "_RDESKTOP_CLIPBOARD_TARGET", False); |
380 |
incr_atom = XInternAtom(g_display, "INCR", False); |
incr_atom = XInternAtom(g_display, "INCR", False); |
381 |
targets[0] = targets_atom; |
targets[0] = targets_atom; |
382 |
targets[1] = XInternAtom(g_display, "TEXT", False); |
targets[1] = XInternAtom(g_display, "TEXT", False); |