@samitouri / QOSamiQemu / commits / 56729b7aaf

ui/input: Store QKeyCode directly in QemuInputKeyEvent

Since commit af07e5ff02ae ("ui: convert key events to QKeyCodes immediately"), all internal key events are expected to be represented as QKeyCode. Replace KeyValue in QemuInputKeyEvent with QKeyCode to enforce that and simplify key code retrieval. 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-3-7c9e4c7abe34@rsg.ci.i.u-tokyo.ac.jp>

Akihiko Odaki committed May 20, 2026 at 15:47 UTC 56729b7aaf7252c0843d106be50c18243c17f98a
14 files changed +29 -70
hw/arm/musicpal.c
+1 -1
@@ -1069,7 +1069,7 @@ static void musicpal_key_event(DeviceState *dev, QemuConsole *src,
1069 QemuInputEvent *evt)
1070 {
1071 musicpal_key_state *s = MUSICPAL_KEY(dev);
1072 - int qcode = qemu_input_key_value_to_qcode(&evt->key.key);
1072 + int qcode = evt->key.key;
1073 uint32_t event = 0;
1074 int i;
1075
hw/char/escc.c
+1 -1
@@ -800,7 +800,7 @@ static void sunkbd_handle_event(DeviceState *dev, QemuConsole *src,
800 int qcode, keycode;
801
802 assert(evt->type == INPUT_EVENT_KIND_KEY);
803 - qcode = qemu_input_key_value_to_qcode(&evt->key.key);
803 + qcode = evt->key.key;
804 trace_escc_sunkbd_event_in(qcode, QKeyCode_str(qcode),
805 evt->key.down);
806
hw/display/xenfb.c
+1 -1
@@ -203,7 +203,7 @@ static void xenfb_key_event(DeviceState *dev, QemuConsole *src,
203 QemuInputEvent *evt)
204 {
205 struct XenInput *xenfb = (struct XenInput *)dev;
206 - int qcode = qemu_input_key_value_to_qcode(&evt->key.key);
206 + int qcode = evt->key.key;
207 int lnx;
208
209 if (qcode < qemu_input_map_qcode_to_linux_len) {
hw/input/adb-kbd.c
+1 -1
@@ -311,7 +311,7 @@ static void adb_keyboard_event(DeviceState *dev, QemuConsole *src,
311 KBDState *s = (KBDState *)dev;
312 int qcode, keycode;
313
314 - qcode = qemu_input_key_value_to_qcode(&evt->key.key);
314 + qcode = evt->key.key;
315 if (qcode >= ARRAY_SIZE(qcode_to_adb_keycode)) {
316 return;
317 }
hw/input/hid.c
+2 -3
@@ -227,9 +227,8 @@ static void hid_keyboard_event(DeviceState *dev, QemuConsole *src,
227 int scancodes[3], i, count;
228 int slot;
229
230 - count = qemu_input_key_value_to_scancode(&evt->key.key,
231 - evt->key.down,
232 - scancodes);
230 + count = qemu_input_qcode_to_scancode(evt->key.key, evt->key.down,
231 + scancodes);
232 if (hs->n + count > QUEUE_LENGTH) {
233 trace_hid_kbd_queue_full();
234 return;
hw/input/ps2.c
+1 -1
@@ -324,7 +324,7 @@ 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_key_value_to_qcode(&evt->key.key);
327 + qcode = evt->key.key;
328
329 mod = ps2_modifier_bit(qcode);
330 trace_ps2_keyboard_event(s, qcode, evt->key.down, mod,
hw/input/stellaris_gamepad.c
+1 -1
@@ -19,7 +19,7 @@ static void stellaris_gamepad_event(DeviceState *dev, QemuConsole *src,
19 QemuInputEvent *evt)
20 {
21 StellarisGamepad *s = STELLARIS_GAMEPAD(dev);
22 - int qcode = qemu_input_key_value_to_qcode(&evt->key.key);
22 + int qcode = evt->key.key;
23 int i;
24
25 for (i = 0; i < s->num_buttons; i++) {
hw/input/virtio-input-hid.c
+1 -1
@@ -87,7 +87,7 @@ static void virtio_input_handle_event(DeviceState *dev, QemuConsole *src,
87
88 switch (evt->type) {
89 case INPUT_EVENT_KIND_KEY:
90 - qcode = qemu_input_key_value_to_qcode(&evt->key.key);
90 + 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);
hw/m68k/next-kbd.c
+1 -1
@@ -248,7 +248,7 @@ static void nextkbd_event(DeviceState *dev, QemuConsole *src,
248 NextKBDState *s = NEXTKBD(dev);
249 int qcode, keycode;
250
251 - qcode = qemu_input_key_value_to_qcode(&evt->key.key);
251 + qcode = evt->key.key;
252 if (qcode >= ARRAY_SIZE(qcode_to_nextkbd_keycode)) {
253 return;
254 }
include/ui/input.h
+2 -3
@@ -19,7 +19,7 @@ typedef struct QemuInputHandler QemuInputHandler;
19 typedef struct QemuInputHandlerState QemuInputHandlerState;
20
21 typedef struct QemuInputKeyEvent {
22 - KeyValue key;
22 + QKeyCode key;
23 bool down;
24 } QemuInputKeyEvent;
25
@@ -65,8 +65,7 @@ void qemu_input_event_send_key_delay(uint32_t delay_ms);
65 int qemu_input_key_number_to_qcode(unsigned int nr);
66 int qemu_input_key_value_to_number(const KeyValue *value);
67 int qemu_input_key_value_to_qcode(const KeyValue *value);
68 -int qemu_input_key_value_to_scancode(const KeyValue *value, bool down,
69 - int *codes);
68 +int qemu_input_qcode_to_scancode(QKeyCode qcode, bool down, int *codes);
69 int qemu_input_linux_to_qcode(unsigned int lnx);
70
71 void qemu_input_queue_btn(QemuConsole *src, InputButton btn, bool down);
replay/replay-input.c
+6 -23
@@ -23,21 +23,9 @@ void replay_save_input_event(QemuInputEvent *evt)
23
24 switch (evt->type) {
25 case INPUT_EVENT_KIND_KEY:
26 - replay_put_dword(evt->key.key.type);
27 -
28 - switch (evt->key.key.type) {
29 - case KEY_VALUE_KIND_NUMBER:
30 - replay_put_qword(evt->key.key.u.number.data);
31 - replay_put_byte(evt->key.down);
32 - break;
33 - case KEY_VALUE_KIND_QCODE:
34 - replay_put_dword(evt->key.key.u.qcode.data);
35 - replay_put_byte(evt->key.down);
36 - break;
37 - case KEY_VALUE_KIND__MAX:
38 - /* keep gcc happy */
39 - break;
40 - }
26 + replay_put_dword(KEY_VALUE_KIND_QCODE);
27 + replay_put_dword(evt->key.key);
28 + replay_put_byte(evt->key.down);
29 break;
30 case INPUT_EVENT_KIND_BTN:
31 replay_put_dword(evt->btn.button);
@@ -71,20 +59,15 @@ QemuInputEvent *replay_read_input_event(void)
59 evt->type = replay_get_dword();
60 switch (evt->type) {
61 case INPUT_EVENT_KIND_KEY:
74 - evt->key.key.type = replay_get_dword();
75 -
76 - switch (evt->key.key.type) {
62 + switch (replay_get_dword()) {
63 case KEY_VALUE_KIND_NUMBER:
78 - evt->key.key.u.number.data = replay_get_qword();
64 + evt->key.key = qemu_input_key_number_to_qcode(replay_get_qword());
65 evt->key.down = replay_get_byte();
66 break;
67 case KEY_VALUE_KIND_QCODE:
82 - evt->key.key.u.qcode.data = (QKeyCode)replay_get_dword();
68 + evt->key.key = (QKeyCode)replay_get_dword();
69 evt->key.down = replay_get_byte();
70 break;
85 - case KEY_VALUE_KIND__MAX:
86 - /* keep gcc happy */
87 - break;
71 }
72 break;
73 case INPUT_EVENT_KIND_BTN:
ui/input-keymap.c
+4 -5
@@ -61,14 +61,13 @@ int qemu_input_key_value_to_qcode(const KeyValue *value)
61 }
62 }
63
64 -int qemu_input_key_value_to_scancode(const KeyValue *value, bool down,
65 - int *codes)
64 +int qemu_input_qcode_to_scancode(QKeyCode qcode, bool down, int *codes)
65 {
67 - int keycode = qemu_input_key_value_to_number(value);
66 + int keycode = qcode < qemu_input_map_qcode_to_qnum_len ?
67 + qemu_input_map_qcode_to_qnum[qcode] : 0;
68 int count = 0;
69
70 - if (value->type == KEY_VALUE_KIND_QCODE &&
71 - value->u.qcode.data == Q_KEY_CODE_PAUSE) {
70 + if (qcode == Q_KEY_CODE_PAUSE) {
71 /* specific case */
72 int v = down ? 0 : 0x80;
73 codes[count++] = 0xe1;
ui/input.c
+7 -27
@@ -180,8 +180,7 @@ void qmp_input_send_event(const char *device,
180 g_assert_not_reached();
181 }
182
183 - evt.key.key.type = KEY_VALUE_KIND_QCODE;
184 - evt.key.key.u.qcode.data = code;
183 + evt.key.key = code;
184 evt.key.down = qapi->u.key.data->down;
185 break;
186 }
@@ -215,7 +214,7 @@ void qmp_input_send_event(const char *device,
214 static void qemu_input_event_trace(QemuConsole *src, QemuInputEvent *evt)
215 {
216 const char *name;
218 - int qcode, idx = -1;
217 + int idx = -1;
218 QemuInputKeyEvent *key;
219 InputBtnEvent *btn;
220 InputMoveEvent *move;
@@ -227,21 +226,8 @@ static void qemu_input_event_trace(QemuConsole *src, QemuInputEvent *evt)
226 switch (evt->type) {
227 case INPUT_EVENT_KIND_KEY:
228 key = &evt->key;
230 - switch (evt->key.key.type) {
231 - case KEY_VALUE_KIND_NUMBER:
232 - qcode = qemu_input_key_number_to_qcode(key->key.u.number.data);
233 - name = QKeyCode_str(qcode);
234 - trace_input_event_key_number(idx, key->key.u.number.data,
235 - name, key->down);
236 - break;
237 - case KEY_VALUE_KIND_QCODE:
238 - name = QKeyCode_str(key->key.u.qcode.data);
239 - trace_input_event_key_qcode(idx, name, key->down);
240 - break;
241 - case KEY_VALUE_KIND__MAX:
242 - /* keep gcc happy */
243 - break;
244 - }
229 + name = QKeyCode_str(key->key);
230 + trace_input_event_key_qcode(idx, name, key->down);
231 break;
232 case INPUT_EVENT_KIND_BTN:
233 btn = &evt->btn;
@@ -357,12 +343,6 @@ void qemu_input_event_send_impl(QemuConsole *src, QemuInputEvent *evt)
343
344 void qemu_input_event_send(QemuConsole *src, QemuInputEvent *evt)
345 {
360 - /* Expect all parts of QEMU to send events with QCodes exclusively.
361 - * Key numbers are only supported as end-user input via QMP */
362 - assert(!(evt->type == INPUT_EVENT_KIND_KEY &&
363 - evt->key.key.type == KEY_VALUE_KIND_NUMBER));
364 -
365 -
346 /*
347 * 'sysrq' was mistakenly added to hack around the fact that
348 * the ps2 driver was not generating correct scancodes sequences
@@ -372,8 +352,8 @@ void qemu_input_event_send(QemuConsole *src, QemuInputEvent *evt)
352 * need to deal with this mistake
353 */
354 if (evt->type == INPUT_EVENT_KIND_KEY &&
375 - evt->key.key.u.qcode.data == Q_KEY_CODE_SYSRQ) {
376 - evt->key.key.u.qcode.data = Q_KEY_CODE_PRINT;
355 + evt->key.key == Q_KEY_CODE_SYSRQ) {
356 + evt->key.key = Q_KEY_CODE_PRINT;
357 }
358
359 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
@@ -414,7 +394,7 @@ void qemu_input_event_send_key(QemuConsole *src, KeyValue *key, bool down)
394 QemuInputEvent evt = {
395 .type = INPUT_EVENT_KIND_KEY,
396 .key = {
417 - .key = *key,
397 + .key = qemu_input_key_value_to_qcode(key),
398 .down = down,
399 },
400 };
ui/trace-events
-1
@@ -127,7 +127,6 @@ vnc_tight_zlib_init(void *state, int stream_id, const void *opaque) "VNC tight z
127
128
129 # input.c
130 -input_event_key_number(int conidx, int number, const char *qcode, bool down) "con %d, key number 0x%x [%s], down %d"
130 input_event_key_qcode(int conidx, const char *qcode, bool down) "con %d, key qcode %s, down %d"
131 input_event_btn(int conidx, const char *btn, bool down) "con %d, button %s, down %d"
132 input_event_rel(int conidx, const char *axis, int value) "con %d, axis %s, value %d"