util/bufferiszero: Introduce biz_accel_fn typedef
[qemu/armbru.git] / qemu-keymap.c
blob8c80f7a4ed6583bb044ba69e035920817bcd75e9
1 /*
2 * QEMU keymap utility
4 * Copyright Red Hat, Inc. 2017
6 * Authors:
7 * Gerd Hoffmann <kraxel@redhat.com>
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.
12 #include "qemu/osdep.h"
13 #include "qemu/notify.h"
14 #include "ui/input.h"
16 #include <xkbcommon/xkbcommon.h>
18 struct xkb_rule_names names = {
19 .rules = NULL,
20 .model = "pc105",
21 .layout = "us",
22 .variant = NULL,
23 .options = NULL,
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;
31 static FILE *outfile;
33 /* ------------------------------------------------------------------------ */
35 static uint32_t qcode_to_number(uint32_t qcode)
37 KeyValue keyvalue;
38 uint32_t number;
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;
47 static void print_sym(xkb_keysym_t sym, uint32_t qcode, const char *mod)
49 char name[64];
51 if (sym == XKB_KEY_NoSymbol) {
52 return;
54 xkb_keysym_get_name(sym, name, sizeof(name));
56 /* TODO: make ui/keymap.c parser accept QKeyCode names */
57 fprintf(outfile, "%s 0x%02x%s\n", name, qcode_to_number(qcode), mod);
60 static void walk_map(struct xkb_keymap *map, xkb_keycode_t code, void *data)
62 struct xkb_state *state = data;
63 xkb_keysym_t kbase, knumlock, kshift, kaltgr, kaltgrshift;
64 uint32_t evdev, qcode;
65 char name[64];
67 fprintf(outfile, "\n");
70 * map xkb keycode -> QKeyCode
72 * xkb keycode is linux evdev shifted by 8
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;
84 fprintf(outfile, "# evdev %d (0x%x), QKeyCode \"%s\", number 0x%x\n",
85 evdev, evdev,
86 QKeyCode_str(qcode),
87 qcode_to_number(qcode));
90 * check which modifier states generate which keysyms
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, "");
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");
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");
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");
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");
119 return;
122 static void usage(FILE *out)
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 ?: "-");
143 static xkb_mod_mask_t get_mod(struct xkb_keymap *map, const char *name)
145 xkb_mod_index_t mod;
146 xkb_mod_mask_t mask = 0;
148 mod = xkb_keymap_mod_get_index(map, name);
149 if (mod != XKB_MOD_INVALID) {
150 mask = (1 << mod);
152 return mask;
155 int main(int argc, char *argv[])
157 struct xkb_context *ctx;
158 struct xkb_keymap *map;
159 struct xkb_state *state;
160 xkb_mod_index_t mod, mods;
161 int rc;
163 for (;;) {
164 rc = getopt(argc, argv, "hm:l:v:o:f:");
165 if (rc == -1) {
166 break;
168 switch (rc) {
169 case 'm':
170 names.model = optarg;
171 break;
172 case 'l':
173 names.layout = optarg;
174 break;
175 case 'v':
176 names.variant = optarg;
177 break;
178 case 'o':
179 names.options = optarg;
180 break;
181 case 'f':
182 outfile = fopen(optarg, "w");
183 if (outfile == NULL) {
184 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
185 exit(1);
187 break;
188 case 'h':
189 usage(stdout);
190 exit(0);
191 default:
192 usage(stderr);
193 exit(1);
197 if (outfile == NULL) {
198 outfile = stdout;
201 fprintf(outfile,
202 "# SPDX-License-Identifier: GPL-2.0-or-later\n"
203 "#\n"
204 "# generated by qemu-keymap\n"
205 "# model : %s\n"
206 "# layout : %s\n"
207 "# variant : %s\n"
208 "# options : %s\n"
209 "\n",
210 names.model, names.layout,
211 names.variant ?: "-",
212 names.options ?: "-");
214 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
215 map = xkb_keymap_new_from_names(ctx, &names, XKB_KEYMAP_COMPILE_NO_FLAGS);
216 if (!map) {
217 /* libxkbcommon prints error */
218 exit(1);
221 fprintf(outfile, "# name: \"%s\"\n\n",
222 xkb_keymap_layout_get_name(map, 0));
223 fprintf(outfile, "# modifiers\n");
224 mods = xkb_keymap_num_mods(map);
225 for (mod = 0; mod < mods; mod++) {
226 fprintf(outfile, "# %2d: %s\n",
227 mod, xkb_keymap_mod_get_name(map, mod));
230 shift = get_mod(map, "Shift");
231 ctrl = get_mod(map, "Control");
232 altgr = get_mod(map, "AltGr");
233 numlock = get_mod(map, "NumLock");
235 state = xkb_state_new(map);
236 xkb_keymap_key_for_each(map, walk_map, state);
237 xkb_state_unref(state);
238 state = NULL;
240 /* add quirks */
241 fprintf(outfile,
242 "\n"
243 "#\n"
244 "# quirks section start\n"
245 "#\n"
246 "# Sometimes multiple keysyms map to the same keycodes.\n"
247 "# The keycode -> keysym lookup finds only one of the\n"
248 "# keysyms. So append them here.\n"
249 "#\n"
250 "\n");
251 print_sym(XKB_KEY_Print, Q_KEY_CODE_SYSRQ, "");
252 print_sym(XKB_KEY_Sys_Req, Q_KEY_CODE_SYSRQ, "");
253 print_sym(XKB_KEY_Execute, Q_KEY_CODE_SYSRQ, "");
255 print_sym(XKB_KEY_KP_Decimal, Q_KEY_CODE_KP_DECIMAL, " numlock");
256 print_sym(XKB_KEY_KP_Separator, Q_KEY_CODE_KP_DECIMAL, " numlock");
258 print_sym(XKB_KEY_Alt_R, Q_KEY_CODE_ALT_R, "");
259 print_sym(XKB_KEY_ISO_Level3_Shift, Q_KEY_CODE_ALT_R, "");
260 print_sym(XKB_KEY_Mode_switch, Q_KEY_CODE_ALT_R, "");
262 fprintf(outfile,
263 "\n"
264 "# quirks section end\n");
266 exit(0);