VirtualBox

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

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

VBoxSharedClipboardSvc: If guest provided zero bytes, we get no payload. bugref:9631

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