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