11ab64890Smrg/*
21ab64890Smrg
31ab64890SmrgCopyright 1985, 1987, 1994, 1998  The Open Group
41ab64890Smrg
51ab64890SmrgPermission to use, copy, modify, distribute, and sell this software and its
61ab64890Smrgdocumentation for any purpose is hereby granted without fee, provided that
71ab64890Smrgthe above copyright notice appear in all copies and that both that
81ab64890Smrgcopyright notice and this permission notice appear in supporting
91ab64890Smrgdocumentation.
101ab64890Smrg
111ab64890SmrgThe above copyright notice and this permission notice shall be included
121ab64890Smrgin all copies or substantial portions of the Software.
131ab64890Smrg
141ab64890SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151ab64890SmrgOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161ab64890SmrgMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
171ab64890SmrgIN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
181ab64890SmrgOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
191ab64890SmrgARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
201ab64890SmrgOTHER DEALINGS IN THE SOFTWARE.
211ab64890Smrg
221ab64890SmrgExcept as contained in this notice, the name of The Open Group shall
231ab64890Smrgnot be used in advertising or otherwise to promote the sale, use or
241ab64890Smrgother dealings in this Software without prior written authorization
251ab64890Smrgfrom The Open Group.
261ab64890Smrg
271ab64890Smrg*/
281ab64890Smrg
29818534a1Smrg        /* the new monsters ate the old ones */
301ab64890Smrg
311ab64890Smrg#ifdef HAVE_CONFIG_H
321ab64890Smrg#include <config.h>
331ab64890Smrg#endif
341ab64890Smrg#include "XKBlib.h"
351ab64890Smrg#include <X11/Xlibint.h>
361ab64890Smrg#include <X11/Xutil.h>
371ab64890Smrg#include <X11/keysym.h>
381ab64890Smrg#include <stdio.h>
391ab64890Smrg#include <ctype.h>
401ab64890Smrg
411ab64890Smrg#include <X11/extensions/XKBproto.h>
421ab64890Smrg#include "XKBlibint.h"
431ab64890Smrg
441ab64890Smrg#define AllMods (ShiftMask|LockMask|ControlMask| \
45818534a1Smrg                 Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask)
461ab64890Smrg
47818534a1Smrgstatic int _XkbLoadDpy(Display *dpy);
481ab64890Smrg
491ab64890Smrgstruct _XKeytrans {
50818534a1Smrg    struct _XKeytrans *next;    /* next on list */
51818534a1Smrg    char *string;               /* string to return when the time comes */
52818534a1Smrg    int len;                    /* length of string (since NULL is legit) */
53818534a1Smrg    KeySym key;                 /* keysym rebound */
54818534a1Smrg    unsigned int state;         /* modifier state */
55818534a1Smrg    KeySym *modifiers;          /* modifier keysyms you want */
56818534a1Smrg    int mlen;                   /* length of modifier list */
571ab64890Smrg};
581ab64890Smrg
591ab64890SmrgKeySym
601ab64890SmrgXkbKeycodeToKeysym(Display *dpy,
611ab64890Smrg#if NeedWidePrototypes
62818534a1Smrg                   unsigned int kc,
631ab64890Smrg#else
64818534a1Smrg                   KeyCode kc,
651ab64890Smrg#endif
66818534a1Smrg                   int group,
67818534a1Smrg                   int level)
681ab64890Smrg{
69818534a1Smrg    XkbDescRec *xkb;
7061b2299dSmrg
711ab64890Smrg    if (_XkbUnavailable(dpy))
72818534a1Smrg        return NoSymbol;
731ab64890Smrg
74818534a1Smrg    _XkbCheckPendingRefresh(dpy, dpy->xkb_info);
751ab64890Smrg
761ab64890Smrg    xkb = dpy->xkb_info->desc;
77818534a1Smrg    if ((kc < xkb->min_key_code) || (kc > xkb->max_key_code))
78818534a1Smrg        return NoSymbol;
79818534a1Smrg
80818534a1Smrg    if ((group < 0) || (level < 0) || (group >= XkbKeyNumGroups(xkb, kc)))
81818534a1Smrg        return NoSymbol;
82818534a1Smrg    if (level >= XkbKeyGroupWidth(xkb, kc, group)) {
83818534a1Smrg        /* for compatibility with the core protocol, _always_ allow  */
84818534a1Smrg        /* two symbols in the first two groups.   If either of the   */
85818534a1Smrg        /* two is of type ONE_LEVEL, just replicate the first symbol */
86818534a1Smrg        if ((group > XkbGroup2Index) || (XkbKeyGroupWidth(xkb, kc, group) != 1)
87818534a1Smrg            || (level != 1)) {
88818534a1Smrg            return NoSymbol;
89818534a1Smrg        }
90818534a1Smrg        level = 0;
911ab64890Smrg    }
92818534a1Smrg    return XkbKeySymEntry(xkb, kc, level, group);
931ab64890Smrg}
941ab64890Smrg
951ab64890SmrgKeySym
961ab64890SmrgXKeycodeToKeysym(Display *dpy,
971ab64890Smrg#if NeedWidePrototypes
98818534a1Smrg                 unsigned int kc,
991ab64890Smrg#else
100818534a1Smrg                 KeyCode kc,
1011ab64890Smrg#endif
102818534a1Smrg                 int col)
1031ab64890Smrg{
104818534a1Smrg    XkbDescRec *xkb;
10561b2299dSmrg
1061ab64890Smrg    if (_XkbUnavailable(dpy))
107818534a1Smrg        return _XKeycodeToKeysym(dpy, kc, col);
1081ab64890Smrg
109818534a1Smrg    _XkbCheckPendingRefresh(dpy, dpy->xkb_info);
1101ab64890Smrg
1111ab64890Smrg    xkb = dpy->xkb_info->desc;
112818534a1Smrg    if ((kc < xkb->min_key_code) || (kc > xkb->max_key_code))
113818534a1Smrg        return NoSymbol;
114818534a1Smrg
115818534a1Smrg    if (col > 3) {
1165efbdfc3Smrg        int firstSym, nGrp, grp;
117818534a1Smrg
1185efbdfc3Smrg        firstSym = 4;
119818534a1Smrg        nGrp = XkbKeyNumGroups(xkb, kc);
1205efbdfc3Smrg        for (grp = 0; grp < nGrp; grp++) {
1215efbdfc3Smrg            int width = XkbKeyGroupWidth(xkb, kc, grp);
1225efbdfc3Smrg            int skip = 0;
1235efbdfc3Smrg            if (grp < 2) {
1245efbdfc3Smrg                /* Skip the first two symbols in the first two groups, since we
1255efbdfc3Smrg                 * return them below for indexes 0-3. */
1265efbdfc3Smrg                skip = 2;
1275efbdfc3Smrg                width -= skip;
1285efbdfc3Smrg                if (width < 0)
1295efbdfc3Smrg                    width = 0;
1305efbdfc3Smrg            }
1315efbdfc3Smrg            if (col < firstSym + width)
1325efbdfc3Smrg                return XkbKeycodeToKeysym(dpy, kc, grp, col - firstSym + skip);
1335efbdfc3Smrg            firstSym += width;
134818534a1Smrg        }
135818534a1Smrg        return NoSymbol;
1361ab64890Smrg    }
137818534a1Smrg    return XkbKeycodeToKeysym(dpy, kc, (col >> 1), (col & 1));
1381ab64890Smrg}
1391ab64890Smrg
1401ab64890SmrgKeyCode
1411ab64890SmrgXKeysymToKeycode(Display *dpy, KeySym ks)
1421ab64890Smrg{
1431ab64890Smrg    register int i, j, gotOne;
1441ab64890Smrg
1451ab64890Smrg    if (_XkbUnavailable(dpy))
146818534a1Smrg        return _XKeysymToKeycode(dpy, ks);
147818534a1Smrg    _XkbCheckPendingRefresh(dpy, dpy->xkb_info);
1481ab64890Smrg
149818534a1Smrg    j = 0;
1501ab64890Smrg    do {
151818534a1Smrg        register XkbDescRec *xkb = dpy->xkb_info->desc;
152818534a1Smrg        gotOne = 0;
153818534a1Smrg        for (i = dpy->min_keycode; i <= dpy->max_keycode; i++) {
154818534a1Smrg            if (j < (int) XkbKeyNumSyms(xkb, i)) {
155818534a1Smrg                gotOne = 1;
156818534a1Smrg                if ((XkbKeySym(xkb, i, j) == ks))
157818534a1Smrg                    return i;
158818534a1Smrg            }
159818534a1Smrg        }
160818534a1Smrg        j++;
1611ab64890Smrg    } while (gotOne);
1621ab64890Smrg    return 0;
1631ab64890Smrg}
1641ab64890Smrg
1651ab64890Smrgstatic int
1661ab64890Smrg_XkbComputeModmap(Display *dpy)
1671ab64890Smrg{
168818534a1Smrg    register XkbDescPtr xkb;
1691ab64890Smrg
170818534a1Smrg    xkb = dpy->xkb_info->desc;
171818534a1Smrg    if (XkbGetUpdatedMap(dpy, XkbModifierMapMask, xkb) == Success)
172818534a1Smrg        return 1;
1731ab64890Smrg    return 0;
1741ab64890Smrg}
1751ab64890Smrg
1761ab64890Smrgunsigned
177818534a1SmrgXkbKeysymToModifiers(Display *dpy, KeySym ks)
1781ab64890Smrg{
1791ab64890Smrg    XkbDescRec *xkb;
180818534a1Smrg    register int i, j;
1811ab64890Smrg    register KeySym *pSyms;
1821ab64890Smrg    CARD8 mods;
1831ab64890Smrg
1841ab64890Smrg    if (_XkbUnavailable(dpy))
185818534a1Smrg        return _XKeysymToModifiers(dpy, ks);
186818534a1Smrg    _XkbCheckPendingRefresh(dpy, dpy->xkb_info);
187818534a1Smrg
188818534a1Smrg    if (_XkbNeedModmap(dpy->xkb_info) && (!_XkbComputeModmap(dpy)))
189818534a1Smrg        return _XKeysymToModifiers(dpy, ks);
190818534a1Smrg
191818534a1Smrg    xkb = dpy->xkb_info->desc;
192818534a1Smrg    mods = 0;
193818534a1Smrg    for (i = xkb->min_key_code; i <= (int) xkb->max_key_code; i++) {
194818534a1Smrg        pSyms = XkbKeySymsPtr(xkb, i);
195818534a1Smrg        for (j = XkbKeyNumSyms(xkb, i) - 1; j >= 0; j--) {
196818534a1Smrg            if (pSyms[j] == ks) {
197818534a1Smrg                mods |= xkb->map->modmap[i];
198818534a1Smrg                break;
199818534a1Smrg            }
200818534a1Smrg        }
2011ab64890Smrg    }
2021ab64890Smrg    return mods;
2031ab64890Smrg}
2041ab64890Smrg
2050efe039aSmartin#ifdef __clang__
2060efe039aSmartin#pragma clang diagnostic push
2070efe039aSmartin#pragma clang diagnostic ignored "-Wdeprecated-declarations"
2080efe039aSmartin#elif defined(__GNUC__)
2090efe039aSmartin#pragma GCC diagnostic push
2100efe039aSmartin#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2110efe039aSmartin#endif
2120efe039aSmartin
2131ab64890SmrgKeySym
214818534a1SmrgXLookupKeysym(register XKeyEvent * event, int col)
2151ab64890Smrg{
2161ab64890Smrg    Display *dpy = event->display;
217818534a1Smrg
2181ab64890Smrg    if (_XkbUnavailable(dpy))
219818534a1Smrg        return _XLookupKeysym(event, col);
220818534a1Smrg    _XkbCheckPendingRefresh(dpy, dpy->xkb_info);
221d4a3aaf4Smrg
2221ab64890Smrg    return XKeycodeToKeysym(dpy, event->keycode, col);
2230efe039aSmartin}
2240efe039aSmartin
225d4a3aaf4Smrg#ifdef __clang__
226d4a3aaf4Smrg#pragma clang diagnostic pop
227d4a3aaf4Smrg#elif defined(__GNUC__)
228d4a3aaf4Smrg#pragma GCC diagnostic pop
229d4a3aaf4Smrg#endif
230d4a3aaf4Smrg
2311ab64890Smrg   /*
23261b2299dSmrg    * Not a public entry point -- XkbTranslateKey is an obsolete name
23361b2299dSmrg    * that is preserved here so that functions linked against the old
2341ab64890Smrg    * version will continue to work in a shared library environment.
2351ab64890Smrg    */
2361ab64890Smrgint
237818534a1SmrgXkbTranslateKey(register Display *dpy,
238818534a1Smrg                KeyCode key,
239818534a1Smrg                register unsigned int mods,
240818534a1Smrg                unsigned int *mods_rtrn,
241818534a1Smrg                KeySym *keysym_rtrn);
242818534a1Smrg
2431ab64890Smrgint
244818534a1SmrgXkbTranslateKey(register Display *dpy,
245818534a1Smrg                KeyCode key,
246818534a1Smrg                register unsigned int mods,
247818534a1Smrg                unsigned int *mods_rtrn,
248818534a1Smrg                KeySym *keysym_rtrn)
2491ab64890Smrg{
250818534a1Smrg    return XkbLookupKeySym(dpy, key, mods, mods_rtrn, keysym_rtrn);
2511ab64890Smrg}
2521ab64890Smrg
2531ab64890SmrgBool
254818534a1SmrgXkbLookupKeySym(register Display *dpy,
255818534a1Smrg                KeyCode key,
256818534a1Smrg                register unsigned int mods,
257818534a1Smrg                unsigned int *mods_rtrn,
258818534a1Smrg                KeySym *keysym_rtrn)
2591ab64890Smrg{
2601ab64890Smrg    if (_XkbUnavailable(dpy))
261818534a1Smrg        return _XTranslateKey(dpy, key, mods, mods_rtrn, keysym_rtrn);
262818534a1Smrg    _XkbCheckPendingRefresh(dpy, dpy->xkb_info);
263818534a1Smrg    return XkbTranslateKeyCode(dpy->xkb_info->desc, key, mods, mods_rtrn,
264818534a1Smrg                               keysym_rtrn);
2651ab64890Smrg}
2661ab64890Smrg
2671ab64890SmrgBool
268818534a1SmrgXkbTranslateKeyCode(register XkbDescPtr xkb,
269818534a1Smrg                    KeyCode key,
270818534a1Smrg                    register unsigned int mods,
271818534a1Smrg                    unsigned int *mods_rtrn,
272818534a1Smrg                    KeySym *keysym_rtrn)
2731ab64890Smrg{
2741ab64890Smrg    XkbKeyTypeRec *type;
275818534a1Smrg    int col, nKeyGroups;
276818534a1Smrg    unsigned preserve, effectiveGroup;
2771ab64890Smrg    KeySym *syms;
2781ab64890Smrg
279818534a1Smrg    if (mods_rtrn != NULL)
280818534a1Smrg        *mods_rtrn = 0;
2811ab64890Smrg
282818534a1Smrg    nKeyGroups = XkbKeyNumGroups(xkb, key);
283818534a1Smrg    if ((!XkbKeycodeInRange(xkb, key)) || (nKeyGroups == 0)) {
284818534a1Smrg        if (keysym_rtrn != NULL)
285818534a1Smrg            *keysym_rtrn = NoSymbol;
286818534a1Smrg        return False;
2871ab64890Smrg    }
2881ab64890Smrg
289818534a1Smrg    syms = XkbKeySymsPtr(xkb, key);
2901ab64890Smrg
2911ab64890Smrg    /* find the offset of the effective group */
2921ab64890Smrg    col = 0;
293818534a1Smrg    effectiveGroup = XkbGroupForCoreState(mods);
294818534a1Smrg    if (effectiveGroup >= nKeyGroups) {
295818534a1Smrg        unsigned groupInfo = XkbKeyGroupInfo(xkb, key);
296818534a1Smrg
297818534a1Smrg        switch (XkbOutOfRangeGroupAction(groupInfo)) {
298818534a1Smrg        default:
299818534a1Smrg            effectiveGroup %= nKeyGroups;
300818534a1Smrg            break;
301818534a1Smrg        case XkbClampIntoRange:
302818534a1Smrg            effectiveGroup = nKeyGroups - 1;
303818534a1Smrg            break;
304818534a1Smrg        case XkbRedirectIntoRange:
305818534a1Smrg            effectiveGroup = XkbOutOfRangeGroupNumber(groupInfo);
306818534a1Smrg            if (effectiveGroup >= nKeyGroups)
307818534a1Smrg                effectiveGroup = 0;
308818534a1Smrg            break;
309818534a1Smrg        }
3101ab64890Smrg    }
311818534a1Smrg    col = effectiveGroup * XkbKeyGroupsWidth(xkb, key);
312818534a1Smrg    type = XkbKeyKeyType(xkb, key, effectiveGroup);
313818534a1Smrg
314818534a1Smrg    preserve = 0;
315818534a1Smrg    if (type->map) {  /* find the column (shift level) within the group */
316818534a1Smrg        register int i;
317818534a1Smrg        register XkbKTMapEntryPtr entry;
318818534a1Smrg
319818534a1Smrg        for (i = 0, entry = type->map; i < type->map_count; i++, entry++) {
320818534a1Smrg            if ((entry->active) &&
321818534a1Smrg                ((mods & type->mods.mask) == entry->mods.mask)) {
322818534a1Smrg                col += entry->level;
323818534a1Smrg                if (type->preserve)
324818534a1Smrg                    preserve = type->preserve[i].mask;
325818534a1Smrg                break;
326818534a1Smrg            }
327818534a1Smrg        }
3281ab64890Smrg    }
3291ab64890Smrg
330818534a1Smrg    if (keysym_rtrn != NULL)
331818534a1Smrg        *keysym_rtrn = syms[col];
3321ab64890Smrg    if (mods_rtrn) {
333818534a1Smrg        *mods_rtrn = type->mods.mask & (~preserve);
334818534a1Smrg        /* The Motif VTS doesn't get the help callback called if help
335818534a1Smrg         * is bound to Shift+<whatever>, and it appears as though it
336818534a1Smrg         * is XkbTranslateKeyCode that is causing the problem.  The
337818534a1Smrg         * core X version of XTranslateKey always OR's in ShiftMask
338818534a1Smrg         * and LockMask for mods_rtrn, so this "fix" keeps this behavior
339818534a1Smrg         * and solves the VTS problem.
340818534a1Smrg         */
341818534a1Smrg        if ((xkb->dpy) && (xkb->dpy->xkb_info) &&
342818534a1Smrg            (xkb->dpy->xkb_info->
343818534a1Smrg             xlib_ctrls & XkbLC_AlwaysConsumeShiftAndLock)) {
344818534a1Smrg            *mods_rtrn |= (ShiftMask | LockMask);
345818534a1Smrg        }
3461ab64890Smrg    }
347818534a1Smrg    return (syms[col] != NoSymbol);
3481ab64890Smrg}
3491ab64890Smrg
3501ab64890SmrgStatus
351818534a1SmrgXkbRefreshKeyboardMapping(register XkbMapNotifyEvent * event)
3521ab64890Smrg{
3531ab64890Smrg    Display *dpy = event->display;
3541ab64890Smrg    XkbInfoPtr xkbi;
3551ab64890Smrg
3561ab64890Smrg    if (_XkbUnavailable(dpy)) {
357818534a1Smrg        _XRefreshKeyboardMapping((XMappingEvent *) event);
358818534a1Smrg        return Success;
3591ab64890Smrg    }
360818534a1Smrg    xkbi = dpy->xkb_info;
3611ab64890Smrg
362818534a1Smrg    if (((event->type & 0x7f) - xkbi->codes->first_event) != XkbEventCode)
363818534a1Smrg        return BadMatch;
364818534a1Smrg    if (event->xkb_type == XkbNewKeyboardNotify) {
365818534a1Smrg        _XkbReloadDpy(dpy);
366818534a1Smrg        return Success;
3671ab64890Smrg    }
368818534a1Smrg    if (event->xkb_type == XkbMapNotify) {
369818534a1Smrg        XkbMapChangesRec changes;
370818534a1Smrg        Status rtrn;
371818534a1Smrg
372818534a1Smrg        if (xkbi->flags & XkbMapPending)
373818534a1Smrg            changes = xkbi->changes;
374818534a1Smrg        else
375818534a1Smrg            bzero(&changes, sizeof(changes));
376818534a1Smrg        XkbNoteMapChanges(&changes, event, XKB_XLIB_MAP_MASK);
377818534a1Smrg        if ((rtrn = XkbGetMapChanges(dpy, xkbi->desc, &changes)) != Success) {
3781ab64890Smrg#ifdef DEBUG
379818534a1Smrg            fprintf(stderr, "Internal Error! XkbGetMapChanges failed:\n");
3801ab64890Smrg#endif
381818534a1Smrg            xkbi->changes = changes;
382818534a1Smrg        }
383818534a1Smrg        else if (xkbi->flags & XkbMapPending) {
384818534a1Smrg            xkbi->flags &= ~XkbMapPending;
385818534a1Smrg            bzero(&xkbi->changes, sizeof(XkbMapChangesRec));
386818534a1Smrg        }
387818534a1Smrg        return rtrn;
3881ab64890Smrg    }
3891ab64890Smrg    return BadMatch;
3901ab64890Smrg}
3911ab64890Smrg
3921ab64890Smrgint
393818534a1SmrgXRefreshKeyboardMapping(register XMappingEvent * event)
3941ab64890Smrg{
395818534a1Smrg    XkbEvent *xkbevent = (XkbEvent *) event;
3961ab64890Smrg    Display *dpy = event->display;
3971ab64890Smrg    XkbMapChangesRec changes;
3981ab64890Smrg    XkbInfoPtr xkbi;
3991ab64890Smrg
4001ab64890Smrg    /* always do this for input methods, which still use the old keymap */
4011ab64890Smrg    (void) _XRefreshKeyboardMapping(event);
4021ab64890Smrg
4031ab64890Smrg    if (_XkbUnavailable(dpy))
404818534a1Smrg        return 1;
4051ab64890Smrg
4061ab64890Smrg    xkbi = dpy->xkb_info;
4071ab64890Smrg
408818534a1Smrg    if (((event->type & 0x7f) - xkbi->codes->first_event) == XkbEventCode)
409818534a1Smrg        return XkbRefreshKeyboardMapping(&xkbevent->map);
4101ab64890Smrg
411818534a1Smrg    if (xkbi->flags & XkbXlibNewKeyboard) {
412818534a1Smrg        _XkbReloadDpy(dpy);
413818534a1Smrg        return 1;
4141ab64890Smrg    }
4151ab64890Smrg
416818534a1Smrg    if ((xkbi->flags & XkbMapPending) || (event->request == MappingKeyboard)) {
417818534a1Smrg        if (xkbi->flags & XkbMapPending) {
418818534a1Smrg            changes = xkbi->changes;
419818534a1Smrg            _XkbNoteCoreMapChanges(&changes, event, XKB_XLIB_MAP_MASK);
420818534a1Smrg        }
421818534a1Smrg        else {
422818534a1Smrg            bzero(&changes, sizeof(changes));
423818534a1Smrg            changes.changed = XkbKeySymsMask;
424818534a1Smrg            if (xkbi->desc->min_key_code < xkbi->desc->max_key_code) {
425818534a1Smrg                changes.first_key_sym = xkbi->desc->min_key_code;
426818534a1Smrg                changes.num_key_syms = xkbi->desc->max_key_code -
427818534a1Smrg                    xkbi->desc->min_key_code + 1;
428818534a1Smrg            }
429818534a1Smrg            else {
430818534a1Smrg                changes.first_key_sym = event->first_keycode;
431818534a1Smrg                changes.num_key_syms = event->count;
432818534a1Smrg            }
433818534a1Smrg        }
434818534a1Smrg
435818534a1Smrg        if (XkbGetMapChanges(dpy, xkbi->desc, &changes) != Success) {
4361ab64890Smrg#ifdef DEBUG
437818534a1Smrg            fprintf(stderr, "Internal Error! XkbGetMapChanges failed:\n");
438818534a1Smrg            if (changes.changed & XkbKeyTypesMask) {
439818534a1Smrg                int first = changes.first_type;
440818534a1Smrg                int last = changes.first_type + changes.num_types - 1;
441818534a1Smrg
442818534a1Smrg                fprintf(stderr, "       types:  %d..%d\n", first, last);
443818534a1Smrg            }
444818534a1Smrg            if (changes.changed & XkbKeySymsMask) {
445818534a1Smrg                int first = changes.first_key_sym;
446818534a1Smrg                int last = changes.first_key_sym + changes.num_key_syms - 1;
447818534a1Smrg
448818534a1Smrg                fprintf(stderr, "     symbols:  %d..%d\n", first, last);
449818534a1Smrg            }
450818534a1Smrg            if (changes.changed & XkbKeyActionsMask) {
451818534a1Smrg                int first = changes.first_key_act;
452818534a1Smrg                int last = changes.first_key_act + changes.num_key_acts - 1;
453818534a1Smrg
454818534a1Smrg                fprintf(stderr, "     acts:  %d..%d\n", first, last);
455818534a1Smrg            }
456818534a1Smrg            if (changes.changed & XkbKeyBehaviorsMask) {
457818534a1Smrg                int first = changes.first_key_behavior;
458818534a1Smrg                int last = first + changes.num_key_behaviors - 1;
459818534a1Smrg
460818534a1Smrg                fprintf(stderr, "   behaviors:  %d..%d\n", first, last);
461818534a1Smrg            }
462818534a1Smrg            if (changes.changed & XkbVirtualModsMask) {
463818534a1Smrg                fprintf(stderr, "virtual mods: 0x%04x\n", changes.vmods);
464818534a1Smrg            }
465818534a1Smrg            if (changes.changed & XkbExplicitComponentsMask) {
466818534a1Smrg                int first = changes.first_key_explicit;
467818534a1Smrg                int last = first + changes.num_key_explicit - 1;
468818534a1Smrg
469818534a1Smrg                fprintf(stderr, "    explicit:  %d..%d\n", first, last);
470818534a1Smrg            }
4711ab64890Smrg#endif
472818534a1Smrg        }
473818534a1Smrg        LockDisplay(dpy);
474818534a1Smrg        if (xkbi->flags & XkbMapPending) {
475818534a1Smrg            xkbi->flags &= ~XkbMapPending;
476818534a1Smrg            bzero(&xkbi->changes, sizeof(XkbMapChangesRec));
477818534a1Smrg        }
478818534a1Smrg        UnlockDisplay(dpy);
4791ab64890Smrg    }
480818534a1Smrg    if (event->request == MappingModifier) {
481818534a1Smrg        LockDisplay(dpy);
482818534a1Smrg        if (xkbi->desc->map->modmap) {
483818534a1Smrg            _XkbFree(xkbi->desc->map->modmap);
484818534a1Smrg            xkbi->desc->map->modmap = NULL;
485818534a1Smrg        }
486818534a1Smrg        if (dpy->key_bindings) {
487818534a1Smrg            register struct _XKeytrans *p;
488818534a1Smrg
489818534a1Smrg            for (p = dpy->key_bindings; p; p = p->next) {
490818534a1Smrg                register int i;
491818534a1Smrg
492818534a1Smrg                p->state = 0;
493818534a1Smrg                if (p->mlen > 0) {
494818534a1Smrg                    for (i = 0; i < p->mlen; i++) {
495818534a1Smrg                        p->state |= XkbKeysymToModifiers(dpy, p->modifiers[i]);
496818534a1Smrg                    }
497818534a1Smrg                    if (p->state)
498818534a1Smrg                        p->state &= AllMods;
499818534a1Smrg                    else
500818534a1Smrg                        p->state = AnyModifier;
501818534a1Smrg                }
502818534a1Smrg            }
503818534a1Smrg        }
504818534a1Smrg        UnlockDisplay(dpy);
5051ab64890Smrg    }
5061ab64890Smrg    return 1;
5071ab64890Smrg}
5081ab64890Smrg
5091ab64890Smrgstatic int
5101ab64890Smrg_XkbLoadDpy(Display *dpy)
5111ab64890Smrg{
5121ab64890Smrg    XkbInfoPtr xkbi;
513818534a1Smrg    unsigned query, oldEvents;
5141ab64890Smrg    XkbDescRec *desc;
5151ab64890Smrg
516818534a1Smrg    if (!XkbUseExtension(dpy, NULL, NULL))
517818534a1Smrg        return 0;
5181ab64890Smrg
5191ab64890Smrg    xkbi = dpy->xkb_info;
5201ab64890Smrg    query = XkbAllClientInfoMask;
521818534a1Smrg    desc = XkbGetMap(dpy, query, XkbUseCoreKbd);
5221ab64890Smrg    if (!desc) {
5231ab64890Smrg#ifdef DEBUG
524818534a1Smrg        fprintf(stderr, "Warning! XkbGetMap failed!\n");
5251ab64890Smrg#endif
526818534a1Smrg        return 0;
5271ab64890Smrg    }
5281ab64890Smrg    LockDisplay(dpy);
5291ab64890Smrg    xkbi->desc = desc;
5301ab64890Smrg
5311ab64890Smrg    UnlockDisplay(dpy);
532818534a1Smrg    oldEvents = xkbi->selected_events;
533818534a1Smrg    if (!(xkbi->xlib_ctrls & XkbLC_IgnoreNewKeyboards)) {
534818534a1Smrg        XkbSelectEventDetails(dpy, xkbi->desc->device_spec,
535818534a1Smrg                              XkbNewKeyboardNotify,
536818534a1Smrg                              XkbNKN_KeycodesMask | XkbNKN_DeviceIDMask,
537818534a1Smrg                              XkbNKN_KeycodesMask | XkbNKN_DeviceIDMask);
5381ab64890Smrg    }
539818534a1Smrg    XkbSelectEventDetails(dpy, xkbi->desc->device_spec, XkbMapNotify,
540818534a1Smrg                          XkbAllClientInfoMask, XkbAllClientInfoMask);
5411ab64890Smrg    LockDisplay(dpy);
542818534a1Smrg    xkbi->selected_events = oldEvents;
5431ab64890Smrg    UnlockDisplay(dpy);
5441ab64890Smrg    return 1;
5451ab64890Smrg}
5461ab64890Smrg
5471ab64890Smrgvoid
5481ab64890Smrg_XkbReloadDpy(Display *dpy)
5491ab64890Smrg{
5501ab64890Smrg    XkbInfoPtr xkbi;
5511ab64890Smrg    XkbDescRec *desc;
552818534a1Smrg    unsigned oldDeviceID;
5531ab64890Smrg
5541ab64890Smrg    if (_XkbUnavailable(dpy))
555818534a1Smrg        return;
5561ab64890Smrg
5571ab64890Smrg    xkbi = dpy->xkb_info;
5581ab64890Smrg    LockDisplay(dpy);
5591ab64890Smrg    if (xkbi->desc) {
560818534a1Smrg        oldDeviceID = xkbi->desc->device_spec;
561818534a1Smrg        XkbFreeKeyboard(xkbi->desc, XkbAllComponentsMask, True);
562818534a1Smrg        xkbi->desc = NULL;
563818534a1Smrg        xkbi->flags &= ~(XkbMapPending | XkbXlibNewKeyboard);
564818534a1Smrg        xkbi->changes.changed = 0;
5651ab64890Smrg    }
566818534a1Smrg    else
567818534a1Smrg        oldDeviceID = XkbUseCoreKbd;
5681ab64890Smrg    UnlockDisplay(dpy);
569818534a1Smrg    desc = XkbGetMap(dpy, XkbAllClientInfoMask, XkbUseCoreKbd);
5701ab64890Smrg    if (!desc)
571818534a1Smrg        return;
5721ab64890Smrg    LockDisplay(dpy);
5731ab64890Smrg    xkbi->desc = desc;
5741ab64890Smrg    UnlockDisplay(dpy);
5751ab64890Smrg
576818534a1Smrg    if (desc->device_spec != oldDeviceID) {
577818534a1Smrg        /* transfer(?) event masks here */
5781ab64890Smrg#ifdef NOTYET
579818534a1Smrg        unsigned oldEvents;
580818534a1Smrg
581818534a1Smrg        oldEvents = xkbi->selected_events;
582818534a1Smrg        XkbSelectEventDetails(dpy, xkbi->desc->device_spec, XkbMapNotify,
583818534a1Smrg                              XkbAllMapComponentsMask, XkbAllClientInfoMask);
584818534a1Smrg        LockDisplay(dpy);
585818534a1Smrg        xkbi->selected_events = oldEvents;
586818534a1Smrg        UnlockDisplay(dpy);
5871ab64890Smrg#endif
5881ab64890Smrg    }
5891ab64890Smrg    return;
5901ab64890Smrg}
5911ab64890Smrg
5921ab64890Smrgint
593d7c63e31SmrgXkbTranslateKeySym(Display *dpy,
594d7c63e31Smrg                   KeySym *sym_rtrn,
595818534a1Smrg                   unsigned int mods,
596818534a1Smrg                   char *buffer,
597818534a1Smrg                   int nbytes,
598818534a1Smrg                   int *extra_rtrn)
5991ab64890Smrg{
600818534a1Smrg    register XkbInfoPtr xkb;
6011ab64890Smrg    XkbKSToMBFunc cvtr;
6021ab64890Smrg    XPointer priv;
6031ab64890Smrg    char tmp[4];
6041ab64890Smrg    int n;
6051ab64890Smrg
606818534a1Smrg    xkb = dpy->xkb_info;
6071ab64890Smrg    if (!xkb->cvt.KSToMB) {
608818534a1Smrg        _XkbGetConverters(_XkbGetCharset(), &xkb->cvt);
609818534a1Smrg        _XkbGetConverters("ISO8859-1", &xkb->latin1cvt);
6101ab64890Smrg    }
6111ab64890Smrg
6121ab64890Smrg    if (extra_rtrn)
613818534a1Smrg        *extra_rtrn = 0;
6141ab64890Smrg
615818534a1Smrg    if ((buffer == NULL) || (nbytes == 0)) {
616818534a1Smrg        buffer = tmp;
617818534a1Smrg        nbytes = 4;
6181ab64890Smrg    }
6191ab64890Smrg
6201ab64890Smrg    /* see if symbol rebound, if so, return that string. */
621818534a1Smrg    n = XkbLookupKeyBinding(dpy, *sym_rtrn, mods, buffer, nbytes, extra_rtrn);
6221ab64890Smrg    if (n)
6231ab64890Smrg        return n;
6241ab64890Smrg
625818534a1Smrg    if (nbytes > 0)
626818534a1Smrg        buffer[0] = '\0';
6271ab64890Smrg
628818534a1Smrg    if (xkb->cvt.KSToUpper && (mods & LockMask)) {
629818534a1Smrg        *sym_rtrn = (*xkb->cvt.KSToUpper) (*sym_rtrn);
6301ab64890Smrg    }
6311ab64890Smrg    if (xkb->xlib_ctrls & XkbLC_ForceLatin1Lookup) {
632818534a1Smrg        cvtr = xkb->latin1cvt.KSToMB;
633818534a1Smrg        priv = xkb->latin1cvt.KSToMBPriv;
634818534a1Smrg    }
635818534a1Smrg    else {
636818534a1Smrg        cvtr = xkb->cvt.KSToMB;
637818534a1Smrg        priv = xkb->cvt.KSToMBPriv;
6381ab64890Smrg    }
6391ab64890Smrg
640818534a1Smrg    n = (*cvtr) (priv, *sym_rtrn, buffer, nbytes, extra_rtrn);
641818534a1Smrg
642818534a1Smrg    if ((!xkb->cvt.KSToUpper) && (mods & LockMask)) {
643818534a1Smrg        register int i;
644818534a1Smrg        int change;
645818534a1Smrg
646818534a1Smrg        for (i = change = 0; i < n; i++) {
647818534a1Smrg            char ch = toupper(buffer[i]);
648818534a1Smrg            change = (change || (buffer[i] != ch));
649818534a1Smrg            buffer[i] = ch;
650818534a1Smrg        }
651818534a1Smrg        if (change) {
652818534a1Smrg            if (n == 1)
653818534a1Smrg                *sym_rtrn =
654818534a1Smrg                    (*xkb->cvt.MBToKS) (xkb->cvt.MBToKSPriv, buffer, n, NULL);
655818534a1Smrg            else
656818534a1Smrg                *sym_rtrn = NoSymbol;
657818534a1Smrg        }
6581ab64890Smrg    }
6591ab64890Smrg
660818534a1Smrg    if (mods & ControlMask) {
661818534a1Smrg        if (n == 1) {
662818534a1Smrg            buffer[0] = XkbToControl(buffer[0]);
663818534a1Smrg            if (nbytes > 1)
664818534a1Smrg                buffer[1] = '\0';
665818534a1Smrg            return 1;
666818534a1Smrg        }
667818534a1Smrg        if (nbytes > 0)
668818534a1Smrg            buffer[0] = '\0';
669818534a1Smrg        return 0;
6701ab64890Smrg    }
6711ab64890Smrg    return n;
6721ab64890Smrg}
6731ab64890Smrg
6741ab64890Smrgint
675818534a1SmrgXLookupString(register XKeyEvent *event,
676818534a1Smrg              char *buffer,
677818534a1Smrg              int nbytes,
678818534a1Smrg              KeySym *keysym,
679818534a1Smrg              XComposeStatus *status)
6801ab64890Smrg{
681818534a1Smrg    KeySym dummy;
6821ab64890Smrg    int rtrnLen;
6831ab64890Smrg    unsigned int new_mods;
6841ab64890Smrg    Display *dpy = event->display;
6851ab64890Smrg
686818534a1Smrg    if (keysym == NULL)
687818534a1Smrg        keysym = &dummy;
688818534a1Smrg    if (!XkbLookupKeySym(dpy, event->keycode, event->state, &new_mods, keysym))
689818534a1Smrg        return 0;
690818534a1Smrg    new_mods = (event->state & (~new_mods));
6911ab64890Smrg
6921ab64890Smrg    /* find the group where a symbol can be converted to control one */
693818534a1Smrg    if (new_mods & ControlMask && *keysym > 0x7F &&
694818534a1Smrg        (dpy->xkb_info->xlib_ctrls & XkbLC_ControlFallback)) {
695818534a1Smrg        XKeyEvent tmp_ev = *event;
696818534a1Smrg        KeySym tmp_keysym;
697818534a1Smrg        unsigned int tmp_new_mods;
698818534a1Smrg
699818534a1Smrg        if (_XkbUnavailable(dpy)) {
700818534a1Smrg            tmp_ev.state = event->state ^ dpy->mode_switch;
7011ab64890Smrg            if (XkbLookupKeySym(dpy, tmp_ev.keycode, tmp_ev.state,
7021ab64890Smrg                                &tmp_new_mods, &tmp_keysym) &&
703818534a1Smrg                tmp_keysym != NoSymbol && tmp_keysym < 0x80) {
7041ab64890Smrg                *keysym = tmp_keysym;
7051ab64890Smrg            }
706818534a1Smrg        }
707818534a1Smrg        else {
7081ab64890Smrg            int n = XkbKeyNumGroups(dpy->xkb_info->desc, tmp_ev.keycode);
7091ab64890Smrg            int i;
710818534a1Smrg
7111ab64890Smrg            for (i = 0; i < n; i++) {
7121ab64890Smrg                if (XkbGroupForCoreState(event->state) == i)
7131ab64890Smrg                    continue;
714818534a1Smrg                tmp_ev.state = XkbBuildCoreState(tmp_ev.state, i);
7151ab64890Smrg                if (XkbLookupKeySym(dpy, tmp_ev.keycode, tmp_ev.state,
716818534a1Smrg                                    &tmp_new_mods, &tmp_keysym) &&
717818534a1Smrg                    tmp_keysym != NoSymbol && tmp_keysym < 0x80) {
7181ab64890Smrg                    *keysym = tmp_keysym;
719818534a1Smrg                    new_mods = (event->state & (~tmp_new_mods));
7201ab64890Smrg                    break;
7211ab64890Smrg                }
7221ab64890Smrg            }
7231ab64890Smrg        }
72461b2299dSmrg    }
7251ab64890Smrg
7261ab64890Smrg    /* We *should* use the new_mods (which does not contain any modifiers */
7271ab64890Smrg    /* that were used to compute the symbol here, but pre-XKB XLookupString */
7281ab64890Smrg    /* did not and we have to remain compatible.  Sigh. */
7291ab64890Smrg    if (_XkbUnavailable(dpy) ||
730818534a1Smrg        (dpy->xkb_info->xlib_ctrls & XkbLC_ConsumeLookupMods) == 0)
731818534a1Smrg        new_mods = event->state;
7321ab64890Smrg
733818534a1Smrg    rtrnLen = XkbLookupKeyBinding(dpy, *keysym, new_mods, buffer, nbytes, NULL);
734818534a1Smrg    if (rtrnLen > 0)
735818534a1Smrg        return rtrnLen;
7361ab64890Smrg
737818534a1Smrg    return XkbTranslateKeySym(dpy, keysym, new_mods, buffer, nbytes, NULL);
7381ab64890Smrg}
7391ab64890Smrg
7401ab64890Smrg
7411ab64890Smrgint
742818534a1SmrgXkbLookupKeyBinding(Display *dpy,
743818534a1Smrg                    register KeySym sym,
744818534a1Smrg                    unsigned int mods,
745818534a1Smrg                    char *buffer,
746818534a1Smrg                    int nbytes,
747818534a1Smrg                    int *extra_rtrn)
7481ab64890Smrg{
74961b2299dSmrg    register struct _XKeytrans *p;
7501ab64890Smrg
7511ab64890Smrg    if (extra_rtrn)
752818534a1Smrg        *extra_rtrn = 0;
7531ab64890Smrg    for (p = dpy->key_bindings; p; p = p->next) {
754818534a1Smrg        if (((mods & AllMods) == p->state) && (sym == p->key)) {
755818534a1Smrg            int tmp = p->len;
756818534a1Smrg
757818534a1Smrg            if (tmp > nbytes) {
758818534a1Smrg                if (extra_rtrn)
759818534a1Smrg                    *extra_rtrn = (tmp - nbytes);
760818534a1Smrg                tmp = nbytes;
761818534a1Smrg            }
7629c019ec5Smaya            memcpy(buffer, p->string, (size_t) tmp);
763818534a1Smrg            if (tmp < nbytes)
764818534a1Smrg                buffer[tmp] = '\0';
765818534a1Smrg            return tmp;
766818534a1Smrg        }
7671ab64890Smrg    }
7681ab64890Smrg    return 0;
7691ab64890Smrg}
7701ab64890Smrg
7711ab64890Smrgchar
772818534a1SmrgXkbToControl(char ch)
7731ab64890Smrg{
7741ab64890Smrg    register char c = ch;
77561b2299dSmrg
776818534a1Smrg    if ((c >= '@' && c < '\177') || c == ' ')
777818534a1Smrg        c &= 0x1F;
778818534a1Smrg    else if (c == '2')
779818534a1Smrg        c = '\000';
780818534a1Smrg    else if (c >= '3' && c <= '7')
781818534a1Smrg        c -= ('3' - '\033');
782818534a1Smrg    else if (c == '8')
783818534a1Smrg        c = '\177';
784818534a1Smrg    else if (c == '/')
785818534a1Smrg        c = '_' & 0x1F;
7861ab64890Smrg    return c;
7871ab64890Smrg}
788