Initial commit
[xorg_rtime.git] / xorg-server-1.4 / hw / xnest / Keyboard.c
blobbb3cb1376d3e7c7de231d1cd7e73eafb7d5e8790
1 /*
3 Copyright 1993 by Davor Matic
5 Permission to use, copy, modify, distribute, and sell this software
6 and its documentation for any purpose is hereby granted without fee,
7 provided that the above copyright notice appear in all copies and that
8 both that copyright notice and this permission notice appear in
9 supporting documentation. Davor Matic makes no representations about
10 the suitability of this software for any purpose. It is provided "as
11 is" without express or implied warranty.
15 #define NEED_EVENTS
16 #ifdef HAVE_XNEST_CONFIG_H
17 #include <xnest-config.h>
18 #endif
20 #include <X11/X.h>
21 #include <X11/Xproto.h>
22 #include <X11/keysym.h>
23 #include "screenint.h"
24 #include "inputstr.h"
25 #include "misc.h"
26 #include "scrnintstr.h"
27 #include "servermd.h"
29 #include "Xnest.h"
31 #include "Display.h"
32 #include "Screen.h"
33 #include "Keyboard.h"
34 #include "Args.h"
35 #include "Events.h"
37 #ifdef XKB
38 #include <X11/extensions/XKB.h>
39 #include <xkbsrv.h>
40 #include <X11/extensions/XKBconfig.h>
42 extern Bool
43 XkbQueryExtension(
44 Display * /* dpy */,
45 int * /* opcodeReturn */,
46 int * /* eventBaseReturn */,
47 int * /* errorBaseReturn */,
48 int * /* majorRtrn */,
49 int * /* minorRtrn */
52 extern XkbDescPtr XkbGetKeyboard(
53 Display * /* dpy */,
54 unsigned int /* which */,
55 unsigned int /* deviceSpec */
58 extern Status XkbGetControls(
59 Display * /* dpy */,
60 unsigned long /* which */,
61 XkbDescPtr /* desc */
64 #ifndef XKB_BASE_DIRECTORY
65 #define XKB_BASE_DIRECTORY "/usr/X11R6/lib/X11/xkb/"
66 #endif
67 #ifndef XKB_CONFIG_FILE
68 #define XKB_CONFIG_FILE "X0-config.keyboard"
69 #endif
70 #ifndef XKB_DFLT_RULES_FILE
71 #define XKB_DFLT_RULES_FILE __XKBDEFRULES__
72 #endif
73 #ifndef XKB_DFLT_KB_LAYOUT
74 #define XKB_DFLT_KB_LAYOUT "us"
75 #endif
76 #ifndef XKB_DFLT_KB_MODEL
77 #define XKB_DFLT_KB_MODEL "pc101"
78 #endif
79 #ifndef XKB_DFLT_KB_VARIANT
80 #define XKB_DFLT_KB_VARIANT NULL
81 #endif
82 #ifndef XKB_DFLT_KB_OPTIONS
83 #define XKB_DFLT_KB_OPTIONS NULL
84 #endif
86 #endif
88 DeviceIntPtr xnestKeyboardDevice = NULL;
90 void
91 xnestBell(int volume, DeviceIntPtr pDev, pointer ctrl, int cls)
93 XBell(xnestDisplay, volume);
96 void
97 DDXRingBell(int volume, int pitch, int duration)
99 XBell(xnestDisplay, volume);
102 void
103 xnestChangeKeyboardControl(DeviceIntPtr pDev, KeybdCtrl *ctrl)
105 #if 0
106 unsigned long value_mask;
107 XKeyboardControl values;
108 int i;
110 value_mask = KBKeyClickPercent |
111 KBBellPercent |
112 KBBellPitch |
113 KBBellDuration |
114 KBAutoRepeatMode;
116 values.key_click_percent = ctrl->click;
117 values.bell_percent = ctrl->bell;
118 values.bell_pitch = ctrl->bell_pitch;
119 values.bell_duration = ctrl->bell_duration;
120 values.auto_repeat_mode = ctrl->autoRepeat ?
121 AutoRepeatModeOn : AutoRepeatModeOff;
123 XChangeKeyboardControl(xnestDisplay, value_mask, &values);
126 value_mask = KBKey | KBAutoRepeatMode;
127 At this point, we need to walk through the vector and compare it
128 to the current server vector. If there are differences, report them.
131 value_mask = KBLed | KBLedMode;
132 for (i = 1; i <= 32; i++) {
133 values.led = i;
134 values.led_mode = (ctrl->leds & (1 << (i - 1))) ? LedModeOn : LedModeOff;
135 XChangeKeyboardControl(xnestDisplay, value_mask, &values);
137 #endif
141 xnestKeyboardProc(DeviceIntPtr pDev, int onoff)
143 XModifierKeymap *modifier_keymap;
144 KeySym *keymap;
145 int mapWidth;
146 int min_keycode, max_keycode;
147 KeySymsRec keySyms;
148 CARD8 modmap[MAP_LENGTH];
149 int i, j;
150 XKeyboardState values;
152 switch (onoff)
154 case DEVICE_INIT:
155 modifier_keymap = XGetModifierMapping(xnestDisplay);
156 XDisplayKeycodes(xnestDisplay, &min_keycode, &max_keycode);
157 #ifdef _XSERVER64
159 KeySym64 *keymap64;
160 int i, len;
161 keymap64 = XGetKeyboardMapping(xnestDisplay,
162 min_keycode,
163 max_keycode - min_keycode + 1,
164 &mapWidth);
165 len = (max_keycode - min_keycode + 1) * mapWidth;
166 keymap = (KeySym *)xalloc(len * sizeof(KeySym));
167 for(i = 0; i < len; ++i)
168 keymap[i] = keymap64[i];
169 XFree(keymap64);
171 #else
172 keymap = XGetKeyboardMapping(xnestDisplay,
173 min_keycode,
174 max_keycode - min_keycode + 1,
175 &mapWidth);
176 #endif
178 for (i = 0; i < MAP_LENGTH; i++)
179 modmap[i] = 0;
180 for (j = 0; j < 8; j++)
181 for(i = 0; i < modifier_keymap->max_keypermod; i++) {
182 CARD8 keycode;
183 if ((keycode =
184 modifier_keymap->
185 modifiermap[j * modifier_keymap->max_keypermod + i]))
186 modmap[keycode] |= 1<<j;
188 XFreeModifiermap(modifier_keymap);
190 keySyms.minKeyCode = min_keycode;
191 keySyms.maxKeyCode = max_keycode;
192 keySyms.mapWidth = mapWidth;
193 keySyms.map = keymap;
195 #ifdef XKB
196 if (noXkbExtension) {
197 XkbError:
198 #endif
199 XGetKeyboardControl(xnestDisplay, &values);
201 memmove((char *) defaultKeyboardControl.autoRepeats,
202 (char *) values.auto_repeats, sizeof(values.auto_repeats));
204 InitKeyboardDeviceStruct(&pDev->public, &keySyms, modmap,
205 xnestBell, xnestChangeKeyboardControl);
206 #ifdef XKB
207 } else {
208 XkbComponentNamesRec names;
209 char *rules, *model, *layout, *variants, *options;
211 XkbDescPtr xkb;
212 int op, event, error, major, minor;
214 if (XkbQueryExtension(xnestDisplay, &op, &event, &error, &major, &minor) == 0) {
215 ErrorF("Unable to initialize XKEYBOARD extension.\n");
216 goto XkbError;
218 xkb = XkbGetKeyboard(xnestDisplay, XkbGBN_AllComponentsMask, XkbUseCoreKbd);
219 if (xkb == NULL || xkb->geom == NULL) {
220 ErrorF("Couldn't get keyboard.\n");
221 goto XkbError;
223 XkbGetControls(xnestDisplay, XkbAllControlsMask, xkb);
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;
232 XkbSetRulesDflts(rules, model, layout, variants, options);
233 XkbInitKeyboardDeviceStruct(pDev, &names, &keySyms, modmap,
234 xnestBell, xnestChangeKeyboardControl);
235 XkbDDXChangeControls(pDev, xkb->ctrls, xkb->ctrls);
236 XkbFreeKeyboard(xkb, 0, False);
238 #endif
239 #ifdef _XSERVER64
240 xfree(keymap);
241 #else
242 XFree(keymap);
243 #endif
244 break;
245 case DEVICE_ON:
246 xnestEventMask |= XNEST_KEYBOARD_EVENT_MASK;
247 for (i = 0; i < xnestNumScreens; i++)
248 XSelectInput(xnestDisplay, xnestDefaultWindows[i], xnestEventMask);
249 break;
250 case DEVICE_OFF:
251 xnestEventMask &= ~XNEST_KEYBOARD_EVENT_MASK;
252 for (i = 0; i < xnestNumScreens; i++)
253 XSelectInput(xnestDisplay, xnestDefaultWindows[i], xnestEventMask);
254 break;
255 case DEVICE_CLOSE:
256 break;
258 return Success;
261 Bool
262 LegalModifier(unsigned int key, DeviceIntPtr pDev)
264 return TRUE;
267 void
268 xnestUpdateModifierState(unsigned int state)
270 DeviceIntPtr pDev = xnestKeyboardDevice;
271 KeyClassPtr keyc = pDev->key;
272 int i;
273 CARD8 mask;
275 state = state & 0xff;
277 if (keyc->state == state)
278 return;
280 for (i = 0, mask = 1; i < 8; i++, mask <<= 1) {
281 int key;
283 /* Modifier is down, but shouldn't be
285 if ((keyc->state & mask) && !(state & mask)) {
286 int count = keyc->modifierKeyCount[i];
288 for (key = 0; key < MAP_LENGTH; key++)
289 if (keyc->modifierMap[key] & mask) {
290 int bit;
291 BYTE *kptr;
293 kptr = &keyc->down[key >> 3];
294 bit = 1 << (key & 7);
296 if (*kptr & bit)
297 xnestQueueKeyEvent(KeyRelease, key);
299 if (--count == 0)
300 break;
304 /* Modifier shoud be down, but isn't
306 if (!(keyc->state & mask) && (state & mask))
307 for (key = 0; key < MAP_LENGTH; key++)
308 if (keyc->modifierMap[key] & mask) {
309 xnestQueueKeyEvent(KeyPress, key);
310 break;