XKBBind.c 23.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*

Copyright 1985, 1987, 1994, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from The Open Group.

*/

29
        /* the new monsters ate the old ones */
30 31 32 33 34

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "XKBlib.h"
35 36 37
#include <nx-X11/Xlibint.h>
#include <nx-X11/Xutil.h>
#include <nx-X11/keysym.h>
38 39 40
#include <stdio.h>
#include <ctype.h>

41
#include <nx-X11/extensions/XKBproto.h>
42 43 44
#include "XKBlibint.h"

#define AllMods (ShiftMask|LockMask|ControlMask| \
45
                 Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask)
46

47
static int _XkbLoadDpy(Display *dpy);
48 49

struct _XKeytrans {
50 51 52 53 54 55 56
    struct _XKeytrans *next;    /* next on list */
    char *string;               /* string to return when the time comes */
    int len;                    /* length of string (since NULL is legit) */
    KeySym key;                 /* keysym rebound */
    unsigned int state;         /* modifier state */
    KeySym *modifiers;          /* modifier keysyms you want */
    int mlen;                   /* length of modifier list */
57 58 59 60 61
};

KeySym
XkbKeycodeToKeysym(Display *dpy,
#if NeedWidePrototypes
62
                   unsigned int kc,
63
#else
64
                   KeyCode kc,
65
#endif
66 67
                   int group,
                   int level)
68
{
69
    XkbDescRec *xkb;
70

71
    if (_XkbUnavailable(dpy))
72
        return NoSymbol;
73

74
    _XkbCheckPendingRefresh(dpy, dpy->xkb_info);
75 76

    xkb = dpy->xkb_info->desc;
77 78 79 80 81 82 83 84 85 86 87 88 89 90
    if ((kc < xkb->min_key_code) || (kc > xkb->max_key_code))
        return NoSymbol;

    if ((group < 0) || (level < 0) || (group >= XkbKeyNumGroups(xkb, kc)))
        return NoSymbol;
    if (level >= XkbKeyGroupWidth(xkb, kc, group)) {
        /* for compatibility with the core protocol, _always_ allow  */
        /* two symbols in the first two groups.   If either of the   */
        /* two is of type ONE_LEVEL, just replicate the first symbol */
        if ((group > XkbGroup2Index) || (XkbKeyGroupWidth(xkb, kc, group) != 1)
            || (level != 1)) {
            return NoSymbol;
        }
        level = 0;
91
    }
92
    return XkbKeySymEntry(xkb, kc, level, group);
93 94 95 96 97
}

KeySym
XKeycodeToKeysym(Display *dpy,
#if NeedWidePrototypes
98
                 unsigned int kc,
99
#else
100
                 KeyCode kc,
101
#endif
102
                 int col)
103
{
104
    XkbDescRec *xkb;
105

106
    if (_XkbUnavailable(dpy))
107
        return _XKeycodeToKeysym(dpy, kc, col);
108

109
    _XkbCheckPendingRefresh(dpy, dpy->xkb_info);
110 111

    xkb = dpy->xkb_info->desc;
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    if ((kc < xkb->min_key_code) || (kc > xkb->max_key_code))
        return NoSymbol;

    if (col > 3) {
        int lastSym, tmp, nGrp;

        lastSym = 3;
        nGrp = XkbKeyNumGroups(xkb, kc);
        if ((nGrp > 0) &&
            ((tmp = XkbKeyGroupWidth(xkb, kc, XkbGroup1Index)) > 2)) {
            if (col <= (lastSym + tmp - 2))
                return XkbKeycodeToKeysym(dpy, kc, XkbGroup1Index,
                                          col - lastSym + 2);
            lastSym += tmp - 2;
        }
        if ((nGrp > 1) &&
            ((tmp = XkbKeyGroupWidth(xkb, kc, XkbGroup2Index)) > 2)) {
            if (col <= (lastSym + tmp - 2))
                return XkbKeycodeToKeysym(dpy, kc, XkbGroup2Index,
                                          col - lastSym + 2);
            lastSym += tmp - 2;
        }
        if (nGrp > 2) {
            tmp = XkbKeyGroupWidth(xkb, kc, XkbGroup3Index);
            if (col <= lastSym + tmp)
                return XkbKeycodeToKeysym(dpy, kc, XkbGroup3Index,
                                          col - lastSym);
            lastSym += tmp;
        }
        if (nGrp > 3) {
            tmp = XkbKeyGroupWidth(xkb, kc, XkbGroup4Index);
            if (col <= lastSym + tmp)
                return XkbKeycodeToKeysym(dpy, kc, XkbGroup4Index,
                                          col - lastSym);
        }
        return NoSymbol;
148
    }
149
    return XkbKeycodeToKeysym(dpy, kc, (col >> 1), (col & 1));
150 151 152 153 154 155 156 157
}

KeyCode
XKeysymToKeycode(Display *dpy, KeySym ks)
{
    register int i, j, gotOne;

    if (_XkbUnavailable(dpy))
158 159
        return _XKeysymToKeycode(dpy, ks);
    _XkbCheckPendingRefresh(dpy, dpy->xkb_info);
160

161
    j = 0;
162
    do {
163 164 165 166 167 168 169 170 171 172
        register XkbDescRec *xkb = dpy->xkb_info->desc;
        gotOne = 0;
        for (i = dpy->min_keycode; i <= dpy->max_keycode; i++) {
            if (j < (int) XkbKeyNumSyms(xkb, i)) {
                gotOne = 1;
                if ((XkbKeySym(xkb, i, j) == ks))
                    return i;
            }
        }
        j++;
173 174 175 176 177 178 179
    } while (gotOne);
    return 0;
}

static int
_XkbComputeModmap(Display *dpy)
{
180
    register XkbDescPtr xkb;
181

182 183 184
    xkb = dpy->xkb_info->desc;
    if (XkbGetUpdatedMap(dpy, XkbModifierMapMask, xkb) == Success)
        return 1;
185 186 187 188
    return 0;
}

unsigned
189
XkbKeysymToModifiers(Display *dpy, KeySym ks)
190 191
{
    XkbDescRec *xkb;
192
    register int i, j;
193 194 195 196
    register KeySym *pSyms;
    CARD8 mods;

    if (_XkbUnavailable(dpy))
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
        return _XKeysymToModifiers(dpy, ks);
    _XkbCheckPendingRefresh(dpy, dpy->xkb_info);

    if (_XkbNeedModmap(dpy->xkb_info) && (!_XkbComputeModmap(dpy)))
        return _XKeysymToModifiers(dpy, ks);

    xkb = dpy->xkb_info->desc;
    mods = 0;
    for (i = xkb->min_key_code; i <= (int) xkb->max_key_code; i++) {
        pSyms = XkbKeySymsPtr(xkb, i);
        for (j = XkbKeyNumSyms(xkb, i) - 1; j >= 0; j--) {
            if (pSyms[j] == ks) {
                mods |= xkb->map->modmap[i];
                break;
            }
        }
213 214 215 216 217
    }
    return mods;
}

KeySym
218
XLookupKeysym(register XKeyEvent * event, int col)
219 220
{
    Display *dpy = event->display;
221

222
    if (_XkbUnavailable(dpy))
223 224
        return _XLookupKeysym(event, col);
    _XkbCheckPendingRefresh(dpy, dpy->xkb_info);
225 226 227 228
    return XKeycodeToKeysym(dpy, event->keycode, col);
}

   /*
229 230
    * Not a public entry point -- XkbTranslateKey is an obsolete name
    * that is preserved here so that functions linked against the old
231 232 233
    * version will continue to work in a shared library environment.
    */
int
234 235 236 237 238 239
XkbTranslateKey(register Display *dpy,
                KeyCode key,
                register unsigned int mods,
                unsigned int *mods_rtrn,
                KeySym *keysym_rtrn);

240
int
241 242 243 244 245
XkbTranslateKey(register Display *dpy,
                KeyCode key,
                register unsigned int mods,
                unsigned int *mods_rtrn,
                KeySym *keysym_rtrn)
246
{
247
    return XkbLookupKeySym(dpy, key, mods, mods_rtrn, keysym_rtrn);
248 249 250
}

Bool
251 252 253 254 255
XkbLookupKeySym(register Display *dpy,
                KeyCode key,
                register unsigned int mods,
                unsigned int *mods_rtrn,
                KeySym *keysym_rtrn)
256 257
{
    if (_XkbUnavailable(dpy))
258 259 260 261
        return _XTranslateKey(dpy, key, mods, mods_rtrn, keysym_rtrn);
    _XkbCheckPendingRefresh(dpy, dpy->xkb_info);
    return XkbTranslateKeyCode(dpy->xkb_info->desc, key, mods, mods_rtrn,
                               keysym_rtrn);
262 263 264
}

Bool
265 266 267 268 269
XkbTranslateKeyCode(register XkbDescPtr xkb,
                    KeyCode key,
                    register unsigned int mods,
                    unsigned int *mods_rtrn,
                    KeySym *keysym_rtrn)
270 271
{
    XkbKeyTypeRec *type;
272 273
    int col, nKeyGroups;
    unsigned preserve, effectiveGroup;
274 275
    KeySym *syms;

276 277
    if (mods_rtrn != NULL)
        *mods_rtrn = 0;
278

279 280 281 282 283
    nKeyGroups = XkbKeyNumGroups(xkb, key);
    if ((!XkbKeycodeInRange(xkb, key)) || (nKeyGroups == 0)) {
        if (keysym_rtrn != NULL)
            *keysym_rtrn = NoSymbol;
        return False;
284 285
    }

286
    syms = XkbKeySymsPtr(xkb, key);
287 288 289

    /* find the offset of the effective group */
    col = 0;
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
    effectiveGroup = XkbGroupForCoreState(mods);
    if (effectiveGroup >= nKeyGroups) {
        unsigned groupInfo = XkbKeyGroupInfo(xkb, key);

        switch (XkbOutOfRangeGroupAction(groupInfo)) {
        default:
            effectiveGroup %= nKeyGroups;
            break;
        case XkbClampIntoRange:
            effectiveGroup = nKeyGroups - 1;
            break;
        case XkbRedirectIntoRange:
            effectiveGroup = XkbOutOfRangeGroupNumber(groupInfo);
            if (effectiveGroup >= nKeyGroups)
                effectiveGroup = 0;
            break;
        }
307
    }
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
    col = effectiveGroup * XkbKeyGroupsWidth(xkb, key);
    type = XkbKeyKeyType(xkb, key, effectiveGroup);

    preserve = 0;
    if (type->map) {  /* find the column (shift level) within the group */
        register int i;
        register XkbKTMapEntryPtr entry;

        for (i = 0, entry = type->map; i < type->map_count; i++, entry++) {
            if ((entry->active) &&
                ((mods & type->mods.mask) == entry->mods.mask)) {
                col += entry->level;
                if (type->preserve)
                    preserve = type->preserve[i].mask;
                break;
            }
        }
325 326
    }

327 328
    if (keysym_rtrn != NULL)
        *keysym_rtrn = syms[col];
329
    if (mods_rtrn) {
330 331 332 333 334 335 336 337 338 339 340 341 342
        *mods_rtrn = type->mods.mask & (~preserve);
        /* The Motif VTS doesn't get the help callback called if help
         * is bound to Shift+<whatever>, and it appears as though it
         * is XkbTranslateKeyCode that is causing the problem.  The
         * core X version of XTranslateKey always OR's in ShiftMask
         * and LockMask for mods_rtrn, so this "fix" keeps this behavior
         * and solves the VTS problem.
         */
        if ((xkb->dpy) && (xkb->dpy->xkb_info) &&
            (xkb->dpy->xkb_info->
             xlib_ctrls & XkbLC_AlwaysConsumeShiftAndLock)) {
            *mods_rtrn |= (ShiftMask | LockMask);
        }
343
    }
344
    return (syms[col] != NoSymbol);
345 346 347
}

Status
348
XkbRefreshKeyboardMapping(register XkbMapNotifyEvent * event)
349 350 351 352 353
{
    Display *dpy = event->display;
    XkbInfoPtr xkbi;

    if (_XkbUnavailable(dpy)) {
354 355
        _XRefreshKeyboardMapping((XMappingEvent *) event);
        return Success;
356
    }
357
    xkbi = dpy->xkb_info;
358

359 360 361 362 363
    if (((event->type & 0x7f) - xkbi->codes->first_event) != XkbEventCode)
        return BadMatch;
    if (event->xkb_type == XkbNewKeyboardNotify) {
        _XkbReloadDpy(dpy);
        return Success;
364
    }
365 366 367 368 369 370 371 372 373 374
    if (event->xkb_type == XkbMapNotify) {
        XkbMapChangesRec changes;
        Status rtrn;

        if (xkbi->flags & XkbMapPending)
            changes = xkbi->changes;
        else
            bzero(&changes, sizeof(changes));
        XkbNoteMapChanges(&changes, event, XKB_XLIB_MAP_MASK);
        if ((rtrn = XkbGetMapChanges(dpy, xkbi->desc, &changes)) != Success) {
375
#ifdef DEBUG
376
            fprintf(stderr, "Internal Error! XkbGetMapChanges failed:\n");
377
#endif
378 379 380 381 382 383 384
            xkbi->changes = changes;
        }
        else if (xkbi->flags & XkbMapPending) {
            xkbi->flags &= ~XkbMapPending;
            bzero(&xkbi->changes, sizeof(XkbMapChangesRec));
        }
        return rtrn;
385 386 387 388 389
    }
    return BadMatch;
}

int
390
XRefreshKeyboardMapping(register XMappingEvent * event)
391
{
392
    XkbEvent *xkbevent = (XkbEvent *) event;
393 394 395 396 397 398 399 400
    Display *dpy = event->display;
    XkbMapChangesRec changes;
    XkbInfoPtr xkbi;

    /* always do this for input methods, which still use the old keymap */
    (void) _XRefreshKeyboardMapping(event);

    if (_XkbUnavailable(dpy))
401
        return 1;
402 403 404

    xkbi = dpy->xkb_info;

405 406
    if (((event->type & 0x7f) - xkbi->codes->first_event) == XkbEventCode)
        return XkbRefreshKeyboardMapping(&xkbevent->map);
407

408 409 410
    if (xkbi->flags & XkbXlibNewKeyboard) {
        _XkbReloadDpy(dpy);
        return 1;
411 412
    }

413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
    if ((xkbi->flags & XkbMapPending) || (event->request == MappingKeyboard)) {
        if (xkbi->flags & XkbMapPending) {
            changes = xkbi->changes;
            _XkbNoteCoreMapChanges(&changes, event, XKB_XLIB_MAP_MASK);
        }
        else {
            bzero(&changes, sizeof(changes));
            changes.changed = XkbKeySymsMask;
            if (xkbi->desc->min_key_code < xkbi->desc->max_key_code) {
                changes.first_key_sym = xkbi->desc->min_key_code;
                changes.num_key_syms = xkbi->desc->max_key_code -
                    xkbi->desc->min_key_code + 1;
            }
            else {
                changes.first_key_sym = event->first_keycode;
                changes.num_key_syms = event->count;
            }
        }

        if (XkbGetMapChanges(dpy, xkbi->desc, &changes) != Success) {
433
#ifdef DEBUG
434 435 436 437 438 439 440 441 442 443 444 445 446 447
            fprintf(stderr, "Internal Error! XkbGetMapChanges failed:\n");
            if (changes.changed & XkbKeyTypesMask) {
                int first = changes.first_type;
                int last = changes.first_type + changes.num_types - 1;

                fprintf(stderr, "       types:  %d..%d\n", first, last);
            }
            if (changes.changed & XkbKeySymsMask) {
                int first = changes.first_key_sym;
                int last = changes.first_key_sym + changes.num_key_syms - 1;

                fprintf(stderr, "     symbols:  %d..%d\n", first, last);
            }
            if (changes.changed & XkbKeyActionsMask) {
448 449
                int first = changes.first_key_act;
                int last = changes.first_key_act + changes.num_key_acts - 1;
450 451 452 453

                fprintf(stderr, "     acts:  %d..%d\n", first, last);
            }
            if (changes.changed & XkbKeyBehaviorsMask) {
454 455 456
                int first = changes.first_key_behavior;
                int last = first + changes.num_key_behaviors - 1;

457 458 459 460 461 462
                fprintf(stderr, "   behaviors:  %d..%d\n", first, last);
            }
            if (changes.changed & XkbVirtualModsMask) {
                fprintf(stderr, "virtual mods: 0x%04x\n", changes.vmods);
            }
            if (changes.changed & XkbExplicitComponentsMask) {
463 464
                int first = changes.first_key_explicit;
                int last = first + changes.num_key_explicit - 1;
465 466 467

                fprintf(stderr, "    explicit:  %d..%d\n", first, last);
            }
468
#endif
469 470 471 472 473 474 475
        }
        LockDisplay(dpy);
        if (xkbi->flags & XkbMapPending) {
            xkbi->flags &= ~XkbMapPending;
            bzero(&xkbi->changes, sizeof(XkbMapChangesRec));
        }
        UnlockDisplay(dpy);
476
    }
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
    if (event->request == MappingModifier) {
        LockDisplay(dpy);
        if (xkbi->desc->map->modmap) {
            _XkbFree(xkbi->desc->map->modmap);
            xkbi->desc->map->modmap = NULL;
        }
        if (dpy->key_bindings) {
            register struct _XKeytrans *p;

            for (p = dpy->key_bindings; p; p = p->next) {
                register int i;

                p->state = 0;
                if (p->mlen > 0) {
                    for (i = 0; i < p->mlen; i++) {
                        p->state |= XkbKeysymToModifiers(dpy, p->modifiers[i]);
                    }
                    if (p->state)
                        p->state &= AllMods;
                    else
                        p->state = AnyModifier;
                }
            }
        }
        UnlockDisplay(dpy);
502 503 504 505 506 507 508 509
    }
    return 1;
}

static int
_XkbLoadDpy(Display *dpy)
{
    XkbInfoPtr xkbi;
510
    unsigned query, oldEvents;
511 512
    XkbDescRec *desc;

513 514
    if (!XkbUseExtension(dpy, NULL, NULL))
        return 0;
515 516 517

    xkbi = dpy->xkb_info;
    query = XkbAllClientInfoMask;
518
    desc = XkbGetMap(dpy, query, XkbUseCoreKbd);
519 520
    if (!desc) {
#ifdef DEBUG
521
        fprintf(stderr, "Warning! XkbGetMap failed!\n");
522
#endif
523
        return 0;
524 525 526 527 528
    }
    LockDisplay(dpy);
    xkbi->desc = desc;

    UnlockDisplay(dpy);
529 530 531 532 533 534
    oldEvents = xkbi->selected_events;
    if (!(xkbi->xlib_ctrls & XkbLC_IgnoreNewKeyboards)) {
        XkbSelectEventDetails(dpy, xkbi->desc->device_spec,
                              XkbNewKeyboardNotify,
                              XkbNKN_KeycodesMask | XkbNKN_DeviceIDMask,
                              XkbNKN_KeycodesMask | XkbNKN_DeviceIDMask);
535
    }
536 537
    XkbSelectEventDetails(dpy, xkbi->desc->device_spec, XkbMapNotify,
                          XkbAllClientInfoMask, XkbAllClientInfoMask);
538
    LockDisplay(dpy);
539
    xkbi->selected_events = oldEvents;
540 541 542 543 544 545 546 547 548
    UnlockDisplay(dpy);
    return 1;
}

void
_XkbReloadDpy(Display *dpy)
{
    XkbInfoPtr xkbi;
    XkbDescRec *desc;
549
    unsigned oldDeviceID;
550 551

    if (_XkbUnavailable(dpy))
552
        return;
553 554 555 556

    xkbi = dpy->xkb_info;
    LockDisplay(dpy);
    if (xkbi->desc) {
557 558 559 560 561
        oldDeviceID = xkbi->desc->device_spec;
        XkbFreeKeyboard(xkbi->desc, XkbAllComponentsMask, True);
        xkbi->desc = NULL;
        xkbi->flags &= ~(XkbMapPending | XkbXlibNewKeyboard);
        xkbi->changes.changed = 0;
562
    }
563 564
    else
        oldDeviceID = XkbUseCoreKbd;
565
    UnlockDisplay(dpy);
566
    desc = XkbGetMap(dpy, XkbAllClientInfoMask, XkbUseCoreKbd);
567
    if (!desc)
568
        return;
569 570 571 572
    LockDisplay(dpy);
    xkbi->desc = desc;
    UnlockDisplay(dpy);

573 574
    if (desc->device_spec != oldDeviceID) {
        /* transfer(?) event masks here */
575
#ifdef NOTYET
576 577 578 579 580 581 582 583
        unsigned oldEvents;

        oldEvents = xkbi->selected_events;
        XkbSelectEventDetails(dpy, xkbi->desc->device_spec, XkbMapNotify,
                              XkbAllMapComponentsMask, XkbAllClientInfoMask);
        LockDisplay(dpy);
        xkbi->selected_events = oldEvents;
        UnlockDisplay(dpy);
584 585 586 587 588 589
#endif
    }
    return;
}

int
590 591 592 593 594 595
XkbTranslateKeySym(register Display *dpy,
                   register KeySym *sym_rtrn,
                   unsigned int mods,
                   char *buffer,
                   int nbytes,
                   int *extra_rtrn)
596
{
597
    register XkbInfoPtr xkb;
598 599 600 601 602
    XkbKSToMBFunc cvtr;
    XPointer priv;
    char tmp[4];
    int n;

603
    xkb = dpy->xkb_info;
604
    if (!xkb->cvt.KSToMB) {
605 606
        _XkbGetConverters(_XkbGetCharset(), &xkb->cvt);
        _XkbGetConverters("ISO8859-1", &xkb->latin1cvt);
607 608 609
    }

    if (extra_rtrn)
610
        *extra_rtrn = 0;
611

612 613 614
    if ((buffer == NULL) || (nbytes == 0)) {
        buffer = tmp;
        nbytes = 4;
615 616 617
    }

    /* see if symbol rebound, if so, return that string. */
618
    n = XkbLookupKeyBinding(dpy, *sym_rtrn, mods, buffer, nbytes, extra_rtrn);
619 620 621
    if (n)
        return n;

622 623
    if (nbytes > 0)
        buffer[0] = '\0';
624

625 626
    if (xkb->cvt.KSToUpper && (mods & LockMask)) {
        *sym_rtrn = (*xkb->cvt.KSToUpper) (*sym_rtrn);
627 628
    }
    if (xkb->xlib_ctrls & XkbLC_ForceLatin1Lookup) {
629 630 631 632 633 634
        cvtr = xkb->latin1cvt.KSToMB;
        priv = xkb->latin1cvt.KSToMBPriv;
    }
    else {
        cvtr = xkb->cvt.KSToMB;
        priv = xkb->cvt.KSToMBPriv;
635 636
    }

637 638 639 640 641 642 643
    n = (*cvtr) (priv, *sym_rtrn, buffer, nbytes, extra_rtrn);

    if ((!xkb->cvt.KSToUpper) && (mods & LockMask)) {
        register int i;
        int change;

        for (i = change = 0; i < n; i++) {
644
            char ch = toupper(buffer[i]);
645 646 647 648 649 650 651 652 653 654
            change = (change || (buffer[i] != ch));
            buffer[i] = ch;
        }
        if (change) {
            if (n == 1)
                *sym_rtrn =
                    (*xkb->cvt.MBToKS) (xkb->cvt.MBToKSPriv, buffer, n, NULL);
            else
                *sym_rtrn = NoSymbol;
        }
655 656
    }

657 658 659 660 661 662 663 664 665 666
    if (mods & ControlMask) {
        if (n == 1) {
            buffer[0] = XkbToControl(buffer[0]);
            if (nbytes > 1)
                buffer[1] = '\0';
            return 1;
        }
        if (nbytes > 0)
            buffer[0] = '\0';
        return 0;
667 668 669 670 671
    }
    return n;
}

int
672 673 674 675 676
XLookupString(register XKeyEvent *event,
              char *buffer,
              int nbytes,
              KeySym *keysym,
              XComposeStatus *status)
677
{
678
    KeySym dummy;
679 680 681 682
    int rtrnLen;
    unsigned int new_mods;
    Display *dpy = event->display;

683 684 685 686 687
    if (keysym == NULL)
        keysym = &dummy;
    if (!XkbLookupKeySym(dpy, event->keycode, event->state, &new_mods, keysym))
        return 0;
    new_mods = (event->state & (~new_mods));
688 689

    /* find the group where a symbol can be converted to control one */
690 691 692 693 694 695 696 697
    if (new_mods & ControlMask && *keysym > 0x7F &&
        (dpy->xkb_info->xlib_ctrls & XkbLC_ControlFallback)) {
        XKeyEvent tmp_ev = *event;
        KeySym tmp_keysym;
        unsigned int tmp_new_mods;

        if (_XkbUnavailable(dpy)) {
            tmp_ev.state = event->state ^ dpy->mode_switch;
698 699
            if (XkbLookupKeySym(dpy, tmp_ev.keycode, tmp_ev.state,
                                &tmp_new_mods, &tmp_keysym) &&
700
                tmp_keysym != NoSymbol && tmp_keysym < 0x80) {
701 702
                *keysym = tmp_keysym;
            }
703 704
        }
        else {
705 706
            int n = XkbKeyNumGroups(dpy->xkb_info->desc, tmp_ev.keycode);
            int i;
707

708 709 710
            for (i = 0; i < n; i++) {
                if (XkbGroupForCoreState(event->state) == i)
                    continue;
711
                tmp_ev.state = XkbBuildCoreState(tmp_ev.state, i);
712
                if (XkbLookupKeySym(dpy, tmp_ev.keycode, tmp_ev.state,
713 714
                                    &tmp_new_mods, &tmp_keysym) &&
                    tmp_keysym != NoSymbol && tmp_keysym < 0x80) {
715
                    *keysym = tmp_keysym;
716
                    new_mods = (event->state & (~tmp_new_mods));
717 718 719 720
                    break;
                }
            }
        }
721
    }
722 723 724 725 726

    /* We *should* use the new_mods (which does not contain any modifiers */
    /* that were used to compute the symbol here, but pre-XKB XLookupString */
    /* did not and we have to remain compatible.  Sigh. */
    if (_XkbUnavailable(dpy) ||
727 728
        (dpy->xkb_info->xlib_ctrls & XkbLC_ConsumeLookupMods) == 0)
        new_mods = event->state;
729

730 731 732
    rtrnLen = XkbLookupKeyBinding(dpy, *keysym, new_mods, buffer, nbytes, NULL);
    if (rtrnLen > 0)
        return rtrnLen;
733

734
    return XkbTranslateKeySym(dpy, keysym, new_mods, buffer, nbytes, NULL);
735 736 737 738
}


int
739 740 741 742 743 744
XkbLookupKeyBinding(Display *dpy,
                    register KeySym sym,
                    unsigned int mods,
                    char *buffer,
                    int nbytes,
                    int *extra_rtrn)
745
{
746
    register struct _XKeytrans *p;
747 748

    if (extra_rtrn)
749
        *extra_rtrn = 0;
750
    for (p = dpy->key_bindings; p; p = p->next) {
751 752 753 754 755 756 757 758 759 760 761 762 763
        if (((mods & AllMods) == p->state) && (sym == p->key)) {
            int tmp = p->len;

            if (tmp > nbytes) {
                if (extra_rtrn)
                    *extra_rtrn = (tmp - nbytes);
                tmp = nbytes;
            }
            memcpy(buffer, p->string, tmp);
            if (tmp < nbytes)
                buffer[tmp] = '\0';
            return tmp;
        }
764 765 766 767 768
    }
    return 0;
}

char
769
XkbToControl(char ch)
770 771
{
    register char c = ch;
772

773 774 775 776 777 778 779 780 781 782
    if ((c >= '@' && c < '\177') || c == ' ')
        c &= 0x1F;
    else if (c == '2')
        c = '\000';
    else if (c >= '3' && c <= '7')
        c -= ('3' - '\033');
    else if (c == '8')
        c = '\177';
    else if (c == '/')
        c = '_' & 0x1F;
783 784
    return c;
}