1 | /** @file
|
---|
2 | *
|
---|
3 | * Shared Clipboard:
|
---|
4 | * Host service entry points.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 |
|
---|
19 | #include <VBox/HostServices/VBoxClipboardSvc.h>
|
---|
20 | #include <VBox/HostServices/VBoxClipboardExt.h>
|
---|
21 |
|
---|
22 | #include <iprt/alloc.h>
|
---|
23 | #include <iprt/string.h>
|
---|
24 | #include <iprt/assert.h>
|
---|
25 | #include <iprt/critsect.h>
|
---|
26 | #include <VBox/ssm.h>
|
---|
27 |
|
---|
28 | #include "VBoxClipboard.h"
|
---|
29 |
|
---|
30 | #define VBOX_HGCM_PARM_COUNT(a) ((sizeof (a) - sizeof (((a *)0)->hdr)) / sizeof (HGCMFunctionParameter))
|
---|
31 |
|
---|
32 | static void VBoxHGCMParmUInt32Set (VBOXHGCMSVCPARM *pParm, uint32_t u32)
|
---|
33 | {
|
---|
34 | pParm->type = VBOX_HGCM_SVC_PARM_32BIT;
|
---|
35 | pParm->u.uint32 = u32;
|
---|
36 | }
|
---|
37 |
|
---|
38 | static int VBoxHGCMParmUInt32Get (VBOXHGCMSVCPARM *pParm, uint32_t *pu32)
|
---|
39 | {
|
---|
40 | if (pParm->type == VBOX_HGCM_SVC_PARM_32BIT)
|
---|
41 | {
|
---|
42 | *pu32 = pParm->u.uint32;
|
---|
43 | return VINF_SUCCESS;
|
---|
44 | }
|
---|
45 |
|
---|
46 | return VERR_INVALID_PARAMETER;
|
---|
47 | }
|
---|
48 |
|
---|
49 | #if 0
|
---|
50 | static void VBoxHGCMParmPtrSet (VBOXHGCMSVCPARM *pParm, void *pv, uint32_t cb)
|
---|
51 | {
|
---|
52 | pParm->type = VBOX_HGCM_SVC_PARM_PTR;
|
---|
53 | pParm->u.pointer.size = cb;
|
---|
54 | pParm->u.pointer.addr = pv;
|
---|
55 | }
|
---|
56 | #endif
|
---|
57 |
|
---|
58 | static int VBoxHGCMParmPtrGet (VBOXHGCMSVCPARM *pParm, void **ppv, uint32_t *pcb)
|
---|
59 | {
|
---|
60 | if (pParm->type == VBOX_HGCM_SVC_PARM_PTR)
|
---|
61 | {
|
---|
62 | *ppv = pParm->u.pointer.addr;
|
---|
63 | *pcb = pParm->u.pointer.size;
|
---|
64 | return VINF_SUCCESS;
|
---|
65 | }
|
---|
66 |
|
---|
67 | return VERR_INVALID_PARAMETER;
|
---|
68 | }
|
---|
69 |
|
---|
70 | static PVBOXHGCMSVCHELPERS g_pHelpers;
|
---|
71 |
|
---|
72 | static RTCRITSECT critsect;
|
---|
73 | static uint32_t g_u32Mode;
|
---|
74 |
|
---|
75 | static PFNHGCMSVCEXT g_pfnExtension;
|
---|
76 | static void *g_pvExtension;
|
---|
77 |
|
---|
78 | static VBOXCLIPBOARDCLIENTDATA *g_pClient;
|
---|
79 |
|
---|
80 | static uint32_t vboxSvcClipboardMode (void)
|
---|
81 | {
|
---|
82 | return g_u32Mode;
|
---|
83 | }
|
---|
84 |
|
---|
85 | static void vboxSvcClipboardModeSet (uint32_t u32Mode)
|
---|
86 | {
|
---|
87 | switch (u32Mode)
|
---|
88 | {
|
---|
89 | case VBOX_SHARED_CLIPBOARD_MODE_OFF:
|
---|
90 | case VBOX_SHARED_CLIPBOARD_MODE_HOST_TO_GUEST:
|
---|
91 | case VBOX_SHARED_CLIPBOARD_MODE_GUEST_TO_HOST:
|
---|
92 | case VBOX_SHARED_CLIPBOARD_MODE_BIDIRECTIONAL:
|
---|
93 | g_u32Mode = u32Mode;
|
---|
94 | break;
|
---|
95 |
|
---|
96 | default:
|
---|
97 | g_u32Mode = VBOX_SHARED_CLIPBOARD_MODE_OFF;
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | bool vboxSvcClipboardLock (void)
|
---|
102 | {
|
---|
103 | return VBOX_SUCCESS(RTCritSectEnter (&critsect));
|
---|
104 | }
|
---|
105 |
|
---|
106 | void vboxSvcClipboardUnlock (void)
|
---|
107 | {
|
---|
108 | RTCritSectLeave (&critsect);
|
---|
109 | }
|
---|
110 |
|
---|
111 | /* Set the HGCM parameters according to pending messages.
|
---|
112 | * Executed under the clipboard lock.
|
---|
113 | */
|
---|
114 | static bool vboxSvcClipboardReturnMsg (VBOXCLIPBOARDCLIENTDATA *pClient, VBOXHGCMSVCPARM paParms[])
|
---|
115 | {
|
---|
116 | /* Message priority is taken into account. */
|
---|
117 | if (pClient->fMsgQuit)
|
---|
118 | {
|
---|
119 | LogFlow(("vboxSvcClipboardReturnMsg: Quit\n"));
|
---|
120 | VBoxHGCMParmUInt32Set (&paParms[0], VBOX_SHARED_CLIPBOARD_HOST_MSG_QUIT);
|
---|
121 | VBoxHGCMParmUInt32Set (&paParms[1], 0);
|
---|
122 | pClient->fMsgQuit = false;
|
---|
123 | }
|
---|
124 | else if (pClient->fMsgReadData)
|
---|
125 | {
|
---|
126 | LogFlow(("vboxSvcClipboardReturnMsg: ReadData %02X\n", pClient->u32RequestedFormat));
|
---|
127 | VBoxHGCMParmUInt32Set (&paParms[0], VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA);
|
---|
128 | VBoxHGCMParmUInt32Set (&paParms[1], pClient->u32RequestedFormat);
|
---|
129 | pClient->fMsgReadData = false;
|
---|
130 | }
|
---|
131 | else if (pClient->fMsgFormats)
|
---|
132 | {
|
---|
133 | LogFlow(("vboxSvcClipboardReturnMsg: Formats %02X\n", pClient->u32AvailableFormats));
|
---|
134 | VBoxHGCMParmUInt32Set (&paParms[0], VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS);
|
---|
135 | VBoxHGCMParmUInt32Set (&paParms[1], pClient->u32AvailableFormats);
|
---|
136 | pClient->fMsgFormats = false;
|
---|
137 | }
|
---|
138 | else
|
---|
139 | {
|
---|
140 | /* No pending messages. */
|
---|
141 | LogFlow(("vboxSvcClipboardReturnMsg: no message\n"));
|
---|
142 | return false;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /* Message information assigned. */
|
---|
146 | return true;
|
---|
147 | }
|
---|
148 |
|
---|
149 | void vboxSvcClipboardReportMsg (VBOXCLIPBOARDCLIENTDATA *pClient, uint32_t u32Msg, uint32_t u32Formats)
|
---|
150 | {
|
---|
151 | if (vboxSvcClipboardLock ())
|
---|
152 | {
|
---|
153 | switch (u32Msg)
|
---|
154 | {
|
---|
155 | case VBOX_SHARED_CLIPBOARD_HOST_MSG_QUIT:
|
---|
156 | {
|
---|
157 | LogFlow(("vboxSvcClipboardReportMsg: Quit\n"));
|
---|
158 | pClient->fMsgQuit = true;
|
---|
159 | } break;
|
---|
160 | case VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA:
|
---|
161 | {
|
---|
162 | if ( vboxSvcClipboardMode () != VBOX_SHARED_CLIPBOARD_MODE_GUEST_TO_HOST
|
---|
163 | && vboxSvcClipboardMode () != VBOX_SHARED_CLIPBOARD_MODE_BIDIRECTIONAL)
|
---|
164 | {
|
---|
165 | /* Skip the message. */
|
---|
166 | break;
|
---|
167 | }
|
---|
168 |
|
---|
169 | LogFlow(("vboxSvcClipboardReportMsg: ReadData %02X\n", u32Formats));
|
---|
170 | pClient->u32RequestedFormat = u32Formats;
|
---|
171 | pClient->fMsgReadData = true;
|
---|
172 | } break;
|
---|
173 | case VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS:
|
---|
174 | {
|
---|
175 | if ( vboxSvcClipboardMode () != VBOX_SHARED_CLIPBOARD_MODE_HOST_TO_GUEST
|
---|
176 | && vboxSvcClipboardMode () != VBOX_SHARED_CLIPBOARD_MODE_BIDIRECTIONAL)
|
---|
177 | {
|
---|
178 | /* Skip the message. */
|
---|
179 | break;
|
---|
180 | }
|
---|
181 |
|
---|
182 | LogFlow(("vboxSvcClipboardReportMsg: Formats %02X\n", u32Formats));
|
---|
183 | pClient->u32AvailableFormats = u32Formats;
|
---|
184 | pClient->fMsgFormats = true;
|
---|
185 | } break;
|
---|
186 | default:
|
---|
187 | {
|
---|
188 | /* Invalid message. */
|
---|
189 | LogFlow(("vboxSvcClipboardReportMsg: invalid message %d\n", u32Msg));
|
---|
190 | } break;
|
---|
191 | }
|
---|
192 |
|
---|
193 | if (pClient->fAsync)
|
---|
194 | {
|
---|
195 | /* The client waits for a responce. */
|
---|
196 | bool fMessageReturned = vboxSvcClipboardReturnMsg (pClient, pClient->async.paParms);
|
---|
197 |
|
---|
198 | /* Make a copy of the handle. */
|
---|
199 | VBOXHGCMCALLHANDLE callHandle = pClient->async.callHandle;
|
---|
200 |
|
---|
201 | if (fMessageReturned)
|
---|
202 | {
|
---|
203 | /* There is a responce. */
|
---|
204 | pClient->fAsync = false;
|
---|
205 | }
|
---|
206 |
|
---|
207 | vboxSvcClipboardUnlock ();
|
---|
208 |
|
---|
209 | if (fMessageReturned)
|
---|
210 | {
|
---|
211 | LogFlow(("vboxSvcClipboardReportMsg: CallComplete\n"));
|
---|
212 | g_pHelpers->pfnCallComplete (callHandle, VINF_SUCCESS);
|
---|
213 | }
|
---|
214 | }
|
---|
215 | else
|
---|
216 | {
|
---|
217 | vboxSvcClipboardUnlock ();
|
---|
218 | }
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | static int svcInit (void)
|
---|
223 | {
|
---|
224 | int rc = RTCritSectInit (&critsect);
|
---|
225 |
|
---|
226 | if (RT_SUCCESS (rc))
|
---|
227 | {
|
---|
228 | vboxSvcClipboardModeSet (VBOX_SHARED_CLIPBOARD_MODE_OFF);
|
---|
229 |
|
---|
230 | rc = vboxClipboardInit ();
|
---|
231 |
|
---|
232 | /* Clean up on failure, because 'svnUnload' will not be called
|
---|
233 | * if the 'svcInit' returns an error.
|
---|
234 | */
|
---|
235 | if (VBOX_FAILURE (rc))
|
---|
236 | {
|
---|
237 | RTCritSectDelete (&critsect);
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | return rc;
|
---|
242 | }
|
---|
243 |
|
---|
244 | static DECLCALLBACK(int) svcUnload (void)
|
---|
245 | {
|
---|
246 | vboxClipboardDestroy ();
|
---|
247 | RTCritSectDelete (&critsect);
|
---|
248 | return VINF_SUCCESS;
|
---|
249 | }
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * Disconnect the host side of the shared clipboard and send a "host disconnected" message
|
---|
253 | * to the guest side.
|
---|
254 | */
|
---|
255 | static DECLCALLBACK(int) svcDisconnect (uint32_t u32ClientID, void *pvClient)
|
---|
256 | {
|
---|
257 | VBOXCLIPBOARDCLIENTDATA *pClient = (VBOXCLIPBOARDCLIENTDATA *)pvClient;
|
---|
258 |
|
---|
259 | vboxSvcClipboardReportMsg (pClient, VBOX_SHARED_CLIPBOARD_HOST_MSG_QUIT, 0);
|
---|
260 |
|
---|
261 | vboxClipboardDisconnect (pClient);
|
---|
262 |
|
---|
263 | memset (pClient, 0, sizeof (*pClient));
|
---|
264 |
|
---|
265 | g_pClient = NULL;
|
---|
266 |
|
---|
267 | return VINF_SUCCESS;
|
---|
268 | }
|
---|
269 |
|
---|
270 | static DECLCALLBACK(int) svcConnect (uint32_t u32ClientID, void *pvClient)
|
---|
271 | {
|
---|
272 | VBOXCLIPBOARDCLIENTDATA *pClient = (VBOXCLIPBOARDCLIENTDATA *)pvClient;
|
---|
273 |
|
---|
274 | int rc = VINF_SUCCESS;
|
---|
275 |
|
---|
276 | /* If there is already a client connected then we want to release it first. */
|
---|
277 | if (g_pClient != NULL)
|
---|
278 | {
|
---|
279 | uint32_t u32ClientID = g_pClient->u32ClientID;
|
---|
280 |
|
---|
281 | svcDisconnect(u32ClientID, g_pClient);
|
---|
282 | /* And free the resources in the hgcm subsystem. */
|
---|
283 | g_pHelpers->pfnDisconnectClient(g_pHelpers->pvInstance, u32ClientID);
|
---|
284 | }
|
---|
285 |
|
---|
286 | /* Register the client. */
|
---|
287 | memset (pClient, 0, sizeof (*pClient));
|
---|
288 |
|
---|
289 | pClient->u32ClientID = u32ClientID;
|
---|
290 |
|
---|
291 | rc = vboxClipboardConnect (pClient);
|
---|
292 |
|
---|
293 | if (VBOX_SUCCESS (rc))
|
---|
294 | {
|
---|
295 | g_pClient = pClient;
|
---|
296 | }
|
---|
297 |
|
---|
298 | Log(("vboxClipboardConnect: rc = %Vrc\n", rc));
|
---|
299 |
|
---|
300 | return rc;
|
---|
301 | }
|
---|
302 |
|
---|
303 | static DECLCALLBACK(void) svcCall (VBOXHGCMCALLHANDLE callHandle,
|
---|
304 | uint32_t u32ClientID,
|
---|
305 | void *pvClient,
|
---|
306 | uint32_t u32Function,
|
---|
307 | uint32_t cParms,
|
---|
308 | VBOXHGCMSVCPARM paParms[])
|
---|
309 | {
|
---|
310 | int rc = VINF_SUCCESS;
|
---|
311 |
|
---|
312 | Log(("svcCall: u32ClientID = %d, fn = %d, cParms = %d, pparms = %d\n",
|
---|
313 | u32ClientID, u32Function, cParms, paParms));
|
---|
314 |
|
---|
315 | VBOXCLIPBOARDCLIENTDATA *pClient = (VBOXCLIPBOARDCLIENTDATA *)pvClient;
|
---|
316 |
|
---|
317 | bool fAsynchronousProcessing = false;
|
---|
318 |
|
---|
319 | #ifdef DEBUG
|
---|
320 | uint32_t i;
|
---|
321 |
|
---|
322 | for (i = 0; i < cParms; i++)
|
---|
323 | {
|
---|
324 | /** @todo parameters other than 32 bit */
|
---|
325 | Log((" pparms[%d]: type %d value %d\n", i, paParms[i].type, paParms[i].u.uint32));
|
---|
326 | }
|
---|
327 | #endif
|
---|
328 |
|
---|
329 | switch (u32Function)
|
---|
330 | {
|
---|
331 | case VBOX_SHARED_CLIPBOARD_FN_GET_HOST_MSG:
|
---|
332 | {
|
---|
333 | /* The quest requests a host message. */
|
---|
334 | Log(("svcCall: VBOX_SHARED_CLIPBOARD_FN_GET_HOST_MSG\n"));
|
---|
335 |
|
---|
336 | if (cParms != VBOX_HGCM_PARM_COUNT (VBoxClipboardGetHostMsg))
|
---|
337 | {
|
---|
338 | rc = VERR_INVALID_PARAMETER;
|
---|
339 | }
|
---|
340 | else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* msg */
|
---|
341 | || paParms[1].type != VBOX_HGCM_SVC_PARM_32BIT /* formats */
|
---|
342 | )
|
---|
343 | {
|
---|
344 | rc = VERR_INVALID_PARAMETER;
|
---|
345 | }
|
---|
346 | else
|
---|
347 | {
|
---|
348 | /* Atomically verify the client's state. */
|
---|
349 | if (vboxSvcClipboardLock ())
|
---|
350 | {
|
---|
351 | bool fMessageReturned = vboxSvcClipboardReturnMsg (pClient, paParms);
|
---|
352 |
|
---|
353 | if (fMessageReturned)
|
---|
354 | {
|
---|
355 | /* Just return to the caller. */
|
---|
356 | pClient->fAsync = false;
|
---|
357 | }
|
---|
358 | else
|
---|
359 | {
|
---|
360 | /* No event available at the time. Process asynchronously. */
|
---|
361 | fAsynchronousProcessing = true;
|
---|
362 |
|
---|
363 | pClient->fAsync = true;
|
---|
364 | pClient->async.callHandle = callHandle;
|
---|
365 | pClient->async.paParms = paParms;
|
---|
366 |
|
---|
367 | Log(("svcCall: async.\n"));
|
---|
368 | }
|
---|
369 |
|
---|
370 | vboxSvcClipboardUnlock ();
|
---|
371 | }
|
---|
372 | else
|
---|
373 | {
|
---|
374 | rc = VERR_NOT_SUPPORTED;
|
---|
375 | }
|
---|
376 | }
|
---|
377 | } break;
|
---|
378 |
|
---|
379 | case VBOX_SHARED_CLIPBOARD_FN_FORMATS:
|
---|
380 | {
|
---|
381 | /* The guest reports that some formats are available. */
|
---|
382 | Log(("svcCall: VBOX_SHARED_CLIPBOARD_FN_FORMATS\n"));
|
---|
383 |
|
---|
384 | if (cParms != VBOX_HGCM_PARM_COUNT (VBoxClipboardFormats))
|
---|
385 | {
|
---|
386 | rc = VERR_INVALID_PARAMETER;
|
---|
387 | }
|
---|
388 | else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* formats */
|
---|
389 | )
|
---|
390 | {
|
---|
391 | rc = VERR_INVALID_PARAMETER;
|
---|
392 | }
|
---|
393 | else
|
---|
394 | {
|
---|
395 | uint32_t u32Formats;
|
---|
396 |
|
---|
397 | rc = VBoxHGCMParmUInt32Get (&paParms[0], &u32Formats);
|
---|
398 |
|
---|
399 | if (VBOX_SUCCESS (rc))
|
---|
400 | {
|
---|
401 | if ( vboxSvcClipboardMode () != VBOX_SHARED_CLIPBOARD_MODE_GUEST_TO_HOST
|
---|
402 | && vboxSvcClipboardMode () != VBOX_SHARED_CLIPBOARD_MODE_BIDIRECTIONAL)
|
---|
403 | {
|
---|
404 | rc = VERR_NOT_SUPPORTED;
|
---|
405 | break;
|
---|
406 | }
|
---|
407 |
|
---|
408 | if (g_pfnExtension)
|
---|
409 | {
|
---|
410 | VBOXCLIPBOARDEXTPARMS parms;
|
---|
411 |
|
---|
412 | parms.u32Format = u32Formats;
|
---|
413 |
|
---|
414 | g_pfnExtension (g_pvExtension, VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE, &parms, sizeof (parms));
|
---|
415 | }
|
---|
416 | else
|
---|
417 | {
|
---|
418 | vboxClipboardFormatAnnounce (pClient, u32Formats);
|
---|
419 | }
|
---|
420 | }
|
---|
421 | }
|
---|
422 | } break;
|
---|
423 |
|
---|
424 | case VBOX_SHARED_CLIPBOARD_FN_READ_DATA:
|
---|
425 | {
|
---|
426 | /* The guest wants to read data in the given format. */
|
---|
427 | Log(("svcCall: VBOX_SHARED_CLIPBOARD_FN_READ_DATA\n"));
|
---|
428 |
|
---|
429 | if (cParms != VBOX_HGCM_PARM_COUNT (VBoxClipboardReadData))
|
---|
430 | {
|
---|
431 | rc = VERR_INVALID_PARAMETER;
|
---|
432 | }
|
---|
433 | else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* format */
|
---|
434 | || paParms[1].type != VBOX_HGCM_SVC_PARM_PTR /* ptr */
|
---|
435 | || paParms[2].type != VBOX_HGCM_SVC_PARM_32BIT /* size */
|
---|
436 | )
|
---|
437 | {
|
---|
438 | rc = VERR_INVALID_PARAMETER;
|
---|
439 | }
|
---|
440 | else
|
---|
441 | {
|
---|
442 | uint32_t u32Format;
|
---|
443 | void *pv;
|
---|
444 | uint32_t cb;
|
---|
445 |
|
---|
446 | rc = VBoxHGCMParmUInt32Get (&paParms[0], &u32Format);
|
---|
447 |
|
---|
448 | if (VBOX_SUCCESS (rc))
|
---|
449 | {
|
---|
450 | rc = VBoxHGCMParmPtrGet (&paParms[1], &pv, &cb);
|
---|
451 |
|
---|
452 | if (VBOX_SUCCESS (rc))
|
---|
453 | {
|
---|
454 | if ( vboxSvcClipboardMode () != VBOX_SHARED_CLIPBOARD_MODE_HOST_TO_GUEST
|
---|
455 | && vboxSvcClipboardMode () != VBOX_SHARED_CLIPBOARD_MODE_BIDIRECTIONAL)
|
---|
456 | {
|
---|
457 | rc = VERR_NOT_SUPPORTED;
|
---|
458 | break;
|
---|
459 | }
|
---|
460 |
|
---|
461 | uint32_t cbActual = 0;
|
---|
462 |
|
---|
463 | if (g_pfnExtension)
|
---|
464 | {
|
---|
465 | VBOXCLIPBOARDEXTPARMS parms;
|
---|
466 |
|
---|
467 | parms.u32Format = u32Format;
|
---|
468 | parms.pvData = pv;
|
---|
469 | parms.cbData = cb;
|
---|
470 |
|
---|
471 | rc = g_pfnExtension (g_pvExtension, VBOX_CLIPBOARD_EXT_FN_DATA_READ, &parms, sizeof (parms));
|
---|
472 |
|
---|
473 | if (VBOX_SUCCESS (rc))
|
---|
474 | {
|
---|
475 | cbActual = parms.cbData;
|
---|
476 | }
|
---|
477 | }
|
---|
478 | else
|
---|
479 | {
|
---|
480 | rc = vboxClipboardReadData (pClient, u32Format, pv, cb, &cbActual);
|
---|
481 | }
|
---|
482 |
|
---|
483 | if (VBOX_SUCCESS (rc))
|
---|
484 | {
|
---|
485 | VBoxHGCMParmUInt32Set (&paParms[2], cbActual);
|
---|
486 | }
|
---|
487 | }
|
---|
488 | }
|
---|
489 | }
|
---|
490 | } break;
|
---|
491 |
|
---|
492 | case VBOX_SHARED_CLIPBOARD_FN_WRITE_DATA:
|
---|
493 | {
|
---|
494 | /* The guest writes the requested data. */
|
---|
495 | Log(("svcCall: VBOX_SHARED_CLIPBOARD_FN_WRITE_DATA\n"));
|
---|
496 |
|
---|
497 | if (cParms != VBOX_HGCM_PARM_COUNT (VBoxClipboardWriteData))
|
---|
498 | {
|
---|
499 | rc = VERR_INVALID_PARAMETER;
|
---|
500 | }
|
---|
501 | else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* format */
|
---|
502 | || paParms[1].type != VBOX_HGCM_SVC_PARM_PTR /* ptr */
|
---|
503 | )
|
---|
504 | {
|
---|
505 | rc = VERR_INVALID_PARAMETER;
|
---|
506 | }
|
---|
507 | else
|
---|
508 | {
|
---|
509 | void *pv;
|
---|
510 | uint32_t cb;
|
---|
511 | uint32_t u32Format;
|
---|
512 |
|
---|
513 | rc = VBoxHGCMParmUInt32Get (&paParms[0], &u32Format);
|
---|
514 |
|
---|
515 | if (VBOX_SUCCESS (rc))
|
---|
516 | {
|
---|
517 | rc = VBoxHGCMParmPtrGet (&paParms[1], &pv, &cb);
|
---|
518 |
|
---|
519 | if (VBOX_SUCCESS (rc))
|
---|
520 | {
|
---|
521 | if ( vboxSvcClipboardMode () != VBOX_SHARED_CLIPBOARD_MODE_GUEST_TO_HOST
|
---|
522 | && vboxSvcClipboardMode () != VBOX_SHARED_CLIPBOARD_MODE_BIDIRECTIONAL)
|
---|
523 | {
|
---|
524 | rc = VERR_NOT_SUPPORTED;
|
---|
525 | break;
|
---|
526 | }
|
---|
527 |
|
---|
528 | if (g_pfnExtension)
|
---|
529 | {
|
---|
530 | VBOXCLIPBOARDEXTPARMS parms;
|
---|
531 |
|
---|
532 | parms.u32Format = u32Format;
|
---|
533 | parms.pvData = pv;
|
---|
534 | parms.cbData = cb;
|
---|
535 |
|
---|
536 | g_pfnExtension (g_pvExtension, VBOX_CLIPBOARD_EXT_FN_DATA_WRITE, &parms, sizeof (parms));
|
---|
537 | }
|
---|
538 | else
|
---|
539 | {
|
---|
540 | vboxClipboardWriteData (pClient, pv, cb, u32Format);
|
---|
541 | }
|
---|
542 | }
|
---|
543 | }
|
---|
544 | }
|
---|
545 | } break;
|
---|
546 |
|
---|
547 | default:
|
---|
548 | {
|
---|
549 | rc = VERR_NOT_IMPLEMENTED;
|
---|
550 | }
|
---|
551 | }
|
---|
552 |
|
---|
553 | LogFlow(("svcCall: rc = %Vrc\n", rc));
|
---|
554 |
|
---|
555 | if (!fAsynchronousProcessing)
|
---|
556 | {
|
---|
557 | g_pHelpers->pfnCallComplete (callHandle, rc);
|
---|
558 | }
|
---|
559 | }
|
---|
560 |
|
---|
561 | /*
|
---|
562 | * We differentiate between a function handler for the guest and one for the host.
|
---|
563 | */
|
---|
564 | static DECLCALLBACK(int) svcHostCall (uint32_t u32Function,
|
---|
565 | uint32_t cParms,
|
---|
566 | VBOXHGCMSVCPARM paParms[])
|
---|
567 | {
|
---|
568 | int rc = VINF_SUCCESS;
|
---|
569 |
|
---|
570 | Log(("svcHostCall: fn = %d, cParms = %d, pparms = %d\n",
|
---|
571 | u32Function, cParms, paParms));
|
---|
572 |
|
---|
573 | switch (u32Function)
|
---|
574 | {
|
---|
575 | case VBOX_SHARED_CLIPBOARD_HOST_FN_SET_MODE:
|
---|
576 | {
|
---|
577 | Log(("svcCall: VBOX_SHARED_CLIPBOARD_HOST_FN_SET_MODE\n"));
|
---|
578 |
|
---|
579 | if (cParms != 1)
|
---|
580 | {
|
---|
581 | rc = VERR_INVALID_PARAMETER;
|
---|
582 | }
|
---|
583 | else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* mode */
|
---|
584 | )
|
---|
585 | {
|
---|
586 | rc = VERR_INVALID_PARAMETER;
|
---|
587 | }
|
---|
588 | else
|
---|
589 | {
|
---|
590 | uint32_t u32Mode = VBOX_SHARED_CLIPBOARD_MODE_OFF;
|
---|
591 |
|
---|
592 | rc = VBoxHGCMParmUInt32Get (&paParms[0], &u32Mode);
|
---|
593 |
|
---|
594 | /* The setter takes care of invalid values. */
|
---|
595 | vboxSvcClipboardModeSet (u32Mode);
|
---|
596 | }
|
---|
597 | } break;
|
---|
598 |
|
---|
599 | default:
|
---|
600 | break;
|
---|
601 | }
|
---|
602 |
|
---|
603 | LogFlow(("svcHostCall: rc = %Vrc\n", rc));
|
---|
604 | return rc;
|
---|
605 | }
|
---|
606 |
|
---|
607 | static DECLCALLBACK(int) svcSaveState(uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM)
|
---|
608 | {
|
---|
609 | /* If there are any pending requests, they must be completed here. Since
|
---|
610 | * the service is single threaded, there could be only requests
|
---|
611 | * which the service itself has postponed.
|
---|
612 | *
|
---|
613 | * HGCM knows that the state is being saved and that the pfnComplete
|
---|
614 | * calls are just clean ups. These requests are saved by the VMMDev.
|
---|
615 | *
|
---|
616 | * When the state will be restored, these requests will be reissued
|
---|
617 | * by VMMDev. The service therefore must save state as if there were no
|
---|
618 | * pending request.
|
---|
619 | */
|
---|
620 | Log(("svcSaveState: u32ClientID = %d\n", u32ClientID));
|
---|
621 |
|
---|
622 | VBOXCLIPBOARDCLIENTDATA *pClient = (VBOXCLIPBOARDCLIENTDATA *)pvClient;
|
---|
623 |
|
---|
624 | /* Save client structure length & contents */
|
---|
625 | int rc = SSMR3PutU32(pSSM, sizeof(*pClient));
|
---|
626 | AssertRCReturn(rc, rc);
|
---|
627 |
|
---|
628 | rc = SSMR3PutMem(pSSM, pClient, sizeof(*pClient));
|
---|
629 | AssertRCReturn(rc, rc);
|
---|
630 |
|
---|
631 | if (pClient->fAsync)
|
---|
632 | {
|
---|
633 | g_pHelpers->pfnCallComplete (pClient->async.callHandle, VINF_SUCCESS /* error code is not important here. */);
|
---|
634 | pClient->fAsync = false;
|
---|
635 | }
|
---|
636 |
|
---|
637 | return VINF_SUCCESS;
|
---|
638 | }
|
---|
639 |
|
---|
640 | static DECLCALLBACK(int) svcLoadState(uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM)
|
---|
641 | {
|
---|
642 | Log(("svcLoadState: u32ClientID = %d\n", u32ClientID));
|
---|
643 |
|
---|
644 | VBOXCLIPBOARDCLIENTDATA *pClient = (VBOXCLIPBOARDCLIENTDATA *)pvClient;
|
---|
645 |
|
---|
646 | /* Existing client can not be in async state yet. */
|
---|
647 | Assert(!pClient->fAsync);
|
---|
648 |
|
---|
649 | /* Restore the client data. */
|
---|
650 | uint32_t len;
|
---|
651 | int rc = SSMR3GetU32(pSSM, &len);
|
---|
652 | AssertRCReturn(rc, rc);
|
---|
653 |
|
---|
654 | if (len != sizeof(VBOXCLIPBOARDCLIENTDATA))
|
---|
655 | {
|
---|
656 | Log(("Client len mismatch: %d %d\n", len, sizeof (VBOXCLIPBOARDCLIENTDATA)));
|
---|
657 | return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
|
---|
658 | }
|
---|
659 |
|
---|
660 | VBOXCLIPBOARDCLIENTDATA client;
|
---|
661 | rc = SSMR3GetMem(pSSM, &client, sizeof(client));
|
---|
662 | AssertRCReturn(rc, rc);
|
---|
663 |
|
---|
664 | /* Verify the loaded clients data and update the pClient. */
|
---|
665 | if (pClient->u32ClientID != client.u32ClientID)
|
---|
666 | {
|
---|
667 | Log(("Client ID mismatch: %d %d\n", pClient->u32ClientID, client.u32ClientID));
|
---|
668 | return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
|
---|
669 | }
|
---|
670 |
|
---|
671 | pClient->fMsgQuit = client.fMsgQuit;
|
---|
672 | pClient->fMsgReadData = client.fMsgReadData;
|
---|
673 | pClient->fMsgFormats = client.fMsgFormats;
|
---|
674 | pClient->u32RequestedFormat = client.u32RequestedFormat;
|
---|
675 |
|
---|
676 | /* Actual host data are to be reported to guest (SYNC). */
|
---|
677 | vboxClipboardSync (pClient);
|
---|
678 |
|
---|
679 | return VINF_SUCCESS;
|
---|
680 | }
|
---|
681 |
|
---|
682 | static DECLCALLBACK(int) extCallback (uint32_t u32Function, uint32_t u32Format, void *pvData, uint32_t cbData)
|
---|
683 | {
|
---|
684 | if (g_pClient != NULL)
|
---|
685 | {
|
---|
686 | switch (u32Function)
|
---|
687 | {
|
---|
688 | case VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE:
|
---|
689 | {
|
---|
690 | vboxSvcClipboardReportMsg (g_pClient, VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS, u32Format);
|
---|
691 | } break;
|
---|
692 |
|
---|
693 | case VBOX_CLIPBOARD_EXT_FN_DATA_READ:
|
---|
694 | {
|
---|
695 | vboxSvcClipboardReportMsg (g_pClient, VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA, u32Format);
|
---|
696 | } break;
|
---|
697 |
|
---|
698 | default:
|
---|
699 | return VERR_NOT_SUPPORTED;
|
---|
700 | }
|
---|
701 | }
|
---|
702 |
|
---|
703 | return VINF_SUCCESS;
|
---|
704 | }
|
---|
705 |
|
---|
706 | static DECLCALLBACK(int) svcRegisterExtension(PFNHGCMSVCEXT pfnExtension, void *pvExtension)
|
---|
707 | {
|
---|
708 | LogFlowFunc(("pfnExtension = %p\n", pfnExtension));
|
---|
709 |
|
---|
710 | VBOXCLIPBOARDEXTPARMS parms;
|
---|
711 |
|
---|
712 | if (pfnExtension)
|
---|
713 | {
|
---|
714 | /* Install extension. */
|
---|
715 | g_pfnExtension = pfnExtension;
|
---|
716 | g_pvExtension = pvExtension;
|
---|
717 |
|
---|
718 | parms.pvData = (void *)extCallback;
|
---|
719 | g_pfnExtension (g_pvExtension, VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK, &parms, sizeof (parms));
|
---|
720 | }
|
---|
721 | else
|
---|
722 | {
|
---|
723 | if (g_pfnExtension)
|
---|
724 | {
|
---|
725 | parms.pvData = NULL;
|
---|
726 | g_pfnExtension (g_pvExtension, VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK, &parms, sizeof (parms));
|
---|
727 | }
|
---|
728 |
|
---|
729 | /* Uninstall extension. */
|
---|
730 | g_pfnExtension = NULL;
|
---|
731 | g_pvExtension = NULL;
|
---|
732 | }
|
---|
733 |
|
---|
734 | return VINF_SUCCESS;
|
---|
735 | }
|
---|
736 |
|
---|
737 | extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad (VBOXHGCMSVCFNTABLE *ptable)
|
---|
738 | {
|
---|
739 | int rc = VINF_SUCCESS;
|
---|
740 |
|
---|
741 | LogFlowFunc(("ptable = %p\n", ptable));
|
---|
742 |
|
---|
743 | if (!ptable)
|
---|
744 | {
|
---|
745 | rc = VERR_INVALID_PARAMETER;
|
---|
746 | }
|
---|
747 | else
|
---|
748 | {
|
---|
749 | Log(("VBoxHGCMSvcLoad: ptable->cbSize = %d, ptable->u32Version = 0x%08X\n", ptable->cbSize, ptable->u32Version));
|
---|
750 |
|
---|
751 | if ( ptable->cbSize != sizeof (VBOXHGCMSVCFNTABLE)
|
---|
752 | || ptable->u32Version != VBOX_HGCM_SVC_VERSION)
|
---|
753 | {
|
---|
754 | rc = VERR_INVALID_PARAMETER;
|
---|
755 | }
|
---|
756 | else
|
---|
757 | {
|
---|
758 | g_pHelpers = ptable->pHelpers;
|
---|
759 |
|
---|
760 | ptable->cbClient = sizeof (VBOXCLIPBOARDCLIENTDATA);
|
---|
761 |
|
---|
762 | ptable->pfnUnload = svcUnload;
|
---|
763 | ptable->pfnConnect = svcConnect;
|
---|
764 | ptable->pfnDisconnect = svcDisconnect;
|
---|
765 | ptable->pfnCall = svcCall;
|
---|
766 | ptable->pfnHostCall = svcHostCall;
|
---|
767 | ptable->pfnSaveState = svcSaveState;
|
---|
768 | ptable->pfnLoadState = svcLoadState;
|
---|
769 | ptable->pfnRegisterExtension = svcRegisterExtension;
|
---|
770 |
|
---|
771 | /* Service specific initialization. */
|
---|
772 | rc = svcInit ();
|
---|
773 | }
|
---|
774 | }
|
---|
775 |
|
---|
776 | return rc;
|
---|
777 | }
|
---|