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
*event
= e
->value
;
162 qemu_input_event_send(con
, event
);
165 qemu_input_event_sync();
168 static int qemu_input_transform_invert_abs_value(int value
)
170 return (int64_t)INPUT_EVENT_ABS_MAX
- value
+ INPUT_EVENT_ABS_MIN
;
173 static void qemu_input_transform_abs_rotate(InputEvent
*evt
)
175 InputMoveEvent
*move
= evt
->u
.abs
.data
;
176 switch (graphic_rotate
) {
178 if (move
->axis
== INPUT_AXIS_X
) {
179 move
->axis
= INPUT_AXIS_Y
;
180 } else if (move
->axis
== INPUT_AXIS_Y
) {
181 move
->axis
= INPUT_AXIS_X
;
182 move
->value
= qemu_input_transform_invert_abs_value(move
->value
);
186 move
->value
= qemu_input_transform_invert_abs_value(move
->value
);
189 if (move
->axis
== INPUT_AXIS_X
) {
190 move
->axis
= INPUT_AXIS_Y
;
191 move
->value
= qemu_input_transform_invert_abs_value(move
->value
);
192 } else if (move
->axis
== INPUT_AXIS_Y
) {
193 move
->axis
= INPUT_AXIS_X
;
199 static void qemu_input_event_trace(QemuConsole
*src
, InputEvent
*evt
)
205 InputMoveEvent
*move
;
208 idx
= qemu_console_get_index(src
);
211 case INPUT_EVENT_KIND_KEY
:
212 key
= evt
->u
.key
.data
;
213 switch (key
->key
->type
) {
214 case KEY_VALUE_KIND_NUMBER
:
215 qcode
= qemu_input_key_number_to_qcode(key
->key
->u
.number
.data
);
216 name
= QKeyCode_str(qcode
);
217 trace_input_event_key_number(idx
, key
->key
->u
.number
.data
,
220 case KEY_VALUE_KIND_QCODE
:
221 name
= QKeyCode_str(key
->key
->u
.qcode
.data
);
222 trace_input_event_key_qcode(idx
, name
, key
->down
);
224 case KEY_VALUE_KIND__MAX
:
229 case INPUT_EVENT_KIND_BTN
:
230 btn
= evt
->u
.btn
.data
;
231 name
= InputButton_str(btn
->button
);
232 trace_input_event_btn(idx
, name
, btn
->down
);
234 case INPUT_EVENT_KIND_REL
:
235 move
= evt
->u
.rel
.data
;
236 name
= InputAxis_str(move
->axis
);
237 trace_input_event_rel(idx
, name
, move
->value
);
239 case INPUT_EVENT_KIND_ABS
:
240 move
= evt
->u
.abs
.data
;
241 name
= InputAxis_str(move
->axis
);
242 trace_input_event_abs(idx
, name
, move
->value
);
244 case INPUT_EVENT_KIND__MAX
:
250 static void qemu_input_queue_process(void *opaque
)
252 struct QemuInputEventQueueHead
*queue
= opaque
;
253 QemuInputEventQueue
*item
;
255 g_assert(!QTAILQ_EMPTY(queue
));
256 item
= QTAILQ_FIRST(queue
);
257 g_assert(item
->type
== QEMU_INPUT_QUEUE_DELAY
);
258 QTAILQ_REMOVE(queue
, item
, node
);
262 while (!QTAILQ_EMPTY(queue
)) {
263 item
= QTAILQ_FIRST(queue
);
264 switch (item
->type
) {
265 case QEMU_INPUT_QUEUE_DELAY
:
266 timer_mod(item
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
)
269 case QEMU_INPUT_QUEUE_EVENT
:
270 qemu_input_event_send(item
->src
, item
->evt
);
271 qapi_free_InputEvent(item
->evt
);
273 case QEMU_INPUT_QUEUE_SYNC
:
274 qemu_input_event_sync();
277 QTAILQ_REMOVE(queue
, item
, node
);
283 static void qemu_input_queue_delay(struct QemuInputEventQueueHead
*queue
,
284 QEMUTimer
*timer
, uint32_t delay_ms
)
286 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
287 bool start_timer
= QTAILQ_EMPTY(queue
);
289 item
->type
= QEMU_INPUT_QUEUE_DELAY
;
290 item
->delay_ms
= delay_ms
;
292 QTAILQ_INSERT_TAIL(queue
, item
, node
);
296 timer_mod(item
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
)
301 static void qemu_input_queue_event(struct QemuInputEventQueueHead
*queue
,
302 QemuConsole
*src
, InputEvent
*evt
)
304 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
306 item
->type
= QEMU_INPUT_QUEUE_EVENT
;
309 QTAILQ_INSERT_TAIL(queue
, item
, node
);
313 static void qemu_input_queue_sync(struct QemuInputEventQueueHead
*queue
)
315 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
317 item
->type
= QEMU_INPUT_QUEUE_SYNC
;
318 QTAILQ_INSERT_TAIL(queue
, item
, node
);
322 void qemu_input_event_send_impl(QemuConsole
*src
, InputEvent
*evt
)
324 QemuInputHandlerState
*s
;
326 qemu_input_event_trace(src
, evt
);
329 if (graphic_rotate
&& (evt
->type
== INPUT_EVENT_KIND_ABS
)) {
330 qemu_input_transform_abs_rotate(evt
);
334 s
= qemu_input_find_handler(1 << evt
->type
, src
);
338 s
->handler
->event(s
->dev
, src
, evt
);
342 void qemu_input_event_send(QemuConsole
*src
, InputEvent
*evt
)
344 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
348 replay_input_event(src
, evt
);
351 void qemu_input_event_sync_impl(void)
353 QemuInputHandlerState
*s
;
355 trace_input_event_sync();
357 QTAILQ_FOREACH(s
, &handlers
, node
) {
361 if (s
->handler
->sync
) {
362 s
->handler
->sync(s
->dev
);
368 void qemu_input_event_sync(void)
370 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
374 replay_input_sync_event();
377 InputEvent
*qemu_input_event_new_key(KeyValue
*key
, bool down
)
379 InputEvent
*evt
= g_new0(InputEvent
, 1);
380 evt
->u
.key
.data
= g_new0(InputKeyEvent
, 1);
381 evt
->type
= INPUT_EVENT_KIND_KEY
;
382 evt
->u
.key
.data
->key
= key
;
383 evt
->u
.key
.data
->down
= down
;
387 void qemu_input_event_send_key(QemuConsole
*src
, KeyValue
*key
, bool down
)
390 evt
= qemu_input_event_new_key(key
, down
);
391 if (QTAILQ_EMPTY(&kbd_queue
)) {
392 qemu_input_event_send(src
, evt
);
393 qemu_input_event_sync();
394 qapi_free_InputEvent(evt
);
395 } else if (queue_count
< queue_limit
) {
396 qemu_input_queue_event(&kbd_queue
, src
, evt
);
397 qemu_input_queue_sync(&kbd_queue
);
401 void qemu_input_event_send_key_number(QemuConsole
*src
, int num
, bool down
)
403 KeyValue
*key
= g_new0(KeyValue
, 1);
404 key
->type
= KEY_VALUE_KIND_NUMBER
;
405 key
->u
.number
.data
= num
;
406 qemu_input_event_send_key(src
, key
, down
);
409 void qemu_input_event_send_key_qcode(QemuConsole
*src
, QKeyCode q
, bool down
)
411 KeyValue
*key
= g_new0(KeyValue
, 1);
412 key
->type
= KEY_VALUE_KIND_QCODE
;
413 key
->u
.qcode
.data
= q
;
414 qemu_input_event_send_key(src
, key
, down
);
417 void qemu_input_event_send_key_delay(uint32_t delay_ms
)
419 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
424 kbd_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
, qemu_input_queue_process
,
427 if (queue_count
< queue_limit
) {
428 qemu_input_queue_delay(&kbd_queue
, kbd_timer
,
429 delay_ms
? delay_ms
: kbd_default_delay_ms
);
433 InputEvent
*qemu_input_event_new_btn(InputButton btn
, bool down
)
435 InputEvent
*evt
= g_new0(InputEvent
, 1);
436 evt
->u
.btn
.data
= g_new0(InputBtnEvent
, 1);
437 evt
->type
= INPUT_EVENT_KIND_BTN
;
438 evt
->u
.btn
.data
->button
= btn
;
439 evt
->u
.btn
.data
->down
= down
;
443 void qemu_input_queue_btn(QemuConsole
*src
, InputButton btn
, bool down
)
446 evt
= qemu_input_event_new_btn(btn
, down
);
447 qemu_input_event_send(src
, evt
);
448 qapi_free_InputEvent(evt
);
451 void qemu_input_update_buttons(QemuConsole
*src
, uint32_t *button_map
,
452 uint32_t button_old
, uint32_t button_new
)
457 for (btn
= 0; btn
< INPUT_BUTTON__MAX
; btn
++) {
458 mask
= button_map
[btn
];
459 if ((button_old
& mask
) == (button_new
& mask
)) {
462 qemu_input_queue_btn(src
, btn
, button_new
& mask
);
466 bool qemu_input_is_absolute(void)
468 QemuInputHandlerState
*s
;
470 s
= qemu_input_find_handler(INPUT_EVENT_MASK_REL
| INPUT_EVENT_MASK_ABS
,
472 return (s
!= NULL
) && (s
->handler
->mask
& INPUT_EVENT_MASK_ABS
);
475 int qemu_input_scale_axis(int value
,
476 int min_in
, int max_in
,
477 int min_out
, int max_out
)
479 int64_t range_in
= (int64_t)max_in
- min_in
;
480 int64_t range_out
= (int64_t)max_out
- min_out
;
483 return min_out
+ range_out
/ 2;
485 return ((int64_t)value
- min_in
) * range_out
/ range_in
+ min_out
;
488 InputEvent
*qemu_input_event_new_move(InputEventKind kind
,
489 InputAxis axis
, int value
)
491 InputEvent
*evt
= g_new0(InputEvent
, 1);
492 InputMoveEvent
*move
= g_new0(InputMoveEvent
, 1);
495 evt
->u
.rel
.data
= move
; /* evt->u.rel is the same as evt->u.abs */
501 void qemu_input_queue_rel(QemuConsole
*src
, InputAxis axis
, int value
)
504 evt
= qemu_input_event_new_move(INPUT_EVENT_KIND_REL
, axis
, value
);
505 qemu_input_event_send(src
, evt
);
506 qapi_free_InputEvent(evt
);
509 void qemu_input_queue_abs(QemuConsole
*src
, InputAxis axis
, int value
,
510 int min_in
, int max_in
)
513 int scaled
= qemu_input_scale_axis(value
, min_in
, max_in
,
515 INPUT_EVENT_ABS_MAX
);
516 evt
= qemu_input_event_new_move(INPUT_EVENT_KIND_ABS
, axis
, scaled
);
517 qemu_input_event_send(src
, evt
);
518 qapi_free_InputEvent(evt
);
521 void qemu_input_check_mode_change(void)
523 static int current_is_absolute
;
526 is_absolute
= qemu_input_is_absolute();
528 if (is_absolute
!= current_is_absolute
) {
529 trace_input_mouse_mode(is_absolute
);
530 notifier_list_notify(&mouse_mode_notifiers
, NULL
);
533 current_is_absolute
= is_absolute
;
536 void qemu_add_mouse_mode_change_notifier(Notifier
*notify
)
538 notifier_list_add(&mouse_mode_notifiers
, notify
);
541 void qemu_remove_mouse_mode_change_notifier(Notifier
*notify
)
543 notifier_remove(notify
);
546 MouseInfoList
*qmp_query_mice(Error
**errp
)
548 MouseInfoList
*mice_list
= NULL
;
550 QemuInputHandlerState
*s
;
553 QTAILQ_FOREACH(s
, &handlers
, node
) {
554 if (!(s
->handler
->mask
&
555 (INPUT_EVENT_MASK_REL
| INPUT_EVENT_MASK_ABS
))) {
559 info
= g_new0(MouseInfoList
, 1);
560 info
->value
= g_new0(MouseInfo
, 1);
561 info
->value
->index
= s
->id
;
562 info
->value
->name
= g_strdup(s
->handler
->name
);
563 info
->value
->absolute
= s
->handler
->mask
& INPUT_EVENT_MASK_ABS
;
564 info
->value
->current
= current
;
567 info
->next
= mice_list
;
574 void hmp_mouse_set(Monitor
*mon
, const QDict
*qdict
)
576 QemuInputHandlerState
*s
;
577 int index
= qdict_get_int(qdict
, "index");
580 QTAILQ_FOREACH(s
, &handlers
, node
) {
581 if (s
->id
!= index
) {
584 if (!(s
->handler
->mask
& (INPUT_EVENT_MASK_REL
|
585 INPUT_EVENT_MASK_ABS
))) {
586 error_report("Input device '%s' is not a mouse", s
->handler
->name
);
590 qemu_input_handler_activate(s
);
595 error_report("Mouse at index '%d' not found", index
);
598 qemu_input_check_mode_change();