clipboard.cpp revision 6e02683e95b183b690de90f27b412d089cf1058a
/** $Id$ */
/** @file
* Guest Additions - X11 Shared Clipboard.
*/
/*
* Copyright (C) 2007 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/****************************************************************************
* Header Files *
****************************************************************************/
#include <iprt/initterm.h>
#include <iprt/semaphore.h>
#include <X11/Intrinsic.h>
#include <vector>
#include "VBoxClient.h"
#include "clipboard.h"
/** The formats which we support in the guest. These can be deactivated in order to test specific code paths. */
#define USE_UTF16
#define USE_UTF8
#define USE_CTEXT
/****************************************************************************
* Global Variables *
****************************************************************************/
/** The different clipboard formats which we support. */
enum g_eClipboardFormat
{
INVALID = 0,
UTF8,
};
/** The X11 clipboard uses several names for the same format. This structure maps an X11 name to a format. */
typedef struct
{
unsigned hostFormat;
/** Does the host or the guest currently own the clipboard? */
enum g_eClipboardOwner
{
NONE = 0,
HOST,
};
/**
* Global clipboard context information.
*/
typedef struct
{
/** The Xt application context structure */
/** We have a separate thread to wait for Window and Clipboard events */
/** The Xt widget which we use as our clipboard client. It is never made visible. */
/** X11 atom refering to the clipboard: CLIPBOARD */
/** X11 atom refering to the selection: PRIMARY */
/** X11 atom refering to the clipboard: TARGETS */
/** X11 atom refering to the clipboard: MULTIPLE */
/** X11 atom refering to the clipboard: TIMESTAMP */
/** X11 atom refering to the clipboard utf8 text format: UTF8_STRING */
/** X11 atom refering to the native X11 clipboard text format: COMPOUND_TEXT */
/** A list of the X11 formats which we support, mapped to our identifier for them, in the order
we prefer to have them in. */
/** Does the host or the guest currently own the clipboard? */
volatile enum g_eClipboardOwner eOwner;
/** What is the best text format the guest has to offer? INVALID for none. */
/** Atom corresponding to the guest text format */
/** What is the best bitmap format the guest has to offer? INVALID for none. */
/** Atom corresponding to the guest Bitmap format */
/** What formats does the host have on offer? */
int hostFormats;
/** Windows caches the clipboard data it receives. Since we have no way of knowing whether
that data is still valid, we always send a "data changed" message after a successful
transfer to invalidate the cache. */
bool notifyHost;
/** Since the clipboard data moves asynchronously, we use an event semaphore to wait for it. */
/** Format which we are reading from the guest clipboard (valid during a request for the
guest clipboard) */
/** The guest buffer to write guest clipboard data to (valid during a request for the host
clipboard) */
void *requestBuffer;
/** The size of the host buffer to write guest clipboard data to (valid during a request for
the guest clipboard) */
unsigned requestBufferSize;
/** The size of the guest clipboard data written to the host buffer (valid during a request
for the guest clipboard) */
/** Client ID for the clipboard subsystem */
/** Only one client is supported. There seems to be no need for more clients. */
static VBOXCLIPBOARDCONTEXT g_ctx;
/**
* Transfer clipboard data from the guest to the host.
*
* @returns VBox result code
* @param u32Format The format of the data being sent
* @param pv Pointer to the data being sent
* @param cb Size of the data being sent in bytes
*/
{
int rc;
return rc;
}
/**
* Get clipboard data from the host.
*
* @returns VBox result code
* @param u32Format The format of the data being requested
* @retval ppv On success and if pcb > 0, this will point to a buffer
* to be freed with RTMemFree containing the data read.
* @retval pcb On success, this contains the number of bytes of data
* returned
*/
{
int rc = VINF_SUCCESS;
*ppv = 0;
if (RT_UNLIKELY(!pv))
rc = VERR_NO_MEMORY;
if (RT_SUCCESS(rc))
/* A return value of VINF_BUFFER_OVERFLOW tells us to try again with a
* larger buffer. The size of the buffer needed is placed in *pcb.
* So we start all over again. */
if (rc == VINF_BUFFER_OVERFLOW)
{
if (RT_UNLIKELY(!pv))
rc = VERR_NO_MEMORY;
if (RT_SUCCESS(rc))
}
/* Catch other errors. This also catches the case in which the buffer was
* too small a second time, possibly because the clipboard contents
* changed half-way through the operation. Since we can't say whether or
* not this is actually an error, we just return size 0.
*/
{
*pcb = 0;
}
if (RT_SUCCESS(rc))
return rc;
}
/**
* Convert a Utf16 text with Linux EOLs to zero-terminated Utf16-LE with Windows EOLs, allocating
* memory for the converted text. Does no checking for validity.
*
* @returns VBox status code
*
* @param pu16Src Source Utf16 text to convert
* @param cwSrc Size of the source text in 16 bit words
* @retval ppu16Dest Where to store the pointer to the converted text. Only valid on success
* and if pcwDest is greater than 0.
* @retval pcwDest Size of the converted text in 16 bit words, including the trailing null
* if present
*/
{
if (cwSrc == 0)
{
*ppu16Dest = 0;
*pcwDest = 0;
LogFlowFunc(("*ppu16Dest=0, *pcwDest=0, rc=VINF_SUCCESS\n"));
return VINF_SUCCESS;
}
cwDest = 0;
{
++cwDest;
if (pu16Src[i] == 0)
break;
}
/* Leave space for a trailing null */
++cwDest;
if (pu16Dest == 0)
{
LogFlowFunc(("rc=VERR_NO_MEMORY\n"));
return VERR_NO_MEMORY;
}
{
{
pu16Dest[j] = CARRIAGERETURN;
++j;
}
if (pu16Src[i] == 0)
break;
}
/* The trailing null */
pu16Dest[j] = 0;
return VINF_SUCCESS;
}
/**
* Convert the UTF-16 text returned from the guest X11 clipboard to UTF-16LE with Windows EOLs
* and send it to the host.
*
* @param pValue Source UTF-16 text
* @param cwSourceLen Length in 16-bit words of the source text
*/
{
int rc;
LogFlowFunc(("converting Utf-16 to Utf-16LE. Original is %.*ls\n",
if (rc != VINF_SUCCESS)
{
vboxClipboardSendData(0, NULL, 0);
LogFlowFunc(("sending empty data and returning\n"));
return;
}
RTMemFree(reinterpret_cast<void *>(pu16DestText));
LogFlowFunc(("returning\n"));
}
/**
* Convert the UTF-8 text returned from the guest X11 clipboard to UTF-16LE with Windows EOLs
* and send it to the host.
*
* @param pValue Source UTF-8 text
* @param cbSourceLen Length in 8-bit bytes of the source text
*/
{
char *pu8SourceText = reinterpret_cast<char *>(pValue);
int rc;
LogFlowFunc(("\n"));
/* First convert the UTF8 to UTF16 */
if (rc != VINF_SUCCESS)
{
vboxClipboardSendData(0, NULL, 0);
LogFlowFunc(("sending empty data and returning\n"));
return;
}
if (rc != VINF_SUCCESS)
{
RTMemFree(reinterpret_cast<void *>(pu16SourceText));
vboxClipboardSendData(0, NULL, 0);
LogFlowFunc(("sending empty data and returning\n"));
return;
}
RTMemFree(reinterpret_cast<void *>(pu16SourceText));
RTMemFree(reinterpret_cast<void *>(pu16DestText));
LogFlowFunc(("returning\n"));
}
/**
* Convert the compound text returned from the guest X11 clipboard to UTF-16LE with Windows EOLs
* and send it to the host.
*
* @param pValue Source compound text
* @param cbSourceLen Length in 8-bit bytes of the source text
*/
{
char **ppu8SourceText = 0;
LogFlowFunc(("\n"));
LogFlow(("vboxClipboardGetCText: converting compound text to Utf-16LE. Original is %.*s\n",
cbSourceLen, pValue));
/* Quick fix for 2.2. */
if (cbSourceLen == 0)
{
NULL, 0);
return;
}
/* First convert the compound text to Utf8 */
#ifdef RT_OS_SOLARIS
#else
#endif
if (rc < 0)
{
const char *pcReason;
switch(rc)
{
case XNoMemory:
pcReason = "out of memory";
break;
case XLocaleNotSupported:
pcReason = "locale (Utf8) not supported";
break;
case XConverterNotFound:
pcReason = "converter not found";
break;
default:
pcReason = "unknown error";
}
vboxClipboardSendData(0, NULL, 0);
LogFlowFunc(("sending empty data and returning\n"));
return;
}
/* Next convert the UTF8 to UTF16 */
if (rc != VINF_SUCCESS)
{
vboxClipboardSendData(0, NULL, 0);
LogFlowFunc(("sending empty data and returning\n"));
return;
}
RTMemFree(reinterpret_cast<void *>(pu16SourceText));
if (rc != VINF_SUCCESS)
{
vboxClipboardSendData(0, NULL, 0);
LogFlowFunc(("sending empty data and returning\n"));
return;
}
RTMemFree(reinterpret_cast<void *>(pu16DestText));
LogFlowFunc(("returning\n"));
}
/**
* Convert the Latin1 text returned from the guest X11 clipboard to UTF-16LE with Windows EOLs
* and send it to the host.
*
* @param pValue Source Latin1 text
* @param cbSourceLen Length in 8-bit bytes of the source text
*/
{
/* Leave space for an additional null character at the end of the destination text. */
char *pu8SourceText = reinterpret_cast<char *>(pValue);
/* Find the size of the destination text */
for (size_t i = 0; i < cbSourceLen; i++)
{
if (pu8SourceText[i] == LINEFEED)
++cwDestLen;
}
if (pu16DestText == 0)
{
vboxClipboardSendData(0, NULL, 0);
LogFlowFunc(("sending empty data and returning\n"));
return;
}
/* Copy the original X clipboard string to the destination, replacing Linux EOLs with
Windows ones */
cwDestPos = 0;
{
if (pu8SourceText[i] == LINEFEED)
{
++cwDestPos;
}
/* latin1 < utf-16LE */
if (pu8SourceText[i] == 0)
break;
}
pu16DestText[cwDestPos] = 0;
RTMemFree(reinterpret_cast<void *>(pu16DestText));
LogFlowFunc(("returning\n"));
}
/** Convert the clipboard text from the current format to Utf-16 with Windows line breaks.
We are reading the guest clipboard to make it available to the host. */
int *piFormat)
{
LogFlowFunc(("*pcLen=%lu, *piFormat=%d, requested target format: %d, g_ctx.requestBufferSize=%d\n",
/* The X Toolkit may have failed to get the clipboard selection for us. */
if (*atomType == XT_CONVERT_FAIL)
{
vboxClipboardSendData(0, NULL, 0);
LogFlowFunc(("Xt failed to convert the data. Sending empty data and returning\n"));
return;
}
{
vboxClipboardSendData(0, NULL, 0);
LogFlowFunc(("The clipboard contents changed while we were requesting them. Sending empty data and returning\n"));
return;
}
/* In which format did we request the clipboard data? */
switch (g_ctx.requestGuestFormat)
{
case UTF16:
break;
case CTEXT:
break;
case UTF8:
{
/* If we are given broken Utf-8, we treat it as Latin1. Is this acceptable? */
char *pu8SourceText = reinterpret_cast<char *>(pValue);
else
break;
}
default:
{
Log(("vboxClipboardGetProc: bad target format\n"));
vboxClipboardSendData(0, NULL, 0);
LogFlowFunc(("sending empty data and returning\n"));
return;
}
}
g_ctx.notifyHost = true;
LogFlowFunc(("returning\n"));
}
/**
* Tell the host that new clipboard formats are available.
*
* @returns VBox status code.
* @param u32Formats The formats to advertise
*/
{
int rc;
return rc;
}
/** Callback to handle a reply to a request for the targets the current clipboard holder can
handle. We are reading the guest clipboard to make it available to the host. */
{
static int cCalls = 0;
/* The number of format atoms the clipboard holder is offering us */
/* The best clipboard format we have found so far */
/* The atom corresponding to our best clipboard format found */
if ((cCalls % 10) == 0)
if (*atomType == XT_CONVERT_FAIL)
{
Log(("vboxClipboardTargetsProc: reading clipboard from guest, X toolkit failed to convert the selection\n"));
LogFlowFunc(("returning\n"));
return;
}
/* Run through the atoms offered to us to see if we recognise them. If we find the atom for
a "better" format than the best we have found so far, we remember it as our new "best"
format. */
for (unsigned i = 0; i < cAtoms; ++i)
{
{
{
}
break;
}
#ifdef DEBUG
if (szAtomName != 0)
{
if ((cCalls % 10) == 0)
}
else
{
if ((cCalls % 10) == 0)
Log3(("vboxClipboardTargetsProc: the guest returned a bad atom: %d\n",
atomTargets[i]));
}
#endif
}
/* If the available formats as seen by the host have changed, or if we suspect that
the host has cached the clipboard data (which can change without our noticing it),
then tell the host that new clipboard contents are available. */
{
uint32_t u32Formats = 0;
#ifdef DEBUG
if (atomBestTarget != None)
{
LogFlow(("vboxClipboardTargetsProc: switching to guest text target %s. Available targets are:\n", szAtomName));
}
else
LogFlow(("vboxClipboardTargetsProc: no supported host text target found. Available targets are:\n"));
for (unsigned i = 0; i < cAtoms; ++i)
{
if (szAtomName != 0)
{
}
}
#endif
if (eBestTarget != INVALID)
g_ctx.notifyHost = false;
}
++cCalls;
}
/**
* This callback is called every 200ms to check the contents of the guest clipboard.
*/
{
static int cCalls = 0;
/* Get the current clipboard contents */
{
if ((cCalls % 10) == 0)
Log3(("vboxClipboardTimerProc: requesting the targets that the guest clipboard offers\n"));
}
/* Re-arm our timer */
++cCalls;
}
/**
* Satisfy a request from the guest for available clipboard targets.
*
* @returns true if we successfully convert the data to the format requested, false otherwise.
*
* @param atomTypeReturn The type of the data we are returning
* @param pValReturn A pointer to the data we are returning. This should be to memory
* allocated by XtMalloc, which will be freed by the toolkit later
* @param pcLenReturn The length of the data we are returning
* @param piFormatReturn The format (8bit, 16bit, 32bit) of the data we are returning
*/
unsigned long *pcLenReturn, int *piFormatReturn)
{
unsigned cTargets = 0;
LogFlowFunc(("\n"));
for (unsigned i = 0; i < uListSize; ++i)
{
{
++cTargets;
}
}
#ifdef DEBUG
for (unsigned i = 0; i < cTargets + 3; i++)
{
if (szAtomName != 0)
{
}
else
}
#endif
*piFormatReturn = 32;
LogFlowFunc(("returning true\n"));
return true;
}
/**
* Get the size of the buffer needed to hold a zero-terminated Utf16 string with Linux EOLs
* converted from a Utf16 string with Windows EOLs.
*
* @returns The size of the buffer needed in bytes
*
* @param pu16Src The source Utf16 string
* @param cwSrc The length in 16 bit words of the source string
*/
{
/* We only take little endian Utf16 */
if (cwSrc == 0)
{
LogFlowFunc(("returning 0\n"));
return 0;
}
/* Calculate the size of the destination text string. */
/* Is this Utf16 or Utf16-LE? */
if (pu16Src[0] == 0xfeff)
cwDest = 0;
else
cwDest = 1;
{
if ( (i + 1 < cwSrc)
&& (pu16Src[i] == CARRIAGERETURN)
++i;
if (pu16Src[i] == 0)
break;
}
/* The terminating null */
++cwDest;
return cwDest * 2;
}
/**
* Convert Utf16-LE text with Windows EOLs to Utf16 with Linux EOLs. This function does not
* verify that the Utf16 is valid.
*
* @returns VBox status code
*
* @param pu16Src Text to convert
* @param cwSrc Size of the source text in 16 bit words
* @param pu16Dest The buffer to store the converted text to
* @param cwDest The size of the buffer for the destination text
*/
{
LogFlowFunc(("pu16Src=%.*ls, cwSrc=%u, pu16Dest=%p, cwDest=%u\n",
/* A buffer of size 0 may not be an error, but it is not a good idea either. */
/* We only take little endian Utf16 */
if (cwSrc == 0)
{
if (cwDest != 0)
pu16Dest[0] = 0;
LogFlowFunc(("set empty string. Returning VINF_SUCCESS\n"));
return VINF_SUCCESS;
}
/* Prepend the Utf16 byte order marker if it is missing. */
if (pu16Src[0] == 0xfeff)
cwDestPos = 0;
else
{
if (cwDest == 0)
{
LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
return VERR_BUFFER_OVERFLOW;
}
pu16Dest[0] = 0xfeff;
cwDestPos = 1;
}
{
{
LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
return VERR_BUFFER_OVERFLOW;
}
if ( (i + 1 < cwSrc)
&& (pu16Src[i] == CARRIAGERETURN)
++i;
if (pu16Src[i] == 0)
break;
}
{
LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
return VERR_BUFFER_OVERFLOW;
}
return VINF_SUCCESS;
}
/**
* Satisfy a request from the guest to convert the clipboard text to Utf16.
*
* @returns true if we successfully convert the data to the format requested, false otherwise.
*
* @param atomTypeReturn The type of the data we are returning
* @param pValReturn A pointer to the data we are returning. This should be to memory
* allocated by XtMalloc, which will be freed by the toolkit later
* @param pcLenReturn The length of the data we are returning
* @param piFormatReturn The format (8bit, 16bit, 32bit) of the data we are returning
*/
unsigned long *pcLenReturn, int *piFormatReturn)
{
int rc;
LogFlowFunc(("\n"));
reinterpret_cast<void **>(&pu16HostText), &cbHostText);
{
LogFlow(("vboxClipboardConvertUtf16: vboxClipboardReadHostData returned %Rrc, %d bytes of data\n", rc, cbHostText));
g_ctx.hostFormats = 0;
LogFlowFunc(("rc = false\n"));
return false;
}
if (pu16GuestText == 0)
{
RTMemFree(reinterpret_cast<void *>(pu16HostText));
LogFlowFunc(("rc = false\n"));
return false;
}
if (rc != VINF_SUCCESS)
{
RTMemFree(reinterpret_cast<void *>(pu16HostText));
XtFree(reinterpret_cast<char *>(pu16GuestText));
LogFlowFunc(("rc = false\n"));
return false;
}
LogFlow(("vboxClipboardConvertUtf16: returning Unicode, original text is %.*ls\n", cwHostText, pu16HostText));
LogFlow(("vboxClipboardConvertUtf16: converted text is %.*ls\n", cwGuestText - 1, pu16GuestText + 1));
RTMemFree(reinterpret_cast<void *>(pu16HostText));
*piFormatReturn = 8;
LogFlowFunc(("rc = true\n"));
return true;
}
/**
* Satisfy a request from the guest to convert the clipboard text to Utf8.
*
* @returns true if we successfully convert the data to the format requested, false otherwise.
*
* @param atomTypeReturn The type of the data we are returning
* @param pValReturn A pointer to the data we are returning. This should be to memory
* allocated by XtMalloc, which will be freed by the toolkit later
* @param pcLenReturn The length of the data we are returning
* @param piFormatReturn The format (8bit, 16bit, 32bit) of the data we are returning
*/
unsigned long *pcLenReturn, int *piFormatReturn)
{
char *pcGuestText;
int rc;
LogFlowFunc(("\n"));
/* Get the host Utf16 data and convert it to Linux Utf16. */
reinterpret_cast<void **>(&pu16HostText), &cbHostText);
{
LogFlow(("vboxClipboardConvertUtf16: vboxClipboardReadHostData returned %Rrc, %d bytes of data\n", rc, cbHostText));
g_ctx.hostFormats = 0;
LogFlowFunc(("rc = false\n"));
return false;
}
if (pu16GuestText == 0)
{
RTMemFree(reinterpret_cast<char *>(pu16HostText));
LogFlowFunc(("rc = false\n"));
return false;
}
RTMemFree(reinterpret_cast<char *>(pu16HostText));
if (rc != VINF_SUCCESS)
{
RTMemFree(reinterpret_cast<char *>(pu16GuestText));
LogFlowFunc(("rc = false\n"));
return false;
}
/* Now convert the Utf16 Linux text to Utf8 */
if (pcGuestText == 0)
{
RTMemFree(reinterpret_cast<char *>(pu16GuestText));
LogFlowFunc(("rc = false\n"));
return false;
}
/* Our runtime can't cope with endian markers. */
cbGuestTextActual = 0;
rc = RTUtf16ToUtf8Ex(pu16GuestText + 1, cwGuestText - 1, &pcGuestText, cbGuestTextBuffer, &cbGuestTextActual);
RTMemFree(reinterpret_cast<char *>(pu16GuestText));
if (rc != VINF_SUCCESS)
{
LogFlowFunc(("rc = false\n"));
return false;
}
*pcLenReturn = (unsigned long)cbGuestTextActual;
*piFormatReturn = 8;
LogFlowFunc(("rc = true\n"));
return true;
}
/**
* Satisfy a request from the guest to convert the clipboard text to compound text.
*
* @returns true if we successfully convert the data to the format requested, false otherwise.
*
* @param atomTypeReturn The type of the data we are returning
* @param pValReturn A pointer to the data we are returning. This should be to memory
* allocated by XtMalloc, which will be freed by the toolkit later
* @param pcLenReturn The length of the data we are returning
* @param piFormatReturn The format (8bit, 16bit, 32bit) of the data we are returning
*/
unsigned long *pcLenReturn, int *piFormatReturn)
{
char *pcUtf8Text;
int rc;
LogFlowFunc(("\n"));
/* Get the host Utf16 data and convert it to Linux Utf16. */
reinterpret_cast<void **>(&pu16HostText), &cbHostText);
{
LogFlow(("vboxClipboardConvertCText: vboxClipboardReadHostData returned %Rrc, %d bytes of data\n", rc, cbHostText));
g_ctx.hostFormats = 0;
LogFlowFunc(("rc = false\n"));
return false;
}
if (pu16GuestText == 0)
{
Log(("vboxClipboardConvertCText: out of memory\n"));
RTMemFree(reinterpret_cast<char *>(pu16HostText));
LogFlowFunc(("rc = false\n"));
return false;
}
RTMemFree(reinterpret_cast<char *>(pu16HostText));
if (rc != VINF_SUCCESS)
{
RTMemFree(reinterpret_cast<char *>(pu16GuestText));
LogFlowFunc(("rc = false\n"));
return false;
}
/* Now convert the Utf16 Linux text to Utf8 */
if (pcUtf8Text == 0)
{
Log(("vboxClipboardConvertCText: out of memory\n"));
RTMemFree(reinterpret_cast<char *>(pu16GuestText));
LogFlowFunc(("rc = false\n"));
return false;
}
/* Our runtime can't cope with endian markers. */
RTMemFree(reinterpret_cast<char *>(pu16GuestText));
if (rc != VINF_SUCCESS)
{
LogFlowFunc(("rc = false\n"));
return false;
}
/* And finally (!) convert the Utf8 text to compound text. */
#ifdef RT_OS_SOLARIS
#else
#endif
if (rc < 0)
{
const char *pcReason;
switch(rc)
{
case XNoMemory:
pcReason = "out of memory";
break;
case XLocaleNotSupported:
pcReason = "locale (Utf8) not supported";
break;
case XConverterNotFound:
pcReason = "converter not found";
break;
default:
pcReason = "unknown error";
}
LogRel(("vboxClipboardConvertCText: Xutf8TextListToTextProperty failed. Reason: %s\n",
pcReason));
LogFlowFunc(("rc = false\n"));
return false;
}
LogFlowFunc(("rc = true\n"));
return true;
}
/**
* Callback to convert the hosts clipboard data for an application on the guest. Called by the
* X Toolkit.
* @returns true if we successfully convert the data to the format requested, false otherwise.
*
* @param atomSelection The selection which is being requested. We only handle the clipboard.
* @param atomTarget The format we should convert the data to
* @param atomTypeReturn The type of the data we are returning
* @param pValReturn A pointer to the data we are returning. This should be to memory
* allocated by XtMalloc, which will be freed by the toolkit later
* @param pcLenReturn The length of the data we are returning
* @param piFormatReturn The format (8bit, 16bit, 32bit) of the data we are returning
*/
unsigned long *pcLenReturn, int *piFormatReturn)
{
int rc;
LogFlowFunc(("\n"));
)
{
LogFlowFunc(("rc = false\n"));
return false;
}
#ifdef DEBUG
if (szAtomName != 0)
{
}
else
#endif
else
{
{
{
break;
}
}
}
switch (eFormat)
{
case TARGETS:
return rc;
case UTF16:
return rc;
case UTF8:
return rc;
case CTEXT:
return rc;
default:
Log(("vboxClipboardConvertProc: bad format\n"));
LogFlowFunc(("rc = false\n"));
return false;
}
}
{
LogFlowFunc(("giving the guest clipboard ownership\n"));
g_ctx.notifyHost = true;
LogFlowFunc(("returning\n"));
}
/**
* The host is taking possession of the shared clipboard. Called by the HGCM clipboard
* subsystem.
*
* @param u32Formats Clipboard formats the guest is offering
*/
{
if (u32Formats == 0)
{
/* This is just an automatism, not a genuine announcement */
LogFlowFunc(("returning\n"));
return;
}
LogFlow(("vboxClipboardFormatAnnounce: giving the host clipboard ownership\n"));
== True)
vboxClipboardConvertProc, NULL, 0);
else
{
LogFlow(("vboxClipboardFormatAnnounce: returning clipboard ownership to the guest\n"));
g_ctx.notifyHost = true;
}
LogFlowFunc(("returning\n"));
}
/**
* Called when the host wants to read the guest clipboard.
*
* @param u32Format The format that the host would like to receive the data in
*/
{
/*
* The guest wants to read data in the given format.
*/
{
{
/* No data available. */
vboxClipboardSendData(0, NULL, 0);
LogFlowFunc(("sent empty data, returned VINF_SUCCESS\n"));
return VINF_SUCCESS;
}
/* Send out a request for the data to the current clipboard owner */
/* When the data arrives, the vboxClipboardGetProc callback will be called. The
callback will signal the event semaphore when it has processed the data for us. */
}
else
{
vboxClipboardSendData(0, NULL, 0);
LogFlowFunc(("sent empty data, returned VERR_NOT_IMPLEMENTED\n"));
return VERR_NOT_IMPLEMENTED;
}
LogFlowFunc(("returned VINF_SUCCESS\n"));
return VINF_SUCCESS;
}
/** Terminate the guest side of the shared clipboard. */
void vboxClipboardDestroy (void)
{
LogFlowFunc(("\n"));
/* Set the termination flag. */
LogFlowFunc(("returning\n"));
}
/**
* The main loop of our clipboard reader.
*/
{
int rc;
LogFlowFunc(("Starting clipboard thread\n"));
for (;;)
{
if (RT_SUCCESS(rc))
{
switch (Msg)
{
{
/* The host has announced available clipboard formats.
* Save the information so that it is available for
* future requests from guest applications.
*/
break;
}
{
/* The host needs data in the specified format. */
break;
}
{
/* The host is terminating. */
LogFlowFunc(("VBOX_SHARED_CLIPBOARD_HOST_MSG_QUIT\n"));
break;
}
default:
Log(("Unsupported message from host!!!\n"));
}
}
if (rc == VERR_INTERRUPTED)
{
/* Wait for termination event. */
break;
}
if (RT_FAILURE(rc))
{
/* Wait a bit before retrying. */
break;
continue;
}
}
return rc;
}
/**
* Disconnect the guest clipboard from the host.
*/
void vboxClipboardDisconnect(void)
{
#if 0
#endif
LogFlowFunc(("\n"));
LogFlowFunc(("returning\n"));
}
/**
* Connect the guest clipboard to the host.
*
* @returns VBox status code
*/
int vboxClipboardConnect(void)
{
int rc;
LogFlowFunc(("\n"));
/* Only one client is supported for now */
/* Initialise the termination semaphore. */
/* Initialise threading in X11 and in Xt. */
if (!XInitThreads() || !XtToolkitThreadInitialize())
{
LogRel(("VBoxClient: error initialising threads in X11, exiting.\n"));
return VERR_NOT_SUPPORTED;
}
if (RT_FAILURE(rc))
{
return rc;
}
{
LogRel(("Invalid client ID of 0\n"));
return VERR_NOT_SUPPORTED;
}
/* Assume that if the guest clipboard already contains data then the
* user put it there for a reason. */
return rc;
}
/** We store information about the target formats we can handle in a global vector for internal
use. */
static void vboxClipboardAddFormat(const char *pszName, g_eClipboardFormat eFormat, unsigned hostFormat)
{
/* Get an atom from the X server for that target format */
if (atomFormat == 0)
{
LogFlowFunc(("atomFormat=0! returning...\n"));
return;
}
LogFlowFunc(("returning\n"));
}
/** Create the X11 window which we use to interact with the guest clipboard */
static int vboxClipboardCreateWindow(void)
{
/* Create a window and make it a clipboard viewer. */
int cArgc = 0;
char *pcArgv = 0;
/* Set up the Clipbard application context and main window. */
/* Get hold of the atoms which we need */
g_ctx.atomClipboard = XInternAtom(XtDisplay(g_ctx.widget), "CLIPBOARD", false /* only_if_exists */);
/* And build up the vector of supported formats */
#ifdef USE_UTF16
vboxClipboardAddFormat("text/plain;charset=ISO-10646-UCS-2", UTF16, VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT);
#endif
#ifdef USE_UTF8
#endif
#ifdef USE_CTEXT
#endif
return VINF_SUCCESS;
}
/** Initialise the guest side of the shared clipboard. */
int vboxClipboardMain(void)
{
int rc;
LogFlowFunc(("\n"));
if (RT_FAILURE(rc))
return rc;
RTTHREADFLAGS_WAITABLE, "SHCLIP");
/* Set up a timer to poll the host clipboard */
/* Set the termination signal. */
return rc;
}
{
public:
virtual const char *getPidFilePath()
{
return ".vboxclient-clipboard.pid";
}
virtual int run()
{
int rc = vboxClipboardConnect();
if (RT_SUCCESS(rc))
rc = vboxClipboardMain();
return rc;
}
virtual void cleanup()
{
/* Nothing to do. */
}
};
{
return new ClipboardService;
}