/[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 66 - (hide annotations)
Thu Jul 18 18:28:12 2002 UTC (21 years, 10 months ago) by astrand
File MIME type: text/plain
File size: 9957 byte(s)
Merged new keysym-based keyboard handling (from alt. CVS repos)

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

  ViewVC Help
Powered by ViewVC 1.1.26