]>
Commit | Line | Data |
---|---|---|
6a021536 GH |
1 | /* |
2 | * QEMU keymap utility | |
3 | * | |
4 | * Copyright Red Hat, Inc. 2017 | |
5 | * | |
6 | * Authors: | |
7 | * Gerd Hoffmann <kraxel@redhat.com> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
10 | * See the COPYING file in the top-level directory. | |
11 | */ | |
12 | #include "qemu/osdep.h" | |
6a021536 GH |
13 | #include "qemu/notify.h" |
14 | #include "ui/input.h" | |
15 | ||
16 | #include <xkbcommon/xkbcommon.h> | |
17 | ||
18 | struct xkb_rule_names names = { | |
19 | .rules = NULL, | |
20 | .model = "pc105", | |
21 | .layout = "us", | |
22 | .variant = NULL, | |
23 | .options = NULL, | |
24 | }; | |
25 | ||
26 | static xkb_mod_mask_t shift; | |
27 | static xkb_mod_mask_t ctrl; | |
28 | static xkb_mod_mask_t altgr; | |
29 | static xkb_mod_mask_t numlock; | |
30 | ||
31 | static FILE *outfile; | |
32 | ||
33 | /* ------------------------------------------------------------------------ */ | |
34 | ||
35 | static uint32_t qcode_to_number(uint32_t qcode) | |
36 | { | |
37 | KeyValue keyvalue; | |
38 | uint32_t number; | |
39 | ||
40 | keyvalue.type = KEY_VALUE_KIND_QCODE; | |
41 | keyvalue.u.qcode.data = qcode; | |
42 | number = qemu_input_key_value_to_number(&keyvalue); | |
43 | assert(number != 0); | |
44 | return number; | |
45 | } | |
46 | ||
47 | static void print_sym(xkb_keysym_t sym, uint32_t qcode, const char *mod) | |
48 | { | |
49 | char name[64]; | |
50 | ||
51 | if (sym == XKB_KEY_NoSymbol) { | |
52 | return; | |
53 | } | |
54 | xkb_keysym_get_name(sym, name, sizeof(name)); | |
55 | ||
56 | /* TODO: make ui/keymap.c parser accept QKeyCode names */ | |
57 | fprintf(outfile, "%s 0x%02x%s\n", name, qcode_to_number(qcode), mod); | |
58 | } | |
59 | ||
60 | static void walk_map(struct xkb_keymap *map, xkb_keycode_t code, void *data) | |
61 | { | |
62 | struct xkb_state *state = data; | |
63 | xkb_keysym_t kbase, knumlock, kshift, kaltgr, kaltgrshift; | |
64 | uint32_t evdev, qcode; | |
65 | char name[64]; | |
66 | ||
67 | fprintf(outfile, "\n"); | |
68 | ||
69 | /* | |
70 | * map xkb keycode -> QKeyCode | |
71 | * | |
72 | * xkb keycode is linux evdev shifted by 8 | |
73 | */ | |
74 | evdev = code - 8; | |
75 | qcode = qemu_input_linux_to_qcode(evdev); | |
76 | if (qcode == Q_KEY_CODE_UNMAPPED) { | |
77 | xkb_state_update_mask(state, 0, 0, 0, 0, 0, 0); | |
78 | kbase = xkb_state_key_get_one_sym(state, code); | |
79 | xkb_keysym_get_name(kbase, name, sizeof(name)); | |
80 | fprintf(outfile, "# evdev %d (0x%x): no evdev -> QKeyCode mapping" | |
81 | " (xkb keysym %s)\n", evdev, evdev, name); | |
82 | return; | |
83 | } | |
84 | fprintf(outfile, "# evdev %d (0x%x), QKeyCode \"%s\", number 0x%x\n", | |
85 | evdev, evdev, | |
c809d1d2 | 86 | QKeyCode_str(qcode), |
6a021536 GH |
87 | qcode_to_number(qcode)); |
88 | ||
89 | /* | |
90 | * check which modifier states generate which keysyms | |
91 | */ | |
92 | xkb_state_update_mask(state, 0, 0, 0, 0, 0, 0); | |
93 | kbase = xkb_state_key_get_one_sym(state, code); | |
94 | print_sym(kbase, qcode, ""); | |
95 | ||
96 | xkb_state_update_mask(state, 0, 0, numlock, 0, 0, 0); | |
97 | knumlock = xkb_state_key_get_one_sym(state, code); | |
98 | if (kbase != knumlock) { | |
99 | print_sym(knumlock, qcode, " numlock"); | |
100 | } | |
101 | ||
102 | xkb_state_update_mask(state, shift, 0, 0, 0, 0, 0); | |
103 | kshift = xkb_state_key_get_one_sym(state, code); | |
104 | if (kbase != kshift && knumlock != kshift) { | |
105 | print_sym(kshift, qcode, " shift"); | |
106 | } | |
107 | ||
108 | xkb_state_update_mask(state, altgr, 0, 0, 0, 0, 0); | |
109 | kaltgr = xkb_state_key_get_one_sym(state, code); | |
110 | if (kbase != kaltgr) { | |
111 | print_sym(kaltgr, qcode, " altgr"); | |
112 | } | |
113 | ||
114 | xkb_state_update_mask(state, altgr | shift, 0, 0, 0, 0, 0); | |
115 | kaltgrshift = xkb_state_key_get_one_sym(state, code); | |
116 | if (kshift != kaltgrshift && kaltgr != kaltgrshift) { | |
117 | print_sym(kaltgrshift, qcode, " shift altgr"); | |
118 | } | |
119 | return; | |
120 | } | |
121 | ||
122 | static void usage(FILE *out) | |
123 | { | |
124 | fprintf(out, | |
125 | "\n" | |
126 | "This tool generates qemu reverse keymaps from xkb keymaps,\n" | |
127 | "which can be used with the qemu \"-k\" command line switch.\n" | |
128 | "\n" | |
129 | "usage: qemu-keymap <options>\n" | |
130 | "options:\n" | |
131 | " -h print this text\n" | |
132 | " -f <file> set output file (default: stdout)\n" | |
133 | " -m <model> set kbd model (default: %s)\n" | |
134 | " -l <layout> set kbd layout (default: %s)\n" | |
135 | " -v <variant> set kbd variant (default: %s)\n" | |
136 | " -o <options> set kbd options (default: %s)\n" | |
137 | "\n", | |
138 | names.model, names.layout, | |
139 | names.variant ?: "-", | |
140 | names.options ?: "-"); | |
141 | } | |
142 | ||
143 | int main(int argc, char *argv[]) | |
144 | { | |
145 | struct xkb_context *ctx; | |
146 | struct xkb_keymap *map; | |
147 | struct xkb_state *state; | |
148 | xkb_mod_index_t mod, mods; | |
149 | int rc; | |
150 | ||
151 | for (;;) { | |
152 | rc = getopt(argc, argv, "hm:l:v:o:f:"); | |
153 | if (rc == -1) { | |
154 | break; | |
155 | } | |
156 | switch (rc) { | |
157 | case 'm': | |
158 | names.model = optarg; | |
159 | break; | |
160 | case 'l': | |
161 | names.layout = optarg; | |
162 | break; | |
163 | case 'v': | |
164 | names.variant = optarg; | |
165 | break; | |
166 | case 'o': | |
167 | names.options = optarg; | |
168 | break; | |
169 | case 'f': | |
170 | outfile = fopen(optarg, "w"); | |
171 | if (outfile == NULL) { | |
172 | fprintf(stderr, "open %s: %s\n", optarg, strerror(errno)); | |
173 | exit(1); | |
174 | } | |
175 | break; | |
176 | case 'h': | |
177 | usage(stdout); | |
178 | exit(0); | |
179 | default: | |
180 | usage(stderr); | |
181 | exit(1); | |
182 | } | |
183 | } | |
184 | ||
185 | if (outfile == NULL) { | |
186 | outfile = stdout; | |
187 | } | |
188 | ||
189 | fprintf(outfile, | |
190 | "#\n" | |
191 | "# generated by qemu-keymap\n" | |
192 | "# model : %s\n" | |
193 | "# layout : %s\n" | |
194 | "# variant : %s\n" | |
195 | "# options : %s\n" | |
196 | "\n", | |
197 | names.model, names.layout, | |
198 | names.variant ?: "-", | |
199 | names.options ?: "-"); | |
200 | ||
201 | ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS); | |
202 | map = xkb_keymap_new_from_names(ctx, &names, XKB_KEYMAP_COMPILE_NO_FLAGS); | |
203 | if (!map) { | |
204 | /* libxkbcommon prints error */ | |
205 | exit(1); | |
206 | } | |
207 | ||
208 | fprintf(outfile, "# name: \"%s\"\n\n", | |
209 | xkb_keymap_layout_get_name(map, 0)); | |
210 | fprintf(outfile, "# modifiers\n"); | |
211 | mods = xkb_keymap_num_mods(map); | |
212 | for (mod = 0; mod < mods; mod++) { | |
213 | fprintf(outfile, "# %2d: %s\n", | |
214 | mod, xkb_keymap_mod_get_name(map, mod)); | |
215 | } | |
216 | ||
217 | mod = xkb_keymap_mod_get_index(map, "Shift"); | |
218 | shift = (1 << mod); | |
219 | mod = xkb_keymap_mod_get_index(map, "Control"); | |
220 | ctrl = (1 << mod); | |
221 | mod = xkb_keymap_mod_get_index(map, "AltGr"); | |
222 | altgr = (1 << mod); | |
223 | mod = xkb_keymap_mod_get_index(map, "NumLock"); | |
224 | numlock = (1 << mod); | |
225 | ||
226 | state = xkb_state_new(map); | |
227 | xkb_keymap_key_for_each(map, walk_map, state); | |
228 | ||
229 | /* add quirks */ | |
230 | fprintf(outfile, | |
231 | "\n" | |
232 | "#\n" | |
233 | "# quirks section start\n" | |
234 | "#\n" | |
235 | "# Sometimes multiple keysyms map to the same keycodes.\n" | |
236 | "# The keycode -> keysym lookup finds only one of the\n" | |
237 | "# keysyms. So append them here.\n" | |
238 | "#\n" | |
239 | "\n"); | |
240 | print_sym(XKB_KEY_Print, Q_KEY_CODE_SYSRQ, ""); | |
241 | print_sym(XKB_KEY_Sys_Req, Q_KEY_CODE_SYSRQ, ""); | |
242 | print_sym(XKB_KEY_Execute, Q_KEY_CODE_SYSRQ, ""); | |
243 | ||
244 | print_sym(XKB_KEY_KP_Decimal, Q_KEY_CODE_KP_DECIMAL, " numlock"); | |
245 | print_sym(XKB_KEY_KP_Separator, Q_KEY_CODE_KP_DECIMAL, " numlock"); | |
246 | ||
247 | print_sym(XKB_KEY_Alt_R, Q_KEY_CODE_ALT_R, ""); | |
248 | print_sym(XKB_KEY_ISO_Level3_Shift, Q_KEY_CODE_ALT_R, ""); | |
249 | print_sym(XKB_KEY_Mode_switch, Q_KEY_CODE_ALT_R, ""); | |
250 | ||
251 | fprintf(outfile, | |
252 | "\n" | |
253 | "# quirks section end\n"); | |
254 | ||
255 | exit(0); | |
256 | } |