VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedClipboard/darwin-pasteboard.cpp@ 83621

Last change on this file since 83621 was 83621, checked in by vboxsync, 5 years ago

SharedClipboard/darwin: Logging. bugref:9620

  • Property eol-style set to native
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.9 KB
Line 
1/* $Id: darwin-pasteboard.cpp 83621 2020-04-08 15:02:16Z vboxsync $ */
2/** @file
3 * Shared Clipboard Service - Mac OS X host implementation.
4 */
5
6/*
7 * Includes contributions from François Revol
8 *
9 * Copyright (C) 2008-2020 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20
21/*********************************************************************************************************************************
22* Header Files *
23*********************************************************************************************************************************/
24#define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
25#include <Carbon/Carbon.h>
26
27#include <iprt/assert.h>
28#include <iprt/mem.h>
29#include <iprt/errcore.h>
30#include <iprt/utf16.h>
31
32#include "VBox/log.h"
33#include "VBox/HostServices/VBoxClipboardSvc.h"
34#include "VBox/GuestHost/SharedClipboard.h"
35#include "VBox/GuestHost/clipboard-helper.h"
36
37
38/*********************************************************************************************************************************
39* Defined Constants And Macros *
40*********************************************************************************************************************************/
41/* For debugging */
42//#define SHOW_CLIPBOARD_CONTENT
43
44
45/**
46 * Initialize the global pasteboard and return a reference to it.
47 *
48 * @param pPasteboardRef Reference to the global pasteboard.
49 *
50 * @returns IPRT status code.
51 */
52int initPasteboard(PasteboardRef *pPasteboardRef)
53{
54 int rc = VINF_SUCCESS;
55
56 if (PasteboardCreate(kPasteboardClipboard, pPasteboardRef))
57 rc = VERR_NOT_SUPPORTED;
58
59 return rc;
60}
61
62/**
63 * Release the reference to the global pasteboard.
64 *
65 * @param pPasteboardRef Reference to the global pasteboard.
66 */
67void destroyPasteboard(PasteboardRef *pPasteboardRef)
68{
69 CFRelease(*pPasteboardRef);
70 *pPasteboardRef = NULL;
71}
72
73/**
74 * Inspect the global pasteboard for new content. Check if there is some type
75 * that is supported by vbox and return it.
76 *
77 * @param pPasteboard Reference to the global pasteboard.
78 * @param pfFormats Pointer for the bit combination of the
79 * supported types.
80 * @param pfChanged True if something has changed after the
81 * last call.
82 *
83 * @returns IPRT status code. (Always VINF_SUCCESS atm.)
84 */
85int queryNewPasteboardFormats(PasteboardRef pPasteboard, uint32_t *pfFormats, bool *pfChanged)
86{
87 OSStatus err = noErr;
88 *pfFormats = 0;
89 *pfChanged = true;
90
91 PasteboardSyncFlags syncFlags;
92 /* Make sure all is in sync */
93 syncFlags = PasteboardSynchronize(pPasteboard);
94 /* If nothing changed return */
95 if (!(syncFlags & kPasteboardModified))
96 {
97 *pfChanged = false;
98 Log2(("queryNewPasteboardFormats: no change\n"));
99 return VINF_SUCCESS;
100 }
101
102 /* Are some items in the pasteboard? */
103 ItemCount itemCount;
104 err = PasteboardGetItemCount(pPasteboard, &itemCount);
105 if (itemCount < 1)
106 {
107 Log(("queryNewPasteboardFormats: changed: No items on the pasteboard\n"));
108 return VINF_SUCCESS;
109 }
110
111 /* The id of the first element in the pasteboard */
112 int rc = VINF_SUCCESS;
113 PasteboardItemID itemID;
114 if (!(err = PasteboardGetItemIdentifier(pPasteboard, 1, &itemID)))
115 {
116 /* Retrieve all flavors in the pasteboard, maybe there
117 * is something we can use. */
118 CFArrayRef flavorTypeArray;
119 if (!(err = PasteboardCopyItemFlavors(pPasteboard, itemID, &flavorTypeArray)))
120 {
121 CFIndex flavorCount;
122 flavorCount = CFArrayGetCount(flavorTypeArray);
123 for (CFIndex flavorIndex = 0; flavorIndex < flavorCount; flavorIndex++)
124 {
125 CFStringRef flavorType;
126 flavorType = static_cast <CFStringRef>(CFArrayGetValueAtIndex(flavorTypeArray, flavorIndex));
127 /* Currently only unicode supported */
128 if ( UTTypeConformsTo(flavorType, kUTTypeUTF8PlainText)
129 || UTTypeConformsTo(flavorType, kUTTypeUTF16PlainText))
130 {
131 Log(("queryNewPasteboardFormats: Unicode flavor detected.\n"));
132 *pfFormats |= VBOX_SHCL_FMT_UNICODETEXT;
133 }
134 else if (UTTypeConformsTo(flavorType, kUTTypeBMP))
135 {
136 Log(("queryNewPasteboardFormats: BMP flavor detected.\n"));
137 *pfFormats |= VBOX_SHCL_FMT_BITMAP;
138 }
139 }
140 CFRelease(flavorTypeArray);
141 }
142 }
143
144 Log(("queryNewPasteboardFormats: changed: *pfFormats=%#x\n", *pfFormats));
145 return rc;
146}
147
148/**
149 * Read content from the host clipboard and write it to the internal clipboard
150 * structure for further processing.
151 *
152 * @param pPasteboard Reference to the global pasteboard.
153 * @param fFormat The format type which should be read.
154 * @param pv The destination buffer.
155 * @param cb The size of the destination buffer.
156 * @param pcbActual The size which is needed to transfer the content.
157 *
158 * @returns IPRT status code.
159 */
160int readFromPasteboard(PasteboardRef pPasteboard, uint32_t fFormat, void *pv, uint32_t cb, uint32_t *pcbActual)
161{
162 Log(("readFromPasteboard: fFormat = %02X\n", fFormat));
163
164 OSStatus err = noErr;
165
166 /* Make sure all is in sync */
167 PasteboardSynchronize(pPasteboard);
168
169 /* Are some items in the pasteboard? */
170 ItemCount itemCount;
171 err = PasteboardGetItemCount(pPasteboard, &itemCount);
172 if (itemCount < 1)
173 return VINF_SUCCESS;
174
175 /* The id of the first element in the pasteboard */
176 int rc = VERR_NOT_SUPPORTED;
177 PasteboardItemID itemID;
178 if (!(err = PasteboardGetItemIdentifier(pPasteboard, 1, &itemID)))
179 {
180 /* The guest request unicode */
181 if (fFormat & VBOX_SHCL_FMT_UNICODETEXT)
182 {
183 CFDataRef outData;
184 PRTUTF16 pwszTmp = NULL;
185 /* Try utf-16 first */
186 if (!(err = PasteboardCopyItemFlavorData(pPasteboard, itemID, kUTTypeUTF16PlainText, &outData)))
187 {
188 Log(("Clipboard content is utf-16\n"));
189
190 PRTUTF16 pwszString = (PRTUTF16)CFDataGetBytePtr(outData);
191 if (pwszString)
192 rc = RTUtf16DupEx(&pwszTmp, pwszString, 0);
193 else
194 rc = VERR_INVALID_PARAMETER;
195 }
196 /* Second try is utf-8 */
197 else
198 if (!(err = PasteboardCopyItemFlavorData(pPasteboard, itemID, kUTTypeUTF8PlainText, &outData)))
199 {
200 Log(("readFromPasteboard: clipboard content is utf-8\n"));
201 const char *pszString = (const char *)CFDataGetBytePtr(outData);
202 if (pszString)
203 rc = RTStrToUtf16(pszString, &pwszTmp);
204 else
205 rc = VERR_INVALID_PARAMETER;
206 }
207 if (pwszTmp)
208 {
209 /* Check how much longer will the converted text will be. */
210 size_t cwSrc = RTUtf16Len(pwszTmp);
211 size_t cwDest;
212 rc = ShClUtf16GetWinSize(pwszTmp, cwSrc, &cwDest);
213 if (RT_FAILURE(rc))
214 {
215 RTUtf16Free(pwszTmp);
216 Log(("readFromPasteboard: clipboard conversion failed. vboxClipboardUtf16GetWinSize returned %Rrc. Abandoning.\n", rc));
217 AssertRCReturn(rc, rc);
218 }
219 /* Set the actually needed data size */
220 *pcbActual = cwDest * 2;
221 /* Return success state */
222 rc = VINF_SUCCESS;
223 /* Do not copy data if the dst buffer is not big enough. */
224 if (*pcbActual <= cb)
225 {
226 rc = ShClUtf16LinToWin(pwszTmp, RTUtf16Len(pwszTmp), static_cast <PRTUTF16>(pv), cb / 2);
227 if (RT_FAILURE(rc))
228 {
229 RTUtf16Free(pwszTmp);
230 Log(("readFromPasteboard: clipboard conversion failed. vboxClipboardUtf16LinToWin() returned %Rrc. Abandoning.\n", rc));
231 AssertRCReturn(rc, rc);
232 }
233#ifdef SHOW_CLIPBOARD_CONTENT
234 Log(("readFromPasteboard: clipboard content: %ls\n", static_cast <PRTUTF16>(pv)));
235#endif
236 }
237 /* Free the temp string */
238 RTUtf16Free(pwszTmp);
239 }
240 }
241 /* The guest request BITMAP */
242 else if (fFormat & VBOX_SHCL_FMT_BITMAP)
243 {
244 CFDataRef outData;
245 const void *pTmp = NULL;
246 size_t cbTmpSize;
247 /* Get the data from the pasteboard */
248 if (!(err = PasteboardCopyItemFlavorData(pPasteboard, itemID, kUTTypeBMP, &outData)))
249 {
250 Log(("Clipboard content is BMP\n"));
251 pTmp = CFDataGetBytePtr(outData);
252 cbTmpSize = CFDataGetLength(outData);
253 }
254 if (pTmp)
255 {
256 const void *pDib;
257 size_t cbDibSize;
258 rc = ShClBmpGetDib(pTmp, cbTmpSize, &pDib, &cbDibSize);
259 if (RT_FAILURE(rc))
260 {
261 rc = VERR_NOT_SUPPORTED;
262 Log(("readFromPasteboard: unknown bitmap format. vboxClipboardBmpGetDib returned %Rrc. Abandoning.\n", rc));
263 AssertRCReturn(rc, rc);
264 }
265
266 *pcbActual = cbDibSize;
267 /* Return success state */
268 rc = VINF_SUCCESS;
269 /* Do not copy data if the dst buffer is not big enough. */
270 if (*pcbActual <= cb)
271 {
272 memcpy(pv, pDib, cbDibSize);
273#ifdef SHOW_CLIPBOARD_CONTENT
274 Log(("readFromPasteboard: clipboard content bitmap %d bytes\n", cbDibSize));
275#endif
276 }
277 }
278 }
279 }
280
281 Log(("readFromPasteboard: rc = %02X\n", rc));
282 return rc;
283}
284
285/**
286 * Write clipboard content to the host clipboard from the internal clipboard
287 * structure.
288 *
289 * @param pPasteboard Reference to the global pasteboard.
290 * @param pv The source buffer.
291 * @param cb The size of the source buffer.
292 * @param fFormat The format type which should be written.
293 *
294 * @returns IPRT status code.
295 */
296int writeToPasteboard(PasteboardRef pPasteboard, void *pv, uint32_t cb, uint32_t fFormat)
297{
298 Log(("writeToPasteboard: fFormat = %02X\n", fFormat));
299
300 /* Clear the pasteboard */
301 if (PasteboardClear(pPasteboard))
302 return VERR_NOT_SUPPORTED;
303
304 /* Make sure all is in sync */
305 PasteboardSynchronize(pPasteboard);
306
307 int rc = VERR_NOT_SUPPORTED;
308 /* Handle the unicode text */
309 if (fFormat & VBOX_SHCL_FMT_UNICODETEXT)
310 {
311 PRTUTF16 pwszSrcText = static_cast <PRTUTF16>(pv);
312 size_t cwSrc = cb / 2;
313 size_t cwDest = 0;
314 /* How long will the converted text be? */
315 rc = ShClUtf16GetLinSize(pwszSrcText, cwSrc, &cwDest);
316 if (RT_FAILURE(rc))
317 {
318 Log(("writeToPasteboard: clipboard conversion failed. vboxClipboardUtf16GetLinSize returned %Rrc. Abandoning.\n", rc));
319 AssertRCReturn(rc, rc);
320 }
321 /* Empty clipboard? Not critical */
322 if (cwDest == 0)
323 {
324 Log(("writeToPasteboard: received empty clipboard data from the guest, returning false.\n"));
325 return VINF_SUCCESS;
326 }
327 /* Allocate the necessary memory */
328 PRTUTF16 pwszDestText = static_cast <PRTUTF16>(RTMemAlloc(cwDest * 2));
329 if (pwszDestText == NULL)
330 {
331 Log(("writeToPasteboard: failed to allocate %d bytes\n", cwDest * 2));
332 return VERR_NO_MEMORY;
333 }
334 /* Convert the EOL */
335 rc = ShClUtf16WinToLin(pwszSrcText, cwSrc, pwszDestText, cwDest);
336 if (RT_FAILURE(rc))
337 {
338 Log(("writeToPasteboard: clipboard conversion failed. vboxClipboardUtf16WinToLin() returned %Rrc. Abandoning.\n", rc));
339 RTMemFree(pwszDestText);
340 AssertRCReturn(rc, rc);
341 }
342
343 CFDataRef textData = NULL;
344 /* Item id is 1. Nothing special here. */
345 PasteboardItemID itemId = (PasteboardItemID)1;
346 /* Create a CData object which we could pass to the pasteboard */
347 if ((textData = CFDataCreate(kCFAllocatorDefault,
348 reinterpret_cast<UInt8*>(pwszDestText), cwDest * 2)))
349 {
350 /* Put the Utf-16 version to the pasteboard */
351 PasteboardPutItemFlavor(pPasteboard, itemId,
352 kUTTypeUTF16PlainText,
353 textData, 0);
354 }
355 /* Create a Utf-8 version */
356 char *pszDestText;
357 rc = RTUtf16ToUtf8(pwszDestText, &pszDestText);
358 if (RT_SUCCESS(rc))
359 {
360 /* Create a CData object which we could pass to the pasteboard */
361 if ((textData = CFDataCreate(kCFAllocatorDefault,
362 reinterpret_cast<UInt8*>(pszDestText), strlen(pszDestText))))
363 {
364 /* Put the Utf-8 version to the pasteboard */
365 PasteboardPutItemFlavor(pPasteboard, itemId,
366 kUTTypeUTF8PlainText,
367 textData, 0);
368 }
369 RTStrFree(pszDestText);
370 }
371
372 RTMemFree(pwszDestText);
373 rc = VINF_SUCCESS;
374 }
375 /* Handle the bitmap */
376 else if (fFormat & VBOX_SHCL_FMT_BITMAP)
377 {
378 /* Create a full BMP from it */
379 void *pBmp;
380 size_t cbBmpSize;
381 CFDataRef bmpData = NULL;
382 /* Item id is 1. Nothing special here. */
383 PasteboardItemID itemId = (PasteboardItemID)1;
384
385 rc = ShClDibToBmp(pv, cb, &pBmp, &cbBmpSize);
386 if (RT_SUCCESS(rc))
387 {
388 /* Create a CData object which we could pass to the pasteboard */
389 if ((bmpData = CFDataCreate(kCFAllocatorDefault,
390 reinterpret_cast<UInt8*>(pBmp), cbBmpSize)))
391 {
392 /* Put the Utf-8 version to the pasteboard */
393 PasteboardPutItemFlavor(pPasteboard, itemId,
394 kUTTypeBMP,
395 bmpData, 0);
396 }
397 RTMemFree(pBmp);
398 }
399 rc = VINF_SUCCESS;
400 }
401 else
402 rc = VERR_NOT_IMPLEMENTED;
403
404 Log(("writeToPasteboard: rc = %02X\n", rc));
405 return rc;
406}
407
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette