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