VirtualBox

Changeset 83628 in vbox


Ignore:
Timestamp:
Apr 8, 2020 6:49:21 PM (5 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
137067
Message:

SharedClipboard: Trying to make the darwin approach work better. The problems reported in bugref:9620 is kind of addressed here, but there is more stuff to clean up here. Lots more.

Location:
trunk/src/VBox/HostServices/SharedClipboard
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/HostServices/SharedClipboard/VBoxSharedClipboardSvc-darwin.cpp

    r83624 r83628  
    2525#include <iprt/assert.h>
    2626#include <iprt/asm.h>
     27#include <iprt/process.h>
     28#include <iprt/rand.h>
     29#include <iprt/string.h>
    2730#include <iprt/thread.h>
    2831
     
    4548    /** Shared clipboard client. */
    4649    PSHCLCLIENT             pClient;
     50    /** Random 64-bit number embedded into szGuestOwnershipFlavor. */
     51    uint64_t                idGuestOwnership;
     52    /** The guest ownership flavor (type) string. */
     53    char                    szGuestOwnershipFlavor[64];
    4754} SHCLCONTEXT;
    4855
     
    201208#endif
    202209
     210    SHCLCONTEXT *pCtx = pClient->State.pCtx;
     211    ShClSvcLock();
     212
     213    /*
     214     * Generate a unique flavor string for this format announcement.
     215     */
     216    uint64_t idFlavor = RTRandU64();
     217    pCtx->idGuestOwnership = idFlavor;
     218    RTStrPrintf(pCtx->szGuestOwnershipFlavor, sizeof(pCtx->szGuestOwnershipFlavor),
     219                "org.virtualbox.sharedclipboard.%RTproc.%RX64", RTProcSelf(), idFlavor);
     220
     221    /*
     222     * Empty the pasteboard and put our ownership indicator flavor there
     223     * with the stringified formats as value.
     224     */
     225    char szValue[32];
     226    RTStrPrintf(szValue, sizeof(szValue), "%#x", fFormats);
     227
     228    takePasteboardOwnership(pCtx->hPasteboard, pCtx->idGuestOwnership, pCtx->szGuestOwnershipFlavor, szValue);
     229
     230    ShClSvcUnlock();
     231
     232    /*
     233     * Now, request the data from the guest.
     234     */
    203235    return ShClSvcDataReadRequest(pClient, fFormats, NULL /* pidEvent */);
    204236}
     
    227259    ShClSvcLock();
    228260
    229     writeToPasteboard(pClient->State.pCtx->hPasteboard, pvData, cbData, fFormat);
     261    writeToPasteboard(pClient->State.pCtx->hPasteboard, pClient->State.pCtx->idGuestOwnership, pvData, cbData, fFormat);
    230262
    231263    ShClSvcUnlock();
  • trunk/src/VBox/HostServices/SharedClipboard/darwin-pasteboard.cpp

    r83621 r83628  
    284284
    285285/**
     286 * Takes the ownership of the pasteboard.
     287 *
     288 * This is called when the other end reports available formats.
     289 *
     290 * @returns VBox status code.
     291 * @param   hPasteboard         The pastboard handle (reference).
     292 * @param   idOwnership         The ownership ID to use now.
     293 * @param   pszOwnershipFlavor  The ownership indicator flavor
     294 * @param   pszOwnershipValue   The ownership value (stringified format mask).
     295 *
     296 * @todo    Add fFormats so we can make promises about available formats at once
     297 *          without needing to request any data first.  That might help on
     298 *          flavor priority.
     299 */
     300int takePasteboardOwnership(PasteboardRef hPasteboard, uint64_t idOwnership,
     301                            const char *pszOwnershipFlavor, const char *pszOwnershipValue)
     302{
     303    /*
     304     * Clear the pasteboard and take ownership over it.
     305     */
     306    OSStatus orc = PasteboardClear(hPasteboard);
     307    if (orc == 0)
     308    {
     309        /* For good measure. */
     310        PasteboardSynchronize(hPasteboard);
     311
     312        /*
     313         * Put the ownership flavor and value onto the clipboard.
     314         */
     315        CFDataRef hData = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)pszOwnershipValue, strlen(pszOwnershipValue));
     316        if (hData)
     317        {
     318            CFStringRef hFlavor = CFStringCreateWithCString(kCFAllocatorDefault, pszOwnershipFlavor, kCFStringEncodingUTF8);
     319            if (hFlavor)
     320            {
     321                orc = PasteboardPutItemFlavor(hPasteboard, (PasteboardItemID)idOwnership,
     322                                              hFlavor, hData, kPasteboardFlavorNoFlags);
     323                if (orc == 0)
     324                    Log(("takePasteboardOwnership: idOwnership=%RX64 flavor=%s value=%s\n",
     325                         idOwnership, pszOwnershipFlavor, pszOwnershipValue));
     326                else
     327                    Log(("takePasteboardOwnership: PasteboardPutItemFlavor -> %d (%#x)!\n", orc, orc));
     328                CFRelease(hFlavor);
     329            }
     330            else
     331                Log(("takePasteboardOwnership: CFStringCreateWithCString failed!\n"));
     332            CFRelease(hData);
     333        }
     334        else
     335            Log(("takePasteboardOwnership: CFDataCreate failed!\n"));
     336    }
     337    else
     338        Log(("takePasteboardOwnership: PasteboardClear failed -> %d (%#x)\n", orc, orc));
     339    return orc == 0 ? VINF_SUCCESS : VERR_GENERAL_FAILURE;
     340}
     341
     342/**
    286343 * Write clipboard content to the host clipboard from the internal clipboard
    287344 * structure.
    288345 *
    289346 * @param   pPasteboard    Reference to the global pasteboard.
     347 * @param   idOwnership    The ownership ID.
    290348 * @param   pv             The source buffer.
    291349 * @param   cb             The size of the source buffer.
     
    294352 * @returns IPRT status code.
    295353 */
    296 int writeToPasteboard(PasteboardRef pPasteboard, void *pv, uint32_t cb, uint32_t fFormat)
     354int writeToPasteboard(PasteboardRef pPasteboard, uint64_t idOwnership, void *pv, uint32_t cb, uint32_t fFormat)
    297355{
    298356    Log(("writeToPasteboard: fFormat = %02X\n", fFormat));
    299 
    300     /* Clear the pasteboard */
    301     if (PasteboardClear(pPasteboard))
    302         return VERR_NOT_SUPPORTED;
    303357
    304358    /* Make sure all is in sync */
     
    342396
    343397        CFDataRef textData = NULL;
    344         /* Item id is 1. Nothing special here. */
    345         PasteboardItemID itemId = (PasteboardItemID)1;
    346398        /* Create a CData object which we could pass to the pasteboard */
    347399        if ((textData = CFDataCreate(kCFAllocatorDefault,
     
    349401        {
    350402            /* Put the Utf-16 version to the pasteboard */
    351             PasteboardPutItemFlavor(pPasteboard, itemId,
    352                                     kUTTypeUTF16PlainText,
    353                                     textData, 0);
     403            PasteboardPutItemFlavor(pPasteboard, (PasteboardItemID)idOwnership, kUTTypeUTF16PlainText, textData, 0);
    354404        }
    355405        /* Create a Utf-8 version */
     
    363413            {
    364414                /* Put the Utf-8 version to the pasteboard */
    365                 PasteboardPutItemFlavor(pPasteboard, itemId,
    366                                         kUTTypeUTF8PlainText,
    367                                         textData, 0);
     415                PasteboardPutItemFlavor(pPasteboard, (PasteboardItemID)idOwnership, kUTTypeUTF8PlainText, textData, 0);
    368416            }
    369417            RTStrFree(pszDestText);
     
    380428        size_t cbBmpSize;
    381429        CFDataRef bmpData = NULL;
    382         /* Item id is 1. Nothing special here. */
    383         PasteboardItemID itemId = (PasteboardItemID)1;
    384430
    385431        rc = ShClDibToBmp(pv, cb, &pBmp, &cbBmpSize);
     
    391437            {
    392438                /* Put the Utf-8 version to the pasteboard */
    393                 PasteboardPutItemFlavor(pPasteboard, itemId,
    394                                         kUTTypeBMP,
    395                                         bmpData, 0);
     439                PasteboardPutItemFlavor(pPasteboard, (PasteboardItemID)idOwnership, kUTTypeBMP, bmpData, 0);
    396440            }
    397441            RTMemFree(pBmp);
  • trunk/src/VBox/HostServices/SharedClipboard/darwin-pasteboard.h

    r82968 r83628  
    2929int queryNewPasteboardFormats(PasteboardRef pPasteboard, uint32_t *pfFormats, bool *pfChanged);
    3030int readFromPasteboard(PasteboardRef pPasteboard, uint32_t fFormat, void *pv, uint32_t cb, uint32_t *pcbActual);
    31 int writeToPasteboard(PasteboardRef pPasteboard, void *pv, uint32_t cb, uint32_t fFormat);
     31int takePasteboardOwnership(PasteboardRef pPasteboard, uint64_t idOwnership,
     32                            const char *pszOwnershipFlavor, const char *pszOwnershipValue);
     33int writeToPasteboard(PasteboardRef pPasteboard, uint64_t idOwnership, void *pv, uint32_t cb, uint32_t fFormat);
    3234
    3335#endif /* !VBOX_INCLUDED_SRC_SharedClipboard_darwin_pasteboard_h */
Note: See TracChangeset for help on using the changeset viewer.

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