1 #include "qemu/osdep.h"
2 #include "sysemu/sysemu.h"
3 #include "qapi/error.h"
4 #include "qapi/qapi-commands-ui.h"
5 #include "qapi/qmp/qdict.h"
6 #include "qemu/error-report.h"
9 #include "ui/console.h"
10 #include "sysemu/replay.h"
12 struct QemuInputHandlerState
{
14 QemuInputHandler
*handler
;
18 QTAILQ_ENTRY(QemuInputHandlerState
) node
;
21 typedef struct QemuInputEventQueue QemuInputEventQueue
;
22 typedef QTAILQ_HEAD(QemuInputEventQueueHead
, QemuInputEventQueue
)
23 QemuInputEventQueueHead
;
25 struct QemuInputEventQueue
{
27 QEMU_INPUT_QUEUE_DELAY
= 1,
28 QEMU_INPUT_QUEUE_EVENT
,
29 QEMU_INPUT_QUEUE_SYNC
,
35 QTAILQ_ENTRY(QemuInputEventQueue
) node
;
38 static QTAILQ_HEAD(, QemuInputHandlerState
) handlers
=
39 QTAILQ_HEAD_INITIALIZER(handlers
);
40 static NotifierList mouse_mode_notifiers
=
41 NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers
);
43 static QemuInputEventQueueHead kbd_queue
= QTAILQ_HEAD_INITIALIZER(kbd_queue
);
44 static QEMUTimer
*kbd_timer
;
45 static uint32_t kbd_default_delay_ms
= 10;
46 static uint32_t queue_count
;
47 static uint32_t queue_limit
= 1024;
49 QemuInputHandlerState
*qemu_input_handler_register(DeviceState
*dev
,
50 QemuInputHandler
*handler
)
52 QemuInputHandlerState
*s
= g_new0(QemuInputHandlerState
, 1);
58 QTAILQ_INSERT_TAIL(&handlers
, s
, node
);
60 qemu_input_check_mode_change();
64 void qemu_input_handler_activate(QemuInputHandlerState
*s
)
66 QTAILQ_REMOVE(&handlers
, s
, node
);
67 QTAILQ_INSERT_HEAD(&handlers
, s
, node
);
68 qemu_input_check_mode_change();
71 void qemu_input_handler_deactivate(QemuInputHandlerState
*s
)
73 QTAILQ_REMOVE(&handlers
, s
, node
);
74 QTAILQ_INSERT_TAIL(&handlers
, s
, node
);
75 qemu_input_check_mode_change();
78 void qemu_input_handler_unregister(QemuInputHandlerState
*s
)
80 QTAILQ_REMOVE(&handlers
, s
, node
);
82 qemu_input_check_mode_change();
85 void qemu_input_handler_bind(QemuInputHandlerState
*s
,
86 const char *device_id
, int head
,
92 con
= qemu_console_lookup_by_device_name(device_id
, head
, &err
);
94 error_propagate(errp
, err
);
101 static QemuInputHandlerState
*
102 qemu_input_find_handler(uint32_t mask
, QemuConsole
*con
)
104 QemuInputHandlerState
*s
;
106 QTAILQ_FOREACH(s
, &handlers
, node
) {
107 if (s
->con
== NULL
|| s
->con
!= con
) {
110 if (mask
& s
->handler
->mask
) {
115 QTAILQ_FOREACH(s
, &handlers
, node
) {
116 if (s
->con
!= NULL
) {
119 if (mask
& s
->handler
->mask
) {
126 void qmp_input_send_event(bool has_device
, const char *device
,
127 bool has_head
, int64_t head
,
128 InputEventList
*events
, Error
**errp
)
139 con
= qemu_console_lookup_by_device_name(device
, head
, &err
);
141 error_propagate(errp
, err
);
146 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
147 error_setg(errp
, "VM not running");
151 for (e
= events
; e
!= NULL
; e
= e
->next
) {
152 InputEvent
*event
= e
->value
;
154 if (!qemu_input_find_handler(1 << event
->type
, con
)) {
155 error_setg(errp
, "Input handler not found for "
157 InputEventKind_str(event
->type
));
162 for (e
= events
; e
!= NULL
; e
= e
->next
) {
163 InputEvent
*evt
= e
->value
;
165 if (evt
->type
== INPUT_EVENT_KIND_KEY
&&
166 evt
->u
.key
.data
->key
->type
== KEY_VALUE_KIND_NUMBER
) {
167 KeyValue
*key
= evt
->u
.key
.data
->key
;
168 QKeyCode code
= qemu_input_key_number_to_qcode(key
->u
.number
.data
);
169 qemu_input_event_send_key_qcode(con
, code
, evt
->u
.key
.data
->down
);
171 qemu_input_event_send(con
, evt
);
175 qemu_input_event_sync();
178 static int qemu_input_transform_invert_abs_value(int value
)
180 return (int64_t)INPUT_EVENT_ABS_MAX
- value
+ INPUT_EVENT_ABS_MIN
;
183 static void qemu_input_transform_abs_rotate(InputEvent
*evt
)
185 InputMoveEvent
*move
= evt
->u
.abs
.data
;
186 switch (graphic_rotate
) {
188 if (move
->axis
== INPUT_AXIS_X
) {
189 move
->axis
= INPUT_AXIS_Y
;
190 } else if (move
->axis
== INPUT_AXIS_Y
) {
191 move
->axis
= INPUT_AXIS_X
;
192 move
->value
= qemu_input_transform_invert_abs_value(move
->value
);
196 move
->value
= qemu_input_transform_invert_abs_value(move
->value
);
199 if (move
->axis
== INPUT_AXIS_X
) {
200 move
->axis
= INPUT_AXIS_Y
;
201 move
->value
= qemu_input_transform_invert_abs_value(move
->value
);
202 } else if (move
->axis
== INPUT_AXIS_Y
) {
203 move
->axis
= INPUT_AXIS_X
;
209 static void qemu_input_event_trace(QemuConsole
*src
, InputEvent
*evt
)
215 InputMoveEvent
*move
;
218 idx
= qemu_console_get_index(src
);
221 case INPUT_EVENT_KIND_KEY
:
222 key
= evt
->u
.key
.data
;
223 switch (key
->key
->type
) {
224 case KEY_VALUE_KIND_NUMBER
:
225 qcode
= qemu_input_key_number_to_qcode(key
->key
->u
.number
.data
);
226 name
= QKeyCode_str(qcode
);
227 trace_input_event_key_number(idx
, key
->key
->u
.number
.data
,
230 case KEY_VALUE_KIND_QCODE
:
231 name
= QKeyCode_str(key
->key
->u
.qcode
.data
);
232 trace_input_event_key_qcode(idx
, name
, key
->down
);
234 case KEY_VALUE_KIND__MAX
:
239 case INPUT_EVENT_KIND_BTN
:
240 btn
= evt
->u
.btn
.data
;
241 name
= InputButton_str(btn
->button
);
242 trace_input_event_btn(idx
, name
, btn
->down
);
244 case INPUT_EVENT_KIND_REL
:
245 move
= evt
->u
.rel
.data
;
246 name
= InputAxis_str(move
->axis
);
247 trace_input_event_rel(idx
, name
, move
->value
);
249 case INPUT_EVENT_KIND_ABS
:
250 move
= evt
->u
.abs
.data
;
251 name
= InputAxis_str(move
->axis
);
252 trace_input_event_abs(idx
, name
, move
->value
);
254 case INPUT_EVENT_KIND__MAX
:
260 static void qemu_input_queue_process(void *opaque
)
262 QemuInputEventQueueHead
*queue
= opaque
;
263 QemuInputEventQueue
*item
;
265 g_assert(!QTAILQ_EMPTY(queue
));
266 item
= QTAILQ_FIRST(queue
);
267 g_assert(item
->type
== QEMU_INPUT_QUEUE_DELAY
);
268 QTAILQ_REMOVE(queue
, item
, node
);
272 while (!QTAILQ_EMPTY(queue
)) {
273 item
= QTAILQ_FIRST(queue
);
274 switch (item
->type
) {
275 case QEMU_INPUT_QUEUE_DELAY
:
276 timer_mod(item
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
)
279 case QEMU_INPUT_QUEUE_EVENT
:
280 qemu_input_event_send(item
->src
, item
->evt
);
281 qapi_free_InputEvent(item
->evt
);
283 case QEMU_INPUT_QUEUE_SYNC
:
284 qemu_input_event_sync();
287 QTAILQ_REMOVE(queue
, item
, node
);
293 static void qemu_input_queue_delay(QemuInputEventQueueHead
*queue
,
294 QEMUTimer
*timer
, uint32_t delay_ms
)
296 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
297 bool start_timer
= QTAILQ_EMPTY(queue
);
299 item
->type
= QEMU_INPUT_QUEUE_DELAY
;
300 item
->delay_ms
= delay_ms
;
302 QTAILQ_INSERT_TAIL(queue
, item
, node
);
306 timer_mod(item
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
)
311 static void qemu_input_queue_event(QemuInputEventQueueHead
*queue
,
312 QemuConsole
*src
, InputEvent
*evt
)
314 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
316 item
->type
= QEMU_INPUT_QUEUE_EVENT
;
319 QTAILQ_INSERT_TAIL(queue
, item
, node
);
323 static void qemu_input_queue_sync(QemuInputEventQueueHead
*queue
)
325 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
327 item
->type
= QEMU_INPUT_QUEUE_SYNC
;
328 QTAILQ_INSERT_TAIL(queue
, item
, node
);
332 void qemu_input_event_send_impl(QemuConsole
*src
, InputEvent
*evt
)
334 QemuInputHandlerState
*s
;
336 qemu_input_event_trace(src
, evt
);
339 if (graphic_rotate
&& (evt
->type
== INPUT_EVENT_KIND_ABS
)) {
340 qemu_input_transform_abs_rotate(evt
);
344 s
= qemu_input_find_handler(1 << evt
->type
, src
);
348 s
->handler
->event(s
->dev
, src
, evt
);
352 void qemu_input_event_send(QemuConsole
*src
, InputEvent
*evt
)
354 /* Expect all parts of QEMU to send events with QCodes exclusively.
355 * Key numbers are only supported as end-user input via QMP */
356 assert(!(evt
->type
== INPUT_EVENT_KIND_KEY
&&
357 evt
->u
.key
.data
->key
->type
== KEY_VALUE_KIND_NUMBER
));
361 * 'sysrq' was mistakenly added to hack around the fact that
362 * the ps2 driver was not generating correct scancodes sequences
363 * when 'alt+print' was pressed. This flaw is now fixed and the
364 * 'sysrq' key serves no further purpose. We normalize it to
365 * 'print', so that downstream receivers of the event don't
366 * neeed to deal with this mistake
368 if (evt
->type
== INPUT_EVENT_KIND_KEY
&&
369 evt
->u
.key
.data
->key
->u
.qcode
.data
== Q_KEY_CODE_SYSRQ
) {
370 evt
->u
.key
.data
->key
->u
.qcode
.data
= Q_KEY_CODE_PRINT
;
373 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
377 replay_input_event(src
, evt
);
380 void qemu_input_event_sync_impl(void)
382 QemuInputHandlerState
*s
;
384 trace_input_event_sync();
386 QTAILQ_FOREACH(s
, &handlers
, node
) {
390 if (s
->handler
->sync
) {
391 s
->handler
->sync(s
->dev
);
397 void qemu_input_event_sync(void)
399 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
403 replay_input_sync_event();
406 static InputEvent
*qemu_input_event_new_key(KeyValue
*key
, bool down
)
408 InputEvent
*evt
= g_new0(InputEvent
, 1);
409 evt
->u
.key
.data
= g_new0(InputKeyEvent
, 1);
410 evt
->type
= INPUT_EVENT_KIND_KEY
;
411 evt
->u
.key
.data
->key
= key
;
412 evt
->u
.key
.data
->down
= down
;
416 void qemu_input_event_send_key(QemuConsole
*src
, KeyValue
*key
, bool down
)
419 evt
= qemu_input_event_new_key(key
, down
);
420 if (QTAILQ_EMPTY(&kbd_queue
)) {
421 qemu_input_event_send(src
, evt
);
422 qemu_input_event_sync();
423 qapi_free_InputEvent(evt
);
424 } else if (queue_count
< queue_limit
) {
425 qemu_input_queue_event(&kbd_queue
, src
, evt
);
426 qemu_input_queue_sync(&kbd_queue
);
428 qapi_free_InputEvent(evt
);
432 void qemu_input_event_send_key_number(QemuConsole
*src
, int num
, bool down
)
434 QKeyCode code
= qemu_input_key_number_to_qcode(num
);
435 qemu_input_event_send_key_qcode(src
, code
, down
);
438 void qemu_input_event_send_key_qcode(QemuConsole
*src
, QKeyCode q
, bool down
)
440 KeyValue
*key
= g_new0(KeyValue
, 1);
441 key
->type
= KEY_VALUE_KIND_QCODE
;
442 key
->u
.qcode
.data
= q
;
443 qemu_input_event_send_key(src
, key
, down
);
446 void qemu_input_event_send_key_delay(uint32_t delay_ms
)
448 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
453 kbd_timer
= timer_new_full(NULL
, QEMU_CLOCK_VIRTUAL
,
454 SCALE_MS
, QEMU_TIMER_ATTR_EXTERNAL
,
455 qemu_input_queue_process
, &kbd_queue
);
457 if (queue_count
< queue_limit
) {
458 qemu_input_queue_delay(&kbd_queue
, kbd_timer
,
459 delay_ms
? delay_ms
: kbd_default_delay_ms
);
463 void qemu_input_queue_btn(QemuConsole
*src
, InputButton btn
, bool down
)
465 InputBtnEvent bevt
= {
470 .type
= INPUT_EVENT_KIND_BTN
,
474 qemu_input_event_send(src
, &evt
);
477 void qemu_input_update_buttons(QemuConsole
*src
, uint32_t *button_map
,
478 uint32_t button_old
, uint32_t button_new
)
483 for (btn
= 0; btn
< INPUT_BUTTON__MAX
; btn
++) {
484 mask
= button_map
[btn
];
485 if ((button_old
& mask
) == (button_new
& mask
)) {
488 qemu_input_queue_btn(src
, btn
, button_new
& mask
);
492 bool qemu_input_is_absolute(void)
494 QemuInputHandlerState
*s
;
496 s
= qemu_input_find_handler(INPUT_EVENT_MASK_REL
| INPUT_EVENT_MASK_ABS
,
498 return (s
!= NULL
) && (s
->handler
->mask
& INPUT_EVENT_MASK_ABS
);
501 int qemu_input_scale_axis(int value
,
502 int min_in
, int max_in
,
503 int min_out
, int max_out
)
505 int64_t range_in
= (int64_t)max_in
- min_in
;
506 int64_t range_out
= (int64_t)max_out
- min_out
;
509 return min_out
+ range_out
/ 2;
511 return ((int64_t)value
- min_in
) * range_out
/ range_in
+ min_out
;
514 void qemu_input_queue_rel(QemuConsole
*src
, InputAxis axis
, int value
)
516 InputMoveEvent move
= {
521 .type
= INPUT_EVENT_KIND_REL
,
525 qemu_input_event_send(src
, &evt
);
528 void qemu_input_queue_abs(QemuConsole
*src
, InputAxis axis
, int value
,
529 int min_in
, int max_in
)
531 InputMoveEvent move
= {
533 .value
= qemu_input_scale_axis(value
, min_in
, max_in
,
535 INPUT_EVENT_ABS_MAX
),
538 .type
= INPUT_EVENT_KIND_ABS
,
542 qemu_input_event_send(src
, &evt
);
545 void qemu_input_check_mode_change(void)
547 static int current_is_absolute
;
550 is_absolute
= qemu_input_is_absolute();
552 if (is_absolute
!= current_is_absolute
) {
553 trace_input_mouse_mode(is_absolute
);
554 notifier_list_notify(&mouse_mode_notifiers
, NULL
);
557 current_is_absolute
= is_absolute
;
560 void qemu_add_mouse_mode_change_notifier(Notifier
*notify
)
562 notifier_list_add(&mouse_mode_notifiers
, notify
);
565 void qemu_remove_mouse_mode_change_notifier(Notifier
*notify
)
567 notifier_remove(notify
);
570 MouseInfoList
*qmp_query_mice(Error
**errp
)
572 MouseInfoList
*mice_list
= NULL
;
574 QemuInputHandlerState
*s
;
577 QTAILQ_FOREACH(s
, &handlers
, node
) {
578 if (!(s
->handler
->mask
&
579 (INPUT_EVENT_MASK_REL
| INPUT_EVENT_MASK_ABS
))) {
583 info
= g_new0(MouseInfoList
, 1);
584 info
->value
= g_new0(MouseInfo
, 1);
585 info
->value
->index
= s
->id
;
586 info
->value
->name
= g_strdup(s
->handler
->name
);
587 info
->value
->absolute
= s
->handler
->mask
& INPUT_EVENT_MASK_ABS
;
588 info
->value
->current
= current
;
591 info
->next
= mice_list
;
598 void hmp_mouse_set(Monitor
*mon
, const QDict
*qdict
)
600 QemuInputHandlerState
*s
;
601 int index
= qdict_get_int(qdict
, "index");
604 QTAILQ_FOREACH(s
, &handlers
, node
) {
605 if (s
->id
!= index
) {
608 if (!(s
->handler
->mask
& (INPUT_EVENT_MASK_REL
|
609 INPUT_EVENT_MASK_ABS
))) {
610 error_report("Input device '%s' is not a mouse", s
->handler
->name
);
614 qemu_input_handler_activate(s
);
619 error_report("Mouse at index '%d' not found", index
);
622 qemu_input_check_mode_change();