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 <limits.h> |
27 |
#include "rdesktop.h" |
28 |
|
29 |
#define KEYMAP_SIZE 4096 |
30 |
#define KEYMAP_MASK (KEYMAP_SIZE - 1) |
31 |
|
32 |
extern Display *display; |
33 |
extern char keymapname[16]; |
34 |
extern int keylayout; |
35 |
|
36 |
static uint8 keymap[KEYMAP_SIZE]; |
37 |
static unsigned int min_keycode; |
38 |
|
39 |
static BOOL xkeymap_read(char *mapname) |
40 |
{ |
41 |
FILE *fp; |
42 |
char line[PATH_MAX], path[PATH_MAX]; |
43 |
char *keyname, *p; |
44 |
KeySym keysym; |
45 |
unsigned char keycode; |
46 |
|
47 |
strcpy(path, KEYMAP_PATH); |
48 |
strncat(path, mapname, sizeof(path) - sizeof(KEYMAP_PATH)); |
49 |
|
50 |
fp = fopen(path, "r"); |
51 |
if (fp == NULL) |
52 |
{ |
53 |
error("Failed to open keymap %s\n", path); |
54 |
return False; |
55 |
} |
56 |
|
57 |
while (fgets(line, sizeof(line), fp) != NULL) |
58 |
{ |
59 |
p = strchr(line, '\n'); |
60 |
if (p != NULL) |
61 |
*p = 0; |
62 |
|
63 |
keycode = strtol(line, &keyname, 16); |
64 |
if ((keycode != 0) && (*keyname == ' ')) |
65 |
{ |
66 |
do |
67 |
{ |
68 |
keyname++; |
69 |
p = strchr(keyname, ' '); |
70 |
if (p != NULL) |
71 |
*p = 0; |
72 |
|
73 |
keysym = XStringToKeysym(keyname); |
74 |
if (keysym == NoSymbol) |
75 |
error("Bad keysym %s in keymap %s\n", keyname, mapname); |
76 |
|
77 |
keymap[keysym & KEYMAP_MASK] = keycode; |
78 |
keyname = p; |
79 |
|
80 |
} while (keyname != NULL); |
81 |
} |
82 |
else if (strncmp(line, "include ", 8) == 0) |
83 |
{ |
84 |
if (!xkeymap_read(line+8)) |
85 |
return False; |
86 |
} |
87 |
else if (strncmp(line, "map ", 4) == 0) |
88 |
{ |
89 |
keylayout = strtol(line+4, NULL, 16); |
90 |
} |
91 |
else if (line[0] != '#') |
92 |
{ |
93 |
error("Malformed line in keymap %s\n", mapname); |
94 |
} |
95 |
} |
96 |
|
97 |
fclose(fp); |
98 |
return True; |
99 |
} |
100 |
|
101 |
void xkeymap_init(void) |
102 |
{ |
103 |
unsigned int max_keycode; |
104 |
|
105 |
XDisplayKeycodes(display, &min_keycode, &max_keycode); |
106 |
|
107 |
if (strcmp(keymapname, "none")) |
108 |
xkeymap_read(keymapname); |
109 |
} |
110 |
|
111 |
uint8 xkeymap_translate_key(unsigned int keysym, unsigned int keycode, uint16 *flags) |
112 |
{ |
113 |
uint8 scancode; |
114 |
|
115 |
scancode = keymap[keysym & KEYMAP_MASK]; |
116 |
if (scancode != 0) |
117 |
{ |
118 |
if (scancode & 0x80) |
119 |
*flags |= KBD_FLAG_EXT; |
120 |
|
121 |
return (scancode & 0x7f); |
122 |
} |
123 |
|
124 |
/* not in keymap, try to interpret the raw scancode */ |
125 |
|
126 |
if ((keycode >= min_keycode) && (keycode <= 0x60)) |
127 |
return (uint8)(keycode - min_keycode); |
128 |
|
129 |
*flags |= KBD_FLAG_EXT; |
130 |
|
131 |
switch (keycode) |
132 |
{ |
133 |
case 0x61: /* home */ |
134 |
return 0x47; |
135 |
case 0x62: /* up arrow */ |
136 |
return 0x48; |
137 |
case 0x63: /* page up */ |
138 |
return 0x49; |
139 |
case 0x64: /* left arrow */ |
140 |
return 0x4b; |
141 |
case 0x66: /* right arrow */ |
142 |
return 0x4d; |
143 |
case 0x67: /* end */ |
144 |
return 0x4f; |
145 |
case 0x68: /* down arrow */ |
146 |
return 0x50; |
147 |
case 0x69: /* page down */ |
148 |
return 0x51; |
149 |
case 0x6a: /* insert */ |
150 |
return 0x52; |
151 |
case 0x6b: /* delete */ |
152 |
return 0x53; |
153 |
case 0x6c: /* keypad enter */ |
154 |
return 0x1c; |
155 |
case 0x6d: /* right ctrl */ |
156 |
return 0x1d; |
157 |
case 0x6f: /* ctrl - print screen */ |
158 |
return 0x37; |
159 |
case 0x70: /* keypad '/' */ |
160 |
return 0x35; |
161 |
case 0x71: /* right alt */ |
162 |
return 0x38; |
163 |
case 0x72: /* ctrl break */ |
164 |
return 0x46; |
165 |
case 0x73: /* left window key */ |
166 |
return 0x5b; |
167 |
case 0x74: /* right window key */ |
168 |
return 0x5c; |
169 |
case 0x75: /* menu key */ |
170 |
return 0x5d; |
171 |
} |
172 |
|
173 |
return 0; |
174 |
} |
175 |
|
176 |
uint16 xkeymap_translate_button(unsigned int button) |
177 |
{ |
178 |
switch (button) |
179 |
{ |
180 |
case Button1: /* left */ |
181 |
return MOUSE_FLAG_BUTTON1; |
182 |
case Button2: /* middle */ |
183 |
return MOUSE_FLAG_BUTTON3; |
184 |
case Button3: /* right */ |
185 |
return MOUSE_FLAG_BUTTON2; |
186 |
} |
187 |
|
188 |
return 0; |
189 |
} |