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 struct QemuInputEventQueue
{
24 QEMU_INPUT_QUEUE_DELAY
= 1,
25 QEMU_INPUT_QUEUE_EVENT
,
26 QEMU_INPUT_QUEUE_SYNC
,
32 QTAILQ_ENTRY(QemuInputEventQueue
) node
;
35 static QTAILQ_HEAD(, QemuInputHandlerState
) handlers
=
36 QTAILQ_HEAD_INITIALIZER(handlers
);
37 static NotifierList mouse_mode_notifiers
=
38 NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers
);
40 static QTAILQ_HEAD(QemuInputEventQueueHead
, QemuInputEventQueue
) kbd_queue
=
41 QTAILQ_HEAD_INITIALIZER(kbd_queue
);
42 static QEMUTimer
*kbd_timer
;
43 static uint32_t kbd_default_delay_ms
= 10;
44 static uint32_t queue_count
;
45 static uint32_t queue_limit
= 1024;
47 QemuInputHandlerState
*qemu_input_handler_register(DeviceState
*dev
,
48 QemuInputHandler
*handler
)
50 QemuInputHandlerState
*s
= g_new0(QemuInputHandlerState
, 1);
56 QTAILQ_INSERT_TAIL(&handlers
, s
, node
);
58 qemu_input_check_mode_change();
62 void qemu_input_handler_activate(QemuInputHandlerState
*s
)
64 QTAILQ_REMOVE(&handlers
, s
, node
);
65 QTAILQ_INSERT_HEAD(&handlers
, s
, node
);
66 qemu_input_check_mode_change();
69 void qemu_input_handler_deactivate(QemuInputHandlerState
*s
)
71 QTAILQ_REMOVE(&handlers
, s
, node
);
72 QTAILQ_INSERT_TAIL(&handlers
, s
, node
);
73 qemu_input_check_mode_change();
76 void qemu_input_handler_unregister(QemuInputHandlerState
*s
)
78 QTAILQ_REMOVE(&handlers
, s
, node
);
80 qemu_input_check_mode_change();
83 void qemu_input_handler_bind(QemuInputHandlerState
*s
,
84 const char *device_id
, int head
,
90 con
= qemu_console_lookup_by_device_name(device_id
, head
, &err
);
92 error_propagate(errp
, err
);
99 static QemuInputHandlerState
*
100 qemu_input_find_handler(uint32_t mask
, QemuConsole
*con
)
102 QemuInputHandlerState
*s
;
104 QTAILQ_FOREACH(s
, &handlers
, node
) {
105 if (s
->con
== NULL
|| s
->con
!= con
) {
108 if (mask
& s
->handler
->mask
) {
113 QTAILQ_FOREACH(s
, &handlers
, node
) {
114 if (s
->con
!= NULL
) {
117 if (mask
& s
->handler
->mask
) {
124 void qmp_input_send_event(bool has_device
, const char *device
,
125 bool has_head
, int64_t head
,
126 InputEventList
*events
, Error
**errp
)
137 con
= qemu_console_lookup_by_device_name(device
, head
, &err
);
139 error_propagate(errp
, err
);
144 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
145 error_setg(errp
, "VM not running");
149 for (e
= events
; e
!= NULL
; e
= e
->next
) {
150 InputEvent
*event
= e
->value
;
152 if (!qemu_input_find_handler(1 << event
->type
, con
)) {
153 error_setg(errp
, "Input handler not found for "
155 InputEventKind_str(event
->type
));
160 for (e
= events
; e
!= NULL
; e
= e
->next
) {
161 InputEvent
*evt
= e
->value
;
163 if (evt
->type
== INPUT_EVENT_KIND_KEY
&&
164 evt
->u
.key
.data
->key
->type
== KEY_VALUE_KIND_NUMBER
) {
165 KeyValue
*key
= evt
->u
.key
.data
->key
;
166 QKeyCode code
= qemu_input_key_number_to_qcode(key
->u
.number
.data
);
167 qemu_input_event_send_key_qcode(con
, code
, evt
->u
.key
.data
->down
);
169 qemu_input_event_send(con
, evt
);
173 qemu_input_event_sync();
176 static int qemu_input_transform_invert_abs_value(int value
)
178 return (int64_t)INPUT_EVENT_ABS_MAX
- value
+ INPUT_EVENT_ABS_MIN
;
181 static void qemu_input_transform_abs_rotate(InputEvent
*evt
)
183 InputMoveEvent
*move
= evt
->u
.abs
.data
;
184 switch (graphic_rotate
) {
186 if (move
->axis
== INPUT_AXIS_X
) {
187 move
->axis
= INPUT_AXIS_Y
;
188 } else if (move
->axis
== INPUT_AXIS_Y
) {
189 move
->axis
= INPUT_AXIS_X
;
190 move
->value
= qemu_input_transform_invert_abs_value(move
->value
);
194 move
->value
= qemu_input_transform_invert_abs_value(move
->value
);
197 if (move
->axis
== INPUT_AXIS_X
) {
198 move
->axis
= INPUT_AXIS_Y
;
199 move
->value
= qemu_input_transform_invert_abs_value(move
->value
);
200 } else if (move
->axis
== INPUT_AXIS_Y
) {
201 move
->axis
= INPUT_AXIS_X
;
207 static void qemu_input_event_trace(QemuConsole
*src
, InputEvent
*evt
)
213 InputMoveEvent
*move
;
216 idx
= qemu_console_get_index(src
);
219 case INPUT_EVENT_KIND_KEY
:
220 key
= evt
->u
.key
.data
;
221 switch (key
->key
->type
) {
222 case KEY_VALUE_KIND_NUMBER
:
223 qcode
= qemu_input_key_number_to_qcode(key
->key
->u
.number
.data
);
224 name
= QKeyCode_str(qcode
);
225 trace_input_event_key_number(idx
, key
->key
->u
.number
.data
,
228 case KEY_VALUE_KIND_QCODE
:
229 name
= QKeyCode_str(key
->key
->u
.qcode
.data
);
230 trace_input_event_key_qcode(idx
, name
, key
->down
);
232 case KEY_VALUE_KIND__MAX
:
237 case INPUT_EVENT_KIND_BTN
:
238 btn
= evt
->u
.btn
.data
;
239 name
= InputButton_str(btn
->button
);
240 trace_input_event_btn(idx
, name
, btn
->down
);
242 case INPUT_EVENT_KIND_REL
:
243 move
= evt
->u
.rel
.data
;
244 name
= InputAxis_str(move
->axis
);
245 trace_input_event_rel(idx
, name
, move
->value
);
247 case INPUT_EVENT_KIND_ABS
:
248 move
= evt
->u
.abs
.data
;
249 name
= InputAxis_str(move
->axis
);
250 trace_input_event_abs(idx
, name
, move
->value
);
252 case INPUT_EVENT_KIND__MAX
:
258 static void qemu_input_queue_process(void *opaque
)
260 struct QemuInputEventQueueHead
*queue
= opaque
;
261 QemuInputEventQueue
*item
;
263 g_assert(!QTAILQ_EMPTY(queue
));
264 item
= QTAILQ_FIRST(queue
);
265 g_assert(item
->type
== QEMU_INPUT_QUEUE_DELAY
);
266 QTAILQ_REMOVE(queue
, item
, node
);
270 while (!QTAILQ_EMPTY(queue
)) {
271 item
= QTAILQ_FIRST(queue
);
272 switch (item
->type
) {
273 case QEMU_INPUT_QUEUE_DELAY
:
274 timer_mod(item
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
)
277 case QEMU_INPUT_QUEUE_EVENT
:
278 qemu_input_event_send(item
->src
, item
->evt
);
279 qapi_free_InputEvent(item
->evt
);
281 case QEMU_INPUT_QUEUE_SYNC
:
282 qemu_input_event_sync();
285 QTAILQ_REMOVE(queue
, item
, node
);
291 static void qemu_input_queue_delay(struct QemuInputEventQueueHead
*queue
,
292 QEMUTimer
*timer
, uint32_t delay_ms
)
294 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
295 bool start_timer
= QTAILQ_EMPTY(queue
);
297 item
->type
= QEMU_INPUT_QUEUE_DELAY
;
298 item
->delay_ms
= delay_ms
;
300 QTAILQ_INSERT_TAIL(queue
, item
, node
);
304 timer_mod(item
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
)
309 static void qemu_input_queue_event(struct QemuInputEventQueueHead
*queue
,
310 QemuConsole
*src
, InputEvent
*evt
)
312 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
314 item
->type
= QEMU_INPUT_QUEUE_EVENT
;
317 QTAILQ_INSERT_TAIL(queue
, item
, node
);
321 static void qemu_input_queue_sync(struct QemuInputEventQueueHead
*queue
)
323 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
325 item
->type
= QEMU_INPUT_QUEUE_SYNC
;
326 QTAILQ_INSERT_TAIL(queue
, item
, node
);
330 void qemu_input_event_send_impl(QemuConsole
*src
, InputEvent
*evt
)
332 QemuInputHandlerState
*s
;
334 qemu_input_event_trace(src
, evt
);
337 if (graphic_rotate
&& (evt
->type
== INPUT_EVENT_KIND_ABS
)) {
338 qemu_input_transform_abs_rotate(evt
);
342 s
= qemu_input_find_handler(1 << evt
->type
, src
);
346 s
->handler
->event(s
->dev
, src
, evt
);
350 void qemu_input_event_send(QemuConsole
*src
, InputEvent
*evt
)
352 /* Expect all parts of QEMU to send events with QCodes exclusively.
353 * Key numbers are only supported as end-user input via QMP */
354 assert(!(evt
->type
== INPUT_EVENT_KIND_KEY
&&
355 evt
->u
.key
.data
->key
->type
== KEY_VALUE_KIND_NUMBER
));
359 * 'sysrq' was mistakenly added to hack around the fact that
360 * the ps2 driver was not generating correct scancodes sequences
361 * when 'alt+print' was pressed. This flaw is now fixed and the
362 * 'sysrq' key serves no further purpose. We normalize it to
363 * 'print', so that downstream receivers of the event don't
364 * neeed to deal with this mistake
366 if (evt
->type
== INPUT_EVENT_KIND_KEY
&&
367 evt
->u
.key
.data
->key
->u
.qcode
.data
== Q_KEY_CODE_SYSRQ
) {
368 evt
->u
.key
.data
->key
->u
.qcode
.data
= Q_KEY_CODE_PRINT
;
371 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
375 replay_input_event(src
, evt
);
378 void qemu_input_event_sync_impl(void)
380 QemuInputHandlerState
*s
;
382 trace_input_event_sync();
384 QTAILQ_FOREACH(s
, &handlers
, node
) {
388 if (s
->handler
->sync
) {
389 s
->handler
->sync(s
->dev
);
395 void qemu_input_event_sync(void)
397 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
401 replay_input_sync_event();
404 static InputEvent
*qemu_input_event_new_key(KeyValue
*key
, bool down
)
406 InputEvent
*evt
= g_new0(InputEvent
, 1);
407 evt
->u
.key
.data
= g_new0(InputKeyEvent
, 1);
408 evt
->type
= INPUT_EVENT_KIND_KEY
;
409 evt
->u
.key
.data
->key
= key
;
410 evt
->u
.key
.data
->down
= down
;
414 void qemu_input_event_send_key(QemuConsole
*src
, KeyValue
*key
, bool down
)
417 evt
= qemu_input_event_new_key(key
, down
);
418 if (QTAILQ_EMPTY(&kbd_queue
)) {
419 qemu_input_event_send(src
, evt
);
420 qemu_input_event_sync();
421 qapi_free_InputEvent(evt
);
422 } else if (queue_count
< queue_limit
) {
423 qemu_input_queue_event(&kbd_queue
, src
, evt
);
424 qemu_input_queue_sync(&kbd_queue
);
426 qapi_free_InputEvent(evt
);
430 void qemu_input_event_send_key_number(QemuConsole
*src
, int num
, bool down
)
432 QKeyCode code
= qemu_input_key_number_to_qcode(num
);
433 qemu_input_event_send_key_qcode(src
, code
, down
);
436 void qemu_input_event_send_key_qcode(QemuConsole
*src
, QKeyCode q
, bool down
)
438 KeyValue
*key
= g_new0(KeyValue
, 1);
439 key
->type
= KEY_VALUE_KIND_QCODE
;
440 key
->u
.qcode
.data
= q
;
441 qemu_input_event_send_key(src
, key
, down
);
444 void qemu_input_event_send_key_delay(uint32_t delay_ms
)
446 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
451 kbd_timer
= timer_new_full(NULL
, QEMU_CLOCK_VIRTUAL
,
452 SCALE_MS
, QEMU_TIMER_ATTR_EXTERNAL
,
453 qemu_input_queue_process
, &kbd_queue
);
455 if (queue_count
< queue_limit
) {
456 qemu_input_queue_delay(&kbd_queue
, kbd_timer
,
457 delay_ms
? delay_ms
: kbd_default_delay_ms
);
461 InputEvent
*qemu_input_event_new_btn(InputButton btn
, bool down
)
463 InputEvent
*evt
= g_new0(InputEvent
, 1);
464 evt
->u
.btn
.data
= g_new0(InputBtnEvent
, 1);
465 evt
->type
= INPUT_EVENT_KIND_BTN
;
466 evt
->u
.btn
.data
->button
= btn
;
467 evt
->u
.btn
.data
->down
= down
;
471 void qemu_input_queue_btn(QemuConsole
*src
, InputButton btn
, bool down
)
474 evt
= qemu_input_event_new_btn(btn
, down
);
475 qemu_input_event_send(src
, evt
);
476 qapi_free_InputEvent(evt
);
479 void qemu_input_update_buttons(QemuConsole
*src
, uint32_t *button_map
,
480 uint32_t button_old
, uint32_t button_new
)
485 for (btn
= 0; btn
< INPUT_BUTTON__MAX
; btn
++) {
486 mask
= button_map
[btn
];
487 if ((button_old
& mask
) == (button_new
& mask
)) {
490 qemu_input_queue_btn(src
, btn
, button_new
& mask
);
494 bool qemu_input_is_absolute(void)
496 QemuInputHandlerState
*s
;
498 s
= qemu_input_find_handler(INPUT_EVENT_MASK_REL
| INPUT_EVENT_MASK_ABS
,
500 return (s
!= NULL
) && (s
->handler
->mask
& INPUT_EVENT_MASK_ABS
);
503 int qemu_input_scale_axis(int value
,
504 int min_in
, int max_in
,
505 int min_out
, int max_out
)
507 int64_t range_in
= (int64_t)max_in
- min_in
;
508 int64_t range_out
= (int64_t)max_out
- min_out
;
511 return min_out
+ range_out
/ 2;
513 return ((int64_t)value
- min_in
) * range_out
/ range_in
+ min_out
;
516 InputEvent
*qemu_input_event_new_move(InputEventKind kind
,
517 InputAxis axis
, int value
)
519 InputEvent
*evt
= g_new0(InputEvent
, 1);
520 InputMoveEvent
*move
= g_new0(InputMoveEvent
, 1);
523 evt
->u
.rel
.data
= move
; /* evt->u.rel is the same as evt->u.abs */
529 void qemu_input_queue_rel(QemuConsole
*src
, InputAxis axis
, int value
)
532 evt
= qemu_input_event_new_move(INPUT_EVENT_KIND_REL
, axis
, value
);
533 qemu_input_event_send(src
, evt
);
534 qapi_free_InputEvent(evt
);
537 void qemu_input_queue_abs(QemuConsole
*src
, InputAxis axis
, int value
,
538 int min_in
, int max_in
)
541 int scaled
= qemu_input_scale_axis(value
, min_in
, max_in
,
543 INPUT_EVENT_ABS_MAX
);
544 evt
= qemu_input_event_new_move(INPUT_EVENT_KIND_ABS
, axis
, scaled
);
545 qemu_input_event_send(src
, evt
);
546 qapi_free_InputEvent(evt
);
549 void qemu_input_check_mode_change(void)
551 static int current_is_absolute
;
554 is_absolute
= qemu_input_is_absolute();
556 if (is_absolute
!= current_is_absolute
) {
557 trace_input_mouse_mode(is_absolute
);
558 notifier_list_notify(&mouse_mode_notifiers
, NULL
);
561 current_is_absolute
= is_absolute
;
564 void qemu_add_mouse_mode_change_notifier(Notifier
*notify
)
566 notifier_list_add(&mouse_mode_notifiers
, notify
);
569 void qemu_remove_mouse_mode_change_notifier(Notifier
*notify
)
571 notifier_remove(notify
);
574 MouseInfoList
*qmp_query_mice(Error
**errp
)
576 MouseInfoList
*mice_list
= NULL
;
578 QemuInputHandlerState
*s
;
581 QTAILQ_FOREACH(s
, &handlers
, node
) {
582 if (!(s
->handler
->mask
&
583 (INPUT_EVENT_MASK_REL
| INPUT_EVENT_MASK_ABS
))) {
587 info
= g_new0(MouseInfoList
, 1);
588 info
->value
= g_new0(MouseInfo
, 1);
589 info
->value
->index
= s
->id
;
590 info
->value
->name
= g_strdup(s
->handler
->name
);
591 info
->value
->absolute
= s
->handler
->mask
& INPUT_EVENT_MASK_ABS
;
592 info
->value
->current
= current
;
595 info
->next
= mice_list
;
602 void hmp_mouse_set(Monitor
*mon
, const QDict
*qdict
)
604 QemuInputHandlerState
*s
;
605 int index
= qdict_get_int(qdict
, "index");
608 QTAILQ_FOREACH(s
, &handlers
, node
) {
609 if (s
->id
!= index
) {
612 if (!(s
->handler
->mask
& (INPUT_EVENT_MASK_REL
|
613 INPUT_EVENT_MASK_ABS
))) {
614 error_report("Input device '%s' is not a mouse", s
->handler
->name
);
618 qemu_input_handler_activate(s
);
623 error_report("Mouse at index '%d' not found", index
);
626 qemu_input_check_mode_change();