clipboard.cpp revision c97989161fbe75bc14cea477a5443bbf474dd3ad
/** X11 atom refering to the clipboard: CLIPBOARD */ /** X11 atom refering to the clipboard: TARGETS */ /** X11 atom refering to the clipboard: MULTIPLE */ /** X11 atom refering to the clipboard: TIMESTAMP */ /** 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? */ /** 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? */ /** 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. */ /** Since the clipboard data moves asynchronously, we use an event semaphore to wait for /** Are we running as a daemon? */ /** Format which we are reading from the guest clipboard (valid during a request for the /** The guest buffer to write guest clipboard data to (valid during a request for the host /** The size of the host buffer to write guest clipboard data to (valid during a request for /** 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. */ * 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 * Get clipboard data from the host * @returns VBox result code * @param u32Format The format of the data being requested * @retval ppv On success, this will point to a buffer to be freed with RTMemFree * containing the data read if pcb > 0. * @retval pcb On success, this contains the number of bytes of data returned /* Allocate a 1K buffer for receiving the clipboard data to start with. If this is too small, the host will tell us what size of buffer we need, and we will try again with a buffer of /* Set up the HGCM call structure and make the call to the host. */ /* Check whether the buffer we supplied was big enough. */ /* Our initial 1024 byte buffer was big enough for the clipboard data. */ /* Else if u32Size > cb, we try again with a buffer of size u32Size. */ /* Set up the HGCM call structure and make the call to the host. */ /* Check whether the buffer we supplied was big enough. */ /* The buffer was big enough. */ /* The buffer was to small again. Perhaps the clipboard contents changed half-way through the operation. Since I can't say whether or not this is actually an error, we will just * Convert a Utf16 text with Linux EOLs to 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 LogFlowFunc((
"*ppu16Dest=0, *pcwDest=0, rc=VINF_SUCCESS\n"));
/* Leave space for a trailing null in any case */ for (i = (
pu16Src[0] ==
0xfeff ?
1 : 0), j = 0; i <
cwSrc; ++i, ++j)
* 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 LogFlowFunc((
"converting Utf-16 to Utf-16LE. Original is %.*ls\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 Log2 ((
"vboxClipboardGetUtf8: converting Utf-8 to Utf-16LE. Original is %.*s\n",
cbSourceLen,
/* First convert the UTF8 to UTF16 */ * 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. */ /* Find the size of the destination text */ Log2 ((
"vboxClipboardGetLatin1: failed to allocate %d bytes!\n",
cwDestLen *
2));
/* Copy the original X clipboard string to the destination, replacing Linux EOLs with /** 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. */ /* The X Toolkit may have failed to get the clipboard selection for us. */ LogFlowFunc((
"*pcLen=%lu, *piFormat=%d, requested target format: %d, g_ctx.requestBufferSize=%d\n",
LogFlowFunc((
"Xt failed to convert the data. Sending empty data and returning\n"));
/* In which format did we request the clipboard data? */ /* If we are given broken Utf-8, we treat it as Latin1. Is this acceptable? */ Log ((
"vboxClipboardGetProc: bad target format\n"));
* Tell the host that new clipboard formats are available /** 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. */ /* 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 */ Log3 ((
"vboxClipboardTargetsProc called, cAtoms=%d\n",
cAtoms));
Log ((
"vboxClipboardTargetsProc: reading clipboard from guest, X toolkit failed to convert the selection\n"));
/* 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" for (
unsigned i = 0; i <
cAtoms; ++i)
Log3 ((
"vboxClipboardTargetsProc: the guest offers target %s\n",
szAtomName));
Log3 ((
"vboxClipboardTargetsProc: the guest returned a bad atom: %d\n",
/* 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. */ Log2 ((
"vboxClipboardTargetsProc: switching to guest text target %s\n",
szAtomName));
Log2((
"vboxClipboardTargetsProc: no supported host text target found.\n"));
* This callback is called every 200ms to check the contents of the guest clipboard. Log3 ((
"vboxClipboardTimerProc called\n"));
/* Get the current clipboard contents */ Log3 ((
"vboxClipboardTimerProc: requesting the targets that the guest clipboard offers\n"));
* 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 Log2((
"vboxClipboardConvertTargets: uListSize=%u\n",
uListSize));
for (
unsigned i = 0; i <
cTargets +
3; i++)
Log2 ((
"vboxClipboardConvertTargets: returning target %s\n",
szAtomName));
Log((
"vboxClipboardConvertTargets: invalid atom %d in the list!\n",
atomTargets[i]));
* Get the size of the buffer needed to hold a 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 */ /* Calculate the size of the destination text string. */ /* Is this Utf16 or Utf16-LE? */ /* The terminating zero is included in the size */ * 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 */ LogFlowFunc((
"set empty string. Returning VINF_SUCCESS\n"));
/* Prepend the Utf16 byte order marker if it is missing. */ * 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 Log ((
"vboxClipboardConvertUtf16: vboxClipboardReadHostData returned %Vrc, %d bytes of data\n",
rc,
cbHostText));
Log2((
"vboxClipboardConvertUtf16: vboxClipboardUtf16WinToLin returned %Vrc\n",
rc));
Log2 ((
"vboxClipboardConvertUtf16: returning Unicode, original text is %.*ls\n",
Log2 ((
"vboxClipboardConvertUtf16: converted text is %.*ls\n",
cwGuestText -
1,
* 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 /* Get the host Utf16 data and convert it to Linux Utf16. */ Log ((
"vboxClipboardConvertUtf16: vboxClipboardReadHostData returned %Vrc, %d bytes of data\n",
rc,
cbGuestText));
Log2((
"vboxClipboardConvertUtf16: vboxClipboardUtf16WinToLin returned %Vrc\n",
rc));
/* Now convert the Utf16 Linux text to Utf8 */ /* Our runtime can't cope with endian markers. */ Log2 ((
"vboxClipboardConvertUtf8: returning Utf-8, original text is %.*ls\n",
cwHostText,
* Satisfy a request from the guest to convert the clipboard text to Latin1. * @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 /* Get the host UTF16 data */ Log ((
"vboxClipboardConvertUtf16: vboxClipboardReadHostData returned %Vrc, %d bytes of data\n",
rc,
cbGuestText));
/* Any better ideas as to how to do this? */ Log ((
"vboxClipboardConvertLatin1: returning Latin-1, original text is %.*ls\n",
cwHostText,
Log ((
"vboxClipboardConvertLatin1: converted text is %.*s\n",
cbGuestPos,
* Callback to convert the hosts clipboard data for an application on the guest. Called by the * @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 Log2 ((
"vboxClipboardConvertProc: request for format %s\n",
szAtomName));
Log((
"vboxClipboardConvertProc: request for invalid target atom %d!\n", *
atomTarget));
Log((
"vboxClipboardConvertProc: bad format\n"));
LogFlowFunc((
"giving the guest clipboard ownership\n"));
* The host is taking possession of the shared clipboard. Called by the HGCM clipboard * @param u32Formats Clipboard formats the the guest is offering /* This is just an automatism, not a genuine anouncement */ Log2 ((
"vboxClipboardFormatAnnounce: giving the host clipboard ownership\n"));
/* We set this so that the host gets notified when we take the clipboard, even if no guest formats are found which we understand. */ Log2 ((
"vboxClipboardFormatAnnounce: returning clipboard ownership to the guest\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. LogFlowFunc((
"sent empty data, returned VINF_SUCCESS\n"));
/* 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. */ LogFlowFunc((
"sent empty data, returned VERR_NOT_IMPLEMENTED\n"));
/** Terminate the guest side of the shared clipboard. */ /* Set the termination flag. */ * The main loop of our clipboard reader. Log((
"Failed to call the driver for host message.\n"));
/* Wait a bit before retrying. */ /* The host has announced available clipboard formats. * Save the information so that it is available for * future requests from guest applications. /* The host needs data in the specified format. /* The host is terminating. Log((
"Unsupported message from host!!!\n"));
/* Wait for termination event. */ /* Wait a bit before retrying. */ Log((
"processed host event rc = %d\n",
rc));
* Disconnect the guest clipboard from the host. /* This can be triggered in debug builds if a guest application passes a bad atom in its list of supported clipboard formats. As such it is harmless. */ LogFlowFunc((
"ignoring BadAtom error and returning\n"));
Log((
"%s: an X Window protocol error occurred: %s. Request code: %d, minor code: %d, serial number: %d\n",
* Connect the guest clipboard to the host. * @returns RT status code /* Only one client is supported for now */ cout <<
"We got an invalid client ID of 0!" <<
endl;
cout <<
"Unable to connect to the host system." <<
endl;
Log((
"Error performing VMM request ioctl. errno = %d\n",
errno));
cout <<
"The VirtualBox device in the guest is not responding correctly." <<
endl;
/* If the programme exits without disconnecting from the shared clipboard service, we will need to reboot the guest in order to use it again. So we set handlers for as many fatal conditions as possible which disconnect and exit the programme. */ /** We store information about the target formats we can handle in a global vector for internal /* Get an atom from the X server for that target format */ /** Create the X11 window which we use to interact with the guest clipboard */ /* Create a window and make it a clipboard viewer. */ /* Set up the Clipbard application context and main window. */ /* Get hold of the atoms which we need */ /* And build up the vector of supported formats */ /** Initialise the guest side of the shared clipboard. */ /* Set up a timer to poll the host clipboard */ /* Set the termination signal. */ * Become a daemon process /* First fork and exit the parent process, so that we are sure we are not session leader. */ /* Detach from the controlling terminal by creating our own session. */ /* And change to the root directory to avoid holding the one we were started in open. */ /* Close the standard files. */ /* Parse our option(s) */ cout <<
"Unrecognized command line option: " <<
static_cast<
char>(
cOpt) <<
endl;
/* Initialise our runtime before all else. */ /* Initialise threading in Xt before we start any new threads. */ /* Initialise the termination semaphore. */ /* Open a connection to the driver for sending requests. */ Log((
"Error opening kernel module! errno = %d\n",
errno));
cout <<
"Failed to open the VirtualBox device in the guest." <<
endl;
/* Open a connection to the driver for polling for host requests. */ Log((
"Error opening kernel module! rc = %d\n",
errno));
cout <<
"Failed to open the VirtualBox device in the guest" <<
endl;
/* Connect to the host clipboard. */ Log((
"vboxClipboardConnect failed with rc = %d\n",
rc));
cout <<
"Failed to connect to the host clipboard." <<
endl;