VirtualBox

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

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

Shared Clipboard/HostService: Renaming to match terminology (*SvcImpl* -> *Backend*).

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