XKBUse.c 30.1 KB
Newer Older
1 2 3 4 5 6 7 8
/************************************************************
Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.

Permission to use, copy, modify, and distribute this
software and its documentation for any purpose and without
fee is hereby granted, provided that the above copyright
notice appear in all copies and that both that copyright
notice and this permission notice appear in supporting
9 10
documentation, and that the name of Silicon Graphics not be
used in advertising or publicity pertaining to distribution
11
of the software without specific prior written permission.
12
Silicon Graphics makes no representation about the suitability
13 14 15
of this software for any purpose. It is provided "as is"
without any express or implied warranty.

16 17
SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18
AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19 20 21
GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22 23 24 25 26 27 28 29 30 31 32
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
THE USE OR PERFORMANCE OF THIS SOFTWARE.

********************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <ctype.h>
#include "Xlibint.h"
33
#include <nx-X11/extensions/XKBproto.h>
34 35
#include "XKBlibint.h"

36
static Bool _XkbIgnoreExtension = False;
37 38

void
39 40 41
XkbNoteMapChanges(XkbMapChangesPtr old,
                  XkbMapNotifyEvent *new,
                  unsigned wanted)
42
{
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    int first, oldLast, newLast;

    wanted &= new->changed;

    if (wanted & XkbKeyTypesMask) {
        if (old->changed & XkbKeyTypesMask) {
            first = old->first_type;
            oldLast = old->first_type + old->num_types - 1;
            newLast = new->first_type + new->num_types - 1;

            if (new->first_type < first)
                first = new->first_type;
            if (oldLast > newLast)
                newLast = oldLast;
            old->first_type = first;
            old->num_types = newLast - first + 1;
        }
        else {
            old->first_type = new->first_type;
            old->num_types = new->num_types;
        }
    }
    if (wanted & XkbKeySymsMask) {
        if (old->changed & XkbKeySymsMask) {
            first = old->first_key_sym;
            oldLast = old->first_key_sym + old->num_key_syms - 1;
            newLast = new->first_key_sym + new->num_key_syms - 1;

            if (new->first_key_sym < first)
                first = new->first_key_sym;
            if (oldLast > newLast)
                newLast = oldLast;
            old->first_key_sym = first;
            old->num_key_syms = newLast - first + 1;
        }
        else {
            old->first_key_sym = new->first_key_sym;
            old->num_key_syms = new->num_key_syms;
        }
    }
    if (wanted & XkbKeyActionsMask) {
        if (old->changed & XkbKeyActionsMask) {
            first = old->first_key_act;
            oldLast = old->first_key_act + old->num_key_acts - 1;
            newLast = new->first_key_act + new->num_key_acts - 1;

            if (new->first_key_act < first)
                first = new->first_key_act;
            if (oldLast > newLast)
                newLast = oldLast;
            old->first_key_act = first;
            old->num_key_acts = newLast - first + 1;
        }
        else {
            old->first_key_act = new->first_key_act;
            old->num_key_acts = new->num_key_acts;
        }
    }
    if (wanted & XkbKeyBehaviorsMask) {
        if (old->changed & XkbKeyBehaviorsMask) {
            first = old->first_key_behavior;
            oldLast = old->first_key_behavior + old->num_key_behaviors - 1;
            newLast = new->first_key_behavior + new->num_key_behaviors - 1;

            if (new->first_key_behavior < first)
                first = new->first_key_behavior;
            if (oldLast > newLast)
                newLast = oldLast;
            old->first_key_behavior = first;
            old->num_key_behaviors = newLast - first + 1;
        }
        else {
            old->first_key_behavior = new->first_key_behavior;
            old->num_key_behaviors = new->num_key_behaviors;
        }
    }
    if (wanted & XkbVirtualModsMask) {
        old->vmods |= new->vmods;
    }
    if (wanted & XkbExplicitComponentsMask) {
        if (old->changed & XkbExplicitComponentsMask) {
            first = old->first_key_explicit;
            oldLast = old->first_key_explicit + old->num_key_explicit - 1;
            newLast = new->first_key_explicit + new->num_key_explicit - 1;

            if (new->first_key_explicit < first)
                first = new->first_key_explicit;
            if (oldLast > newLast)
                newLast = oldLast;
            old->first_key_explicit = first;
            old->num_key_explicit = newLast - first + 1;
        }
        else {
            old->first_key_explicit = new->first_key_explicit;
            old->num_key_explicit = new->num_key_explicit;
        }
    }
    if (wanted & XkbModifierMapMask) {
        if (old->changed & XkbModifierMapMask) {
            first = old->first_modmap_key;
            oldLast = old->first_modmap_key + old->num_modmap_keys - 1;
            newLast = new->first_modmap_key + new->num_modmap_keys - 1;

            if (new->first_modmap_key < first)
                first = new->first_modmap_key;
            if (oldLast > newLast)
                newLast = oldLast;
            old->first_modmap_key = first;
            old->num_modmap_keys = newLast - first + 1;
        }
        else {
            old->first_modmap_key = new->first_modmap_key;
            old->num_modmap_keys = new->num_modmap_keys;
        }
    }
    if (wanted & XkbVirtualModMapMask) {
        if (old->changed & XkbVirtualModMapMask) {
            first = old->first_vmodmap_key;
            oldLast = old->first_vmodmap_key + old->num_vmodmap_keys - 1;
            newLast = new->first_vmodmap_key + new->num_vmodmap_keys - 1;

            if (new->first_vmodmap_key < first)
                first = new->first_vmodmap_key;
            if (oldLast > newLast)
                newLast = oldLast;
            old->first_vmodmap_key = first;
            old->num_vmodmap_keys = newLast - first + 1;
        }
        else {
            old->first_vmodmap_key = new->first_vmodmap_key;
            old->num_vmodmap_keys = new->num_vmodmap_keys;
        }
    }
    old->changed |= wanted;
177 178 179 180
    return;
}

void
181 182 183
_XkbNoteCoreMapChanges(XkbMapChangesPtr old,
                       XMappingEvent *new,
                       unsigned int wanted)
184
{
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
    int first, oldLast, newLast;

    if ((new->request == MappingKeyboard) && (wanted & XkbKeySymsMask)) {
        if (old->changed & XkbKeySymsMask) {
            first = old->first_key_sym;
            oldLast = old->first_key_sym + old->num_key_syms - 1;
            newLast = new->first_keycode + new->count - 1;

            if (new->first_keycode < first)
                first = new->first_keycode;
            if (oldLast > newLast)
                newLast = oldLast;
            old->first_key_sym = first;
            old->num_key_syms = newLast - first + 1;
        }
        else {
            old->changed |= XkbKeySymsMask;
            old->first_key_sym = new->first_keycode;
            old->num_key_syms = new->count;
        }
205 206 207 208 209
    }
    return;
}

static Bool
210
wire_to_event(Display *dpy, XEvent *re, xEvent *event)
211
{
212
    xkbEvent *xkbevent = (xkbEvent *) event;
213 214 215
    XkbInfoPtr xkbi;

    if ((dpy->flags & XlibDisplayNoXkb) ||
216 217
        (!dpy->xkb_info && !XkbUseExtension(dpy, NULL, NULL)))
        return False;
218
    xkbi = dpy->xkb_info;
219 220
    if (((event->u.u.type & 0x7f) - xkbi->codes->first_event) != XkbEventCode)
        return False;
221 222

    switch (xkbevent->u.any.xkbType) {
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
    case XkbStateNotify:
    {
        xkbStateNotify *sn = (xkbStateNotify *) event;

        if (xkbi->selected_events & XkbStateNotifyMask) {
            XkbStateNotifyEvent *sev = (XkbStateNotifyEvent *) re;

            sev->type = XkbEventCode + xkbi->codes->first_event;
            sev->xkb_type = XkbStateNotify;
            sev->serial = _XSetLastRequestRead(dpy, (xGenericReply *) event);
            sev->send_event = ((event->u.u.type & 0x80) != 0);
            sev->display = dpy;
            sev->time = sn->time;
            sev->device = sn->deviceID;
            sev->keycode = sn->keycode;
            sev->event_type = sn->eventType;
            sev->req_major = sn->requestMajor;
            sev->req_minor = sn->requestMinor;
            sev->changed = sn->changed;
            sev->group = sn->group;
            sev->base_group = sn->baseGroup;
            sev->latched_group = sn->latchedGroup;
            sev->locked_group = sn->lockedGroup;
            sev->mods = sn->mods;
            sev->base_mods = sn->baseMods;
            sev->latched_mods = sn->latchedMods;
            sev->locked_mods = sn->lockedMods;
            sev->compat_state = sn->compatState;
            sev->grab_mods = sn->grabMods;
            sev->compat_grab_mods = sn->compatGrabMods;
            sev->lookup_mods = sn->lookupMods;
            sev->compat_lookup_mods = sn->compatLookupMods;
            sev->ptr_buttons = sn->ptrBtnState;
            return True;
        }
    }
        break;
    case XkbMapNotify:
    {
        xkbMapNotify *mn = (xkbMapNotify *) event;

        if ((xkbi->selected_events & XkbMapNotifyMask) &&
            (xkbi->selected_map_details & mn->changed)) {
266
            XkbMapNotifyEvent *mev = (XkbMapNotifyEvent *) re;
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319

            mev->type = XkbEventCode + xkbi->codes->first_event;
            mev->xkb_type = XkbMapNotify;
            mev->serial = _XSetLastRequestRead(dpy, (xGenericReply *) event);
            mev->send_event = ((event->u.u.type & 0x80) != 0);
            mev->display = dpy;
            mev->time = mn->time;
            mev->device = mn->deviceID;
            mev->changed = mn->changed;
            mev->min_key_code = mn->minKeyCode;
            mev->max_key_code = mn->maxKeyCode;
            mev->first_type = mn->firstType;
            mev->num_types = mn->nTypes;
            mev->first_key_sym = mn->firstKeySym;
            mev->num_key_syms = mn->nKeySyms;
            mev->first_key_act = mn->firstKeyAct;
            mev->num_key_acts = mn->nKeyActs;
            mev->first_key_behavior = mn->firstKeyBehavior;
            mev->num_key_behaviors = mn->nKeyBehaviors;
            mev->vmods = mn->virtualMods;
            mev->first_key_explicit = mn->firstKeyExplicit;
            mev->num_key_explicit = mn->nKeyExplicit;
            mev->first_modmap_key = mn->firstModMapKey;
            mev->num_modmap_keys = mn->nModMapKeys;
            mev->first_vmodmap_key = mn->firstVModMapKey;
            mev->num_vmodmap_keys = mn->nVModMapKeys;
            XkbNoteMapChanges(&xkbi->changes, mev, XKB_XLIB_MAP_MASK);
            if (xkbi->changes.changed)
                xkbi->flags |= XkbMapPending;
            return True;
        }
        else if (mn->nKeySyms > 0) {
            register XMappingEvent *ev = (XMappingEvent *) re;

            ev->type = MappingNotify;
            ev->serial = _XSetLastRequestRead(dpy, (xGenericReply *) event);
            ev->send_event = ((event->u.u.type & 0x80) != 0);
            ev->display = dpy;
            ev->window = 0;
            ev->first_keycode = mn->firstKeySym;
            ev->request = MappingKeyboard;
            ev->count = mn->nKeySyms;
            _XkbNoteCoreMapChanges(&xkbi->changes, ev, XKB_XLIB_MAP_MASK);
            if (xkbi->changes.changed)
                xkbi->flags |= XkbMapPending;
            return True;
        }
    }
        break;
    case XkbControlsNotify:
    {
        if (xkbi->selected_events & XkbControlsNotifyMask) {
            xkbControlsNotify *cn = (xkbControlsNotify *) event;
320
            XkbControlsNotifyEvent *cev = (XkbControlsNotifyEvent *) re;
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344

            cev->type = XkbEventCode + xkbi->codes->first_event;
            cev->xkb_type = XkbControlsNotify;
            cev->serial = _XSetLastRequestRead(dpy, (xGenericReply *) event);
            cev->send_event = ((event->u.u.type & 0x80) != 0);
            cev->display = dpy;
            cev->time = cn->time;
            cev->device = cn->deviceID;
            cev->changed_ctrls = cn->changedControls;
            cev->enabled_ctrls = cn->enabledControls;
            cev->enabled_ctrl_changes = cn->enabledControlChanges;
            cev->keycode = cn->keycode;
            cev->num_groups = cn->numGroups;
            cev->event_type = cn->eventType;
            cev->req_major = cn->requestMajor;
            cev->req_minor = cn->requestMinor;
            return True;
        }
    }
        break;
    case XkbIndicatorMapNotify:
    {
        if (xkbi->selected_events & XkbIndicatorMapNotifyMask) {
            xkbIndicatorNotify *in = (xkbIndicatorNotify *) event;
345
            XkbIndicatorNotifyEvent *iev = (XkbIndicatorNotifyEvent *) re;
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363

            iev->type = XkbEventCode + xkbi->codes->first_event;
            iev->xkb_type = XkbIndicatorMapNotify;
            iev->serial = _XSetLastRequestRead(dpy, (xGenericReply *) event);
            iev->send_event = ((event->u.u.type & 0x80) != 0);
            iev->display = dpy;
            iev->time = in->time;
            iev->device = in->deviceID;
            iev->changed = in->changed;
            iev->state = in->state;
            return True;
        }
    }
        break;
    case XkbIndicatorStateNotify:
    {
        if (xkbi->selected_events & XkbIndicatorStateNotifyMask) {
            xkbIndicatorNotify *in = (xkbIndicatorNotify *) event;
364
            XkbIndicatorNotifyEvent *iev = (XkbIndicatorNotifyEvent *) re;
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382

            iev->type = XkbEventCode + xkbi->codes->first_event;
            iev->xkb_type = XkbIndicatorStateNotify;
            iev->serial = _XSetLastRequestRead(dpy, (xGenericReply *) event);
            iev->send_event = ((event->u.u.type & 0x80) != 0);
            iev->display = dpy;
            iev->time = in->time;
            iev->device = in->deviceID;
            iev->changed = in->changed;
            iev->state = in->state;
            return True;
        }
    }
        break;
    case XkbBellNotify:
    {
        if (xkbi->selected_events & XkbBellNotifyMask) {
            xkbBellNotify *bn = (xkbBellNotify *) event;
383
            XkbBellNotifyEvent *bev = (XkbBellNotifyEvent *) re;
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

            bev->type = XkbEventCode + xkbi->codes->first_event;
            bev->xkb_type = XkbBellNotify;
            bev->serial = _XSetLastRequestRead(dpy, (xGenericReply *) event);
            bev->send_event = ((event->u.u.type & 0x80) != 0);
            bev->display = dpy;
            bev->time = bn->time;
            bev->device = bn->deviceID;
            bev->percent = bn->percent;
            bev->pitch = bn->pitch;
            bev->duration = bn->duration;
            bev->bell_class = bn->bellClass;
            bev->bell_id = bn->bellID;
            bev->name = bn->name;
            bev->window = bn->window;
            bev->event_only = bn->eventOnly;
            return True;
        }
    }
        break;
    case XkbAccessXNotify:
    {
        if (xkbi->selected_events & XkbAccessXNotifyMask) {
            xkbAccessXNotify *axn = (xkbAccessXNotify *) event;
408
            XkbAccessXNotifyEvent *axev = (XkbAccessXNotifyEvent *) re;
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428

            axev->type = XkbEventCode + xkbi->codes->first_event;
            axev->xkb_type = XkbAccessXNotify;
            axev->serial = _XSetLastRequestRead(dpy, (xGenericReply *) event);
            axev->send_event = ((event->u.u.type & 0x80) != 0);
            axev->display = dpy;
            axev->time = axn->time;
            axev->device = axn->deviceID;
            axev->detail = axn->detail;
            axev->keycode = axn->keycode;
            axev->sk_delay = axn->slowKeysDelay;
            axev->debounce_delay = axn->debounceDelay;
            return True;
        }
    }
        break;
    case XkbNamesNotify:
    {
        if (xkbi->selected_events & XkbNamesNotifyMask) {
            xkbNamesNotify *nn = (xkbNamesNotify *) event;
429
            XkbNamesNotifyEvent *nev = (XkbNamesNotifyEvent *) re;
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457

            nev->type = XkbEventCode + xkbi->codes->first_event;
            nev->xkb_type = XkbNamesNotify;
            nev->serial = _XSetLastRequestRead(dpy, (xGenericReply *) event);
            nev->send_event = ((event->u.u.type & 0x80) != 0);
            nev->display = dpy;
            nev->time = nn->time;
            nev->device = nn->deviceID;
            nev->changed = nn->changed;
            nev->first_type = nn->firstType;
            nev->num_types = nn->nTypes;
            nev->first_lvl = nn->firstLevelName;
            nev->num_lvls = nn->nLevelNames;
            nev->num_aliases = nn->nAliases;
            nev->num_radio_groups = nn->nRadioGroups;
            nev->changed_vmods = nn->changedVirtualMods;
            nev->changed_groups = nn->changedGroupNames;
            nev->changed_indicators = nn->changedIndicators;
            nev->first_key = nn->firstKey;
            nev->num_keys = nn->nKeys;
            return True;
        }
    }
        break;
    case XkbCompatMapNotify:
    {
        if (xkbi->selected_events & XkbCompatMapNotifyMask) {
            xkbCompatMapNotify *cmn = (xkbCompatMapNotify *) event;
458
            XkbCompatMapNotifyEvent *cmev = (XkbCompatMapNotifyEvent *) re;
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478

            cmev->type = XkbEventCode + xkbi->codes->first_event;
            cmev->xkb_type = XkbCompatMapNotify;
            cmev->serial = _XSetLastRequestRead(dpy, (xGenericReply *) event);
            cmev->send_event = ((event->u.u.type & 0x80) != 0);
            cmev->display = dpy;
            cmev->time = cmn->time;
            cmev->device = cmn->deviceID;
            cmev->changed_groups = cmn->changedGroups;
            cmev->first_si = cmn->firstSI;
            cmev->num_si = cmn->nSI;
            cmev->num_total_si = cmn->nTotalSI;
            return True;
        }
    }
        break;
    case XkbActionMessage:
    {
        if (xkbi->selected_events & XkbActionMessageMask) {
            xkbActionMessage *am = (xkbActionMessage *) event;
479
            XkbActionMessageEvent *amev = (XkbActionMessageEvent *) re;
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502

            amev->type = XkbEventCode + xkbi->codes->first_event;
            amev->xkb_type = XkbActionMessage;
            amev->serial = _XSetLastRequestRead(dpy, (xGenericReply *) event);
            amev->send_event = ((event->u.u.type & 0x80) != 0);
            amev->display = dpy;
            amev->time = am->time;
            amev->device = am->deviceID;
            amev->keycode = am->keycode;
            amev->press = am->press;
            amev->key_event_follows = am->keyEventFollows;
            amev->group = am->group;
            amev->mods = am->mods;
            memcpy(amev->message, am->message, XkbActionMessageLength);
            amev->message[XkbActionMessageLength] = '\0';
            return True;
        }
    }
        break;
    case XkbExtensionDeviceNotify:
    {
        if (xkbi->selected_events & XkbExtensionDeviceNotifyMask) {
            xkbExtensionDeviceNotify *ed = (xkbExtensionDeviceNotify *) event;
503 504
            XkbExtensionDeviceNotifyEvent *edev
                = (XkbExtensionDeviceNotifyEvent *) re;
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531

            edev->type = XkbEventCode + xkbi->codes->first_event;
            edev->xkb_type = XkbExtensionDeviceNotify;
            edev->serial = _XSetLastRequestRead(dpy, (xGenericReply *) event);
            edev->send_event = ((event->u.u.type & 0x80) != 0);
            edev->display = dpy;
            edev->time = ed->time;
            edev->device = ed->deviceID;
            edev->led_class = ed->ledClass;
            edev->led_id = ed->ledID;
            edev->reason = ed->reason;
            edev->supported = ed->supported;
            edev->leds_defined = ed->ledsDefined;
            edev->led_state = ed->ledState;
            edev->first_btn = ed->firstBtn;
            edev->num_btns = ed->nBtns;
            edev->unsupported = ed->unsupported;
            return True;
        }
    }
        break;
    case XkbNewKeyboardNotify:
    {
        xkbNewKeyboardNotify *nkn = (xkbNewKeyboardNotify *) event;

        if ((xkbi->selected_events & XkbNewKeyboardNotifyMask) &&
            (xkbi->selected_nkn_details & nkn->changed)) {
532
            XkbNewKeyboardNotifyEvent *nkev = (XkbNewKeyboardNotifyEvent *) re;
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576

            nkev->type = XkbEventCode + xkbi->codes->first_event;
            nkev->xkb_type = XkbNewKeyboardNotify;
            nkev->serial = _XSetLastRequestRead(dpy, (xGenericReply *) event);
            nkev->send_event = ((event->u.u.type & 0x80) != 0);
            nkev->display = dpy;
            nkev->time = nkn->time;
            nkev->device = nkn->deviceID;
            nkev->old_device = nkn->oldDeviceID;
            nkev->min_key_code = nkn->minKeyCode;
            nkev->max_key_code = nkn->maxKeyCode;
            nkev->old_min_key_code = nkn->oldMinKeyCode;
            nkev->old_max_key_code = nkn->oldMaxKeyCode;
            nkev->req_major = nkn->requestMajor;
            nkev->req_minor = nkn->requestMinor;
            nkev->changed = nkn->changed;
            if ((xkbi->desc) && (nkev->send_event == 0) &&
                ((xkbi->desc->device_spec == nkev->old_device) ||
                 (nkev->device != nkev->old_device))) {
                xkbi->flags = XkbMapPending | XkbXlibNewKeyboard;
            }
            return True;
        }
        else if (nkn->changed & (XkbNKN_KeycodesMask | XkbNKN_DeviceIDMask)) {
            register XMappingEvent *ev = (XMappingEvent *) re;

            ev->type = MappingNotify;
            ev->serial = _XSetLastRequestRead(dpy, (xGenericReply *) event);
            ev->send_event = ((event->u.u.type & 0x80) != 0);
            ev->display = dpy;
            ev->window = 0;
            ev->first_keycode = dpy->min_keycode;
            ev->request = MappingKeyboard;
            ev->count = (dpy->max_keycode - dpy->min_keycode) + 1;
            if ((xkbi->desc) && (ev->send_event == 0) &&
                ((xkbi->desc->device_spec == nkn->oldDeviceID) ||
                 (nkn->deviceID != nkn->oldDeviceID))) {
                xkbi->flags |= XkbMapPending | XkbXlibNewKeyboard;
            }
            return True;
        }
    }
        break;
    default:
577
#ifdef DEBUG
578 579
        fprintf(stderr, "Got unknown XKEYBOARD event (%d, base=%d)\n",
                re->type, xkbi->codes->first_event);
580
#endif
581
        break;
582 583 584 585 586 587 588
    }
    return False;
}

Bool
XkbIgnoreExtension(Bool ignore)
{
589
    if (getenv("XKB_FORCE") != NULL) {
590
#ifdef DEBUG
591 592
        fprintf(stderr,
                "Forcing use of XKEYBOARD (overriding an IgnoreExtensions)\n");
593
#endif
594
        return False;
595 596
    }
#ifdef DEBUG
597 598 599
    else if (getenv("XKB_DEBUG") != NULL) {
        fprintf(stderr, "Explicitly %signoring XKEYBOARD\n",
                ignore ? "" : "not ");
600 601 602 603 604 605 606 607 608 609
    }
#endif
    _XkbIgnoreExtension = ignore;
    return True;
}

static void
_XkbFreeInfo(Display *dpy)
{
    XkbInfoPtr xkbi = dpy->xkb_info;
610

611
    if (xkbi) {
612 613 614 615
        if (xkbi->desc)
            XkbFreeKeyboard(xkbi->desc, XkbAllComponentsMask, True);
        Xfree(xkbi);
    }
616 617 618
}

Bool
619
XkbUseExtension(Display *dpy, int *major_rtrn, int *minor_rtrn)
620 621 622
{
    xkbUseExtensionReply rep;
    register xkbUseExtensionReq *req;
623 624
    XExtCodes *codes;
    int ev_base, forceIgnore;
625
    XkbInfoPtr xkbi;
626
    char *str;
627
    static int debugMsg;
628 629 630 631 632 633 634 635
    static int been_here = 0;

    if (dpy->xkb_info && !(dpy->flags & XlibDisplayNoXkb)) {
        if (major_rtrn)
            *major_rtrn = dpy->xkb_info->srv_major;
        if (minor_rtrn)
            *minor_rtrn = dpy->xkb_info->srv_minor;
        return True;
636 637
    }
    if (!been_here) {
638 639
        debugMsg = (getenv("XKB_DEBUG") != NULL);
        been_here = 1;
640 641
    }

642 643 644 645
    if (major_rtrn)
        *major_rtrn = 0;
    if (minor_rtrn)
        *minor_rtrn = 0;
646 647 648

    if (!dpy->xkb_info) {
        xkbi = _XkbTypedCalloc(1, XkbInfoRec);
649 650
        if (!xkbi)
            return False;
651 652 653
        dpy->xkb_info = xkbi;
        dpy->free_funcs->xkb = _XkbFreeInfo;

654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
        xkbi->xlib_ctrls |= (XkbLC_ControlFallback | XkbLC_ConsumeLookupMods);
        if ((str = getenv("_XKB_OPTIONS_ENABLE")) != NULL) {
            if ((str = getenv("_XKB_LATIN1_LOOKUP")) != NULL) {
                if ((strcmp(str, "off") == 0) || (strcmp(str, "0") == 0))
                    xkbi->xlib_ctrls &= ~XkbLC_ForceLatin1Lookup;
                else
                    xkbi->xlib_ctrls |= XkbLC_ForceLatin1Lookup;
            }
            if ((str = getenv("_XKB_CONSUME_LOOKUP_MODS")) != NULL) {
                if ((strcmp(str, "off") == 0) || (strcmp(str, "0") == 0))
                    xkbi->xlib_ctrls &= ~XkbLC_ConsumeLookupMods;
                else
                    xkbi->xlib_ctrls |= XkbLC_ConsumeLookupMods;
            }
            if ((str = getenv("_XKB_CONSUME_SHIFT_AND_LOCK")) != NULL) {
                if ((strcmp(str, "off") == 0) || (strcmp(str, "0") == 0))
                    xkbi->xlib_ctrls &= ~XkbLC_AlwaysConsumeShiftAndLock;
                else
                    xkbi->xlib_ctrls |= XkbLC_AlwaysConsumeShiftAndLock;
            }
            if ((str = getenv("_XKB_IGNORE_NEW_KEYBOARDS")) != NULL) {
                if ((strcmp(str, "off") == 0) || (strcmp(str, "0") == 0))
                    xkbi->xlib_ctrls &= ~XkbLC_IgnoreNewKeyboards;
                else
                    xkbi->xlib_ctrls |= XkbLC_IgnoreNewKeyboards;
            }
            if ((str = getenv("_XKB_CONTROL_FALLBACK")) != NULL) {
                if ((strcmp(str, "off") == 0) || (strcmp(str, "0") == 0))
                    xkbi->xlib_ctrls &= ~XkbLC_ControlFallback;
                else
                    xkbi->xlib_ctrls |= XkbLC_ControlFallback;
            }
            if ((str = getenv("_XKB_COMP_LED")) != NULL) {
                if ((strcmp(str, "off") == 0) || (strcmp(str, "0") == 0))
                    xkbi->xlib_ctrls &= ~XkbLC_ComposeLED;
                else {
                    xkbi->xlib_ctrls |= XkbLC_ComposeLED;
                    if (strlen(str) > 0)
                        xkbi->composeLED = XInternAtom(dpy, str, False);
                }
            }
            if ((str = getenv("_XKB_COMP_FAIL_BEEP")) != NULL) {
                if ((strcmp(str, "off") == 0) || (strcmp(str, "0") == 0))
                    xkbi->xlib_ctrls &= ~XkbLC_BeepOnComposeFail;
                else
                    xkbi->xlib_ctrls |= XkbLC_BeepOnComposeFail;
            }
        }
        if ((xkbi->composeLED == None) &&
            ((xkbi->xlib_ctrls & XkbLC_ComposeLED) != 0))
            xkbi->composeLED = XInternAtom(dpy, "Compose", False);
705 706
#ifdef DEBUG
        if (debugMsg) {
707 708 709 710 711 712 713 714 715 716 717 718 719 720
            register unsigned c = xkbi->xlib_ctrls;

            fprintf(stderr,
                    "XKEYBOARD compose: beep on failure is %s, LED is %s\n",
                    ((c & XkbLC_BeepOnComposeFail) ? "on" : "off"),
                    ((c & XkbLC_ComposeLED) ? "on" : "off"));
            fprintf(stderr,
                    "XKEYBOARD XLookupString: %slatin-1, %s lookup modifiers\n",
                    ((c & XkbLC_ForceLatin1Lookup) ? "allow non-" : "force "),
                    ((c & XkbLC_ConsumeLookupMods) ? "consume" : "re-use"));
            fprintf(stderr,
                    "XKEYBOARD XLookupString: %sconsume shift and lock, %scontrol fallback\n",
                    ((c & XkbLC_AlwaysConsumeShiftAndLock) ? "always " :
                     "don't "), ((c & XkbLC_ControlFallback) ? "" : "no "));
721 722 723

        }
#endif
724 725
    }
    else
726 727
        xkbi = dpy->xkb_info;

728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
    forceIgnore = (dpy->flags & XlibDisplayNoXkb) || dpy->keysyms;
    forceIgnore = forceIgnore && (major_rtrn == NULL) && (minor_rtrn == NULL);
    if (forceIgnore || _XkbIgnoreExtension || getenv("XKB_DISABLE")) {
        LockDisplay(dpy);
        dpy->flags |= XlibDisplayNoXkb;
        UnlockDisplay(dpy);
        if (debugMsg)
            fprintf(stderr, "XKEYBOARD extension disabled or missing\n");
        return False;
    }

    if ((codes = XInitExtension(dpy, XkbName)) == NULL) {
        LockDisplay(dpy);
        dpy->flags |= XlibDisplayNoXkb;
        UnlockDisplay(dpy);
        if (debugMsg)
            fprintf(stderr, "XKEYBOARD extension not present\n");
        return False;
746 747 748 749 750 751 752 753 754
    }
    xkbi->codes = codes;
    LockDisplay(dpy);

    GetReq(kbUseExtension, req);
    req->reqType = xkbi->codes->major_opcode;
    req->xkbReqType = X_kbUseExtension;
    req->wantedMajor = XkbMajorVersion;
    req->wantedMinor = XkbMinorVersion;
755
    if (!_XReply(dpy, (xReply *) &rep, 0, xFalse) || !rep.supported) {
756
        Bool fail = True;
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790

        if (debugMsg)
            fprintf(stderr,
                    "XKEYBOARD version mismatch (want %d.%02d, got %d.%02d)\n",
                    XkbMajorVersion, XkbMinorVersion,
                    rep.serverMajor, rep.serverMinor);

        /* pre-release 0.65 is very close to 1.00 */
        if ((rep.serverMajor == 0) && (rep.serverMinor == 65)) {
            if (debugMsg)
                fprintf(stderr, "Trying to fall back to version 0.65...");
            GetReq(kbUseExtension, req);
            req->reqType = xkbi->codes->major_opcode;
            req->xkbReqType = X_kbUseExtension;
            req->wantedMajor = 0;
            req->wantedMinor = 65;
            if (_XReply(dpy, (xReply *) &rep, 0, xFalse) && rep.supported) {
                if (debugMsg)
                    fprintf(stderr, "succeeded\n");
                fail = False;
            }
            else if (debugMsg)
                fprintf(stderr, "failed\n");
        }
        if (fail) {
            dpy->flags |= XlibDisplayNoXkb;
            UnlockDisplay(dpy);
            SyncHandle();
            if (major_rtrn)
                *major_rtrn = rep.serverMajor;
            if (minor_rtrn)
                *minor_rtrn = rep.serverMinor;
            return False;
        }
791 792
    }
#ifdef DEBUG
793 794 795
    else if (forceIgnore) {
        fprintf(stderr,
                "Internal Error!  XkbUseExtension succeeded with forceIgnore set\n");
796 797 798
    }
#endif
    UnlockDisplay(dpy);
799 800 801 802 803 804
    xkbi->srv_major = rep.serverMajor;
    xkbi->srv_minor = rep.serverMinor;
    if (major_rtrn)
        *major_rtrn = rep.serverMajor;
    if (minor_rtrn)
        *minor_rtrn = rep.serverMinor;
805
    if (debugMsg)
806 807 808
        fprintf(stderr, "XKEYBOARD (version %d.%02d/%d.%02d) OK!\n",
                XkbMajorVersion, XkbMinorVersion,
                rep.serverMajor, rep.serverMinor);
809 810

    ev_base = codes->first_event;
811
    XESetWireToEvent(dpy, ev_base + XkbEventCode, wire_to_event);
812 813 814
    SyncHandle();
    return True;
}