VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedClipboard/VBoxSharedClipboardSvc-x11.cpp@ 97215

Last change on this file since 97215 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.6 KB
Line 
1/* $Id: VBoxSharedClipboardSvc-x11.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * Shared Clipboard Service - Linux host.
4 */
5
6/*
7 * Copyright (C) 2006-2022 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#include "VBoxSharedClipboardSvc-internal.h"
47
48/* Number of currently extablished connections. */
49static volatile uint32_t g_cShClConnections;
50
51
52/*********************************************************************************************************************************
53* Structures and Typedefs *
54*********************************************************************************************************************************/
55/**
56 * Global context information used by the host glue for the X11 clipboard backend.
57 */
58struct SHCLCONTEXT
59{
60 /** This mutex is grabbed during any critical operations on the clipboard
61 * which might clash with others. */
62 RTCRITSECT CritSect;
63 /** X11 context data. */
64 SHCLX11CTX X11;
65 /** Pointer to the VBox host client data structure. */
66 PSHCLCLIENT pClient;
67 /** We set this when we start shutting down as a hint not to post any new
68 * requests. */
69 bool fShuttingDown;
70};
71
72
73/*********************************************************************************************************************************
74* Prototypes *
75*********************************************************************************************************************************/
76static DECLCALLBACK(int) shClReportFormatsCallback(PSHCLCONTEXT pCtx, uint32_t fFormats, void *pvUser);
77static DECLCALLBACK(int) shClSendDataToDestCallback(PSHCLCONTEXT pCtx, void *pv, uint32_t cb, void *pvUser);
78static DECLCALLBACK(int) shClRequestDataFromSourceCallback(PSHCLCONTEXT pCtx, SHCLFORMAT uFmt, void **ppv, uint32_t *pcb, void *pvUser);
79
80
81int ShClBackendInit(PSHCLBACKEND pBackend, VBOXHGCMSVCFNTABLE *pTable)
82{
83 RT_NOREF(pBackend);
84
85 LogFlowFuncEnter();
86
87 /* Override the connection limit. */
88 for (uintptr_t i = 0; i < RT_ELEMENTS(pTable->acMaxClients); i++)
89 pTable->acMaxClients[i] = RT_MIN(VBOX_SHARED_CLIPBOARD_X11_CONNECTIONS_MAX, pTable->acMaxClients[i]);
90
91 RT_ZERO(pBackend->Callbacks);
92 /* Use internal callbacks by default. */
93 pBackend->Callbacks.pfnReportFormats = shClReportFormatsCallback;
94 pBackend->Callbacks.pfnOnRequestDataFromSource = shClRequestDataFromSourceCallback;
95 pBackend->Callbacks.pfnOnSendDataToDest = shClSendDataToDestCallback;
96
97 return VINF_SUCCESS;
98}
99
100void ShClBackendDestroy(PSHCLBACKEND pBackend)
101{
102 RT_NOREF(pBackend);
103
104 LogFlowFuncEnter();
105}
106
107void ShClBackendSetCallbacks(PSHCLBACKEND pBackend, PSHCLCALLBACKS pCallbacks)
108{
109#define SET_FN_IF_NOT_NULL(a_Fn) \
110 if (pCallbacks->pfn##a_Fn) \
111 pBackend->Callbacks.pfn##a_Fn = pCallbacks->pfn##a_Fn;
112
113 SET_FN_IF_NOT_NULL(ReportFormats);
114 SET_FN_IF_NOT_NULL(OnClipboardRead);
115 SET_FN_IF_NOT_NULL(OnClipboardWrite);
116 SET_FN_IF_NOT_NULL(OnRequestDataFromSource);
117 SET_FN_IF_NOT_NULL(OnSendDataToDest);
118
119#undef SET_FN_IF_NOT_NULL
120}
121
122/**
123 * @note On the host, we assume that some other application already owns
124 * the clipboard and leave ownership to X11.
125 */
126int ShClBackendConnect(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, bool fHeadless)
127{
128 int rc;
129
130 /* Check if maximum allowed connections count has reached. */
131 if (ASMAtomicIncU32(&g_cShClConnections) > VBOX_SHARED_CLIPBOARD_X11_CONNECTIONS_MAX)
132 {
133 ASMAtomicDecU32(&g_cShClConnections);
134 LogRel(("Shared Clipboard: maximum amount for client connections reached\n"));
135 return VERR_OUT_OF_RESOURCES;
136 }
137
138 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)RTMemAllocZ(sizeof(SHCLCONTEXT));
139 if (pCtx)
140 {
141 rc = RTCritSectInit(&pCtx->CritSect);
142 if (RT_SUCCESS(rc))
143 {
144 rc = ShClX11Init(&pCtx->X11, &pBackend->Callbacks, pCtx, fHeadless);
145 if (RT_SUCCESS(rc))
146 {
147 pClient->State.pCtx = pCtx;
148 pCtx->pClient = pClient;
149
150 rc = ShClX11ThreadStart(&pCtx->X11, true /* grab shared clipboard */);
151 if (RT_FAILURE(rc))
152 ShClX11Destroy(&pCtx->X11);
153 }
154
155 if (RT_FAILURE(rc))
156 RTCritSectDelete(&pCtx->CritSect);
157 }
158
159 if (RT_FAILURE(rc))
160 {
161 pClient->State.pCtx = NULL;
162 RTMemFree(pCtx);
163 }
164 }
165 else
166 rc = VERR_NO_MEMORY;
167
168 if (RT_FAILURE(rc))
169 {
170 /* Restore active connections count. */
171 ASMAtomicDecU32(&g_cShClConnections);
172 }
173
174 LogFlowFuncLeaveRC(rc);
175 return rc;
176}
177
178int ShClBackendSync(PSHCLBACKEND pBackend, PSHCLCLIENT pClient)
179{
180 RT_NOREF(pBackend);
181
182 LogFlowFuncEnter();
183
184 /* Tell the guest we have no data in case X11 is not available. If
185 * there is data in the host clipboard it will automatically be sent to
186 * the guest when the clipboard starts up. */
187 if (ShClSvcIsBackendActive())
188 return ShClSvcHostReportFormats(pClient, VBOX_SHCL_FMT_NONE);
189 return VINF_SUCCESS;
190}
191
192/*
193 * Shut down the shared clipboard service and "disconnect" the guest.
194 * Note! Host glue code
195 */
196int ShClBackendDisconnect(PSHCLBACKEND pBackend, PSHCLCLIENT pClient)
197{
198 RT_NOREF(pBackend);
199
200 LogFlowFuncEnter();
201
202 PSHCLCONTEXT pCtx = pClient->State.pCtx;
203 AssertPtr(pCtx);
204
205 /* Drop the reference to the client, in case it is still there. This
206 * will cause any outstanding clipboard data requests from X11 to fail
207 * immediately. */
208 pCtx->fShuttingDown = true;
209
210 int rc = ShClX11ThreadStop(&pCtx->X11);
211 /** @todo handle this slightly more reasonably, or be really sure
212 * it won't go wrong. */
213 AssertRC(rc);
214
215 ShClX11Destroy(&pCtx->X11);
216 RTCritSectDelete(&pCtx->CritSect);
217
218 RTMemFree(pCtx);
219
220 /* Decrease active connections count. */
221 ASMAtomicDecU32(&g_cShClConnections);
222
223 LogFlowFuncLeaveRC(rc);
224 return rc;
225}
226
227int ShClBackendReportFormats(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, SHCLFORMATS fFormats)
228{
229 RT_NOREF(pBackend);
230
231 int rc = ShClX11ReportFormatsToX11(&pClient->State.pCtx->X11, fFormats);
232
233 LogFlowFuncLeaveRC(rc);
234 return rc;
235}
236
237/** Structure describing a request for clipoard data from the guest. */
238struct CLIPREADCBREQ
239{
240 /** User-supplied data pointer, based on the request type. */
241 void *pv;
242 /** The size (in bytes) of the the user-supplied pointer in pv. */
243 uint32_t cb;
244 /** The actual size of the data written. */
245 uint32_t *pcbActual;
246 /** The request's event ID. */
247 SHCLEVENTID idEvent;
248};
249
250/**
251 * @note We always fail or complete asynchronously.
252 * @note On success allocates a CLIPREADCBREQ structure which must be
253 * freed in ClipCompleteDataRequestFromX11 when it is called back from
254 * the backend code.
255 */
256int ShClBackendReadData(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLCLIENTCMDCTX pCmdCtx, SHCLFORMAT uFormat,
257 void *pvData, uint32_t cbData, uint32_t *pcbActual)
258{
259 RT_NOREF(pBackend);
260
261 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
262 AssertPtrReturn(pCmdCtx, VERR_INVALID_POINTER);
263 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
264 AssertPtrReturn(pcbActual, VERR_INVALID_POINTER);
265
266 RT_NOREF(pCmdCtx);
267
268 LogFlowFunc(("pClient=%p, uFormat=%#x, pv=%p, cb=%RU32, pcbActual=%p\n",
269 pClient, uFormat, pvData, cbData, pcbActual));
270
271 int rc;
272
273 CLIPREADCBREQ *pReq = (CLIPREADCBREQ *)RTMemAllocZ(sizeof(CLIPREADCBREQ));
274 if (pReq)
275 {
276 PSHCLEVENT pEvent;
277 rc = ShClEventSourceGenerateAndRegisterEvent(&pClient->EventSrc, &pEvent);
278 if (RT_SUCCESS(rc))
279 {
280 pReq->pv = pvData;
281 pReq->cb = cbData;
282 pReq->pcbActual = pcbActual;
283 pReq->idEvent = pEvent->idEvent;
284
285 /* Note: ShClX11ReadDataFromX11() will consume pReq on success. */
286 rc = ShClX11ReadDataFromX11(&pClient->State.pCtx->X11, uFormat, pReq);
287 if (RT_SUCCESS(rc))
288 {
289 PSHCLEVENTPAYLOAD pPayload;
290 rc = ShClEventWait(pEvent, 30 * 1000, &pPayload);
291 if (RT_SUCCESS(rc))
292 {
293 if (pPayload)
294 {
295 memcpy(pvData, pPayload->pvData, RT_MIN(cbData, pPayload->cbData));
296
297 *pcbActual = (uint32_t)pPayload->cbData;
298
299 ShClPayloadFree(pPayload);
300 }
301 else /* No payload given; could happen on invalid / not-expected formats. */
302 *pcbActual = 0;
303 }
304 }
305
306 ShClEventRelease(pEvent);
307 }
308
309 if (RT_FAILURE(rc))
310 RTMemFree(pReq);
311 }
312 else
313 rc = VERR_NO_MEMORY;
314
315 if (RT_FAILURE(rc))
316 LogRel(("Shared Clipboard: Error reading host clipboard data from X11, rc=%Rrc\n", rc));
317
318 LogFlowFuncLeaveRC(rc);
319 return rc;
320}
321
322int ShClBackendWriteData(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLCLIENTCMDCTX pCmdCtx, SHCLFORMAT uFormat, void *pvData, uint32_t cbData)
323{
324 RT_NOREF(pBackend, pClient, pCmdCtx, uFormat, pvData, cbData);
325
326 LogFlowFuncEnter();
327
328 /* Nothing to do here yet. */
329
330 LogFlowFuncLeave();
331 return VINF_SUCCESS;
332}
333
334/** @copydoc SHCLCALLBACKS::pfnReportFormats */
335static DECLCALLBACK(int) shClReportFormatsCallback(PSHCLCONTEXT pCtx, uint32_t fFormats, void *pvUser)
336{
337 RT_NOREF(pvUser);
338
339 LogFlowFunc(("pCtx=%p, fFormats=%#x\n", pCtx, fFormats));
340
341 int rc = VINF_SUCCESS;
342 PSHCLCLIENT pClient = pCtx->pClient;
343 AssertPtr(pClient);
344
345 rc = RTCritSectEnter(&pClient->CritSect);
346 if (RT_SUCCESS(rc))
347 {
348 if (ShClSvcIsBackendActive())
349 {
350 /** @todo r=bird: BUGBUG: Revisit this */
351 if (fFormats != VBOX_SHCL_FMT_NONE) /* No formats to report? */
352 {
353 rc = ShClSvcHostReportFormats(pCtx->pClient, fFormats);
354 }
355 }
356
357 RTCritSectLeave(&pClient->CritSect);
358 }
359
360 LogFlowFuncLeaveRC(rc);
361 return rc;
362}
363
364/** @copydoc SHCLCALLBACKS::pfnOnSendDataToDest */
365static DECLCALLBACK(int) shClSendDataToDestCallback(PSHCLCONTEXT pCtx, void *pv, uint32_t cb, void *pvUser)
366{
367 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
368 AssertPtrReturn(pvUser, VERR_INVALID_POINTER);
369
370 PSHCLX11READDATAREQ pData = (PSHCLX11READDATAREQ)pvUser;
371 CLIPREADCBREQ *pReq = pData->pReq;
372
373 LogFlowFunc(("rcCompletion=%Rrc, pReq=%p, pv=%p, cb=%RU32, idEvent=%RU32\n",
374 pData->rcCompletion, pReq, pv, cb, pReq->idEvent));
375
376 if (pReq->idEvent != NIL_SHCLEVENTID)
377 {
378 int rc2;
379
380 PSHCLEVENTPAYLOAD pPayload = NULL;
381 if ( RT_SUCCESS(pData->rcCompletion)
382 && pv
383 && cb)
384 {
385 rc2 = ShClPayloadAlloc(pReq->idEvent, pv, cb, &pPayload);
386 AssertRC(rc2);
387 }
388
389 rc2 = RTCritSectEnter(&pCtx->pClient->CritSect);
390 if (RT_SUCCESS(rc2))
391 {
392 const PSHCLEVENT pEvent = ShClEventSourceGetFromId(&pCtx->pClient->EventSrc, pReq->idEvent);
393 if (pEvent)
394 rc2 = ShClEventSignal(pEvent, pPayload);
395
396 RTCritSectLeave(&pCtx->pClient->CritSect);
397
398 if (RT_SUCCESS(rc2))
399 pPayload = NULL;
400 }
401
402 if (pPayload)
403 ShClPayloadFree(pPayload);
404 }
405
406 if (pReq)
407 RTMemFree(pReq);
408
409 LogRel2(("Shared Clipboard: Reading X11 clipboard data from host completed with %Rrc\n", pData->rcCompletion));
410
411 return VINF_SUCCESS;
412}
413
414/** @copydoc SHCLCALLBACKS::pfnOnRequestDataFromSource */
415static DECLCALLBACK(int) shClRequestDataFromSourceCallback(PSHCLCONTEXT pCtx, SHCLFORMAT uFmt, void **ppv, uint32_t *pcb, void *pvUser)
416{
417 RT_NOREF(pvUser);
418
419 LogFlowFunc(("pCtx=%p, uFmt=0x%x\n", pCtx, uFmt));
420
421 if (pCtx->fShuttingDown)
422 {
423 /* The shared clipboard is disconnecting. */
424 LogRel(("Shared Clipboard: Host requested guest clipboard data after guest had disconnected\n"));
425 return VERR_WRONG_ORDER;
426 }
427
428 PSHCLCLIENT pClient = pCtx->pClient;
429 AssertPtr(pClient);
430
431 RTCritSectEnter(&pClient->CritSect);
432
433 int rc = VINF_SUCCESS;
434
435#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
436 /*
437 * Note: We always return a generic URI list here.
438 * As we don't know which Atom target format was requested by the caller, the X11 clipboard codes needs
439 * to decide & transform the list into the actual clipboard Atom target format the caller wanted.
440 */
441 if (uFmt == VBOX_SHCL_FMT_URI_LIST)
442 {
443 PSHCLTRANSFER pTransfer;
444 rc = shClSvcTransferStart(pCtx->pClient,
445 SHCLTRANSFERDIR_FROM_REMOTE, SHCLSOURCE_REMOTE,
446 &pTransfer);
447 if (RT_SUCCESS(rc))
448 {
449
450 }
451 else
452 LogRel(("Shared Clipboard: Initializing read transfer from guest failed with %Rrc\n", rc));
453
454 *ppv = NULL;
455 *pcb = 0;
456
457 rc = VERR_NO_DATA;
458 }
459#endif
460
461 if (RT_SUCCESS(rc))
462 {
463 /* Request data from the guest. */
464 PSHCLEVENT pEvent;
465 rc = ShClSvcGuestDataRequest(pCtx->pClient, uFmt, &pEvent);
466 if (RT_SUCCESS(rc))
467 {
468 RTCritSectLeave(&pClient->CritSect);
469
470 PSHCLEVENTPAYLOAD pPayload;
471 rc = ShClEventWait(pEvent, 30 * 1000, &pPayload);
472 if (RT_SUCCESS(rc))
473 {
474 if ( !pPayload
475 || !pPayload->cbData)
476 {
477 rc = VERR_NO_DATA;
478 }
479 else
480 {
481 *ppv = pPayload->pvData;
482 *pcb = pPayload->cbData;
483 }
484 }
485
486 RTCritSectEnter(&pClient->CritSect);
487
488 ShClEventRelease(pEvent);
489 }
490 }
491
492 RTCritSectLeave(&pClient->CritSect);
493
494 if (RT_FAILURE(rc))
495 LogRel(("Shared Clipboard: Requesting data in format %#x for X11 host failed with %Rrc\n", uFmt, rc));
496
497 LogFlowFuncLeaveRC(rc);
498 return rc;
499}
500
501#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
502
503int ShClBackendTransferCreate(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLTRANSFER pTransfer)
504{
505 RT_NOREF(pBackend);
506#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP
507 return ShClHttpTransferRegister(&pClient->State.pCtx->X11.HttpCtx, pTransfer);
508#else
509 RT_NOREF(pClient, pTransfer);
510#endif
511 return VERR_NOT_IMPLEMENTED;
512}
513
514int ShClBackendTransferDestroy(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLTRANSFER pTransfer)
515{
516 RT_NOREF(pBackend);
517#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP
518 return ShClHttpTransferUnregister(&pClient->State.pCtx->X11.HttpCtx, pTransfer);
519#else
520 RT_NOREF(pClient, pTransfer);
521#endif
522
523 return VINF_SUCCESS;
524}
525
526int ShClBackendTransferGetRoots(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLTRANSFER pTransfer)
527{
528 RT_NOREF(pBackend);
529
530 LogFlowFuncEnter();
531
532 PSHCLEVENT pEvent;
533 int rc = ShClEventSourceGenerateAndRegisterEvent(&pClient->EventSrc, &pEvent);
534 if (RT_SUCCESS(rc))
535 {
536 CLIPREADCBREQ *pReq = (CLIPREADCBREQ *)RTMemAllocZ(sizeof(CLIPREADCBREQ));
537 if (pReq)
538 {
539 pReq->idEvent = pEvent->idEvent;
540
541 rc = ShClX11ReadDataFromX11(&pClient->State.pCtx->X11, VBOX_SHCL_FMT_URI_LIST, pReq);
542 if (RT_SUCCESS(rc))
543 {
544 /* X supplies the data asynchronously, so we need to wait for data to arrive first. */
545 PSHCLEVENTPAYLOAD pPayload;
546 rc = ShClEventWait(pEvent, 30 * 1000, &pPayload);
547 if (RT_SUCCESS(rc))
548 {
549 rc = ShClTransferRootsSet(pTransfer,
550 (char *)pPayload->pvData, pPayload->cbData + 1 /* Include termination */);
551 }
552 }
553 }
554 else
555 rc = VERR_NO_MEMORY;
556
557 ShClEventRelease(pEvent);
558 }
559 else
560 rc = VERR_SHCLPB_MAX_EVENTS_REACHED;
561
562 LogFlowFuncLeaveRC(rc);
563 return rc;
564}
565#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
566
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