47 |
extern BOOL g_numlock_sync; |
extern BOOL g_numlock_sync; |
48 |
|
|
49 |
static BOOL keymap_loaded; |
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; |
static uint16 saved_remote_modifier_state = 0; |
54 |
|
|
55 |
static void update_modifier_state(uint8 scancode, BOOL pressed); |
static void update_modifier_state(uint8 scancode, BOOL pressed); |
56 |
|
|
57 |
|
/* Free key_translation structure, included linked list */ |
58 |
|
void |
59 |
|
free_key_translation(key_translation * ptr) |
60 |
|
{ |
61 |
|
key_translation *next; |
62 |
|
|
63 |
|
while (ptr) |
64 |
|
{ |
65 |
|
next = ptr->next; |
66 |
|
xfree(ptr); |
67 |
|
ptr = next; |
68 |
|
} |
69 |
|
} |
70 |
|
|
71 |
static void |
static void |
72 |
add_to_keymap(char *keyname, uint8 scancode, uint16 modifiers, char *mapname) |
add_to_keymap(char *keyname, uint8 scancode, uint16 modifiers, char *mapname) |
73 |
{ |
{ |
74 |
KeySym keysym; |
KeySym keysym; |
75 |
|
key_translation *tr; |
76 |
|
|
77 |
keysym = XStringToKeysym(keyname); |
keysym = XStringToKeysym(keyname); |
78 |
if (keysym == NoSymbol) |
if (keysym == NoSymbol) |
84 |
DEBUG_KBD(("Adding translation, keysym=0x%x, scancode=0x%x, " |
DEBUG_KBD(("Adding translation, keysym=0x%x, scancode=0x%x, " |
85 |
"modifiers=0x%x\n", (unsigned int) keysym, scancode, modifiers)); |
"modifiers=0x%x\n", (unsigned int) keysym, scancode, modifiers)); |
86 |
|
|
87 |
keymap[keysym & KEYMAP_MASK].scancode = scancode; |
tr = (key_translation *) xmalloc(sizeof(key_translation)); |
88 |
keymap[keysym & KEYMAP_MASK].modifiers = modifiers; |
memset(tr, 0, sizeof(key_translation)); |
89 |
|
tr->scancode = scancode; |
90 |
|
tr->modifiers = modifiers; |
91 |
|
free_key_translation(keymap[keysym & KEYMAP_MASK]); |
92 |
|
keymap[keysym & KEYMAP_MASK] = tr; |
93 |
|
|
94 |
return; |
return; |
95 |
} |
} |
96 |
|
|
97 |
|
static void |
98 |
|
add_sequence(char *rest, char *mapname) |
99 |
|
{ |
100 |
|
KeySym keysym; |
101 |
|
key_translation *tr, **prev_next; |
102 |
|
size_t chars; |
103 |
|
char keyname[KEYMAP_MAX_LINE_LENGTH]; |
104 |
|
|
105 |
|
/* Skip over whitespace after the sequence keyword */ |
106 |
|
chars = strspn(rest, " \t"); |
107 |
|
rest += chars; |
108 |
|
|
109 |
|
/* Fetch the keysym name */ |
110 |
|
chars = strcspn(rest, " \t\0"); |
111 |
|
STRNCPY(keyname, rest, chars + 1); |
112 |
|
rest += chars; |
113 |
|
|
114 |
|
keysym = XStringToKeysym(keyname); |
115 |
|
if (keysym == NoSymbol) |
116 |
|
{ |
117 |
|
DEBUG_KBD(("Bad keysym \"%s\" in keymap %s (ignoring line)\n", keyname, mapname)); |
118 |
|
return; |
119 |
|
} |
120 |
|
|
121 |
|
|
122 |
|
DEBUG_KBD(("Adding sequence for keysym (0x%lx, %s) -> ", keysym, keyname)); |
123 |
|
|
124 |
|
free_key_translation(keymap[keysym & KEYMAP_MASK]); |
125 |
|
prev_next = &keymap[keysym & KEYMAP_MASK]; |
126 |
|
|
127 |
|
while (*rest) |
128 |
|
{ |
129 |
|
/* Skip whitespace */ |
130 |
|
chars = strspn(rest, " \t"); |
131 |
|
rest += chars; |
132 |
|
|
133 |
|
/* Fetch the keysym name */ |
134 |
|
chars = strcspn(rest, " \t\0"); |
135 |
|
STRNCPY(keyname, rest, chars + 1); |
136 |
|
rest += chars; |
137 |
|
|
138 |
|
keysym = XStringToKeysym(keyname); |
139 |
|
if (keysym == NoSymbol) |
140 |
|
{ |
141 |
|
DEBUG_KBD(("Bad keysym \"%s\" in keymap %s (ignoring line)\n", keyname, |
142 |
|
mapname)); |
143 |
|
return; |
144 |
|
} |
145 |
|
|
146 |
|
/* Allocate space for key_translation structure */ |
147 |
|
tr = (key_translation *) xmalloc(sizeof(key_translation)); |
148 |
|
memset(tr, 0, sizeof(key_translation)); |
149 |
|
*prev_next = tr; |
150 |
|
prev_next = &tr->next; |
151 |
|
tr->seq_keysym = keysym; |
152 |
|
|
153 |
|
DEBUG_KBD(("0x%x, ", (unsigned int) keysym)); |
154 |
|
} |
155 |
|
DEBUG_KBD(("\n")); |
156 |
|
} |
157 |
|
|
158 |
static BOOL |
static BOOL |
159 |
xkeymap_read(char *mapname) |
xkeymap_read(char *mapname) |
229 |
continue; |
continue; |
230 |
} |
} |
231 |
|
|
232 |
|
/* sequence */ |
233 |
|
if (strncmp(line, "sequence", 8) == 0) |
234 |
|
{ |
235 |
|
add_sequence(line + 8, mapname); |
236 |
|
continue; |
237 |
|
} |
238 |
|
|
239 |
/* Comment */ |
/* Comment */ |
240 |
if (line[0] == '#') |
if (line[0] == '#') |
241 |
{ |
{ |
379 |
} |
} |
380 |
} |
} |
381 |
|
|
382 |
/* Handles, for example, multi-scancode keypresses (which is not |
/* Handle special key combinations */ |
|
possible via keymap-files) */ |
|
383 |
BOOL |
BOOL |
384 |
handle_special_keys(uint32 keysym, unsigned int state, uint32 ev_time, BOOL pressed) |
handle_special_keys(uint32 keysym, unsigned int state, uint32 ev_time, BOOL pressed) |
385 |
{ |
{ |
457 |
&& (get_key_state(state, XK_Alt_L) || get_key_state(state, XK_Alt_R))) |
&& (get_key_state(state, XK_Alt_L) || get_key_state(state, XK_Alt_R))) |
458 |
return True; |
return True; |
459 |
break; |
break; |
460 |
|
|
461 |
case XK_Num_Lock: |
case XK_Num_Lock: |
462 |
/* FIXME: We might want to do RDP_INPUT_SYNCHRONIZE here, if g_numlock_sync */ |
/* Synchronize on key release */ |
463 |
if (!g_numlock_sync) |
if (g_numlock_sync && !pressed) |
464 |
/* Inhibit */ |
rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0, |
465 |
return True; |
ui_get_numlock_state(read_keyboard_state()), 0); |
466 |
|
|
467 |
|
/* Inhibit */ |
468 |
|
return True; |
469 |
break; |
break; |
470 |
|
|
471 |
} |
} |
476 |
key_translation |
key_translation |
477 |
xkeymap_translate_key(uint32 keysym, unsigned int keycode, unsigned int state) |
xkeymap_translate_key(uint32 keysym, unsigned int keycode, unsigned int state) |
478 |
{ |
{ |
479 |
key_translation tr = { 0, 0 }; |
key_translation tr = { 0, 0, 0, 0 }; |
480 |
|
key_translation *ptr; |
481 |
|
|
482 |
tr = keymap[keysym & KEYMAP_MASK]; |
ptr = keymap[keysym & KEYMAP_MASK]; |
483 |
|
if (ptr) |
|
if (tr.modifiers & MapInhibitMask) |
|
484 |
{ |
{ |
485 |
DEBUG_KBD(("Inhibiting key\n")); |
tr = *ptr; |
486 |
tr.scancode = 0; |
if (tr.seq_keysym == 0) /* Normal scancode translation */ |
|
return tr; |
|
|
} |
|
|
|
|
|
if (tr.modifiers & MapLocalStateMask) |
|
|
{ |
|
|
/* The modifiers to send for this key should be obtained |
|
|
from the local state. Currently, only shift is implemented. */ |
|
|
if (state & ShiftMask) |
|
487 |
{ |
{ |
488 |
tr.modifiers = MapLeftShiftMask; |
if (tr.modifiers & MapInhibitMask) |
489 |
|
{ |
490 |
|
DEBUG_KBD(("Inhibiting key\n")); |
491 |
|
tr.scancode = 0; |
492 |
|
return tr; |
493 |
|
} |
494 |
|
|
495 |
|
if (tr.modifiers & MapLocalStateMask) |
496 |
|
{ |
497 |
|
/* The modifiers to send for this key should be obtained |
498 |
|
from the local state. Currently, only shift is implemented. */ |
499 |
|
if (state & ShiftMask) |
500 |
|
{ |
501 |
|
tr.modifiers = MapLeftShiftMask; |
502 |
|
} |
503 |
|
} |
504 |
|
|
505 |
|
if ((tr.modifiers & MapLeftShiftMask) |
506 |
|
&& ((remote_modifier_state & MapLeftCtrlMask) |
507 |
|
|| (remote_modifier_state & MapRightCtrlMask)) |
508 |
|
&& get_key_state(state, XK_Caps_Lock)) |
509 |
|
{ |
510 |
|
DEBUG_KBD(("CapsLock + Ctrl pressed, releasing LeftShift\n")); |
511 |
|
tr.modifiers ^= MapLeftShiftMask; |
512 |
|
} |
513 |
|
|
514 |
|
DEBUG_KBD(("Found scancode translation, scancode=0x%x, modifiers=0x%x\n", |
515 |
|
tr.scancode, tr.modifiers)); |
516 |
} |
} |
517 |
} |
} |
518 |
|
else |
|
if (((remote_modifier_state & MapLeftCtrlMask) |
|
|
|| (remote_modifier_state & MapRightCtrlMask)) && get_key_state(state, XK_Caps_Lock)) |
|
519 |
{ |
{ |
520 |
DEBUG_KBD(("CapsLock + Ctrl pressed, releasing LeftShift\n")); |
if (keymap_loaded) |
521 |
tr.modifiers ^= MapLeftShiftMask; |
warning("No translation for (keysym 0x%lx, %s)\n", keysym, |
522 |
} |
get_ksname(keysym)); |
523 |
|
|
524 |
|
/* not in keymap, try to interpret the raw scancode */ |
525 |
|
if (((int) keycode >= min_keycode) && (keycode <= 0x60)) |
526 |
|
{ |
527 |
|
tr.scancode = keycode - min_keycode; |
528 |
|
|
529 |
|
/* The modifiers to send for this key should be |
530 |
|
obtained from the local state. Currently, only |
531 |
|
shift is implemented. */ |
532 |
|
if (state & ShiftMask) |
533 |
|
{ |
534 |
|
tr.modifiers = MapLeftShiftMask; |
535 |
|
} |
536 |
|
|
537 |
if (tr.scancode != 0) |
DEBUG_KBD(("Sending guessed scancode 0x%x\n", tr.scancode)); |
538 |
{ |
} |
539 |
DEBUG_KBD(("Found key translation, scancode=0x%x, modifiers=0x%x\n", |
else |
540 |
tr.scancode, tr.modifiers)); |
{ |
541 |
return tr; |
DEBUG_KBD(("No good guess for keycode 0x%x found\n", keycode)); |
542 |
|
} |
543 |
} |
} |
544 |
|
|
545 |
if (keymap_loaded) |
return tr; |
546 |
warning("No translation for (keysym 0x%lx, %s)\n", keysym, get_ksname(keysym)); |
} |
547 |
|
|
548 |
|
void |
549 |
|
xkeymap_send_keys(uint32 keysym, unsigned int keycode, unsigned int state, uint32 ev_time, |
550 |
|
BOOL pressed) |
551 |
|
{ |
552 |
|
key_translation tr, *ptr; |
553 |
|
tr = xkeymap_translate_key(keysym, keycode, state); |
554 |
|
|
555 |
/* not in keymap, try to interpret the raw scancode */ |
if (tr.seq_keysym == 0) |
|
if (((int) keycode >= min_keycode) && (keycode <= 0x60)) |
|
556 |
{ |
{ |
557 |
tr.scancode = keycode - min_keycode; |
/* Scancode translation */ |
558 |
|
if (tr.scancode == 0) |
559 |
|
return; |
560 |
|
|
561 |
/* The modifiers to send for this key should be |
if (pressed) |
|
obtained from the local state. Currently, only |
|
|
shift is implemented. */ |
|
|
if (state & ShiftMask) |
|
562 |
{ |
{ |
563 |
tr.modifiers = MapLeftShiftMask; |
save_remote_modifiers(tr.scancode); |
564 |
|
ensure_remote_modifiers(ev_time, tr); |
565 |
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode); |
566 |
|
restore_remote_modifiers(ev_time, tr.scancode); |
567 |
} |
} |
568 |
|
else |
569 |
DEBUG_KBD(("Sending guessed scancode 0x%x\n", tr.scancode)); |
{ |
570 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, tr.scancode); |
571 |
|
} |
572 |
|
return; |
573 |
} |
} |
574 |
else |
|
575 |
|
/* Sequence, only on key down */ |
576 |
|
if (pressed) |
577 |
{ |
{ |
578 |
DEBUG_KBD(("No good guess for keycode 0x%x found\n", keycode)); |
ptr = &tr; |
579 |
|
do |
580 |
|
{ |
581 |
|
DEBUG_KBD(("Handling sequence element, keysym=0x%x\n", |
582 |
|
(unsigned int) ptr->seq_keysym)); |
583 |
|
xkeymap_send_keys(ptr->seq_keysym, keycode, state, ev_time, True); |
584 |
|
xkeymap_send_keys(ptr->seq_keysym, keycode, state, ev_time, False); |
585 |
|
ptr = ptr->next; |
586 |
|
} |
587 |
|
while (ptr); |
588 |
} |
} |
|
|
|
|
return tr; |
|
589 |
} |
} |
590 |
|
|
591 |
uint16 |
uint16 |