xkbAccessX.c 27.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
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
THE USE OR PERFORMANCE OF THIS SOFTWARE.

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

27 28 29 30
#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

31 32 33 34 35
#include <stdio.h>
#include <math.h>
#include <X11/X.h>
#include <X11/Xproto.h>
#include <X11/keysym.h>
36 37
#include "exglobals.h"
#include <X11/extensions/XIproto.h>
38
#include "inputstr.h"
39
#include "eventstr.h"
40
#include "inpututils.h"
41
#include <xkbsrv.h>
42
#if !defined(WIN32)
43 44 45
#include <sys/time.h>
#endif

46 47
int XkbDfltRepeatDelay = 660;
int XkbDfltRepeatInterval = 40;
48 49 50 51

#define	DFLT_TIMEOUT_CTRLS (XkbAX_KRGMask|XkbStickyKeysMask|XkbMouseKeysMask)
#define	DFLT_TIMEOUT_OPTS  (XkbAX_IndicatorFBMask)

52 53 54 55 56 57 58 59 60
unsigned short XkbDfltAccessXTimeout = 120;
unsigned int XkbDfltAccessXTimeoutMask = DFLT_TIMEOUT_CTRLS;
static unsigned int XkbDfltAccessXTimeoutValues = 0;
static unsigned int XkbDfltAccessXTimeoutOptionsMask = DFLT_TIMEOUT_OPTS;
static unsigned int XkbDfltAccessXTimeoutOptionsValues = 0;
unsigned int XkbDfltAccessXFeedback = XkbAccessXFeedbackMask;
unsigned short XkbDfltAccessXOptions =
    XkbAX_AllOptionsMask & ~(XkbAX_IndicatorFBMask | XkbAX_SKReleaseFBMask |
                             XkbAX_SKRejectFBMask);
61

62
void
63
AccessXComputeCurveFactor(XkbSrvInfoPtr xkbi, XkbControlsPtr ctrls)
64
{
65 66 67 68
    xkbi->mouseKeysCurve = 1.0 + (((double) ctrls->mk_curve) * 0.001);
    xkbi->mouseKeysCurveFactor = (((double) ctrls->mk_max_speed) /
                                  pow((double) ctrls->mk_time_to_max,
                                      xkbi->mouseKeysCurve));
69 70 71
    return;
}

72
void
73 74
AccessXInit(DeviceIntPtr keybd)
{
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    XkbSrvInfoPtr xkbi = keybd->key->xkbInfo;
    XkbControlsPtr ctrls = xkbi->desc->ctrls;

    xkbi->shiftKeyCount = 0;
    xkbi->mouseKeysCounter = 0;
    xkbi->inactiveKey = 0;
    xkbi->slowKey = 0;
    xkbi->repeatKey = 0;
    xkbi->krgTimerActive = _OFF_TIMER;
    xkbi->beepType = _BEEP_NONE;
    xkbi->beepCount = 0;
    xkbi->mouseKeyTimer = NULL;
    xkbi->slowKeysTimer = NULL;
    xkbi->bounceKeysTimer = NULL;
    xkbi->repeatKeyTimer = NULL;
    xkbi->krgTimer = NULL;
    xkbi->beepTimer = NULL;
92
    xkbi->checkRepeat = NULL;
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    ctrls->repeat_delay = XkbDfltRepeatDelay;
    ctrls->repeat_interval = XkbDfltRepeatInterval;
    ctrls->debounce_delay = 300;
    ctrls->slow_keys_delay = 300;
    ctrls->mk_delay = 160;
    ctrls->mk_interval = 40;
    ctrls->mk_time_to_max = 30;
    ctrls->mk_max_speed = 30;
    ctrls->mk_curve = 500;
    ctrls->mk_dflt_btn = 1;
    ctrls->ax_timeout = XkbDfltAccessXTimeout;
    ctrls->axt_ctrls_mask = XkbDfltAccessXTimeoutMask;
    ctrls->axt_ctrls_values = XkbDfltAccessXTimeoutValues;
    ctrls->axt_opts_mask = XkbDfltAccessXTimeoutOptionsMask;
    ctrls->axt_opts_values = XkbDfltAccessXTimeoutOptionsValues;
    if (XkbDfltAccessXTimeout)
109
        ctrls->enabled_ctrls |= XkbAccessXTimeoutMask;
110
    else
111
        ctrls->enabled_ctrls &= ~XkbAccessXTimeoutMask;
112
    ctrls->enabled_ctrls |= XkbDfltAccessXFeedback;
113 114
    ctrls->ax_options = XkbDfltAccessXOptions;
    AccessXComputeCurveFactor(xkbi, ctrls);
115 116 117 118 119 120 121 122 123 124
    return;
}

/************************************************************************/
/*									*/
/* AccessXKeyboardEvent							*/
/*									*/
/*	Generate a synthetic keyboard event.				*/
/*									*/
/************************************************************************/
125 126
static void
AccessXKeyboardEvent(DeviceIntPtr keybd, int type, BYTE keyCode, Bool isRepeat)
127
{
128
    DeviceEvent event;
129

130
    init_device_event(&event, keybd, GetTimeInMillis(), EVENT_SOURCE_NORMAL);
131 132
    event.type = type;
    event.detail.key = keyCode;
133
    event.key_repeat = isRepeat;
134

135 136
    if (xkbDebugFlags & 0x8) {
        DebugF("[xkb] AXKE: Key %d %s\n", keyCode,
137
               (event.type == ET_KeyPress ? "down" : "up"));
138 139
    }

140
    XkbProcessKeyboardEvent(&event, keybd);
141
    return;
142
}                               /* AccessXKeyboardEvent */
143 144 145 146 147 148 149 150 151

/************************************************************************/
/*									*/
/* AccessXKRGTurnOn							*/
/*									*/
/*	Turn the keyboard response group on.				*/
/*									*/
/************************************************************************/
static void
152
AccessXKRGTurnOn(DeviceIntPtr dev, CARD16 KRGControl, xkbControlsNotify * pCN)
153
{
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    XkbSrvInfoPtr xkbi = dev->key->xkbInfo;
    XkbControlsPtr ctrls = xkbi->desc->ctrls;
    XkbControlsRec old;
    XkbEventCauseRec cause;
    XkbSrvLedInfoPtr sli;

    old = *ctrls;
    ctrls->enabled_ctrls |= (KRGControl & XkbAX_KRGMask);
    if (XkbComputeControlsNotify(dev, &old, ctrls, pCN, FALSE))
        XkbSendControlsNotify(dev, pCN);
    cause.kc = pCN->keycode;
    cause.event = pCN->eventType;
    cause.mjr = pCN->requestMajor;
    cause.mnr = pCN->requestMinor;
    sli = XkbFindSrvLedInfo(dev, XkbDfltXIClass, XkbDfltXIId, 0);
    XkbUpdateIndicators(dev, sli->usesControls, TRUE, NULL, &cause);
    if (XkbAX_NeedFeedback(ctrls, XkbAX_FeatureFBMask))
        XkbDDXAccessXBeep(dev, _BEEP_FEATURE_ON, KRGControl);
172
    return;
173 174

}                               /* AccessXKRGTurnOn */
175 176 177 178 179 180 181 182

/************************************************************************/
/*									*/
/* AccessXKRGTurnOff							*/
/*									*/
/*	Turn the keyboard response group off.				*/
/*									*/
/************************************************************************/
183 184
static void
AccessXKRGTurnOff(DeviceIntPtr dev, xkbControlsNotify * pCN)
185
{
186 187 188 189 190
    XkbSrvInfoPtr xkbi = dev->key->xkbInfo;
    XkbControlsPtr ctrls = xkbi->desc->ctrls;
    XkbControlsRec old;
    XkbEventCauseRec cause;
    XkbSrvLedInfoPtr sli;
191 192 193

    old = *ctrls;
    ctrls->enabled_ctrls &= ~XkbAX_KRGMask;
194 195 196 197 198 199 200 201 202 203 204 205
    if (XkbComputeControlsNotify(dev, &old, ctrls, pCN, FALSE))
        XkbSendControlsNotify(dev, pCN);
    cause.kc = pCN->keycode;
    cause.event = pCN->eventType;
    cause.mjr = pCN->requestMajor;
    cause.mnr = pCN->requestMinor;
    sli = XkbFindSrvLedInfo(dev, XkbDfltXIClass, XkbDfltXIId, 0);
    XkbUpdateIndicators(dev, sli->usesControls, TRUE, NULL, &cause);
    if (XkbAX_NeedFeedback(ctrls, XkbAX_FeatureFBMask)) {
        unsigned changes = old.enabled_ctrls ^ ctrls->enabled_ctrls;

        XkbDDXAccessXBeep(dev, _BEEP_FEATURE_OFF, changes);
206 207
    }
    return;
208 209

}                               /* AccessXKRGTurnOff */
210 211 212 213 214 215 216 217 218

/************************************************************************/
/*									*/
/* AccessXStickyKeysTurnOn						*/
/*									*/
/*	Turn StickyKeys on.						*/
/*									*/
/************************************************************************/
static void
219
AccessXStickyKeysTurnOn(DeviceIntPtr dev, xkbControlsNotify * pCN)
220
{
221 222 223 224 225
    XkbSrvInfoPtr xkbi = dev->key->xkbInfo;
    XkbControlsPtr ctrls = xkbi->desc->ctrls;
    XkbControlsRec old;
    XkbEventCauseRec cause;
    XkbSrvLedInfoPtr sli;
226 227 228 229

    old = *ctrls;
    ctrls->enabled_ctrls |= XkbStickyKeysMask;
    xkbi->shiftKeyCount = 0;
230 231 232 233 234 235 236 237 238 239
    if (XkbComputeControlsNotify(dev, &old, ctrls, pCN, FALSE))
        XkbSendControlsNotify(dev, pCN);
    cause.kc = pCN->keycode;
    cause.event = pCN->eventType;
    cause.mjr = pCN->requestMajor;
    cause.mnr = pCN->requestMinor;
    sli = XkbFindSrvLedInfo(dev, XkbDfltXIClass, XkbDfltXIId, 0);
    XkbUpdateIndicators(dev, sli->usesControls, TRUE, NULL, &cause);
    if (XkbAX_NeedFeedback(ctrls, XkbAX_FeatureFBMask)) {
        XkbDDXAccessXBeep(dev, _BEEP_FEATURE_ON, XkbStickyKeysMask);
240 241
    }
    return;
242 243

}                               /* AccessXStickyKeysTurnOn */
244 245 246 247 248 249 250 251 252

/************************************************************************/
/*									*/
/* AccessXStickyKeysTurnOff						*/
/*									*/
/*	Turn StickyKeys off.						*/
/*									*/
/************************************************************************/
static void
253
AccessXStickyKeysTurnOff(DeviceIntPtr dev, xkbControlsNotify * pCN)
254
{
255 256 257 258 259
    XkbSrvInfoPtr xkbi = dev->key->xkbInfo;
    XkbControlsPtr ctrls = xkbi->desc->ctrls;
    XkbControlsRec old;
    XkbEventCauseRec cause;
    XkbSrvLedInfoPtr sli;
260 261 262 263

    old = *ctrls;
    ctrls->enabled_ctrls &= ~XkbStickyKeysMask;
    xkbi->shiftKeyCount = 0;
264 265 266 267 268 269 270 271 272 273 274
    if (XkbComputeControlsNotify(dev, &old, ctrls, pCN, FALSE))
        XkbSendControlsNotify(dev, pCN);

    cause.kc = pCN->keycode;
    cause.event = pCN->eventType;
    cause.mjr = pCN->requestMajor;
    cause.mnr = pCN->requestMinor;
    sli = XkbFindSrvLedInfo(dev, XkbDfltXIClass, XkbDfltXIId, 0);
    XkbUpdateIndicators(dev, sli->usesControls, TRUE, NULL, &cause);
    if (XkbAX_NeedFeedback(ctrls, XkbAX_FeatureFBMask)) {
        XkbDDXAccessXBeep(dev, _BEEP_FEATURE_OFF, XkbStickyKeysMask);
275 276
    }
#ifndef NO_CLEAR_LATCHES_FOR_STICKY_KEYS_OFF
277
    XkbClearAllLatchesAndLocks(dev, xkbi, FALSE, &cause);
278 279
#endif
    return;
280
}                               /* AccessXStickyKeysTurnOff */
281 282

static CARD32
283
AccessXKRGExpire(OsTimerPtr timer, CARD32 now, void *arg)
284
{
285
    xkbControlsNotify cn;
286 287
    DeviceIntPtr dev = arg;
    XkbSrvInfoPtr xkbi = dev->key->xkbInfo;
288 289

    if (xkbi->krgTimerActive == _KRG_WARN_TIMER) {
290
        XkbDDXAccessXBeep(dev, _BEEP_SLOW_WARN, XkbStickyKeysMask);
291 292
        xkbi->krgTimerActive = _KRG_TIMER;
        return 4000;
293
    }
294
    xkbi->krgTimerActive = _OFF_TIMER;
295 296
    cn.keycode = xkbi->slowKeyEnableKey;
    cn.eventType = KeyPress;
297 298
    cn.requestMajor = 0;
    cn.requestMinor = 0;
299
    if (xkbi->desc->ctrls->enabled_ctrls & XkbSlowKeysMask) {
300
        AccessXKRGTurnOff(dev, &cn);
301 302 303
        LogMessage(X_INFO, "XKB SlowKeys are disabled.\n");
    }
    else {
304
        AccessXKRGTurnOn(dev, XkbSlowKeysMask, &cn);
305 306 307
        LogMessage(X_INFO, "XKB SlowKeys are now enabled. Hold shift to disable.\n");
    }

308
    xkbi->slowKeyEnableKey = 0;
309 310 311 312
    return 0;
}

static CARD32
313
AccessXRepeatKeyExpire(OsTimerPtr timer, CARD32 now, void *arg)
314
{
315 316
    DeviceIntPtr dev = (DeviceIntPtr) arg;
    XkbSrvInfoPtr xkbi = dev->key->xkbInfo;
317

318
    if (xkbi->repeatKey == 0)
319
        return 0;
320

321 322
    if (xkbi->checkRepeat == NULL || xkbi->checkRepeat (dev, xkbi, xkbi->repeatKey))
        AccessXKeyboardEvent(dev, ET_KeyPress, xkbi->repeatKey, TRUE);
323

324 325 326
    return xkbi->desc->ctrls->repeat_interval;
}

327
void
328
AccessXCancelRepeatKey(XkbSrvInfoPtr xkbi, KeyCode key)
329
{
330 331
    if (xkbi->repeatKey == key)
        xkbi->repeatKey = 0;
332 333 334 335
    return;
}

static CARD32
336
AccessXSlowKeyExpire(OsTimerPtr timer, CARD32 now, void *arg)
337
{
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
    DeviceIntPtr keybd;
    XkbSrvInfoPtr xkbi;
    XkbDescPtr xkb;
    XkbControlsPtr ctrls;

    keybd = (DeviceIntPtr) arg;
    xkbi = keybd->key->xkbInfo;
    xkb = xkbi->desc;
    ctrls = xkb->ctrls;
    if (xkbi->slowKey != 0) {
        xkbAccessXNotify ev;
        KeySym *sym = XkbKeySymsPtr(xkb, xkbi->slowKey);

        ev.detail = XkbAXN_SKAccept;
        ev.keycode = xkbi->slowKey;
        ev.slowKeysDelay = ctrls->slow_keys_delay;
        ev.debounceDelay = ctrls->debounce_delay;
        XkbSendAccessXNotify(keybd, &ev);
        if (XkbAX_NeedFeedback(ctrls, XkbAX_SKAcceptFBMask))
            XkbDDXAccessXBeep(keybd, _BEEP_SLOW_ACCEPT, XkbSlowKeysMask);
        AccessXKeyboardEvent(keybd, ET_KeyPress, xkbi->slowKey, FALSE);
        /* check for magic sequences */
        if ((ctrls->enabled_ctrls & XkbAccessXKeysMask) &&
            ((sym[0] == XK_Shift_R) || (sym[0] == XK_Shift_L)))
            xkbi->shiftKeyCount++;

        /* Start repeating if necessary.  Stop autorepeating if the user
         * presses a non-modifier key that doesn't autorepeat.
         */
        if (keybd->kbdfeed->ctrl.autoRepeat &&
            ((xkbi->slowKey != xkbi->mouseKey) || (!xkbi->mouseKeysAccel)) &&
            (ctrls->enabled_ctrls & XkbRepeatKeysMask)) {
            if (BitIsOn(keybd->kbdfeed->ctrl.autoRepeats, xkbi->slowKey)) {
                xkbi->repeatKey = xkbi->slowKey;
                xkbi->repeatKeyTimer = TimerSet(xkbi->repeatKeyTimer,
                                                0, ctrls->repeat_delay,
                                                AccessXRepeatKeyExpire,
375
                                                (void *) keybd);
376 377
            }
        }
378 379 380 381 382
    }
    return 0;
}

static CARD32
383
AccessXBounceKeyExpire(OsTimerPtr timer, CARD32 now, void *arg)
384
{
385
    XkbSrvInfoPtr xkbi = ((DeviceIntPtr) arg)->key->xkbInfo;
386

387
    xkbi->inactiveKey = 0;
388 389 390 391
    return 0;
}

static CARD32
392
AccessXTimeoutExpire(OsTimerPtr timer, CARD32 now, void *arg)
393
{
394 395 396 397 398 399 400
    DeviceIntPtr dev = (DeviceIntPtr) arg;
    XkbSrvInfoPtr xkbi = dev->key->xkbInfo;
    XkbControlsPtr ctrls = xkbi->desc->ctrls;
    XkbControlsRec old;
    xkbControlsNotify cn;
    XkbEventCauseRec cause;
    XkbSrvLedInfoPtr sli;
401 402

    if (xkbi->lastPtrEventTime) {
403 404
        unsigned timeToWait = (ctrls->ax_timeout * 1000);
        unsigned timeElapsed = (now - xkbi->lastPtrEventTime);
405

406 407
        if (timeToWait > timeElapsed)
            return timeToWait - timeElapsed;
408
    }
409 410 411 412
    old = *ctrls;
    xkbi->shiftKeyCount = 0;
    ctrls->enabled_ctrls &= ~ctrls->axt_ctrls_mask;
    ctrls->enabled_ctrls |= (ctrls->axt_ctrls_values & ctrls->axt_ctrls_mask);
413
    if (ctrls->axt_opts_mask) {
414 415
        ctrls->ax_options &= ~ctrls->axt_opts_mask;
        ctrls->ax_options |= (ctrls->axt_opts_values & ctrls->axt_opts_mask);
416
    }
417 418 419 420 421 422
    if (XkbComputeControlsNotify(dev, &old, ctrls, &cn, FALSE)) {
        cn.keycode = 0;
        cn.eventType = 0;
        cn.requestMajor = 0;
        cn.requestMinor = 0;
        XkbSendControlsNotify(dev, &cn);
423 424
    }
    XkbSetCauseUnknown(&cause);
425 426 427 428 429 430 431 432 433 434 435 436 437 438
    sli = XkbFindSrvLedInfo(dev, XkbDfltXIClass, XkbDfltXIId, 0);
    XkbUpdateIndicators(dev, sli->usesControls, TRUE, NULL, &cause);
    if (ctrls->ax_options != old.ax_options) {
        unsigned set, cleared, bell;

        set = ctrls->ax_options & (~old.ax_options);
        cleared = (~ctrls->ax_options) & old.ax_options;
        if (set && cleared)
            bell = _BEEP_FEATURE_CHANGE;
        else if (set)
            bell = _BEEP_FEATURE_ON;
        else
            bell = _BEEP_FEATURE_OFF;
        XkbDDXAccessXBeep(dev, bell, XkbAccessXTimeoutMask);
439
    }
440
    xkbi->krgTimerActive = _OFF_TIMER;
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
    return 0;
}

/************************************************************************/
/*									*/
/* AccessXFilterPressEvent						*/
/*									*/
/* Filter events before they get any further if SlowKeys is turned on.	*/
/* In addition, this routine handles the ever so popular magic key	*/
/* acts for turning various accessibility features on/off.		*/
/*									*/
/* Returns TRUE if this routine has discarded the event.		*/
/* Returns FALSE if the event needs further processing.			*/
/*									*/
/************************************************************************/
456
Bool
457
AccessXFilterPressEvent(DeviceEvent *event, DeviceIntPtr keybd)
458
{
459 460 461 462 463 464 465 466 467
    XkbSrvInfoPtr xkbi = keybd->key->xkbInfo;
    XkbControlsPtr ctrls = xkbi->desc->ctrls;
    Bool ignoreKeyEvent = FALSE;
    KeyCode key = event->detail.key;
    KeySym *sym = XkbKeySymsPtr(xkbi->desc, key);

    if (ctrls->enabled_ctrls & XkbAccessXKeysMask) {
        /* check for magic sequences */
        if ((sym[0] == XK_Shift_R) || (sym[0] == XK_Shift_L)) {
468
            xkbi->slowKeyEnableKey = key;
469 470 471
            if (XkbAX_NeedFeedback(ctrls, XkbAX_SlowWarnFBMask)) {
                xkbi->krgTimerActive = _KRG_WARN_TIMER;
                xkbi->krgTimer = TimerSet(xkbi->krgTimer, 0, 4000,
472
                                          AccessXKRGExpire, (void *) keybd);
473 474 475 476
            }
            else {
                xkbi->krgTimerActive = _KRG_TIMER;
                xkbi->krgTimer = TimerSet(xkbi->krgTimer, 0, 8000,
477
                                          AccessXKRGExpire, (void *) keybd);
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
            }
            if (!(ctrls->enabled_ctrls & XkbSlowKeysMask)) {
                CARD32 now = GetTimeInMillis();

                if ((now - xkbi->lastShiftEventTime) > 15000)
                    xkbi->shiftKeyCount = 1;
                else
                    xkbi->shiftKeyCount++;
                xkbi->lastShiftEventTime = now;
            }
        }
        else {
            if (xkbi->krgTimerActive) {
                xkbi->krgTimer = TimerSet(xkbi->krgTimer, 0, 0, NULL, NULL);
                xkbi->krgTimerActive = _OFF_TIMER;
            }
        }
495
    }
496

497 498 499 500 501
    /* Don't transmit the KeyPress if SlowKeys is turned on;
     * The wakeup handler will synthesize one for us if the user
     * has held the key long enough.
     */
    if (ctrls->enabled_ctrls & XkbSlowKeysMask) {
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
        xkbAccessXNotify ev;

        /* If key was already pressed, ignore subsequent press events
         * from the server's autorepeat
         */
        if (xkbi->slowKey == key)
            return TRUE;
        ev.detail = XkbAXN_SKPress;
        ev.keycode = key;
        ev.slowKeysDelay = ctrls->slow_keys_delay;
        ev.debounceDelay = ctrls->debounce_delay;
        XkbSendAccessXNotify(keybd, &ev);
        if (XkbAX_NeedFeedback(ctrls, XkbAX_SKPressFBMask))
            XkbDDXAccessXBeep(keybd, _BEEP_SLOW_PRESS, XkbSlowKeysMask);
        xkbi->slowKey = key;
        xkbi->slowKeysTimer = TimerSet(xkbi->slowKeysTimer,
                                       0, ctrls->slow_keys_delay,
519
                                       AccessXSlowKeyExpire, (void *) keybd);
520
        ignoreKeyEvent = TRUE;
521 522 523 524 525 526
    }

    /* Don't transmit the KeyPress if BounceKeys is turned on
     * and the user pressed the same key within a given time period
     * from the last release.
     */
527 528 529 530 531
    else if ((ctrls->enabled_ctrls & XkbBounceKeysMask) &&
             (key == xkbi->inactiveKey)) {
        if (XkbAX_NeedFeedback(ctrls, XkbAX_BKRejectFBMask))
            XkbDDXAccessXBeep(keybd, _BEEP_BOUNCE_REJECT, XkbBounceKeysMask);
        ignoreKeyEvent = TRUE;
532 533 534 535 536 537
    }

    /* Start repeating if necessary.  Stop autorepeating if the user
     * presses a non-modifier key that doesn't autorepeat.
     */
    if (XkbDDXUsesSoftRepeat(keybd)) {
538 539 540 541 542 543 544 545 546 547 548 549 550
        if ((keybd->kbdfeed->ctrl.autoRepeat) &&
            ((ctrls->enabled_ctrls & (XkbSlowKeysMask | XkbRepeatKeysMask)) ==
             XkbRepeatKeysMask)) {
            if (BitIsOn(keybd->kbdfeed->ctrl.autoRepeats, key)) {
                if (xkbDebugFlags & 0x10)
                    DebugF("Starting software autorepeat...\n");
                if (xkbi->repeatKey == key)
                    ignoreKeyEvent = TRUE;
                else {
                    xkbi->repeatKey = key;
                    xkbi->repeatKeyTimer = TimerSet(xkbi->repeatKeyTimer,
                                                    0, ctrls->repeat_delay,
                                                    AccessXRepeatKeyExpire,
551
                                                    (void *) keybd);
552 553 554
                }
            }
        }
555
    }
556

557 558 559
    /* Check for two keys being pressed at the same time.  This section
     * essentially says the following:
     *
560
     *  If StickyKeys is on, and a modifier is currently being held down,
561 562 563 564
     *  and one of the following is true:  the current key is not a modifier
     *  or the currentKey is a modifier, but not the only modifier being
     *  held down, turn StickyKeys off if the TwoKeys off ctrl is set.
     */
565 566 567 568 569 570 571 572 573 574
    if ((ctrls->enabled_ctrls & XkbStickyKeysMask) &&
        (xkbi->state.base_mods != 0) &&
        (XkbAX_NeedOption(ctrls, XkbAX_TwoKeysMask))) {
        xkbControlsNotify cn;

        cn.keycode = key;
        cn.eventType = KeyPress;
        cn.requestMajor = 0;
        cn.requestMinor = 0;
        AccessXStickyKeysTurnOff(keybd, &cn);
575
    }
576

577
    if (!ignoreKeyEvent)
578
        XkbProcessKeyboardEvent(event, keybd);
579
    return ignoreKeyEvent;
580
}                               /* AccessXFilterPressEvent */
581 582 583 584 585 586 587 588 589 590 591 592 593

/************************************************************************/
/*									*/
/* AccessXFilterReleaseEvent						*/
/*									*/
/* Filter events before they get any further if SlowKeys is turned on.	*/
/* In addition, this routine handles the ever so popular magic key	*/
/* acts for turning various accessibility features on/off.		*/
/*									*/
/* Returns TRUE if this routine has discarded the event.		*/
/* Returns FALSE if the event needs further processing.			*/
/*									*/
/************************************************************************/
594
Bool
595
AccessXFilterReleaseEvent(DeviceEvent *event, DeviceIntPtr keybd)
596
{
597 598 599 600 601
    XkbSrvInfoPtr xkbi = keybd->key->xkbInfo;
    XkbControlsPtr ctrls = xkbi->desc->ctrls;
    KeyCode key = event->detail.key;
    Bool ignoreKeyEvent = FALSE;

602
    /* Don't transmit the KeyRelease if BounceKeys is on and
603
     * this is the release of a key that was ignored due to
604 605 606
     * BounceKeys.
     */
    if (ctrls->enabled_ctrls & XkbBounceKeysMask) {
607 608 609 610 611 612
        if ((key != xkbi->mouseKey) && (!BitIsOn(keybd->key->down, key)))
            ignoreKeyEvent = TRUE;
        xkbi->inactiveKey = key;
        xkbi->bounceKeysTimer = TimerSet(xkbi->bounceKeysTimer, 0,
                                         ctrls->debounce_delay,
                                         AccessXBounceKeyExpire,
613
                                         (void *) keybd);
614 615 616 617 618 619 620
    }

    /* Don't transmit the KeyRelease if SlowKeys is turned on and
     * the user didn't hold the key long enough.  We know we passed
     * the key if the down bit was set by CoreProcessKeyboadEvent.
     */
    if (ctrls->enabled_ctrls & XkbSlowKeysMask) {
621 622
        xkbAccessXNotify ev;
        unsigned beep_type;
623
        unsigned mask;
624 625 626 627 628 629 630

        ev.keycode = key;
        ev.slowKeysDelay = ctrls->slow_keys_delay;
        ev.debounceDelay = ctrls->debounce_delay;
        if (BitIsOn(keybd->key->down, key) || (xkbi->mouseKey == key)) {
            ev.detail = XkbAXN_SKRelease;
            beep_type = _BEEP_SLOW_RELEASE;
631
            mask = XkbAX_SKReleaseFBMask;
632 633 634 635
        }
        else {
            ev.detail = XkbAXN_SKReject;
            beep_type = _BEEP_SLOW_REJECT;
636
            mask = XkbAX_SKRejectFBMask;
637 638 639
            ignoreKeyEvent = TRUE;
        }
        XkbSendAccessXNotify(keybd, &ev);
640
        if (XkbAX_NeedFeedback(ctrls, mask)) {
641 642 643 644
            XkbDDXAccessXBeep(keybd, beep_type, XkbSlowKeysMask);
        }
        if (xkbi->slowKey == key)
            xkbi->slowKey = 0;
645 646 647 648 649
    }

    /* Stop Repeating if the user releases the key that is currently
     * repeating.
     */
650 651
    if (xkbi->repeatKey == key) {
        xkbi->repeatKey = 0;
652 653
    }

654 655 656 657 658
    if ((ctrls->enabled_ctrls & XkbAccessXTimeoutMask) &&
        (ctrls->ax_timeout > 0)) {
        xkbi->lastPtrEventTime = 0;
        xkbi->krgTimer = TimerSet(xkbi->krgTimer, 0,
                                  ctrls->ax_timeout * 1000,
659
                                  AccessXTimeoutExpire, (void *) keybd);
660
        xkbi->krgTimerActive = _ALL_TIMEOUT_TIMER;
661
    }
662 663 664
    else if (xkbi->krgTimerActive != _OFF_TIMER) {
        xkbi->krgTimer = TimerSet(xkbi->krgTimer, 0, 0, NULL, NULL);
        xkbi->krgTimerActive = _OFF_TIMER;
665
    }
666

667 668 669 670
    /* Keep track of how many times the Shift key has been pressed.
     * If it has been pressed and released 5 times in a row, toggle
     * the state of StickyKeys.
     */
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
    if ((!ignoreKeyEvent) && (xkbi->shiftKeyCount)) {
        KeySym *pSym = XkbKeySymsPtr(xkbi->desc, key);

        if ((pSym[0] != XK_Shift_L) && (pSym[0] != XK_Shift_R)) {
            xkbi->shiftKeyCount = 0;
        }
        else if (xkbi->shiftKeyCount >= 5) {
            xkbControlsNotify cn;

            cn.keycode = key;
            cn.eventType = KeyPress;
            cn.requestMajor = 0;
            cn.requestMinor = 0;
            if (ctrls->enabled_ctrls & XkbStickyKeysMask)
                AccessXStickyKeysTurnOff(keybd, &cn);
            else
                AccessXStickyKeysTurnOn(keybd, &cn);
            xkbi->shiftKeyCount = 0;
        }
690
    }
691

692
    if (!ignoreKeyEvent)
693
        XkbProcessKeyboardEvent(event, keybd);
694
    return ignoreKeyEvent;
695 696

}                               /* AccessXFilterReleaseEvent */
697 698 699 700 701 702 703 704 705 706 707

/************************************************************************/
/*									*/
/* ProcessPointerEvent							*/
/*									*/
/* This routine merely sets the shiftKeyCount and clears the keyboard   */
/* response group timer (if necessary) on a mouse event.  This is so	*/
/* multiple shifts with just the mouse and shift-drags with the mouse	*/
/* don't accidentally turn on StickyKeys or the Keyboard Response Group.*/
/*									*/
/************************************************************************/
708
extern int xkbDevicePrivateIndex;
709
void
710
ProcessPointerEvent(InternalEvent *ev, DeviceIntPtr mouse)
711
{
712 713 714 715 716 717
    DeviceIntPtr dev;
    XkbSrvInfoPtr xkbi = NULL;
    unsigned changed = 0;
    ProcessInputProc backupproc;
    xkbDeviceInfoPtr xkbPrivPtr = XKBDEVICEINFO(mouse);
    DeviceEvent *event = &ev->device_event;
718

719
    dev = IsFloating(mouse) ? mouse : GetMaster(mouse, MASTER_KEYBOARD);
720

721 722 723 724
    if (dev && dev->key) {
        xkbi = dev->key->xkbInfo;
        xkbi->shiftKeyCount = 0;
        xkbi->lastPtrEventTime = event->time;
725
    }
726

727
    if (event->type == ET_ButtonPress) {
728
        changed |= XkbPointerButtonMask;
729
    }
730
    else if (event->type == ET_ButtonRelease) {
731 732 733 734 735 736 737 738 739
        if (IsMaster(dev)) {
            DeviceIntPtr source;
            int rc;

            rc = dixLookupDevice(&source, event->sourceid, serverClient,
                    DixWriteAccess);
            if (rc != Success)
                ErrorF("[xkb] bad sourceid '%d' on button release event.\n",
                        event->sourceid);
740 741 742 743 744 745 746
            else if (!IsXTestDevice(source, GetMaster(dev, MASTER_POINTER))) {
                DeviceIntPtr xtest_device;

                xtest_device = GetXTestDevice(GetMaster(dev, MASTER_POINTER));
                if (button_is_down(xtest_device, ev->device_event.detail.button, BUTTON_PROCESSED))
                    XkbFakeDeviceButton(dev, FALSE, event->detail.key);
            }
747
        }
748

749 750 751
        if (xkbi)
            xkbi->lockedPtrButtons &= ~(1 << (event->detail.key & 0x7));

752
        changed |= XkbPointerButtonMask;
753
    }
754 755

    UNWRAP_PROCESS_INPUT_PROC(mouse, xkbPrivPtr, backupproc);
756
    mouse->public.processInputProc(ev, mouse);
757
    COND_WRAP_PROCESS_INPUT_PROC(mouse, xkbPrivPtr, backupproc, xkbUnwrapProc);
758

759
    if (!xkbi)
760
        return;
761

762
    xkbi->state.ptr_buttons = (mouse->button) ? mouse->button->state : 0;
763

764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
    /* clear any latched modifiers */
    if (xkbi->state.latched_mods && (event->type == ET_ButtonRelease)) {
        unsigned changed_leds;
        XkbStateRec oldState;
        XkbSrvLedInfoPtr sli;

        sli = XkbFindSrvLedInfo(dev, XkbDfltXIClass, XkbDfltXIId, 0);
        oldState = xkbi->state;
        XkbLatchModifiers(dev, 0xFF, 0x00);

        XkbComputeDerivedState(xkbi);
        changed |= XkbStateChangedFlags(&oldState, &xkbi->state);
        if (changed & sli->usedComponents) {
            changed_leds = XkbIndicatorsToUpdate(dev, changed, FALSE);
            if (changed_leds) {
                XkbEventCauseRec cause;

                XkbSetCauseKey(&cause, (event->detail.key & 0x7), event->type);
                XkbUpdateIndicators(dev, changed_leds, TRUE, NULL, &cause);
            }
        }
785 786
    }

787 788
    if (((xkbi->flags & _XkbStateNotifyInProgress) == 0) && (changed != 0)) {
        xkbStateNotify sn;
789

790 791 792 793 794 795
        sn.keycode = event->detail.key;
        sn.eventType = event->type;
        sn.requestMajor = sn.requestMinor = 0;
        sn.changed = changed;
        XkbSendStateNotify(dev, &sn);
    }
796

797
}                               /* ProcessPointerEvent */