1 | /** @file
|
---|
2 | *
|
---|
3 | * Shared Clipboard:
|
---|
4 | * X11 backend code.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2007 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 | /* Note: to automatically run regression tests on the shared clipboard,
|
---|
20 | * execute the tstClipboardX11 testcase. If you often make changes to the
|
---|
21 | * clipboard code, adding the line
|
---|
22 | * OTHERS += $(PATH_tstClipboardX11)/tstClipboardX11.run
|
---|
23 | * to LocalConfig.kmk will cause the tests to be run every time the code is
|
---|
24 | * changed. */
|
---|
25 |
|
---|
26 | #define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
|
---|
27 |
|
---|
28 | #include <errno.h>
|
---|
29 |
|
---|
30 | #include <dlfcn.h>
|
---|
31 | #include <fcntl.h>
|
---|
32 | #include <unistd.h>
|
---|
33 |
|
---|
34 | #ifdef RT_OS_SOLARIS
|
---|
35 | #include <tsol/label.h>
|
---|
36 | #endif
|
---|
37 |
|
---|
38 | #include <X11/Xlib.h>
|
---|
39 | #include <X11/Xatom.h>
|
---|
40 | #include <X11/Intrinsic.h>
|
---|
41 | #include <X11/Shell.h>
|
---|
42 | #include <X11/Xproto.h>
|
---|
43 | #include <X11/StringDefs.h>
|
---|
44 |
|
---|
45 | #include <iprt/types.h>
|
---|
46 | #include <iprt/mem.h>
|
---|
47 | #include <iprt/semaphore.h>
|
---|
48 | #include <iprt/thread.h>
|
---|
49 |
|
---|
50 | #include <VBox/log.h>
|
---|
51 |
|
---|
52 | #include <VBox/GuestHost/SharedClipboard.h>
|
---|
53 | #include <VBox/GuestHost/clipboard-helper.h>
|
---|
54 | #include <VBox/HostServices/VBoxClipboardSvc.h>
|
---|
55 |
|
---|
56 | static Atom clipGetAtom(Widget widget, const char *pszName);
|
---|
57 |
|
---|
58 | /** The different clipboard formats which we support. */
|
---|
59 | enum CLIPFORMAT
|
---|
60 | {
|
---|
61 | INVALID = 0,
|
---|
62 | TARGETS,
|
---|
63 | TEXT, /* Treat this as Utf8, but it may really be ascii */
|
---|
64 | CTEXT,
|
---|
65 | UTF8
|
---|
66 | };
|
---|
67 |
|
---|
68 | /** The table mapping X11 names to data formats and to the corresponding
|
---|
69 | * VBox clipboard formats (currently only Unicode) */
|
---|
70 | static struct _CLIPFORMATTABLE
|
---|
71 | {
|
---|
72 | /** The X11 atom name of the format (several names can match one format)
|
---|
73 | */
|
---|
74 | const char *pcszAtom;
|
---|
75 | /** The format corresponding to the name */
|
---|
76 | CLIPFORMAT enmFormat;
|
---|
77 | /** The corresponding VBox clipboard format */
|
---|
78 | uint32_t u32VBoxFormat;
|
---|
79 | } g_aFormats[] =
|
---|
80 | {
|
---|
81 | { "INVALID", INVALID, 0 },
|
---|
82 | { "UTF8_STRING", UTF8, VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT },
|
---|
83 | { "text/plain;charset=UTF-8", UTF8,
|
---|
84 | VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT },
|
---|
85 | { "text/plain;charset=utf-8", UTF8,
|
---|
86 | VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT },
|
---|
87 | { "STRING", TEXT, VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT },
|
---|
88 | { "TEXT", TEXT, VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT },
|
---|
89 | { "text/plain", TEXT, VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT },
|
---|
90 | { "COMPOUND_TEXT", CTEXT, VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT }
|
---|
91 | };
|
---|
92 |
|
---|
93 | typedef unsigned CLIPX11FORMAT;
|
---|
94 |
|
---|
95 | enum
|
---|
96 | {
|
---|
97 | NIL_CLIPX11FORMAT = 0,
|
---|
98 | MAX_CLIP_X11_FORMATS = RT_ELEMENTS(g_aFormats)
|
---|
99 | };
|
---|
100 |
|
---|
101 | /** Return the atom corresponding to a supported X11 format.
|
---|
102 | * @param widget a valid Xt widget
|
---|
103 | */
|
---|
104 | static Atom clipAtomForX11Format(Widget widget, CLIPX11FORMAT format)
|
---|
105 | {
|
---|
106 | return clipGetAtom(widget, g_aFormats[format].pcszAtom);
|
---|
107 | }
|
---|
108 |
|
---|
109 | /** Return the CLIPFORMAT corresponding to a supported X11 format. */
|
---|
110 | static CLIPFORMAT clipRealFormatForX11Format(CLIPX11FORMAT format)
|
---|
111 | {
|
---|
112 | return g_aFormats[format].enmFormat;
|
---|
113 | }
|
---|
114 |
|
---|
115 | /** Return the atom corresponding to a supported X11 format. */
|
---|
116 | static uint32_t clipVBoxFormatForX11Format(CLIPX11FORMAT format)
|
---|
117 | {
|
---|
118 | return g_aFormats[format].u32VBoxFormat;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /** Lookup the X11 format matching a given X11 atom.
|
---|
122 | * @returns the format on success, NIL_CLIPX11FORMAT on failure
|
---|
123 | * @param widget a valid Xt widget
|
---|
124 | */
|
---|
125 | static CLIPX11FORMAT clipFindX11FormatByAtom(Widget widget, Atom atomFormat)
|
---|
126 | {
|
---|
127 | for (unsigned i = 0; i < RT_ELEMENTS(g_aFormats); ++i)
|
---|
128 | if (clipAtomForX11Format(widget, i) == atomFormat)
|
---|
129 | return i;
|
---|
130 | return NIL_CLIPX11FORMAT;
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Enumerates supported X11 clipboard formats corresponding to a given VBox
|
---|
135 | * format.
|
---|
136 | * @returns the next matching X11 format in the list, or NIL_CLIPX11FORMAT if
|
---|
137 | * there are no more
|
---|
138 | * @param lastFormat The value returned from the last call of this function.
|
---|
139 | * Use NIL_CLIPX11FORMAT to start the enumeration.
|
---|
140 | */
|
---|
141 | static CLIPX11FORMAT clipEnumX11Formats(uint32_t u32VBoxFormats,
|
---|
142 | CLIPX11FORMAT lastFormat)
|
---|
143 | {
|
---|
144 | for (unsigned i = lastFormat + 1; i < RT_ELEMENTS(g_aFormats); ++i)
|
---|
145 | if (u32VBoxFormats & clipVBoxFormatForX11Format(i))
|
---|
146 | return i;
|
---|
147 | return NIL_CLIPX11FORMAT;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /** Global context information used by the X11 clipboard backend */
|
---|
151 | struct _CLIPBACKEND
|
---|
152 | {
|
---|
153 | /** Opaque data structure describing the front-end. */
|
---|
154 | VBOXCLIPBOARDCONTEXT *pFrontend;
|
---|
155 | /** Is an X server actually available? */
|
---|
156 | bool fHaveX11;
|
---|
157 | /** The X Toolkit application context structure */
|
---|
158 | XtAppContext appContext;
|
---|
159 |
|
---|
160 | /** We have a separate thread to wait for Window and Clipboard events */
|
---|
161 | RTTHREAD thread;
|
---|
162 | /** The X Toolkit widget which we use as our clipboard client. It is never made visible. */
|
---|
163 | Widget widget;
|
---|
164 |
|
---|
165 | /** Should we try to grab the clipboard on startup? */
|
---|
166 | bool fGrabClipboardOnStart;
|
---|
167 |
|
---|
168 | /** The best text format X11 has to offer, as an index into the formats
|
---|
169 | * table */
|
---|
170 | CLIPX11FORMAT X11TextFormat;
|
---|
171 | /** The best bitmap format X11 has to offer, as an index into the formats
|
---|
172 | * table */
|
---|
173 | CLIPX11FORMAT X11BitmapFormat;
|
---|
174 | /** What formats does VBox have on offer? */
|
---|
175 | uint32_t vboxFormats;
|
---|
176 | /** Cache of the last unicode data that we received */
|
---|
177 | void *pvUnicodeCache;
|
---|
178 | /** Size of the unicode data in the cache */
|
---|
179 | uint32_t cbUnicodeCache;
|
---|
180 | /** When we wish the clipboard to exit, we have to wake up the event
|
---|
181 | * loop. We do this by writing into a pipe. This end of the pipe is
|
---|
182 | * the end that another thread can write to. */
|
---|
183 | int wakeupPipeWrite;
|
---|
184 | /** The reader end of the pipe */
|
---|
185 | int wakeupPipeRead;
|
---|
186 | /** A pointer to the XFixesSelectSelectionInput function */
|
---|
187 | void (*fixesSelectInput)(Display *, Window, Atom, unsigned long);
|
---|
188 | /** The first XFixes event number */
|
---|
189 | int fixesEventBase;
|
---|
190 | };
|
---|
191 |
|
---|
192 | /** The number of simultaneous instances we support. For all normal purposes
|
---|
193 | * we should never need more than one. For the testcase it is convenient to
|
---|
194 | * have a second instance that the first can interact with in order to have
|
---|
195 | * a more controlled environment. */
|
---|
196 | enum { CLIP_MAX_CONTEXTS = 20 };
|
---|
197 |
|
---|
198 | /** Array of structures for mapping Xt widgets to context pointers. We
|
---|
199 | * need this because the widget clipboard callbacks do not pass user data. */
|
---|
200 | static struct {
|
---|
201 | /** The widget we want to associate the context with */
|
---|
202 | Widget widget;
|
---|
203 | /** The context associated with the widget */
|
---|
204 | CLIPBACKEND *pCtx;
|
---|
205 | } g_contexts[CLIP_MAX_CONTEXTS];
|
---|
206 |
|
---|
207 | /** Register a new X11 clipboard context. */
|
---|
208 | static int clipRegisterContext(CLIPBACKEND *pCtx)
|
---|
209 | {
|
---|
210 | bool found = false;
|
---|
211 | AssertReturn(pCtx != NULL, VERR_INVALID_PARAMETER);
|
---|
212 | Widget widget = pCtx->widget;
|
---|
213 | AssertReturn(widget != NULL, VERR_INVALID_PARAMETER);
|
---|
214 | for (unsigned i = 0; i < RT_ELEMENTS(g_contexts); ++i)
|
---|
215 | {
|
---|
216 | AssertReturn( (g_contexts[i].widget != widget)
|
---|
217 | && (g_contexts[i].pCtx != pCtx), VERR_WRONG_ORDER);
|
---|
218 | if (g_contexts[i].widget == NULL && !found)
|
---|
219 | {
|
---|
220 | AssertReturn(g_contexts[i].pCtx == NULL, VERR_INTERNAL_ERROR);
|
---|
221 | g_contexts[i].widget = widget;
|
---|
222 | g_contexts[i].pCtx = pCtx;
|
---|
223 | found = true;
|
---|
224 | }
|
---|
225 | }
|
---|
226 | return found ? VINF_SUCCESS : VERR_OUT_OF_RESOURCES;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /** Unregister an X11 clipboard context. */
|
---|
230 | static void clipUnregisterContext(CLIPBACKEND *pCtx)
|
---|
231 | {
|
---|
232 | bool found = false;
|
---|
233 | AssertReturnVoid(pCtx != NULL);
|
---|
234 | Widget widget = pCtx->widget;
|
---|
235 | AssertReturnVoid(widget != NULL);
|
---|
236 | for (unsigned i = 0; i < RT_ELEMENTS(g_contexts); ++i)
|
---|
237 | {
|
---|
238 | Assert(!found || g_contexts[i].widget != widget);
|
---|
239 | if (g_contexts[i].widget == widget)
|
---|
240 | {
|
---|
241 | Assert(g_contexts[i].pCtx != NULL);
|
---|
242 | g_contexts[i].widget = NULL;
|
---|
243 | g_contexts[i].pCtx = NULL;
|
---|
244 | found = true;
|
---|
245 | }
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | /** Find an X11 clipboard context. */
|
---|
250 | static CLIPBACKEND *clipLookupContext(Widget widget)
|
---|
251 | {
|
---|
252 | AssertReturn(widget != NULL, NULL);
|
---|
253 | for (unsigned i = 0; i < RT_ELEMENTS(g_contexts); ++i)
|
---|
254 | {
|
---|
255 | if (g_contexts[i].widget == widget)
|
---|
256 | {
|
---|
257 | Assert(g_contexts[i].pCtx != NULL);
|
---|
258 | return g_contexts[i].pCtx;
|
---|
259 | }
|
---|
260 | }
|
---|
261 | return NULL;
|
---|
262 | }
|
---|
263 |
|
---|
264 | /** Convert an atom name string to an X11 atom, looking it up in a cache
|
---|
265 | * before asking the server */
|
---|
266 | static Atom clipGetAtom(Widget widget, const char *pszName)
|
---|
267 | {
|
---|
268 | AssertPtrReturn(pszName, None);
|
---|
269 | Atom retval = None;
|
---|
270 | XrmValue nameVal, atomVal;
|
---|
271 | nameVal.addr = (char *) pszName;
|
---|
272 | nameVal.size = strlen(pszName);
|
---|
273 | atomVal.size = sizeof(Atom);
|
---|
274 | atomVal.addr = (char *) &retval;
|
---|
275 | XtConvertAndStore(widget, XtRString, &nameVal, XtRAtom, &atomVal);
|
---|
276 | return retval;
|
---|
277 | }
|
---|
278 |
|
---|
279 | static void clipQueueToEventThread(CLIPBACKEND *pCtx,
|
---|
280 | XtTimerCallbackProc proc,
|
---|
281 | XtPointer client_data);
|
---|
282 |
|
---|
283 | /** String written to the wakeup pipe. */
|
---|
284 | #define WAKE_UP_STRING "WakeUp!"
|
---|
285 | /** Length of the string written. */
|
---|
286 | #define WAKE_UP_STRING_LEN ( sizeof(WAKE_UP_STRING) - 1 )
|
---|
287 |
|
---|
288 | #ifndef TESTCASE
|
---|
289 | /** Schedule a function call to run on the Xt event thread by passing it to
|
---|
290 | * the application context as a 0ms timeout and waking up the event loop by
|
---|
291 | * writing to the wakeup pipe which it monitors. */
|
---|
292 | void clipQueueToEventThread(CLIPBACKEND *pCtx,
|
---|
293 | XtTimerCallbackProc proc,
|
---|
294 | XtPointer client_data)
|
---|
295 | {
|
---|
296 | LogRel2(("clipQueueToEventThread: proc=%p, client_data=%p\n",
|
---|
297 | proc, client_data));
|
---|
298 | XtAppAddTimeOut(pCtx->appContext, 0, proc, client_data);
|
---|
299 | write(pCtx->wakeupPipeWrite, WAKE_UP_STRING, WAKE_UP_STRING_LEN);
|
---|
300 | }
|
---|
301 | #endif
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * Report the formats currently supported by the X11 clipboard to VBox.
|
---|
305 | */
|
---|
306 | static void clipReportFormatsToVBox(CLIPBACKEND *pCtx)
|
---|
307 | {
|
---|
308 | uint32_t u32VBoxFormats = clipVBoxFormatForX11Format(pCtx->X11TextFormat);
|
---|
309 | ClipReportX11Formats(pCtx->pFrontend, u32VBoxFormats);
|
---|
310 | }
|
---|
311 |
|
---|
312 | /**
|
---|
313 | * Forget which formats were previously in the X11 clipboard. Called when we
|
---|
314 | * grab the clipboard. */
|
---|
315 | static void clipResetX11Formats(CLIPBACKEND *pCtx)
|
---|
316 | {
|
---|
317 | pCtx->X11TextFormat = INVALID;
|
---|
318 | pCtx->X11BitmapFormat = INVALID;
|
---|
319 | }
|
---|
320 |
|
---|
321 | /** Tell VBox that X11 currently has nothing in its clipboard. */
|
---|
322 | static void clipReportEmptyX11CB(CLIPBACKEND *pCtx)
|
---|
323 | {
|
---|
324 | clipResetX11Formats(pCtx);
|
---|
325 | clipReportFormatsToVBox(pCtx);
|
---|
326 | }
|
---|
327 |
|
---|
328 | /**
|
---|
329 | * Go through an array of X11 clipboard targets to see if they contain a text
|
---|
330 | * format we can support, and if so choose the ones we prefer (e.g. we like
|
---|
331 | * Utf8 better than compound text).
|
---|
332 | * @param pCtx the clipboard backend context structure
|
---|
333 | * @param pTargets the list of targets
|
---|
334 | * @param cTargets the size of the list in @a pTargets
|
---|
335 | */
|
---|
336 | static CLIPX11FORMAT clipGetTextFormatFromTargets(CLIPBACKEND *pCtx,
|
---|
337 | Atom *pTargets,
|
---|
338 | size_t cTargets)
|
---|
339 | {
|
---|
340 | CLIPX11FORMAT bestTextFormat = NIL_CLIPX11FORMAT;
|
---|
341 | CLIPFORMAT enmBestTextTarget = INVALID;
|
---|
342 | AssertPtrReturn(pCtx, NIL_CLIPX11FORMAT);
|
---|
343 | AssertReturn(VALID_PTR(pTargets) || cTargets == 0, NIL_CLIPX11FORMAT);
|
---|
344 | for (unsigned i = 0; i < cTargets; ++i)
|
---|
345 | {
|
---|
346 | CLIPX11FORMAT format = clipFindX11FormatByAtom(pCtx->widget,
|
---|
347 | pTargets[i]);
|
---|
348 | if (format != NIL_CLIPX11FORMAT)
|
---|
349 | {
|
---|
350 | if ( (clipVBoxFormatForX11Format(format)
|
---|
351 | == VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT)
|
---|
352 | && enmBestTextTarget < clipRealFormatForX11Format(format))
|
---|
353 | {
|
---|
354 | enmBestTextTarget = clipRealFormatForX11Format(format);
|
---|
355 | bestTextFormat = format;
|
---|
356 | }
|
---|
357 | }
|
---|
358 | }
|
---|
359 | return bestTextFormat;
|
---|
360 | }
|
---|
361 |
|
---|
362 | #ifdef TESTCASE
|
---|
363 | static bool clipTestTextFormatConversion(CLIPBACKEND *pCtx)
|
---|
364 | {
|
---|
365 | bool success = true;
|
---|
366 | Atom targets[3];
|
---|
367 | CLIPX11FORMAT x11Format;
|
---|
368 | targets[0] = clipGetAtom(NULL, "COMPOUND_TEXT");
|
---|
369 | targets[1] = clipGetAtom(NULL, "text/plain");
|
---|
370 | targets[2] = clipGetAtom(NULL, "TARGETS");
|
---|
371 | x11Format = clipGetTextFormatFromTargets(pCtx, targets, 3);
|
---|
372 | if (clipRealFormatForX11Format(x11Format) != CTEXT)
|
---|
373 | success = false;
|
---|
374 | targets[0] = clipGetAtom(NULL, "UTF8_STRING");
|
---|
375 | targets[1] = clipGetAtom(NULL, "text/plain");
|
---|
376 | targets[2] = clipGetAtom(NULL, "COMPOUND_TEXT");
|
---|
377 | x11Format = clipGetTextFormatFromTargets(pCtx, targets, 3);
|
---|
378 | if (clipRealFormatForX11Format(x11Format) != UTF8)
|
---|
379 | success = false;
|
---|
380 | return success;
|
---|
381 | }
|
---|
382 | #endif
|
---|
383 |
|
---|
384 | /**
|
---|
385 | * Go through an array of X11 clipboard targets to see if we can support any
|
---|
386 | * of them and if relevant to choose the ones we prefer (e.g. we like Utf8
|
---|
387 | * better than compound text).
|
---|
388 | * @param pCtx the clipboard backend context structure
|
---|
389 | * @param pTargets the list of targets
|
---|
390 | * @param cTargets the size of the list in @a pTargets
|
---|
391 | */
|
---|
392 | static void clipGetFormatsFromTargets(CLIPBACKEND *pCtx, Atom *pTargets,
|
---|
393 | size_t cTargets)
|
---|
394 | {
|
---|
395 | AssertPtrReturnVoid(pCtx);
|
---|
396 | AssertPtrReturnVoid(pTargets);
|
---|
397 | CLIPX11FORMAT bestTextFormat;
|
---|
398 | bestTextFormat = clipGetTextFormatFromTargets(pCtx, pTargets, cTargets);
|
---|
399 | if (pCtx->X11TextFormat != bestTextFormat)
|
---|
400 | {
|
---|
401 | pCtx->X11TextFormat = bestTextFormat;
|
---|
402 | #if defined(DEBUG) && !defined(TESTCASE)
|
---|
403 | for (unsigned i = 0; i < cTargets; ++i)
|
---|
404 | if (pTargets[i])
|
---|
405 | {
|
---|
406 | char *pszName = XGetAtomName(XtDisplay(pCtx->widget),
|
---|
407 | pTargets[i]);
|
---|
408 | LogRel2(("%s: found target %s\n", __PRETTY_FUNCTION__, pszName));
|
---|
409 | XFree(pszName);
|
---|
410 | }
|
---|
411 | #endif
|
---|
412 | }
|
---|
413 | pCtx->X11BitmapFormat = INVALID; /* not yet supported */
|
---|
414 | }
|
---|
415 |
|
---|
416 | /**
|
---|
417 | * Update the context's information about targets currently supported by X11,
|
---|
418 | * based on an array of X11 atoms.
|
---|
419 | * @param pCtx the context to be updated
|
---|
420 | * @param pTargets the array of atoms describing the targets supported
|
---|
421 | * @param cTargets the size of the array @a pTargets
|
---|
422 | */
|
---|
423 | static void clipUpdateX11Targets(CLIPBACKEND *pCtx, Atom *pTargets,
|
---|
424 | size_t cTargets)
|
---|
425 | {
|
---|
426 | LogRel2 (("%s: called\n", __PRETTY_FUNCTION__));
|
---|
427 | clipGetFormatsFromTargets(pCtx, pTargets, cTargets);
|
---|
428 | clipReportFormatsToVBox(pCtx);
|
---|
429 | }
|
---|
430 |
|
---|
431 | /**
|
---|
432 | * Notify the VBox clipboard about available data formats, based on the
|
---|
433 | * "targets" information obtained from the X11 clipboard.
|
---|
434 | * @note callback for XtGetSelectionValue
|
---|
435 | */
|
---|
436 | static void clipConvertX11Targets(Widget, XtPointer pClientData,
|
---|
437 | Atom * /* selection */, Atom *atomType,
|
---|
438 | XtPointer pValue, long unsigned int *pcLen,
|
---|
439 | int *piFormat)
|
---|
440 | {
|
---|
441 | CLIPBACKEND *pCtx =
|
---|
442 | reinterpret_cast<CLIPBACKEND *>(pClientData);
|
---|
443 | LogRel2(("clipConvertX11Targets: pValue=%p, *pcLen=%u, *atomType=%d, XT_CONVERT_FAIL=%d\n",
|
---|
444 | pValue, *pcLen, *atomType, XT_CONVERT_FAIL));
|
---|
445 | if ( (*atomType == XT_CONVERT_FAIL) /* timeout */
|
---|
446 | || (pValue == NULL)) /* No data available */
|
---|
447 | {
|
---|
448 | clipReportEmptyX11CB(pCtx);
|
---|
449 | return;
|
---|
450 | }
|
---|
451 | clipUpdateX11Targets(pCtx, (Atom *)pValue, *pcLen);
|
---|
452 | XtFree(reinterpret_cast<char *>(pValue));
|
---|
453 | }
|
---|
454 |
|
---|
455 | /**
|
---|
456 | * Callback to notify us when the contents of the X11 clipboard change.
|
---|
457 | */
|
---|
458 | void clipQueryX11CBFormats(CLIPBACKEND *pCtx)
|
---|
459 | {
|
---|
460 | LogRel2 (("%s: requesting the targets that the X11 clipboard offers\n",
|
---|
461 | __PRETTY_FUNCTION__));
|
---|
462 | XtGetSelectionValue(pCtx->widget,
|
---|
463 | clipGetAtom(pCtx->widget, "CLIPBOARD"),
|
---|
464 | clipGetAtom(pCtx->widget, "TARGETS"),
|
---|
465 | clipConvertX11Targets, pCtx,
|
---|
466 | CurrentTime);
|
---|
467 | }
|
---|
468 |
|
---|
469 | #ifndef TESTCASE
|
---|
470 |
|
---|
471 | typedef struct {
|
---|
472 | int type; /* event base */
|
---|
473 | unsigned long serial;
|
---|
474 | Bool send_event;
|
---|
475 | Display *display;
|
---|
476 | Window window;
|
---|
477 | int subtype;
|
---|
478 | Window owner;
|
---|
479 | Atom selection;
|
---|
480 | Time timestamp;
|
---|
481 | Time selection_timestamp;
|
---|
482 | } XFixesSelectionNotifyEvent;
|
---|
483 |
|
---|
484 | /**
|
---|
485 | * Wait until an event arrives and handle it if it is an XFIXES selection
|
---|
486 | * event, which Xt doesn't know about.
|
---|
487 | */
|
---|
488 | void clipPeekEventAndDoXFixesHandling(CLIPBACKEND *pCtx)
|
---|
489 | {
|
---|
490 | union
|
---|
491 | {
|
---|
492 | XEvent event;
|
---|
493 | XFixesSelectionNotifyEvent fixes;
|
---|
494 | } event = { { 0 } };
|
---|
495 |
|
---|
496 | if (XtAppPeekEvent(pCtx->appContext, &event.event))
|
---|
497 | if ( (event.event.type == pCtx->fixesEventBase)
|
---|
498 | && (event.fixes.owner != XtWindow(pCtx->widget)))
|
---|
499 | {
|
---|
500 | if ( (event.fixes.subtype == 0 /* XFixesSetSelectionOwnerNotify */)
|
---|
501 | && (event.fixes.owner != 0))
|
---|
502 | clipQueryX11CBFormats(pCtx);
|
---|
503 | else
|
---|
504 | clipReportEmptyX11CB(pCtx);
|
---|
505 | }
|
---|
506 | }
|
---|
507 |
|
---|
508 | /**
|
---|
509 | * The main loop of our clipboard reader.
|
---|
510 | * @note X11 backend code.
|
---|
511 | */
|
---|
512 | static int clipEventThread(RTTHREAD self, void *pvUser)
|
---|
513 | {
|
---|
514 | LogRel(("Shared clipboard: starting shared clipboard thread\n"));
|
---|
515 |
|
---|
516 | CLIPBACKEND *pCtx = (CLIPBACKEND *)pvUser;
|
---|
517 |
|
---|
518 | if (pCtx->fGrabClipboardOnStart)
|
---|
519 | clipQueryX11CBFormats(pCtx);
|
---|
520 | while (XtAppGetExitFlag(pCtx->appContext) == FALSE)
|
---|
521 | {
|
---|
522 | clipPeekEventAndDoXFixesHandling(pCtx);
|
---|
523 | XtAppProcessEvent(pCtx->appContext, XtIMAll);
|
---|
524 | }
|
---|
525 | LogRel(("Shared clipboard: shared clipboard thread terminated successfully\n"));
|
---|
526 | return VINF_SUCCESS;
|
---|
527 | }
|
---|
528 | #endif
|
---|
529 |
|
---|
530 | /** X11 specific uninitialisation for the shared clipboard.
|
---|
531 | * @note X11 backend code.
|
---|
532 | */
|
---|
533 | static void clipUninit(CLIPBACKEND *pCtx)
|
---|
534 | {
|
---|
535 | AssertPtrReturnVoid(pCtx);
|
---|
536 | if (pCtx->widget)
|
---|
537 | {
|
---|
538 | /* Valid widget + invalid appcontext = bug. But don't return yet. */
|
---|
539 | AssertPtr(pCtx->appContext);
|
---|
540 | clipUnregisterContext(pCtx);
|
---|
541 | XtDestroyWidget(pCtx->widget);
|
---|
542 | }
|
---|
543 | pCtx->widget = NULL;
|
---|
544 | if (pCtx->appContext)
|
---|
545 | XtDestroyApplicationContext(pCtx->appContext);
|
---|
546 | pCtx->appContext = NULL;
|
---|
547 | if (pCtx->wakeupPipeRead != 0)
|
---|
548 | close(pCtx->wakeupPipeRead);
|
---|
549 | if (pCtx->wakeupPipeWrite != 0)
|
---|
550 | close(pCtx->wakeupPipeWrite);
|
---|
551 | pCtx->wakeupPipeRead = 0;
|
---|
552 | pCtx->wakeupPipeWrite = 0;
|
---|
553 | }
|
---|
554 |
|
---|
555 | /** Worker function for stopping the clipboard which runs on the event
|
---|
556 | * thread. */
|
---|
557 | static void clipStopEventThreadWorker(XtPointer pUserData, XtIntervalId *)
|
---|
558 | {
|
---|
559 |
|
---|
560 | CLIPBACKEND *pCtx = (CLIPBACKEND *)pUserData;
|
---|
561 |
|
---|
562 | /* This might mean that we are getting stopped twice. */
|
---|
563 | Assert(pCtx->widget != NULL);
|
---|
564 |
|
---|
565 | /* Set the termination flag to tell the Xt event loop to exit. We
|
---|
566 | * reiterate that any outstanding requests from the X11 event loop to
|
---|
567 | * the VBox part *must* have returned before we do this. */
|
---|
568 | XtAppSetExitFlag(pCtx->appContext);
|
---|
569 | }
|
---|
570 |
|
---|
571 | #ifndef TESTCASE
|
---|
572 | /** Setup the XFixes library and load the XFixesSelectSelectionInput symbol */
|
---|
573 | static int clipLoadXFixes(Display *pDisplay, CLIPBACKEND *pCtx)
|
---|
574 | {
|
---|
575 | int dummy1 = 0, dummy2 = 0, rc = VINF_SUCCESS;
|
---|
576 | void *hFixesLib;
|
---|
577 |
|
---|
578 | hFixesLib = dlopen("libXfixes.so.1", RTLD_LAZY);
|
---|
579 | if (!hFixesLib)
|
---|
580 | hFixesLib = dlopen("libXfixes.so.2", RTLD_LAZY);
|
---|
581 | if (!hFixesLib)
|
---|
582 | hFixesLib = dlopen("libXfixes.so.3", RTLD_LAZY);
|
---|
583 | if (hFixesLib)
|
---|
584 | pCtx->fixesSelectInput =
|
---|
585 | (void (*)(Display *, Window, Atom, long unsigned int))
|
---|
586 | (uintptr_t)dlsym(hFixesLib, "XFixesSelectSelectionInput");
|
---|
587 | /* For us, a NULL function pointer is a failure */
|
---|
588 | if (!hFixesLib || !pCtx->fixesSelectInput)
|
---|
589 | rc = VERR_NOT_SUPPORTED;
|
---|
590 | if ( RT_SUCCESS(rc)
|
---|
591 | && !XQueryExtension(pDisplay, "XFIXES", &dummy1,
|
---|
592 | &pCtx->fixesEventBase, &dummy2))
|
---|
593 | rc = VERR_NOT_SUPPORTED;
|
---|
594 | if (RT_SUCCESS(rc) && pCtx->fixesEventBase < 0)
|
---|
595 | rc = VERR_NOT_SUPPORTED;
|
---|
596 | return rc;
|
---|
597 | }
|
---|
598 | #endif
|
---|
599 |
|
---|
600 | /** This is the callback which is scheduled when data is available on the
|
---|
601 | * wakeup pipe. It simply reads all data from the pipe. */
|
---|
602 | static void clipDrainWakeupPipe(XtPointer pUserData, int *, XtInputId *)
|
---|
603 | {
|
---|
604 | CLIPBACKEND *pCtx = (CLIPBACKEND *)pUserData;
|
---|
605 | char acBuf[WAKE_UP_STRING_LEN];
|
---|
606 |
|
---|
607 | LogRel2(("clipDrainWakeupPipe: called\n"));
|
---|
608 | while (read(pCtx->wakeupPipeRead, acBuf, sizeof(acBuf)) > 0) {}
|
---|
609 | }
|
---|
610 |
|
---|
611 | /** X11 specific initialisation for the shared clipboard.
|
---|
612 | * @note X11 backend code.
|
---|
613 | */
|
---|
614 | static int clipInit(CLIPBACKEND *pCtx)
|
---|
615 | {
|
---|
616 | /* Create a window and make it a clipboard viewer. */
|
---|
617 | int cArgc = 0;
|
---|
618 | char *pcArgv = 0;
|
---|
619 | int rc = VINF_SUCCESS;
|
---|
620 | Display *pDisplay;
|
---|
621 |
|
---|
622 | /* Make sure we are thread safe */
|
---|
623 | XtToolkitThreadInitialize();
|
---|
624 | /* Set up the Clipboard application context and main window. We call all
|
---|
625 | * these functions directly instead of calling XtOpenApplication() so
|
---|
626 | * that we can fail gracefully if we can't get an X11 display. */
|
---|
627 | XtToolkitInitialize();
|
---|
628 | pCtx->appContext = XtCreateApplicationContext();
|
---|
629 | pDisplay = XtOpenDisplay(pCtx->appContext, 0, 0, "VBoxClipboard", 0, 0, &cArgc, &pcArgv);
|
---|
630 | if (NULL == pDisplay)
|
---|
631 | {
|
---|
632 | LogRel(("Shared clipboard: failed to connect to the X11 clipboard - the window system may not be running.\n"));
|
---|
633 | rc = VERR_NOT_SUPPORTED;
|
---|
634 | }
|
---|
635 | #ifndef TESTCASE
|
---|
636 | if (RT_SUCCESS(rc))
|
---|
637 | {
|
---|
638 | rc = clipLoadXFixes(pDisplay, pCtx);
|
---|
639 | if (RT_FAILURE(rc))
|
---|
640 | LogRel(("Shared clipboard: failed to load the XFIXES extension.\n"));
|
---|
641 | }
|
---|
642 | #endif
|
---|
643 | if (RT_SUCCESS(rc))
|
---|
644 | {
|
---|
645 | pCtx->widget = XtVaAppCreateShell(0, "VBoxClipboard",
|
---|
646 | applicationShellWidgetClass,
|
---|
647 | pDisplay, XtNwidth, 1, XtNheight,
|
---|
648 | 1, NULL);
|
---|
649 | if (NULL == pCtx->widget)
|
---|
650 | {
|
---|
651 | LogRel(("Shared clipboard: failed to construct the X11 window for the shared clipboard manager.\n"));
|
---|
652 | rc = VERR_NO_MEMORY;
|
---|
653 | }
|
---|
654 | else
|
---|
655 | rc = clipRegisterContext(pCtx);
|
---|
656 | }
|
---|
657 | if (RT_SUCCESS(rc))
|
---|
658 | {
|
---|
659 | EventMask mask = 0;
|
---|
660 |
|
---|
661 | XtSetMappedWhenManaged(pCtx->widget, false);
|
---|
662 | XtRealizeWidget(pCtx->widget);
|
---|
663 | #ifndef TESTCASE
|
---|
664 | /* Enable clipboard update notification */
|
---|
665 | pCtx->fixesSelectInput(pDisplay, XtWindow(pCtx->widget),
|
---|
666 | clipGetAtom(pCtx->widget, "CLIPBOARD"),
|
---|
667 | 7 /* All XFixes*Selection*NotifyMask flags */);
|
---|
668 | #endif
|
---|
669 | }
|
---|
670 | /* Create the pipes */
|
---|
671 | int pipes[2];
|
---|
672 | if (!pipe(pipes))
|
---|
673 | {
|
---|
674 | pCtx->wakeupPipeRead = pipes[0];
|
---|
675 | pCtx->wakeupPipeWrite = pipes[1];
|
---|
676 | if (!XtAppAddInput(pCtx->appContext, pCtx->wakeupPipeRead,
|
---|
677 | (XtPointer) XtInputReadMask,
|
---|
678 | clipDrainWakeupPipe, (XtPointer) pCtx))
|
---|
679 | rc = VERR_NO_MEMORY; /* What failure means is not doc'ed. */
|
---|
680 | if ( RT_SUCCESS(rc)
|
---|
681 | && (fcntl(pCtx->wakeupPipeRead, F_SETFL, O_NONBLOCK) != 0))
|
---|
682 | rc = RTErrConvertFromErrno(errno);
|
---|
683 | if (RT_FAILURE(rc))
|
---|
684 | LogRel(("Shared clipboard: failed to setup the termination mechanism.\n"));
|
---|
685 | }
|
---|
686 | else
|
---|
687 | rc = RTErrConvertFromErrno(errno);
|
---|
688 | if (RT_FAILURE(rc))
|
---|
689 | clipUninit(pCtx);
|
---|
690 | if (RT_FAILURE(rc))
|
---|
691 | LogRel(("Shared clipboard: initialisation failed: %Rrc\n", rc));
|
---|
692 | return rc;
|
---|
693 | }
|
---|
694 |
|
---|
695 | /**
|
---|
696 | * Construct the X11 backend of the shared clipboard.
|
---|
697 | * @note X11 backend code
|
---|
698 | */
|
---|
699 | CLIPBACKEND *ClipConstructX11(VBOXCLIPBOARDCONTEXT *pFrontend, bool fHeadless)
|
---|
700 | {
|
---|
701 | int rc;
|
---|
702 |
|
---|
703 | CLIPBACKEND *pCtx = (CLIPBACKEND *)
|
---|
704 | RTMemAllocZ(sizeof(CLIPBACKEND));
|
---|
705 | if (pCtx && fHeadless)
|
---|
706 | {
|
---|
707 | /*
|
---|
708 | * If we don't find the DISPLAY environment variable we assume that
|
---|
709 | * we are not connected to an X11 server. Don't actually try to do
|
---|
710 | * this then, just fail silently and report success on every call.
|
---|
711 | * This is important for VBoxHeadless.
|
---|
712 | */
|
---|
713 | LogRelFunc(("X11 DISPLAY variable not set -- disabling shared clipboard\n"));
|
---|
714 | pCtx->fHaveX11 = false;
|
---|
715 | return pCtx;
|
---|
716 | }
|
---|
717 |
|
---|
718 | pCtx->fHaveX11 = true;
|
---|
719 |
|
---|
720 | LogRel(("Initializing X11 clipboard backend\n"));
|
---|
721 | if (pCtx)
|
---|
722 | pCtx->pFrontend = pFrontend;
|
---|
723 | return pCtx;
|
---|
724 | }
|
---|
725 |
|
---|
726 | /**
|
---|
727 | * Destruct the shared clipboard X11 backend.
|
---|
728 | * @note X11 backend code
|
---|
729 | */
|
---|
730 | void ClipDestructX11(CLIPBACKEND *pCtx)
|
---|
731 | {
|
---|
732 | /*
|
---|
733 | * Immediately return if we are not connected to the X server.
|
---|
734 | */
|
---|
735 | if (!pCtx->fHaveX11)
|
---|
736 | return;
|
---|
737 |
|
---|
738 | /* We set this to NULL when the event thread exits. It really should
|
---|
739 | * have exited at this point, when we are about to unload the code from
|
---|
740 | * memory. */
|
---|
741 | Assert(pCtx->widget == NULL);
|
---|
742 | }
|
---|
743 |
|
---|
744 | /**
|
---|
745 | * Announce to the X11 backend that we are ready to start.
|
---|
746 | * @param grab whether we should try to grab the shared clipboard at once
|
---|
747 | */
|
---|
748 | int ClipStartX11(CLIPBACKEND *pCtx, bool grab)
|
---|
749 | {
|
---|
750 | int rc = VINF_SUCCESS;
|
---|
751 | LogRelFlowFunc(("\n"));
|
---|
752 | /*
|
---|
753 | * Immediately return if we are not connected to the X server.
|
---|
754 | */
|
---|
755 | if (!pCtx->fHaveX11)
|
---|
756 | return VINF_SUCCESS;
|
---|
757 |
|
---|
758 | rc = clipInit(pCtx);
|
---|
759 | if (RT_SUCCESS(rc))
|
---|
760 | {
|
---|
761 | clipResetX11Formats(pCtx);
|
---|
762 | pCtx->fGrabClipboardOnStart = grab;
|
---|
763 | }
|
---|
764 | #ifndef TESTCASE
|
---|
765 | if (RT_SUCCESS(rc))
|
---|
766 | {
|
---|
767 | rc = RTThreadCreate(&pCtx->thread, clipEventThread, pCtx, 0,
|
---|
768 | RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "SHCLIP");
|
---|
769 | if (RT_FAILURE(rc))
|
---|
770 | {
|
---|
771 | LogRel(("Failed to start the shared clipboard thread.\n"));
|
---|
772 | clipUninit(pCtx);
|
---|
773 | }
|
---|
774 | }
|
---|
775 | #endif
|
---|
776 | return rc;
|
---|
777 | }
|
---|
778 |
|
---|
779 | /**
|
---|
780 | * Shut down the shared clipboard X11 backend.
|
---|
781 | * @note X11 backend code
|
---|
782 | * @note Any requests from this object to get clipboard data from VBox
|
---|
783 | * *must* have completed or aborted before we are called, as
|
---|
784 | * otherwise the X11 event loop will still be waiting for the request
|
---|
785 | * to return and will not be able to terminate.
|
---|
786 | */
|
---|
787 | int ClipStopX11(CLIPBACKEND *pCtx)
|
---|
788 | {
|
---|
789 | int rc, rcThread;
|
---|
790 | unsigned count = 0;
|
---|
791 | /*
|
---|
792 | * Immediately return if we are not connected to the X server.
|
---|
793 | */
|
---|
794 | if (!pCtx->fHaveX11)
|
---|
795 | return VINF_SUCCESS;
|
---|
796 |
|
---|
797 | LogRelFunc(("stopping the shared clipboard X11 backend\n"));
|
---|
798 | /* Write to the "stop" pipe */
|
---|
799 | clipQueueToEventThread(pCtx, clipStopEventThreadWorker, (XtPointer) pCtx);
|
---|
800 | #ifndef TESTCASE
|
---|
801 | do
|
---|
802 | {
|
---|
803 | rc = RTThreadWait(pCtx->thread, 1000, &rcThread);
|
---|
804 | ++count;
|
---|
805 | Assert(RT_SUCCESS(rc) || ((VERR_TIMEOUT == rc) && (count != 5)));
|
---|
806 | } while ((VERR_TIMEOUT == rc) && (count < 300));
|
---|
807 | #else
|
---|
808 | rc = VINF_SUCCESS;
|
---|
809 | rcThread = VINF_SUCCESS;
|
---|
810 | #endif
|
---|
811 | if (RT_SUCCESS(rc))
|
---|
812 | AssertRC(rcThread);
|
---|
813 | else
|
---|
814 | LogRelFunc(("rc=%Rrc\n", rc));
|
---|
815 | clipUninit(pCtx);
|
---|
816 | LogRelFlowFunc(("returning %Rrc.\n", rc));
|
---|
817 | return rc;
|
---|
818 | }
|
---|
819 |
|
---|
820 | /**
|
---|
821 | * Satisfy a request from X11 for clipboard targets supported by VBox.
|
---|
822 | *
|
---|
823 | * @returns iprt status code
|
---|
824 | * @param atomTypeReturn The type of the data we are returning
|
---|
825 | * @param pValReturn A pointer to the data we are returning. This
|
---|
826 | * should be set to memory allocated by XtMalloc,
|
---|
827 | * which will be freed later by the Xt toolkit.
|
---|
828 | * @param pcLenReturn The length of the data we are returning
|
---|
829 | * @param piFormatReturn The format (8bit, 16bit, 32bit) of the data we are
|
---|
830 | * returning
|
---|
831 | * @note X11 backend code, called by the XtOwnSelection callback.
|
---|
832 | */
|
---|
833 | static int clipCreateX11Targets(CLIPBACKEND *pCtx, Atom *atomTypeReturn,
|
---|
834 | XtPointer *pValReturn,
|
---|
835 | unsigned long *pcLenReturn,
|
---|
836 | int *piFormatReturn)
|
---|
837 | {
|
---|
838 | Atom *atomTargets = (Atom *)XtMalloc( (MAX_CLIP_X11_FORMATS + 3)
|
---|
839 | * sizeof(Atom));
|
---|
840 | unsigned cTargets = 0;
|
---|
841 | LogRelFlowFunc (("called\n"));
|
---|
842 | CLIPX11FORMAT format = NIL_CLIPX11FORMAT;
|
---|
843 | do
|
---|
844 | {
|
---|
845 | format = clipEnumX11Formats(pCtx->vboxFormats, format);
|
---|
846 | if (format != NIL_CLIPX11FORMAT)
|
---|
847 | {
|
---|
848 | atomTargets[cTargets] = clipAtomForX11Format(pCtx->widget,
|
---|
849 | format);
|
---|
850 | ++cTargets;
|
---|
851 | }
|
---|
852 | } while (format != NIL_CLIPX11FORMAT);
|
---|
853 | /* We always offer these */
|
---|
854 | atomTargets[cTargets] = clipGetAtom(pCtx->widget, "TARGETS");
|
---|
855 | atomTargets[cTargets + 1] = clipGetAtom(pCtx->widget, "MULTIPLE");
|
---|
856 | atomTargets[cTargets + 2] = clipGetAtom(pCtx->widget, "TIMESTAMP");
|
---|
857 | *atomTypeReturn = XA_ATOM;
|
---|
858 | *pValReturn = (XtPointer)atomTargets;
|
---|
859 | *pcLenReturn = cTargets + 3;
|
---|
860 | *piFormatReturn = 32;
|
---|
861 | return VINF_SUCCESS;
|
---|
862 | }
|
---|
863 |
|
---|
864 | /** This is a wrapper around ClipRequestDataForX11 that will cache the
|
---|
865 | * data returned.
|
---|
866 | */
|
---|
867 | static int clipReadVBoxClipboard(CLIPBACKEND *pCtx, uint32_t u32Format,
|
---|
868 | void **ppv, uint32_t *pcb)
|
---|
869 | {
|
---|
870 | int rc = VINF_SUCCESS;
|
---|
871 | LogRelFlowFunc(("pCtx=%p, u32Format=%02X, ppv=%p, pcb=%p\n", pCtx,
|
---|
872 | u32Format, ppv, pcb));
|
---|
873 | if (u32Format == VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT)
|
---|
874 | {
|
---|
875 | if (pCtx->pvUnicodeCache == NULL)
|
---|
876 | rc = ClipRequestDataForX11(pCtx->pFrontend, u32Format,
|
---|
877 | &pCtx->pvUnicodeCache,
|
---|
878 | &pCtx->cbUnicodeCache);
|
---|
879 | if (RT_SUCCESS(rc))
|
---|
880 | {
|
---|
881 | *ppv = RTMemDup(pCtx->pvUnicodeCache, pCtx->cbUnicodeCache);
|
---|
882 | *pcb = pCtx->cbUnicodeCache;
|
---|
883 | if (*ppv == NULL)
|
---|
884 | rc = VERR_NO_MEMORY;
|
---|
885 | }
|
---|
886 | }
|
---|
887 | else
|
---|
888 | rc = ClipRequestDataForX11(pCtx->pFrontend, u32Format,
|
---|
889 | ppv, pcb);
|
---|
890 | LogRelFlowFunc(("returning %Rrc\n", rc));
|
---|
891 | if (RT_SUCCESS(rc))
|
---|
892 | LogRelFlowFunc(("*ppv=%.*ls, *pcb=%u\n", *pcb, *ppv, *pcb));
|
---|
893 | return rc;
|
---|
894 | }
|
---|
895 |
|
---|
896 | /**
|
---|
897 | * Calculate a buffer size large enough to hold the source Windows format
|
---|
898 | * text converted into Unix Utf8, including the null terminator
|
---|
899 | * @returns iprt status code
|
---|
900 | * @param pwsz the source text in UCS-2 with Windows EOLs
|
---|
901 | * @param cwc the size in USC-2 elements of the source text, with or
|
---|
902 | * without the terminator
|
---|
903 | * @param pcbActual where to store the buffer size needed
|
---|
904 | */
|
---|
905 | static int clipWinTxtBufSizeForUtf8(PRTUTF16 pwsz, size_t cwc,
|
---|
906 | size_t *pcbActual)
|
---|
907 | {
|
---|
908 | size_t cbRet = 0;
|
---|
909 | int rc = RTUtf16CalcUtf8LenEx(pwsz, cwc, &cbRet);
|
---|
910 | if (RT_SUCCESS(rc))
|
---|
911 | *pcbActual = cbRet + 1; /* null terminator */
|
---|
912 | return rc;
|
---|
913 | }
|
---|
914 |
|
---|
915 | /**
|
---|
916 | * Convert text from Windows format (UCS-2 with CRLF line endings) to standard
|
---|
917 | * Utf-8.
|
---|
918 | *
|
---|
919 | * @returns iprt status code
|
---|
920 | *
|
---|
921 | * @param pwszSrc the text to be converted
|
---|
922 | * @param cbSrc the length of @a pwszSrc in bytes
|
---|
923 | * @param pszBuf where to write the converted string
|
---|
924 | * @param cbBuf the size of the buffer pointed to by @a pszBuf
|
---|
925 | * @param pcbActual where to store the size of the converted string.
|
---|
926 | * optional.
|
---|
927 | */
|
---|
928 | static int clipWinTxtToUtf8(PRTUTF16 pwszSrc, size_t cbSrc, char *pszBuf,
|
---|
929 | size_t cbBuf, size_t *pcbActual)
|
---|
930 | {
|
---|
931 | PRTUTF16 pwszTmp = NULL;
|
---|
932 | size_t cwSrc = cbSrc / 2, cwTmp = 0, cbDest = 0;
|
---|
933 | int rc = VINF_SUCCESS;
|
---|
934 |
|
---|
935 | LogRelFlowFunc (("pwszSrc=%.*ls, cbSrc=%u\n", cbSrc, pwszSrc, cbSrc));
|
---|
936 | /* How long will the converted text be? */
|
---|
937 | AssertPtr(pwszSrc);
|
---|
938 | AssertPtr(pszBuf);
|
---|
939 | rc = vboxClipboardUtf16GetLinSize(pwszSrc, cwSrc, &cwTmp);
|
---|
940 | if (RT_SUCCESS(rc) && cwTmp == 0)
|
---|
941 | rc = VERR_NO_DATA;
|
---|
942 | if (RT_SUCCESS(rc))
|
---|
943 | pwszTmp = (PRTUTF16)RTMemAlloc(cwTmp * 2);
|
---|
944 | if (!pwszTmp)
|
---|
945 | rc = VERR_NO_MEMORY;
|
---|
946 | /* Convert the text. */
|
---|
947 | if (RT_SUCCESS(rc))
|
---|
948 | rc = vboxClipboardUtf16WinToLin(pwszSrc, cwSrc, pwszTmp, cwTmp);
|
---|
949 | if (RT_SUCCESS(rc))
|
---|
950 | /* Convert the Utf16 string to Utf8. */
|
---|
951 | rc = RTUtf16ToUtf8Ex(pwszTmp + 1, cwTmp - 1, &pszBuf, cbBuf,
|
---|
952 | &cbDest);
|
---|
953 | RTMemFree(reinterpret_cast<void *>(pwszTmp));
|
---|
954 | if (pcbActual)
|
---|
955 | *pcbActual = cbDest + 1;
|
---|
956 | LogRelFlowFunc(("returning %Rrc\n", rc));
|
---|
957 | if (RT_SUCCESS(rc))
|
---|
958 | LogRelFlowFunc (("converted string is %.*s. Returning.\n", cbDest,
|
---|
959 | pszBuf));
|
---|
960 | return rc;
|
---|
961 | }
|
---|
962 |
|
---|
963 | /**
|
---|
964 | * Satisfy a request from X11 to convert the clipboard text to Utf-8. We
|
---|
965 | * return null-terminated text, but can cope with non-null-terminated input.
|
---|
966 | *
|
---|
967 | * @returns iprt status code
|
---|
968 | * @param pDisplay an X11 display structure, needed for conversions
|
---|
969 | * performed by Xlib
|
---|
970 | * @param pv the text to be converted (UCS-2 with Windows EOLs)
|
---|
971 | * @param cb the length of the text in @cb in bytes
|
---|
972 | * @param atomTypeReturn where to store the atom for the type of the data
|
---|
973 | * we are returning
|
---|
974 | * @param pValReturn where to store the pointer to the data we are
|
---|
975 | * returning. This should be to memory allocated by
|
---|
976 | * XtMalloc, which will be freed by the Xt toolkit
|
---|
977 | * later.
|
---|
978 | * @param pcLenReturn where to store the length of the data we are
|
---|
979 | * returning
|
---|
980 | * @param piFormatReturn where to store the bit width (8, 16, 32) of the
|
---|
981 | * data we are returning
|
---|
982 | */
|
---|
983 | static int clipWinTxtToUtf8ForX11CB(Display *pDisplay, PRTUTF16 pwszSrc,
|
---|
984 | size_t cbSrc, Atom *atomTarget,
|
---|
985 | Atom *atomTypeReturn,
|
---|
986 | XtPointer *pValReturn,
|
---|
987 | unsigned long *pcLenReturn,
|
---|
988 | int *piFormatReturn)
|
---|
989 | {
|
---|
990 | /* This may slightly overestimate the space needed. */
|
---|
991 | size_t cbDest = 0;
|
---|
992 | int rc = clipWinTxtBufSizeForUtf8(pwszSrc, cbSrc / 2, &cbDest);
|
---|
993 | if (RT_SUCCESS(rc))
|
---|
994 | {
|
---|
995 | char *pszDest = (char *)XtMalloc(cbDest);
|
---|
996 | size_t cbActual = 0;
|
---|
997 | if (pszDest)
|
---|
998 | rc = clipWinTxtToUtf8(pwszSrc, cbSrc, pszDest, cbDest,
|
---|
999 | &cbActual);
|
---|
1000 | if (RT_SUCCESS(rc))
|
---|
1001 | {
|
---|
1002 | *atomTypeReturn = *atomTarget;
|
---|
1003 | *pValReturn = (XtPointer)pszDest;
|
---|
1004 | *pcLenReturn = cbActual;
|
---|
1005 | *piFormatReturn = 8;
|
---|
1006 | }
|
---|
1007 | }
|
---|
1008 | return rc;
|
---|
1009 | }
|
---|
1010 |
|
---|
1011 | /**
|
---|
1012 | * Satisfy a request from X11 to convert the clipboard text to
|
---|
1013 | * COMPOUND_TEXT. We return null-terminated text, but can cope with non-null-
|
---|
1014 | * terminated input.
|
---|
1015 | *
|
---|
1016 | * @returns iprt status code
|
---|
1017 | * @param pDisplay an X11 display structure, needed for conversions
|
---|
1018 | * performed by Xlib
|
---|
1019 | * @param pv the text to be converted (UCS-2 with Windows EOLs)
|
---|
1020 | * @param cb the length of the text in @cb in bytes
|
---|
1021 | * @param atomTypeReturn where to store the atom for the type of the data
|
---|
1022 | * we are returning
|
---|
1023 | * @param pValReturn where to store the pointer to the data we are
|
---|
1024 | * returning. This should be to memory allocated by
|
---|
1025 | * XtMalloc, which will be freed by the Xt toolkit
|
---|
1026 | * later.
|
---|
1027 | * @param pcLenReturn where to store the length of the data we are
|
---|
1028 | * returning
|
---|
1029 | * @param piFormatReturn where to store the bit width (8, 16, 32) of the
|
---|
1030 | * data we are returning
|
---|
1031 | */
|
---|
1032 | static int clipWinTxtToCTextForX11CB(Display *pDisplay, PRTUTF16 pwszSrc,
|
---|
1033 | size_t cbSrc, Atom *atomTypeReturn,
|
---|
1034 | XtPointer *pValReturn,
|
---|
1035 | unsigned long *pcLenReturn,
|
---|
1036 | int *piFormatReturn)
|
---|
1037 | {
|
---|
1038 | char *pszTmp = NULL, *pszTmp2 = NULL;
|
---|
1039 | size_t cbTmp = 0, cbActual = 0;
|
---|
1040 | XTextProperty property;
|
---|
1041 | int rc = VINF_SUCCESS, xrc = 0;
|
---|
1042 |
|
---|
1043 | LogRelFlowFunc(("pwszSrc=%.*ls, cbSrc=%u\n", cbSrc / 2, pwszSrc, cbSrc));
|
---|
1044 | AssertPtrReturn(pDisplay, false);
|
---|
1045 | AssertPtrReturn(pwszSrc, false);
|
---|
1046 | rc = clipWinTxtBufSizeForUtf8(pwszSrc, cbSrc / 2, &cbTmp);
|
---|
1047 | if (RT_SUCCESS(rc))
|
---|
1048 | {
|
---|
1049 | pszTmp = (char *)RTMemAlloc(cbTmp);
|
---|
1050 | if (!pszTmp)
|
---|
1051 | rc = VERR_NO_MEMORY;
|
---|
1052 | }
|
---|
1053 | if (RT_SUCCESS(rc))
|
---|
1054 | rc = clipWinTxtToUtf8(pwszSrc, cbSrc, pszTmp, cbTmp + 1,
|
---|
1055 | &cbActual);
|
---|
1056 | /* Convert the Utf8 text to the current encoding (usually a noop). */
|
---|
1057 | if (RT_SUCCESS(rc))
|
---|
1058 | rc = RTStrUtf8ToCurrentCP(&pszTmp2, pszTmp);
|
---|
1059 | /* And finally (!) convert the resulting text to compound text. */
|
---|
1060 | if (RT_SUCCESS(rc))
|
---|
1061 | xrc = XmbTextListToTextProperty(pDisplay, &pszTmp2, 1,
|
---|
1062 | XCompoundTextStyle, &property);
|
---|
1063 | if (RT_SUCCESS(rc) && xrc < 0)
|
---|
1064 | rc = ( xrc == XNoMemory ? VERR_NO_MEMORY
|
---|
1065 | : xrc == XLocaleNotSupported ? VERR_NOT_SUPPORTED
|
---|
1066 | : xrc == XConverterNotFound ? VERR_NOT_SUPPORTED
|
---|
1067 | : VERR_UNRESOLVED_ERROR);
|
---|
1068 | RTMemFree(pszTmp);
|
---|
1069 | RTStrFree(pszTmp2);
|
---|
1070 | *atomTypeReturn = property.encoding;
|
---|
1071 | *pValReturn = reinterpret_cast<XtPointer>(property.value);
|
---|
1072 | *pcLenReturn = property.nitems + 1;
|
---|
1073 | *piFormatReturn = property.format;
|
---|
1074 | LogRelFlowFunc(("returning %Rrc\n", rc));
|
---|
1075 | if (RT_SUCCESS(rc))
|
---|
1076 | LogRelFlowFunc (("converted string is %s\n", property.value));
|
---|
1077 | return rc;
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 | /**
|
---|
1081 | * Does this atom correspond to one of the two selection types we support?
|
---|
1082 | * @param widget a valid Xt widget
|
---|
1083 | * @param selType the atom in question
|
---|
1084 | */
|
---|
1085 | static bool clipIsSupportedSelectionType(Widget widget, Atom selType)
|
---|
1086 | {
|
---|
1087 | return( (selType == clipGetAtom(widget, "CLIPBOARD"))
|
---|
1088 | || (selType == clipGetAtom(widget, "PRIMARY")));
|
---|
1089 | }
|
---|
1090 |
|
---|
1091 | /**
|
---|
1092 | * Remove a trailing nul character from a string by adjusting the string
|
---|
1093 | * length. Some X11 applications don't like zero-terminated text...
|
---|
1094 | * @param pText the text in question
|
---|
1095 | * @param pcText the length of the text, adjusted on return
|
---|
1096 | * @param format the format of the text
|
---|
1097 | */
|
---|
1098 | static void clipTrimTrailingNul(XtPointer pText, unsigned long *pcText,
|
---|
1099 | CLIPFORMAT format)
|
---|
1100 | {
|
---|
1101 | AssertPtrReturnVoid(pText);
|
---|
1102 | AssertPtrReturnVoid(pcText);
|
---|
1103 | AssertReturnVoid((format == UTF8) || (format == CTEXT) || (format == TEXT));
|
---|
1104 | if (((char *)pText)[*pcText - 1] == '\0')
|
---|
1105 | --(*pcText);
|
---|
1106 | }
|
---|
1107 |
|
---|
1108 | static int clipConvertVBoxCBForX11(CLIPBACKEND *pCtx, Atom *atomTarget,
|
---|
1109 | Atom *atomTypeReturn,
|
---|
1110 | XtPointer *pValReturn,
|
---|
1111 | unsigned long *pcLenReturn,
|
---|
1112 | int *piFormatReturn)
|
---|
1113 | {
|
---|
1114 | int rc = VINF_SUCCESS;
|
---|
1115 | CLIPX11FORMAT x11Format = clipFindX11FormatByAtom(pCtx->widget,
|
---|
1116 | *atomTarget);
|
---|
1117 | CLIPFORMAT format = clipRealFormatForX11Format(x11Format);
|
---|
1118 | if ( ((format == UTF8) || (format == CTEXT) || (format == TEXT))
|
---|
1119 | && (pCtx->vboxFormats & VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT))
|
---|
1120 | {
|
---|
1121 | void *pv = NULL;
|
---|
1122 | uint32_t cb = 0;
|
---|
1123 | rc = clipReadVBoxClipboard(pCtx,
|
---|
1124 | VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT,
|
---|
1125 | &pv, &cb);
|
---|
1126 | if (RT_SUCCESS(rc) && (cb == 0))
|
---|
1127 | rc = VERR_NO_DATA;
|
---|
1128 | if (RT_SUCCESS(rc) && ((format == UTF8) || (format == TEXT)))
|
---|
1129 | rc = clipWinTxtToUtf8ForX11CB(XtDisplay(pCtx->widget),
|
---|
1130 | (PRTUTF16)pv, cb, atomTarget,
|
---|
1131 | atomTypeReturn, pValReturn,
|
---|
1132 | pcLenReturn, piFormatReturn);
|
---|
1133 | else if (RT_SUCCESS(rc) && (format == CTEXT))
|
---|
1134 | rc = clipWinTxtToCTextForX11CB(XtDisplay(pCtx->widget),
|
---|
1135 | (PRTUTF16)pv, cb,
|
---|
1136 | atomTypeReturn, pValReturn,
|
---|
1137 | pcLenReturn, piFormatReturn);
|
---|
1138 | if (RT_SUCCESS(rc))
|
---|
1139 | clipTrimTrailingNul(*(XtPointer *)pValReturn, pcLenReturn, format);
|
---|
1140 | RTMemFree(pv);
|
---|
1141 | }
|
---|
1142 | else
|
---|
1143 | rc = VERR_NOT_SUPPORTED;
|
---|
1144 | return rc;
|
---|
1145 | }
|
---|
1146 |
|
---|
1147 | /**
|
---|
1148 | * Return VBox's clipboard data for an X11 client.
|
---|
1149 | * @note X11 backend code, callback for XtOwnSelection
|
---|
1150 | */
|
---|
1151 | static Boolean clipXtConvertSelectionProc(Widget widget, Atom *atomSelection,
|
---|
1152 | Atom *atomTarget,
|
---|
1153 | Atom *atomTypeReturn,
|
---|
1154 | XtPointer *pValReturn,
|
---|
1155 | unsigned long *pcLenReturn,
|
---|
1156 | int *piFormatReturn)
|
---|
1157 | {
|
---|
1158 | CLIPBACKEND *pCtx = clipLookupContext(widget);
|
---|
1159 | int rc = VINF_SUCCESS;
|
---|
1160 |
|
---|
1161 | LogRelFlowFunc(("\n"));
|
---|
1162 | if (!clipIsSupportedSelectionType(pCtx->widget, *atomSelection))
|
---|
1163 | return false;
|
---|
1164 | if (*atomTarget == clipGetAtom(pCtx->widget, "TARGETS"))
|
---|
1165 | rc = clipCreateX11Targets(pCtx, atomTypeReturn, pValReturn,
|
---|
1166 | pcLenReturn, piFormatReturn);
|
---|
1167 | else
|
---|
1168 | rc = clipConvertVBoxCBForX11(pCtx, atomTarget, atomTypeReturn,
|
---|
1169 | pValReturn, pcLenReturn, piFormatReturn);
|
---|
1170 | LogRelFlowFunc(("returning, internal status code %Rrc\n", rc));
|
---|
1171 | return RT_SUCCESS(rc);
|
---|
1172 | }
|
---|
1173 |
|
---|
1174 | /** Structure used to pass information about formats that VBox supports */
|
---|
1175 | typedef struct _CLIPNEWVBOXFORMATS
|
---|
1176 | {
|
---|
1177 | /** Context information for the X11 clipboard */
|
---|
1178 | CLIPBACKEND *pCtx;
|
---|
1179 | /** Formats supported by VBox */
|
---|
1180 | uint32_t formats;
|
---|
1181 | } CLIPNEWVBOXFORMATS;
|
---|
1182 |
|
---|
1183 | /** Invalidates the local cache of the data in the VBox clipboard. */
|
---|
1184 | static void clipInvalidateVBoxCBCache(CLIPBACKEND *pCtx)
|
---|
1185 | {
|
---|
1186 | if (pCtx->pvUnicodeCache != NULL)
|
---|
1187 | {
|
---|
1188 | RTMemFree(pCtx->pvUnicodeCache);
|
---|
1189 | pCtx->pvUnicodeCache = NULL;
|
---|
1190 | }
|
---|
1191 | }
|
---|
1192 |
|
---|
1193 | /**
|
---|
1194 | * Take possession of the X11 clipboard (and middle-button selection).
|
---|
1195 | */
|
---|
1196 | static void clipGrabX11CB(CLIPBACKEND *pCtx, uint32_t u32Formats)
|
---|
1197 | {
|
---|
1198 | if (XtOwnSelection(pCtx->widget, clipGetAtom(pCtx->widget, "CLIPBOARD"),
|
---|
1199 | CurrentTime, clipXtConvertSelectionProc, NULL, 0))
|
---|
1200 | {
|
---|
1201 | pCtx->vboxFormats = u32Formats;
|
---|
1202 | /* Grab the middle-button paste selection too. */
|
---|
1203 | XtOwnSelection(pCtx->widget, clipGetAtom(pCtx->widget, "PRIMARY"),
|
---|
1204 | CurrentTime, clipXtConvertSelectionProc, NULL, 0);
|
---|
1205 | }
|
---|
1206 | }
|
---|
1207 |
|
---|
1208 | /**
|
---|
1209 | * Worker function for ClipAnnounceFormatToX11 which runs on the
|
---|
1210 | * event thread.
|
---|
1211 | * @param pUserData Pointer to a CLIPNEWVBOXFORMATS structure containing
|
---|
1212 | * information about the VBox formats available and the
|
---|
1213 | * clipboard context data. Must be freed by the worker.
|
---|
1214 | */
|
---|
1215 | static void clipNewVBoxFormatsWorker(XtPointer pUserData,
|
---|
1216 | XtIntervalId * /* interval */)
|
---|
1217 | {
|
---|
1218 | CLIPNEWVBOXFORMATS *pFormats = (CLIPNEWVBOXFORMATS *)pUserData;
|
---|
1219 | CLIPBACKEND *pCtx = pFormats->pCtx;
|
---|
1220 | uint32_t u32Formats = pFormats->formats;
|
---|
1221 | RTMemFree(pFormats);
|
---|
1222 | LogRelFlowFunc (("u32Formats=%d\n", u32Formats));
|
---|
1223 | clipInvalidateVBoxCBCache(pCtx);
|
---|
1224 | clipGrabX11CB(pCtx, u32Formats);
|
---|
1225 | clipResetX11Formats(pCtx);
|
---|
1226 | LogRelFlowFunc(("returning\n"));
|
---|
1227 | }
|
---|
1228 |
|
---|
1229 | /**
|
---|
1230 | * VBox is taking possession of the shared clipboard.
|
---|
1231 | *
|
---|
1232 | * @param u32Formats Clipboard formats that VBox is offering
|
---|
1233 | * @note X11 backend code
|
---|
1234 | */
|
---|
1235 | void ClipAnnounceFormatToX11(CLIPBACKEND *pCtx,
|
---|
1236 | uint32_t u32Formats)
|
---|
1237 | {
|
---|
1238 | /*
|
---|
1239 | * Immediately return if we are not connected to the X server.
|
---|
1240 | */
|
---|
1241 | if (!pCtx->fHaveX11)
|
---|
1242 | return;
|
---|
1243 | /* This must be freed by the worker callback */
|
---|
1244 | CLIPNEWVBOXFORMATS *pFormats =
|
---|
1245 | (CLIPNEWVBOXFORMATS *) RTMemAlloc(sizeof(CLIPNEWVBOXFORMATS));
|
---|
1246 | if (pFormats != NULL) /* if it is we will soon have other problems */
|
---|
1247 | {
|
---|
1248 | pFormats->pCtx = pCtx;
|
---|
1249 | pFormats->formats = u32Formats;
|
---|
1250 | clipQueueToEventThread(pCtx, clipNewVBoxFormatsWorker,
|
---|
1251 | (XtPointer) pFormats);
|
---|
1252 | }
|
---|
1253 | }
|
---|
1254 |
|
---|
1255 | /**
|
---|
1256 | * Massage generic Utf16 with CR end-of-lines into the format Windows expects
|
---|
1257 | * and return the result in a RTMemAlloc allocated buffer.
|
---|
1258 | * @returns IPRT status code
|
---|
1259 | * @param pwcSrc The source Utf16
|
---|
1260 | * @param cwcSrc The number of 16bit elements in @a pwcSrc, not counting
|
---|
1261 | * the terminating zero
|
---|
1262 | * @param ppwszDest Where to store the buffer address
|
---|
1263 | * @param pcbDest On success, where to store the number of bytes written.
|
---|
1264 | * Undefined otherwise. Optional
|
---|
1265 | */
|
---|
1266 | static int clipUtf16ToWinTxt(RTUTF16 *pwcSrc, size_t cwcSrc,
|
---|
1267 | PRTUTF16 *ppwszDest, uint32_t *pcbDest)
|
---|
1268 | {
|
---|
1269 | LogRelFlowFunc(("pwcSrc=%p, cwcSrc=%u, ppwszDest=%p\n", pwcSrc, cwcSrc,
|
---|
1270 | ppwszDest));
|
---|
1271 | AssertPtrReturn(pwcSrc, VERR_INVALID_POINTER);
|
---|
1272 | AssertPtrReturn(ppwszDest, VERR_INVALID_POINTER);
|
---|
1273 | if (pcbDest)
|
---|
1274 | *pcbDest = 0;
|
---|
1275 | PRTUTF16 pwszDest = NULL;
|
---|
1276 | size_t cwcDest;
|
---|
1277 | int rc = vboxClipboardUtf16GetWinSize(pwcSrc, cwcSrc + 1, &cwcDest);
|
---|
1278 | if (RT_SUCCESS(rc))
|
---|
1279 | {
|
---|
1280 | pwszDest = (PRTUTF16) RTMemAlloc(cwcDest * 2);
|
---|
1281 | if (!pwszDest)
|
---|
1282 | rc = VERR_NO_MEMORY;
|
---|
1283 | }
|
---|
1284 | if (RT_SUCCESS(rc))
|
---|
1285 | rc = vboxClipboardUtf16LinToWin(pwcSrc, cwcSrc + 1, pwszDest,
|
---|
1286 | cwcDest);
|
---|
1287 | if (RT_SUCCESS(rc))
|
---|
1288 | {
|
---|
1289 | LogRelFlowFunc (("converted string is %.*ls\n", cwcDest, pwszDest));
|
---|
1290 | *ppwszDest = pwszDest;
|
---|
1291 | if (pcbDest)
|
---|
1292 | *pcbDest = cwcDest * 2;
|
---|
1293 | }
|
---|
1294 | else
|
---|
1295 | RTMemFree(pwszDest);
|
---|
1296 | LogRelFlowFunc(("returning %Rrc\n", rc));
|
---|
1297 | if (pcbDest)
|
---|
1298 | LogRelFlowFunc(("*pcbDest=%u\n", *pcbDest));
|
---|
1299 | return rc;
|
---|
1300 | }
|
---|
1301 |
|
---|
1302 | /**
|
---|
1303 | * Convert Utf-8 text with CR end-of-lines into Utf-16 as Windows expects it
|
---|
1304 | * and return the result in a RTMemAlloc allocated buffer.
|
---|
1305 | * @returns IPRT status code
|
---|
1306 | * @param pcSrc The source Utf-8
|
---|
1307 | * @param cbSrc The size of the source in bytes, not counting the
|
---|
1308 | * terminating zero
|
---|
1309 | * @param ppwszDest Where to store the buffer address
|
---|
1310 | * @param pcbDest On success, where to store the number of bytes written.
|
---|
1311 | * Undefined otherwise. Optional
|
---|
1312 | */
|
---|
1313 | static int clipUtf8ToWinTxt(const char *pcSrc, unsigned cbSrc,
|
---|
1314 | PRTUTF16 *ppwszDest, uint32_t *pcbDest)
|
---|
1315 | {
|
---|
1316 | LogRelFlowFunc(("pcSrc=%p, cbSrc=%u, ppwszDest=%p\n", pcSrc, cbSrc,
|
---|
1317 | ppwszDest));
|
---|
1318 | AssertPtrReturn(pcSrc, VERR_INVALID_POINTER);
|
---|
1319 | AssertPtrReturn(ppwszDest, VERR_INVALID_POINTER);
|
---|
1320 | if (pcbDest)
|
---|
1321 | *pcbDest = 0;
|
---|
1322 | /* Intermediate conversion to UTF16 */
|
---|
1323 | size_t cwcTmp;
|
---|
1324 | PRTUTF16 pwcTmp = NULL;
|
---|
1325 | int rc = RTStrToUtf16Ex(pcSrc, cbSrc, &pwcTmp, 0, &cwcTmp);
|
---|
1326 | if (RT_SUCCESS(rc))
|
---|
1327 | rc = clipUtf16ToWinTxt(pwcTmp, cwcTmp, ppwszDest, pcbDest);
|
---|
1328 | RTUtf16Free(pwcTmp);
|
---|
1329 | LogRelFlowFunc(("Returning %Rrc\n", rc));
|
---|
1330 | if (pcbDest)
|
---|
1331 | LogRelFlowFunc(("*pcbDest=%u\n", *pcbDest));
|
---|
1332 | return rc;
|
---|
1333 | }
|
---|
1334 |
|
---|
1335 | /**
|
---|
1336 | * Convert COMPOUND TEXT with CR end-of-lines into Utf-16 as Windows expects
|
---|
1337 | * it and return the result in a RTMemAlloc allocated buffer.
|
---|
1338 | * @returns IPRT status code
|
---|
1339 | * @param widget An Xt widget, necessary because we use Xt/Xlib for the
|
---|
1340 | * conversion
|
---|
1341 | * @param pcSrc The source text
|
---|
1342 | * @param cbSrc The size of the source in bytes, not counting the
|
---|
1343 | * terminating zero
|
---|
1344 | * @param ppwszDest Where to store the buffer address
|
---|
1345 | * @param pcbDest On success, where to store the number of bytes written.
|
---|
1346 | * Undefined otherwise. Optional
|
---|
1347 | */
|
---|
1348 | static int clipCTextToWinTxt(Widget widget, unsigned char *pcSrc,
|
---|
1349 | unsigned cbSrc, PRTUTF16 *ppwszDest,
|
---|
1350 | uint32_t *pcbDest)
|
---|
1351 | {
|
---|
1352 | LogRelFlowFunc(("widget=%p, pcSrc=%p, cbSrc=%u, ppwszDest=%p\n", widget,
|
---|
1353 | pcSrc, cbSrc, ppwszDest));
|
---|
1354 | AssertReturn(widget, VERR_INVALID_PARAMETER);
|
---|
1355 | AssertPtrReturn(pcSrc, VERR_INVALID_POINTER);
|
---|
1356 | AssertPtrReturn(ppwszDest, VERR_INVALID_POINTER);
|
---|
1357 | if (pcbDest)
|
---|
1358 | *pcbDest = 0;
|
---|
1359 |
|
---|
1360 | /* Special case as X*TextProperty* can't seem to handle empty strings. */
|
---|
1361 | if (cbSrc == 0)
|
---|
1362 | {
|
---|
1363 | *ppwszDest = (PRTUTF16) RTMemAlloc(2);
|
---|
1364 | if (!*ppwszDest)
|
---|
1365 | return VERR_NO_MEMORY;
|
---|
1366 | **ppwszDest = 0;
|
---|
1367 | if (pcbDest)
|
---|
1368 | *pcbDest = 2;
|
---|
1369 | return VINF_SUCCESS;
|
---|
1370 | }
|
---|
1371 |
|
---|
1372 | if (pcbDest)
|
---|
1373 | *pcbDest = 0;
|
---|
1374 | /* Intermediate conversion to Utf8 */
|
---|
1375 | int rc = VINF_SUCCESS;
|
---|
1376 | XTextProperty property;
|
---|
1377 | char **ppcTmp = NULL, *pszTmp = NULL;
|
---|
1378 | int cProps;
|
---|
1379 |
|
---|
1380 | property.value = pcSrc;
|
---|
1381 | property.encoding = clipGetAtom(widget, "COMPOUND_TEXT");
|
---|
1382 | property.format = 8;
|
---|
1383 | property.nitems = cbSrc;
|
---|
1384 | int xrc = XmbTextPropertyToTextList(XtDisplay(widget), &property,
|
---|
1385 | &ppcTmp, &cProps);
|
---|
1386 | if (xrc < 0)
|
---|
1387 | rc = ( xrc == XNoMemory ? VERR_NO_MEMORY
|
---|
1388 | : xrc == XLocaleNotSupported ? VERR_NOT_SUPPORTED
|
---|
1389 | : xrc == XConverterNotFound ? VERR_NOT_SUPPORTED
|
---|
1390 | : VERR_UNRESOLVED_ERROR);
|
---|
1391 | /* Convert the text returned to UTF8 */
|
---|
1392 | if (RT_SUCCESS(rc))
|
---|
1393 | rc = RTStrCurrentCPToUtf8(&pszTmp, *ppcTmp);
|
---|
1394 | /* Now convert the UTF8 to UTF16 */
|
---|
1395 | if (RT_SUCCESS(rc))
|
---|
1396 | rc = clipUtf8ToWinTxt(pszTmp, strlen(pszTmp), ppwszDest, pcbDest);
|
---|
1397 | if (ppcTmp != NULL)
|
---|
1398 | XFreeStringList(ppcTmp);
|
---|
1399 | RTStrFree(pszTmp);
|
---|
1400 | LogRelFlowFunc(("Returning %Rrc\n", rc));
|
---|
1401 | if (pcbDest)
|
---|
1402 | LogRelFlowFunc(("*pcbDest=%u\n", *pcbDest));
|
---|
1403 | return rc;
|
---|
1404 | }
|
---|
1405 |
|
---|
1406 | /**
|
---|
1407 | * Convert Latin-1 text with CR end-of-lines into Utf-16 as Windows expects
|
---|
1408 | * it and return the result in a RTMemAlloc allocated buffer.
|
---|
1409 | * @returns IPRT status code
|
---|
1410 | * @param pcSrc The source text
|
---|
1411 | * @param cbSrc The size of the source in bytes, not counting the
|
---|
1412 | * terminating zero
|
---|
1413 | * @param ppwszDest Where to store the buffer address
|
---|
1414 | * @param pcbDest On success, where to store the number of bytes written.
|
---|
1415 | * Undefined otherwise. Optional
|
---|
1416 | */
|
---|
1417 | static int clipLatin1ToWinTxt(char *pcSrc, unsigned cbSrc,
|
---|
1418 | PRTUTF16 *ppwszDest, uint32_t *pcbDest)
|
---|
1419 | {
|
---|
1420 | LogRelFlowFunc (("pcSrc=%.*s, cbSrc=%u, ppwszDest=%p\n", cbSrc,
|
---|
1421 | (char *) pcSrc, cbSrc, ppwszDest));
|
---|
1422 | AssertPtrReturn(pcSrc, VERR_INVALID_POINTER);
|
---|
1423 | AssertPtrReturn(ppwszDest, VERR_INVALID_POINTER);
|
---|
1424 | PRTUTF16 pwszDest = NULL;
|
---|
1425 | int rc = VINF_SUCCESS;
|
---|
1426 |
|
---|
1427 | /* Calculate the space needed */
|
---|
1428 | unsigned cwcDest = 0;
|
---|
1429 | for (unsigned i = 0; i < cbSrc && pcSrc[i] != '\0'; ++i)
|
---|
1430 | if (pcSrc[i] == LINEFEED)
|
---|
1431 | cwcDest += 2;
|
---|
1432 | else
|
---|
1433 | ++cwcDest;
|
---|
1434 | ++cwcDest; /* Leave space for the terminator */
|
---|
1435 | if (pcbDest)
|
---|
1436 | *pcbDest = cwcDest * 2;
|
---|
1437 | pwszDest = (PRTUTF16) RTMemAlloc(cwcDest * 2);
|
---|
1438 | if (!pwszDest)
|
---|
1439 | rc = VERR_NO_MEMORY;
|
---|
1440 |
|
---|
1441 | /* And do the conversion, bearing in mind that Latin-1 expands "naturally"
|
---|
1442 | * to Utf-16. */
|
---|
1443 | if (RT_SUCCESS(rc))
|
---|
1444 | {
|
---|
1445 | for (unsigned i = 0, j = 0; i < cbSrc; ++i, ++j)
|
---|
1446 | if (pcSrc[i] != LINEFEED)
|
---|
1447 | pwszDest[j] = pcSrc[i];
|
---|
1448 | else
|
---|
1449 | {
|
---|
1450 | pwszDest[j] = CARRIAGERETURN;
|
---|
1451 | pwszDest[j + 1] = LINEFEED;
|
---|
1452 | ++j;
|
---|
1453 | }
|
---|
1454 | pwszDest[cwcDest - 1] = '\0'; /* Make sure we are zero-terminated. */
|
---|
1455 | LogRelFlowFunc (("converted text is %.*ls\n", cwcDest, pwszDest));
|
---|
1456 | }
|
---|
1457 | if (RT_SUCCESS(rc))
|
---|
1458 | *ppwszDest = pwszDest;
|
---|
1459 | else
|
---|
1460 | RTMemFree(pwszDest);
|
---|
1461 | LogRelFlowFunc(("Returning %Rrc\n", rc));
|
---|
1462 | if (pcbDest)
|
---|
1463 | LogRelFlowFunc(("*pcbDest=%u\n", *pcbDest));
|
---|
1464 | return rc;
|
---|
1465 | }
|
---|
1466 |
|
---|
1467 | /** A structure containing information about where to store a request
|
---|
1468 | * for the X11 clipboard contents. */
|
---|
1469 | struct _CLIPREADX11CBREQ
|
---|
1470 | {
|
---|
1471 | /** The format VBox would like the data in */
|
---|
1472 | uint32_t mFormat;
|
---|
1473 | /** The text format we requested from X11 if we requested text */
|
---|
1474 | CLIPX11FORMAT mTextFormat;
|
---|
1475 | /** The clipboard context this request is associated with */
|
---|
1476 | CLIPBACKEND *mCtx;
|
---|
1477 | /** The request structure passed in from the backend. */
|
---|
1478 | CLIPREADCBREQ *mReq;
|
---|
1479 | };
|
---|
1480 |
|
---|
1481 | typedef struct _CLIPREADX11CBREQ CLIPREADX11CBREQ;
|
---|
1482 |
|
---|
1483 | /**
|
---|
1484 | * Convert the text obtained from the X11 clipboard to UTF-16LE with Windows
|
---|
1485 | * EOLs, place it in the buffer supplied and signal that data has arrived.
|
---|
1486 | * @note X11 backend code, callback for XtGetSelectionValue, for use when
|
---|
1487 | * the X11 clipboard contains a text format we understand.
|
---|
1488 | */
|
---|
1489 | static void clipConvertX11CB(Widget widget, XtPointer pClientData,
|
---|
1490 | Atom * /* selection */, Atom *atomType,
|
---|
1491 | XtPointer pvSrc, long unsigned int *pcLen,
|
---|
1492 | int *piFormat)
|
---|
1493 | {
|
---|
1494 | CLIPREADX11CBREQ *pReq = (CLIPREADX11CBREQ *) pClientData;
|
---|
1495 | LogRelFlowFunc(("pReq->mFormat=%02X, pReq->mTextFormat=%u, pReq->mCtx=%p\n",
|
---|
1496 | pReq->mFormat, pReq->mTextFormat, pReq->mCtx));
|
---|
1497 | AssertPtr(pReq->mCtx);
|
---|
1498 | Assert(pReq->mFormat != 0); /* sanity */
|
---|
1499 | int rc = VINF_SUCCESS;
|
---|
1500 | CLIPBACKEND *pCtx = pReq->mCtx;
|
---|
1501 | unsigned cbSrc = (*pcLen) * (*piFormat) / 8;
|
---|
1502 | void *pvDest = NULL;
|
---|
1503 | uint32_t cbDest = 0;
|
---|
1504 |
|
---|
1505 | if (pvSrc == NULL)
|
---|
1506 | /* The clipboard selection may have changed before we could get it. */
|
---|
1507 | rc = VERR_NO_DATA;
|
---|
1508 | else if (*atomType == XT_CONVERT_FAIL) /* Xt timeout */
|
---|
1509 | rc = VERR_TIMEOUT;
|
---|
1510 | else if (pReq->mFormat == VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT)
|
---|
1511 | {
|
---|
1512 | /* In which format is the clipboard data? */
|
---|
1513 | switch (clipRealFormatForX11Format(pReq->mTextFormat))
|
---|
1514 | {
|
---|
1515 | case CTEXT:
|
---|
1516 | rc = clipCTextToWinTxt(widget, (unsigned char *)pvSrc, cbSrc,
|
---|
1517 | (PRTUTF16 *) &pvDest, &cbDest);
|
---|
1518 | break;
|
---|
1519 | case UTF8:
|
---|
1520 | case TEXT:
|
---|
1521 | {
|
---|
1522 | /* If we are given broken Utf-8, we treat it as Latin1. Is
|
---|
1523 | * this acceptable? */
|
---|
1524 | if (RT_SUCCESS(RTStrValidateEncodingEx((char *)pvSrc, cbSrc,
|
---|
1525 | 0)))
|
---|
1526 | rc = clipUtf8ToWinTxt((const char *)pvSrc, cbSrc,
|
---|
1527 | (PRTUTF16 *) &pvDest, &cbDest);
|
---|
1528 | else
|
---|
1529 | rc = clipLatin1ToWinTxt((char *) pvSrc, cbSrc,
|
---|
1530 | (PRTUTF16 *) &pvDest, &cbDest);
|
---|
1531 | break;
|
---|
1532 | }
|
---|
1533 | default:
|
---|
1534 | rc = VERR_INVALID_PARAMETER;
|
---|
1535 | }
|
---|
1536 | }
|
---|
1537 | else
|
---|
1538 | rc = VERR_NOT_IMPLEMENTED;
|
---|
1539 | XtFree((char *)pvSrc);
|
---|
1540 | ClipCompleteDataRequestFromX11(pReq->mCtx->pFrontend, rc, pReq->mReq,
|
---|
1541 | pvDest, cbDest);
|
---|
1542 | RTMemFree(pvDest);
|
---|
1543 | RTMemFree(pReq);
|
---|
1544 | LogRelFlowFunc(("rc=%Rrc\n", rc));
|
---|
1545 | }
|
---|
1546 |
|
---|
1547 | /** Worker function for ClipRequestDataFromX11 which runs on the event
|
---|
1548 | * thread. */
|
---|
1549 | static void vboxClipboardReadX11Worker(XtPointer pUserData,
|
---|
1550 | XtIntervalId * /* interval */)
|
---|
1551 | {
|
---|
1552 | CLIPREADX11CBREQ *pReq = (CLIPREADX11CBREQ *)pUserData;
|
---|
1553 | CLIPBACKEND *pCtx = pReq->mCtx;
|
---|
1554 | LogRelFlowFunc (("pReq->mFormat = %02X\n", pReq->mFormat));
|
---|
1555 |
|
---|
1556 | int rc = VINF_SUCCESS;
|
---|
1557 | /*
|
---|
1558 | * VBox wants to read data in the given format.
|
---|
1559 | */
|
---|
1560 | if (pReq->mFormat == VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT)
|
---|
1561 | {
|
---|
1562 | pReq->mTextFormat = pCtx->X11TextFormat;
|
---|
1563 | if (pReq->mTextFormat == INVALID)
|
---|
1564 | /* VBox thinks we have data and we don't */
|
---|
1565 | rc = VERR_NO_DATA;
|
---|
1566 | else
|
---|
1567 | /* Send out a request for the data to the current clipboard
|
---|
1568 | * owner */
|
---|
1569 | XtGetSelectionValue(pCtx->widget, clipGetAtom(pCtx->widget, "CLIPBOARD"),
|
---|
1570 | clipAtomForX11Format(pCtx->widget,
|
---|
1571 | pCtx->X11TextFormat),
|
---|
1572 | clipConvertX11CB,
|
---|
1573 | reinterpret_cast<XtPointer>(pReq),
|
---|
1574 | CurrentTime);
|
---|
1575 | }
|
---|
1576 | else
|
---|
1577 | rc = VERR_NOT_IMPLEMENTED;
|
---|
1578 | if (RT_FAILURE(rc))
|
---|
1579 | {
|
---|
1580 | /* The clipboard callback was never scheduled, so we must signal
|
---|
1581 | * that the request processing is finished and clean up ourselves. */
|
---|
1582 | ClipCompleteDataRequestFromX11(pReq->mCtx->pFrontend, rc, pReq->mReq,
|
---|
1583 | NULL, 0);
|
---|
1584 | RTMemFree(pReq);
|
---|
1585 | }
|
---|
1586 | LogRelFlowFunc(("status %Rrc\n", rc));
|
---|
1587 | }
|
---|
1588 |
|
---|
1589 | /**
|
---|
1590 | * Called when VBox wants to read the X11 clipboard.
|
---|
1591 | *
|
---|
1592 | * @returns iprt status code
|
---|
1593 | * @param pCtx Context data for the clipboard backend
|
---|
1594 | * @param u32Format The format that the VBox would like to receive the data
|
---|
1595 | * in
|
---|
1596 | * @param pv Where to write the data to
|
---|
1597 | * @param cb The size of the buffer to write the data to
|
---|
1598 | * @param pcbActual Where to write the actual size of the written data
|
---|
1599 | * @note We allocate a request structure which must be freed by the worker
|
---|
1600 | */
|
---|
1601 | int ClipRequestDataFromX11(CLIPBACKEND *pCtx, uint32_t u32Format,
|
---|
1602 | CLIPREADCBREQ *pReq)
|
---|
1603 | {
|
---|
1604 | /*
|
---|
1605 | * Immediately return if we are not connected to the X server.
|
---|
1606 | */
|
---|
1607 | if (!pCtx->fHaveX11)
|
---|
1608 | return VERR_NO_DATA;
|
---|
1609 | int rc = VINF_SUCCESS;
|
---|
1610 | CLIPREADX11CBREQ *pX11Req;
|
---|
1611 | pX11Req = (CLIPREADX11CBREQ *)RTMemAllocZ(sizeof(*pX11Req));
|
---|
1612 | if (!pX11Req)
|
---|
1613 | rc = VERR_NO_MEMORY;
|
---|
1614 | else
|
---|
1615 | {
|
---|
1616 | pX11Req->mFormat = u32Format;
|
---|
1617 | pX11Req->mCtx = pCtx;
|
---|
1618 | pX11Req->mReq = pReq;
|
---|
1619 | /* We use this to schedule a worker function on the event thread. */
|
---|
1620 | clipQueueToEventThread(pCtx, vboxClipboardReadX11Worker,
|
---|
1621 | (XtPointer) pX11Req);
|
---|
1622 | }
|
---|
1623 | return rc;
|
---|
1624 | }
|
---|
1625 |
|
---|
1626 | #ifdef TESTCASE
|
---|
1627 |
|
---|
1628 | /** @todo This unit test currently works by emulating the X11 and X toolkit
|
---|
1629 | * APIs to exercise the code, since I didn't want to rewrite the code too much
|
---|
1630 | * when I wrote the tests. However, this makes it rather ugly and hard to
|
---|
1631 | * understand. Anyone doing any work on the code should feel free to
|
---|
1632 | * rewrite the tests and the code to make them cleaner and more readable. */
|
---|
1633 |
|
---|
1634 | #include <iprt/test.h>
|
---|
1635 | #include <poll.h>
|
---|
1636 |
|
---|
1637 | #define TEST_WIDGET (Widget)0xffff
|
---|
1638 |
|
---|
1639 | /* For the purpose of the test case, we just execute the procedure to be
|
---|
1640 | * scheduled, as we are running single threaded. */
|
---|
1641 | void clipQueueToEventThread(CLIPBACKEND *pCtx,
|
---|
1642 | XtTimerCallbackProc proc,
|
---|
1643 | XtPointer client_data)
|
---|
1644 | {
|
---|
1645 | proc(client_data, NULL);
|
---|
1646 | }
|
---|
1647 |
|
---|
1648 | void XtFree(char *ptr)
|
---|
1649 | { RTMemFree((void *) ptr); }
|
---|
1650 |
|
---|
1651 | /* The data in the simulated VBox clipboard */
|
---|
1652 | static int g_vboxDataRC = VINF_SUCCESS;
|
---|
1653 | static void *g_vboxDatapv = NULL;
|
---|
1654 | static uint32_t g_vboxDatacb = 0;
|
---|
1655 |
|
---|
1656 | /* Set empty data in the simulated VBox clipboard. */
|
---|
1657 | static void clipEmptyVBox(CLIPBACKEND *pCtx, int retval)
|
---|
1658 | {
|
---|
1659 | g_vboxDataRC = retval;
|
---|
1660 | RTMemFree(g_vboxDatapv);
|
---|
1661 | g_vboxDatapv = NULL;
|
---|
1662 | g_vboxDatacb = 0;
|
---|
1663 | ClipAnnounceFormatToX11(pCtx, 0);
|
---|
1664 | }
|
---|
1665 |
|
---|
1666 | /* Set the data in the simulated VBox clipboard. */
|
---|
1667 | static int clipSetVBoxUtf16(CLIPBACKEND *pCtx, int retval,
|
---|
1668 | const char *pcszData, size_t cb)
|
---|
1669 | {
|
---|
1670 | PRTUTF16 pwszData = NULL;
|
---|
1671 | size_t cwData = 0;
|
---|
1672 | int rc = RTStrToUtf16Ex(pcszData, RTSTR_MAX, &pwszData, 0, &cwData);
|
---|
1673 | if (RT_FAILURE(rc))
|
---|
1674 | return rc;
|
---|
1675 | AssertReturn(cb <= cwData * 2 + 2, VERR_BUFFER_OVERFLOW);
|
---|
1676 | void *pv = RTMemDup(pwszData, cb);
|
---|
1677 | RTUtf16Free(pwszData);
|
---|
1678 | if (pv == NULL)
|
---|
1679 | return VERR_NO_MEMORY;
|
---|
1680 | if (g_vboxDatapv)
|
---|
1681 | RTMemFree(g_vboxDatapv);
|
---|
1682 | g_vboxDataRC = retval;
|
---|
1683 | g_vboxDatapv = pv;
|
---|
1684 | g_vboxDatacb = cb;
|
---|
1685 | ClipAnnounceFormatToX11(pCtx,
|
---|
1686 | VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT);
|
---|
1687 | return VINF_SUCCESS;
|
---|
1688 | }
|
---|
1689 |
|
---|
1690 | /* Return the data in the simulated VBox clipboard. */
|
---|
1691 | int ClipRequestDataForX11(VBOXCLIPBOARDCONTEXT *pCtx,
|
---|
1692 | uint32_t u32Format, void **ppv,
|
---|
1693 | uint32_t *pcb)
|
---|
1694 | {
|
---|
1695 | *pcb = g_vboxDatacb;
|
---|
1696 | if (g_vboxDatapv != NULL)
|
---|
1697 | {
|
---|
1698 | void *pv = RTMemDup(g_vboxDatapv, g_vboxDatacb);
|
---|
1699 | *ppv = pv;
|
---|
1700 | return pv != NULL ? g_vboxDataRC : VERR_NO_MEMORY;
|
---|
1701 | }
|
---|
1702 | *ppv = NULL;
|
---|
1703 | return g_vboxDataRC;
|
---|
1704 | }
|
---|
1705 |
|
---|
1706 | Display *XtDisplay(Widget w)
|
---|
1707 | { return (Display *) 0xffff; }
|
---|
1708 |
|
---|
1709 | int XmbTextListToTextProperty(Display *display, char **list, int count,
|
---|
1710 | XICCEncodingStyle style,
|
---|
1711 | XTextProperty *text_prop_return)
|
---|
1712 | {
|
---|
1713 | /* We don't fully reimplement this API for obvious reasons. */
|
---|
1714 | AssertReturn(count == 1, XLocaleNotSupported);
|
---|
1715 | AssertReturn(style == XCompoundTextStyle, XLocaleNotSupported);
|
---|
1716 | /* We simplify the conversion by only accepting ASCII. */
|
---|
1717 | for (unsigned i = 0; (*list)[i] != 0; ++i)
|
---|
1718 | AssertReturn(((*list)[i] & 0x80) == 0, XLocaleNotSupported);
|
---|
1719 | text_prop_return->value =
|
---|
1720 | (unsigned char*)RTMemDup(*list, strlen(*list) + 1);
|
---|
1721 | text_prop_return->encoding = clipGetAtom(NULL, "COMPOUND_TEXT");
|
---|
1722 | text_prop_return->format = 8;
|
---|
1723 | text_prop_return->nitems = strlen(*list);
|
---|
1724 | return 0;
|
---|
1725 | }
|
---|
1726 |
|
---|
1727 | int Xutf8TextListToTextProperty(Display *display, char **list, int count,
|
---|
1728 | XICCEncodingStyle style,
|
---|
1729 | XTextProperty *text_prop_return)
|
---|
1730 | {
|
---|
1731 | return XmbTextListToTextProperty(display, list, count, style,
|
---|
1732 | text_prop_return);
|
---|
1733 | }
|
---|
1734 |
|
---|
1735 | int XmbTextPropertyToTextList(Display *display,
|
---|
1736 | const XTextProperty *text_prop,
|
---|
1737 | char ***list_return, int *count_return)
|
---|
1738 | {
|
---|
1739 | int rc = 0;
|
---|
1740 | if (text_prop->nitems == 0)
|
---|
1741 | {
|
---|
1742 | *list_return = NULL;
|
---|
1743 | *count_return = 0;
|
---|
1744 | return 0;
|
---|
1745 | }
|
---|
1746 | /* Only accept simple ASCII properties */
|
---|
1747 | for (unsigned i = 0; i < text_prop->nitems; ++i)
|
---|
1748 | AssertReturn(!(text_prop->value[i] & 0x80), XConverterNotFound);
|
---|
1749 | char **ppList = (char **)RTMemAlloc(sizeof(char *));
|
---|
1750 | char *pValue = (char *)RTMemAlloc(text_prop->nitems + 1);
|
---|
1751 | if (pValue)
|
---|
1752 | {
|
---|
1753 | memcpy(pValue, text_prop->value, text_prop->nitems);
|
---|
1754 | pValue[text_prop->nitems] = 0;
|
---|
1755 | }
|
---|
1756 | if (ppList)
|
---|
1757 | *ppList = pValue;
|
---|
1758 | if (!ppList || !pValue)
|
---|
1759 | {
|
---|
1760 | RTMemFree(ppList);
|
---|
1761 | RTMemFree(pValue);
|
---|
1762 | rc = XNoMemory;
|
---|
1763 | }
|
---|
1764 | else
|
---|
1765 | {
|
---|
1766 | /* NULL-terminate the string */
|
---|
1767 | pValue[text_prop->nitems] = '\0';
|
---|
1768 | *count_return = 1;
|
---|
1769 | *list_return = ppList;
|
---|
1770 | }
|
---|
1771 | return rc;
|
---|
1772 | }
|
---|
1773 |
|
---|
1774 | int Xutf8TextPropertyToTextList(Display *display,
|
---|
1775 | const XTextProperty *text_prop,
|
---|
1776 | char ***list_return, int *count_return)
|
---|
1777 | {
|
---|
1778 | return XmbTextPropertyToTextList(display, text_prop, list_return,
|
---|
1779 | count_return);
|
---|
1780 | }
|
---|
1781 |
|
---|
1782 | void XtAppSetExitFlag(XtAppContext app_context) {}
|
---|
1783 |
|
---|
1784 | void XtDestroyWidget(Widget w) {}
|
---|
1785 |
|
---|
1786 | XtAppContext XtCreateApplicationContext(void) { return (XtAppContext)0xffff; }
|
---|
1787 |
|
---|
1788 | void XtDestroyApplicationContext(XtAppContext app_context) {}
|
---|
1789 |
|
---|
1790 | void XtToolkitInitialize(void) {}
|
---|
1791 |
|
---|
1792 | Boolean XtToolkitThreadInitialize(void) { return True; }
|
---|
1793 |
|
---|
1794 | Display *XtOpenDisplay(XtAppContext app_context,
|
---|
1795 | _Xconst _XtString display_string,
|
---|
1796 | _Xconst _XtString application_name,
|
---|
1797 | _Xconst _XtString application_class,
|
---|
1798 | XrmOptionDescRec *options, Cardinal num_options,
|
---|
1799 | int *argc, char **argv)
|
---|
1800 | { return (Display *)0xffff; }
|
---|
1801 |
|
---|
1802 | Widget XtVaAppCreateShell(_Xconst _XtString application_name,
|
---|
1803 | _Xconst _XtString application_class,
|
---|
1804 | WidgetClass widget_class, Display *display, ...)
|
---|
1805 | { return TEST_WIDGET; }
|
---|
1806 |
|
---|
1807 | void XtSetMappedWhenManaged(Widget widget, _XtBoolean mapped_when_managed) {}
|
---|
1808 |
|
---|
1809 | void XtRealizeWidget(Widget widget) {}
|
---|
1810 |
|
---|
1811 | XtInputId XtAppAddInput(XtAppContext app_context, int source,
|
---|
1812 | XtPointer condition, XtInputCallbackProc proc,
|
---|
1813 | XtPointer closure)
|
---|
1814 | { return 0xffff; }
|
---|
1815 |
|
---|
1816 | /* Atoms we need other than the formats we support. */
|
---|
1817 | static const char *g_apszSupAtoms[] =
|
---|
1818 | {
|
---|
1819 | "PRIMARY", "CLIPBOARD", "TARGETS", "MULTIPLE", "TIMESTAMP"
|
---|
1820 | };
|
---|
1821 |
|
---|
1822 | /* This just looks for the atom names in a couple of tables and returns an
|
---|
1823 | * index with an offset added. */
|
---|
1824 | Boolean XtConvertAndStore(Widget widget, _Xconst _XtString from_type,
|
---|
1825 | XrmValue* from, _Xconst _XtString to_type,
|
---|
1826 | XrmValue* to_in_out)
|
---|
1827 | {
|
---|
1828 | Boolean rc = False;
|
---|
1829 | /* What we support is: */
|
---|
1830 | AssertReturn(from_type == XtRString, False);
|
---|
1831 | AssertReturn(to_type == XtRAtom, False);
|
---|
1832 | for (unsigned i = 0; i < RT_ELEMENTS(g_aFormats); ++i)
|
---|
1833 | if (!strcmp(from->addr, g_aFormats[i].pcszAtom))
|
---|
1834 | {
|
---|
1835 | *(Atom *)(to_in_out->addr) = (Atom) (i + 0x1000);
|
---|
1836 | rc = True;
|
---|
1837 | }
|
---|
1838 | for (unsigned i = 0; i < RT_ELEMENTS(g_apszSupAtoms); ++i)
|
---|
1839 | if (!strcmp(from->addr, g_apszSupAtoms[i]))
|
---|
1840 | {
|
---|
1841 | *(Atom *)(to_in_out->addr) = (Atom) (i + 0x2000);
|
---|
1842 | rc = True;
|
---|
1843 | }
|
---|
1844 | Assert(rc == True); /* Have we missed any atoms? */
|
---|
1845 | return rc;
|
---|
1846 | }
|
---|
1847 |
|
---|
1848 | /* The current values of the X selection, which will be returned to the
|
---|
1849 | * XtGetSelectionValue callback. */
|
---|
1850 | static Atom g_selTarget[1] = { 0 };
|
---|
1851 | static Atom g_selType = 0;
|
---|
1852 | static const void *g_pSelData = NULL;
|
---|
1853 | static unsigned long g_cSelData = 0;
|
---|
1854 | static int g_selFormat = 0;
|
---|
1855 | static bool g_fTargetsTimeout = false;
|
---|
1856 | static bool g_fTargetsFailure = false;
|
---|
1857 |
|
---|
1858 | void XtGetSelectionValue(Widget widget, Atom selection, Atom target,
|
---|
1859 | XtSelectionCallbackProc callback,
|
---|
1860 | XtPointer closure, Time time)
|
---|
1861 | {
|
---|
1862 | unsigned long count = 0;
|
---|
1863 | int format = 0;
|
---|
1864 | Atom type = XA_STRING;
|
---|
1865 | if ( ( selection != clipGetAtom(NULL, "PRIMARY")
|
---|
1866 | && selection != clipGetAtom(NULL, "CLIPBOARD")
|
---|
1867 | && selection != clipGetAtom(NULL, "TARGETS"))
|
---|
1868 | || ( target != g_selTarget[0]
|
---|
1869 | && target != clipGetAtom(NULL, "TARGETS")))
|
---|
1870 | {
|
---|
1871 | /* Otherwise this is probably a caller error. */
|
---|
1872 | Assert(target != g_selTarget[0]);
|
---|
1873 | callback(widget, closure, &selection, &type, NULL, &count, &format);
|
---|
1874 | /* Could not convert to target. */
|
---|
1875 | return;
|
---|
1876 | }
|
---|
1877 | XtPointer pValue = NULL;
|
---|
1878 | if (target == clipGetAtom(NULL, "TARGETS"))
|
---|
1879 | {
|
---|
1880 | if (g_fTargetsFailure)
|
---|
1881 | pValue = NULL;
|
---|
1882 | else
|
---|
1883 | pValue = (XtPointer) RTMemDup(&g_selTarget, sizeof(g_selTarget));
|
---|
1884 | type = g_fTargetsTimeout ? XT_CONVERT_FAIL : XA_ATOM;
|
---|
1885 | count = g_fTargetsFailure ? 0 : RT_ELEMENTS(g_selTarget);
|
---|
1886 | format = 32;
|
---|
1887 | }
|
---|
1888 | else
|
---|
1889 | {
|
---|
1890 | pValue = (XtPointer) g_pSelData ? RTMemDup(g_pSelData, g_cSelData)
|
---|
1891 | : NULL;
|
---|
1892 | type = g_selType;
|
---|
1893 | count = g_pSelData ? g_cSelData : 0;
|
---|
1894 | format = g_selFormat;
|
---|
1895 | }
|
---|
1896 | if (!pValue)
|
---|
1897 | {
|
---|
1898 | count = 0;
|
---|
1899 | format = 0;
|
---|
1900 | }
|
---|
1901 | callback(widget, closure, &selection, &type, pValue,
|
---|
1902 | &count, &format);
|
---|
1903 | }
|
---|
1904 |
|
---|
1905 | /* The formats currently on offer from X11 via the shared clipboard */
|
---|
1906 | static uint32_t g_fX11Formats = 0;
|
---|
1907 |
|
---|
1908 | void ClipReportX11Formats(VBOXCLIPBOARDCONTEXT* pCtx,
|
---|
1909 | uint32_t u32Formats)
|
---|
1910 | {
|
---|
1911 | g_fX11Formats = u32Formats;
|
---|
1912 | }
|
---|
1913 |
|
---|
1914 | static uint32_t clipQueryFormats()
|
---|
1915 | {
|
---|
1916 | return g_fX11Formats;
|
---|
1917 | }
|
---|
1918 |
|
---|
1919 | static void clipInvalidateFormats()
|
---|
1920 | {
|
---|
1921 | g_fX11Formats = ~0;
|
---|
1922 | }
|
---|
1923 |
|
---|
1924 | /* Does our clipboard code currently own the selection? */
|
---|
1925 | static bool g_ownsSel = false;
|
---|
1926 | /* The procedure that is called when we should convert the selection to a
|
---|
1927 | * given format. */
|
---|
1928 | static XtConvertSelectionProc g_pfnSelConvert = NULL;
|
---|
1929 | /* The procedure which is called when we lose the selection. */
|
---|
1930 | static XtLoseSelectionProc g_pfnSelLose = NULL;
|
---|
1931 | /* The procedure which is called when the selection transfer has completed. */
|
---|
1932 | static XtSelectionDoneProc g_pfnSelDone = NULL;
|
---|
1933 |
|
---|
1934 | Boolean XtOwnSelection(Widget widget, Atom selection, Time time,
|
---|
1935 | XtConvertSelectionProc convert,
|
---|
1936 | XtLoseSelectionProc lose,
|
---|
1937 | XtSelectionDoneProc done)
|
---|
1938 | {
|
---|
1939 | if (selection != clipGetAtom(NULL, "CLIPBOARD"))
|
---|
1940 | return True; /* We don't really care about this. */
|
---|
1941 | g_ownsSel = true; /* Always succeed. */
|
---|
1942 | g_pfnSelConvert = convert;
|
---|
1943 | g_pfnSelLose = lose;
|
---|
1944 | g_pfnSelDone = done;
|
---|
1945 | return True;
|
---|
1946 | }
|
---|
1947 |
|
---|
1948 | void XtDisownSelection(Widget widget, Atom selection, Time time)
|
---|
1949 | {
|
---|
1950 | g_ownsSel = false;
|
---|
1951 | g_pfnSelConvert = NULL;
|
---|
1952 | g_pfnSelLose = NULL;
|
---|
1953 | g_pfnSelDone = NULL;
|
---|
1954 | }
|
---|
1955 |
|
---|
1956 | /* Request the shared clipboard to convert its data to a given format. */
|
---|
1957 | static bool clipConvertSelection(const char *pcszTarget, Atom *type,
|
---|
1958 | XtPointer *value, unsigned long *length,
|
---|
1959 | int *format)
|
---|
1960 | {
|
---|
1961 | Atom target = clipGetAtom(NULL, pcszTarget);
|
---|
1962 | if (target == 0)
|
---|
1963 | return false;
|
---|
1964 | /* Initialise all return values in case we make a quick exit. */
|
---|
1965 | *type = XA_STRING;
|
---|
1966 | *value = NULL;
|
---|
1967 | *length = 0;
|
---|
1968 | *format = 0;
|
---|
1969 | if (!g_ownsSel)
|
---|
1970 | return false;
|
---|
1971 | if (!g_pfnSelConvert)
|
---|
1972 | return false;
|
---|
1973 | Atom clipAtom = clipGetAtom(NULL, "CLIPBOARD");
|
---|
1974 | if (!g_pfnSelConvert(TEST_WIDGET, &clipAtom, &target, type,
|
---|
1975 | value, length, format))
|
---|
1976 | return false;
|
---|
1977 | if (g_pfnSelDone)
|
---|
1978 | g_pfnSelDone(TEST_WIDGET, &clipAtom, &target);
|
---|
1979 | return true;
|
---|
1980 | }
|
---|
1981 |
|
---|
1982 | /* Set the current X selection data */
|
---|
1983 | static void clipSetSelectionValues(const char *pcszTarget, Atom type,
|
---|
1984 | const void *data,
|
---|
1985 | unsigned long count, int format)
|
---|
1986 | {
|
---|
1987 | Atom clipAtom = clipGetAtom(NULL, "CLIPBOARD");
|
---|
1988 | g_selTarget[0] = clipGetAtom(NULL, pcszTarget);
|
---|
1989 | g_selType = type;
|
---|
1990 | g_pSelData = data;
|
---|
1991 | g_cSelData = count;
|
---|
1992 | g_selFormat = format;
|
---|
1993 | if (g_pfnSelLose)
|
---|
1994 | g_pfnSelLose(TEST_WIDGET, &clipAtom);
|
---|
1995 | g_ownsSel = false;
|
---|
1996 | g_fTargetsTimeout = false;
|
---|
1997 | g_fTargetsFailure = false;
|
---|
1998 | }
|
---|
1999 |
|
---|
2000 | static void clipSendTargetUpdate(CLIPBACKEND *pCtx)
|
---|
2001 | {
|
---|
2002 | clipUpdateX11Targets(pCtx, g_selTarget, RT_ELEMENTS(g_selTarget));
|
---|
2003 | }
|
---|
2004 |
|
---|
2005 | /* Configure if and how the X11 TARGETS clipboard target will fail */
|
---|
2006 | static void clipSetTargetsFailure(bool fTimeout, bool fFailure)
|
---|
2007 | {
|
---|
2008 | g_fTargetsTimeout = fTimeout;
|
---|
2009 | g_fTargetsFailure = fFailure;
|
---|
2010 | }
|
---|
2011 |
|
---|
2012 | char *XtMalloc(Cardinal size) { return (char *) RTMemAlloc(size); }
|
---|
2013 |
|
---|
2014 | char *XGetAtomName(Display *display, Atom atom)
|
---|
2015 | {
|
---|
2016 | AssertReturn((unsigned)atom < RT_ELEMENTS(g_aFormats) + 1, NULL);
|
---|
2017 | const char *pcszName = NULL;
|
---|
2018 | if (atom < 0x1000)
|
---|
2019 | return NULL;
|
---|
2020 | else if (0x1000 <= atom && atom < 0x2000)
|
---|
2021 | {
|
---|
2022 | unsigned index = atom - 0x1000;
|
---|
2023 | AssertReturn(index < RT_ELEMENTS(g_aFormats), NULL);
|
---|
2024 | pcszName = g_aFormats[index].pcszAtom;
|
---|
2025 | }
|
---|
2026 | else
|
---|
2027 | {
|
---|
2028 | unsigned index = atom - 0x2000;
|
---|
2029 | AssertReturn(index < RT_ELEMENTS(g_apszSupAtoms), NULL);
|
---|
2030 | pcszName = g_apszSupAtoms[index];
|
---|
2031 | }
|
---|
2032 | return (char *)RTMemDup(pcszName, sizeof(pcszName) + 1);
|
---|
2033 | }
|
---|
2034 |
|
---|
2035 | int XFree(void *data)
|
---|
2036 | {
|
---|
2037 | RTMemFree(data);
|
---|
2038 | return 0;
|
---|
2039 | }
|
---|
2040 |
|
---|
2041 | void XFreeStringList(char **list)
|
---|
2042 | {
|
---|
2043 | if (list)
|
---|
2044 | RTMemFree(*list);
|
---|
2045 | RTMemFree(list);
|
---|
2046 | }
|
---|
2047 |
|
---|
2048 | #define MAX_BUF_SIZE 256
|
---|
2049 |
|
---|
2050 | static int g_completedRC = VINF_SUCCESS;
|
---|
2051 | static int g_completedCB = 0;
|
---|
2052 | static CLIPREADCBREQ *g_completedReq = NULL;
|
---|
2053 | static char g_completedBuf[MAX_BUF_SIZE];
|
---|
2054 |
|
---|
2055 | void ClipCompleteDataRequestFromX11(VBOXCLIPBOARDCONTEXT *pCtx, int rc,
|
---|
2056 | CLIPREADCBREQ *pReq, void *pv,
|
---|
2057 | uint32_t cb)
|
---|
2058 | {
|
---|
2059 | if (cb <= MAX_BUF_SIZE)
|
---|
2060 | {
|
---|
2061 | g_completedRC = rc;
|
---|
2062 | memcpy(g_completedBuf, pv, cb);
|
---|
2063 | }
|
---|
2064 | else
|
---|
2065 | g_completedRC = VERR_BUFFER_OVERFLOW;
|
---|
2066 | g_completedCB = cb;
|
---|
2067 | g_completedReq = pReq;
|
---|
2068 | }
|
---|
2069 |
|
---|
2070 | static void clipGetCompletedRequest(int *prc, char ** ppc, uint32_t *pcb,
|
---|
2071 | CLIPREADCBREQ **ppReq)
|
---|
2072 | {
|
---|
2073 | *prc = g_completedRC;
|
---|
2074 | *ppc = g_completedBuf;
|
---|
2075 | *pcb = g_completedCB;
|
---|
2076 | *ppReq = g_completedReq;
|
---|
2077 | }
|
---|
2078 | #ifdef RT_OS_SOLARIS_10
|
---|
2079 | char XtStrings [] = "";
|
---|
2080 | _WidgetClassRec* applicationShellWidgetClass;
|
---|
2081 | char XtShellStrings [] = "";
|
---|
2082 | int XmbTextPropertyToTextList(
|
---|
2083 | Display* /* display */,
|
---|
2084 | XTextProperty* /* text_prop */,
|
---|
2085 | char*** /* list_return */,
|
---|
2086 | int* /* count_return */
|
---|
2087 | )
|
---|
2088 | {
|
---|
2089 | return 0;
|
---|
2090 | }
|
---|
2091 | #else
|
---|
2092 | const char XtStrings [] = "";
|
---|
2093 | _WidgetClassRec* applicationShellWidgetClass;
|
---|
2094 | const char XtShellStrings [] = "";
|
---|
2095 | #endif
|
---|
2096 |
|
---|
2097 | static void testStringFromX11(RTTEST hTest, CLIPBACKEND *pCtx,
|
---|
2098 | const char *pcszExp, int rcExp)
|
---|
2099 | {
|
---|
2100 | bool retval = true;
|
---|
2101 | clipSendTargetUpdate(pCtx);
|
---|
2102 | if (clipQueryFormats() != VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT)
|
---|
2103 | RTTestFailed(hTest, "Wrong targets reported: %02X\n",
|
---|
2104 | clipQueryFormats());
|
---|
2105 | else
|
---|
2106 | {
|
---|
2107 | char *pc;
|
---|
2108 | CLIPREADCBREQ *pReq = (CLIPREADCBREQ *)&pReq, *pReqRet = NULL;
|
---|
2109 | ClipRequestDataFromX11(pCtx, VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT,
|
---|
2110 | pReq);
|
---|
2111 | int rc = VINF_SUCCESS;
|
---|
2112 | uint32_t cbActual = 0;
|
---|
2113 | clipGetCompletedRequest(&rc, &pc, &cbActual, &pReqRet);
|
---|
2114 | if (rc != rcExp)
|
---|
2115 | RTTestFailed(hTest, "Wrong return code, expected %Rrc, got %Rrc\n",
|
---|
2116 | rcExp, rc);
|
---|
2117 | else if (pReqRet != pReq)
|
---|
2118 | RTTestFailed(hTest, "Wrong returned request data, expected %p, got %p\n",
|
---|
2119 | pReq, pReqRet);
|
---|
2120 | else if (RT_FAILURE(rcExp))
|
---|
2121 | retval = true;
|
---|
2122 | else
|
---|
2123 | {
|
---|
2124 | RTUTF16 wcExp[MAX_BUF_SIZE / 2];
|
---|
2125 | RTUTF16 *pwcExp = wcExp;
|
---|
2126 | size_t cwc = 0;
|
---|
2127 | rc = RTStrToUtf16Ex(pcszExp, RTSTR_MAX, &pwcExp,
|
---|
2128 | RT_ELEMENTS(wcExp), &cwc);
|
---|
2129 | size_t cbExp = cwc * 2 + 2;
|
---|
2130 | AssertRC(rc);
|
---|
2131 | if (RT_SUCCESS(rc))
|
---|
2132 | {
|
---|
2133 | if (cbActual != cbExp)
|
---|
2134 | {
|
---|
2135 | RTTestFailed(hTest, "Returned string is the wrong size, string \"%.*ls\", size %u, expected \"%s\", size %u\n",
|
---|
2136 | RT_MIN(MAX_BUF_SIZE, cbActual), pc, cbActual,
|
---|
2137 | pcszExp, cbExp);
|
---|
2138 | }
|
---|
2139 | else
|
---|
2140 | {
|
---|
2141 | if (memcmp(pc, wcExp, cbExp) == 0)
|
---|
2142 | retval = true;
|
---|
2143 | else
|
---|
2144 | RTTestFailed(hTest, "Returned string \"%.*ls\" does not match expected string \"%s\"\n",
|
---|
2145 | MAX_BUF_SIZE, pc, pcszExp);
|
---|
2146 | }
|
---|
2147 | }
|
---|
2148 | }
|
---|
2149 | }
|
---|
2150 | if (!retval)
|
---|
2151 | RTTestFailureDetails(hTest, "Expected: string \"%s\", rc %Rrc\n",
|
---|
2152 | pcszExp, rcExp);
|
---|
2153 | }
|
---|
2154 |
|
---|
2155 | static void testLatin1FromX11(RTTEST hTest, CLIPBACKEND *pCtx,
|
---|
2156 | const char *pcszExp, int rcExp)
|
---|
2157 | {
|
---|
2158 | bool retval = false;
|
---|
2159 | clipSendTargetUpdate(pCtx);
|
---|
2160 | if (clipQueryFormats() != VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT)
|
---|
2161 | RTTestFailed(hTest, "Wrong targets reported: %02X\n",
|
---|
2162 | clipQueryFormats());
|
---|
2163 | else
|
---|
2164 | {
|
---|
2165 | char *pc;
|
---|
2166 | CLIPREADCBREQ *pReq = (CLIPREADCBREQ *)&pReq, *pReqRet = NULL;
|
---|
2167 | ClipRequestDataFromX11(pCtx, VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT,
|
---|
2168 | pReq);
|
---|
2169 | int rc = VINF_SUCCESS;
|
---|
2170 | uint32_t cbActual = 0;
|
---|
2171 | clipGetCompletedRequest(&rc, &pc, &cbActual, &pReqRet);
|
---|
2172 | if (rc != rcExp)
|
---|
2173 | RTTestFailed(hTest, "Wrong return code, expected %Rrc, got %Rrc\n",
|
---|
2174 | rcExp, rc);
|
---|
2175 | else if (pReqRet != pReq)
|
---|
2176 | RTTestFailed(hTest, "Wrong returned request data, expected %p, got %p\n",
|
---|
2177 | pReq, pReqRet);
|
---|
2178 | else if (RT_FAILURE(rcExp))
|
---|
2179 | retval = true;
|
---|
2180 | else
|
---|
2181 | {
|
---|
2182 | RTUTF16 wcExp[MAX_BUF_SIZE / 2];
|
---|
2183 | RTUTF16 *pwcExp = wcExp;
|
---|
2184 | size_t cwc;
|
---|
2185 | for (cwc = 0; cwc == 0 || pcszExp[cwc - 1] != '\0'; ++cwc)
|
---|
2186 | wcExp[cwc] = pcszExp[cwc];
|
---|
2187 | size_t cbExp = cwc * 2;
|
---|
2188 | if (cbActual != cbExp)
|
---|
2189 | {
|
---|
2190 | RTTestFailed(hTest, "Returned string is the wrong size, string \"%.*ls\", size %u, expected \"%s\", size %u\n",
|
---|
2191 | RT_MIN(MAX_BUF_SIZE, cbActual), pc, cbActual,
|
---|
2192 | pcszExp, cbExp);
|
---|
2193 | }
|
---|
2194 | else
|
---|
2195 | {
|
---|
2196 | if (memcmp(pc, wcExp, cbExp) == 0)
|
---|
2197 | retval = true;
|
---|
2198 | else
|
---|
2199 | RTTestFailed(hTest, "Returned string \"%.*ls\" does not match expected string \"%s\"\n",
|
---|
2200 | MAX_BUF_SIZE, pc, pcszExp);
|
---|
2201 | }
|
---|
2202 | }
|
---|
2203 | }
|
---|
2204 | if (!retval)
|
---|
2205 | RTTestFailureDetails(hTest, "Expected: string \"%s\", rc %Rrc\n",
|
---|
2206 | pcszExp, rcExp);
|
---|
2207 | }
|
---|
2208 |
|
---|
2209 | static void testStringFromVBox(RTTEST hTest, CLIPBACKEND *pCtx,
|
---|
2210 | const char *pcszTarget, Atom typeExp,
|
---|
2211 | const char *valueExp)
|
---|
2212 | {
|
---|
2213 | bool retval = false;
|
---|
2214 | Atom type;
|
---|
2215 | XtPointer value = NULL;
|
---|
2216 | unsigned long length;
|
---|
2217 | int format;
|
---|
2218 | size_t lenExp = strlen(valueExp);
|
---|
2219 | if (clipConvertSelection(pcszTarget, &type, &value, &length, &format))
|
---|
2220 | {
|
---|
2221 | if ( type != typeExp
|
---|
2222 | || length != lenExp
|
---|
2223 | || format != 8
|
---|
2224 | || memcmp((const void *) value, (const void *)valueExp,
|
---|
2225 | lenExp))
|
---|
2226 | {
|
---|
2227 | RTTestFailed(hTest, "Bad data: type %d, (expected %d), length %u, (%u), format %d (%d), value \"%.*s\" (\"%.*s\")\n",
|
---|
2228 | type, typeExp, length, lenExp, format, 8,
|
---|
2229 | RT_MIN(length, 20), value, RT_MIN(lenExp, 20), valueExp);
|
---|
2230 | }
|
---|
2231 | else
|
---|
2232 | retval = true;
|
---|
2233 | }
|
---|
2234 | else
|
---|
2235 | RTTestFailed(hTest, "Conversion failed\n");
|
---|
2236 | XtFree((char *)value);
|
---|
2237 | if (!retval)
|
---|
2238 | RTTestFailureDetails(hTest, "Conversion to %s, expected \"%s\"\n",
|
---|
2239 | pcszTarget, valueExp);
|
---|
2240 | }
|
---|
2241 |
|
---|
2242 | static void testNoX11(CLIPBACKEND *pCtx, const char *pcszTestCtx)
|
---|
2243 | {
|
---|
2244 | CLIPREADCBREQ *pReq = (CLIPREADCBREQ *)&pReq, *pReqRet = NULL;
|
---|
2245 | int rc = ClipRequestDataFromX11(pCtx,
|
---|
2246 | VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT,
|
---|
2247 | pReq);
|
---|
2248 | RTTESTI_CHECK_MSG(rc == VERR_NO_DATA, ("context: %s\n", pcszTestCtx));
|
---|
2249 | }
|
---|
2250 |
|
---|
2251 | static void testStringFromVBoxFailed(RTTEST hTest, CLIPBACKEND *pCtx,
|
---|
2252 | const char *pcszTarget)
|
---|
2253 | {
|
---|
2254 | bool retval = false;
|
---|
2255 | Atom type;
|
---|
2256 | XtPointer value = NULL;
|
---|
2257 | unsigned long length;
|
---|
2258 | int format;
|
---|
2259 | RTTEST_CHECK_MSG(hTest, !clipConvertSelection(pcszTarget, &type, &value,
|
---|
2260 | &length, &format),
|
---|
2261 | (hTest, "Conversion to target %s, should have failed but didn't, returned type %d, length %u, format %d, value \"%.*s\"\n",
|
---|
2262 | pcszTarget, type, length, format, RT_MIN(length, 20),
|
---|
2263 | value));
|
---|
2264 | XtFree((char *)value);
|
---|
2265 | }
|
---|
2266 |
|
---|
2267 | static void testNoSelectionOwnership(CLIPBACKEND *pCtx,
|
---|
2268 | const char *pcszTestCtx)
|
---|
2269 | {
|
---|
2270 | RTTESTI_CHECK_MSG(!g_ownsSel, ("context: %s\n", pcszTestCtx));
|
---|
2271 | }
|
---|
2272 |
|
---|
2273 | int main()
|
---|
2274 | {
|
---|
2275 | /*
|
---|
2276 | * Init the runtime, test and say hello.
|
---|
2277 | */
|
---|
2278 | RTTEST hTest;
|
---|
2279 | int rc = RTTestInitAndCreate("tstClipboardX11", &hTest);
|
---|
2280 | if (rc)
|
---|
2281 | return rc;
|
---|
2282 | RTTestBanner(hTest);
|
---|
2283 |
|
---|
2284 | /*
|
---|
2285 | * Run the test.
|
---|
2286 | */
|
---|
2287 | CLIPBACKEND *pCtx = ClipConstructX11(NULL, false);
|
---|
2288 | char *pc;
|
---|
2289 | uint32_t cbActual;
|
---|
2290 | CLIPREADCBREQ *pReq = (CLIPREADCBREQ *)&pReq, *pReqRet = NULL;
|
---|
2291 | rc = ClipStartX11(pCtx);
|
---|
2292 | AssertRCReturn(rc, 1);
|
---|
2293 |
|
---|
2294 | /*** Utf-8 from X11 ***/
|
---|
2295 | RTTestSub(hTest, "reading Utf-8 from X11");
|
---|
2296 | /* Simple test */
|
---|
2297 | clipSetSelectionValues("UTF8_STRING", XA_STRING, "hello world",
|
---|
2298 | sizeof("hello world"), 8);
|
---|
2299 | testStringFromX11(hTest, pCtx, "hello world", VINF_SUCCESS);
|
---|
2300 | /* With an embedded carriage return */
|
---|
2301 | clipSetSelectionValues("text/plain;charset=UTF-8", XA_STRING,
|
---|
2302 | "hello\nworld", sizeof("hello\nworld"), 8);
|
---|
2303 | testStringFromX11(hTest, pCtx, "hello\r\nworld", VINF_SUCCESS);
|
---|
2304 | /* With an embedded CRLF */
|
---|
2305 | clipSetSelectionValues("text/plain;charset=UTF-8", XA_STRING,
|
---|
2306 | "hello\r\nworld", sizeof("hello\r\nworld"), 8);
|
---|
2307 | testStringFromX11(hTest, pCtx, "hello\r\r\nworld", VINF_SUCCESS);
|
---|
2308 | /* With an embedded LFCR */
|
---|
2309 | clipSetSelectionValues("text/plain;charset=UTF-8", XA_STRING,
|
---|
2310 | "hello\n\rworld", sizeof("hello\n\rworld"), 8);
|
---|
2311 | testStringFromX11(hTest, pCtx, "hello\r\n\rworld", VINF_SUCCESS);
|
---|
2312 | /* An empty string */
|
---|
2313 | clipSetSelectionValues("text/plain;charset=utf-8", XA_STRING, "",
|
---|
2314 | sizeof(""), 8);
|
---|
2315 | testStringFromX11(hTest, pCtx, "", VINF_SUCCESS);
|
---|
2316 | /* With an embedded Utf-8 character. */
|
---|
2317 | clipSetSelectionValues("STRING", XA_STRING,
|
---|
2318 | "100\xE2\x82\xAC" /* 100 Euro */,
|
---|
2319 | sizeof("100\xE2\x82\xAC"), 8);
|
---|
2320 | testStringFromX11(hTest, pCtx, "100\xE2\x82\xAC", VINF_SUCCESS);
|
---|
2321 | /* A non-zero-terminated string */
|
---|
2322 | clipSetSelectionValues("TEXT", XA_STRING,
|
---|
2323 | "hello world", sizeof("hello world") - 1, 8);
|
---|
2324 | testStringFromX11(hTest, pCtx, "hello world", VINF_SUCCESS);
|
---|
2325 |
|
---|
2326 | /*** COMPOUND TEXT from X11 ***/
|
---|
2327 | RTTestSub(hTest, "reading compound text from X11");
|
---|
2328 | /* Simple test */
|
---|
2329 | clipSetSelectionValues("COMPOUND_TEXT", XA_STRING, "hello world",
|
---|
2330 | sizeof("hello world"), 8);
|
---|
2331 | testStringFromX11(hTest, pCtx, "hello world", VINF_SUCCESS);
|
---|
2332 | /* With an embedded carriage return */
|
---|
2333 | clipSetSelectionValues("COMPOUND_TEXT", XA_STRING, "hello\nworld",
|
---|
2334 | sizeof("hello\nworld"), 8);
|
---|
2335 | testStringFromX11(hTest, pCtx, "hello\r\nworld", VINF_SUCCESS);
|
---|
2336 | /* With an embedded CRLF */
|
---|
2337 | clipSetSelectionValues("COMPOUND_TEXT", XA_STRING, "hello\r\nworld",
|
---|
2338 | sizeof("hello\r\nworld"), 8);
|
---|
2339 | testStringFromX11(hTest, pCtx, "hello\r\r\nworld", VINF_SUCCESS);
|
---|
2340 | /* With an embedded LFCR */
|
---|
2341 | clipSetSelectionValues("COMPOUND_TEXT", XA_STRING, "hello\n\rworld",
|
---|
2342 | sizeof("hello\n\rworld"), 8);
|
---|
2343 | testStringFromX11(hTest, pCtx, "hello\r\n\rworld", VINF_SUCCESS);
|
---|
2344 | /* An empty string */
|
---|
2345 | clipSetSelectionValues("COMPOUND_TEXT", XA_STRING, "",
|
---|
2346 | sizeof(""), 8);
|
---|
2347 | testStringFromX11(hTest, pCtx, "", VINF_SUCCESS);
|
---|
2348 | /* A non-zero-terminated string */
|
---|
2349 | clipSetSelectionValues("COMPOUND_TEXT", XA_STRING,
|
---|
2350 | "hello world", sizeof("hello world") - 1, 8);
|
---|
2351 | testStringFromX11(hTest, pCtx, "hello world", VINF_SUCCESS);
|
---|
2352 |
|
---|
2353 | /*** Latin1 from X11 ***/
|
---|
2354 | RTTestSub(hTest, "reading Latin1 from X11");
|
---|
2355 | /* Simple test */
|
---|
2356 | clipSetSelectionValues("STRING", XA_STRING, "Georges Dupr\xEA",
|
---|
2357 | sizeof("Georges Dupr\xEA"), 8);
|
---|
2358 | testLatin1FromX11(hTest, pCtx, "Georges Dupr\xEA", VINF_SUCCESS);
|
---|
2359 | /* With an embedded carriage return */
|
---|
2360 | clipSetSelectionValues("TEXT", XA_STRING, "Georges\nDupr\xEA",
|
---|
2361 | sizeof("Georges\nDupr\xEA"), 8);
|
---|
2362 | testLatin1FromX11(hTest, pCtx, "Georges\r\nDupr\xEA", VINF_SUCCESS);
|
---|
2363 | /* With an embedded CRLF */
|
---|
2364 | clipSetSelectionValues("TEXT", XA_STRING, "Georges\r\nDupr\xEA",
|
---|
2365 | sizeof("Georges\r\nDupr\xEA"), 8);
|
---|
2366 | testLatin1FromX11(hTest, pCtx, "Georges\r\r\nDupr\xEA", VINF_SUCCESS);
|
---|
2367 | /* With an embedded LFCR */
|
---|
2368 | clipSetSelectionValues("TEXT", XA_STRING, "Georges\n\rDupr\xEA",
|
---|
2369 | sizeof("Georges\n\rDupr\xEA"), 8);
|
---|
2370 | testLatin1FromX11(hTest, pCtx, "Georges\r\n\rDupr\xEA", VINF_SUCCESS);
|
---|
2371 | /* A non-zero-terminated string */
|
---|
2372 | clipSetSelectionValues("text/plain", XA_STRING,
|
---|
2373 | "Georges Dupr\xEA!",
|
---|
2374 | sizeof("Georges Dupr\xEA!") - 1, 8);
|
---|
2375 | testLatin1FromX11(hTest, pCtx, "Georges Dupr\xEA!", VINF_SUCCESS);
|
---|
2376 |
|
---|
2377 | /*** Unknown X11 format ***/
|
---|
2378 | RTTestSub(hTest, "handling of an unknown X11 format");
|
---|
2379 | clipInvalidateFormats();
|
---|
2380 | clipSetSelectionValues("CLIPBOARD", XA_STRING, "Test",
|
---|
2381 | sizeof("Test"), 8);
|
---|
2382 | clipSendTargetUpdate(pCtx);
|
---|
2383 | RTTEST_CHECK_MSG(hTest, clipQueryFormats() == 0,
|
---|
2384 | (hTest, "Failed to send a format update notification\n"));
|
---|
2385 |
|
---|
2386 | /*** Timeout from X11 ***/
|
---|
2387 | RTTestSub(hTest, "X11 timeout");
|
---|
2388 | clipSetSelectionValues("UTF8_STRING", XT_CONVERT_FAIL, "hello world",
|
---|
2389 | sizeof("hello world"), 8);
|
---|
2390 | testStringFromX11(hTest, pCtx, "hello world", VERR_TIMEOUT);
|
---|
2391 |
|
---|
2392 | /*** No data in X11 clipboard ***/
|
---|
2393 | RTTestSub(hTest, "a data request from an empty X11 clipboard");
|
---|
2394 | clipSetSelectionValues("UTF8_STRING", XA_STRING, NULL,
|
---|
2395 | 0, 8);
|
---|
2396 | ClipRequestDataFromX11(pCtx, VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT,
|
---|
2397 | pReq);
|
---|
2398 | clipGetCompletedRequest(&rc, &pc, &cbActual, &pReqRet);
|
---|
2399 | RTTEST_CHECK_MSG(hTest, rc == VERR_NO_DATA,
|
---|
2400 | (hTest, "Returned %Rrc instead of VERR_NO_DATA\n",
|
---|
2401 | rc));
|
---|
2402 | RTTEST_CHECK_MSG(hTest, pReqRet == pReq,
|
---|
2403 | (hTest, "Wrong returned request data, expected %p, got %p\n",
|
---|
2404 | pReq, pReqRet));
|
---|
2405 |
|
---|
2406 | /*** Ensure that VBox is notified when we return the CB to X11 ***/
|
---|
2407 | RTTestSub(hTest, "notification of switch to X11 clipboard");
|
---|
2408 | clipInvalidateFormats();
|
---|
2409 | clipReportEmptyX11CB(pCtx);
|
---|
2410 | RTTEST_CHECK_MSG(hTest, clipQueryFormats() == 0,
|
---|
2411 | (hTest, "Failed to send a format update (release) notification\n"));
|
---|
2412 |
|
---|
2413 | /*** request for an invalid VBox format from X11 ***/
|
---|
2414 | RTTestSub(hTest, "a request for an invalid VBox format from X11");
|
---|
2415 | ClipRequestDataFromX11(pCtx, 0xffff, pReq);
|
---|
2416 | clipGetCompletedRequest(&rc, &pc, &cbActual, &pReqRet);
|
---|
2417 | RTTEST_CHECK_MSG(hTest, rc == VERR_NOT_IMPLEMENTED,
|
---|
2418 | (hTest, "Returned %Rrc instead of VERR_NOT_IMPLEMENTED\n",
|
---|
2419 | rc));
|
---|
2420 | RTTEST_CHECK_MSG(hTest, pReqRet == pReq,
|
---|
2421 | (hTest, "Wrong returned request data, expected %p, got %p\n",
|
---|
2422 | pReq, pReqRet));
|
---|
2423 |
|
---|
2424 | /*** Targets failure from X11 ***/
|
---|
2425 | RTTestSub(hTest, "X11 targets conversion failure");
|
---|
2426 | clipSetSelectionValues("UTF8_STRING", XA_STRING, "hello world",
|
---|
2427 | sizeof("hello world"), 8);
|
---|
2428 | clipSetTargetsFailure(false, true);
|
---|
2429 | Atom atom = XA_STRING;
|
---|
2430 | long unsigned int cLen = 0;
|
---|
2431 | int format = 8;
|
---|
2432 | clipConvertX11Targets(NULL, (XtPointer) pCtx, NULL, &atom, NULL, &cLen,
|
---|
2433 | &format);
|
---|
2434 | RTTEST_CHECK_MSG(hTest, clipQueryFormats() == 0,
|
---|
2435 | (hTest, "Wrong targets reported: %02X\n",
|
---|
2436 | clipQueryFormats()));
|
---|
2437 |
|
---|
2438 | /*** X11 text format conversion ***/
|
---|
2439 | RTTestSub(hTest, "handling of X11 selection targets");
|
---|
2440 | RTTEST_CHECK_MSG(hTest, clipTestTextFormatConversion(pCtx),
|
---|
2441 | (hTest, "failed to select the right X11 text formats\n"));
|
---|
2442 |
|
---|
2443 | /*** Utf-8 from VBox ***/
|
---|
2444 | RTTestSub(hTest, "reading Utf-8 from VBox");
|
---|
2445 | /* Simple test */
|
---|
2446 | clipSetVBoxUtf16(pCtx, VINF_SUCCESS, "hello world",
|
---|
2447 | sizeof("hello world") * 2);
|
---|
2448 | testStringFromVBox(hTest, pCtx, "UTF8_STRING",
|
---|
2449 | clipGetAtom(NULL, "UTF8_STRING"), "hello world");
|
---|
2450 | /* With an embedded carriage return */
|
---|
2451 | clipSetVBoxUtf16(pCtx, VINF_SUCCESS, "hello\r\nworld",
|
---|
2452 | sizeof("hello\r\nworld") * 2);
|
---|
2453 | testStringFromVBox(hTest, pCtx, "text/plain;charset=UTF-8",
|
---|
2454 | clipGetAtom(NULL, "text/plain;charset=UTF-8"),
|
---|
2455 | "hello\nworld");
|
---|
2456 | /* With an embedded CRCRLF */
|
---|
2457 | clipSetVBoxUtf16(pCtx, VINF_SUCCESS, "hello\r\r\nworld",
|
---|
2458 | sizeof("hello\r\r\nworld") * 2);
|
---|
2459 | testStringFromVBox(hTest, pCtx, "text/plain;charset=UTF-8",
|
---|
2460 | clipGetAtom(NULL, "text/plain;charset=UTF-8"),
|
---|
2461 | "hello\r\nworld");
|
---|
2462 | /* With an embedded CRLFCR */
|
---|
2463 | clipSetVBoxUtf16(pCtx, VINF_SUCCESS, "hello\r\n\rworld",
|
---|
2464 | sizeof("hello\r\n\rworld") * 2);
|
---|
2465 | testStringFromVBox(hTest, pCtx, "text/plain;charset=UTF-8",
|
---|
2466 | clipGetAtom(NULL, "text/plain;charset=UTF-8"),
|
---|
2467 | "hello\n\rworld");
|
---|
2468 | /* An empty string */
|
---|
2469 | clipSetVBoxUtf16(pCtx, VINF_SUCCESS, "", 2);
|
---|
2470 | testStringFromVBox(hTest, pCtx, "text/plain;charset=utf-8",
|
---|
2471 | clipGetAtom(NULL, "text/plain;charset=utf-8"), "");
|
---|
2472 | /* With an embedded Utf-8 character. */
|
---|
2473 | clipSetVBoxUtf16(pCtx, VINF_SUCCESS, "100\xE2\x82\xAC" /* 100 Euro */,
|
---|
2474 | 10);
|
---|
2475 | testStringFromVBox(hTest, pCtx, "STRING",
|
---|
2476 | clipGetAtom(NULL, "STRING"), "100\xE2\x82\xAC");
|
---|
2477 | /* A non-zero-terminated string */
|
---|
2478 | clipSetVBoxUtf16(pCtx, VINF_SUCCESS, "hello world",
|
---|
2479 | sizeof("hello world") * 2 - 2);
|
---|
2480 | testStringFromVBox(hTest, pCtx, "TEXT", clipGetAtom(NULL, "TEXT"),
|
---|
2481 | "hello world");
|
---|
2482 |
|
---|
2483 | /*** COMPOUND TEXT from VBox ***/
|
---|
2484 | RTTestSub(hTest, "reading COMPOUND TEXT from VBox");
|
---|
2485 | /* Simple test */
|
---|
2486 | clipSetVBoxUtf16(pCtx, VINF_SUCCESS, "hello world",
|
---|
2487 | sizeof("hello world") * 2);
|
---|
2488 | testStringFromVBox(hTest, pCtx, "COMPOUND_TEXT",
|
---|
2489 | clipGetAtom(NULL, "COMPOUND_TEXT"), "hello world");
|
---|
2490 | /* With an embedded carriage return */
|
---|
2491 | clipSetVBoxUtf16(pCtx, VINF_SUCCESS, "hello\r\nworld",
|
---|
2492 | sizeof("hello\r\nworld") * 2);
|
---|
2493 | testStringFromVBox(hTest, pCtx, "COMPOUND_TEXT",
|
---|
2494 | clipGetAtom(NULL, "COMPOUND_TEXT"), "hello\nworld");
|
---|
2495 | /* With an embedded CRCRLF */
|
---|
2496 | clipSetVBoxUtf16(pCtx, VINF_SUCCESS, "hello\r\r\nworld",
|
---|
2497 | sizeof("hello\r\r\nworld") * 2);
|
---|
2498 | testStringFromVBox(hTest, pCtx, "COMPOUND_TEXT",
|
---|
2499 | clipGetAtom(NULL, "COMPOUND_TEXT"), "hello\r\nworld");
|
---|
2500 | /* With an embedded CRLFCR */
|
---|
2501 | clipSetVBoxUtf16(pCtx, VINF_SUCCESS, "hello\r\n\rworld",
|
---|
2502 | sizeof("hello\r\n\rworld") * 2);
|
---|
2503 | testStringFromVBox(hTest, pCtx, "COMPOUND_TEXT",
|
---|
2504 | clipGetAtom(NULL, "COMPOUND_TEXT"), "hello\n\rworld");
|
---|
2505 | /* An empty string */
|
---|
2506 | clipSetVBoxUtf16(pCtx, VINF_SUCCESS, "", 2);
|
---|
2507 | testStringFromVBox(hTest, pCtx, "COMPOUND_TEXT",
|
---|
2508 | clipGetAtom(NULL, "COMPOUND_TEXT"), "");
|
---|
2509 | /* A non-zero-terminated string */
|
---|
2510 | clipSetVBoxUtf16(pCtx, VINF_SUCCESS, "hello world",
|
---|
2511 | sizeof("hello world") * 2 - 2);
|
---|
2512 | testStringFromVBox(hTest, pCtx, "COMPOUND_TEXT",
|
---|
2513 | clipGetAtom(NULL, "COMPOUND_TEXT"), "hello world");
|
---|
2514 |
|
---|
2515 | /*** Timeout from VBox ***/
|
---|
2516 | RTTestSub(hTest, "reading from VBox with timeout");
|
---|
2517 | clipEmptyVBox(pCtx, VERR_TIMEOUT);
|
---|
2518 | testStringFromVBoxFailed(hTest, pCtx, "UTF8_STRING");
|
---|
2519 |
|
---|
2520 | /*** No data in VBox clipboard ***/
|
---|
2521 | RTTestSub(hTest, "an empty VBox clipboard");
|
---|
2522 | clipSetSelectionValues("TEXT", XA_STRING, "", sizeof(""), 8);
|
---|
2523 | clipEmptyVBox(pCtx, VINF_SUCCESS);
|
---|
2524 | RTTEST_CHECK_MSG(hTest, g_ownsSel,
|
---|
2525 | (hTest, "VBox grabbed the clipboard with no data and we ignored it\n"));
|
---|
2526 | testStringFromVBoxFailed(hTest, pCtx, "UTF8_STRING");
|
---|
2527 |
|
---|
2528 | /*** An unknown VBox format ***/
|
---|
2529 | RTTestSub(hTest, "reading an unknown VBox format");
|
---|
2530 | clipSetSelectionValues("TEXT", XA_STRING, "", sizeof(""), 8);
|
---|
2531 | clipSetVBoxUtf16(pCtx, VINF_SUCCESS, "", 2);
|
---|
2532 | ClipAnnounceFormatToX11(pCtx, 0xa0000);
|
---|
2533 | RTTEST_CHECK_MSG(hTest, g_ownsSel,
|
---|
2534 | (hTest, "VBox grabbed the clipboard with unknown data and we ignored it\n"));
|
---|
2535 | testStringFromVBoxFailed(hTest, pCtx, "UTF8_STRING");
|
---|
2536 | rc = ClipStopX11(pCtx);
|
---|
2537 | AssertRCReturn(rc, 1);
|
---|
2538 | ClipDestructX11(pCtx);
|
---|
2539 |
|
---|
2540 | /*** Headless clipboard tests ***/
|
---|
2541 |
|
---|
2542 | pCtx = ClipConstructX11(NULL, true);
|
---|
2543 | rc = ClipStartX11(pCtx);
|
---|
2544 | AssertRCReturn(rc, 1);
|
---|
2545 |
|
---|
2546 | /*** Read from X11 ***/
|
---|
2547 | RTTestSub(hTest, "reading from X11, headless clipboard");
|
---|
2548 | /* Simple test */
|
---|
2549 | clipSetVBoxUtf16(pCtx, VINF_SUCCESS, "",
|
---|
2550 | sizeof("") * 2);
|
---|
2551 | clipSetSelectionValues("UTF8_STRING", XA_STRING, "hello world",
|
---|
2552 | sizeof("hello world"), 8);
|
---|
2553 | testNoX11(pCtx, "reading from X11, headless clipboard");
|
---|
2554 |
|
---|
2555 | /*** Read from VBox ***/
|
---|
2556 | RTTestSub(hTest, "reading from VBox, headless clipboard");
|
---|
2557 | /* Simple test */
|
---|
2558 | clipEmptyVBox(pCtx, VERR_WRONG_ORDER);
|
---|
2559 | clipSetSelectionValues("TEXT", XA_STRING, "", sizeof(""), 8);
|
---|
2560 | clipSetVBoxUtf16(pCtx, VINF_SUCCESS, "hello world",
|
---|
2561 | sizeof("hello world") * 2);
|
---|
2562 | testNoSelectionOwnership(pCtx, "reading from VBox, headless clipboard");
|
---|
2563 |
|
---|
2564 | rc = ClipStopX11(pCtx);
|
---|
2565 | AssertRCReturn(rc, 1);
|
---|
2566 | ClipDestructX11(pCtx);
|
---|
2567 |
|
---|
2568 | return RTTestSummaryAndDestroy(hTest);
|
---|
2569 | }
|
---|
2570 |
|
---|
2571 | #endif
|
---|
2572 |
|
---|
2573 | #ifdef SMOKETEST
|
---|
2574 |
|
---|
2575 | /* This is a simple test case that just starts a copy of the X11 clipboard
|
---|
2576 | * backend, checks the X11 clipboard and exits. If ever needed I will add an
|
---|
2577 | * interactive mode in which the user can read and copy to the clipboard from
|
---|
2578 | * the command line. */
|
---|
2579 |
|
---|
2580 | #include <iprt/env.h>
|
---|
2581 | #include <iprt/test.h>
|
---|
2582 |
|
---|
2583 | int ClipRequestDataForX11(VBOXCLIPBOARDCONTEXT *pCtx,
|
---|
2584 | uint32_t u32Format, void **ppv,
|
---|
2585 | uint32_t *pcb)
|
---|
2586 | {
|
---|
2587 | return VERR_NO_DATA;
|
---|
2588 | }
|
---|
2589 |
|
---|
2590 | void ClipReportX11Formats(VBOXCLIPBOARDCONTEXT *pCtx,
|
---|
2591 | uint32_t u32Formats)
|
---|
2592 | {}
|
---|
2593 |
|
---|
2594 | void ClipCompleteDataRequestFromX11(VBOXCLIPBOARDCONTEXT *pCtx, int rc,
|
---|
2595 | CLIPREADCBREQ *pReq, void *pv,
|
---|
2596 | uint32_t cb)
|
---|
2597 | {}
|
---|
2598 |
|
---|
2599 | int main()
|
---|
2600 | {
|
---|
2601 | /*
|
---|
2602 | * Init the runtime, test and say hello.
|
---|
2603 | */
|
---|
2604 | RTTEST hTest;
|
---|
2605 | int rc = RTTestInitAndCreate("tstClipboardX11Smoke", &hTest);
|
---|
2606 | if (rc)
|
---|
2607 | return rc;
|
---|
2608 | RTTestBanner(hTest);
|
---|
2609 |
|
---|
2610 | /*
|
---|
2611 | * Run the test.
|
---|
2612 | */
|
---|
2613 | rc = VINF_SUCCESS;
|
---|
2614 | /* We can't test anything without an X session, so just return success
|
---|
2615 | * in that case. */
|
---|
2616 | if (!RTEnvExist("DISPLAY"))
|
---|
2617 | {
|
---|
2618 | RTTestPrintf(hTest, RTTESTLVL_INFO,
|
---|
2619 | "X11 not available, not running test\n");
|
---|
2620 | return RTTestSummaryAndDestroy(hTest);
|
---|
2621 | }
|
---|
2622 | CLIPBACKEND *pCtx = ClipConstructX11(NULL, false);
|
---|
2623 | AssertReturn(pCtx, 1);
|
---|
2624 | rc = ClipStartX11(pCtx);
|
---|
2625 | AssertRCReturn(rc, 1);
|
---|
2626 | /* Give the clipboard time to synchronise. */
|
---|
2627 | RTThreadSleep(500);
|
---|
2628 | rc = ClipStopX11(pCtx);
|
---|
2629 | AssertRCReturn(rc, 1);
|
---|
2630 | ClipDestructX11(pCtx);
|
---|
2631 | return RTTestSummaryAndDestroy(hTest);
|
---|
2632 | }
|
---|
2633 |
|
---|
2634 | #endif /* SMOKETEST defined */
|
---|