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 |
|
|
5 |
Copyright (C) Matthew Chapman 1999-2002 |
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 |
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 |
#define XK_MISCELLANY |
#include <X11/keysym.h> |
28 |
#include <X11/keysymdef.h> |
#endif |
29 |
|
|
30 |
#include <ctype.h> |
#include <ctype.h> |
31 |
#include <limits.h> |
#include <limits.h> |
32 |
#include <time.h> |
#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 char keymapname[16]; |
extern char keymapname[16]; |
42 |
extern int keylayout; |
extern int keylayout; |
43 |
extern BOOL enable_compose; |
extern int g_win_button_size; |
44 |
|
extern BOOL g_enable_compose; |
45 |
|
extern BOOL g_use_rdp5; |
46 |
|
|
47 |
static BOOL keymap_loaded; |
static BOOL keymap_loaded; |
48 |
static key_translation keymap[KEYMAP_SIZE]; |
static key_translation keymap[KEYMAP_SIZE]; |
49 |
static int min_keycode; |
static int min_keycode; |
50 |
static uint16 remote_modifier_state = 0; |
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); |
static void update_modifier_state(uint8 scancode, BOOL pressed); |
54 |
|
|
60 |
keysym = XStringToKeysym(keyname); |
keysym = XStringToKeysym(keyname); |
61 |
if (keysym == NoSymbol) |
if (keysym == NoSymbol) |
62 |
{ |
{ |
63 |
error("Bad keysym %s in keymap %s\n", keyname, mapname); |
DEBUG_KBD(("Bad keysym \"%s\" in keymap %s (ignoring)\n", keyname, mapname)); |
64 |
return; |
return; |
65 |
} |
} |
66 |
|
|
144 |
if (strncmp(line, "enable_compose", 15) == 0) |
if (strncmp(line, "enable_compose", 15) == 0) |
145 |
{ |
{ |
146 |
DEBUG_KBD(("Enabling compose handling\n")); |
DEBUG_KBD(("Enabling compose handling\n")); |
147 |
enable_compose = True; |
g_enable_compose = True; |
148 |
continue; |
continue; |
149 |
} |
} |
150 |
|
|
207 |
/* Automatically add uppercase key, with same modifiers |
/* Automatically add uppercase key, with same modifiers |
208 |
plus shift */ |
plus shift */ |
209 |
for (p = keyname; *p; p++) |
for (p = keyname; *p; p++) |
210 |
*p = toupper(*p); |
*p = toupper((int) *p); |
211 |
MASK_ADD_BITS(modifiers, MapLeftShiftMask); |
MASK_ADD_BITS(modifiers, MapLeftShiftMask); |
212 |
add_to_keymap(keyname, scancode, modifiers, mapname); |
add_to_keymap(keyname, scancode, modifiers, mapname); |
213 |
} |
} |
223 |
xkeymap_init(void) |
xkeymap_init(void) |
224 |
{ |
{ |
225 |
unsigned int max_keycode; |
unsigned int max_keycode; |
226 |
|
char *mapname_ptr; |
227 |
|
|
228 |
|
/* 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 |
{ |
{ |
239 |
keymap_loaded = True; |
keymap_loaded = True; |
240 |
} |
} |
241 |
|
|
242 |
XDisplayKeycodes(display, &min_keycode, (int *) &max_keycode); |
XDisplayKeycodes(g_display, &min_keycode, (int *) &max_keycode); |
243 |
|
} |
244 |
|
|
245 |
|
static void |
246 |
|
send_winkey(uint32 ev_time, BOOL pressed, BOOL leftkey) |
247 |
|
{ |
248 |
|
uint8 winkey; |
249 |
|
|
250 |
|
if (leftkey) |
251 |
|
winkey = SCANCODE_CHAR_LWIN; |
252 |
|
else |
253 |
|
winkey = SCANCODE_CHAR_RWIN; |
254 |
|
|
255 |
|
if (pressed) |
256 |
|
{ |
257 |
|
if (g_use_rdp5) |
258 |
|
{ |
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 |
/* Handles, for example, multi-scancode keypresses (which is not |
287 |
{ |
{ |
288 |
switch (keysym) |
switch (keysym) |
289 |
{ |
{ |
290 |
case XK_Break: |
case XK_Return: |
|
case XK_Pause: |
|
291 |
if ((get_key_state(state, XK_Alt_L) || get_key_state(state, XK_Alt_R)) |
if ((get_key_state(state, XK_Alt_L) || get_key_state(state, XK_Alt_R)) |
292 |
&& (get_key_state(state, XK_Control_L) || get_key_state(state, XK_Control_R))) |
&& (get_key_state(state, XK_Control_L) |
293 |
|
|| get_key_state(state, XK_Control_R))) |
294 |
{ |
{ |
295 |
/* Ctrl-Alt-Break: toggle full screen */ |
/* Ctrl-Alt-Enter: toggle full screen */ |
296 |
if (pressed) |
if (pressed) |
297 |
xwin_toggle_fullscreen(); |
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 |
else if (keysym == XK_Break) |
/* 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 |
/* Send Break sequence E0 46 E0 C6 */ |
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0xe1, 0); |
326 |
if (pressed) |
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_scancode(ev_time, RDP_KEYPRESS, |
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0xe1, 0); |
329 |
(SCANCODE_EXTENDED | 0x46)); |
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0x9d, 0); |
330 |
rdp_send_scancode(ev_time, RDP_KEYPRESS, |
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0xc5, 0); |
|
(SCANCODE_EXTENDED | 0xc6)); |
|
|
} |
|
|
/* No break sequence */ |
|
331 |
} |
} |
332 |
else /* XK_Pause */ |
else |
333 |
{ |
{ |
334 |
/* According to MS Keyboard Scan Code |
/* Release Left Ctrl */ |
335 |
Specification, pressing Pause should result |
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYRELEASE, |
336 |
in E1 1D 45 E1 9D C5. I'm not exactly sure |
0x1d, 0); |
|
of how this is supposed to be sent via |
|
|
RDP. The code below seems to work, but with |
|
|
the side effect that Left Ctrl stays |
|
|
down. Therefore, we release it when Pause |
|
|
is released. */ |
|
|
if (pressed) |
|
|
{ |
|
|
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0xe1, 0); |
|
|
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0x1d, 0); |
|
|
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0x45, 0); |
|
|
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0xe1, 0); |
|
|
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0x9d, 0); |
|
|
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0xc5, 0); |
|
|
} |
|
|
else |
|
|
{ |
|
|
/* Release Left Ctrl */ |
|
|
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYRELEASE, 0x1d, |
|
|
0); |
|
|
} |
|
337 |
} |
} |
338 |
return True; |
return True; |
339 |
|
|
340 |
case XK_Meta_L: /* Windows keys */ |
case XK_Meta_L: /* Windows keys */ |
341 |
case XK_Super_L: |
case XK_Super_L: |
342 |
case XK_Hyper_L: |
case XK_Hyper_L: |
343 |
|
send_winkey(ev_time, pressed, True); |
344 |
|
return True; |
345 |
|
|
346 |
case XK_Meta_R: |
case XK_Meta_R: |
347 |
case XK_Super_R: |
case XK_Super_R: |
348 |
case XK_Hyper_R: |
case XK_Hyper_R: |
349 |
if (pressed) |
send_winkey(ev_time, pressed, False); |
|
{ |
|
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_LCTRL); |
|
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_ESC); |
|
|
} |
|
|
else |
|
|
{ |
|
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_ESC); |
|
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL); |
|
|
} |
|
350 |
return True; |
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 |
|
|
358 |
} |
} |
359 |
return False; |
return False; |
360 |
} |
} |
392 |
} |
} |
393 |
|
|
394 |
if (keymap_loaded) |
if (keymap_loaded) |
395 |
error("No translation for (keysym 0x%lx, %s)\n", keysym, get_ksname(keysym)); |
warning("No translation for (keysym 0x%lx, %s)\n", keysym, get_ksname(keysym)); |
396 |
|
|
397 |
/* not in keymap, try to interpret the raw scancode */ |
/* not in keymap, try to interpret the raw scancode */ |
398 |
if ((keycode >= min_keycode) && (keycode <= 0x60)) |
if (((int) keycode >= min_keycode) && (keycode <= 0x60)) |
399 |
{ |
{ |
400 |
tr.scancode = keycode - min_keycode; |
tr.scancode = keycode - min_keycode; |
401 |
|
|
450 |
return ksname; |
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 |
void |
470 |
ensure_remote_modifiers(uint32 ev_time, key_translation tr) |
ensure_remote_modifiers(uint32 ev_time, key_translation tr) |
486 |
break; |
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. */ |
/* Shift. Left shift and right shift are treated as equal; either is fine. */ |
513 |
if (MASK_HAS_BITS(tr.modifiers, MapShiftMask) |
if (MASK_HAS_BITS(tr.modifiers, MapShiftMask) |
514 |
!= MASK_HAS_BITS(remote_modifier_state, MapShiftMask)) |
!= MASK_HAS_BITS(remote_modifier_state, MapShiftMask)) |
553 |
} |
} |
554 |
} |
} |
555 |
|
|
|
/* NumLock */ |
|
|
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask) |
|
|
!= MASK_HAS_BITS(remote_modifier_state, MapNumLockMask)) |
|
|
{ |
|
|
/* The remote modifier state is not correct */ |
|
|
uint16 new_remote_state = 0; |
|
556 |
|
|
|
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask)) |
|
|
{ |
|
|
DEBUG_KBD(("Remote NumLock state is incorrect, activating NumLock.\n")); |
|
|
new_remote_state |= KBD_FLAG_NUMLOCK; |
|
|
} |
|
|
else |
|
|
{ |
|
|
DEBUG_KBD(("Remote NumLock state is incorrect, deactivating NumLock.\n")); |
|
|
} |
|
|
|
|
|
rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0, new_remote_state, 0); |
|
|
update_modifier_state(SCANCODE_CHAR_NUMLOCK, True); |
|
|
} |
|
557 |
} |
} |
558 |
|
|
559 |
|
|
564 |
uint32 ev_time; |
uint32 ev_time; |
565 |
ev_time = time(NULL); |
ev_time = time(NULL); |
566 |
|
|
567 |
if (MASK_HAS_BITS(remote_modifier_state, MapLeftShiftMask) && !get_key_state(state, XK_Shift_L)) |
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); |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT); |
570 |
|
|
571 |
if (MASK_HAS_BITS(remote_modifier_state, MapRightShiftMask) && !get_key_state(state, XK_Shift_R)) |
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); |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RSHIFT); |
574 |
|
|
575 |
if (MASK_HAS_BITS(remote_modifier_state, MapLeftCtrlMask) && !get_key_state(state, XK_Control_L)) |
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); |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL); |
578 |
|
|
579 |
if (MASK_HAS_BITS(remote_modifier_state, MapRightCtrlMask) && !get_key_state(state, XK_Control_R)) |
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); |
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)) |
if (MASK_HAS_BITS(remote_modifier_state, MapLeftAltMask) && !get_key_state(state, XK_Alt_L)) |