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

« back to all changes in this revision

Viewing changes to hw/xnest/Keyboard.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
/* $Xorg: Keyboard.c,v 1.3 2000/08/17 19:53:28 cpqbld Exp $ */
 
2
/* $XdotOrg: xserver/xorg/hw/xnest/Keyboard.c,v 1.8 2005/07/14 03:36:44 kem Exp $ */
 
3
/*
 
4
 
 
5
Copyright 1993 by Davor Matic
 
6
 
 
7
Permission to use, copy, modify, distribute, and sell this software
 
8
and its documentation for any purpose is hereby granted without fee,
 
9
provided that the above copyright notice appear in all copies and that
 
10
both that copyright notice and this permission notice appear in
 
11
supporting documentation.  Davor Matic makes no representations about
 
12
the suitability of this software for any purpose.  It is provided "as
 
13
is" without express or implied warranty.
 
14
 
 
15
*/
 
16
/* $XFree86: xc/programs/Xserver/hw/xnest/Keyboard.c,v 1.9 2003/09/13 21:33:09 dawes Exp $ */
 
17
 
 
18
#define NEED_EVENTS
 
19
#ifdef HAVE_XNEST_CONFIG_H
 
20
#include <xnest-config.h>
 
21
#endif
 
22
 
 
23
#include <X11/X.h>
 
24
#include <X11/Xproto.h>
 
25
#include <X11/keysym.h>
 
26
#include "screenint.h"
 
27
#include "inputstr.h"
 
28
#include "misc.h"
 
29
#include "scrnintstr.h"
 
30
#include "servermd.h"
 
31
 
 
32
#include "Xnest.h"
 
33
 
 
34
#include "Display.h"
 
35
#include "Screen.h"
 
36
#include "Keyboard.h"
 
37
#include "Args.h"
 
38
#include "Events.h"
 
39
 
 
40
#ifdef XKB
 
41
#include <X11/extensions/XKB.h>
 
42
#include <X11/extensions/XKBsrv.h>
 
43
#include <X11/extensions/XKBconfig.h>
 
44
 
 
45
extern Bool
 
46
XkbQueryExtension(
 
47
        Display *               /* dpy */,
 
48
        int *                   /* opcodeReturn */,
 
49
        int *                   /* eventBaseReturn */,
 
50
        int *                   /* errorBaseReturn */,
 
51
        int *                   /* majorRtrn */,
 
52
        int *                   /* minorRtrn */
 
53
);
 
54
 
 
55
extern  XkbDescPtr XkbGetKeyboard(
 
56
        Display *               /* dpy */,
 
57
        unsigned int            /* which */,
 
58
        unsigned int            /* deviceSpec */
 
59
);
 
60
 
 
61
extern  Status  XkbGetControls(
 
62
        Display *               /* dpy */,
 
63
        unsigned long           /* which */,
 
64
        XkbDescPtr              /* desc */
 
65
);
 
66
 
 
67
#ifndef XKB_BASE_DIRECTORY
 
68
#define XKB_BASE_DIRECTORY      "/usr/X11R6/lib/X11/xkb/"
 
69
#endif
 
70
#ifndef XKB_CONFIG_FILE
 
71
#define XKB_CONFIG_FILE         "X0-config.keyboard"
 
72
#endif
 
73
#ifndef XKB_DFLT_RULES_FILE
 
74
#define XKB_DFLT_RULES_FILE     __XKBDEFRULES__
 
75
#endif
 
76
#ifndef XKB_DFLT_KB_LAYOUT
 
77
#define XKB_DFLT_KB_LAYOUT      "us"
 
78
#endif
 
79
#ifndef XKB_DFLT_KB_MODEL
 
80
#define XKB_DFLT_KB_MODEL       "pc101"
 
81
#endif
 
82
#ifndef XKB_DFLT_KB_VARIANT
 
83
#define XKB_DFLT_KB_VARIANT     NULL
 
84
#endif
 
85
#ifndef XKB_DFLT_KB_OPTIONS
 
86
#define XKB_DFLT_KB_OPTIONS     NULL
 
87
#endif
 
88
 
 
89
#endif
 
90
 
 
91
DeviceIntPtr xnestKeyboardDevice = NULL;
 
92
 
 
93
void
 
94
xnestBell(int volume, DeviceIntPtr pDev, pointer ctrl, int cls)
 
95
{
 
96
  XBell(xnestDisplay, volume);
 
97
}
 
98
 
 
99
void
 
100
xnestChangeKeyboardControl(DeviceIntPtr pDev, KeybdCtrl *ctrl)
 
101
{
 
102
#if 0
 
103
  unsigned long value_mask;
 
104
  XKeyboardControl values;
 
105
  int i;
 
106
 
 
107
  value_mask = KBKeyClickPercent |
 
108
               KBBellPercent |
 
109
               KBBellPitch |
 
110
               KBBellDuration |
 
111
               KBAutoRepeatMode;
 
112
 
 
113
  values.key_click_percent = ctrl->click;
 
114
  values.bell_percent = ctrl->bell;
 
115
  values.bell_pitch = ctrl->bell_pitch;
 
116
  values.bell_duration = ctrl->bell_duration;
 
117
  values.auto_repeat_mode = ctrl->autoRepeat ? 
 
118
                             AutoRepeatModeOn : AutoRepeatModeOff;
 
119
 
 
120
  XChangeKeyboardControl(xnestDisplay, value_mask, &values);
 
121
 
 
122
  /*
 
123
  value_mask = KBKey | KBAutoRepeatMode;
 
124
  At this point, we need to walk through the vector and compare it
 
125
  to the current server vector.  If there are differences, report them.
 
126
  */
 
127
 
 
128
  value_mask = KBLed | KBLedMode;
 
129
  for (i = 1; i <= 32; i++) {
 
130
    values.led = i;
 
131
    values.led_mode = (ctrl->leds & (1 << (i - 1))) ? LedModeOn : LedModeOff;
 
132
    XChangeKeyboardControl(xnestDisplay, value_mask, &values);
 
133
  }
 
134
#endif
 
135
}
 
136
 
 
137
int
 
138
xnestKeyboardProc(DeviceIntPtr pDev, int onoff)
 
139
{
 
140
  XModifierKeymap *modifier_keymap;
 
141
  KeySym *keymap;
 
142
  int mapWidth;
 
143
  int min_keycode, max_keycode;
 
144
  KeySymsRec keySyms;
 
145
  CARD8 modmap[MAP_LENGTH];
 
146
  int i, j;
 
147
  XKeyboardState values;
 
148
 
 
149
  switch (onoff)
 
150
    {
 
151
    case DEVICE_INIT: 
 
152
      modifier_keymap = XGetModifierMapping(xnestDisplay);
 
153
      XDisplayKeycodes(xnestDisplay, &min_keycode, &max_keycode);
 
154
#ifdef _XSERVER64
 
155
      {
 
156
        KeySym64 *keymap64;
 
157
        int i, len;
 
158
        keymap64 = XGetKeyboardMapping(xnestDisplay,
 
159
                                     min_keycode,
 
160
                                     max_keycode - min_keycode + 1,
 
161
                                     &mapWidth);
 
162
        len = (max_keycode - min_keycode + 1) * mapWidth;
 
163
        keymap = (KeySym *)xalloc(len * sizeof(KeySym));
 
164
        for(i = 0; i < len; ++i)
 
165
          keymap[i] = keymap64[i];
 
166
        XFree(keymap64);
 
167
      }
 
168
#else
 
169
      keymap = XGetKeyboardMapping(xnestDisplay, 
 
170
                                   min_keycode,
 
171
                                   max_keycode - min_keycode + 1,
 
172
                                   &mapWidth);
 
173
#endif
 
174
      
 
175
      for (i = 0; i < MAP_LENGTH; i++)
 
176
        modmap[i] = 0;
 
177
      for (j = 0; j < 8; j++)
 
178
        for(i = 0; i < modifier_keymap->max_keypermod; i++) {
 
179
          CARD8 keycode;
 
180
          if ((keycode = 
 
181
              modifier_keymap->
 
182
                modifiermap[j * modifier_keymap->max_keypermod + i]))
 
183
            modmap[keycode] |= 1<<j;
 
184
        }
 
185
      XFreeModifiermap(modifier_keymap);
 
186
      
 
187
      keySyms.minKeyCode = min_keycode;
 
188
      keySyms.maxKeyCode = max_keycode;
 
189
      keySyms.mapWidth = mapWidth;
 
190
      keySyms.map = keymap;
 
191
 
 
192
#ifdef XKB
 
193
      if (noXkbExtension) {
 
194
XkbError:
 
195
#endif
 
196
      XGetKeyboardControl(xnestDisplay, &values);
 
197
 
 
198
      memmove((char *) defaultKeyboardControl.autoRepeats,
 
199
             (char *) values.auto_repeats, sizeof(values.auto_repeats));
 
200
 
 
201
      InitKeyboardDeviceStruct(&pDev->public, &keySyms, modmap,
 
202
                               xnestBell, xnestChangeKeyboardControl);
 
203
#ifdef XKB
 
204
      } else {
 
205
        FILE *file;
 
206
        XkbConfigRtrnRec config;
 
207
 
 
208
        XkbComponentNamesRec names;
 
209
        char *rules, *model, *layout, *variants, *options;
 
210
 
 
211
        XkbDescPtr xkb;
 
212
        int op, event, error, major, minor;
 
213
 
 
214
        if (XkbQueryExtension(xnestDisplay, &op, &event, &error, &major, &minor) == 0) {
 
215
          ErrorF("Unable to initialize XKEYBOARD extension.\n");
 
216
          goto XkbError;
 
217
        }
 
218
        xkb = XkbGetKeyboard(xnestDisplay, XkbGBN_AllComponentsMask, XkbUseCoreKbd);
 
219
        if (xkb == NULL || xkb->geom == NULL) {
 
220
          ErrorF("Couldn't get keyboard.\n");
 
221
          goto XkbError;
 
222
        }
 
223
        XkbGetControls(xnestDisplay, XkbAllControlsMask, xkb);
 
224
 
 
225
        memset(&names, 0, sizeof(XkbComponentNamesRec));
 
226
        rules = XKB_DFLT_RULES_FILE;
 
227
        model = XKB_DFLT_KB_MODEL;
 
228
        layout = XKB_DFLT_KB_LAYOUT;
 
229
        variants = XKB_DFLT_KB_VARIANT;
 
230
        options = XKB_DFLT_KB_OPTIONS;
 
231
        if (XkbInitialMap) {
 
232
          if ((names.keymap = strchr(XkbInitialMap, '/')) != NULL)
 
233
            ++names.keymap;
 
234
          else
 
235
            names.keymap = XkbInitialMap;
 
236
        }
 
237
 
 
238
        if ((file = fopen(XKB_BASE_DIRECTORY XKB_CONFIG_FILE, "r")) != NULL) {
 
239
          if (XkbCFParse(file, XkbCFDflts, xkb, &config) == 0) {
 
240
            ErrorF("Error parsing config file.\n");
 
241
            fclose(file);
 
242
            goto XkbError;
 
243
          }
 
244
          if (config.rules_file)
 
245
            rules = config.rules_file;
 
246
          if (config.model)
 
247
            model = config.model;
 
248
          if (config.layout)
 
249
            layout = config.layout;
 
250
          if (config.variant)
 
251
            variants = config.variant;
 
252
          if (config.options)
 
253
            options = config.options;
 
254
 
 
255
          fclose(file);
 
256
        }
 
257
 
 
258
        XkbSetRulesDflts(rules, model, layout, variants, options);
 
259
        XkbInitKeyboardDeviceStruct(pDev, &names, &keySyms, modmap,
 
260
                                    xnestBell, xnestChangeKeyboardControl);
 
261
        XkbDDXChangeControls(pDev, xkb->ctrls, xkb->ctrls);
 
262
        XkbFreeKeyboard(xkb, 0, False);
 
263
      }
 
264
#endif
 
265
#ifdef _XSERVER64
 
266
      xfree(keymap);
 
267
#else
 
268
      XFree(keymap);
 
269
#endif
 
270
      break;
 
271
    case DEVICE_ON: 
 
272
      xnestEventMask |= XNEST_KEYBOARD_EVENT_MASK;
 
273
      for (i = 0; i < xnestNumScreens; i++)
 
274
        XSelectInput(xnestDisplay, xnestDefaultWindows[i], xnestEventMask);
 
275
      break;
 
276
    case DEVICE_OFF: 
 
277
      xnestEventMask &= ~XNEST_KEYBOARD_EVENT_MASK;
 
278
      for (i = 0; i < xnestNumScreens; i++)
 
279
        XSelectInput(xnestDisplay, xnestDefaultWindows[i], xnestEventMask);
 
280
      break;
 
281
    case DEVICE_CLOSE: 
 
282
      break;
 
283
    }
 
284
  return Success;
 
285
}
 
286
 
 
287
Bool
 
288
LegalModifier(unsigned int key, DevicePtr pDev)
 
289
{
 
290
  return TRUE;
 
291
}
 
292
 
 
293
void
 
294
xnestUpdateModifierState(unsigned int state)
 
295
{
 
296
  DeviceIntPtr pDev = xnestKeyboardDevice;
 
297
  KeyClassPtr keyc = pDev->key;
 
298
  int i;
 
299
  CARD8 mask;
 
300
 
 
301
  state = state & 0xff;
 
302
 
 
303
  if (keyc->state == state)
 
304
    return;
 
305
 
 
306
  for (i = 0, mask = 1; i < 8; i++, mask <<= 1) {
 
307
    int key;
 
308
 
 
309
    /* Modifier is down, but shouldn't be
 
310
     */
 
311
    if ((keyc->state & mask) && !(state & mask)) {
 
312
      int count = keyc->modifierKeyCount[i];
 
313
 
 
314
      for (key = 0; key < MAP_LENGTH; key++)
 
315
        if (keyc->modifierMap[key] & mask) {
 
316
          int bit;
 
317
          BYTE *kptr;
 
318
 
 
319
          kptr = &keyc->down[key >> 3];
 
320
          bit = 1 << (key & 7);
 
321
 
 
322
          if (*kptr & bit)
 
323
            xnestQueueKeyEvent(KeyRelease, key);
 
324
 
 
325
          if (--count == 0)
 
326
            break;
 
327
        }
 
328
    }
 
329
 
 
330
    /* Modifier shoud be down, but isn't
 
331
     */
 
332
    if (!(keyc->state & mask) && (state & mask))
 
333
      for (key = 0; key < MAP_LENGTH; key++)
 
334
        if (keyc->modifierMap[key] & mask) {
 
335
          xnestQueueKeyEvent(KeyPress, key);
 
336
          break;
 
337
        }
 
338
  }
 
339
}