VirtualBox

Changeset 46313 in vbox for trunk/src/VBox/GuestHost


Ignore:
Timestamp:
May 29, 2013 2:13:03 PM (12 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
86079
Message:

GuestHost/SharedClipboard/X11: XInternAtom() does caching too, and is much simpler.

File:
1 edited

Legend:

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

    r46307 r46313  
    5454#include <VBox/HostServices/VBoxClipboardSvc.h>
    5555
    56 static Atom clipGetAtom(Widget widget, const char *pszName);
     56static Atom clipGetAtom(CLIPBACKEND *pCtx, const char *pszName);
    5757
    5858/** The different clipboard formats which we support. */
     
    105105 * @param widget a valid Xt widget
    106106 */
    107 static Atom clipAtomForX11Format(Widget widget, CLIPX11FORMAT format)
    108 {
    109     return clipGetAtom(widget, g_aFormats[format].pcszAtom);
     107static Atom clipAtomForX11Format(CLIPBACKEND *pCtx, CLIPX11FORMAT format)
     108{
     109    return clipGetAtom(pCtx, g_aFormats[format].pcszAtom);
    110110}
    111111
     
    126126 * @param   widget a valid Xt widget
    127127 */
    128 static CLIPX11FORMAT clipFindX11FormatByAtom(Widget widget, Atom atomFormat)
     128static CLIPX11FORMAT clipFindX11FormatByAtom(CLIPBACKEND *pCtx, Atom atomFormat)
    129129{
    130130    for (unsigned i = 0; i < RT_ELEMENTS(g_aFormats); ++i)
    131         if (clipAtomForX11Format(widget, i) == atomFormat)
     131        if (clipAtomForX11Format(pCtx, i) == atomFormat)
    132132            return i;
    133133    return NIL_CLIPX11FORMAT;
     
    273273/** Convert an atom name string to an X11 atom, looking it up in a cache
    274274 * before asking the server */
    275 static Atom clipGetAtom(Widget widget, const char *pszName)
     275static Atom clipGetAtom(CLIPBACKEND *pCtx, const char *pszName)
    276276{
    277277    AssertPtrReturn(pszName, None);
    278     Atom retval = None;
    279     XrmValue nameVal, atomVal;
    280     nameVal.addr = (char *) pszName;
    281     nameVal.size = strlen(pszName);
    282     atomVal.size = sizeof(Atom);
    283     atomVal.addr = (char *) &retval;
    284     XtConvertAndStore(widget, XtRString, &nameVal, XtRAtom, &atomVal);
    285     return retval;
     278    return XInternAtom(XtDisplay(pCtx->widget), pszName, False);
    286279}
    287280
     
    354347    for (unsigned i = 0; i < cTargets; ++i)
    355348    {
    356         CLIPX11FORMAT format = clipFindX11FormatByAtom(pCtx->widget,
    357                                                        pTargets[i]);
     349        CLIPX11FORMAT format = clipFindX11FormatByAtom(pCtx, pTargets[i]);
    358350        if (format != NIL_CLIPX11FORMAT)
    359351        {
     
    376368    Atom targets[2];
    377369    CLIPX11FORMAT x11Format;
    378     targets[0] = clipGetAtom(NULL, "text/plain");
    379     targets[1] = clipGetAtom(NULL, "TARGETS");
     370    targets[0] = clipGetAtom(pCtx, "text/plain");
     371    targets[1] = clipGetAtom(pCtx, "TARGETS");
    380372    x11Format = clipGetTextFormatFromTargets(pCtx, targets, 3);
    381373    if (clipRealFormatForX11Format(x11Format) != TEXT)
    382374        success = false;
    383     targets[0] = clipGetAtom(NULL, "UTF8_STRING");
    384     targets[1] = clipGetAtom(NULL, "text/plain");
     375    targets[0] = clipGetAtom(pCtx, "UTF8_STRING");
     376    targets[1] = clipGetAtom(pCtx, "text/plain");
    385377    x11Format = clipGetTextFormatFromTargets(pCtx, targets, 3);
    386378    if (clipRealFormatForX11Format(x11Format) != UTF8)
     
    408400    for (unsigned i = 0; i < cTargets; ++i)
    409401    {
    410         CLIPX11FORMAT format = clipFindX11FormatByAtom(pCtx->widget,
    411                                                        pTargets[i]);
     402        CLIPX11FORMAT format = clipFindX11FormatByAtom(pCtx, pTargets[i]);
    412403        if (format != NIL_CLIPX11FORMAT)
    413404        {
     
    527518    pCtx->fBusy = true;
    528519    XtGetSelectionValue(pCtx->widget,
    529                         clipGetAtom(pCtx->widget, "CLIPBOARD"),
    530                         clipGetAtom(pCtx->widget, "TARGETS"),
     520                        clipGetAtom(pCtx, "CLIPBOARD"),
     521                        clipGetAtom(pCtx, "TARGETS"),
    531522                        clipConvertX11Targets, pCtx,
    532523                        CurrentTime);
     
    730721        /* Enable clipboard update notification */
    731722        pCtx->fixesSelectInput(pDisplay, XtWindow(pCtx->widget),
    732                                clipGetAtom(pCtx->widget, "CLIPBOARD"),
     723                               clipGetAtom(pCtx, "CLIPBOARD"),
    733724                               7 /* All XFixes*Selection*NotifyMask flags */);
    734725#endif
     
    912903        if (format != NIL_CLIPX11FORMAT)
    913904        {
    914             atomTargets[cTargets] = clipAtomForX11Format(pCtx->widget,
    915                                                           format);
     905            atomTargets[cTargets] = clipAtomForX11Format(pCtx, format);
    916906            ++cTargets;
    917907        }
    918908    } while (format != NIL_CLIPX11FORMAT);
    919909    /* We always offer these */
    920     atomTargets[cTargets] = clipGetAtom(pCtx->widget, "TARGETS");
    921     atomTargets[cTargets + 1] = clipGetAtom(pCtx->widget, "MULTIPLE");
    922     atomTargets[cTargets + 2] = clipGetAtom(pCtx->widget, "TIMESTAMP");
     910    atomTargets[cTargets] = clipGetAtom(pCtx, "TARGETS");
     911    atomTargets[cTargets + 1] = clipGetAtom(pCtx, "MULTIPLE");
     912    atomTargets[cTargets + 2] = clipGetAtom(pCtx, "TIMESTAMP");
    923913    *atomTypeReturn = XA_ATOM;
    924914    *pValReturn = (XtPointer)atomTargets;
     
    10801070 * @param  selType  the atom in question
    10811071 */
    1082 static bool clipIsSupportedSelectionType(Widget widget, Atom selType)
    1083 {
    1084     return(   (selType == clipGetAtom(widget, "CLIPBOARD"))
    1085            || (selType == clipGetAtom(widget, "PRIMARY")));
     1072static bool clipIsSupportedSelectionType(CLIPBACKEND *pCtx, Atom selType)
     1073{
     1074    return(   (selType == clipGetAtom(pCtx, "CLIPBOARD"))
     1075           || (selType == clipGetAtom(pCtx, "PRIMARY")));
    10861076}
    10871077
     
    11101100{
    11111101    int rc = VINF_SUCCESS;
    1112     CLIPX11FORMAT x11Format = clipFindX11FormatByAtom(pCtx->widget,
    1113                                                       *atomTarget);
     1102    CLIPX11FORMAT x11Format = clipFindX11FormatByAtom(pCtx, *atomTarget);
    11141103    CLIPFORMAT format = clipRealFormatForX11Format(x11Format);
    11151104    if (   ((format == UTF8) || (format == TEXT))
     
    11801169    if (!pCtx)
    11811170        return false;
    1182     if (!clipIsSupportedSelectionType(pCtx->widget, *atomSelection))
     1171    if (!clipIsSupportedSelectionType(pCtx, *atomSelection))
    11831172        return false;
    1184     if (*atomTarget == clipGetAtom(pCtx->widget, "TARGETS"))
     1173    if (*atomTarget == clipGetAtom(pCtx, "TARGETS"))
    11851174        rc = clipCreateX11Targets(pCtx, atomTypeReturn, pValReturn,
    11861175                                  pcLenReturn, piFormatReturn);
     
    12161205static void clipGrabX11CB(CLIPBACKEND *pCtx, uint32_t u32Formats)
    12171206{
    1218     if (XtOwnSelection(pCtx->widget, clipGetAtom(pCtx->widget, "CLIPBOARD"),
     1207    if (XtOwnSelection(pCtx->widget, clipGetAtom(pCtx, "CLIPBOARD"),
    12191208                       CurrentTime, clipXtConvertSelectionProc, NULL, 0))
    12201209    {
    12211210        pCtx->vboxFormats = u32Formats;
    12221211        /* Grab the middle-button paste selection too. */
    1223         XtOwnSelection(pCtx->widget, clipGetAtom(pCtx->widget, "PRIMARY"),
     1212        XtOwnSelection(pCtx->widget, clipGetAtom(pCtx, "PRIMARY"),
    12241213                       CurrentTime, clipXtConvertSelectionProc, NULL, 0);
    12251214#ifndef TESTCASE
     
    12271216         * ourselves. */
    12281217        XSetSelectionOwner(XtDisplay(pCtx->widget),
    1229                            clipGetAtom(pCtx->widget, "CLIPBOARD"),
     1218                           clipGetAtom(pCtx, "CLIPBOARD"),
    12301219                           XtWindow(pCtx->widget), CurrentTime);
    12311220        XSetSelectionOwner(XtDisplay(pCtx->widget),
    1232                            clipGetAtom(pCtx->widget, "PRIMARY"),
     1221                           clipGetAtom(pCtx, "PRIMARY"),
    12331222                           XtWindow(pCtx->widget), CurrentTime);
    12341223#endif
     
    15641553            /* Send out a request for the data to the current clipboard
    15651554             * owner */
    1566             XtGetSelectionValue(pCtx->widget, clipGetAtom(pCtx->widget, "CLIPBOARD"),
    1567                                 clipAtomForX11Format(pCtx->widget,
    1568                                                      pCtx->X11TextFormat),
     1555            XtGetSelectionValue(pCtx->widget, clipGetAtom(pCtx, "CLIPBOARD"),
     1556                                clipAtomForX11Format(pCtx, pCtx->X11TextFormat),
    15691557                                clipConvertX11CB,
    15701558                                reinterpret_cast<XtPointer>(pReq),
     
    15801568            /* Send out a request for the data to the current clipboard
    15811569             * owner */
    1582             XtGetSelectionValue(pCtx->widget, clipGetAtom(pCtx->widget, "CLIPBOARD"),
    1583                                 clipAtomForX11Format(pCtx->widget,
     1570            XtGetSelectionValue(pCtx->widget, clipGetAtom(pCtx, "CLIPBOARD"),
     1571                                clipAtomForX11Format(pCtx,
    15841572                                                     pCtx->X11BitmapFormat),
    15851573                                clipConvertX11CB,
     
    17621750/* This just looks for the atom names in a couple of tables and returns an
    17631751 * index with an offset added. */
    1764 Boolean XtConvertAndStore(Widget widget, _Xconst _XtString from_type,
    1765                           XrmValue* from, _Xconst _XtString to_type,
    1766                           XrmValue* to_in_out)
    1767 {
    1768     Boolean rc = False;
    1769     /* What we support is: */
    1770     AssertReturn(from_type == XtRString, False);
    1771     AssertReturn(to_type == XtRAtom, False);
     1752Atom XInternAtom(Display *, const char *pcsz, int)
     1753{
     1754    Atom atom = 0;
    17721755    for (unsigned i = 0; i < RT_ELEMENTS(g_aFormats); ++i)
    1773         if (!strcmp(from->addr, g_aFormats[i].pcszAtom))
    1774         {
    1775             *(Atom *)(to_in_out->addr) = (Atom) (i + 0x1000);
    1776             rc = True;
    1777         }
     1756        if (!strcmp(pcsz, g_aFormats[i].pcszAtom))
     1757            atom = (Atom) (i + 0x1000);
    17781758    for (unsigned i = 0; i < RT_ELEMENTS(g_apszSupAtoms); ++i)
    1779         if (!strcmp(from->addr, g_apszSupAtoms[i]))
    1780         {
    1781             *(Atom *)(to_in_out->addr) = (Atom) (i + 0x2000);
    1782             rc = True;
    1783         }
    1784     Assert(rc == True);  /* Have we missed any atoms? */
    1785     return rc;
     1759        if (!strcmp(pcsz, g_apszSupAtoms[i]))
     1760            atom = (Atom) (i + 0x2000);
     1761    Assert(atom);  /* Have we missed any atoms? */
     1762    return atom;
    17861763}
    17871764
     
    18031780    int format = 0;
    18041781    Atom type = XA_STRING;
    1805     if (   (   selection != clipGetAtom(NULL, "PRIMARY")
    1806             && selection != clipGetAtom(NULL, "CLIPBOARD")
    1807             && selection != clipGetAtom(NULL, "TARGETS"))
     1782    if (   (   selection != XInternAtom(NULL, "PRIMARY", 0)
     1783            && selection != XInternAtom(NULL, "CLIPBOARD", 0)
     1784            && selection != XInternAtom(NULL, "TARGETS", 0))
    18081785        || (   target != g_selTarget[0]
    1809             && target != clipGetAtom(NULL, "TARGETS")))
     1786            && target != XInternAtom(NULL, "TARGETS", 0)))
    18101787    {
    18111788        /* Otherwise this is probably a caller error. */
     
    18161793    }
    18171794    XtPointer pValue = NULL;
    1818     if (target == clipGetAtom(NULL, "TARGETS"))
     1795    if (target == XInternAtom(NULL, "TARGETS", 0))
    18191796    {
    18201797        if (g_fTargetsFailure)
     
    18771854                       XtSelectionDoneProc done)
    18781855{
    1879     if (selection != clipGetAtom(NULL, "CLIPBOARD"))
     1856    if (selection != XInternAtom(NULL, "CLIPBOARD", 0))
    18801857        return True;  /* We don't really care about this. */
    18811858    g_ownsSel = true;  /* Always succeed. */
     
    18991876                                 int *format)
    19001877{
    1901     Atom target = clipGetAtom(NULL, pcszTarget);
     1878    Atom target = XInternAtom(NULL, pcszTarget, 0);
    19021879    if (target == 0)
    19031880        return false;
     
    19111888    if (!g_pfnSelConvert)
    19121889        return false;
    1913     Atom clipAtom = clipGetAtom(NULL, "CLIPBOARD");
     1890    Atom clipAtom = XInternAtom(NULL, "CLIPBOARD", 0);
    19141891    if (!g_pfnSelConvert(TEST_WIDGET, &clipAtom, &target, type,
    19151892                         value, length, format))
     
    19251902                                   unsigned long count, int format)
    19261903{
    1927     Atom clipAtom = clipGetAtom(NULL, "CLIPBOARD");
    1928     g_selTarget[0] = clipGetAtom(NULL, pcszTarget);
     1904    Atom clipAtom = XInternAtom(NULL, "CLIPBOARD", 0);
     1905    g_selTarget[0] = XInternAtom(NULL, pcszTarget, 0);
    19291906    g_selType = type;
    19301907    g_pSelData = data;
     
    23602337                     sizeof("hello world") * 2);
    23612338    testStringFromVBox(hTest, pCtx, "UTF8_STRING",
    2362                        clipGetAtom(NULL, "UTF8_STRING"), "hello world");
     2339                       clipGetAtom(pCtx, "UTF8_STRING"), "hello world");
    23632340    /* With an embedded carriage return */
    23642341    clipSetVBoxUtf16(pCtx, VINF_SUCCESS, "hello\r\nworld",
    23652342                     sizeof("hello\r\nworld") * 2);
    23662343    testStringFromVBox(hTest, pCtx, "text/plain;charset=UTF-8",
    2367                        clipGetAtom(NULL, "text/plain;charset=UTF-8"),
     2344                       clipGetAtom(pCtx, "text/plain;charset=UTF-8"),
    23682345                       "hello\nworld");
    23692346    /* With an embedded CRCRLF */
     
    23712348                     sizeof("hello\r\r\nworld") * 2);
    23722349    testStringFromVBox(hTest, pCtx, "text/plain;charset=UTF-8",
    2373                        clipGetAtom(NULL, "text/plain;charset=UTF-8"),
     2350                       clipGetAtom(pCtx, "text/plain;charset=UTF-8"),
    23742351                       "hello\r\nworld");
    23752352    /* With an embedded CRLFCR */
     
    23772354                     sizeof("hello\r\n\rworld") * 2);
    23782355    testStringFromVBox(hTest, pCtx, "text/plain;charset=UTF-8",
    2379                        clipGetAtom(NULL, "text/plain;charset=UTF-8"),
     2356                       clipGetAtom(pCtx, "text/plain;charset=UTF-8"),
    23802357                       "hello\n\rworld");
    23812358    /* An empty string */
    23822359    clipSetVBoxUtf16(pCtx, VINF_SUCCESS, "", 2);
    23832360    testStringFromVBox(hTest, pCtx, "text/plain;charset=utf-8",
    2384                        clipGetAtom(NULL, "text/plain;charset=utf-8"), "");
     2361                       clipGetAtom(pCtx, "text/plain;charset=utf-8"), "");
    23852362    /* With an embedded Utf-8 character. */
    23862363    clipSetVBoxUtf16(pCtx, VINF_SUCCESS, "100\xE2\x82\xAC" /* 100 Euro */,
    23872364                     10);
    23882365    testStringFromVBox(hTest, pCtx, "STRING",
    2389                        clipGetAtom(NULL, "STRING"), "100\xE2\x82\xAC");
     2366                       clipGetAtom(pCtx, "STRING"), "100\xE2\x82\xAC");
    23902367    /* A non-zero-terminated string */
    23912368    clipSetVBoxUtf16(pCtx, VINF_SUCCESS, "hello world",
    23922369                     sizeof("hello world") * 2 - 2);
    2393     testStringFromVBox(hTest, pCtx, "TEXT", clipGetAtom(NULL, "TEXT"),
     2370    testStringFromVBox(hTest, pCtx, "TEXT", clipGetAtom(pCtx, "TEXT"),
    23942371                       "hello world");
    23952372
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