1 |
matthewc |
38 |
/* |
2 |
|
|
rdesktop: A Remote Desktop Protocol client. |
3 |
|
|
User interface services - X keyboard mapping |
4 |
|
|
Copyright (C) Matthew Chapman 1999-2001 |
5 |
|
|
|
6 |
|
|
This program is free software; you can redistribute it and/or modify |
7 |
|
|
it under the terms of the GNU General Public License as published by |
8 |
|
|
the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
(at your option) any later version. |
10 |
|
|
|
11 |
|
|
This program is distributed in the hope that it will be useful, |
12 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
GNU General Public License for more details. |
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License |
17 |
|
|
along with this program; if not, write to the Free Software |
18 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#include <X11/Xlib.h> |
22 |
|
|
#include <X11/keysym.h> |
23 |
|
|
#include <stdio.h> |
24 |
|
|
#include <stdlib.h> |
25 |
|
|
#include <string.h> |
26 |
astrand |
66 |
#include <ctype.h> |
27 |
matthewc |
39 |
#include <limits.h> |
28 |
matthewc |
38 |
#include "rdesktop.h" |
29 |
astrand |
66 |
#include "scancodes.h" |
30 |
matthewc |
38 |
|
31 |
|
|
#define KEYMAP_SIZE 4096 |
32 |
|
|
#define KEYMAP_MASK (KEYMAP_SIZE - 1) |
33 |
astrand |
66 |
#define KEYMAP_MAX_LINE_LENGTH 80 |
34 |
matthewc |
38 |
|
35 |
matthewc |
50 |
extern Display *display; |
36 |
matthewc |
38 |
extern char keymapname[16]; |
37 |
|
|
extern int keylayout; |
38 |
|
|
|
39 |
astrand |
66 |
static key_translation keymap[KEYMAP_SIZE]; |
40 |
matthewc |
38 |
static unsigned int min_keycode; |
41 |
astrand |
66 |
static uint16 remote_modifier_state = 0; |
42 |
matthewc |
38 |
|
43 |
astrand |
66 |
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 |
astrand |
64 |
static BOOL |
67 |
|
|
xkeymap_read(char *mapname) |
68 |
matthewc |
38 |
{ |
69 |
|
|
FILE *fp; |
70 |
astrand |
66 |
char line[KEYMAP_MAX_LINE_LENGTH], path[PATH_MAX]; |
71 |
|
|
unsigned int line_num = 0; |
72 |
|
|
unsigned int line_length = 0; |
73 |
matthewc |
38 |
char *keyname, *p; |
74 |
astrand |
66 |
char *line_rest; |
75 |
|
|
uint8 scancode; |
76 |
|
|
uint16 modifiers; |
77 |
matthewc |
38 |
|
78 |
astrand |
66 |
|
79 |
matthewc |
38 |
strcpy(path, KEYMAP_PATH); |
80 |
|
|
strncat(path, mapname, sizeof(path) - sizeof(KEYMAP_PATH)); |
81 |
|
|
|
82 |
|
|
fp = fopen(path, "r"); |
83 |
|
|
if (fp == NULL) |
84 |
|
|
{ |
85 |
|
|
error("Failed to open keymap %s\n", path); |
86 |
|
|
return False; |
87 |
|
|
} |
88 |
|
|
|
89 |
astrand |
66 |
/* FIXME: More tolerant on white space */ |
90 |
matthewc |
38 |
while (fgets(line, sizeof(line), fp) != NULL) |
91 |
|
|
{ |
92 |
astrand |
66 |
line_num++; |
93 |
|
|
|
94 |
|
|
/* Replace the \n with \0 */ |
95 |
matthewc |
38 |
p = strchr(line, '\n'); |
96 |
|
|
if (p != NULL) |
97 |
|
|
*p = 0; |
98 |
|
|
|
99 |
astrand |
66 |
line_length = strlen(line); |
100 |
|
|
|
101 |
|
|
/* Completely empty line */ |
102 |
|
|
if (strspn(line, " \t\n\r\f\v") == line_length) |
103 |
matthewc |
38 |
{ |
104 |
astrand |
66 |
continue; |
105 |
|
|
} |
106 |
matthewc |
38 |
|
107 |
astrand |
66 |
/* Include */ |
108 |
|
|
if (strncmp(line, "include ", 8) == 0) |
109 |
matthewc |
38 |
{ |
110 |
astrand |
64 |
if (!xkeymap_read(line + 8)) |
111 |
matthewc |
38 |
return False; |
112 |
astrand |
66 |
continue; |
113 |
matthewc |
38 |
} |
114 |
astrand |
66 |
|
115 |
|
|
/* map */ |
116 |
|
|
if (strncmp(line, "map ", 4) == 0) |
117 |
matthewc |
38 |
{ |
118 |
astrand |
64 |
keylayout = strtol(line + 4, NULL, 16); |
119 |
astrand |
66 |
DEBUG_KBD("Keylayout 0x%x\n", keylayout); |
120 |
|
|
continue; |
121 |
matthewc |
38 |
} |
122 |
astrand |
66 |
|
123 |
|
|
/* Comment */ |
124 |
|
|
if (line[0] == '#') |
125 |
matthewc |
38 |
{ |
126 |
astrand |
66 |
continue; |
127 |
matthewc |
38 |
} |
128 |
astrand |
66 |
|
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 |
|
|
|
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 |
|
|
/* 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 |
matthewc |
38 |
} |
178 |
|
|
|
179 |
|
|
fclose(fp); |
180 |
|
|
return True; |
181 |
|
|
} |
182 |
|
|
|
183 |
astrand |
66 |
|
184 |
|
|
/* Before connecting and creating UI */ |
185 |
astrand |
64 |
void |
186 |
astrand |
66 |
xkeymap_init1(void) |
187 |
matthewc |
38 |
{ |
188 |
astrand |
66 |
int i; |
189 |
matthewc |
38 |
|
190 |
astrand |
66 |
/* Zeroing keymap */ |
191 |
|
|
for (i = 0; i < KEYMAP_SIZE; i++) |
192 |
|
|
{ |
193 |
|
|
keymap[i].scancode = 0; |
194 |
|
|
keymap[i].modifiers = 0; |
195 |
|
|
} |
196 |
matthewc |
38 |
|
197 |
|
|
if (strcmp(keymapname, "none")) |
198 |
astrand |
66 |
{ |
199 |
matthewc |
38 |
xkeymap_read(keymapname); |
200 |
astrand |
66 |
} |
201 |
|
|
|
202 |
matthewc |
38 |
} |
203 |
|
|
|
204 |
astrand |
66 |
/* After connecting and creating UI */ |
205 |
|
|
void |
206 |
|
|
xkeymap_init2(void) |
207 |
matthewc |
38 |
{ |
208 |
astrand |
66 |
unsigned int max_keycode; |
209 |
|
|
XDisplayKeycodes(display, &min_keycode, &max_keycode); |
210 |
|
|
} |
211 |
matthewc |
38 |
|
212 |
astrand |
66 |
|
213 |
|
|
key_translation |
214 |
|
|
xkeymap_translate_key(KeySym keysym, unsigned int keycode) |
215 |
|
|
{ |
216 |
|
|
key_translation tr = { 0, 0 }; |
217 |
|
|
|
218 |
|
|
tr = keymap[keysym & KEYMAP_MASK]; |
219 |
|
|
|
220 |
|
|
if (tr.scancode != 0) |
221 |
matthewc |
50 |
{ |
222 |
astrand |
66 |
DEBUG_KBD |
223 |
|
|
("Found key translation, scancode=0x%x, modifiers=0x%x\n", |
224 |
|
|
tr.scancode, tr.modifiers); |
225 |
|
|
return tr; |
226 |
matthewc |
50 |
} |
227 |
|
|
|
228 |
astrand |
66 |
printf("No translation for (keysym 0x%lx, %s)\n", keysym, |
229 |
|
|
get_ksname(keysym)); |
230 |
|
|
|
231 |
matthewc |
38 |
/* not in keymap, try to interpret the raw scancode */ |
232 |
|
|
if ((keycode >= min_keycode) && (keycode <= 0x60)) |
233 |
|
|
{ |
234 |
astrand |
66 |
tr.scancode = keycode - min_keycode; |
235 |
|
|
printf("Sending guessed scancode 0x%x\n", tr.scancode); |
236 |
matthewc |
38 |
} |
237 |
astrand |
66 |
else |
238 |
|
|
{ |
239 |
|
|
printf("No good guess for keycode 0x%x found\n", keycode); |
240 |
|
|
} |
241 |
matthewc |
38 |
|
242 |
astrand |
66 |
return tr; |
243 |
matthewc |
38 |
} |
244 |
|
|
|
245 |
astrand |
64 |
uint16 |
246 |
|
|
xkeymap_translate_button(unsigned int button) |
247 |
matthewc |
38 |
{ |
248 |
|
|
switch (button) |
249 |
|
|
{ |
250 |
astrand |
64 |
case Button1: /* left */ |
251 |
matthewc |
38 |
return MOUSE_FLAG_BUTTON1; |
252 |
astrand |
64 |
case Button2: /* middle */ |
253 |
matthewc |
38 |
return MOUSE_FLAG_BUTTON3; |
254 |
astrand |
64 |
case Button3: /* right */ |
255 |
matthewc |
38 |
return MOUSE_FLAG_BUTTON2; |
256 |
jsorg71 |
67 |
case Button4: /* wheel up */ |
257 |
|
|
return MOUSE_FLAG_BUTTON4; |
258 |
|
|
case Button5: /* wheel down */ |
259 |
|
|
return MOUSE_FLAG_BUTTON5; |
260 |
matthewc |
38 |
} |
261 |
|
|
|
262 |
|
|
return 0; |
263 |
|
|
} |
264 |
astrand |
66 |
|
265 |
|
|
char * |
266 |
|
|
get_ksname(KeySym keysym) |
267 |
|
|
{ |
268 |
|
|
char *ksname = NULL; |
269 |
|
|
|
270 |
|
|
if (keysym == NoSymbol) |
271 |
|
|
ksname = "NoSymbol"; |
272 |
|
|
else if (!(ksname = XKeysymToString(keysym))) |
273 |
|
|
ksname = "(no name)"; |
274 |
|
|
|
275 |
|
|
return ksname; |
276 |
|
|
} |
277 |
|
|
|
278 |
|
|
BOOL |
279 |
|
|
inhibit_key(KeySym keysym) |
280 |
|
|
{ |
281 |
|
|
switch (keysym) |
282 |
|
|
{ |
283 |
|
|
case XK_Caps_Lock: |
284 |
|
|
return True; |
285 |
|
|
break; |
286 |
|
|
case XK_Multi_key: |
287 |
|
|
return True; |
288 |
|
|
break; |
289 |
|
|
default: |
290 |
|
|
break; |
291 |
|
|
} |
292 |
|
|
return False; |
293 |
|
|
} |
294 |
|
|
|
295 |
|
|
void |
296 |
|
|
ensure_remote_modifiers(uint32 ev_time, key_translation tr) |
297 |
|
|
{ |
298 |
|
|
/* If this key is a modifier, do nothing */ |
299 |
|
|
switch (tr.scancode) |
300 |
|
|
{ |
301 |
|
|
case SCANCODE_CHAR_LSHIFT: |
302 |
|
|
case SCANCODE_CHAR_RSHIFT: |
303 |
|
|
case SCANCODE_CHAR_LCTRL: |
304 |
|
|
case SCANCODE_CHAR_RCTRL: |
305 |
|
|
case SCANCODE_CHAR_LALT: |
306 |
|
|
case SCANCODE_CHAR_RALT: |
307 |
|
|
case SCANCODE_CHAR_LWIN: |
308 |
|
|
case SCANCODE_CHAR_RWIN: |
309 |
|
|
case SCANCODE_CHAR_NUMLOCK: |
310 |
|
|
return; |
311 |
|
|
default: |
312 |
|
|
break; |
313 |
|
|
} |
314 |
|
|
|
315 |
|
|
/* Shift */ |
316 |
|
|
if (MASK_HAS_BITS(tr.modifiers, MapShiftMask) |
317 |
|
|
!= MASK_HAS_BITS(remote_modifier_state, MapShiftMask)) |
318 |
|
|
{ |
319 |
|
|
/* The remote modifier state is not correct */ |
320 |
|
|
if (MASK_HAS_BITS(tr.modifiers, MapShiftMask)) |
321 |
|
|
{ |
322 |
|
|
/* Needs this modifier. Send down. */ |
323 |
|
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, |
324 |
|
|
SCANCODE_CHAR_LSHIFT); |
325 |
|
|
} |
326 |
|
|
else |
327 |
|
|
{ |
328 |
|
|
/* Should not use this modifier. Send up. */ |
329 |
|
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, |
330 |
|
|
SCANCODE_CHAR_LSHIFT); |
331 |
|
|
} |
332 |
|
|
} |
333 |
|
|
|
334 |
|
|
/* AltGr */ |
335 |
|
|
if (MASK_HAS_BITS(tr.modifiers, MapAltGrMask) |
336 |
|
|
!= MASK_HAS_BITS(remote_modifier_state, MapAltGrMask)) |
337 |
|
|
{ |
338 |
|
|
/* The remote modifier state is not correct */ |
339 |
|
|
if (MASK_HAS_BITS(tr.modifiers, MapAltGrMask)) |
340 |
|
|
{ |
341 |
|
|
/* Needs this modifier. Send down. */ |
342 |
|
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, |
343 |
|
|
SCANCODE_CHAR_RALT); |
344 |
|
|
} |
345 |
|
|
else |
346 |
|
|
{ |
347 |
|
|
/* Should not use this modifier. Send up. */ |
348 |
|
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, |
349 |
|
|
SCANCODE_CHAR_RALT); |
350 |
|
|
} |
351 |
|
|
} |
352 |
|
|
|
353 |
|
|
/* NumLock */ |
354 |
|
|
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask) |
355 |
|
|
!= MASK_HAS_BITS(remote_modifier_state, MapNumLockMask)) |
356 |
|
|
{ |
357 |
|
|
/* The remote modifier state is not correct */ |
358 |
|
|
DEBUG_KBD("Remote NumLock state is incorrect. Toggling\n"); |
359 |
|
|
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask)) |
360 |
|
|
{ |
361 |
|
|
/* Needs this modifier. Toggle */ |
362 |
|
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, |
363 |
|
|
SCANCODE_CHAR_NUMLOCK); |
364 |
|
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, |
365 |
|
|
SCANCODE_CHAR_NUMLOCK); |
366 |
|
|
} |
367 |
|
|
else |
368 |
|
|
{ |
369 |
|
|
/* Should not use this modifier. Toggle */ |
370 |
|
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, |
371 |
|
|
SCANCODE_CHAR_NUMLOCK); |
372 |
|
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, |
373 |
|
|
SCANCODE_CHAR_NUMLOCK); |
374 |
|
|
} |
375 |
|
|
} |
376 |
|
|
|
377 |
|
|
} |
378 |
|
|
|
379 |
|
|
|
380 |
|
|
static void |
381 |
|
|
update_modifier_state(uint16 modifiers, BOOL pressed) |
382 |
|
|
{ |
383 |
|
|
|
384 |
|
|
DEBUG_KBD("Before updating modifier_state:0x%x, pressed=0x%x\n", |
385 |
|
|
remote_modifier_state, pressed); |
386 |
|
|
switch (modifiers) |
387 |
|
|
{ |
388 |
|
|
case SCANCODE_CHAR_LSHIFT: |
389 |
|
|
MASK_CHANGE_BIT(remote_modifier_state, |
390 |
|
|
MapLeftShiftMask, pressed); |
391 |
|
|
break; |
392 |
|
|
case SCANCODE_CHAR_RSHIFT: |
393 |
|
|
MASK_CHANGE_BIT(remote_modifier_state, |
394 |
|
|
MapRightShiftMask, pressed); |
395 |
|
|
break; |
396 |
|
|
case SCANCODE_CHAR_LCTRL: |
397 |
|
|
MASK_CHANGE_BIT(remote_modifier_state, |
398 |
|
|
MapLeftCtrlMask, pressed); |
399 |
|
|
break; |
400 |
|
|
case SCANCODE_CHAR_RCTRL: |
401 |
|
|
MASK_CHANGE_BIT(remote_modifier_state, |
402 |
|
|
MapRightCtrlMask, pressed); |
403 |
|
|
break; |
404 |
|
|
case SCANCODE_CHAR_LALT: |
405 |
|
|
MASK_CHANGE_BIT(remote_modifier_state, MapLeftAltMask, |
406 |
|
|
pressed); |
407 |
|
|
break; |
408 |
|
|
case SCANCODE_CHAR_RALT: |
409 |
|
|
MASK_CHANGE_BIT(remote_modifier_state, |
410 |
|
|
MapRightAltMask, pressed); |
411 |
|
|
break; |
412 |
|
|
case SCANCODE_CHAR_LWIN: |
413 |
|
|
MASK_CHANGE_BIT(remote_modifier_state, MapLeftWinMask, |
414 |
|
|
pressed); |
415 |
|
|
break; |
416 |
|
|
case SCANCODE_CHAR_RWIN: |
417 |
|
|
MASK_CHANGE_BIT(remote_modifier_state, |
418 |
|
|
MapRightWinMask, pressed); |
419 |
|
|
break; |
420 |
|
|
case SCANCODE_CHAR_NUMLOCK: |
421 |
|
|
/* KeyReleases for NumLocks are sent immediately. Toggle the |
422 |
|
|
modifier state only on Keypress */ |
423 |
|
|
if (pressed) |
424 |
|
|
{ |
425 |
|
|
BOOL newNumLockState; |
426 |
|
|
newNumLockState = |
427 |
|
|
(MASK_HAS_BITS |
428 |
|
|
(remote_modifier_state, |
429 |
|
|
MapNumLockMask) == False); |
430 |
|
|
MASK_CHANGE_BIT(remote_modifier_state, |
431 |
|
|
MapNumLockMask, |
432 |
|
|
newNumLockState); |
433 |
|
|
} |
434 |
|
|
break; |
435 |
|
|
} |
436 |
|
|
DEBUG_KBD("After updating modifier_state:0x%x\n", |
437 |
|
|
remote_modifier_state); |
438 |
|
|
|
439 |
|
|
} |
440 |
|
|
|
441 |
|
|
/* Send keyboard input */ |
442 |
|
|
void |
443 |
|
|
rdp_send_scancode(uint32 time, uint16 flags, uint16 scancode) |
444 |
|
|
{ |
445 |
|
|
update_modifier_state(scancode, !(flags & RDP_KEYRELEASE)); |
446 |
|
|
|
447 |
|
|
if (scancode & SCANCODE_EXTENDED) |
448 |
|
|
{ |
449 |
|
|
DEBUG_KBD("Sending extended scancode=0x%x, flags=0x%x\n", |
450 |
|
|
scancode & ~SCANCODE_EXTENDED, flags); |
451 |
|
|
rdp_send_input(time, RDP_INPUT_SCANCODE, flags | KBD_FLAG_EXT, |
452 |
|
|
scancode & ~SCANCODE_EXTENDED, 0); |
453 |
|
|
} |
454 |
|
|
else |
455 |
|
|
{ |
456 |
|
|
DEBUG_KBD("Sending scancode=0x%x, flags=0x%x\n", scancode, |
457 |
|
|
flags); |
458 |
|
|
rdp_send_input(time, RDP_INPUT_SCANCODE, flags, scancode, 0); |
459 |
|
|
} |
460 |
|
|
} |