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 | RTCritSectInit (&critsect);
|
---|
225 | vboxSvcClipboardModeSet (VBOX_SHARED_CLIPBOARD_MODE_OFF);
|
---|
226 | return vboxClipboardInit ();
|
---|
227 | }
|
---|
228 |
|
---|
229 | static DECLCALLBACK(int) svcUnload (void)
|
---|
230 | {
|
---|
231 | vboxClipboardDestroy ();
|
---|
232 | RTCritSectDelete (&critsect);
|
---|
233 | return VINF_SUCCESS;
|
---|
234 | }
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * Disconnect the host side of the shared clipboard and send a "host disconnected" message
|
---|
238 | * to the guest side.
|
---|
239 | */
|
---|
240 | static DECLCALLBACK(int) svcDisconnect (uint32_t u32ClientID, void *pvClient)
|
---|
241 | {
|
---|
242 | VBOXCLIPBOARDCLIENTDATA *pClient = (VBOXCLIPBOARDCLIENTDATA *)pvClient;
|
---|
243 |
|
---|
244 | vboxSvcClipboardReportMsg (pClient, VBOX_SHARED_CLIPBOARD_HOST_MSG_QUIT, 0);
|
---|
245 |
|
---|
246 | vboxClipboardDisconnect (pClient);
|
---|
247 |
|
---|
248 | memset (pClient, 0, sizeof (*pClient));
|
---|
249 |
|
---|
250 | g_pClient = NULL;
|
---|
251 |
|
---|
252 | return VINF_SUCCESS;
|
---|
253 | }
|
---|
254 |
|
---|
255 | static DECLCALLBACK(int) svcConnect (uint32_t u32ClientID, void *pvClient)
|
---|
256 | {
|
---|
257 | VBOXCLIPBOARDCLIENTDATA *pClient = (VBOXCLIPBOARDCLIENTDATA *)pvClient;
|
---|
258 |
|
---|
259 | int rc = VINF_SUCCESS;
|
---|
260 |
|
---|
261 | /* If there is already a client connected then we want to release it first. */
|
---|
262 | if (g_pClient != NULL)
|
---|
263 | {
|
---|
264 | uint32_t u32ClientID = g_pClient->u32ClientID;
|
---|
265 |
|
---|
266 | svcDisconnect(u32ClientID, g_pClient);
|
---|
267 | /* And free the resources in the hgcm subsystem. */
|
---|
268 | g_pHelpers->pfnDisconnectClient(g_pHelpers->pvInstance, u32ClientID);
|
---|
269 | }
|
---|
270 |
|
---|
271 | /* Register the client. */
|
---|
272 | memset (pClient, 0, sizeof (*pClient));
|
---|
273 |
|
---|
274 | pClient->u32ClientID = u32ClientID;
|
---|
275 |
|
---|
276 | rc = vboxClipboardConnect (pClient);
|
---|
277 |
|
---|
278 | if (VBOX_SUCCESS (rc))
|
---|
279 | {
|
---|
280 | g_pClient = pClient;
|
---|
281 | }
|
---|
282 |
|
---|
283 | Log(("vboxClipboardConnect: rc = %Vrc\n", rc));
|
---|
284 |
|
---|
285 | return rc;
|
---|
286 | }
|
---|
287 |
|
---|
288 | static DECLCALLBACK(void) svcCall (VBOXHGCMCALLHANDLE callHandle,
|
---|
289 | uint32_t u32ClientID,
|
---|
290 | void *pvClient,
|
---|
291 | uint32_t u32Function,
|
---|
292 | uint32_t cParms,
|
---|
293 | VBOXHGCMSVCPARM paParms[])
|
---|
294 | {
|
---|
295 | int rc = VINF_SUCCESS;
|
---|
296 |
|
---|
297 | Log(("svcCall: u32ClientID = %d, fn = %d, cParms = %d, pparms = %d\n",
|
---|
298 | u32ClientID, u32Function, cParms, paParms));
|
---|
299 |
|
---|
300 | VBOXCLIPBOARDCLIENTDATA *pClient = (VBOXCLIPBOARDCLIENTDATA *)pvClient;
|
---|
301 |
|
---|
302 | bool fAsynchronousProcessing = false;
|
---|
303 |
|
---|
304 | #ifdef DEBUG
|
---|
305 | uint32_t i;
|
---|
306 |
|
---|
307 | for (i = 0; i < cParms; i++)
|
---|
308 | {
|
---|
309 | /** @todo parameters other than 32 bit */
|
---|
310 | Log((" pparms[%d]: type %d value %d\n", i, paParms[i].type, paParms[i].u.uint32));
|
---|
311 | }
|
---|
312 | #endif
|
---|
313 |
|
---|
314 | switch (u32Function)
|
---|
315 | {
|
---|
316 | case VBOX_SHARED_CLIPBOARD_FN_GET_HOST_MSG:
|
---|
317 | {
|
---|
318 | /* The quest requests a host message. */
|
---|
319 | Log(("svcCall: VBOX_SHARED_CLIPBOARD_FN_GET_HOST_MSG\n"));
|
---|
320 |
|
---|
321 | if (cParms != VBOX_HGCM_PARM_COUNT (VBoxClipboardGetHostMsg))
|
---|
322 | {
|
---|
323 | rc = VERR_INVALID_PARAMETER;
|
---|
324 | }
|
---|
325 | else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* msg */
|
---|
326 | || paParms[1].type != VBOX_HGCM_SVC_PARM_32BIT /* formats */
|
---|
327 | )
|
---|
328 | {
|
---|
329 | rc = VERR_INVALID_PARAMETER;
|
---|
330 | }
|
---|
331 | else
|
---|
332 | {
|
---|
333 | /* Atomically verify the client's state. */
|
---|
334 | if (vboxSvcClipboardLock ())
|
---|
335 | {
|
---|
336 | bool fMessageReturned = vboxSvcClipboardReturnMsg (pClient, paParms);
|
---|
337 |
|
---|
338 | if (fMessageReturned)
|
---|
339 | {
|
---|
340 | /* Just return to the caller. */
|
---|
341 | pClient->fAsync = false;
|
---|
342 | }
|
---|
343 | else
|
---|
344 | {
|
---|
345 | /* No event available at the time. Process asynchronously. */
|
---|
346 | fAsynchronousProcessing = true;
|
---|
347 |
|
---|
348 | pClient->fAsync = true;
|
---|
349 | pClient->async.callHandle = callHandle;
|
---|
350 | pClient->async.paParms = paParms;
|
---|
351 |
|
---|
352 | Log(("svcCall: async.\n"));
|
---|
353 | }
|
---|
354 |
|
---|
355 | vboxSvcClipboardUnlock ();
|
---|
356 | }
|
---|
357 | else
|
---|
358 | {
|
---|
359 | rc = VERR_NOT_SUPPORTED;
|
---|
360 | }
|
---|
361 | }
|
---|
362 | } break;
|
---|
363 |
|
---|
364 | case VBOX_SHARED_CLIPBOARD_FN_FORMATS:
|
---|
365 | {
|
---|
366 | /* The guest reports that some formats are available. */
|
---|
367 | Log(("svcCall: VBOX_SHARED_CLIPBOARD_FN_FORMATS\n"));
|
---|
368 |
|
---|
369 | if (cParms != VBOX_HGCM_PARM_COUNT (VBoxClipboardFormats))
|
---|
370 | {
|
---|
371 | rc = VERR_INVALID_PARAMETER;
|
---|
372 | }
|
---|
373 | else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* formats */
|
---|
374 | )
|
---|
375 | {
|
---|
376 | rc = VERR_INVALID_PARAMETER;
|
---|
377 | }
|
---|
378 | else
|
---|
379 | {
|
---|
380 | uint32_t u32Formats;
|
---|
381 |
|
---|
382 | rc = VBoxHGCMParmUInt32Get (&paParms[0], &u32Formats);
|
---|
383 |
|
---|
384 | if (VBOX_SUCCESS (rc))
|
---|
385 | {
|
---|
386 | if ( vboxSvcClipboardMode () != VBOX_SHARED_CLIPBOARD_MODE_GUEST_TO_HOST
|
---|
387 | && vboxSvcClipboardMode () != VBOX_SHARED_CLIPBOARD_MODE_BIDIRECTIONAL)
|
---|
388 | {
|
---|
389 | rc = VERR_NOT_SUPPORTED;
|
---|
390 | break;
|
---|
391 | }
|
---|
392 |
|
---|
393 | if (g_pfnExtension)
|
---|
394 | {
|
---|
395 | VBOXCLIPBOARDEXTPARMS parms;
|
---|
396 |
|
---|
397 | parms.u32Format = u32Formats;
|
---|
398 |
|
---|
399 | g_pfnExtension (g_pvExtension, VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE, &parms, sizeof (parms));
|
---|
400 | }
|
---|
401 | else
|
---|
402 | {
|
---|
403 | vboxClipboardFormatAnnounce (pClient, u32Formats);
|
---|
404 | }
|
---|
405 | }
|
---|
406 | }
|
---|
407 | } break;
|
---|
408 |
|
---|
409 | case VBOX_SHARED_CLIPBOARD_FN_READ_DATA:
|
---|
410 | {
|
---|
411 | /* The guest wants to read data in the given format. */
|
---|
412 | Log(("svcCall: VBOX_SHARED_CLIPBOARD_FN_READ_DATA\n"));
|
---|
413 |
|
---|
414 | if (cParms != VBOX_HGCM_PARM_COUNT (VBoxClipboardReadData))
|
---|
415 | {
|
---|
416 | rc = VERR_INVALID_PARAMETER;
|
---|
417 | }
|
---|
418 | else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* format */
|
---|
419 | || paParms[1].type != VBOX_HGCM_SVC_PARM_PTR /* ptr */
|
---|
420 | || paParms[2].type != VBOX_HGCM_SVC_PARM_32BIT /* size */
|
---|
421 | )
|
---|
422 | {
|
---|
423 | rc = VERR_INVALID_PARAMETER;
|
---|
424 | }
|
---|
425 | else
|
---|
426 | {
|
---|
427 | uint32_t u32Format;
|
---|
428 | void *pv;
|
---|
429 | uint32_t cb;
|
---|
430 |
|
---|
431 | rc = VBoxHGCMParmUInt32Get (&paParms[0], &u32Format);
|
---|
432 |
|
---|
433 | if (VBOX_SUCCESS (rc))
|
---|
434 | {
|
---|
435 | rc = VBoxHGCMParmPtrGet (&paParms[1], &pv, &cb);
|
---|
436 |
|
---|
437 | if (VBOX_SUCCESS (rc))
|
---|
438 | {
|
---|
439 | if ( vboxSvcClipboardMode () != VBOX_SHARED_CLIPBOARD_MODE_HOST_TO_GUEST
|
---|
440 | && vboxSvcClipboardMode () != VBOX_SHARED_CLIPBOARD_MODE_BIDIRECTIONAL)
|
---|
441 | {
|
---|
442 | rc = VERR_NOT_SUPPORTED;
|
---|
443 | break;
|
---|
444 | }
|
---|
445 |
|
---|
446 | uint32_t cbActual = 0;
|
---|
447 |
|
---|
448 | if (g_pfnExtension)
|
---|
449 | {
|
---|
450 | VBOXCLIPBOARDEXTPARMS parms;
|
---|
451 |
|
---|
452 | parms.u32Format = u32Format;
|
---|
453 | parms.pvData = pv;
|
---|
454 | parms.cbData = cb;
|
---|
455 |
|
---|
456 | rc = g_pfnExtension (g_pvExtension, VBOX_CLIPBOARD_EXT_FN_DATA_READ, &parms, sizeof (parms));
|
---|
457 |
|
---|
458 | if (VBOX_SUCCESS (rc))
|
---|
459 | {
|
---|
460 | cbActual = parms.cbData;
|
---|
461 | }
|
---|
462 | }
|
---|
463 | else
|
---|
464 | {
|
---|
465 | rc = vboxClipboardReadData (pClient, u32Format, pv, cb, &cbActual);
|
---|
466 | }
|
---|
467 |
|
---|
468 | if (VBOX_SUCCESS (rc))
|
---|
469 | {
|
---|
470 | VBoxHGCMParmUInt32Set (&paParms[2], cbActual);
|
---|
471 | }
|
---|
472 | }
|
---|
473 | }
|
---|
474 | }
|
---|
475 | } break;
|
---|
476 |
|
---|
477 | case VBOX_SHARED_CLIPBOARD_FN_WRITE_DATA:
|
---|
478 | {
|
---|
479 | /* The guest writes the requested data. */
|
---|
480 | Log(("svcCall: VBOX_SHARED_CLIPBOARD_FN_WRITE_DATA\n"));
|
---|
481 |
|
---|
482 | if (cParms != VBOX_HGCM_PARM_COUNT (VBoxClipboardWriteData))
|
---|
483 | {
|
---|
484 | rc = VERR_INVALID_PARAMETER;
|
---|
485 | }
|
---|
486 | else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* format */
|
---|
487 | || paParms[1].type != VBOX_HGCM_SVC_PARM_PTR /* ptr */
|
---|
488 | )
|
---|
489 | {
|
---|
490 | rc = VERR_INVALID_PARAMETER;
|
---|
491 | }
|
---|
492 | else
|
---|
493 | {
|
---|
494 | void *pv;
|
---|
495 | uint32_t cb;
|
---|
496 | uint32_t u32Format;
|
---|
497 |
|
---|
498 | rc = VBoxHGCMParmUInt32Get (&paParms[0], &u32Format);
|
---|
499 |
|
---|
500 | if (VBOX_SUCCESS (rc))
|
---|
501 | {
|
---|
502 | rc = VBoxHGCMParmPtrGet (&paParms[1], &pv, &cb);
|
---|
503 |
|
---|
504 | if (VBOX_SUCCESS (rc))
|
---|
505 | {
|
---|
506 | if ( vboxSvcClipboardMode () != VBOX_SHARED_CLIPBOARD_MODE_GUEST_TO_HOST
|
---|
507 | && vboxSvcClipboardMode () != VBOX_SHARED_CLIPBOARD_MODE_BIDIRECTIONAL)
|
---|
508 | {
|
---|
509 | rc = VERR_NOT_SUPPORTED;
|
---|
510 | break;
|
---|
511 | }
|
---|
512 |
|
---|
513 | if (g_pfnExtension)
|
---|
514 | {
|
---|
515 | VBOXCLIPBOARDEXTPARMS parms;
|
---|
516 |
|
---|
517 | parms.u32Format = u32Format;
|
---|
518 | parms.pvData = pv;
|
---|
519 | parms.cbData = cb;
|
---|
520 |
|
---|
521 | g_pfnExtension (g_pvExtension, VBOX_CLIPBOARD_EXT_FN_DATA_WRITE, &parms, sizeof (parms));
|
---|
522 | }
|
---|
523 | else
|
---|
524 | {
|
---|
525 | vboxClipboardWriteData (pClient, pv, cb, u32Format);
|
---|
526 | }
|
---|
527 | }
|
---|
528 | }
|
---|
529 | }
|
---|
530 | } break;
|
---|
531 |
|
---|
532 | default:
|
---|
533 | {
|
---|
534 | rc = VERR_NOT_IMPLEMENTED;
|
---|
535 | }
|
---|
536 | }
|
---|
537 |
|
---|
538 | LogFlow(("svcCall: rc = %Vrc\n", rc));
|
---|
539 |
|
---|
540 | if (!fAsynchronousProcessing)
|
---|
541 | {
|
---|
542 | g_pHelpers->pfnCallComplete (callHandle, rc);
|
---|
543 | }
|
---|
544 | }
|
---|
545 |
|
---|
546 | /*
|
---|
547 | * We differentiate between a function handler for the guest and one for the host.
|
---|
548 | */
|
---|
549 | static DECLCALLBACK(int) svcHostCall (uint32_t u32Function,
|
---|
550 | uint32_t cParms,
|
---|
551 | VBOXHGCMSVCPARM paParms[])
|
---|
552 | {
|
---|
553 | int rc = VINF_SUCCESS;
|
---|
554 |
|
---|
555 | Log(("svcHostCall: fn = %d, cParms = %d, pparms = %d\n",
|
---|
556 | u32Function, cParms, paParms));
|
---|
557 |
|
---|
558 | switch (u32Function)
|
---|
559 | {
|
---|
560 | case VBOX_SHARED_CLIPBOARD_HOST_FN_SET_MODE:
|
---|
561 | {
|
---|
562 | Log(("svcCall: VBOX_SHARED_CLIPBOARD_HOST_FN_SET_MODE\n"));
|
---|
563 |
|
---|
564 | if (cParms != 1)
|
---|
565 | {
|
---|
566 | rc = VERR_INVALID_PARAMETER;
|
---|
567 | }
|
---|
568 | else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* mode */
|
---|
569 | )
|
---|
570 | {
|
---|
571 | rc = VERR_INVALID_PARAMETER;
|
---|
572 | }
|
---|
573 | else
|
---|
574 | {
|
---|
575 | uint32_t u32Mode = VBOX_SHARED_CLIPBOARD_MODE_OFF;
|
---|
576 |
|
---|
577 | rc = VBoxHGCMParmUInt32Get (&paParms[0], &u32Mode);
|
---|
578 |
|
---|
579 | /* The setter takes care of invalid values. */
|
---|
580 | vboxSvcClipboardModeSet (u32Mode);
|
---|
581 | }
|
---|
582 | } break;
|
---|
583 |
|
---|
584 | default:
|
---|
585 | break;
|
---|
586 | }
|
---|
587 |
|
---|
588 | LogFlow(("svcHostCall: rc = %Vrc\n", rc));
|
---|
589 | return rc;
|
---|
590 | }
|
---|
591 |
|
---|
592 | static DECLCALLBACK(int) svcSaveState(uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM)
|
---|
593 | {
|
---|
594 | /* If there are any pending requests, they must be completed here. Since
|
---|
595 | * the service is single threaded, there could be only requests
|
---|
596 | * which the service itself has postponed.
|
---|
597 | *
|
---|
598 | * HGCM knows that the state is being saved and that the pfnComplete
|
---|
599 | * calls are just clean ups. These requests are saved by the VMMDev.
|
---|
600 | *
|
---|
601 | * When the state will be restored, these requests will be reissued
|
---|
602 | * by VMMDev. The service therefore must save state as if there were no
|
---|
603 | * pending request.
|
---|
604 | */
|
---|
605 | Log(("svcSaveState: u32ClientID = %d\n", u32ClientID));
|
---|
606 |
|
---|
607 | VBOXCLIPBOARDCLIENTDATA *pClient = (VBOXCLIPBOARDCLIENTDATA *)pvClient;
|
---|
608 |
|
---|
609 | /* Save client structure length & contents */
|
---|
610 | int rc = SSMR3PutU32(pSSM, sizeof(*pClient));
|
---|
611 | AssertRCReturn(rc, rc);
|
---|
612 |
|
---|
613 | rc = SSMR3PutMem(pSSM, pClient, sizeof(*pClient));
|
---|
614 | AssertRCReturn(rc, rc);
|
---|
615 |
|
---|
616 | if (pClient->fAsync)
|
---|
617 | {
|
---|
618 | g_pHelpers->pfnCallComplete (pClient->async.callHandle, VINF_SUCCESS /* error code is not important here. */);
|
---|
619 | pClient->fAsync = false;
|
---|
620 | }
|
---|
621 |
|
---|
622 | return VINF_SUCCESS;
|
---|
623 | }
|
---|
624 |
|
---|
625 | static DECLCALLBACK(int) svcLoadState(uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM)
|
---|
626 | {
|
---|
627 | Log(("svcLoadState: u32ClientID = %d\n", u32ClientID));
|
---|
628 |
|
---|
629 | VBOXCLIPBOARDCLIENTDATA *pClient = (VBOXCLIPBOARDCLIENTDATA *)pvClient;
|
---|
630 |
|
---|
631 | /* Existing client can not be in async state yet. */
|
---|
632 | Assert(!pClient->fAsync);
|
---|
633 |
|
---|
634 | /* Restore the client data. */
|
---|
635 | uint32_t len;
|
---|
636 | int rc = SSMR3GetU32(pSSM, &len);
|
---|
637 | AssertRCReturn(rc, rc);
|
---|
638 |
|
---|
639 | if (len != sizeof(VBOXCLIPBOARDCLIENTDATA))
|
---|
640 | {
|
---|
641 | Log(("Client len mismatch: %d %d\n", len, sizeof (VBOXCLIPBOARDCLIENTDATA)));
|
---|
642 | return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
|
---|
643 | }
|
---|
644 |
|
---|
645 | VBOXCLIPBOARDCLIENTDATA client;
|
---|
646 | rc = SSMR3GetMem(pSSM, &client, sizeof(client));
|
---|
647 | AssertRCReturn(rc, rc);
|
---|
648 |
|
---|
649 | /* Verify the loaded clients data and update the pClient. */
|
---|
650 | if (pClient->u32ClientID != client.u32ClientID)
|
---|
651 | {
|
---|
652 | Log(("Client ID mismatch: %d %d\n", pClient->u32ClientID, client.u32ClientID));
|
---|
653 | return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
|
---|
654 | }
|
---|
655 |
|
---|
656 | pClient->fMsgQuit = client.fMsgQuit;
|
---|
657 | pClient->fMsgReadData = client.fMsgReadData;
|
---|
658 | pClient->fMsgFormats = client.fMsgFormats;
|
---|
659 | pClient->u32RequestedFormat = client.u32RequestedFormat;
|
---|
660 |
|
---|
661 | /* Actual host data are to be reported to guest (SYNC). */
|
---|
662 | vboxClipboardSync (pClient);
|
---|
663 |
|
---|
664 | return VINF_SUCCESS;
|
---|
665 | }
|
---|
666 |
|
---|
667 | static DECLCALLBACK(int) extCallback (uint32_t u32Function, uint32_t u32Format, void *pvData, uint32_t cbData)
|
---|
668 | {
|
---|
669 | if (g_pClient != NULL)
|
---|
670 | {
|
---|
671 | switch (u32Function)
|
---|
672 | {
|
---|
673 | case VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE:
|
---|
674 | {
|
---|
675 | vboxSvcClipboardReportMsg (g_pClient, VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS, u32Format);
|
---|
676 | } break;
|
---|
677 |
|
---|
678 | case VBOX_CLIPBOARD_EXT_FN_DATA_READ:
|
---|
679 | {
|
---|
680 | vboxSvcClipboardReportMsg (g_pClient, VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA, u32Format);
|
---|
681 | } break;
|
---|
682 |
|
---|
683 | default:
|
---|
684 | return VERR_NOT_SUPPORTED;
|
---|
685 | }
|
---|
686 | }
|
---|
687 |
|
---|
688 | return VINF_SUCCESS;
|
---|
689 | }
|
---|
690 |
|
---|
691 | static DECLCALLBACK(int) svcRegisterExtension(PFNHGCMSVCEXT pfnExtension, void *pvExtension)
|
---|
692 | {
|
---|
693 | LogFlowFunc(("pfnExtension = %p\n", pfnExtension));
|
---|
694 |
|
---|
695 | VBOXCLIPBOARDEXTPARMS parms;
|
---|
696 |
|
---|
697 | if (pfnExtension)
|
---|
698 | {
|
---|
699 | /* Install extension. */
|
---|
700 | g_pfnExtension = pfnExtension;
|
---|
701 | g_pvExtension = pvExtension;
|
---|
702 |
|
---|
703 | parms.pvData = (void *)extCallback;
|
---|
704 | g_pfnExtension (g_pvExtension, VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK, &parms, sizeof (parms));
|
---|
705 | }
|
---|
706 | else
|
---|
707 | {
|
---|
708 | if (g_pfnExtension)
|
---|
709 | {
|
---|
710 | parms.pvData = NULL;
|
---|
711 | g_pfnExtension (g_pvExtension, VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK, &parms, sizeof (parms));
|
---|
712 | }
|
---|
713 |
|
---|
714 | /* Uninstall extension. */
|
---|
715 | g_pfnExtension = NULL;
|
---|
716 | g_pvExtension = NULL;
|
---|
717 | }
|
---|
718 |
|
---|
719 | return VINF_SUCCESS;
|
---|
720 | }
|
---|
721 |
|
---|
722 | extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad (VBOXHGCMSVCFNTABLE *ptable)
|
---|
723 | {
|
---|
724 | int rc = VINF_SUCCESS;
|
---|
725 |
|
---|
726 | LogFlowFunc(("ptable = %p\n", ptable));
|
---|
727 |
|
---|
728 | if (!ptable)
|
---|
729 | {
|
---|
730 | rc = VERR_INVALID_PARAMETER;
|
---|
731 | }
|
---|
732 | else
|
---|
733 | {
|
---|
734 | Log(("VBoxHGCMSvcLoad: ptable->cbSize = %d, ptable->u32Version = 0x%08X\n", ptable->cbSize, ptable->u32Version));
|
---|
735 |
|
---|
736 | if ( ptable->cbSize != sizeof (VBOXHGCMSVCFNTABLE)
|
---|
737 | || ptable->u32Version != VBOX_HGCM_SVC_VERSION)
|
---|
738 | {
|
---|
739 | rc = VERR_INVALID_PARAMETER;
|
---|
740 | }
|
---|
741 | else
|
---|
742 | {
|
---|
743 | g_pHelpers = ptable->pHelpers;
|
---|
744 |
|
---|
745 | ptable->cbClient = sizeof (VBOXCLIPBOARDCLIENTDATA);
|
---|
746 |
|
---|
747 | ptable->pfnUnload = svcUnload;
|
---|
748 | ptable->pfnConnect = svcConnect;
|
---|
749 | ptable->pfnDisconnect = svcDisconnect;
|
---|
750 | ptable->pfnCall = svcCall;
|
---|
751 | ptable->pfnHostCall = svcHostCall;
|
---|
752 | ptable->pfnSaveState = svcSaveState;
|
---|
753 | ptable->pfnLoadState = svcLoadState;
|
---|
754 | ptable->pfnRegisterExtension = svcRegisterExtension;
|
---|
755 |
|
---|
756 | /* Service specific initialization. */
|
---|
757 | rc = svcInit ();
|
---|
758 | }
|
---|
759 | }
|
---|
760 |
|
---|
761 | return rc;
|
---|
762 | }
|
---|