/[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 957 by astrand, Wed Aug 3 08:25:32 2005 UTC
# Line 47  extern BOOL g_use_rdp5; Line 47  extern BOOL g_use_rdp5;
47  extern BOOL g_numlock_sync;  extern BOOL g_numlock_sync;
48    
49  static BOOL keymap_loaded;  static BOOL keymap_loaded;
50  static key_translation keymap[KEYMAP_SIZE];  static key_translation *keymap[KEYMAP_SIZE];
51  static int min_keycode;  static int min_keycode;
52  static uint16 remote_modifier_state = 0;  static uint16 remote_modifier_state = 0;
53  static uint16 saved_remote_modifier_state = 0;  static uint16 saved_remote_modifier_state = 0;
54    
55  static void update_modifier_state(uint8 scancode, BOOL pressed);  static void update_modifier_state(uint8 scancode, BOOL pressed);
56    
57    /* Free key_translation structure, including linked list */
58    static void
59    free_key_translation(key_translation * ptr)
60    {
61            key_translation *next;
62    
63            while (ptr)
64            {
65                    next = ptr->next;
66                    xfree(ptr);
67                    ptr = next;
68            }
69    }
70    
71  static void  static void
72  add_to_keymap(char *keyname, uint8 scancode, uint16 modifiers, char *mapname)  add_to_keymap(char *keyname, uint8 scancode, uint16 modifiers, char *mapname)
73  {  {
74          KeySym keysym;          KeySym keysym;
75            key_translation *tr;
76    
77          keysym = XStringToKeysym(keyname);          keysym = XStringToKeysym(keyname);
78          if (keysym == NoSymbol)          if (keysym == NoSymbol)
# Line 69  add_to_keymap(char *keyname, uint8 scanc Line 84  add_to_keymap(char *keyname, uint8 scanc
84          DEBUG_KBD(("Adding translation, keysym=0x%x, scancode=0x%x, "          DEBUG_KBD(("Adding translation, keysym=0x%x, scancode=0x%x, "
85                     "modifiers=0x%x\n", (unsigned int) keysym, scancode, modifiers));                     "modifiers=0x%x\n", (unsigned int) keysym, scancode, modifiers));
86    
87          keymap[keysym & KEYMAP_MASK].scancode = scancode;          tr = (key_translation *) xmalloc(sizeof(key_translation));
88          keymap[keysym & KEYMAP_MASK].modifiers = modifiers;          memset(tr, 0, sizeof(key_translation));
89            tr->scancode = scancode;
90            tr->modifiers = modifiers;
91            free_key_translation(keymap[keysym & KEYMAP_MASK]);
92            keymap[keysym & KEYMAP_MASK] = tr;
93    
94          return;          return;
95  }  }
96    
97    static void
98    add_sequence(char *rest, char *mapname)
99    {
100            KeySym keysym;
101            key_translation *tr, **prev_next;
102            size_t chars;
103            char keyname[KEYMAP_MAX_LINE_LENGTH];
104    
105            /* Skip over whitespace after the sequence keyword */
106            chars = strspn(rest, " \t");
107            rest += chars;
108    
109            /* Fetch the keysym name */
110            chars = strcspn(rest, " \t\0");
111            STRNCPY(keyname, rest, chars + 1);
112            rest += chars;
113    
114            keysym = XStringToKeysym(keyname);
115            if (keysym == NoSymbol)
116            {
117                    DEBUG_KBD(("Bad keysym \"%s\" in keymap %s (ignoring line)\n", keyname, mapname));
118                    return;
119            }
120    
121    
122            DEBUG_KBD(("Adding sequence for keysym (0x%lx, %s) -> ", keysym, keyname));
123    
124            free_key_translation(keymap[keysym & KEYMAP_MASK]);
125            prev_next = &keymap[keysym & KEYMAP_MASK];
126    
127            while (*rest)
128            {
129                    /* Skip whitespace */
130                    chars = strspn(rest, " \t");
131                    rest += chars;
132    
133                    /* Fetch the keysym name */
134                    chars = strcspn(rest, " \t\0");
135                    STRNCPY(keyname, rest, chars + 1);
136                    rest += chars;
137    
138                    keysym = XStringToKeysym(keyname);
139                    if (keysym == NoSymbol)
140                    {
141                            DEBUG_KBD(("Bad keysym \"%s\" in keymap %s (ignoring line)\n", keyname,
142                                       mapname));
143                            return;
144                    }
145    
146                    /* Allocate space for key_translation structure */
147                    tr = (key_translation *) xmalloc(sizeof(key_translation));
148                    memset(tr, 0, sizeof(key_translation));
149                    *prev_next = tr;
150                    prev_next = &tr->next;
151                    tr->seq_keysym = keysym;
152    
153                    DEBUG_KBD(("0x%x, ", (unsigned int) keysym));
154            }
155            DEBUG_KBD(("\n"));
156    }
157    
158    /* Joins two path components. The result should be freed with
159       xfree(). */
160    static char *
161    pathjoin(const char *a, const char *b)
162    {
163            char *result;
164            result = xmalloc(PATH_MAX * 2 + 1);
165    
166            if (b[0] == '/')
167            {
168                    strncpy(result, b, PATH_MAX);
169            }
170            else
171            {
172                    strncpy(result, a, PATH_MAX);
173                    strcat(result, "/");
174                    strncat(result, b, PATH_MAX);
175            }
176            return result;
177    }
178    
179    /* Try to open a keymap with fopen() */
180    FILE *
181    xkeymap_open(const char *filename)
182    {
183            char *path1, *path2;
184            char *home;
185            FILE *fp;
186    
187            /* Try KEYMAP_PATH */
188            path1 = pathjoin(KEYMAP_PATH, filename);
189            fp = fopen(path1, "r");
190            xfree(path1);
191            if (fp)
192                    return fp;
193    
194            /* Try ~/.rdesktop/keymaps */
195            home = getenv("HOME");
196            if (home)
197            {
198                    path1 = pathjoin(home, ".rdesktop/keymaps");
199                    path2 = pathjoin(path1, filename);
200                    xfree(path1);
201                    fp = fopen(path2, "r");
202                    xfree(path2);
203                    if (fp)
204                            return fp;
205            }
206    
207            /* Try current directory, in case we are running from the source
208               tree */
209            path1 = pathjoin("keymaps", filename);
210            fp = fopen(path1, "r");
211            xfree(path1);
212            if (fp)
213                    return fp;
214    
215            return NULL;
216    }
217    
218  static BOOL  static BOOL
219  xkeymap_read(char *mapname)  xkeymap_read(char *mapname)
220  {  {
221          FILE *fp;          FILE *fp;
222          char line[KEYMAP_MAX_LINE_LENGTH];          char line[KEYMAP_MAX_LINE_LENGTH];
         char path[PATH_MAX], inplace_path[PATH_MAX];  
223          unsigned int line_num = 0;          unsigned int line_num = 0;
224          unsigned int line_length = 0;          unsigned int line_length = 0;
225          char *keyname, *p;          char *keyname, *p;
# Line 89  xkeymap_read(char *mapname) Line 227  xkeymap_read(char *mapname)
227          uint8 scancode;          uint8 scancode;
228          uint16 modifiers;          uint16 modifiers;
229    
230            fp = xkeymap_open(mapname);
         strcpy(path, KEYMAP_PATH);  
         strncat(path, mapname, sizeof(path) - sizeof(KEYMAP_PATH));  
   
         fp = fopen(path, "r");  
231          if (fp == NULL)          if (fp == NULL)
232          {          {
233                  /* in case we are running from the source tree */                  error("Failed to open keymap %s\n", mapname);
234                  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;  
                 }  
235          }          }
236    
237          /* FIXME: More tolerant on white space */          /* FIXME: More tolerant on white space */
# Line 150  xkeymap_read(char *mapname) Line 276  xkeymap_read(char *mapname)
276                          continue;                          continue;
277                  }                  }
278    
279                    /* sequence */
280                    if (strncmp(line, "sequence", 8) == 0)
281                    {
282                            add_sequence(line + 8, mapname);
283                            continue;
284                    }
285    
286                  /* Comment */                  /* Comment */
287                  if (line[0] == '#')                  if (line[0] == '#')
288                  {                  {
# Line 293  reset_winkey(uint32 ev_time) Line 426  reset_winkey(uint32 ev_time)
426          }          }
427  }  }
428    
429  /* Handles, for example, multi-scancode keypresses (which is not  /* Handle special key combinations */
    possible via keymap-files) */  
430  BOOL  BOOL
431  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)
432  {  {
# Line 372  handle_special_keys(uint32 keysym, unsig Line 504  handle_special_keys(uint32 keysym, unsig
504                              && (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)))
505                                  return True;                                  return True;
506                          break;                          break;
507    
508                  case XK_Num_Lock:                  case XK_Num_Lock:
509                          /* FIXME: We might want to do RDP_INPUT_SYNCHRONIZE here, if g_numlock_sync */                          /* Synchronize on key release */
510                          if (!g_numlock_sync)                          if (g_numlock_sync && !pressed)
511                                  /* Inhibit */                                  rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0,
512                                  return True;                                                 ui_get_numlock_state(read_keyboard_state()), 0);
513    
514                            /* Inhibit */
515                            return True;
516                          break;                          break;
517    
518          }          }
# Line 387  handle_special_keys(uint32 keysym, unsig Line 523  handle_special_keys(uint32 keysym, unsig
523  key_translation  key_translation
524  xkeymap_translate_key(uint32 keysym, unsigned int keycode, unsigned int state)  xkeymap_translate_key(uint32 keysym, unsigned int keycode, unsigned int state)
525  {  {
526          key_translation tr = { 0, 0 };          key_translation tr = { 0, 0, 0, 0 };
527            key_translation *ptr;
         tr = keymap[keysym & KEYMAP_MASK];  
   
         if (tr.modifiers & MapInhibitMask)  
         {  
                 DEBUG_KBD(("Inhibiting key\n"));  
                 tr.scancode = 0;  
                 return tr;  
         }  
528    
529          if (tr.modifiers & MapLocalStateMask)          ptr = keymap[keysym & KEYMAP_MASK];
530            if (ptr)
531          {          {
532                  /* The modifiers to send for this key should be obtained                  tr = *ptr;
533                     from the local state. Currently, only shift is implemented. */                  if (tr.seq_keysym == 0) /* Normal scancode translation */
                 if (state & ShiftMask)  
534                  {                  {
535                          tr.modifiers = MapLeftShiftMask;                          if (tr.modifiers & MapInhibitMask)
536                            {
537                                    DEBUG_KBD(("Inhibiting key\n"));
538                                    tr.scancode = 0;
539                                    return tr;
540                            }
541    
542                            if (tr.modifiers & MapLocalStateMask)
543                            {
544                                    /* The modifiers to send for this key should be obtained
545                                       from the local state. Currently, only shift is implemented. */
546                                    if (state & ShiftMask)
547                                    {
548                                            tr.modifiers = MapLeftShiftMask;
549                                    }
550                            }
551    
552                            if ((tr.modifiers & MapLeftShiftMask)
553                                && ((remote_modifier_state & MapLeftCtrlMask)
554                                    || (remote_modifier_state & MapRightCtrlMask))
555                                && get_key_state(state, XK_Caps_Lock))
556                            {
557                                    DEBUG_KBD(("CapsLock + Ctrl pressed, releasing LeftShift\n"));
558                                    tr.modifiers ^= MapLeftShiftMask;
559                            }
560    
561                            DEBUG_KBD(("Found scancode translation, scancode=0x%x, modifiers=0x%x\n",
562                                       tr.scancode, tr.modifiers));
563                  }                  }
564          }          }
565            else
 #if 0  
         if (((remote_modifier_state & MapLeftCtrlMask)  
              || (remote_modifier_state & MapRightCtrlMask)) && get_key_state(state, XK_Caps_Lock))  
566          {          {
567                  DEBUG_KBD(("CapsLock + Ctrl pressed, releasing LeftShift\n"));                  if (keymap_loaded)
568                  tr.modifiers ^= MapLeftShiftMask;                          warning("No translation for (keysym 0x%lx, %s)\n", keysym,
569          }                                  get_ksname(keysym));
570  #endif  
571                    /* not in keymap, try to interpret the raw scancode */
572                    if (((int) keycode >= min_keycode) && (keycode <= 0x60))
573                    {
574                            tr.scancode = keycode - min_keycode;
575    
576                            /* The modifiers to send for this key should be
577                               obtained from the local state. Currently, only
578                               shift is implemented. */
579                            if (state & ShiftMask)
580                            {
581                                    tr.modifiers = MapLeftShiftMask;
582                            }
583    
584          if (tr.scancode != 0)                          DEBUG_KBD(("Sending guessed scancode 0x%x\n", tr.scancode));
585          {                  }
586                  DEBUG_KBD(("Found key translation, scancode=0x%x, modifiers=0x%x\n",                  else
587                             tr.scancode, tr.modifiers));                  {
588                  return tr;                          DEBUG_KBD(("No good guess for keycode 0x%x found\n", keycode));
589                    }
590          }          }
591    
592          if (keymap_loaded)          return tr;
593                  warning("No translation for (keysym 0x%lx, %s)\n", keysym, get_ksname(keysym));  }
594    
595    void
596    xkeymap_send_keys(uint32 keysym, unsigned int keycode, unsigned int state, uint32 ev_time,
597                      BOOL pressed)
598    {
599            key_translation tr, *ptr;
600            tr = xkeymap_translate_key(keysym, keycode, state);
601    
602          /* not in keymap, try to interpret the raw scancode */          if (tr.seq_keysym == 0)
         if (((int) keycode >= min_keycode) && (keycode <= 0x60))  
603          {          {
604                  tr.scancode = keycode - min_keycode;                  /* Scancode translation */
605                    if (tr.scancode == 0)
606                            return;
607    
608                  /* The modifiers to send for this key should be                  if (pressed)
                    obtained from the local state. Currently, only  
                    shift is implemented. */  
                 if (state & ShiftMask)  
609                  {                  {
610                          tr.modifiers = MapLeftShiftMask;                          save_remote_modifiers(tr.scancode);
611                            ensure_remote_modifiers(ev_time, tr);
612                            rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);
613                            restore_remote_modifiers(ev_time, tr.scancode);
614                  }                  }
615                    else
616                  DEBUG_KBD(("Sending guessed scancode 0x%x\n", tr.scancode));                  {
617                            rdp_send_scancode(ev_time, RDP_KEYRELEASE, tr.scancode);
618                    }
619                    return;
620          }          }
621          else  
622            /* Sequence, only on key down */
623            if (pressed)
624          {          {
625                  DEBUG_KBD(("No good guess for keycode 0x%x found\n", keycode));                  ptr = &tr;
626                    do
627                    {
628                            DEBUG_KBD(("Handling sequence element, keysym=0x%x\n",
629                                       (unsigned int) ptr->seq_keysym));
630                            xkeymap_send_keys(ptr->seq_keysym, keycode, state, ev_time, True);
631                            xkeymap_send_keys(ptr->seq_keysym, keycode, state, ev_time, False);
632                            ptr = ptr->next;
633                    }
634                    while (ptr);
635          }          }
   
         return tr;  
636  }  }
637    
638  uint16  uint16

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

  ViewVC Help
Powered by ViewVC 1.1.26