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