~ubuntu-branches/ubuntu/intrepid/xserver-xgl/intrepid

« back to all changes in this revision

Viewing changes to hw/xfree86/common/xf86Io.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthew Garrett
  • Date: 2006-02-13 14:21:43 UTC
  • Revision ID: james.westby@ubuntu.com-20060213142143-mad6z9xzem7hzxz9
Tags: upstream-7.0.0
ImportĀ upstreamĀ versionĀ 7.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $XFree86: xc/programs/Xserver/hw/xfree86/common/xf86Io.c,v 3.56 2003/11/03 05:11:02 tsi Exp $ */
 
2
/*
 
3
 * Copyright 1990,91 by Thomas Roell, Dinkelscherben, Germany.
 
4
 *
 
5
 * Permission to use, copy, modify, distribute, and sell this software and its
 
6
 * documentation for any purpose is hereby granted without fee, provided that
 
7
 * the above copyright notice appear in all copies and that both that
 
8
 * copyright notice and this permission notice appear in supporting
 
9
 * documentation, and that the name of Thomas Roell not be used in
 
10
 * advertising or publicity pertaining to distribution of the software without
 
11
 * specific, written prior permission.  Thomas Roell makes no representations
 
12
 * about the suitability of this software for any purpose.  It is provided
 
13
 * "as is" without express or implied warranty.
 
14
 *
 
15
 * THOMAS ROELL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 
16
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 
17
 * EVENT SHALL THOMAS ROELL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 
18
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 
19
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 
20
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 
21
 * PERFORMANCE OF THIS SOFTWARE.
 
22
 *
 
23
 */
 
24
/*
 
25
 * Copyright (c) 1992-2003 by The XFree86 Project, Inc.
 
26
 *
 
27
 * Permission is hereby granted, free of charge, to any person obtaining a
 
28
 * copy of this software and associated documentation files (the "Software"),
 
29
 * to deal in the Software without restriction, including without limitation
 
30
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 
31
 * and/or sell copies of the Software, and to permit persons to whom the
 
32
 * Software is furnished to do so, subject to the following conditions:
 
33
 *
 
34
 * The above copyright notice and this permission notice shall be included in
 
35
 * all copies or substantial portions of the Software.
 
36
 *
 
37
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
38
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
39
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 
40
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 
41
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 
42
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 
43
 * OTHER DEALINGS IN THE SOFTWARE.
 
44
 *
 
45
 * Except as contained in this notice, the name of the copyright holder(s)
 
46
 * and author(s) shall not be used in advertising or otherwise to promote
 
47
 * the sale, use or other dealings in this Software without prior written
 
48
 * authorization from the copyright holder(s) and author(s).
 
49
 */
 
50
 
 
51
/* $XConsortium: xf86Io.c /main/27 1996/10/19 17:58:55 kaleb $ */
 
52
/* $XdotOrg: xserver/xorg/hw/xfree86/common/xf86Io.c,v 1.5 2005/07/03 07:01:24 daniels Exp $ */
 
53
 
 
54
#define NEED_EVENTS
 
55
#ifdef HAVE_XORG_CONFIG_H
 
56
#include <xorg-config.h>
 
57
#endif
 
58
 
 
59
#include <X11/X.h>
 
60
#include <X11/Xproto.h>
 
61
#include "inputstr.h"
 
62
#include "scrnintstr.h"
 
63
 
 
64
#include "compiler.h"
 
65
 
 
66
#include "xf86.h"
 
67
#include "xf86Priv.h"
 
68
#define XF86_OS_PRIVS
 
69
#include "xf86_OSlib.h"
 
70
#include "mipointer.h"
 
71
 
 
72
#ifdef XINPUT
 
73
#include "xf86Xinput.h"
 
74
#include <X11/extensions/XIproto.h>
 
75
#include "exevents.h"
 
76
#endif
 
77
 
 
78
#ifdef XKB
 
79
#include <X11/extensions/XKB.h>
 
80
#include <X11/extensions/XKBstr.h>
 
81
#include <X11/extensions/XKBsrv.h>
 
82
#endif
 
83
 
 
84
unsigned int xf86InitialCaps = 0;
 
85
unsigned int xf86InitialNum = 0;
 
86
unsigned int xf86InitialScroll = 0;
 
87
 
 
88
#include "atKeynames.h"
 
89
 
 
90
/*
 
91
 * xf86KbdBell --
 
92
 *      Ring the terminal/keyboard bell for an amount of time proportional to
 
93
 *      "loudness".
 
94
 */
 
95
 
 
96
void
 
97
xf86KbdBell(percent, pKeyboard, ctrl, unused)
 
98
     int           percent;          /* Percentage of full volume */
 
99
     DeviceIntPtr  pKeyboard;        /* Keyboard to ring */
 
100
     pointer       ctrl;        
 
101
     int           unused;      
 
102
{
 
103
  xf86SoundKbdBell(percent, xf86Info.bell_pitch, xf86Info.bell_duration);
 
104
}
 
105
 
 
106
void
 
107
xf86UpdateKbdLeds()
 
108
{
 
109
  int leds = 0;
 
110
  if (xf86Info.capsLock) leds |= XLED1;
 
111
  if (xf86Info.numLock)  leds |= XLED2;
 
112
  if (xf86Info.scrollLock || xf86Info.modeSwitchLock) leds |= XLED3;
 
113
  if (xf86Info.composeLock) leds |= XLED4;
 
114
  xf86Info.leds = (xf86Info.leds & xf86Info.xleds) | (leds & ~xf86Info.xleds);
 
115
  xf86KbdLeds();
 
116
}
 
117
 
 
118
void
 
119
xf86KbdLeds ()
 
120
{
 
121
  int leds, real_leds = 0;
 
122
 
 
123
#if defined (__sparc__) && defined(__linux__)
 
124
  static int kbdSun = -1;
 
125
  if (kbdSun == -1) {
 
126
  if ((xf86Info.xkbmodel && !strcmp(xf86Info.xkbmodel, "sun")) ||
 
127
      (xf86Info.xkbrules && !strcmp(xf86Info.xkbrules, "sun")))
 
128
      kbdSun = 1;
 
129
  else
 
130
      kbdSun = 0;
 
131
  }
 
132
  if (kbdSun) {
 
133
     if (xf86Info.leds & 0x08) real_leds |= XLED1;
 
134
     if (xf86Info.leds & 0x04) real_leds |= XLED3;
 
135
     if (xf86Info.leds & 0x02) real_leds |= XLED4;
 
136
     if (xf86Info.leds & 0x01) real_leds |= XLED2;
 
137
     leds = real_leds;
 
138
     real_leds = 0;
 
139
  } else {
 
140
     leds = xf86Info.leds;
 
141
  }
 
142
#else
 
143
  leds = xf86Info.leds;
 
144
#endif /* defined (__sparc__) */
 
145
 
 
146
#ifdef LED_CAP
 
147
  if (leds & XLED1)  real_leds |= LED_CAP;
 
148
  if (leds & XLED2)  real_leds |= LED_NUM;
 
149
  if (leds & XLED3)  real_leds |= LED_SCR;
 
150
#ifdef LED_COMP
 
151
  if (leds & XLED4)  real_leds |= LED_COMP;
 
152
#else
 
153
  if (leds & XLED4)  real_leds |= LED_SCR;
 
154
#endif
 
155
#endif
 
156
#ifdef sun
 
157
  /* Pass through any additional LEDs, such as Kana LED on Sun Japanese kbd */
 
158
  real_leds |= (leds & 0xFFFFFFF0);
 
159
#endif
 
160
  xf86SetKbdLeds(real_leds);
 
161
  (void)leds;
 
162
}
 
163
 
 
164
/*
 
165
 * xf86KbdCtrl --
 
166
 *      Alter some of the keyboard control parameters. All special protocol
 
167
 *      values are handled by dix (ProgChangeKeyboardControl)
 
168
 */
 
169
 
 
170
void
 
171
xf86KbdCtrl (pKeyboard, ctrl)
 
172
     DevicePtr     pKeyboard;        /* Keyboard to alter */
 
173
     KeybdCtrl     *ctrl;
 
174
{
 
175
  int leds;
 
176
  xf86Info.bell_pitch    = ctrl->bell_pitch;
 
177
  xf86Info.bell_duration = ctrl->bell_duration;
 
178
  xf86Info.autoRepeat    = ctrl->autoRepeat;
 
179
 
 
180
  xf86Info.composeLock   = (ctrl->leds & XCOMP) ? TRUE : FALSE;
 
181
 
 
182
  leds = (ctrl->leds & ~(XCAPS | XNUM | XSCR));
 
183
#ifdef XKB
 
184
  if (noXkbExtension) {
 
185
#endif
 
186
      xf86Info.leds = (leds & xf86Info.xleds)|(xf86Info.leds & ~xf86Info.xleds);
 
187
#ifdef XKB
 
188
  } else {
 
189
      xf86Info.leds = leds;
 
190
  }
 
191
#endif
 
192
 
 
193
  xf86KbdLeds();
 
194
}
 
195
 
 
196
/*
 
197
 * xf86InitKBD --
 
198
 *      Reinitialize the keyboard. Only set Lockkeys according to ours leds.
 
199
 *      Depress all other keys.
 
200
 */
 
201
 
 
202
void
 
203
xf86InitKBD(init)
 
204
Bool init;
 
205
{
 
206
  char            leds = 0, rad;
 
207
  unsigned int    i;
 
208
  xEvent          kevent;
 
209
  DeviceIntPtr    pKeyboard = xf86Info.pKeyboard;
 
210
  KeyClassRec     *keyc = xf86Info.pKeyboard->key;
 
211
  KeySym          *map = keyc->curKeySyms.map;
 
212
 
 
213
  kevent.u.keyButtonPointer.time = GetTimeInMillis();
 
214
  kevent.u.keyButtonPointer.rootX = 0;
 
215
  kevent.u.keyButtonPointer.rootY = 0;
 
216
 
 
217
  /*
 
218
   * Hmm... here is the biggest hack of every time !
 
219
   * It may be possible that a switch-vt procedure has finished BEFORE
 
220
   * you released all keys neccessary to do this. That peculiar behavior
 
221
   * can fool the X-server pretty much, cause it assumes that some keys
 
222
   * were not released. TWM may stuck alsmost completly....
 
223
   * OK, what we are doing here is after returning from the vt-switch
 
224
   * exeplicitely unrelease all keyboard keys before the input-devices
 
225
   * are reenabled.
 
226
   */
 
227
  for (i = keyc->curKeySyms.minKeyCode, map = keyc->curKeySyms.map;
 
228
       i < keyc->curKeySyms.maxKeyCode;
 
229
       i++, map += keyc->curKeySyms.mapWidth)
 
230
    if (KeyPressed(i))
 
231
      {
 
232
        switch (*map) {
 
233
        /* Don't release the lock keys */
 
234
        case XK_Caps_Lock:
 
235
        case XK_Shift_Lock:
 
236
        case XK_Num_Lock:
 
237
        case XK_Scroll_Lock:
 
238
        case XK_Kana_Lock:
 
239
          break;
 
240
        default:
 
241
          kevent.u.u.detail = i;
 
242
          kevent.u.u.type = KeyRelease;
 
243
          (* pKeyboard->public.processInputProc)(&kevent, pKeyboard, 1);
 
244
        }
 
245
      }
 
246
  
 
247
  xf86Info.scanPrefix      = 0;
 
248
 
 
249
  if (init)
 
250
    {
 
251
      /*
 
252
       * we must deal here with the fact, that on some cases the numlock or
 
253
       * capslock key are enabled BEFORE the server is started up. So look
 
254
       * here at the state on the according LEDS to determine whether a
 
255
       * lock-key is already set.
 
256
       */
 
257
 
 
258
      xf86Info.capsLock        = FALSE;
 
259
      xf86Info.numLock         = FALSE;
 
260
      xf86Info.scrollLock      = FALSE;
 
261
      xf86Info.modeSwitchLock  = FALSE;
 
262
      xf86Info.composeLock     = FALSE;
 
263
    
 
264
#ifdef LED_CAP
 
265
#ifdef INHERIT_LOCK_STATE
 
266
      leds = xf86Info.leds;
 
267
 
 
268
      for (i = keyc->curKeySyms.minKeyCode, map = keyc->curKeySyms.map;
 
269
           i < keyc->curKeySyms.maxKeyCode;
 
270
           i++, map += keyc->curKeySyms.mapWidth)
 
271
 
 
272
        switch(*map) {
 
273
 
 
274
        case XK_Caps_Lock:
 
275
        case XK_Shift_Lock:
 
276
          if (leds & LED_CAP) 
 
277
            {
 
278
              xf86InitialCaps = i;
 
279
              xf86Info.capsLock = TRUE;
 
280
            }
 
281
          break;
 
282
 
 
283
        case XK_Num_Lock:
 
284
          if (leds & LED_NUM)
 
285
            {
 
286
              xf86InitialNum = i;
 
287
              xf86Info.numLock = TRUE;
 
288
            }
 
289
          break;
 
290
 
 
291
        case XK_Scroll_Lock:
 
292
        case XK_Kana_Lock:
 
293
          if (leds & LED_SCR)
 
294
            {
 
295
              xf86InitialScroll = i;
 
296
              xf86Info.scrollLock = TRUE;
 
297
            }
 
298
          break;
 
299
        }
 
300
#endif /* INHERIT_LOCK_STATE */
 
301
      xf86SetKbdLeds(leds);
 
302
#endif /* LED_CAP */
 
303
      (void)leds;
 
304
 
 
305
      if      (xf86Info.kbdDelay <= 375) rad = 0x00;
 
306
      else if (xf86Info.kbdDelay <= 625) rad = 0x20;
 
307
      else if (xf86Info.kbdDelay <= 875) rad = 0x40;
 
308
      else                               rad = 0x60;
 
309
    
 
310
      if      (xf86Info.kbdRate <=  2)   rad |= 0x1F;
 
311
      else if (xf86Info.kbdRate >= 30)   rad |= 0x00;
 
312
      else                               rad |= ((58 / xf86Info.kbdRate) - 2);
 
313
    
 
314
      xf86SetKbdRepeat(rad);
 
315
    }
 
316
}
 
317
 
 
318
/*
 
319
 * xf86KbdProc --
 
320
 *      Handle the initialization, etc. of a keyboard.
 
321
 */
 
322
 
 
323
int
 
324
xf86KbdProc (pKeyboard, what)
 
325
     DeviceIntPtr pKeyboard;    /* Keyboard to manipulate */
 
326
     int       what;            /* What to do to it */
 
327
{
 
328
  KeySymsRec           keySyms;
 
329
  CARD8                modMap[MAP_LENGTH];
 
330
  int                  kbdFd;
 
331
 
 
332
  switch (what) {
 
333
 
 
334
  case DEVICE_INIT:
 
335
    /*
 
336
     * First open and find the current state of the keyboard.
 
337
     */
 
338
 
 
339
    xf86KbdInit();
 
340
 
 
341
    xf86KbdGetMapping(&keySyms, modMap);
 
342
    
 
343
 
 
344
#ifndef XKB
 
345
    defaultKeyboardControl.leds = xf86GetKbdLeds();
 
346
#else
 
347
    defaultKeyboardControl.leds = 0;
 
348
#endif
 
349
 
 
350
    /*
 
351
     * Perform final initialization of the system private keyboard
 
352
     * structure and fill in various slots in the device record
 
353
     * itself which couldn't be filled in before.
 
354
     */
 
355
 
 
356
    pKeyboard->public.on = FALSE;
 
357
 
 
358
#ifdef XKB
 
359
    if (noXkbExtension) {
 
360
#endif
 
361
    InitKeyboardDeviceStruct((DevicePtr)xf86Info.pKeyboard,
 
362
                             &keySyms,
 
363
                             modMap,
 
364
                             xf86KbdBell,
 
365
                             (KbdCtrlProcPtr)xf86KbdCtrl);
 
366
#ifdef XKB
 
367
    } else {
 
368
        XkbComponentNamesRec    names;
 
369
        XkbDescPtr              desc;
 
370
        Bool                    foundTerminate = FALSE;
 
371
        int                     keyc;
 
372
        if (XkbInitialMap) {
 
373
            if ((xf86Info.xkbkeymap = strchr(XkbInitialMap, '/')) != NULL)
 
374
                xf86Info.xkbkeymap++;
 
375
            else
 
376
                xf86Info.xkbkeymap = XkbInitialMap;
 
377
        }
 
378
        if (xf86Info.xkbkeymap) {
 
379
            names.keymap = xf86Info.xkbkeymap;
 
380
            names.keycodes = NULL;
 
381
            names.types = NULL;
 
382
            names.compat = NULL;
 
383
            names.symbols = NULL;
 
384
            names.geometry = NULL;
 
385
        } else {
 
386
            names.keymap = NULL;
 
387
            names.keycodes = xf86Info.xkbkeycodes;
 
388
            names.types = xf86Info.xkbtypes;
 
389
            names.compat = xf86Info.xkbcompat;
 
390
            names.symbols = xf86Info.xkbsymbols;
 
391
            names.geometry = xf86Info.xkbgeometry;
 
392
        }
 
393
        if ((xf86Info.xkbkeymap || xf86Info.xkbcomponents_specified)
 
394
           && (xf86Info.xkbmodel == NULL || xf86Info.xkblayout == NULL)) {
 
395
                xf86Info.xkbrules = NULL;
 
396
        }
 
397
        XkbSetRulesDflts(xf86Info.xkbrules, xf86Info.xkbmodel,
 
398
                         xf86Info.xkblayout, xf86Info.xkbvariant,
 
399
                         xf86Info.xkboptions);
 
400
        
 
401
        XkbInitKeyboardDeviceStruct(pKeyboard, 
 
402
                                    &names,
 
403
                                    &keySyms, 
 
404
                                    modMap, 
 
405
                                    xf86KbdBell,
 
406
                                    (KbdCtrlProcPtr)xf86KbdCtrl);
 
407
 
 
408
        /* Search keymap for Terminate action */
 
409
        desc  = pKeyboard->key->xkbInfo->desc;
 
410
        for (keyc = desc->min_key_code; keyc <= desc->max_key_code; keyc++) {
 
411
            int i;
 
412
            for (i = 1; i <= XkbKeyNumActions(desc, keyc); i++) {
 
413
                if (XkbKeyAction(desc, keyc, i)
 
414
                  && XkbKeyAction(desc, keyc, i)->type == XkbSA_Terminate) {
 
415
                    foundTerminate = TRUE;
 
416
                    goto searchdone;
 
417
                }
 
418
            }
 
419
        }
 
420
searchdone:
 
421
        xf86Info.ActionKeyBindingsSet = foundTerminate;
 
422
        if (!foundTerminate)
 
423
            xf86Msg(X_INFO, "Server_Terminate keybinding not found\n");
 
424
    }
 
425
#endif
 
426
    
 
427
    xf86InitKBD(TRUE);
 
428
    break;
 
429
    
 
430
  case DEVICE_ON:
 
431
    /*
 
432
     * Set the keyboard into "direct" mode and turn on
 
433
     * event translation.
 
434
     */
 
435
 
 
436
    kbdFd = xf86KbdOn();
 
437
    /*
 
438
     * Discard any pending input after a VT switch to prevent the server
 
439
     * passing on parts of the VT switch sequence.
 
440
     */
 
441
    sleep(1);
 
442
#if defined(WSCONS_SUPPORT)
 
443
    if (xf86Info.consType != WSCONS) {
 
444
#endif
 
445
        if (kbdFd != -1) {
 
446
                char buf[16];
 
447
                read(kbdFd, buf, 16);
 
448
        }
 
449
#if defined(WSCONS_SUPPORT)
 
450
    }
 
451
#endif
 
452
 
 
453
#if !defined(__UNIXOS2__) /* Under EMX, keyboard cannot be select()'ed */
 
454
    if (kbdFd != -1)
 
455
      AddEnabledDevice(kbdFd);
 
456
#endif  /* __UNIXOS2__ */
 
457
 
 
458
    pKeyboard->public.on = TRUE;
 
459
    xf86InitKBD(FALSE);
 
460
    break;
 
461
    
 
462
  case DEVICE_CLOSE:
 
463
  case DEVICE_OFF:
 
464
    /*
 
465
     * Restore original keyboard directness and translation.
 
466
     */
 
467
 
 
468
    kbdFd = xf86KbdOff();
 
469
 
 
470
    if (kbdFd != -1)
 
471
      RemoveEnabledDevice(kbdFd);
 
472
 
 
473
    pKeyboard->public.on = FALSE;
 
474
    break;
 
475
 
 
476
  }
 
477
  return (Success);
 
478
}
 
479
 
 
480
#if defined(DDXTIME) && !defined(QNX4)
 
481
/*
 
482
 * These are getting tossed in here until I can think of where
 
483
 * they really belong
 
484
 */
 
485
#define HALFMONTH ((unsigned long) 1<<31)
 
486
CARD32
 
487
GetTimeInMillis()
 
488
{
 
489
    struct timeval  tp;
 
490
    register CARD32 val;
 
491
    register INT32 diff;
 
492
    static CARD32 oldval = 0;
 
493
    static CARD32 time = 0;
 
494
 
 
495
    gettimeofday(&tp, 0);
 
496
    val = (tp.tv_sec * 1000) + (tp.tv_usec / 1000);
 
497
    if (oldval) {
 
498
        diff = val - oldval;
 
499
        if (diff > 0)
 
500
            time += diff;
 
501
    }
 
502
    oldval = val;
 
503
 
 
504
    return time;
 
505
}
 
506
#endif /* DDXTIME && !QNX4 */
 
507