/[rdesktop]/jpeg/rdesktop/trunk/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 /jpeg/rdesktop/trunk/xkeymap.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 73 - (hide annotations)
Mon Jul 29 19:21:51 2002 UTC (21 years, 9 months ago) by astrand
Original Path: sourceforge.net/trunk/rdesktop/xkeymap.c
File MIME type: text/plain
File size: 10622 byte(s)
Applied patch from Bob Bell (#2)

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     "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 astrand 64 static BOOL
68     xkeymap_read(char *mapname)
69 matthewc 38 {
70     FILE *fp;
71 astrand 66 char line[KEYMAP_MAX_LINE_LENGTH], path[PATH_MAX];
72     unsigned int line_num = 0;
73     unsigned int line_length = 0;
74 matthewc 38 char *keyname, *p;
75 astrand 66 char *line_rest;
76     uint8 scancode;
77     uint16 modifiers;
78 matthewc 38
79 astrand 66
80 matthewc 38 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 astrand 66 /* FIXME: More tolerant on white space */
91 matthewc 38 while (fgets(line, sizeof(line), fp) != NULL)
92     {
93 astrand 66 line_num++;
94    
95     /* Replace the \n with \0 */
96 matthewc 38 p = strchr(line, '\n');
97     if (p != NULL)
98     *p = 0;
99    
100 astrand 66 line_length = strlen(line);
101    
102     /* Completely empty line */
103     if (strspn(line, " \t\n\r\f\v") == line_length)
104 matthewc 38 {
105 astrand 66 continue;
106     }
107 matthewc 38
108 astrand 66 /* Include */
109     if (strncmp(line, "include ", 8) == 0)
110 matthewc 38 {
111 astrand 64 if (!xkeymap_read(line + 8))
112 matthewc 38 return False;
113 astrand 66 continue;
114 matthewc 38 }
115 astrand 66
116     /* map */
117     if (strncmp(line, "map ", 4) == 0)
118 matthewc 38 {
119 astrand 64 keylayout = strtol(line + 4, NULL, 16);
120 astrand 66 DEBUG_KBD("Keylayout 0x%x\n", keylayout);
121     continue;
122 matthewc 38 }
123 astrand 66
124 astrand 70 /* 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 astrand 66 /* Comment */
133     if (line[0] == '#')
134 matthewc 38 {
135 astrand 66 continue;
136 matthewc 38 }
137 astrand 66
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 astrand 69 if (strstr(line_rest, "localstate"))
176     {
177     MASK_ADD_BITS(modifiers, MapLocalStateMask);
178     }
179    
180 astrand 66 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 matthewc 38 }
192    
193     fclose(fp);
194     return True;
195     }
196    
197 astrand 66
198     /* Before connecting and creating UI */
199 astrand 64 void
200 astrand 66 xkeymap_init1(void)
201 matthewc 38 {
202 astrand 66 int i;
203 matthewc 38
204 astrand 66 /* Zeroing keymap */
205     for (i = 0; i < KEYMAP_SIZE; i++)
206     {
207     keymap[i].scancode = 0;
208     keymap[i].modifiers = 0;
209     }
210 matthewc 38
211     if (strcmp(keymapname, "none"))
212 astrand 66 {
213 matthewc 38 xkeymap_read(keymapname);
214 astrand 66 }
215    
216 matthewc 38 }
217    
218 astrand 66 /* After connecting and creating UI */
219     void
220     xkeymap_init2(void)
221 matthewc 38 {
222 astrand 66 unsigned int max_keycode;
223 astrand 73 XDisplayKeycodes(display, &min_keycode, (int *)&max_keycode);
224 astrand 66 }
225 matthewc 38
226 astrand 66
227     key_translation
228 astrand 69 xkeymap_translate_key(KeySym keysym, unsigned int keycode, unsigned int state)
229 astrand 66 {
230     key_translation tr = { 0, 0 };
231    
232     tr = keymap[keysym & KEYMAP_MASK];
233    
234 astrand 69 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 astrand 66 if (tr.scancode != 0)
245 matthewc 50 {
246 astrand 66 DEBUG_KBD
247     ("Found key translation, scancode=0x%x, modifiers=0x%x\n",
248     tr.scancode, tr.modifiers);
249     return tr;
250 matthewc 50 }
251    
252 astrand 66 printf("No translation for (keysym 0x%lx, %s)\n", keysym,
253     get_ksname(keysym));
254    
255 matthewc 38 /* not in keymap, try to interpret the raw scancode */
256     if ((keycode >= min_keycode) && (keycode <= 0x60))
257     {
258 astrand 66 tr.scancode = keycode - min_keycode;
259     printf("Sending guessed scancode 0x%x\n", tr.scancode);
260 matthewc 38 }
261 astrand 66 else
262     {
263     printf("No good guess for keycode 0x%x found\n", keycode);
264     }
265 matthewc 38
266 astrand 66 return tr;
267 matthewc 38 }
268    
269 astrand 64 uint16
270     xkeymap_translate_button(unsigned int button)
271 matthewc 38 {
272     switch (button)
273     {
274 astrand 64 case Button1: /* left */
275 matthewc 38 return MOUSE_FLAG_BUTTON1;
276 astrand 64 case Button2: /* middle */
277 matthewc 38 return MOUSE_FLAG_BUTTON3;
278 astrand 64 case Button3: /* right */
279 matthewc 38 return MOUSE_FLAG_BUTTON2;
280 jsorg71 67 case Button4: /* wheel up */
281     return MOUSE_FLAG_BUTTON4;
282     case Button5: /* wheel down */
283     return MOUSE_FLAG_BUTTON5;
284 matthewc 38 }
285    
286     return 0;
287     }
288 astrand 66
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