1 | /* $Id: clipboard-win.cpp 82497 2019-12-08 00:26:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Shared Clipboard: Windows-specific functions for clipboard handling.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
|
---|
19 | #include <VBox/GuestHost/SharedClipboard.h>
|
---|
20 |
|
---|
21 | #include <iprt/assert.h>
|
---|
22 | #include <iprt/errcore.h>
|
---|
23 | #include <iprt/ldr.h>
|
---|
24 | #include <iprt/mem.h>
|
---|
25 | #include <iprt/thread.h>
|
---|
26 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
27 | # include <iprt/win/windows.h>
|
---|
28 | # include <iprt/win/shlobj.h> /* For CFSTR_FILEDESCRIPTORXXX + CFSTR_FILECONTENTS. */
|
---|
29 | # include <iprt/utf16.h>
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #include <VBox/log.h>
|
---|
33 |
|
---|
34 | #include <VBox/HostServices/VBoxClipboardSvc.h>
|
---|
35 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
36 | # include <VBox/GuestHost/SharedClipboard-transfers.h>
|
---|
37 | #endif
|
---|
38 | #include <VBox/GuestHost/SharedClipboard-win.h>
|
---|
39 | #include <VBox/GuestHost/clipboard-helper.h>
|
---|
40 |
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Opens the clipboard of a specific window.
|
---|
44 | *
|
---|
45 | * @returns VBox status code.
|
---|
46 | * @param hWnd Handle of window to open clipboard for.
|
---|
47 | */
|
---|
48 | int SharedClipboardWinOpen(HWND hWnd)
|
---|
49 | {
|
---|
50 | /* "OpenClipboard fails if another window has the clipboard open."
|
---|
51 | * So try a few times and wait up to 1 second.
|
---|
52 | */
|
---|
53 | BOOL fOpened = FALSE;
|
---|
54 |
|
---|
55 | LogFlowFunc(("hWnd=%p\n", hWnd));
|
---|
56 |
|
---|
57 | int i = 0;
|
---|
58 | for (;;)
|
---|
59 | {
|
---|
60 | if (OpenClipboard(hWnd))
|
---|
61 | {
|
---|
62 | fOpened = TRUE;
|
---|
63 | break;
|
---|
64 | }
|
---|
65 |
|
---|
66 | if (i >= 10) /* sleep interval = [1..512] ms */
|
---|
67 | break;
|
---|
68 |
|
---|
69 | RTThreadSleep(1 << i);
|
---|
70 | ++i;
|
---|
71 | }
|
---|
72 |
|
---|
73 | #ifdef LOG_ENABLED
|
---|
74 | if (i > 0)
|
---|
75 | LogFlowFunc(("%d times tried to open clipboard\n", i + 1));
|
---|
76 | #endif
|
---|
77 |
|
---|
78 | int rc;
|
---|
79 | if (fOpened)
|
---|
80 | rc = VINF_SUCCESS;
|
---|
81 | else
|
---|
82 | {
|
---|
83 | const DWORD dwLastErr = GetLastError();
|
---|
84 | rc = RTErrConvertFromWin32(dwLastErr);
|
---|
85 | LogFunc(("Failed to open clipboard, rc=%Rrc (0x%x)\n", rc, dwLastErr));
|
---|
86 | }
|
---|
87 |
|
---|
88 | return rc;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Closes the clipboard for the current thread.
|
---|
93 | *
|
---|
94 | * @returns VBox status code.
|
---|
95 | */
|
---|
96 | int SharedClipboardWinClose(void)
|
---|
97 | {
|
---|
98 | int rc;
|
---|
99 |
|
---|
100 | const BOOL fRc = CloseClipboard();
|
---|
101 | if (RT_UNLIKELY(!fRc))
|
---|
102 | {
|
---|
103 | const DWORD dwLastErr = GetLastError();
|
---|
104 | if (dwLastErr == ERROR_CLIPBOARD_NOT_OPEN)
|
---|
105 | {
|
---|
106 | rc = VINF_SUCCESS; /* Not important, so just report success instead. */
|
---|
107 | }
|
---|
108 | else
|
---|
109 | {
|
---|
110 | rc = RTErrConvertFromWin32(dwLastErr);
|
---|
111 | LogFunc(("Failed with %Rrc (0x%x)\n", rc, dwLastErr));
|
---|
112 | }
|
---|
113 | }
|
---|
114 | else
|
---|
115 | rc = VINF_SUCCESS;
|
---|
116 |
|
---|
117 | LogFlowFuncLeaveRC(rc);
|
---|
118 | return rc;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Clears the clipboard for the current thread.
|
---|
123 | *
|
---|
124 | * @returns VBox status code.
|
---|
125 | */
|
---|
126 | int SharedClipboardWinClear(void)
|
---|
127 | {
|
---|
128 | int rc;
|
---|
129 |
|
---|
130 | LogFlowFuncEnter();
|
---|
131 |
|
---|
132 | const BOOL fRc = EmptyClipboard();
|
---|
133 | if (RT_UNLIKELY(!fRc))
|
---|
134 | {
|
---|
135 | const DWORD dwLastErr = GetLastError();
|
---|
136 | if (dwLastErr == ERROR_CLIPBOARD_NOT_OPEN)
|
---|
137 | rc = VERR_INVALID_STATE;
|
---|
138 | else
|
---|
139 | rc = RTErrConvertFromWin32(dwLastErr);
|
---|
140 |
|
---|
141 | LogFunc(("Failed with %Rrc (0x%x)\n", rc, dwLastErr));
|
---|
142 | }
|
---|
143 | else
|
---|
144 | rc = VINF_SUCCESS;
|
---|
145 |
|
---|
146 | return rc;
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Initializes a Shared Clipboard Windows context.
|
---|
151 | *
|
---|
152 | * @returns VBox status code.
|
---|
153 | * @param pWinCtx Shared Clipboard Windows context to initialize.
|
---|
154 | */
|
---|
155 | int SharedClipboardWinCtxInit(PSHCLWINCTX pWinCtx)
|
---|
156 | {
|
---|
157 | int rc = RTCritSectInit(&pWinCtx->CritSect);
|
---|
158 | if (RT_SUCCESS(rc))
|
---|
159 | {
|
---|
160 | /* Check that new Clipboard API is available. */
|
---|
161 | SharedClipboardWinCheckAndInitNewAPI(&pWinCtx->newAPI);
|
---|
162 | /* Do *not* check the rc, as the call might return VERR_SYMBOL_NOT_FOUND is the new API isn't available. */
|
---|
163 |
|
---|
164 | pWinCtx->hWnd = NULL;
|
---|
165 | pWinCtx->hWndClipboardOwnerUs = NULL;
|
---|
166 | pWinCtx->hWndNextInChain = NULL;
|
---|
167 | }
|
---|
168 |
|
---|
169 | LogFlowFuncLeaveRC(rc);
|
---|
170 | return rc;
|
---|
171 | }
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Destroys a Shared Clipboard Windows context.
|
---|
175 | *
|
---|
176 | * @param pWinCtx Shared Clipboard Windows context to destroy.
|
---|
177 | */
|
---|
178 | void SharedClipboardWinCtxDestroy(PSHCLWINCTX pWinCtx)
|
---|
179 | {
|
---|
180 | if (!pWinCtx)
|
---|
181 | return;
|
---|
182 |
|
---|
183 | LogFlowFuncEnter();
|
---|
184 |
|
---|
185 | if (RTCritSectIsInitialized(&pWinCtx->CritSect))
|
---|
186 | {
|
---|
187 | int rc2 = RTCritSectDelete(&pWinCtx->CritSect);
|
---|
188 | AssertRC(rc2);
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Checks and initializes function pointer which are required for using
|
---|
194 | * the new clipboard API.
|
---|
195 | *
|
---|
196 | * @returns VBox status code, or VERR_SYMBOL_NOT_FOUND if the new API is not available.
|
---|
197 | * @param pAPI Where to store the retrieved function pointers.
|
---|
198 | * Will be set to NULL if the new API is not available.
|
---|
199 | */
|
---|
200 | int SharedClipboardWinCheckAndInitNewAPI(PSHCLWINAPINEW pAPI)
|
---|
201 | {
|
---|
202 | RTLDRMOD hUser32 = NIL_RTLDRMOD;
|
---|
203 | int rc = RTLdrLoadSystem("User32.dll", /* fNoUnload = */ true, &hUser32);
|
---|
204 | if (RT_SUCCESS(rc))
|
---|
205 | {
|
---|
206 | rc = RTLdrGetSymbol(hUser32, "AddClipboardFormatListener", (void **)&pAPI->pfnAddClipboardFormatListener);
|
---|
207 | if (RT_SUCCESS(rc))
|
---|
208 | {
|
---|
209 | rc = RTLdrGetSymbol(hUser32, "RemoveClipboardFormatListener", (void **)&pAPI->pfnRemoveClipboardFormatListener);
|
---|
210 | }
|
---|
211 |
|
---|
212 | RTLdrClose(hUser32);
|
---|
213 | }
|
---|
214 |
|
---|
215 | if (RT_SUCCESS(rc))
|
---|
216 | {
|
---|
217 | LogRel(("Shared Clipboard: New Clipboard API enabled\n"));
|
---|
218 | }
|
---|
219 | else
|
---|
220 | {
|
---|
221 | RT_BZERO(pAPI, sizeof(SHCLWINAPINEW));
|
---|
222 | LogRel(("Shared Clipboard: New Clipboard API not available (%Rrc)\n", rc));
|
---|
223 | }
|
---|
224 |
|
---|
225 | LogFlowFuncLeaveRC(rc);
|
---|
226 | return rc;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Returns if the new clipboard API is available or not.
|
---|
231 | *
|
---|
232 | * @returns @c true if the new API is available, or @c false if not.
|
---|
233 | * @param pAPI Structure used for checking if the new clipboard API is available or not.
|
---|
234 | */
|
---|
235 | bool SharedClipboardWinIsNewAPI(PSHCLWINAPINEW pAPI)
|
---|
236 | {
|
---|
237 | if (!pAPI)
|
---|
238 | return false;
|
---|
239 | return pAPI->pfnAddClipboardFormatListener != NULL;
|
---|
240 | }
|
---|
241 |
|
---|
242 | /**
|
---|
243 | * Adds ourselves into the chain of cliboard listeners.
|
---|
244 | *
|
---|
245 | * @returns VBox status code.
|
---|
246 | * @param pCtx Windows clipboard context to use to add ourselves.
|
---|
247 | */
|
---|
248 | int SharedClipboardWinChainAdd(PSHCLWINCTX pCtx)
|
---|
249 | {
|
---|
250 | const PSHCLWINAPINEW pAPI = &pCtx->newAPI;
|
---|
251 |
|
---|
252 | BOOL fRc;
|
---|
253 | if (SharedClipboardWinIsNewAPI(pAPI))
|
---|
254 | {
|
---|
255 | fRc = pAPI->pfnAddClipboardFormatListener(pCtx->hWnd);
|
---|
256 | }
|
---|
257 | else
|
---|
258 | {
|
---|
259 | pCtx->hWndNextInChain = SetClipboardViewer(pCtx->hWnd);
|
---|
260 | fRc = pCtx->hWndNextInChain != NULL;
|
---|
261 | }
|
---|
262 |
|
---|
263 | int rc = VINF_SUCCESS;
|
---|
264 |
|
---|
265 | if (!fRc)
|
---|
266 | {
|
---|
267 | const DWORD dwLastErr = GetLastError();
|
---|
268 | rc = RTErrConvertFromWin32(dwLastErr);
|
---|
269 | LogFunc(("Failed with %Rrc (0x%x)\n", rc, dwLastErr));
|
---|
270 | }
|
---|
271 |
|
---|
272 | return rc;
|
---|
273 | }
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * Remove ourselves from the chain of cliboard listeners
|
---|
277 | *
|
---|
278 | * @returns VBox status code.
|
---|
279 | * @param pCtx Windows clipboard context to use to remove ourselves.
|
---|
280 | */
|
---|
281 | int SharedClipboardWinChainRemove(PSHCLWINCTX pCtx)
|
---|
282 | {
|
---|
283 | if (!pCtx->hWnd)
|
---|
284 | return VINF_SUCCESS;
|
---|
285 |
|
---|
286 | const PSHCLWINAPINEW pAPI = &pCtx->newAPI;
|
---|
287 |
|
---|
288 | BOOL fRc;
|
---|
289 | if (SharedClipboardWinIsNewAPI(pAPI))
|
---|
290 | {
|
---|
291 | fRc = pAPI->pfnRemoveClipboardFormatListener(pCtx->hWnd);
|
---|
292 | }
|
---|
293 | else
|
---|
294 | {
|
---|
295 | fRc = ChangeClipboardChain(pCtx->hWnd, pCtx->hWndNextInChain);
|
---|
296 | if (fRc)
|
---|
297 | pCtx->hWndNextInChain = NULL;
|
---|
298 | }
|
---|
299 |
|
---|
300 | int rc = VINF_SUCCESS;
|
---|
301 |
|
---|
302 | if (!fRc)
|
---|
303 | {
|
---|
304 | const DWORD dwLastErr = GetLastError();
|
---|
305 | rc = RTErrConvertFromWin32(dwLastErr);
|
---|
306 | LogFunc(("Failed with %Rrc (0x%x)\n", rc, dwLastErr));
|
---|
307 | }
|
---|
308 |
|
---|
309 | return rc;
|
---|
310 | }
|
---|
311 |
|
---|
312 | /**
|
---|
313 | * Callback which is invoked when we have successfully pinged ourselves down the
|
---|
314 | * clipboard chain. We simply unset a boolean flag to say that we are responding.
|
---|
315 | * There is a race if a ping returns after the next one is initiated, but nothing
|
---|
316 | * very bad is likely to happen.
|
---|
317 | *
|
---|
318 | * @param hWnd Window handle to use for this callback. Not used currently.
|
---|
319 | * @param uMsg Message to handle. Not used currently.
|
---|
320 | * @param dwData Pointer to user-provided data. Contains our Windows clipboard context.
|
---|
321 | * @param lResult Additional data to pass. Not used currently.
|
---|
322 | */
|
---|
323 | VOID CALLBACK SharedClipboardWinChainPingProc(HWND hWnd, UINT uMsg, ULONG_PTR dwData, LRESULT lResult)
|
---|
324 | {
|
---|
325 | RT_NOREF(hWnd);
|
---|
326 | RT_NOREF(uMsg);
|
---|
327 | RT_NOREF(lResult);
|
---|
328 |
|
---|
329 | /** @todo r=andy Why not using SetWindowLongPtr for keeping the context? */
|
---|
330 | PSHCLWINCTX pCtx = (PSHCLWINCTX)dwData;
|
---|
331 | AssertPtrReturnVoid(pCtx);
|
---|
332 |
|
---|
333 | pCtx->oldAPI.fCBChainPingInProcess = FALSE;
|
---|
334 | }
|
---|
335 |
|
---|
336 | /**
|
---|
337 | * Passes a window message to the next window in the clipboard chain.
|
---|
338 | *
|
---|
339 | * @returns LRESULT
|
---|
340 | * @param pWinCtx Window context to use.
|
---|
341 | * @param msg Window message to pass.
|
---|
342 | * @param wParam WPARAM to pass.
|
---|
343 | * @param lParam LPARAM to pass.
|
---|
344 | */
|
---|
345 | LRESULT SharedClipboardWinChainPassToNext(PSHCLWINCTX pWinCtx,
|
---|
346 | UINT msg, WPARAM wParam, LPARAM lParam)
|
---|
347 | {
|
---|
348 | LogFlowFuncEnter();
|
---|
349 |
|
---|
350 | LRESULT lresultRc = 0;
|
---|
351 |
|
---|
352 | if (pWinCtx->hWndNextInChain)
|
---|
353 | {
|
---|
354 | LogFunc(("hWndNextInChain=%p\n", pWinCtx->hWndNextInChain));
|
---|
355 |
|
---|
356 | /* Pass the message to next window in the clipboard chain. */
|
---|
357 | DWORD_PTR dwResult;
|
---|
358 | lresultRc = SendMessageTimeout(pWinCtx->hWndNextInChain, msg, wParam, lParam, 0,
|
---|
359 | SHCL_WIN_CBCHAIN_TIMEOUT_MS, &dwResult);
|
---|
360 | if (!lresultRc)
|
---|
361 | lresultRc = dwResult;
|
---|
362 | }
|
---|
363 |
|
---|
364 | LogFlowFunc(("lresultRc=%ld\n", lresultRc));
|
---|
365 | return lresultRc;
|
---|
366 | }
|
---|
367 |
|
---|
368 | /**
|
---|
369 | * Converts a (registered or standard) Windows clipboard format to a VBox clipboard format.
|
---|
370 | *
|
---|
371 | * @returns Converted VBox clipboard format, or VBOX_SHCL_FMT_NONE if not found.
|
---|
372 | * @param uFormat Windows clipboard format to convert.
|
---|
373 | */
|
---|
374 | SHCLFORMAT SharedClipboardWinClipboardFormatToVBox(UINT uFormat)
|
---|
375 | {
|
---|
376 | /* Insert the requested clipboard format data into the clipboard. */
|
---|
377 | SHCLFORMAT vboxFormat = VBOX_SHCL_FMT_NONE;
|
---|
378 |
|
---|
379 | switch (uFormat)
|
---|
380 | {
|
---|
381 | case CF_UNICODETEXT:
|
---|
382 | vboxFormat = VBOX_SHCL_FMT_UNICODETEXT;
|
---|
383 | break;
|
---|
384 |
|
---|
385 | case CF_DIB:
|
---|
386 | vboxFormat = VBOX_SHCL_FMT_BITMAP;
|
---|
387 | break;
|
---|
388 |
|
---|
389 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
390 | /* CF_HDROP handles file system entries which are locally present
|
---|
391 | * on source for transferring to the target.
|
---|
392 | *
|
---|
393 | * This does *not* invoke any IDataObject / IStream implementations! */
|
---|
394 | case CF_HDROP:
|
---|
395 | vboxFormat = VBOX_SHCL_FMT_URI_LIST;
|
---|
396 | break;
|
---|
397 | #endif
|
---|
398 |
|
---|
399 | default:
|
---|
400 | if (uFormat >= 0xC000) /** Formats registered with RegisterClipboardFormat() start at this index. */
|
---|
401 | {
|
---|
402 | TCHAR szFormatName[256]; /** @todo r=andy Do we need Unicode support here as well? */
|
---|
403 | int cActual = GetClipboardFormatName(uFormat, szFormatName, sizeof(szFormatName) / sizeof(TCHAR));
|
---|
404 | if (cActual)
|
---|
405 | {
|
---|
406 | LogFlowFunc(("uFormat=%u -> szFormatName=%s\n", uFormat, szFormatName));
|
---|
407 |
|
---|
408 | if (RTStrCmp(szFormatName, SHCL_WIN_REGFMT_HTML) == 0)
|
---|
409 | vboxFormat = VBOX_SHCL_FMT_HTML;
|
---|
410 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
411 | /* These types invoke our IDataObject / IStream implementations. */
|
---|
412 | else if ( (RTStrCmp(szFormatName, CFSTR_FILEDESCRIPTORA) == 0)
|
---|
413 | || (RTStrCmp(szFormatName, CFSTR_FILECONTENTS) == 0))
|
---|
414 | vboxFormat = VBOX_SHCL_FMT_URI_LIST;
|
---|
415 | /** @todo Do we need to handle CFSTR_FILEDESCRIPTORW here as well? */
|
---|
416 | #endif
|
---|
417 | }
|
---|
418 | }
|
---|
419 | break;
|
---|
420 | }
|
---|
421 |
|
---|
422 | LogFlowFunc(("uFormat=%u -> vboxFormat=0x%x\n", uFormat, vboxFormat));
|
---|
423 | return vboxFormat;
|
---|
424 | }
|
---|
425 |
|
---|
426 | /**
|
---|
427 | * Retrieves all supported clipboard formats of a specific clipboard.
|
---|
428 | *
|
---|
429 | * @returns VBox status code.
|
---|
430 | * @param pCtx Windows clipboard context to retrieve formats for.
|
---|
431 | * @param pFormats Where to store the retrieved formats.
|
---|
432 | */
|
---|
433 | int SharedClipboardWinGetFormats(PSHCLWINCTX pCtx, PSHCLFORMATDATA pFormats)
|
---|
434 | {
|
---|
435 | AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
|
---|
436 | AssertPtrReturn(pFormats, VERR_INVALID_POINTER);
|
---|
437 |
|
---|
438 | SHCLFORMATS fFormats = VBOX_SHCL_FMT_NONE;
|
---|
439 |
|
---|
440 | /* Query list of available formats and report to host. */
|
---|
441 | int rc = SharedClipboardWinOpen(pCtx->hWnd);
|
---|
442 | if (RT_SUCCESS(rc))
|
---|
443 | {
|
---|
444 | UINT uCurFormat = 0; /* Must be set to zero for EnumClipboardFormats(). */
|
---|
445 | while ((uCurFormat = EnumClipboardFormats(uCurFormat)) != 0)
|
---|
446 | fFormats |= SharedClipboardWinClipboardFormatToVBox(uCurFormat);
|
---|
447 |
|
---|
448 | int rc2 = SharedClipboardWinClose();
|
---|
449 | AssertRC(rc2);
|
---|
450 | }
|
---|
451 |
|
---|
452 | if (RT_FAILURE(rc))
|
---|
453 | {
|
---|
454 | LogFunc(("Failed with rc=%Rrc\n", rc));
|
---|
455 | }
|
---|
456 | else
|
---|
457 | {
|
---|
458 | LogFlowFunc(("fFormats=0x%08X\n", fFormats));
|
---|
459 |
|
---|
460 | pFormats->Formats = fFormats;
|
---|
461 | pFormats->fFlags = 0; /** @todo Handle flags. */
|
---|
462 | }
|
---|
463 |
|
---|
464 | return rc;
|
---|
465 | }
|
---|
466 |
|
---|
467 | /**
|
---|
468 | * Extracts a field value from CF_HTML data.
|
---|
469 | *
|
---|
470 | * @returns VBox status code.
|
---|
471 | * @param pszSrc source in CF_HTML format.
|
---|
472 | * @param pszOption Name of CF_HTML field.
|
---|
473 | * @param puValue Where to return extracted value of CF_HTML field.
|
---|
474 | */
|
---|
475 | int SharedClipboardWinGetCFHTMLHeaderValue(const char *pszSrc, const char *pszOption, uint32_t *puValue)
|
---|
476 | {
|
---|
477 | AssertPtrReturn(pszSrc, VERR_INVALID_POINTER);
|
---|
478 | AssertPtrReturn(pszOption, VERR_INVALID_POINTER);
|
---|
479 |
|
---|
480 | int rc = VERR_INVALID_PARAMETER;
|
---|
481 |
|
---|
482 | const char *pszOptionValue = RTStrStr(pszSrc, pszOption);
|
---|
483 | if (pszOptionValue)
|
---|
484 | {
|
---|
485 | size_t cchOption = strlen(pszOption);
|
---|
486 | Assert(cchOption);
|
---|
487 |
|
---|
488 | rc = RTStrToUInt32Ex(pszOptionValue + cchOption, NULL, 10, puValue);
|
---|
489 | }
|
---|
490 | return rc;
|
---|
491 | }
|
---|
492 |
|
---|
493 | /**
|
---|
494 | * Check that the source string contains CF_HTML struct.
|
---|
495 | *
|
---|
496 | * @returns @c true if the @a pszSource string is in CF_HTML format.
|
---|
497 | * @param pszSource Source string to check.
|
---|
498 | */
|
---|
499 | bool SharedClipboardWinIsCFHTML(const char *pszSource)
|
---|
500 | {
|
---|
501 | return RTStrStr(pszSource, "Version:") != NULL
|
---|
502 | && RTStrStr(pszSource, "StartHTML:") != NULL;
|
---|
503 | }
|
---|
504 |
|
---|
505 | /**
|
---|
506 | * Converts clipboard data from CF_HTML format to MIME clipboard format.
|
---|
507 | *
|
---|
508 | * Returns allocated buffer that contains html converted to text/html mime type
|
---|
509 | *
|
---|
510 | * @returns VBox status code.
|
---|
511 | * @param pszSource The input.
|
---|
512 | * @param cch The length of the input.
|
---|
513 | * @param ppszOutput Where to return the result. Free using RTMemFree.
|
---|
514 | * @param pcbOutput Where to the return length of the result (bytes/chars).
|
---|
515 | */
|
---|
516 | int SharedClipboardWinConvertCFHTMLToMIME(const char *pszSource, const uint32_t cch, char **ppszOutput, uint32_t *pcbOutput)
|
---|
517 | {
|
---|
518 | Assert(pszSource);
|
---|
519 | Assert(cch);
|
---|
520 | Assert(ppszOutput);
|
---|
521 | Assert(pcbOutput);
|
---|
522 |
|
---|
523 | uint32_t offStart;
|
---|
524 | int rc = SharedClipboardWinGetCFHTMLHeaderValue(pszSource, "StartFragment:", &offStart);
|
---|
525 | if (RT_SUCCESS(rc))
|
---|
526 | {
|
---|
527 | uint32_t offEnd;
|
---|
528 | rc = SharedClipboardWinGetCFHTMLHeaderValue(pszSource, "EndFragment:", &offEnd);
|
---|
529 | if (RT_SUCCESS(rc))
|
---|
530 | {
|
---|
531 | if ( offStart > 0
|
---|
532 | && offEnd > 0
|
---|
533 | && offEnd > offStart
|
---|
534 | && offEnd <= cch)
|
---|
535 | {
|
---|
536 | uint32_t cchSubStr = offEnd - offStart;
|
---|
537 | char *pszResult = (char *)RTMemAlloc(cchSubStr + 1);
|
---|
538 | if (pszResult)
|
---|
539 | {
|
---|
540 | rc = RTStrCopyEx(pszResult, cchSubStr + 1, pszSource + offStart, cchSubStr);
|
---|
541 | if (RT_SUCCESS(rc))
|
---|
542 | {
|
---|
543 | *ppszOutput = pszResult;
|
---|
544 | *pcbOutput = (uint32_t)(cchSubStr + 1);
|
---|
545 | rc = VINF_SUCCESS;
|
---|
546 | }
|
---|
547 | else
|
---|
548 | {
|
---|
549 | LogRelFlowFunc(("Error: Unknown CF_HTML format. Expected EndFragment. rc = %Rrc\n", rc));
|
---|
550 | RTMemFree(pszResult);
|
---|
551 | }
|
---|
552 | }
|
---|
553 | else
|
---|
554 | {
|
---|
555 | LogRelFlowFunc(("Error: Unknown CF_HTML format. Expected EndFragment\n"));
|
---|
556 | rc = VERR_NO_MEMORY;
|
---|
557 | }
|
---|
558 | }
|
---|
559 | else
|
---|
560 | {
|
---|
561 | LogRelFlowFunc(("Error: CF_HTML out of bounds - offStart=%#x offEnd=%#x cch=%#x\n", offStart, offEnd, cch));
|
---|
562 | rc = VERR_INVALID_PARAMETER;
|
---|
563 | }
|
---|
564 | }
|
---|
565 | else
|
---|
566 | {
|
---|
567 | LogRelFlowFunc(("Error: Unknown CF_HTML format. Expected EndFragment. rc = %Rrc\n", rc));
|
---|
568 | rc = VERR_INVALID_PARAMETER;
|
---|
569 | }
|
---|
570 | }
|
---|
571 | else
|
---|
572 | {
|
---|
573 | LogRelFlowFunc(("Error: Unknown CF_HTML format. Expected StartFragment. rc = %Rrc\n", rc));
|
---|
574 | rc = VERR_INVALID_PARAMETER;
|
---|
575 | }
|
---|
576 |
|
---|
577 | return rc;
|
---|
578 | }
|
---|
579 |
|
---|
580 | /**
|
---|
581 | * Converts source UTF-8 MIME HTML clipboard data to UTF-8 CF_HTML format.
|
---|
582 | *
|
---|
583 | * This is just encapsulation work, slapping a header on the data.
|
---|
584 | *
|
---|
585 | * It allocates [..]
|
---|
586 | *
|
---|
587 | * Calculations:
|
---|
588 | * Header length = format Length + (2*(10 - 5('%010d'))('digits')) - 2('%s') = format length + 8
|
---|
589 | * EndHtml = Header length + fragment length
|
---|
590 | * StartHtml = 105(constant)
|
---|
591 | * StartFragment = 141(constant) may vary if the header html content will be extended
|
---|
592 | * EndFragment = Header length + fragment length - 38(ending length)
|
---|
593 | *
|
---|
594 | * @param pszSource Source buffer that contains utf-16 string in mime html format
|
---|
595 | * @param cb Size of source buffer in bytes
|
---|
596 | * @param ppszOutput Where to return the allocated output buffer to put converted UTF-8
|
---|
597 | * CF_HTML clipboard data. This function allocates memory for this.
|
---|
598 | * @param pcbOutput Where to return the size of allocated result buffer in bytes/chars, including zero terminator
|
---|
599 | *
|
---|
600 | * @note output buffer should be free using RTMemFree()
|
---|
601 | * @note Everything inside of fragment can be UTF8. Windows allows it. Everything in header should be Latin1.
|
---|
602 | */
|
---|
603 | int SharedClipboardWinConvertMIMEToCFHTML(const char *pszSource, size_t cb, char **ppszOutput, uint32_t *pcbOutput)
|
---|
604 | {
|
---|
605 | Assert(ppszOutput);
|
---|
606 | Assert(pcbOutput);
|
---|
607 | Assert(pszSource);
|
---|
608 | Assert(cb);
|
---|
609 |
|
---|
610 | /* construct CF_HTML formatted string */
|
---|
611 | char *pszResult = NULL;
|
---|
612 | size_t cchFragment;
|
---|
613 | int rc = RTStrNLenEx(pszSource, cb, &cchFragment);
|
---|
614 | if (!RT_SUCCESS(rc))
|
---|
615 | {
|
---|
616 | LogRelFlowFunc(("Error: invalid source fragment. rc = %Rrc\n"));
|
---|
617 | return VERR_INVALID_PARAMETER;
|
---|
618 | }
|
---|
619 |
|
---|
620 | /*
|
---|
621 | @StartHtml - pos before <html>
|
---|
622 | @EndHtml - whole size of text excluding ending zero char
|
---|
623 | @StartFragment - pos after <!--StartFragment-->
|
---|
624 | @EndFragment - pos before <!--EndFragment-->
|
---|
625 | @note: all values includes CR\LF inserted into text
|
---|
626 | Calculations:
|
---|
627 | Header length = format Length + (3*6('digits')) - 2('%s') = format length + 16 (control value - 183)
|
---|
628 | EndHtml = Header length + fragment length
|
---|
629 | StartHtml = 105(constant)
|
---|
630 | StartFragment = 143(constant)
|
---|
631 | EndFragment = Header length + fragment length - 40(ending length)
|
---|
632 | */
|
---|
633 | static const char s_szFormatSample[] =
|
---|
634 | /* 0: */ "Version:1.0\r\n"
|
---|
635 | /* 13: */ "StartHTML:000000101\r\n"
|
---|
636 | /* 34: */ "EndHTML:%0000009u\r\n" // END HTML = Header length + fragment length
|
---|
637 | /* 53: */ "StartFragment:000000137\r\n"
|
---|
638 | /* 78: */ "EndFragment:%0000009u\r\n"
|
---|
639 | /* 101: */ "<html>\r\n"
|
---|
640 | /* 109: */ "<body>\r\n"
|
---|
641 | /* 117: */ "<!--StartFragment-->"
|
---|
642 | /* 137: */ "%s"
|
---|
643 | /* 137+2: */ "<!--EndFragment-->\r\n"
|
---|
644 | /* 157+2: */ "</body>\r\n"
|
---|
645 | /* 166+2: */ "</html>\r\n";
|
---|
646 | /* 175+2: */
|
---|
647 | AssertCompile(sizeof(s_szFormatSample) == 175 + 2 + 1);
|
---|
648 |
|
---|
649 | /* calculate parameters of CF_HTML header */
|
---|
650 | size_t cchHeader = sizeof(s_szFormatSample) - 1;
|
---|
651 | size_t offEndHtml = cchHeader + cchFragment;
|
---|
652 | size_t offEndFragment = cchHeader + cchFragment - 38; /* 175-137 = 38 */
|
---|
653 | pszResult = (char *)RTMemAlloc(offEndHtml + 1);
|
---|
654 | if (pszResult == NULL)
|
---|
655 | {
|
---|
656 | LogRelFlowFunc(("Error: Cannot allocate memory for result buffer. rc = %Rrc\n"));
|
---|
657 | return VERR_NO_MEMORY;
|
---|
658 | }
|
---|
659 |
|
---|
660 | /* format result CF_HTML string */
|
---|
661 | size_t cchFormatted = RTStrPrintf(pszResult, offEndHtml + 1,
|
---|
662 | s_szFormatSample, offEndHtml, offEndFragment, pszSource);
|
---|
663 | Assert(offEndHtml == cchFormatted); NOREF(cchFormatted);
|
---|
664 |
|
---|
665 | #ifdef VBOX_STRICT
|
---|
666 | /* Control calculations. check consistency.*/
|
---|
667 | static const char s_szStartFragment[] = "<!--StartFragment-->";
|
---|
668 | static const char s_szEndFragment[] = "<!--EndFragment-->";
|
---|
669 |
|
---|
670 | /* check 'StartFragment:' value */
|
---|
671 | const char *pszRealStartFragment = RTStrStr(pszResult, s_szStartFragment);
|
---|
672 | Assert(&pszRealStartFragment[sizeof(s_szStartFragment) - 1] - pszResult == 137);
|
---|
673 |
|
---|
674 | /* check 'EndFragment:' value */
|
---|
675 | const char *pszRealEndFragment = RTStrStr(pszResult, s_szEndFragment);
|
---|
676 | Assert((size_t)(pszRealEndFragment - pszResult) == offEndFragment);
|
---|
677 | #endif
|
---|
678 |
|
---|
679 | *ppszOutput = pszResult;
|
---|
680 | *pcbOutput = (uint32_t)cchFormatted + 1;
|
---|
681 | Assert(*pcbOutput == cchFormatted + 1);
|
---|
682 |
|
---|
683 | return VINF_SUCCESS;
|
---|
684 | }
|
---|
685 |
|
---|
686 | /**
|
---|
687 | * Handles the WM_CHANGECBCHAIN code.
|
---|
688 | *
|
---|
689 | * @returns LRESULT
|
---|
690 | * @param pWinCtx Windows context to use.
|
---|
691 | * @param hWnd Window handle to use.
|
---|
692 | * @param msg Message ID to pass on.
|
---|
693 | * @param wParam wParam to pass on
|
---|
694 | * @param lParam lParam to pass on.
|
---|
695 | */
|
---|
696 | LRESULT SharedClipboardWinHandleWMChangeCBChain(PSHCLWINCTX pWinCtx,
|
---|
697 | HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
|
---|
698 | {
|
---|
699 | LRESULT lresultRc = 0;
|
---|
700 |
|
---|
701 | LogFlowFuncEnter();
|
---|
702 |
|
---|
703 | if (SharedClipboardWinIsNewAPI(&pWinCtx->newAPI))
|
---|
704 | {
|
---|
705 | lresultRc = DefWindowProc(hWnd, msg, wParam, lParam);
|
---|
706 | }
|
---|
707 | else /* Old API */
|
---|
708 | {
|
---|
709 | HWND hwndRemoved = (HWND)wParam;
|
---|
710 | HWND hwndNext = (HWND)lParam;
|
---|
711 |
|
---|
712 | if (hwndRemoved == pWinCtx->hWndNextInChain)
|
---|
713 | {
|
---|
714 | /* The window that was next to our in the chain is being removed.
|
---|
715 | * Relink to the new next window.
|
---|
716 | */
|
---|
717 | pWinCtx->hWndNextInChain = hwndNext;
|
---|
718 | }
|
---|
719 | else
|
---|
720 | {
|
---|
721 | if (pWinCtx->hWndNextInChain)
|
---|
722 | {
|
---|
723 | /* Pass the message further. */
|
---|
724 | DWORD_PTR dwResult;
|
---|
725 | lresultRc = SendMessageTimeout(pWinCtx->hWndNextInChain, WM_CHANGECBCHAIN, wParam, lParam, 0,
|
---|
726 | SHCL_WIN_CBCHAIN_TIMEOUT_MS,
|
---|
727 | &dwResult);
|
---|
728 | if (!lresultRc)
|
---|
729 | lresultRc = (LRESULT)dwResult;
|
---|
730 | }
|
---|
731 | }
|
---|
732 | }
|
---|
733 |
|
---|
734 | LogFlowFunc(("lresultRc=%ld\n", lresultRc));
|
---|
735 | return lresultRc;
|
---|
736 | }
|
---|
737 |
|
---|
738 | /**
|
---|
739 | * Handles the WM_DESTROY code.
|
---|
740 | *
|
---|
741 | * @returns VBox status code.
|
---|
742 | * @param pWinCtx Windows context to use.
|
---|
743 | */
|
---|
744 | int SharedClipboardWinHandleWMDestroy(PSHCLWINCTX pWinCtx)
|
---|
745 | {
|
---|
746 | LogFlowFuncEnter();
|
---|
747 |
|
---|
748 | int rc = VINF_SUCCESS;
|
---|
749 |
|
---|
750 | /* MS recommends to remove from Clipboard chain in this callback. */
|
---|
751 | SharedClipboardWinChainRemove(pWinCtx);
|
---|
752 |
|
---|
753 | if (pWinCtx->oldAPI.timerRefresh)
|
---|
754 | {
|
---|
755 | Assert(pWinCtx->hWnd);
|
---|
756 | KillTimer(pWinCtx->hWnd, 0);
|
---|
757 | }
|
---|
758 |
|
---|
759 | LogFlowFuncLeaveRC(rc);
|
---|
760 | return rc;
|
---|
761 | }
|
---|
762 |
|
---|
763 | /**
|
---|
764 | * Handles the WM_RENDERALLFORMATS message.
|
---|
765 | *
|
---|
766 | * @returns VBox status code.
|
---|
767 | * @param pWinCtx Windows context to use.
|
---|
768 | * @param hWnd Window handle to use.
|
---|
769 | */
|
---|
770 | int SharedClipboardWinHandleWMRenderAllFormats(PSHCLWINCTX pWinCtx, HWND hWnd)
|
---|
771 | {
|
---|
772 | RT_NOREF(pWinCtx);
|
---|
773 |
|
---|
774 | LogFlowFuncEnter();
|
---|
775 |
|
---|
776 | /* Do nothing. The clipboard formats will be unavailable now, because the
|
---|
777 | * windows is to be destroyed and therefore the guest side becomes inactive.
|
---|
778 | */
|
---|
779 | int rc = SharedClipboardWinOpen(hWnd);
|
---|
780 | if (RT_SUCCESS(rc))
|
---|
781 | {
|
---|
782 | SharedClipboardWinClear();
|
---|
783 | SharedClipboardWinClose();
|
---|
784 | }
|
---|
785 |
|
---|
786 | LogFlowFuncLeaveRC(rc);
|
---|
787 | return rc;
|
---|
788 | }
|
---|
789 |
|
---|
790 | /**
|
---|
791 | * Handles the WM_TIMER code, which is needed if we're running with the so-called "old" Windows clipboard API.
|
---|
792 | * Does nothing if we're running with the "new" Windows API.
|
---|
793 | *
|
---|
794 | * @returns VBox status code.
|
---|
795 | * @param pWinCtx Windows context to use.
|
---|
796 | */
|
---|
797 | int SharedClipboardWinHandleWMTimer(PSHCLWINCTX pWinCtx)
|
---|
798 | {
|
---|
799 | int rc = VINF_SUCCESS;
|
---|
800 |
|
---|
801 | if (!SharedClipboardWinIsNewAPI(&pWinCtx->newAPI)) /* Only run when using the "old" Windows API. */
|
---|
802 | {
|
---|
803 | LogFlowFuncEnter();
|
---|
804 |
|
---|
805 | HWND hViewer = GetClipboardViewer();
|
---|
806 |
|
---|
807 | /* Re-register ourselves in the clipboard chain if our last ping
|
---|
808 | * timed out or there seems to be no valid chain. */
|
---|
809 | if (!hViewer || pWinCtx->oldAPI.fCBChainPingInProcess)
|
---|
810 | {
|
---|
811 | SharedClipboardWinChainRemove(pWinCtx);
|
---|
812 | SharedClipboardWinChainAdd(pWinCtx);
|
---|
813 | }
|
---|
814 |
|
---|
815 | /* Start a new ping by passing a dummy WM_CHANGECBCHAIN to be
|
---|
816 | * processed by ourselves to the chain. */
|
---|
817 | pWinCtx->oldAPI.fCBChainPingInProcess = TRUE;
|
---|
818 |
|
---|
819 | hViewer = GetClipboardViewer();
|
---|
820 | if (hViewer)
|
---|
821 | SendMessageCallback(hViewer, WM_CHANGECBCHAIN, (WPARAM)pWinCtx->hWndNextInChain, (LPARAM)pWinCtx->hWndNextInChain,
|
---|
822 | SharedClipboardWinChainPingProc, (ULONG_PTR)pWinCtx);
|
---|
823 | }
|
---|
824 |
|
---|
825 | LogFlowFuncLeaveRC(rc);
|
---|
826 | return rc;
|
---|
827 | }
|
---|
828 |
|
---|
829 | /**
|
---|
830 | * Announces a clipboard format to the Windows clipboard.
|
---|
831 | * The actual rendering (setting) of the clipboard data will be done later with a separate WM_RENDERFORMAT message.
|
---|
832 | *
|
---|
833 | * @returns VBox status code. VERR_NOT_SUPPORTED if the format is not supported / handled.
|
---|
834 | * @param pWinCtx Windows context to use.
|
---|
835 | * @param fFormats Clipboard format(s) to announce.
|
---|
836 | */
|
---|
837 | int SharedClipboardWinAnnounceFormats(PSHCLWINCTX pWinCtx, SHCLFORMATS fFormats)
|
---|
838 | {
|
---|
839 | LogFunc(("fFormats=0x%x\n", fFormats));
|
---|
840 |
|
---|
841 | HANDLE hClip = NULL;
|
---|
842 | UINT cfFormat = 0;
|
---|
843 |
|
---|
844 | int rc = VINF_SUCCESS;
|
---|
845 |
|
---|
846 | /** @todo r=andy Only one clipboard format can be set at once, at least on Windows. */
|
---|
847 | /** @todo Implement more flexible clipboard precedence for supported formats. */
|
---|
848 |
|
---|
849 | if (fFormats & VBOX_SHCL_FMT_UNICODETEXT)
|
---|
850 | {
|
---|
851 | LogFunc(("CF_UNICODETEXT\n"));
|
---|
852 | hClip = SetClipboardData(CF_UNICODETEXT, NULL);
|
---|
853 | }
|
---|
854 | else if (fFormats & VBOX_SHCL_FMT_BITMAP)
|
---|
855 | {
|
---|
856 | LogFunc(("CF_DIB\n"));
|
---|
857 | hClip = SetClipboardData(CF_DIB, NULL);
|
---|
858 | }
|
---|
859 | else if (fFormats & VBOX_SHCL_FMT_HTML)
|
---|
860 | {
|
---|
861 | LogFunc(("VBOX_SHCL_FMT_HTML\n"));
|
---|
862 | cfFormat = RegisterClipboardFormat(SHCL_WIN_REGFMT_HTML);
|
---|
863 | if (cfFormat != 0)
|
---|
864 | hClip = SetClipboardData(cfFormat, NULL);
|
---|
865 | }
|
---|
866 | else
|
---|
867 | {
|
---|
868 | LogRel(("Shared Clipboard: Unsupported format(s) (0x%x), skipping\n", fFormats));
|
---|
869 | rc = VERR_NOT_SUPPORTED;
|
---|
870 | }
|
---|
871 |
|
---|
872 | if (RT_SUCCESS(rc))
|
---|
873 | {
|
---|
874 | pWinCtx->hWndClipboardOwnerUs = GetClipboardOwner();
|
---|
875 | }
|
---|
876 |
|
---|
877 | LogFlowFuncLeaveRC(rc);
|
---|
878 | return rc;
|
---|
879 | }
|
---|
880 |
|
---|
881 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
882 | /**
|
---|
883 | * Creates an Shared Clipboard transfer by announcing transfer data (via IDataObject) to Windows.
|
---|
884 | *
|
---|
885 | * This creates the necessary IDataObject + IStream implementations and initiates the actual transfers required for getting
|
---|
886 | * the meta data. Whether or not the actual (file++) transfer(s) are happening is up to the user (at some point) later then.
|
---|
887 | *
|
---|
888 | * @returns VBox status code.
|
---|
889 | * @param pWinCtx Windows context to use.
|
---|
890 | * @param pTransferCtxCtx Transfer contextto use.
|
---|
891 | * @param pTransfer Shared Clipboard transfer to use.
|
---|
892 | */
|
---|
893 | int SharedClipboardWinTransferCreate(PSHCLWINCTX pWinCtx, PSHCLTRANSFER pTransfer)
|
---|
894 | {
|
---|
895 | AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
|
---|
896 |
|
---|
897 | LogFlowFunc(("pWinCtx=%p\n", pWinCtx));
|
---|
898 |
|
---|
899 | AssertReturn(pTransfer->pvUser == NULL, VERR_WRONG_ORDER);
|
---|
900 |
|
---|
901 | /* Make sure to enter the critical section before setting the clipboard data, as otherwise WM_CLIPBOARDUPDATE
|
---|
902 | * might get called *before* we had the opportunity to set pWinCtx->hWndClipboardOwnerUs below. */
|
---|
903 | int rc = RTCritSectEnter(&pWinCtx->CritSect);
|
---|
904 | if (RT_SUCCESS(rc))
|
---|
905 | {
|
---|
906 | SharedClipboardWinTransferCtx *pWinURITransferCtx = new SharedClipboardWinTransferCtx();
|
---|
907 | if (pWinURITransferCtx)
|
---|
908 | {
|
---|
909 | pTransfer->pvUser = pWinURITransferCtx;
|
---|
910 | pTransfer->cbUser = sizeof(SharedClipboardWinTransferCtx);
|
---|
911 |
|
---|
912 | pWinURITransferCtx->pDataObj = new SharedClipboardWinDataObject(pTransfer);
|
---|
913 | if (pWinURITransferCtx->pDataObj)
|
---|
914 | {
|
---|
915 | rc = pWinURITransferCtx->pDataObj->Init();
|
---|
916 | if (RT_SUCCESS(rc))
|
---|
917 | {
|
---|
918 | SharedClipboardWinClose();
|
---|
919 | /* Note: Clipboard must be closed first before calling OleSetClipboard(). */
|
---|
920 |
|
---|
921 | /** @todo There is a potential race between SharedClipboardWinClose() and OleSetClipboard(),
|
---|
922 | * where another application could own the clipboard (open), and thus the call to
|
---|
923 | * OleSetClipboard() will fail. Needs (better) fixing. */
|
---|
924 | HRESULT hr = S_OK;
|
---|
925 |
|
---|
926 | for (unsigned uTries = 0; uTries < 3; uTries++)
|
---|
927 | {
|
---|
928 | hr = OleSetClipboard(pWinURITransferCtx->pDataObj);
|
---|
929 | if (SUCCEEDED(hr))
|
---|
930 | {
|
---|
931 | Assert(OleIsCurrentClipboard(pWinURITransferCtx->pDataObj) == S_OK); /* Sanity. */
|
---|
932 |
|
---|
933 | /*
|
---|
934 | * Calling OleSetClipboard() changed the clipboard owner, which in turn will let us receive
|
---|
935 | * a WM_CLIPBOARDUPDATE message. To not confuse ourselves with our own clipboard owner changes,
|
---|
936 | * save a new window handle and deal with it in WM_CLIPBOARDUPDATE.
|
---|
937 | */
|
---|
938 | pWinCtx->hWndClipboardOwnerUs = GetClipboardOwner();
|
---|
939 |
|
---|
940 | LogFlowFunc(("hWndClipboardOwnerUs=%p\n", pWinCtx->hWndClipboardOwnerUs));
|
---|
941 | break;
|
---|
942 | }
|
---|
943 |
|
---|
944 | LogFlowFunc(("Failed with %Rhrc (try %u/3)\n", hr, uTries + 1));
|
---|
945 | RTThreadSleep(500); /* Wait a bit. */
|
---|
946 | }
|
---|
947 |
|
---|
948 | if (FAILED(hr))
|
---|
949 | {
|
---|
950 | rc = VERR_ACCESS_DENIED; /** @todo Fudge; fix this. */
|
---|
951 | LogRel(("Shared Clipboard: Failed with %Rhrc when setting data object to clipboard\n", hr));
|
---|
952 | }
|
---|
953 | }
|
---|
954 | }
|
---|
955 | else
|
---|
956 | rc = VERR_NO_MEMORY;
|
---|
957 | }
|
---|
958 | else
|
---|
959 | rc = VERR_NO_MEMORY;
|
---|
960 |
|
---|
961 | int rc2 = RTCritSectLeave(&pWinCtx->CritSect);
|
---|
962 | AssertRC(rc2);
|
---|
963 | }
|
---|
964 |
|
---|
965 | LogFlowFuncLeaveRC(rc);
|
---|
966 | return rc;
|
---|
967 | }
|
---|
968 |
|
---|
969 | /**
|
---|
970 | * Destroys implementation-specific data for an Shared Clipboard transfer.
|
---|
971 | *
|
---|
972 | * @param pWinCtx Windows context to use.
|
---|
973 | * @param pTransfer Shared Clipboard transfer to create implementation-specific data for.
|
---|
974 | */
|
---|
975 | void SharedClipboardWinTransferDestroy(PSHCLWINCTX pWinCtx, PSHCLTRANSFER pTransfer)
|
---|
976 | {
|
---|
977 | RT_NOREF(pWinCtx);
|
---|
978 |
|
---|
979 | if (!pTransfer)
|
---|
980 | return;
|
---|
981 |
|
---|
982 | LogFlowFuncEnter();
|
---|
983 |
|
---|
984 | if (pTransfer->pvUser)
|
---|
985 | {
|
---|
986 | Assert(pTransfer->cbUser == sizeof(SharedClipboardWinTransferCtx));
|
---|
987 | SharedClipboardWinTransferCtx *pWinURITransferCtx = (SharedClipboardWinTransferCtx *)pTransfer->pvUser;
|
---|
988 | Assert(pWinURITransferCtx);
|
---|
989 |
|
---|
990 | if (pWinURITransferCtx->pDataObj)
|
---|
991 | {
|
---|
992 | delete pWinURITransferCtx->pDataObj;
|
---|
993 | pWinURITransferCtx->pDataObj = NULL;
|
---|
994 | }
|
---|
995 |
|
---|
996 | delete pWinURITransferCtx;
|
---|
997 |
|
---|
998 | pTransfer->pvUser = NULL;
|
---|
999 | pTransfer->cbUser = 0;
|
---|
1000 | }
|
---|
1001 | }
|
---|
1002 |
|
---|
1003 | /**
|
---|
1004 | * Retrieves the roots for a transfer by opening the clipboard and getting the clipboard data
|
---|
1005 | * as string list (CF_HDROP), assigning it to the transfer as roots then.
|
---|
1006 | *
|
---|
1007 | * @returns VBox status code.
|
---|
1008 | * @param pWinCtx Windows context to use.
|
---|
1009 | * @param pTransfer Transfer to get roots for.
|
---|
1010 | */
|
---|
1011 | int SharedClipboardWinGetRoots(PSHCLWINCTX pWinCtx, PSHCLTRANSFER pTransfer)
|
---|
1012 | {
|
---|
1013 | AssertPtrReturn(pWinCtx, VERR_INVALID_POINTER);
|
---|
1014 | AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
|
---|
1015 |
|
---|
1016 | Assert(ShClTransferGetSource(pTransfer) == SHCLSOURCE_LOCAL); /* Sanity. */
|
---|
1017 |
|
---|
1018 | int rc = SharedClipboardWinOpen(pWinCtx->hWnd);
|
---|
1019 | if (RT_SUCCESS(rc))
|
---|
1020 | {
|
---|
1021 | /* The data data in CF_HDROP format, as the files are locally present and don't need to be
|
---|
1022 | * presented as a IDataObject or IStream. */
|
---|
1023 | HANDLE hClip = hClip = GetClipboardData(CF_HDROP);
|
---|
1024 | if (hClip)
|
---|
1025 | {
|
---|
1026 | HDROP hDrop = (HDROP)GlobalLock(hClip);
|
---|
1027 | if (hDrop)
|
---|
1028 | {
|
---|
1029 | char *papszList = NULL;
|
---|
1030 | uint32_t cbList;
|
---|
1031 | rc = SharedClipboardWinDropFilesToStringList((DROPFILES *)hDrop, &papszList, &cbList);
|
---|
1032 |
|
---|
1033 | GlobalUnlock(hClip);
|
---|
1034 |
|
---|
1035 | if (RT_SUCCESS(rc))
|
---|
1036 | {
|
---|
1037 | rc = ShClTransferRootsSet(pTransfer,
|
---|
1038 | papszList, cbList + 1 /* Include termination */);
|
---|
1039 | RTStrFree(papszList);
|
---|
1040 | }
|
---|
1041 | }
|
---|
1042 | else
|
---|
1043 | LogRel(("Shared Clipboard: Unable to lock clipboard data, last error: %ld\n", GetLastError()));
|
---|
1044 | }
|
---|
1045 | else
|
---|
1046 | LogRel(("Shared Clipboard: Unable to retrieve clipboard data from clipboard (CF_HDROP), last error: %ld\n",
|
---|
1047 | GetLastError()));
|
---|
1048 |
|
---|
1049 | SharedClipboardWinClose();
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 | LogFlowFuncLeaveRC(rc);
|
---|
1053 | return rc;
|
---|
1054 | }
|
---|
1055 |
|
---|
1056 | /**
|
---|
1057 | * Converts a DROPFILES (HDROP) structure to a string list, separated by \r\n.
|
---|
1058 | * Does not do any locking on the input data.
|
---|
1059 | *
|
---|
1060 | * @returns VBox status code.
|
---|
1061 | * @param pDropFiles Pointer to DROPFILES structure to convert.
|
---|
1062 | * @param papszList Where to store the allocated string list.
|
---|
1063 | * @param pcbList Where to store the size (in bytes) of the allocated string list.
|
---|
1064 | */
|
---|
1065 | int SharedClipboardWinDropFilesToStringList(DROPFILES *pDropFiles, char **papszList, uint32_t *pcbList)
|
---|
1066 | {
|
---|
1067 | AssertPtrReturn(pDropFiles, VERR_INVALID_POINTER);
|
---|
1068 | AssertPtrReturn(papszList, VERR_INVALID_POINTER);
|
---|
1069 | AssertPtrReturn(pcbList, VERR_INVALID_POINTER);
|
---|
1070 |
|
---|
1071 | /* Do we need to do Unicode stuff? */
|
---|
1072 | const bool fUnicode = RT_BOOL(pDropFiles->fWide);
|
---|
1073 |
|
---|
1074 | /* Get the offset of the file list. */
|
---|
1075 | Assert(pDropFiles->pFiles >= sizeof(DROPFILES));
|
---|
1076 |
|
---|
1077 | /* Note: This is *not* pDropFiles->pFiles! DragQueryFile only
|
---|
1078 | * will work with the plain storage medium pointer! */
|
---|
1079 | HDROP hDrop = (HDROP)(pDropFiles);
|
---|
1080 |
|
---|
1081 | int rc = VINF_SUCCESS;
|
---|
1082 |
|
---|
1083 | /* First, get the file count. */
|
---|
1084 | /** @todo Does this work on Windows 2000 / NT4? */
|
---|
1085 | char *pszFiles = NULL;
|
---|
1086 | uint32_t cchFiles = 0;
|
---|
1087 | UINT cFiles = DragQueryFile(hDrop, UINT32_MAX /* iFile */, NULL /* lpszFile */, 0 /* cchFile */);
|
---|
1088 |
|
---|
1089 | LogFlowFunc(("Got %RU16 file(s), fUnicode=%RTbool\n", cFiles, fUnicode));
|
---|
1090 |
|
---|
1091 | for (UINT i = 0; i < cFiles; i++)
|
---|
1092 | {
|
---|
1093 | UINT cchFile = DragQueryFile(hDrop, i /* File index */, NULL /* Query size first */, 0 /* cchFile */);
|
---|
1094 | Assert(cchFile);
|
---|
1095 |
|
---|
1096 | if (RT_FAILURE(rc))
|
---|
1097 | break;
|
---|
1098 |
|
---|
1099 | char *pszFileUtf8 = NULL; /* UTF-8 version. */
|
---|
1100 | UINT cchFileUtf8 = 0;
|
---|
1101 | if (fUnicode)
|
---|
1102 | {
|
---|
1103 | /* Allocate enough space (including terminator). */
|
---|
1104 | WCHAR *pwszFile = (WCHAR *)RTMemAlloc((cchFile + 1) * sizeof(WCHAR));
|
---|
1105 | if (pwszFile)
|
---|
1106 | {
|
---|
1107 | const UINT cwcFileUtf16 = DragQueryFileW(hDrop, i /* File index */,
|
---|
1108 | pwszFile, cchFile + 1 /* Include terminator */);
|
---|
1109 |
|
---|
1110 | AssertMsg(cwcFileUtf16 == cchFile, ("cchFileUtf16 (%RU16) does not match cchFile (%RU16)\n",
|
---|
1111 | cwcFileUtf16, cchFile));
|
---|
1112 | RT_NOREF(cwcFileUtf16);
|
---|
1113 |
|
---|
1114 | rc = RTUtf16ToUtf8(pwszFile, &pszFileUtf8);
|
---|
1115 | if (RT_SUCCESS(rc))
|
---|
1116 | {
|
---|
1117 | cchFileUtf8 = (UINT)strlen(pszFileUtf8);
|
---|
1118 | Assert(cchFileUtf8);
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 | RTMemFree(pwszFile);
|
---|
1122 | }
|
---|
1123 | else
|
---|
1124 | rc = VERR_NO_MEMORY;
|
---|
1125 | }
|
---|
1126 | else /* ANSI */
|
---|
1127 | {
|
---|
1128 | /* Allocate enough space (including terminator). */
|
---|
1129 | char *pszFileANSI = (char *)RTMemAlloc((cchFile + 1) * sizeof(char));
|
---|
1130 | UINT cchFileANSI = 0;
|
---|
1131 | if (pszFileANSI)
|
---|
1132 | {
|
---|
1133 | cchFileANSI = DragQueryFileA(hDrop, i /* File index */,
|
---|
1134 | pszFileANSI, cchFile + 1 /* Include terminator */);
|
---|
1135 |
|
---|
1136 | AssertMsg(cchFileANSI == cchFile, ("cchFileANSI (%RU16) does not match cchFile (%RU16)\n",
|
---|
1137 | cchFileANSI, cchFile));
|
---|
1138 |
|
---|
1139 | /* Convert the ANSI codepage to UTF-8. */
|
---|
1140 | rc = RTStrCurrentCPToUtf8(&pszFileUtf8, pszFileANSI);
|
---|
1141 | if (RT_SUCCESS(rc))
|
---|
1142 | {
|
---|
1143 | cchFileUtf8 = (UINT)strlen(pszFileUtf8);
|
---|
1144 | }
|
---|
1145 | }
|
---|
1146 | else
|
---|
1147 | rc = VERR_NO_MEMORY;
|
---|
1148 | }
|
---|
1149 |
|
---|
1150 | if (RT_SUCCESS(rc))
|
---|
1151 | {
|
---|
1152 | LogFlowFunc(("\tFile: %s (cchFile=%RU16)\n", pszFileUtf8, cchFileUtf8));
|
---|
1153 |
|
---|
1154 | LogRel2(("Shared Clipboard: Adding file '%s' to transfer\n", pszFileUtf8));
|
---|
1155 |
|
---|
1156 | rc = RTStrAAppendExN(&pszFiles, 1 /* cPairs */, pszFileUtf8, strlen(pszFileUtf8));
|
---|
1157 | cchFiles += (uint32_t)strlen(pszFileUtf8);
|
---|
1158 | }
|
---|
1159 |
|
---|
1160 | if (pszFileUtf8)
|
---|
1161 | RTStrFree(pszFileUtf8);
|
---|
1162 |
|
---|
1163 | if (RT_FAILURE(rc))
|
---|
1164 | {
|
---|
1165 | LogFunc(("Error handling file entry #%u, rc=%Rrc\n", i, rc));
|
---|
1166 | break;
|
---|
1167 | }
|
---|
1168 |
|
---|
1169 | /* Add separation between filenames.
|
---|
1170 | * Note: Also do this for the last element of the list. */
|
---|
1171 | rc = RTStrAAppendExN(&pszFiles, 1 /* cPairs */, "\r\n", 2 /* Bytes */);
|
---|
1172 | if (RT_SUCCESS(rc))
|
---|
1173 | cchFiles += 2; /* Include \r\n */
|
---|
1174 | }
|
---|
1175 |
|
---|
1176 | if (RT_SUCCESS(rc))
|
---|
1177 | {
|
---|
1178 | cchFiles += 1; /* Add string termination. */
|
---|
1179 | uint32_t cbFiles = cchFiles * sizeof(char); /* UTF-8. */
|
---|
1180 |
|
---|
1181 | LogFlowFunc(("cFiles=%u, cchFiles=%RU32, cbFiles=%RU32, pszFiles=0x%p\n",
|
---|
1182 | cFiles, cchFiles, cbFiles, pszFiles));
|
---|
1183 |
|
---|
1184 | *papszList = pszFiles;
|
---|
1185 | *pcbList = cbFiles;
|
---|
1186 | }
|
---|
1187 | else
|
---|
1188 | {
|
---|
1189 | if (pszFiles)
|
---|
1190 | RTStrFree(pszFiles);
|
---|
1191 | }
|
---|
1192 |
|
---|
1193 | LogFlowFuncLeaveRC(rc);
|
---|
1194 | return rc;
|
---|
1195 | }
|
---|
1196 | #endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
|
---|
1197 |
|
---|