/[rdesktop]/sourceforge.net/trunk/rdesktop/xkeymap.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /sourceforge.net/trunk/rdesktop/xkeymap.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 77 - (show annotations)
Mon Jul 29 20:17:10 2002 UTC (21 years, 9 months ago) by astrand
File MIME type: text/plain
File size: 10623 byte(s)
Indentation fixes

1 /*
2 rdesktop: A Remote Desktop Protocol client.
3 User interface services - X keyboard mapping
4 Copyright (C) Matthew Chapman 1999-2001
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <X11/Xlib.h>
22 #include <X11/keysym.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <limits.h>
28 #include "rdesktop.h"
29 #include "scancodes.h"
30
31 #define KEYMAP_SIZE 4096
32 #define KEYMAP_MASK (KEYMAP_SIZE - 1)
33 #define KEYMAP_MAX_LINE_LENGTH 80
34
35 extern Display *display;
36 extern char keymapname[16];
37 extern int keylayout;
38 extern BOOL enable_compose;
39
40 static key_translation keymap[KEYMAP_SIZE];
41 static int min_keycode;
42 static uint16 remote_modifier_state = 0;
43
44 static void
45 add_to_keymap(char *keyname, uint8 scancode, uint16 modifiers, char *mapname)
46 {
47 KeySym keysym;
48
49 keysym = XStringToKeysym(keyname);
50 if (keysym == NoSymbol)
51 {
52 error("Bad keysym %s in keymap %s\n", keyname, mapname);
53 return;
54 }
55
56 DEBUG_KBD("Adding translation, keysym=0x%x, scancode=0x%x, "
57 "modifiers=0x%x\n", (unsigned int) keysym, scancode,
58 modifiers);
59
60 keymap[keysym & KEYMAP_MASK].scancode = scancode;
61 keymap[keysym & KEYMAP_MASK].modifiers = modifiers;
62
63 return;
64 }
65
66
67 static BOOL
68 xkeymap_read(char *mapname)
69 {
70 FILE *fp;
71 char line[KEYMAP_MAX_LINE_LENGTH], path[PATH_MAX];
72 unsigned int line_num = 0;
73 unsigned int line_length = 0;
74 char *keyname, *p;
75 char *line_rest;
76 uint8 scancode;
77 uint16 modifiers;
78
79
80 strcpy(path, KEYMAP_PATH);
81 strncat(path, mapname, sizeof(path) - sizeof(KEYMAP_PATH));
82
83 fp = fopen(path, "r");
84 if (fp == NULL)
85 {
86 error("Failed to open keymap %s\n", path);
87 return False;
88 }
89
90 /* FIXME: More tolerant on white space */
91 while (fgets(line, sizeof(line), fp) != NULL)
92 {
93 line_num++;
94
95 /* Replace the \n with \0 */
96 p = strchr(line, '\n');
97 if (p != NULL)
98 *p = 0;
99
100 line_length = strlen(line);
101
102 /* Completely empty line */
103 if (strspn(line, " \t\n\r\f\v") == line_length)
104 {
105 continue;
106 }
107
108 /* Include */
109 if (strncmp(line, "include ", 8) == 0)
110 {
111 if (!xkeymap_read(line + 8))
112 return False;
113 continue;
114 }
115
116 /* map */
117 if (strncmp(line, "map ", 4) == 0)
118 {
119 keylayout = strtol(line + 4, NULL, 16);
120 DEBUG_KBD("Keylayout 0x%x\n", keylayout);
121 continue;
122 }
123
124 /* compose */
125 if (strncmp(line, "enable_compose", 15) == 0)
126 {
127 DEBUG_KBD("Enabling compose handling\n");
128 enable_compose = True;
129 continue;
130 }
131
132 /* Comment */
133 if (line[0] == '#')
134 {
135 continue;
136 }
137
138 /* Normal line */
139 keyname = line;
140 p = strchr(line, ' ');
141 if (p == NULL)
142 {
143 error("Bad line %d in keymap %s\n", line_num,
144 mapname);
145 continue;
146 }
147 else
148 {
149 *p = 0;
150 }
151
152 /* scancode */
153 p++;
154 scancode = strtol(p, &line_rest, 16);
155
156 /* flags */
157 /* FIXME: Should allow case-insensitive flag names.
158 Fix by using lex+yacc... */
159 modifiers = 0;
160 if (strstr(line_rest, "altgr"))
161 {
162 MASK_ADD_BITS(modifiers, MapAltGrMask);
163 }
164
165 if (strstr(line_rest, "shift"))
166 {
167 MASK_ADD_BITS(modifiers, MapLeftShiftMask);
168 }
169
170 if (strstr(line_rest, "numlock"))
171 {
172 MASK_ADD_BITS(modifiers, MapNumLockMask);
173 }
174
175 if (strstr(line_rest, "localstate"))
176 {
177 MASK_ADD_BITS(modifiers, MapLocalStateMask);
178 }
179
180 add_to_keymap(keyname, scancode, modifiers, mapname);
181
182 if (strstr(line_rest, "addupper"))
183 {
184 /* Automatically add uppercase key, with same modifiers
185 plus shift */
186 for (p = keyname; *p; p++)
187 *p = toupper(*p);
188 MASK_ADD_BITS(modifiers, MapLeftShiftMask);
189 add_to_keymap(keyname, scancode, modifiers, mapname);
190 }
191 }
192
193 fclose(fp);
194 return True;
195 }
196
197
198 /* Before connecting and creating UI */
199 void
200 xkeymap_init1(void)
201 {
202 int i;
203
204 /* Zeroing keymap */
205 for (i = 0; i < KEYMAP_SIZE; i++)
206 {
207 keymap[i].scancode = 0;
208 keymap[i].modifiers = 0;
209 }
210
211 if (strcmp(keymapname, "none"))
212 {
213 xkeymap_read(keymapname);
214 }
215
216 }
217
218 /* After connecting and creating UI */
219 void
220 xkeymap_init2(void)
221 {
222 unsigned int max_keycode;
223 XDisplayKeycodes(display, &min_keycode, (int *) &max_keycode);
224 }
225
226
227 key_translation
228 xkeymap_translate_key(KeySym keysym, unsigned int keycode, unsigned int state)
229 {
230 key_translation tr = { 0, 0 };
231
232 tr = keymap[keysym & KEYMAP_MASK];
233
234 if (tr.modifiers & MapLocalStateMask)
235 {
236 /* The modifiers to send for this key should be obtained
237 from the local state. Currently, only shift is implemented. */
238 if (state & ShiftMask)
239 {
240 tr.modifiers = MapLeftShiftMask;
241 }
242 }
243
244 if (tr.scancode != 0)
245 {
246 DEBUG_KBD
247 ("Found key translation, scancode=0x%x, modifiers=0x%x\n",
248 tr.scancode, tr.modifiers);
249 return tr;
250 }
251
252 printf("No translation for (keysym 0x%lx, %s)\n", keysym,
253 get_ksname(keysym));
254
255 /* not in keymap, try to interpret the raw scancode */
256 if ((keycode >= min_keycode) && (keycode <= 0x60))
257 {
258 tr.scancode = keycode - min_keycode;
259 printf("Sending guessed scancode 0x%x\n", tr.scancode);
260 }
261 else
262 {
263 printf("No good guess for keycode 0x%x found\n", keycode);
264 }
265
266 return tr;
267 }
268
269 uint16
270 xkeymap_translate_button(unsigned int button)
271 {
272 switch (button)
273 {
274 case Button1: /* left */
275 return MOUSE_FLAG_BUTTON1;
276 case Button2: /* middle */
277 return MOUSE_FLAG_BUTTON3;
278 case Button3: /* right */
279 return MOUSE_FLAG_BUTTON2;
280 case Button4: /* wheel up */
281 return MOUSE_FLAG_BUTTON4;
282 case Button5: /* wheel down */
283 return MOUSE_FLAG_BUTTON5;
284 }
285
286 return 0;
287 }
288
289 char *
290 get_ksname(KeySym keysym)
291 {
292 char *ksname = NULL;
293
294 if (keysym == NoSymbol)
295 ksname = "NoSymbol";
296 else if (!(ksname = XKeysymToString(keysym)))
297 ksname = "(no name)";
298
299 return ksname;
300 }
301
302 BOOL
303 inhibit_key(KeySym keysym)
304 {
305 switch (keysym)
306 {
307 case XK_Caps_Lock:
308 return True;
309 break;
310 case XK_Multi_key:
311 return True;
312 break;
313 default:
314 break;
315 }
316 return False;
317 }
318
319 void
320 ensure_remote_modifiers(uint32 ev_time, key_translation tr)
321 {
322 /* If this key is a modifier, do nothing */
323 switch (tr.scancode)
324 {
325 case SCANCODE_CHAR_LSHIFT:
326 case SCANCODE_CHAR_RSHIFT:
327 case SCANCODE_CHAR_LCTRL:
328 case SCANCODE_CHAR_RCTRL:
329 case SCANCODE_CHAR_LALT:
330 case SCANCODE_CHAR_RALT:
331 case SCANCODE_CHAR_LWIN:
332 case SCANCODE_CHAR_RWIN:
333 case SCANCODE_CHAR_NUMLOCK:
334 return;
335 default:
336 break;
337 }
338
339 /* Shift */
340 if (MASK_HAS_BITS(tr.modifiers, MapShiftMask)
341 != MASK_HAS_BITS(remote_modifier_state, MapShiftMask))
342 {
343 /* The remote modifier state is not correct */
344 if (MASK_HAS_BITS(tr.modifiers, MapShiftMask))
345 {
346 /* Needs this modifier. Send down. */
347 rdp_send_scancode(ev_time, RDP_KEYPRESS,
348 SCANCODE_CHAR_LSHIFT);
349 }
350 else
351 {
352 /* Should not use this modifier. Send up. */
353 rdp_send_scancode(ev_time, RDP_KEYRELEASE,
354 SCANCODE_CHAR_LSHIFT);
355 }
356 }
357
358 /* AltGr */
359 if (MASK_HAS_BITS(tr.modifiers, MapAltGrMask)
360 != MASK_HAS_BITS(remote_modifier_state, MapAltGrMask))
361 {
362 /* The remote modifier state is not correct */
363 if (MASK_HAS_BITS(tr.modifiers, MapAltGrMask))
364 {
365 /* Needs this modifier. Send down. */
366 rdp_send_scancode(ev_time, RDP_KEYPRESS,
367 SCANCODE_CHAR_RALT);
368 }
369 else
370 {
371 /* Should not use this modifier. Send up. */
372 rdp_send_scancode(ev_time, RDP_KEYRELEASE,
373 SCANCODE_CHAR_RALT);
374 }
375 }
376
377 /* NumLock */
378 if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask)
379 != MASK_HAS_BITS(remote_modifier_state, MapNumLockMask))
380 {
381 /* The remote modifier state is not correct */
382 DEBUG_KBD("Remote NumLock state is incorrect. Toggling\n");
383 if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask))
384 {
385 /* Needs this modifier. Toggle */
386 rdp_send_scancode(ev_time, RDP_KEYPRESS,
387 SCANCODE_CHAR_NUMLOCK);
388 rdp_send_scancode(ev_time, RDP_KEYRELEASE,
389 SCANCODE_CHAR_NUMLOCK);
390 }
391 else
392 {
393 /* Should not use this modifier. Toggle */
394 rdp_send_scancode(ev_time, RDP_KEYPRESS,
395 SCANCODE_CHAR_NUMLOCK);
396 rdp_send_scancode(ev_time, RDP_KEYRELEASE,
397 SCANCODE_CHAR_NUMLOCK);
398 }
399 }
400
401 }
402
403
404 static void
405 update_modifier_state(uint16 modifiers, BOOL pressed)
406 {
407
408 DEBUG_KBD("Before updating modifier_state:0x%x, pressed=0x%x\n",
409 remote_modifier_state, pressed);
410 switch (modifiers)
411 {
412 case SCANCODE_CHAR_LSHIFT:
413 MASK_CHANGE_BIT(remote_modifier_state,
414 MapLeftShiftMask, pressed);
415 break;
416 case SCANCODE_CHAR_RSHIFT:
417 MASK_CHANGE_BIT(remote_modifier_state,
418 MapRightShiftMask, pressed);
419 break;
420 case SCANCODE_CHAR_LCTRL:
421 MASK_CHANGE_BIT(remote_modifier_state,
422 MapLeftCtrlMask, pressed);
423 break;
424 case SCANCODE_CHAR_RCTRL:
425 MASK_CHANGE_BIT(remote_modifier_state,
426 MapRightCtrlMask, pressed);
427 break;
428 case SCANCODE_CHAR_LALT:
429 MASK_CHANGE_BIT(remote_modifier_state, MapLeftAltMask,
430 pressed);
431 break;
432 case SCANCODE_CHAR_RALT:
433 MASK_CHANGE_BIT(remote_modifier_state,
434 MapRightAltMask, pressed);
435 break;
436 case SCANCODE_CHAR_LWIN:
437 MASK_CHANGE_BIT(remote_modifier_state, MapLeftWinMask,
438 pressed);
439 break;
440 case SCANCODE_CHAR_RWIN:
441 MASK_CHANGE_BIT(remote_modifier_state,
442 MapRightWinMask, pressed);
443 break;
444 case SCANCODE_CHAR_NUMLOCK:
445 /* KeyReleases for NumLocks are sent immediately. Toggle the
446 modifier state only on Keypress */
447 if (pressed)
448 {
449 BOOL newNumLockState;
450 newNumLockState =
451 (MASK_HAS_BITS
452 (remote_modifier_state,
453 MapNumLockMask) == False);
454 MASK_CHANGE_BIT(remote_modifier_state,
455 MapNumLockMask,
456 newNumLockState);
457 }
458 break;
459 }
460 DEBUG_KBD("After updating modifier_state:0x%x\n",
461 remote_modifier_state);
462
463 }
464
465 /* Send keyboard input */
466 void
467 rdp_send_scancode(uint32 time, uint16 flags, uint16 scancode)
468 {
469 update_modifier_state(scancode, !(flags & RDP_KEYRELEASE));
470
471 if (scancode & SCANCODE_EXTENDED)
472 {
473 DEBUG_KBD("Sending extended scancode=0x%x, flags=0x%x\n",
474 scancode & ~SCANCODE_EXTENDED, flags);
475 rdp_send_input(time, RDP_INPUT_SCANCODE, flags | KBD_FLAG_EXT,
476 scancode & ~SCANCODE_EXTENDED, 0);
477 }
478 else
479 {
480 DEBUG_KBD("Sending scancode=0x%x, flags=0x%x\n", scancode,
481 flags);
482 rdp_send_input(time, RDP_INPUT_SCANCODE, flags, scancode, 0);
483 }
484 }

  ViewVC Help
Powered by ViewVC 1.1.26