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