darwin-pasteboard.cpp revision dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2e
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync/** @file
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync *
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync * Shared Clipboard:
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync * Mac OS X host implementation.
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync */
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync/*
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync * Copyright (C) 2008 innotek GmbH
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync *
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 */
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync// @todo: same as defined in VBoxClipboardSvc.h
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync#define VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT 0x01
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync#define VBOX_SHARED_CLIPBOARD_FMT_BITMAP 0x02
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync#define VBOX_SHARED_CLIPBOARD_FMT_HTML 0x04
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync#include <Carbon/Carbon.h>
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync#include <iprt/mem.h>
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync#include <iprt/assert.h>
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync#include "iprt/err.h"
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync#define LOG_GROUP LOG_GROUP_HGCM
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync#include "VBox/log.h"
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync#include "clipboard-helper.h"
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync//#define SHOW_CLIPBOARD_CONTENT
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsyncint initPasteboard (PasteboardRef &pPasteboard)
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync{
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync int rc = VINF_SUCCESS;
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync if (PasteboardCreate (kPasteboardClipboard, &pPasteboard))
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync rc = VERR_NOT_SUPPORTED;
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync return rc;
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync}
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsyncvoid destroyPasteboard (PasteboardRef &pPasteboard)
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync{
17ef1920962b3df57bf6d2704ced1586396d96f0vboxsync CFRelease (pPasteboard);
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync pPasteboard = NULL;
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync}
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsyncint queryPasteboardFormats (PasteboardRef pPasteboard, uint32_t &u32Formats)
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync{
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync Log (("queryPasteboardFormats\n"));
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync OSStatus err = noErr;
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync PasteboardSyncFlags syncFlags;
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync /* Make sure all is in sync */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync syncFlags = PasteboardSynchronize (pPasteboard);
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync /* If nothing changed return */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync if (!(syncFlags & kPasteboardModified))
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync return VINF_SUCCESS;
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync /* Are some items in the pasteboard? */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync ItemCount itemCount;
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync err = PasteboardGetItemCount (pPasteboard, &itemCount);
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync if (itemCount < 1)
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync return VINF_SUCCESS;
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync /* The id of the first element in the pastboard */
8867771015571c5542d39e393d7fe6304421a928vboxsync int rc = VERR_NOT_SUPPORTED;
8867771015571c5542d39e393d7fe6304421a928vboxsync PasteboardItemID itemID;
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync if (!(err = PasteboardGetItemIdentifier (pPasteboard, 1, &itemID)))
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync {
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync /* Retrieve all flavors in the pasteboard, maybe there
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync * is something we can use. */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync CFArrayRef flavorTypeArray;
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync if (!(err = PasteboardCopyItemFlavors (pPasteboard, itemID, &flavorTypeArray)))
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync {
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync CFIndex flavorCount;
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync flavorCount = CFArrayGetCount (flavorTypeArray);
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync for (CFIndex flavorIndex = 0; flavorIndex < flavorCount; flavorIndex++)
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync {
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync CFStringRef flavorType;
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync flavorType = static_cast <CFStringRef> (CFArrayGetValueAtIndex (flavorTypeArray,
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync flavorIndex));
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync /* Currently only unicode supported */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync if (UTTypeConformsTo (flavorType, CFSTR ("public.utf8-plain-text")) ||
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync UTTypeConformsTo (flavorType, CFSTR ("public.utf16-plain-text")))
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync {
8867771015571c5542d39e393d7fe6304421a928vboxsync Log (("Unicode flavor detected.\n"));
8867771015571c5542d39e393d7fe6304421a928vboxsync u32Formats |= VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT;
8867771015571c5542d39e393d7fe6304421a928vboxsync }
8867771015571c5542d39e393d7fe6304421a928vboxsync }
8867771015571c5542d39e393d7fe6304421a928vboxsync rc = VINF_SUCCESS;
8867771015571c5542d39e393d7fe6304421a928vboxsync CFRelease (flavorTypeArray);
8867771015571c5542d39e393d7fe6304421a928vboxsync }
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync }
8867771015571c5542d39e393d7fe6304421a928vboxsync
8867771015571c5542d39e393d7fe6304421a928vboxsync Log (("queryPasteboardFormats: rc = %02X\n", rc));
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync return rc;
8867771015571c5542d39e393d7fe6304421a928vboxsync}
8867771015571c5542d39e393d7fe6304421a928vboxsync
8867771015571c5542d39e393d7fe6304421a928vboxsyncint readFromPasteboard (PasteboardRef pPasteboard, uint32_t u32Format, void *pv, uint32_t cb, uint32_t *pcbActual)
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync{
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync Log (("readFromPastboard: u32Format = %02X\n", u32Format));
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync OSStatus err = noErr;
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync /* Make sure all is in sync */
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync PasteboardSynchronize (pPasteboard);
8867771015571c5542d39e393d7fe6304421a928vboxsync
8867771015571c5542d39e393d7fe6304421a928vboxsync /* Are some items in the pasteboard? */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync ItemCount itemCount;
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync err = PasteboardGetItemCount (pPasteboard, &itemCount);
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync if (itemCount < 1)
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync return VINF_SUCCESS;
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync /* The id of the first element in the pastboard */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync int rc = VERR_NOT_SUPPORTED;
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync PasteboardItemID itemID;
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync if (!(err = PasteboardGetItemIdentifier (pPasteboard, 1, &itemID)))
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync {
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync /* The guest request unicode */
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync if (u32Format & VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT)
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync {
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync CFDataRef outData;
8867771015571c5542d39e393d7fe6304421a928vboxsync PRTUTF16 pu16Tmp = NULL;
8867771015571c5542d39e393d7fe6304421a928vboxsync /* Utf-16 is currently broken on more than one line.
8867771015571c5542d39e393d7fe6304421a928vboxsync * Has to be investigated. */
8867771015571c5542d39e393d7fe6304421a928vboxsync#if 0
8867771015571c5542d39e393d7fe6304421a928vboxsync /* Try utf-16 first */
8867771015571c5542d39e393d7fe6304421a928vboxsync if (!(err = PasteboardCopyItemFlavorData (pPasteboard, itemID, CFSTR ("public.utf16-plain-text"), &outData)))
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync {
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync Log (("Clipboard content is utf-16\n"));
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync rc = RTUtf16DupEx (&pu16Tmp, (RTUTF16*)CFDataGetBytePtr (outData), 0);
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync }
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync /* Second try is utf-8 */
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync else
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync#endif
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync if (!(err = PasteboardCopyItemFlavorData (pPasteboard, itemID, CFSTR ("public.utf8-plain-text"), &outData)))
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync {
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync Log (("readFromPastboard: clipboard content is utf-8\n"));
8867771015571c5542d39e393d7fe6304421a928vboxsync rc = RTStrToUtf16 ((const char*)CFDataGetBytePtr (outData), &pu16Tmp);
8867771015571c5542d39e393d7fe6304421a928vboxsync }
8867771015571c5542d39e393d7fe6304421a928vboxsync if (pu16Tmp)
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync {
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync /* Check how much longer will the converted text will be. */
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync size_t cwSrcLen = RTUtf16Len (pu16Tmp);
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync size_t cwDestLen;
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync rc = vboxClipboardUtf16GetWinSize (pu16Tmp, cwSrcLen, &cwDestLen);
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync if (RT_FAILURE (rc))
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync {
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync RTUtf16Free (pu16Tmp);
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync Log (("readFromPastboard: clipboard conversion failed. vboxClipboardUtf16GetWinSize returned %Vrc. Abandoning.\n", rc));
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync AssertRCReturn (rc, rc);
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync }
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync /* Set the actually needed data size */
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync *pcbActual = cwDestLen * 2;
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync /* Return success state */
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync rc = VINF_SUCCESS;
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync /* Do not copy data if the dst buffer is not big enough. */
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync if (*pcbActual <= cb)
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync {
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync rc = vboxClipboardUtf16LinToWin (pu16Tmp, RTUtf16Len (pu16Tmp), static_cast <PRTUTF16> (pv), cb / 2);
8867771015571c5542d39e393d7fe6304421a928vboxsync if (RT_FAILURE (rc))
8867771015571c5542d39e393d7fe6304421a928vboxsync {
8867771015571c5542d39e393d7fe6304421a928vboxsync RTUtf16Free (pu16Tmp);
8867771015571c5542d39e393d7fe6304421a928vboxsync Log (("readFromPastboard: clipboard conversion failed. vboxClipboardUtf16LinToWin() returned %Vrc. Abandoning.\n", rc));
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync AssertRCReturn (rc, rc);
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync }
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync#ifdef SHOW_CLIPBOARD_CONTENT
8867771015571c5542d39e393d7fe6304421a928vboxsync Log (("readFromPastboard: clipboard content: %ls\n", static_cast <PRTUTF16> (pv)));
8867771015571c5542d39e393d7fe6304421a928vboxsync#endif
8867771015571c5542d39e393d7fe6304421a928vboxsync }
8867771015571c5542d39e393d7fe6304421a928vboxsync /* Free the temp string */
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync RTUtf16Free (pu16Tmp);
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync }
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync }
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync }
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync Log (("readFromPastboard: rc = %02X\n", rc));
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync return rc;
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync}
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsyncint writeToPasteboard (PasteboardRef pPasteboard, void *pv, uint32_t cb, uint32_t u32Format)
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync{
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync Log (("writeToPasteboard: u32Format = %02X\n", u32Format));
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync /* Clear the pastboard */
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync if (PasteboardClear (pPasteboard))
9de47c4ec7b0fc9a384e4b815153de399da7b8devboxsync return VERR_NOT_SUPPORTED;
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync
fe14fe6d46ce87a9b25cbdacb3a20b1f87bf34c7vboxsync /* Make sure all is in sync */
8867771015571c5542d39e393d7fe6304421a928vboxsync PasteboardSynchronize (pPasteboard);
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync int rc = VERR_NOT_SUPPORTED;
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync /* Handle the unicode text */
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync if (u32Format & VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT)
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync {
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync PRTUTF16 pu16SrcText = static_cast <PRTUTF16> (pv);
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync size_t cwSrcLen = cb / 2;
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync size_t cwDestLen = 0;
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync /* How long will the converted text be? */
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync rc = vboxClipboardUtf16GetLinSize (pu16SrcText, cwSrcLen, &cwDestLen);
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync if (RT_FAILURE (rc))
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync {
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync Log (("writeToPasteboard: clipboard conversion failed. vboxClipboardUtf16GetLinSize returned %Vrc. Abandoning.\n", rc));
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync AssertRCReturn (rc, rc);
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync }
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync /* Empty clipboard? Not critical */
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync if (cwDestLen == 0)
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync {
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync Log (("writeToPasteboard: received empty clipboard data from the guest, returning false.\n"));
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync return VINF_SUCCESS;
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync }
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync /* Allocate the necessary memory */
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync PRTUTF16 pu16DestText = static_cast <PRTUTF16> (RTMemAlloc (cwDestLen * 2));
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync if (pu16DestText == 0)
8867771015571c5542d39e393d7fe6304421a928vboxsync {
8867771015571c5542d39e393d7fe6304421a928vboxsync Log (("writeToPasteboard: failed to allocate %d bytes\n", cwDestLen * 2));
8867771015571c5542d39e393d7fe6304421a928vboxsync return VERR_NO_MEMORY;
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync }
c1b4529a269091edd0274bd98d35b75663fd66c0vboxsync /* Convert the EOL */
8867771015571c5542d39e393d7fe6304421a928vboxsync rc = vboxClipboardUtf16WinToLin (pu16SrcText, cwSrcLen, pu16DestText, cwDestLen);
8867771015571c5542d39e393d7fe6304421a928vboxsync if (RT_FAILURE (rc))
8867771015571c5542d39e393d7fe6304421a928vboxsync {
8867771015571c5542d39e393d7fe6304421a928vboxsync Log (("writeToPasteboard: clipboard conversion failed. vboxClipboardUtf16WinToLin() returned %Vrc. Abandoning.\n", rc));
8867771015571c5542d39e393d7fe6304421a928vboxsync RTMemFree (pu16DestText);
8867771015571c5542d39e393d7fe6304421a928vboxsync AssertRCReturn (rc, rc);
8867771015571c5542d39e393d7fe6304421a928vboxsync }
8867771015571c5542d39e393d7fe6304421a928vboxsync
8867771015571c5542d39e393d7fe6304421a928vboxsync CFDataRef textData = NULL;
8867771015571c5542d39e393d7fe6304421a928vboxsync /* Item id is 1. Nothing special here. */
8867771015571c5542d39e393d7fe6304421a928vboxsync PasteboardItemID itemId = (PasteboardItemID)1;
8867771015571c5542d39e393d7fe6304421a928vboxsync /* Create a CData object which we could pass to the pasteboard */
8867771015571c5542d39e393d7fe6304421a928vboxsync if ((textData = CFDataCreate (kCFAllocatorDefault,
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync reinterpret_cast<UInt8*> (pu16DestText), cwDestLen * 2)))
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync {
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync /* Put the Utf-16 version to the pasteboard */
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync PasteboardPutItemFlavor (pPasteboard, itemId,
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync CFSTR ("public.utf16-plain-text"),
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync textData, 0);
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync }
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync /* Create a Utf-8 version */
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync char *pu8DestText;
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync rc = RTUtf16ToUtf8 (pu16DestText, &pu8DestText);
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync if (RT_SUCCESS (rc))
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync {
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync /* Create a CData object which we could pass to the pasteboard */
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync if ((textData = CFDataCreate (kCFAllocatorDefault,
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync reinterpret_cast<UInt8*> (pu8DestText), RTUtf16CalcUtf8Len (pu16DestText))))
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync {
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync /* Put the Utf-8 version to the pasteboard */
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync PasteboardPutItemFlavor (pPasteboard, itemId,
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync CFSTR ("public.utf8-plain-text"),
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync textData, 0);
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync }
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync RTStrFree (pu8DestText);
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync }
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync RTMemFree (pu16DestText);
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync rc = VINF_SUCCESS;
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync }
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync else
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync rc = VERR_NOT_IMPLEMENTED;
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync Log (("writeToPasteboard: rc = %02X\n", rc));
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync return rc;
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync}
4c464a0800535c29782d7ab5bcd7820bc7115cd7vboxsync