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