23 |
#include <stdio.h> |
#include <stdio.h> |
24 |
#include <stdlib.h> |
#include <stdlib.h> |
25 |
#include <string.h> |
#include <string.h> |
26 |
|
#include <ctype.h> |
27 |
#include <limits.h> |
#include <limits.h> |
28 |
#include "rdesktop.h" |
#include "rdesktop.h" |
29 |
|
#include "scancodes.h" |
30 |
|
|
31 |
#define KEYMAP_SIZE 4096 |
#define KEYMAP_SIZE 4096 |
32 |
#define KEYMAP_MASK (KEYMAP_SIZE - 1) |
#define KEYMAP_MASK (KEYMAP_SIZE - 1) |
33 |
|
#define KEYMAP_MAX_LINE_LENGTH 80 |
34 |
|
|
35 |
|
extern Display *display; |
36 |
extern char keymapname[16]; |
extern char keymapname[16]; |
37 |
extern int keylayout; |
extern int keylayout; |
38 |
|
extern BOOL enable_compose; |
39 |
|
|
40 |
static uint8 keymap[KEYMAP_SIZE]; |
static key_translation keymap[KEYMAP_SIZE]; |
41 |
static unsigned int min_keycode; |
static int min_keycode; |
42 |
|
static uint16 remote_modifier_state = 0; |
43 |
|
|
44 |
static BOOL xkeymap_read(char *mapname) |
static void |
45 |
|
add_to_keymap(char *keyname, uint8 scancode, uint16 modifiers, char *mapname) |
46 |
|
{ |
47 |
|
KeySym keysym; |
48 |
|
|
49 |
|
keysym = XStringToKeysym(keyname); |
50 |
|
if (keysym == NoSymbol) |
51 |
|
{ |
52 |
|
error("Bad keysym %s in keymap %s\n", keyname, mapname); |
53 |
|
return; |
54 |
|
} |
55 |
|
|
56 |
|
DEBUG_KBD("Adding translation, keysym=0x%x, scancode=0x%x, " |
57 |
|
"modifiers=0x%x\n", (unsigned int) keysym, scancode, |
58 |
|
modifiers); |
59 |
|
|
60 |
|
keymap[keysym & KEYMAP_MASK].scancode = scancode; |
61 |
|
keymap[keysym & KEYMAP_MASK].modifiers = modifiers; |
62 |
|
|
63 |
|
return; |
64 |
|
} |
65 |
|
|
66 |
|
|
67 |
|
static BOOL |
68 |
|
xkeymap_read(char *mapname) |
69 |
{ |
{ |
70 |
FILE *fp; |
FILE *fp; |
71 |
char line[PATH_MAX], path[PATH_MAX]; |
char line[KEYMAP_MAX_LINE_LENGTH], path[PATH_MAX]; |
72 |
|
unsigned int line_num = 0; |
73 |
|
unsigned int line_length = 0; |
74 |
char *keyname, *p; |
char *keyname, *p; |
75 |
KeySym keysym; |
char *line_rest; |
76 |
unsigned char keycode; |
uint8 scancode; |
77 |
unsigned int mapcode = 0; |
uint16 modifiers; |
78 |
|
|
79 |
|
|
80 |
strcpy(path, KEYMAP_PATH); |
strcpy(path, KEYMAP_PATH); |
81 |
strncat(path, mapname, sizeof(path) - sizeof(KEYMAP_PATH)); |
strncat(path, mapname, sizeof(path) - sizeof(KEYMAP_PATH)); |
87 |
return False; |
return False; |
88 |
} |
} |
89 |
|
|
90 |
|
/* FIXME: More tolerant on white space */ |
91 |
while (fgets(line, sizeof(line), fp) != NULL) |
while (fgets(line, sizeof(line), fp) != NULL) |
92 |
{ |
{ |
93 |
|
line_num++; |
94 |
|
|
95 |
|
/* Replace the \n with \0 */ |
96 |
p = strchr(line, '\n'); |
p = strchr(line, '\n'); |
97 |
if (p != NULL) |
if (p != NULL) |
98 |
*p = 0; |
*p = 0; |
99 |
|
|
100 |
keycode = strtol(line, &keyname, 16); |
line_length = strlen(line); |
101 |
if ((keycode != 0) && (*keyname == ' ')) |
|
102 |
|
/* Completely empty line */ |
103 |
|
if (strspn(line, " \t\n\r\f\v") == line_length) |
104 |
{ |
{ |
105 |
do |
continue; |
106 |
{ |
} |
107 |
keyname++; |
|
108 |
p = strchr(keyname, ' '); |
/* Include */ |
109 |
if (p != NULL) |
if (strncmp(line, "include ", 8) == 0) |
110 |
*p = 0; |
{ |
111 |
|
if (!xkeymap_read(line + 8)) |
112 |
|
return False; |
113 |
|
continue; |
114 |
|
} |
115 |
|
|
116 |
keysym = XStringToKeysym(keyname); |
/* map */ |
117 |
if (keysym == NoSymbol) |
if (strncmp(line, "map ", 4) == 0) |
118 |
error("Bad keysym %s in keymap %s\n", keyname, mapname); |
{ |
119 |
|
keylayout = strtol(line + 4, NULL, 16); |
120 |
|
DEBUG_KBD("Keylayout 0x%x\n", keylayout); |
121 |
|
continue; |
122 |
|
} |
123 |
|
|
124 |
keymap[keysym & KEYMAP_MASK] = keycode; |
/* compose */ |
125 |
keyname = p; |
if (strncmp(line, "enable_compose", 15) == 0) |
126 |
|
{ |
127 |
|
DEBUG_KBD("Enabling compose handling\n"); |
128 |
|
enable_compose = True; |
129 |
|
continue; |
130 |
|
} |
131 |
|
|
132 |
} while (keyname != NULL); |
/* Comment */ |
133 |
|
if (line[0] == '#') |
134 |
|
{ |
135 |
|
continue; |
136 |
} |
} |
137 |
else if (strncmp(line, "include ", 8) == 0) |
|
138 |
|
/* Normal line */ |
139 |
|
keyname = line; |
140 |
|
p = strchr(line, ' '); |
141 |
|
if (p == NULL) |
142 |
{ |
{ |
143 |
if (!xkeymap_read(line+8)) |
error("Bad line %d in keymap %s\n", line_num, |
144 |
return False; |
mapname); |
145 |
|
continue; |
146 |
} |
} |
147 |
else if (strncmp(line, "map ", 4) == 0) |
else |
148 |
{ |
{ |
149 |
keylayout = strtol(line+4, NULL, 16); |
*p = 0; |
150 |
} |
} |
151 |
else if (line[0] != '#') |
|
152 |
|
/* scancode */ |
153 |
|
p++; |
154 |
|
scancode = strtol(p, &line_rest, 16); |
155 |
|
|
156 |
|
/* flags */ |
157 |
|
/* FIXME: Should allow case-insensitive flag names. |
158 |
|
Fix by using lex+yacc... */ |
159 |
|
modifiers = 0; |
160 |
|
if (strstr(line_rest, "altgr")) |
161 |
{ |
{ |
162 |
error("Malformed line in keymap %s\n", mapname); |
MASK_ADD_BITS(modifiers, MapAltGrMask); |
163 |
|
} |
164 |
|
|
165 |
|
if (strstr(line_rest, "shift")) |
166 |
|
{ |
167 |
|
MASK_ADD_BITS(modifiers, MapLeftShiftMask); |
168 |
|
} |
169 |
|
|
170 |
|
if (strstr(line_rest, "numlock")) |
171 |
|
{ |
172 |
|
MASK_ADD_BITS(modifiers, MapNumLockMask); |
173 |
|
} |
174 |
|
|
175 |
|
if (strstr(line_rest, "localstate")) |
176 |
|
{ |
177 |
|
MASK_ADD_BITS(modifiers, MapLocalStateMask); |
178 |
|
} |
179 |
|
|
180 |
|
add_to_keymap(keyname, scancode, modifiers, mapname); |
181 |
|
|
182 |
|
if (strstr(line_rest, "addupper")) |
183 |
|
{ |
184 |
|
/* Automatically add uppercase key, with same modifiers |
185 |
|
plus shift */ |
186 |
|
for (p = keyname; *p; p++) |
187 |
|
*p = toupper(*p); |
188 |
|
MASK_ADD_BITS(modifiers, MapLeftShiftMask); |
189 |
|
add_to_keymap(keyname, scancode, modifiers, mapname); |
190 |
} |
} |
191 |
} |
} |
192 |
|
|
194 |
return True; |
return True; |
195 |
} |
} |
196 |
|
|
197 |
void xkeymap_init(Display *display) |
|
198 |
|
/* Before connecting and creating UI */ |
199 |
|
void |
200 |
|
xkeymap_init1(void) |
201 |
{ |
{ |
202 |
unsigned int max_keycode; |
int i; |
203 |
|
|
204 |
XDisplayKeycodes(display, &min_keycode, &max_keycode); |
/* Zeroing keymap */ |
205 |
|
for (i = 0; i < KEYMAP_SIZE; i++) |
206 |
|
{ |
207 |
|
keymap[i].scancode = 0; |
208 |
|
keymap[i].modifiers = 0; |
209 |
|
} |
210 |
|
|
211 |
if (strcmp(keymapname, "none")) |
if (strcmp(keymapname, "none")) |
212 |
|
{ |
213 |
xkeymap_read(keymapname); |
xkeymap_read(keymapname); |
214 |
|
} |
215 |
|
|
216 |
} |
} |
217 |
|
|
218 |
uint8 xkeymap_translate_key(KeySym keysym, unsigned int keycode) |
/* After connecting and creating UI */ |
219 |
|
void |
220 |
|
xkeymap_init2(void) |
221 |
{ |
{ |
222 |
uint8 scancode; |
unsigned int max_keycode; |
223 |
|
XDisplayKeycodes(display, &min_keycode, (int *) &max_keycode); |
224 |
|
} |
225 |
|
|
|
scancode = keymap[keysym & KEYMAP_MASK]; |
|
|
if (scancode != 0) |
|
|
return scancode; |
|
226 |
|
|
227 |
/* not in keymap, try to interpret the raw scancode */ |
key_translation |
228 |
|
xkeymap_translate_key(KeySym keysym, unsigned int keycode, unsigned int state) |
229 |
|
{ |
230 |
|
key_translation tr = { 0, 0 }; |
231 |
|
|
232 |
if ((keycode >= min_keycode) && (keycode <= 0x60)) |
tr = keymap[keysym & KEYMAP_MASK]; |
|
return (uint8)(keycode - min_keycode); |
|
233 |
|
|
234 |
switch (keycode) |
if (tr.modifiers & MapLocalStateMask) |
235 |
{ |
{ |
236 |
case 0x61: /* home */ |
/* The modifiers to send for this key should be obtained |
237 |
return 0x47 | 0x80; |
from the local state. Currently, only shift is implemented. */ |
238 |
case 0x62: /* up arrow */ |
if (state & ShiftMask) |
239 |
return 0x48 | 0x80; |
{ |
240 |
case 0x63: /* page up */ |
tr.modifiers = MapLeftShiftMask; |
241 |
return 0x49 | 0x80; |
} |
|
case 0x64: /* left arrow */ |
|
|
return 0x4b | 0x80; |
|
|
case 0x66: /* right arrow */ |
|
|
return 0x4d | 0x80; |
|
|
case 0x67: /* end */ |
|
|
return 0x4f | 0x80; |
|
|
case 0x68: /* down arrow */ |
|
|
return 0x50 | 0x80; |
|
|
case 0x69: /* page down */ |
|
|
return 0x51 | 0x80; |
|
|
case 0x6a: /* insert */ |
|
|
return 0x52 | 0x80; |
|
|
case 0x6b: /* delete */ |
|
|
return 0x53 | 0x80; |
|
|
case 0x6c: /* keypad enter */ |
|
|
return 0x1c | 0x80; |
|
|
case 0x6d: /* right ctrl */ |
|
|
return 0x1d | 0x80; |
|
|
case 0x6f: /* ctrl - print screen */ |
|
|
return 0x37 | 0x80; |
|
|
case 0x70: /* keypad '/' */ |
|
|
return 0x35 | 0x80; |
|
|
case 0x71: /* right alt */ |
|
|
return 0x38 | 0x80; |
|
|
case 0x72: /* ctrl break */ |
|
|
return 0x46 | 0x80; |
|
|
case 0x73: /* left window key */ |
|
|
return 0x5b | 0x80; |
|
|
case 0x74: /* right window key */ |
|
|
return 0x5c | 0x80; |
|
|
case 0x75: /* menu key */ |
|
|
return 0x5d | 0x80; |
|
242 |
} |
} |
243 |
|
|
244 |
return 0; |
if (tr.scancode != 0) |
245 |
|
{ |
246 |
|
DEBUG_KBD |
247 |
|
("Found key translation, scancode=0x%x, modifiers=0x%x\n", |
248 |
|
tr.scancode, tr.modifiers); |
249 |
|
return tr; |
250 |
|
} |
251 |
|
|
252 |
|
printf("No translation for (keysym 0x%lx, %s)\n", keysym, |
253 |
|
get_ksname(keysym)); |
254 |
|
|
255 |
|
/* not in keymap, try to interpret the raw scancode */ |
256 |
|
if ((keycode >= min_keycode) && (keycode <= 0x60)) |
257 |
|
{ |
258 |
|
tr.scancode = keycode - min_keycode; |
259 |
|
printf("Sending guessed scancode 0x%x\n", tr.scancode); |
260 |
|
} |
261 |
|
else |
262 |
|
{ |
263 |
|
printf("No good guess for keycode 0x%x found\n", keycode); |
264 |
|
} |
265 |
|
|
266 |
|
return tr; |
267 |
} |
} |
268 |
|
|
269 |
uint16 xkeymap_translate_button(unsigned int button) |
uint16 |
270 |
|
xkeymap_translate_button(unsigned int button) |
271 |
{ |
{ |
272 |
switch (button) |
switch (button) |
273 |
{ |
{ |
274 |
case Button1: /* left */ |
case Button1: /* left */ |
275 |
return MOUSE_FLAG_BUTTON1; |
return MOUSE_FLAG_BUTTON1; |
276 |
case Button2: /* middle */ |
case Button2: /* middle */ |
277 |
return MOUSE_FLAG_BUTTON3; |
return MOUSE_FLAG_BUTTON3; |
278 |
case Button3: /* right */ |
case Button3: /* right */ |
279 |
return MOUSE_FLAG_BUTTON2; |
return MOUSE_FLAG_BUTTON2; |
280 |
|
case Button4: /* wheel up */ |
281 |
|
return MOUSE_FLAG_BUTTON4; |
282 |
|
case Button5: /* wheel down */ |
283 |
|
return MOUSE_FLAG_BUTTON5; |
284 |
} |
} |
285 |
|
|
286 |
return 0; |
return 0; |
287 |
} |
} |
288 |
|
|
289 |
|
char * |
290 |
|
get_ksname(KeySym keysym) |
291 |
|
{ |
292 |
|
char *ksname = NULL; |
293 |
|
|
294 |
|
if (keysym == NoSymbol) |
295 |
|
ksname = "NoSymbol"; |
296 |
|
else if (!(ksname = XKeysymToString(keysym))) |
297 |
|
ksname = "(no name)"; |
298 |
|
|
299 |
|
return ksname; |
300 |
|
} |
301 |
|
|
302 |
|
BOOL |
303 |
|
inhibit_key(KeySym keysym) |
304 |
|
{ |
305 |
|
switch (keysym) |
306 |
|
{ |
307 |
|
case XK_Caps_Lock: |
308 |
|
return True; |
309 |
|
break; |
310 |
|
case XK_Multi_key: |
311 |
|
return True; |
312 |
|
break; |
313 |
|
default: |
314 |
|
break; |
315 |
|
} |
316 |
|
return False; |
317 |
|
} |
318 |
|
|
319 |
|
void |
320 |
|
ensure_remote_modifiers(uint32 ev_time, key_translation tr) |
321 |
|
{ |
322 |
|
/* If this key is a modifier, do nothing */ |
323 |
|
switch (tr.scancode) |
324 |
|
{ |
325 |
|
case SCANCODE_CHAR_LSHIFT: |
326 |
|
case SCANCODE_CHAR_RSHIFT: |
327 |
|
case SCANCODE_CHAR_LCTRL: |
328 |
|
case SCANCODE_CHAR_RCTRL: |
329 |
|
case SCANCODE_CHAR_LALT: |
330 |
|
case SCANCODE_CHAR_RALT: |
331 |
|
case SCANCODE_CHAR_LWIN: |
332 |
|
case SCANCODE_CHAR_RWIN: |
333 |
|
case SCANCODE_CHAR_NUMLOCK: |
334 |
|
return; |
335 |
|
default: |
336 |
|
break; |
337 |
|
} |
338 |
|
|
339 |
|
/* Shift */ |
340 |
|
if (MASK_HAS_BITS(tr.modifiers, MapShiftMask) |
341 |
|
!= MASK_HAS_BITS(remote_modifier_state, MapShiftMask)) |
342 |
|
{ |
343 |
|
/* The remote modifier state is not correct */ |
344 |
|
if (MASK_HAS_BITS(tr.modifiers, MapShiftMask)) |
345 |
|
{ |
346 |
|
/* Needs this modifier. Send down. */ |
347 |
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, |
348 |
|
SCANCODE_CHAR_LSHIFT); |
349 |
|
} |
350 |
|
else |
351 |
|
{ |
352 |
|
/* Should not use this modifier. Send up. */ |
353 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, |
354 |
|
SCANCODE_CHAR_LSHIFT); |
355 |
|
} |
356 |
|
} |
357 |
|
|
358 |
|
/* AltGr */ |
359 |
|
if (MASK_HAS_BITS(tr.modifiers, MapAltGrMask) |
360 |
|
!= MASK_HAS_BITS(remote_modifier_state, MapAltGrMask)) |
361 |
|
{ |
362 |
|
/* The remote modifier state is not correct */ |
363 |
|
if (MASK_HAS_BITS(tr.modifiers, MapAltGrMask)) |
364 |
|
{ |
365 |
|
/* Needs this modifier. Send down. */ |
366 |
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, |
367 |
|
SCANCODE_CHAR_RALT); |
368 |
|
} |
369 |
|
else |
370 |
|
{ |
371 |
|
/* Should not use this modifier. Send up. */ |
372 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, |
373 |
|
SCANCODE_CHAR_RALT); |
374 |
|
} |
375 |
|
} |
376 |
|
|
377 |
|
/* NumLock */ |
378 |
|
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask) |
379 |
|
!= MASK_HAS_BITS(remote_modifier_state, MapNumLockMask)) |
380 |
|
{ |
381 |
|
/* The remote modifier state is not correct */ |
382 |
|
DEBUG_KBD("Remote NumLock state is incorrect. Toggling\n"); |
383 |
|
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask)) |
384 |
|
{ |
385 |
|
/* Needs this modifier. Toggle */ |
386 |
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, |
387 |
|
SCANCODE_CHAR_NUMLOCK); |
388 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, |
389 |
|
SCANCODE_CHAR_NUMLOCK); |
390 |
|
} |
391 |
|
else |
392 |
|
{ |
393 |
|
/* Should not use this modifier. Toggle */ |
394 |
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, |
395 |
|
SCANCODE_CHAR_NUMLOCK); |
396 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, |
397 |
|
SCANCODE_CHAR_NUMLOCK); |
398 |
|
} |
399 |
|
} |
400 |
|
|
401 |
|
} |
402 |
|
|
403 |
|
|
404 |
|
static void |
405 |
|
update_modifier_state(uint16 modifiers, BOOL pressed) |
406 |
|
{ |
407 |
|
|
408 |
|
DEBUG_KBD("Before updating modifier_state:0x%x, pressed=0x%x\n", |
409 |
|
remote_modifier_state, pressed); |
410 |
|
switch (modifiers) |
411 |
|
{ |
412 |
|
case SCANCODE_CHAR_LSHIFT: |
413 |
|
MASK_CHANGE_BIT(remote_modifier_state, |
414 |
|
MapLeftShiftMask, pressed); |
415 |
|
break; |
416 |
|
case SCANCODE_CHAR_RSHIFT: |
417 |
|
MASK_CHANGE_BIT(remote_modifier_state, |
418 |
|
MapRightShiftMask, pressed); |
419 |
|
break; |
420 |
|
case SCANCODE_CHAR_LCTRL: |
421 |
|
MASK_CHANGE_BIT(remote_modifier_state, |
422 |
|
MapLeftCtrlMask, pressed); |
423 |
|
break; |
424 |
|
case SCANCODE_CHAR_RCTRL: |
425 |
|
MASK_CHANGE_BIT(remote_modifier_state, |
426 |
|
MapRightCtrlMask, pressed); |
427 |
|
break; |
428 |
|
case SCANCODE_CHAR_LALT: |
429 |
|
MASK_CHANGE_BIT(remote_modifier_state, MapLeftAltMask, |
430 |
|
pressed); |
431 |
|
break; |
432 |
|
case SCANCODE_CHAR_RALT: |
433 |
|
MASK_CHANGE_BIT(remote_modifier_state, |
434 |
|
MapRightAltMask, pressed); |
435 |
|
break; |
436 |
|
case SCANCODE_CHAR_LWIN: |
437 |
|
MASK_CHANGE_BIT(remote_modifier_state, MapLeftWinMask, |
438 |
|
pressed); |
439 |
|
break; |
440 |
|
case SCANCODE_CHAR_RWIN: |
441 |
|
MASK_CHANGE_BIT(remote_modifier_state, |
442 |
|
MapRightWinMask, pressed); |
443 |
|
break; |
444 |
|
case SCANCODE_CHAR_NUMLOCK: |
445 |
|
/* KeyReleases for NumLocks are sent immediately. Toggle the |
446 |
|
modifier state only on Keypress */ |
447 |
|
if (pressed) |
448 |
|
{ |
449 |
|
BOOL newNumLockState; |
450 |
|
newNumLockState = |
451 |
|
(MASK_HAS_BITS |
452 |
|
(remote_modifier_state, |
453 |
|
MapNumLockMask) == False); |
454 |
|
MASK_CHANGE_BIT(remote_modifier_state, |
455 |
|
MapNumLockMask, |
456 |
|
newNumLockState); |
457 |
|
} |
458 |
|
break; |
459 |
|
} |
460 |
|
DEBUG_KBD("After updating modifier_state:0x%x\n", |
461 |
|
remote_modifier_state); |
462 |
|
|
463 |
|
} |
464 |
|
|
465 |
|
/* Send keyboard input */ |
466 |
|
void |
467 |
|
rdp_send_scancode(uint32 time, uint16 flags, uint16 scancode) |
468 |
|
{ |
469 |
|
update_modifier_state(scancode, !(flags & RDP_KEYRELEASE)); |
470 |
|
|
471 |
|
if (scancode & SCANCODE_EXTENDED) |
472 |
|
{ |
473 |
|
DEBUG_KBD("Sending extended scancode=0x%x, flags=0x%x\n", |
474 |
|
scancode & ~SCANCODE_EXTENDED, flags); |
475 |
|
rdp_send_input(time, RDP_INPUT_SCANCODE, flags | KBD_FLAG_EXT, |
476 |
|
scancode & ~SCANCODE_EXTENDED, 0); |
477 |
|
} |
478 |
|
else |
479 |
|
{ |
480 |
|
DEBUG_KBD("Sending scancode=0x%x, flags=0x%x\n", scancode, |
481 |
|
flags); |
482 |
|
rdp_send_input(time, RDP_INPUT_SCANCODE, flags, scancode, 0); |
483 |
|
} |
484 |
|
} |