VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedClipboard/service.cpp@ 69223

Last change on this file since 69223 was 63495, checked in by vboxsync, 8 years ago

warnings (clang)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 33.5 KB
Line 
1/* $Id: service.cpp 63495 2016-08-15 17:19:46Z vboxsync $ */
2/** @file
3 * Shared Clipboard: Host service entry points.
4 */
5
6/*
7 * Copyright (C) 2006-2016 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/** @page pg_hostclip The Shared Clipboard Host Service
20 *
21 * The shared clipboard host service provides a proxy between the host's
22 * clipboard and a similar proxy running on a guest. The service is split
23 * into a platform-independent core and platform-specific backends. The
24 * service defines two communication protocols - one to communicate with the
25 * clipboard service running on the guest, and one to communicate with the
26 * backend. These will be described in a very skeletal fashion here.
27 *
28 * @section sec_hostclip_guest_proto The guest communication protocol
29 *
30 * The guest clipboard service communicates with the host service via HGCM
31 * (the host service runs as an HGCM service). The guest clipboard must
32 * connect to the host service before all else (Windows hosts currently only
33 * support one simultaneous connection). Once it has connected, it can send
34 * HGCM messages to the host services, some of which will receive replies from
35 * the host. The host can only reply to a guest message, it cannot initiate
36 * any communication. The guest can in theory send any number of messages in
37 * parallel (see the descriptions of the messages for the practice), and the
38 * host will receive these in sequence, and may reply to them at once
39 * (releasing the caller in the guest) or defer the reply until later.
40 *
41 * There are currently four messages defined. The first is
42 * VBOX_SHARED_CLIPBOARD_FN_GET_HOST_MSG, which waits for a message from the
43 * host. Host messages currently defined are
44 * VBOX_SHARED_CLIPBOARD_HOST_MSG_QUIT (unused),
45 * VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA (request that the guest send the
46 * contents of its clipboard to the host) and
47 * VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS (to notify the guest that new
48 * clipboard data is available). If a host message is sent while the guest is
49 * not waiting, it will be queued until the guest requests it. At most one
50 * host message of each type will be kept in the queue. The host code only
51 * supports a single simultaneous VBOX_SHARED_CLIPBOARD_FN_GET_HOST_MSG call
52 * from the guest.
53 *
54 * The second guest message is VBOX_SHARED_CLIPBOARD_FN_FORMATS, which tells
55 * the host that the guest has new clipboard data available. The third is
56 * VBOX_SHARED_CLIPBOARD_FN_READ_DATA, which asks the host to send its
57 * clipboard data and waits until it arrives. The host supports at most one
58 * simultaneous VBOX_SHARED_CLIPBOARD_FN_READ_DATA call from the guest - if a
59 * second call is made before the first has returned, the first will be
60 * aborted.
61 *
62 * The last guest message is VBOX_SHARED_CLIPBOARD_FN_WRITE_DATA, which is
63 * used to send the contents of the guest clipboard to the host. This call
64 * should be used after the host has requested data from the guest.
65 *
66 * @section sec_hostclip_backend_proto The communication protocol with the
67 * platform-specific backend
68 *
69 * This section may be written in the future :)
70 */
71
72#include <VBox/HostServices/VBoxClipboardSvc.h>
73#include <VBox/HostServices/VBoxClipboardExt.h>
74
75#include <iprt/alloc.h>
76#include <iprt/string.h>
77#include <iprt/assert.h>
78#include <iprt/critsect.h>
79#include <VBox/vmm/ssm.h>
80
81#include "VBoxClipboard.h"
82
83static void VBoxHGCMParmUInt32Set (VBOXHGCMSVCPARM *pParm, uint32_t u32)
84{
85 pParm->type = VBOX_HGCM_SVC_PARM_32BIT;
86 pParm->u.uint32 = u32;
87}
88
89static int VBoxHGCMParmUInt32Get (VBOXHGCMSVCPARM *pParm, uint32_t *pu32)
90{
91 if (pParm->type == VBOX_HGCM_SVC_PARM_32BIT)
92 {
93 *pu32 = pParm->u.uint32;
94 return VINF_SUCCESS;
95 }
96
97 return VERR_INVALID_PARAMETER;
98}
99
100#if 0
101static void VBoxHGCMParmPtrSet (VBOXHGCMSVCPARM *pParm, void *pv, uint32_t cb)
102{
103 pParm->type = VBOX_HGCM_SVC_PARM_PTR;
104 pParm->u.pointer.size = cb;
105 pParm->u.pointer.addr = pv;
106}
107#endif
108
109static int VBoxHGCMParmPtrGet (VBOXHGCMSVCPARM *pParm, void **ppv, uint32_t *pcb)
110{
111 if (pParm->type == VBOX_HGCM_SVC_PARM_PTR)
112 {
113 *ppv = pParm->u.pointer.addr;
114 *pcb = pParm->u.pointer.size;
115 return VINF_SUCCESS;
116 }
117
118 return VERR_INVALID_PARAMETER;
119}
120
121static PVBOXHGCMSVCHELPERS g_pHelpers;
122
123static RTCRITSECT critsect;
124static uint32_t g_u32Mode;
125
126static PFNHGCMSVCEXT g_pfnExtension;
127static void *g_pvExtension;
128
129static VBOXCLIPBOARDCLIENTDATA *g_pClient;
130
131/* Serialization of data reading and format announcements from the RDP client. */
132static bool g_fReadingData = false;
133static bool g_fDelayedAnnouncement = false;
134static uint32_t g_u32DelayedFormats = 0;
135
136/** Is the clipboard running in headless mode? */
137static bool g_fHeadless = false;
138
139static uint32_t vboxSvcClipboardMode (void)
140{
141 return g_u32Mode;
142}
143
144#ifdef UNIT_TEST
145/** Testing interface, getter for clipboard mode */
146uint32_t TestClipSvcGetMode(void)
147{
148 return vboxSvcClipboardMode();
149}
150#endif
151
152/** Getter for headless setting */
153bool vboxSvcClipboardGetHeadless(void)
154{
155 return g_fHeadless;
156}
157
158static void vboxSvcClipboardModeSet (uint32_t u32Mode)
159{
160 switch (u32Mode)
161 {
162 case VBOX_SHARED_CLIPBOARD_MODE_OFF:
163 case VBOX_SHARED_CLIPBOARD_MODE_HOST_TO_GUEST:
164 case VBOX_SHARED_CLIPBOARD_MODE_GUEST_TO_HOST:
165 case VBOX_SHARED_CLIPBOARD_MODE_BIDIRECTIONAL:
166 g_u32Mode = u32Mode;
167 break;
168
169 default:
170 g_u32Mode = VBOX_SHARED_CLIPBOARD_MODE_OFF;
171 }
172}
173
174bool vboxSvcClipboardLock (void)
175{
176 return RT_SUCCESS(RTCritSectEnter (&critsect));
177}
178
179void vboxSvcClipboardUnlock (void)
180{
181 RTCritSectLeave (&critsect);
182}
183
184/* Set the HGCM parameters according to pending messages.
185 * Executed under the clipboard lock.
186 */
187static bool vboxSvcClipboardReturnMsg (VBOXCLIPBOARDCLIENTDATA *pClient, VBOXHGCMSVCPARM paParms[])
188{
189 /* Message priority is taken into account. */
190 if (pClient->fMsgQuit)
191 {
192 LogRelFlow(("vboxSvcClipboardReturnMsg: Quit\n"));
193 VBoxHGCMParmUInt32Set (&paParms[0], VBOX_SHARED_CLIPBOARD_HOST_MSG_QUIT);
194 VBoxHGCMParmUInt32Set (&paParms[1], 0);
195 pClient->fMsgQuit = false;
196 }
197 else if (pClient->fMsgReadData)
198 {
199 LogRelFlow(("vboxSvcClipboardReturnMsg: ReadData %02X\n", pClient->u32RequestedFormat));
200 VBoxHGCMParmUInt32Set (&paParms[0], VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA);
201 VBoxHGCMParmUInt32Set (&paParms[1], pClient->u32RequestedFormat);
202 pClient->fMsgReadData = false;
203 }
204 else if (pClient->fMsgFormats)
205 {
206 LogRelFlow(("vboxSvcClipboardReturnMsg: Formats %02X\n", pClient->u32AvailableFormats));
207 VBoxHGCMParmUInt32Set (&paParms[0], VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS);
208 VBoxHGCMParmUInt32Set (&paParms[1], pClient->u32AvailableFormats);
209 pClient->fMsgFormats = false;
210 }
211 else
212 {
213 /* No pending messages. */
214 LogRelFlow(("vboxSvcClipboardReturnMsg: no message\n"));
215 return false;
216 }
217
218 /* Message information assigned. */
219 return true;
220}
221
222void vboxSvcClipboardReportMsg (VBOXCLIPBOARDCLIENTDATA *pClient, uint32_t u32Msg, uint32_t u32Formats)
223{
224 if (vboxSvcClipboardLock ())
225 {
226 switch (u32Msg)
227 {
228 case VBOX_SHARED_CLIPBOARD_HOST_MSG_QUIT:
229 {
230 LogRelFlow(("vboxSvcClipboardReportMsg: Quit\n"));
231 pClient->fMsgQuit = true;
232 } break;
233 case VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA:
234 {
235 if ( vboxSvcClipboardMode () != VBOX_SHARED_CLIPBOARD_MODE_GUEST_TO_HOST
236 && vboxSvcClipboardMode () != VBOX_SHARED_CLIPBOARD_MODE_BIDIRECTIONAL)
237 {
238 /* Skip the message. */
239 break;
240 }
241
242 LogRelFlow(("vboxSvcClipboardReportMsg: ReadData %02X\n", u32Formats));
243 pClient->u32RequestedFormat = u32Formats;
244 pClient->fMsgReadData = true;
245 } break;
246 case VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS:
247 {
248 if ( vboxSvcClipboardMode () != VBOX_SHARED_CLIPBOARD_MODE_HOST_TO_GUEST
249 && vboxSvcClipboardMode () != VBOX_SHARED_CLIPBOARD_MODE_BIDIRECTIONAL)
250 {
251 /* Skip the message. */
252 break;
253 }
254
255 LogRelFlow(("vboxSvcClipboardReportMsg: Formats %02X\n", u32Formats));
256 pClient->u32AvailableFormats = u32Formats;
257 pClient->fMsgFormats = true;
258 } break;
259 default:
260 {
261 /* Invalid message. */
262 LogRelFlow(("vboxSvcClipboardReportMsg: invalid message %d\n", u32Msg));
263 } break;
264 }
265
266 if (pClient->fAsync)
267 {
268 /* The client waits for a response. */
269 bool fMessageReturned = vboxSvcClipboardReturnMsg (pClient, pClient->async.paParms);
270
271 /* Make a copy of the handle. */
272 VBOXHGCMCALLHANDLE callHandle = pClient->async.callHandle;
273
274 if (fMessageReturned)
275 {
276 /* There is a response. */
277 pClient->fAsync = false;
278 }
279
280 vboxSvcClipboardUnlock ();
281
282 if (fMessageReturned)
283 {
284 LogRelFlow(("vboxSvcClipboardReportMsg: CallComplete\n"));
285 g_pHelpers->pfnCallComplete (callHandle, VINF_SUCCESS);
286 }
287 }
288 else
289 {
290 vboxSvcClipboardUnlock ();
291 }
292 }
293}
294
295static int svcInit (void)
296{
297 int rc = RTCritSectInit (&critsect);
298
299 if (RT_SUCCESS (rc))
300 {
301 vboxSvcClipboardModeSet (VBOX_SHARED_CLIPBOARD_MODE_OFF);
302
303 rc = vboxClipboardInit ();
304
305 /* Clean up on failure, because 'svnUnload' will not be called
306 * if the 'svcInit' returns an error.
307 */
308 if (RT_FAILURE (rc))
309 {
310 RTCritSectDelete (&critsect);
311 }
312 }
313
314 return rc;
315}
316
317static DECLCALLBACK(int) svcUnload (void *)
318{
319 vboxClipboardDestroy ();
320 RTCritSectDelete (&critsect);
321 return VINF_SUCCESS;
322}
323
324/**
325 * Disconnect the host side of the shared clipboard and send a "host disconnected" message
326 * to the guest side.
327 */
328static DECLCALLBACK(int) svcDisconnect (void *, uint32_t u32ClientID, void *pvClient)
329{
330 VBOXCLIPBOARDCLIENTDATA *pClient = (VBOXCLIPBOARDCLIENTDATA *)pvClient;
331
332 LogRel2(("svcDisconnect: u32ClientID = %d\n", u32ClientID));
333
334 vboxSvcClipboardReportMsg (pClient, VBOX_SHARED_CLIPBOARD_HOST_MSG_QUIT, 0);
335
336 vboxSvcClipboardCompleteReadData(pClient, VERR_NO_DATA, 0);
337
338 vboxClipboardDisconnect (pClient);
339
340 memset (pClient, 0, sizeof (*pClient));
341
342 g_pClient = NULL;
343
344 return VINF_SUCCESS;
345}
346
347static DECLCALLBACK(int) svcConnect (void *, uint32_t u32ClientID, void *pvClient)
348{
349 VBOXCLIPBOARDCLIENTDATA *pClient = (VBOXCLIPBOARDCLIENTDATA *)pvClient;
350
351 int rc = VINF_SUCCESS;
352
353 /* If there is already a client connected then we want to release it first. */
354 if (g_pClient != NULL)
355 {
356 uint32_t u32OldClientID = g_pClient->u32ClientID;
357
358 svcDisconnect(NULL, u32OldClientID, g_pClient);
359 /* And free the resources in the hgcm subsystem. */
360 g_pHelpers->pfnDisconnectClient(g_pHelpers->pvInstance, u32OldClientID);
361 }
362
363 /* Register the client. */
364 memset (pClient, 0, sizeof (*pClient));
365
366 pClient->u32ClientID = u32ClientID;
367
368 rc = vboxClipboardConnect (pClient, vboxSvcClipboardGetHeadless());
369
370 if (RT_SUCCESS (rc))
371 {
372 g_pClient = pClient;
373 }
374
375 LogRel2(("vboxClipboardConnect: rc = %Rrc\n", rc));
376
377 return rc;
378}
379
380static DECLCALLBACK(void) svcCall (void *,
381 VBOXHGCMCALLHANDLE callHandle,
382 uint32_t u32ClientID,
383 void *pvClient,
384 uint32_t u32Function,
385 uint32_t cParms,
386 VBOXHGCMSVCPARM paParms[])
387{
388 int rc = VINF_SUCCESS;
389
390 LogRel2(("svcCall: u32ClientID = %d, fn = %d, cParms = %d, pparms = %d\n",
391 u32ClientID, u32Function, cParms, paParms));
392
393 VBOXCLIPBOARDCLIENTDATA *pClient = (VBOXCLIPBOARDCLIENTDATA *)pvClient;
394
395 bool fAsynchronousProcessing = false;
396
397#ifdef DEBUG
398 uint32_t i;
399
400 for (i = 0; i < cParms; i++)
401 {
402 /** @todo parameters other than 32 bit */
403 LogRel2((" pparms[%d]: type %d value %d\n", i, paParms[i].type, paParms[i].u.uint32));
404 }
405#endif
406
407 switch (u32Function)
408 {
409 case VBOX_SHARED_CLIPBOARD_FN_GET_HOST_MSG:
410 {
411 /* The quest requests a host message. */
412 LogRel2(("svcCall: VBOX_SHARED_CLIPBOARD_FN_GET_HOST_MSG\n"));
413
414 if (cParms != VBOX_SHARED_CLIPBOARD_CPARMS_GET_HOST_MSG)
415 {
416 rc = VERR_INVALID_PARAMETER;
417 }
418 else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* msg */
419 || paParms[1].type != VBOX_HGCM_SVC_PARM_32BIT /* formats */
420 )
421 {
422 rc = VERR_INVALID_PARAMETER;
423 }
424 else
425 {
426 /* Atomically verify the client's state. */
427 if (vboxSvcClipboardLock ())
428 {
429 bool fMessageReturned = vboxSvcClipboardReturnMsg (pClient, paParms);
430
431 if (fMessageReturned)
432 {
433 /* Just return to the caller. */
434 pClient->fAsync = false;
435 }
436 else
437 {
438 /* No event available at the time. Process asynchronously. */
439 fAsynchronousProcessing = true;
440
441 pClient->fAsync = true;
442 pClient->async.callHandle = callHandle;
443 pClient->async.paParms = paParms;
444
445 LogRel2(("svcCall: async.\n"));
446 }
447
448 vboxSvcClipboardUnlock ();
449 }
450 else
451 {
452 rc = VERR_NOT_SUPPORTED;
453 }
454 }
455 } break;
456
457 case VBOX_SHARED_CLIPBOARD_FN_FORMATS:
458 {
459 /* The guest reports that some formats are available. */
460 LogRel2(("svcCall: VBOX_SHARED_CLIPBOARD_FN_FORMATS\n"));
461
462 if (cParms != VBOX_SHARED_CLIPBOARD_CPARMS_FORMATS)
463 {
464 rc = VERR_INVALID_PARAMETER;
465 }
466 else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* formats */
467 )
468 {
469 rc = VERR_INVALID_PARAMETER;
470 }
471 else
472 {
473 uint32_t u32Formats;
474
475 rc = VBoxHGCMParmUInt32Get (&paParms[0], &u32Formats);
476
477 if (RT_SUCCESS (rc))
478 {
479 if ( vboxSvcClipboardMode () != VBOX_SHARED_CLIPBOARD_MODE_GUEST_TO_HOST
480 && vboxSvcClipboardMode () != VBOX_SHARED_CLIPBOARD_MODE_BIDIRECTIONAL)
481 {
482 rc = VERR_NOT_SUPPORTED;
483 break;
484 }
485
486 if (g_pfnExtension)
487 {
488 VBOXCLIPBOARDEXTPARMS parms;
489
490 parms.u32Format = u32Formats;
491
492 g_pfnExtension (g_pvExtension, VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE, &parms, sizeof (parms));
493 }
494 else
495 {
496 vboxClipboardFormatAnnounce (pClient, u32Formats);
497 }
498 }
499 }
500 } break;
501
502 case VBOX_SHARED_CLIPBOARD_FN_READ_DATA:
503 {
504 /* The guest wants to read data in the given format. */
505 LogRel2(("svcCall: VBOX_SHARED_CLIPBOARD_FN_READ_DATA\n"));
506
507 if (cParms != VBOX_SHARED_CLIPBOARD_CPARMS_READ_DATA)
508 {
509 rc = VERR_INVALID_PARAMETER;
510 }
511 else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* format */
512 || paParms[1].type != VBOX_HGCM_SVC_PARM_PTR /* ptr */
513 || paParms[2].type != VBOX_HGCM_SVC_PARM_32BIT /* size */
514 )
515 {
516 rc = VERR_INVALID_PARAMETER;
517 }
518 else
519 {
520 uint32_t u32Format;
521 void *pv;
522 uint32_t cb;
523
524 rc = VBoxHGCMParmUInt32Get (&paParms[0], &u32Format);
525
526 if (RT_SUCCESS (rc))
527 {
528 rc = VBoxHGCMParmPtrGet (&paParms[1], &pv, &cb);
529
530 if (RT_SUCCESS (rc))
531 {
532 if ( vboxSvcClipboardMode () != VBOX_SHARED_CLIPBOARD_MODE_HOST_TO_GUEST
533 && vboxSvcClipboardMode () != VBOX_SHARED_CLIPBOARD_MODE_BIDIRECTIONAL)
534 {
535 rc = VERR_NOT_SUPPORTED;
536 break;
537 }
538
539 uint32_t cbActual = 0;
540
541 if (g_pfnExtension)
542 {
543 VBOXCLIPBOARDEXTPARMS parms;
544
545 parms.u32Format = u32Format;
546 parms.u.pvData = pv;
547 parms.cbData = cb;
548
549 g_fReadingData = true;
550 rc = g_pfnExtension (g_pvExtension, VBOX_CLIPBOARD_EXT_FN_DATA_READ, &parms, sizeof (parms));
551 LogRelFlow(("DATA: g_fDelayedAnnouncement = %d, g_u32DelayedFormats = 0x%x\n", g_fDelayedAnnouncement, g_u32DelayedFormats));
552 if (g_fDelayedAnnouncement)
553 {
554 vboxSvcClipboardReportMsg (g_pClient, VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS, g_u32DelayedFormats);
555 g_fDelayedAnnouncement = false;
556 g_u32DelayedFormats = 0;
557 }
558 g_fReadingData = false;
559
560 if (RT_SUCCESS (rc))
561 {
562 cbActual = parms.cbData;
563 }
564 }
565 else
566 {
567 /* Release any other pending read, as we only
568 * support one pending read at one time. */
569 vboxSvcClipboardCompleteReadData(pClient, VERR_NO_DATA, 0);
570 rc = vboxClipboardReadData (pClient, u32Format, pv, cb, &cbActual);
571 }
572
573 /* Remember our read request until it is completed.
574 * See the protocol description above for more
575 * information. */
576 if (rc == VINF_HGCM_ASYNC_EXECUTE)
577 {
578 if (vboxSvcClipboardLock())
579 {
580 pClient->asyncRead.callHandle = callHandle;
581 pClient->asyncRead.paParms = paParms;
582 pClient->fReadPending = true;
583 fAsynchronousProcessing = true;
584 vboxSvcClipboardUnlock();
585 }
586 else
587 rc = VERR_NOT_SUPPORTED;
588 }
589 else if (RT_SUCCESS (rc))
590 {
591 VBoxHGCMParmUInt32Set (&paParms[2], cbActual);
592 }
593 }
594 }
595 }
596 } break;
597
598 case VBOX_SHARED_CLIPBOARD_FN_WRITE_DATA:
599 {
600 /* The guest writes the requested data. */
601 LogRel2(("svcCall: VBOX_SHARED_CLIPBOARD_FN_WRITE_DATA\n"));
602
603 if (cParms != VBOX_SHARED_CLIPBOARD_CPARMS_WRITE_DATA)
604 {
605 rc = VERR_INVALID_PARAMETER;
606 }
607 else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* format */
608 || paParms[1].type != VBOX_HGCM_SVC_PARM_PTR /* ptr */
609 )
610 {
611 rc = VERR_INVALID_PARAMETER;
612 }
613 else
614 {
615 void *pv;
616 uint32_t cb;
617 uint32_t u32Format;
618
619 rc = VBoxHGCMParmUInt32Get (&paParms[0], &u32Format);
620
621 if (RT_SUCCESS (rc))
622 {
623 rc = VBoxHGCMParmPtrGet (&paParms[1], &pv, &cb);
624
625 if (RT_SUCCESS (rc))
626 {
627 if ( vboxSvcClipboardMode () != VBOX_SHARED_CLIPBOARD_MODE_GUEST_TO_HOST
628 && vboxSvcClipboardMode () != VBOX_SHARED_CLIPBOARD_MODE_BIDIRECTIONAL)
629 {
630 rc = VERR_NOT_SUPPORTED;
631 break;
632 }
633
634 if (g_pfnExtension)
635 {
636 VBOXCLIPBOARDEXTPARMS parms;
637
638 parms.u32Format = u32Format;
639 parms.u.pvData = pv;
640 parms.cbData = cb;
641
642 g_pfnExtension (g_pvExtension, VBOX_CLIPBOARD_EXT_FN_DATA_WRITE, &parms, sizeof (parms));
643 }
644 else
645 {
646 vboxClipboardWriteData (pClient, pv, cb, u32Format);
647 }
648 }
649 }
650 }
651 } break;
652
653 default:
654 {
655 rc = VERR_NOT_IMPLEMENTED;
656 }
657 }
658
659 LogRelFlow(("svcCall: rc = %Rrc\n", rc));
660
661 if (!fAsynchronousProcessing)
662 {
663 g_pHelpers->pfnCallComplete (callHandle, rc);
664 }
665}
666
667/** If the client in the guest is waiting for a read operation to complete
668 * then complete it, otherwise return. See the protocol description in the
669 * shared clipboard module description. */
670void vboxSvcClipboardCompleteReadData(VBOXCLIPBOARDCLIENTDATA *pClient, int rc, uint32_t cbActual)
671{
672 VBOXHGCMCALLHANDLE callHandle = NULL;
673 VBOXHGCMSVCPARM *paParms = NULL;
674 bool fReadPending = false;
675 if (vboxSvcClipboardLock()) /* if not can we do anything useful? */
676 {
677 callHandle = pClient->asyncRead.callHandle;
678 paParms = pClient->asyncRead.paParms;
679 fReadPending = pClient->fReadPending;
680 pClient->fReadPending = false;
681 vboxSvcClipboardUnlock();
682 }
683 if (fReadPending)
684 {
685 VBoxHGCMParmUInt32Set (&paParms[2], cbActual);
686 g_pHelpers->pfnCallComplete (callHandle, rc);
687 }
688}
689
690/*
691 * We differentiate between a function handler for the guest and one for the host.
692 */
693static DECLCALLBACK(int) svcHostCall (void *,
694 uint32_t u32Function,
695 uint32_t cParms,
696 VBOXHGCMSVCPARM paParms[])
697{
698 int rc = VINF_SUCCESS;
699
700 LogRel2(("svcHostCall: fn = %d, cParms = %d, pparms = %d\n",
701 u32Function, cParms, paParms));
702
703 switch (u32Function)
704 {
705 case VBOX_SHARED_CLIPBOARD_HOST_FN_SET_MODE:
706 {
707 LogRel2(("svcCall: VBOX_SHARED_CLIPBOARD_HOST_FN_SET_MODE\n"));
708
709 if (cParms != 1)
710 {
711 rc = VERR_INVALID_PARAMETER;
712 }
713 else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* mode */
714 )
715 {
716 rc = VERR_INVALID_PARAMETER;
717 }
718 else
719 {
720 uint32_t u32Mode = VBOX_SHARED_CLIPBOARD_MODE_OFF;
721
722 rc = VBoxHGCMParmUInt32Get (&paParms[0], &u32Mode);
723
724 /* The setter takes care of invalid values. */
725 vboxSvcClipboardModeSet (u32Mode);
726 }
727 } break;
728
729 case VBOX_SHARED_CLIPBOARD_HOST_FN_SET_HEADLESS:
730 {
731 uint32_t u32Headless = g_fHeadless;
732
733 rc = VERR_INVALID_PARAMETER;
734 if (cParms != 1)
735 break;
736 rc = VBoxHGCMParmUInt32Get (&paParms[0], &u32Headless);
737 if (RT_SUCCESS(rc))
738 LogRelFlow(("svcCall: VBOX_SHARED_CLIPBOARD_HOST_FN_SET_HEADLESS, u32Headless=%u\n",
739 (unsigned) u32Headless));
740 g_fHeadless = RT_BOOL(u32Headless);
741 } break;
742
743 default:
744 break;
745 }
746
747 LogRelFlow(("svcHostCall: rc = %Rrc\n", rc));
748 return rc;
749}
750
751#ifndef UNIT_TEST
752/**
753 * SSM descriptor table for the VBOXCLIPBOARDCLIENTDATA structure.
754 */
755static SSMFIELD const g_aClipboardClientDataFields[] =
756{
757 SSMFIELD_ENTRY(VBOXCLIPBOARDCLIENTDATA, u32ClientID), /* for validation purposes */
758 SSMFIELD_ENTRY(VBOXCLIPBOARDCLIENTDATA, fMsgQuit),
759 SSMFIELD_ENTRY(VBOXCLIPBOARDCLIENTDATA, fMsgReadData),
760 SSMFIELD_ENTRY(VBOXCLIPBOARDCLIENTDATA, fMsgFormats),
761 SSMFIELD_ENTRY(VBOXCLIPBOARDCLIENTDATA, u32RequestedFormat),
762 SSMFIELD_ENTRY_TERM()
763};
764#endif
765
766static DECLCALLBACK(int) svcSaveState(void *, uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM)
767{
768#ifndef UNIT_TEST
769 /*
770 * When the state will be restored, pending requests will be reissued
771 * by VMMDev. The service therefore must save state as if there were no
772 * pending request.
773 * Pending requests, if any, will be completed in svcDisconnect.
774 */
775 LogRel2 (("svcSaveState: u32ClientID = %d\n", u32ClientID));
776
777 VBOXCLIPBOARDCLIENTDATA *pClient = (VBOXCLIPBOARDCLIENTDATA *)pvClient;
778
779 /* This field used to be the length. We're using it as a version field
780 with the high bit set. */
781 SSMR3PutU32 (pSSM, UINT32_C (0x80000002));
782 int rc = SSMR3PutStructEx (pSSM, pClient, sizeof(*pClient), 0 /*fFlags*/, &g_aClipboardClientDataFields[0], NULL);
783 AssertRCReturn (rc, rc);
784
785#else /* UNIT_TEST */
786 RT_NOREF3(u32ClientID, pvClient, pSSM);
787#endif /* UNIT_TEST */
788 return VINF_SUCCESS;
789}
790
791/**
792 * This structure corresponds to the original layout of the
793 * VBOXCLIPBOARDCLIENTDATA structure. As the structure was saved as a whole
794 * when saving state, we need to remember it forever in order to preserve
795 * compatibility.
796 *
797 * (Starting with 3.1 this is no longer used.)
798 *
799 * @remarks Putting this outside svcLoadState to avoid visibility warning caused
800 * by -Wattributes.
801 */
802typedef struct CLIPSAVEDSTATEDATA
803{
804 struct CLIPSAVEDSTATEDATA *pNext;
805 struct CLIPSAVEDSTATEDATA *pPrev;
806
807 VBOXCLIPBOARDCONTEXT *pCtx;
808
809 uint32_t u32ClientID;
810
811 bool fAsync: 1; /* Guest is waiting for a message. */
812
813 bool fMsgQuit: 1;
814 bool fMsgReadData: 1;
815 bool fMsgFormats: 1;
816
817 struct {
818 VBOXHGCMCALLHANDLE callHandle;
819 VBOXHGCMSVCPARM *paParms;
820 } async;
821
822 struct {
823 void *pv;
824 uint32_t cb;
825 uint32_t u32Format;
826 } data;
827
828 uint32_t u32AvailableFormats;
829 uint32_t u32RequestedFormat;
830
831} CLIPSAVEDSTATEDATA;
832
833static DECLCALLBACK(int) svcLoadState(void *, uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM)
834{
835#ifndef UNIT_TEST
836 LogRel2 (("svcLoadState: u32ClientID = %d\n", u32ClientID));
837
838 VBOXCLIPBOARDCLIENTDATA *pClient = (VBOXCLIPBOARDCLIENTDATA *)pvClient;
839
840 /* Existing client can not be in async state yet. */
841 Assert (!pClient->fAsync);
842
843 /* Save the client ID for data validation. */
844 /** @todo isn't this the same as u32ClientID? Playing safe for now... */
845 uint32_t const u32ClientIDOld = pClient->u32ClientID;
846
847 /* Restore the client data. */
848 uint32_t lenOrVer;
849 int rc = SSMR3GetU32 (pSSM, &lenOrVer);
850 AssertRCReturn (rc, rc);
851 if (lenOrVer == UINT32_C (0x80000002))
852 {
853 rc = SSMR3GetStructEx (pSSM, pClient, sizeof(*pClient), 0 /*fFlags*/, &g_aClipboardClientDataFields[0], NULL);
854 AssertRCReturn (rc, rc);
855 }
856 else if (lenOrVer == (SSMR3HandleHostBits (pSSM) == 64 ? 72U : 48U))
857 {
858 /**
859 * SSM descriptor table for the CLIPSAVEDSTATEDATA structure.
860 */
861 static SSMFIELD const s_aClipSavedStateDataFields30[] =
862 {
863 SSMFIELD_ENTRY_IGN_HCPTR( CLIPSAVEDSTATEDATA, pNext),
864 SSMFIELD_ENTRY_IGN_HCPTR( CLIPSAVEDSTATEDATA, pPrev),
865 SSMFIELD_ENTRY_IGN_HCPTR( CLIPSAVEDSTATEDATA, pCtx),
866 SSMFIELD_ENTRY( CLIPSAVEDSTATEDATA, u32ClientID),
867 SSMFIELD_ENTRY_CUSTOM(fMsgQuit+fMsgReadData+fMsgFormats, RT_OFFSETOF(CLIPSAVEDSTATEDATA, u32ClientID) + 4, 4),
868 SSMFIELD_ENTRY_IGN_HCPTR( CLIPSAVEDSTATEDATA, async.callHandle),
869 SSMFIELD_ENTRY_IGN_HCPTR( CLIPSAVEDSTATEDATA, async.paParms),
870 SSMFIELD_ENTRY_IGNORE( CLIPSAVEDSTATEDATA, data.pv),
871 SSMFIELD_ENTRY_IGNORE( CLIPSAVEDSTATEDATA, data.cb),
872 SSMFIELD_ENTRY_IGNORE( CLIPSAVEDSTATEDATA, data.u32Format),
873 SSMFIELD_ENTRY_IGNORE( CLIPSAVEDSTATEDATA, u32AvailableFormats),
874 SSMFIELD_ENTRY( CLIPSAVEDSTATEDATA, u32RequestedFormat),
875 SSMFIELD_ENTRY_TERM()
876 };
877
878 CLIPSAVEDSTATEDATA savedState;
879 RT_ZERO (savedState);
880 rc = SSMR3GetStructEx (pSSM, &savedState, sizeof(savedState), SSMSTRUCT_FLAGS_MEM_BAND_AID,
881 &s_aClipSavedStateDataFields30[0], NULL);
882 AssertRCReturn (rc, rc);
883
884 pClient->fMsgQuit = savedState.fMsgQuit;
885 pClient->fMsgReadData = savedState.fMsgReadData;
886 pClient->fMsgFormats = savedState.fMsgFormats;
887 pClient->u32RequestedFormat = savedState.u32RequestedFormat;
888 }
889 else
890 {
891 LogRel (("Client data size mismatch: got %#x\n", lenOrVer));
892 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
893 }
894
895 /* Verify the client ID. */
896 if (pClient->u32ClientID != u32ClientIDOld)
897 {
898 LogRel (("Client ID mismatch: expected %d, got %d\n", u32ClientIDOld, pClient->u32ClientID));
899 pClient->u32ClientID = u32ClientIDOld;
900 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
901 }
902
903 /* Actual host data are to be reported to guest (SYNC). */
904 vboxClipboardSync (pClient);
905
906#else /* UNIT_TEST*/
907 RT_NOREF3(u32ClientID, pvClient, pSSM);
908#endif /* UNIT_TEST */
909 return VINF_SUCCESS;
910}
911
912static DECLCALLBACK(int) extCallback (uint32_t u32Function, uint32_t u32Format, void *pvData, uint32_t cbData)
913{
914 RT_NOREF2(pvData, cbData);
915 if (g_pClient != NULL)
916 {
917 switch (u32Function)
918 {
919 case VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE:
920 {
921 LogRelFlow(("ANNOUNCE: g_fReadingData = %d\n", g_fReadingData));
922 if (g_fReadingData)
923 {
924 g_fDelayedAnnouncement = true;
925 g_u32DelayedFormats = u32Format;
926 }
927 else
928 {
929 vboxSvcClipboardReportMsg (g_pClient, VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS, u32Format);
930 }
931 } break;
932
933 case VBOX_CLIPBOARD_EXT_FN_DATA_READ:
934 {
935 vboxSvcClipboardReportMsg (g_pClient, VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA, u32Format);
936 } break;
937
938 default:
939 return VERR_NOT_SUPPORTED;
940 }
941 }
942
943 return VINF_SUCCESS;
944}
945
946static DECLCALLBACK(int) svcRegisterExtension(void *, PFNHGCMSVCEXT pfnExtension, void *pvExtension)
947{
948 LogRelFlowFunc(("pfnExtension = %p\n", pfnExtension));
949
950 VBOXCLIPBOARDEXTPARMS parms;
951
952 if (pfnExtension)
953 {
954 /* Install extension. */
955 g_pfnExtension = pfnExtension;
956 g_pvExtension = pvExtension;
957
958 parms.u.pfnCallback = extCallback;
959 g_pfnExtension (g_pvExtension, VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK, &parms, sizeof (parms));
960 }
961 else
962 {
963 if (g_pfnExtension)
964 {
965 parms.u.pfnCallback = NULL;
966 g_pfnExtension (g_pvExtension, VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK, &parms, sizeof (parms));
967 }
968
969 /* Uninstall extension. */
970 g_pfnExtension = NULL;
971 g_pvExtension = NULL;
972 }
973
974 return VINF_SUCCESS;
975}
976
977extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad (VBOXHGCMSVCFNTABLE *ptable)
978{
979 int rc = VINF_SUCCESS;
980
981 LogRelFlowFunc(("ptable = %p\n", ptable));
982
983 if (!ptable)
984 {
985 rc = VERR_INVALID_PARAMETER;
986 }
987 else
988 {
989 LogRel2(("VBoxHGCMSvcLoad: ptable->cbSize = %d, ptable->u32Version = 0x%08X\n", ptable->cbSize, ptable->u32Version));
990
991 if ( ptable->cbSize != sizeof (VBOXHGCMSVCFNTABLE)
992 || ptable->u32Version != VBOX_HGCM_SVC_VERSION)
993 {
994 rc = VERR_INVALID_PARAMETER;
995 }
996 else
997 {
998 g_pHelpers = ptable->pHelpers;
999
1000 ptable->cbClient = sizeof (VBOXCLIPBOARDCLIENTDATA);
1001
1002 ptable->pfnUnload = svcUnload;
1003 ptable->pfnConnect = svcConnect;
1004 ptable->pfnDisconnect = svcDisconnect;
1005 ptable->pfnCall = svcCall;
1006 ptable->pfnHostCall = svcHostCall;
1007 ptable->pfnSaveState = svcSaveState;
1008 ptable->pfnLoadState = svcLoadState;
1009 ptable->pfnRegisterExtension = svcRegisterExtension;
1010 ptable->pvService = NULL;
1011
1012 /* Service specific initialization. */
1013 rc = svcInit ();
1014 }
1015 }
1016
1017 return rc;
1018}
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