19 |
*/ |
*/ |
20 |
|
|
21 |
#include <X11/Xlib.h> |
#include <X11/Xlib.h> |
22 |
#include <X11/keysym.h> |
#define XK_MISCELLANY |
23 |
#include <stdio.h> |
#include <X11/keysymdef.h> |
|
#include <stdlib.h> |
|
|
#include <string.h> |
|
24 |
#include <ctype.h> |
#include <ctype.h> |
25 |
#include <limits.h> |
#include <limits.h> |
26 |
|
#include <time.h> |
27 |
#include "rdesktop.h" |
#include "rdesktop.h" |
28 |
#include "scancodes.h" |
#include "scancodes.h" |
29 |
|
|
40 |
static int min_keycode; |
static int min_keycode; |
41 |
static uint16 remote_modifier_state = 0; |
static uint16 remote_modifier_state = 0; |
42 |
|
|
43 |
|
static void update_modifier_state(uint16 modifiers, BOOL pressed); |
44 |
|
|
45 |
static void |
static void |
46 |
add_to_keymap(char *keyname, uint8 scancode, uint16 modifiers, char *mapname) |
add_to_keymap(char *keyname, uint8 scancode, uint16 modifiers, char *mapname) |
47 |
{ |
{ |
54 |
return; |
return; |
55 |
} |
} |
56 |
|
|
57 |
DEBUG_KBD("Adding translation, keysym=0x%x, scancode=0x%x, " |
DEBUG_KBD(("Adding translation, keysym=0x%x, scancode=0x%x, " |
58 |
"modifiers=0x%x\n", (unsigned int) keysym, scancode, modifiers); |
"modifiers=0x%x\n", (unsigned int) keysym, scancode, modifiers)); |
59 |
|
|
60 |
keymap[keysym & KEYMAP_MASK].scancode = scancode; |
keymap[keysym & KEYMAP_MASK].scancode = scancode; |
61 |
keymap[keysym & KEYMAP_MASK].modifiers = modifiers; |
keymap[keysym & KEYMAP_MASK].modifiers = modifiers; |
117 |
if (strncmp(line, "map ", 4) == 0) |
if (strncmp(line, "map ", 4) == 0) |
118 |
{ |
{ |
119 |
keylayout = strtol(line + 4, NULL, 16); |
keylayout = strtol(line + 4, NULL, 16); |
120 |
DEBUG_KBD("Keylayout 0x%x\n", keylayout); |
DEBUG_KBD(("Keylayout 0x%x\n", keylayout)); |
121 |
continue; |
continue; |
122 |
} |
} |
123 |
|
|
124 |
/* compose */ |
/* compose */ |
125 |
if (strncmp(line, "enable_compose", 15) == 0) |
if (strncmp(line, "enable_compose", 15) == 0) |
126 |
{ |
{ |
127 |
DEBUG_KBD("Enabling compose handling\n"); |
DEBUG_KBD(("Enabling compose handling\n")); |
128 |
enable_compose = True; |
enable_compose = True; |
129 |
continue; |
continue; |
130 |
} |
} |
176 |
MASK_ADD_BITS(modifiers, MapLocalStateMask); |
MASK_ADD_BITS(modifiers, MapLocalStateMask); |
177 |
} |
} |
178 |
|
|
179 |
|
if (strstr(line_rest, "inhibit")) |
180 |
|
{ |
181 |
|
MASK_ADD_BITS(modifiers, MapInhibitMask); |
182 |
|
} |
183 |
|
|
184 |
add_to_keymap(keyname, scancode, modifiers, mapname); |
add_to_keymap(keyname, scancode, modifiers, mapname); |
185 |
|
|
186 |
if (strstr(line_rest, "addupper")) |
if (strstr(line_rest, "addupper")) |
201 |
|
|
202 |
/* Before connecting and creating UI */ |
/* Before connecting and creating UI */ |
203 |
void |
void |
204 |
xkeymap_init1(void) |
xkeymap_init(void) |
205 |
{ |
{ |
206 |
int i; |
unsigned int max_keycode; |
|
|
|
|
/* Zeroing keymap */ |
|
|
for (i = 0; i < KEYMAP_SIZE; i++) |
|
|
{ |
|
|
keymap[i].scancode = 0; |
|
|
keymap[i].modifiers = 0; |
|
|
} |
|
207 |
|
|
208 |
if (strcmp(keymapname, "none")) |
if (strcmp(keymapname, "none")) |
|
{ |
|
209 |
xkeymap_read(keymapname); |
xkeymap_read(keymapname); |
|
} |
|
210 |
|
|
211 |
|
XDisplayKeycodes(display, &min_keycode, (int *) &max_keycode); |
212 |
} |
} |
213 |
|
|
214 |
/* After connecting and creating UI */ |
/* Handles, for example, multi-scancode keypresses (which is not |
215 |
void |
possible via keymap-files) */ |
216 |
xkeymap_init2(void) |
BOOL |
217 |
|
handle_special_keys(uint32 keysym, uint32 ev_time, BOOL pressed) |
218 |
{ |
{ |
219 |
unsigned int max_keycode; |
switch (keysym) |
220 |
XDisplayKeycodes(display, &min_keycode, (int *) &max_keycode); |
{ |
221 |
|
case XK_Break: /* toggle full screen */ |
222 |
|
if (get_key_state(XK_Alt_L) || get_key_state(XK_Alt_R)) |
223 |
|
{ |
224 |
|
if (pressed) |
225 |
|
xwin_toggle_fullscreen(); |
226 |
|
return True; |
227 |
|
} |
228 |
|
break; |
229 |
|
|
230 |
|
case XK_Pause: |
231 |
|
/* According to MS Keyboard Scan Code |
232 |
|
Specification, pressing Pause should result |
233 |
|
in E1 1D 45 E1 9D C5. I'm not exactly sure |
234 |
|
of how this is supposed to be sent via |
235 |
|
RDP. The code below seems to work, but with |
236 |
|
the side effect that Left Ctrl stays |
237 |
|
down. Therefore, we release it when Pause |
238 |
|
is released. */ |
239 |
|
if (pressed) |
240 |
|
{ |
241 |
|
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0xe1, 0); |
242 |
|
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0x1d, 0); |
243 |
|
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0x45, 0); |
244 |
|
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0xe1, 0); |
245 |
|
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0x9d, 0); |
246 |
|
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0xc5, 0); |
247 |
|
} |
248 |
|
else |
249 |
|
{ |
250 |
|
// Release Left Ctrl |
251 |
|
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYRELEASE, 0x1d, |
252 |
|
0); |
253 |
|
} |
254 |
|
|
255 |
|
return True; |
256 |
|
break; |
257 |
|
|
258 |
|
case XK_Meta_L: /* Windows keys */ |
259 |
|
case XK_Super_L: |
260 |
|
case XK_Hyper_L: |
261 |
|
case XK_Meta_R: |
262 |
|
case XK_Super_R: |
263 |
|
case XK_Hyper_R: |
264 |
|
if (pressed) |
265 |
|
{ |
266 |
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_LCTRL); |
267 |
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_ESC); |
268 |
|
} |
269 |
|
else |
270 |
|
{ |
271 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_ESC); |
272 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL); |
273 |
|
} |
274 |
|
return True; |
275 |
|
break; |
276 |
|
} |
277 |
|
return False; |
278 |
} |
} |
279 |
|
|
280 |
|
|
281 |
key_translation |
key_translation |
282 |
xkeymap_translate_key(KeySym keysym, unsigned int keycode, unsigned int state) |
xkeymap_translate_key(uint32 keysym, unsigned int keycode, unsigned int state) |
283 |
{ |
{ |
284 |
key_translation tr = { 0, 0 }; |
key_translation tr = { 0, 0 }; |
285 |
|
|
286 |
tr = keymap[keysym & KEYMAP_MASK]; |
tr = keymap[keysym & KEYMAP_MASK]; |
287 |
|
|
288 |
|
if (tr.modifiers & MapInhibitMask) |
289 |
|
{ |
290 |
|
DEBUG_KBD(("Inhibiting key\n")); |
291 |
|
tr.scancode = 0; |
292 |
|
return tr; |
293 |
|
} |
294 |
|
|
295 |
if (tr.modifiers & MapLocalStateMask) |
if (tr.modifiers & MapLocalStateMask) |
296 |
{ |
{ |
297 |
/* The modifiers to send for this key should be obtained |
/* The modifiers to send for this key should be obtained |
304 |
|
|
305 |
if (tr.scancode != 0) |
if (tr.scancode != 0) |
306 |
{ |
{ |
307 |
DEBUG_KBD |
DEBUG_KBD(("Found key translation, scancode=0x%x, modifiers=0x%x\n", |
308 |
("Found key translation, scancode=0x%x, modifiers=0x%x\n", |
tr.scancode, tr.modifiers)); |
|
tr.scancode, tr.modifiers); |
|
309 |
return tr; |
return tr; |
310 |
} |
} |
311 |
|
|
312 |
printf("No translation for (keysym 0x%lx, %s)\n", keysym, get_ksname(keysym)); |
DEBUG_KBD(("No translation for (keysym 0x%lx, %s)\n", keysym, get_ksname(keysym))); |
313 |
|
|
314 |
/* not in keymap, try to interpret the raw scancode */ |
/* not in keymap, try to interpret the raw scancode */ |
315 |
if ((keycode >= min_keycode) && (keycode <= 0x60)) |
if ((keycode >= min_keycode) && (keycode <= 0x60)) |
316 |
{ |
{ |
317 |
tr.scancode = keycode - min_keycode; |
tr.scancode = keycode - min_keycode; |
318 |
printf("Sending guessed scancode 0x%x\n", tr.scancode); |
|
319 |
|
/* The modifiers to send for this key should be |
320 |
|
obtained from the local state. Currently, only |
321 |
|
shift is implemented. */ |
322 |
|
if (state & ShiftMask) |
323 |
|
{ |
324 |
|
tr.modifiers = MapLeftShiftMask; |
325 |
|
} |
326 |
|
|
327 |
|
DEBUG_KBD(("Sending guessed scancode 0x%x\n", tr.scancode)); |
328 |
} |
} |
329 |
else |
else |
330 |
{ |
{ |
331 |
printf("No good guess for keycode 0x%x found\n", keycode); |
DEBUG_KBD(("No good guess for keycode 0x%x found\n", keycode)); |
332 |
} |
} |
333 |
|
|
334 |
return tr; |
return tr; |
355 |
} |
} |
356 |
|
|
357 |
char * |
char * |
358 |
get_ksname(KeySym keysym) |
get_ksname(uint32 keysym) |
359 |
{ |
{ |
360 |
char *ksname = NULL; |
char *ksname = NULL; |
361 |
|
|
367 |
return ksname; |
return ksname; |
368 |
} |
} |
369 |
|
|
|
BOOL |
|
|
inhibit_key(KeySym keysym) |
|
|
{ |
|
|
switch (keysym) |
|
|
{ |
|
|
case XK_Caps_Lock: |
|
|
return True; |
|
|
break; |
|
|
case XK_Multi_key: |
|
|
return True; |
|
|
break; |
|
|
default: |
|
|
break; |
|
|
} |
|
|
return False; |
|
|
} |
|
370 |
|
|
371 |
void |
void |
372 |
ensure_remote_modifiers(uint32 ev_time, key_translation tr) |
ensure_remote_modifiers(uint32 ev_time, key_translation tr) |
388 |
break; |
break; |
389 |
} |
} |
390 |
|
|
391 |
/* Shift */ |
/* Shift. Left shift and right shift are treated as equal; either is fine. */ |
392 |
if (MASK_HAS_BITS(tr.modifiers, MapShiftMask) |
if (MASK_HAS_BITS(tr.modifiers, MapShiftMask) |
393 |
!= MASK_HAS_BITS(remote_modifier_state, MapShiftMask)) |
!= MASK_HAS_BITS(remote_modifier_state, MapShiftMask)) |
394 |
{ |
{ |
395 |
/* The remote modifier state is not correct */ |
/* The remote modifier state is not correct */ |
396 |
if (MASK_HAS_BITS(tr.modifiers, MapShiftMask)) |
if (MASK_HAS_BITS(tr.modifiers, MapLeftShiftMask)) |
397 |
{ |
{ |
398 |
/* Needs this modifier. Send down. */ |
/* Needs left shift. Send down. */ |
399 |
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_LSHIFT); |
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_LSHIFT); |
400 |
} |
} |
401 |
|
else if (MASK_HAS_BITS(tr.modifiers, MapRightShiftMask)) |
402 |
|
{ |
403 |
|
/* Needs right shift. Send down. */ |
404 |
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_RSHIFT); |
405 |
|
} |
406 |
else |
else |
407 |
{ |
{ |
408 |
/* Should not use this modifier. Send up. */ |
/* Should not use this modifier. Send up for shift currently pressed. */ |
409 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT); |
if (MASK_HAS_BITS(remote_modifier_state, MapLeftShiftMask)) |
410 |
|
/* Left shift is down */ |
411 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT); |
412 |
|
else |
413 |
|
/* Right shift is down */ |
414 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RSHIFT); |
415 |
} |
} |
416 |
} |
} |
417 |
|
|
437 |
!= MASK_HAS_BITS(remote_modifier_state, MapNumLockMask)) |
!= MASK_HAS_BITS(remote_modifier_state, MapNumLockMask)) |
438 |
{ |
{ |
439 |
/* The remote modifier state is not correct */ |
/* The remote modifier state is not correct */ |
440 |
DEBUG_KBD("Remote NumLock state is incorrect. Toggling\n"); |
uint16 new_remote_state = 0; |
441 |
|
|
442 |
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask)) |
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask)) |
443 |
{ |
{ |
444 |
/* Needs this modifier. Toggle */ |
DEBUG_KBD(("Remote NumLock state is incorrect, activating NumLock.\n")); |
445 |
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_NUMLOCK); |
new_remote_state |= KBD_FLAG_NUMLOCK; |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_NUMLOCK); |
|
446 |
} |
} |
447 |
else |
else |
448 |
{ |
{ |
449 |
/* Should not use this modifier. Toggle */ |
DEBUG_KBD(("Remote NumLock state is incorrect, deactivating NumLock.\n")); |
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_NUMLOCK); |
|
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_NUMLOCK); |
|
450 |
} |
} |
451 |
|
|
452 |
|
rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0, new_remote_state, 0); |
453 |
|
update_modifier_state(SCANCODE_CHAR_NUMLOCK, True); |
454 |
} |
} |
455 |
|
} |
456 |
|
|
457 |
|
|
458 |
|
void |
459 |
|
reset_modifier_keys(void) |
460 |
|
{ |
461 |
|
/* reset keys */ |
462 |
|
uint32 ev_time; |
463 |
|
ev_time = time(NULL); |
464 |
|
|
465 |
|
if (MASK_HAS_BITS(remote_modifier_state, MapLeftShiftMask) && !get_key_state(XK_Shift_L)) |
466 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT); |
467 |
|
|
468 |
|
if (MASK_HAS_BITS(remote_modifier_state, MapRightShiftMask) && !get_key_state(XK_Shift_R)) |
469 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RSHIFT); |
470 |
|
|
471 |
|
if (MASK_HAS_BITS(remote_modifier_state, MapLeftCtrlMask) && !get_key_state(XK_Control_L)) |
472 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL); |
473 |
|
|
474 |
|
if (MASK_HAS_BITS(remote_modifier_state, MapRightCtrlMask) && !get_key_state(XK_Control_R)) |
475 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RCTRL); |
476 |
|
|
477 |
|
if (MASK_HAS_BITS(remote_modifier_state, MapLeftAltMask) && !get_key_state(XK_Alt_L)) |
478 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LALT); |
479 |
|
|
480 |
|
if (MASK_HAS_BITS(remote_modifier_state, MapRightAltMask) && |
481 |
|
!get_key_state(XK_Alt_R) && !get_key_state(XK_Mode_switch)) |
482 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT); |
483 |
} |
} |
484 |
|
|
485 |
|
|
486 |
static void |
static void |
487 |
update_modifier_state(uint16 modifiers, BOOL pressed) |
update_modifier_state(uint16 modifiers, BOOL pressed) |
488 |
{ |
{ |
489 |
|
#ifdef WITH_DEBUG_KBD |
490 |
|
uint16 old_modifier_state; |
491 |
|
|
492 |
|
old_modifier_state = remote_modifier_state; |
493 |
|
#endif |
494 |
|
|
|
DEBUG_KBD("Before updating modifier_state:0x%x, pressed=0x%x\n", |
|
|
remote_modifier_state, pressed); |
|
495 |
switch (modifiers) |
switch (modifiers) |
496 |
{ |
{ |
497 |
case SCANCODE_CHAR_LSHIFT: |
case SCANCODE_CHAR_LSHIFT: |
532 |
} |
} |
533 |
break; |
break; |
534 |
} |
} |
535 |
DEBUG_KBD("After updating modifier_state:0x%x\n", remote_modifier_state); |
|
536 |
|
#ifdef WITH_DEBUG_KBD |
537 |
|
if (old_modifier_state != remote_modifier_state) |
538 |
|
{ |
539 |
|
DEBUG_KBD(("Before updating modifier_state:0x%x, pressed=0x%x\n", |
540 |
|
old_modifier_state, pressed)); |
541 |
|
DEBUG_KBD(("After updating modifier_state:0x%x\n", remote_modifier_state)); |
542 |
|
} |
543 |
|
#endif |
544 |
|
|
545 |
} |
} |
546 |
|
|
552 |
|
|
553 |
if (scancode & SCANCODE_EXTENDED) |
if (scancode & SCANCODE_EXTENDED) |
554 |
{ |
{ |
555 |
DEBUG_KBD("Sending extended scancode=0x%x, flags=0x%x\n", |
DEBUG_KBD(("Sending extended scancode=0x%x, flags=0x%x\n", |
556 |
scancode & ~SCANCODE_EXTENDED, flags); |
scancode & ~SCANCODE_EXTENDED, flags)); |
557 |
rdp_send_input(time, RDP_INPUT_SCANCODE, flags | KBD_FLAG_EXT, |
rdp_send_input(time, RDP_INPUT_SCANCODE, flags | KBD_FLAG_EXT, |
558 |
scancode & ~SCANCODE_EXTENDED, 0); |
scancode & ~SCANCODE_EXTENDED, 0); |
559 |
} |
} |
560 |
else |
else |
561 |
{ |
{ |
562 |
DEBUG_KBD("Sending scancode=0x%x, flags=0x%x\n", scancode, flags); |
DEBUG_KBD(("Sending scancode=0x%x, flags=0x%x\n", scancode, flags)); |
563 |
rdp_send_input(time, RDP_INPUT_SCANCODE, flags, scancode, 0); |
rdp_send_input(time, RDP_INPUT_SCANCODE, flags, scancode, 0); |
564 |
} |
} |
565 |
} |
} |