1 | /* $Id: VBoxSharedClipboardSvc-x11.cpp 104627 2024-05-14 12:13:09Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Shared Clipboard Service - Linux host.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
|
---|
33 | #include <iprt/assert.h>
|
---|
34 | #include <iprt/critsect.h>
|
---|
35 | #include <iprt/env.h>
|
---|
36 | #include <iprt/mem.h>
|
---|
37 | #include <iprt/semaphore.h>
|
---|
38 | #include <iprt/string.h>
|
---|
39 | #include <iprt/asm.h>
|
---|
40 |
|
---|
41 | #include <VBox/GuestHost/SharedClipboard.h>
|
---|
42 | #include <VBox/GuestHost/SharedClipboard-x11.h>
|
---|
43 | #include <VBox/HostServices/VBoxClipboardSvc.h>
|
---|
44 | #include <iprt/errcore.h>
|
---|
45 |
|
---|
46 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
47 | # include <VBox/GuestHost/SharedClipboard-transfers.h>
|
---|
48 | # ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP
|
---|
49 | # include <VBox/GuestHost/SharedClipboard-transfers.h>
|
---|
50 | # endif
|
---|
51 | #endif
|
---|
52 |
|
---|
53 | #include "VBoxSharedClipboardSvc-internal.h"
|
---|
54 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
55 | # include "VBoxSharedClipboardSvc-transfers.h"
|
---|
56 | #endif
|
---|
57 |
|
---|
58 | /* Number of currently extablished connections. */
|
---|
59 | static volatile uint32_t g_cShClConnections;
|
---|
60 |
|
---|
61 |
|
---|
62 | /*********************************************************************************************************************************
|
---|
63 | * Structures and Typedefs *
|
---|
64 | *********************************************************************************************************************************/
|
---|
65 | /**
|
---|
66 | * Global context information used by the host glue for the X11 clipboard backend.
|
---|
67 | */
|
---|
68 | struct SHCLCONTEXT
|
---|
69 | {
|
---|
70 | /** This mutex is grabbed during any critical operations on the clipboard
|
---|
71 | * which might clash with others. */
|
---|
72 | RTCRITSECT CritSect;
|
---|
73 | /** X11 context data. */
|
---|
74 | SHCLX11CTX X11;
|
---|
75 | /** Pointer to the VBox host client data structure. */
|
---|
76 | PSHCLCLIENT pClient;
|
---|
77 | /** We set this when we start shutting down as a hint not to post any new
|
---|
78 | * requests. */
|
---|
79 | bool fShuttingDown;
|
---|
80 | };
|
---|
81 |
|
---|
82 |
|
---|
83 | /*********************************************************************************************************************************
|
---|
84 | * Prototypes *
|
---|
85 | *********************************************************************************************************************************/
|
---|
86 | static DECLCALLBACK(int) shClSvcX11ReportFormatsCallback(PSHCLCONTEXT pCtx, uint32_t fFormats, void *pvUser);
|
---|
87 | static DECLCALLBACK(int) shClSvcX11RequestDataFromSourceCallback(PSHCLCONTEXT pCtx, SHCLFORMAT uFmt, void **ppv, uint32_t *pcb, void *pvUser);
|
---|
88 |
|
---|
89 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
90 | static DECLCALLBACK(void) shClSvcX11TransferOnCreatedCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx);
|
---|
91 | static DECLCALLBACK(int) shClSvcX11TransferOnInitCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx);
|
---|
92 | static DECLCALLBACK(void) shClSvcX11TransferOnDestroyCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx);
|
---|
93 | static DECLCALLBACK(void) shClSvcX11TransferOnUnregisteredCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx, PSHCLTRANSFERCTX pTransferCtx);
|
---|
94 |
|
---|
95 | static DECLCALLBACK(int) shClSvcX11TransferIfaceHGRootListRead(PSHCLTXPROVIDERCTX pCtx);
|
---|
96 | #endif
|
---|
97 |
|
---|
98 |
|
---|
99 | /*********************************************************************************************************************************
|
---|
100 | * Backend implementation *
|
---|
101 | *********************************************************************************************************************************/
|
---|
102 | int ShClBackendInit(PSHCLBACKEND pBackend, VBOXHGCMSVCFNTABLE *pTable)
|
---|
103 | {
|
---|
104 | RT_NOREF(pBackend);
|
---|
105 |
|
---|
106 | LogFlowFuncEnter();
|
---|
107 |
|
---|
108 | /* Override the connection limit. */
|
---|
109 | for (uintptr_t i = 0; i < RT_ELEMENTS(pTable->acMaxClients); i++)
|
---|
110 | pTable->acMaxClients[i] = RT_MIN(VBOX_SHARED_CLIPBOARD_X11_CONNECTIONS_MAX, pTable->acMaxClients[i]);
|
---|
111 |
|
---|
112 | RT_ZERO(pBackend->Callbacks);
|
---|
113 | /* Use internal callbacks by default. */
|
---|
114 | pBackend->Callbacks.pfnReportFormats = shClSvcX11ReportFormatsCallback;
|
---|
115 | pBackend->Callbacks.pfnOnRequestDataFromSource = shClSvcX11RequestDataFromSourceCallback;
|
---|
116 |
|
---|
117 | return VINF_SUCCESS;
|
---|
118 | }
|
---|
119 |
|
---|
120 | void ShClBackendDestroy(PSHCLBACKEND pBackend)
|
---|
121 | {
|
---|
122 | RT_NOREF(pBackend);
|
---|
123 |
|
---|
124 | LogFlowFuncEnter();
|
---|
125 | }
|
---|
126 |
|
---|
127 | void ShClBackendSetCallbacks(PSHCLBACKEND pBackend, PSHCLCALLBACKS pCallbacks)
|
---|
128 | {
|
---|
129 | #define SET_FN_IF_NOT_NULL(a_Fn) \
|
---|
130 | if (pCallbacks->pfn##a_Fn) \
|
---|
131 | pBackend->Callbacks.pfn##a_Fn = pCallbacks->pfn##a_Fn;
|
---|
132 |
|
---|
133 | SET_FN_IF_NOT_NULL(ReportFormats);
|
---|
134 | SET_FN_IF_NOT_NULL(OnRequestDataFromSource);
|
---|
135 |
|
---|
136 | #undef SET_FN_IF_NOT_NULL
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * @note On the host, we assume that some other application already owns
|
---|
141 | * the clipboard and leave ownership to X11.
|
---|
142 | */
|
---|
143 | int ShClBackendConnect(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, bool fHeadless)
|
---|
144 | {
|
---|
145 | int rc;
|
---|
146 |
|
---|
147 | /* Check if maximum allowed connections count has reached. */
|
---|
148 | if (ASMAtomicIncU32(&g_cShClConnections) > VBOX_SHARED_CLIPBOARD_X11_CONNECTIONS_MAX)
|
---|
149 | {
|
---|
150 | ASMAtomicDecU32(&g_cShClConnections);
|
---|
151 | LogRel(("Shared Clipboard: maximum amount for client connections reached\n"));
|
---|
152 | return VERR_OUT_OF_RESOURCES;
|
---|
153 | }
|
---|
154 |
|
---|
155 | PSHCLCONTEXT pCtx = (PSHCLCONTEXT)RTMemAllocZ(sizeof(SHCLCONTEXT));
|
---|
156 | if (pCtx)
|
---|
157 | {
|
---|
158 | rc = RTCritSectInit(&pCtx->CritSect);
|
---|
159 | if (RT_SUCCESS(rc))
|
---|
160 | {
|
---|
161 | rc = ShClX11Init(&pCtx->X11, &pBackend->Callbacks, pCtx, fHeadless);
|
---|
162 | if (RT_SUCCESS(rc))
|
---|
163 | {
|
---|
164 | pClient->State.pCtx = pCtx;
|
---|
165 | pCtx->pClient = pClient;
|
---|
166 |
|
---|
167 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
168 | /*
|
---|
169 | * Set callbacks.
|
---|
170 | * Those will be registered within ShClSvcTransferInit() when a new transfer gets initialized.
|
---|
171 | *
|
---|
172 | * Used for starting / stopping the HTTP server.
|
---|
173 | */
|
---|
174 | RT_ZERO(pClient->Transfers.Callbacks);
|
---|
175 |
|
---|
176 | pClient->Transfers.Callbacks.pvUser = pCtx; /* Assign context as user-provided callback data. */
|
---|
177 | pClient->Transfers.Callbacks.cbUser = sizeof(SHCLCONTEXT);
|
---|
178 |
|
---|
179 | pClient->Transfers.Callbacks.pfnOnCreated = shClSvcX11TransferOnCreatedCallback;
|
---|
180 | pClient->Transfers.Callbacks.pfnOnInitialize = shClSvcX11TransferOnInitCallback;
|
---|
181 | pClient->Transfers.Callbacks.pfnOnDestroy = shClSvcX11TransferOnDestroyCallback;
|
---|
182 | pClient->Transfers.Callbacks.pfnOnUnregistered = shClSvcX11TransferOnUnregisteredCallback;
|
---|
183 | #endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
|
---|
184 |
|
---|
185 | rc = ShClX11ThreadStart(&pCtx->X11, true /* grab shared clipboard */);
|
---|
186 | if (RT_FAILURE(rc))
|
---|
187 | ShClX11Destroy(&pCtx->X11);
|
---|
188 | }
|
---|
189 |
|
---|
190 | if (RT_FAILURE(rc))
|
---|
191 | RTCritSectDelete(&pCtx->CritSect);
|
---|
192 | }
|
---|
193 |
|
---|
194 | if (RT_FAILURE(rc))
|
---|
195 | {
|
---|
196 | pClient->State.pCtx = NULL;
|
---|
197 | RTMemFree(pCtx);
|
---|
198 | }
|
---|
199 | }
|
---|
200 | else
|
---|
201 | rc = VERR_NO_MEMORY;
|
---|
202 |
|
---|
203 | if (RT_FAILURE(rc))
|
---|
204 | {
|
---|
205 | /* Restore active connections count. */
|
---|
206 | ASMAtomicDecU32(&g_cShClConnections);
|
---|
207 | }
|
---|
208 |
|
---|
209 | LogFlowFuncLeaveRC(rc);
|
---|
210 | return rc;
|
---|
211 | }
|
---|
212 |
|
---|
213 | int ShClBackendSync(PSHCLBACKEND pBackend, PSHCLCLIENT pClient)
|
---|
214 | {
|
---|
215 | RT_NOREF(pBackend);
|
---|
216 |
|
---|
217 | LogFlowFuncEnter();
|
---|
218 |
|
---|
219 | /* Tell the guest we have no data in case X11 is not available. If
|
---|
220 | * there is data in the host clipboard it will automatically be sent to
|
---|
221 | * the guest when the clipboard starts up. */
|
---|
222 | return ShClSvcReportFormats(pClient, VBOX_SHCL_FMT_NONE);
|
---|
223 | }
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Shuts down the shared clipboard service and "disconnect" the guest.
|
---|
227 | * Note! Host glue code
|
---|
228 | */
|
---|
229 | int ShClBackendDisconnect(PSHCLBACKEND pBackend, PSHCLCLIENT pClient)
|
---|
230 | {
|
---|
231 | RT_NOREF(pBackend);
|
---|
232 |
|
---|
233 | LogFlowFuncEnter();
|
---|
234 |
|
---|
235 | PSHCLCONTEXT pCtx = pClient->State.pCtx;
|
---|
236 | AssertPtr(pCtx);
|
---|
237 |
|
---|
238 | /* Drop the reference to the client, in case it is still there. This
|
---|
239 | * will cause any outstanding clipboard data requests from X11 to fail
|
---|
240 | * immediately. */
|
---|
241 | pCtx->fShuttingDown = true;
|
---|
242 |
|
---|
243 | int rc = ShClX11ThreadStop(&pCtx->X11);
|
---|
244 | /** @todo handle this slightly more reasonably, or be really sure
|
---|
245 | * it won't go wrong. */
|
---|
246 | AssertRC(rc);
|
---|
247 |
|
---|
248 | ShClX11Destroy(&pCtx->X11);
|
---|
249 | RTCritSectDelete(&pCtx->CritSect);
|
---|
250 |
|
---|
251 | RTMemFree(pCtx);
|
---|
252 |
|
---|
253 | /* Decrease active connections count. */
|
---|
254 | ASMAtomicDecU32(&g_cShClConnections);
|
---|
255 |
|
---|
256 | LogFlowFuncLeaveRC(rc);
|
---|
257 | return rc;
|
---|
258 | }
|
---|
259 |
|
---|
260 | /**
|
---|
261 | * Reports clipboard formats to the host clipboard.
|
---|
262 | */
|
---|
263 | int ShClBackendReportFormats(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, SHCLFORMATS fFormats)
|
---|
264 | {
|
---|
265 | RT_NOREF(pBackend);
|
---|
266 |
|
---|
267 | int rc = ShClX11ReportFormatsToX11Async(&pClient->State.pCtx->X11, fFormats);
|
---|
268 |
|
---|
269 | LogFlowFuncLeaveRC(rc);
|
---|
270 | return rc;
|
---|
271 | }
|
---|
272 |
|
---|
273 | /**
|
---|
274 | * Reads data from the host clipboard.
|
---|
275 | *
|
---|
276 | * Schedules a request to the X11 event thread.
|
---|
277 | *
|
---|
278 | * @note We always fail or complete asynchronously.
|
---|
279 | */
|
---|
280 | int ShClBackendReadData(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLCLIENTCMDCTX pCmdCtx, SHCLFORMAT uFormat,
|
---|
281 | void *pvData, uint32_t cbData, uint32_t *pcbActual)
|
---|
282 | {
|
---|
283 | RT_NOREF(pBackend);
|
---|
284 |
|
---|
285 | AssertPtrReturn(pClient, VERR_INVALID_POINTER);
|
---|
286 | AssertPtrReturn(pCmdCtx, VERR_INVALID_POINTER);
|
---|
287 | AssertPtrReturn(pvData, VERR_INVALID_POINTER);
|
---|
288 | AssertPtrReturn(pcbActual, VERR_INVALID_POINTER);
|
---|
289 |
|
---|
290 | RT_NOREF(pCmdCtx);
|
---|
291 |
|
---|
292 | LogFlowFunc(("pClient=%p, uFormat=%#x, pv=%p, cb=%RU32, pcbActual=%p\n",
|
---|
293 | pClient, uFormat, pvData, cbData, pcbActual));
|
---|
294 |
|
---|
295 | uint32_t cbRead;
|
---|
296 | int rc = ShClX11ReadDataFromX11(&pClient->State.pCtx->X11, &pClient->EventSrc,
|
---|
297 | SHCL_TIMEOUT_DEFAULT_MS, uFormat, pvData, cbData, &cbRead);
|
---|
298 | if (RT_SUCCESS(rc))
|
---|
299 | {
|
---|
300 | LogRel2(("Shared Clipboard: Read %RU32 bytes host X11 clipboard data\n", cbRead));
|
---|
301 | *pcbActual = cbRead;
|
---|
302 | }
|
---|
303 |
|
---|
304 | if (RT_FAILURE(rc))
|
---|
305 | LogRel(("Shared Clipboard: Error reading host clipboard data from X11, rc=%Rrc\n", rc));
|
---|
306 |
|
---|
307 | LogFlowFuncLeaveRC(rc);
|
---|
308 | return rc;
|
---|
309 | }
|
---|
310 |
|
---|
311 | int ShClBackendWriteData(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLCLIENTCMDCTX pCmdCtx,
|
---|
312 | SHCLFORMAT uFormat, void *pvData, uint32_t cbData)
|
---|
313 | {
|
---|
314 | RT_NOREF(pBackend, pClient, pCmdCtx, uFormat, pvData, cbData);
|
---|
315 |
|
---|
316 | LogFlowFuncEnter();
|
---|
317 |
|
---|
318 | /* Nothing to do here yet. */
|
---|
319 |
|
---|
320 | LogFlowFuncLeave();
|
---|
321 | return VINF_SUCCESS;
|
---|
322 | }
|
---|
323 |
|
---|
324 | /**
|
---|
325 | * @copydoc SHCLCALLBACKS::pfnReportFormats
|
---|
326 | *
|
---|
327 | * Reports clipboard formats to the guest.
|
---|
328 | */
|
---|
329 | static DECLCALLBACK(int) shClSvcX11ReportFormatsCallback(PSHCLCONTEXT pCtx, uint32_t fFormats, void *pvUser)
|
---|
330 | {
|
---|
331 | RT_NOREF(pvUser);
|
---|
332 |
|
---|
333 | LogFlowFunc(("pCtx=%p, fFormats=%#x\n", pCtx, fFormats));
|
---|
334 |
|
---|
335 | int rc = VINF_SUCCESS;
|
---|
336 | PSHCLCLIENT pClient = pCtx->pClient;
|
---|
337 | AssertPtr(pClient);
|
---|
338 |
|
---|
339 | rc = ShClSvcReportFormats(pClient, fFormats);
|
---|
340 |
|
---|
341 | LogFlowFuncLeaveRC(rc);
|
---|
342 | return rc;
|
---|
343 | }
|
---|
344 |
|
---|
345 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
346 | /**
|
---|
347 | * @copydoc SHCLTRANSFERCALLBACKS::pfnOnCreated
|
---|
348 | *
|
---|
349 | * @thread Service main thread.
|
---|
350 | */
|
---|
351 | static DECLCALLBACK(void) shClSvcX11TransferOnCreatedCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx)
|
---|
352 | {
|
---|
353 | LogFlowFuncEnter();
|
---|
354 |
|
---|
355 | PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pCbCtx->pvUser;
|
---|
356 | AssertPtr(pCtx);
|
---|
357 |
|
---|
358 | PSHCLTRANSFER pTransfer = pCbCtx->pTransfer;
|
---|
359 | AssertPtr(pTransfer);
|
---|
360 |
|
---|
361 | PSHCLCLIENT const pClient = pCtx->pClient;
|
---|
362 | AssertPtr(pClient);
|
---|
363 |
|
---|
364 | /*
|
---|
365 | * Set transfer provider.
|
---|
366 | * Those will be registered within ShClSvcTransferInit() when a new transfer gets initialized.
|
---|
367 | */
|
---|
368 |
|
---|
369 | /* Set the interface to the local provider by default first. */
|
---|
370 | RT_ZERO(pClient->Transfers.Provider);
|
---|
371 | ShClTransferProviderLocalQueryInterface(&pClient->Transfers.Provider);
|
---|
372 |
|
---|
373 | PSHCLTXPROVIDERIFACE pIface = &pClient->Transfers.Provider.Interface;
|
---|
374 |
|
---|
375 | pClient->Transfers.Provider.enmSource = pClient->State.enmSource;
|
---|
376 | pClient->Transfers.Provider.pvUser = pClient;
|
---|
377 |
|
---|
378 | switch (ShClTransferGetDir(pTransfer))
|
---|
379 | {
|
---|
380 | case SHCLTRANSFERDIR_FROM_REMOTE: /* Guest -> Host. */
|
---|
381 | {
|
---|
382 | pIface->pfnRootListRead = ShClSvcTransferIfaceGHRootListRead;
|
---|
383 |
|
---|
384 | pIface->pfnListOpen = ShClSvcTransferIfaceGHListOpen;
|
---|
385 | pIface->pfnListClose = ShClSvcTransferIfaceGHListClose;
|
---|
386 | pIface->pfnListHdrRead = ShClSvcTransferIfaceGHListHdrRead;
|
---|
387 | pIface->pfnListEntryRead = ShClSvcTransferIfaceGHListEntryRead;
|
---|
388 |
|
---|
389 | pIface->pfnObjOpen = ShClSvcTransferIfaceGHObjOpen;
|
---|
390 | pIface->pfnObjClose = ShClSvcTransferIfaceGHObjClose;
|
---|
391 | pIface->pfnObjRead = ShClSvcTransferIfaceGHObjRead;
|
---|
392 | break;
|
---|
393 | }
|
---|
394 |
|
---|
395 | case SHCLTRANSFERDIR_TO_REMOTE: /* Host -> Guest. */
|
---|
396 | {
|
---|
397 | pIface->pfnRootListRead = shClSvcX11TransferIfaceHGRootListRead;
|
---|
398 | break;
|
---|
399 | }
|
---|
400 |
|
---|
401 | default:
|
---|
402 | AssertFailed();
|
---|
403 | }
|
---|
404 |
|
---|
405 | int rc = ShClTransferSetProvider(pTransfer, &pClient->Transfers.Provider); RT_NOREF(rc);
|
---|
406 |
|
---|
407 | LogFlowFuncLeaveRC(rc);
|
---|
408 | }
|
---|
409 |
|
---|
410 | /**
|
---|
411 | * @copydoc SHCLTRANSFERCALLBACKS::pfnOnInitialize
|
---|
412 | *
|
---|
413 | * For G->H: Starts the HTTP server if not done yet and registers the transfer with it.
|
---|
414 | * For H->G: Called on transfer intialization to populate the transfer's root list.
|
---|
415 | *
|
---|
416 | * @thread Service main thread.
|
---|
417 | */
|
---|
418 | static DECLCALLBACK(int) shClSvcX11TransferOnInitCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx)
|
---|
419 | {
|
---|
420 | LogFlowFuncEnter();
|
---|
421 |
|
---|
422 | # ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP
|
---|
423 | PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pCbCtx->pvUser;
|
---|
424 | AssertPtr(pCtx);
|
---|
425 | # endif
|
---|
426 |
|
---|
427 | PSHCLTRANSFER pTransfer = pCbCtx->pTransfer;
|
---|
428 | AssertPtr(pTransfer);
|
---|
429 |
|
---|
430 | int rc;
|
---|
431 |
|
---|
432 | switch (ShClTransferGetDir(pTransfer))
|
---|
433 | {
|
---|
434 | case SHCLTRANSFERDIR_FROM_REMOTE: /* G->H */
|
---|
435 | {
|
---|
436 | # ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP
|
---|
437 | /* We only need to start the HTTP server when we actually receive data from the remote (host). */
|
---|
438 | rc = ShClTransferHttpServerMaybeStart(&pCtx->X11.HttpCtx);
|
---|
439 | # endif
|
---|
440 | break;
|
---|
441 | }
|
---|
442 |
|
---|
443 | case SHCLTRANSFERDIR_TO_REMOTE: /* H->G */
|
---|
444 | {
|
---|
445 | rc = ShClTransferRootListRead(pTransfer); /* Calls shClSvcX11TransferIfaceHGRootListRead(). */
|
---|
446 | break;
|
---|
447 | }
|
---|
448 |
|
---|
449 | default:
|
---|
450 | AssertFailedStmt(rc = VERR_NOT_SUPPORTED);
|
---|
451 | break;
|
---|
452 | }
|
---|
453 |
|
---|
454 | LogFlowFuncLeaveRC(rc);
|
---|
455 | return rc;
|
---|
456 | }
|
---|
457 |
|
---|
458 | /**
|
---|
459 | * @copydoc SHCLTRANSFERCALLBACKS::pfnOnDestroy
|
---|
460 | *
|
---|
461 | * This stops the HTTP server if not done yet.
|
---|
462 | *
|
---|
463 | * @thread Service main thread.
|
---|
464 | */
|
---|
465 | static DECLCALLBACK(void) shClSvcX11TransferOnDestroyCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx)
|
---|
466 | {
|
---|
467 | LogFlowFuncEnter();
|
---|
468 |
|
---|
469 | # ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP
|
---|
470 | PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pCbCtx->pvUser;
|
---|
471 | AssertPtr(pCtx);
|
---|
472 |
|
---|
473 | PSHCLTRANSFER pTransfer = pCbCtx->pTransfer;
|
---|
474 | AssertPtr(pTransfer);
|
---|
475 |
|
---|
476 | if (ShClTransferGetDir(pTransfer) == SHCLTRANSFERDIR_FROM_REMOTE)
|
---|
477 | ShClTransferHttpServerMaybeStop(&pCtx->X11.HttpCtx);
|
---|
478 | # else
|
---|
479 | RT_NOREF(pCbCtx);
|
---|
480 | # endif
|
---|
481 |
|
---|
482 | LogFlowFuncLeave();
|
---|
483 | }
|
---|
484 |
|
---|
485 | /**
|
---|
486 | * Unregisters a transfer from a HTTP server.
|
---|
487 | *
|
---|
488 | * This also stops the HTTP server if no active transfers are found anymore.
|
---|
489 | *
|
---|
490 | * @param pCtx Shared clipboard context to unregister transfer for.
|
---|
491 | * @param pTransfer Transfer to unregister.
|
---|
492 | *
|
---|
493 | * @thread Clipboard main thread.
|
---|
494 | */
|
---|
495 | static void shClSvcX11HttpTransferUnregister(PSHCLCONTEXT pCtx, PSHCLTRANSFER pTransfer)
|
---|
496 | {
|
---|
497 | if (ShClTransferGetDir(pTransfer) == SHCLTRANSFERDIR_FROM_REMOTE)
|
---|
498 | {
|
---|
499 | # ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP
|
---|
500 | if (ShClTransferHttpServerIsInitialized(&pCtx->X11.HttpCtx.HttpServer))
|
---|
501 | {
|
---|
502 | ShClTransferHttpServerUnregisterTransfer(&pCtx->X11.HttpCtx.HttpServer, pTransfer);
|
---|
503 | ShClTransferHttpServerMaybeStop(&pCtx->X11.HttpCtx);
|
---|
504 | }
|
---|
505 | # else
|
---|
506 | RT_NOREF(pCtx);
|
---|
507 | # endif
|
---|
508 | }
|
---|
509 |
|
---|
510 | //ShClTransferRelease(pTransfer);
|
---|
511 | }
|
---|
512 |
|
---|
513 | /**
|
---|
514 | * @copydoc SHCLTRANSFERCALLBACKS::pfnOnUnregistered
|
---|
515 | *
|
---|
516 | * Unregisters a (now) unregistered transfer from the HTTP server.
|
---|
517 | *
|
---|
518 | * @thread Clipboard main thread.
|
---|
519 | */
|
---|
520 | static DECLCALLBACK(void) shClSvcX11TransferOnUnregisteredCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx, PSHCLTRANSFERCTX pTransferCtx)
|
---|
521 | {
|
---|
522 | RT_NOREF(pTransferCtx);
|
---|
523 | shClSvcX11HttpTransferUnregister((PSHCLCONTEXT)pCbCtx->pvUser, pCbCtx->pTransfer);
|
---|
524 | }
|
---|
525 | #endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
|
---|
526 |
|
---|
527 | /**
|
---|
528 | * @copydoc SHCLCALLBACKS::pfnOnRequestDataFromSource
|
---|
529 | *
|
---|
530 | * Requests clipboard data from the guest.
|
---|
531 | *
|
---|
532 | * @thread Called from X11 event thread.
|
---|
533 | */
|
---|
534 | static DECLCALLBACK(int) shClSvcX11RequestDataFromSourceCallback(PSHCLCONTEXT pCtx,
|
---|
535 | SHCLFORMAT uFmt, void **ppv, uint32_t *pcb, void *pvUser)
|
---|
536 | {
|
---|
537 | RT_NOREF(pvUser);
|
---|
538 |
|
---|
539 | LogFlowFunc(("pCtx=%p, uFmt=0x%x\n", pCtx, uFmt));
|
---|
540 |
|
---|
541 | if (pCtx->fShuttingDown)
|
---|
542 | {
|
---|
543 | /* The shared clipboard is disconnecting. */
|
---|
544 | LogRel(("Shared Clipboard: Host requested guest clipboard data after guest had disconnected\n"));
|
---|
545 | return VERR_WRONG_ORDER;
|
---|
546 | }
|
---|
547 |
|
---|
548 | PSHCLCLIENT const pClient = pCtx->pClient;
|
---|
549 | int rc = ShClSvcReadDataFromGuest(pClient, uFmt, ppv, pcb);
|
---|
550 | if (RT_FAILURE(rc))
|
---|
551 | return rc;
|
---|
552 |
|
---|
553 | /*
|
---|
554 | * Note: We always return a generic URI list (as HTTP links) here.
|
---|
555 | * As we don't know which Atom target format was requested by the caller, the X11 clipboard codes needs
|
---|
556 | * to decide & transform the list into the actual clipboard Atom target format the caller wanted.
|
---|
557 | */
|
---|
558 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
559 | if (uFmt == VBOX_SHCL_FMT_URI_LIST)
|
---|
560 | {
|
---|
561 | PSHCLTRANSFER pTransfer;
|
---|
562 | rc = ShClSvcTransferCreate(pClient, SHCLTRANSFERDIR_FROM_REMOTE, SHCLSOURCE_REMOTE,
|
---|
563 | NIL_SHCLTRANSFERID /* Creates a new transfer ID */, &pTransfer);
|
---|
564 | if (RT_SUCCESS(rc))
|
---|
565 | {
|
---|
566 | /* Initialize the transfer on the host side. */
|
---|
567 | rc = ShClSvcTransferInit(pClient, pTransfer);
|
---|
568 | if (RT_FAILURE(rc))
|
---|
569 | ShClSvcTransferDestroy(pClient, pTransfer);
|
---|
570 | }
|
---|
571 |
|
---|
572 | if (RT_SUCCESS(rc))
|
---|
573 | {
|
---|
574 | /* We have to wait for the guest reporting the transfer as being initialized.
|
---|
575 | * Only then we can start reading stuff. */
|
---|
576 | rc = ShClTransferWaitForStatus(pTransfer, SHCL_TIMEOUT_DEFAULT_MS, SHCLTRANSFERSTATUS_INITIALIZED);
|
---|
577 | if (RT_SUCCESS(rc))
|
---|
578 | {
|
---|
579 | rc = ShClTransferRootListRead(pTransfer);
|
---|
580 | if (RT_SUCCESS(rc))
|
---|
581 | {
|
---|
582 | # ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP
|
---|
583 | /* As soon as we register the transfer with the HTTP server, the transfer needs to have its roots set. */
|
---|
584 | PSHCLHTTPSERVER const pHttpSrv = &pCtx->X11.HttpCtx.HttpServer;
|
---|
585 | rc = ShClTransferHttpServerRegisterTransfer(pHttpSrv, pTransfer);
|
---|
586 | if (RT_SUCCESS(rc))
|
---|
587 | {
|
---|
588 | char *pszData;
|
---|
589 | size_t cbData;
|
---|
590 | rc = ShClTransferHttpConvertToStringList(pHttpSrv, pTransfer, &pszData, &cbData);
|
---|
591 | if (RT_SUCCESS(rc))
|
---|
592 | {
|
---|
593 | *ppv = pszData;
|
---|
594 | *pcb = cbData;
|
---|
595 | /* ppv has ownership of pszData now. */
|
---|
596 | }
|
---|
597 | }
|
---|
598 | # else
|
---|
599 | rc = VERR_NOT_SUPPORTED;
|
---|
600 | # endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP */
|
---|
601 | }
|
---|
602 | }
|
---|
603 | }
|
---|
604 | }
|
---|
605 | #endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
|
---|
606 |
|
---|
607 | if (RT_FAILURE(rc))
|
---|
608 | LogRel(("Shared Clipboard: Requesting X11 data in format %#x from guest failed with %Rrc\n", uFmt, rc));
|
---|
609 |
|
---|
610 | LogFlowFuncLeaveRC(rc);
|
---|
611 | return rc;
|
---|
612 | }
|
---|
613 |
|
---|
614 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
615 | /**
|
---|
616 | * Handles transfer status replies from the guest.
|
---|
617 | */
|
---|
618 | int ShClBackendTransferHandleStatusReply(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLTRANSFER pTransfer, SHCLSOURCE enmSource, SHCLTRANSFERSTATUS enmStatus, int rcStatus)
|
---|
619 | {
|
---|
620 | RT_NOREF(pBackend, pClient, enmSource, rcStatus);
|
---|
621 |
|
---|
622 | PSHCLCONTEXT pCtx = pClient->State.pCtx; RT_NOREF(pCtx);
|
---|
623 |
|
---|
624 | if (ShClTransferGetDir(pTransfer) == SHCLTRANSFERDIR_FROM_REMOTE) /* Guest -> Host */
|
---|
625 | {
|
---|
626 | switch (enmStatus)
|
---|
627 | {
|
---|
628 | case SHCLTRANSFERSTATUS_INITIALIZED:
|
---|
629 | {
|
---|
630 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP
|
---|
631 | int rc2 = ShClTransferHttpServerMaybeStart(&pCtx->X11.HttpCtx);
|
---|
632 | if (RT_SUCCESS(rc2))
|
---|
633 | {
|
---|
634 |
|
---|
635 | }
|
---|
636 |
|
---|
637 | if (RT_FAILURE(rc2))
|
---|
638 | LogRel(("Shared Clipboard: Registering HTTP transfer failed: %Rrc\n", rc2));
|
---|
639 | #endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP */
|
---|
640 | break;
|
---|
641 | }
|
---|
642 |
|
---|
643 | default:
|
---|
644 | break;
|
---|
645 | }
|
---|
646 | }
|
---|
647 |
|
---|
648 | return VINF_SUCCESS;
|
---|
649 | }
|
---|
650 |
|
---|
651 |
|
---|
652 | /*********************************************************************************************************************************
|
---|
653 | * Provider interface implementation *
|
---|
654 | *********************************************************************************************************************************/
|
---|
655 |
|
---|
656 | /** @copydoc SHCLTXPROVIDERIFACE::pfnRootListRead */
|
---|
657 | static DECLCALLBACK(int) shClSvcX11TransferIfaceHGRootListRead(PSHCLTXPROVIDERCTX pCtx)
|
---|
658 | {
|
---|
659 | LogFlowFuncEnter();
|
---|
660 |
|
---|
661 | PSHCLCLIENT pClient = (PSHCLCLIENT)pCtx->pvUser;
|
---|
662 | AssertPtr(pClient);
|
---|
663 |
|
---|
664 | AssertPtr(pClient->State.pCtx);
|
---|
665 | PSHCLX11CTX pX11 = &pClient->State.pCtx->X11;
|
---|
666 |
|
---|
667 | /* X supplies the data asynchronously, so we need to wait for data to arrive first. */
|
---|
668 | void *pvData;
|
---|
669 | uint32_t cbData;
|
---|
670 | int rc = ShClX11ReadDataFromX11Ex(pX11, &pClient->EventSrc, SHCL_TIMEOUT_DEFAULT_MS, VBOX_SHCL_FMT_URI_LIST,
|
---|
671 | &pvData, &cbData);
|
---|
672 | if (RT_SUCCESS(rc))
|
---|
673 | {
|
---|
674 | rc = ShClTransferRootsSetFromStringList(pCtx->pTransfer, (const char *)pvData, cbData);
|
---|
675 | if (RT_SUCCESS(rc))
|
---|
676 | LogRel2(("Shared Clipboard: Host reported %RU64 X11 root entries for transfer to guest\n",
|
---|
677 | ShClTransferRootsCount(pCtx->pTransfer)));
|
---|
678 |
|
---|
679 | RTMemFree(pvData);
|
---|
680 | }
|
---|
681 |
|
---|
682 | LogFlowFuncLeaveRC(rc);
|
---|
683 | return rc;
|
---|
684 | }
|
---|
685 | #endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
|
---|
686 |
|
---|