VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedClipboard/x11-clipboard.cpp@ 19560

Last change on this file since 19560 was 19536, checked in by vboxsync, 16 years ago

GuestHost/SharedClipboard/x11: renamed a struct

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.6 KB
Line 
1/** @file
2 *
3 * Shared Clipboard:
4 * Linux host.
5 */
6
7/*
8 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
19 * Clara, CA 95054 USA or visit http://www.sun.com if you need
20 * additional information or have any questions.
21 */
22
23#define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
24
25#include <string.h>
26
27#include <iprt/assert.h>
28#include <iprt/critsect.h>
29#include <iprt/mem.h>
30#include <iprt/semaphore.h>
31
32#include <VBox/GuestHost/SharedClipboard.h>
33#include <VBox/HostServices/VBoxClipboardSvc.h>
34
35#include "VBoxClipboard.h"
36
37/** A request for clipboard data from VBox */
38typedef struct _VBOXCLIPBOARDREQFROMVBOX
39{
40 /** Data received */
41 void *pv;
42 /** The size of the data */
43 uint32_t cb;
44 /** Format of the data */
45 uint32_t format;
46 /** A semaphore for waiting for the data */
47 RTSEMEVENT finished;
48} VBOXCLIPBOARDREQFROMVBOX;
49
50/** Global context information used by the host glue for the X11 clipboard
51 * backend */
52struct _VBOXCLIPBOARDCONTEXT
53{
54 /** This mutex is grabbed during any critical operations on the clipboard
55 * which might clash with others. */
56 RTCRITSECT clipboardMutex;
57 /** The currently pending request for data from VBox. NULL if there is
58 * no request pending. The protocol for completing a request is to grab
59 * the critical section, check that @a pReq is not NULL, fill in the data
60 * fields and set @a pReq to NULL. The protocol for cancelling a pending
61 * request is to grab the critical section and set pReq to NULL.
62 * It is an error if a request arrives while another one is pending, and
63 * the backend is responsible for ensuring that this does not happen. */
64 VBOXCLIPBOARDREQFROMVBOX *pReq;
65
66 /** Pointer to the opaque X11 backend structure */
67 CLIPBACKEND *pBackend;
68 /** Pointer to the VBox host client data structure. */
69 VBOXCLIPBOARDCLIENTDATA *pClient;
70 /** We set this when we start shutting down as a hint not to post any new
71 * requests. */
72 bool fShuttingDown;
73};
74
75static int vboxClipboardWaitForReq(VBOXCLIPBOARDCONTEXT *pCtx,
76 VBOXCLIPBOARDREQFROMVBOX *pReq,
77 uint32_t u32Format)
78{
79 int rc = VINF_SUCCESS;
80 LogFlowFunc(("pCtx=%p, pReq=%p, u32Format=%02X\n", pCtx, pReq, u32Format));
81 /* Request data from VBox */
82 vboxSvcClipboardReportMsg(pCtx->pClient,
83 VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA,
84 u32Format);
85 /* Which will signal us when it is ready. We use a timeout here
86 * because we can't be sure that the guest will behave correctly.
87 */
88 rc = RTSemEventWait(pReq->finished, CLIPBOARD_TIMEOUT);
89 /* If the request hasn't yet completed then we cancel it. We use
90 * the critical section to prevent these operations colliding. */
91 RTCritSectEnter(&pCtx->clipboardMutex);
92 /* The data may have arrived between the semaphore timing out and
93 * our grabbing the mutex. */
94 if (rc == VERR_TIMEOUT && pReq->pv != NULL)
95 rc = VINF_SUCCESS;
96 if (pCtx->pReq == pReq)
97 pCtx->pReq = NULL;
98 Assert(pCtx->pReq == NULL);
99 RTCritSectLeave(&pCtx->clipboardMutex);
100 LogFlowFunc(("returning %Rrc\n", rc));
101 return rc;
102}
103
104/** Post a request for clipboard data to VBox/the guest and wait for it to be
105 * completed. */
106static int vboxClipboardPostAndWaitForReq(VBOXCLIPBOARDCONTEXT *pCtx,
107 VBOXCLIPBOARDREQFROMVBOX *pReq,
108 uint32_t u32Format)
109{
110 int rc = VINF_SUCCESS;
111 LogFlowFunc(("pCtx=%p, pReq=%p, u32Format=%02X\n", pCtx, pReq,
112 u32Format));
113 /* Start by "posting" the request for the next invocation of
114 * vboxClipboardWriteData. */
115 RTCritSectEnter(&pCtx->clipboardMutex);
116 if (pCtx->pReq != NULL)
117 {
118 /* This would be a violation of the protocol, see the comments in the
119 * context structure definition. */
120 Assert(false);
121 rc = VERR_WRONG_ORDER;
122 }
123 else
124 pCtx->pReq = pReq;
125 RTCritSectLeave(&pCtx->clipboardMutex);
126 if (RT_SUCCESS(rc))
127 rc = vboxClipboardWaitForReq(pCtx, pReq, u32Format);
128 LogFlowFunc(("returning %Rrc\n", rc));
129 return rc;
130}
131
132/**
133 * Send a request to VBox to transfer the contents of its clipboard to X11.
134 *
135 * @param pCtx Pointer to the host clipboard structure
136 * @param u32Format The format in which the data should be transfered
137 * @param ppv On success and if pcb > 0, this will point to a buffer
138 * to be freed with RTMemFree containing the data read.
139 * @param pcb On success, this contains the number of bytes of data
140 * returned
141 * @note Host glue code.
142 */
143int VBoxX11ClipboardReadVBoxData (VBOXCLIPBOARDCONTEXT *pCtx,
144 uint32_t u32Format, void **ppv,
145 uint32_t *pcb)
146{
147 VBOXCLIPBOARDREQFROMVBOX request = { NULL };
148
149 LogFlowFunc(("pCtx=%p, u32Format=%02X, ppv=%p, pcb=%p\n", pCtx,
150 u32Format, ppv, pcb));
151 if (pCtx->fShuttingDown)
152 {
153 /* The shared clipboard is disconnecting. */
154 LogFunc(("host requested guest clipboard data after guest had disconnected.\n"));
155 return VERR_WRONG_ORDER;
156 }
157 int rc = RTSemEventCreate(&request.finished);
158 if (RT_SUCCESS(rc))
159 {
160 rc = vboxClipboardPostAndWaitForReq(pCtx, &request,
161 u32Format);
162 RTSemEventDestroy(request.finished);
163 }
164 if (RT_SUCCESS(rc))
165 {
166 *ppv = request.pv;
167 *pcb = request.cb;
168 }
169 LogFlowFunc(("returning %Rrc\n", rc));
170 if (RT_SUCCESS(rc))
171 LogFlowFunc(("*ppv=%.*ls, *pcb=%u\n", *pcb / 2, *ppv, *pcb));
172 return rc;
173}
174
175/**
176 * Report formats available in the X11 clipboard to VBox.
177 * @param pCtx Opaque context pointer for the glue code
178 * @param u32Formats The formats available
179 * @note Host glue code
180 */
181void VBoxX11ClipboardReportX11Formats(VBOXCLIPBOARDCONTEXT *pCtx,
182 uint32_t u32Formats)
183{
184 LogFlowFunc(("called. pCtx=%p, u32Formats=%02X\n", pCtx, u32Formats));
185 vboxSvcClipboardReportMsg(pCtx->pClient,
186 VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS,
187 u32Formats);
188}
189
190/**
191 * Initialise the host side of the shared clipboard.
192 * @note Host glue code
193 */
194int vboxClipboardInit (void)
195{
196 return VINF_SUCCESS;
197}
198
199/**
200 * Terminate the host side of the shared clipboard.
201 * @note host glue code
202 */
203void vboxClipboardDestroy (void)
204{
205
206}
207
208/**
209 * Connect a guest to the shared clipboard.
210 * @note host glue code
211 * @note on the host, we assume that some other application already owns
212 * the clipboard and leave ownership to X11.
213 */
214int vboxClipboardConnect (VBOXCLIPBOARDCLIENTDATA *pClient)
215{
216 int rc = VINF_SUCCESS;
217 CLIPBACKEND *pBackend = NULL;
218
219 LogRel(("Starting host clipboard service\n"));
220 VBOXCLIPBOARDCONTEXT *pCtx =
221 (VBOXCLIPBOARDCONTEXT *) RTMemAllocZ(sizeof(VBOXCLIPBOARDCONTEXT));
222 if (!pCtx)
223 rc = VERR_NO_MEMORY;
224 else
225 {
226 RTCritSectInit(&pCtx->clipboardMutex);
227 pBackend = VBoxX11ClipboardConstructX11(pCtx);
228 if (pBackend == NULL)
229 rc = VERR_NO_MEMORY;
230 else
231 {
232 pCtx->pBackend = pBackend;
233 pClient->pCtx = pCtx;
234 pCtx->pClient = pClient;
235 rc = VBoxX11ClipboardStartX11(pBackend);
236 }
237 if (RT_FAILURE(rc) && pBackend)
238 VBoxX11ClipboardStopX11(pCtx->pBackend);
239 if (RT_FAILURE(rc))
240 RTCritSectDelete(&pCtx->clipboardMutex);
241 }
242 if (RT_FAILURE(rc))
243 {
244 RTMemFree(pCtx);
245 LogRel(("Failed to initialise the shared clipboard\n"));
246 }
247 LogFlowFunc(("returning %Rrc\n", rc));
248 return rc;
249}
250
251/**
252 * Synchronise the contents of the host clipboard with the guest, called
253 * after a save and restore of the guest.
254 * @note Host glue code
255 */
256int vboxClipboardSync (VBOXCLIPBOARDCLIENTDATA *pClient)
257{
258 /* Tell the guest we have no data in case X11 is not available. If
259 * there is data in the host clipboard it will automatically be sent to
260 * the guest when the clipboard starts up. */
261 vboxSvcClipboardReportMsg (pClient,
262 VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS, 0);
263 return VINF_SUCCESS;
264}
265
266/**
267 * Shut down the shared clipboard service and "disconnect" the guest.
268 * @note Host glue code
269 */
270void vboxClipboardDisconnect (VBOXCLIPBOARDCLIENTDATA *pClient)
271{
272 LogFlow(("vboxClipboardDisconnect\n"));
273
274 LogRel(("Stopping the host clipboard service\n"));
275 VBOXCLIPBOARDCONTEXT *pCtx = pClient->pCtx;
276 /* Drop the reference to the client, in case it is still there. This
277 * will cause any outstanding clipboard data requests from X11 to fail
278 * immediately. */
279 pCtx->fShuttingDown = true;
280 /* If there is a currently pending request, release it immediately. */
281 vboxClipboardWriteData(pClient, NULL, 0, 0);
282 int rc = VBoxX11ClipboardStopX11(pCtx->pBackend);
283 /** @todo handle this slightly more reasonably, or be really sure
284 * it won't go wrong. */
285 AssertRC(rc);
286 if (RT_SUCCESS(rc)) /* And if not? */
287 {
288 VBoxX11ClipboardDestructX11(pCtx->pBackend);
289 RTCritSectDelete(&pCtx->clipboardMutex);
290 RTMemFree(pCtx);
291 }
292}
293
294/**
295 * VBox is taking possession of the shared clipboard.
296 *
297 * @param pClient Context data for the guest system
298 * @param u32Formats Clipboard formats the guest is offering
299 * @note Host glue code
300 */
301void vboxClipboardFormatAnnounce (VBOXCLIPBOARDCLIENTDATA *pClient,
302 uint32_t u32Formats)
303{
304 LogFlowFunc(("called. pClient=%p, u32Formats=%02X\n", pClient,
305 u32Formats));
306 VBoxX11ClipboardAnnounceVBoxFormat (pClient->pCtx->pBackend, u32Formats);
307}
308
309/**
310 * Called when VBox wants to read the X11 clipboard.
311 *
312 * @param pClient Context information about the guest VM
313 * @param u32Format The format that the guest would like to receive the data in
314 * @param pv Where to write the data to
315 * @param cb The size of the buffer to write the data to
316 * @param pcbActual Where to write the actual size of the written data
317 * @note Host glue code
318 */
319int vboxClipboardReadData (VBOXCLIPBOARDCLIENTDATA *pClient,
320 uint32_t u32Format, void *pv, uint32_t cb,
321 uint32_t *pcbActual)
322{
323 LogFlowFunc(("pClient=%p, u32Format=%02X, pv=%p, cb=%u, pcbActual=%p",
324 pClient, u32Format, pv, cb, pcbActual));
325 int rc = VBoxX11ClipboardReadX11Data(pClient->pCtx->pBackend, u32Format,
326 pv, cb, pcbActual);
327 LogFlowFunc(("returning %Rrc\n", rc));
328 if (RT_SUCCESS(rc))
329 LogFlowFunc(("*pv=%.*ls, *pcbActual=%u\n", *pcbActual / 2, pv,
330 *pcbActual));
331 return rc;
332}
333
334/**
335 * Called when we have requested data from VBox and that data has arrived.
336 *
337 * @param pClient Context information about the guest VM
338 * @param pv Buffer to which the data was written
339 * @param cb The size of the data written
340 * @param u32Format The format of the data written
341 * @note Host glue code
342 */
343void vboxClipboardWriteData (VBOXCLIPBOARDCLIENTDATA *pClient,
344 void *pv, uint32_t cb, uint32_t u32Format)
345{
346 LogFlowFunc (("called. pClient=%p, pv=%p (%.*ls), cb=%u, u32Format=%02X\n",
347 pClient, pv, cb / 2, pv, cb, u32Format));
348
349 VBOXCLIPBOARDCONTEXT *pCtx = pClient->pCtx;
350 /* Grab the mutex and check whether there is a pending request for data.
351 */
352 RTCritSectEnter(&pCtx->clipboardMutex);
353 VBOXCLIPBOARDREQFROMVBOX *pReq = pCtx->pReq;
354 if (pReq != NULL)
355 {
356 if (cb > 0)
357 {
358 pReq->pv = RTMemDup(pv, cb);
359 if (pReq->pv != NULL) /* NULL may also mean no memory... */
360 {
361 pReq->cb = cb;
362 pReq->format = u32Format;
363 }
364 }
365 /* Signal that the request has been completed. */
366 RTSemEventSignal(pReq->finished);
367 }
368 pCtx->pReq = NULL;
369 RTCritSectLeave(&pCtx->clipboardMutex);
370}
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