x11-clipboard.cpp revision 73712ecde1373991eab670665c3bbe07c1206429
/** @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"
struct _VBOXCLIPBOARDREQFROMVBOX;
typedef struct _VBOXCLIPBOARDREQFROMVBOX VBOXCLIPBOARDREQFROMVBOX;
/** 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;
};
/**
* 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
*/
{
LogRelFlow(("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));
}
/** Structure describing a request for clipoard data from the guest. */
struct _CLIPREADCBREQ
{
/** Where to write the returned data to. */
void *pv;
/** The size of the buffer in pv */
/** The actual size of the data written */
};
/**
* Called when VBox wants to read the X11 clipboard.
*
* @returns VINF_SUCCESS on successful completion
* @returns VINF_HGCM_ASYNC_EXECUTE if the operation will complete
* asynchronously
* @returns iprt status code on failure
* @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 We always fail or complete asynchronously
* @note On success allocates a CLIPREADCBREQ structure which must be
* freed in ClipCompleteDataRequestFromX11 when it is called back from
* the backend code.
*
*/
{
LogRelFlowFunc(("pClient=%p, u32Format=%02X, pv=%p, cb=%u, pcbActual=%p",
int rc = VINF_SUCCESS;
if (!pReq)
rc = VERR_NO_MEMORY;
else
{
if (RT_SUCCESS(rc))
}
return rc;
}
/**
* Complete a request from VBox for the X11 clipboard data. The data should
* be written to the buffer provided in the initial request.
* @param pCtx request context information
* @param rc the completion status of the request
* @param cbActual on successful completion, the number of bytes of data
* actually written, on buffer overflow the size of the
* buffer needed, ignored otherwise
* @todo change this to deal with the buffer issues rather than offloading
* them onto the caller
*/
{
}
/** A request for clipboard data from VBox */
struct _VBOXCLIPBOARDREQFROMVBOX
{
/** Data received */
void *pv;
/** The size of the data */
/** Format of the data */
/** A semaphore for waiting for the data */
};
/** Wait for clipboard data requested from VBox to arrive. */
{
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;
rc = VERR_NO_DATA;
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. */
LogRelFunc(("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;
}
/**
* 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
*/
{
LogRelFlowFunc (("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. */
}
}
#ifdef TESTCASE
#include <iprt/initterm.h>
#define TEST_NAME "tstClipboardX11-2"
struct _CLIPBACKEND
{
struct _READDATA
{
int rc;
} readData;
struct _COMPLETEREAD
{
int rc;
} completeRead;
struct _WRITEDATA
{
void *pv;
bool timeout;
} writeData;
struct _REPORTDATA
{
} reportData;
};
void vboxSvcClipboardReportMsg (VBOXCLIPBOARDCLIENTDATA *pClient, uint32_t u32Msg, uint32_t u32Formats)
{
else
return;
}
{
}
{
}
{
}
{
return VINF_SUCCESS;
}
{
return VINF_SUCCESS;
}
{
}
{
}
int main()
{
unsigned cErrors = 0;
{
RTPrintf(TEST_NAME ": vboxClipboardFormatAnnounce failed with VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT\n");
++cErrors;
}
if (rc != VINF_HGCM_ASYNC_EXECUTE)
{
++cErrors;
}
else
{
{
++cErrors;
}
else
{
{
++cErrors;
}
}
}
void *pv;
if ( rc != VINF_SUCCESS
|| cb != sizeof("testing"))
{
++cErrors;
}
else
if (rc != VERR_TIMEOUT)
{
++cErrors;
}
if (rc != VERR_NO_DATA)
{
++cErrors;
}
/* Data arriving after a timeout should *not* cause any segfaults or
* memory leaks. Check with Valgrind! */
if (cErrors > 0)
return cErrors > 0 ? 1 : 0;
}
#endif /* TESTCASE */