VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedClipboard/VBoxSharedClipboardSvc-win.cpp@ 91749

Last change on this file since 91749 was 91749, checked in by vboxsync, 3 years ago

VRDP: Shared Clipboard: delegate clipboard processing to VRDP service when remote RDP clients are connected, bugref:10117.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 27.6 KB
Line 
1/* $Id: VBoxSharedClipboardSvc-win.cpp 91749 2021-10-14 21:02:32Z vboxsync $ */
2/** @file
3 * Shared Clipboard Service - Win32 host.
4 */
5
6/*
7 * Copyright (C) 2006-2020 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 <iprt/win/windows.h>
24
25#include <VBox/HostServices/VBoxClipboardSvc.h>
26#include <VBox/GuestHost/clipboard-helper.h>
27#include <VBox/GuestHost/SharedClipboard-win.h>
28#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
29# include <VBox/GuestHost/SharedClipboard-transfers.h>
30#endif
31
32#include <iprt/alloc.h>
33#include <iprt/string.h>
34#include <iprt/asm.h>
35#include <iprt/assert.h>
36#include <iprt/ldr.h>
37#include <iprt/semaphore.h>
38#include <iprt/thread.h>
39#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
40# include <iprt/utf16.h>
41#endif
42
43#include <process.h>
44#include <iprt/win/shlobj.h> /* Needed for shell objects. */
45
46#include "VBoxSharedClipboardSvc-internal.h"
47#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
48# include "VBoxSharedClipboardSvc-transfers.h"
49#endif
50
51
52/*********************************************************************************************************************************
53* Internal Functions *
54*********************************************************************************************************************************/
55static int vboxClipboardSvcWinSyncInternal(PSHCLCONTEXT pCtx);
56
57struct SHCLCONTEXT
58{
59 /** Handle for window message handling thread. */
60 RTTHREAD hThread;
61 /** Structure for keeping and communicating with service client. */
62 PSHCLCLIENT pClient;
63 /** Windows-specific context data. */
64 SHCLWINCTX Win;
65};
66
67
68/** @todo Someone please explain the protocol wrt overflows... */
69static int vboxClipboardSvcWinDataGet(uint32_t u32Format, const void *pvSrc, uint32_t cbSrc,
70 void *pvDst, uint32_t cbDst, uint32_t *pcbActualDst)
71{
72 AssertPtrReturn(pvSrc, VERR_INVALID_POINTER);
73 AssertReturn (cbSrc, VERR_INVALID_PARAMETER);
74 AssertPtrReturn(pvDst, VERR_INVALID_POINTER);
75 AssertReturn (cbDst, VERR_INVALID_PARAMETER);
76 AssertPtrReturn(pcbActualDst, VERR_INVALID_POINTER);
77
78 LogFlowFunc(("cbSrc = %d, cbDst = %d\n", cbSrc, cbDst));
79
80 if ( u32Format == VBOX_SHCL_FMT_HTML
81 && SharedClipboardWinIsCFHTML((const char *)pvSrc))
82 {
83 /** @todo r=bird: Why the double conversion? */
84 char *pszBuf = NULL;
85 uint32_t cbBuf = 0;
86 int rc = SharedClipboardWinConvertCFHTMLToMIME((const char *)pvSrc, cbSrc, &pszBuf, &cbBuf);
87 if (RT_SUCCESS(rc))
88 {
89 *pcbActualDst = cbBuf;
90 if (cbBuf > cbDst)
91 {
92 /* Do not copy data. The dst buffer is not enough. */
93 RTMemFree(pszBuf);
94 return VERR_BUFFER_OVERFLOW;
95 }
96 memcpy(pvDst, pszBuf, cbBuf);
97 RTMemFree(pszBuf);
98 }
99 else
100 *pcbActualDst = 0;
101 }
102 else
103 {
104 *pcbActualDst = cbSrc; /* Tell the caller how much space we need. */
105
106 if (cbSrc > cbDst)
107 return VERR_BUFFER_OVERFLOW;
108
109 memcpy(pvDst, pvSrc, cbSrc);
110 }
111
112#ifdef LOG_ENABLED
113 ShClDbgDumpData(pvDst, cbSrc, u32Format);
114#endif
115
116 return VINF_SUCCESS;
117}
118
119/**
120 * Sets (places) clipboard data into the Windows clipboard.
121 *
122 * @returns VBox status code.
123 * @param pCtx Shared Clipboard context to use.
124 * @param cfFormat Windows clipboard format to set data for.
125 * @param pvData Pointer to actual clipboard data to set.
126 * @param cbData Size (in bytes) of actual clipboard data to set.
127 * @note
128 */
129static int vboxClipboardSvcWinDataSet(PSHCLCONTEXT pCtx, UINT cfFormat, void *pvData, uint32_t cbData)
130{
131 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
132 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
133 AssertReturn (cbData, VERR_INVALID_PARAMETER);
134
135 int rc = VINF_SUCCESS;
136
137 HANDLE hMem = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, cbData);
138
139 LogFlowFunc(("hMem=%p\n", hMem));
140
141 if (hMem)
142 {
143 void *pMem = GlobalLock(hMem);
144
145 LogFlowFunc(("pMem=%p, GlobalSize=%zu\n", pMem, GlobalSize(hMem)));
146
147 if (pMem)
148 {
149 LogFlowFunc(("Setting data\n"));
150
151 memcpy(pMem, pvData, cbData);
152
153 /* The memory must be unlocked before inserting to the Clipboard. */
154 GlobalUnlock(hMem);
155
156 /* 'hMem' contains the host clipboard data.
157 * size is 'cb' and format is 'format'.
158 */
159 HANDLE hClip = SetClipboardData(cfFormat, hMem);
160
161 LogFlowFunc(("hClip=%p\n", hClip));
162
163 if (hClip)
164 {
165 /* The hMem ownership has gone to the system. Nothing to do. */
166 }
167 else
168 rc = RTErrConvertFromWin32(GetLastError());
169 }
170 else
171 rc = VERR_ACCESS_DENIED;
172
173 GlobalFree(hMem);
174 }
175 else
176 rc = RTErrConvertFromWin32(GetLastError());
177
178 if (RT_FAILURE(rc))
179 LogRel(("Shared Clipboard: Setting clipboard data for Windows host failed with %Rrc\n", rc));
180
181 LogFlowFuncLeaveRC(rc);
182 return rc;
183}
184
185static int vboxClipboardSvcWinDataRead(PSHCLCONTEXT pCtx, UINT uFormat, void **ppvData, uint32_t *pcbData)
186{
187 SHCLFORMAT fFormat = SharedClipboardWinClipboardFormatToVBox(uFormat);
188 LogFlowFunc(("uFormat=%u -> uFmt=0x%x\n", uFormat, fFormat));
189
190 if (fFormat == VBOX_SHCL_FMT_NONE)
191 {
192 LogRel2(("Shared Clipboard: Windows format %u not supported, ignoring\n", uFormat));
193 return VERR_NOT_SUPPORTED;
194 }
195
196 SHCLEVENTID idEvent = 0;
197 int rc = ShClSvcGuestDataRequest(pCtx->pClient, fFormat, &idEvent);
198 if (RT_SUCCESS(rc))
199 {
200 PSHCLEVENTPAYLOAD pPayload;
201 rc = ShClEventWait(&pCtx->pClient->EventSrc, idEvent, 30 * 1000, &pPayload);
202 if (RT_SUCCESS(rc))
203 {
204 *ppvData = pPayload ? pPayload->pvData : NULL;
205 *pcbData = pPayload ? pPayload->cbData : 0;
206 }
207
208 ShClEventRelease(&pCtx->pClient->EventSrc, idEvent);
209 ShClEventUnregister(&pCtx->pClient->EventSrc, idEvent);
210 }
211
212 if (RT_FAILURE(rc))
213 LogRel(("Shared Clipboard: Reading guest clipboard data for Windows host failed with %Rrc\n", rc));
214
215 LogFlowFuncLeaveRC(rc);
216 return rc;
217}
218
219static LRESULT CALLBACK vboxClipboardSvcWinWndProcMain(PSHCLCONTEXT pCtx,
220 HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) RT_NOTHROW_DEF
221{
222 AssertPtr(pCtx);
223
224 LRESULT lresultRc = 0;
225
226 const PSHCLWINCTX pWinCtx = &pCtx->Win;
227
228 switch (uMsg)
229 {
230 case WM_CLIPBOARDUPDATE:
231 {
232 LogFunc(("WM_CLIPBOARDUPDATE\n"));
233
234 int rc = RTCritSectEnter(&pWinCtx->CritSect);
235 if (RT_SUCCESS(rc))
236 {
237 const HWND hWndClipboardOwner = GetClipboardOwner();
238
239 LogFunc(("WM_CLIPBOARDUPDATE: hWndClipboardOwnerUs=%p, hWndNewClipboardOwner=%p\n",
240 pWinCtx->hWndClipboardOwnerUs, hWndClipboardOwner));
241
242 if (pWinCtx->hWndClipboardOwnerUs != hWndClipboardOwner)
243 {
244 int rc2 = RTCritSectLeave(&pWinCtx->CritSect);
245 AssertRC(rc2);
246
247 /* Clipboard was updated by another application, retrieve formats and report back. */
248 rc = vboxClipboardSvcWinSyncInternal(pCtx);
249 }
250 else
251 {
252 int rc2 = RTCritSectLeave(&pWinCtx->CritSect);
253 AssertRC(rc2);
254 }
255 }
256
257 if (RT_FAILURE(rc))
258 LogRel(("Shared Clipboard: WM_CLIPBOARDUPDATE failed with %Rrc\n", rc));
259
260 break;
261 }
262
263 case WM_CHANGECBCHAIN:
264 {
265 LogFunc(("WM_CHANGECBCHAIN\n"));
266 lresultRc = SharedClipboardWinHandleWMChangeCBChain(pWinCtx, hWnd, uMsg, wParam, lParam);
267 break;
268 }
269
270 case WM_DRAWCLIPBOARD:
271 {
272 LogFunc(("WM_DRAWCLIPBOARD\n"));
273
274 int rc = RTCritSectEnter(&pWinCtx->CritSect);
275 if (RT_SUCCESS(rc))
276 {
277 const HWND hWndClipboardOwner = GetClipboardOwner();
278
279 LogFunc(("WM_DRAWCLIPBOARD: hWndClipboardOwnerUs=%p, hWndNewClipboardOwner=%p\n",
280 pWinCtx->hWndClipboardOwnerUs, hWndClipboardOwner));
281
282 if (pWinCtx->hWndClipboardOwnerUs != hWndClipboardOwner)
283 {
284 int rc2 = RTCritSectLeave(&pWinCtx->CritSect);
285 AssertRC(rc2);
286
287 /* Clipboard was updated by another application, retrieve formats and report back. */
288 rc = vboxClipboardSvcWinSyncInternal(pCtx);
289 }
290 else
291 {
292 int rc2 = RTCritSectLeave(&pWinCtx->CritSect);
293 AssertRC(rc2);
294 }
295 }
296
297 lresultRc = SharedClipboardWinChainPassToNext(pWinCtx, uMsg, wParam, lParam);
298 break;
299 }
300
301 case WM_TIMER:
302 {
303 int rc = SharedClipboardWinHandleWMTimer(pWinCtx);
304 AssertRC(rc);
305
306 break;
307 }
308
309 case WM_RENDERFORMAT:
310 {
311 LogFunc(("WM_RENDERFORMAT\n"));
312
313 /* Insert the requested clipboard format data into the clipboard. */
314 const UINT uFormat = (UINT)wParam;
315 const SHCLFORMAT fFormat = SharedClipboardWinClipboardFormatToVBox(uFormat);
316 LogFunc(("WM_RENDERFORMAT: uFormat=%u -> fFormat=0x%x\n", uFormat, fFormat));
317
318 if ( fFormat == VBOX_SHCL_FMT_NONE
319 || pCtx->pClient == NULL)
320 {
321 /* Unsupported clipboard format is requested. */
322 LogFunc(("WM_RENDERFORMAT unsupported format requested or client is not active\n"));
323 SharedClipboardWinClear();
324 }
325 else
326 {
327 void *pvData = NULL;
328 uint32_t cbData = 0;
329 int rc = vboxClipboardSvcWinDataRead(pCtx, uFormat, &pvData, &cbData);
330 if ( RT_SUCCESS(rc)
331 && pvData
332 && cbData)
333 {
334 rc = vboxClipboardSvcWinDataSet(pCtx, uFormat, pvData, cbData);
335
336 RTMemFree(pvData);
337 cbData = 0;
338 }
339
340 if (RT_FAILURE(rc))
341 SharedClipboardWinClear();
342 }
343
344 break;
345 }
346
347 case WM_RENDERALLFORMATS:
348 {
349 LogFunc(("WM_RENDERALLFORMATS\n"));
350
351 int rc = SharedClipboardWinHandleWMRenderAllFormats(pWinCtx, hWnd);
352 AssertRC(rc);
353
354 break;
355 }
356
357 case SHCL_WIN_WM_REPORT_FORMATS:
358 {
359 /* Announce available formats. Do not insert data -- will be inserted in WM_RENDERFORMAT (or via IDataObject). */
360 SHCLFORMATS fFormats = (uint32_t)lParam;
361 LogFunc(("SHCL_WIN_WM_REPORT_FORMATS: fFormats=%#xn", fFormats));
362
363#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
364 if (fFormats & VBOX_SHCL_FMT_URI_LIST)
365 {
366 PSHCLTRANSFER pTransfer;
367 int rc = shClSvcTransferStart(pCtx->pClient,
368 SHCLTRANSFERDIR_FROM_REMOTE, SHCLSOURCE_REMOTE,
369 &pTransfer);
370 if (RT_SUCCESS(rc))
371 {
372 /* Create the IDataObject implementation the host OS needs and assign
373 * the newly created transfer to this object. */
374 rc = SharedClipboardWinTransferCreate(&pCtx->Win, pTransfer);
375
376 /* Note: The actual requesting + retrieving of data will be done in the IDataObject implementation
377 (ClipboardDataObjectImpl::GetData()). */
378 }
379 else
380 LogRel(("Shared Clipboard: Initializing read transfer failed with %Rrc\n", rc));
381 }
382 else
383 {
384#endif
385 int rc = SharedClipboardWinClearAndAnnounceFormats(pWinCtx, fFormats, hWnd);
386 if (RT_FAILURE(rc))
387 LogRel(("Shared Clipboard: Reporting clipboard formats %#x to Windows host failed with %Rrc\n", fFormats, rc));
388
389#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
390 }
391#endif
392 LogFunc(("SHCL_WIN_WM_REPORT_FORMATS: lastErr=%ld\n", GetLastError()));
393 break;
394 }
395
396 case WM_DESTROY:
397 {
398 LogFunc(("WM_DESTROY\n"));
399
400 int rc = SharedClipboardWinHandleWMDestroy(pWinCtx);
401 AssertRC(rc);
402
403 PostQuitMessage(0);
404 break;
405 }
406
407 default:
408 lresultRc = DefWindowProc(hWnd, uMsg, wParam, lParam);
409 break;
410 }
411
412 LogFlowFunc(("LEAVE hWnd=%p, WM_ %u -> %#zx\n", hWnd, uMsg, lresultRc));
413 return lresultRc;
414}
415
416/**
417 * Static helper function for having a per-client proxy window instances.
418 */
419static LRESULT CALLBACK vboxClipboardSvcWinWndProcInstance(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) RT_NOTHROW_DEF
420{
421 LONG_PTR pUserData = GetWindowLongPtr(hWnd, GWLP_USERDATA);
422 AssertPtrReturn(pUserData, 0);
423
424 PSHCLCONTEXT pCtx = reinterpret_cast<PSHCLCONTEXT>(pUserData);
425 if (pCtx)
426 return vboxClipboardSvcWinWndProcMain(pCtx, hWnd, uMsg, wParam, lParam);
427
428 return 0;
429}
430
431/**
432 * Static helper function for routing Windows messages to a specific
433 * proxy window instance.
434 */
435static LRESULT CALLBACK vboxClipboardSvcWinWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) RT_NOTHROW_DEF
436{
437 /* Note: WM_NCCREATE is not the first ever message which arrives, but
438 * early enough for us. */
439 if (uMsg == WM_NCCREATE)
440 {
441 LogFlowFunc(("WM_NCCREATE\n"));
442
443 LPCREATESTRUCT pCS = (LPCREATESTRUCT)lParam;
444 AssertPtr(pCS);
445 SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)pCS->lpCreateParams);
446 SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)vboxClipboardSvcWinWndProcInstance);
447
448 return vboxClipboardSvcWinWndProcInstance(hWnd, uMsg, wParam, lParam);
449 }
450
451 /* No window associated yet. */
452 return DefWindowProc(hWnd, uMsg, wParam, lParam);
453}
454
455DECLCALLBACK(int) vboxClipboardSvcWinThread(RTTHREAD hThreadSelf, void *pvUser)
456{
457 LogFlowFuncEnter();
458
459 bool fThreadSignalled = false;
460
461 const PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pvUser;
462 AssertPtr(pCtx);
463 const PSHCLWINCTX pWinCtx = &pCtx->Win;
464
465 HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
466
467 /* Register the Window Class. */
468 WNDCLASS wc;
469 RT_ZERO(wc);
470
471 wc.style = CS_NOCLOSE;
472 wc.lpfnWndProc = vboxClipboardSvcWinWndProc;
473 wc.hInstance = hInstance;
474 wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
475
476 /* Register an unique wnd class name. */
477 char szWndClassName[32];
478 RTStrPrintf2(szWndClassName, sizeof(szWndClassName),
479 "%s-%RU64", SHCL_WIN_WNDCLASS_NAME, RTThreadGetNative(hThreadSelf));
480 wc.lpszClassName = szWndClassName;
481
482 int rc;
483
484 ATOM atomWindowClass = RegisterClass(&wc);
485 if (atomWindowClass == 0)
486 {
487 LogFunc(("Failed to register window class\n"));
488 rc = VERR_NOT_SUPPORTED;
489 }
490 else
491 {
492 /* Create a window and make it a clipboard viewer. */
493 pWinCtx->hWnd = CreateWindowEx(WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
494 szWndClassName, szWndClassName,
495 WS_POPUPWINDOW,
496 -200, -200, 100, 100, NULL, NULL, hInstance, pCtx /* lpParam */);
497 if (pWinCtx->hWnd == NULL)
498 {
499 LogFunc(("Failed to create window\n"));
500 rc = VERR_NOT_SUPPORTED;
501 }
502 else
503 {
504 SetWindowPos(pWinCtx->hWnd, HWND_TOPMOST, -200, -200, 0, 0,
505 SWP_NOACTIVATE | SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_NOSIZE);
506
507 rc = SharedClipboardWinChainAdd(&pCtx->Win);
508 if (RT_SUCCESS(rc))
509 {
510 if (!SharedClipboardWinIsNewAPI(&pWinCtx->newAPI))
511 pWinCtx->oldAPI.timerRefresh = SetTimer(pWinCtx->hWnd, 0, 10 * 1000, NULL);
512 }
513
514#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
515 if (RT_SUCCESS(rc))
516 {
517 HRESULT hr = OleInitialize(NULL);
518 if (FAILED(hr))
519 {
520 LogRel(("Shared Clipboard: Initializing window thread OLE failed (%Rhrc) -- file transfers unavailable\n", hr));
521 /* Not critical, the rest of the clipboard might work. */
522 }
523 else
524 LogRel(("Shared Clipboard: Initialized window thread OLE\n"));
525 }
526#endif
527 int rc2 = RTThreadUserSignal(hThreadSelf);
528 AssertRC(rc2);
529
530 fThreadSignalled = true;
531
532 MSG msg;
533 BOOL msgret = 0;
534 while ((msgret = GetMessage(&msg, NULL, 0, 0)) > 0)
535 {
536 TranslateMessage(&msg);
537 DispatchMessage(&msg);
538 }
539
540 /*
541 * Window procedure can return error, * but this is exceptional situation that should be
542 * identified in testing.
543 */
544 Assert(msgret >= 0);
545 LogFunc(("Message loop finished. GetMessage returned %d, message id: %d \n", msgret, msg.message));
546
547#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
548 OleSetClipboard(NULL); /* Make sure to flush the clipboard on destruction. */
549 OleUninitialize();
550#endif
551 }
552 }
553
554 pWinCtx->hWnd = NULL;
555
556 if (atomWindowClass != 0)
557 {
558 UnregisterClass(szWndClassName, hInstance);
559 atomWindowClass = 0;
560 }
561
562 if (!fThreadSignalled)
563 {
564 int rc2 = RTThreadUserSignal(hThreadSelf);
565 AssertRC(rc2);
566 }
567
568 LogFlowFuncLeaveRC(rc);
569 return rc;
570}
571
572/**
573 * Synchronizes the host and the guest clipboard formats by sending all supported host clipboard
574 * formats to the guest.
575 *
576 * @returns VBox status code, VINF_NO_CHANGE if no synchronization was required.
577 * @param pCtx Clipboard context to synchronize.
578 */
579static int vboxClipboardSvcWinSyncInternal(PSHCLCONTEXT pCtx)
580{
581 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
582
583 LogFlowFuncEnter();
584
585 int rc;
586
587 if (pCtx->pClient)
588 {
589 SHCLFORMATS fFormats = 0;
590 rc = SharedClipboardWinGetFormats(&pCtx->Win, &fFormats);
591 if ( RT_SUCCESS(rc)
592 && fFormats != VBOX_SHCL_FMT_NONE /** @todo r=bird: BUGBUG: revisit this. */
593 && ShClSvcIsBackendActive())
594 rc = ShClSvcHostReportFormats(pCtx->pClient, fFormats);
595 }
596 else /* If we don't have any client data (yet), bail out. */
597 rc = VINF_NO_CHANGE;
598
599 LogFlowFuncLeaveRC(rc);
600 return rc;
601}
602
603/*
604 * Public platform dependent functions.
605 */
606
607int ShClBackendInit(VBOXHGCMSVCFNTABLE *pTable)
608{
609 RT_NOREF(pTable);
610#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
611 HRESULT hr = OleInitialize(NULL);
612 if (FAILED(hr))
613 {
614 LogRel(("Shared Clipboard: Initializing OLE failed (%Rhrc) -- file transfers unavailable\n", hr));
615 /* Not critical, the rest of the clipboard might work. */
616 }
617 else
618 LogRel(("Shared Clipboard: Initialized OLE\n"));
619#endif
620
621 return VINF_SUCCESS;
622}
623
624void ShClBackendDestroy(void)
625{
626#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
627 OleSetClipboard(NULL); /* Make sure to flush the clipboard on destruction. */
628 OleUninitialize();
629#endif
630}
631
632int ShClBackendConnect(PSHCLCLIENT pClient, bool fHeadless)
633{
634 RT_NOREF(fHeadless);
635
636 LogFlowFuncEnter();
637
638 int rc;
639
640 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)RTMemAllocZ(sizeof(SHCLCONTEXT));
641 if (pCtx)
642 {
643 rc = SharedClipboardWinCtxInit(&pCtx->Win);
644 if (RT_SUCCESS(rc))
645 {
646 rc = RTThreadCreate(&pCtx->hThread, vboxClipboardSvcWinThread, pCtx /* pvUser */, _64K /* Stack size */,
647 RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "SHCLIP");
648 if (RT_SUCCESS(rc))
649 {
650 int rc2 = RTThreadUserWait(pCtx->hThread, 30 * 1000 /* Timeout in ms */);
651 AssertRC(rc2);
652 }
653 }
654
655 pClient->State.pCtx = pCtx;
656 pClient->State.pCtx->pClient = pClient;
657 }
658 else
659 rc = VERR_NO_MEMORY;
660
661 LogFlowFuncLeaveRC(rc);
662 return rc;
663}
664
665int ShClBackendSync(PSHCLCLIENT pClient)
666{
667 /* Sync the host clipboard content with the client. */
668 return vboxClipboardSvcWinSyncInternal(pClient->State.pCtx);
669}
670
671int ShClBackendDisconnect(PSHCLCLIENT pClient)
672{
673 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
674
675 LogFlowFuncEnter();
676
677 int rc = VINF_SUCCESS;
678
679 PSHCLCONTEXT pCtx = pClient->State.pCtx;
680 if (pCtx)
681 {
682 if (pCtx->Win.hWnd)
683 PostMessage(pCtx->Win.hWnd, WM_DESTROY, 0 /* wParam */, 0 /* lParam */);
684
685 if (pCtx->hThread != NIL_RTTHREAD)
686 {
687 LogFunc(("Waiting for thread to terminate ...\n"));
688
689 /* Wait for the window thread to terminate. */
690 rc = RTThreadWait(pCtx->hThread, 30 * 1000 /* Timeout in ms */, NULL);
691 if (RT_FAILURE(rc))
692 LogRel(("Shared Clipboard: Waiting for window thread termination failed with rc=%Rrc\n", rc));
693
694 pCtx->hThread = NIL_RTTHREAD;
695 }
696
697 SharedClipboardWinCtxDestroy(&pCtx->Win);
698
699 if (RT_SUCCESS(rc))
700 {
701 RTMemFree(pCtx);
702 pCtx = NULL;
703
704 pClient->State.pCtx = NULL;
705 }
706 }
707
708 LogFlowFuncLeaveRC(rc);
709 return rc;
710}
711
712int ShClBackendFormatAnnounce(PSHCLCLIENT pClient, SHCLFORMATS fFormats)
713{
714 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
715
716 PSHCLCONTEXT pCtx = pClient->State.pCtx;
717 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
718
719 LogFlowFunc(("fFormats=0x%x, hWnd=%p\n", fFormats, pCtx->Win.hWnd));
720
721 /*
722 * The guest announced formats. Forward to the window thread.
723 */
724 PostMessage(pCtx->Win.hWnd, SHCL_WIN_WM_REPORT_FORMATS,
725 0 /* wParam */, fFormats /* lParam */);
726
727
728 LogFlowFuncLeaveRC(VINF_SUCCESS);
729 return VINF_SUCCESS;
730}
731
732int ShClBackendReadData(PSHCLCLIENT pClient, PSHCLCLIENTCMDCTX pCmdCtx,
733 SHCLFORMAT uFmt, void *pvData, uint32_t cbData, uint32_t *pcbActual)
734{
735 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
736 AssertPtrReturn(pCmdCtx, VERR_INVALID_POINTER);
737 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
738 AssertPtrReturn(pcbActual, VERR_INVALID_POINTER);
739
740 RT_NOREF(pCmdCtx);
741
742 AssertPtrReturn(pClient->State.pCtx, VERR_INVALID_POINTER);
743
744 LogFlowFunc(("uFmt=%#x\n", uFmt));
745
746 HANDLE hClip = NULL;
747
748 const PSHCLWINCTX pWinCtx = &pClient->State.pCtx->Win;
749
750 /*
751 * The guest wants to read data in the given format.
752 */
753 int rc = SharedClipboardWinOpen(pWinCtx->hWnd);
754 if (RT_SUCCESS(rc))
755 {
756 if (uFmt & VBOX_SHCL_FMT_BITMAP)
757 {
758 LogFunc(("CF_DIB\n"));
759 hClip = GetClipboardData(CF_DIB);
760 if (hClip != NULL)
761 {
762 LPVOID lp = GlobalLock(hClip);
763 if (lp != NULL)
764 {
765 rc = vboxClipboardSvcWinDataGet(VBOX_SHCL_FMT_BITMAP, lp, GlobalSize(hClip),
766 pvData, cbData, pcbActual);
767 GlobalUnlock(hClip);
768 }
769 else
770 {
771 hClip = NULL;
772 }
773 }
774 }
775 else if (uFmt & VBOX_SHCL_FMT_UNICODETEXT)
776 {
777 LogFunc(("CF_UNICODETEXT\n"));
778 hClip = GetClipboardData(CF_UNICODETEXT);
779 if (hClip != NULL)
780 {
781 LPWSTR uniString = (LPWSTR)GlobalLock(hClip);
782 if (uniString != NULL)
783 {
784 rc = vboxClipboardSvcWinDataGet(VBOX_SHCL_FMT_UNICODETEXT, uniString, (lstrlenW(uniString) + 1) * 2,
785 pvData, cbData, pcbActual);
786 GlobalUnlock(hClip);
787 }
788 else
789 {
790 hClip = NULL;
791 }
792 }
793 }
794 else if (uFmt & VBOX_SHCL_FMT_HTML)
795 {
796 LogFunc(("SHCL_WIN_REGFMT_HTML\n"));
797 UINT uRegFmt = RegisterClipboardFormat(SHCL_WIN_REGFMT_HTML);
798 if (uRegFmt != 0)
799 {
800 hClip = GetClipboardData(uRegFmt);
801 if (hClip != NULL)
802 {
803 LPVOID lp = GlobalLock(hClip);
804 if (lp != NULL)
805 {
806 rc = vboxClipboardSvcWinDataGet(VBOX_SHCL_FMT_HTML, lp, GlobalSize(hClip),
807 pvData, cbData, pcbActual);
808#ifdef LOG_ENABLED
809 if (RT_SUCCESS(rc))
810 {
811 LogFlowFunc(("Raw HTML clipboard data from host:\n"));
812 ShClDbgDumpHtml((char *)pvData, cbData);
813 }
814#endif
815 GlobalUnlock(hClip);
816 }
817 else
818 {
819 hClip = NULL;
820 }
821 }
822 }
823 }
824#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
825 else if (uFmt & VBOX_SHCL_FMT_URI_LIST)
826 {
827 AssertFailed(); /** @todo */
828 }
829#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
830 SharedClipboardWinClose();
831 }
832
833 if (hClip == NULL) /* Empty data is not fatal. */
834 {
835 /* Reply with empty data. */
836 vboxClipboardSvcWinDataGet(0, NULL, 0, pvData, cbData, pcbActual);
837 }
838
839 if (RT_FAILURE(rc))
840 LogRel(("Shared Clipboard: Error reading host clipboard data in format %#x from Windows, rc=%Rrc\n", uFmt, rc));
841
842 LogFlowFuncLeaveRC(rc);
843 return rc;
844}
845
846int ShClBackendWriteData(PSHCLCLIENT pClient, PSHCLCLIENTCMDCTX pCmdCtx,
847 SHCLFORMAT uFormat, void *pvData, uint32_t cbData)
848{
849 RT_NOREF(pClient, pCmdCtx, uFormat, pvData, cbData);
850
851 LogFlowFuncEnter();
852
853 /* Nothing to do here yet. */
854
855 LogFlowFuncLeave();
856 return VINF_SUCCESS;
857}
858
859#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
860int ShClBackendTransferCreate(PSHCLCLIENT pClient, PSHCLTRANSFER pTransfer)
861{
862 RT_NOREF(pClient, pTransfer);
863
864 LogFlowFuncEnter();
865
866 return VINF_SUCCESS;
867}
868
869int ShClBackendTransferDestroy(PSHCLCLIENT pClient, PSHCLTRANSFER pTransfer)
870{
871 LogFlowFuncEnter();
872
873 SharedClipboardWinTransferDestroy(&pClient->State.pCtx->Win, pTransfer);
874
875 return VINF_SUCCESS;
876}
877
878int ShClBackendTransferGetRoots(PSHCLCLIENT pClient, PSHCLTRANSFER pTransfer)
879{
880 LogFlowFuncEnter();
881
882 const PSHCLWINCTX pWinCtx = &pClient->State.pCtx->Win;
883
884 int rc = SharedClipboardWinGetRoots(pWinCtx, pTransfer);
885
886 LogFlowFuncLeaveRC(rc);
887 return rc;
888}
889#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
890
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