VirtualBox

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

Last change on this file since 78223 was 78161, checked in by vboxsync, 6 years ago

Shared Clipboard: Unified naming of shared guest/host files; too many similarly named files in different directories otherwise, can be confusing.

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