root / keymaps.c @ 86cc1ce0
History | View | Annotate | Download (4.2 kB)
1 | 3d11d0eb | bellard | /*
|
---|---|---|---|
2 | 3d11d0eb | bellard | * QEMU keysym to keycode conversion using rdesktop keymaps
|
3 | 3d11d0eb | bellard | *
|
4 | 3d11d0eb | bellard | * Copyright (c) 2004 Johannes Schindelin
|
5 | 3d11d0eb | bellard | *
|
6 | 3d11d0eb | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 3d11d0eb | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 3d11d0eb | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 3d11d0eb | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 3d11d0eb | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 3d11d0eb | bellard | * furnished to do so, subject to the following conditions:
|
12 | 3d11d0eb | bellard | *
|
13 | 3d11d0eb | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 3d11d0eb | bellard | * all copies or substantial portions of the Software.
|
15 | 3d11d0eb | bellard | *
|
16 | 3d11d0eb | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 3d11d0eb | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 3d11d0eb | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 3d11d0eb | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 3d11d0eb | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 3d11d0eb | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 3d11d0eb | bellard | * THE SOFTWARE.
|
23 | 3d11d0eb | bellard | */
|
24 | 3d11d0eb | bellard | |
25 | 3d11d0eb | bellard | static int get_keysym(const char *name) |
26 | 3d11d0eb | bellard | { |
27 | 3d11d0eb | bellard | name2keysym_t *p; |
28 | 3d11d0eb | bellard | for(p = name2keysym; p->name != NULL; p++) { |
29 | 3d11d0eb | bellard | if (!strcmp(p->name, name))
|
30 | 3d11d0eb | bellard | return p->keysym;
|
31 | 3d11d0eb | bellard | } |
32 | 3d11d0eb | bellard | return 0; |
33 | 3d11d0eb | bellard | } |
34 | 3d11d0eb | bellard | |
35 | 3d11d0eb | bellard | #define MAX_NORMAL_KEYCODE 512 |
36 | 3d11d0eb | bellard | #define MAX_EXTRA_COUNT 256 |
37 | 3d11d0eb | bellard | typedef struct { |
38 | 3d11d0eb | bellard | uint16_t keysym2keycode[MAX_NORMAL_KEYCODE]; |
39 | 3d11d0eb | bellard | struct {
|
40 | 3d11d0eb | bellard | int keysym;
|
41 | 3d11d0eb | bellard | uint16_t keycode; |
42 | 3d11d0eb | bellard | } keysym2keycode_extra[MAX_EXTRA_COUNT]; |
43 | 3d11d0eb | bellard | int extra_count;
|
44 | 3d11d0eb | bellard | } kbd_layout_t; |
45 | 3d11d0eb | bellard | |
46 | 3d11d0eb | bellard | static kbd_layout_t *parse_keyboard_layout(const char *language, |
47 | 3d11d0eb | bellard | kbd_layout_t * k) |
48 | 3d11d0eb | bellard | { |
49 | 3d11d0eb | bellard | FILE *f; |
50 | 3d11d0eb | bellard | char file_name[1024]; |
51 | 3d11d0eb | bellard | char line[1024]; |
52 | 3d11d0eb | bellard | int len;
|
53 | 3d11d0eb | bellard | |
54 | 3d11d0eb | bellard | snprintf(file_name, sizeof(file_name),
|
55 | 3d11d0eb | bellard | "%s/keymaps/%s", bios_dir, language);
|
56 | 3d11d0eb | bellard | |
57 | 3d11d0eb | bellard | if (!k)
|
58 | 3d11d0eb | bellard | k = qemu_mallocz(sizeof(kbd_layout_t));
|
59 | 3d11d0eb | bellard | if (!k)
|
60 | 3d11d0eb | bellard | return 0; |
61 | 3d11d0eb | bellard | if (!(f = fopen(file_name, "r"))) { |
62 | 3d11d0eb | bellard | fprintf(stderr, |
63 | 3d11d0eb | bellard | "Could not read keymap file: '%s'\n", file_name);
|
64 | 3d11d0eb | bellard | return 0; |
65 | 3d11d0eb | bellard | } |
66 | 3d11d0eb | bellard | for(;;) {
|
67 | 3d11d0eb | bellard | if (fgets(line, 1024, f) == NULL) |
68 | 3d11d0eb | bellard | break;
|
69 | 3d11d0eb | bellard | len = strlen(line); |
70 | 3d11d0eb | bellard | if (len > 0 && line[len - 1] == '\n') |
71 | 3d11d0eb | bellard | line[len - 1] = '\0'; |
72 | 3d11d0eb | bellard | if (line[0] == '#') |
73 | 3d11d0eb | bellard | continue;
|
74 | 3d11d0eb | bellard | if (!strncmp(line, "map ", 4)) |
75 | 3d11d0eb | bellard | continue;
|
76 | 3d11d0eb | bellard | if (!strncmp(line, "include ", 8)) { |
77 | 3d11d0eb | bellard | parse_keyboard_layout(line + 8, k);
|
78 | 3d11d0eb | bellard | } else {
|
79 | 3d11d0eb | bellard | char *end_of_keysym = line;
|
80 | 3d11d0eb | bellard | while (*end_of_keysym != 0 && *end_of_keysym != ' ') |
81 | 3d11d0eb | bellard | end_of_keysym++; |
82 | 3d11d0eb | bellard | if (*end_of_keysym) {
|
83 | 3d11d0eb | bellard | int keysym;
|
84 | 3d11d0eb | bellard | *end_of_keysym = 0;
|
85 | 3d11d0eb | bellard | keysym = get_keysym(line); |
86 | 3d11d0eb | bellard | if (keysym == 0) { |
87 | 3d11d0eb | bellard | // fprintf(stderr, "Warning: unknown keysym %s\n", line);
|
88 | 3d11d0eb | bellard | } else {
|
89 | 3d11d0eb | bellard | const char *rest = end_of_keysym + 1; |
90 | 3d11d0eb | bellard | int keycode = strtol(rest, NULL, 0); |
91 | 3d11d0eb | bellard | /* if(keycode&0x80)
|
92 | 3d11d0eb | bellard | keycode=(keycode<<8)^0x80e0; */
|
93 | 3d11d0eb | bellard | if (keysym < MAX_NORMAL_KEYCODE) {
|
94 | 3d11d0eb | bellard | //fprintf(stderr,"Setting keysym %s (%d) to %d\n",line,keysym,keycode);
|
95 | 3d11d0eb | bellard | k->keysym2keycode[keysym] = keycode; |
96 | 3d11d0eb | bellard | } else {
|
97 | 3d11d0eb | bellard | if (k->extra_count >= MAX_EXTRA_COUNT) {
|
98 | 3d11d0eb | bellard | fprintf(stderr, |
99 | 3d11d0eb | bellard | "Warning: Could not assign keysym %s (0x%x) because of memory constraints.\n",
|
100 | 3d11d0eb | bellard | line, keysym); |
101 | 3d11d0eb | bellard | } else {
|
102 | 24236869 | bellard | #if 0
|
103 | 3d11d0eb | bellard | fprintf(stderr, "Setting %d: %d,%d\n",
|
104 | 3d11d0eb | bellard | k->extra_count, keysym, keycode);
|
105 | 24236869 | bellard | #endif
|
106 | 3d11d0eb | bellard | k->keysym2keycode_extra[k->extra_count]. |
107 | 3d11d0eb | bellard | keysym = keysym; |
108 | 3d11d0eb | bellard | k->keysym2keycode_extra[k->extra_count]. |
109 | 3d11d0eb | bellard | keycode = keycode; |
110 | 3d11d0eb | bellard | k->extra_count++; |
111 | 3d11d0eb | bellard | } |
112 | 3d11d0eb | bellard | } |
113 | 3d11d0eb | bellard | } |
114 | 3d11d0eb | bellard | } |
115 | 3d11d0eb | bellard | } |
116 | 3d11d0eb | bellard | } |
117 | 3d11d0eb | bellard | fclose(f); |
118 | 3d11d0eb | bellard | return k;
|
119 | 3d11d0eb | bellard | } |
120 | 3d11d0eb | bellard | |
121 | 3d11d0eb | bellard | static void *init_keyboard_layout(const char *language) |
122 | 3d11d0eb | bellard | { |
123 | 3d11d0eb | bellard | return parse_keyboard_layout(language, 0); |
124 | 3d11d0eb | bellard | } |
125 | 3d11d0eb | bellard | |
126 | 3d11d0eb | bellard | static int keysym2scancode(void *kbd_layout, int keysym) |
127 | 3d11d0eb | bellard | { |
128 | 3d11d0eb | bellard | kbd_layout_t *k = kbd_layout; |
129 | 3d11d0eb | bellard | if (keysym < MAX_NORMAL_KEYCODE) {
|
130 | 3d11d0eb | bellard | if (k->keysym2keycode[keysym] == 0) |
131 | 3d11d0eb | bellard | fprintf(stderr, "Warning: no scancode found for keysym %d\n",
|
132 | 3d11d0eb | bellard | keysym); |
133 | 3d11d0eb | bellard | return k->keysym2keycode[keysym];
|
134 | 3d11d0eb | bellard | } else {
|
135 | 3d11d0eb | bellard | int i;
|
136 | 3d11d0eb | bellard | #ifdef XK_ISO_Left_Tab
|
137 | 3d11d0eb | bellard | if (keysym == XK_ISO_Left_Tab)
|
138 | 3d11d0eb | bellard | keysym = XK_Tab; |
139 | 3d11d0eb | bellard | #endif
|
140 | 3d11d0eb | bellard | for (i = 0; i < k->extra_count; i++) |
141 | 3d11d0eb | bellard | if (k->keysym2keycode_extra[i].keysym == keysym)
|
142 | 3d11d0eb | bellard | return k->keysym2keycode_extra[i].keycode;
|
143 | 3d11d0eb | bellard | } |
144 | 3d11d0eb | bellard | return 0; |
145 | 3d11d0eb | bellard | } |