1 #include "qemu/osdep.h"
3 #include "sysemu/sysemu.h"
4 #include "qapi-types.h"
5 #include "qemu/error-report.h"
6 #include "qmp-commands.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_lookup
[event
->type
]);
160 for (e
= events
; e
!= NULL
; e
= e
->next
) {
161 InputEvent
*event
= e
->value
;
163 qemu_input_event_send(con
, event
);
166 qemu_input_event_sync();
169 static void qemu_input_transform_abs_rotate(InputEvent
*evt
)
171 InputMoveEvent
*move
= evt
->u
.abs
.data
;
172 switch (graphic_rotate
) {
174 if (move
->axis
== INPUT_AXIS_X
) {
175 move
->axis
= INPUT_AXIS_Y
;
176 } else if (move
->axis
== INPUT_AXIS_Y
) {
177 move
->axis
= INPUT_AXIS_X
;
178 move
->value
= INPUT_EVENT_ABS_SIZE
- 1 - move
->value
;
182 move
->value
= INPUT_EVENT_ABS_SIZE
- 1 - move
->value
;
185 if (move
->axis
== INPUT_AXIS_X
) {
186 move
->axis
= INPUT_AXIS_Y
;
187 move
->value
= INPUT_EVENT_ABS_SIZE
- 1 - move
->value
;
188 } else if (move
->axis
== INPUT_AXIS_Y
) {
189 move
->axis
= INPUT_AXIS_X
;
195 static void qemu_input_event_trace(QemuConsole
*src
, InputEvent
*evt
)
201 InputMoveEvent
*move
;
204 idx
= qemu_console_get_index(src
);
207 case INPUT_EVENT_KIND_KEY
:
208 key
= evt
->u
.key
.data
;
209 switch (key
->key
->type
) {
210 case KEY_VALUE_KIND_NUMBER
:
211 qcode
= qemu_input_key_number_to_qcode(key
->key
->u
.number
.data
);
212 name
= QKeyCode_lookup
[qcode
];
213 trace_input_event_key_number(idx
, key
->key
->u
.number
.data
,
216 case KEY_VALUE_KIND_QCODE
:
217 name
= QKeyCode_lookup
[key
->key
->u
.qcode
.data
];
218 trace_input_event_key_qcode(idx
, name
, key
->down
);
220 case KEY_VALUE_KIND__MAX
:
225 case INPUT_EVENT_KIND_BTN
:
226 btn
= evt
->u
.btn
.data
;
227 name
= InputButton_lookup
[btn
->button
];
228 trace_input_event_btn(idx
, name
, btn
->down
);
230 case INPUT_EVENT_KIND_REL
:
231 move
= evt
->u
.rel
.data
;
232 name
= InputAxis_lookup
[move
->axis
];
233 trace_input_event_rel(idx
, name
, move
->value
);
235 case INPUT_EVENT_KIND_ABS
:
236 move
= evt
->u
.abs
.data
;
237 name
= InputAxis_lookup
[move
->axis
];
238 trace_input_event_abs(idx
, name
, move
->value
);
240 case INPUT_EVENT_KIND__MAX
:
246 static void qemu_input_queue_process(void *opaque
)
248 struct QemuInputEventQueueHead
*queue
= opaque
;
249 QemuInputEventQueue
*item
;
251 g_assert(!QTAILQ_EMPTY(queue
));
252 item
= QTAILQ_FIRST(queue
);
253 g_assert(item
->type
== QEMU_INPUT_QUEUE_DELAY
);
254 QTAILQ_REMOVE(queue
, item
, node
);
257 while (!QTAILQ_EMPTY(queue
)) {
258 item
= QTAILQ_FIRST(queue
);
259 switch (item
->type
) {
260 case QEMU_INPUT_QUEUE_DELAY
:
261 timer_mod(item
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
)
264 case QEMU_INPUT_QUEUE_EVENT
:
265 qemu_input_event_send(item
->src
, item
->evt
);
266 qapi_free_InputEvent(item
->evt
);
268 case QEMU_INPUT_QUEUE_SYNC
:
269 qemu_input_event_sync();
272 QTAILQ_REMOVE(queue
, item
, node
);
278 static void qemu_input_queue_delay(struct QemuInputEventQueueHead
*queue
,
279 QEMUTimer
*timer
, uint32_t delay_ms
)
281 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
282 bool start_timer
= QTAILQ_EMPTY(queue
);
284 item
->type
= QEMU_INPUT_QUEUE_DELAY
;
285 item
->delay_ms
= delay_ms
;
287 QTAILQ_INSERT_TAIL(queue
, item
, node
);
291 timer_mod(item
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
)
296 static void qemu_input_queue_event(struct QemuInputEventQueueHead
*queue
,
297 QemuConsole
*src
, InputEvent
*evt
)
299 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
301 item
->type
= QEMU_INPUT_QUEUE_EVENT
;
304 QTAILQ_INSERT_TAIL(queue
, item
, node
);
308 static void qemu_input_queue_sync(struct QemuInputEventQueueHead
*queue
)
310 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
312 item
->type
= QEMU_INPUT_QUEUE_SYNC
;
313 QTAILQ_INSERT_TAIL(queue
, item
, node
);
317 void qemu_input_event_send_impl(QemuConsole
*src
, InputEvent
*evt
)
319 QemuInputHandlerState
*s
;
321 qemu_input_event_trace(src
, evt
);
324 if (graphic_rotate
&& (evt
->type
== INPUT_EVENT_KIND_ABS
)) {
325 qemu_input_transform_abs_rotate(evt
);
329 s
= qemu_input_find_handler(1 << evt
->type
, src
);
333 s
->handler
->event(s
->dev
, src
, evt
);
337 void qemu_input_event_send(QemuConsole
*src
, InputEvent
*evt
)
339 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
343 replay_input_event(src
, evt
);
346 void qemu_input_event_sync_impl(void)
348 QemuInputHandlerState
*s
;
350 trace_input_event_sync();
352 QTAILQ_FOREACH(s
, &handlers
, node
) {
356 if (s
->handler
->sync
) {
357 s
->handler
->sync(s
->dev
);
363 void qemu_input_event_sync(void)
365 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
369 replay_input_sync_event();
372 InputEvent
*qemu_input_event_new_key(KeyValue
*key
, bool down
)
374 InputEvent
*evt
= g_new0(InputEvent
, 1);
375 evt
->u
.key
.data
= g_new0(InputKeyEvent
, 1);
376 evt
->type
= INPUT_EVENT_KIND_KEY
;
377 evt
->u
.key
.data
->key
= key
;
378 evt
->u
.key
.data
->down
= down
;
382 void qemu_input_event_send_key(QemuConsole
*src
, KeyValue
*key
, bool down
)
385 evt
= qemu_input_event_new_key(key
, down
);
386 if (QTAILQ_EMPTY(&kbd_queue
)) {
387 qemu_input_event_send(src
, evt
);
388 qemu_input_event_sync();
389 qapi_free_InputEvent(evt
);
390 } else if (queue_count
< queue_limit
) {
391 qemu_input_queue_event(&kbd_queue
, src
, evt
);
392 qemu_input_queue_sync(&kbd_queue
);
396 void qemu_input_event_send_key_number(QemuConsole
*src
, int num
, bool down
)
398 KeyValue
*key
= g_new0(KeyValue
, 1);
399 key
->type
= KEY_VALUE_KIND_NUMBER
;
400 key
->u
.number
.data
= num
;
401 qemu_input_event_send_key(src
, key
, down
);
404 void qemu_input_event_send_key_qcode(QemuConsole
*src
, QKeyCode q
, bool down
)
406 KeyValue
*key
= g_new0(KeyValue
, 1);
407 key
->type
= KEY_VALUE_KIND_QCODE
;
408 key
->u
.qcode
.data
= q
;
409 qemu_input_event_send_key(src
, key
, down
);
412 void qemu_input_event_send_key_delay(uint32_t delay_ms
)
414 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
419 kbd_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
, qemu_input_queue_process
,
422 if (queue_count
< queue_limit
) {
423 qemu_input_queue_delay(&kbd_queue
, kbd_timer
,
424 delay_ms
? delay_ms
: kbd_default_delay_ms
);
428 InputEvent
*qemu_input_event_new_btn(InputButton btn
, bool down
)
430 InputEvent
*evt
= g_new0(InputEvent
, 1);
431 evt
->u
.btn
.data
= g_new0(InputBtnEvent
, 1);
432 evt
->type
= INPUT_EVENT_KIND_BTN
;
433 evt
->u
.btn
.data
->button
= btn
;
434 evt
->u
.btn
.data
->down
= down
;
438 void qemu_input_queue_btn(QemuConsole
*src
, InputButton btn
, bool down
)
441 evt
= qemu_input_event_new_btn(btn
, down
);
442 qemu_input_event_send(src
, evt
);
443 qapi_free_InputEvent(evt
);
446 void qemu_input_update_buttons(QemuConsole
*src
, uint32_t *button_map
,
447 uint32_t button_old
, uint32_t button_new
)
452 for (btn
= 0; btn
< INPUT_BUTTON__MAX
; btn
++) {
453 mask
= button_map
[btn
];
454 if ((button_old
& mask
) == (button_new
& mask
)) {
457 qemu_input_queue_btn(src
, btn
, button_new
& mask
);
461 bool qemu_input_is_absolute(void)
463 QemuInputHandlerState
*s
;
465 s
= qemu_input_find_handler(INPUT_EVENT_MASK_REL
| INPUT_EVENT_MASK_ABS
,
467 return (s
!= NULL
) && (s
->handler
->mask
& INPUT_EVENT_MASK_ABS
);
470 int qemu_input_scale_axis(int value
, int size_in
, int size_out
)
475 return (int64_t)value
* (size_out
- 1) / (size_in
- 1);
478 InputEvent
*qemu_input_event_new_move(InputEventKind kind
,
479 InputAxis axis
, int value
)
481 InputEvent
*evt
= g_new0(InputEvent
, 1);
482 InputMoveEvent
*move
= g_new0(InputMoveEvent
, 1);
485 evt
->u
.rel
.data
= move
; /* evt->u.rel is the same as evt->u.abs */
491 void qemu_input_queue_rel(QemuConsole
*src
, InputAxis axis
, int value
)
494 evt
= qemu_input_event_new_move(INPUT_EVENT_KIND_REL
, axis
, value
);
495 qemu_input_event_send(src
, evt
);
496 qapi_free_InputEvent(evt
);
499 void qemu_input_queue_abs(QemuConsole
*src
, InputAxis axis
, int value
, int size
)
502 int scaled
= qemu_input_scale_axis(value
, size
, INPUT_EVENT_ABS_SIZE
);
503 evt
= qemu_input_event_new_move(INPUT_EVENT_KIND_ABS
, axis
, scaled
);
504 qemu_input_event_send(src
, evt
);
505 qapi_free_InputEvent(evt
);
508 void qemu_input_check_mode_change(void)
510 static int current_is_absolute
;
513 is_absolute
= qemu_input_is_absolute();
515 if (is_absolute
!= current_is_absolute
) {
516 trace_input_mouse_mode(is_absolute
);
517 notifier_list_notify(&mouse_mode_notifiers
, NULL
);
520 current_is_absolute
= is_absolute
;
523 void qemu_add_mouse_mode_change_notifier(Notifier
*notify
)
525 notifier_list_add(&mouse_mode_notifiers
, notify
);
528 void qemu_remove_mouse_mode_change_notifier(Notifier
*notify
)
530 notifier_remove(notify
);
533 MouseInfoList
*qmp_query_mice(Error
**errp
)
535 MouseInfoList
*mice_list
= NULL
;
537 QemuInputHandlerState
*s
;
540 QTAILQ_FOREACH(s
, &handlers
, node
) {
541 if (!(s
->handler
->mask
&
542 (INPUT_EVENT_MASK_REL
| INPUT_EVENT_MASK_ABS
))) {
546 info
= g_new0(MouseInfoList
, 1);
547 info
->value
= g_new0(MouseInfo
, 1);
548 info
->value
->index
= s
->id
;
549 info
->value
->name
= g_strdup(s
->handler
->name
);
550 info
->value
->absolute
= s
->handler
->mask
& INPUT_EVENT_MASK_ABS
;
551 info
->value
->current
= current
;
554 info
->next
= mice_list
;
561 void hmp_mouse_set(Monitor
*mon
, const QDict
*qdict
)
563 QemuInputHandlerState
*s
;
564 int index
= qdict_get_int(qdict
, "index");
567 QTAILQ_FOREACH(s
, &handlers
, node
) {
568 if (s
->id
!= index
) {
571 if (!(s
->handler
->mask
& (INPUT_EVENT_MASK_REL
|
572 INPUT_EVENT_MASK_ABS
))) {
573 error_report("Input device '%s' is not a mouse", s
->handler
->name
);
577 qemu_input_handler_activate(s
);
582 error_report("Mouse at index '%d' not found", index
);
585 qemu_input_check_mode_change();