VirtualBox

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

Last change on this file since 103551 was 103453, checked in by vboxsync, 12 months ago

Shared Clipboard: Condensed more code by adding a new Windows-specific function SharedClipboardWinTransferHandOffToDataObject() [build fix]. ​bugref:9437

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 36.7 KB
Line 
1/* $Id: VBoxSharedClipboardSvc-win.cpp 103453 2024-02-19 15:22:49Z vboxsync $ */
2/** @file
3 * Shared Clipboard Service - Win32 host.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
33#include <iprt/win/windows.h>
34
35#include <VBox/HostServices/VBoxClipboardSvc.h>
36#include <VBox/GuestHost/clipboard-helper.h>
37#include <VBox/GuestHost/SharedClipboard-win.h>
38#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
39# include <VBox/GuestHost/SharedClipboard-transfers.h>
40#endif
41
42#include <iprt/alloc.h>
43#include <iprt/string.h>
44#include <iprt/asm.h>
45#include <iprt/assert.h>
46#include <iprt/ldr.h>
47#include <iprt/semaphore.h>
48#include <iprt/thread.h>
49#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
50# include <iprt/utf16.h>
51#endif
52
53#include <process.h>
54#include <iprt/win/shlobj.h> /* Needed for shell objects. */
55
56#include "VBoxSharedClipboardSvc-internal.h"
57#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
58# include "VBoxSharedClipboardSvc-transfers.h"
59#endif
60
61
62/*********************************************************************************************************************************
63* Structures and Typedefs *
64*********************************************************************************************************************************/
65/**
66 * Global context information used by the host glue for the X11 clipboard backend.
67 */
68struct SHCLCONTEXT
69{
70 /** Handle for window message handling thread. */
71 RTTHREAD hThread;
72 /** Structure for keeping and communicating with service client. */
73 PSHCLCLIENT pClient;
74 /** Windows-specific context data. */
75 SHCLWINCTX Win;
76};
77
78
79/*********************************************************************************************************************************
80* Prototypes *
81*********************************************************************************************************************************/
82static int vboxClipboardSvcWinSyncInternal(PSHCLCONTEXT pCtx);
83
84#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
85static DECLCALLBACK(int) shClSvcWinTransferIfaceHGRootListRead(PSHCLTXPROVIDERCTX pCtx);
86#endif
87
88
89/**
90 * Copy clipboard data into the guest buffer.
91 *
92 * At first attempt, guest will provide a buffer of default size.
93 * Usually 1K or 4K (see platform specific Guest Additions code around
94 * VbglR3ClipboardReadData calls). If this buffer is not big enough
95 * to fit host clipboard content, this function will return VINF_BUFFER_OVERFLOW
96 * and provide guest with host's clipboard buffer actual size. This will be a
97 * signal for the guest to re-read host clipboard data providing bigger buffer
98 * to store it.
99 *
100 * @returns IPRT status code.
101 * @returns VINF_BUFFER_OVERFLOW returned when guest buffer size if not big
102 * enough to store host clipboard data. This is a signal to the guest
103 * to re-issue host clipboard read request with bigger buffer size
104 * (specified in @a pcbActualDst output parameter).
105 * @param u32Format VBox clipboard format (VBOX_SHCL_FMT_XXX) of copied data.
106 * VBOX_SHCL_FMT_NONE returns 0 data.
107 * @param pvSrc Pointer to host clipboard data.
108 * @param cbSrc Size (in bytes) of actual clipboard data to copy.
109 * @param pvDst Pointer to guest buffer to store clipboard data.
110 * @param cbDst Size (in bytes) of guest buffer.
111 * @param pcbActualDst Actual size (in bytes) of host clipboard data.
112 * Only set if guest buffer size if not big enough
113 * to store host clipboard content. When set,
114 * function returns VINF_BUFFER_OVERFLOW.
115 */
116static int vboxClipboardSvcWinDataGet(SHCLFORMAT u32Format, const void *pvSrc, uint32_t cbSrc,
117 void *pvDst, uint32_t cbDst, uint32_t *pcbActualDst)
118{
119 AssertPtrReturn(pcbActualDst, VERR_INVALID_POINTER);
120 if (u32Format == VBOX_SHCL_FMT_NONE)
121 {
122 *pcbActualDst = 0;
123 return VINF_SUCCESS;
124 }
125
126 AssertPtrReturn(pvSrc, VERR_INVALID_POINTER);
127 AssertReturn (cbSrc, VERR_INVALID_PARAMETER);
128 AssertPtrReturn(pvDst, VERR_INVALID_POINTER);
129 AssertReturn (cbDst, VERR_INVALID_PARAMETER);
130
131 LogFlowFunc(("cbSrc = %d, cbDst = %d\n", cbSrc, cbDst));
132
133 if ( u32Format == VBOX_SHCL_FMT_HTML
134 && SharedClipboardWinIsCFHTML((const char *)pvSrc))
135 {
136 /** @todo r=bird: Why the double conversion? */
137 char *pszBuf = NULL;
138 uint32_t cbBuf = 0;
139 int rc = SharedClipboardWinConvertCFHTMLToMIME((const char *)pvSrc, cbSrc, &pszBuf, &cbBuf);
140 if (RT_SUCCESS(rc))
141 {
142 *pcbActualDst = cbBuf;
143 if (cbBuf > cbDst)
144 {
145 /* Do not copy data. The dst buffer is not enough. */
146 RTMemFree(pszBuf);
147 return VINF_BUFFER_OVERFLOW;
148 }
149 memcpy(pvDst, pszBuf, cbBuf);
150 RTMemFree(pszBuf);
151 }
152 else
153 *pcbActualDst = 0;
154 }
155 else
156 {
157 *pcbActualDst = cbSrc; /* Tell the caller how much space we need. */
158
159 if (cbSrc > cbDst)
160 return VINF_BUFFER_OVERFLOW;
161
162 memcpy(pvDst, pvSrc, cbSrc);
163 }
164
165#ifdef LOG_ENABLED
166 ShClDbgDumpData(pvDst, cbSrc, u32Format);
167#endif
168
169 return VINF_SUCCESS;
170}
171
172/**
173 * Worker for a reading clipboard from the guest.
174 */
175static int vboxClipboardSvcWinReadDataFromGuestWorker(PSHCLCONTEXT pCtx, SHCLFORMAT uFmt, void **ppvData, uint32_t *pcbData)
176{
177 return ShClSvcReadDataFromGuest(pCtx->pClient, uFmt, ppvData, pcbData);
178}
179
180static int vboxClipboardSvcWinReadDataFromGuest(PSHCLCONTEXT pCtx, UINT uWinFormat, void **ppvData, uint32_t *pcbData)
181{
182 SHCLFORMAT uVBoxFmt = SharedClipboardWinClipboardFormatToVBox(uWinFormat);
183 if (uVBoxFmt == VBOX_SHCL_FMT_NONE)
184 {
185 LogRel2(("Shared Clipboard: Windows format %u not supported, ignoring\n", uWinFormat));
186 return VERR_NOT_SUPPORTED;
187 }
188
189 int rc = vboxClipboardSvcWinReadDataFromGuestWorker(pCtx, uVBoxFmt, ppvData, pcbData);
190
191 LogFlowFuncLeaveRC(rc);
192 return rc;
193}
194
195/**
196 * @copydoc SHCLCALLBACKS::pfnOnRequestDataFromSource
197 *
198 * Called from the IDataObject implementation to request data from the guest.
199 *
200 * @thread Windows event thread.
201 */
202static DECLCALLBACK(int) vboxClipboardSvcWinRequestDataFromSourceCallback(PSHCLCONTEXT pCtx,
203 SHCLFORMAT uFmt, void **ppv, uint32_t *pcb, void *pvUser)
204{
205 RT_NOREF(pvUser);
206
207 LogFlowFuncEnter();
208
209 int rc = vboxClipboardSvcWinReadDataFromGuestWorker(pCtx, uFmt, ppv, pcb);
210
211 LogFlowFuncLeaveRC(rc);
212
213 return rc;
214}
215
216
217#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
218/**
219 * @copydoc SHCLTRANSFERCALLBACKS::pfnOnCreated
220 *
221 * @thread Service main thread.
222 */
223static DECLCALLBACK(void) shClSvcWinTransferOnCreatedCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx)
224{
225 LogFlowFuncEnter();
226
227 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pCbCtx->pvUser;
228 AssertPtr(pCtx);
229
230 PSHCLTRANSFER pTransfer = pCbCtx->pTransfer;
231 AssertPtr(pTransfer);
232
233 PSHCLCLIENT const pClient = pCtx->pClient;
234 AssertPtr(pClient);
235
236 /*
237 * Set transfer provider.
238 * Those will be registered within ShClSvcTransferInit() when a new transfer gets initialized.
239 */
240
241 /* Set the interface to the local provider by default first. */
242 RT_ZERO(pClient->Transfers.Provider);
243 ShClTransferProviderLocalQueryInterface(&pClient->Transfers.Provider);
244
245 PSHCLTXPROVIDERIFACE pIface = &pClient->Transfers.Provider.Interface;
246
247 pClient->Transfers.Provider.enmSource = pClient->State.enmSource;
248 pClient->Transfers.Provider.pvUser = pClient;
249
250 int rc = VINF_SUCCESS;
251
252 switch (ShClTransferGetDir(pTransfer))
253 {
254 case SHCLTRANSFERDIR_FROM_REMOTE: /* G->H */
255 {
256 pIface->pfnRootListRead = shClSvcTransferIfaceGHRootListRead;
257
258 pIface->pfnListOpen = shClSvcTransferIfaceGHListOpen;
259 pIface->pfnListClose = shClSvcTransferIfaceGHListClose;
260 pIface->pfnListHdrRead = shClSvcTransferIfaceGHListHdrRead;
261 pIface->pfnListEntryRead = shClSvcTransferIfaceGHListEntryRead;
262
263 pIface->pfnObjOpen = shClSvcTransferIfaceGHObjOpen;
264 pIface->pfnObjClose = shClSvcTransferIfaceGHObjClose;
265 pIface->pfnObjRead = shClSvcTransferIfaceGHObjRead;
266 break;
267 }
268
269 case SHCLTRANSFERDIR_TO_REMOTE: /* H->G */
270 {
271 pIface->pfnRootListRead = shClSvcWinTransferIfaceHGRootListRead;
272 break;
273 }
274
275 default:
276 AssertFailedStmt(rc = VERR_NOT_SUPPORTED);
277 break;
278 }
279
280 if (RT_SUCCESS(rc))
281 {
282 rc = ShClTransferSetProvider(pTransfer, &pClient->Transfers.Provider);
283 if (RT_SUCCESS(rc))
284 rc = SharedClipboardWinTransferCreate(&pCtx->Win, pTransfer);
285 }
286
287 LogFlowFuncLeaveRC(rc);
288}
289
290/**
291 * @copydoc SHCLTRANSFERCALLBACKS::pfnOnInitialize
292 *
293 * For G->H: Called on transfer intialization to notify the "in-flight" IDataObject about a data transfer.
294 * For H->G: Called on transfer intialization to populate the transfer's root list.
295 *
296 * @thread Service main thread.
297 */
298static DECLCALLBACK(int) shClSvcWinTransferOnInitializeCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx)
299{
300 LogFlowFuncEnter();
301
302 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pCbCtx->pvUser;
303 AssertPtr(pCtx);
304
305 PSHCLTRANSFER pTransfer = pCbCtx->pTransfer;
306 AssertPtr(pTransfer);
307
308 int rc = VINF_SUCCESS;
309
310 switch (ShClTransferGetDir(pTransfer))
311 {
312 case SHCLTRANSFERDIR_FROM_REMOTE: /* G->H */
313 {
314 rc = SharedClipboardWinTransferHandOffToDataObject(&pCtx->Win, pTransfer);
315 break;
316 }
317
318 case SHCLTRANSFERDIR_TO_REMOTE: /* H->G */
319 {
320 rc = ShClTransferRootListRead(pTransfer); /* Calls shClSvcWinTransferIfaceHGRootListRead(). */
321 break;
322 }
323
324 default:
325 AssertFailedStmt(rc = VERR_NOT_SUPPORTED);
326 break;
327 }
328
329 LogFlowFuncLeaveRC(rc);
330 return rc;
331}
332
333/**
334 * @copydoc SHCLTRANSFERCALLBACKS::pfnOnDestroy
335 *
336 * @thread Service main thread.
337 */
338static DECLCALLBACK(void) shClSvcWinTransferOnDestroyCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx)
339{
340 LogFlowFuncEnter();
341
342 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pCbCtx->pvUser;
343 AssertPtr(pCtx);
344
345 PSHCLTRANSFER pTransfer = pCbCtx->pTransfer;
346 AssertPtr(pTransfer);
347
348 SharedClipboardWinTransferDestroy(&pCtx->Win, pTransfer);
349}
350
351/**
352 * @copydoc SharedClipboardWinDataObject::CALLBACKS::pfnTransferBegin
353 *
354 * Called by SharedClipboardWinDataObject::GetData() when the user wants to paste data.
355 * This then creates and initializes a new transfer on the host + lets the guest know about that new transfer.
356 *
357 * @thread Service main thread.
358 */
359static DECLCALLBACK(int) shClSvcWinDataObjectTransferBeginCallback(SharedClipboardWinDataObject::PCALLBACKCTX pCbCtx)
360{
361 LogFlowFuncEnter();
362
363 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pCbCtx->pvUser;
364 AssertPtr(pCtx);
365
366 PSHCLTRANSFER pTransfer;
367 int rc = ShClSvcTransferCreate(pCtx->pClient, SHCLTRANSFERDIR_FROM_REMOTE, SHCLSOURCE_REMOTE,
368 NIL_SHCLTRANSFERID /* Creates a new transfer ID */, &pTransfer);
369 if (RT_SUCCESS(rc))
370 {
371 /* Initialize the transfer on the host side. */
372 rc = ShClSvcTransferInit(pCtx->pClient, pTransfer);
373 if (RT_FAILURE(rc))
374 ShClSvcTransferDestroy(pCtx->pClient, pTransfer);
375 }
376
377 LogFlowFuncLeaveRC(rc);
378 return rc;
379}
380#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
381
382static LRESULT CALLBACK vboxClipboardSvcWinWndProcMain(PSHCLCONTEXT pCtx,
383 HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) RT_NOTHROW_DEF
384{
385 AssertPtr(pCtx);
386
387 LRESULT lresultRc = 0;
388
389 const PSHCLWINCTX pWinCtx = &pCtx->Win;
390
391 switch (uMsg)
392 {
393 case WM_CLIPBOARDUPDATE:
394 {
395 LogFunc(("WM_CLIPBOARDUPDATE\n"));
396
397 int rc = RTCritSectEnter(&pWinCtx->CritSect);
398 if (RT_SUCCESS(rc))
399 {
400 const HWND hWndClipboardOwner = GetClipboardOwner();
401
402 LogFunc(("WM_CLIPBOARDUPDATE: hWndClipboardOwnerUs=%p, hWndNewClipboardOwner=%p\n",
403 pWinCtx->hWndClipboardOwnerUs, hWndClipboardOwner));
404
405 if (pWinCtx->hWndClipboardOwnerUs != hWndClipboardOwner)
406 {
407 int rc2 = RTCritSectLeave(&pWinCtx->CritSect);
408 AssertRC(rc2);
409
410 /* Clipboard was updated by another application, retrieve formats and report back. */
411 rc = vboxClipboardSvcWinSyncInternal(pCtx);
412 }
413 else
414 {
415 int rc2 = RTCritSectLeave(&pWinCtx->CritSect);
416 AssertRC(rc2);
417 }
418 }
419
420 if (RT_FAILURE(rc))
421 LogRel(("Shared Clipboard: WM_CLIPBOARDUPDATE failed with %Rrc\n", rc));
422
423 break;
424 }
425
426 case WM_CHANGECBCHAIN:
427 {
428 LogFunc(("WM_CHANGECBCHAIN\n"));
429 lresultRc = SharedClipboardWinHandleWMChangeCBChain(pWinCtx, hWnd, uMsg, wParam, lParam);
430 break;
431 }
432
433 case WM_DRAWCLIPBOARD:
434 {
435 LogFunc(("WM_DRAWCLIPBOARD\n"));
436
437 int rc = RTCritSectEnter(&pWinCtx->CritSect);
438 if (RT_SUCCESS(rc))
439 {
440 const HWND hWndClipboardOwner = GetClipboardOwner();
441
442 LogFunc(("WM_DRAWCLIPBOARD: hWndClipboardOwnerUs=%p, hWndNewClipboardOwner=%p\n",
443 pWinCtx->hWndClipboardOwnerUs, hWndClipboardOwner));
444
445 if (pWinCtx->hWndClipboardOwnerUs != hWndClipboardOwner)
446 {
447 int rc2 = RTCritSectLeave(&pWinCtx->CritSect);
448 AssertRC(rc2);
449
450 /* Clipboard was updated by another application, retrieve formats and report back. */
451 rc = vboxClipboardSvcWinSyncInternal(pCtx);
452 }
453 else
454 {
455 int rc2 = RTCritSectLeave(&pWinCtx->CritSect);
456 AssertRC(rc2);
457 }
458 }
459
460 lresultRc = SharedClipboardWinChainPassToNext(pWinCtx, uMsg, wParam, lParam);
461 break;
462 }
463
464 case WM_TIMER:
465 {
466 int rc = SharedClipboardWinHandleWMTimer(pWinCtx);
467 AssertRC(rc);
468
469 break;
470 }
471
472 case WM_RENDERFORMAT:
473 {
474 /* Insert the requested clipboard format data into the clipboard. */
475 const UINT uFmtWin = (UINT)wParam;
476 const SHCLFORMAT uFmtVBox = SharedClipboardWinClipboardFormatToVBox(uFmtWin);
477
478 LogFunc(("WM_RENDERFORMAT: uFmtWin=%u -> uFmtVBox=0x%x\n", uFmtWin, uFmtVBox));
479#ifdef LOG_ENABLED
480 char *pszFmts = ShClFormatsToStrA(uFmtVBox);
481 AssertPtrReturn(pszFmts, 0);
482 LogRel(("Shared Clipboard: Rendering Windows format %#x as VBox format '%s'\n", uFmtWin, pszFmts));
483 RTStrFree(pszFmts);
484#endif
485 if ( uFmtVBox == VBOX_SHCL_FMT_NONE
486 || pCtx->pClient == NULL)
487 {
488 /* Unsupported clipboard format is requested. */
489 LogFunc(("WM_RENDERFORMAT unsupported format requested or client is not active\n"));
490 SharedClipboardWinClear();
491 }
492 else
493 {
494 void *pvData = NULL;
495 uint32_t cbData = 0;
496 int rc = ShClSvcReadDataFromGuest(pCtx->pClient, uFmtVBox, &pvData, &cbData);
497 if (RT_SUCCESS(rc))
498 {
499 /* Wrap HTML clipboard content info CF_HTML format if needed. */
500 if (uFmtVBox == VBOX_SHCL_FMT_HTML
501 && !SharedClipboardWinIsCFHTML((char *)pvData))
502 {
503 char *pszWrapped = NULL;
504 uint32_t cbWrapped = 0;
505 rc = SharedClipboardWinConvertMIMEToCFHTML((char *)pvData, cbData, &pszWrapped, &cbWrapped);
506 if (RT_SUCCESS(rc))
507 {
508 /* Replace buffer with wrapped data content. */
509 RTMemFree(pvData);
510 pvData = (void *)pszWrapped;
511 cbData = cbWrapped;
512 }
513 else
514 LogRel(("Shared Clipboard: cannot convert HTML clipboard into CF_HTML format, rc=%Rrc\n", rc));
515 }
516
517 rc = SharedClipboardWinDataWrite(uFmtWin, pvData, cbData);
518 if (RT_FAILURE(rc))
519 LogRel(("Shared Clipboard: Setting clipboard data for Windows host failed with %Rrc\n", rc));
520
521 RTMemFree(pvData);
522 cbData = 0;
523 }
524
525 if (RT_FAILURE(rc))
526 SharedClipboardWinClear();
527 }
528
529 break;
530 }
531
532 case WM_RENDERALLFORMATS:
533 {
534 LogFunc(("WM_RENDERALLFORMATS\n"));
535
536 int rc = SharedClipboardWinHandleWMRenderAllFormats(pWinCtx, hWnd);
537 AssertRC(rc);
538
539 break;
540 }
541
542 case SHCL_WIN_WM_REPORT_FORMATS: /* Guest reported clipboard formats. */
543 {
544 /* Announce available formats. Do not insert data -- will be inserted in WM_RENDERFORMAT (or via IDataObject). */
545 SHCLFORMATS fFormats = (uint32_t)lParam;
546 LogFunc(("SHCL_WIN_WM_REPORT_FORMATS: fFormats=%#xn", fFormats));
547
548 int rc = SharedClipboardWinClearAndAnnounceFormats(pWinCtx, fFormats, hWnd);
549#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
550 if ( RT_SUCCESS(rc)
551 && fFormats & VBOX_SHCL_FMT_URI_LIST)
552 {
553 /*
554 * Create our IDataObject implementation and push it to the Windows clibpoard.
555 * That way Windows will recognize that there is a data transfer available.
556 */
557 SharedClipboardWinDataObject::CALLBACKS Callbacks;
558 RT_ZERO(Callbacks);
559 Callbacks.pfnTransferBegin = shClSvcWinDataObjectTransferBeginCallback;
560
561 rc = SharedClipboardWinTransferCreateAndSetDataObject(pWinCtx, pCtx, &Callbacks);
562 }
563#else
564 RT_NOREF(rc);
565#endif
566 LogFunc(("SHCL_WIN_WM_REPORT_FORMATS: lastErr=%ld\n", GetLastError()));
567 break;
568 }
569
570 case WM_DESTROY:
571 {
572 LogFunc(("WM_DESTROY\n"));
573
574 int rc = SharedClipboardWinHandleWMDestroy(pWinCtx);
575 AssertRC(rc);
576
577 PostQuitMessage(0);
578 break;
579 }
580
581 default:
582 lresultRc = DefWindowProc(hWnd, uMsg, wParam, lParam);
583 break;
584 }
585
586 LogFlowFunc(("LEAVE hWnd=%p, WM_ %u -> %#zx\n", hWnd, uMsg, lresultRc));
587 return lresultRc;
588}
589
590/**
591 * Static helper function for having a per-client proxy window instances.
592 */
593static LRESULT CALLBACK vboxClipboardSvcWinWndProcInstance(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) RT_NOTHROW_DEF
594{
595 LONG_PTR pUserData = GetWindowLongPtr(hWnd, GWLP_USERDATA);
596 AssertPtrReturn(pUserData, 0);
597
598 PSHCLCONTEXT pCtx = reinterpret_cast<PSHCLCONTEXT>(pUserData);
599 if (pCtx)
600 return vboxClipboardSvcWinWndProcMain(pCtx, hWnd, uMsg, wParam, lParam);
601
602 return 0;
603}
604
605/**
606 * Static helper function for routing Windows messages to a specific
607 * proxy window instance.
608 */
609static LRESULT CALLBACK vboxClipboardSvcWinWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) RT_NOTHROW_DEF
610{
611 /* Note: WM_NCCREATE is not the first ever message which arrives, but
612 * early enough for us. */
613 if (uMsg == WM_NCCREATE)
614 {
615 LogFlowFunc(("WM_NCCREATE\n"));
616
617 LPCREATESTRUCT pCS = (LPCREATESTRUCT)lParam;
618 AssertPtr(pCS);
619 SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)pCS->lpCreateParams);
620 SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)vboxClipboardSvcWinWndProcInstance);
621
622 return vboxClipboardSvcWinWndProcInstance(hWnd, uMsg, wParam, lParam);
623 }
624
625 /* No window associated yet. */
626 return DefWindowProc(hWnd, uMsg, wParam, lParam);
627}
628
629DECLCALLBACK(int) vboxClipboardSvcWinThread(RTTHREAD hThreadSelf, void *pvUser)
630{
631 LogFlowFuncEnter();
632
633 bool fThreadSignalled = false;
634
635 const PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pvUser;
636 AssertPtr(pCtx);
637 const PSHCLWINCTX pWinCtx = &pCtx->Win;
638
639 HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
640
641 /* Register the Window Class. */
642 WNDCLASS wc;
643 RT_ZERO(wc);
644
645 wc.style = CS_NOCLOSE;
646 wc.lpfnWndProc = vboxClipboardSvcWinWndProc;
647 wc.hInstance = hInstance;
648 wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
649
650 /* Register an unique wnd class name. */
651 char szWndClassName[32];
652 RTStrPrintf2(szWndClassName, sizeof(szWndClassName),
653 "%s-%RU64", SHCL_WIN_WNDCLASS_NAME, RTThreadGetNative(hThreadSelf));
654 wc.lpszClassName = szWndClassName;
655
656 int rc;
657
658 ATOM atomWindowClass = RegisterClass(&wc);
659 if (atomWindowClass == 0)
660 {
661 LogFunc(("Failed to register window class\n"));
662 rc = VERR_NOT_SUPPORTED;
663 }
664 else
665 {
666 /* Create a window and make it a clipboard viewer. */
667 pWinCtx->hWnd = CreateWindowEx(WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
668 szWndClassName, szWndClassName,
669 WS_POPUPWINDOW,
670 -200, -200, 100, 100, NULL, NULL, hInstance, pCtx /* lpParam */);
671 if (pWinCtx->hWnd == NULL)
672 {
673 LogFunc(("Failed to create window\n"));
674 rc = VERR_NOT_SUPPORTED;
675 }
676 else
677 {
678 SetWindowPos(pWinCtx->hWnd, HWND_TOPMOST, -200, -200, 0, 0,
679 SWP_NOACTIVATE | SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_NOSIZE);
680
681 rc = SharedClipboardWinChainAdd(&pCtx->Win);
682 if (RT_SUCCESS(rc))
683 {
684 if (!SharedClipboardWinIsNewAPI(&pWinCtx->newAPI))
685 pWinCtx->oldAPI.timerRefresh = SetTimer(pWinCtx->hWnd, 0, 10 * 1000, NULL);
686 }
687
688#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
689 if (RT_SUCCESS(rc))
690 {
691 HRESULT hr = OleInitialize(NULL);
692 if (FAILED(hr))
693 {
694 LogRel(("Shared Clipboard: Initializing window thread OLE failed (%Rhrc) -- file transfers unavailable\n", hr));
695 /* Not critical, the rest of the clipboard might work. */
696 }
697 else
698 LogRel(("Shared Clipboard: Initialized window thread OLE\n"));
699 }
700#endif
701 int rc2 = RTThreadUserSignal(hThreadSelf);
702 AssertRC(rc2);
703
704 fThreadSignalled = true;
705
706 MSG msg;
707 BOOL msgret = 0;
708 while ((msgret = GetMessage(&msg, NULL, 0, 0)) > 0)
709 {
710 TranslateMessage(&msg);
711 DispatchMessage(&msg);
712 }
713
714 /*
715 * Window procedure can return error, * but this is exceptional situation that should be
716 * identified in testing.
717 */
718 Assert(msgret >= 0);
719 LogFunc(("Message loop finished. GetMessage returned %d, message id: %d \n", msgret, msg.message));
720
721#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
722 OleSetClipboard(NULL); /* Make sure to flush the clipboard on destruction. */
723 OleUninitialize();
724#endif
725 }
726 }
727
728 pWinCtx->hWnd = NULL;
729
730 if (atomWindowClass != 0)
731 {
732 UnregisterClass(szWndClassName, hInstance);
733 atomWindowClass = 0;
734 }
735
736 if (!fThreadSignalled)
737 {
738 int rc2 = RTThreadUserSignal(hThreadSelf);
739 AssertRC(rc2);
740 }
741
742 LogFlowFuncLeaveRC(rc);
743 return rc;
744}
745
746/**
747 * Synchronizes the host and the guest clipboard formats by sending all supported host clipboard
748 * formats to the guest.
749 *
750 * @returns VBox status code, VINF_NO_CHANGE if no synchronization was required.
751 * @param pCtx Clipboard context to synchronize.
752 */
753static int vboxClipboardSvcWinSyncInternal(PSHCLCONTEXT pCtx)
754{
755 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
756
757 LogFlowFuncEnter();
758
759 int rc;
760
761 if (pCtx->pClient)
762 {
763 SHCLFORMATS fFormats = 0;
764 rc = SharedClipboardWinGetFormats(&pCtx->Win, &fFormats);
765 if (RT_SUCCESS(rc))
766 rc = ShClSvcReportFormats(pCtx->pClient, fFormats);
767 }
768 else /* If we don't have any client data (yet), bail out. */
769 rc = VINF_NO_CHANGE;
770
771 LogFlowFuncLeaveRC(rc);
772 return rc;
773}
774
775
776/*********************************************************************************************************************************
777* Backend implementation *
778*********************************************************************************************************************************/
779int ShClBackendInit(PSHCLBACKEND pBackend, VBOXHGCMSVCFNTABLE *pTable)
780{
781 RT_NOREF(pBackend, pTable);
782#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
783 HRESULT hr = OleInitialize(NULL);
784 if (FAILED(hr))
785 {
786 LogRel(("Shared Clipboard: Initializing OLE failed (%Rhrc) -- file transfers unavailable\n", hr));
787 /* Not critical, the rest of the clipboard might work. */
788 }
789 else
790 LogRel(("Shared Clipboard: Initialized OLE\n"));
791#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
792
793 return VINF_SUCCESS;
794}
795
796void ShClBackendDestroy(PSHCLBACKEND pBackend)
797{
798 RT_NOREF(pBackend);
799
800#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
801 OleSetClipboard(NULL); /* Make sure to flush the clipboard on destruction. */
802 OleUninitialize();
803#endif
804}
805
806int ShClBackendConnect(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, bool fHeadless)
807{
808 RT_NOREF(pBackend, fHeadless);
809
810 LogFlowFuncEnter();
811
812 int rc;
813
814 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)RTMemAllocZ(sizeof(SHCLCONTEXT));
815 if (pCtx)
816 {
817 rc = SharedClipboardWinCtxInit(&pCtx->Win);
818 if (RT_SUCCESS(rc))
819 {
820 rc = RTThreadCreate(&pCtx->hThread, vboxClipboardSvcWinThread, pCtx /* pvUser */, _64K /* Stack size */,
821 RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "ShClWin");
822 if (RT_SUCCESS(rc))
823 {
824 int rc2 = RTThreadUserWait(pCtx->hThread, RT_MS_30SEC /* Timeout in ms */);
825 AssertRC(rc2);
826 }
827 }
828
829 pClient->State.pCtx = pCtx;
830 pClient->State.pCtx->pClient = pClient;
831
832#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
833 /*
834 * Set callbacks.
835 * Those will be registered within ShClSvcTransferInit() when a new transfer gets initialized.
836 */
837 RT_ZERO(pClient->Transfers.Callbacks);
838
839 pClient->Transfers.Callbacks.pvUser = pCtx; /* Assign context as user-provided callback data. */
840 pClient->Transfers.Callbacks.cbUser = sizeof(SHCLCONTEXT);
841
842 pClient->Transfers.Callbacks.pfnOnCreated = shClSvcWinTransferOnCreatedCallback;
843 pClient->Transfers.Callbacks.pfnOnInitialize = shClSvcWinTransferOnInitializeCallback;
844 pClient->Transfers.Callbacks.pfnOnDestroy = shClSvcWinTransferOnDestroyCallback;
845#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
846 }
847 else
848 rc = VERR_NO_MEMORY;
849
850 LogFlowFuncLeaveRC(rc);
851 return rc;
852}
853
854int ShClBackendSync(PSHCLBACKEND pBackend, PSHCLCLIENT pClient)
855{
856 RT_NOREF(pBackend);
857
858 /* Sync the host clipboard content with the client. */
859 return vboxClipboardSvcWinSyncInternal(pClient->State.pCtx);
860}
861
862int ShClBackendDisconnect(PSHCLBACKEND pBackend, PSHCLCLIENT pClient)
863{
864 RT_NOREF(pBackend);
865
866 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
867
868 LogFlowFuncEnter();
869
870 int rc = VINF_SUCCESS;
871
872 PSHCLCONTEXT pCtx = pClient->State.pCtx;
873 if (pCtx)
874 {
875 if (pCtx->Win.hWnd)
876 PostMessage(pCtx->Win.hWnd, WM_DESTROY, 0 /* wParam */, 0 /* lParam */);
877
878 if (pCtx->hThread != NIL_RTTHREAD)
879 {
880 LogFunc(("Waiting for thread to terminate ...\n"));
881
882 /* Wait for the window thread to terminate. */
883 rc = RTThreadWait(pCtx->hThread, RT_MS_30SEC /* Timeout in ms */, NULL);
884 if (RT_FAILURE(rc))
885 LogRel(("Shared Clipboard: Waiting for window thread termination failed with rc=%Rrc\n", rc));
886
887 pCtx->hThread = NIL_RTTHREAD;
888 }
889
890 SharedClipboardWinCtxDestroy(&pCtx->Win);
891
892 if (RT_SUCCESS(rc))
893 {
894 RTMemFree(pCtx);
895 pCtx = NULL;
896
897 pClient->State.pCtx = NULL;
898 }
899 }
900
901 LogFlowFuncLeaveRC(rc);
902 return rc;
903}
904
905int ShClBackendReportFormats(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, SHCLFORMATS fFormats)
906{
907 RT_NOREF(pBackend);
908
909 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
910
911 PSHCLCONTEXT pCtx = pClient->State.pCtx;
912 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
913
914 LogFlowFunc(("fFormats=0x%x, hWnd=%p\n", fFormats, pCtx->Win.hWnd));
915
916 /*
917 * The guest announced formats. Forward to the window thread.
918 */
919 PostMessage(pCtx->Win.hWnd, SHCL_WIN_WM_REPORT_FORMATS, 0 /* wParam */, fFormats /* lParam */);
920
921 LogFlowFuncLeaveRC(VINF_SUCCESS);
922 return VINF_SUCCESS;
923}
924
925int ShClBackendReadData(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLCLIENTCMDCTX pCmdCtx,
926 SHCLFORMAT uFmt, void *pvData, uint32_t cbData, uint32_t *pcbActual)
927{
928 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
929 AssertPtrReturn(pCmdCtx, VERR_INVALID_POINTER);
930 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
931 AssertPtrReturn(pcbActual, VERR_INVALID_POINTER);
932
933 RT_NOREF(pBackend, pCmdCtx);
934
935 AssertPtrReturn(pClient->State.pCtx, VERR_INVALID_POINTER);
936
937 LogFlowFunc(("uFmt=%#x\n", uFmt));
938
939 HANDLE hClip = NULL;
940
941 const PSHCLWINCTX pWinCtx = &pClient->State.pCtx->Win;
942
943 /*
944 * The guest wants to read data in the given format.
945 */
946 int rc = SharedClipboardWinOpen(pWinCtx->hWnd);
947 if (RT_SUCCESS(rc))
948 {
949 if (uFmt & VBOX_SHCL_FMT_BITMAP)
950 {
951 LogFunc(("CF_DIB\n"));
952 hClip = GetClipboardData(CF_DIB);
953 if (hClip != NULL)
954 {
955 LPVOID lp = GlobalLock(hClip);
956 if (lp != NULL)
957 {
958 rc = vboxClipboardSvcWinDataGet(VBOX_SHCL_FMT_BITMAP, lp, GlobalSize(hClip),
959 pvData, cbData, pcbActual);
960 GlobalUnlock(hClip);
961 }
962 else
963 {
964 hClip = NULL;
965 }
966 }
967 }
968 else if (uFmt & VBOX_SHCL_FMT_UNICODETEXT)
969 {
970 LogFunc(("CF_UNICODETEXT\n"));
971 hClip = GetClipboardData(CF_UNICODETEXT);
972 if (hClip != NULL)
973 {
974 LPWSTR uniString = (LPWSTR)GlobalLock(hClip);
975 if (uniString != NULL)
976 {
977 rc = vboxClipboardSvcWinDataGet(VBOX_SHCL_FMT_UNICODETEXT, uniString, (lstrlenW(uniString) + 1) * 2,
978 pvData, cbData, pcbActual);
979 GlobalUnlock(hClip);
980 }
981 else
982 {
983 hClip = NULL;
984 }
985 }
986 }
987 else if (uFmt & VBOX_SHCL_FMT_HTML)
988 {
989 LogFunc(("SHCL_WIN_REGFMT_HTML\n"));
990 UINT uRegFmt = RegisterClipboardFormat(SHCL_WIN_REGFMT_HTML);
991 if (uRegFmt != 0)
992 {
993 hClip = GetClipboardData(uRegFmt);
994 if (hClip != NULL)
995 {
996 LPVOID lp = GlobalLock(hClip);
997 if (lp != NULL)
998 {
999 rc = vboxClipboardSvcWinDataGet(VBOX_SHCL_FMT_HTML, lp, GlobalSize(hClip),
1000 pvData, cbData, pcbActual);
1001#ifdef LOG_ENABLED
1002 if (RT_SUCCESS(rc))
1003 {
1004 LogFlowFunc(("Raw HTML clipboard data from host:\n"));
1005 ShClDbgDumpHtml((char *)pvData, cbData);
1006 }
1007#endif
1008 GlobalUnlock(hClip);
1009 }
1010 else
1011 {
1012 hClip = NULL;
1013 }
1014 }
1015 }
1016 }
1017#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1018 else if (uFmt & VBOX_SHCL_FMT_URI_LIST)
1019 {
1020 hClip = hClip = GetClipboardData(CF_HDROP);
1021 if (hClip)
1022 {
1023 HDROP hDrop = (HDROP)GlobalLock(hClip);
1024 if (hDrop)
1025 {
1026 char *pszList = NULL;
1027 uint32_t cbList;
1028 rc = SharedClipboardWinTransferDropFilesToStringList((DROPFILES *)hDrop, &pszList, &cbList);
1029
1030 GlobalUnlock(hClip);
1031
1032 if (RT_SUCCESS(rc))
1033 {
1034 if (cbList <= cbData)
1035 {
1036 memcpy(pvData, pszList, cbList);
1037 *pcbActual = cbList;
1038 }
1039
1040 RTStrFree(pszList);
1041 }
1042 }
1043 else
1044 LogRel(("Shared Clipboard: Unable to lock clipboard data, last error: %ld\n", GetLastError()));
1045 }
1046 else
1047 LogRel(("Shared Clipboard: Unable to retrieve clipboard data from clipboard (CF_HDROP), last error: %ld\n",
1048 GetLastError()));
1049 }
1050#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
1051 SharedClipboardWinClose();
1052 }
1053
1054 if (RT_FAILURE(rc))
1055 LogRel(("Shared Clipboard: Error reading host clipboard data in format %#x from Windows, rc=%Rrc\n", uFmt, rc));
1056
1057 LogFlowFuncLeaveRC(rc);
1058 return rc;
1059}
1060
1061int ShClBackendWriteData(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLCLIENTCMDCTX pCmdCtx,
1062 SHCLFORMAT uFormat, void *pvData, uint32_t cbData)
1063{
1064 RT_NOREF(pBackend, pClient, pCmdCtx, uFormat, pvData, cbData);
1065
1066 LogFlowFuncEnter();
1067
1068 /* Nothing to do here yet. */
1069
1070 LogFlowFuncLeave();
1071 return VINF_SUCCESS;
1072}
1073
1074#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1075/**
1076 * Handles transfer status replies from the guest.
1077 */
1078int ShClBackendTransferHandleStatusReply(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLTRANSFER pTransfer, SHCLSOURCE enmSource, SHCLTRANSFERSTATUS enmStatus, int rcStatus)
1079{
1080 RT_NOREF(pBackend, pClient, pTransfer, enmSource, enmStatus, rcStatus);
1081
1082 return VINF_SUCCESS;
1083}
1084
1085
1086/*********************************************************************************************************************************
1087* Provider interface implementation *
1088*********************************************************************************************************************************/
1089
1090/** @copydoc SHCLTXPROVIDERIFACE::pfnRootListRead */
1091static DECLCALLBACK(int) shClSvcWinTransferIfaceHGRootListRead(PSHCLTXPROVIDERCTX pCtx)
1092{
1093 LogFlowFuncEnter();
1094
1095 PSHCLCLIENT pClient = (PSHCLCLIENT)pCtx->pvUser;
1096 AssertPtr(pClient);
1097
1098 AssertPtr(pClient->State.pCtx);
1099 PSHCLWINCTX pWin = &pClient->State.pCtx->Win;
1100
1101 int rc = SharedClipboardWinTransferGetRootsFromClipboard(pWin, pCtx->pTransfer);
1102
1103 LogFlowFuncLeaveRC(rc);
1104 return rc;
1105}
1106#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
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