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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 227 by astrand, Fri Oct 11 09:38:49 2002 UTC revision 963 by astrand, Wed Aug 3 10:56:16 2005 UTC
# Line 1  Line 1 
1  /*  /* -*- c-basic-offset: 8 -*-
2     rdesktop: A Remote Desktop Protocol client.     rdesktop: A Remote Desktop Protocol client.
3     User interface services - X keyboard mapping     User interface services - X keyboard mapping
4     Copyright (C) Matthew Chapman 1999-2002  
5       Copyright (C) Matthew Chapman 1999-2005
6       Copyright (C) Peter Astrand <peter@cendio.se> 2003
7        
8     This program is free software; you can redistribute it and/or modify     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by     it under the terms of the GNU General Public License as published by
# Line 12  Line 14 
14     but WITHOUT ANY WARRANTY; without even the implied warranty of     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.     GNU General Public License for more details.
17      
18     You should have received a copy of the GNU General Public License     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software     along with this program; if not, write to the Free Software
20     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */  */
22    
23    #ifdef RDP2VNC
24    #include "vnc/x11stubs.h"
25    #else
26  #include <X11/Xlib.h>  #include <X11/Xlib.h>
27  #define XK_MISCELLANY  #include <X11/keysym.h>
28  #include <X11/keysymdef.h>  #endif
29    
30  #include <ctype.h>  #include <ctype.h>
31  #include <limits.h>  #include <limits.h>
32  #include <time.h>  #include <time.h>
33    #include <string.h>
34  #include "rdesktop.h"  #include "rdesktop.h"
35  #include "scancodes.h"  #include "scancodes.h"
36    
37  #define KEYMAP_SIZE 4096  #define KEYMAP_SIZE 0xffff+1
38  #define KEYMAP_MASK (KEYMAP_SIZE - 1)  #define KEYMAP_MASK 0xffff
39  #define KEYMAP_MAX_LINE_LENGTH 80  #define KEYMAP_MAX_LINE_LENGTH 80
40    
41  extern Display *display;  extern Display *g_display;
42    extern Window g_wnd;
43  extern char keymapname[16];  extern char keymapname[16];
44  extern int keylayout;  extern int g_keylayout;
45  extern BOOL enable_compose;  extern int g_win_button_size;
46    extern BOOL g_enable_compose;
47    extern BOOL g_use_rdp5;
48    extern BOOL g_numlock_sync;
49    
50  static BOOL keymap_loaded;  static BOOL keymap_loaded;
51  static key_translation keymap[KEYMAP_SIZE];  static key_translation *keymap[KEYMAP_SIZE];
52  static int min_keycode;  static int min_keycode;
53  static uint16 remote_modifier_state = 0;  static uint16 remote_modifier_state = 0;
54    static uint16 saved_remote_modifier_state = 0;
55    
56  static void update_modifier_state(uint8 scancode, BOOL pressed);  static void update_modifier_state(uint8 scancode, BOOL pressed);
57    
58    /* Free key_translation structure, including linked list */
59    static void
60    free_key_translation(key_translation * ptr)
61    {
62            key_translation *next;
63    
64            while (ptr)
65            {
66                    next = ptr->next;
67                    xfree(ptr);
68                    ptr = next;
69            }
70    }
71    
72  static void  static void
73  add_to_keymap(char *keyname, uint8 scancode, uint16 modifiers, char *mapname)  add_to_keymap(char *keyname, uint8 scancode, uint16 modifiers, char *mapname)
74  {  {
75          KeySym keysym;          KeySym keysym;
76            key_translation *tr;
77    
78          keysym = XStringToKeysym(keyname);          keysym = XStringToKeysym(keyname);
79          if (keysym == NoSymbol)          if (keysym == NoSymbol)
80          {          {
81                  error("Bad keysym %s in keymap %s\n", keyname, mapname);                  DEBUG_KBD(("Bad keysym \"%s\" in keymap %s (ignoring)\n", keyname, mapname));
82                  return;                  return;
83          }          }
84    
85          DEBUG_KBD(("Adding translation, keysym=0x%x, scancode=0x%x, "          DEBUG_KBD(("Adding translation, keysym=0x%x, scancode=0x%x, "
86                     "modifiers=0x%x\n", (unsigned int) keysym, scancode, modifiers));                     "modifiers=0x%x\n", (unsigned int) keysym, scancode, modifiers));
87    
88          keymap[keysym & KEYMAP_MASK].scancode = scancode;          tr = (key_translation *) xmalloc(sizeof(key_translation));
89          keymap[keysym & KEYMAP_MASK].modifiers = modifiers;          memset(tr, 0, sizeof(key_translation));
90            tr->scancode = scancode;
91            tr->modifiers = modifiers;
92            free_key_translation(keymap[keysym & KEYMAP_MASK]);
93            keymap[keysym & KEYMAP_MASK] = tr;
94    
95          return;          return;
96  }  }
97    
98    static void
99    add_sequence(char *rest, char *mapname)
100    {
101            KeySym keysym;
102            key_translation *tr, **prev_next;
103            size_t chars;
104            char keyname[KEYMAP_MAX_LINE_LENGTH];
105    
106            /* Skip over whitespace after the sequence keyword */
107            chars = strspn(rest, " \t");
108            rest += chars;
109    
110            /* Fetch the keysym name */
111            chars = strcspn(rest, " \t\0");
112            STRNCPY(keyname, rest, chars + 1);
113            rest += chars;
114    
115            keysym = XStringToKeysym(keyname);
116            if (keysym == NoSymbol)
117            {
118                    DEBUG_KBD(("Bad keysym \"%s\" in keymap %s (ignoring line)\n", keyname, mapname));
119                    return;
120            }
121    
122    
123            DEBUG_KBD(("Adding sequence for keysym (0x%lx, %s) -> ", keysym, keyname));
124    
125            free_key_translation(keymap[keysym & KEYMAP_MASK]);
126            prev_next = &keymap[keysym & KEYMAP_MASK];
127    
128            while (*rest)
129            {
130                    /* Skip whitespace */
131                    chars = strspn(rest, " \t");
132                    rest += chars;
133    
134                    /* Fetch the keysym name */
135                    chars = strcspn(rest, " \t\0");
136                    STRNCPY(keyname, rest, chars + 1);
137                    rest += chars;
138    
139                    keysym = XStringToKeysym(keyname);
140                    if (keysym == NoSymbol)
141                    {
142                            DEBUG_KBD(("Bad keysym \"%s\" in keymap %s (ignoring line)\n", keyname,
143                                       mapname));
144                            return;
145                    }
146    
147                    /* Allocate space for key_translation structure */
148                    tr = (key_translation *) xmalloc(sizeof(key_translation));
149                    memset(tr, 0, sizeof(key_translation));
150                    *prev_next = tr;
151                    prev_next = &tr->next;
152                    tr->seq_keysym = keysym;
153    
154                    DEBUG_KBD(("0x%x, ", (unsigned int) keysym));
155            }
156            DEBUG_KBD(("\n"));
157    }
158    
159    void
160    xkeymap_from_locale(const char *locale)
161    {
162            char *str, *ptr;
163            FILE *fp;
164    
165            /* Create a working copy */
166            str = strdup(locale);
167            if (str == NULL)
168            {
169                    perror("strdup");
170                    exit(1);
171            }
172    
173            /* Truncate at dot and at */
174            ptr = strrchr(str, '.');
175            if (ptr)
176                    *ptr = '\0';
177            ptr = strrchr(str, '@');
178            if (ptr)
179                    *ptr = '\0';
180    
181            /* Replace _ with - */
182            ptr = strrchr(str, '_');
183            if (ptr)
184                    *ptr = '-';
185    
186            /* Convert to lowercase */
187            ptr = str;
188            while (*ptr)
189            {
190                    *ptr = tolower((int) *ptr);
191                    ptr++;
192            }
193    
194            /* Try to open this keymap (da-dk) */
195            fp = xkeymap_open(str);
196            if (fp == NULL)
197            {
198                    /* Truncate at dash */
199                    ptr = strrchr(str, '-');
200                    if (ptr)
201                            *ptr = '\0';
202    
203                    /* Try the short name (da) */
204                    fp = xkeymap_open(str);
205            }
206    
207            if (fp)
208            {
209                    fclose(fp);
210                    STRNCPY(keymapname, str, sizeof(keymapname));
211                    fprintf(stderr, "Autoselected keyboard map %s.\n", keymapname);
212            }
213    }
214    
215    
216    /* Joins two path components. The result should be freed with
217       xfree(). */
218    static char *
219    pathjoin(const char *a, const char *b)
220    {
221            char *result;
222            result = xmalloc(PATH_MAX * 2 + 1);
223    
224            if (b[0] == '/')
225            {
226                    strncpy(result, b, PATH_MAX);
227            }
228            else
229            {
230                    strncpy(result, a, PATH_MAX);
231                    strcat(result, "/");
232                    strncat(result, b, PATH_MAX);
233            }
234            return result;
235    }
236    
237    /* Try to open a keymap with fopen() */
238    FILE *
239    xkeymap_open(const char *filename)
240    {
241            char *path1, *path2;
242            char *home;
243            FILE *fp;
244    
245            /* Try ~/.rdesktop/keymaps */
246            home = getenv("HOME");
247            if (home)
248            {
249                    path1 = pathjoin(home, ".rdesktop/keymaps");
250                    path2 = pathjoin(path1, filename);
251                    xfree(path1);
252                    fp = fopen(path2, "r");
253                    xfree(path2);
254                    if (fp)
255                            return fp;
256            }
257    
258            /* Try KEYMAP_PATH */
259            path1 = pathjoin(KEYMAP_PATH, filename);
260            fp = fopen(path1, "r");
261            xfree(path1);
262            if (fp)
263                    return fp;
264    
265            /* Try current directory, in case we are running from the source
266               tree */
267            path1 = pathjoin("keymaps", filename);
268            fp = fopen(path1, "r");
269            xfree(path1);
270            if (fp)
271                    return fp;
272    
273            return NULL;
274    }
275    
276  static BOOL  static BOOL
277  xkeymap_read(char *mapname)  xkeymap_read(char *mapname)
278  {  {
279          FILE *fp;          FILE *fp;
280          char line[KEYMAP_MAX_LINE_LENGTH];          char line[KEYMAP_MAX_LINE_LENGTH];
         char path[PATH_MAX], inplace_path[PATH_MAX];  
281          unsigned int line_num = 0;          unsigned int line_num = 0;
282          unsigned int line_length = 0;          unsigned int line_length = 0;
283          char *keyname, *p;          char *keyname, *p;
# Line 78  xkeymap_read(char *mapname) Line 285  xkeymap_read(char *mapname)
285          uint8 scancode;          uint8 scancode;
286          uint16 modifiers;          uint16 modifiers;
287    
288            fp = xkeymap_open(mapname);
         strcpy(path, KEYMAP_PATH);  
         strncat(path, mapname, sizeof(path) - sizeof(KEYMAP_PATH));  
   
         fp = fopen(path, "r");  
289          if (fp == NULL)          if (fp == NULL)
290          {          {
291                  /* in case we are running from the source tree */                  error("Failed to open keymap %s\n", mapname);
292                  strcpy(inplace_path, "keymaps/");                  return False;
                 strncat(inplace_path, mapname, sizeof(inplace_path) - sizeof("keymaps/"));  
   
                 fp = fopen(inplace_path, "r");  
                 if (fp == NULL)  
                 {  
                         error("Failed to open keymap %s\n", path);  
                         return False;  
                 }  
293          }          }
294    
295          /* FIXME: More tolerant on white space */          /* FIXME: More tolerant on white space */
# Line 126  xkeymap_read(char *mapname) Line 321  xkeymap_read(char *mapname)
321                  /* map */                  /* map */
322                  if (strncmp(line, "map ", 4) == 0)                  if (strncmp(line, "map ", 4) == 0)
323                  {                  {
324                          keylayout = strtol(line + 4, NULL, 16);                          g_keylayout = strtol(line + 4, NULL, 16);
325                          DEBUG_KBD(("Keylayout 0x%x\n", keylayout));                          DEBUG_KBD(("Keylayout 0x%x\n", g_keylayout));
326                          continue;                          continue;
327                  }                  }
328    
# Line 135  xkeymap_read(char *mapname) Line 330  xkeymap_read(char *mapname)
330                  if (strncmp(line, "enable_compose", 15) == 0)                  if (strncmp(line, "enable_compose", 15) == 0)
331                  {                  {
332                          DEBUG_KBD(("Enabling compose handling\n"));                          DEBUG_KBD(("Enabling compose handling\n"));
333                          enable_compose = True;                          g_enable_compose = True;
334                            continue;
335                    }
336    
337                    /* sequence */
338                    if (strncmp(line, "sequence", 8) == 0)
339                    {
340                            add_sequence(line + 8, mapname);
341                          continue;                          continue;
342                  }                  }
343    
# Line 198  xkeymap_read(char *mapname) Line 400  xkeymap_read(char *mapname)
400                          /* Automatically add uppercase key, with same modifiers                          /* Automatically add uppercase key, with same modifiers
401                             plus shift */                             plus shift */
402                          for (p = keyname; *p; p++)                          for (p = keyname; *p; p++)
403                                  *p = toupper(*p);                                  *p = toupper((int) *p);
404                          MASK_ADD_BITS(modifiers, MapLeftShiftMask);                          MASK_ADD_BITS(modifiers, MapLeftShiftMask);
405                          add_to_keymap(keyname, scancode, modifiers, mapname);                          add_to_keymap(keyname, scancode, modifiers, mapname);
406                  }                  }
# Line 214  void Line 416  void
416  xkeymap_init(void)  xkeymap_init(void)
417  {  {
418          unsigned int max_keycode;          unsigned int max_keycode;
         char *mapname_ptr;  
   
         /* Make keymapname lowercase */  
         mapname_ptr = keymapname;  
         while (*mapname_ptr)  
         {  
                 *mapname_ptr = tolower(*mapname_ptr);  
                 mapname_ptr++;  
         }  
419    
420          if (strcmp(keymapname, "none"))          if (strcmp(keymapname, "none"))
421          {          {
# Line 230  xkeymap_init(void) Line 423  xkeymap_init(void)
423                          keymap_loaded = True;                          keymap_loaded = True;
424          }          }
425    
426          XDisplayKeycodes(display, &min_keycode, (int *) &max_keycode);          XDisplayKeycodes(g_display, &min_keycode, (int *) &max_keycode);
427    }
428    
429    static void
430    send_winkey(uint32 ev_time, BOOL pressed, BOOL leftkey)
431    {
432            uint8 winkey;
433    
434            if (leftkey)
435                    winkey = SCANCODE_CHAR_LWIN;
436            else
437                    winkey = SCANCODE_CHAR_RWIN;
438    
439            if (pressed)
440            {
441                    if (g_use_rdp5)
442                    {
443                            rdp_send_scancode(ev_time, RDP_KEYPRESS, winkey);
444                    }
445                    else
446                    {
447                            /* RDP4 doesn't support winkey. Fake with Ctrl-Esc */
448                            rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_LCTRL);
449                            rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_ESC);
450                    }
451            }
452            else
453            {
454                    /* key released */
455                    if (g_use_rdp5)
456                    {
457                            rdp_send_scancode(ev_time, RDP_KEYRELEASE, winkey);
458                    }
459                    else
460                    {
461                            rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_ESC);
462                            rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL);
463                    }
464            }
465    }
466    
467    static void
468    reset_winkey(uint32 ev_time)
469    {
470            if (g_use_rdp5)
471            {
472                    /* For some reason, it seems to suffice to release
473                     *either* the left or right winkey. */
474                    rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LWIN);
475            }
476  }  }
477    
478  /* Handles, for example, multi-scancode keypresses (which is not  /* Handle special key combinations */
    possible via keymap-files) */  
479  BOOL  BOOL
480  handle_special_keys(uint32 keysym, unsigned int state, uint32 ev_time, BOOL pressed)  handle_special_keys(uint32 keysym, unsigned int state, uint32 ev_time, BOOL pressed)
481  {  {
482          switch (keysym)          switch (keysym)
483          {          {
484                  case XK_Break:                  case XK_Return:
                 case XK_Pause:  
485                          if ((get_key_state(state, XK_Alt_L) || get_key_state(state, XK_Alt_R))                          if ((get_key_state(state, XK_Alt_L) || get_key_state(state, XK_Alt_R))
486                              && (get_key_state(state, XK_Control_L)                              && (get_key_state(state, XK_Control_L)
487                                  || get_key_state(state, XK_Control_R)))                                  || get_key_state(state, XK_Control_R)))
488                          {                          {
489                                  /* Ctrl-Alt-Break: toggle full screen */                                  /* Ctrl-Alt-Enter: toggle full screen */
490                                  if (pressed)                                  if (pressed)
491                                          xwin_toggle_fullscreen();                                          xwin_toggle_fullscreen();
492                                    return True;
493                            }
494                            break;
495    
496                    case XK_Break:
497                            /* Send Break sequence E0 46 E0 C6 */
498                            if (pressed)
499                            {
500                                    rdp_send_scancode(ev_time, RDP_KEYPRESS,
501                                                      (SCANCODE_EXTENDED | 0x46));
502                                    rdp_send_scancode(ev_time, RDP_KEYPRESS,
503                                                      (SCANCODE_EXTENDED | 0xc6));
504                          }                          }
505                          else if (keysym == XK_Break)                          /* No release sequence */
506                            return True;
507                            break;
508    
509                    case XK_Pause:
510                            /* According to MS Keyboard Scan Code
511                               Specification, pressing Pause should result
512                               in E1 1D 45 E1 9D C5. I'm not exactly sure
513                               of how this is supposed to be sent via
514                               RDP. The code below seems to work, but with
515                               the side effect that Left Ctrl stays
516                               down. Therefore, we release it when Pause
517                               is released. */
518                            if (pressed)
519                          {                          {
520                                  /* Send Break sequence E0 46 E0 C6 */                                  rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0xe1, 0);
521                                  if (pressed)                                  rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0x1d, 0);
522                                  {                                  rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0x45, 0);
523                                          rdp_send_scancode(ev_time, RDP_KEYPRESS,                                  rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0xe1, 0);
524                                                            (SCANCODE_EXTENDED | 0x46));                                  rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0x9d, 0);
525                                          rdp_send_scancode(ev_time, RDP_KEYPRESS,                                  rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0xc5, 0);
                                                           (SCANCODE_EXTENDED | 0xc6));  
                                 }  
                                 /* No break sequence */  
526                          }                          }
527                          else    /* XK_Pause */                          else
528                          {                          {
529                                  /* According to MS Keyboard Scan Code                                  /* Release Left Ctrl */
530                                     Specification, pressing Pause should result                                  rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYRELEASE,
531                                     in E1 1D 45 E1 9D C5. I'm not exactly sure                                                 0x1d, 0);
                                    of how this is supposed to be sent via  
                                    RDP. The code below seems to work, but with  
                                    the side effect that Left Ctrl stays  
                                    down. Therefore, we release it when Pause  
                                    is released. */  
                                 if (pressed)  
                                 {  
                                         rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS,  
                                                        0xe1, 0);  
                                         rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS,  
                                                        0x1d, 0);  
                                         rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS,  
                                                        0x45, 0);  
                                         rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS,  
                                                        0xe1, 0);  
                                         rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS,  
                                                        0x9d, 0);  
                                         rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS,  
                                                        0xc5, 0);  
                                 }  
                                 else  
                                 {  
                                         /* Release Left Ctrl */  
                                         rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYRELEASE,  
                                                        0x1d, 0);  
                                 }  
532                          }                          }
533                          return True;                          return True;
534                            break;
535    
536                  case XK_Meta_L: /* Windows keys */                  case XK_Meta_L: /* Windows keys */
537                  case XK_Super_L:                  case XK_Super_L:
538                  case XK_Hyper_L:                  case XK_Hyper_L:
539                            send_winkey(ev_time, pressed, True);
540                            return True;
541                            break;
542    
543                  case XK_Meta_R:                  case XK_Meta_R:
544                  case XK_Super_R:                  case XK_Super_R:
545                  case XK_Hyper_R:                  case XK_Hyper_R:
546                          if (pressed)                          send_winkey(ev_time, pressed, False);
                         {  
                                 rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_LCTRL);  
                                 rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_ESC);  
                         }  
                         else  
                         {  
                                 rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_ESC);  
                                 rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL);  
                         }  
547                          return True;                          return True;
548                            break;
549    
550                    case XK_space:
551                            /* Prevent access to the Windows system menu in single app mode */
552                            if (g_win_button_size
553                                && (get_key_state(state, XK_Alt_L) || get_key_state(state, XK_Alt_R)))
554                                    return True;
555                            break;
556    
557                    case XK_Num_Lock:
558                            /* Synchronize on key release */
559                            if (g_numlock_sync && !pressed)
560                                    rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0,
561                                                   ui_get_numlock_state(read_keyboard_state()), 0);
562    
563                            /* Inhibit */
564                            return True;
565                            break;
566    
567          }          }
568          return False;          return False;
569  }  }
# Line 322  handle_special_keys(uint32 keysym, unsig Line 572  handle_special_keys(uint32 keysym, unsig
572  key_translation  key_translation
573  xkeymap_translate_key(uint32 keysym, unsigned int keycode, unsigned int state)  xkeymap_translate_key(uint32 keysym, unsigned int keycode, unsigned int state)
574  {  {
575          key_translation tr = { 0, 0 };          key_translation tr = { 0, 0, 0, 0 };
576            key_translation *ptr;
         tr = keymap[keysym & KEYMAP_MASK];  
577    
578          if (tr.modifiers & MapInhibitMask)          ptr = keymap[keysym & KEYMAP_MASK];
579            if (ptr)
580          {          {
581                  DEBUG_KBD(("Inhibiting key\n"));                  tr = *ptr;
582                  tr.scancode = 0;                  if (tr.seq_keysym == 0) /* Normal scancode translation */
583                  return tr;                  {
584          }                          if (tr.modifiers & MapInhibitMask)
585                            {
586                                    DEBUG_KBD(("Inhibiting key\n"));
587                                    tr.scancode = 0;
588                                    return tr;
589                            }
590    
591                            if (tr.modifiers & MapLocalStateMask)
592                            {
593                                    /* The modifiers to send for this key should be obtained
594                                       from the local state. Currently, only shift is implemented. */
595                                    if (state & ShiftMask)
596                                    {
597                                            tr.modifiers = MapLeftShiftMask;
598                                    }
599                            }
600    
601          if (tr.modifiers & MapLocalStateMask)                          if ((tr.modifiers & MapLeftShiftMask)
602                                && ((remote_modifier_state & MapLeftCtrlMask)
603                                    || (remote_modifier_state & MapRightCtrlMask))
604                                && get_key_state(state, XK_Caps_Lock))
605                            {
606                                    DEBUG_KBD(("CapsLock + Ctrl pressed, releasing LeftShift\n"));
607                                    tr.modifiers ^= MapLeftShiftMask;
608                            }
609    
610                            DEBUG_KBD(("Found scancode translation, scancode=0x%x, modifiers=0x%x\n",
611                                       tr.scancode, tr.modifiers));
612                    }
613            }
614            else
615          {          {
616                  /* The modifiers to send for this key should be obtained                  if (keymap_loaded)
617                     from the local state. Currently, only shift is implemented. */                          warning("No translation for (keysym 0x%lx, %s)\n", keysym,
618                  if (state & ShiftMask)                                  get_ksname(keysym));
619    
620                    /* not in keymap, try to interpret the raw scancode */
621                    if (((int) keycode >= min_keycode) && (keycode <= 0x60))
622                    {
623                            tr.scancode = keycode - min_keycode;
624    
625                            /* The modifiers to send for this key should be
626                               obtained from the local state. Currently, only
627                               shift is implemented. */
628                            if (state & ShiftMask)
629                            {
630                                    tr.modifiers = MapLeftShiftMask;
631                            }
632    
633                            DEBUG_KBD(("Sending guessed scancode 0x%x\n", tr.scancode));
634                    }
635                    else
636                  {                  {
637                          tr.modifiers = MapLeftShiftMask;                          DEBUG_KBD(("No good guess for keycode 0x%x found\n", keycode));
638                  }                  }
639          }          }
640    
641          if (tr.scancode != 0)          return tr;
642          {  }
                 DEBUG_KBD(("Found key translation, scancode=0x%x, modifiers=0x%x\n",  
                            tr.scancode, tr.modifiers));  
                 return tr;  
         }  
643    
644          if (keymap_loaded)  void
645                  error("No translation for (keysym 0x%lx, %s)\n", keysym, get_ksname(keysym));  xkeymap_send_keys(uint32 keysym, unsigned int keycode, unsigned int state, uint32 ev_time,
646                      BOOL pressed)
647    {
648            key_translation tr, *ptr;
649            tr = xkeymap_translate_key(keysym, keycode, state);
650    
651          /* not in keymap, try to interpret the raw scancode */          if (tr.seq_keysym == 0)
         if ((keycode >= min_keycode) && (keycode <= 0x60))  
652          {          {
653                  tr.scancode = keycode - min_keycode;                  /* Scancode translation */
654                    if (tr.scancode == 0)
655                            return;
656    
657                  /* The modifiers to send for this key should be                  if (pressed)
                    obtained from the local state. Currently, only  
                    shift is implemented. */  
                 if (state & ShiftMask)  
658                  {                  {
659                          tr.modifiers = MapLeftShiftMask;                          save_remote_modifiers(tr.scancode);
660                            ensure_remote_modifiers(ev_time, tr);
661                            rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);
662                            restore_remote_modifiers(ev_time, tr.scancode);
663                  }                  }
664                    else
665                  DEBUG_KBD(("Sending guessed scancode 0x%x\n", tr.scancode));                  {
666                            rdp_send_scancode(ev_time, RDP_KEYRELEASE, tr.scancode);
667                    }
668                    return;
669          }          }
670          else  
671            /* Sequence, only on key down */
672            if (pressed)
673          {          {
674                  DEBUG_KBD(("No good guess for keycode 0x%x found\n", keycode));                  ptr = &tr;
675                    do
676                    {
677                            DEBUG_KBD(("Handling sequence element, keysym=0x%x\n",
678                                       (unsigned int) ptr->seq_keysym));
679                            xkeymap_send_keys(ptr->seq_keysym, keycode, state, ev_time, True);
680                            xkeymap_send_keys(ptr->seq_keysym, keycode, state, ev_time, False);
681                            ptr = ptr->next;
682                    }
683                    while (ptr);
684          }          }
   
         return tr;  
685  }  }
686    
687  uint16  uint16
# Line 409  get_ksname(uint32 keysym) Line 717  get_ksname(uint32 keysym)
717          return ksname;          return ksname;
718  }  }
719    
720    static BOOL
721  void  is_modifier(uint8 scancode)
 ensure_remote_modifiers(uint32 ev_time, key_translation tr)  
722  {  {
723          /* If this key is a modifier, do nothing */          switch (scancode)
         switch (tr.scancode)  
724          {          {
725                  case SCANCODE_CHAR_LSHIFT:                  case SCANCODE_CHAR_LSHIFT:
726                  case SCANCODE_CHAR_RSHIFT:                  case SCANCODE_CHAR_RSHIFT:
# Line 425  ensure_remote_modifiers(uint32 ev_time, Line 731  ensure_remote_modifiers(uint32 ev_time,
731                  case SCANCODE_CHAR_LWIN:                  case SCANCODE_CHAR_LWIN:
732                  case SCANCODE_CHAR_RWIN:                  case SCANCODE_CHAR_RWIN:
733                  case SCANCODE_CHAR_NUMLOCK:                  case SCANCODE_CHAR_NUMLOCK:
734                          return;                          return True;
735                  default:                  default:
736                          break;                          break;
737          }          }
738            return False;
739    }
740    
741    void
742    save_remote_modifiers(uint8 scancode)
743    {
744            if (is_modifier(scancode))
745                    return;
746    
747            saved_remote_modifier_state = remote_modifier_state;
748    }
749    
750    void
751    restore_remote_modifiers(uint32 ev_time, uint8 scancode)
752    {
753            key_translation dummy;
754    
755            if (is_modifier(scancode))
756                    return;
757    
758            dummy.scancode = 0;
759            dummy.modifiers = saved_remote_modifier_state;
760            ensure_remote_modifiers(ev_time, dummy);
761    }
762    
763    void
764    ensure_remote_modifiers(uint32 ev_time, key_translation tr)
765    {
766            /* If this key is a modifier, do nothing */
767            if (is_modifier(tr.scancode))
768                    return;
769    
770            if (!g_numlock_sync)
771            {
772                    /* NumLock */
773                    if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask)
774                        != MASK_HAS_BITS(remote_modifier_state, MapNumLockMask))
775                    {
776                            /* The remote modifier state is not correct */
777                            uint16 new_remote_state;
778    
779                            if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask))
780                            {
781                                    DEBUG_KBD(("Remote NumLock state is incorrect, activating NumLock.\n"));
782                                    new_remote_state = KBD_FLAG_NUMLOCK;
783                                    remote_modifier_state = MapNumLockMask;
784                            }
785                            else
786                            {
787                                    DEBUG_KBD(("Remote NumLock state is incorrect, deactivating NumLock.\n"));
788                                    new_remote_state = 0;
789                                    remote_modifier_state = 0;
790                            }
791    
792                            rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0, new_remote_state, 0);
793                    }
794            }
795    
796    
797          /* Shift. Left shift and right shift are treated as equal; either is fine. */          /* Shift. Left shift and right shift are treated as equal; either is fine. */
798          if (MASK_HAS_BITS(tr.modifiers, MapShiftMask)          if (MASK_HAS_BITS(tr.modifiers, MapShiftMask)
# Line 474  ensure_remote_modifiers(uint32 ev_time, Line 838  ensure_remote_modifiers(uint32 ev_time,
838                  }                  }
839          }          }
840    
         /* NumLock */  
         if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask)  
             != MASK_HAS_BITS(remote_modifier_state, MapNumLockMask))  
         {  
                 /* The remote modifier state is not correct */  
                 uint16 new_remote_state = 0;  
841    
842                  if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask))  }
                 {  
                         DEBUG_KBD(("Remote NumLock state is incorrect, activating NumLock.\n"));  
                         new_remote_state |= KBD_FLAG_NUMLOCK;  
                 }  
                 else  
                 {  
                         DEBUG_KBD(("Remote NumLock state is incorrect, deactivating NumLock.\n"));  
                 }  
843    
844                  rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0, new_remote_state, 0);  
845                  update_modifier_state(SCANCODE_CHAR_NUMLOCK, True);  unsigned int
846          }  read_keyboard_state()
847    {
848    #ifdef RDP2VNC
849            return 0;
850    #else
851            unsigned int state;
852            Window wdummy;
853            int dummy;
854    
855            XQueryPointer(g_display, g_wnd, &wdummy, &wdummy, &dummy, &dummy, &dummy, &dummy, &state);
856            return state;
857    #endif
858    }
859    
860    
861    uint16
862    ui_get_numlock_state(unsigned int state)
863    {
864            uint16 numlock_state = 0;
865    
866            if (get_key_state(state, XK_Num_Lock))
867                    numlock_state = KBD_FLAG_NUMLOCK;
868    
869            return numlock_state;
870  }  }
871    
872    
873  void  void
874  reset_modifier_keys(unsigned int state)  reset_modifier_keys()
875  {  {
876            unsigned int state = read_keyboard_state();
877    
878          /* reset keys */          /* reset keys */
879          uint32 ev_time;          uint32 ev_time;
880          ev_time = time(NULL);          ev_time = time(NULL);
# Line 524  reset_modifier_keys(unsigned int state) Line 899  reset_modifier_keys(unsigned int state)
899                  rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LALT);                  rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LALT);
900    
901          if (MASK_HAS_BITS(remote_modifier_state, MapRightAltMask) &&          if (MASK_HAS_BITS(remote_modifier_state, MapRightAltMask) &&
902              !get_key_state(state, XK_Alt_R) && !get_key_state(state, XK_Mode_switch))              !get_key_state(state, XK_Alt_R) && !get_key_state(state, XK_Mode_switch)
903                && !get_key_state(state, XK_ISO_Level3_Shift))
904                  rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT);                  rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT);
905    
906            reset_winkey(ev_time);
907    
908            if (g_numlock_sync)
909                    rdp_send_input(ev_time, RDP_INPUT_SYNCHRONIZE, 0, ui_get_numlock_state(state), 0);
910  }  }
911    
912    
# Line 567  update_modifier_state(uint8 scancode, BO Line 948  update_modifier_state(uint8 scancode, BO
948                  case SCANCODE_CHAR_NUMLOCK:                  case SCANCODE_CHAR_NUMLOCK:
949                          /* KeyReleases for NumLocks are sent immediately. Toggle the                          /* KeyReleases for NumLocks are sent immediately. Toggle the
950                             modifier state only on Keypress */                             modifier state only on Keypress */
951                          if (pressed)                          if (pressed && !g_numlock_sync)
952                          {                          {
953                                  BOOL newNumLockState;                                  BOOL newNumLockState;
954                                  newNumLockState =                                  newNumLockState =
# Line 576  update_modifier_state(uint8 scancode, BO Line 957  update_modifier_state(uint8 scancode, BO
957                                  MASK_CHANGE_BIT(remote_modifier_state,                                  MASK_CHANGE_BIT(remote_modifier_state,
958                                                  MapNumLockMask, newNumLockState);                                                  MapNumLockMask, newNumLockState);
959                          }                          }
                         break;  
960          }          }
961    
962  #ifdef WITH_DEBUG_KBD  #ifdef WITH_DEBUG_KBD

Legend:
Removed from v.227  
changed lines
  Added in v.963

  ViewVC Help
Powered by ViewVC 1.1.26