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

Diff of /jpeg/rdesktop/trunk/xkeymap.c

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

revision 894 by stargo, Mon Apr 25 05:32:23 2005 UTC revision 1294 by astrand, Wed Oct 11 17:59:18 2006 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    
# Line 30  Line 30 
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    
# Line 39  Line 40 
40    
41  extern Display *g_display;  extern Display *g_display;
42  extern Window g_wnd;  extern Window g_wnd;
43  extern char keymapname[16];  extern char g_keymapname[16];
44  extern int g_keylayout;  extern unsigned int g_keylayout;
45    extern int g_keyboard_type;
46    extern int g_keyboard_subtype;
47    extern int g_keyboard_functionkeys;
48  extern int g_win_button_size;  extern int g_win_button_size;
49  extern BOOL g_enable_compose;  extern BOOL g_enable_compose;
50  extern BOOL g_use_rdp5;  extern BOOL g_use_rdp5;
51  extern BOOL g_numlock_sync;  extern BOOL g_numlock_sync;
52    
53  static BOOL keymap_loaded;  static BOOL keymap_loaded;
54  static key_translation keymap[KEYMAP_SIZE];  static key_translation *keymap[KEYMAP_SIZE];
55  static int min_keycode;  static int min_keycode;
56  static uint16 remote_modifier_state = 0;  static uint16 remote_modifier_state = 0;
57  static uint16 saved_remote_modifier_state = 0;  static uint16 saved_remote_modifier_state = 0;
58    
59  static void update_modifier_state(uint8 scancode, BOOL pressed);  static void update_modifier_state(uint8 scancode, BOOL pressed);
60    
61    /* Free key_translation structure, including linked list */
62    static void
63    free_key_translation(key_translation * ptr)
64    {
65            key_translation *next;
66    
67            while (ptr)
68            {
69                    next = ptr->next;
70                    xfree(ptr);
71                    ptr = next;
72            }
73    }
74    
75  static void  static void
76  add_to_keymap(char *keyname, uint8 scancode, uint16 modifiers, char *mapname)  add_to_keymap(char *keyname, uint8 scancode, uint16 modifiers, char *mapname)
77  {  {
78          KeySym keysym;          KeySym keysym;
79            key_translation *tr;
80    
81          keysym = XStringToKeysym(keyname);          keysym = XStringToKeysym(keyname);
82          if (keysym == NoSymbol)          if (keysym == NoSymbol)
# Line 69  add_to_keymap(char *keyname, uint8 scanc Line 88  add_to_keymap(char *keyname, uint8 scanc
88          DEBUG_KBD(("Adding translation, keysym=0x%x, scancode=0x%x, "          DEBUG_KBD(("Adding translation, keysym=0x%x, scancode=0x%x, "
89                     "modifiers=0x%x\n", (unsigned int) keysym, scancode, modifiers));                     "modifiers=0x%x\n", (unsigned int) keysym, scancode, modifiers));
90    
91          keymap[keysym & KEYMAP_MASK].scancode = scancode;          tr = (key_translation *) xmalloc(sizeof(key_translation));
92          keymap[keysym & KEYMAP_MASK].modifiers = modifiers;          memset(tr, 0, sizeof(key_translation));
93            tr->scancode = scancode;
94            tr->modifiers = modifiers;
95            free_key_translation(keymap[keysym & KEYMAP_MASK]);
96            keymap[keysym & KEYMAP_MASK] = tr;
97    
98          return;          return;
99  }  }
100    
101    static void
102    add_sequence(char *rest, char *mapname)
103    {
104            KeySym keysym;
105            key_translation *tr, **prev_next;
106            size_t chars;
107            char keyname[KEYMAP_MAX_LINE_LENGTH];
108    
109            /* Skip over whitespace after the sequence keyword */
110            chars = strspn(rest, " \t");
111            rest += chars;
112    
113            /* Fetch the keysym name */
114            chars = strcspn(rest, " \t\0");
115            STRNCPY(keyname, rest, chars + 1);
116            rest += chars;
117    
118            keysym = XStringToKeysym(keyname);
119            if (keysym == NoSymbol)
120            {
121                    DEBUG_KBD(("Bad keysym \"%s\" in keymap %s (ignoring line)\n", keyname, mapname));
122                    return;
123            }
124    
125    
126            DEBUG_KBD(("Adding sequence for keysym (0x%lx, %s) -> ", keysym, keyname));
127    
128            free_key_translation(keymap[keysym & KEYMAP_MASK]);
129            prev_next = &keymap[keysym & KEYMAP_MASK];
130    
131            while (*rest)
132            {
133                    /* Skip whitespace */
134                    chars = strspn(rest, " \t");
135                    rest += chars;
136    
137                    /* Fetch the keysym name */
138                    chars = strcspn(rest, " \t\0");
139                    STRNCPY(keyname, rest, chars + 1);
140                    rest += chars;
141    
142                    keysym = XStringToKeysym(keyname);
143                    if (keysym == NoSymbol)
144                    {
145                            DEBUG_KBD(("Bad keysym \"%s\" in keymap %s (ignoring line)\n", keyname,
146                                       mapname));
147                            return;
148                    }
149    
150                    /* Allocate space for key_translation structure */
151                    tr = (key_translation *) xmalloc(sizeof(key_translation));
152                    memset(tr, 0, sizeof(key_translation));
153                    *prev_next = tr;
154                    prev_next = &tr->next;
155                    tr->seq_keysym = keysym;
156    
157                    DEBUG_KBD(("0x%x, ", (unsigned int) keysym));
158            }
159            DEBUG_KBD(("\n"));
160    }
161    
162    BOOL
163    xkeymap_from_locale(const char *locale)
164    {
165            char *str, *ptr;
166            FILE *fp;
167    
168            /* Create a working copy */
169            str = xstrdup(locale);
170    
171            /* Truncate at dot and at */
172            ptr = strrchr(str, '.');
173            if (ptr)
174                    *ptr = '\0';
175            ptr = strrchr(str, '@');
176            if (ptr)
177                    *ptr = '\0';
178    
179            /* Replace _ with - */
180            ptr = strrchr(str, '_');
181            if (ptr)
182                    *ptr = '-';
183    
184            /* Convert to lowercase */
185            ptr = str;
186            while (*ptr)
187            {
188                    *ptr = tolower((int) *ptr);
189                    ptr++;
190            }
191    
192            /* Try to open this keymap (da-dk) */
193            fp = xkeymap_open(str);
194            if (fp == NULL)
195            {
196                    /* Truncate at dash */
197                    ptr = strrchr(str, '-');
198                    if (ptr)
199                            *ptr = '\0';
200    
201                    /* Try the short name (da) */
202                    fp = xkeymap_open(str);
203            }
204    
205            if (fp)
206            {
207                    fclose(fp);
208                    STRNCPY(g_keymapname, str, sizeof(g_keymapname));
209                    xfree(str);
210                    return True;
211            }
212    
213            xfree(str);
214            return False;
215    }
216    
217    
218    /* Joins two path components. The result should be freed with
219       xfree(). */
220    static char *
221    pathjoin(const char *a, const char *b)
222    {
223            char *result;
224            result = xmalloc(PATH_MAX * 2 + 1);
225    
226            if (b[0] == '/')
227            {
228                    strncpy(result, b, PATH_MAX);
229            }
230            else
231            {
232                    strncpy(result, a, PATH_MAX);
233                    strcat(result, "/");
234                    strncat(result, b, PATH_MAX);
235            }
236            return result;
237    }
238    
239    /* Try to open a keymap with fopen() */
240    FILE *
241    xkeymap_open(const char *filename)
242    {
243            char *path1, *path2;
244            char *home;
245            FILE *fp;
246    
247            /* Try ~/.rdesktop/keymaps */
248            home = getenv("HOME");
249            if (home)
250            {
251                    path1 = pathjoin(home, ".rdesktop/keymaps");
252                    path2 = pathjoin(path1, filename);
253                    xfree(path1);
254                    fp = fopen(path2, "r");
255                    xfree(path2);
256                    if (fp)
257                            return fp;
258            }
259    
260            /* Try KEYMAP_PATH */
261            path1 = pathjoin(KEYMAP_PATH, filename);
262            fp = fopen(path1, "r");
263            xfree(path1);
264            if (fp)
265                    return fp;
266    
267            /* Try current directory, in case we are running from the source
268               tree */
269            path1 = pathjoin("keymaps", filename);
270            fp = fopen(path1, "r");
271            xfree(path1);
272            if (fp)
273                    return fp;
274    
275            return NULL;
276    }
277    
278  static BOOL  static BOOL
279  xkeymap_read(char *mapname)  xkeymap_read(char *mapname)
280  {  {
281          FILE *fp;          FILE *fp;
282          char line[KEYMAP_MAX_LINE_LENGTH];          char line[KEYMAP_MAX_LINE_LENGTH];
         char path[PATH_MAX], inplace_path[PATH_MAX];  
283          unsigned int line_num = 0;          unsigned int line_num = 0;
284          unsigned int line_length = 0;          unsigned int line_length = 0;
285          char *keyname, *p;          char *keyname, *p;
# Line 89  xkeymap_read(char *mapname) Line 287  xkeymap_read(char *mapname)
287          uint8 scancode;          uint8 scancode;
288          uint16 modifiers;          uint16 modifiers;
289    
290            fp = xkeymap_open(mapname);
         strcpy(path, KEYMAP_PATH);  
         strncat(path, mapname, sizeof(path) - sizeof(KEYMAP_PATH));  
   
         fp = fopen(path, "r");  
291          if (fp == NULL)          if (fp == NULL)
292          {          {
293                  /* in case we are running from the source tree */                  error("Failed to open keymap %s\n", mapname);
294                  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;  
                 }  
295          }          }
296    
297          /* FIXME: More tolerant on white space */          /* FIXME: More tolerant on white space */
# Line 127  xkeymap_read(char *mapname) Line 313  xkeymap_read(char *mapname)
313                  }                  }
314    
315                  /* Include */                  /* Include */
316                  if (strncmp(line, "include ", 8) == 0)                  if (str_startswith(line, "include "))
317                  {                  {
318                          if (!xkeymap_read(line + 8))                          if (!xkeymap_read(line + sizeof("include ") - 1))
319                                  return False;                                  return False;
320                          continue;                          continue;
321                  }                  }
322    
323                  /* map */                  /* map */
324                  if (strncmp(line, "map ", 4) == 0)                  if (str_startswith(line, "map "))
325                  {                  {
326                          g_keylayout = strtol(line + 4, NULL, 16);                          g_keylayout = strtoul(line + sizeof("map ") - 1, NULL, 16);
327                          DEBUG_KBD(("Keylayout 0x%x\n", g_keylayout));                          DEBUG_KBD(("Keylayout 0x%x\n", g_keylayout));
328                          continue;                          continue;
329                  }                  }
330    
331                  /* compose */                  /* compose */
332                  if (strncmp(line, "enable_compose", 15) == 0)                  if (str_startswith(line, "enable_compose"))
333                  {                  {
334                          DEBUG_KBD(("Enabling compose handling\n"));                          DEBUG_KBD(("Enabling compose handling\n"));
335                          g_enable_compose = True;                          g_enable_compose = True;
336                          continue;                          continue;
337                  }                  }
338    
339                    /* sequence */
340                    if (str_startswith(line, "sequence"))
341                    {
342                            add_sequence(line + sizeof("sequence") - 1, mapname);
343                            continue;
344                    }
345    
346                    /* keyboard_type */
347                    if (str_startswith(line, "keyboard_type "))
348                    {
349                            g_keyboard_type = strtol(line + sizeof("keyboard_type ") - 1, NULL, 16);
350                            DEBUG_KBD(("keyboard_type 0x%x\n", g_keyboard_type));
351                            continue;
352                    }
353    
354                    /* keyboard_subtype */
355                    if (str_startswith(line, "keyboard_subtype "))
356                    {
357                            g_keyboard_subtype =
358                                    strtol(line + sizeof("keyboard_subtype ") - 1, NULL, 16);
359                            DEBUG_KBD(("keyboard_subtype 0x%x\n", g_keyboard_subtype));
360                            continue;
361                    }
362    
363                    /* keyboard_functionkeys */
364                    if (str_startswith(line, "keyboard_functionkeys "))
365                    {
366                            g_keyboard_functionkeys =
367                                    strtol(line + sizeof("keyboard_functionkeys ") - 1, NULL, 16);
368                            DEBUG_KBD(("keyboard_functionkeys 0x%x\n", g_keyboard_functionkeys));
369                            continue;
370                    }
371    
372                  /* Comment */                  /* Comment */
373                  if (line[0] == '#')                  if (line[0] == '#')
374                  {                  {
# Line 225  void Line 444  void
444  xkeymap_init(void)  xkeymap_init(void)
445  {  {
446          unsigned int max_keycode;          unsigned int max_keycode;
         char *mapname_ptr;  
   
         /* Make keymapname lowercase */  
         mapname_ptr = keymapname;  
         while (*mapname_ptr)  
         {  
                 *mapname_ptr = tolower((int) *mapname_ptr);  
                 mapname_ptr++;  
         }  
447    
448          if (strcmp(keymapname, "none"))          if (strcmp(g_keymapname, "none"))
449          {          {
450                  if (xkeymap_read(keymapname))                  if (xkeymap_read(g_keymapname))
451                          keymap_loaded = True;                          keymap_loaded = True;
452          }          }
453    
# Line 293  reset_winkey(uint32 ev_time) Line 503  reset_winkey(uint32 ev_time)
503          }          }
504  }  }
505    
506  /* Handles, for example, multi-scancode keypresses (which is not  /* Handle special key combinations */
    possible via keymap-files) */  
507  BOOL  BOOL
508  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)
509  {  {
# Line 372  handle_special_keys(uint32 keysym, unsig Line 581  handle_special_keys(uint32 keysym, unsig
581                              && (get_key_state(state, XK_Alt_L) || get_key_state(state, XK_Alt_R)))                              && (get_key_state(state, XK_Alt_L) || get_key_state(state, XK_Alt_R)))
582                                  return True;                                  return True;
583                          break;                          break;
584    
585                  case XK_Num_Lock:                  case XK_Num_Lock:
586                          /* FIXME: We might want to do RDP_INPUT_SYNCHRONIZE here, if g_numlock_sync */                          /* Synchronize on key release */
587                          if (!g_numlock_sync)                          if (g_numlock_sync && !pressed)
588                                  /* Inhibit */                                  rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0,
589                                  return True;                                                 ui_get_numlock_state(read_keyboard_state()), 0);
590    
591                            /* Inhibit */
592                            return True;
593                            break;
594                    case XK_Overlay1_Enable:
595                            /* Toggle SeamlessRDP */
596                            if (pressed)
597                                    ui_seamless_toggle();
598                          break;                          break;
599    
600          }          }
# Line 387  handle_special_keys(uint32 keysym, unsig Line 605  handle_special_keys(uint32 keysym, unsig
605  key_translation  key_translation
606  xkeymap_translate_key(uint32 keysym, unsigned int keycode, unsigned int state)  xkeymap_translate_key(uint32 keysym, unsigned int keycode, unsigned int state)
607  {  {
608          key_translation tr = { 0, 0 };          key_translation tr = { 0, 0, 0, 0 };
609            key_translation *ptr;
         tr = keymap[keysym & KEYMAP_MASK];  
610    
611          if (tr.modifiers & MapInhibitMask)          ptr = keymap[keysym & KEYMAP_MASK];
612            if (ptr)
613          {          {
614                  DEBUG_KBD(("Inhibiting key\n"));                  tr = *ptr;
615                  tr.scancode = 0;                  if (tr.seq_keysym == 0) /* Normal scancode translation */
                 return tr;  
         }  
   
         if (tr.modifiers & MapLocalStateMask)  
         {  
                 /* The modifiers to send for this key should be obtained  
                    from the local state. Currently, only shift is implemented. */  
                 if (state & ShiftMask)  
616                  {                  {
617                          tr.modifiers = MapLeftShiftMask;                          if (MASK_HAS_BITS(tr.modifiers, MapInhibitMask))
618                            {
619                                    DEBUG_KBD(("Inhibiting key\n"));
620                                    tr.scancode = 0;
621                                    return tr;
622                            }
623    
624                            if (MASK_HAS_BITS(tr.modifiers, MapLocalStateMask))
625                            {
626                                    /* The modifiers to send for this key should be obtained
627                                       from the local state. Currently, only shift is implemented. */
628                                    if (MASK_HAS_BITS(state, ShiftMask))
629                                    {
630                                            tr.modifiers = MapLeftShiftMask;
631                                    }
632                            }
633    
634                            /* Windows interprets CapsLock+Ctrl+key
635                               differently from Shift+Ctrl+key. Since we
636                               are simulating CapsLock with Shifts, things
637                               like Ctrl+f with CapsLock on breaks. To
638                               solve this, we are releasing Shift if Ctrl
639                               is on, but only if Shift isn't physically pressed. */
640                            if (MASK_HAS_BITS(tr.modifiers, MapShiftMask)
641                                && MASK_HAS_BITS(remote_modifier_state, MapCtrlMask)
642                                && !MASK_HAS_BITS(state, ShiftMask))
643                            {
644                                    DEBUG_KBD(("Non-physical Shift + Ctrl pressed, releasing Shift\n"));
645                                    MASK_REMOVE_BITS(tr.modifiers, MapShiftMask);
646                            }
647    
648                            DEBUG_KBD(("Found scancode translation, scancode=0x%x, modifiers=0x%x\n",
649                                       tr.scancode, tr.modifiers));
650                  }                  }
651          }          }
652            else
 #if 0  
         if (((remote_modifier_state & MapLeftCtrlMask)  
              || (remote_modifier_state & MapRightCtrlMask)) && get_key_state(state, XK_Caps_Lock))  
653          {          {
654                  DEBUG_KBD(("CapsLock + Ctrl pressed, releasing LeftShift\n"));                  if (keymap_loaded)
655                  tr.modifiers ^= MapLeftShiftMask;                          warning("No translation for (keysym 0x%lx, %s)\n", keysym,
656          }                                  get_ksname(keysym));
657  #endif  
658                    /* not in keymap, try to interpret the raw scancode */
659                    if (((int) keycode >= min_keycode) && (keycode <= 0x60))
660                    {
661                            tr.scancode = keycode - min_keycode;
662    
663                            /* The modifiers to send for this key should be
664                               obtained from the local state. Currently, only
665                               shift is implemented. */
666                            if (MASK_HAS_BITS(state, ShiftMask))
667                            {
668                                    tr.modifiers = MapLeftShiftMask;
669                            }
670    
671          if (tr.scancode != 0)                          DEBUG_KBD(("Sending guessed scancode 0x%x\n", tr.scancode));
672          {                  }
673                  DEBUG_KBD(("Found key translation, scancode=0x%x, modifiers=0x%x\n",                  else
674                             tr.scancode, tr.modifiers));                  {
675                  return tr;                          DEBUG_KBD(("No good guess for keycode 0x%x found\n", keycode));
676                    }
677          }          }
678    
679          if (keymap_loaded)          return tr;
680                  warning("No translation for (keysym 0x%lx, %s)\n", keysym, get_ksname(keysym));  }
681    
682    void
683    xkeymap_send_keys(uint32 keysym, unsigned int keycode, unsigned int state, uint32 ev_time,
684                      BOOL pressed, uint8 nesting)
685    {
686            key_translation tr, *ptr;
687            tr = xkeymap_translate_key(keysym, keycode, state);
688    
689          /* not in keymap, try to interpret the raw scancode */          if (tr.seq_keysym == 0)
         if (((int) keycode >= min_keycode) && (keycode <= 0x60))  
690          {          {
691                  tr.scancode = keycode - min_keycode;                  /* Scancode translation */
692                    if (tr.scancode == 0)
693                            return;
694    
695                  /* The modifiers to send for this key should be                  if (pressed)
                    obtained from the local state. Currently, only  
                    shift is implemented. */  
                 if (state & ShiftMask)  
696                  {                  {
697                          tr.modifiers = MapLeftShiftMask;                          save_remote_modifiers(tr.scancode);
698                            ensure_remote_modifiers(ev_time, tr);
699                            rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);
700                            restore_remote_modifiers(ev_time, tr.scancode);
701                  }                  }
702                    else
703                  DEBUG_KBD(("Sending guessed scancode 0x%x\n", tr.scancode));                  {
704                            rdp_send_scancode(ev_time, RDP_KEYRELEASE, tr.scancode);
705                    }
706                    return;
707          }          }
708          else  
709            /* Sequence, only on key down */
710            if (pressed)
711          {          {
712                  DEBUG_KBD(("No good guess for keycode 0x%x found\n", keycode));                  ptr = &tr;
713          }                  do
714                    {
715                            DEBUG_KBD(("Handling sequence element, keysym=0x%x\n",
716                                       (unsigned int) ptr->seq_keysym));
717    
718          return tr;                          if (nesting++ > 32)
719                            {
720                                    error("Sequence nesting too deep\n");
721                                    return;
722                            }
723    
724                            xkeymap_send_keys(ptr->seq_keysym, keycode, state, ev_time, True, nesting);
725                            xkeymap_send_keys(ptr->seq_keysym, keycode, state, ev_time, False, nesting);
726                            ptr = ptr->next;
727                    }
728                    while (ptr);
729            }
730  }  }
731    
732  uint16  uint16

Legend:
Removed from v.894  
changed lines
  Added in v.1294

  ViewVC Help
Powered by ViewVC 1.1.26