1 | /* $Id: VBoxClipboard.cpp 103615 2024-02-29 18:36:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxClipboard - Shared clipboard, Windows Guest Implementation.
|
---|
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 <VBox/log.h>
|
---|
34 |
|
---|
35 | #include "VBoxTray.h"
|
---|
36 | #include "VBoxHelpers.h"
|
---|
37 |
|
---|
38 | #include <iprt/asm.h>
|
---|
39 | #include <iprt/errcore.h>
|
---|
40 | #include <iprt/ldr.h>
|
---|
41 | #include <iprt/mem.h>
|
---|
42 | #include <iprt/utf16.h>
|
---|
43 |
|
---|
44 | #include <VBox/GuestHost/SharedClipboard.h>
|
---|
45 | #include <VBox/GuestHost/SharedClipboard-win.h>
|
---|
46 | #include <VBox/GuestHost/clipboard-helper.h>
|
---|
47 | #include <VBox/HostServices/VBoxClipboardSvc.h> /* Temp, remove. */
|
---|
48 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
49 | # include <VBox/GuestHost/SharedClipboard-transfers.h>
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
53 | # include <iprt/win/shlobj.h>
|
---|
54 | # include <iprt/win/shlwapi.h>
|
---|
55 | #endif
|
---|
56 |
|
---|
57 |
|
---|
58 | /*********************************************************************************************************************************
|
---|
59 | * Structures and Typedefs *
|
---|
60 | *********************************************************************************************************************************/
|
---|
61 | struct SHCLCONTEXT
|
---|
62 | {
|
---|
63 | /** Pointer to the VBoxClient service environment. */
|
---|
64 | const VBOXSERVICEENV *pEnv;
|
---|
65 | /** Command context. */
|
---|
66 | VBGLR3SHCLCMDCTX CmdCtx;
|
---|
67 | /** Windows-specific context data. */
|
---|
68 | SHCLWINCTX Win;
|
---|
69 | /** Thread handle for window thread. */
|
---|
70 | RTTHREAD hThread;
|
---|
71 | /** Start indicator flag. */
|
---|
72 | bool fStarted;
|
---|
73 | /** Shutdown indicator flag. */
|
---|
74 | bool fShutdown;
|
---|
75 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
76 | /** Associated transfer data. */
|
---|
77 | SHCLTRANSFERCTX TransferCtx;
|
---|
78 | #endif
|
---|
79 | };
|
---|
80 |
|
---|
81 |
|
---|
82 | /*********************************************************************************************************************************
|
---|
83 | * Static variables *
|
---|
84 | *********************************************************************************************************************************/
|
---|
85 | /** Static clipboard context (since it is the single instance). Directly used in the windows proc. */
|
---|
86 | static SHCLCONTEXT g_Ctx = { NULL };
|
---|
87 | /** Static window class name. */
|
---|
88 | static char s_szClipWndClassName[] = SHCL_WIN_WNDCLASS_NAME;
|
---|
89 |
|
---|
90 |
|
---|
91 | /*********************************************************************************************************************************
|
---|
92 | * Prototypes *
|
---|
93 | *********************************************************************************************************************************/
|
---|
94 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
95 | static DECLCALLBACK(void) vbtrShClTransferCreatedCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx);
|
---|
96 | static DECLCALLBACK(void) vbtrShClTransferDestroyCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx);
|
---|
97 | static DECLCALLBACK(void) vbtrShClTransferInitializedCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx);
|
---|
98 | static DECLCALLBACK(void) vbtrShClTransferStartedCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx);
|
---|
99 | static DECLCALLBACK(void) vbtrShClTransferErrorCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx, int rc);
|
---|
100 | #endif
|
---|
101 |
|
---|
102 |
|
---|
103 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
104 | /**
|
---|
105 | * @copydoc SharedClipboardWinDataObject::CALLBACKS::pfnTransferBegin
|
---|
106 | *
|
---|
107 | * Called by SharedClipboardWinDataObject::GetData() when the user wants to paste data.
|
---|
108 | * This then requests a new transfer on the host.
|
---|
109 | *
|
---|
110 | * @thread Clipboard main thread.
|
---|
111 | */
|
---|
112 | static DECLCALLBACK(int) vbtrShClDataObjectTransferBeginCallback(SharedClipboardWinDataObject::PCALLBACKCTX pCbCtx)
|
---|
113 | {
|
---|
114 | LogFlowFuncEnter();
|
---|
115 |
|
---|
116 | PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pCbCtx->pvUser;
|
---|
117 | AssertPtr(pCtx);
|
---|
118 |
|
---|
119 | int rc = VbglR3ClipboardTransferRequest(&pCtx->CmdCtx);
|
---|
120 |
|
---|
121 | LogFlowFuncLeaveRC(rc);
|
---|
122 | return rc;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * @copydoc SHCLTRANSFERCALLBACKS::pfnOnCreated
|
---|
127 | *
|
---|
128 | * Called by ShClTransferCreate via VbglR3.
|
---|
129 | *
|
---|
130 | * @thread Clipboard main thread.
|
---|
131 | */
|
---|
132 | static DECLCALLBACK(void) vbtrShClTransferCreatedCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx)
|
---|
133 | {
|
---|
134 | LogFlowFuncEnter();
|
---|
135 |
|
---|
136 | PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pCbCtx->pvUser;
|
---|
137 | AssertPtr(pCtx);
|
---|
138 |
|
---|
139 | int rc = SharedClipboardWinTransferCreate(&pCtx->Win, pCbCtx->pTransfer);
|
---|
140 |
|
---|
141 | LogFlowFuncLeaveRC(rc);
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * @copydoc SHCLTRANSFERCALLBACKS::pfnOnDestroy
|
---|
146 | *
|
---|
147 | * Called by ShClTransferDestroy via VbglR3.
|
---|
148 | *
|
---|
149 | * @thread Clipboard main thread.
|
---|
150 | */
|
---|
151 | static DECLCALLBACK(void) vbtrShClTransferDestroyCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx)
|
---|
152 | {
|
---|
153 | LogFlowFuncEnter();
|
---|
154 |
|
---|
155 | PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pCbCtx->pvUser;
|
---|
156 | AssertPtr(pCtx);
|
---|
157 |
|
---|
158 | SharedClipboardWinTransferDestroy(&pCtx->Win, pCbCtx->pTransfer);
|
---|
159 |
|
---|
160 | LogFlowFuncLeave();
|
---|
161 | }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * @copydoc SHCLTRANSFERCALLBACKS::pfnOnInitialize
|
---|
165 | *
|
---|
166 | * Called by ShClTransferInit via VbglR3.
|
---|
167 | * For H->G: Called on transfer intialization to initialize the "in-flight" IDataObject for a data transfer.
|
---|
168 | * For G->H: Called on transfer intialization to populate the transfer's root list.
|
---|
169 | *
|
---|
170 | * @thread Clipboard main thread.
|
---|
171 | */
|
---|
172 | static DECLCALLBACK(int) vbtrShClTransferInitializeCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx)
|
---|
173 | {
|
---|
174 | LogFlowFuncEnter();
|
---|
175 |
|
---|
176 | int rc = VINF_SUCCESS;
|
---|
177 |
|
---|
178 | PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pCbCtx->pvUser;
|
---|
179 | AssertPtr(pCtx);
|
---|
180 |
|
---|
181 | PSHCLTRANSFER pTransfer = pCbCtx->pTransfer;
|
---|
182 | AssertPtr(pTransfer);
|
---|
183 |
|
---|
184 | switch(ShClTransferGetDir(pTransfer))
|
---|
185 | {
|
---|
186 | case SHCLTRANSFERDIR_FROM_REMOTE: /* H->G */
|
---|
187 | {
|
---|
188 | rc = SharedClipboardWinTransferInitialize(&pCtx->Win, pTransfer);
|
---|
189 | break;
|
---|
190 | }
|
---|
191 |
|
---|
192 | case SHCLTRANSFERDIR_TO_REMOTE: /* G->H */
|
---|
193 | {
|
---|
194 | rc = SharedClipboardWinTransferGetRootsFromClipboard(&pCtx->Win, pTransfer);
|
---|
195 | break;
|
---|
196 | }
|
---|
197 |
|
---|
198 | default:
|
---|
199 | break;
|
---|
200 | }
|
---|
201 |
|
---|
202 | LogFlowFuncLeaveRC(rc);
|
---|
203 | return rc;
|
---|
204 | }
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * @copydoc SHCLTRANSFERCALLBACKS::pfnOnInitialized
|
---|
208 | *
|
---|
209 | * Called by ShClTransferInit via VbglR3.
|
---|
210 | * For H->G: Called on transfer intialization to start the data transfer for the "in-flight" IDataObject.
|
---|
211 | * For G->H: Nothing to do here.
|
---|
212 | *
|
---|
213 | * @thread Clipboard main thread.
|
---|
214 | */
|
---|
215 | static DECLCALLBACK(void) vbtrShClTransferInitializedCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx)
|
---|
216 | {
|
---|
217 | LogFlowFuncEnter();
|
---|
218 |
|
---|
219 | int rc = VINF_SUCCESS;
|
---|
220 |
|
---|
221 | PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pCbCtx->pvUser;
|
---|
222 | AssertPtr(pCtx);
|
---|
223 |
|
---|
224 | PSHCLTRANSFER pTransfer = pCbCtx->pTransfer;
|
---|
225 | AssertPtr(pTransfer);
|
---|
226 |
|
---|
227 | switch(ShClTransferGetDir(pTransfer))
|
---|
228 | {
|
---|
229 | case SHCLTRANSFERDIR_FROM_REMOTE: /* H->G */
|
---|
230 | {
|
---|
231 | rc = SharedClipboardWinTransferStart(&pCtx->Win, pTransfer);
|
---|
232 | break;
|
---|
233 | }
|
---|
234 |
|
---|
235 | case SHCLTRANSFERDIR_TO_REMOTE: /* G->H */
|
---|
236 | break;
|
---|
237 |
|
---|
238 | default:
|
---|
239 | break;
|
---|
240 | }
|
---|
241 |
|
---|
242 | LogFlowFuncLeaveRC(rc);
|
---|
243 | }
|
---|
244 |
|
---|
245 | /**
|
---|
246 | * Worker for a reading clipboard from the host.
|
---|
247 | *
|
---|
248 | * @returns VBox status code.
|
---|
249 | * @retval VERR_SHCLPB_NO_DATA if no clipboard data is available.
|
---|
250 | * @param pCtx Shared Clipbaord context to use.
|
---|
251 | * @param uFmt The format to read clipboard data in.
|
---|
252 | * @param ppvData Where to return the allocated data read.
|
---|
253 | * Must be free'd by the caller.
|
---|
254 | * @param pcbData Where to return number of bytes read.
|
---|
255 | * @param pvUser User-supplied context.
|
---|
256 | *
|
---|
257 | * @thread Clipboard main thread.
|
---|
258 | *
|
---|
259 | */
|
---|
260 | static DECLCALLBACK(int) vbtrShClRequestDataFromSourceCallbackWorker(PSHCLCONTEXT pCtx,
|
---|
261 | SHCLFORMAT uFmt, void **ppvData, uint32_t *pcbData, void *pvUser)
|
---|
262 | {
|
---|
263 | RT_NOREF(pvUser);
|
---|
264 |
|
---|
265 | return VbglR3ClipboardReadDataEx(&pCtx->CmdCtx, uFmt, ppvData, pcbData);
|
---|
266 | }
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * @copydoc SHCLCALLBACKS::pfnOnRequestDataFromSource
|
---|
270 | *
|
---|
271 | * Called from the IDataObject implementation to request data from the host.
|
---|
272 | *
|
---|
273 | * @thread shclwnd thread.
|
---|
274 | */
|
---|
275 | DECLCALLBACK(int) vbtrShClRequestDataFromSourceCallback(PSHCLCONTEXT pCtx,
|
---|
276 | SHCLFORMAT uFmt, void **ppv, uint32_t *pcb, void *pvUser)
|
---|
277 | {
|
---|
278 | PRTREQ pReq = NULL;
|
---|
279 | int rc = RTReqQueueCallEx(pCtx->Win.hReqQ, &pReq, SHCL_TIMEOUT_DEFAULT_MS, RTREQFLAGS_IPRT_STATUS,
|
---|
280 | (PFNRT)vbtrShClRequestDataFromSourceCallbackWorker, 5, pCtx, uFmt, ppv, pcb, pvUser);
|
---|
281 | RTReqRelease(pReq);
|
---|
282 | return rc;
|
---|
283 | }
|
---|
284 |
|
---|
285 | /**
|
---|
286 | * @copydoc SHCLTRANSFERCALLBACKS::pfnOnStart
|
---|
287 | *
|
---|
288 | * Called from VbglR3 (main thread) to notify the IDataObject.
|
---|
289 | *
|
---|
290 | * @thread Clipboard main thread.
|
---|
291 | */
|
---|
292 | static DECLCALLBACK(void) vbtrShClTransferStartedCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx)
|
---|
293 | {
|
---|
294 | PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pCbCtx->pvUser;
|
---|
295 | AssertPtr(pCtx);
|
---|
296 |
|
---|
297 | PSHCLTRANSFER pTransfer = pCbCtx->pTransfer;
|
---|
298 | AssertPtr(pTransfer);
|
---|
299 |
|
---|
300 | SHCLTRANSFERDIR const enmDir = ShClTransferGetDir(pTransfer);
|
---|
301 |
|
---|
302 | int rc = VINF_SUCCESS;
|
---|
303 |
|
---|
304 | /* The guest wants to transfer data to the host. */
|
---|
305 | if (enmDir == SHCLTRANSFERDIR_TO_REMOTE) /* G->H */
|
---|
306 | {
|
---|
307 | rc = SharedClipboardWinTransferGetRootsFromClipboard(&pCtx->Win, pTransfer);
|
---|
308 | }
|
---|
309 | else if (enmDir == SHCLTRANSFERDIR_FROM_REMOTE) /* H->G */
|
---|
310 | {
|
---|
311 | /* Nothing to do here. */
|
---|
312 | }
|
---|
313 | else
|
---|
314 | AssertFailedStmt(rc = VERR_NOT_SUPPORTED);
|
---|
315 |
|
---|
316 | if (RT_FAILURE(rc))
|
---|
317 | LogRel(("Shared Clipboard: Starting transfer failed, rc=%Rrc\n", rc));
|
---|
318 | }
|
---|
319 |
|
---|
320 | /** @copydoc SHCLTRANSFERCALLBACKS::pfnOnCompleted */
|
---|
321 | static DECLCALLBACK(void) vbtrShClTransferCompletedCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx, int rcCompletion)
|
---|
322 | {
|
---|
323 | PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pCbCtx->pvUser;
|
---|
324 | AssertPtr(pCtx);
|
---|
325 |
|
---|
326 | LogFlowFunc(("rcCompletion=%Rrc\n", rcCompletion));
|
---|
327 |
|
---|
328 | LogRel2(("Shared Clipboard: Transfer %RU16 %s\n",
|
---|
329 | ShClTransferGetID(pCbCtx->pTransfer), rcCompletion == VERR_CANCELLED ? "canceled" : "complete"));
|
---|
330 |
|
---|
331 | SHCLTRANSFERSTATUS enmSts;
|
---|
332 |
|
---|
333 | switch (rcCompletion)
|
---|
334 | {
|
---|
335 | case VERR_CANCELLED:
|
---|
336 | enmSts = SHCLTRANSFERSTATUS_CANCELED;
|
---|
337 | break;
|
---|
338 |
|
---|
339 | case VINF_SUCCESS:
|
---|
340 | enmSts = SHCLTRANSFERSTATUS_COMPLETED;
|
---|
341 | break;
|
---|
342 |
|
---|
343 | default:
|
---|
344 | AssertFailedStmt(enmSts = SHCLTRANSFERSTATUS_ERROR);
|
---|
345 | break;
|
---|
346 | }
|
---|
347 |
|
---|
348 | int rc = VbglR3ClipboardTransferSendStatus(&pCtx->CmdCtx, pCbCtx->pTransfer, enmSts, rcCompletion);
|
---|
349 | LogFlowFuncLeaveRC(rc);
|
---|
350 | }
|
---|
351 |
|
---|
352 | /** @copydoc SHCLTRANSFERCALLBACKS::pfnOnError */
|
---|
353 | static DECLCALLBACK(void) vbtrShClTransferErrorCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx, int rcError)
|
---|
354 | {
|
---|
355 | PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pCbCtx->pvUser;
|
---|
356 | AssertPtr(pCtx);
|
---|
357 |
|
---|
358 | LogRel(("Shared Clipboard: Transfer %RU16 failed with %Rrc\n", ShClTransferGetID(pCbCtx->pTransfer), rcError));
|
---|
359 |
|
---|
360 | if (g_cVerbosity) /* Only show this in verbose mode. */
|
---|
361 | {
|
---|
362 | char szMsg [256]; /* Sizes according to MSDN. */
|
---|
363 | char szTitle[64];
|
---|
364 |
|
---|
365 | /** @todo Add some translation macros here. */
|
---|
366 | RTStrPrintf(szTitle, sizeof(szTitle), "VirtualBox Shared Clipboard");
|
---|
367 | RTStrPrintf(szMsg, sizeof(szMsg),
|
---|
368 | "Transfer %RU16 failed with %Rrc", ShClTransferGetID(pCbCtx->pTransfer), rcError);
|
---|
369 |
|
---|
370 | hlpShowBalloonTip(g_hInstance, g_hwndToolWindow, ID_TRAYICON,
|
---|
371 | szMsg, szTitle,
|
---|
372 | 5000 /* Time to display in msec */, NIIF_INFO);
|
---|
373 | }
|
---|
374 |
|
---|
375 | int rc = VbglR3ClipboardTransferSendStatus(&pCtx->CmdCtx, pCbCtx->pTransfer, SHCLTRANSFERSTATUS_ERROR, rcError);
|
---|
376 | LogFlowFuncLeaveRC(rc);
|
---|
377 | }
|
---|
378 | #endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
|
---|
379 |
|
---|
380 | static LRESULT vbtrShClWndProcWorker(PSHCLCONTEXT pCtx, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
|
---|
381 | {
|
---|
382 | AssertPtr(pCtx);
|
---|
383 |
|
---|
384 | const PSHCLWINCTX pWinCtx = &pCtx->Win;
|
---|
385 |
|
---|
386 | LRESULT lresultRc = 0;
|
---|
387 |
|
---|
388 | switch (msg)
|
---|
389 | {
|
---|
390 | case WM_CLIPBOARDUPDATE:
|
---|
391 | {
|
---|
392 | LogFunc(("WM_CLIPBOARDUPDATE: pWinCtx=%p\n", pWinCtx));
|
---|
393 |
|
---|
394 | if (pCtx->fShutdown) /* If we're about to shut down, skip handling stuff here. */
|
---|
395 | break;
|
---|
396 |
|
---|
397 | int rc = RTCritSectEnter(&pWinCtx->CritSect);
|
---|
398 | if (RT_SUCCESS(rc))
|
---|
399 | {
|
---|
400 | const HWND hWndClipboardOwner = GetClipboardOwner();
|
---|
401 |
|
---|
402 | LogFunc(("WM_CLIPBOARDUPDATE: hWndOldClipboardOwner=%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.
|
---|
411 | * Report available formats to the host. */
|
---|
412 | SHCLFORMATS fFormats;
|
---|
413 | rc = SharedClipboardWinGetFormats(pWinCtx, &fFormats);
|
---|
414 | if (RT_SUCCESS(rc))
|
---|
415 | {
|
---|
416 | LogFunc(("WM_CLIPBOARDUPDATE: Reporting formats %#x\n", fFormats));
|
---|
417 | rc = VbglR3ClipboardReportFormats(pCtx->CmdCtx.idClient, fFormats);
|
---|
418 | }
|
---|
419 | }
|
---|
420 | else
|
---|
421 | {
|
---|
422 | int rc2 = RTCritSectLeave(&pWinCtx->CritSect);
|
---|
423 | AssertRC(rc2);
|
---|
424 | }
|
---|
425 | }
|
---|
426 |
|
---|
427 | if (RT_FAILURE(rc))
|
---|
428 | LogRel(("Shared Clipboard: WM_CLIPBOARDUPDATE failed with %Rrc\n", rc));
|
---|
429 |
|
---|
430 | break;
|
---|
431 | }
|
---|
432 |
|
---|
433 | case WM_CHANGECBCHAIN:
|
---|
434 | {
|
---|
435 | LogFunc(("WM_CHANGECBCHAIN\n"));
|
---|
436 | lresultRc = SharedClipboardWinHandleWMChangeCBChain(pWinCtx, hwnd, msg, wParam, lParam);
|
---|
437 | break;
|
---|
438 | }
|
---|
439 |
|
---|
440 | case WM_DRAWCLIPBOARD:
|
---|
441 | {
|
---|
442 | LogFlowFunc(("WM_DRAWCLIPBOARD: pWinCtx=%p\n", pWinCtx));
|
---|
443 |
|
---|
444 | int rc = RTCritSectEnter(&pWinCtx->CritSect);
|
---|
445 | if (RT_SUCCESS(rc))
|
---|
446 | {
|
---|
447 | const HWND hWndClipboardOwner = GetClipboardOwner();
|
---|
448 |
|
---|
449 | LogFunc(("WM_DRAWCLIPBOARD: hWndClipboardOwnerUs=%p, hWndNewClipboardOwner=%p\n",
|
---|
450 | pWinCtx->hWndClipboardOwnerUs, hWndClipboardOwner));
|
---|
451 |
|
---|
452 | if (pWinCtx->hWndClipboardOwnerUs != hWndClipboardOwner)
|
---|
453 | {
|
---|
454 | int rc2 = RTCritSectLeave(&pWinCtx->CritSect);
|
---|
455 | AssertRC(rc2);
|
---|
456 |
|
---|
457 | /* Clipboard was updated by another application. */
|
---|
458 | /* WM_DRAWCLIPBOARD always expects a return code of 0, so don't change "rc" here. */
|
---|
459 | SHCLFORMATS fFormats;
|
---|
460 | rc = SharedClipboardWinGetFormats(pWinCtx, &fFormats);
|
---|
461 | if ( RT_SUCCESS(rc)
|
---|
462 | && fFormats != VBOX_SHCL_FMT_NONE)
|
---|
463 | rc = VbglR3ClipboardReportFormats(pCtx->CmdCtx.idClient, fFormats);
|
---|
464 | }
|
---|
465 | else
|
---|
466 | {
|
---|
467 | int rc2 = RTCritSectLeave(&pWinCtx->CritSect);
|
---|
468 | AssertRC(rc2);
|
---|
469 | }
|
---|
470 | }
|
---|
471 |
|
---|
472 | lresultRc = SharedClipboardWinChainPassToNext(pWinCtx, msg, wParam, lParam);
|
---|
473 | break;
|
---|
474 | }
|
---|
475 |
|
---|
476 | case WM_TIMER:
|
---|
477 | {
|
---|
478 | int rc = SharedClipboardWinHandleWMTimer(pWinCtx);
|
---|
479 | AssertRC(rc);
|
---|
480 |
|
---|
481 | break;
|
---|
482 | }
|
---|
483 |
|
---|
484 | case WM_CLOSE:
|
---|
485 | {
|
---|
486 | /* Do nothing. Ignore the message. */
|
---|
487 | break;
|
---|
488 | }
|
---|
489 |
|
---|
490 | case WM_RENDERFORMAT: /* Guest wants to render the clipboard data. */
|
---|
491 | {
|
---|
492 | /* Insert the requested clipboard format data into the clipboard. */
|
---|
493 | const UINT uFmtWin = (UINT)wParam;
|
---|
494 | const SHCLFORMAT uFmtVBox = SharedClipboardWinClipboardFormatToVBox(uFmtWin);
|
---|
495 |
|
---|
496 | LogFunc(("WM_RENDERFORMAT: uFmtWin=%u -> uFmtVBox=0x%x\n", uFmtWin, uFmtVBox));
|
---|
497 | #ifdef LOG_ENABLED
|
---|
498 | char *pszFmts = ShClFormatsToStrA(uFmtVBox);
|
---|
499 | AssertPtrReturn(pszFmts, 0);
|
---|
500 | LogRel(("Shared Clipboard: Rendering Windows format %#x as VBox format '%s'\n", uFmtWin, pszFmts));
|
---|
501 | RTStrFree(pszFmts);
|
---|
502 | #endif
|
---|
503 | if (uFmtVBox == VBOX_SHCL_FMT_NONE)
|
---|
504 | {
|
---|
505 | LogRel(("Shared Clipboard: Unsupported format (%#x) requested\n", uFmtWin));
|
---|
506 | SharedClipboardWinClear();
|
---|
507 | }
|
---|
508 | else
|
---|
509 | {
|
---|
510 | void *pvData = NULL;
|
---|
511 | uint32_t cbData;
|
---|
512 |
|
---|
513 | HANDLE hMem;
|
---|
514 | int rc = VbglR3ClipboardReadDataEx(&pCtx->CmdCtx, uFmtVBox, &pvData, &cbData);
|
---|
515 | if (RT_SUCCESS(rc))
|
---|
516 | {
|
---|
517 | /* Verify the size of returned text, the memory block for clipboard must have the exact string size. */
|
---|
518 | if (uFmtVBox == VBOX_SHCL_FMT_UNICODETEXT)
|
---|
519 | {
|
---|
520 | size_t cwcActual = 0;
|
---|
521 | rc = RTUtf16NLenEx((PCRTUTF16)pvData, cbData / sizeof(RTUTF16), &cwcActual);
|
---|
522 | if (RT_SUCCESS(rc))
|
---|
523 | cbData = (uint32_t)((cwcActual + 1 /* '\0' */) * sizeof(RTUTF16));
|
---|
524 | else
|
---|
525 | LogRel(("Shared Clipboard: Invalid UTF16 string from host: cb=%RU32, cwcActual=%zu, rc=%Rrc\n",
|
---|
526 | cbData, cwcActual, rc));
|
---|
527 | }
|
---|
528 | else if (uFmtVBox == VBOX_SHCL_FMT_HTML)
|
---|
529 | {
|
---|
530 | /* Wrap content into CF_HTML clipboard format if needed. */
|
---|
531 | if (!SharedClipboardWinIsCFHTML((const char *)pvData))
|
---|
532 | {
|
---|
533 | char *pszWrapped = NULL;
|
---|
534 | uint32_t cbWrapped = 0;
|
---|
535 | rc = SharedClipboardWinConvertMIMEToCFHTML((const char *)pvData, cbData, &pszWrapped, &cbWrapped);
|
---|
536 | if (RT_SUCCESS(rc))
|
---|
537 | {
|
---|
538 | AssertBreakStmt(cbWrapped, rc = VERR_INVALID_PARAMETER);
|
---|
539 | pvData = RTMemRealloc(pvData, cbWrapped);
|
---|
540 | if (pvData)
|
---|
541 | {
|
---|
542 | memcpy(pvData, pszWrapped, cbWrapped);
|
---|
543 | cbData = cbWrapped;
|
---|
544 | }
|
---|
545 | else
|
---|
546 | rc = VERR_NO_MEMORY;
|
---|
547 | RTMemFree(pszWrapped);
|
---|
548 | }
|
---|
549 |
|
---|
550 | if (RT_FAILURE(rc))
|
---|
551 | LogRel(("Shared Clipboard: Cannot convert HTML clipboard data into CF_HTML clipboard format, rc=%Rrc\n", rc));
|
---|
552 | }
|
---|
553 | }
|
---|
554 |
|
---|
555 | hMem = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, cbData);
|
---|
556 | if (hMem)
|
---|
557 | {
|
---|
558 | void *pvMem = GlobalLock(hMem);
|
---|
559 | if (pvMem)
|
---|
560 | {
|
---|
561 | memcpy(pvMem, pvData, cbData);
|
---|
562 | GlobalUnlock(hMem);
|
---|
563 |
|
---|
564 | HANDLE hClip = SetClipboardData(uFmtWin, hMem);
|
---|
565 | if (!hClip)
|
---|
566 | {
|
---|
567 | /* The hMem ownership has gone to the system. Finish the processing. */
|
---|
568 | break;
|
---|
569 | }
|
---|
570 | else
|
---|
571 | LogRel(("Shared Clipboard: Setting host data buffer to clipboard failed with %Rrc\n",
|
---|
572 | RTErrConvertFromWin32(GetLastError())));
|
---|
573 | }
|
---|
574 | else
|
---|
575 | LogRel(("Shared Clipboard: Failed to lock memory (%Rrc)\n", RTErrConvertFromWin32(GetLastError())));
|
---|
576 | GlobalFree(hMem);
|
---|
577 | }
|
---|
578 | else
|
---|
579 | LogRel(("Shared Clipboard: No memory for allocating host data buffer\n"));
|
---|
580 | }
|
---|
581 | }
|
---|
582 |
|
---|
583 | break;
|
---|
584 | }
|
---|
585 |
|
---|
586 | case WM_RENDERALLFORMATS:
|
---|
587 | {
|
---|
588 | LogFunc(("WM_RENDERALLFORMATS\n"));
|
---|
589 |
|
---|
590 | int rc = SharedClipboardWinHandleWMRenderAllFormats(pWinCtx, hwnd);
|
---|
591 | AssertRC(rc);
|
---|
592 |
|
---|
593 | break;
|
---|
594 | }
|
---|
595 |
|
---|
596 | case SHCL_WIN_WM_REPORT_FORMATS: /* Host reported clipboard formats. */
|
---|
597 | {
|
---|
598 | LogFunc(("SHCL_WIN_WM_REPORT_FORMATS\n"));
|
---|
599 |
|
---|
600 | /* Announce available formats. Do not insert data -- will be inserted in WM_RENDERFORMAT. */
|
---|
601 | PVBGLR3CLIPBOARDEVENT pEvent = (PVBGLR3CLIPBOARDEVENT)lParam;
|
---|
602 | AssertPtr(pEvent);
|
---|
603 | Assert(pEvent->enmType == VBGLR3CLIPBOARDEVENTTYPE_REPORT_FORMATS);
|
---|
604 |
|
---|
605 | const SHCLFORMATS fFormats = pEvent->u.fReportedFormats;
|
---|
606 |
|
---|
607 | #ifdef LOG_ENABLED
|
---|
608 | char *pszFmts = ShClFormatsToStrA(fFormats);
|
---|
609 | AssertPtrReturn(pszFmts, 0);
|
---|
610 | LogRel(("Shared Clipboard: Host reported formats '%s'\n", pszFmts));
|
---|
611 | RTStrFree(pszFmts);
|
---|
612 | #endif
|
---|
613 | if (fFormats != VBOX_SHCL_FMT_NONE) /* Could arrive with some older GA versions. */
|
---|
614 | {
|
---|
615 | int rc = SharedClipboardWinClearAndAnnounceFormats(pWinCtx, fFormats, hwnd);
|
---|
616 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
617 | if ( RT_SUCCESS(rc)
|
---|
618 | && fFormats & VBOX_SHCL_FMT_URI_LIST)
|
---|
619 | {
|
---|
620 | /*
|
---|
621 | * Create our IDataObject implementation and push it to the Windows clibpoard.
|
---|
622 | * That way Windows will recognize that there is a data transfer available.
|
---|
623 | */
|
---|
624 | SharedClipboardWinDataObject::CALLBACKS Callbacks;
|
---|
625 | RT_ZERO(Callbacks);
|
---|
626 | Callbacks.pfnTransferBegin = vbtrShClDataObjectTransferBeginCallback;
|
---|
627 |
|
---|
628 | rc = SharedClipboardWinTransferCreateAndSetDataObject(pWinCtx, pCtx, &Callbacks);
|
---|
629 | }
|
---|
630 | #else
|
---|
631 | RT_NOREF(rc);
|
---|
632 | #endif
|
---|
633 | }
|
---|
634 |
|
---|
635 | LogFunc(("SHCL_WIN_WM_REPORT_FORMATS: fFormats=0x%x, lastErr=%ld\n", fFormats, GetLastError()));
|
---|
636 | break;
|
---|
637 | }
|
---|
638 |
|
---|
639 | case SHCL_WIN_WM_READ_DATA: /* Host wants to read clipboard data from the guest. */
|
---|
640 | {
|
---|
641 | /* Send data in the specified format to the host. */
|
---|
642 | PVBGLR3CLIPBOARDEVENT pEvent = (PVBGLR3CLIPBOARDEVENT)lParam;
|
---|
643 | AssertPtr(pEvent);
|
---|
644 | Assert(pEvent->enmType == VBGLR3CLIPBOARDEVENTTYPE_READ_DATA);
|
---|
645 |
|
---|
646 | const SHCLFORMAT fFormat = (uint32_t)pEvent->u.fReadData;
|
---|
647 |
|
---|
648 | LogFlowFunc(("SHCL_WIN_WM_READ_DATA: fFormat=%#x\n", fFormat));
|
---|
649 | #ifdef LOG_ENABLED
|
---|
650 | char *pszFmts = ShClFormatsToStrA(fFormat);
|
---|
651 | AssertPtrReturn(pszFmts, 0);
|
---|
652 | LogRel(("Shared Clipboard: Sending data to host as '%s'\n", pszFmts));
|
---|
653 | RTStrFree(pszFmts);
|
---|
654 | #endif
|
---|
655 | int rc = SharedClipboardWinOpen(hwnd);
|
---|
656 | HANDLE hClip = NULL;
|
---|
657 | if (RT_SUCCESS(rc))
|
---|
658 | {
|
---|
659 | if (fFormat & VBOX_SHCL_FMT_BITMAP)
|
---|
660 | {
|
---|
661 | hClip = GetClipboardData(CF_DIB);
|
---|
662 | if (hClip != NULL)
|
---|
663 | {
|
---|
664 | LPVOID lp = GlobalLock(hClip);
|
---|
665 | if (lp != NULL)
|
---|
666 | {
|
---|
667 | rc = VbglR3ClipboardWriteDataEx(&pEvent->cmdCtx, fFormat, lp, (uint32_t)GlobalSize(hClip));
|
---|
668 |
|
---|
669 | GlobalUnlock(hClip);
|
---|
670 | }
|
---|
671 | else
|
---|
672 | hClip = NULL;
|
---|
673 | }
|
---|
674 | }
|
---|
675 | else if (fFormat & VBOX_SHCL_FMT_UNICODETEXT)
|
---|
676 | {
|
---|
677 | hClip = GetClipboardData(CF_UNICODETEXT);
|
---|
678 | if (hClip != NULL)
|
---|
679 | {
|
---|
680 | LPWSTR uniString = (LPWSTR)GlobalLock(hClip);
|
---|
681 | if (uniString != NULL)
|
---|
682 | {
|
---|
683 | rc = VbglR3ClipboardWriteDataEx(&pEvent->cmdCtx,
|
---|
684 | fFormat, uniString, ((uint32_t)lstrlenW(uniString) + 1) * 2);
|
---|
685 |
|
---|
686 | GlobalUnlock(hClip);
|
---|
687 | }
|
---|
688 | else
|
---|
689 | hClip = NULL;
|
---|
690 | }
|
---|
691 | }
|
---|
692 | else if (fFormat & VBOX_SHCL_FMT_HTML)
|
---|
693 | {
|
---|
694 | UINT format = RegisterClipboardFormat(SHCL_WIN_REGFMT_HTML);
|
---|
695 | if (format != 0)
|
---|
696 | {
|
---|
697 | hClip = GetClipboardData(format);
|
---|
698 | if (hClip != NULL)
|
---|
699 | {
|
---|
700 | LPVOID const pvClip = GlobalLock(hClip);
|
---|
701 | if (pvClip != NULL)
|
---|
702 | {
|
---|
703 | uint32_t const cbClip = (uint32_t)GlobalSize(hClip);
|
---|
704 |
|
---|
705 | /* Unwrap clipboard content from CF_HTML format if needed. */
|
---|
706 | if (SharedClipboardWinIsCFHTML((const char *)pvClip))
|
---|
707 | {
|
---|
708 | char *pszBuf = NULL;
|
---|
709 | uint32_t cbBuf = 0;
|
---|
710 | rc = SharedClipboardWinConvertCFHTMLToMIME((const char *)pvClip, cbClip, &pszBuf, &cbBuf);
|
---|
711 | if (RT_SUCCESS(rc))
|
---|
712 | {
|
---|
713 | rc = VbglR3ClipboardWriteDataEx(&pEvent->cmdCtx, fFormat, pszBuf, cbBuf);
|
---|
714 | RTMemFree(pszBuf);
|
---|
715 | }
|
---|
716 | else
|
---|
717 | rc = VbglR3ClipboardWriteDataEx(&pEvent->cmdCtx, fFormat, pvClip, cbClip);
|
---|
718 | }
|
---|
719 | else
|
---|
720 | rc = VbglR3ClipboardWriteDataEx(&pEvent->cmdCtx, fFormat, pvClip, cbClip);
|
---|
721 |
|
---|
722 | GlobalUnlock(hClip);
|
---|
723 | }
|
---|
724 | else
|
---|
725 | hClip = NULL;
|
---|
726 | }
|
---|
727 | }
|
---|
728 | }
|
---|
729 |
|
---|
730 | if (hClip == NULL)
|
---|
731 | LogFunc(("SHCL_WIN_WM_READ_DATA: hClip=NULL, lastError=%ld\n", GetLastError()));
|
---|
732 |
|
---|
733 | SharedClipboardWinClose();
|
---|
734 | }
|
---|
735 |
|
---|
736 | /* If the requested clipboard format is not available, we must send empty data. */
|
---|
737 | if (hClip == NULL)
|
---|
738 | VbglR3ClipboardWriteDataEx(&pEvent->cmdCtx, VBOX_SHCL_FMT_NONE, NULL, 0);
|
---|
739 | break;
|
---|
740 | }
|
---|
741 |
|
---|
742 | case WM_DESTROY:
|
---|
743 | {
|
---|
744 | LogFunc(("WM_DESTROY\n"));
|
---|
745 |
|
---|
746 | int rc = SharedClipboardWinHandleWMDestroy(pWinCtx);
|
---|
747 | AssertRC(rc);
|
---|
748 |
|
---|
749 | /*
|
---|
750 | * Don't need to call PostQuitMessage cause
|
---|
751 | * the VBoxTray already finished a message loop.
|
---|
752 | */
|
---|
753 |
|
---|
754 | break;
|
---|
755 | }
|
---|
756 |
|
---|
757 | default:
|
---|
758 | {
|
---|
759 | LogFunc(("WM_ %p\n", msg));
|
---|
760 | lresultRc = DefWindowProc(hwnd, msg, wParam, lParam);
|
---|
761 | break;
|
---|
762 | }
|
---|
763 | }
|
---|
764 |
|
---|
765 | LogFunc(("WM_ rc %d\n", lresultRc));
|
---|
766 | return lresultRc;
|
---|
767 | }
|
---|
768 |
|
---|
769 | static LRESULT CALLBACK vbtrShClWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
|
---|
770 |
|
---|
771 | static int vbtrShClCreateWindow(PSHCLCONTEXT pCtx)
|
---|
772 | {
|
---|
773 | AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
|
---|
774 |
|
---|
775 | int rc = VINF_SUCCESS;
|
---|
776 |
|
---|
777 | AssertPtr(pCtx->pEnv);
|
---|
778 | HINSTANCE hInstance = pCtx->pEnv->hInstance;
|
---|
779 | Assert(hInstance != 0);
|
---|
780 |
|
---|
781 | /* Register the Window Class. */
|
---|
782 | WNDCLASSEX wc;
|
---|
783 | RT_ZERO(wc);
|
---|
784 |
|
---|
785 | wc.cbSize = sizeof(WNDCLASSEX);
|
---|
786 |
|
---|
787 | if (!GetClassInfoEx(hInstance, s_szClipWndClassName, &wc))
|
---|
788 | {
|
---|
789 | wc.style = CS_NOCLOSE;
|
---|
790 | wc.lpfnWndProc = vbtrShClWndProc;
|
---|
791 | wc.hInstance = pCtx->pEnv->hInstance;
|
---|
792 | wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
|
---|
793 | wc.lpszClassName = s_szClipWndClassName;
|
---|
794 |
|
---|
795 | ATOM wndClass = RegisterClassEx(&wc);
|
---|
796 | if (wndClass == 0)
|
---|
797 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
798 | }
|
---|
799 |
|
---|
800 | if (RT_SUCCESS(rc))
|
---|
801 | {
|
---|
802 | const PSHCLWINCTX pWinCtx = &pCtx->Win;
|
---|
803 |
|
---|
804 | /* Create the window. */
|
---|
805 | pWinCtx->hWnd = CreateWindowEx(WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
|
---|
806 | s_szClipWndClassName, s_szClipWndClassName,
|
---|
807 | WS_POPUPWINDOW,
|
---|
808 | -200, -200, 100, 100, NULL, NULL, hInstance, NULL);
|
---|
809 | if (pWinCtx->hWnd == NULL)
|
---|
810 | {
|
---|
811 | rc = VERR_NOT_SUPPORTED;
|
---|
812 | }
|
---|
813 | else
|
---|
814 | {
|
---|
815 | SetWindowPos(pWinCtx->hWnd, HWND_TOPMOST, -200, -200, 0, 0,
|
---|
816 | SWP_NOACTIVATE | SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_NOSIZE);
|
---|
817 |
|
---|
818 | rc = SharedClipboardWinChainAdd(pWinCtx);
|
---|
819 | if (RT_SUCCESS(rc))
|
---|
820 | {
|
---|
821 | if (!SharedClipboardWinIsNewAPI(&pWinCtx->newAPI))
|
---|
822 | pWinCtx->oldAPI.timerRefresh = SetTimer(pWinCtx->hWnd, 0, 10 * 1000 /* 10s */, NULL);
|
---|
823 | }
|
---|
824 | }
|
---|
825 | }
|
---|
826 |
|
---|
827 | LogFlowFuncLeaveRC(rc);
|
---|
828 | return rc;
|
---|
829 | }
|
---|
830 |
|
---|
831 | static DECLCALLBACK(int) vbtrShClWindowThread(RTTHREAD hThread, void *pvUser)
|
---|
832 | {
|
---|
833 | PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pvUser;
|
---|
834 | AssertPtr(pCtx);
|
---|
835 |
|
---|
836 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
837 | HRESULT hr = OleInitialize(NULL);
|
---|
838 | if (FAILED(hr))
|
---|
839 | {
|
---|
840 | LogRel(("Shared Clipboard: Initializing OLE in window thread failed (%Rhrc) -- file transfers unavailable\n", hr));
|
---|
841 | /* Not critical, the rest of the clipboard might work. */
|
---|
842 | }
|
---|
843 | else
|
---|
844 | LogRel(("Shared Clipboard: Initialized OLE in window thread\n"));
|
---|
845 | #endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
|
---|
846 |
|
---|
847 | int rc = vbtrShClCreateWindow(pCtx);
|
---|
848 | if (RT_FAILURE(rc))
|
---|
849 | {
|
---|
850 | LogRel(("Shared Clipboard: Unable to create window, rc=%Rrc\n", rc));
|
---|
851 | return rc;
|
---|
852 | }
|
---|
853 |
|
---|
854 | pCtx->fStarted = true; /* Set started indicator. */
|
---|
855 |
|
---|
856 | int rc2 = RTThreadUserSignal(hThread);
|
---|
857 | bool fSignalled = RT_SUCCESS(rc2);
|
---|
858 |
|
---|
859 | LogRel2(("Shared Clipboard: Window thread running\n"));
|
---|
860 |
|
---|
861 | if (RT_SUCCESS(rc))
|
---|
862 | {
|
---|
863 | for (;;)
|
---|
864 | {
|
---|
865 | MSG uMsg;
|
---|
866 | BOOL fRet;
|
---|
867 | while ((fRet = GetMessage(&uMsg, 0, 0, 0)) > 0)
|
---|
868 | {
|
---|
869 | TranslateMessage(&uMsg);
|
---|
870 | DispatchMessage(&uMsg);
|
---|
871 | }
|
---|
872 | Assert(fRet >= 0);
|
---|
873 |
|
---|
874 | if (ASMAtomicReadBool(&pCtx->fShutdown))
|
---|
875 | break;
|
---|
876 |
|
---|
877 | /** @todo Immediately drop on failure? */
|
---|
878 | }
|
---|
879 | }
|
---|
880 |
|
---|
881 | if (!fSignalled)
|
---|
882 | {
|
---|
883 | rc2 = RTThreadUserSignal(hThread);
|
---|
884 | AssertRC(rc2);
|
---|
885 | }
|
---|
886 |
|
---|
887 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
888 | OleSetClipboard(NULL); /* Make sure to flush the clipboard on destruction. */
|
---|
889 | OleUninitialize();
|
---|
890 | #endif
|
---|
891 |
|
---|
892 | LogRel(("Shared Clipboard: Window thread ended\n"));
|
---|
893 |
|
---|
894 | LogFlowFuncLeaveRC(rc);
|
---|
895 | return rc;
|
---|
896 | }
|
---|
897 |
|
---|
898 | static LRESULT CALLBACK vbtrShClWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
---|
899 | {
|
---|
900 | PSHCLCONTEXT pCtx = &g_Ctx; /** @todo r=andy Make pCtx available through SetWindowLongPtr() / GWL_USERDATA. */
|
---|
901 | AssertPtr(pCtx);
|
---|
902 |
|
---|
903 | /* Forward with proper context. */
|
---|
904 | return vbtrShClWndProcWorker(pCtx, hWnd, uMsg, wParam, lParam);
|
---|
905 | }
|
---|
906 |
|
---|
907 | DECLCALLBACK(int) vbtrShClInit(const PVBOXSERVICEENV pEnv, void **ppInstance)
|
---|
908 | {
|
---|
909 | LogFlowFuncEnter();
|
---|
910 |
|
---|
911 | PSHCLCONTEXT pCtx = &g_Ctx; /* Only one instance for now. */
|
---|
912 | AssertPtr(pCtx);
|
---|
913 |
|
---|
914 | if (pCtx->pEnv)
|
---|
915 | {
|
---|
916 | /* Clipboard was already initialized. 2 or more instances are not supported. */
|
---|
917 | return VERR_NOT_SUPPORTED;
|
---|
918 | }
|
---|
919 |
|
---|
920 | if (VbglR3AutoLogonIsRemoteSession())
|
---|
921 | {
|
---|
922 | /* Do not use clipboard for remote sessions. */
|
---|
923 | LogRel(("Shared Clipboard: Clipboard has been disabled for a remote session\n"));
|
---|
924 | return VERR_NOT_SUPPORTED;
|
---|
925 | }
|
---|
926 |
|
---|
927 | pCtx->pEnv = pEnv;
|
---|
928 | pCtx->hThread = NIL_RTTHREAD;
|
---|
929 | pCtx->fStarted = false;
|
---|
930 | pCtx->fShutdown = false;
|
---|
931 |
|
---|
932 | int rc = RTReqQueueCreate(&pCtx->Win.hReqQ);
|
---|
933 | AssertRCReturn(rc, rc);
|
---|
934 |
|
---|
935 | rc = SharedClipboardWinCtxInit(&pCtx->Win);
|
---|
936 | if (RT_SUCCESS(rc))
|
---|
937 | rc = VbglR3ClipboardConnectEx(&pCtx->CmdCtx, VBOX_SHCL_GF_0_CONTEXT_ID);
|
---|
938 | if (RT_SUCCESS(rc))
|
---|
939 | {
|
---|
940 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
941 | rc = ShClTransferCtxInit(&pCtx->TransferCtx);
|
---|
942 | #endif
|
---|
943 | if (RT_SUCCESS(rc))
|
---|
944 | {
|
---|
945 | /* Message pump thread for our proxy window. */
|
---|
946 | rc = RTThreadCreate(&pCtx->hThread, vbtrShClWindowThread, pCtx /* pvUser */,
|
---|
947 | 0, RTTHREADTYPE_MSG_PUMP, RTTHREADFLAGS_WAITABLE,
|
---|
948 | "shclwnd");
|
---|
949 | if (RT_SUCCESS(rc))
|
---|
950 | {
|
---|
951 | int rc2 = RTThreadUserWait(pCtx->hThread, RT_MS_30SEC /* Timeout in ms */);
|
---|
952 | AssertRC(rc2);
|
---|
953 |
|
---|
954 | if (!pCtx->fStarted) /* Did the thread fail to start? */
|
---|
955 | rc = VERR_NOT_SUPPORTED; /* Report back Shared Clipboard as not being supported. */
|
---|
956 | }
|
---|
957 | }
|
---|
958 |
|
---|
959 | if (RT_SUCCESS(rc))
|
---|
960 | {
|
---|
961 | *ppInstance = pCtx;
|
---|
962 | }
|
---|
963 | else
|
---|
964 | VbglR3ClipboardDisconnectEx(&pCtx->CmdCtx);
|
---|
965 | }
|
---|
966 |
|
---|
967 | if (RT_FAILURE(rc))
|
---|
968 | LogRel(("Shared Clipboard: Unable to initialize, rc=%Rrc\n", rc));
|
---|
969 |
|
---|
970 | LogFlowFuncLeaveRC(rc);
|
---|
971 | return rc;
|
---|
972 | }
|
---|
973 |
|
---|
974 | DECLCALLBACK(int) vbtrShClWorker(void *pInstance, bool volatile *pfShutdown)
|
---|
975 | {
|
---|
976 | AssertPtr(pInstance);
|
---|
977 | LogFlowFunc(("pInstance=%p\n", pInstance));
|
---|
978 |
|
---|
979 | /*
|
---|
980 | * Tell the control thread that it can continue
|
---|
981 | * spawning services.
|
---|
982 | */
|
---|
983 | RTThreadUserSignal(RTThreadSelf());
|
---|
984 |
|
---|
985 | const PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pInstance;
|
---|
986 | AssertPtr(pCtx);
|
---|
987 |
|
---|
988 | const PSHCLWINCTX pWinCtx = &pCtx->Win;
|
---|
989 |
|
---|
990 | LogRel2(("Shared Clipboard: Worker loop running\n"));
|
---|
991 |
|
---|
992 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
993 | HRESULT hr = OleInitialize(NULL);
|
---|
994 | if (FAILED(hr))
|
---|
995 | {
|
---|
996 | LogRel(("Shared Clipboard: Initializing OLE in worker thread failed (%Rhrc) -- file transfers unavailable\n", hr));
|
---|
997 | /* Not critical, the rest of the clipboard might work. */
|
---|
998 | }
|
---|
999 | else
|
---|
1000 | LogRel(("Shared Clipboard: Initialized OLE in worker thread\n"));
|
---|
1001 |
|
---|
1002 | /*
|
---|
1003 | * Init callbacks.
|
---|
1004 | * Those will be registered within VbglR3 when a new transfer gets initialized.
|
---|
1005 | */
|
---|
1006 | RT_ZERO(pCtx->CmdCtx.Transfers.Callbacks);
|
---|
1007 |
|
---|
1008 | pCtx->CmdCtx.Transfers.Callbacks.pvUser = pCtx; /* Assign context as user-provided callback data. */
|
---|
1009 | pCtx->CmdCtx.Transfers.Callbacks.cbUser = sizeof(SHCLCONTEXT);
|
---|
1010 |
|
---|
1011 | pCtx->CmdCtx.Transfers.Callbacks.pfnOnCreated = vbtrShClTransferCreatedCallback;
|
---|
1012 | pCtx->CmdCtx.Transfers.Callbacks.pfnOnDestroy = vbtrShClTransferDestroyCallback;
|
---|
1013 | pCtx->CmdCtx.Transfers.Callbacks.pfnOnInitialize = vbtrShClTransferInitializeCallback;
|
---|
1014 | pCtx->CmdCtx.Transfers.Callbacks.pfnOnInitialized = vbtrShClTransferInitializedCallback;
|
---|
1015 | pCtx->CmdCtx.Transfers.Callbacks.pfnOnStarted = vbtrShClTransferStartedCallback;
|
---|
1016 | pCtx->CmdCtx.Transfers.Callbacks.pfnOnCompleted = vbtrShClTransferCompletedCallback;
|
---|
1017 | pCtx->CmdCtx.Transfers.Callbacks.pfnOnError = vbtrShClTransferErrorCallback;
|
---|
1018 | #endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
|
---|
1019 |
|
---|
1020 | int rc;
|
---|
1021 |
|
---|
1022 | /* The thread waits for incoming messages from the host. */
|
---|
1023 | PVBGLR3CLIPBOARDEVENT pEvent = NULL;
|
---|
1024 | for (;;)
|
---|
1025 | {
|
---|
1026 | LogFlowFunc(("Waiting for host message (fUseLegacyProtocol=%RTbool, fHostFeatures=%#RX64) ...\n",
|
---|
1027 | pCtx->CmdCtx.fUseLegacyProtocol, pCtx->CmdCtx.fHostFeatures));
|
---|
1028 |
|
---|
1029 | if (!pEvent)
|
---|
1030 | pEvent = (PVBGLR3CLIPBOARDEVENT)RTMemAllocZ(sizeof(VBGLR3CLIPBOARDEVENT));
|
---|
1031 | AssertPtrBreakStmt(pEvent, rc = VERR_NO_MEMORY);
|
---|
1032 |
|
---|
1033 | uint32_t idMsg = 0;
|
---|
1034 | uint32_t cParms = 0;
|
---|
1035 | rc = VbglR3ClipboardMsgPeekWait(&pCtx->CmdCtx, &idMsg, &cParms, NULL /* pidRestoreCheck */);
|
---|
1036 | if (RT_SUCCESS(rc))
|
---|
1037 | {
|
---|
1038 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
1039 | rc = VbglR3ClipboardEventGetNextEx(idMsg, cParms, &pCtx->CmdCtx, &pCtx->TransferCtx, pEvent);
|
---|
1040 | #else
|
---|
1041 | rc = VbglR3ClipboardEventGetNext(idMsg, cParms, &pCtx->CmdCtx, pEvent);
|
---|
1042 | #endif
|
---|
1043 | }
|
---|
1044 | else if (rc == VERR_TRY_AGAIN) /* No new message (yet). */
|
---|
1045 | {
|
---|
1046 | RTReqQueueProcess(pCtx->Win.hReqQ, RT_MS_1SEC);
|
---|
1047 | continue;
|
---|
1048 | }
|
---|
1049 |
|
---|
1050 | if (RT_FAILURE(rc))
|
---|
1051 | {
|
---|
1052 | LogFlowFunc(("Getting next event failed with %Rrc\n", rc));
|
---|
1053 |
|
---|
1054 | VbglR3ClipboardEventFree(pEvent);
|
---|
1055 | pEvent = NULL;
|
---|
1056 |
|
---|
1057 | if (*pfShutdown)
|
---|
1058 | break;
|
---|
1059 |
|
---|
1060 | /* Wait a bit before retrying. */
|
---|
1061 | RTThreadSleep(1000);
|
---|
1062 | continue;
|
---|
1063 | }
|
---|
1064 | else
|
---|
1065 | {
|
---|
1066 | AssertPtr(pEvent);
|
---|
1067 | LogFlowFunc(("Event uType=%RU32\n", pEvent->enmType));
|
---|
1068 |
|
---|
1069 | switch (pEvent->enmType)
|
---|
1070 | {
|
---|
1071 | case VBGLR3CLIPBOARDEVENTTYPE_REPORT_FORMATS:
|
---|
1072 | {
|
---|
1073 | /* The host has announced available clipboard formats.
|
---|
1074 | * Forward the information to the window, so it can later
|
---|
1075 | * respond to WM_RENDERFORMAT message. */
|
---|
1076 | ::PostMessage(pWinCtx->hWnd, SHCL_WIN_WM_REPORT_FORMATS,
|
---|
1077 | 0 /* wParam */, (LPARAM)pEvent /* lParam */);
|
---|
1078 |
|
---|
1079 | pEvent = NULL; /* Consume pointer. */
|
---|
1080 | break;
|
---|
1081 | }
|
---|
1082 |
|
---|
1083 | case VBGLR3CLIPBOARDEVENTTYPE_READ_DATA:
|
---|
1084 | {
|
---|
1085 | /* The host needs data in the specified format. */
|
---|
1086 | ::PostMessage(pWinCtx->hWnd, SHCL_WIN_WM_READ_DATA,
|
---|
1087 | 0 /* wParam */, (LPARAM)pEvent /* lParam */);
|
---|
1088 |
|
---|
1089 | pEvent = NULL; /* Consume pointer. */
|
---|
1090 | break;
|
---|
1091 | }
|
---|
1092 |
|
---|
1093 | case VBGLR3CLIPBOARDEVENTTYPE_QUIT:
|
---|
1094 | {
|
---|
1095 | LogRel2(("Shared Clipboard: Host requested termination\n"));
|
---|
1096 | ASMAtomicXchgBool(pfShutdown, true);
|
---|
1097 | break;
|
---|
1098 | }
|
---|
1099 |
|
---|
1100 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
1101 | case VBGLR3CLIPBOARDEVENTTYPE_TRANSFER_STATUS:
|
---|
1102 | {
|
---|
1103 | /* Nothing to do here. */
|
---|
1104 | rc = VINF_SUCCESS;
|
---|
1105 | break;
|
---|
1106 | }
|
---|
1107 | #endif
|
---|
1108 | case VBGLR3CLIPBOARDEVENTTYPE_NONE:
|
---|
1109 | {
|
---|
1110 | /* Nothing to do here. */
|
---|
1111 | rc = VINF_SUCCESS;
|
---|
1112 | break;
|
---|
1113 | }
|
---|
1114 |
|
---|
1115 | default:
|
---|
1116 | {
|
---|
1117 | AssertMsgFailedBreakStmt(("Event type %RU32 not implemented\n", pEvent->enmType), rc = VERR_NOT_SUPPORTED);
|
---|
1118 | }
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 | if (pEvent)
|
---|
1122 | {
|
---|
1123 | VbglR3ClipboardEventFree(pEvent);
|
---|
1124 | pEvent = NULL;
|
---|
1125 | }
|
---|
1126 | }
|
---|
1127 |
|
---|
1128 | if (*pfShutdown)
|
---|
1129 | break;
|
---|
1130 | }
|
---|
1131 |
|
---|
1132 | LogRel2(("Shared Clipboard: Worker loop ended\n"));
|
---|
1133 |
|
---|
1134 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
1135 | OleSetClipboard(NULL); /* Make sure to flush the clipboard on destruction. */
|
---|
1136 | OleUninitialize();
|
---|
1137 | #endif
|
---|
1138 |
|
---|
1139 | LogFlowFuncLeaveRC(rc);
|
---|
1140 | return rc;
|
---|
1141 | }
|
---|
1142 |
|
---|
1143 | DECLCALLBACK(int) vbtrShClStop(void *pInstance)
|
---|
1144 | {
|
---|
1145 | AssertPtrReturn(pInstance, VERR_INVALID_POINTER);
|
---|
1146 |
|
---|
1147 | LogFunc(("Stopping pInstance=%p\n", pInstance));
|
---|
1148 |
|
---|
1149 | PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pInstance;
|
---|
1150 | AssertPtr(pCtx);
|
---|
1151 |
|
---|
1152 | /* Set shutdown indicator. */
|
---|
1153 | ASMAtomicWriteBool(&pCtx->fShutdown, true);
|
---|
1154 |
|
---|
1155 | /* Let our clipboard know that we're going to shut down. */
|
---|
1156 | PostMessage(pCtx->Win.hWnd, WM_QUIT, 0, 0);
|
---|
1157 |
|
---|
1158 | /* Disconnect from the host service.
|
---|
1159 | * This will also send a VBOX_SHCL_HOST_MSG_QUIT from the host so that we can break out from our message worker. */
|
---|
1160 | VbglR3ClipboardDisconnect(pCtx->CmdCtx.idClient);
|
---|
1161 | pCtx->CmdCtx.idClient = 0;
|
---|
1162 |
|
---|
1163 | LogFlowFuncLeaveRC(VINF_SUCCESS);
|
---|
1164 | return VINF_SUCCESS;
|
---|
1165 | }
|
---|
1166 |
|
---|
1167 | DECLCALLBACK(void) vbtrShClDestroy(void *pInstance)
|
---|
1168 | {
|
---|
1169 | AssertPtrReturnVoid(pInstance);
|
---|
1170 |
|
---|
1171 | PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pInstance;
|
---|
1172 | AssertPtrReturnVoid(pCtx);
|
---|
1173 |
|
---|
1174 | /* Make sure that we are disconnected. */
|
---|
1175 | Assert(pCtx->CmdCtx.idClient == 0);
|
---|
1176 |
|
---|
1177 | LogFlowFunc(("pCtx=%p\n", pCtx));
|
---|
1178 |
|
---|
1179 | LogRel2(("Shared Clipboard: Destroying ...\n"));
|
---|
1180 |
|
---|
1181 | const PSHCLWINCTX pWinCtx = &pCtx->Win;
|
---|
1182 |
|
---|
1183 | if (pCtx->hThread != NIL_RTTHREAD)
|
---|
1184 | {
|
---|
1185 | int rcThread = VERR_WRONG_ORDER;
|
---|
1186 | int rc = RTThreadWait(pCtx->hThread, 60 * 1000 /* Timeout in ms */, &rcThread);
|
---|
1187 | LogFlowFunc(("Waiting for thread resulted in %Rrc (thread exited with %Rrc)\n",
|
---|
1188 | rc, rcThread));
|
---|
1189 | RT_NOREF(rc);
|
---|
1190 | }
|
---|
1191 |
|
---|
1192 | if (pWinCtx->hWnd)
|
---|
1193 | {
|
---|
1194 | DestroyWindow(pWinCtx->hWnd);
|
---|
1195 | pWinCtx->hWnd = NULL;
|
---|
1196 | }
|
---|
1197 |
|
---|
1198 | UnregisterClass(s_szClipWndClassName, pCtx->pEnv->hInstance);
|
---|
1199 |
|
---|
1200 | SharedClipboardWinCtxDestroy(&pCtx->Win);
|
---|
1201 |
|
---|
1202 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
1203 | ShClTransferCtxDestroy(&pCtx->TransferCtx);
|
---|
1204 | #endif
|
---|
1205 |
|
---|
1206 | RTReqQueueDestroy(pCtx->Win.hReqQ);
|
---|
1207 |
|
---|
1208 | LogRel2(("Shared Clipboard: Destroyed\n"));
|
---|
1209 |
|
---|
1210 | return;
|
---|
1211 | }
|
---|
1212 |
|
---|
1213 | /**
|
---|
1214 | * The service description.
|
---|
1215 | */
|
---|
1216 | VBOXSERVICEDESC g_SvcDescClipboard =
|
---|
1217 | {
|
---|
1218 | /* pszName. */
|
---|
1219 | "clipboard",
|
---|
1220 | /* pszDescription. */
|
---|
1221 | "Shared Clipboard",
|
---|
1222 | /* methods */
|
---|
1223 | vbtrShClInit,
|
---|
1224 | vbtrShClWorker,
|
---|
1225 | vbtrShClStop,
|
---|
1226 | vbtrShClDestroy
|
---|
1227 | };
|
---|