1 #include "qemu/osdep.h"
2 #include "sysemu/sysemu.h"
3 #include "qapi/error.h"
4 #include "qapi/qapi-commands-ui.h"
7 #include "ui/console.h"
8 #include "sysemu/replay.h"
9 #include "sysemu/runstate.h"
11 struct QemuInputHandlerState
{
13 QemuInputHandler
*handler
;
17 QTAILQ_ENTRY(QemuInputHandlerState
) node
;
20 typedef struct QemuInputEventQueue QemuInputEventQueue
;
21 typedef QTAILQ_HEAD(QemuInputEventQueueHead
, QemuInputEventQueue
)
22 QemuInputEventQueueHead
;
24 struct QemuInputEventQueue
{
26 QEMU_INPUT_QUEUE_DELAY
= 1,
27 QEMU_INPUT_QUEUE_EVENT
,
28 QEMU_INPUT_QUEUE_SYNC
,
34 QTAILQ_ENTRY(QemuInputEventQueue
) node
;
37 static QTAILQ_HEAD(, QemuInputHandlerState
) handlers
=
38 QTAILQ_HEAD_INITIALIZER(handlers
);
39 static NotifierList mouse_mode_notifiers
=
40 NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers
);
42 static QemuInputEventQueueHead kbd_queue
= QTAILQ_HEAD_INITIALIZER(kbd_queue
);
43 static QEMUTimer
*kbd_timer
;
44 static uint32_t kbd_default_delay_ms
= 10;
45 static uint32_t queue_count
;
46 static uint32_t queue_limit
= 1024;
48 QemuInputHandlerState
*qemu_input_handler_register(DeviceState
*dev
,
49 QemuInputHandler
*handler
)
51 QemuInputHandlerState
*s
= g_new0(QemuInputHandlerState
, 1);
57 QTAILQ_INSERT_TAIL(&handlers
, s
, node
);
59 qemu_input_check_mode_change();
63 void qemu_input_handler_activate(QemuInputHandlerState
*s
)
65 QTAILQ_REMOVE(&handlers
, s
, node
);
66 QTAILQ_INSERT_HEAD(&handlers
, s
, node
);
67 qemu_input_check_mode_change();
70 void qemu_input_handler_deactivate(QemuInputHandlerState
*s
)
72 QTAILQ_REMOVE(&handlers
, s
, node
);
73 QTAILQ_INSERT_TAIL(&handlers
, s
, node
);
74 qemu_input_check_mode_change();
77 void qemu_input_handler_unregister(QemuInputHandlerState
*s
)
79 QTAILQ_REMOVE(&handlers
, s
, node
);
81 qemu_input_check_mode_change();
84 void qemu_input_handler_bind(QemuInputHandlerState
*s
,
85 const char *device_id
, int head
,
91 con
= qemu_console_lookup_by_device_name(device_id
, head
, &err
);
93 error_propagate(errp
, err
);
100 static QemuInputHandlerState
*
101 qemu_input_find_handler(uint32_t mask
, QemuConsole
*con
)
103 QemuInputHandlerState
*s
;
105 QTAILQ_FOREACH(s
, &handlers
, node
) {
106 if (s
->con
== NULL
|| s
->con
!= con
) {
109 if (mask
& s
->handler
->mask
) {
114 QTAILQ_FOREACH(s
, &handlers
, node
) {
115 if (s
->con
!= NULL
) {
118 if (mask
& s
->handler
->mask
) {
125 void qmp_input_send_event(const char *device
,
126 bool has_head
, int64_t head
,
127 InputEventList
*events
, Error
**errp
)
138 con
= qemu_console_lookup_by_device_name(device
, head
, &err
);
140 error_propagate(errp
, err
);
145 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
146 error_setg(errp
, "VM not running");
150 for (e
= events
; e
!= NULL
; e
= e
->next
) {
151 InputEvent
*event
= e
->value
;
153 if (!qemu_input_find_handler(1 << event
->type
, con
)) {
154 error_setg(errp
, "Input handler not found for "
156 InputEventKind_str(event
->type
));
161 for (e
= events
; e
!= NULL
; e
= e
->next
) {
162 InputEvent
*evt
= e
->value
;
164 if (evt
->type
== INPUT_EVENT_KIND_KEY
&&
165 evt
->u
.key
.data
->key
->type
== KEY_VALUE_KIND_NUMBER
) {
166 KeyValue
*key
= evt
->u
.key
.data
->key
;
167 QKeyCode code
= qemu_input_key_number_to_qcode(key
->u
.number
.data
);
168 qemu_input_event_send_key_qcode(con
, code
, evt
->u
.key
.data
->down
);
170 qemu_input_event_send(con
, evt
);
174 qemu_input_event_sync();
177 static int qemu_input_transform_invert_abs_value(int value
)
179 return (int64_t)INPUT_EVENT_ABS_MAX
- value
+ INPUT_EVENT_ABS_MIN
;
182 static void qemu_input_transform_abs_rotate(InputEvent
*evt
)
184 InputMoveEvent
*move
= evt
->u
.abs
.data
;
185 switch (graphic_rotate
) {
187 if (move
->axis
== INPUT_AXIS_X
) {
188 move
->axis
= INPUT_AXIS_Y
;
189 } else if (move
->axis
== INPUT_AXIS_Y
) {
190 move
->axis
= INPUT_AXIS_X
;
191 move
->value
= qemu_input_transform_invert_abs_value(move
->value
);
195 move
->value
= qemu_input_transform_invert_abs_value(move
->value
);
198 if (move
->axis
== INPUT_AXIS_X
) {
199 move
->axis
= INPUT_AXIS_Y
;
200 move
->value
= qemu_input_transform_invert_abs_value(move
->value
);
201 } else if (move
->axis
== INPUT_AXIS_Y
) {
202 move
->axis
= INPUT_AXIS_X
;
208 static void qemu_input_event_trace(QemuConsole
*src
, InputEvent
*evt
)
214 InputMoveEvent
*move
;
217 idx
= qemu_console_get_index(src
);
220 case INPUT_EVENT_KIND_KEY
:
221 key
= evt
->u
.key
.data
;
222 switch (key
->key
->type
) {
223 case KEY_VALUE_KIND_NUMBER
:
224 qcode
= qemu_input_key_number_to_qcode(key
->key
->u
.number
.data
);
225 name
= QKeyCode_str(qcode
);
226 trace_input_event_key_number(idx
, key
->key
->u
.number
.data
,
229 case KEY_VALUE_KIND_QCODE
:
230 name
= QKeyCode_str(key
->key
->u
.qcode
.data
);
231 trace_input_event_key_qcode(idx
, name
, key
->down
);
233 case KEY_VALUE_KIND__MAX
:
238 case INPUT_EVENT_KIND_BTN
:
239 btn
= evt
->u
.btn
.data
;
240 name
= InputButton_str(btn
->button
);
241 trace_input_event_btn(idx
, name
, btn
->down
);
243 case INPUT_EVENT_KIND_REL
:
244 move
= evt
->u
.rel
.data
;
245 name
= InputAxis_str(move
->axis
);
246 trace_input_event_rel(idx
, name
, move
->value
);
248 case INPUT_EVENT_KIND_ABS
:
249 move
= evt
->u
.abs
.data
;
250 name
= InputAxis_str(move
->axis
);
251 trace_input_event_abs(idx
, name
, move
->value
);
253 case INPUT_EVENT_KIND__MAX
:
259 static void qemu_input_queue_process(void *opaque
)
261 QemuInputEventQueueHead
*queue
= opaque
;
262 QemuInputEventQueue
*item
;
264 g_assert(!QTAILQ_EMPTY(queue
));
265 item
= QTAILQ_FIRST(queue
);
266 g_assert(item
->type
== QEMU_INPUT_QUEUE_DELAY
);
267 QTAILQ_REMOVE(queue
, item
, node
);
271 while (!QTAILQ_EMPTY(queue
)) {
272 item
= QTAILQ_FIRST(queue
);
273 switch (item
->type
) {
274 case QEMU_INPUT_QUEUE_DELAY
:
275 timer_mod(item
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
)
278 case QEMU_INPUT_QUEUE_EVENT
:
279 qemu_input_event_send(item
->src
, item
->evt
);
280 qapi_free_InputEvent(item
->evt
);
282 case QEMU_INPUT_QUEUE_SYNC
:
283 qemu_input_event_sync();
286 QTAILQ_REMOVE(queue
, item
, node
);
292 static void qemu_input_queue_delay(QemuInputEventQueueHead
*queue
,
293 QEMUTimer
*timer
, uint32_t delay_ms
)
295 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
296 bool start_timer
= QTAILQ_EMPTY(queue
);
298 item
->type
= QEMU_INPUT_QUEUE_DELAY
;
299 item
->delay_ms
= delay_ms
;
301 QTAILQ_INSERT_TAIL(queue
, item
, node
);
305 timer_mod(item
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
)
310 static void qemu_input_queue_event(QemuInputEventQueueHead
*queue
,
311 QemuConsole
*src
, InputEvent
*evt
)
313 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
315 item
->type
= QEMU_INPUT_QUEUE_EVENT
;
318 QTAILQ_INSERT_TAIL(queue
, item
, node
);
322 static void qemu_input_queue_sync(QemuInputEventQueueHead
*queue
)
324 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
326 item
->type
= QEMU_INPUT_QUEUE_SYNC
;
327 QTAILQ_INSERT_TAIL(queue
, item
, node
);
331 void qemu_input_event_send_impl(QemuConsole
*src
, InputEvent
*evt
)
333 QemuInputHandlerState
*s
;
335 qemu_input_event_trace(src
, evt
);
338 if (graphic_rotate
&& (evt
->type
== INPUT_EVENT_KIND_ABS
)) {
339 qemu_input_transform_abs_rotate(evt
);
343 s
= qemu_input_find_handler(1 << evt
->type
, src
);
347 s
->handler
->event(s
->dev
, src
, evt
);
351 void qemu_input_event_send(QemuConsole
*src
, InputEvent
*evt
)
353 /* Expect all parts of QEMU to send events with QCodes exclusively.
354 * Key numbers are only supported as end-user input via QMP */
355 assert(!(evt
->type
== INPUT_EVENT_KIND_KEY
&&
356 evt
->u
.key
.data
->key
->type
== KEY_VALUE_KIND_NUMBER
));
360 * 'sysrq' was mistakenly added to hack around the fact that
361 * the ps2 driver was not generating correct scancodes sequences
362 * when 'alt+print' was pressed. This flaw is now fixed and the
363 * 'sysrq' key serves no further purpose. We normalize it to
364 * 'print', so that downstream receivers of the event don't
365 * need to deal with this mistake
367 if (evt
->type
== INPUT_EVENT_KIND_KEY
&&
368 evt
->u
.key
.data
->key
->u
.qcode
.data
== Q_KEY_CODE_SYSRQ
) {
369 evt
->u
.key
.data
->key
->u
.qcode
.data
= Q_KEY_CODE_PRINT
;
372 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
376 replay_input_event(src
, evt
);
379 void qemu_input_event_sync_impl(void)
381 QemuInputHandlerState
*s
;
383 trace_input_event_sync();
385 QTAILQ_FOREACH(s
, &handlers
, node
) {
389 if (s
->handler
->sync
) {
390 s
->handler
->sync(s
->dev
);
396 void qemu_input_event_sync(void)
398 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
402 replay_input_sync_event();
405 static InputEvent
*qemu_input_event_new_key(KeyValue
*key
, bool down
)
407 InputEvent
*evt
= g_new0(InputEvent
, 1);
408 evt
->u
.key
.data
= g_new0(InputKeyEvent
, 1);
409 evt
->type
= INPUT_EVENT_KIND_KEY
;
410 evt
->u
.key
.data
->key
= key
;
411 evt
->u
.key
.data
->down
= down
;
415 void qemu_input_event_send_key(QemuConsole
*src
, KeyValue
*key
, bool down
)
418 evt
= qemu_input_event_new_key(key
, down
);
419 if (QTAILQ_EMPTY(&kbd_queue
)) {
420 qemu_input_event_send(src
, evt
);
421 qemu_input_event_sync();
422 qapi_free_InputEvent(evt
);
423 } else if (queue_count
< queue_limit
) {
424 qemu_input_queue_event(&kbd_queue
, src
, evt
);
425 qemu_input_queue_sync(&kbd_queue
);
427 qapi_free_InputEvent(evt
);
431 void qemu_input_event_send_key_number(QemuConsole
*src
, int num
, bool down
)
433 QKeyCode code
= qemu_input_key_number_to_qcode(num
);
434 qemu_input_event_send_key_qcode(src
, code
, down
);
437 void qemu_input_event_send_key_qcode(QemuConsole
*src
, QKeyCode q
, bool down
)
439 KeyValue
*key
= g_new0(KeyValue
, 1);
440 key
->type
= KEY_VALUE_KIND_QCODE
;
441 key
->u
.qcode
.data
= q
;
442 qemu_input_event_send_key(src
, key
, down
);
445 void qemu_input_event_send_key_delay(uint32_t delay_ms
)
447 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
452 kbd_timer
= timer_new_full(NULL
, QEMU_CLOCK_VIRTUAL
,
453 SCALE_MS
, QEMU_TIMER_ATTR_EXTERNAL
,
454 qemu_input_queue_process
, &kbd_queue
);
456 if (queue_count
< queue_limit
) {
457 qemu_input_queue_delay(&kbd_queue
, kbd_timer
,
458 delay_ms
? delay_ms
: kbd_default_delay_ms
);
462 void qemu_input_queue_btn(QemuConsole
*src
, InputButton btn
, bool down
)
464 InputBtnEvent bevt
= {
469 .type
= INPUT_EVENT_KIND_BTN
,
473 qemu_input_event_send(src
, &evt
);
476 void qemu_input_update_buttons(QemuConsole
*src
, uint32_t *button_map
,
477 uint32_t button_old
, uint32_t button_new
)
482 for (btn
= 0; btn
< INPUT_BUTTON__MAX
; btn
++) {
483 mask
= button_map
[btn
];
484 if ((button_old
& mask
) == (button_new
& mask
)) {
487 qemu_input_queue_btn(src
, btn
, button_new
& mask
);
491 bool qemu_input_is_absolute(void)
493 QemuInputHandlerState
*s
;
495 s
= qemu_input_find_handler(INPUT_EVENT_MASK_REL
| INPUT_EVENT_MASK_ABS
,
497 return (s
!= NULL
) && (s
->handler
->mask
& INPUT_EVENT_MASK_ABS
);
500 int qemu_input_scale_axis(int value
,
501 int min_in
, int max_in
,
502 int min_out
, int max_out
)
504 int64_t range_in
= (int64_t)max_in
- min_in
;
505 int64_t range_out
= (int64_t)max_out
- min_out
;
508 return min_out
+ range_out
/ 2;
510 return ((int64_t)value
- min_in
) * range_out
/ range_in
+ min_out
;
513 void qemu_input_queue_rel(QemuConsole
*src
, InputAxis axis
, int value
)
515 InputMoveEvent move
= {
520 .type
= INPUT_EVENT_KIND_REL
,
524 qemu_input_event_send(src
, &evt
);
527 void qemu_input_queue_abs(QemuConsole
*src
, InputAxis axis
, int value
,
528 int min_in
, int max_in
)
530 InputMoveEvent move
= {
532 .value
= qemu_input_scale_axis(value
, min_in
, max_in
,
534 INPUT_EVENT_ABS_MAX
),
537 .type
= INPUT_EVENT_KIND_ABS
,
541 qemu_input_event_send(src
, &evt
);
544 void qemu_input_check_mode_change(void)
546 static int current_is_absolute
;
549 is_absolute
= qemu_input_is_absolute();
551 if (is_absolute
!= current_is_absolute
) {
552 trace_input_mouse_mode(is_absolute
);
553 notifier_list_notify(&mouse_mode_notifiers
, NULL
);
556 current_is_absolute
= is_absolute
;
559 void qemu_add_mouse_mode_change_notifier(Notifier
*notify
)
561 notifier_list_add(&mouse_mode_notifiers
, notify
);
564 void qemu_remove_mouse_mode_change_notifier(Notifier
*notify
)
566 notifier_remove(notify
);
569 MouseInfoList
*qmp_query_mice(Error
**errp
)
571 MouseInfoList
*mice_list
= NULL
;
573 QemuInputHandlerState
*s
;
576 QTAILQ_FOREACH(s
, &handlers
, node
) {
577 if (!(s
->handler
->mask
&
578 (INPUT_EVENT_MASK_REL
| INPUT_EVENT_MASK_ABS
))) {
582 info
= g_new0(MouseInfo
, 1);
584 info
->name
= g_strdup(s
->handler
->name
);
585 info
->absolute
= s
->handler
->mask
& INPUT_EVENT_MASK_ABS
;
586 info
->current
= current
;
589 QAPI_LIST_PREPEND(mice_list
, info
);
595 bool qemu_mouse_set(int index
, Error
**errp
)
597 QemuInputHandlerState
*s
;
599 QTAILQ_FOREACH(s
, &handlers
, node
) {
600 if (s
->id
== index
) {
606 error_setg(errp
, "Mouse at index '%d' not found", index
);
610 if (!(s
->handler
->mask
& (INPUT_EVENT_MASK_REL
|
611 INPUT_EVENT_MASK_ABS
))) {
612 error_setg(errp
, "Input device '%s' is not a mouse",
617 qemu_input_handler_activate(s
);
618 qemu_input_check_mode_change();