VirtualBox

source: vbox/trunk/src/VBox/GuestHost/SharedClipboard/x11-clipboard.cpp@ 64387

Last change on this file since 64387 was 63590, checked in by vboxsync, 8 years ago

warning about explicit braces

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette