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 |
{ |
{ |
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 (pressed && (get_key_state(XK_Alt_L) || get_key_state(XK_Alt_R))) |
223 |
|
{ |
224 |
|
xwin_toggle_fullscreen(); |
225 |
|
return True; |
226 |
|
} |
227 |
|
break; |
228 |
|
|
229 |
|
case XK_Meta_L: /* Windows keys */ |
230 |
|
case XK_Super_L: |
231 |
|
case XK_Hyper_L: |
232 |
|
case XK_Meta_R: |
233 |
|
case XK_Super_R: |
234 |
|
case XK_Hyper_R: |
235 |
|
if (pressed) |
236 |
|
{ |
237 |
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_LCTRL); |
238 |
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_ESC); |
239 |
|
} |
240 |
|
else |
241 |
|
{ |
242 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_ESC); |
243 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL); |
244 |
|
} |
245 |
|
return True; |
246 |
|
break; |
247 |
|
} |
248 |
|
return False; |
249 |
} |
} |
250 |
|
|
251 |
|
|
252 |
key_translation |
key_translation |
253 |
xkeymap_translate_key(KeySym keysym, unsigned int keycode, unsigned int state) |
xkeymap_translate_key(uint32 keysym, unsigned int keycode, unsigned int state) |
254 |
{ |
{ |
255 |
key_translation tr = { 0, 0 }; |
key_translation tr = { 0, 0 }; |
256 |
|
|
257 |
tr = keymap[keysym & KEYMAP_MASK]; |
tr = keymap[keysym & KEYMAP_MASK]; |
258 |
|
|
259 |
|
if (tr.modifiers & MapInhibitMask) |
260 |
|
{ |
261 |
|
DEBUG_KBD(("Inhibiting key\n")); |
262 |
|
tr.scancode = 0; |
263 |
|
return tr; |
264 |
|
} |
265 |
|
|
266 |
if (tr.modifiers & MapLocalStateMask) |
if (tr.modifiers & MapLocalStateMask) |
267 |
{ |
{ |
268 |
/* The modifiers to send for this key should be obtained |
/* The modifiers to send for this key should be obtained |
275 |
|
|
276 |
if (tr.scancode != 0) |
if (tr.scancode != 0) |
277 |
{ |
{ |
278 |
DEBUG_KBD |
DEBUG_KBD(("Found key translation, scancode=0x%x, modifiers=0x%x\n", |
|
(("Found key translation, scancode=0x%x, modifiers=0x%x\n", |
|
279 |
tr.scancode, tr.modifiers)); |
tr.scancode, tr.modifiers)); |
280 |
return tr; |
return tr; |
281 |
} |
} |
282 |
|
|
283 |
fprintf(stderr, "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))); |
284 |
|
|
285 |
/* not in keymap, try to interpret the raw scancode */ |
/* not in keymap, try to interpret the raw scancode */ |
286 |
if ((keycode >= min_keycode) && (keycode <= 0x60)) |
if ((keycode >= min_keycode) && (keycode <= 0x60)) |
287 |
{ |
{ |
288 |
tr.scancode = keycode - min_keycode; |
tr.scancode = keycode - min_keycode; |
289 |
fprintf(stderr, "Sending guessed scancode 0x%x\n", tr.scancode); |
|
290 |
|
/* The modifiers to send for this key should be |
291 |
|
obtained from the local state. Currently, only |
292 |
|
shift is implemented. */ |
293 |
|
if (state & ShiftMask) |
294 |
|
{ |
295 |
|
tr.modifiers = MapLeftShiftMask; |
296 |
|
} |
297 |
|
|
298 |
|
DEBUG_KBD(("Sending guessed scancode 0x%x\n", tr.scancode)); |
299 |
} |
} |
300 |
else |
else |
301 |
{ |
{ |
302 |
fprintf(stderr, "No good guess for keycode 0x%x found\n", keycode); |
DEBUG_KBD(("No good guess for keycode 0x%x found\n", keycode)); |
303 |
} |
} |
304 |
|
|
305 |
return tr; |
return tr; |
326 |
} |
} |
327 |
|
|
328 |
char * |
char * |
329 |
get_ksname(KeySym keysym) |
get_ksname(uint32 keysym) |
330 |
{ |
{ |
331 |
char *ksname = NULL; |
char *ksname = NULL; |
332 |
|
|
338 |
return ksname; |
return ksname; |
339 |
} |
} |
340 |
|
|
|
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; |
|
|
} |
|
341 |
|
|
342 |
void |
void |
343 |
ensure_remote_modifiers(uint32 ev_time, key_translation tr) |
ensure_remote_modifiers(uint32 ev_time, key_translation tr) |
359 |
break; |
break; |
360 |
} |
} |
361 |
|
|
362 |
/* Shift */ |
/* Shift. Left shift and right shift are treated as equal; either is fine. */ |
363 |
if (MASK_HAS_BITS(tr.modifiers, MapShiftMask) |
if (MASK_HAS_BITS(tr.modifiers, MapShiftMask) |
364 |
!= MASK_HAS_BITS(remote_modifier_state, MapShiftMask)) |
!= MASK_HAS_BITS(remote_modifier_state, MapShiftMask)) |
365 |
{ |
{ |
366 |
/* The remote modifier state is not correct */ |
/* The remote modifier state is not correct */ |
367 |
if (MASK_HAS_BITS(tr.modifiers, MapShiftMask)) |
if (MASK_HAS_BITS(tr.modifiers, MapLeftShiftMask)) |
368 |
{ |
{ |
369 |
/* Needs this modifier. Send down. */ |
/* Needs left shift. Send down. */ |
370 |
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_LSHIFT); |
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_LSHIFT); |
371 |
} |
} |
372 |
|
else if (MASK_HAS_BITS(tr.modifiers, MapRightShiftMask)) |
373 |
|
{ |
374 |
|
/* Needs right shift. Send down. */ |
375 |
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_RSHIFT); |
376 |
|
} |
377 |
else |
else |
378 |
{ |
{ |
379 |
/* Should not use this modifier. Send up. */ |
/* Should not use this modifier. Send up for shift currently pressed. */ |
380 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT); |
if (MASK_HAS_BITS(remote_modifier_state, MapLeftShiftMask)) |
381 |
|
/* Left shift is down */ |
382 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT); |
383 |
|
else |
384 |
|
/* Right shift is down */ |
385 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RSHIFT); |
386 |
} |
} |
387 |
} |
} |
388 |
|
|
408 |
!= MASK_HAS_BITS(remote_modifier_state, MapNumLockMask)) |
!= MASK_HAS_BITS(remote_modifier_state, MapNumLockMask)) |
409 |
{ |
{ |
410 |
/* The remote modifier state is not correct */ |
/* The remote modifier state is not correct */ |
411 |
DEBUG_KBD(("Remote NumLock state is incorrect. Toggling\n")); |
uint16 new_remote_state = 0; |
412 |
|
|
413 |
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask)) |
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask)) |
414 |
{ |
{ |
415 |
/* Needs this modifier. Toggle */ |
DEBUG_KBD(("Remote NumLock state is incorrect, activating NumLock.\n")); |
416 |
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); |
|
417 |
} |
} |
418 |
else |
else |
419 |
{ |
{ |
420 |
/* 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); |
|
421 |
} |
} |
422 |
|
|
423 |
|
rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0, new_remote_state, 0); |
424 |
|
update_modifier_state(SCANCODE_CHAR_NUMLOCK, True); |
425 |
} |
} |
426 |
|
} |
427 |
|
|
428 |
|
|
429 |
|
void |
430 |
|
reset_modifier_keys(void) |
431 |
|
{ |
432 |
|
/* reset keys */ |
433 |
|
uint32 ev_time; |
434 |
|
ev_time = time(NULL); |
435 |
|
|
436 |
|
if (MASK_HAS_BITS(remote_modifier_state, MapLeftShiftMask) && !get_key_state(XK_Shift_L)) |
437 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT); |
438 |
|
|
439 |
|
if (MASK_HAS_BITS(remote_modifier_state, MapRightShiftMask) && !get_key_state(XK_Shift_R)) |
440 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RSHIFT); |
441 |
|
|
442 |
|
if (MASK_HAS_BITS(remote_modifier_state, MapLeftCtrlMask) && !get_key_state(XK_Control_L)) |
443 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL); |
444 |
|
|
445 |
|
if (MASK_HAS_BITS(remote_modifier_state, MapRightCtrlMask) && !get_key_state(XK_Control_R)) |
446 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RCTRL); |
447 |
|
|
448 |
|
if (MASK_HAS_BITS(remote_modifier_state, MapLeftAltMask) && !get_key_state(XK_Alt_L)) |
449 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LALT); |
450 |
|
|
451 |
|
if (MASK_HAS_BITS(remote_modifier_state, MapRightAltMask) && |
452 |
|
!get_key_state(XK_Alt_R) && !get_key_state(XK_Mode_switch)) |
453 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT); |
454 |
} |
} |
455 |
|
|
456 |
|
|
457 |
static void |
static void |
458 |
update_modifier_state(uint16 modifiers, BOOL pressed) |
update_modifier_state(uint16 modifiers, BOOL pressed) |
459 |
{ |
{ |
460 |
|
#ifdef WITH_DEBUG_KBD |
461 |
|
uint16 old_modifier_state; |
462 |
|
|
463 |
|
old_modifier_state = remote_modifier_state; |
464 |
|
#endif |
465 |
|
|
|
DEBUG_KBD(("Before updating modifier_state:0x%x, pressed=0x%x\n", |
|
|
remote_modifier_state, pressed)); |
|
466 |
switch (modifiers) |
switch (modifiers) |
467 |
{ |
{ |
468 |
case SCANCODE_CHAR_LSHIFT: |
case SCANCODE_CHAR_LSHIFT: |
503 |
} |
} |
504 |
break; |
break; |
505 |
} |
} |
506 |
DEBUG_KBD(("After updating modifier_state:0x%x\n", remote_modifier_state)); |
|
507 |
|
#ifdef WITH_DEBUG_KBD |
508 |
|
if (old_modifier_state != remote_modifier_state) |
509 |
|
{ |
510 |
|
DEBUG_KBD(("Before updating modifier_state:0x%x, pressed=0x%x\n", |
511 |
|
old_modifier_state, pressed)); |
512 |
|
DEBUG_KBD(("After updating modifier_state:0x%x\n", remote_modifier_state)); |
513 |
|
} |
514 |
|
#endif |
515 |
|
|
516 |
} |
} |
517 |
|
|