/[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 82 - (hide annotations)
Tue Jul 30 07:18:48 2002 UTC (21 years, 9 months ago) by astrand
File MIME type: text/plain
File size: 10484 byte(s)
Changed max line length to 100

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 astrand 70 extern BOOL enable_compose;
39 matthewc 38
40 astrand 66 static key_translation keymap[KEYMAP_SIZE];
41 astrand 73 static int min_keycode;
42 astrand 66 static uint16 remote_modifier_state = 0;
43 matthewc 38
44 astrand 66 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 astrand 82 "modifiers=0x%x\n", (unsigned int) keysym, scancode, modifiers);
58 astrand 66
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 astrand 70 /* compose */
124     if (strncmp(line, "enable_compose", 15) == 0)
125     {
126     DEBUG_KBD("Enabling compose handling\n");
127     enable_compose = True;
128     continue;
129     }
130    
131 astrand 66 /* Comment */
132     if (line[0] == '#')
133 matthewc 38 {
134 astrand 66 continue;
135 matthewc 38 }
136 astrand 66
137     /* Normal line */
138     keyname = line;
139     p = strchr(line, ' ');
140     if (p == NULL)
141     {
142 astrand 82 error("Bad line %d in keymap %s\n", line_num, mapname);
143 astrand 66 continue;
144     }
145     else
146     {
147     *p = 0;
148     }
149    
150     /* scancode */
151     p++;
152     scancode = strtol(p, &line_rest, 16);
153    
154     /* flags */
155     /* FIXME: Should allow case-insensitive flag names.
156     Fix by using lex+yacc... */
157     modifiers = 0;
158     if (strstr(line_rest, "altgr"))
159     {
160     MASK_ADD_BITS(modifiers, MapAltGrMask);
161     }
162    
163     if (strstr(line_rest, "shift"))
164     {
165     MASK_ADD_BITS(modifiers, MapLeftShiftMask);
166     }
167    
168     if (strstr(line_rest, "numlock"))
169     {
170     MASK_ADD_BITS(modifiers, MapNumLockMask);
171     }
172    
173 astrand 69 if (strstr(line_rest, "localstate"))
174     {
175     MASK_ADD_BITS(modifiers, MapLocalStateMask);
176     }
177    
178 astrand 66 add_to_keymap(keyname, scancode, modifiers, mapname);
179    
180     if (strstr(line_rest, "addupper"))
181     {
182     /* Automatically add uppercase key, with same modifiers
183     plus shift */
184     for (p = keyname; *p; p++)
185     *p = toupper(*p);
186     MASK_ADD_BITS(modifiers, MapLeftShiftMask);
187     add_to_keymap(keyname, scancode, modifiers, mapname);
188     }
189 matthewc 38 }
190    
191     fclose(fp);
192     return True;
193     }
194    
195 astrand 66
196     /* Before connecting and creating UI */
197 astrand 64 void
198 astrand 66 xkeymap_init1(void)
199 matthewc 38 {
200 astrand 66 int i;
201 matthewc 38
202 astrand 66 /* Zeroing keymap */
203     for (i = 0; i < KEYMAP_SIZE; i++)
204     {
205     keymap[i].scancode = 0;
206     keymap[i].modifiers = 0;
207     }
208 matthewc 38
209     if (strcmp(keymapname, "none"))
210 astrand 66 {
211 matthewc 38 xkeymap_read(keymapname);
212 astrand 66 }
213    
214 matthewc 38 }
215    
216 astrand 66 /* After connecting and creating UI */
217     void
218     xkeymap_init2(void)
219 matthewc 38 {
220 astrand 66 unsigned int max_keycode;
221 astrand 77 XDisplayKeycodes(display, &min_keycode, (int *) &max_keycode);
222 astrand 66 }
223 matthewc 38
224 astrand 66
225     key_translation
226 astrand 69 xkeymap_translate_key(KeySym keysym, unsigned int keycode, unsigned int state)
227 astrand 66 {
228     key_translation tr = { 0, 0 };
229    
230     tr = keymap[keysym & KEYMAP_MASK];
231    
232 astrand 69 if (tr.modifiers & MapLocalStateMask)
233     {
234     /* The modifiers to send for this key should be obtained
235     from the local state. Currently, only shift is implemented. */
236     if (state & ShiftMask)
237     {
238     tr.modifiers = MapLeftShiftMask;
239     }
240     }
241    
242 astrand 66 if (tr.scancode != 0)
243 matthewc 50 {
244 astrand 66 DEBUG_KBD
245     ("Found key translation, scancode=0x%x, modifiers=0x%x\n",
246     tr.scancode, tr.modifiers);
247     return tr;
248 matthewc 50 }
249    
250 astrand 82 printf("No translation for (keysym 0x%lx, %s)\n", keysym, get_ksname(keysym));
251 astrand 66
252 matthewc 38 /* not in keymap, try to interpret the raw scancode */
253     if ((keycode >= min_keycode) && (keycode <= 0x60))
254     {
255 astrand 66 tr.scancode = keycode - min_keycode;
256     printf("Sending guessed scancode 0x%x\n", tr.scancode);
257 matthewc 38 }
258 astrand 66 else
259     {
260     printf("No good guess for keycode 0x%x found\n", keycode);
261     }
262 matthewc 38
263 astrand 66 return tr;
264 matthewc 38 }
265    
266 astrand 64 uint16
267     xkeymap_translate_button(unsigned int button)
268 matthewc 38 {
269     switch (button)
270     {
271 astrand 64 case Button1: /* left */
272 matthewc 38 return MOUSE_FLAG_BUTTON1;
273 astrand 64 case Button2: /* middle */
274 matthewc 38 return MOUSE_FLAG_BUTTON3;
275 astrand 64 case Button3: /* right */
276 matthewc 38 return MOUSE_FLAG_BUTTON2;
277 jsorg71 67 case Button4: /* wheel up */
278     return MOUSE_FLAG_BUTTON4;
279     case Button5: /* wheel down */
280     return MOUSE_FLAG_BUTTON5;
281 matthewc 38 }
282    
283     return 0;
284     }
285 astrand 66
286     char *
287     get_ksname(KeySym keysym)
288     {
289     char *ksname = NULL;
290    
291     if (keysym == NoSymbol)
292     ksname = "NoSymbol";
293     else if (!(ksname = XKeysymToString(keysym)))
294     ksname = "(no name)";
295    
296     return ksname;
297     }
298    
299     BOOL
300     inhibit_key(KeySym keysym)
301     {
302     switch (keysym)
303     {
304     case XK_Caps_Lock:
305     return True;
306     break;
307     case XK_Multi_key:
308     return True;
309     break;
310     default:
311     break;
312     }
313     return False;
314     }
315    
316     void
317     ensure_remote_modifiers(uint32 ev_time, key_translation tr)
318     {
319     /* If this key is a modifier, do nothing */
320     switch (tr.scancode)
321     {
322     case SCANCODE_CHAR_LSHIFT:
323     case SCANCODE_CHAR_RSHIFT:
324     case SCANCODE_CHAR_LCTRL:
325     case SCANCODE_CHAR_RCTRL:
326     case SCANCODE_CHAR_LALT:
327     case SCANCODE_CHAR_RALT:
328     case SCANCODE_CHAR_LWIN:
329     case SCANCODE_CHAR_RWIN:
330     case SCANCODE_CHAR_NUMLOCK:
331     return;
332     default:
333     break;
334     }
335    
336     /* Shift */
337     if (MASK_HAS_BITS(tr.modifiers, MapShiftMask)
338     != MASK_HAS_BITS(remote_modifier_state, MapShiftMask))
339     {
340     /* The remote modifier state is not correct */
341     if (MASK_HAS_BITS(tr.modifiers, MapShiftMask))
342     {
343     /* Needs this modifier. Send down. */
344 astrand 82 rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_LSHIFT);
345 astrand 66 }
346     else
347     {
348     /* Should not use this modifier. Send up. */
349 astrand 82 rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT);
350 astrand 66 }
351     }
352    
353     /* AltGr */
354     if (MASK_HAS_BITS(tr.modifiers, MapAltGrMask)
355     != MASK_HAS_BITS(remote_modifier_state, MapAltGrMask))
356     {
357     /* The remote modifier state is not correct */
358     if (MASK_HAS_BITS(tr.modifiers, MapAltGrMask))
359     {
360     /* Needs this modifier. Send down. */
361 astrand 82 rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_RALT);
362 astrand 66 }
363     else
364     {
365     /* Should not use this modifier. Send up. */
366 astrand 82 rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT);
367 astrand 66 }
368     }
369    
370     /* NumLock */
371     if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask)
372     != MASK_HAS_BITS(remote_modifier_state, MapNumLockMask))
373     {
374     /* The remote modifier state is not correct */
375     DEBUG_KBD("Remote NumLock state is incorrect. Toggling\n");
376     if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask))
377     {
378     /* Needs this modifier. Toggle */
379 astrand 82 rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_NUMLOCK);
380     rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_NUMLOCK);
381 astrand 66 }
382     else
383     {
384     /* Should not use this modifier. Toggle */
385 astrand 82 rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_NUMLOCK);
386     rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_NUMLOCK);
387 astrand 66 }
388     }
389    
390     }
391    
392    
393     static void
394     update_modifier_state(uint16 modifiers, BOOL pressed)
395     {
396    
397     DEBUG_KBD("Before updating modifier_state:0x%x, pressed=0x%x\n",
398     remote_modifier_state, pressed);
399     switch (modifiers)
400     {
401     case SCANCODE_CHAR_LSHIFT:
402 astrand 82 MASK_CHANGE_BIT(remote_modifier_state, MapLeftShiftMask, pressed);
403 astrand 66 break;
404     case SCANCODE_CHAR_RSHIFT:
405 astrand 82 MASK_CHANGE_BIT(remote_modifier_state, MapRightShiftMask, pressed);
406 astrand 66 break;
407     case SCANCODE_CHAR_LCTRL:
408 astrand 82 MASK_CHANGE_BIT(remote_modifier_state, MapLeftCtrlMask, pressed);
409 astrand 66 break;
410     case SCANCODE_CHAR_RCTRL:
411 astrand 82 MASK_CHANGE_BIT(remote_modifier_state, MapRightCtrlMask, pressed);
412 astrand 66 break;
413     case SCANCODE_CHAR_LALT:
414 astrand 82 MASK_CHANGE_BIT(remote_modifier_state, MapLeftAltMask, pressed);
415 astrand 66 break;
416     case SCANCODE_CHAR_RALT:
417 astrand 82 MASK_CHANGE_BIT(remote_modifier_state, MapRightAltMask, pressed);
418 astrand 66 break;
419     case SCANCODE_CHAR_LWIN:
420 astrand 82 MASK_CHANGE_BIT(remote_modifier_state, MapLeftWinMask, pressed);
421 astrand 66 break;
422     case SCANCODE_CHAR_RWIN:
423 astrand 82 MASK_CHANGE_BIT(remote_modifier_state, MapRightWinMask, pressed);
424 astrand 66 break;
425     case SCANCODE_CHAR_NUMLOCK:
426     /* KeyReleases for NumLocks are sent immediately. Toggle the
427     modifier state only on Keypress */
428     if (pressed)
429     {
430     BOOL newNumLockState;
431     newNumLockState =
432     (MASK_HAS_BITS
433 astrand 82 (remote_modifier_state, MapNumLockMask) == False);
434 astrand 66 MASK_CHANGE_BIT(remote_modifier_state,
435 astrand 82 MapNumLockMask, newNumLockState);
436 astrand 66 }
437     break;
438     }
439 astrand 82 DEBUG_KBD("After updating modifier_state:0x%x\n", remote_modifier_state);
440 astrand 66
441     }
442    
443     /* Send keyboard input */
444     void
445     rdp_send_scancode(uint32 time, uint16 flags, uint16 scancode)
446     {
447     update_modifier_state(scancode, !(flags & RDP_KEYRELEASE));
448    
449     if (scancode & SCANCODE_EXTENDED)
450     {
451     DEBUG_KBD("Sending extended scancode=0x%x, flags=0x%x\n",
452     scancode & ~SCANCODE_EXTENDED, flags);
453     rdp_send_input(time, RDP_INPUT_SCANCODE, flags | KBD_FLAG_EXT,
454     scancode & ~SCANCODE_EXTENDED, 0);
455     }
456     else
457     {
458 astrand 82 DEBUG_KBD("Sending scancode=0x%x, flags=0x%x\n", scancode, flags);
459 astrand 66 rdp_send_input(time, RDP_INPUT_SCANCODE, flags, scancode, 0);
460     }
461     }

  ViewVC Help
Powered by ViewVC 1.1.26