darwin-pasteboard.cpp revision dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2e
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync * Shared Clipboard:
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync * Mac OS X host implementation.
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync * Copyright (C) 2008 innotek GmbH
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync * available from http://www.virtualbox.org. This file is free software;
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync * you can redistribute it and/or modify it under the terms of the GNU
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync * General Public License (GPL) as published by the Free Software
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync// @todo: same as defined in VBoxClipboardSvc.h
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync//#define SHOW_CLIPBOARD_CONTENT
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync if (PasteboardCreate (kPasteboardClipboard, &pPasteboard))
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsyncint queryPasteboardFormats (PasteboardRef pPasteboard, uint32_t &u32Formats)
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync /* Make sure all is in sync */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync /* If nothing changed return */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync /* Are some items in the pasteboard? */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync err = PasteboardGetItemCount (pPasteboard, &itemCount);
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync /* The id of the first element in the pastboard */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync if (!(err = PasteboardGetItemIdentifier (pPasteboard, 1, &itemID)))
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync /* Retrieve all flavors in the pasteboard, maybe there
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync * is something we can use. */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync if (!(err = PasteboardCopyItemFlavors (pPasteboard, itemID, &flavorTypeArray)))
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync for (CFIndex flavorIndex = 0; flavorIndex < flavorCount; flavorIndex++)
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync flavorType = static_cast <CFStringRef> (CFArrayGetValueAtIndex (flavorTypeArray,
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync /* Currently only unicode supported */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync if (UTTypeConformsTo (flavorType, CFSTR ("public.utf8-plain-text")) ||
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync UTTypeConformsTo (flavorType, CFSTR ("public.utf16-plain-text")))
8867771015571c5542d39e393d7fe6304421a928vboxsync u32Formats |= VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT;
8867771015571c5542d39e393d7fe6304421a928vboxsyncint readFromPasteboard (PasteboardRef pPasteboard, uint32_t u32Format, void *pv, uint32_t cb, uint32_t *pcbActual)
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync Log (("readFromPastboard: u32Format = %02X\n", u32Format));
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync /* Make sure all is in sync */
8867771015571c5542d39e393d7fe6304421a928vboxsync /* Are some items in the pasteboard? */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync err = PasteboardGetItemCount (pPasteboard, &itemCount);
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync /* The id of the first element in the pastboard */
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync if (!(err = PasteboardGetItemIdentifier (pPasteboard, 1, &itemID)))
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync /* The guest request unicode */
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync if (u32Format & VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT)
8867771015571c5542d39e393d7fe6304421a928vboxsync /* Utf-16 is currently broken on more than one line.
8867771015571c5542d39e393d7fe6304421a928vboxsync * Has to be investigated. */
8867771015571c5542d39e393d7fe6304421a928vboxsync /* Try utf-16 first */
8867771015571c5542d39e393d7fe6304421a928vboxsync if (!(err = PasteboardCopyItemFlavorData (pPasteboard, itemID, CFSTR ("public.utf16-plain-text"), &outData)))
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync rc = RTUtf16DupEx (&pu16Tmp, (RTUTF16*)CFDataGetBytePtr (outData), 0);
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync /* Second try is utf-8 */
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync if (!(err = PasteboardCopyItemFlavorData (pPasteboard, itemID, CFSTR ("public.utf8-plain-text"), &outData)))
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync Log (("readFromPastboard: clipboard content is utf-8\n"));
8867771015571c5542d39e393d7fe6304421a928vboxsync rc = RTStrToUtf16 ((const char*)CFDataGetBytePtr (outData), &pu16Tmp);
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync /* Check how much longer will the converted text will be. */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync rc = vboxClipboardUtf16GetWinSize (pu16Tmp, cwSrcLen, &cwDestLen);
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync Log (("readFromPastboard: clipboard conversion failed. vboxClipboardUtf16GetWinSize returned %Vrc. Abandoning.\n", rc));
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync /* Set the actually needed data size */
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync /* Return success state */
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync /* Do not copy data if the dst buffer is not big enough. */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync rc = vboxClipboardUtf16LinToWin (pu16Tmp, RTUtf16Len (pu16Tmp), static_cast <PRTUTF16> (pv), cb / 2);
8867771015571c5542d39e393d7fe6304421a928vboxsync Log (("readFromPastboard: clipboard conversion failed. vboxClipboardUtf16LinToWin() returned %Vrc. Abandoning.\n", rc));
8867771015571c5542d39e393d7fe6304421a928vboxsync Log (("readFromPastboard: clipboard content: %ls\n", static_cast <PRTUTF16> (pv)));
8867771015571c5542d39e393d7fe6304421a928vboxsync /* Free the temp string */
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsyncint writeToPasteboard (PasteboardRef pPasteboard, void *pv, uint32_t cb, uint32_t u32Format)
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync Log (("writeToPasteboard: u32Format = %02X\n", u32Format));
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync /* Clear the pastboard */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync /* Make sure all is in sync */
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync /* Handle the unicode text */
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync if (u32Format & VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT)
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync PRTUTF16 pu16SrcText = static_cast <PRTUTF16> (pv);
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync /* How long will the converted text be? */
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync rc = vboxClipboardUtf16GetLinSize (pu16SrcText, cwSrcLen, &cwDestLen);
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync Log (("writeToPasteboard: clipboard conversion failed. vboxClipboardUtf16GetLinSize returned %Vrc. Abandoning.\n", rc));
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync /* Empty clipboard? Not critical */
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync Log (("writeToPasteboard: received empty clipboard data from the guest, returning false.\n"));
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync /* Allocate the necessary memory */
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync PRTUTF16 pu16DestText = static_cast <PRTUTF16> (RTMemAlloc (cwDestLen * 2));
8867771015571c5542d39e393d7fe6304421a928vboxsync Log (("writeToPasteboard: failed to allocate %d bytes\n", cwDestLen * 2));
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync /* Convert the EOL */
8867771015571c5542d39e393d7fe6304421a928vboxsync rc = vboxClipboardUtf16WinToLin (pu16SrcText, cwSrcLen, pu16DestText, cwDestLen);
8867771015571c5542d39e393d7fe6304421a928vboxsync Log (("writeToPasteboard: clipboard conversion failed. vboxClipboardUtf16WinToLin() returned %Vrc. Abandoning.\n", rc));
8867771015571c5542d39e393d7fe6304421a928vboxsync /* Item id is 1. Nothing special here. */
8867771015571c5542d39e393d7fe6304421a928vboxsync /* Create a CData object which we could pass to the pasteboard */
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync reinterpret_cast<UInt8*> (pu16DestText), cwDestLen * 2)))
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync /* Put the Utf-16 version to the pasteboard */
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync /* Create a Utf-8 version */
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync /* Create a CData object which we could pass to the pasteboard */
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync reinterpret_cast<UInt8*> (pu8DestText), RTUtf16CalcUtf8Len (pu16DestText))))
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync /* Put the Utf-8 version to the pasteboard */