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 | #include <string.h>
|
---|
24 |
|
---|
25 | #include <iprt/asm.h> /* For atomic operations */
|
---|
26 | #include <iprt/assert.h>
|
---|
27 | #include <iprt/mem.h>
|
---|
28 | #include <iprt/semaphore.h>
|
---|
29 |
|
---|
30 | #include <VBox/GuestHost/SharedClipboard.h>
|
---|
31 | #include <VBox/HostServices/VBoxClipboardSvc.h>
|
---|
32 |
|
---|
33 | #include "VBoxClipboard.h"
|
---|
34 |
|
---|
35 | /** Global context information used by the host glue for the X11 clipboard
|
---|
36 | * backend */
|
---|
37 | struct _VBOXCLIPBOARDCONTEXT
|
---|
38 | {
|
---|
39 | /** Since the clipboard data moves asynchronously, we use an event
|
---|
40 | * semaphore to wait for it. When a function issues a request for
|
---|
41 | * clipboard data it must wait for this semaphore, which is triggered
|
---|
42 | * when the data arrives. */
|
---|
43 | RTSEMEVENT waitForData;
|
---|
44 | /** Who (if anyone) is currently waiting for data? Used for sanity
|
---|
45 | * checks when data arrives. */
|
---|
46 | volatile uint32_t waiter;
|
---|
47 | /** This mutex is grabbed during any critical operations on the clipboard
|
---|
48 | * which might clash with others. */
|
---|
49 | RTSEMMUTEX clipboardMutex;
|
---|
50 |
|
---|
51 | /** Pointer to the opaque X11 backend structure */
|
---|
52 | VBOXCLIPBOARDCONTEXTX11 *pBackend;
|
---|
53 | /** Pointer to the client data structure */
|
---|
54 | VBOXCLIPBOARDCLIENTDATA *pClient;
|
---|
55 | };
|
---|
56 |
|
---|
57 | /* Only one client is supported. There seems to be no need for more clients.
|
---|
58 | */
|
---|
59 | static VBOXCLIPBOARDCONTEXT g_ctxHost;
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Send a request to VBox to transfer the contents of its clipboard to X11.
|
---|
63 | *
|
---|
64 | * @param pCtx Pointer to the host clipboard structure
|
---|
65 | * @param u32Format The format in which the data should be transfered
|
---|
66 | * @param ppv On success and if pcb > 0, this will point to a buffer
|
---|
67 | * to be freed with RTMemFree containing the data read.
|
---|
68 | * @param pcb On success, this contains the number of bytes of data
|
---|
69 | * returned
|
---|
70 | * @note Host glue code.
|
---|
71 | */
|
---|
72 | int VBoxX11ClipboardReadVBoxData (VBOXCLIPBOARDCONTEXT *pCtx,
|
---|
73 | uint32_t u32Format, void **ppv,
|
---|
74 | uint32_t *pcb)
|
---|
75 | {
|
---|
76 | volatile VBOXCLIPBOARDCLIENTDATA *pClient = pCtx->pClient;
|
---|
77 |
|
---|
78 | LogFlowFunc(("u32Format=%02X\n", u32Format));
|
---|
79 | /* Zero our output parameters */
|
---|
80 | *ppv = NULL;
|
---|
81 | *pcb = 0;
|
---|
82 | if (pClient == NULL)
|
---|
83 | {
|
---|
84 | /* This can legitimately happen if we disconnect during a request for
|
---|
85 | * data from X11. */
|
---|
86 | LogFunc(("host requested guest clipboard data after guest had disconnected.\n"));
|
---|
87 | VBoxX11ClipboardAnnounceVBoxFormat(pCtx->pBackend, 0);
|
---|
88 | pCtx->waiter = NONE;
|
---|
89 | return VERR_TIMEOUT;
|
---|
90 | }
|
---|
91 | /* Assert that no other transfer is in process (requests are serialised)
|
---|
92 | * and that the last transfer cleaned up properly. */
|
---|
93 | AssertLogRelReturn( pClient->data.pv == NULL
|
---|
94 | && pClient->data.cb == 0
|
---|
95 | && pClient->data.u32Format == 0,
|
---|
96 | VERR_WRONG_ORDER
|
---|
97 | );
|
---|
98 | /* No one else (X11 or VBox) should currently be waiting. The first because
|
---|
99 | * requests from X11 are serialised and the second because VBox previously
|
---|
100 | * grabbed the clipboard, so it should not be waiting for data from us. */
|
---|
101 | AssertLogRelReturn (ASMAtomicCmpXchgU32(&pCtx->waiter, X11, NONE), VERR_DEADLOCK);
|
---|
102 | /* Request data from VBox */
|
---|
103 | vboxSvcClipboardReportMsg(pCtx->pClient,
|
---|
104 | VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA,
|
---|
105 | u32Format);
|
---|
106 | /* Which will signal us when it is ready. We use a timeout here because
|
---|
107 | * we can't be sure that the guest will behave correctly. */
|
---|
108 | int rc = RTSemEventWait(pCtx->waitForData, CLIPBOARDTIMEOUT);
|
---|
109 | if (rc == VERR_TIMEOUT)
|
---|
110 | rc = VINF_SUCCESS; /* Timeout handling follows. */
|
---|
111 | /* Now we have a potential race between the HGCM thread delivering the data
|
---|
112 | * and our setting waiter to NONE to say that we are no longer waiting for
|
---|
113 | * it. We solve this as follows: both of these operations are done under
|
---|
114 | * the clipboard mutex. The HGCM thread will only deliver the data if we
|
---|
115 | * are still waiting after it acquires the mutex. After we release the
|
---|
116 | * mutex, we finally do our check to see whether the data was delivered. */
|
---|
117 | RTSemMutexRequest(g_ctxHost.clipboardMutex, RT_INDEFINITE_WAIT);
|
---|
118 | pCtx->waiter = NONE;
|
---|
119 | RTSemMutexRelease(g_ctxHost.clipboardMutex);
|
---|
120 | AssertLogRelRCSuccess(rc);
|
---|
121 | if (RT_FAILURE(rc))
|
---|
122 | {
|
---|
123 | /* I believe this should not happen. Wait until the assertions arrive
|
---|
124 | * to prove the contrary. */
|
---|
125 | RTMemFree(pClient->data.pv);
|
---|
126 | g_ctxHost.pClient->data.pv = 0;
|
---|
127 | g_ctxHost.pClient->data.cb = 0;
|
---|
128 | g_ctxHost.pClient->data.u32Format = 0;
|
---|
129 | vboxClipboardFormatAnnounce(NULL, 0);
|
---|
130 | return rc;
|
---|
131 | }
|
---|
132 | if (pClient->data.pv == NULL)
|
---|
133 | return VERR_TIMEOUT;
|
---|
134 | LogFlowFunc(("wait completed. Returning.\n"));
|
---|
135 | *ppv = pClient->data.pv;
|
---|
136 | *pcb = pClient->data.cb;
|
---|
137 | g_ctxHost.pClient->data.pv = 0;
|
---|
138 | g_ctxHost.pClient->data.cb = 0;
|
---|
139 | g_ctxHost.pClient->data.u32Format = 0;
|
---|
140 | return VINF_SUCCESS;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Report formats available in the X11 clipboard to VBox.
|
---|
145 | * @param pCtx Opaque context pointer for the glue code
|
---|
146 | * @param u32Formats The formats available
|
---|
147 | * @note Host glue code
|
---|
148 | */
|
---|
149 | void VBoxX11ClipboardReportX11Formats(VBOXCLIPBOARDCONTEXT *pCtx,
|
---|
150 | uint32_t u32Formats)
|
---|
151 | {
|
---|
152 | vboxSvcClipboardReportMsg(pCtx->pClient,
|
---|
153 | VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS,
|
---|
154 | u32Formats);
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Initialise the host side of the shared clipboard.
|
---|
159 | * @note Host glue code
|
---|
160 | */
|
---|
161 | int vboxClipboardInit (void)
|
---|
162 | {
|
---|
163 | int rc = VINF_SUCCESS;
|
---|
164 | VBOXCLIPBOARDCONTEXTX11 *pBackend = NULL;
|
---|
165 |
|
---|
166 | LogRel(("Initializing host clipboard service\n"));
|
---|
167 | RTSemEventCreate(&g_ctxHost.waitForData);
|
---|
168 | RTSemMutexCreate(&g_ctxHost.clipboardMutex);
|
---|
169 | pBackend = VBoxX11ClipboardConstructX11(&g_ctxHost);
|
---|
170 | if (pBackend == NULL)
|
---|
171 | {
|
---|
172 | RTSemEventDestroy(g_ctxHost.waitForData);
|
---|
173 | RTSemMutexDestroy(g_ctxHost.clipboardMutex);
|
---|
174 | LogRel(("Failed to start the host shared clipboard service, out of memory.\n"));
|
---|
175 | rc = VERR_NO_MEMORY;
|
---|
176 | }
|
---|
177 | else
|
---|
178 | g_ctxHost.pBackend = pBackend;
|
---|
179 | return rc;
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Terminate the host side of the shared clipboard.
|
---|
184 | * @note host glue code
|
---|
185 | */
|
---|
186 | void vboxClipboardDestroy (void)
|
---|
187 | {
|
---|
188 | int rc = VINF_SUCCESS;
|
---|
189 | LogRelFunc(("shutting down host clipboard\n"));
|
---|
190 | VBoxX11ClipboardDestructX11(g_ctxHost.pBackend);
|
---|
191 | /* We can safely destroy these as the backend has exited
|
---|
192 | * successfully and no other calls from the host code should be
|
---|
193 | * forthcoming. */
|
---|
194 | RTSemEventDestroy(g_ctxHost.waitForData);
|
---|
195 | RTSemMutexDestroy(g_ctxHost.clipboardMutex);
|
---|
196 | }
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Connect a guest to the shared clipboard.
|
---|
200 | * @note host glue code
|
---|
201 | * @note on the host, we assume that some other application already owns
|
---|
202 | * the clipboard and leave ownership to X11.
|
---|
203 | */
|
---|
204 | int vboxClipboardConnect (VBOXCLIPBOARDCLIENTDATA *pClient)
|
---|
205 | {
|
---|
206 | int rc = VINF_SUCCESS;
|
---|
207 | LogFlowFunc(("\n"));
|
---|
208 | /* Only one client is supported for now */
|
---|
209 | AssertLogRelReturn(g_ctxHost.pClient == 0, VERR_NOT_SUPPORTED);
|
---|
210 | pClient->pCtx = &g_ctxHost;
|
---|
211 | pClient->pCtx->pClient = pClient;
|
---|
212 | /** The pClient pointer is a dummy anyway, as we only support a single
|
---|
213 | * client at a time. */
|
---|
214 | rc = VBoxX11ClipboardStartX11(g_ctxHost.pBackend,
|
---|
215 | true /* fOwnClipboard */);
|
---|
216 | return rc;
|
---|
217 | }
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Synchronise the contents of the host clipboard with the guest, called
|
---|
221 | * after a save and restore of the guest.
|
---|
222 | * @note Host glue code
|
---|
223 | */
|
---|
224 | int vboxClipboardSync (VBOXCLIPBOARDCLIENTDATA *pClient)
|
---|
225 | {
|
---|
226 |
|
---|
227 | /* On a Linux host, the guest should never synchronise/cache its
|
---|
228 | * clipboard contents, as we have no way of reliably telling when the
|
---|
229 | * host clipboard data changes. So instead of synchronising, we tell
|
---|
230 | * the guest to empty its clipboard, and we set the cached flag so that
|
---|
231 | * we report formats to the guest next time we poll for them. */
|
---|
232 | /** @note This has been changed so that the message is sent even if
|
---|
233 | * X11 is not available. */
|
---|
234 | vboxSvcClipboardReportMsg (g_ctxHost.pClient,
|
---|
235 | VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS, 0);
|
---|
236 | VBoxX11ClipboardRequestSyncX11(g_ctxHost.pBackend);
|
---|
237 |
|
---|
238 | return VINF_SUCCESS;
|
---|
239 | }
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Shut down the shared clipboard service and "disconnect" the guest.
|
---|
243 | * @note Host glue code
|
---|
244 | */
|
---|
245 | void vboxClipboardDisconnect (VBOXCLIPBOARDCLIENTDATA *)
|
---|
246 | {
|
---|
247 | LogFlow(("vboxClipboardDisconnect\n"));
|
---|
248 |
|
---|
249 | RTSemMutexRequest(g_ctxHost.clipboardMutex, RT_INDEFINITE_WAIT);
|
---|
250 | /* Drop the reference to the client, in case it is still there. This
|
---|
251 | * will cause any outstanding clipboard data requests from X11 to fail
|
---|
252 | * immediately. */
|
---|
253 | g_ctxHost.pClient = NULL;
|
---|
254 | /* The backend may be waiting for data from VBox. At this point it is no
|
---|
255 | * longer going to arrive, and we must release it to allow the event
|
---|
256 | * loop to terminate. In this case the buffer where VBox would have
|
---|
257 | * written the clipboard data will still be empty and we will just
|
---|
258 | * return "no data" to the backend. Any subsequent attempts to get the
|
---|
259 | * data from VBox will fail immediately as the client reference is gone.
|
---|
260 | */
|
---|
261 | /** @note This has been made unconditional, as it should do no harm
|
---|
262 | * even if we are not waiting. */
|
---|
263 | RTSemEventSignal(g_ctxHost.waitForData);
|
---|
264 | int rc = VBoxX11ClipboardStopX11(g_ctxHost.pBackend);
|
---|
265 | /** @todo handle this slightly more reasonably, or be really sure
|
---|
266 | * it won't go wrong. */
|
---|
267 | AssertRC(rc);
|
---|
268 | RTSemMutexRelease(g_ctxHost.clipboardMutex);
|
---|
269 | }
|
---|
270 |
|
---|
271 | /**
|
---|
272 | * VBox is taking possession of the shared clipboard.
|
---|
273 | *
|
---|
274 | * @param pClient Context data for the guest system
|
---|
275 | * @param u32Formats Clipboard formats the guest is offering
|
---|
276 | * @note Host glue code
|
---|
277 | */
|
---|
278 | void vboxClipboardFormatAnnounce (VBOXCLIPBOARDCLIENTDATA *pClient, uint32_t u32Formats)
|
---|
279 | {
|
---|
280 | VBoxX11ClipboardAnnounceVBoxFormat (g_ctxHost.pBackend, u32Formats);
|
---|
281 | }
|
---|
282 |
|
---|
283 | /**
|
---|
284 | * Called when VBox wants to read the X11 clipboard.
|
---|
285 | *
|
---|
286 | * @param pClient Context information about the guest VM
|
---|
287 | * @param u32Format The format that the guest would like to receive the data in
|
---|
288 | * @param pv Where to write the data to
|
---|
289 | * @param cb The size of the buffer to write the data to
|
---|
290 | * @param pcbActual Where to write the actual size of the written data
|
---|
291 | * @note Host glue code
|
---|
292 | */
|
---|
293 | int vboxClipboardReadData (VBOXCLIPBOARDCLIENTDATA *pClient,
|
---|
294 | uint32_t u32Format, void *pv, uint32_t cb,
|
---|
295 | uint32_t *pcbActual)
|
---|
296 | {
|
---|
297 | int rc = VINF_SUCCESS;
|
---|
298 | VBOXCLIPBOARDREQUEST request;
|
---|
299 | /* No one else (VBox or X11) should currently be waiting. The first
|
---|
300 | * because requests from VBox are serialised and the second because X11
|
---|
301 | * previously grabbed the clipboard, so it should not be waiting for
|
---|
302 | * data from us. */
|
---|
303 | AssertLogRelReturn (ASMAtomicCmpXchgU32(&g_ctxHost.waiter, VB, NONE),
|
---|
304 | VERR_DEADLOCK);
|
---|
305 | request.pv = pv;
|
---|
306 | request.cb = cb;
|
---|
307 | request.pcbActual = pcbActual;
|
---|
308 | request.pCtx = g_ctxHost.pBackend;
|
---|
309 | rc = VBoxX11ClipboardReadX11Data(g_ctxHost.pBackend, u32Format, &request);
|
---|
310 | g_ctxHost.waiter = NONE;
|
---|
311 | return rc;
|
---|
312 | }
|
---|
313 |
|
---|
314 | /**
|
---|
315 | * Called when we have requested data from VBox and that data has arrived.
|
---|
316 | *
|
---|
317 | * @param pClient Context information about the guest VM
|
---|
318 | * @param pv Buffer to which the data was written
|
---|
319 | * @param cb The size of the data written
|
---|
320 | * @param u32Format The format of the data written
|
---|
321 | * @note Host glue code
|
---|
322 | */
|
---|
323 | void vboxClipboardWriteData (VBOXCLIPBOARDCLIENTDATA *pClient,
|
---|
324 | void *pv, uint32_t cb, uint32_t u32Format)
|
---|
325 | {
|
---|
326 | /* Assume that if this gets called at all then the X11 backend is running. */
|
---|
327 | #if 0
|
---|
328 | if (!g_fHaveX11)
|
---|
329 | return;
|
---|
330 | #endif
|
---|
331 |
|
---|
332 | LogFlowFunc (("called\n"));
|
---|
333 |
|
---|
334 | /* Assert that no other transfer is in process (requests are serialised)
|
---|
335 | * or has not cleaned up properly. */
|
---|
336 | AssertLogRelReturnVoid ( pClient->data.pv == NULL
|
---|
337 | && pClient->data.cb == 0
|
---|
338 | && pClient->data.u32Format == 0);
|
---|
339 |
|
---|
340 | /* Grab the mutex and check that X11 is still waiting for the data before
|
---|
341 | * delivering it. See the explanation in VBoxX11ClipboardReadVBoxData. */
|
---|
342 | RTSemMutexRequest(g_ctxHost.clipboardMutex, RT_INDEFINITE_WAIT);
|
---|
343 | if (g_ctxHost.waiter == X11 && cb > 0)
|
---|
344 | {
|
---|
345 | pClient->data.pv = RTMemAlloc (cb);
|
---|
346 |
|
---|
347 | if (pClient->data.pv)
|
---|
348 | {
|
---|
349 | memcpy (pClient->data.pv, pv, cb);
|
---|
350 | pClient->data.cb = cb;
|
---|
351 | pClient->data.u32Format = u32Format;
|
---|
352 | }
|
---|
353 | }
|
---|
354 | RTSemMutexRelease(g_ctxHost.clipboardMutex);
|
---|
355 |
|
---|
356 | RTSemEventSignal(g_ctxHost.waitForData);
|
---|
357 | }
|
---|
358 |
|
---|