VirtualBox

Changeset 18852 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Apr 8, 2009 7:19:05 PM (16 years ago)
Author:
vboxsync
Message:

GuestHost/SharedClipboard: removed Utf16 support for X11 and split a function

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/GuestHost/SharedClipboard/x11-clipboard.cpp

    r18756 r18852  
    4747#include <VBox/HostServices/VBoxClipboardSvc.h>
    4848
    49 /** Do we want to test Utf16 by disabling other text formats? */
    50 static bool g_testUtf16 = false;
    5149/** Do we want to test Utf8 by disabling other text formats? */
    5250static bool g_testUtf8 = false;
     
    6260    TARGETS,
    6361    CTEXT,
    64     UTF8,
    65     UTF16
     62    UTF8
    6663};
    6764
     
    9794    /** X11 atom refering to the clipboard timestamp target: TIMESTAMP */
    9895    Atom atomTimestamp;
    99     /** X11 atom refering to the clipboard utf16 text format: text/plain;charset=ISO-10646-UCS-2 */
    100     Atom atomUtf16;
    10196    /** X11 atom refering to the clipboard utf8 text format: UTF8_STRING */
    10297    Atom atomUtf8;
     
    142137static bool g_fHaveX11;
    143138
    144 /**
    145  * Convert the UTF-16 text obtained from the X11 clipboard to UTF-16LE with
    146  * Windows EOLs, place it in the buffer supplied and signal that data has
    147  * arrived.
    148  *
    149  * @param pValue      Source UTF-16 text
    150  * @param cwSourceLen Length in 16-bit words of the source text
    151  * @param pv          Where to store the converted data
    152  * @param cb          Length in bytes of the buffer pointed to by cb
    153  * @param pcbActual   Where to store the size of the converted data
    154  * @param pClient     Pointer to the client context structure
    155  * @note  X11 backend code, called from the Xt callback when we wish to read
    156  *        the X11 clipboard.
    157  */
    158 static void vboxClipboardGetUtf16(VBOXCLIPBOARDCONTEXTX11 *pCtx,
    159                                   XtPointer pValue, unsigned cwSrcLen,
    160                                   void *pv, unsigned cb,
    161                                   uint32_t *pcbActual)
     139static int vboxClipboardWriteUtf16LE(VBOXCLIPBOARDCONTEXTX11 *pCtx,
     140                                     PRTUTF16 pu16SrcText,
     141                                     size_t cwSrcLen,
     142                                     void *pv, unsigned cb,
     143                                     uint32_t *pcbActual)
    162144{
    163145    size_t cwDestLen;
    164     PRTUTF16 pu16SrcText = reinterpret_cast<PRTUTF16>(pValue);
    165146    PRTUTF16 pu16DestText = reinterpret_cast<PRTUTF16>(pv);
    166 
    167     LogFlowFunc (("converting Utf-16 to Utf-16LE.  cwSrcLen=%d, cb=%d, pu16SrcText+1=%.*ls\n",
    168                    cwSrcLen, cb, cwSrcLen - 1, pu16SrcText + 1));
    169     *pcbActual = 0;  /* Only set this to the right value on success. */
    170     /* How long will the converted text be? */
    171     int rc = vboxClipboardUtf16GetWinSize(pu16SrcText, cwSrcLen, &cwDestLen);
     147    int rc = VINF_SUCCESS;
     148    /* Check how much longer will the converted text will be. */
     149    rc = vboxClipboardUtf16GetWinSize(pu16SrcText, cwSrcLen, &cwDestLen);
    172150    if (RT_SUCCESS(rc) && (cb < cwDestLen * 2))
    173151    {
     
    186164        *pcbActual = cwDestLen * 2;
    187165    }
    188     /* We need to do this whether we succeed or fail. */
    189     XtFree(reinterpret_cast<char *>(pValue));
    190     RTSemEventSignal(pCtx->waitForData);
    191     LogFlowFunc(("Returning.  Status is %Rrc\n", rc));
     166    return rc;
    192167}
    193168
     
    211186                                        uint32_t *pcbActual)
    212187{
    213     size_t cwSrcLen, cwDestLen;
     188    size_t cwSrcLen;
    214189    char *pu8SrcText = reinterpret_cast<char *>(pValue);
    215190    PRTUTF16 pu16SrcText = NULL;
    216     PRTUTF16 pu16DestText = reinterpret_cast<PRTUTF16>(pv);
    217191
    218192    LogFlowFunc (("converting Utf-8 to Utf-16LE.  cbSrcLen=%d, cb=%d, pu8SrcText=%.*s\n",
     
    221195    /* First convert the UTF8 to UTF16 */
    222196    int rc = RTStrToUtf16Ex(pu8SrcText, cbSrcLen, &pu16SrcText, 0, &cwSrcLen);
    223     /* Check how much longer will the converted text will be. */
    224197    if (RT_SUCCESS(rc))
    225         rc = vboxClipboardUtf16GetWinSize(pu16SrcText, cwSrcLen, &cwDestLen);
    226     if (RT_SUCCESS(rc) && (cb < cwDestLen * 2))
    227     {
    228         /* Not enough buffer space provided - report the amount needed. */
    229         LogFlowFunc (("guest buffer too small: size %d bytes, needed %d.  Returning.\n",
    230                        cb, cwDestLen * 2));
    231         *pcbActual = cwDestLen * 2;
    232         rc = VERR_BUFFER_OVERFLOW;
    233     }
    234     /* Convert the text. */
    235     if (RT_SUCCESS(rc))
    236         rc = vboxClipboardUtf16LinToWin(pu16SrcText, cwSrcLen, pu16DestText, cb / 2);
    237     if (RT_SUCCESS(rc))
    238     {
    239         LogFlowFunc (("converted string is %.*ls.\n", cwDestLen, pu16DestText));
    240         *pcbActual = cwDestLen * 2;
    241     }
     198        rc = vboxClipboardWriteUtf16LE(pCtx, pu16SrcText, cwSrcLen,
     199                                       pv, cb, pcbActual);
    242200    XtFree(reinterpret_cast<char *>(pValue));
    243201    RTUtf16Free(pu16SrcText);
     
    265223                                         uint32_t *pcbActual)
    266224{
    267     size_t cwSrcLen, cwDestLen;
     225    size_t cwSrcLen;
    268226    char **ppu8SrcText = NULL;
    269227    PRTUTF16 pu16SrcText = NULL;
    270     PRTUTF16 pu16DestText = reinterpret_cast<PRTUTF16>(pv);
    271228    XTextProperty property;
    272229    int rc = VINF_SUCCESS;
     
    305262    if (RT_SUCCESS(rc))
    306263        rc = RTStrToUtf16Ex(*ppu8SrcText, cbSrcLen, &pu16SrcText, 0, &cwSrcLen);
    307     /* Check how much longer will the converted text will be. */
    308264    if (RT_SUCCESS(rc))
    309         rc = vboxClipboardUtf16GetWinSize(pu16SrcText, cwSrcLen, &cwDestLen);
    310     if (RT_SUCCESS(rc) && (cb < cwDestLen * 2))
    311     {
    312         /* Not enough buffer space provided - report the amount needed. */
    313         LogFlowFunc (("guest buffer too small: size %d bytes, needed %d.  Returning.\n",
    314                        cb, cwDestLen * 2));
    315         *pcbActual = cwDestLen * 2;
    316         rc = VERR_BUFFER_OVERFLOW;
    317     }
    318     /* Convert the text. */
    319     if (RT_SUCCESS(rc))
    320         rc = vboxClipboardUtf16LinToWin(pu16SrcText, cwSrcLen, pu16DestText, cb / 2);
    321     if (RT_SUCCESS(rc))
    322     {
    323         LogFlowFunc (("converted string is %.*ls\n", cwDestLen, pu16DestText));
    324         *pcbActual = cwDestLen * 2;
    325     }
     265        rc = vboxClipboardWriteUtf16LE(pCtx, pu16SrcText, cwSrcLen,
     266                                       pv, cb, pcbActual);
    326267    if (ppu8SrcText != NULL)
    327268        XFreeStringList(ppu8SrcText);
     
    418359    switch (pCtx->X11TextFormat)
    419360    {
    420     case UTF16:
    421         vboxClipboardGetUtf16(pCtx, pValue, cTextLen / 2, pRequest->pv,
    422                               pRequest->cb, pRequest->pcbActual);
    423         break;
    424361    case CTEXT:
    425362        vboxClipboardGetCTextFromX11(pCtx, pValue, cTextLen, pRequest->pv,
     
    660597        pCtx->atomMultiple  = XInternAtom(XtDisplay(pCtx->widget), "MULTIPLE",  false);
    661598        pCtx->atomTimestamp = XInternAtom(XtDisplay(pCtx->widget), "TIMESTAMP", false);
    662         pCtx->atomUtf16     = XInternAtom(XtDisplay(pCtx->widget),
    663                                           "text/plain;charset=ISO-10646-UCS-2", false);
    664599        pCtx->atomUtf8      = XInternAtom(XtDisplay(pCtx->widget), "UTF_STRING", false);
    665600        /* And build up the vector of supported formats */
    666601        pCtx->atomCText     = XInternAtom(XtDisplay(pCtx->widget), "COMPOUND_TEXT", false);
    667602        /* And build up the vector of supported formats */
    668         if (!g_testUtf8 && !g_testCText)
    669             vboxClipboardAddFormat(pCtx,
    670                                    "text/plain;charset=ISO-10646-UCS-2",
    671                                    UTF16,
    672                                    VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT);
    673         if (!g_testUtf16 && !g_testCText)
     603        if (!g_testCText)
    674604        {
    675605            vboxClipboardAddFormat(pCtx, "UTF8_STRING", UTF8,
     
    686616                                   VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT);
    687617}
    688         if (!g_testUtf16 && !g_testUtf8)
     618        if (!g_testUtf8)
    689619            vboxClipboardAddFormat(pCtx, "COMPOUND_TEXT", CTEXT,
    690620                                   VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT);
     
    721651    }
    722652
    723     if (RTEnvGet("VBOX_CBTEST_UTF16"))
    724     {
    725         g_testUtf16 = true;
    726         LogRel(("Host clipboard: testing Utf16\n"));
    727     }
    728     else if (RTEnvGet("VBOX_CBTEST_UTF8"))
     653    if (RTEnvGet("VBOX_CBTEST_UTF8"))
    729654    {
    730655        g_testUtf8 = true;
     
    938863    *pcLenReturn = cTargets + 3;
    939864    *piFormatReturn = 32;
    940     return true;
    941 }
    942 
    943 /**
    944  * Satisfy a request from X11 to convert the clipboard text to Utf16.  We
    945  * return non-zero terminated text.
    946  * @todo that works, but it is bad.  Change it to return zero-terminated
    947  *       text.
    948  *
    949  * @returns true if we successfully convert the data to the format
    950  * requested, false otherwise.
    951  *
    952  * @param  atomTypeReturn  Where to store the atom for the type of the data
    953  *                         we are returning
    954  * @param  pValReturn      Where to store the pointer to the data we are
    955  *                         returning.  This should be to memory allocated by
    956  *                         XtMalloc, which will be freed by the Xt toolkit
    957  *                         later.
    958  * @param  pcLenReturn     Where to store the length of the data we are
    959  *                         returning
    960  * @param  piFormatReturn  Where to store the bit width (8, 16, 32) of the
    961  *                         data we are returning
    962  * @note  X11 backend code, called by the callback for XtOwnSelection.
    963  */
    964 static Boolean vboxClipboardConvertUtf16(VBOXCLIPBOARDCONTEXTX11 *pCtx,
    965                                          Atom *atomTypeReturn,
    966                                          XtPointer *pValReturn,
    967                                          unsigned long *pcLenReturn,
    968                                          int *piFormatReturn)
    969 {
    970     PRTUTF16 pu16SrcText, pu16DestText;
    971     void *pvVBox;
    972     uint32_t cbVBox;
    973     size_t cwSrcLen, cwDestLen;
    974     int rc;
    975 
    976     LogFlowFunc (("called\n"));
    977     rc = VBoxX11ClipboardReadVBoxData(pCtx->pFrontend, VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT, &pvVBox, &cbVBox);
    978     if ((RT_FAILURE(rc)) || (cbVBox == 0))
    979     {
    980         /* If VBoxX11ClipboardReadVBoxData fails then pClient may be invalid */
    981         LogRelFunc (("VBoxX11ClipboardReadVBoxData returned %Rrc%s\n", rc,
    982                     RT_SUCCESS(rc) ? ", cbVBox == 0" :  ""));
    983         RTMemFree(pvVBox);
    984         return false;
    985     }
    986     pu16SrcText = reinterpret_cast<PRTUTF16>(pvVBox);
    987     cwSrcLen = cbVBox / 2;
    988     /* How long will the converted text be? */
    989     rc = vboxClipboardUtf16GetLinSize(pu16SrcText, cwSrcLen, &cwDestLen);
    990     if (RT_FAILURE(rc))
    991     {
    992         LogRel(("vboxClipboardConvertUtf16: clipboard conversion failed.  vboxClipboardUtf16GetLinSize returned %Rrc.  Abandoning.\n", rc));
    993         RTMemFree(pvVBox);
    994         AssertRCReturn(rc, false);
    995     }
    996     if (cwDestLen == 0)
    997     {
    998         LogFlowFunc(("received empty clipboard data from the guest, returning false.\n"));
    999         RTMemFree(pvVBox);
    1000         return false;
    1001     }
    1002     pu16DestText = reinterpret_cast<PRTUTF16>(XtMalloc(cwDestLen * 2));
    1003     if (pu16DestText == 0)
    1004     {
    1005         LogRel(("vboxClipboardConvertUtf16: failed to allocate %d bytes\n", cwDestLen * 2));
    1006         RTMemFree(pvVBox);
    1007         return false;
    1008     }
    1009     /* Convert the text. */
    1010     rc = vboxClipboardUtf16WinToLin(pu16SrcText, cwSrcLen, pu16DestText, cwDestLen);
    1011     if (RT_FAILURE(rc))
    1012     {
    1013         LogRel(("vboxClipboardConvertUtf16: clipboard conversion failed.  vboxClipboardUtf16WinToLin returned %Rrc.  Abandoning.\n", rc));
    1014         XtFree(reinterpret_cast<char *>(pu16DestText));
    1015         RTMemFree(pvVBox);
    1016         return false;
    1017     }
    1018     LogFlowFunc (("converted string is %.*ls. Returning.\n", cwDestLen, pu16DestText));
    1019     RTMemFree(pvVBox);
    1020     *atomTypeReturn = pCtx->atomUtf16;
    1021     *pValReturn = reinterpret_cast<XtPointer>(pu16DestText);
    1022     *pcLenReturn = cwDestLen;
    1023     *piFormatReturn = 16;
    1024865    return true;
    1025866}
     
    13211162                                                 pValReturn, pcLenReturn,
    13221163                                                 piFormatReturn);
    1323     case UTF16:
    1324         return vboxClipboardConvertUtf16(pCtx, atomTypeReturn, pValReturn,
    1325                                          pcLenReturn, piFormatReturn);
    13261164    case UTF8:
    13271165        return vboxClipboardConvertToUtf8ForX11(pCtx, atomTypeReturn,
Note: See TracChangeset for help on using the changeset viewer.

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