/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 183 - (hide annotations)
Tue Sep 17 16:57:07 2002 UTC (21 years, 8 months ago) by astrand
File MIME type: text/plain
File size: 13249 byte(s)
Shift optimization: Either left or right shift is fine.

1 matthewc 38 /*
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 astrand 66 #include <ctype.h>
27 matthewc 39 #include <limits.h>
28 astrand 183 #include <assert.h>
29 matthewc 38 #include "rdesktop.h"
30 astrand 66 #include "scancodes.h"
31 matthewc 38
32     #define KEYMAP_SIZE 4096
33     #define KEYMAP_MASK (KEYMAP_SIZE - 1)
34 astrand 66 #define KEYMAP_MAX_LINE_LENGTH 80
35 matthewc 38
36 matthewc 50 extern Display *display;
37 matthewc 38 extern char keymapname[16];
38     extern int keylayout;
39 astrand 70 extern BOOL enable_compose;
40 matthewc 38
41 astrand 66 static key_translation keymap[KEYMAP_SIZE];
42 astrand 73 static int min_keycode;
43 astrand 66 static uint16 remote_modifier_state = 0;
44 matthewc 38
45 astrand 115 static void update_modifier_state(uint16 modifiers, BOOL pressed);
46    
47 astrand 66 static void
48     add_to_keymap(char *keyname, uint8 scancode, uint16 modifiers, char *mapname)
49     {
50     KeySym keysym;
51    
52     keysym = XStringToKeysym(keyname);
53     if (keysym == NoSymbol)
54     {
55     error("Bad keysym %s in keymap %s\n", keyname, mapname);
56     return;
57     }
58    
59 astrand 84 DEBUG_KBD(("Adding translation, keysym=0x%x, scancode=0x%x, "
60     "modifiers=0x%x\n", (unsigned int) keysym, scancode, modifiers));
61 astrand 66
62     keymap[keysym & KEYMAP_MASK].scancode = scancode;
63     keymap[keysym & KEYMAP_MASK].modifiers = modifiers;
64    
65     return;
66     }
67    
68    
69 astrand 64 static BOOL
70     xkeymap_read(char *mapname)
71 matthewc 38 {
72     FILE *fp;
73 astrand 66 char line[KEYMAP_MAX_LINE_LENGTH], path[PATH_MAX];
74     unsigned int line_num = 0;
75     unsigned int line_length = 0;
76 matthewc 38 char *keyname, *p;
77 astrand 66 char *line_rest;
78     uint8 scancode;
79     uint16 modifiers;
80 matthewc 38
81 astrand 66
82 matthewc 38 strcpy(path, KEYMAP_PATH);
83     strncat(path, mapname, sizeof(path) - sizeof(KEYMAP_PATH));
84    
85     fp = fopen(path, "r");
86     if (fp == NULL)
87     {
88     error("Failed to open keymap %s\n", path);
89     return False;
90     }
91    
92 astrand 66 /* FIXME: More tolerant on white space */
93 matthewc 38 while (fgets(line, sizeof(line), fp) != NULL)
94     {
95 astrand 66 line_num++;
96    
97     /* Replace the \n with \0 */
98 matthewc 38 p = strchr(line, '\n');
99     if (p != NULL)
100     *p = 0;
101    
102 astrand 66 line_length = strlen(line);
103    
104     /* Completely empty line */
105     if (strspn(line, " \t\n\r\f\v") == line_length)
106 matthewc 38 {
107 astrand 66 continue;
108     }
109 matthewc 38
110 astrand 66 /* Include */
111     if (strncmp(line, "include ", 8) == 0)
112 matthewc 38 {
113 astrand 64 if (!xkeymap_read(line + 8))
114 matthewc 38 return False;
115 astrand 66 continue;
116 matthewc 38 }
117 astrand 66
118     /* map */
119     if (strncmp(line, "map ", 4) == 0)
120 matthewc 38 {
121 astrand 64 keylayout = strtol(line + 4, NULL, 16);
122 astrand 84 DEBUG_KBD(("Keylayout 0x%x\n", keylayout));
123 astrand 66 continue;
124 matthewc 38 }
125 astrand 66
126 astrand 70 /* compose */
127     if (strncmp(line, "enable_compose", 15) == 0)
128     {
129 astrand 84 DEBUG_KBD(("Enabling compose handling\n"));
130 astrand 70 enable_compose = True;
131     continue;
132     }
133    
134 astrand 66 /* Comment */
135     if (line[0] == '#')
136 matthewc 38 {
137 astrand 66 continue;
138 matthewc 38 }
139 astrand 66
140     /* Normal line */
141     keyname = line;
142     p = strchr(line, ' ');
143     if (p == NULL)
144     {
145 astrand 82 error("Bad line %d in keymap %s\n", line_num, mapname);
146 astrand 66 continue;
147     }
148     else
149     {
150     *p = 0;
151     }
152    
153     /* scancode */
154     p++;
155     scancode = strtol(p, &line_rest, 16);
156    
157     /* flags */
158     /* FIXME: Should allow case-insensitive flag names.
159     Fix by using lex+yacc... */
160     modifiers = 0;
161     if (strstr(line_rest, "altgr"))
162     {
163     MASK_ADD_BITS(modifiers, MapAltGrMask);
164     }
165    
166     if (strstr(line_rest, "shift"))
167     {
168     MASK_ADD_BITS(modifiers, MapLeftShiftMask);
169     }
170    
171     if (strstr(line_rest, "numlock"))
172     {
173     MASK_ADD_BITS(modifiers, MapNumLockMask);
174     }
175    
176 astrand 69 if (strstr(line_rest, "localstate"))
177     {
178     MASK_ADD_BITS(modifiers, MapLocalStateMask);
179     }
180    
181 astrand 116 if (strstr(line_rest, "inhibit"))
182     {
183     MASK_ADD_BITS(modifiers, MapInhibitMask);
184     }
185    
186 astrand 66 add_to_keymap(keyname, scancode, modifiers, mapname);
187    
188     if (strstr(line_rest, "addupper"))
189     {
190     /* Automatically add uppercase key, with same modifiers
191     plus shift */
192     for (p = keyname; *p; p++)
193     *p = toupper(*p);
194     MASK_ADD_BITS(modifiers, MapLeftShiftMask);
195     add_to_keymap(keyname, scancode, modifiers, mapname);
196     }
197 matthewc 38 }
198    
199     fclose(fp);
200     return True;
201     }
202    
203 astrand 66
204     /* Before connecting and creating UI */
205 astrand 64 void
206 matthewc 121 xkeymap_init(void)
207 matthewc 38 {
208 matthewc 121 unsigned int max_keycode;
209 astrand 66 int i;
210 matthewc 38
211     if (strcmp(keymapname, "none"))
212     xkeymap_read(keymapname);
213 astrand 66
214 astrand 77 XDisplayKeycodes(display, &min_keycode, (int *) &max_keycode);
215 astrand 66 }
216 matthewc 38
217 astrand 118 /* Handles, for example, multi-scancode keypresses (which is not
218     possible via keymap-files) */
219     BOOL
220     handle_special_keys(KeySym keysym, uint32 ev_time, BOOL pressed)
221     {
222     switch (keysym)
223     {
224     case XK_Break: /* toggle full screen */
225     if (pressed && (get_key_state(XK_Alt_L) || get_key_state(XK_Alt_R)))
226     {
227 matthewc 123 xwin_toggle_fullscreen();
228 astrand 118 return True;
229     }
230     break;
231 astrand 66
232 astrand 118 case XK_Meta_L: /* Windows keys */
233     case XK_Super_L:
234     case XK_Hyper_L:
235     case XK_Meta_R:
236     case XK_Super_R:
237     case XK_Hyper_R:
238     if (pressed)
239     {
240     rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_LCTRL);
241     rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_ESC);
242     }
243     else
244     {
245     rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_ESC);
246     rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL);
247     }
248     return True;
249     break;
250     }
251     return False;
252     }
253    
254    
255 astrand 66 key_translation
256 astrand 69 xkeymap_translate_key(KeySym keysym, unsigned int keycode, unsigned int state)
257 astrand 66 {
258     key_translation tr = { 0, 0 };
259    
260     tr = keymap[keysym & KEYMAP_MASK];
261    
262 astrand 116 if (tr.modifiers & MapInhibitMask)
263     {
264     DEBUG_KBD(("Inhibiting key\n"));
265     tr.scancode = 0;
266     return tr;
267     }
268    
269 astrand 69 if (tr.modifiers & MapLocalStateMask)
270     {
271     /* The modifiers to send for this key should be obtained
272     from the local state. Currently, only shift is implemented. */
273     if (state & ShiftMask)
274     {
275     tr.modifiers = MapLeftShiftMask;
276     }
277     }
278    
279 astrand 66 if (tr.scancode != 0)
280 matthewc 50 {
281 astrand 66 DEBUG_KBD
282 astrand 84 (("Found key translation, scancode=0x%x, modifiers=0x%x\n",
283     tr.scancode, tr.modifiers));
284 astrand 66 return tr;
285 matthewc 50 }
286    
287 astrand 111 fprintf(stderr, "No translation for (keysym 0x%lx, %s)\n", keysym, get_ksname(keysym));
288 astrand 66
289 matthewc 38 /* not in keymap, try to interpret the raw scancode */
290     if ((keycode >= min_keycode) && (keycode <= 0x60))
291     {
292 astrand 66 tr.scancode = keycode - min_keycode;
293 astrand 165
294     /* The modifiers to send for this key should be
295     obtained from the local state. Currently, only
296     shift is implemented. */
297     if (state & ShiftMask)
298     {
299     tr.modifiers = MapLeftShiftMask;
300     }
301    
302 astrand 111 fprintf(stderr, "Sending guessed scancode 0x%x\n", tr.scancode);
303 matthewc 38 }
304 astrand 66 else
305     {
306 astrand 111 fprintf(stderr, "No good guess for keycode 0x%x found\n", keycode);
307 astrand 66 }
308 matthewc 38
309 astrand 66 return tr;
310 matthewc 38 }
311    
312 astrand 64 uint16
313     xkeymap_translate_button(unsigned int button)
314 matthewc 38 {
315     switch (button)
316     {
317 astrand 64 case Button1: /* left */
318 matthewc 38 return MOUSE_FLAG_BUTTON1;
319 astrand 64 case Button2: /* middle */
320 matthewc 38 return MOUSE_FLAG_BUTTON3;
321 astrand 64 case Button3: /* right */
322 matthewc 38 return MOUSE_FLAG_BUTTON2;
323 jsorg71 67 case Button4: /* wheel up */
324     return MOUSE_FLAG_BUTTON4;
325     case Button5: /* wheel down */
326     return MOUSE_FLAG_BUTTON5;
327 matthewc 38 }
328    
329     return 0;
330     }
331 astrand 66
332     char *
333     get_ksname(KeySym keysym)
334     {
335     char *ksname = NULL;
336    
337     if (keysym == NoSymbol)
338     ksname = "NoSymbol";
339     else if (!(ksname = XKeysymToString(keysym)))
340     ksname = "(no name)";
341    
342     return ksname;
343     }
344    
345    
346     void
347     ensure_remote_modifiers(uint32 ev_time, key_translation tr)
348     {
349     /* If this key is a modifier, do nothing */
350     switch (tr.scancode)
351     {
352     case SCANCODE_CHAR_LSHIFT:
353     case SCANCODE_CHAR_RSHIFT:
354     case SCANCODE_CHAR_LCTRL:
355     case SCANCODE_CHAR_RCTRL:
356     case SCANCODE_CHAR_LALT:
357     case SCANCODE_CHAR_RALT:
358     case SCANCODE_CHAR_LWIN:
359     case SCANCODE_CHAR_RWIN:
360     case SCANCODE_CHAR_NUMLOCK:
361     return;
362     default:
363     break;
364     }
365    
366 astrand 183 /* Shift. Left shift and right shift are treated as equal; either is fine. */
367 astrand 66 if (MASK_HAS_BITS(tr.modifiers, MapShiftMask)
368     != MASK_HAS_BITS(remote_modifier_state, MapShiftMask))
369     {
370     /* The remote modifier state is not correct */
371 astrand 183 if (MASK_HAS_BITS(tr.modifiers, MapLeftShiftMask))
372 astrand 66 {
373 astrand 183 /* Needs left shift. Send down. */
374 astrand 82 rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_LSHIFT);
375 astrand 66 }
376 astrand 183 else if (MASK_HAS_BITS(tr.modifiers, MapRightShiftMask))
377     {
378     /* Needs right shift. Send down. */
379     rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_RSHIFT);
380     }
381 astrand 66 else
382     {
383 astrand 183 /* Should not use this modifier. Send up for shift currently pressed. */
384     if (MASK_HAS_BITS(remote_modifier_state, MapLeftShiftMask))
385     /* Left shift is down */
386     rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT);
387     else
388     {
389     assert(MASK_HAS_BITS(remote_modifier_state, MapRightShiftMask));
390     /* Right shift is down */
391     rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RSHIFT);
392     }
393    
394 astrand 66 }
395     }
396    
397     /* AltGr */
398     if (MASK_HAS_BITS(tr.modifiers, MapAltGrMask)
399     != MASK_HAS_BITS(remote_modifier_state, MapAltGrMask))
400     {
401     /* The remote modifier state is not correct */
402     if (MASK_HAS_BITS(tr.modifiers, MapAltGrMask))
403     {
404     /* Needs this modifier. Send down. */
405 astrand 82 rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_RALT);
406 astrand 66 }
407     else
408     {
409     /* Should not use this modifier. Send up. */
410 astrand 82 rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT);
411 astrand 66 }
412     }
413    
414     /* NumLock */
415     if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask)
416     != MASK_HAS_BITS(remote_modifier_state, MapNumLockMask))
417     {
418     /* The remote modifier state is not correct */
419 astrand 115 uint16 new_remote_state = 0;
420    
421 astrand 66 if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask))
422     {
423 astrand 115 DEBUG_KBD(("Remote NumLock state is incorrect, activating NumLock.\n"));
424     new_remote_state |= KBD_FLAG_NUMLOCK;
425 astrand 66 }
426     else
427     {
428 astrand 115 DEBUG_KBD(("Remote NumLock state is incorrect, deactivating NumLock.\n"));
429 astrand 66 }
430 astrand 115
431     rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0, new_remote_state, 0);
432     update_modifier_state(SCANCODE_CHAR_NUMLOCK, True);
433 astrand 66 }
434     }
435    
436    
437 astrand 170 void
438     reset_modifier_keys()
439     {
440     /* reset keys */
441     uint32 ev_time;
442     ev_time = time(NULL);
443    
444     if (MASK_HAS_BITS(remote_modifier_state, MapLeftShiftMask) && !get_key_state(XK_Shift_L))
445     rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT);
446    
447     if (MASK_HAS_BITS(remote_modifier_state, MapRightShiftMask) && !get_key_state(XK_Shift_R))
448     rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RSHIFT);
449    
450     if (MASK_HAS_BITS(remote_modifier_state, MapLeftCtrlMask) && !get_key_state(XK_Control_L))
451     rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL);
452    
453     if (MASK_HAS_BITS(remote_modifier_state, MapRightCtrlMask) && !get_key_state(XK_Control_R))
454     rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RCTRL);
455    
456     if (MASK_HAS_BITS(remote_modifier_state, MapLeftAltMask) && !get_key_state(XK_Alt_L))
457     rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LALT);
458    
459     if (MASK_HAS_BITS(remote_modifier_state, MapRightAltMask) &&
460     !get_key_state(XK_Alt_R) && !get_key_state(XK_Mode_switch))
461     rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT);
462     }
463    
464    
465 astrand 66 static void
466     update_modifier_state(uint16 modifiers, BOOL pressed)
467     {
468 astrand 113 #ifdef WITH_DEBUG_KBD
469     uint16 old_modifier_state;
470 astrand 66
471 astrand 113 old_modifier_state = remote_modifier_state;
472     #endif
473    
474 astrand 66 switch (modifiers)
475     {
476     case SCANCODE_CHAR_LSHIFT:
477 astrand 82 MASK_CHANGE_BIT(remote_modifier_state, MapLeftShiftMask, pressed);
478 astrand 66 break;
479     case SCANCODE_CHAR_RSHIFT:
480 astrand 82 MASK_CHANGE_BIT(remote_modifier_state, MapRightShiftMask, pressed);
481 astrand 66 break;
482     case SCANCODE_CHAR_LCTRL:
483 astrand 82 MASK_CHANGE_BIT(remote_modifier_state, MapLeftCtrlMask, pressed);
484 astrand 66 break;
485     case SCANCODE_CHAR_RCTRL:
486 astrand 82 MASK_CHANGE_BIT(remote_modifier_state, MapRightCtrlMask, pressed);
487 astrand 66 break;
488     case SCANCODE_CHAR_LALT:
489 astrand 82 MASK_CHANGE_BIT(remote_modifier_state, MapLeftAltMask, pressed);
490 astrand 66 break;
491     case SCANCODE_CHAR_RALT:
492 astrand 82 MASK_CHANGE_BIT(remote_modifier_state, MapRightAltMask, pressed);
493 astrand 66 break;
494     case SCANCODE_CHAR_LWIN:
495 astrand 82 MASK_CHANGE_BIT(remote_modifier_state, MapLeftWinMask, pressed);
496 astrand 66 break;
497     case SCANCODE_CHAR_RWIN:
498 astrand 82 MASK_CHANGE_BIT(remote_modifier_state, MapRightWinMask, pressed);
499 astrand 66 break;
500     case SCANCODE_CHAR_NUMLOCK:
501     /* KeyReleases for NumLocks are sent immediately. Toggle the
502     modifier state only on Keypress */
503     if (pressed)
504     {
505     BOOL newNumLockState;
506     newNumLockState =
507     (MASK_HAS_BITS
508 astrand 82 (remote_modifier_state, MapNumLockMask) == False);
509 astrand 66 MASK_CHANGE_BIT(remote_modifier_state,
510 astrand 82 MapNumLockMask, newNumLockState);
511 astrand 66 }
512     break;
513     }
514    
515 astrand 113 #ifdef WITH_DEBUG_KBD
516     if (old_modifier_state != remote_modifier_state)
517     {
518     DEBUG_KBD(("Before updating modifier_state:0x%x, pressed=0x%x\n",
519     old_modifier_state, pressed));
520     DEBUG_KBD(("After updating modifier_state:0x%x\n", remote_modifier_state));
521     }
522     #endif
523    
524 astrand 66 }
525    
526     /* Send keyboard input */
527     void
528     rdp_send_scancode(uint32 time, uint16 flags, uint16 scancode)
529     {
530     update_modifier_state(scancode, !(flags & RDP_KEYRELEASE));
531    
532     if (scancode & SCANCODE_EXTENDED)
533     {
534 astrand 84 DEBUG_KBD(("Sending extended scancode=0x%x, flags=0x%x\n",
535     scancode & ~SCANCODE_EXTENDED, flags));
536 astrand 66 rdp_send_input(time, RDP_INPUT_SCANCODE, flags | KBD_FLAG_EXT,
537     scancode & ~SCANCODE_EXTENDED, 0);
538     }
539     else
540     {
541 astrand 85 DEBUG_KBD(("Sending scancode=0x%x, flags=0x%x\n", scancode, flags));
542     rdp_send_input(time, RDP_INPUT_SCANCODE, flags, scancode, 0);
543 astrand 66 }
544     }

  ViewVC Help
Powered by ViewVC 1.1.26