/[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 553 by astrand, Mon Dec 8 15:28:24 2003 UTC revision 951 by astrand, Tue Aug 2 18:07:56 2005 UTC
# Line 2  Line 2 
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    
5     Copyright (C) Matthew Chapman 1999-2002     Copyright (C) Matthew Chapman 1999-2005
6     Copyright (C) Peter Astrand <peter@cendio.se> 2003     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
# Line 40  Line 40 
40  extern Display *g_display;  extern Display *g_display;
41  extern Window g_wnd;  extern Window g_wnd;
42  extern char keymapname[16];  extern char keymapname[16];
43  extern int keylayout;  extern int g_keylayout;
44  extern int g_win_button_size;  extern int g_win_button_size;
45  extern BOOL g_enable_compose;  extern BOOL g_enable_compose;
46  extern BOOL g_use_rdp5;  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, included linked list */
58    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  static BOOL  static BOOL
159  xkeymap_read(char *mapname)  xkeymap_read(char *mapname)
# Line 137  xkeymap_read(char *mapname) Line 216  xkeymap_read(char *mapname)
216                  /* map */                  /* map */
217                  if (strncmp(line, "map ", 4) == 0)                  if (strncmp(line, "map ", 4) == 0)
218                  {                  {
219                          keylayout = strtol(line + 4, NULL, 16);                          g_keylayout = strtol(line + 4, NULL, 16);
220                          DEBUG_KBD(("Keylayout 0x%x\n", keylayout));                          DEBUG_KBD(("Keylayout 0x%x\n", g_keylayout));
221                          continue;                          continue;
222                  }                  }
223    
# Line 150  xkeymap_read(char *mapname) Line 229  xkeymap_read(char *mapname)
229                          continue;                          continue;
230                  }                  }
231    
232                    /* sequence */
233                    if (strncmp(line, "sequence", 8) == 0)
234                    {
235                            add_sequence(line + 8, mapname);
236                            continue;
237                    }
238    
239                  /* Comment */                  /* Comment */
240                  if (line[0] == '#')                  if (line[0] == '#')
241                  {                  {
# Line 372  handle_special_keys(uint32 keysym, unsig Line 458  handle_special_keys(uint32 keysym, unsig
458                              && (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)))
459                                  return True;                                  return True;
460                          break;                          break;
461    
462                  case XK_Num_Lock:                  case XK_Num_Lock:
463                          /* FIXME: We might want to do RDP_INPUT_SYNCHRONIZE here, if g_numlock_sync */                          /* Synchronize on key release */
464                          if (!g_numlock_sync)                          if (g_numlock_sync && !pressed)
465                                  /* Inhibit */                                  rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0,
466                                  return True;                                                 ui_get_numlock_state(read_keyboard_state()), 0);
467    
468                            /* Inhibit */
469                            return True;
470                          break;                          break;
471    
472          }          }
# Line 387  handle_special_keys(uint32 keysym, unsig Line 477  handle_special_keys(uint32 keysym, unsig
477  key_translation  key_translation
478  xkeymap_translate_key(uint32 keysym, unsigned int keycode, unsigned int state)  xkeymap_translate_key(uint32 keysym, unsigned int keycode, unsigned int state)
479  {  {
480          key_translation tr = { 0, 0 };          key_translation tr = { 0, 0, 0, 0 };
481            key_translation *ptr;
482    
483          tr = keymap[keysym & KEYMAP_MASK];          ptr = keymap[keysym & KEYMAP_MASK];
484            if (ptr)
         if (tr.modifiers & MapInhibitMask)  
485          {          {
486                  DEBUG_KBD(("Inhibiting key\n"));                  tr = *ptr;
487                  tr.scancode = 0;                  if (tr.seq_keysym == 0) /* Normal scancode translation */
488                  return tr;                  {
489          }                          if (tr.modifiers & MapInhibitMask)
490                            {
491                                    DEBUG_KBD(("Inhibiting key\n"));
492                                    tr.scancode = 0;
493                                    return tr;
494                            }
495    
496                            if (tr.modifiers & MapLocalStateMask)
497                            {
498                                    /* The modifiers to send for this key should be obtained
499                                       from the local state. Currently, only shift is implemented. */
500                                    if (state & ShiftMask)
501                                    {
502                                            tr.modifiers = MapLeftShiftMask;
503                                    }
504                            }
505    
506          if (tr.modifiers & MapLocalStateMask)                          if ((tr.modifiers & MapLeftShiftMask)
507                                && ((remote_modifier_state & MapLeftCtrlMask)
508                                    || (remote_modifier_state & MapRightCtrlMask))
509                                && get_key_state(state, XK_Caps_Lock))
510                            {
511                                    DEBUG_KBD(("CapsLock + Ctrl pressed, releasing LeftShift\n"));
512                                    tr.modifiers ^= MapLeftShiftMask;
513                            }
514    
515                            DEBUG_KBD(("Found scancode translation, scancode=0x%x, modifiers=0x%x\n",
516                                       tr.scancode, tr.modifiers));
517                    }
518            }
519            else
520          {          {
521                  /* The modifiers to send for this key should be obtained                  if (keymap_loaded)
522                     from the local state. Currently, only shift is implemented. */                          warning("No translation for (keysym 0x%lx, %s)\n", keysym,
523                  if (state & ShiftMask)                                  get_ksname(keysym));
524    
525                    /* not in keymap, try to interpret the raw scancode */
526                    if (((int) keycode >= min_keycode) && (keycode <= 0x60))
527                    {
528                            tr.scancode = keycode - min_keycode;
529    
530                            /* The modifiers to send for this key should be
531                               obtained from the local state. Currently, only
532                               shift is implemented. */
533                            if (state & ShiftMask)
534                            {
535                                    tr.modifiers = MapLeftShiftMask;
536                            }
537    
538                            DEBUG_KBD(("Sending guessed scancode 0x%x\n", tr.scancode));
539                    }
540                    else
541                  {                  {
542                          tr.modifiers = MapLeftShiftMask;                          DEBUG_KBD(("No good guess for keycode 0x%x found\n", keycode));
543                  }                  }
544          }          }
545    
546          if (tr.scancode != 0)          return tr;
547          {  }
                 DEBUG_KBD(("Found key translation, scancode=0x%x, modifiers=0x%x\n",  
                            tr.scancode, tr.modifiers));  
                 return tr;  
         }  
548    
549          if (keymap_loaded)  void
550                  warning("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,
551                      BOOL pressed)
552    {
553            key_translation tr, *ptr;
554            tr = xkeymap_translate_key(keysym, keycode, state);
555    
556          /* not in keymap, try to interpret the raw scancode */          if (tr.seq_keysym == 0)
         if (((int) keycode >= min_keycode) && (keycode <= 0x60))  
557          {          {
558                  tr.scancode = keycode - min_keycode;                  /* Scancode translation */
559                    if (tr.scancode == 0)
560                            return;
561    
562                  /* The modifiers to send for this key should be                  if (pressed)
                    obtained from the local state. Currently, only  
                    shift is implemented. */  
                 if (state & ShiftMask)  
563                  {                  {
564                          tr.modifiers = MapLeftShiftMask;                          save_remote_modifiers(tr.scancode);
565                            ensure_remote_modifiers(ev_time, tr);
566                            rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);
567                            restore_remote_modifiers(ev_time, tr.scancode);
568                  }                  }
569                    else
570                  DEBUG_KBD(("Sending guessed scancode 0x%x\n", tr.scancode));                  {
571                            rdp_send_scancode(ev_time, RDP_KEYRELEASE, tr.scancode);
572                    }
573                    return;
574          }          }
575          else  
576            /* Sequence, only on key down */
577            if (pressed)
578          {          {
579                  DEBUG_KBD(("No good guess for keycode 0x%x found\n", keycode));                  ptr = &tr;
580                    do
581                    {
582                            DEBUG_KBD(("Handling sequence element, keysym=0x%x\n",
583                                       (unsigned int) ptr->seq_keysym));
584                            xkeymap_send_keys(ptr->seq_keysym, keycode, state, ev_time, True);
585                            xkeymap_send_keys(ptr->seq_keysym, keycode, state, ev_time, False);
586                            ptr = ptr->next;
587                    }
588                    while (ptr);
589          }          }
   
         return tr;  
590  }  }
591    
592  uint16  uint16
# Line 602  ensure_remote_modifiers(uint32 ev_time, Line 750  ensure_remote_modifiers(uint32 ev_time,
750  unsigned int  unsigned int
751  read_keyboard_state()  read_keyboard_state()
752  {  {
753    #ifdef RDP2VNC
754            return 0;
755    #else
756          unsigned int state;          unsigned int state;
757          Window wdummy;          Window wdummy;
758          int dummy;          int dummy;
759    
760          XQueryPointer(g_display, g_wnd, &wdummy, &wdummy, &dummy, &dummy, &dummy, &dummy, &state);          XQueryPointer(g_display, g_wnd, &wdummy, &wdummy, &dummy, &dummy, &dummy, &dummy, &state);
761          return state;          return state;
762    #endif
763  }  }
764    
765    
# Line 652  reset_modifier_keys() Line 804  reset_modifier_keys()
804                  rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LALT);                  rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LALT);
805    
806          if (MASK_HAS_BITS(remote_modifier_state, MapRightAltMask) &&          if (MASK_HAS_BITS(remote_modifier_state, MapRightAltMask) &&
807              !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)
808                && !get_key_state(state, XK_ISO_Level3_Shift))
809                  rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT);                  rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT);
810    
811          reset_winkey(ev_time);          reset_winkey(ev_time);

Legend:
Removed from v.553  
changed lines
  Added in v.951

  ViewVC Help
Powered by ViewVC 1.1.26