XKB.c 22.8 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
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 "Xlibint.h"
32
#include <nx-X11/extensions/XKBproto.h>
33 34
#include "XKBlibint.h"

35 36
XkbInternAtomFunc       _XkbInternAtomFunc  = XInternAtom;
XkbGetAtomNameFunc      _XkbGetAtomNameFunc = XGetAtomName;
37

38
Bool
39 40 41 42 43 44
XkbQueryExtension(Display *dpy,
                  int *opcodeReturn,
                  int *eventBaseReturn,
                  int *errorBaseReturn,
                  int *majorReturn,
                  int *minorReturn)
45
{
46 47
    if (!XkbUseExtension(dpy, majorReturn, minorReturn))
        return False;
48
    if (opcodeReturn)
49
        *opcodeReturn = dpy->xkb_info->codes->major_opcode;
50
    if (eventBaseReturn)
51
        *eventBaseReturn = dpy->xkb_info->codes->first_event;
52
    if (errorBaseReturn)
53
        *errorBaseReturn = dpy->xkb_info->codes->first_error;
54
    if (majorReturn)
55
        *majorReturn = dpy->xkb_info->srv_major;
56
    if (minorReturn)
57
        *minorReturn = dpy->xkb_info->srv_minor;
58 59 60
    return True;
}

61
Bool
62
XkbLibraryVersion(int *libMajorRtrn, int *libMinorRtrn)
63
{
64
    int supported;
65 66

    if (*libMajorRtrn != XkbMajorVersion) {
67 68 69 70 71 72
        /* version 0.65 is (almost) compatible with 1.00 */
        if ((XkbMajorVersion == 1) &&
            (((*libMajorRtrn) == 0) && ((*libMinorRtrn) == 65)))
            supported = True;
        else
            supported = False;
73
    }
74
    else {
75
        supported = True;
76 77 78 79 80 81 82 83
    }

    *libMajorRtrn = XkbMajorVersion;
    *libMinorRtrn = XkbMinorVersion;
    return supported;
}

Bool
84 85 86 87
XkbSelectEvents(Display *dpy,
                unsigned int deviceSpec,
                unsigned int affect,
                unsigned int selectAll)
88 89 90 91 92
{
    register xkbSelectEventsReq *req;
    XkbInfoPtr xkbi;

    if ((dpy->flags & XlibDisplayNoXkb) ||
93 94
        (!dpy->xkb_info && !XkbUseExtension(dpy, NULL, NULL)))
        return False;
95 96
    LockDisplay(dpy);
    xkbi = dpy->xkb_info;
97 98
    xkbi->selected_events &= ~affect;
    xkbi->selected_events |= (affect & selectAll);
99 100 101 102
    GetReq(kbSelectEvents, req);
    req->reqType = xkbi->codes->major_opcode;
    req->xkbReqType = X_kbSelectEvents;
    req->deviceSpec = deviceSpec;
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    req->affectWhich = (CARD16) affect;
    req->clear = affect & (~selectAll);
    req->selectAll = affect & selectAll;
    if (affect & XkbMapNotifyMask) {
        req->affectMap = XkbAllMapComponentsMask;
        /* the implicit support needs the client info */
        /* even if the client itself doesn't want it */
        if (selectAll & XkbMapNotifyMask)
            req->map = XkbAllMapEventsMask;
        else
            req->map = XkbAllClientInfoMask;
        if (selectAll & XkbMapNotifyMask)
            xkbi->selected_map_details = XkbAllMapEventsMask;
        else
            xkbi->selected_map_details = 0;
118
    }
119 120 121 122 123 124 125 126 127 128 129
    if (affect & XkbNewKeyboardNotifyMask) {
        if (selectAll & XkbNewKeyboardNotifyMask)
            xkbi->selected_nkn_details = XkbAllNewKeyboardEventsMask;
        else
            xkbi->selected_nkn_details = 0;
        if (!(xkbi->xlib_ctrls & XkbLC_IgnoreNewKeyboards)) {
            /* we want it, even if the client doesn't.  Don't mess */
            /* around with details -- ask for all of them and throw */
            /* away the ones we don't need */
            req->selectAll |= XkbNewKeyboardNotifyMask;
        }
130 131 132 133 134 135 136
    }
    UnlockDisplay(dpy);
    SyncHandle();
    return True;
}

Bool
137 138 139 140 141
XkbSelectEventDetails(Display *dpy,
                      unsigned deviceSpec,
                      unsigned eventType,
                      unsigned long int affect,
                      unsigned long int details)
142 143
{
    register xkbSelectEventsReq *req;
144 145 146
    XkbInfoPtr  xkbi;
    int         size = 0;
    char        *out;
147
    union {
148 149 150
        CARD8   *c8;
        CARD16  *c16;
        CARD32  *c32;
151 152 153
    } u;

    if ((dpy->flags & XlibDisplayNoXkb) ||
154 155
        (!dpy->xkb_info && !XkbUseExtension(dpy, NULL, NULL)))
        return False;
156 157
    LockDisplay(dpy);
    xkbi = dpy->xkb_info;
158 159 160 161
    if (affect & details)
        xkbi->selected_events |= (1 << eventType);
    else
        xkbi->selected_events &= ~(1 << eventType);
162 163 164 165
    GetReq(kbSelectEvents, req);
    req->reqType = xkbi->codes->major_opcode;
    req->xkbReqType = X_kbSelectEvents;
    req->deviceSpec = deviceSpec;
166 167 168 169 170 171 172 173 174 175 176
    req->clear = req->selectAll = 0;
    if (eventType == XkbMapNotify) {
        /* we need all of the client info, even if the application */
        /* doesn't.   Make sure that we always request the stuff */
        /* that the implicit support needs, and just filter out anything */
        /* the client doesn't want later */
        req->affectMap = (CARD16) affect;
        req->map = (CARD16) details | (XkbAllClientInfoMask & affect);
        req->affectWhich = XkbMapNotifyMask;
        xkbi->selected_map_details &= ~affect;
        xkbi->selected_map_details |= (details & affect);
177 178
    }
    else {
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
        req->affectMap = req->map = 0;
        req->affectWhich = (1 << eventType);
        switch (eventType) {
        case XkbNewKeyboardNotify:
            xkbi->selected_nkn_details &= ~affect;
            xkbi->selected_nkn_details |= (details & affect);
            if (!(xkbi->xlib_ctrls & XkbLC_IgnoreNewKeyboards))
                details = (affect & XkbAllNewKeyboardEventsMask);
        case XkbStateNotify:
        case XkbNamesNotify:
        case XkbAccessXNotify:
        case XkbExtensionDeviceNotify:
            size = 2;
            req->length += 1;
            break;
        case XkbControlsNotify:
        case XkbIndicatorStateNotify:
        case XkbIndicatorMapNotify:
            size = 4;
            req->length += 2;
            break;
        case XkbBellNotify:
        case XkbActionMessage:
        case XkbCompatMapNotify:
            size = 1;
            req->length += 1;
            break;
        }
        BufAlloc(char *, out, (((size * 2) + (unsigned) 3) / 4) * 4);

        u.c8 = (CARD8 *) out;
        if (size == 2) {
            u.c16[0] = (CARD16) affect;
            u.c16[1] = (CARD16) details;
        }
        else if (size == 4) {
            u.c32[0] = (CARD32) affect;
            u.c32[1] = (CARD32) details;
        }
        else {
            u.c8[0] = (CARD8) affect;
            u.c8[1] = (CARD8) details;
        }
222 223 224 225 226 227 228
    }
    UnlockDisplay(dpy);
    SyncHandle();
    return True;
}

Bool
229 230 231 232
XkbLockModifiers(Display *dpy,
                 unsigned int deviceSpec,
                 unsigned int affect,
                 unsigned int values)
233 234 235 236 237
{
    register xkbLatchLockStateReq *req;
    XkbInfoPtr xkbi;

    if ((dpy->flags & XlibDisplayNoXkb) ||
238 239
        (!dpy->xkb_info && !XkbUseExtension(dpy, NULL, NULL)))
        return False;
240 241 242 243 244 245
    LockDisplay(dpy);
    xkbi = dpy->xkb_info;
    GetReq(kbLatchLockState, req);
    req->reqType = xkbi->codes->major_opcode;
    req->xkbReqType = X_kbLatchLockState;
    req->deviceSpec = deviceSpec;
246
    req->affectModLocks = affect;
247 248 249 250 251 252 253 254 255 256 257 258 259
    req->modLocks = values;
    req->lockGroup = False;
    req->groupLock = 0;

    req->affectModLatches = req->modLatches = 0;
    req->latchGroup = False;
    req->groupLatch = 0;
    UnlockDisplay(dpy);
    SyncHandle();
    return True;
}

Bool
260 261 262 263
XkbLatchModifiers(Display *dpy,
                  unsigned int deviceSpec,
                  unsigned int affect,
                  unsigned int values)
264 265 266 267 268
{
    register xkbLatchLockStateReq *req;
    XkbInfoPtr xkbi;

    if ((dpy->flags & XlibDisplayNoXkb) ||
269 270
        (!dpy->xkb_info && !XkbUseExtension(dpy, NULL, NULL)))
        return False;
271 272 273 274 275 276 277
    LockDisplay(dpy);
    xkbi = dpy->xkb_info;
    GetReq(kbLatchLockState, req);
    req->reqType = xkbi->codes->major_opcode;
    req->xkbReqType = X_kbLatchLockState;
    req->deviceSpec = deviceSpec;

278
    req->affectModLatches = affect;
279 280 281 282 283 284 285 286 287 288 289 290 291 292
    req->modLatches = values;
    req->latchGroup = False;
    req->groupLatch = 0;

    req->affectModLocks = req->modLocks = 0;
    req->lockGroup = False;
    req->groupLock = 0;

    UnlockDisplay(dpy);
    SyncHandle();
    return True;
}

Bool
293
XkbLockGroup(Display *dpy, unsigned int deviceSpec, unsigned int group)
294 295 296 297 298
{
    register xkbLatchLockStateReq *req;
    XkbInfoPtr xkbi;

    if ((dpy->flags & XlibDisplayNoXkb) ||
299 300
        (!dpy->xkb_info && !XkbUseExtension(dpy, NULL, NULL)))
        return False;
301 302 303 304 305 306
    LockDisplay(dpy);
    xkbi = dpy->xkb_info;
    GetReq(kbLatchLockState, req);
    req->reqType = xkbi->codes->major_opcode;
    req->xkbReqType = X_kbLatchLockState;
    req->deviceSpec = deviceSpec;
307
    req->affectModLocks = 0;
308 309 310 311 312 313 314 315 316 317 318 319 320
    req->modLocks = 0;
    req->lockGroup = True;
    req->groupLock = group;

    req->affectModLatches = req->modLatches = 0;
    req->latchGroup = False;
    req->groupLatch = 0;
    UnlockDisplay(dpy);
    SyncHandle();
    return True;
}

Bool
321
XkbLatchGroup(Display *dpy, unsigned int deviceSpec, unsigned int group)
322 323 324 325 326
{
    register xkbLatchLockStateReq *req;
    XkbInfoPtr xkbi;

    if ((dpy->flags & XlibDisplayNoXkb) ||
327 328
        (!dpy->xkb_info && !XkbUseExtension(dpy, NULL, NULL)))
        return False;
329 330 331 332 333 334 335
    LockDisplay(dpy);
    xkbi = dpy->xkb_info;
    GetReq(kbLatchLockState, req);
    req->reqType = xkbi->codes->major_opcode;
    req->xkbReqType = X_kbLatchLockState;
    req->deviceSpec = deviceSpec;

336
    req->affectModLatches = 0;
337 338 339 340 341 342 343 344 345 346 347 348 349 350
    req->modLatches = 0;
    req->latchGroup = True;
    req->groupLatch = group;

    req->affectModLocks = req->modLocks = 0;
    req->lockGroup = False;
    req->groupLock = 0;

    UnlockDisplay(dpy);
    SyncHandle();
    return True;
}

unsigned
351
XkbSetXlibControls(Display *dpy, unsigned affect, unsigned values)
352 353
{
    if (!dpy->xkb_info)
354
        XkbUseExtension(dpy, NULL, NULL);
355
    if (!dpy->xkb_info)
356 357 358 359
        return 0;
    affect &= XkbLC_AllControls;
    dpy->xkb_info->xlib_ctrls &= ~affect;
    dpy->xkb_info->xlib_ctrls |= (affect & values);
360 361 362 363 364 365 366
    return dpy->xkb_info->xlib_ctrls;
}

unsigned
XkbGetXlibControls(Display *dpy)
{
    if (!dpy->xkb_info)
367
        XkbUseExtension(dpy, NULL, NULL);
368
    if (!dpy->xkb_info)
369
        return 0;
370 371 372 373 374 375
    return dpy->xkb_info->xlib_ctrls;
}

unsigned int
XkbXlibControlsImplemented(void)
{
376
    return XkbLC_AllControls & ~XkbLC_AllComposeControls;
377 378 379
}

Bool
380 381 382 383 384 385 386 387
XkbSetDebuggingFlags(Display *dpy,
                     unsigned int mask,
                     unsigned int flags,
                     char *msg,
                     unsigned int ctrls_mask,
                     unsigned int ctrls,
                     unsigned int *rtrn_flags,
                     unsigned int *rtrn_ctrls)
388 389 390 391 392 393
{
    register xkbSetDebuggingFlagsReq *req;
    xkbSetDebuggingFlagsReply rep;
    XkbInfoPtr xkbi;

    if ((dpy->flags & XlibDisplayNoXkb) ||
394 395
        (!dpy->xkb_info && !XkbUseExtension(dpy, NULL, NULL)))
        return False;
396 397 398
    LockDisplay(dpy);
    xkbi = dpy->xkb_info;
    GetReq(kbSetDebuggingFlags, req);
399 400 401 402 403 404
    req->reqType        = xkbi->codes->major_opcode;
    req->xkbReqType     = X_kbSetDebuggingFlags;
    req->affectFlags    = mask;
    req->flags          = flags;
    req->affectCtrls    = ctrls_mask;
    req->ctrls          = ctrls;
405 406

    if (msg) {
407 408 409 410 411 412
        char *out;

        req->msgLength = (unsigned short) strlen(msg) + 1;
        req->length += (req->msgLength + (unsigned) 3) >> 2;
        BufAlloc(char *, out, ((req->msgLength + (unsigned) 3) / 4) * 4);
        memcpy(out, msg, req->msgLength);
413
    }
414 415 416 417 418 419
    else
        req->msgLength = 0;
    if (!_XReply(dpy, (xReply *) &rep, 0, xFalse)) {
        UnlockDisplay(dpy);
        SyncHandle();
        return False;
420 421
    }
    if (rtrn_flags)
422
        *rtrn_flags = rep.currentFlags;
423
    if (rtrn_ctrls)
424
        *rtrn_ctrls = rep.currentCtrls;
425 426 427 428 429 430
    UnlockDisplay(dpy);
    SyncHandle();
    return True;
}

Bool
431 432 433
XkbComputeEffectiveMap(XkbDescPtr xkb,
                       XkbKeyTypePtr type,
                       unsigned char *map_rtrn)
434
{
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
    register int i;
    unsigned tmp;
    XkbKTMapEntryPtr entry = NULL;

    if ((!xkb) || (!type) || (!xkb->server))
        return False;

    if (type->mods.vmods != 0) {
        if (!XkbVirtualModsToReal(xkb, type->mods.vmods, &tmp))
            return False;

        type->mods.mask = tmp | type->mods.real_mods;
        entry = type->map;
        for (i = 0; i < type->map_count; i++, entry++) {
            tmp = 0;
            if (entry->mods.vmods != 0) {
                if (!XkbVirtualModsToReal(xkb, entry->mods.vmods, &tmp))
                    return False;
                if (tmp == 0) {
                    entry->active = False;
                    continue;
                }
            }
            entry->active = True;
            entry->mods.mask = (entry->mods.real_mods | tmp) & type->mods.mask;
        }
461 462
    }
    else {
463
        type->mods.mask = type->mods.real_mods;
464
    }
465 466 467 468 469 470 471
    if (map_rtrn != NULL) {
        bzero(map_rtrn, type->mods.mask + 1);
        for (i = 0; i < type->map_count; i++) {
            if (entry && entry->active) {
                map_rtrn[type->map[i].mods.mask] = type->map[i].level;
            }
        }
472 473 474 475 476
    }
    return True;
}

Status
477
XkbGetState(Display *dpy, unsigned deviceSpec, XkbStatePtr rtrn)
478
{
479
    register xkbGetStateReq *req;
480 481 482 483
    xkbGetStateReply rep;
    XkbInfoPtr xkbi;

    if ((dpy->flags & XlibDisplayNoXkb) ||
484 485
        (!dpy->xkb_info && !XkbUseExtension(dpy, NULL, NULL)))
        return BadAccess;
486 487 488 489 490 491
    LockDisplay(dpy);
    xkbi = dpy->xkb_info;
    GetReq(kbGetState, req);
    req->reqType = xkbi->codes->major_opcode;
    req->xkbReqType = X_kbGetState;
    req->deviceSpec = deviceSpec;
492 493 494 495
    if (!_XReply(dpy, (xReply *) &rep, 0, xFalse)) {
        UnlockDisplay(dpy);
        SyncHandle();
        return BadImplementation;
496
    }
497 498 499 500 501 502 503 504 505 506 507 508 509 510
    rtrn->mods                  = rep.mods;
    rtrn->base_mods             = rep.baseMods;
    rtrn->latched_mods          = rep.latchedMods;
    rtrn->locked_mods           = rep.lockedMods;
    rtrn->group                 = rep.group;
    rtrn->base_group            = rep.baseGroup;
    rtrn->latched_group         = rep.latchedGroup;
    rtrn->locked_group          = rep.lockedGroup;
    rtrn->compat_state          = rep.compatState;
    rtrn->grab_mods             = rep.grabMods;
    rtrn->compat_grab_mods      = rep.compatGrabMods;
    rtrn->lookup_mods           = rep.lookupMods;
    rtrn->compat_lookup_mods    = rep.compatLookupMods;
    rtrn->ptr_buttons           = rep.ptrBtnState;
511 512 513 514 515 516
    UnlockDisplay(dpy);
    SyncHandle();
    return Success;
}

Bool
517
XkbSetDetectableAutoRepeat(Display *dpy, Bool detectable, Bool *supported)
518
{
519 520 521
    register xkbPerClientFlagsReq *req;
    xkbPerClientFlagsReply rep;
    XkbInfoPtr xkbi;
522 523

    if ((dpy->flags & XlibDisplayNoXkb) ||
524 525
        (!dpy->xkb_info && !XkbUseExtension(dpy, NULL, NULL)))
        return False;
526 527 528 529 530 531 532 533
    LockDisplay(dpy);
    xkbi = dpy->xkb_info;
    GetReq(kbPerClientFlags, req);
    req->reqType = xkbi->codes->major_opcode;
    req->xkbReqType = X_kbPerClientFlags;
    req->deviceSpec = XkbUseCoreKbd;
    req->change = XkbPCF_DetectableAutoRepeatMask;
    if (detectable)
534 535 536 537 538 539 540 541
        req->value = XkbPCF_DetectableAutoRepeatMask;
    else
        req->value = 0;
    req->ctrlsToChange = req->autoCtrls = req->autoCtrlValues = 0;
    if (!_XReply(dpy, (xReply *) &rep, 0, xFalse)) {
        UnlockDisplay(dpy);
        SyncHandle();
        return False;
542 543 544
    }
    UnlockDisplay(dpy);
    SyncHandle();
545 546 547
    if (supported != NULL)
        *supported = ((rep.supported & XkbPCF_DetectableAutoRepeatMask) != 0);
    return ((rep.value & XkbPCF_DetectableAutoRepeatMask) != 0);
548 549 550
}

Bool
551
XkbGetDetectableAutoRepeat(Display *dpy, Bool *supported)
552
{
553 554 555
    register xkbPerClientFlagsReq *req;
    xkbPerClientFlagsReply rep;
    XkbInfoPtr xkbi;
556 557

    if ((dpy->flags & XlibDisplayNoXkb) ||
558 559
        (!dpy->xkb_info && !XkbUseExtension(dpy, NULL, NULL)))
        return False;
560 561 562 563 564 565 566 567
    LockDisplay(dpy);
    xkbi = dpy->xkb_info;
    GetReq(kbPerClientFlags, req);
    req->reqType = xkbi->codes->major_opcode;
    req->xkbReqType = X_kbPerClientFlags;
    req->deviceSpec = XkbUseCoreKbd;
    req->change = 0;
    req->value = 0;
568 569 570 571 572
    req->ctrlsToChange = req->autoCtrls = req->autoCtrlValues = 0;
    if (!_XReply(dpy, (xReply *) &rep, 0, xFalse)) {
        UnlockDisplay(dpy);
        SyncHandle();
        return False;
573 574 575
    }
    UnlockDisplay(dpy);
    SyncHandle();
576 577 578
    if (supported != NULL)
        *supported = ((rep.supported & XkbPCF_DetectableAutoRepeatMask) != 0);
    return ((rep.value & XkbPCF_DetectableAutoRepeatMask) != 0);
579 580 581
}

Bool
582 583 584 585
XkbSetAutoResetControls(Display *dpy,
                        unsigned changes,
                        unsigned *auto_ctrls,
                        unsigned *auto_values)
586
{
587 588 589
    register xkbPerClientFlagsReq *req;
    xkbPerClientFlagsReply rep;
    XkbInfoPtr xkbi;
590 591

    if ((dpy->flags & XlibDisplayNoXkb) ||
592 593
        (!dpy->xkb_info && !XkbUseExtension(dpy, NULL, NULL)))
        return False;
594 595 596 597 598 599 600 601
    LockDisplay(dpy);
    xkbi = dpy->xkb_info;
    GetReq(kbPerClientFlags, req);
    req->reqType = xkbi->codes->major_opcode;
    req->xkbReqType = X_kbPerClientFlags;
    req->change = XkbPCF_AutoResetControlsMask;
    req->deviceSpec = XkbUseCoreKbd;
    req->value = XkbPCF_AutoResetControlsMask;
602 603 604 605 606 607 608
    req->ctrlsToChange = changes;
    req->autoCtrls = *auto_ctrls;
    req->autoCtrlValues = *auto_values;
    if (!_XReply(dpy, (xReply *) &rep, 0, xFalse)) {
        UnlockDisplay(dpy);
        SyncHandle();
        return False;
609 610 611
    }
    UnlockDisplay(dpy);
    SyncHandle();
612 613 614
    *auto_ctrls = rep.autoCtrls;
    *auto_values = rep.autoCtrlValues;
    return ((rep.value & XkbPCF_AutoResetControlsMask) != 0);
615 616 617
}

Bool
618 619 620
XkbGetAutoResetControls(Display *dpy,
                        unsigned *auto_ctrls,
                        unsigned *auto_ctrl_values)
621
{
622 623 624
    register xkbPerClientFlagsReq *req;
    xkbPerClientFlagsReply rep;
    XkbInfoPtr xkbi;
625 626

    if ((dpy->flags & XlibDisplayNoXkb) ||
627 628
        (!dpy->xkb_info && !XkbUseExtension(dpy, NULL, NULL)))
        return False;
629 630 631 632 633 634 635 636
    LockDisplay(dpy);
    xkbi = dpy->xkb_info;
    GetReq(kbPerClientFlags, req);
    req->reqType = xkbi->codes->major_opcode;
    req->xkbReqType = X_kbPerClientFlags;
    req->deviceSpec = XkbUseCoreKbd;
    req->change = 0;
    req->value = 0;
637 638 639 640 641
    req->ctrlsToChange = req->autoCtrls = req->autoCtrlValues = 0;
    if (!_XReply(dpy, (xReply *) &rep, 0, xFalse)) {
        UnlockDisplay(dpy);
        SyncHandle();
        return False;
642 643 644 645
    }
    UnlockDisplay(dpy);
    SyncHandle();
    if (auto_ctrls)
646
        *auto_ctrls = rep.autoCtrls;
647
    if (auto_ctrl_values)
648 649
        *auto_ctrl_values = rep.autoCtrlValues;
    return ((rep.value & XkbPCF_AutoResetControlsMask) != 0);
650 651 652
}

Bool
653
XkbSetPerClientControls(Display *dpy, unsigned change, unsigned *values)
654
{
655 656 657 658
    register xkbPerClientFlagsReq *req;
    xkbPerClientFlagsReply rep;
    XkbInfoPtr xkbi;
    unsigned value_hold = *values;
659 660

    if ((dpy->flags & XlibDisplayNoXkb) ||
661 662 663 664 665
        (!dpy->xkb_info && !XkbUseExtension(dpy, NULL, NULL)) ||
        (change & ~(XkbPCF_GrabsUseXKBStateMask |
                    XkbPCF_LookupStateWhenGrabbed |
                    XkbPCF_SendEventUsesXKBState)))
        return False;
666 667 668 669 670 671 672 673
    LockDisplay(dpy);
    xkbi = dpy->xkb_info;
    GetReq(kbPerClientFlags, req);
    req->reqType = xkbi->codes->major_opcode;
    req->xkbReqType = X_kbPerClientFlags;
    req->change = change;
    req->deviceSpec = XkbUseCoreKbd;
    req->value = *values;
674 675 676 677 678
    req->ctrlsToChange = req->autoCtrls = req->autoCtrlValues = 0;
    if (!_XReply(dpy, (xReply *) &rep, 0, xFalse)) {
        UnlockDisplay(dpy);
        SyncHandle();
        return False;
679 680 681 682
    }
    UnlockDisplay(dpy);
    SyncHandle();
    *values = rep.value;
683
    return ((rep.value & value_hold) != 0);
684 685 686
}

Bool
687
XkbGetPerClientControls(Display *dpy, unsigned *ctrls)
688
{
689 690 691
    register xkbPerClientFlagsReq *req;
    xkbPerClientFlagsReply rep;
    XkbInfoPtr xkbi;
692 693

    if ((dpy->flags & XlibDisplayNoXkb) ||
694
        (!dpy->xkb_info && !XkbUseExtension(dpy, NULL, NULL)) ||
695
        (ctrls == NULL))
696
        return False;
697 698 699 700 701 702 703 704
    LockDisplay(dpy);
    xkbi = dpy->xkb_info;
    GetReq(kbPerClientFlags, req);
    req->reqType = xkbi->codes->major_opcode;
    req->xkbReqType = X_kbPerClientFlags;
    req->deviceSpec = XkbUseCoreKbd;
    req->change = 0;
    req->value = 0;
705 706 707 708 709
    req->ctrlsToChange = req->autoCtrls = req->autoCtrlValues = 0;
    if (!_XReply(dpy, (xReply *) &rep, 0, xFalse)) {
        UnlockDisplay(dpy);
        SyncHandle();
        return False;
710 711 712
    }
    UnlockDisplay(dpy);
    SyncHandle();
713 714 715
    *ctrls = (rep.value & (XkbPCF_GrabsUseXKBStateMask |
                           XkbPCF_LookupStateWhenGrabbed |
                           XkbPCF_SendEventUsesXKBState));
716 717 718 719
    return (True);
}

Display *
720 721 722 723 724 725
XkbOpenDisplay(char *name,
               int *ev_rtrn,
               int *err_rtrn,
               int *major_rtrn,
               int *minor_rtrn,
               int *reason)
726
{
727 728 729 730 731 732 733 734 735
    Display *dpy;
    int major_num, minor_num;

    if ((major_rtrn != NULL) && (minor_rtrn != NULL)) {
        if (!XkbLibraryVersion(major_rtrn, minor_rtrn)) {
            if (reason != NULL)
                *reason = XkbOD_BadLibraryVersion;
            return NULL;
        }
736 737
    }
    else {
738 739 740 741
        major_num = XkbMajorVersion;
        minor_num = XkbMinorVersion;
        major_rtrn = &major_num;
        minor_rtrn = &minor_num;
742
    }
743 744 745 746 747
    dpy = XOpenDisplay(name);
    if (dpy == NULL) {
        if (reason != NULL)
            *reason = XkbOD_ConnectionRefused;
        return NULL;
748
    }
749 750 751 752 753 754 755 756 757 758
    if (!XkbQueryExtension(dpy, NULL, ev_rtrn, err_rtrn,
                           major_rtrn, minor_rtrn)) {
        if (reason != NULL) {
            if ((*major_rtrn != 0) || (*minor_rtrn != 0))
                *reason = XkbOD_BadServerVersion;
            else
                *reason = XkbOD_NonXkbServer;
        }
        XCloseDisplay(dpy);
        return NULL;
759
    }
760 761
    if (reason != NULL)
        *reason = XkbOD_Success;
762 763 764 765
    return dpy;
}

void
766
XkbSetAtomFuncs(XkbInternAtomFunc getAtom, XkbGetAtomNameFunc getName)
767
{
768 769
    _XkbInternAtomFunc = (getAtom ? getAtom : XInternAtom);
    _XkbGetAtomNameFunc = (getName ? getName : XGetAtomName);
770 771
    return;
}