1 #include "qemu/osdep.h"
2 #include "sysemu/sysemu.h"
3 #include "qapi-types.h"
4 #include "qemu/error-report.h"
5 #include "qmp-commands.h"
8 #include "ui/console.h"
9 #include "sysemu/replay.h"
11 struct QemuInputHandlerState
{
13 QemuInputHandler
*handler
;
17 QTAILQ_ENTRY(QemuInputHandlerState
) node
;
20 typedef struct QemuInputEventQueue QemuInputEventQueue
;
21 struct QemuInputEventQueue
{
23 QEMU_INPUT_QUEUE_DELAY
= 1,
24 QEMU_INPUT_QUEUE_EVENT
,
25 QEMU_INPUT_QUEUE_SYNC
,
31 QTAILQ_ENTRY(QemuInputEventQueue
) node
;
34 static QTAILQ_HEAD(, QemuInputHandlerState
) handlers
=
35 QTAILQ_HEAD_INITIALIZER(handlers
);
36 static NotifierList mouse_mode_notifiers
=
37 NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers
);
39 static QTAILQ_HEAD(QemuInputEventQueueHead
, QemuInputEventQueue
) kbd_queue
=
40 QTAILQ_HEAD_INITIALIZER(kbd_queue
);
41 static QEMUTimer
*kbd_timer
;
42 static uint32_t kbd_default_delay_ms
= 10;
43 static uint32_t queue_count
;
44 static uint32_t queue_limit
= 1024;
46 QemuInputHandlerState
*qemu_input_handler_register(DeviceState
*dev
,
47 QemuInputHandler
*handler
)
49 QemuInputHandlerState
*s
= g_new0(QemuInputHandlerState
, 1);
55 QTAILQ_INSERT_TAIL(&handlers
, s
, node
);
57 qemu_input_check_mode_change();
61 void qemu_input_handler_activate(QemuInputHandlerState
*s
)
63 QTAILQ_REMOVE(&handlers
, s
, node
);
64 QTAILQ_INSERT_HEAD(&handlers
, s
, node
);
65 qemu_input_check_mode_change();
68 void qemu_input_handler_deactivate(QemuInputHandlerState
*s
)
70 QTAILQ_REMOVE(&handlers
, s
, node
);
71 QTAILQ_INSERT_TAIL(&handlers
, s
, node
);
72 qemu_input_check_mode_change();
75 void qemu_input_handler_unregister(QemuInputHandlerState
*s
)
77 QTAILQ_REMOVE(&handlers
, s
, node
);
79 qemu_input_check_mode_change();
82 void qemu_input_handler_bind(QemuInputHandlerState
*s
,
83 const char *device_id
, int head
,
89 con
= qemu_console_lookup_by_device_name(device_id
, head
, &err
);
91 error_propagate(errp
, err
);
98 static QemuInputHandlerState
*
99 qemu_input_find_handler(uint32_t mask
, QemuConsole
*con
)
101 QemuInputHandlerState
*s
;
103 QTAILQ_FOREACH(s
, &handlers
, node
) {
104 if (s
->con
== NULL
|| s
->con
!= con
) {
107 if (mask
& s
->handler
->mask
) {
112 QTAILQ_FOREACH(s
, &handlers
, node
) {
113 if (s
->con
!= NULL
) {
116 if (mask
& s
->handler
->mask
) {
123 void qmp_input_send_event(bool has_device
, const char *device
,
124 bool has_head
, int64_t head
,
125 InputEventList
*events
, Error
**errp
)
136 con
= qemu_console_lookup_by_device_name(device
, head
, &err
);
138 error_propagate(errp
, err
);
143 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
144 error_setg(errp
, "VM not running");
148 for (e
= events
; e
!= NULL
; e
= e
->next
) {
149 InputEvent
*event
= e
->value
;
151 if (!qemu_input_find_handler(1 << event
->type
, con
)) {
152 error_setg(errp
, "Input handler not found for "
154 InputEventKind_str(event
->type
));
159 for (e
= events
; e
!= NULL
; e
= e
->next
) {
160 InputEvent
*evt
= e
->value
;
162 if (evt
->type
== INPUT_EVENT_KIND_KEY
&&
163 evt
->u
.key
.data
->key
->type
== KEY_VALUE_KIND_NUMBER
) {
164 KeyValue
*key
= evt
->u
.key
.data
->key
;
165 QKeyCode code
= qemu_input_key_number_to_qcode(key
->u
.number
.data
);
166 qemu_input_event_send_key_qcode(con
, code
, evt
->u
.key
.data
->down
);
168 qemu_input_event_send(con
, evt
);
172 qemu_input_event_sync();
175 static int qemu_input_transform_invert_abs_value(int value
)
177 return (int64_t)INPUT_EVENT_ABS_MAX
- value
+ INPUT_EVENT_ABS_MIN
;
180 static void qemu_input_transform_abs_rotate(InputEvent
*evt
)
182 InputMoveEvent
*move
= evt
->u
.abs
.data
;
183 switch (graphic_rotate
) {
185 if (move
->axis
== INPUT_AXIS_X
) {
186 move
->axis
= INPUT_AXIS_Y
;
187 } else if (move
->axis
== INPUT_AXIS_Y
) {
188 move
->axis
= INPUT_AXIS_X
;
189 move
->value
= qemu_input_transform_invert_abs_value(move
->value
);
193 move
->value
= qemu_input_transform_invert_abs_value(move
->value
);
196 if (move
->axis
== INPUT_AXIS_X
) {
197 move
->axis
= INPUT_AXIS_Y
;
198 move
->value
= qemu_input_transform_invert_abs_value(move
->value
);
199 } else if (move
->axis
== INPUT_AXIS_Y
) {
200 move
->axis
= INPUT_AXIS_X
;
206 static void qemu_input_event_trace(QemuConsole
*src
, InputEvent
*evt
)
212 InputMoveEvent
*move
;
215 idx
= qemu_console_get_index(src
);
218 case INPUT_EVENT_KIND_KEY
:
219 key
= evt
->u
.key
.data
;
220 switch (key
->key
->type
) {
221 case KEY_VALUE_KIND_NUMBER
:
222 qcode
= qemu_input_key_number_to_qcode(key
->key
->u
.number
.data
);
223 name
= QKeyCode_str(qcode
);
224 trace_input_event_key_number(idx
, key
->key
->u
.number
.data
,
227 case KEY_VALUE_KIND_QCODE
:
228 name
= QKeyCode_str(key
->key
->u
.qcode
.data
);
229 trace_input_event_key_qcode(idx
, name
, key
->down
);
231 case KEY_VALUE_KIND__MAX
:
236 case INPUT_EVENT_KIND_BTN
:
237 btn
= evt
->u
.btn
.data
;
238 name
= InputButton_str(btn
->button
);
239 trace_input_event_btn(idx
, name
, btn
->down
);
241 case INPUT_EVENT_KIND_REL
:
242 move
= evt
->u
.rel
.data
;
243 name
= InputAxis_str(move
->axis
);
244 trace_input_event_rel(idx
, name
, move
->value
);
246 case INPUT_EVENT_KIND_ABS
:
247 move
= evt
->u
.abs
.data
;
248 name
= InputAxis_str(move
->axis
);
249 trace_input_event_abs(idx
, name
, move
->value
);
251 case INPUT_EVENT_KIND__MAX
:
257 static void qemu_input_queue_process(void *opaque
)
259 struct QemuInputEventQueueHead
*queue
= opaque
;
260 QemuInputEventQueue
*item
;
262 g_assert(!QTAILQ_EMPTY(queue
));
263 item
= QTAILQ_FIRST(queue
);
264 g_assert(item
->type
== QEMU_INPUT_QUEUE_DELAY
);
265 QTAILQ_REMOVE(queue
, item
, node
);
269 while (!QTAILQ_EMPTY(queue
)) {
270 item
= QTAILQ_FIRST(queue
);
271 switch (item
->type
) {
272 case QEMU_INPUT_QUEUE_DELAY
:
273 timer_mod(item
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
)
276 case QEMU_INPUT_QUEUE_EVENT
:
277 qemu_input_event_send(item
->src
, item
->evt
);
278 qapi_free_InputEvent(item
->evt
);
280 case QEMU_INPUT_QUEUE_SYNC
:
281 qemu_input_event_sync();
284 QTAILQ_REMOVE(queue
, item
, node
);
290 static void qemu_input_queue_delay(struct QemuInputEventQueueHead
*queue
,
291 QEMUTimer
*timer
, uint32_t delay_ms
)
293 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
294 bool start_timer
= QTAILQ_EMPTY(queue
);
296 item
->type
= QEMU_INPUT_QUEUE_DELAY
;
297 item
->delay_ms
= delay_ms
;
299 QTAILQ_INSERT_TAIL(queue
, item
, node
);
303 timer_mod(item
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
)
308 static void qemu_input_queue_event(struct QemuInputEventQueueHead
*queue
,
309 QemuConsole
*src
, InputEvent
*evt
)
311 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
313 item
->type
= QEMU_INPUT_QUEUE_EVENT
;
316 QTAILQ_INSERT_TAIL(queue
, item
, node
);
320 static void qemu_input_queue_sync(struct QemuInputEventQueueHead
*queue
)
322 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
324 item
->type
= QEMU_INPUT_QUEUE_SYNC
;
325 QTAILQ_INSERT_TAIL(queue
, item
, node
);
329 void qemu_input_event_send_impl(QemuConsole
*src
, InputEvent
*evt
)
331 QemuInputHandlerState
*s
;
333 qemu_input_event_trace(src
, evt
);
336 if (graphic_rotate
&& (evt
->type
== INPUT_EVENT_KIND_ABS
)) {
337 qemu_input_transform_abs_rotate(evt
);
341 s
= qemu_input_find_handler(1 << evt
->type
, src
);
345 s
->handler
->event(s
->dev
, src
, evt
);
349 void qemu_input_event_send(QemuConsole
*src
, InputEvent
*evt
)
351 /* Expect all parts of QEMU to send events with QCodes exclusively.
352 * Key numbers are only supported as end-user input via QMP */
353 assert(!(evt
->type
== INPUT_EVENT_KIND_KEY
&&
354 evt
->u
.key
.data
->key
->type
== KEY_VALUE_KIND_NUMBER
));
358 * 'sysrq' was mistakenly added to hack around the fact that
359 * the ps2 driver was not generating correct scancodes sequences
360 * when 'alt+print' was pressed. This flaw is now fixed and the
361 * 'sysrq' key serves no further purpose. We normalize it to
362 * 'print', so that downstream receivers of the event don't
363 * neeed to deal with this mistake
365 if (evt
->type
== INPUT_EVENT_KIND_KEY
&&
366 evt
->u
.key
.data
->key
->u
.qcode
.data
== Q_KEY_CODE_SYSRQ
) {
367 evt
->u
.key
.data
->key
->u
.qcode
.data
= Q_KEY_CODE_PRINT
;
370 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
374 replay_input_event(src
, evt
);
377 void qemu_input_event_sync_impl(void)
379 QemuInputHandlerState
*s
;
381 trace_input_event_sync();
383 QTAILQ_FOREACH(s
, &handlers
, node
) {
387 if (s
->handler
->sync
) {
388 s
->handler
->sync(s
->dev
);
394 void qemu_input_event_sync(void)
396 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
400 replay_input_sync_event();
403 static InputEvent
*qemu_input_event_new_key(KeyValue
*key
, bool down
)
405 InputEvent
*evt
= g_new0(InputEvent
, 1);
406 evt
->u
.key
.data
= g_new0(InputKeyEvent
, 1);
407 evt
->type
= INPUT_EVENT_KIND_KEY
;
408 evt
->u
.key
.data
->key
= key
;
409 evt
->u
.key
.data
->down
= down
;
413 void qemu_input_event_send_key(QemuConsole
*src
, KeyValue
*key
, bool down
)
416 evt
= qemu_input_event_new_key(key
, down
);
417 if (QTAILQ_EMPTY(&kbd_queue
)) {
418 qemu_input_event_send(src
, evt
);
419 qemu_input_event_sync();
420 qapi_free_InputEvent(evt
);
421 } else if (queue_count
< queue_limit
) {
422 qemu_input_queue_event(&kbd_queue
, src
, evt
);
423 qemu_input_queue_sync(&kbd_queue
);
425 qapi_free_InputEvent(evt
);
429 void qemu_input_event_send_key_number(QemuConsole
*src
, int num
, bool down
)
431 QKeyCode code
= qemu_input_key_number_to_qcode(num
);
432 qemu_input_event_send_key_qcode(src
, code
, down
);
435 void qemu_input_event_send_key_qcode(QemuConsole
*src
, QKeyCode q
, bool down
)
437 KeyValue
*key
= g_new0(KeyValue
, 1);
438 key
->type
= KEY_VALUE_KIND_QCODE
;
439 key
->u
.qcode
.data
= q
;
440 qemu_input_event_send_key(src
, key
, down
);
443 void qemu_input_event_send_key_delay(uint32_t delay_ms
)
445 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
450 kbd_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
, qemu_input_queue_process
,
453 if (queue_count
< queue_limit
) {
454 qemu_input_queue_delay(&kbd_queue
, kbd_timer
,
455 delay_ms
? delay_ms
: kbd_default_delay_ms
);
459 InputEvent
*qemu_input_event_new_btn(InputButton btn
, bool down
)
461 InputEvent
*evt
= g_new0(InputEvent
, 1);
462 evt
->u
.btn
.data
= g_new0(InputBtnEvent
, 1);
463 evt
->type
= INPUT_EVENT_KIND_BTN
;
464 evt
->u
.btn
.data
->button
= btn
;
465 evt
->u
.btn
.data
->down
= down
;
469 void qemu_input_queue_btn(QemuConsole
*src
, InputButton btn
, bool down
)
472 evt
= qemu_input_event_new_btn(btn
, down
);
473 qemu_input_event_send(src
, evt
);
474 qapi_free_InputEvent(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 InputEvent
*qemu_input_event_new_move(InputEventKind kind
,
515 InputAxis axis
, int value
)
517 InputEvent
*evt
= g_new0(InputEvent
, 1);
518 InputMoveEvent
*move
= g_new0(InputMoveEvent
, 1);
521 evt
->u
.rel
.data
= move
; /* evt->u.rel is the same as evt->u.abs */
527 void qemu_input_queue_rel(QemuConsole
*src
, InputAxis axis
, int value
)
530 evt
= qemu_input_event_new_move(INPUT_EVENT_KIND_REL
, axis
, value
);
531 qemu_input_event_send(src
, evt
);
532 qapi_free_InputEvent(evt
);
535 void qemu_input_queue_abs(QemuConsole
*src
, InputAxis axis
, int value
,
536 int min_in
, int max_in
)
539 int scaled
= qemu_input_scale_axis(value
, min_in
, max_in
,
541 INPUT_EVENT_ABS_MAX
);
542 evt
= qemu_input_event_new_move(INPUT_EVENT_KIND_ABS
, axis
, scaled
);
543 qemu_input_event_send(src
, evt
);
544 qapi_free_InputEvent(evt
);
547 void qemu_input_check_mode_change(void)
549 static int current_is_absolute
;
552 is_absolute
= qemu_input_is_absolute();
554 if (is_absolute
!= current_is_absolute
) {
555 trace_input_mouse_mode(is_absolute
);
556 notifier_list_notify(&mouse_mode_notifiers
, NULL
);
559 current_is_absolute
= is_absolute
;
562 void qemu_add_mouse_mode_change_notifier(Notifier
*notify
)
564 notifier_list_add(&mouse_mode_notifiers
, notify
);
567 void qemu_remove_mouse_mode_change_notifier(Notifier
*notify
)
569 notifier_remove(notify
);
572 MouseInfoList
*qmp_query_mice(Error
**errp
)
574 MouseInfoList
*mice_list
= NULL
;
576 QemuInputHandlerState
*s
;
579 QTAILQ_FOREACH(s
, &handlers
, node
) {
580 if (!(s
->handler
->mask
&
581 (INPUT_EVENT_MASK_REL
| INPUT_EVENT_MASK_ABS
))) {
585 info
= g_new0(MouseInfoList
, 1);
586 info
->value
= g_new0(MouseInfo
, 1);
587 info
->value
->index
= s
->id
;
588 info
->value
->name
= g_strdup(s
->handler
->name
);
589 info
->value
->absolute
= s
->handler
->mask
& INPUT_EVENT_MASK_ABS
;
590 info
->value
->current
= current
;
593 info
->next
= mice_list
;
600 void hmp_mouse_set(Monitor
*mon
, const QDict
*qdict
)
602 QemuInputHandlerState
*s
;
603 int index
= qdict_get_int(qdict
, "index");
606 QTAILQ_FOREACH(s
, &handlers
, node
) {
607 if (s
->id
!= index
) {
610 if (!(s
->handler
->mask
& (INPUT_EVENT_MASK_REL
|
611 INPUT_EVENT_MASK_ABS
))) {
612 error_report("Input device '%s' is not a mouse", s
->handler
->name
);
616 qemu_input_handler_activate(s
);
621 error_report("Mouse at index '%d' not found", index
);
624 qemu_input_check_mode_change();