/[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 56 by jsorg71, Fri Jul 12 22:40:32 2002 UTC revision 466 by astrand, Mon Sep 8 08:27:57 2003 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>  
30  #include <string.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"
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
39    
40  extern Display *display;  extern Display *g_display;
41  extern char keymapname[16];  extern char keymapname[16];
42  extern int keylayout;  extern int keylayout;
43    extern int g_win_button_size;
44    extern BOOL g_enable_compose;
45    extern BOOL g_use_rdp5;
46    
47    static BOOL keymap_loaded;
48    static key_translation keymap[KEYMAP_SIZE];
49    static int min_keycode;
50    static uint16 remote_modifier_state = 0;
51    static uint16 saved_remote_modifier_state = 0;
52    
53    static void update_modifier_state(uint8 scancode, BOOL pressed);
54    
55    static void
56    add_to_keymap(char *keyname, uint8 scancode, uint16 modifiers, char *mapname)
57    {
58            KeySym keysym;
59    
60            keysym = XStringToKeysym(keyname);
61            if (keysym == NoSymbol)
62            {
63                    DEBUG_KBD(("Bad keysym \"%s\" in keymap %s (ignoring)\n", keyname, mapname));
64                    return;
65            }
66    
67  static uint8 keymap[KEYMAP_SIZE];          DEBUG_KBD(("Adding translation, keysym=0x%x, scancode=0x%x, "
68  static unsigned int min_keycode;                     "modifiers=0x%x\n", (unsigned int) keysym, scancode, modifiers));
69    
70  static BOOL xkeymap_read(char *mapname)          keymap[keysym & KEYMAP_MASK].scancode = scancode;
71            keymap[keysym & KEYMAP_MASK].modifiers = modifiers;
72    
73            return;
74    }
75    
76    
77    static BOOL
78    xkeymap_read(char *mapname)
79  {  {
80          FILE *fp;          FILE *fp;
81          char line[PATH_MAX], path[PATH_MAX];          char line[KEYMAP_MAX_LINE_LENGTH];
82            char path[PATH_MAX], inplace_path[PATH_MAX];
83            unsigned int line_num = 0;
84            unsigned int line_length = 0;
85          char *keyname, *p;          char *keyname, *p;
86          KeySym keysym;          char *line_rest;
87          unsigned char keycode;          uint8 scancode;
88            uint16 modifiers;
89    
90    
91          strcpy(path, KEYMAP_PATH);          strcpy(path, KEYMAP_PATH);
92          strncat(path, mapname, sizeof(path) - sizeof(KEYMAP_PATH));          strncat(path, mapname, sizeof(path) - sizeof(KEYMAP_PATH));
# Line 50  static BOOL xkeymap_read(char *mapname) Line 94  static BOOL xkeymap_read(char *mapname)
94          fp = fopen(path, "r");          fp = fopen(path, "r");
95          if (fp == NULL)          if (fp == NULL)
96          {          {
97                  error("Failed to open keymap %s\n", path);                  /* in case we are running from the source tree */
98                  return False;                  strcpy(inplace_path, "keymaps/");
99                    strncat(inplace_path, mapname, sizeof(inplace_path) - sizeof("keymaps/"));
100    
101                    fp = fopen(inplace_path, "r");
102                    if (fp == NULL)
103                    {
104                            error("Failed to open keymap %s\n", path);
105                            return False;
106                    }
107          }          }
108    
109            /* FIXME: More tolerant on white space */
110          while (fgets(line, sizeof(line), fp) != NULL)          while (fgets(line, sizeof(line), fp) != NULL)
111          {          {
112                    line_num++;
113    
114                    /* Replace the \n with \0 */
115                  p = strchr(line, '\n');                  p = strchr(line, '\n');
116                  if (p != NULL)                  if (p != NULL)
117                          *p = 0;                          *p = 0;
118    
119                  keycode = strtol(line, &keyname, 16);                  line_length = strlen(line);
120                  if ((keycode != 0) && (*keyname == ' '))  
121                    /* Completely empty line */
122                    if (strspn(line, " \t\n\r\f\v") == line_length)
123                  {                  {
124                          do                          continue;
125                          {                  }
                                 keyname++;  
                                 p = strchr(keyname, ' ');  
                                 if (p != NULL)  
                                         *p = 0;  
126    
127                                  keysym = XStringToKeysym(keyname);                  /* Include */
128                                  if (keysym == NoSymbol)                  if (strncmp(line, "include ", 8) == 0)
129                                          error("Bad keysym %s in keymap %s\n", keyname, mapname);                  {
130                            if (!xkeymap_read(line + 8))
131                                    return False;
132                            continue;
133                    }
134    
135                                  keymap[keysym & KEYMAP_MASK] = keycode;                  /* map */
136                                  keyname = p;                  if (strncmp(line, "map ", 4) == 0)
137                    {
138                            keylayout = strtol(line + 4, NULL, 16);
139                            DEBUG_KBD(("Keylayout 0x%x\n", keylayout));
140                            continue;
141                    }
142    
143                          } while (keyname != NULL);                  /* compose */
144                    if (strncmp(line, "enable_compose", 15) == 0)
145                    {
146                            DEBUG_KBD(("Enabling compose handling\n"));
147                            g_enable_compose = True;
148                            continue;
149                  }                  }
150                  else if (strncmp(line, "include ", 8) == 0)  
151                    /* Comment */
152                    if (line[0] == '#')
153                  {                  {
154                          if (!xkeymap_read(line+8))                          continue;
                                 return False;  
155                  }                  }
156                  else if (strncmp(line, "map ", 4) == 0)  
157                    /* Normal line */
158                    keyname = line;
159                    p = strchr(line, ' ');
160                    if (p == NULL)
161                    {
162                            error("Bad line %d in keymap %s\n", line_num, mapname);
163                            continue;
164                    }
165                    else
166                  {                  {
167                          keylayout = strtol(line+4, NULL, 16);                          *p = 0;
168                  }                  }
169                  else if (line[0] != '#')  
170                    /* scancode */
171                    p++;
172                    scancode = strtol(p, &line_rest, 16);
173    
174                    /* flags */
175                    /* FIXME: Should allow case-insensitive flag names.
176                       Fix by using lex+yacc... */
177                    modifiers = 0;
178                    if (strstr(line_rest, "altgr"))
179                    {
180                            MASK_ADD_BITS(modifiers, MapAltGrMask);
181                    }
182    
183                    if (strstr(line_rest, "shift"))
184                    {
185                            MASK_ADD_BITS(modifiers, MapLeftShiftMask);
186                    }
187    
188                    if (strstr(line_rest, "numlock"))
189                  {                  {
190                          error("Malformed line in keymap %s\n", mapname);                          MASK_ADD_BITS(modifiers, MapNumLockMask);
191                    }
192    
193                    if (strstr(line_rest, "localstate"))
194                    {
195                            MASK_ADD_BITS(modifiers, MapLocalStateMask);
196                    }
197    
198                    if (strstr(line_rest, "inhibit"))
199                    {
200                            MASK_ADD_BITS(modifiers, MapInhibitMask);
201                    }
202    
203                    add_to_keymap(keyname, scancode, modifiers, mapname);
204    
205                    if (strstr(line_rest, "addupper"))
206                    {
207                            /* Automatically add uppercase key, with same modifiers
208                               plus shift */
209                            for (p = keyname; *p; p++)
210                                    *p = toupper((int) *p);
211                            MASK_ADD_BITS(modifiers, MapLeftShiftMask);
212                            add_to_keymap(keyname, scancode, modifiers, mapname);
213                  }                  }
214          }          }
215    
# Line 98  static BOOL xkeymap_read(char *mapname) Line 217  static BOOL xkeymap_read(char *mapname)
217          return True;          return True;
218  }  }
219    
220  void xkeymap_init(void)  
221    /* Before connecting and creating UI */
222    void
223    xkeymap_init(void)
224  {  {
225          unsigned int max_keycode;          unsigned int max_keycode;
226            char *mapname_ptr;
227    
228          XDisplayKeycodes(display, &min_keycode, &max_keycode);          /* Make keymapname lowercase */
229            mapname_ptr = keymapname;
230            while (*mapname_ptr)
231            {
232                    *mapname_ptr = tolower((int) *mapname_ptr);
233                    mapname_ptr++;
234            }
235    
236          if (strcmp(keymapname, "none"))          if (strcmp(keymapname, "none"))
237                  xkeymap_read(keymapname);          {
238                    if (xkeymap_read(keymapname))
239                            keymap_loaded = True;
240            }
241    
242            XDisplayKeycodes(g_display, &min_keycode, (int *) &max_keycode);
243  }  }
244    
245  uint8 xkeymap_translate_key(unsigned int keysym, unsigned int keycode, uint16 *flags)  static void
246    send_winkey(uint32 ev_time, BOOL pressed, BOOL leftkey)
247  {  {
248          uint8 scancode;          uint8 winkey;
249    
250          scancode = keymap[keysym & KEYMAP_MASK];          if (leftkey)
251          if (scancode != 0)                  winkey = SCANCODE_CHAR_LWIN;
252            else
253                    winkey = SCANCODE_CHAR_RWIN;
254    
255            if (pressed)
256          {          {
257                  if (scancode & 0x80)                  if (g_use_rdp5)
258                          *flags |= KBD_FLAG_EXT;                  {
259                            rdp_send_scancode(ev_time, RDP_KEYPRESS, winkey);
260                    }
261                    else
262                    {
263                            /* RDP4 doesn't support winkey. Fake with Ctrl-Esc */
264                            rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_LCTRL);
265                            rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_ESC);
266                    }
267            }
268            else
269            {
270                    /* key released */
271                    if (g_use_rdp5)
272                    {
273                            rdp_send_scancode(ev_time, RDP_KEYRELEASE, winkey);
274                    }
275                    else
276                    {
277                            rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_ESC);
278                            rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL);
279                    }
280            }
281    }
282    
283    /* Handles, for example, multi-scancode keypresses (which is not
284       possible via keymap-files) */
285    BOOL
286    handle_special_keys(uint32 keysym, unsigned int state, uint32 ev_time, BOOL pressed)
287    {
288            switch (keysym)
289            {
290                    case XK_Return:
291                            if ((get_key_state(state, XK_Alt_L) || get_key_state(state, XK_Alt_R))
292                                && (get_key_state(state, XK_Control_L)
293                                    || get_key_state(state, XK_Control_R)))
294                            {
295                                    /* Ctrl-Alt-Enter: toggle full screen */
296                                    if (pressed)
297                                            xwin_toggle_fullscreen();
298                                    return True;
299                            }
300                            break;
301    
302                    case XK_Break:
303                            /* Send Break sequence E0 46 E0 C6 */
304                            if (pressed)
305                            {
306                                    rdp_send_scancode(ev_time, RDP_KEYPRESS,
307                                                      (SCANCODE_EXTENDED | 0x46));
308                                    rdp_send_scancode(ev_time, RDP_KEYPRESS,
309                                                      (SCANCODE_EXTENDED | 0xc6));
310                            }
311                            /* No release sequence */
312                            return True;
313    
314                    case XK_Pause:
315                            /* According to MS Keyboard Scan Code
316                               Specification, pressing Pause should result
317                               in E1 1D 45 E1 9D C5. I'm not exactly sure
318                               of how this is supposed to be sent via
319                               RDP. The code below seems to work, but with
320                               the side effect that Left Ctrl stays
321                               down. Therefore, we release it when Pause
322                               is released. */
323                            if (pressed)
324                            {
325                                    rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0xe1, 0);
326                                    rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0x1d, 0);
327                                    rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0x45, 0);
328                                    rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0xe1, 0);
329                                    rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0x9d, 0);
330                                    rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0xc5, 0);
331                            }
332                            else
333                            {
334                                    /* Release Left Ctrl */
335                                    rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYRELEASE,
336                                                   0x1d, 0);
337                            }
338                            return True;
339    
340                    case XK_Meta_L: /* Windows keys */
341                    case XK_Super_L:
342                    case XK_Hyper_L:
343                            send_winkey(ev_time, pressed, True);
344                            return True;
345    
346                    case XK_Meta_R:
347                    case XK_Super_R:
348                    case XK_Hyper_R:
349                            send_winkey(ev_time, pressed, False);
350                            return True;
351    
352                    case XK_space:
353                            /* Prevent access to the Windows system menu in single app mode */
354                            if (g_win_button_size
355                                && (get_key_state(state, XK_Alt_L) || get_key_state(state, XK_Alt_R)))
356                                    return True;
357    
                 return (scancode & 0x7f);  
358          }          }
359            return False;
360    }
361    
         /* not in keymap, try to interpret the raw scancode */  
362    
363          if ((keycode >= min_keycode) && (keycode <= 0x60))  key_translation
364                  return (uint8)(keycode - min_keycode);  xkeymap_translate_key(uint32 keysym, unsigned int keycode, unsigned int state)
365    {
366            key_translation tr = { 0, 0 };
367    
368          *flags |= KBD_FLAG_EXT;          tr = keymap[keysym & KEYMAP_MASK];
369    
370          switch (keycode)          if (tr.modifiers & MapInhibitMask)
371          {          {
372                  case 0x61:      /* home */                  DEBUG_KBD(("Inhibiting key\n"));
373                          return 0x47;                  tr.scancode = 0;
374                  case 0x62:      /* up arrow */                  return tr;
                         return 0x48;  
                 case 0x63:      /* page up */  
                         return 0x49;  
                 case 0x64:      /* left arrow */  
                         return 0x4b;  
                 case 0x66:      /* right arrow */  
                         return 0x4d;  
                 case 0x67:      /* end */  
                         return 0x4f;  
                 case 0x68:      /* down arrow */  
                         return 0x50;  
                 case 0x69:      /* page down */  
                         return 0x51;  
                 case 0x6a:      /* insert */  
                         return 0x52;  
                 case 0x6b:      /* delete */  
                         return 0x53;  
                 case 0x6c:      /* keypad enter */  
                         return 0x1c;  
                 case 0x6d:      /* right ctrl */  
                         return 0x1d;  
                 case 0x6f:      /* ctrl - print screen */  
                         return 0x37;  
                 case 0x70:      /* keypad '/' */  
                         return 0x35;  
                 case 0x71:      /* right alt */  
                         return 0x38;  
                 case 0x72:      /* ctrl break */  
                         return 0x46;  
                 case 0x73:      /* left window key */  
                         return 0x5b;  
                 case 0x74:      /* right window key */  
                         return 0x5c;  
                 case 0x75:      /* menu key */  
                         return 0x5d;  
375          }          }
376    
377          return 0;          if (tr.modifiers & MapLocalStateMask)
378            {
379                    /* The modifiers to send for this key should be obtained
380                       from the local state. Currently, only shift is implemented. */
381                    if (state & ShiftMask)
382                    {
383                            tr.modifiers = MapLeftShiftMask;
384                    }
385            }
386    
387            if (tr.scancode != 0)
388            {
389                    DEBUG_KBD(("Found key translation, scancode=0x%x, modifiers=0x%x\n",
390                               tr.scancode, tr.modifiers));
391                    return tr;
392            }
393    
394            if (keymap_loaded)
395                    warning("No translation for (keysym 0x%lx, %s)\n", keysym, get_ksname(keysym));
396    
397            /* not in keymap, try to interpret the raw scancode */
398            if (((int) keycode >= min_keycode) && (keycode <= 0x60))
399            {
400                    tr.scancode = keycode - min_keycode;
401    
402                    /* The modifiers to send for this key should be
403                       obtained from the local state. Currently, only
404                       shift is implemented. */
405                    if (state & ShiftMask)
406                    {
407                            tr.modifiers = MapLeftShiftMask;
408                    }
409    
410                    DEBUG_KBD(("Sending guessed scancode 0x%x\n", tr.scancode));
411            }
412            else
413            {
414                    DEBUG_KBD(("No good guess for keycode 0x%x found\n", keycode));
415            }
416    
417            return tr;
418  }  }
419    
420  uint16 xkeymap_translate_button(unsigned int button)  uint16
421    xkeymap_translate_button(unsigned int button)
422  {  {
423          switch (button)          switch (button)
424          {          {
425                  case Button1:   /* left */                  case Button1:   /* left */
426                          return MOUSE_FLAG_BUTTON1;                          return MOUSE_FLAG_BUTTON1;
427                  case Button2:   /* middle */                  case Button2:   /* middle */
428                          return MOUSE_FLAG_BUTTON3;                          return MOUSE_FLAG_BUTTON3;
429                  case Button3:   /* right */                  case Button3:   /* right */
430                          return MOUSE_FLAG_BUTTON2;                          return MOUSE_FLAG_BUTTON2;
431                  case Button4:   /* wheel up */                  case Button4:   /* wheel up */
432                          return MOUSE_FLAG_BUTTON4;                          return MOUSE_FLAG_BUTTON4;
433                  case Button5:   /* wheel down */                  case Button5:   /* wheel down */
434                          return MOUSE_FLAG_BUTTON5;                          return MOUSE_FLAG_BUTTON5;
435          }          }
436    
437          return 0;          return 0;
438  }  }
439    
440    char *
441    get_ksname(uint32 keysym)
442    {
443            char *ksname = NULL;
444    
445            if (keysym == NoSymbol)
446                    ksname = "NoSymbol";
447            else if (!(ksname = XKeysymToString(keysym)))
448                    ksname = "(no name)";
449    
450            return ksname;
451    }
452    
453    void
454    save_remote_modifiers()
455    {
456            saved_remote_modifier_state = remote_modifier_state;
457    }
458    
459    void
460    restore_remote_modifiers(uint32 ev_time)
461    {
462            key_translation dummy;
463    
464            dummy.scancode = 0;
465            dummy.modifiers = saved_remote_modifier_state;
466            ensure_remote_modifiers(ev_time, dummy);
467    }
468    
469    void
470    ensure_remote_modifiers(uint32 ev_time, key_translation tr)
471    {
472            /* If this key is a modifier, do nothing */
473            switch (tr.scancode)
474            {
475                    case SCANCODE_CHAR_LSHIFT:
476                    case SCANCODE_CHAR_RSHIFT:
477                    case SCANCODE_CHAR_LCTRL:
478                    case SCANCODE_CHAR_RCTRL:
479                    case SCANCODE_CHAR_LALT:
480                    case SCANCODE_CHAR_RALT:
481                    case SCANCODE_CHAR_LWIN:
482                    case SCANCODE_CHAR_RWIN:
483                    case SCANCODE_CHAR_NUMLOCK:
484                            return;
485                    default:
486                            break;
487            }
488    
489            /* NumLock */
490            if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask)
491                != MASK_HAS_BITS(remote_modifier_state, MapNumLockMask))
492            {
493                    /* The remote modifier state is not correct */
494                    uint16 new_remote_state;
495    
496                    if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask))
497                    {
498                            DEBUG_KBD(("Remote NumLock state is incorrect, activating NumLock.\n"));
499                            new_remote_state = KBD_FLAG_NUMLOCK;
500                            remote_modifier_state = MapNumLockMask;
501                    }
502                    else
503                    {
504                            DEBUG_KBD(("Remote NumLock state is incorrect, deactivating NumLock.\n"));
505                            new_remote_state = 0;
506                            remote_modifier_state = 0;
507                    }
508    
509                    rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0, new_remote_state, 0);
510            }
511    
512            /* Shift. Left shift and right shift are treated as equal; either is fine. */
513            if (MASK_HAS_BITS(tr.modifiers, MapShiftMask)
514                != MASK_HAS_BITS(remote_modifier_state, MapShiftMask))
515            {
516                    /* The remote modifier state is not correct */
517                    if (MASK_HAS_BITS(tr.modifiers, MapLeftShiftMask))
518                    {
519                            /* Needs left shift. Send down. */
520                            rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_LSHIFT);
521                    }
522                    else if (MASK_HAS_BITS(tr.modifiers, MapRightShiftMask))
523                    {
524                            /* Needs right shift. Send down. */
525                            rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_RSHIFT);
526                    }
527                    else
528                    {
529                            /* Should not use this modifier. Send up for shift currently pressed. */
530                            if (MASK_HAS_BITS(remote_modifier_state, MapLeftShiftMask))
531                                    /* Left shift is down */
532                                    rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT);
533                            else
534                                    /* Right shift is down */
535                                    rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RSHIFT);
536                    }
537            }
538    
539            /* AltGr */
540            if (MASK_HAS_BITS(tr.modifiers, MapAltGrMask)
541                != MASK_HAS_BITS(remote_modifier_state, MapAltGrMask))
542            {
543                    /* The remote modifier state is not correct */
544                    if (MASK_HAS_BITS(tr.modifiers, MapAltGrMask))
545                    {
546                            /* Needs this modifier. Send down. */
547                            rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_RALT);
548                    }
549                    else
550                    {
551                            /* Should not use this modifier. Send up. */
552                            rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT);
553                    }
554            }
555    
556    
557    }
558    
559    
560    void
561    reset_modifier_keys(unsigned int state)
562    {
563            /* reset keys */
564            uint32 ev_time;
565            ev_time = time(NULL);
566    
567            if (MASK_HAS_BITS(remote_modifier_state, MapLeftShiftMask)
568                && !get_key_state(state, XK_Shift_L))
569                    rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT);
570    
571            if (MASK_HAS_BITS(remote_modifier_state, MapRightShiftMask)
572                && !get_key_state(state, XK_Shift_R))
573                    rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RSHIFT);
574    
575            if (MASK_HAS_BITS(remote_modifier_state, MapLeftCtrlMask)
576                && !get_key_state(state, XK_Control_L))
577                    rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL);
578    
579            if (MASK_HAS_BITS(remote_modifier_state, MapRightCtrlMask)
580                && !get_key_state(state, XK_Control_R))
581                    rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RCTRL);
582    
583            if (MASK_HAS_BITS(remote_modifier_state, MapLeftAltMask) && !get_key_state(state, XK_Alt_L))
584                    rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LALT);
585    
586            if (MASK_HAS_BITS(remote_modifier_state, MapRightAltMask) &&
587                !get_key_state(state, XK_Alt_R) && !get_key_state(state, XK_Mode_switch))
588                    rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT);
589    }
590    
591    
592    static void
593    update_modifier_state(uint8 scancode, BOOL pressed)
594    {
595    #ifdef WITH_DEBUG_KBD
596            uint16 old_modifier_state;
597    
598            old_modifier_state = remote_modifier_state;
599    #endif
600    
601            switch (scancode)
602            {
603                    case SCANCODE_CHAR_LSHIFT:
604                            MASK_CHANGE_BIT(remote_modifier_state, MapLeftShiftMask, pressed);
605                            break;
606                    case SCANCODE_CHAR_RSHIFT:
607                            MASK_CHANGE_BIT(remote_modifier_state, MapRightShiftMask, pressed);
608                            break;
609                    case SCANCODE_CHAR_LCTRL:
610                            MASK_CHANGE_BIT(remote_modifier_state, MapLeftCtrlMask, pressed);
611                            break;
612                    case SCANCODE_CHAR_RCTRL:
613                            MASK_CHANGE_BIT(remote_modifier_state, MapRightCtrlMask, pressed);
614                            break;
615                    case SCANCODE_CHAR_LALT:
616                            MASK_CHANGE_BIT(remote_modifier_state, MapLeftAltMask, pressed);
617                            break;
618                    case SCANCODE_CHAR_RALT:
619                            MASK_CHANGE_BIT(remote_modifier_state, MapRightAltMask, pressed);
620                            break;
621                    case SCANCODE_CHAR_LWIN:
622                            MASK_CHANGE_BIT(remote_modifier_state, MapLeftWinMask, pressed);
623                            break;
624                    case SCANCODE_CHAR_RWIN:
625                            MASK_CHANGE_BIT(remote_modifier_state, MapRightWinMask, pressed);
626                            break;
627                    case SCANCODE_CHAR_NUMLOCK:
628                            /* KeyReleases for NumLocks are sent immediately. Toggle the
629                               modifier state only on Keypress */
630                            if (pressed)
631                            {
632                                    BOOL newNumLockState;
633                                    newNumLockState =
634                                            (MASK_HAS_BITS
635                                             (remote_modifier_state, MapNumLockMask) == False);
636                                    MASK_CHANGE_BIT(remote_modifier_state,
637                                                    MapNumLockMask, newNumLockState);
638                            }
639                            break;
640            }
641    
642    #ifdef WITH_DEBUG_KBD
643            if (old_modifier_state != remote_modifier_state)
644            {
645                    DEBUG_KBD(("Before updating modifier_state:0x%x, pressed=0x%x\n",
646                               old_modifier_state, pressed));
647                    DEBUG_KBD(("After updating modifier_state:0x%x\n", remote_modifier_state));
648            }
649    #endif
650    
651    }
652    
653    /* Send keyboard input */
654    void
655    rdp_send_scancode(uint32 time, uint16 flags, uint8 scancode)
656    {
657            update_modifier_state(scancode, !(flags & RDP_KEYRELEASE));
658    
659            if (scancode & SCANCODE_EXTENDED)
660            {
661                    DEBUG_KBD(("Sending extended scancode=0x%x, flags=0x%x\n",
662                               scancode & ~SCANCODE_EXTENDED, flags));
663                    rdp_send_input(time, RDP_INPUT_SCANCODE, flags | KBD_FLAG_EXT,
664                                   scancode & ~SCANCODE_EXTENDED, 0);
665            }
666            else
667            {
668                    DEBUG_KBD(("Sending scancode=0x%x, flags=0x%x\n", scancode, flags));
669                    rdp_send_input(time, RDP_INPUT_SCANCODE, flags, scancode, 0);
670            }
671    }

Legend:
Removed from v.56  
changed lines
  Added in v.466

  ViewVC Help
Powered by ViewVC 1.1.26