hw/input/virtio-input: Use Linux key codes
QemuInputEvent now stores Linux key codes for key events. Use those codes directly instead of translating between internal key code representations. Signed-off-by: Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Message-ID: <20260520-input-v3-14-7c9e4c7abe34@rsg.ci.i.u-tokyo.ac.jp>
Akihiko Odaki committed
May 20, 2026 at 15:47 UTC
d5e8b698ec5c8a0043bee2ad9daffc532a6e652d
1 file changed
+23
-17
hw/input/virtio-input-hid.c
+23
-17
@@ -83,23 +83,13 @@ static void virtio_input_handle_event(DeviceState *dev, QemuConsole *src,
83
{
84
VirtIOInput *vinput = VIRTIO_INPUT(dev);
85
virtio_input_event event;
86
- int qcode;
86
87
switch (evt->type) {
88
case INPUT_EVENT_KIND_KEY:
90
- qcode = qemu_input_linux_to_qcode(evt->key.key);
91
- if (qcode < qemu_input_map_qcode_to_linux_len &&
92
- qemu_input_map_qcode_to_linux[qcode]) {
93
- event.type = cpu_to_le16(EV_KEY);
94
- event.code = cpu_to_le16(qemu_input_map_qcode_to_linux[qcode]);
95
- event.value = cpu_to_le32(evt->key.down ? 1 : 0);
96
- virtio_input_send(vinput, &event);
97
- } else {
98
- if (evt->key.down) {
99
- fprintf(stderr, "%s: unmapped key: %d [%s]\n", __func__,
100
- qcode, QKeyCode_str(qcode));
101
- }
102
- }
89
+ event.type = cpu_to_le16(EV_KEY);
90
+ event.code = cpu_to_le16(evt->key.key);
91
+ event.value = cpu_to_le32(evt->key.down ? 1 : 0);
92
+ virtio_input_send(vinput, &event);
93
break;
94
case INPUT_EVENT_KIND_BTN:
95
if ((evt->btn.button == INPUT_BUTTON_WHEEL_UP ||
@@ -293,12 +283,28 @@ static void virtio_keyboard_init(Object *obj)
283
{
284
VirtIOInputHID *vhid = VIRTIO_INPUT_HID(obj);
285
VirtIOInput *vinput = VIRTIO_INPUT(obj);
286
+ virtio_input_config ext = {
287
+ .select = VIRTIO_INPUT_CFG_EV_BITS,
288
+ .subsel = EV_KEY,
289
+ .size = DIV_ROUND_UP(KEY_REPLY, 8)
290
+ };
291
+ unsigned int i;
292
293
vhid->handler = &virtio_keyboard_handler;
294
virtio_input_init_config(vinput, virtio_keyboard_config);
299
- virtio_input_extend_config(vinput, qemu_input_map_qcode_to_linux,
300
- qemu_input_map_qcode_to_linux_len,
301
- VIRTIO_INPUT_CFG_EV_BITS, EV_KEY);
295
+
296
+ /*
297
+ * Cover as many keys as possible since we cannot tell what keys the host
298
+ * supports. This follows Linux xen-kbdfront's approach:
299
+ * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/diff/drivers/input/xen-kbdfront.c?id=4ee36dc08e5c4d16d078f59acd6d9d536f9718dd
300
+ *
301
+ * Stop before KEY_REPLY for migration compatibility.
302
+ */
303
+ for (i = KEY_ESC; i < KEY_REPLY; i++) {
304
+ ext.u.bitmap[i / 8] |= (1 << (i % 8));
305
+ }
306
+
307
+ virtio_input_add_config(vinput, &ext);
308
}
309
310
static const TypeInfo virtio_keyboard_info = {