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"
11 #include "sysemu/runstate.h"
13 struct QemuInputHandlerState
{
15 QemuInputHandler
*handler
;
19 QTAILQ_ENTRY(QemuInputHandlerState
) node
;
22 typedef struct QemuInputEventQueue QemuInputEventQueue
;
23 typedef QTAILQ_HEAD(QemuInputEventQueueHead
, QemuInputEventQueue
)
24 QemuInputEventQueueHead
;
26 struct QemuInputEventQueue
{
28 QEMU_INPUT_QUEUE_DELAY
= 1,
29 QEMU_INPUT_QUEUE_EVENT
,
30 QEMU_INPUT_QUEUE_SYNC
,
36 QTAILQ_ENTRY(QemuInputEventQueue
) node
;
39 static QTAILQ_HEAD(, QemuInputHandlerState
) handlers
=
40 QTAILQ_HEAD_INITIALIZER(handlers
);
41 static NotifierList mouse_mode_notifiers
=
42 NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers
);
44 static QemuInputEventQueueHead kbd_queue
= QTAILQ_HEAD_INITIALIZER(kbd_queue
);
45 static QEMUTimer
*kbd_timer
;
46 static uint32_t kbd_default_delay_ms
= 10;
47 static uint32_t queue_count
;
48 static uint32_t queue_limit
= 1024;
50 QemuInputHandlerState
*qemu_input_handler_register(DeviceState
*dev
,
51 QemuInputHandler
*handler
)
53 QemuInputHandlerState
*s
= g_new0(QemuInputHandlerState
, 1);
59 QTAILQ_INSERT_TAIL(&handlers
, s
, node
);
61 qemu_input_check_mode_change();
65 void qemu_input_handler_activate(QemuInputHandlerState
*s
)
67 QTAILQ_REMOVE(&handlers
, s
, node
);
68 QTAILQ_INSERT_HEAD(&handlers
, s
, node
);
69 qemu_input_check_mode_change();
72 void qemu_input_handler_deactivate(QemuInputHandlerState
*s
)
74 QTAILQ_REMOVE(&handlers
, s
, node
);
75 QTAILQ_INSERT_TAIL(&handlers
, s
, node
);
76 qemu_input_check_mode_change();
79 void qemu_input_handler_unregister(QemuInputHandlerState
*s
)
81 QTAILQ_REMOVE(&handlers
, s
, node
);
83 qemu_input_check_mode_change();
86 void qemu_input_handler_bind(QemuInputHandlerState
*s
,
87 const char *device_id
, int head
,
93 con
= qemu_console_lookup_by_device_name(device_id
, head
, &err
);
95 error_propagate(errp
, err
);
102 static QemuInputHandlerState
*
103 qemu_input_find_handler(uint32_t mask
, QemuConsole
*con
)
105 QemuInputHandlerState
*s
;
107 QTAILQ_FOREACH(s
, &handlers
, node
) {
108 if (s
->con
== NULL
|| s
->con
!= con
) {
111 if (mask
& s
->handler
->mask
) {
116 QTAILQ_FOREACH(s
, &handlers
, node
) {
117 if (s
->con
!= NULL
) {
120 if (mask
& s
->handler
->mask
) {
127 void qmp_input_send_event(bool has_device
, const char *device
,
128 bool has_head
, int64_t head
,
129 InputEventList
*events
, Error
**errp
)
140 con
= qemu_console_lookup_by_device_name(device
, head
, &err
);
142 error_propagate(errp
, err
);
147 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
148 error_setg(errp
, "VM not running");
152 for (e
= events
; e
!= NULL
; e
= e
->next
) {
153 InputEvent
*event
= e
->value
;
155 if (!qemu_input_find_handler(1 << event
->type
, con
)) {
156 error_setg(errp
, "Input handler not found for "
158 InputEventKind_str(event
->type
));
163 for (e
= events
; e
!= NULL
; e
= e
->next
) {
164 InputEvent
*evt
= e
->value
;
166 if (evt
->type
== INPUT_EVENT_KIND_KEY
&&
167 evt
->u
.key
.data
->key
->type
== KEY_VALUE_KIND_NUMBER
) {
168 KeyValue
*key
= evt
->u
.key
.data
->key
;
169 QKeyCode code
= qemu_input_key_number_to_qcode(key
->u
.number
.data
);
170 qemu_input_event_send_key_qcode(con
, code
, evt
->u
.key
.data
->down
);
172 qemu_input_event_send(con
, evt
);
176 qemu_input_event_sync();
179 static int qemu_input_transform_invert_abs_value(int value
)
181 return (int64_t)INPUT_EVENT_ABS_MAX
- value
+ INPUT_EVENT_ABS_MIN
;
184 static void qemu_input_transform_abs_rotate(InputEvent
*evt
)
186 InputMoveEvent
*move
= evt
->u
.abs
.data
;
187 switch (graphic_rotate
) {
189 if (move
->axis
== INPUT_AXIS_X
) {
190 move
->axis
= INPUT_AXIS_Y
;
191 } else if (move
->axis
== INPUT_AXIS_Y
) {
192 move
->axis
= INPUT_AXIS_X
;
193 move
->value
= qemu_input_transform_invert_abs_value(move
->value
);
197 move
->value
= qemu_input_transform_invert_abs_value(move
->value
);
200 if (move
->axis
== INPUT_AXIS_X
) {
201 move
->axis
= INPUT_AXIS_Y
;
202 move
->value
= qemu_input_transform_invert_abs_value(move
->value
);
203 } else if (move
->axis
== INPUT_AXIS_Y
) {
204 move
->axis
= INPUT_AXIS_X
;
210 static void qemu_input_event_trace(QemuConsole
*src
, InputEvent
*evt
)
216 InputMoveEvent
*move
;
219 idx
= qemu_console_get_index(src
);
222 case INPUT_EVENT_KIND_KEY
:
223 key
= evt
->u
.key
.data
;
224 switch (key
->key
->type
) {
225 case KEY_VALUE_KIND_NUMBER
:
226 qcode
= qemu_input_key_number_to_qcode(key
->key
->u
.number
.data
);
227 name
= QKeyCode_str(qcode
);
228 trace_input_event_key_number(idx
, key
->key
->u
.number
.data
,
231 case KEY_VALUE_KIND_QCODE
:
232 name
= QKeyCode_str(key
->key
->u
.qcode
.data
);
233 trace_input_event_key_qcode(idx
, name
, key
->down
);
235 case KEY_VALUE_KIND__MAX
:
240 case INPUT_EVENT_KIND_BTN
:
241 btn
= evt
->u
.btn
.data
;
242 name
= InputButton_str(btn
->button
);
243 trace_input_event_btn(idx
, name
, btn
->down
);
245 case INPUT_EVENT_KIND_REL
:
246 move
= evt
->u
.rel
.data
;
247 name
= InputAxis_str(move
->axis
);
248 trace_input_event_rel(idx
, name
, move
->value
);
250 case INPUT_EVENT_KIND_ABS
:
251 move
= evt
->u
.abs
.data
;
252 name
= InputAxis_str(move
->axis
);
253 trace_input_event_abs(idx
, name
, move
->value
);
255 case INPUT_EVENT_KIND__MAX
:
261 static void qemu_input_queue_process(void *opaque
)
263 QemuInputEventQueueHead
*queue
= opaque
;
264 QemuInputEventQueue
*item
;
266 g_assert(!QTAILQ_EMPTY(queue
));
267 item
= QTAILQ_FIRST(queue
);
268 g_assert(item
->type
== QEMU_INPUT_QUEUE_DELAY
);
269 QTAILQ_REMOVE(queue
, item
, node
);
273 while (!QTAILQ_EMPTY(queue
)) {
274 item
= QTAILQ_FIRST(queue
);
275 switch (item
->type
) {
276 case QEMU_INPUT_QUEUE_DELAY
:
277 timer_mod(item
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
)
280 case QEMU_INPUT_QUEUE_EVENT
:
281 qemu_input_event_send(item
->src
, item
->evt
);
282 qapi_free_InputEvent(item
->evt
);
284 case QEMU_INPUT_QUEUE_SYNC
:
285 qemu_input_event_sync();
288 QTAILQ_REMOVE(queue
, item
, node
);
294 static void qemu_input_queue_delay(QemuInputEventQueueHead
*queue
,
295 QEMUTimer
*timer
, uint32_t delay_ms
)
297 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
298 bool start_timer
= QTAILQ_EMPTY(queue
);
300 item
->type
= QEMU_INPUT_QUEUE_DELAY
;
301 item
->delay_ms
= delay_ms
;
303 QTAILQ_INSERT_TAIL(queue
, item
, node
);
307 timer_mod(item
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
)
312 static void qemu_input_queue_event(QemuInputEventQueueHead
*queue
,
313 QemuConsole
*src
, InputEvent
*evt
)
315 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
317 item
->type
= QEMU_INPUT_QUEUE_EVENT
;
320 QTAILQ_INSERT_TAIL(queue
, item
, node
);
324 static void qemu_input_queue_sync(QemuInputEventQueueHead
*queue
)
326 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
328 item
->type
= QEMU_INPUT_QUEUE_SYNC
;
329 QTAILQ_INSERT_TAIL(queue
, item
, node
);
333 void qemu_input_event_send_impl(QemuConsole
*src
, InputEvent
*evt
)
335 QemuInputHandlerState
*s
;
337 qemu_input_event_trace(src
, evt
);
340 if (graphic_rotate
&& (evt
->type
== INPUT_EVENT_KIND_ABS
)) {
341 qemu_input_transform_abs_rotate(evt
);
345 s
= qemu_input_find_handler(1 << evt
->type
, src
);
349 s
->handler
->event(s
->dev
, src
, evt
);
353 void qemu_input_event_send(QemuConsole
*src
, InputEvent
*evt
)
355 /* Expect all parts of QEMU to send events with QCodes exclusively.
356 * Key numbers are only supported as end-user input via QMP */
357 assert(!(evt
->type
== INPUT_EVENT_KIND_KEY
&&
358 evt
->u
.key
.data
->key
->type
== KEY_VALUE_KIND_NUMBER
));
362 * 'sysrq' was mistakenly added to hack around the fact that
363 * the ps2 driver was not generating correct scancodes sequences
364 * when 'alt+print' was pressed. This flaw is now fixed and the
365 * 'sysrq' key serves no further purpose. We normalize it to
366 * 'print', so that downstream receivers of the event don't
367 * neeed to deal with this mistake
369 if (evt
->type
== INPUT_EVENT_KIND_KEY
&&
370 evt
->u
.key
.data
->key
->u
.qcode
.data
== Q_KEY_CODE_SYSRQ
) {
371 evt
->u
.key
.data
->key
->u
.qcode
.data
= Q_KEY_CODE_PRINT
;
374 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
378 replay_input_event(src
, evt
);
381 void qemu_input_event_sync_impl(void)
383 QemuInputHandlerState
*s
;
385 trace_input_event_sync();
387 QTAILQ_FOREACH(s
, &handlers
, node
) {
391 if (s
->handler
->sync
) {
392 s
->handler
->sync(s
->dev
);
398 void qemu_input_event_sync(void)
400 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
404 replay_input_sync_event();
407 static InputEvent
*qemu_input_event_new_key(KeyValue
*key
, bool down
)
409 InputEvent
*evt
= g_new0(InputEvent
, 1);
410 evt
->u
.key
.data
= g_new0(InputKeyEvent
, 1);
411 evt
->type
= INPUT_EVENT_KIND_KEY
;
412 evt
->u
.key
.data
->key
= key
;
413 evt
->u
.key
.data
->down
= down
;
417 void qemu_input_event_send_key(QemuConsole
*src
, KeyValue
*key
, bool down
)
420 evt
= qemu_input_event_new_key(key
, down
);
421 if (QTAILQ_EMPTY(&kbd_queue
)) {
422 qemu_input_event_send(src
, evt
);
423 qemu_input_event_sync();
424 qapi_free_InputEvent(evt
);
425 } else if (queue_count
< queue_limit
) {
426 qemu_input_queue_event(&kbd_queue
, src
, evt
);
427 qemu_input_queue_sync(&kbd_queue
);
429 qapi_free_InputEvent(evt
);
433 void qemu_input_event_send_key_number(QemuConsole
*src
, int num
, bool down
)
435 QKeyCode code
= qemu_input_key_number_to_qcode(num
);
436 qemu_input_event_send_key_qcode(src
, code
, down
);
439 void qemu_input_event_send_key_qcode(QemuConsole
*src
, QKeyCode q
, bool down
)
441 KeyValue
*key
= g_new0(KeyValue
, 1);
442 key
->type
= KEY_VALUE_KIND_QCODE
;
443 key
->u
.qcode
.data
= q
;
444 qemu_input_event_send_key(src
, key
, down
);
447 void qemu_input_event_send_key_delay(uint32_t delay_ms
)
449 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
454 kbd_timer
= timer_new_full(NULL
, QEMU_CLOCK_VIRTUAL
,
455 SCALE_MS
, QEMU_TIMER_ATTR_EXTERNAL
,
456 qemu_input_queue_process
, &kbd_queue
);
458 if (queue_count
< queue_limit
) {
459 qemu_input_queue_delay(&kbd_queue
, kbd_timer
,
460 delay_ms
? delay_ms
: kbd_default_delay_ms
);
464 void qemu_input_queue_btn(QemuConsole
*src
, InputButton btn
, bool down
)
466 InputBtnEvent bevt
= {
471 .type
= INPUT_EVENT_KIND_BTN
,
475 qemu_input_event_send(src
, &evt
);
478 void qemu_input_update_buttons(QemuConsole
*src
, uint32_t *button_map
,
479 uint32_t button_old
, uint32_t button_new
)
484 for (btn
= 0; btn
< INPUT_BUTTON__MAX
; btn
++) {
485 mask
= button_map
[btn
];
486 if ((button_old
& mask
) == (button_new
& mask
)) {
489 qemu_input_queue_btn(src
, btn
, button_new
& mask
);
493 bool qemu_input_is_absolute(void)
495 QemuInputHandlerState
*s
;
497 s
= qemu_input_find_handler(INPUT_EVENT_MASK_REL
| INPUT_EVENT_MASK_ABS
,
499 return (s
!= NULL
) && (s
->handler
->mask
& INPUT_EVENT_MASK_ABS
);
502 int qemu_input_scale_axis(int value
,
503 int min_in
, int max_in
,
504 int min_out
, int max_out
)
506 int64_t range_in
= (int64_t)max_in
- min_in
;
507 int64_t range_out
= (int64_t)max_out
- min_out
;
510 return min_out
+ range_out
/ 2;
512 return ((int64_t)value
- min_in
) * range_out
/ range_in
+ min_out
;
515 void qemu_input_queue_rel(QemuConsole
*src
, InputAxis axis
, int value
)
517 InputMoveEvent move
= {
522 .type
= INPUT_EVENT_KIND_REL
,
526 qemu_input_event_send(src
, &evt
);
529 void qemu_input_queue_abs(QemuConsole
*src
, InputAxis axis
, int value
,
530 int min_in
, int max_in
)
532 InputMoveEvent move
= {
534 .value
= qemu_input_scale_axis(value
, min_in
, max_in
,
536 INPUT_EVENT_ABS_MAX
),
539 .type
= INPUT_EVENT_KIND_ABS
,
543 qemu_input_event_send(src
, &evt
);
546 void qemu_input_check_mode_change(void)
548 static int current_is_absolute
;
551 is_absolute
= qemu_input_is_absolute();
553 if (is_absolute
!= current_is_absolute
) {
554 trace_input_mouse_mode(is_absolute
);
555 notifier_list_notify(&mouse_mode_notifiers
, NULL
);
558 current_is_absolute
= is_absolute
;
561 void qemu_add_mouse_mode_change_notifier(Notifier
*notify
)
563 notifier_list_add(&mouse_mode_notifiers
, notify
);
566 void qemu_remove_mouse_mode_change_notifier(Notifier
*notify
)
568 notifier_remove(notify
);
571 MouseInfoList
*qmp_query_mice(Error
**errp
)
573 MouseInfoList
*mice_list
= NULL
;
575 QemuInputHandlerState
*s
;
578 QTAILQ_FOREACH(s
, &handlers
, node
) {
579 if (!(s
->handler
->mask
&
580 (INPUT_EVENT_MASK_REL
| INPUT_EVENT_MASK_ABS
))) {
584 info
= g_new0(MouseInfo
, 1);
586 info
->name
= g_strdup(s
->handler
->name
);
587 info
->absolute
= s
->handler
->mask
& INPUT_EVENT_MASK_ABS
;
588 info
->current
= current
;
591 QAPI_LIST_PREPEND(mice_list
, info
);
597 void hmp_mouse_set(Monitor
*mon
, const QDict
*qdict
)
599 QemuInputHandlerState
*s
;
600 int index
= qdict_get_int(qdict
, "index");
603 QTAILQ_FOREACH(s
, &handlers
, node
) {
604 if (s
->id
!= index
) {
607 if (!(s
->handler
->mask
& (INPUT_EVENT_MASK_REL
|
608 INPUT_EVENT_MASK_ABS
))) {
609 error_report("Input device '%s' is not a mouse", s
->handler
->name
);
613 qemu_input_handler_activate(s
);
618 error_report("Mouse at index '%d' not found", index
);
621 qemu_input_check_mode_change();