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