VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxClipboard.cpp@ 82527

Last change on this file since 82527 was 82527, checked in by vboxsync, 5 years ago

SharedClipboardSvc,Vbgl: Worked over the host messages, taking locks when needed, not using C++ lists and a bunch of other simplifications. bugref:9437

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 40.0 KB
Line 
1/* $Id: VBoxClipboard.cpp 82527 2019-12-10 01:46:40Z vboxsync $ */
2/** @file
3 * VBoxClipboard - Shared clipboard, Windows Guest Implementation.
4 */
5
6/*
7 * Copyright (C) 2006-2019 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/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
23#include <VBox/log.h>
24
25#include "VBoxTray.h"
26#include "VBoxHelpers.h"
27
28#include <iprt/asm.h>
29#include <iprt/errcore.h>
30#include <iprt/mem.h>
31#include <iprt/ldr.h>
32
33
34#include <VBox/GuestHost/SharedClipboard.h>
35#include <VBox/GuestHost/SharedClipboard-win.h>
36#include <VBox/GuestHost/clipboard-helper.h>
37#include <VBox/HostServices/VBoxClipboardSvc.h> /* Temp, remove. */
38#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
39# include <VBox/GuestHost/SharedClipboard-transfers.h>
40#endif
41
42#include <strsafe.h>
43
44#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
45# include <iprt/win/shlobj.h>
46# include <iprt/win/shlwapi.h>
47#endif
48
49
50/*********************************************************************************************************************************
51* Structures and Typedefs *
52*********************************************************************************************************************************/
53struct SHCLCONTEXT
54{
55 /** Pointer to the VBoxClient service environment. */
56 const VBOXSERVICEENV *pEnv;
57 /** Command context. */
58 VBGLR3SHCLCMDCTX CmdCtx;
59 /** Windows-specific context data. */
60 SHCLWINCTX Win;
61 /** Thread handle for window thread. */
62 RTTHREAD hThread;
63 /** Start indicator flag. */
64 bool fStarted;
65 /** Shutdown indicator flag. */
66 bool fShutdown;
67#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
68 /** Associated transfer data. */
69 SHCLTRANSFERCTX TransferCtx;
70#endif
71};
72
73
74/*********************************************************************************************************************************
75* Static variables *
76*********************************************************************************************************************************/
77/** Static clipboard context (since it is the single instance). Directly used in the windows proc. */
78static SHCLCONTEXT g_Ctx = { NULL };
79/** Static window class name. */
80static char s_szClipWndClassName[] = SHCL_WIN_WNDCLASS_NAME;
81
82
83/*********************************************************************************************************************************
84* Prototypes *
85*********************************************************************************************************************************/
86#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
87static DECLCALLBACK(int) vboxClipboardOnTransferInitCallback(PSHCLTRANSFERCALLBACKDATA pData);
88static DECLCALLBACK(int) vboxClipboardOnTransferStartCallback(PSHCLTRANSFERCALLBACKDATA pData);
89static DECLCALLBACK(void) vboxClipboardOnTransferCompleteCallback(PSHCLTRANSFERCALLBACKDATA pData, int rc);
90static DECLCALLBACK(void) vboxClipboardOnTransferErrorCallback(PSHCLTRANSFERCALLBACKDATA pData, int rc);
91#endif
92
93
94#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
95
96/**
97 * Cleanup helper function for transfer callbacks.
98 *
99 * @param pData Callback data to cleanup.
100 */
101static void vboxClipboardTransferCallbackCleanup(PSHCLTRANSFERCALLBACKDATA pData)
102{
103 LogFlowFuncEnter();
104
105 PSHCLTRANSFERCTX pCtx = (PSHCLTRANSFERCTX)pData->pvUser;
106 AssertPtr(pCtx);
107
108 PSHCLTRANSFER pTransfer = pData->pTransfer;
109 AssertPtr(pTransfer);
110
111 if (pTransfer->pvUser) /* SharedClipboardWinTransferCtx */
112 {
113 delete pTransfer->pvUser;
114 pTransfer->pvUser = NULL;
115 }
116
117 int rc2 = ShClTransferCtxTransferUnregister(pCtx, pTransfer->State.uID);
118 AssertRC(rc2);
119
120 ShClTransferDestroy(pTransfer);
121
122 RTMemFree(pTransfer);
123 pTransfer = NULL;
124}
125
126static DECLCALLBACK(int) vboxClipboardOnTransferInitCallback(PSHCLTRANSFERCALLBACKDATA pData)
127{
128 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pData->pvUser;
129 AssertPtr(pCtx);
130
131 LogFlowFunc(("pCtx=%p\n", pCtx));
132
133 RT_NOREF(pData, pCtx);
134
135 return VINF_SUCCESS;
136}
137
138static DECLCALLBACK(int) vboxClipboardOnTransferStartCallback(PSHCLTRANSFERCALLBACKDATA pData)
139{
140 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pData->pvUser;
141 AssertPtr(pCtx);
142 Assert(pData->cbUser == sizeof(SHCLCONTEXT));
143
144 PSHCLTRANSFER pTransfer = pData->pTransfer;
145 AssertPtr(pTransfer);
146
147 const SHCLTRANSFERDIR enmDir = ShClTransferGetDir(pTransfer);
148
149 LogFlowFunc(("pCtx=%p, idTransfer=%RU16, enmDir=%RU32\n", pCtx, ShClTransferGetID(pTransfer), enmDir));
150
151 int rc;
152
153 /* The guest wants to write local data to the host? */
154 if (enmDir == SHCLTRANSFERDIR_TO_REMOTE)
155 {
156 rc = SharedClipboardWinGetRoots(&pCtx->Win, pTransfer);
157 }
158 /* The guest wants to read data from a remote source. */
159 else if (enmDir == SHCLTRANSFERDIR_FROM_REMOTE)
160 {
161 /* The IDataObject *must* be created on the same thread as our (proxy) window, so post a message to it
162 * to do the stuff for us. */
163 const SHCLEVENTID uEvent = ShClEventIDGenerate(&pTransfer->Events);
164
165 rc = ShClEventRegister(&pTransfer->Events, uEvent);
166 if (RT_SUCCESS(rc))
167 {
168 /* Don't want to rely on SendMessage (synchronous) here, so just post and wait the event getting signalled. */
169 ::PostMessage(pCtx->Win.hWnd, SHCL_WIN_WM_TRANSFER_START, (WPARAM)pTransfer, (LPARAM)uEvent);
170
171 PSHCLEVENTPAYLOAD pPayload;
172 rc = ShClEventWait(&pTransfer->Events, uEvent, 30 * 1000 /* Timeout in ms */, &pPayload);
173 if (RT_SUCCESS(rc))
174 {
175 Assert(pPayload->cbData == sizeof(int));
176 rc = *(int *)pPayload->pvData;
177
178 ShClPayloadFree(pPayload);
179 }
180
181 ShClEventUnregister(&pTransfer->Events, uEvent);
182 }
183 }
184 else
185 AssertFailedStmt(rc = VERR_NOT_SUPPORTED);
186
187 if (RT_FAILURE(rc))
188 LogRel(("Shared Clipboard: Starting transfer failed, rc=%Rrc\n", rc));
189
190 LogFlowFunc(("LEAVE: idTransfer=%RU16, rc=%Rrc\n", ShClTransferGetID(pTransfer), rc));
191 return rc;
192}
193
194static DECLCALLBACK(void) vboxClipboardOnTransferCompleteCallback(PSHCLTRANSFERCALLBACKDATA pData, int rc)
195{
196 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pData->pvUser;
197 AssertPtr(pCtx);
198
199 RT_NOREF(pCtx, rc);
200
201 LogFlowFunc(("pCtx=%p, rc=%Rrc\n", pCtx, rc));
202
203 LogRel2(("Shared Clipboard: Transfer to destination complete\n"));
204
205 vboxClipboardTransferCallbackCleanup(pData);
206}
207
208static DECLCALLBACK(void) vboxClipboardOnTransferErrorCallback(PSHCLTRANSFERCALLBACKDATA pData, int rc)
209{
210 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pData->pvUser;
211 AssertPtr(pCtx);
212
213 RT_NOREF(pCtx, rc);
214
215 LogFlowFunc(("pCtx=%p, rc=%Rrc\n", pCtx, rc));
216
217 LogRel(("Shared Clipboard: Transfer to destination failed with %Rrc\n", rc));
218
219 vboxClipboardTransferCallbackCleanup(pData);
220}
221
222#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
223
224static LRESULT vboxClipboardWinProcessMsg(PSHCLCONTEXT pCtx, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
225{
226 AssertPtr(pCtx);
227
228 const PSHCLWINCTX pWinCtx = &pCtx->Win;
229
230 LRESULT lresultRc = 0;
231
232 switch (msg)
233 {
234 case WM_CLIPBOARDUPDATE:
235 {
236 LogFunc(("WM_CLIPBOARDUPDATE: pWinCtx=%p\n", pWinCtx));
237
238 if (pCtx->fShutdown) /* If we're about to shut down, skip handling stuff here. */
239 break;
240
241 int rc = RTCritSectEnter(&pWinCtx->CritSect);
242 if (RT_SUCCESS(rc))
243 {
244 const HWND hWndClipboardOwner = GetClipboardOwner();
245
246 LogFunc(("WM_CLIPBOARDUPDATE: hWndOldClipboardOwner=%p, hWndNewClipboardOwner=%p\n",
247 pWinCtx->hWndClipboardOwnerUs, hWndClipboardOwner));
248
249 if (pWinCtx->hWndClipboardOwnerUs != hWndClipboardOwner)
250 {
251 int rc2 = RTCritSectLeave(&pWinCtx->CritSect);
252 AssertRC(rc2);
253
254 /* Clipboard was updated by another application.
255 * Report available formats to the host. */
256 SHCLFORMATDATA Formats;
257 int rc = SharedClipboardWinGetFormats(pWinCtx, &Formats);
258 if (RT_SUCCESS(rc))
259 {
260 LogFunc(("WM_CLIPBOARDUPDATE: Reporting formats %#x\n", Formats.Formats));
261 rc = VbglR3ClipboardReportFormats(pCtx->CmdCtx.idClient, Formats.Formats);
262 }
263 }
264 else
265 {
266 int rc2 = RTCritSectLeave(&pWinCtx->CritSect);
267 AssertRC(rc2);
268 }
269 }
270
271 if (RT_FAILURE(rc))
272 LogRel(("Shared Clipboard: WM_CLIPBOARDUPDATE failed with %Rrc\n", rc));
273
274 break;
275 }
276
277 case WM_CHANGECBCHAIN:
278 {
279 LogFunc(("WM_CHANGECBCHAIN\n"));
280 lresultRc = SharedClipboardWinHandleWMChangeCBChain(pWinCtx, hwnd, msg, wParam, lParam);
281 break;
282 }
283
284 case WM_DRAWCLIPBOARD:
285 {
286 LogFlowFunc(("WM_DRAWCLIPBOARD: pWinCtx=%p\n", pWinCtx));
287
288 int rc = RTCritSectEnter(&pWinCtx->CritSect);
289 if (RT_SUCCESS(rc))
290 {
291 const HWND hWndClipboardOwner = GetClipboardOwner();
292
293 LogFunc(("WM_DRAWCLIPBOARD: hWndClipboardOwnerUs=%p, hWndNewClipboardOwner=%p\n",
294 pWinCtx->hWndClipboardOwnerUs, hWndClipboardOwner));
295
296 if (pWinCtx->hWndClipboardOwnerUs != hWndClipboardOwner)
297 {
298 int rc2 = RTCritSectLeave(&pWinCtx->CritSect);
299 AssertRC(rc2);
300
301 /* Clipboard was updated by another application. */
302 /* WM_DRAWCLIPBOARD always expects a return code of 0, so don't change "rc" here. */
303 SHCLFORMATDATA Formats;
304 rc = SharedClipboardWinGetFormats(pWinCtx, &Formats);
305 if ( RT_SUCCESS(rc)
306 && Formats.Formats != VBOX_SHCL_FMT_NONE)
307 rc = VbglR3ClipboardReportFormats(pCtx->CmdCtx.idClient, Formats.Formats);
308 }
309 else
310 {
311 int rc2 = RTCritSectLeave(&pWinCtx->CritSect);
312 AssertRC(rc2);
313 }
314 }
315
316 lresultRc = SharedClipboardWinChainPassToNext(pWinCtx, msg, wParam, lParam);
317 break;
318 }
319
320 case WM_TIMER:
321 {
322 int rc = SharedClipboardWinHandleWMTimer(pWinCtx);
323 AssertRC(rc);
324
325 break;
326 }
327
328 case WM_CLOSE:
329 {
330 /* Do nothing. Ignore the message. */
331 break;
332 }
333
334 case WM_RENDERFORMAT:
335 {
336 LogFunc(("WM_RENDERFORMAT\n"));
337
338 /* Insert the requested clipboard format data into the clipboard. */
339 const UINT cfFormat = (UINT)wParam;
340
341 const SHCLFORMAT fFormat = SharedClipboardWinClipboardFormatToVBox(cfFormat);
342
343 LogFunc(("WM_RENDERFORMAT: cfFormat=%u -> fFormat=0x%x\n", cfFormat, fFormat));
344
345 if (fFormat == VBOX_SHCL_FMT_NONE)
346 {
347 LogFunc(("WM_RENDERFORMAT: Unsupported format requested\n"));
348 SharedClipboardWinClear();
349 }
350 else
351 {
352 const uint32_t cbPrealloc = _4K;
353 uint32_t cb = 0;
354
355 /* Preallocate a buffer, most of small text transfers will fit into it. */
356 HANDLE hMem = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, cbPrealloc);
357 if (hMem)
358 {
359 void *pMem = GlobalLock(hMem);
360 if (pMem)
361 {
362 SHCLDATABLOCK dataBlock;
363 RT_ZERO(dataBlock);
364
365 dataBlock.cbData = cbPrealloc;
366 dataBlock.pvData = pMem;
367 dataBlock.uFormat = fFormat;
368
369 /* Read the host data to the preallocated buffer. */
370 int rc = VbglR3ClipboardReadDataEx(&pCtx->CmdCtx, &dataBlock, &cb);
371 if (RT_SUCCESS(rc))
372 {
373 if (cb == 0)
374 {
375 /* 0 bytes returned means the clipboard is empty.
376 * Deallocate the memory and set hMem to NULL to get to
377 * the clipboard empty code path. */
378 GlobalUnlock(hMem);
379 GlobalFree(hMem);
380 hMem = NULL;
381 }
382 else if (cb > cbPrealloc)
383 {
384 GlobalUnlock(hMem);
385
386 LogRel2(("Shared Clipboard: Buffer too small (%RU32), needs %RU32 bytes\n", cbPrealloc, cb));
387
388 /* The preallocated buffer is too small, adjust the size. */
389 hMem = GlobalReAlloc(hMem, cb, 0);
390 if (hMem)
391 {
392 pMem = GlobalLock(hMem);
393 if (pMem)
394 {
395 dataBlock.cbData = cb;
396 dataBlock.pvData = pMem;
397
398 /* Read the host data to the preallocated buffer. */
399 uint32_t cbNew = 0;
400 rc = VbglR3ClipboardReadDataEx(&pCtx->CmdCtx, &dataBlock, &cbNew);
401 if ( RT_SUCCESS(rc)
402 && cbNew <= cb)
403 {
404 cb = cbNew;
405 }
406 else
407 {
408 LogRel(("Shared Clipboard: Receiving host data failed with %Rrc\n", rc));
409
410 GlobalUnlock(hMem);
411 GlobalFree(hMem);
412 hMem = NULL;
413 }
414 }
415 else
416 {
417 LogRel(("Shared Clipboard: Error locking reallocated host data buffer\n"));
418
419 GlobalFree(hMem);
420 hMem = NULL;
421 }
422 }
423 else
424 LogRel(("Shared Clipboard: No memory for reallocating host data buffer\n"));
425 }
426
427 if (hMem)
428 {
429 /* pMem is the address of the data. cb is the size of returned data. */
430 /* Verify the size of returned text, the memory block for clipboard
431 * must have the exact string size.
432 */
433 if (fFormat == VBOX_SHCL_FMT_UNICODETEXT)
434 {
435 size_t cbActual = 0;
436 HRESULT hrc = StringCbLengthW((LPWSTR)pMem, cb, &cbActual);
437 if (FAILED(hrc))
438 {
439 LogRel(("Shared Clipboard: Received host data is invalid (%RU32 vs. %zu)\n",
440 cb, cbActual));
441
442 /* Discard invalid data. */
443 GlobalUnlock(hMem);
444 GlobalFree(hMem);
445 hMem = NULL;
446 }
447 else
448 {
449 /* cbActual is the number of bytes, excluding those used
450 * for the terminating null character.
451 */
452 cb = (uint32_t)(cbActual + 2);
453 }
454 }
455 }
456
457 if (hMem)
458 {
459 GlobalUnlock(hMem);
460
461 hMem = GlobalReAlloc(hMem, cb, 0);
462 if (hMem)
463 {
464 /* 'hMem' contains the host clipboard data.
465 * size is 'cb' and format is 'format'. */
466 HANDLE hClip = SetClipboardData(cfFormat, hMem);
467 if (hClip)
468 {
469 /* The hMem ownership has gone to the system. Finish the processing. */
470 break;
471 }
472 else
473 LogRel(("Shared Clipboard: Setting host data buffer to clipboard failed with %ld\n",
474 GetLastError()));
475
476 /* Cleanup follows. */
477 }
478 else
479 LogRel(("Shared Clipboard: No memory for allocating final host data buffer\n"));
480 }
481 }
482
483 if (hMem)
484 GlobalUnlock(hMem);
485 }
486 else
487 LogRel(("Shared Clipboard: No memory for allocating host data buffer\n"));
488
489 if (hMem)
490 GlobalFree(hMem);
491 }
492 }
493
494 break;
495 }
496
497 case WM_RENDERALLFORMATS:
498 {
499 LogFunc(("WM_RENDERALLFORMATS\n"));
500
501 int rc = SharedClipboardWinHandleWMRenderAllFormats(pWinCtx, hwnd);
502 AssertRC(rc);
503
504 break;
505 }
506
507 case SHCL_WIN_WM_REPORT_FORMATS:
508 {
509 LogFunc(("SHCL_WIN_WM_REPORT_FORMATS\n"));
510
511 /* Announce available formats. Do not insert data -- will be inserted in WM_RENDERFORMAT. */
512 PVBGLR3CLIPBOARDEVENT pEvent = (PVBGLR3CLIPBOARDEVENT)lParam;
513 AssertPtr(pEvent);
514 Assert(pEvent->enmType == VBGLR3CLIPBOARDEVENTTYPE_REPORT_FORMATS);
515
516 const SHCLFORMATS fFormats = pEvent->u.ReportedFormats.Formats;
517
518 if (fFormats != VBOX_SHCL_FMT_NONE) /* Could arrive with some older GA versions. */
519 {
520#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
521 if (fFormats & VBOX_SHCL_FMT_URI_LIST)
522 {
523 LogFunc(("VBOX_SHCL_FMT_URI_LIST\n"));
524
525 /*
526 * Creating and starting the actual transfer will be done in vbglR3ClipboardTransferStart() as soon
527 * as the host announces the start of the transfer via a VBOX_SHCL_HOST_MSG_TRANSFER_STATUS message.
528 * Transfers always are controlled and initiated on the host side!
529 *
530 * So don't announce the transfer to the OS here yet. Don't touch the clipboard in any here; otherwise
531 * this will trigger a WM_DRAWCLIPBOARD or friends, which will result in fun bugs coming up.
532 */
533 }
534 else
535 {
536#endif
537 int rc = SharedClipboardWinOpen(hwnd);
538 if (RT_SUCCESS(rc))
539 {
540 SharedClipboardWinClear();
541
542 rc = SharedClipboardWinAnnounceFormats(pWinCtx, fFormats);
543 }
544
545 SharedClipboardWinClose();
546
547#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
548 }
549#endif
550 }
551
552 LogFunc(("SHCL_WIN_WM_REPORT_FORMATS: fFormats=0x%x, lastErr=%ld\n", fFormats, GetLastError()));
553 break;
554 }
555
556 case SHCL_WIN_WM_READ_DATA:
557 {
558 /* Send data in the specified format to the host. */
559 PVBGLR3CLIPBOARDEVENT pEvent = (PVBGLR3CLIPBOARDEVENT)lParam;
560 AssertPtr(pEvent);
561 Assert(pEvent->enmType == VBGLR3CLIPBOARDEVENTTYPE_READ_DATA);
562
563 const SHCLFORMAT fFormat = (uint32_t)pEvent->u.fReadData;
564
565 HANDLE hClip = NULL;
566
567 LogFlowFunc(("SHCL_WIN_WM_READ_DATA: fFormat=%#x\n", fFormat));
568
569 int rc = SharedClipboardWinOpen(hwnd);
570 if (RT_SUCCESS(rc))
571 {
572 if (fFormat & VBOX_SHCL_FMT_BITMAP)
573 {
574 hClip = GetClipboardData(CF_DIB);
575 if (hClip != NULL)
576 {
577 LPVOID lp = GlobalLock(hClip);
578 if (lp != NULL)
579 {
580 SHCLDATABLOCK dataBlock;
581 dataBlock.uFormat = fFormat;
582 dataBlock.pvData = lp;
583 dataBlock.cbData = (uint32_t)GlobalSize(hClip);
584
585 rc = VbglR3ClipboardWriteDataEx(&pEvent->cmdCtx, &dataBlock);
586
587 GlobalUnlock(hClip);
588 }
589 else
590 {
591 hClip = NULL;
592 }
593 }
594 }
595 else if (fFormat & VBOX_SHCL_FMT_UNICODETEXT)
596 {
597 hClip = GetClipboardData(CF_UNICODETEXT);
598 if (hClip != NULL)
599 {
600 LPWSTR uniString = (LPWSTR)GlobalLock(hClip);
601 if (uniString != NULL)
602 {
603 SHCLDATABLOCK dataBlock;
604 dataBlock.uFormat = fFormat;
605 dataBlock.pvData = uniString;
606 dataBlock.cbData = ((uint32_t)lstrlenW(uniString) + 1) * 2;
607
608 rc = VbglR3ClipboardWriteDataEx(&pEvent->cmdCtx, &dataBlock);
609
610 GlobalUnlock(hClip);
611 }
612 else
613 {
614 hClip = NULL;
615 }
616 }
617 }
618 else if (fFormat & VBOX_SHCL_FMT_HTML)
619 {
620 UINT format = RegisterClipboardFormat(SHCL_WIN_REGFMT_HTML);
621 if (format != 0)
622 {
623 hClip = GetClipboardData(format);
624 if (hClip != NULL)
625 {
626 LPVOID lp = GlobalLock(hClip);
627
628 if (lp != NULL)
629 {
630 SHCLDATABLOCK dataBlock;
631 dataBlock.uFormat = fFormat;
632 dataBlock.pvData = lp;
633 dataBlock.cbData = (uint32_t)GlobalSize(hClip);
634
635 rc = VbglR3ClipboardWriteDataEx(&pEvent->cmdCtx, &dataBlock);
636
637 GlobalUnlock(hClip);
638 }
639 else
640 {
641 hClip = NULL;
642 }
643 }
644 }
645 }
646 if (hClip == NULL)
647 {
648 LogFunc(("SHCL_WIN_WM_READ_DATA: hClip=NULL, lastError=%ld\n", GetLastError()));
649
650 /* Requested clipboard format is not available, send empty data. */
651 VbglR3ClipboardWriteData(pCtx->CmdCtx.idClient, VBOX_SHCL_FMT_NONE, NULL, 0);
652 }
653
654 SharedClipboardWinClose();
655 }
656 break;
657 }
658
659#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
660 case SHCL_WIN_WM_TRANSFER_START:
661 {
662 LogFunc(("SHCL_WIN_WM_TRANSFER_START\n"));
663
664 PSHCLTRANSFER pTransfer = (PSHCLTRANSFER)wParam;
665 AssertPtr(pTransfer);
666
667 const SHCLEVENTID uEvent = (SHCLEVENTID)lParam;
668
669 Assert(ShClTransferGetSource(pTransfer) == SHCLSOURCE_REMOTE); /* Sanity. */
670
671 int rcTransfer = SharedClipboardWinTransferCreate(pWinCtx, pTransfer);
672
673 PSHCLEVENTPAYLOAD pPayload = NULL;
674 int rc = ShClPayloadAlloc(uEvent, &rcTransfer, sizeof(rcTransfer), &pPayload);
675 if (RT_SUCCESS(rc))
676 {
677 rc = ShClEventSignal(&pTransfer->Events, uEvent, pPayload);
678 if (RT_FAILURE(rc))
679 ShClPayloadFree(pPayload);
680 }
681
682 break;
683 }
684#endif
685 case WM_DESTROY:
686 {
687 LogFunc(("WM_DESTROY\n"));
688
689 int rc = SharedClipboardWinHandleWMDestroy(pWinCtx);
690 AssertRC(rc);
691
692 /*
693 * Don't need to call PostQuitMessage cause
694 * the VBoxTray already finished a message loop.
695 */
696
697 break;
698 }
699
700 default:
701 {
702 LogFunc(("WM_ %p\n", msg));
703 lresultRc = DefWindowProc(hwnd, msg, wParam, lParam);
704 break;
705 }
706 }
707
708 LogFunc(("WM_ rc %d\n", lresultRc));
709 return lresultRc;
710}
711
712static LRESULT CALLBACK vboxClipboardWinWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
713
714static int vboxClipboardCreateWindow(PSHCLCONTEXT pCtx)
715{
716 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
717
718 int rc = VINF_SUCCESS;
719
720 AssertPtr(pCtx->pEnv);
721 HINSTANCE hInstance = pCtx->pEnv->hInstance;
722 Assert(hInstance != 0);
723
724 /* Register the Window Class. */
725 WNDCLASSEX wc;
726 RT_ZERO(wc);
727
728 wc.cbSize = sizeof(WNDCLASSEX);
729
730 if (!GetClassInfoEx(hInstance, s_szClipWndClassName, &wc))
731 {
732 wc.style = CS_NOCLOSE;
733 wc.lpfnWndProc = vboxClipboardWinWndProc;
734 wc.hInstance = pCtx->pEnv->hInstance;
735 wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
736 wc.lpszClassName = s_szClipWndClassName;
737
738 ATOM wndClass = RegisterClassEx(&wc);
739 if (wndClass == 0)
740 rc = RTErrConvertFromWin32(GetLastError());
741 }
742
743 if (RT_SUCCESS(rc))
744 {
745 const PSHCLWINCTX pWinCtx = &pCtx->Win;
746
747 /* Create the window. */
748 pWinCtx->hWnd = CreateWindowEx(WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
749 s_szClipWndClassName, s_szClipWndClassName,
750 WS_POPUPWINDOW,
751 -200, -200, 100, 100, NULL, NULL, hInstance, NULL);
752 if (pWinCtx->hWnd == NULL)
753 {
754 rc = VERR_NOT_SUPPORTED;
755 }
756 else
757 {
758 SetWindowPos(pWinCtx->hWnd, HWND_TOPMOST, -200, -200, 0, 0,
759 SWP_NOACTIVATE | SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_NOSIZE);
760
761 rc = SharedClipboardWinChainAdd(pWinCtx);
762 if (RT_SUCCESS(rc))
763 {
764 if (!SharedClipboardWinIsNewAPI(&pWinCtx->newAPI))
765 pWinCtx->oldAPI.timerRefresh = SetTimer(pWinCtx->hWnd, 0, 10 * 1000 /* 10s */, NULL);
766 }
767 }
768 }
769
770 LogFlowFuncLeaveRC(rc);
771 return rc;
772}
773
774static int vboxClipboardWindowThread(RTTHREAD hThread, void *pvUser)
775{
776 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pvUser;
777 AssertPtr(pCtx);
778
779#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
780 HRESULT hr = OleInitialize(NULL);
781 if (FAILED(hr))
782 {
783 LogRel(("Shared Clipboard: Initializing OLE in window thread failed (%Rhrc) -- file transfers unavailable\n", hr));
784 /* Not critical, the rest of the clipboard might work. */
785 }
786 else
787 LogRel(("Shared Clipboard: Initialized OLE in window thread\n"));
788#endif
789
790 int rc = vboxClipboardCreateWindow(pCtx);
791 if (RT_FAILURE(rc))
792 {
793 LogRel(("Shared Clipboard: Unable to create window, rc=%Rrc\n", rc));
794 return rc;
795 }
796
797 pCtx->fStarted = true; /* Set started indicator. */
798
799 int rc2 = RTThreadUserSignal(hThread);
800 bool fSignalled = RT_SUCCESS(rc2);
801
802 LogRel2(("Shared Clipboard: Window thread running\n"));
803
804 if (RT_SUCCESS(rc))
805 {
806 for (;;)
807 {
808 MSG uMsg;
809 BOOL fRet;
810 while ((fRet = GetMessage(&uMsg, 0, 0, 0)) > 0)
811 {
812 TranslateMessage(&uMsg);
813 DispatchMessage(&uMsg);
814 }
815 Assert(fRet >= 0);
816
817 if (ASMAtomicReadBool(&pCtx->fShutdown))
818 break;
819
820 /** @todo Immediately drop on failure? */
821 }
822 }
823
824 if (!fSignalled)
825 {
826 rc2 = RTThreadUserSignal(hThread);
827 AssertRC(rc2);
828 }
829
830#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
831 OleSetClipboard(NULL); /* Make sure to flush the clipboard on destruction. */
832 OleUninitialize();
833#endif
834
835 LogRel(("Shared Clipboard: Window thread ended\n"));
836
837 LogFlowFuncLeaveRC(rc);
838 return rc;
839}
840
841static void vboxClipboardDestroy(PSHCLCONTEXT pCtx)
842{
843 AssertPtrReturnVoid(pCtx);
844
845 LogFlowFunc(("pCtx=%p\n", pCtx));
846
847 LogRel2(("Shared Clipboard: Destroying ...\n"));
848
849 const PSHCLWINCTX pWinCtx = &pCtx->Win;
850
851 if (pCtx->hThread != NIL_RTTHREAD)
852 {
853 int rcThread = VERR_WRONG_ORDER;
854 int rc = RTThreadWait(pCtx->hThread, 60 * 1000 /* Timeout in ms */, &rcThread);
855 LogFlowFunc(("Waiting for thread resulted in %Rrc (thread exited with %Rrc)\n",
856 rc, rcThread));
857 RT_NOREF(rc);
858 }
859
860 if (pWinCtx->hWnd)
861 {
862 DestroyWindow(pWinCtx->hWnd);
863 pWinCtx->hWnd = NULL;
864 }
865
866 UnregisterClass(s_szClipWndClassName, pCtx->pEnv->hInstance);
867
868 SharedClipboardWinCtxDestroy(&pCtx->Win);
869
870 LogRel2(("Shared Clipboard: Destroyed\n"));
871}
872
873static LRESULT CALLBACK vboxClipboardWinWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
874{
875 PSHCLCONTEXT pCtx = &g_Ctx; /** @todo r=andy Make pCtx available through SetWindowLongPtr() / GWL_USERDATA. */
876 AssertPtr(pCtx);
877
878 /* Forward with proper context. */
879 return vboxClipboardWinProcessMsg(pCtx, hWnd, uMsg, wParam, lParam);
880}
881
882DECLCALLBACK(int) VBoxShClInit(const PVBOXSERVICEENV pEnv, void **ppInstance)
883{
884 LogFlowFuncEnter();
885
886 PSHCLCONTEXT pCtx = &g_Ctx; /* Only one instance for now. */
887 AssertPtr(pCtx);
888
889 if (pCtx->pEnv)
890 {
891 /* Clipboard was already initialized. 2 or more instances are not supported. */
892 return VERR_NOT_SUPPORTED;
893 }
894
895 if (VbglR3AutoLogonIsRemoteSession())
896 {
897 /* Do not use clipboard for remote sessions. */
898 LogRel(("Shared Clipboard: Clipboard has been disabled for a remote session\n"));
899 return VERR_NOT_SUPPORTED;
900 }
901
902 pCtx->pEnv = pEnv;
903 pCtx->hThread = NIL_RTTHREAD;
904 pCtx->fStarted = false;
905 pCtx->fShutdown = false;
906
907 int rc = VINF_SUCCESS;
908
909#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
910 /* Install callbacks. */
911 RT_ZERO(pCtx->CmdCtx.Transfers.Callbacks);
912
913 pCtx->CmdCtx.Transfers.Callbacks.pvUser = pCtx; /* Assign context as user-provided callback data. */
914 pCtx->CmdCtx.Transfers.Callbacks.cbUser = sizeof(SHCLCONTEXT);
915
916 pCtx->CmdCtx.Transfers.Callbacks.pfnTransferInitialize = vboxClipboardOnTransferInitCallback;
917 pCtx->CmdCtx.Transfers.Callbacks.pfnTransferStart = vboxClipboardOnTransferStartCallback;
918 pCtx->CmdCtx.Transfers.Callbacks.pfnTransferComplete = vboxClipboardOnTransferCompleteCallback;
919 pCtx->CmdCtx.Transfers.Callbacks.pfnTransferError = vboxClipboardOnTransferErrorCallback;
920#endif
921
922 if (RT_SUCCESS(rc))
923 {
924 rc = SharedClipboardWinCtxInit(&pCtx->Win);
925 if (RT_SUCCESS(rc))
926 rc = VbglR3ClipboardConnectEx(&pCtx->CmdCtx, VBOX_SHCL_GF_0_CONTEXT_ID);
927 if (RT_SUCCESS(rc))
928 {
929#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
930 rc = ShClTransferCtxInit(&pCtx->TransferCtx);
931#endif
932 if (RT_SUCCESS(rc))
933 {
934 /* Message pump thread for our proxy window. */
935 rc = RTThreadCreate(&pCtx->hThread, vboxClipboardWindowThread, pCtx /* pvUser */,
936 0, RTTHREADTYPE_MSG_PUMP, RTTHREADFLAGS_WAITABLE,
937 "shclwnd");
938 if (RT_SUCCESS(rc))
939 {
940 int rc2 = RTThreadUserWait(pCtx->hThread, 30 * 1000 /* Timeout in ms */);
941 AssertRC(rc2);
942
943 if (!pCtx->fStarted) /* Did the thread fail to start? */
944 rc = VERR_NOT_SUPPORTED; /* Report back Shared Clipboard as not being supported. */
945 }
946 }
947
948 if (RT_SUCCESS(rc))
949 {
950 *ppInstance = pCtx;
951 }
952 else
953 VbglR3ClipboardDisconnectEx(&pCtx->CmdCtx);
954 }
955 }
956
957 if (RT_FAILURE(rc))
958 LogRel(("Shared Clipboard: Unable to initialize, rc=%Rrc\n", rc));
959
960 LogFlowFuncLeaveRC(rc);
961 return rc;
962}
963
964DECLCALLBACK(int) VBoxShClWorker(void *pInstance, bool volatile *pfShutdown)
965{
966 AssertPtr(pInstance);
967 LogFlowFunc(("pInstance=%p\n", pInstance));
968
969 /*
970 * Tell the control thread that it can continue
971 * spawning services.
972 */
973 RTThreadUserSignal(RTThreadSelf());
974
975 const PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pInstance;
976 AssertPtr(pCtx);
977
978 const PSHCLWINCTX pWinCtx = &pCtx->Win;
979
980 LogRel2(("Shared Clipboard: Worker loop running\n"));
981
982#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
983 HRESULT hr = OleInitialize(NULL);
984 if (FAILED(hr))
985 {
986 LogRel(("Shared Clipboard: Initializing OLE in worker thread failed (%Rhrc) -- file transfers unavailable\n", hr));
987 /* Not critical, the rest of the clipboard might work. */
988 }
989 else
990 LogRel(("Shared Clipboard: Initialized OLE in worker thraed\n"));
991#endif
992
993 int rc;
994
995 /* The thread waits for incoming messages from the host. */
996 for (;;)
997 {
998 LogFlowFunc(("Waiting for host message (fUseLegacyProtocol=%RTbool, fHostFeatures=%#RX64) ...\n",
999 pCtx->CmdCtx.fUseLegacyProtocol, pCtx->CmdCtx.fHostFeatures));
1000
1001 PVBGLR3CLIPBOARDEVENT pEvent = (PVBGLR3CLIPBOARDEVENT)RTMemAllocZ(sizeof(VBGLR3CLIPBOARDEVENT));
1002 AssertPtrBreakStmt(pEvent, rc = VERR_NO_MEMORY);
1003
1004 uint32_t idMsg = 0;
1005 uint32_t cParms = 0;
1006 rc = VbglR3ClipboardMsgPeekWait(&pCtx->CmdCtx, &idMsg, &cParms, NULL /* pidRestoreCheck */);
1007 if (RT_SUCCESS(rc))
1008 {
1009#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1010 rc = VbglR3ClipboardEventGetNextEx(idMsg, cParms, &pCtx->CmdCtx, &pCtx->TransferCtx, pEvent);
1011#else
1012 rc = VbglR3ClipboardEventGetNext(idMsg, cParms, &pCtx->CmdCtx, pEvent);
1013#endif
1014 }
1015
1016 if (RT_FAILURE(rc))
1017 {
1018 LogFlowFunc(("Getting next event failed with %Rrc\n", rc));
1019
1020 VbglR3ClipboardEventFree(pEvent);
1021 pEvent = NULL;
1022
1023 if (*pfShutdown)
1024 break;
1025
1026 /* Wait a bit before retrying. */
1027 RTThreadSleep(1000);
1028 continue;
1029 }
1030 else
1031 {
1032 AssertPtr(pEvent);
1033 LogFlowFunc(("Event uType=%RU32\n", pEvent->enmType));
1034
1035 switch (pEvent->enmType)
1036 {
1037 case VBGLR3CLIPBOARDEVENTTYPE_REPORT_FORMATS:
1038 {
1039 /* The host has announced available clipboard formats.
1040 * Forward the information to the window, so it can later
1041 * respond to WM_RENDERFORMAT message. */
1042 ::PostMessage(pWinCtx->hWnd, SHCL_WIN_WM_REPORT_FORMATS,
1043 0 /* wParam */, (LPARAM)pEvent /* lParam */);
1044
1045 pEvent = NULL; /* Consume pointer. */
1046 break;
1047 }
1048
1049 case VBGLR3CLIPBOARDEVENTTYPE_READ_DATA:
1050 {
1051 /* The host needs data in the specified format. */
1052 ::PostMessage(pWinCtx->hWnd, SHCL_WIN_WM_READ_DATA,
1053 0 /* wParam */, (LPARAM)pEvent /* lParam */);
1054
1055 pEvent = NULL; /* Consume pointer. */
1056 break;
1057 }
1058
1059 case VBGLR3CLIPBOARDEVENTTYPE_QUIT:
1060 {
1061 LogRel2(("Shared Clipboard: Host requested termination\n"));
1062 ASMAtomicXchgBool(pfShutdown, true);
1063 break;
1064 }
1065
1066#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1067 case VBGLR3CLIPBOARDEVENTTYPE_TRANSFER_STATUS:
1068 {
1069 /* Nothing to do here. */
1070 rc = VINF_SUCCESS;
1071 break;
1072 }
1073#endif
1074 case VBGLR3CLIPBOARDEVENTTYPE_NONE:
1075 {
1076 /* Nothing to do here. */
1077 rc = VINF_SUCCESS;
1078 break;
1079 }
1080
1081 default:
1082 {
1083 AssertMsgFailedBreakStmt(("Event type %RU32 not implemented\n", pEvent->enmType), rc = VERR_NOT_SUPPORTED);
1084 }
1085 }
1086
1087 if (pEvent)
1088 {
1089 VbglR3ClipboardEventFree(pEvent);
1090 pEvent = NULL;
1091 }
1092 }
1093
1094 if (*pfShutdown)
1095 break;
1096 }
1097
1098 LogRel2(("Shared Clipboard: Worker loop ended\n"));
1099
1100#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1101 OleSetClipboard(NULL); /* Make sure to flush the clipboard on destruction. */
1102 OleUninitialize();
1103#endif
1104
1105 LogFlowFuncLeaveRC(rc);
1106 return rc;
1107}
1108
1109DECLCALLBACK(int) VBoxShClStop(void *pInstance)
1110{
1111 AssertPtrReturn(pInstance, VERR_INVALID_POINTER);
1112
1113 LogFunc(("Stopping pInstance=%p\n", pInstance));
1114
1115 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pInstance;
1116 AssertPtr(pCtx);
1117
1118 /* Set shutdown indicator. */
1119 ASMAtomicWriteBool(&pCtx->fShutdown, true);
1120
1121 /* Let our clipboard know that we're going to shut down. */
1122 PostMessage(pCtx->Win.hWnd, WM_QUIT, 0, 0);
1123
1124 /* Disconnect from the host service.
1125 * This will also send a VBOX_SHCL_HOST_MSG_QUIT from the host so that we can break out from our message worker. */
1126 VbglR3ClipboardDisconnect(pCtx->CmdCtx.idClient);
1127 pCtx->CmdCtx.idClient = 0;
1128
1129 LogFlowFuncLeaveRC(VINF_SUCCESS);
1130 return VINF_SUCCESS;
1131}
1132
1133DECLCALLBACK(void) VBoxShClDestroy(void *pInstance)
1134{
1135 AssertPtrReturnVoid(pInstance);
1136
1137 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pInstance;
1138 AssertPtr(pCtx);
1139
1140 /* Make sure that we are disconnected. */
1141 Assert(pCtx->CmdCtx.idClient == 0);
1142
1143 vboxClipboardDestroy(pCtx);
1144
1145#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1146 ShClTransferCtxDestroy(&pCtx->TransferCtx);
1147#endif
1148
1149 return;
1150}
1151
1152/**
1153 * The service description.
1154 */
1155VBOXSERVICEDESC g_SvcDescClipboard =
1156{
1157 /* pszName. */
1158 "clipboard",
1159 /* pszDescription. */
1160 "Shared Clipboard",
1161 /* methods */
1162 VBoxShClInit,
1163 VBoxShClWorker,
1164 VBoxShClStop,
1165 VBoxShClDestroy
1166};
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