@samitouri / QOSamiQemu / commits / 2d6dc7218e

hw/input/ps2: 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-13-7c9e4c7abe34@rsg.ci.i.u-tokyo.ac.jp>

Akihiko Odaki committed May 20, 2026 at 15:47 UTC 2d6dc7218e006b47a4f44970945542665f81744a
2 files changed +26 -27
hw/input/ps2.c
+25 -26
@@ -28,6 +28,7 @@
28 #include "hw/core/sysbus.h"
29 #include "hw/input/ps2.h"
30 #include "migration/vmstate.h"
31 +#include "standard-headers/linux/input-event-codes.h"
32 #include "ui/console.h"
33 #include "ui/input.h"
34 #include "system/reset.h"
@@ -123,20 +124,20 @@ static uint8_t translate_table[256] = {
124 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
125 };
126
126 -static unsigned int ps2_modifier_bit(QKeyCode key)
127 +static unsigned int ps2_modifier_bit(unsigned int key)
128 {
129 switch (key) {
129 - case Q_KEY_CODE_CTRL:
130 + case KEY_LEFTCTRL:
131 return MOD_CTRL_L;
131 - case Q_KEY_CODE_CTRL_R:
132 + case KEY_RIGHTCTRL:
133 return MOD_CTRL_R;
133 - case Q_KEY_CODE_SHIFT:
134 + case KEY_LEFTSHIFT:
135 return MOD_SHIFT_L;
135 - case Q_KEY_CODE_SHIFT_R:
136 + case KEY_RIGHTSHIFT:
137 return MOD_SHIFT_R;
137 - case Q_KEY_CODE_ALT:
138 + case KEY_LEFTALT:
139 return MOD_ALT_L;
139 - case Q_KEY_CODE_ALT_R:
140 + case KEY_RIGHTALT:
141 return MOD_ALT_R;
142 default:
143 return 0;
@@ -313,7 +314,6 @@ static void ps2_keyboard_event(DeviceState *dev, QemuConsole *src,
314 QemuInputEvent *evt)
315 {
316 PS2KbdState *s = (PS2KbdState *)dev;
316 - int qcode;
317 uint16_t keycode = 0;
318 int mod;
319
@@ -324,10 +324,9 @@ static void ps2_keyboard_event(DeviceState *dev, QemuConsole *src,
324
325 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
326 assert(evt->type == INPUT_EVENT_KIND_KEY);
327 - qcode = qemu_input_linux_to_qcode(evt->key.key);
327
329 - mod = ps2_modifier_bit(qcode);
330 - trace_ps2_keyboard_event(s, qcode, evt->key.down, mod,
328 + mod = ps2_modifier_bit(evt->key.key);
329 + trace_ps2_keyboard_event(s, evt->key.key, evt->key.down, mod,
330 s->modifiers, s->scancode_set, s->translate);
331 if (evt->key.down) {
332 s->modifiers |= mod;
@@ -336,7 +335,7 @@ static void ps2_keyboard_event(DeviceState *dev, QemuConsole *src,
335 }
336
337 if (s->scancode_set == 1) {
339 - if (qcode == Q_KEY_CODE_PAUSE) {
338 + if (evt->key.key == KEY_PAUSE) {
339 if (s->modifiers & (MOD_CTRL_L | MOD_CTRL_R)) {
340 if (evt->key.down) {
341 ps2_put_keycode(s, 0xe0);
@@ -354,7 +353,7 @@ static void ps2_keyboard_event(DeviceState *dev, QemuConsole *src,
353 ps2_put_keycode(s, 0xc5);
354 }
355 }
357 - } else if (qcode == Q_KEY_CODE_PRINT) {
356 + } else if (evt->key.key == KEY_SYSRQ) {
357 if (s->modifiers & MOD_ALT_L) {
358 if (evt->key.down) {
359 ps2_put_keycode(s, 0xb8);
@@ -401,12 +400,12 @@ static void ps2_keyboard_event(DeviceState *dev, QemuConsole *src,
400 ps2_put_keycode(s, 0xaa);
401 }
402 }
404 - } else if ((qcode == Q_KEY_CODE_LANG1 || qcode == Q_KEY_CODE_LANG2)
403 + } else if ((evt->key.key == KEY_HANGEUL || evt->key.key == KEY_HANJA)
404 && !evt->key.down) {
405 /* Ignore release for these keys */
406 } else {
408 - if (qcode < qemu_input_map_qcode_to_atset1_len) {
409 - keycode = qemu_input_map_qcode_to_atset1[qcode];
407 + if (evt->key.key < qemu_input_map_linux_to_atset1_len) {
408 + keycode = qemu_input_map_linux_to_atset1[evt->key.key];
409 }
410 if (keycode) {
411 if (keycode & 0xff00) {
@@ -418,11 +417,11 @@ static void ps2_keyboard_event(DeviceState *dev, QemuConsole *src,
417 ps2_put_keycode(s, keycode & 0xff);
418 } else {
419 qemu_log_mask(LOG_UNIMP,
421 - "ps2: ignoring key with qcode %d\n", qcode);
420 + "ps2: ignoring key %u\n", evt->key.key);
421 }
422 }
423 } else if (s->scancode_set == 2) {
425 - if (qcode == Q_KEY_CODE_PAUSE) {
424 + if (evt->key.key == KEY_PAUSE) {
425 if (s->modifiers & (MOD_CTRL_L | MOD_CTRL_R)) {
426 if (evt->key.down) {
427 ps2_put_keycode(s, 0xe0);
@@ -443,7 +442,7 @@ static void ps2_keyboard_event(DeviceState *dev, QemuConsole *src,
442 ps2_put_keycode(s, 0x77);
443 }
444 }
446 - } else if (qcode == Q_KEY_CODE_PRINT) {
445 + } else if (evt->key.key == KEY_SYSRQ) {
446 if (s->modifiers & MOD_ALT_L) {
447 if (evt->key.down) {
448 ps2_put_keycode(s, 0xf0);
@@ -499,12 +498,12 @@ static void ps2_keyboard_event(DeviceState *dev, QemuConsole *src,
498 ps2_put_keycode(s, 0x12);
499 }
500 }
502 - } else if ((qcode == Q_KEY_CODE_LANG1 || qcode == Q_KEY_CODE_LANG2) &&
501 + } else if ((evt->key.key == KEY_HANGEUL || evt->key.key == KEY_HANJA) &&
502 !evt->key.down) {
503 /* Ignore release for these keys */
504 } else {
506 - if (qcode < qemu_input_map_qcode_to_atset2_len) {
507 - keycode = qemu_input_map_qcode_to_atset2[qcode];
505 + if (evt->key.key < qemu_input_map_linux_to_atset2_len) {
506 + keycode = qemu_input_map_linux_to_atset2[evt->key.key];
507 }
508 if (keycode) {
509 if (keycode & 0xff00) {
@@ -516,12 +515,12 @@ static void ps2_keyboard_event(DeviceState *dev, QemuConsole *src,
515 ps2_put_keycode(s, keycode & 0xff);
516 } else {
517 qemu_log_mask(LOG_UNIMP,
519 - "ps2: ignoring key with qcode %d\n", qcode);
518 + "ps2: ignoring key %u\n", evt->key.key);
519 }
520 }
521 } else if (s->scancode_set == 3) {
523 - if (qcode < qemu_input_map_qcode_to_atset3_len) {
524 - keycode = qemu_input_map_qcode_to_atset3[qcode];
522 + if (evt->key.key < qemu_input_map_linux_to_atset3_len) {
523 + keycode = qemu_input_map_linux_to_atset3[evt->key.key];
524 }
525 if (keycode) {
526 /* FIXME: break code should be configured on a key by key basis */
@@ -531,7 +530,7 @@ static void ps2_keyboard_event(DeviceState *dev, QemuConsole *src,
530 ps2_put_keycode(s, keycode);
531 } else {
532 qemu_log_mask(LOG_UNIMP,
534 - "ps2: ignoring key with qcode %d\n", qcode);
533 + "ps2: ignoring key %u\n", evt->key.key);
534 }
535 }
536 }
hw/input/trace-events
+1 -1
@@ -30,7 +30,7 @@ pckbd_kbd_write_data(uint64_t val) "0x%02"PRIx64
30
31 # ps2.c
32 ps2_put_keycode(void *opaque, int keycode) "%p keycode 0x%02x"
33 -ps2_keyboard_event(void *opaque, int qcode, int down, unsigned int modifier, unsigned int modifiers, int set, int xlate) "%p qcode %d down %d modifier 0x%x modifiers 0x%x set %d xlate %d"
33 +ps2_keyboard_event(void *opaque, unsigned int lnx, int down, unsigned int modifier, unsigned int modifiers, int set, int xlate) "%p lnx %u down %d modifier 0x%x modifiers 0x%x set %d xlate %d"
34 ps2_read_data(void *opaque) "%p"
35 ps2_set_ledstate(void *s, int ledstate) "%p ledstate %d"
36 ps2_reset_keyboard(void *s) "%p"