Changeset 41183 in vbox
- Timestamp:
- May 7, 2012 1:09:44 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/HostServices/SharedClipboard/darwin-pasteboard.cpp
r41083 r41183 5 5 6 6 /* 7 * Copyright (C) 2008 Oracle Corporation7 * Copyright (C) 2008-2012 Oracle Corporation 8 8 * 9 9 * This file is part of VirtualBox Open Source Edition (OSE), as … … 37 37 * @returns IPRT status code. 38 38 */ 39 int initPasteboard 39 int initPasteboard(PasteboardRef *pPasteboardRef) 40 40 { 41 41 int rc = VINF_SUCCESS; 42 42 43 if (PasteboardCreate 43 if (PasteboardCreate(kPasteboardClipboard, pPasteboardRef)) 44 44 rc = VERR_NOT_SUPPORTED; 45 45 … … 52 52 * @param pPasteboardRef Reference to the global pasteboard. 53 53 */ 54 void destroyPasteboard 55 { 56 CFRelease 54 void destroyPasteboard(PasteboardRef *pPasteboardRef) 55 { 56 CFRelease(*pPasteboardRef); 57 57 *pPasteboardRef = NULL; 58 58 } … … 70 70 * @returns IPRT status code. (Always VINF_SUCCESS atm.) 71 71 */ 72 int queryNewPasteboardFormats 73 { 74 Log 72 int queryNewPasteboardFormats(PasteboardRef pPasteboard, uint32_t *pfFormats, bool *pfChanged) 73 { 74 Log(("queryNewPasteboardFormats\n")); 75 75 76 76 OSStatus err = noErr; … … 79 79 PasteboardSyncFlags syncFlags; 80 80 /* Make sure all is in sync */ 81 syncFlags = PasteboardSynchronize 81 syncFlags = PasteboardSynchronize(pPasteboard); 82 82 /* If nothing changed return */ 83 83 if (!(syncFlags & kPasteboardModified)) … … 89 89 /* Are some items in the pasteboard? */ 90 90 ItemCount itemCount; 91 err = PasteboardGetItemCount 91 err = PasteboardGetItemCount(pPasteboard, &itemCount); 92 92 if (itemCount < 1) 93 93 return VINF_SUCCESS; … … 96 96 int rc = VINF_SUCCESS; 97 97 PasteboardItemID itemID; 98 if (!(err = PasteboardGetItemIdentifier 98 if (!(err = PasteboardGetItemIdentifier(pPasteboard, 1, &itemID))) 99 99 { 100 100 /* Retrieve all flavors in the pasteboard, maybe there 101 101 * is something we can use. */ 102 102 CFArrayRef flavorTypeArray; 103 if (!(err = PasteboardCopyItemFlavors 103 if (!(err = PasteboardCopyItemFlavors(pPasteboard, itemID, &flavorTypeArray))) 104 104 { 105 105 CFIndex flavorCount; 106 flavorCount = CFArrayGetCount 106 flavorCount = CFArrayGetCount(flavorTypeArray); 107 107 for (CFIndex flavorIndex = 0; flavorIndex < flavorCount; flavorIndex++) 108 108 { 109 109 CFStringRef flavorType; 110 flavorType = static_cast <CFStringRef> (CFArrayGetValueAtIndex(flavorTypeArray,111 110 flavorType = static_cast <CFStringRef>(CFArrayGetValueAtIndex(flavorTypeArray, 111 flavorIndex)); 112 112 /* Currently only unicode supported */ 113 if (UTTypeConformsTo (flavorType, CFSTR ("public.utf8-plain-text")) ||114 UTTypeConformsTo (flavorType, CFSTR ("public.utf16-plain-text")))115 { 116 Log 113 if (UTTypeConformsTo(flavorType, kUTTypeUTF8PlainText) || 114 UTTypeConformsTo(flavorType, kUTTypeUTF16PlainText)) 115 { 116 Log(("Unicode flavor detected.\n")); 117 117 *pfFormats |= VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT; 118 118 } 119 else if (UTTypeConformsTo 120 { 121 Log 119 else if (UTTypeConformsTo(flavorType, kUTTypeBMP)) 120 { 121 Log(("BMP flavor detected.\n")); 122 122 *pfFormats |= VBOX_SHARED_CLIPBOARD_FMT_BITMAP; 123 123 } 124 124 } 125 CFRelease 126 } 127 } 128 129 Log 125 CFRelease(flavorTypeArray); 126 } 127 } 128 129 Log(("queryNewPasteboardFormats: rc = %02X\n", rc)); 130 130 return rc; 131 131 } … … 143 143 * @returns IPRT status code. 144 144 */ 145 int readFromPasteboard 146 { 147 Log 145 int readFromPasteboard(PasteboardRef pPasteboard, uint32_t fFormat, void *pv, uint32_t cb, uint32_t *pcbActual) 146 { 147 Log(("readFromPasteboard: fFormat = %02X\n", fFormat)); 148 148 149 149 OSStatus err = noErr; 150 150 151 151 /* Make sure all is in sync */ 152 PasteboardSynchronize 152 PasteboardSynchronize(pPasteboard); 153 153 154 154 /* Are some items in the pasteboard? */ 155 155 ItemCount itemCount; 156 err = PasteboardGetItemCount 156 err = PasteboardGetItemCount(pPasteboard, &itemCount); 157 157 if (itemCount < 1) 158 158 return VINF_SUCCESS; … … 161 161 int rc = VERR_NOT_SUPPORTED; 162 162 PasteboardItemID itemID; 163 if (!(err = PasteboardGetItemIdentifier 163 if (!(err = PasteboardGetItemIdentifier(pPasteboard, 1, &itemID))) 164 164 { 165 165 /* The guest request unicode */ … … 169 169 PRTUTF16 pwszTmp = NULL; 170 170 /* Try utf-16 first */ 171 if (!(err = PasteboardCopyItemFlavorData (pPasteboard, itemID, CFSTR ("public.utf16-plain-text"), &outData)))172 { 173 Log 174 rc = RTUtf16DupEx (&pwszTmp, (PRTUTF16)CFDataGetBytePtr(outData), 0);171 if (!(err = PasteboardCopyItemFlavorData(pPasteboard, itemID, kUTTypeUTF16PlainText, &outData))) 172 { 173 Log(("Clipboard content is utf-16\n")); 174 rc = RTUtf16DupEx(&pwszTmp, (PRTUTF16)CFDataGetBytePtr(outData), 0); 175 175 } 176 176 /* Second try is utf-8 */ 177 177 else 178 if (!(err = PasteboardCopyItemFlavorData (pPasteboard, itemID, CFSTR ("public.utf8-plain-text"), &outData)))179 { 180 Log 181 rc = RTStrToUtf16 ((const char*)CFDataGetBytePtr(outData), &pwszTmp);178 if (!(err = PasteboardCopyItemFlavorData(pPasteboard, itemID, kUTTypeUTF8PlainText, &outData))) 179 { 180 Log(("readFromPasteboard: clipboard content is utf-8\n")); 181 rc = RTStrToUtf16((const char*)CFDataGetBytePtr(outData), &pwszTmp); 182 182 } 183 183 if (pwszTmp) 184 184 { 185 185 /* Check how much longer will the converted text will be. */ 186 size_t cwSrc = RTUtf16Len 186 size_t cwSrc = RTUtf16Len(pwszTmp); 187 187 size_t cwDest; 188 rc = vboxClipboardUtf16GetWinSize 189 if (RT_FAILURE 190 { 191 RTUtf16Free 192 Log 193 AssertRCReturn 188 rc = vboxClipboardUtf16GetWinSize(pwszTmp, cwSrc, &cwDest); 189 if (RT_FAILURE(rc)) 190 { 191 RTUtf16Free(pwszTmp); 192 Log(("readFromPasteboard: clipboard conversion failed. vboxClipboardUtf16GetWinSize returned %Rrc. Abandoning.\n", rc)); 193 AssertRCReturn(rc, rc); 194 194 } 195 195 /* Set the actually needed data size */ … … 200 200 if (*pcbActual <= cb) 201 201 { 202 rc = vboxClipboardUtf16LinToWin (pwszTmp, RTUtf16Len (pwszTmp), static_cast <PRTUTF16>(pv), cb / 2);203 if (RT_FAILURE 202 rc = vboxClipboardUtf16LinToWin(pwszTmp, RTUtf16Len(pwszTmp), static_cast <PRTUTF16>(pv), cb / 2); 203 if (RT_FAILURE(rc)) 204 204 { 205 RTUtf16Free 206 Log 207 AssertRCReturn 205 RTUtf16Free(pwszTmp); 206 Log(("readFromPasteboard: clipboard conversion failed. vboxClipboardUtf16LinToWin() returned %Rrc. Abandoning.\n", rc)); 207 AssertRCReturn(rc, rc); 208 208 } 209 209 #ifdef SHOW_CLIPBOARD_CONTENT 210 Log (("readFromPasteboard: clipboard content: %ls\n", static_cast <PRTUTF16>(pv)));210 Log(("readFromPasteboard: clipboard content: %ls\n", static_cast <PRTUTF16>(pv))); 211 211 #endif 212 212 } 213 213 /* Free the temp string */ 214 RTUtf16Free 214 RTUtf16Free(pwszTmp); 215 215 } 216 216 } … … 222 222 size_t cbTmpSize; 223 223 /* Get the data from the pasteboard */ 224 if (!(err = PasteboardCopyItemFlavorData 225 { 226 Log 227 pTmp = CFDataGetBytePtr 228 cbTmpSize = CFDataGetLength 224 if (!(err = PasteboardCopyItemFlavorData(pPasteboard, itemID, kUTTypeBMP, &outData))) 225 { 226 Log(("Clipboard content is BMP\n")); 227 pTmp = CFDataGetBytePtr(outData); 228 cbTmpSize = CFDataGetLength(outData); 229 229 } 230 230 if (pTmp) … … 232 232 const void *pDib; 233 233 size_t cbDibSize; 234 rc = vboxClipboardBmpGetDib 235 if (RT_FAILURE 234 rc = vboxClipboardBmpGetDib(pTmp, cbTmpSize, &pDib, &cbDibSize); 235 if (RT_FAILURE(rc)) 236 236 { 237 237 rc = VERR_NOT_SUPPORTED; 238 Log 239 AssertRCReturn 238 Log(("readFromPasteboard: unknown bitmap format. vboxClipboardBmpGetDib returned %Rrc. Abandoning.\n", rc)); 239 AssertRCReturn(rc, rc); 240 240 } 241 241 … … 246 246 if (*pcbActual <= cb) 247 247 { 248 memcpy 248 memcpy(pv, pDib, cbDibSize); 249 249 #ifdef SHOW_CLIPBOARD_CONTENT 250 Log 250 Log(("readFromPasteboard: clipboard content bitmap %d bytes\n", cbDibSize)); 251 251 #endif 252 252 } … … 255 255 } 256 256 257 Log 257 Log(("readFromPasteboard: rc = %02X\n", rc)); 258 258 return rc; 259 259 } … … 270 270 * @returns IPRT status code. 271 271 */ 272 int writeToPasteboard 273 { 274 Log 272 int writeToPasteboard(PasteboardRef pPasteboard, void *pv, uint32_t cb, uint32_t fFormat) 273 { 274 Log(("writeToPasteboard: fFormat = %02X\n", fFormat)); 275 275 276 276 /* Clear the pasteboard */ 277 if (PasteboardClear 277 if (PasteboardClear(pPasteboard)) 278 278 return VERR_NOT_SUPPORTED; 279 279 280 280 /* Make sure all is in sync */ 281 PasteboardSynchronize 281 PasteboardSynchronize(pPasteboard); 282 282 283 283 int rc = VERR_NOT_SUPPORTED; … … 285 285 if (fFormat & VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT) 286 286 { 287 PRTUTF16 pwszSrcText = static_cast <PRTUTF16> 287 PRTUTF16 pwszSrcText = static_cast <PRTUTF16>(pv); 288 288 size_t cwSrc = cb / 2; 289 289 size_t cwDest = 0; 290 290 /* How long will the converted text be? */ 291 rc = vboxClipboardUtf16GetLinSize 292 if (RT_FAILURE 293 { 294 Log 295 AssertRCReturn 291 rc = vboxClipboardUtf16GetLinSize(pwszSrcText, cwSrc, &cwDest); 292 if (RT_FAILURE(rc)) 293 { 294 Log(("writeToPasteboard: clipboard conversion failed. vboxClipboardUtf16GetLinSize returned %Rrc. Abandoning.\n", rc)); 295 AssertRCReturn(rc, rc); 296 296 } 297 297 /* Empty clipboard? Not critical */ 298 298 if (cwDest == 0) 299 299 { 300 Log 300 Log(("writeToPasteboard: received empty clipboard data from the guest, returning false.\n")); 301 301 return VINF_SUCCESS; 302 302 } 303 303 /* Allocate the necessary memory */ 304 PRTUTF16 pwszDestText = static_cast <PRTUTF16> (RTMemAlloc(cwDest * 2));304 PRTUTF16 pwszDestText = static_cast <PRTUTF16>(RTMemAlloc(cwDest * 2)); 305 305 if (pwszDestText == NULL) 306 306 { 307 Log 307 Log(("writeToPasteboard: failed to allocate %d bytes\n", cwDest * 2)); 308 308 return VERR_NO_MEMORY; 309 309 } 310 310 /* Convert the EOL */ 311 rc = vboxClipboardUtf16WinToLin 312 if (RT_FAILURE 313 { 314 Log 315 RTMemFree 316 AssertRCReturn 311 rc = vboxClipboardUtf16WinToLin(pwszSrcText, cwSrc, pwszDestText, cwDest); 312 if (RT_FAILURE(rc)) 313 { 314 Log(("writeToPasteboard: clipboard conversion failed. vboxClipboardUtf16WinToLin() returned %Rrc. Abandoning.\n", rc)); 315 RTMemFree(pwszDestText); 316 AssertRCReturn(rc, rc); 317 317 } 318 318 … … 321 321 PasteboardItemID itemId = (PasteboardItemID)1; 322 322 /* Create a CData object which we could pass to the pasteboard */ 323 if ((textData = CFDataCreate 324 reinterpret_cast<UInt8*>(pwszDestText), cwDest * 2)))323 if ((textData = CFDataCreate(kCFAllocatorDefault, 324 reinterpret_cast<UInt8*>(pwszDestText), cwDest * 2))) 325 325 { 326 326 /* Put the Utf-16 version to the pasteboard */ 327 PasteboardPutItemFlavor 328 CFSTR ("public.utf16-plain-text"),329 327 PasteboardPutItemFlavor(pPasteboard, itemId, 328 kUTTypeUTF16PlainText, 329 textData, 0); 330 330 } 331 331 /* Create a Utf-8 version */ 332 332 char *pszDestText; 333 rc = RTUtf16ToUtf8 334 if (RT_SUCCESS 333 rc = RTUtf16ToUtf8(pwszDestText, &pszDestText); 334 if (RT_SUCCESS(rc)) 335 335 { 336 336 /* Create a CData object which we could pass to the pasteboard */ 337 if ((textData = CFDataCreate 338 reinterpret_cast<UInt8*>(pszDestText), strlen(pszDestText))))337 if ((textData = CFDataCreate(kCFAllocatorDefault, 338 reinterpret_cast<UInt8*>(pszDestText), strlen(pszDestText)))) 339 339 { 340 340 /* Put the Utf-8 version to the pasteboard */ 341 PasteboardPutItemFlavor 342 CFSTR ("public.utf8-plain-text"),343 344 } 345 RTStrFree 346 } 347 348 RTMemFree 341 PasteboardPutItemFlavor(pPasteboard, itemId, 342 kUTTypeUTF8PlainText, 343 textData, 0); 344 } 345 RTStrFree(pszDestText); 346 } 347 348 RTMemFree(pwszDestText); 349 349 rc = VINF_SUCCESS; 350 350 } … … 359 359 PasteboardItemID itemId = (PasteboardItemID)1; 360 360 361 rc = vboxClipboardDibToBmp 362 if (RT_SUCCESS 361 rc = vboxClipboardDibToBmp(pv, cb, &pBmp, &cbBmpSize); 362 if (RT_SUCCESS(rc)) 363 363 { 364 364 /* Create a CData object which we could pass to the pasteboard */ 365 if ((bmpData = CFDataCreate 366 reinterpret_cast<UInt8*>(pBmp), cbBmpSize)))365 if ((bmpData = CFDataCreate(kCFAllocatorDefault, 366 reinterpret_cast<UInt8*>(pBmp), cbBmpSize))) 367 367 { 368 368 /* Put the Utf-8 version to the pasteboard */ 369 PasteboardPutItemFlavor 370 371 372 } 373 RTMemFree 369 PasteboardPutItemFlavor(pPasteboard, itemId, 370 kUTTypeBMP, 371 bmpData, 0); 372 } 373 RTMemFree(pBmp); 374 374 } 375 375 rc = VINF_SUCCESS; … … 378 378 rc = VERR_NOT_IMPLEMENTED; 379 379 380 Log 380 Log(("writeToPasteboard: rc = %02X\n", rc)); 381 381 return rc; 382 382 }
Note:
See TracChangeset
for help on using the changeset viewer.