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