/[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 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    
# 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 47  extern BOOL g_use_rdp5; Line 48  extern BOOL g_use_rdp5;
48  extern BOOL g_numlock_sync;  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;  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)
# Line 69  add_to_keymap(char *keyname, uint8 scanc Line 85  add_to_keymap(char *keyname, uint8 scanc
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 89  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 150  xkeymap_read(char *mapname) Line 334  xkeymap_read(char *mapname)
334                          continue;                          continue;
335                  }                  }
336    
337                    /* sequence */
338                    if (strncmp(line, "sequence", 8) == 0)
339                    {
340                            add_sequence(line + 8, mapname);
341                            continue;
342                    }
343    
344                  /* Comment */                  /* Comment */
345                  if (line[0] == '#')                  if (line[0] == '#')
346                  {                  {
# Line 225  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((int) *mapname_ptr);  
                 mapname_ptr++;  
         }  
419    
420          if (strcmp(keymapname, "none"))          if (strcmp(keymapname, "none"))
421          {          {
# Line 293  reset_winkey(uint32 ev_time) Line 475  reset_winkey(uint32 ev_time)
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  {  {
# Line 372  handle_special_keys(uint32 keysym, unsig Line 553  handle_special_keys(uint32 keysym, unsig
553                              && (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)))
554                                  return True;                                  return True;
555                          break;                          break;
556    
557                  case XK_Num_Lock:                  case XK_Num_Lock:
558                          /* FIXME: We might want to do RDP_INPUT_SYNCHRONIZE here, if g_numlock_sync */                          /* Synchronize on key release */
559                          if (!g_numlock_sync)                          if (g_numlock_sync && !pressed)
560                                  /* Inhibit */                                  rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0,
561                                  return True;                                                 ui_get_numlock_state(read_keyboard_state()), 0);
562    
563                            /* Inhibit */
564                            return True;
565                          break;                          break;
566    
567          }          }
# Line 387  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];  
   
         if (tr.modifiers & MapInhibitMask)  
         {  
                 DEBUG_KBD(("Inhibiting key\n"));  
                 tr.scancode = 0;  
                 return tr;  
         }  
577    
578          if (tr.modifiers & MapLocalStateMask)          ptr = keymap[keysym & KEYMAP_MASK];
579            if (ptr)
580          {          {
581                  /* The modifiers to send for this key should be obtained                  tr = *ptr;
582                     from the local state. Currently, only shift is implemented. */                  if (tr.seq_keysym == 0) /* Normal scancode translation */
                 if (state & ShiftMask)  
583                  {                  {
584                          tr.modifiers = MapLeftShiftMask;                          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 & 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
 #if 0  
         if (((remote_modifier_state & MapLeftCtrlMask)  
              || (remote_modifier_state & MapRightCtrlMask)) && get_key_state(state, XK_Caps_Lock))  
615          {          {
616                  DEBUG_KBD(("CapsLock + Ctrl pressed, releasing LeftShift\n"));                  if (keymap_loaded)
617                  tr.modifiers ^= MapLeftShiftMask;                          warning("No translation for (keysym 0x%lx, %s)\n", keysym,
618          }                                  get_ksname(keysym));
619  #endif  
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          if (tr.scancode != 0)                          DEBUG_KBD(("Sending guessed scancode 0x%x\n", tr.scancode));
634          {                  }
635                  DEBUG_KBD(("Found key translation, scancode=0x%x, modifiers=0x%x\n",                  else
636                             tr.scancode, tr.modifiers));                  {
637                  return tr;                          DEBUG_KBD(("No good guess for keycode 0x%x found\n", keycode));
638                    }
639          }          }
640    
641          if (keymap_loaded)          return tr;
642                  warning("No translation for (keysym 0x%lx, %s)\n", keysym, get_ksname(keysym));  }
643    
644    void
645    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 (((int) 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

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

  ViewVC Help
Powered by ViewVC 1.1.26