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 |
Copyright (C) Matthew Chapman 1999-2002 |
5 |
|
|
6 |
This program is free software; you can redistribute it and/or modify |
This program is free software; you can redistribute it and/or modify |
7 |
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 |
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
GNU General Public License for more details. |
GNU General Public License for more details. |
15 |
|
|
16 |
You should have received a copy of the GNU General Public License |
You should have received a copy of the GNU General Public License |
17 |
along with this program; if not, write to the Free Software |
along with this program; if not, write to the Free Software |
18 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
*/ |
*/ |
20 |
|
|
21 |
|
#ifdef RDP2VNC |
22 |
|
#include "vnc/x11stubs.h" |
23 |
|
#else |
24 |
#include <X11/Xlib.h> |
#include <X11/Xlib.h> |
25 |
#include <X11/keysym.h> |
#include <X11/keysym.h> |
26 |
#include <stdio.h> |
#endif |
27 |
#include <stdlib.h> |
|
|
#include <string.h> |
|
28 |
#include <ctype.h> |
#include <ctype.h> |
29 |
#include <limits.h> |
#include <limits.h> |
30 |
|
#include <time.h> |
31 |
#include "rdesktop.h" |
#include "rdesktop.h" |
32 |
#include "scancodes.h" |
#include "scancodes.h" |
33 |
|
|
34 |
#define KEYMAP_SIZE 4096 |
#define KEYMAP_SIZE 0xffff+1 |
35 |
#define KEYMAP_MASK (KEYMAP_SIZE - 1) |
#define KEYMAP_MASK 0xffff |
36 |
#define KEYMAP_MAX_LINE_LENGTH 80 |
#define KEYMAP_MAX_LINE_LENGTH 80 |
37 |
|
|
38 |
extern Display *display; |
extern Display *g_display; |
39 |
extern char keymapname[16]; |
extern char keymapname[16]; |
40 |
extern int keylayout; |
extern int keylayout; |
41 |
|
extern int g_win_button_size; |
42 |
|
extern BOOL g_enable_compose; |
43 |
|
|
44 |
|
static BOOL keymap_loaded; |
45 |
static key_translation keymap[KEYMAP_SIZE]; |
static key_translation keymap[KEYMAP_SIZE]; |
46 |
static unsigned int min_keycode; |
static int min_keycode; |
47 |
static uint16 remote_modifier_state = 0; |
static uint16 remote_modifier_state = 0; |
48 |
|
static uint16 saved_remote_modifier_state = 0; |
49 |
|
|
50 |
|
static void update_modifier_state(uint8 scancode, BOOL pressed); |
51 |
|
|
52 |
static void |
static void |
53 |
add_to_keymap(char *keyname, uint8 scancode, uint16 modifiers, char *mapname) |
add_to_keymap(char *keyname, uint8 scancode, uint16 modifiers, char *mapname) |
57 |
keysym = XStringToKeysym(keyname); |
keysym = XStringToKeysym(keyname); |
58 |
if (keysym == NoSymbol) |
if (keysym == NoSymbol) |
59 |
{ |
{ |
60 |
error("Bad keysym %s in keymap %s\n", keyname, mapname); |
DEBUG_KBD(("Bad keysym \"%s\" in keymap %s (ignoring)\n", keyname, mapname)); |
61 |
return; |
return; |
62 |
} |
} |
63 |
|
|
64 |
DEBUG_KBD("Adding translation, keysym=0x%x, scancode=0x%x, " |
DEBUG_KBD(("Adding translation, keysym=0x%x, scancode=0x%x, " |
65 |
"modifiers=0x%x\n", (unsigned int) keysym, scancode, |
"modifiers=0x%x\n", (unsigned int) keysym, scancode, modifiers)); |
|
modifiers); |
|
66 |
|
|
67 |
keymap[keysym & KEYMAP_MASK].scancode = scancode; |
keymap[keysym & KEYMAP_MASK].scancode = scancode; |
68 |
keymap[keysym & KEYMAP_MASK].modifiers = modifiers; |
keymap[keysym & KEYMAP_MASK].modifiers = modifiers; |
75 |
xkeymap_read(char *mapname) |
xkeymap_read(char *mapname) |
76 |
{ |
{ |
77 |
FILE *fp; |
FILE *fp; |
78 |
char line[KEYMAP_MAX_LINE_LENGTH], path[PATH_MAX]; |
char line[KEYMAP_MAX_LINE_LENGTH]; |
79 |
|
char path[PATH_MAX], inplace_path[PATH_MAX]; |
80 |
unsigned int line_num = 0; |
unsigned int line_num = 0; |
81 |
unsigned int line_length = 0; |
unsigned int line_length = 0; |
82 |
char *keyname, *p; |
char *keyname, *p; |
91 |
fp = fopen(path, "r"); |
fp = fopen(path, "r"); |
92 |
if (fp == NULL) |
if (fp == NULL) |
93 |
{ |
{ |
94 |
error("Failed to open keymap %s\n", path); |
/* in case we are running from the source tree */ |
95 |
return False; |
strcpy(inplace_path, "keymaps/"); |
96 |
|
strncat(inplace_path, mapname, sizeof(inplace_path) - sizeof("keymaps/")); |
97 |
|
|
98 |
|
fp = fopen(inplace_path, "r"); |
99 |
|
if (fp == NULL) |
100 |
|
{ |
101 |
|
error("Failed to open keymap %s\n", path); |
102 |
|
return False; |
103 |
|
} |
104 |
} |
} |
105 |
|
|
106 |
/* FIXME: More tolerant on white space */ |
/* FIXME: More tolerant on white space */ |
133 |
if (strncmp(line, "map ", 4) == 0) |
if (strncmp(line, "map ", 4) == 0) |
134 |
{ |
{ |
135 |
keylayout = strtol(line + 4, NULL, 16); |
keylayout = strtol(line + 4, NULL, 16); |
136 |
DEBUG_KBD("Keylayout 0x%x\n", keylayout); |
DEBUG_KBD(("Keylayout 0x%x\n", keylayout)); |
137 |
|
continue; |
138 |
|
} |
139 |
|
|
140 |
|
/* compose */ |
141 |
|
if (strncmp(line, "enable_compose", 15) == 0) |
142 |
|
{ |
143 |
|
DEBUG_KBD(("Enabling compose handling\n")); |
144 |
|
g_enable_compose = True; |
145 |
continue; |
continue; |
146 |
} |
} |
147 |
|
|
156 |
p = strchr(line, ' '); |
p = strchr(line, ' '); |
157 |
if (p == NULL) |
if (p == NULL) |
158 |
{ |
{ |
159 |
error("Bad line %d in keymap %s\n", line_num, |
error("Bad line %d in keymap %s\n", line_num, mapname); |
|
mapname); |
|
160 |
continue; |
continue; |
161 |
} |
} |
162 |
else |
else |
187 |
MASK_ADD_BITS(modifiers, MapNumLockMask); |
MASK_ADD_BITS(modifiers, MapNumLockMask); |
188 |
} |
} |
189 |
|
|
190 |
|
if (strstr(line_rest, "localstate")) |
191 |
|
{ |
192 |
|
MASK_ADD_BITS(modifiers, MapLocalStateMask); |
193 |
|
} |
194 |
|
|
195 |
|
if (strstr(line_rest, "inhibit")) |
196 |
|
{ |
197 |
|
MASK_ADD_BITS(modifiers, MapInhibitMask); |
198 |
|
} |
199 |
|
|
200 |
add_to_keymap(keyname, scancode, modifiers, mapname); |
add_to_keymap(keyname, scancode, modifiers, mapname); |
201 |
|
|
202 |
if (strstr(line_rest, "addupper")) |
if (strstr(line_rest, "addupper")) |
204 |
/* Automatically add uppercase key, with same modifiers |
/* Automatically add uppercase key, with same modifiers |
205 |
plus shift */ |
plus shift */ |
206 |
for (p = keyname; *p; p++) |
for (p = keyname; *p; p++) |
207 |
*p = toupper(*p); |
*p = toupper((int) *p); |
208 |
MASK_ADD_BITS(modifiers, MapLeftShiftMask); |
MASK_ADD_BITS(modifiers, MapLeftShiftMask); |
209 |
add_to_keymap(keyname, scancode, modifiers, mapname); |
add_to_keymap(keyname, scancode, modifiers, mapname); |
210 |
} |
} |
217 |
|
|
218 |
/* Before connecting and creating UI */ |
/* Before connecting and creating UI */ |
219 |
void |
void |
220 |
xkeymap_init1(void) |
xkeymap_init(void) |
221 |
{ |
{ |
222 |
int i; |
unsigned int max_keycode; |
223 |
|
char *mapname_ptr; |
224 |
|
|
225 |
/* Zeroing keymap */ |
/* Make keymapname lowercase */ |
226 |
for (i = 0; i < KEYMAP_SIZE; i++) |
mapname_ptr = keymapname; |
227 |
|
while (*mapname_ptr) |
228 |
{ |
{ |
229 |
keymap[i].scancode = 0; |
*mapname_ptr = tolower((int) *mapname_ptr); |
230 |
keymap[i].modifiers = 0; |
mapname_ptr++; |
231 |
} |
} |
232 |
|
|
233 |
if (strcmp(keymapname, "none")) |
if (strcmp(keymapname, "none")) |
234 |
{ |
{ |
235 |
xkeymap_read(keymapname); |
if (xkeymap_read(keymapname)) |
236 |
|
keymap_loaded = True; |
237 |
} |
} |
238 |
|
|
239 |
|
XDisplayKeycodes(g_display, &min_keycode, (int *) &max_keycode); |
240 |
} |
} |
241 |
|
|
242 |
/* After connecting and creating UI */ |
/* Handles, for example, multi-scancode keypresses (which is not |
243 |
void |
possible via keymap-files) */ |
244 |
xkeymap_init2(void) |
BOOL |
245 |
|
handle_special_keys(uint32 keysym, unsigned int state, uint32 ev_time, BOOL pressed) |
246 |
{ |
{ |
247 |
unsigned int max_keycode; |
switch (keysym) |
248 |
XDisplayKeycodes(display, &min_keycode, &max_keycode); |
{ |
249 |
|
case XK_Return: |
250 |
|
if ((get_key_state(state, XK_Alt_L) || get_key_state(state, XK_Alt_R)) |
251 |
|
&& (get_key_state(state, XK_Control_L) |
252 |
|
|| get_key_state(state, XK_Control_R))) |
253 |
|
{ |
254 |
|
/* Ctrl-Alt-Enter: toggle full screen */ |
255 |
|
if (pressed) |
256 |
|
xwin_toggle_fullscreen(); |
257 |
|
return True; |
258 |
|
} |
259 |
|
break; |
260 |
|
|
261 |
|
case XK_Break: |
262 |
|
/* Send Break sequence E0 46 E0 C6 */ |
263 |
|
if (pressed) |
264 |
|
{ |
265 |
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, |
266 |
|
(SCANCODE_EXTENDED | 0x46)); |
267 |
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, |
268 |
|
(SCANCODE_EXTENDED | 0xc6)); |
269 |
|
} |
270 |
|
/* No release sequence */ |
271 |
|
return True; |
272 |
|
|
273 |
|
case XK_Pause: |
274 |
|
/* According to MS Keyboard Scan Code |
275 |
|
Specification, pressing Pause should result |
276 |
|
in E1 1D 45 E1 9D C5. I'm not exactly sure |
277 |
|
of how this is supposed to be sent via |
278 |
|
RDP. The code below seems to work, but with |
279 |
|
the side effect that Left Ctrl stays |
280 |
|
down. Therefore, we release it when Pause |
281 |
|
is released. */ |
282 |
|
if (pressed) |
283 |
|
{ |
284 |
|
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0xe1, 0); |
285 |
|
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0x1d, 0); |
286 |
|
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0x45, 0); |
287 |
|
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0xe1, 0); |
288 |
|
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0x9d, 0); |
289 |
|
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0xc5, 0); |
290 |
|
} |
291 |
|
else |
292 |
|
{ |
293 |
|
/* Release Left Ctrl */ |
294 |
|
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYRELEASE, |
295 |
|
0x1d, 0); |
296 |
|
} |
297 |
|
return True; |
298 |
|
|
299 |
|
case XK_Meta_L: /* Windows keys */ |
300 |
|
case XK_Super_L: |
301 |
|
case XK_Hyper_L: |
302 |
|
case XK_Meta_R: |
303 |
|
case XK_Super_R: |
304 |
|
case XK_Hyper_R: |
305 |
|
if (pressed) |
306 |
|
{ |
307 |
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_LCTRL); |
308 |
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_ESC); |
309 |
|
} |
310 |
|
else |
311 |
|
{ |
312 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_ESC); |
313 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL); |
314 |
|
} |
315 |
|
return True; |
316 |
|
|
317 |
|
case XK_space: |
318 |
|
/* Prevent access to the Windows system menu in single app mode */ |
319 |
|
if (g_win_button_size |
320 |
|
&& (get_key_state(state, XK_Alt_L) || get_key_state(state, XK_Alt_R))) |
321 |
|
return True; |
322 |
|
|
323 |
|
} |
324 |
|
return False; |
325 |
} |
} |
326 |
|
|
327 |
|
|
328 |
key_translation |
key_translation |
329 |
xkeymap_translate_key(KeySym keysym, unsigned int keycode) |
xkeymap_translate_key(uint32 keysym, unsigned int keycode, unsigned int state) |
330 |
{ |
{ |
331 |
key_translation tr = { 0, 0 }; |
key_translation tr = { 0, 0 }; |
332 |
|
|
333 |
tr = keymap[keysym & KEYMAP_MASK]; |
tr = keymap[keysym & KEYMAP_MASK]; |
334 |
|
|
335 |
|
if (tr.modifiers & MapInhibitMask) |
336 |
|
{ |
337 |
|
DEBUG_KBD(("Inhibiting key\n")); |
338 |
|
tr.scancode = 0; |
339 |
|
return tr; |
340 |
|
} |
341 |
|
|
342 |
|
if (tr.modifiers & MapLocalStateMask) |
343 |
|
{ |
344 |
|
/* The modifiers to send for this key should be obtained |
345 |
|
from the local state. Currently, only shift is implemented. */ |
346 |
|
if (state & ShiftMask) |
347 |
|
{ |
348 |
|
tr.modifiers = MapLeftShiftMask; |
349 |
|
} |
350 |
|
} |
351 |
|
|
352 |
if (tr.scancode != 0) |
if (tr.scancode != 0) |
353 |
{ |
{ |
354 |
DEBUG_KBD |
DEBUG_KBD(("Found key translation, scancode=0x%x, modifiers=0x%x\n", |
355 |
("Found key translation, scancode=0x%x, modifiers=0x%x\n", |
tr.scancode, tr.modifiers)); |
|
tr.scancode, tr.modifiers); |
|
356 |
return tr; |
return tr; |
357 |
} |
} |
358 |
|
|
359 |
printf("No translation for (keysym 0x%lx, %s)\n", keysym, |
if (keymap_loaded) |
360 |
get_ksname(keysym)); |
warning("No translation for (keysym 0x%lx, %s)\n", keysym, get_ksname(keysym)); |
361 |
|
|
362 |
/* not in keymap, try to interpret the raw scancode */ |
/* not in keymap, try to interpret the raw scancode */ |
363 |
if ((keycode >= min_keycode) && (keycode <= 0x60)) |
if (((int) keycode >= min_keycode) && (keycode <= 0x60)) |
364 |
{ |
{ |
365 |
tr.scancode = keycode - min_keycode; |
tr.scancode = keycode - min_keycode; |
366 |
printf("Sending guessed scancode 0x%x\n", tr.scancode); |
|
367 |
|
/* The modifiers to send for this key should be |
368 |
|
obtained from the local state. Currently, only |
369 |
|
shift is implemented. */ |
370 |
|
if (state & ShiftMask) |
371 |
|
{ |
372 |
|
tr.modifiers = MapLeftShiftMask; |
373 |
|
} |
374 |
|
|
375 |
|
DEBUG_KBD(("Sending guessed scancode 0x%x\n", tr.scancode)); |
376 |
} |
} |
377 |
else |
else |
378 |
{ |
{ |
379 |
printf("No good guess for keycode 0x%x found\n", keycode); |
DEBUG_KBD(("No good guess for keycode 0x%x found\n", keycode)); |
380 |
} |
} |
381 |
|
|
382 |
return tr; |
return tr; |
393 |
return MOUSE_FLAG_BUTTON3; |
return MOUSE_FLAG_BUTTON3; |
394 |
case Button3: /* right */ |
case Button3: /* right */ |
395 |
return MOUSE_FLAG_BUTTON2; |
return MOUSE_FLAG_BUTTON2; |
396 |
|
case Button4: /* wheel up */ |
397 |
|
return MOUSE_FLAG_BUTTON4; |
398 |
|
case Button5: /* wheel down */ |
399 |
|
return MOUSE_FLAG_BUTTON5; |
400 |
} |
} |
401 |
|
|
402 |
return 0; |
return 0; |
403 |
} |
} |
404 |
|
|
405 |
char * |
char * |
406 |
get_ksname(KeySym keysym) |
get_ksname(uint32 keysym) |
407 |
{ |
{ |
408 |
char *ksname = NULL; |
char *ksname = NULL; |
409 |
|
|
415 |
return ksname; |
return ksname; |
416 |
} |
} |
417 |
|
|
418 |
BOOL |
void |
419 |
inhibit_key(KeySym keysym) |
save_remote_modifiers() |
420 |
{ |
{ |
421 |
switch (keysym) |
saved_remote_modifier_state = remote_modifier_state; |
422 |
{ |
} |
423 |
case XK_Caps_Lock: |
|
424 |
return True; |
void |
425 |
break; |
restore_remote_modifiers(uint32 ev_time) |
426 |
case XK_Multi_key: |
{ |
427 |
return True; |
key_translation dummy; |
428 |
break; |
|
429 |
default: |
dummy.scancode = 0; |
430 |
break; |
dummy.modifiers = saved_remote_modifier_state; |
431 |
} |
ensure_remote_modifiers(ev_time, dummy); |
|
return False; |
|
432 |
} |
} |
433 |
|
|
434 |
void |
void |
451 |
break; |
break; |
452 |
} |
} |
453 |
|
|
454 |
/* Shift */ |
/* NumLock */ |
455 |
if (MASK_HAS_BITS(tr.modifiers, MapShiftMask) |
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask) |
456 |
!= MASK_HAS_BITS(remote_modifier_state, MapShiftMask)) |
!= MASK_HAS_BITS(remote_modifier_state, MapNumLockMask)) |
457 |
{ |
{ |
458 |
/* The remote modifier state is not correct */ |
/* The remote modifier state is not correct */ |
459 |
if (MASK_HAS_BITS(tr.modifiers, MapShiftMask)) |
uint16 new_remote_state; |
460 |
|
|
461 |
|
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask)) |
462 |
{ |
{ |
463 |
/* Needs this modifier. Send down. */ |
DEBUG_KBD(("Remote NumLock state is incorrect, activating NumLock.\n")); |
464 |
rdp_send_scancode(ev_time, RDP_KEYPRESS, |
new_remote_state = KBD_FLAG_NUMLOCK; |
465 |
SCANCODE_CHAR_LSHIFT); |
remote_modifier_state = MapNumLockMask; |
466 |
} |
} |
467 |
else |
else |
468 |
{ |
{ |
469 |
/* Should not use this modifier. Send up. */ |
DEBUG_KBD(("Remote NumLock state is incorrect, deactivating NumLock.\n")); |
470 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, |
new_remote_state = 0; |
471 |
SCANCODE_CHAR_LSHIFT); |
remote_modifier_state = 0; |
472 |
} |
} |
473 |
|
|
474 |
|
rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0, new_remote_state, 0); |
475 |
} |
} |
476 |
|
|
477 |
/* AltGr */ |
/* Shift. Left shift and right shift are treated as equal; either is fine. */ |
478 |
if (MASK_HAS_BITS(tr.modifiers, MapAltGrMask) |
if (MASK_HAS_BITS(tr.modifiers, MapShiftMask) |
479 |
!= MASK_HAS_BITS(remote_modifier_state, MapAltGrMask)) |
!= MASK_HAS_BITS(remote_modifier_state, MapShiftMask)) |
480 |
{ |
{ |
481 |
/* The remote modifier state is not correct */ |
/* The remote modifier state is not correct */ |
482 |
if (MASK_HAS_BITS(tr.modifiers, MapAltGrMask)) |
if (MASK_HAS_BITS(tr.modifiers, MapLeftShiftMask)) |
483 |
{ |
{ |
484 |
/* Needs this modifier. Send down. */ |
/* Needs left shift. Send down. */ |
485 |
rdp_send_scancode(ev_time, RDP_KEYPRESS, |
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_LSHIFT); |
486 |
SCANCODE_CHAR_RALT); |
} |
487 |
|
else if (MASK_HAS_BITS(tr.modifiers, MapRightShiftMask)) |
488 |
|
{ |
489 |
|
/* Needs right shift. Send down. */ |
490 |
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_RSHIFT); |
491 |
} |
} |
492 |
else |
else |
493 |
{ |
{ |
494 |
/* Should not use this modifier. Send up. */ |
/* Should not use this modifier. Send up for shift currently pressed. */ |
495 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, |
if (MASK_HAS_BITS(remote_modifier_state, MapLeftShiftMask)) |
496 |
SCANCODE_CHAR_RALT); |
/* Left shift is down */ |
497 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT); |
498 |
|
else |
499 |
|
/* Right shift is down */ |
500 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RSHIFT); |
501 |
} |
} |
502 |
} |
} |
503 |
|
|
504 |
/* NumLock */ |
/* AltGr */ |
505 |
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask) |
if (MASK_HAS_BITS(tr.modifiers, MapAltGrMask) |
506 |
!= MASK_HAS_BITS(remote_modifier_state, MapNumLockMask)) |
!= MASK_HAS_BITS(remote_modifier_state, MapAltGrMask)) |
507 |
{ |
{ |
508 |
/* The remote modifier state is not correct */ |
/* The remote modifier state is not correct */ |
509 |
DEBUG_KBD("Remote NumLock state is incorrect. Toggling\n"); |
if (MASK_HAS_BITS(tr.modifiers, MapAltGrMask)) |
|
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask)) |
|
510 |
{ |
{ |
511 |
/* Needs this modifier. Toggle */ |
/* Needs this modifier. Send down. */ |
512 |
rdp_send_scancode(ev_time, RDP_KEYPRESS, |
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_RALT); |
|
SCANCODE_CHAR_NUMLOCK); |
|
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, |
|
|
SCANCODE_CHAR_NUMLOCK); |
|
513 |
} |
} |
514 |
else |
else |
515 |
{ |
{ |
516 |
/* Should not use this modifier. Toggle */ |
/* Should not use this modifier. Send up. */ |
517 |
rdp_send_scancode(ev_time, RDP_KEYPRESS, |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT); |
|
SCANCODE_CHAR_NUMLOCK); |
|
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, |
|
|
SCANCODE_CHAR_NUMLOCK); |
|
518 |
} |
} |
519 |
} |
} |
520 |
|
|
521 |
|
|
522 |
|
} |
523 |
|
|
524 |
|
|
525 |
|
void |
526 |
|
reset_modifier_keys(unsigned int state) |
527 |
|
{ |
528 |
|
/* reset keys */ |
529 |
|
uint32 ev_time; |
530 |
|
ev_time = time(NULL); |
531 |
|
|
532 |
|
if (MASK_HAS_BITS(remote_modifier_state, MapLeftShiftMask) |
533 |
|
&& !get_key_state(state, XK_Shift_L)) |
534 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT); |
535 |
|
|
536 |
|
if (MASK_HAS_BITS(remote_modifier_state, MapRightShiftMask) |
537 |
|
&& !get_key_state(state, XK_Shift_R)) |
538 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RSHIFT); |
539 |
|
|
540 |
|
if (MASK_HAS_BITS(remote_modifier_state, MapLeftCtrlMask) |
541 |
|
&& !get_key_state(state, XK_Control_L)) |
542 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL); |
543 |
|
|
544 |
|
if (MASK_HAS_BITS(remote_modifier_state, MapRightCtrlMask) |
545 |
|
&& !get_key_state(state, XK_Control_R)) |
546 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RCTRL); |
547 |
|
|
548 |
|
if (MASK_HAS_BITS(remote_modifier_state, MapLeftAltMask) && !get_key_state(state, XK_Alt_L)) |
549 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LALT); |
550 |
|
|
551 |
|
if (MASK_HAS_BITS(remote_modifier_state, MapRightAltMask) && |
552 |
|
!get_key_state(state, XK_Alt_R) && !get_key_state(state, XK_Mode_switch)) |
553 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT); |
554 |
} |
} |
555 |
|
|
556 |
|
|
557 |
static void |
static void |
558 |
update_modifier_state(uint16 modifiers, BOOL pressed) |
update_modifier_state(uint8 scancode, BOOL pressed) |
559 |
{ |
{ |
560 |
|
#ifdef WITH_DEBUG_KBD |
561 |
|
uint16 old_modifier_state; |
562 |
|
|
563 |
|
old_modifier_state = remote_modifier_state; |
564 |
|
#endif |
565 |
|
|
566 |
DEBUG_KBD("Before updating modifier_state:0x%x, pressed=0x%x\n", |
switch (scancode) |
|
remote_modifier_state, pressed); |
|
|
switch (modifiers) |
|
567 |
{ |
{ |
568 |
case SCANCODE_CHAR_LSHIFT: |
case SCANCODE_CHAR_LSHIFT: |
569 |
MASK_CHANGE_BIT(remote_modifier_state, |
MASK_CHANGE_BIT(remote_modifier_state, MapLeftShiftMask, pressed); |
|
MapLeftShiftMask, pressed); |
|
570 |
break; |
break; |
571 |
case SCANCODE_CHAR_RSHIFT: |
case SCANCODE_CHAR_RSHIFT: |
572 |
MASK_CHANGE_BIT(remote_modifier_state, |
MASK_CHANGE_BIT(remote_modifier_state, MapRightShiftMask, pressed); |
|
MapRightShiftMask, pressed); |
|
573 |
break; |
break; |
574 |
case SCANCODE_CHAR_LCTRL: |
case SCANCODE_CHAR_LCTRL: |
575 |
MASK_CHANGE_BIT(remote_modifier_state, |
MASK_CHANGE_BIT(remote_modifier_state, MapLeftCtrlMask, pressed); |
|
MapLeftCtrlMask, pressed); |
|
576 |
break; |
break; |
577 |
case SCANCODE_CHAR_RCTRL: |
case SCANCODE_CHAR_RCTRL: |
578 |
MASK_CHANGE_BIT(remote_modifier_state, |
MASK_CHANGE_BIT(remote_modifier_state, MapRightCtrlMask, pressed); |
|
MapRightCtrlMask, pressed); |
|
579 |
break; |
break; |
580 |
case SCANCODE_CHAR_LALT: |
case SCANCODE_CHAR_LALT: |
581 |
MASK_CHANGE_BIT(remote_modifier_state, MapLeftAltMask, |
MASK_CHANGE_BIT(remote_modifier_state, MapLeftAltMask, pressed); |
|
pressed); |
|
582 |
break; |
break; |
583 |
case SCANCODE_CHAR_RALT: |
case SCANCODE_CHAR_RALT: |
584 |
MASK_CHANGE_BIT(remote_modifier_state, |
MASK_CHANGE_BIT(remote_modifier_state, MapRightAltMask, pressed); |
|
MapRightAltMask, pressed); |
|
585 |
break; |
break; |
586 |
case SCANCODE_CHAR_LWIN: |
case SCANCODE_CHAR_LWIN: |
587 |
MASK_CHANGE_BIT(remote_modifier_state, MapLeftWinMask, |
MASK_CHANGE_BIT(remote_modifier_state, MapLeftWinMask, pressed); |
|
pressed); |
|
588 |
break; |
break; |
589 |
case SCANCODE_CHAR_RWIN: |
case SCANCODE_CHAR_RWIN: |
590 |
MASK_CHANGE_BIT(remote_modifier_state, |
MASK_CHANGE_BIT(remote_modifier_state, MapRightWinMask, pressed); |
|
MapRightWinMask, pressed); |
|
591 |
break; |
break; |
592 |
case SCANCODE_CHAR_NUMLOCK: |
case SCANCODE_CHAR_NUMLOCK: |
593 |
/* KeyReleases for NumLocks are sent immediately. Toggle the |
/* KeyReleases for NumLocks are sent immediately. Toggle the |
597 |
BOOL newNumLockState; |
BOOL newNumLockState; |
598 |
newNumLockState = |
newNumLockState = |
599 |
(MASK_HAS_BITS |
(MASK_HAS_BITS |
600 |
(remote_modifier_state, |
(remote_modifier_state, MapNumLockMask) == False); |
|
MapNumLockMask) == False); |
|
601 |
MASK_CHANGE_BIT(remote_modifier_state, |
MASK_CHANGE_BIT(remote_modifier_state, |
602 |
MapNumLockMask, |
MapNumLockMask, newNumLockState); |
|
newNumLockState); |
|
603 |
} |
} |
604 |
break; |
break; |
605 |
} |
} |
606 |
DEBUG_KBD("After updating modifier_state:0x%x\n", |
|
607 |
remote_modifier_state); |
#ifdef WITH_DEBUG_KBD |
608 |
|
if (old_modifier_state != remote_modifier_state) |
609 |
|
{ |
610 |
|
DEBUG_KBD(("Before updating modifier_state:0x%x, pressed=0x%x\n", |
611 |
|
old_modifier_state, pressed)); |
612 |
|
DEBUG_KBD(("After updating modifier_state:0x%x\n", remote_modifier_state)); |
613 |
|
} |
614 |
|
#endif |
615 |
|
|
616 |
} |
} |
617 |
|
|
618 |
/* Send keyboard input */ |
/* Send keyboard input */ |
619 |
void |
void |
620 |
rdp_send_scancode(uint32 time, uint16 flags, uint16 scancode) |
rdp_send_scancode(uint32 time, uint16 flags, uint8 scancode) |
621 |
{ |
{ |
622 |
update_modifier_state(scancode, !(flags & RDP_KEYRELEASE)); |
update_modifier_state(scancode, !(flags & RDP_KEYRELEASE)); |
623 |
|
|
624 |
if (scancode & SCANCODE_EXTENDED) |
if (scancode & SCANCODE_EXTENDED) |
625 |
{ |
{ |
626 |
DEBUG_KBD("Sending extended scancode=0x%x, flags=0x%x\n", |
DEBUG_KBD(("Sending extended scancode=0x%x, flags=0x%x\n", |
627 |
scancode & ~SCANCODE_EXTENDED, flags); |
scancode & ~SCANCODE_EXTENDED, flags)); |
628 |
rdp_send_input(time, RDP_INPUT_SCANCODE, flags | KBD_FLAG_EXT, |
rdp_send_input(time, RDP_INPUT_SCANCODE, flags | KBD_FLAG_EXT, |
629 |
scancode & ~SCANCODE_EXTENDED, 0); |
scancode & ~SCANCODE_EXTENDED, 0); |
630 |
} |
} |
631 |
else |
else |
632 |
{ |
{ |
633 |
DEBUG_KBD("Sending scancode=0x%x, flags=0x%x\n", scancode, |
DEBUG_KBD(("Sending scancode=0x%x, flags=0x%x\n", scancode, flags)); |
|
flags); |
|
634 |
rdp_send_input(time, RDP_INPUT_SCANCODE, flags, scancode, 0); |
rdp_send_input(time, RDP_INPUT_SCANCODE, flags, scancode, 0); |
635 |
} |
} |
636 |
} |
} |