/[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 113 by astrand, Wed Sep 11 07:39:53 2002 UTC revision 810 by stargo, Sun Jan 23 19:29:31 2005 UTC
# Line 1  Line 1 
1  /*  /*
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-2001  
5       Copyright (C) Matthew Chapman 1999-2002
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  #include <X11/keysym.h>  #include <X11/keysym.h>
28  #include <stdio.h>  #endif
29  #include <stdlib.h>  
 #include <string.h>  
30  #include <ctype.h>  #include <ctype.h>
31  #include <limits.h>  #include <limits.h>
32    #include <time.h>
33  #include "rdesktop.h"  #include "rdesktop.h"
34  #include "scancodes.h"  #include "scancodes.h"
35    
36  #define KEYMAP_SIZE 4096  #define KEYMAP_SIZE 0xffff+1
37  #define KEYMAP_MASK (KEYMAP_SIZE - 1)  #define KEYMAP_MASK 0xffff
38  #define KEYMAP_MAX_LINE_LENGTH 80  #define KEYMAP_MAX_LINE_LENGTH 80
39    
40  extern Display *display;  extern Display *g_display;
41    extern Window g_wnd;
42  extern char keymapname[16];  extern char keymapname[16];
43  extern int keylayout;  extern int g_keylayout;
44  extern BOOL enable_compose;  extern int g_win_button_size;
45    extern BOOL g_enable_compose;
46    extern BOOL g_use_rdp5;
47    extern BOOL g_numlock_sync;
48    
49    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;
54    
55    static void update_modifier_state(uint8 scancode, BOOL pressed);
56    
57  static void  static void
58  add_to_keymap(char *keyname, uint8 scancode, uint16 modifiers, char *mapname)  add_to_keymap(char *keyname, uint8 scancode, uint16 modifiers, char *mapname)
# Line 49  add_to_keymap(char *keyname, uint8 scanc Line 62  add_to_keymap(char *keyname, uint8 scanc
62          keysym = XStringToKeysym(keyname);          keysym = XStringToKeysym(keyname);
63          if (keysym == NoSymbol)          if (keysym == NoSymbol)
64          {          {
65                  error("Bad keysym %s in keymap %s\n", keyname, mapname);                  DEBUG_KBD(("Bad keysym \"%s\" in keymap %s (ignoring)\n", keyname, mapname));
66                  return;                  return;
67          }          }
68    
# Line 67  static BOOL Line 80  static BOOL
80  xkeymap_read(char *mapname)  xkeymap_read(char *mapname)
81  {  {
82          FILE *fp;          FILE *fp;
83          char line[KEYMAP_MAX_LINE_LENGTH], path[PATH_MAX];          char line[KEYMAP_MAX_LINE_LENGTH];
84            char path[PATH_MAX], inplace_path[PATH_MAX];
85          unsigned int line_num = 0;          unsigned int line_num = 0;
86          unsigned int line_length = 0;          unsigned int line_length = 0;
87          char *keyname, *p;          char *keyname, *p;
# Line 82  xkeymap_read(char *mapname) Line 96  xkeymap_read(char *mapname)
96          fp = fopen(path, "r");          fp = fopen(path, "r");
97          if (fp == NULL)          if (fp == NULL)
98          {          {
99                  error("Failed to open keymap %s\n", path);                  /* in case we are running from the source tree */
100                  return False;                  strcpy(inplace_path, "keymaps/");
101                    strncat(inplace_path, mapname, sizeof(inplace_path) - sizeof("keymaps/"));
102    
103                    fp = fopen(inplace_path, "r");
104                    if (fp == NULL)
105                    {
106                            error("Failed to open keymap %s\n", path);
107                            return False;
108                    }
109          }          }
110    
111          /* FIXME: More tolerant on white space */          /* FIXME: More tolerant on white space */
# Line 115  xkeymap_read(char *mapname) Line 137  xkeymap_read(char *mapname)
137                  /* map */                  /* map */
138                  if (strncmp(line, "map ", 4) == 0)                  if (strncmp(line, "map ", 4) == 0)
139                  {                  {
140                          keylayout = strtol(line + 4, NULL, 16);                          g_keylayout = strtol(line + 4, NULL, 16);
141                          DEBUG_KBD(("Keylayout 0x%x\n", keylayout));                          DEBUG_KBD(("Keylayout 0x%x\n", g_keylayout));
142                          continue;                          continue;
143                  }                  }
144    
# Line 124  xkeymap_read(char *mapname) Line 146  xkeymap_read(char *mapname)
146                  if (strncmp(line, "enable_compose", 15) == 0)                  if (strncmp(line, "enable_compose", 15) == 0)
147                  {                  {
148                          DEBUG_KBD(("Enabling compose handling\n"));                          DEBUG_KBD(("Enabling compose handling\n"));
149                          enable_compose = True;                          g_enable_compose = True;
150                          continue;                          continue;
151                  }                  }
152    
# Line 175  xkeymap_read(char *mapname) Line 197  xkeymap_read(char *mapname)
197                          MASK_ADD_BITS(modifiers, MapLocalStateMask);                          MASK_ADD_BITS(modifiers, MapLocalStateMask);
198                  }                  }
199    
200                    if (strstr(line_rest, "inhibit"))
201                    {
202                            MASK_ADD_BITS(modifiers, MapInhibitMask);
203                    }
204    
205                  add_to_keymap(keyname, scancode, modifiers, mapname);                  add_to_keymap(keyname, scancode, modifiers, mapname);
206    
207                  if (strstr(line_rest, "addupper"))                  if (strstr(line_rest, "addupper"))
# Line 182  xkeymap_read(char *mapname) Line 209  xkeymap_read(char *mapname)
209                          /* Automatically add uppercase key, with same modifiers                          /* Automatically add uppercase key, with same modifiers
210                             plus shift */                             plus shift */
211                          for (p = keyname; *p; p++)                          for (p = keyname; *p; p++)
212                                  *p = toupper(*p);                                  *p = toupper((int) *p);
213                          MASK_ADD_BITS(modifiers, MapLeftShiftMask);                          MASK_ADD_BITS(modifiers, MapLeftShiftMask);
214                          add_to_keymap(keyname, scancode, modifiers, mapname);                          add_to_keymap(keyname, scancode, modifiers, mapname);
215                  }                  }
# Line 195  xkeymap_read(char *mapname) Line 222  xkeymap_read(char *mapname)
222    
223  /* Before connecting and creating UI */  /* Before connecting and creating UI */
224  void  void
225  xkeymap_init1(void)  xkeymap_init(void)
226  {  {
227          int i;          unsigned int max_keycode;
228            char *mapname_ptr;
229    
230          /* Zeroing keymap */          /* Make keymapname lowercase */
231          for (i = 0; i < KEYMAP_SIZE; i++)          mapname_ptr = keymapname;
232            while (*mapname_ptr)
233          {          {
234                  keymap[i].scancode = 0;                  *mapname_ptr = tolower((int) *mapname_ptr);
235                  keymap[i].modifiers = 0;                  mapname_ptr++;
236          }          }
237    
238          if (strcmp(keymapname, "none"))          if (strcmp(keymapname, "none"))
239          {          {
240                  xkeymap_read(keymapname);                  if (xkeymap_read(keymapname))
241                            keymap_loaded = True;
242          }          }
243    
244            XDisplayKeycodes(g_display, &min_keycode, (int *) &max_keycode);
245  }  }
246    
247  /* After connecting and creating UI */  static void
248  void  send_winkey(uint32 ev_time, BOOL pressed, BOOL leftkey)
 xkeymap_init2(void)  
249  {  {
250          unsigned int max_keycode;          uint8 winkey;
251          XDisplayKeycodes(display, &min_keycode, (int *) &max_keycode);  
252            if (leftkey)
253                    winkey = SCANCODE_CHAR_LWIN;
254            else
255                    winkey = SCANCODE_CHAR_RWIN;
256    
257            if (pressed)
258            {
259                    if (g_use_rdp5)
260                    {
261                            rdp_send_scancode(ev_time, RDP_KEYPRESS, winkey);
262                    }
263                    else
264                    {
265                            /* RDP4 doesn't support winkey. Fake with Ctrl-Esc */
266                            rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_LCTRL);
267                            rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_ESC);
268                    }
269            }
270            else
271            {
272                    /* key released */
273                    if (g_use_rdp5)
274                    {
275                            rdp_send_scancode(ev_time, RDP_KEYRELEASE, winkey);
276                    }
277                    else
278                    {
279                            rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_ESC);
280                            rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL);
281                    }
282            }
283    }
284    
285    static void
286    reset_winkey(uint32 ev_time)
287    {
288            if (g_use_rdp5)
289            {
290                    /* For some reason, it seems to suffice to release
291                     *either* the left or right winkey. */
292                    rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LWIN);
293            }
294    }
295    
296    /* Handles, for example, multi-scancode keypresses (which is not
297       possible via keymap-files) */
298    BOOL
299    handle_special_keys(uint32 keysym, unsigned int state, uint32 ev_time, BOOL pressed)
300    {
301            switch (keysym)
302            {
303                    case XK_Return:
304                            if ((get_key_state(state, XK_Alt_L) || get_key_state(state, XK_Alt_R))
305                                && (get_key_state(state, XK_Control_L)
306                                    || get_key_state(state, XK_Control_R)))
307                            {
308                                    /* Ctrl-Alt-Enter: toggle full screen */
309                                    if (pressed)
310                                            xwin_toggle_fullscreen();
311                                    return True;
312                            }
313                            break;
314    
315                    case XK_Break:
316                            /* Send Break sequence E0 46 E0 C6 */
317                            if (pressed)
318                            {
319                                    rdp_send_scancode(ev_time, RDP_KEYPRESS,
320                                                      (SCANCODE_EXTENDED | 0x46));
321                                    rdp_send_scancode(ev_time, RDP_KEYPRESS,
322                                                      (SCANCODE_EXTENDED | 0xc6));
323                            }
324                            /* No release sequence */
325                            return True;
326                            break;
327    
328                    case XK_Pause:
329                            /* According to MS Keyboard Scan Code
330                               Specification, pressing Pause should result
331                               in E1 1D 45 E1 9D C5. I'm not exactly sure
332                               of how this is supposed to be sent via
333                               RDP. The code below seems to work, but with
334                               the side effect that Left Ctrl stays
335                               down. Therefore, we release it when Pause
336                               is released. */
337                            if (pressed)
338                            {
339                                    rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0xe1, 0);
340                                    rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0x1d, 0);
341                                    rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0x45, 0);
342                                    rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0xe1, 0);
343                                    rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0x9d, 0);
344                                    rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0xc5, 0);
345                            }
346                            else
347                            {
348                                    /* Release Left Ctrl */
349                                    rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYRELEASE,
350                                                   0x1d, 0);
351                            }
352                            return True;
353                            break;
354    
355                    case XK_Meta_L: /* Windows keys */
356                    case XK_Super_L:
357                    case XK_Hyper_L:
358                            send_winkey(ev_time, pressed, True);
359                            return True;
360                            break;
361    
362                    case XK_Meta_R:
363                    case XK_Super_R:
364                    case XK_Hyper_R:
365                            send_winkey(ev_time, pressed, False);
366                            return True;
367                            break;
368    
369                    case XK_space:
370                            /* Prevent access to the Windows system menu in single app mode */
371                            if (g_win_button_size
372                                && (get_key_state(state, XK_Alt_L) || get_key_state(state, XK_Alt_R)))
373                                    return True;
374                            break;
375                    case XK_Num_Lock:
376                            /* FIXME: We might want to do RDP_INPUT_SYNCHRONIZE here, if g_numlock_sync */
377                            if (!g_numlock_sync)
378                                    /* Inhibit */
379                                    return True;
380                            break;
381    
382            }
383            return False;
384  }  }
385    
386    
387  key_translation  key_translation
388  xkeymap_translate_key(KeySym keysym, unsigned int keycode, unsigned int state)  xkeymap_translate_key(uint32 keysym, unsigned int keycode, unsigned int state)
389  {  {
390          key_translation tr = { 0, 0 };          key_translation tr = { 0, 0 };
391    
392          tr = keymap[keysym & KEYMAP_MASK];          tr = keymap[keysym & KEYMAP_MASK];
393    
394            if (tr.modifiers & MapInhibitMask)
395            {
396                    DEBUG_KBD(("Inhibiting key\n"));
397                    tr.scancode = 0;
398                    return tr;
399            }
400    
401          if (tr.modifiers & MapLocalStateMask)          if (tr.modifiers & MapLocalStateMask)
402          {          {
403                  /* The modifiers to send for this key should be obtained                  /* The modifiers to send for this key should be obtained
# Line 239  xkeymap_translate_key(KeySym keysym, uns Line 408  xkeymap_translate_key(KeySym keysym, uns
408                  }                  }
409          }          }
410    
411            if (((remote_modifier_state & MapLeftCtrlMask)
412                 || (remote_modifier_state & MapRightCtrlMask)) && get_key_state(state, XK_Caps_Lock))
413            {
414                    DEBUG_KBD(("CapsLock + Ctrl pressed, releasing LeftShift\n"));
415                    tr.modifiers ^= MapLeftShiftMask;
416            }
417    
418          if (tr.scancode != 0)          if (tr.scancode != 0)
419          {          {
420                  DEBUG_KBD                  DEBUG_KBD(("Found key translation, scancode=0x%x, modifiers=0x%x\n",
421                          (("Found key translation, scancode=0x%x, modifiers=0x%x\n",                             tr.scancode, tr.modifiers));
                           tr.scancode, tr.modifiers));  
422                  return tr;                  return tr;
423          }          }
424    
425          fprintf(stderr, "No translation for (keysym 0x%lx, %s)\n", keysym, get_ksname(keysym));          if (keymap_loaded)
426                    warning("No translation for (keysym 0x%lx, %s)\n", keysym, get_ksname(keysym));
427    
428          /* not in keymap, try to interpret the raw scancode */          /* not in keymap, try to interpret the raw scancode */
429          if ((keycode >= min_keycode) && (keycode <= 0x60))          if (((int) keycode >= min_keycode) && (keycode <= 0x60))
430          {          {
431                  tr.scancode = keycode - min_keycode;                  tr.scancode = keycode - min_keycode;
432                  fprintf(stderr, "Sending guessed scancode 0x%x\n", tr.scancode);  
433                    /* The modifiers to send for this key should be
434                       obtained from the local state. Currently, only
435                       shift is implemented. */
436                    if (state & ShiftMask)
437                    {
438                            tr.modifiers = MapLeftShiftMask;
439                    }
440    
441                    DEBUG_KBD(("Sending guessed scancode 0x%x\n", tr.scancode));
442          }          }
443          else          else
444          {          {
445                  fprintf(stderr, "No good guess for keycode 0x%x found\n", keycode);                  DEBUG_KBD(("No good guess for keycode 0x%x found\n", keycode));
446          }          }
447    
448          return tr;          return tr;
# Line 284  xkeymap_translate_button(unsigned int bu Line 469  xkeymap_translate_button(unsigned int bu
469  }  }
470    
471  char *  char *
472  get_ksname(KeySym keysym)  get_ksname(uint32 keysym)
473  {  {
474          char *ksname = NULL;          char *ksname = NULL;
475    
# Line 296  get_ksname(KeySym keysym) Line 481  get_ksname(KeySym keysym)
481          return ksname;          return ksname;
482  }  }
483    
484  BOOL  static BOOL
485  inhibit_key(KeySym keysym)  is_modifier(uint8 scancode)
486  {  {
487          switch (keysym)          switch (scancode)
488          {          {
489                  case XK_Caps_Lock:                  case SCANCODE_CHAR_LSHIFT:
490                          return True;                  case SCANCODE_CHAR_RSHIFT:
491                          break;                  case SCANCODE_CHAR_LCTRL:
492                  case XK_Multi_key:                  case SCANCODE_CHAR_RCTRL:
493                    case SCANCODE_CHAR_LALT:
494                    case SCANCODE_CHAR_RALT:
495                    case SCANCODE_CHAR_LWIN:
496                    case SCANCODE_CHAR_RWIN:
497                    case SCANCODE_CHAR_NUMLOCK:
498                          return True;                          return True;
                         break;  
499                  default:                  default:
500                          break;                          break;
501          }          }
# Line 314  inhibit_key(KeySym keysym) Line 503  inhibit_key(KeySym keysym)
503  }  }
504    
505  void  void
506    save_remote_modifiers(uint8 scancode)
507    {
508            if (is_modifier(scancode))
509                    return;
510    
511            saved_remote_modifier_state = remote_modifier_state;
512    }
513    
514    void
515    restore_remote_modifiers(uint32 ev_time, uint8 scancode)
516    {
517            key_translation dummy;
518    
519            if (is_modifier(scancode))
520                    return;
521    
522            dummy.scancode = 0;
523            dummy.modifiers = saved_remote_modifier_state;
524            ensure_remote_modifiers(ev_time, dummy);
525    }
526    
527    void
528  ensure_remote_modifiers(uint32 ev_time, key_translation tr)  ensure_remote_modifiers(uint32 ev_time, key_translation tr)
529  {  {
530          /* If this key is a modifier, do nothing */          /* If this key is a modifier, do nothing */
531          switch (tr.scancode)          if (is_modifier(tr.scancode))
532                    return;
533    
534            if (!g_numlock_sync)
535          {          {
536                  case SCANCODE_CHAR_LSHIFT:                  /* NumLock */
537                  case SCANCODE_CHAR_RSHIFT:                  if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask)
538                  case SCANCODE_CHAR_LCTRL:                      != MASK_HAS_BITS(remote_modifier_state, MapNumLockMask))
539                  case SCANCODE_CHAR_RCTRL:                  {
540                  case SCANCODE_CHAR_LALT:                          /* The remote modifier state is not correct */
541                  case SCANCODE_CHAR_RALT:                          uint16 new_remote_state;
542                  case SCANCODE_CHAR_LWIN:  
543                  case SCANCODE_CHAR_RWIN:                          if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask))
544                  case SCANCODE_CHAR_NUMLOCK:                          {
545                          return;                                  DEBUG_KBD(("Remote NumLock state is incorrect, activating NumLock.\n"));
546                  default:                                  new_remote_state = KBD_FLAG_NUMLOCK;
547                          break;                                  remote_modifier_state = MapNumLockMask;
548                            }
549                            else
550                            {
551                                    DEBUG_KBD(("Remote NumLock state is incorrect, deactivating NumLock.\n"));
552                                    new_remote_state = 0;
553                                    remote_modifier_state = 0;
554                            }
555    
556                            rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0, new_remote_state, 0);
557                    }
558          }          }
559    
560          /* Shift */  
561            /* Shift. Left shift and right shift are treated as equal; either is fine. */
562          if (MASK_HAS_BITS(tr.modifiers, MapShiftMask)          if (MASK_HAS_BITS(tr.modifiers, MapShiftMask)
563              != MASK_HAS_BITS(remote_modifier_state, MapShiftMask))              != MASK_HAS_BITS(remote_modifier_state, MapShiftMask))
564          {          {
565                  /* The remote modifier state is not correct */                  /* The remote modifier state is not correct */
566                  if (MASK_HAS_BITS(tr.modifiers, MapShiftMask))                  if (MASK_HAS_BITS(tr.modifiers, MapLeftShiftMask))
567                  {                  {
568                          /* Needs this modifier. Send down. */                          /* Needs left shift. Send down. */
569                          rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_LSHIFT);                          rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_LSHIFT);
570                  }                  }
571                    else if (MASK_HAS_BITS(tr.modifiers, MapRightShiftMask))
572                    {
573                            /* Needs right shift. Send down. */
574                            rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_RSHIFT);
575                    }
576                  else                  else
577                  {                  {
578                          /* Should not use this modifier. Send up. */                          /* Should not use this modifier. Send up for shift currently pressed. */
579                          rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT);                          if (MASK_HAS_BITS(remote_modifier_state, MapLeftShiftMask))
580                                    /* Left shift is down */
581                                    rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT);
582                            else
583                                    /* Right shift is down */
584                                    rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RSHIFT);
585                  }                  }
586          }          }
587    
# Line 367  ensure_remote_modifiers(uint32 ev_time, Line 602  ensure_remote_modifiers(uint32 ev_time,
602                  }                  }
603          }          }
604    
         /* NumLock */  
         if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask)  
             != MASK_HAS_BITS(remote_modifier_state, MapNumLockMask))  
         {  
                 /* The remote modifier state is not correct */  
                 DEBUG_KBD(("Remote NumLock state is incorrect. Toggling\n"));  
                 if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask))  
                 {  
                         /* Needs this modifier. Toggle */  
                         rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_NUMLOCK);  
                         rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_NUMLOCK);  
                 }  
                 else  
                 {  
                         /* Should not use this modifier. Toggle */  
                         rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_NUMLOCK);  
                         rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_NUMLOCK);  
                 }  
         }  
605    
606  }  }
607    
608    
609    unsigned int
610    read_keyboard_state()
611    {
612    #ifdef RDP2VNC
613            return 0;
614    #else
615            unsigned int state;
616            Window wdummy;
617            int dummy;
618    
619            XQueryPointer(g_display, g_wnd, &wdummy, &wdummy, &dummy, &dummy, &dummy, &dummy, &state);
620            return state;
621    #endif
622    }
623    
624    
625    uint16
626    ui_get_numlock_state(unsigned int state)
627    {
628            uint16 numlock_state = 0;
629    
630            if (get_key_state(state, XK_Num_Lock))
631                    numlock_state = KBD_FLAG_NUMLOCK;
632    
633            return numlock_state;
634    }
635    
636    
637    void
638    reset_modifier_keys()
639    {
640            unsigned int state = read_keyboard_state();
641    
642            /* reset keys */
643            uint32 ev_time;
644            ev_time = time(NULL);
645    
646            if (MASK_HAS_BITS(remote_modifier_state, MapLeftShiftMask)
647                && !get_key_state(state, XK_Shift_L))
648                    rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT);
649    
650            if (MASK_HAS_BITS(remote_modifier_state, MapRightShiftMask)
651                && !get_key_state(state, XK_Shift_R))
652                    rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RSHIFT);
653    
654            if (MASK_HAS_BITS(remote_modifier_state, MapLeftCtrlMask)
655                && !get_key_state(state, XK_Control_L))
656                    rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL);
657    
658            if (MASK_HAS_BITS(remote_modifier_state, MapRightCtrlMask)
659                && !get_key_state(state, XK_Control_R))
660                    rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RCTRL);
661    
662            if (MASK_HAS_BITS(remote_modifier_state, MapLeftAltMask) && !get_key_state(state, XK_Alt_L))
663                    rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LALT);
664    
665            if (MASK_HAS_BITS(remote_modifier_state, MapRightAltMask) &&
666                !get_key_state(state, XK_Alt_R) && !get_key_state(state, XK_Mode_switch)
667                && !get_key_state(state, XK_ISO_Level3_Shift))
668                    rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT);
669    
670            reset_winkey(ev_time);
671    
672            if (g_numlock_sync)
673                    rdp_send_input(ev_time, RDP_INPUT_SYNCHRONIZE, 0, ui_get_numlock_state(state), 0);
674    }
675    
676    
677  static void  static void
678  update_modifier_state(uint16 modifiers, BOOL pressed)  update_modifier_state(uint8 scancode, BOOL pressed)
679  {  {
680  #ifdef WITH_DEBUG_KBD  #ifdef WITH_DEBUG_KBD
681          uint16 old_modifier_state;          uint16 old_modifier_state;
# Line 399  update_modifier_state(uint16 modifiers, Line 683  update_modifier_state(uint16 modifiers,
683          old_modifier_state = remote_modifier_state;          old_modifier_state = remote_modifier_state;
684  #endif  #endif
685    
686          switch (modifiers)          switch (scancode)
687          {          {
688                  case SCANCODE_CHAR_LSHIFT:                  case SCANCODE_CHAR_LSHIFT:
689                          MASK_CHANGE_BIT(remote_modifier_state, MapLeftShiftMask, pressed);                          MASK_CHANGE_BIT(remote_modifier_state, MapLeftShiftMask, pressed);
# Line 428  update_modifier_state(uint16 modifiers, Line 712  update_modifier_state(uint16 modifiers,
712                  case SCANCODE_CHAR_NUMLOCK:                  case SCANCODE_CHAR_NUMLOCK:
713                          /* KeyReleases for NumLocks are sent immediately. Toggle the                          /* KeyReleases for NumLocks are sent immediately. Toggle the
714                             modifier state only on Keypress */                             modifier state only on Keypress */
715                          if (pressed)                          if (pressed && !g_numlock_sync)
716                          {                          {
717                                  BOOL newNumLockState;                                  BOOL newNumLockState;
718                                  newNumLockState =                                  newNumLockState =
# Line 437  update_modifier_state(uint16 modifiers, Line 721  update_modifier_state(uint16 modifiers,
721                                  MASK_CHANGE_BIT(remote_modifier_state,                                  MASK_CHANGE_BIT(remote_modifier_state,
722                                                  MapNumLockMask, newNumLockState);                                                  MapNumLockMask, newNumLockState);
723                          }                          }
                         break;  
724          }          }
725    
726  #ifdef WITH_DEBUG_KBD  #ifdef WITH_DEBUG_KBD
# Line 453  update_modifier_state(uint16 modifiers, Line 736  update_modifier_state(uint16 modifiers,
736    
737  /* Send keyboard input */  /* Send keyboard input */
738  void  void
739  rdp_send_scancode(uint32 time, uint16 flags, uint16 scancode)  rdp_send_scancode(uint32 time, uint16 flags, uint8 scancode)
740  {  {
741          update_modifier_state(scancode, !(flags & RDP_KEYRELEASE));          update_modifier_state(scancode, !(flags & RDP_KEYRELEASE));
742    

Legend:
Removed from v.113  
changed lines
  Added in v.810

  ViewVC Help
Powered by ViewVC 1.1.26