x11-clipboard.cpp revision 2feb1c8e55e737875aba0cfd07e4bd5544621c7b
/** @file
*
* Shared Clipboard:
* Linux host.
*/
/*
* Copyright (C) 2006-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.
*/
#define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
#include <string.h>
#include <iprt/critsect.h>
#include <iprt/semaphore.h>
#include "VBoxClipboard.h"
/** A request for clipboard data from VBox */
typedef struct _VBOXCLIPBOARDREQFROMVBOX
{
/** Data received */
void *pv;
/** The size of the data */
/** Format of the data */
/** A semaphore for waiting for the data */
/** Global context information used by the host glue for the X11 clipboard
* backend */
struct _VBOXCLIPBOARDCONTEXT
{
/** This mutex is grabbed during any critical operations on the clipboard
* which might clash with others. */
/** The currently pending request for data from VBox. NULL if there is
* no request pending. The protocol for completing a request is to grab
* the critical section, check that @a pReq is not NULL, fill in the data
* fields and set @a pReq to NULL. The protocol for cancelling a pending
* request is to grab the critical section and set pReq to NULL.
* It is an error if a request arrives while another one is pending, and
* the backend is responsible for ensuring that this does not happen. */
/** Pointer to the opaque X11 backend structure */
/** Pointer to the VBox host client data structure. */
/** We set this when we start shutting down as a hint not to post any new
* requests. */
bool fShuttingDown;
};
{
int rc = VINF_SUCCESS;
/* Request data from VBox */
/* Which will signal us when it is ready. We use a timeout here
* because we can't be sure that the guest will behave correctly.
*/
/* If the request hasn't yet completed then we cancel it. We use
* the critical section to prevent these operations colliding. */
/* The data may have arrived between the semaphore timing out and
* our grabbing the mutex. */
rc = VINF_SUCCESS;
return rc;
}
* completed. */
{
int rc = VINF_SUCCESS;
u32Format));
/* Start by "posting" the request for the next invocation of
* vboxClipboardWriteData. */
{
/* This would be a violation of the protocol, see the comments in the
* context structure definition. */
Assert(false);
}
else
if (RT_SUCCESS(rc))
return rc;
}
/**
* Send a request to VBox to transfer the contents of its clipboard to X11.
*
* @param pCtx Pointer to the host clipboard structure
* @param u32Format The format in which the data should be transfered
* @param ppv On success and if pcb > 0, this will point to a buffer
* to be freed with RTMemFree containing the data read.
* @param pcb On success, this contains the number of bytes of data
* returned
* @note Host glue code.
*/
{
if (pCtx->fShuttingDown)
{
/* The shared clipboard is disconnecting. */
LogFunc(("host requested guest clipboard data after guest had disconnected.\n"));
return VERR_WRONG_ORDER;
}
if (RT_SUCCESS(rc))
{
}
if (RT_SUCCESS(rc))
{
}
if (RT_SUCCESS(rc))
return rc;
}
/**
* Report formats available in the X11 clipboard to VBox.
* @param pCtx Opaque context pointer for the glue code
* @param u32Formats The formats available
* @note Host glue code
*/
{
}
/**
* Initialise the host side of the shared clipboard.
* @note Host glue code
*/
int vboxClipboardInit (void)
{
return VINF_SUCCESS;
}
/**
* Terminate the host side of the shared clipboard.
* @note host glue code
*/
void vboxClipboardDestroy (void)
{
}
/**
* Connect a guest to the shared clipboard.
* @note host glue code
* @note on the host, we assume that some other application already owns
* the clipboard and leave ownership to X11.
*/
{
int rc = VINF_SUCCESS;
LogRel(("Starting host clipboard service\n"));
if (!pCtx)
rc = VERR_NO_MEMORY;
else
{
rc = VERR_NO_MEMORY;
else
{
}
if (RT_FAILURE(rc))
}
if (RT_FAILURE(rc))
{
LogRel(("Failed to initialise the shared clipboard\n"));
}
return rc;
}
/**
* Synchronise the contents of the host clipboard with the guest, called
* after a save and restore of the guest.
* @note Host glue code
*/
{
/* Tell the guest we have no data in case X11 is not available. If
* there is data in the host clipboard it will automatically be sent to
* the guest when the clipboard starts up. */
return VINF_SUCCESS;
}
/**
* Shut down the shared clipboard service and "disconnect" the guest.
* @note Host glue code
*/
{
LogFlow(("vboxClipboardDisconnect\n"));
LogRel(("Stopping the host clipboard service\n"));
/* Drop the reference to the client, in case it is still there. This
* will cause any outstanding clipboard data requests from X11 to fail
* immediately. */
pCtx->fShuttingDown = true;
/* If there is a currently pending request, release it immediately. */
/** @todo handle this slightly more reasonably, or be really sure
* it won't go wrong. */
{
}
}
/**
* VBox is taking possession of the shared clipboard.
*
* @param pClient Context data for the guest system
* @param u32Formats Clipboard formats the guest is offering
* @note Host glue code
*/
{
u32Formats));
}
/**
* Called when VBox wants to read the X11 clipboard.
*
* @param pClient Context information about the guest VM
* @param u32Format The format that the guest would like to receive the data in
* @param pv Where to write the data to
* @param cb The size of the buffer to write the data to
* @param pcbActual Where to write the actual size of the written data
* @note Host glue code
*/
{
LogFlowFunc(("pClient=%p, u32Format=%02X, pv=%p, cb=%u, pcbActual=%p",
if (RT_SUCCESS(rc))
*pcbActual));
return rc;
}
/**
* Called when we have requested data from VBox and that data has arrived.
*
* @param pClient Context information about the guest VM
* @param pv Buffer to which the data was written
* @param cb The size of the data written
* @param u32Format The format of the data written
* @note Host glue code
*/
{
LogFlowFunc (("called. pClient=%p, pv=%p (%.*ls), cb=%u, u32Format=%02X\n",
/* Grab the mutex and check whether there is a pending request for data.
*/
{
if (cb > 0)
{
{
}
}
/* Signal that the request has been completed. */
}
}