1 |
/* |
/* -*- c-basic-offset: 8 -*- |
2 |
rdesktop: A Remote Desktop Protocol client. |
rdesktop: A Remote Desktop Protocol client. |
3 |
User interface services - X keyboard mapping |
User interface services - X keyboard mapping |
4 |
|
|
30 |
#include <ctype.h> |
#include <ctype.h> |
31 |
#include <limits.h> |
#include <limits.h> |
32 |
#include <time.h> |
#include <time.h> |
33 |
|
#include <string.h> |
34 |
#include "rdesktop.h" |
#include "rdesktop.h" |
35 |
#include "scancodes.h" |
#include "scancodes.h" |
36 |
|
|
55 |
|
|
56 |
static void update_modifier_state(uint8 scancode, BOOL pressed); |
static void update_modifier_state(uint8 scancode, BOOL pressed); |
57 |
|
|
58 |
/* Free key_translation structure, included linked list */ |
/* Free key_translation structure, including linked list */ |
59 |
void |
static void |
60 |
free_key_translation(key_translation * ptr) |
free_key_translation(key_translation * ptr) |
61 |
{ |
{ |
62 |
key_translation *next; |
key_translation *next; |
156 |
DEBUG_KBD(("\n")); |
DEBUG_KBD(("\n")); |
157 |
} |
} |
158 |
|
|
159 |
|
void |
160 |
|
xkeymap_from_locale(const char *locale) |
161 |
|
{ |
162 |
|
char *str, *ptr; |
163 |
|
FILE *fp; |
164 |
|
|
165 |
|
/* Create a working copy */ |
166 |
|
str = strdup(locale); |
167 |
|
if (str == NULL) |
168 |
|
{ |
169 |
|
perror("strdup"); |
170 |
|
exit(1); |
171 |
|
} |
172 |
|
|
173 |
|
/* Truncate at dot and at */ |
174 |
|
ptr = strrchr(str, '.'); |
175 |
|
if (ptr) |
176 |
|
*ptr = '\0'; |
177 |
|
ptr = strrchr(str, '@'); |
178 |
|
if (ptr) |
179 |
|
*ptr = '\0'; |
180 |
|
|
181 |
|
/* Replace _ with - */ |
182 |
|
ptr = strrchr(str, '_'); |
183 |
|
if (ptr) |
184 |
|
*ptr = '-'; |
185 |
|
|
186 |
|
/* Convert to lowercase */ |
187 |
|
ptr = str; |
188 |
|
while (*ptr) |
189 |
|
{ |
190 |
|
*ptr = tolower((int) *ptr); |
191 |
|
ptr++; |
192 |
|
} |
193 |
|
|
194 |
|
/* Try to open this keymap (da-dk) */ |
195 |
|
fp = xkeymap_open(str); |
196 |
|
if (fp == NULL) |
197 |
|
{ |
198 |
|
/* Truncate at dash */ |
199 |
|
ptr = strrchr(str, '-'); |
200 |
|
if (ptr) |
201 |
|
*ptr = '\0'; |
202 |
|
|
203 |
|
/* Try the short name (da) */ |
204 |
|
fp = xkeymap_open(str); |
205 |
|
} |
206 |
|
|
207 |
|
if (fp) |
208 |
|
{ |
209 |
|
fclose(fp); |
210 |
|
STRNCPY(keymapname, str, sizeof(keymapname)); |
211 |
|
fprintf(stderr, "Autoselected keyboard map %s.\n", keymapname); |
212 |
|
} |
213 |
|
} |
214 |
|
|
215 |
|
|
216 |
|
/* Joins two path components. The result should be freed with |
217 |
|
xfree(). */ |
218 |
|
static char * |
219 |
|
pathjoin(const char *a, const char *b) |
220 |
|
{ |
221 |
|
char *result; |
222 |
|
result = xmalloc(PATH_MAX * 2 + 1); |
223 |
|
|
224 |
|
if (b[0] == '/') |
225 |
|
{ |
226 |
|
strncpy(result, b, PATH_MAX); |
227 |
|
} |
228 |
|
else |
229 |
|
{ |
230 |
|
strncpy(result, a, PATH_MAX); |
231 |
|
strcat(result, "/"); |
232 |
|
strncat(result, b, PATH_MAX); |
233 |
|
} |
234 |
|
return result; |
235 |
|
} |
236 |
|
|
237 |
|
/* Try to open a keymap with fopen() */ |
238 |
|
FILE * |
239 |
|
xkeymap_open(const char *filename) |
240 |
|
{ |
241 |
|
char *path1, *path2; |
242 |
|
char *home; |
243 |
|
FILE *fp; |
244 |
|
|
245 |
|
/* Try ~/.rdesktop/keymaps */ |
246 |
|
home = getenv("HOME"); |
247 |
|
if (home) |
248 |
|
{ |
249 |
|
path1 = pathjoin(home, ".rdesktop/keymaps"); |
250 |
|
path2 = pathjoin(path1, filename); |
251 |
|
xfree(path1); |
252 |
|
fp = fopen(path2, "r"); |
253 |
|
xfree(path2); |
254 |
|
if (fp) |
255 |
|
return fp; |
256 |
|
} |
257 |
|
|
258 |
|
/* Try KEYMAP_PATH */ |
259 |
|
path1 = pathjoin(KEYMAP_PATH, filename); |
260 |
|
fp = fopen(path1, "r"); |
261 |
|
xfree(path1); |
262 |
|
if (fp) |
263 |
|
return fp; |
264 |
|
|
265 |
|
/* Try current directory, in case we are running from the source |
266 |
|
tree */ |
267 |
|
path1 = pathjoin("keymaps", filename); |
268 |
|
fp = fopen(path1, "r"); |
269 |
|
xfree(path1); |
270 |
|
if (fp) |
271 |
|
return fp; |
272 |
|
|
273 |
|
return NULL; |
274 |
|
} |
275 |
|
|
276 |
static BOOL |
static BOOL |
277 |
xkeymap_read(char *mapname) |
xkeymap_read(char *mapname) |
278 |
{ |
{ |
279 |
FILE *fp; |
FILE *fp; |
280 |
char line[KEYMAP_MAX_LINE_LENGTH]; |
char line[KEYMAP_MAX_LINE_LENGTH]; |
|
char path[PATH_MAX], inplace_path[PATH_MAX]; |
|
281 |
unsigned int line_num = 0; |
unsigned int line_num = 0; |
282 |
unsigned int line_length = 0; |
unsigned int line_length = 0; |
283 |
char *keyname, *p; |
char *keyname, *p; |
285 |
uint8 scancode; |
uint8 scancode; |
286 |
uint16 modifiers; |
uint16 modifiers; |
287 |
|
|
288 |
|
fp = xkeymap_open(mapname); |
|
strcpy(path, KEYMAP_PATH); |
|
|
strncat(path, mapname, sizeof(path) - sizeof(KEYMAP_PATH)); |
|
|
|
|
|
fp = fopen(path, "r"); |
|
289 |
if (fp == NULL) |
if (fp == NULL) |
290 |
{ |
{ |
291 |
/* in case we are running from the source tree */ |
error("Failed to open keymap %s\n", mapname); |
292 |
strcpy(inplace_path, "keymaps/"); |
return False; |
|
strncat(inplace_path, mapname, sizeof(inplace_path) - sizeof("keymaps/")); |
|
|
|
|
|
fp = fopen(inplace_path, "r"); |
|
|
if (fp == NULL) |
|
|
{ |
|
|
error("Failed to open keymap %s\n", path); |
|
|
return False; |
|
|
} |
|
293 |
} |
} |
294 |
|
|
295 |
/* FIXME: More tolerant on white space */ |
/* FIXME: More tolerant on white space */ |
416 |
xkeymap_init(void) |
xkeymap_init(void) |
417 |
{ |
{ |
418 |
unsigned int max_keycode; |
unsigned int max_keycode; |
|
char *mapname_ptr; |
|
|
|
|
|
/* Make keymapname lowercase */ |
|
|
mapname_ptr = keymapname; |
|
|
while (*mapname_ptr) |
|
|
{ |
|
|
*mapname_ptr = tolower((int) *mapname_ptr); |
|
|
mapname_ptr++; |
|
|
} |
|
419 |
|
|
420 |
if (strcmp(keymapname, "none")) |
if (strcmp(keymapname, "none")) |
421 |
{ |
{ |