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"
10 struct QemuInputHandlerState
{
12 QemuInputHandler
*handler
;
16 QTAILQ_ENTRY(QemuInputHandlerState
) node
;
19 typedef struct QemuInputEventQueue QemuInputEventQueue
;
20 struct QemuInputEventQueue
{
22 QEMU_INPUT_QUEUE_DELAY
= 1,
23 QEMU_INPUT_QUEUE_EVENT
,
24 QEMU_INPUT_QUEUE_SYNC
,
30 QTAILQ_ENTRY(QemuInputEventQueue
) node
;
33 static QTAILQ_HEAD(, QemuInputHandlerState
) handlers
=
34 QTAILQ_HEAD_INITIALIZER(handlers
);
35 static NotifierList mouse_mode_notifiers
=
36 NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers
);
38 static QTAILQ_HEAD(QemuInputEventQueueHead
, QemuInputEventQueue
) kbd_queue
=
39 QTAILQ_HEAD_INITIALIZER(kbd_queue
);
40 static QEMUTimer
*kbd_timer
;
41 static uint32_t kbd_default_delay_ms
= 10;
43 QemuInputHandlerState
*qemu_input_handler_register(DeviceState
*dev
,
44 QemuInputHandler
*handler
)
46 QemuInputHandlerState
*s
= g_new0(QemuInputHandlerState
, 1);
52 QTAILQ_INSERT_TAIL(&handlers
, s
, node
);
54 qemu_input_check_mode_change();
58 void qemu_input_handler_activate(QemuInputHandlerState
*s
)
60 QTAILQ_REMOVE(&handlers
, s
, node
);
61 QTAILQ_INSERT_HEAD(&handlers
, s
, node
);
62 qemu_input_check_mode_change();
65 void qemu_input_handler_deactivate(QemuInputHandlerState
*s
)
67 QTAILQ_REMOVE(&handlers
, s
, node
);
68 QTAILQ_INSERT_TAIL(&handlers
, s
, node
);
69 qemu_input_check_mode_change();
72 void qemu_input_handler_unregister(QemuInputHandlerState
*s
)
74 QTAILQ_REMOVE(&handlers
, s
, node
);
76 qemu_input_check_mode_change();
79 void qemu_input_handler_bind(QemuInputHandlerState
*s
,
80 const char *device_id
, int head
,
86 dev
= qdev_find_recursive(sysbus_get_default(), device_id
);
88 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
89 "Device '%s' not found", device_id
);
93 con
= qemu_console_lookup_by_device(dev
, head
);
95 error_setg(errp
, "Device %s is not bound to a QemuConsole", device_id
);
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_x_input_send_event(bool has_console
, int64_t console
,
128 InputEventList
*events
, Error
**errp
)
135 con
= qemu_console_lookup_by_index(console
);
137 error_setg(errp
, "console %" PRId64
" not found", console
);
142 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
143 error_setg(errp
, "VM not running");
147 for (e
= events
; e
!= NULL
; e
= e
->next
) {
148 InputEvent
*event
= e
->value
;
150 if (!qemu_input_find_handler(1 << event
->kind
, con
)) {
151 error_setg(errp
, "Input handler not found for "
153 InputEventKind_lookup
[event
->kind
]);
158 for (e
= events
; e
!= NULL
; e
= e
->next
) {
159 InputEvent
*event
= e
->value
;
161 qemu_input_event_send(con
, event
);
164 qemu_input_event_sync();
167 static void qemu_input_transform_abs_rotate(InputEvent
*evt
)
169 switch (graphic_rotate
) {
171 if (evt
->abs
->axis
== INPUT_AXIS_X
) {
172 evt
->abs
->axis
= INPUT_AXIS_Y
;
173 } else if (evt
->abs
->axis
== INPUT_AXIS_Y
) {
174 evt
->abs
->axis
= INPUT_AXIS_X
;
175 evt
->abs
->value
= INPUT_EVENT_ABS_SIZE
- 1 - evt
->abs
->value
;
179 evt
->abs
->value
= INPUT_EVENT_ABS_SIZE
- 1 - evt
->abs
->value
;
182 if (evt
->abs
->axis
== INPUT_AXIS_X
) {
183 evt
->abs
->axis
= INPUT_AXIS_Y
;
184 evt
->abs
->value
= INPUT_EVENT_ABS_SIZE
- 1 - evt
->abs
->value
;
185 } else if (evt
->abs
->axis
== INPUT_AXIS_Y
) {
186 evt
->abs
->axis
= INPUT_AXIS_X
;
192 static void qemu_input_event_trace(QemuConsole
*src
, InputEvent
*evt
)
198 idx
= qemu_console_get_index(src
);
201 case INPUT_EVENT_KIND_KEY
:
202 switch (evt
->key
->key
->kind
) {
203 case KEY_VALUE_KIND_NUMBER
:
204 qcode
= qemu_input_key_number_to_qcode(evt
->key
->key
->number
);
205 name
= QKeyCode_lookup
[qcode
];
206 trace_input_event_key_number(idx
, evt
->key
->key
->number
,
207 name
, evt
->key
->down
);
209 case KEY_VALUE_KIND_QCODE
:
210 name
= QKeyCode_lookup
[evt
->key
->key
->qcode
];
211 trace_input_event_key_qcode(idx
, name
, evt
->key
->down
);
213 case KEY_VALUE_KIND_MAX
:
218 case INPUT_EVENT_KIND_BTN
:
219 name
= InputButton_lookup
[evt
->btn
->button
];
220 trace_input_event_btn(idx
, name
, evt
->btn
->down
);
222 case INPUT_EVENT_KIND_REL
:
223 name
= InputAxis_lookup
[evt
->rel
->axis
];
224 trace_input_event_rel(idx
, name
, evt
->rel
->value
);
226 case INPUT_EVENT_KIND_ABS
:
227 name
= InputAxis_lookup
[evt
->abs
->axis
];
228 trace_input_event_abs(idx
, name
, evt
->abs
->value
);
230 case INPUT_EVENT_KIND_MAX
:
236 static void qemu_input_queue_process(void *opaque
)
238 struct QemuInputEventQueueHead
*queue
= opaque
;
239 QemuInputEventQueue
*item
;
241 g_assert(!QTAILQ_EMPTY(queue
));
242 item
= QTAILQ_FIRST(queue
);
243 g_assert(item
->type
== QEMU_INPUT_QUEUE_DELAY
);
244 QTAILQ_REMOVE(queue
, item
, node
);
247 while (!QTAILQ_EMPTY(queue
)) {
248 item
= QTAILQ_FIRST(queue
);
249 switch (item
->type
) {
250 case QEMU_INPUT_QUEUE_DELAY
:
251 timer_mod(item
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
)
254 case QEMU_INPUT_QUEUE_EVENT
:
255 qemu_input_event_send(item
->src
, item
->evt
);
256 qapi_free_InputEvent(item
->evt
);
258 case QEMU_INPUT_QUEUE_SYNC
:
259 qemu_input_event_sync();
262 QTAILQ_REMOVE(queue
, item
, node
);
267 static void qemu_input_queue_delay(struct QemuInputEventQueueHead
*queue
,
268 QEMUTimer
*timer
, uint32_t delay_ms
)
270 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
271 bool start_timer
= QTAILQ_EMPTY(queue
);
273 item
->type
= QEMU_INPUT_QUEUE_DELAY
;
274 item
->delay_ms
= delay_ms
;
276 QTAILQ_INSERT_TAIL(queue
, item
, node
);
279 timer_mod(item
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
)
284 static void qemu_input_queue_event(struct QemuInputEventQueueHead
*queue
,
285 QemuConsole
*src
, InputEvent
*evt
)
287 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
289 item
->type
= QEMU_INPUT_QUEUE_EVENT
;
292 QTAILQ_INSERT_TAIL(queue
, item
, node
);
295 static void qemu_input_queue_sync(struct QemuInputEventQueueHead
*queue
)
297 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
299 item
->type
= QEMU_INPUT_QUEUE_SYNC
;
300 QTAILQ_INSERT_TAIL(queue
, item
, node
);
303 void qemu_input_event_send(QemuConsole
*src
, InputEvent
*evt
)
305 QemuInputHandlerState
*s
;
307 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
311 qemu_input_event_trace(src
, evt
);
314 if (graphic_rotate
&& (evt
->kind
== INPUT_EVENT_KIND_ABS
)) {
315 qemu_input_transform_abs_rotate(evt
);
319 s
= qemu_input_find_handler(1 << evt
->kind
, src
);
323 s
->handler
->event(s
->dev
, src
, evt
);
327 void qemu_input_event_sync(void)
329 QemuInputHandlerState
*s
;
331 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
335 trace_input_event_sync();
337 QTAILQ_FOREACH(s
, &handlers
, node
) {
341 if (s
->handler
->sync
) {
342 s
->handler
->sync(s
->dev
);
348 InputEvent
*qemu_input_event_new_key(KeyValue
*key
, bool down
)
350 InputEvent
*evt
= g_new0(InputEvent
, 1);
351 evt
->key
= g_new0(InputKeyEvent
, 1);
352 evt
->kind
= INPUT_EVENT_KIND_KEY
;
354 evt
->key
->down
= down
;
358 void qemu_input_event_send_key(QemuConsole
*src
, KeyValue
*key
, bool down
)
361 evt
= qemu_input_event_new_key(key
, down
);
362 if (QTAILQ_EMPTY(&kbd_queue
)) {
363 qemu_input_event_send(src
, evt
);
364 qemu_input_event_sync();
365 qapi_free_InputEvent(evt
);
367 qemu_input_queue_event(&kbd_queue
, src
, evt
);
368 qemu_input_queue_sync(&kbd_queue
);
372 void qemu_input_event_send_key_number(QemuConsole
*src
, int num
, bool down
)
374 KeyValue
*key
= g_new0(KeyValue
, 1);
375 key
->kind
= KEY_VALUE_KIND_NUMBER
;
377 qemu_input_event_send_key(src
, key
, down
);
380 void qemu_input_event_send_key_qcode(QemuConsole
*src
, QKeyCode q
, bool down
)
382 KeyValue
*key
= g_new0(KeyValue
, 1);
383 key
->kind
= KEY_VALUE_KIND_QCODE
;
385 qemu_input_event_send_key(src
, key
, down
);
388 void qemu_input_event_send_key_delay(uint32_t delay_ms
)
391 kbd_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
, qemu_input_queue_process
,
394 qemu_input_queue_delay(&kbd_queue
, kbd_timer
,
395 delay_ms
? delay_ms
: kbd_default_delay_ms
);
398 InputEvent
*qemu_input_event_new_btn(InputButton btn
, bool down
)
400 InputEvent
*evt
= g_new0(InputEvent
, 1);
401 evt
->btn
= g_new0(InputBtnEvent
, 1);
402 evt
->kind
= INPUT_EVENT_KIND_BTN
;
403 evt
->btn
->button
= btn
;
404 evt
->btn
->down
= down
;
408 void qemu_input_queue_btn(QemuConsole
*src
, InputButton btn
, bool down
)
411 evt
= qemu_input_event_new_btn(btn
, down
);
412 qemu_input_event_send(src
, evt
);
413 qapi_free_InputEvent(evt
);
416 void qemu_input_update_buttons(QemuConsole
*src
, uint32_t *button_map
,
417 uint32_t button_old
, uint32_t button_new
)
422 for (btn
= 0; btn
< INPUT_BUTTON_MAX
; btn
++) {
423 mask
= button_map
[btn
];
424 if ((button_old
& mask
) == (button_new
& mask
)) {
427 qemu_input_queue_btn(src
, btn
, button_new
& mask
);
431 bool qemu_input_is_absolute(void)
433 QemuInputHandlerState
*s
;
435 s
= qemu_input_find_handler(INPUT_EVENT_MASK_REL
| INPUT_EVENT_MASK_ABS
,
437 return (s
!= NULL
) && (s
->handler
->mask
& INPUT_EVENT_MASK_ABS
);
440 int qemu_input_scale_axis(int value
, int size_in
, int size_out
)
445 return (int64_t)value
* (size_out
- 1) / (size_in
- 1);
448 InputEvent
*qemu_input_event_new_move(InputEventKind kind
,
449 InputAxis axis
, int value
)
451 InputEvent
*evt
= g_new0(InputEvent
, 1);
452 InputMoveEvent
*move
= g_new0(InputMoveEvent
, 1);
461 void qemu_input_queue_rel(QemuConsole
*src
, InputAxis axis
, int value
)
464 evt
= qemu_input_event_new_move(INPUT_EVENT_KIND_REL
, axis
, value
);
465 qemu_input_event_send(src
, evt
);
466 qapi_free_InputEvent(evt
);
469 void qemu_input_queue_abs(QemuConsole
*src
, InputAxis axis
, int value
, int size
)
472 int scaled
= qemu_input_scale_axis(value
, size
, INPUT_EVENT_ABS_SIZE
);
473 evt
= qemu_input_event_new_move(INPUT_EVENT_KIND_ABS
, axis
, scaled
);
474 qemu_input_event_send(src
, evt
);
475 qapi_free_InputEvent(evt
);
478 void qemu_input_check_mode_change(void)
480 static int current_is_absolute
;
483 is_absolute
= qemu_input_is_absolute();
485 if (is_absolute
!= current_is_absolute
) {
486 trace_input_mouse_mode(is_absolute
);
487 notifier_list_notify(&mouse_mode_notifiers
, NULL
);
490 current_is_absolute
= is_absolute
;
493 void qemu_add_mouse_mode_change_notifier(Notifier
*notify
)
495 notifier_list_add(&mouse_mode_notifiers
, notify
);
498 void qemu_remove_mouse_mode_change_notifier(Notifier
*notify
)
500 notifier_remove(notify
);
503 MouseInfoList
*qmp_query_mice(Error
**errp
)
505 MouseInfoList
*mice_list
= NULL
;
507 QemuInputHandlerState
*s
;
510 QTAILQ_FOREACH(s
, &handlers
, node
) {
511 if (!(s
->handler
->mask
&
512 (INPUT_EVENT_MASK_REL
| INPUT_EVENT_MASK_ABS
))) {
516 info
= g_new0(MouseInfoList
, 1);
517 info
->value
= g_new0(MouseInfo
, 1);
518 info
->value
->index
= s
->id
;
519 info
->value
->name
= g_strdup(s
->handler
->name
);
520 info
->value
->absolute
= s
->handler
->mask
& INPUT_EVENT_MASK_ABS
;
521 info
->value
->current
= current
;
524 info
->next
= mice_list
;
531 void hmp_mouse_set(Monitor
*mon
, const QDict
*qdict
)
533 QemuInputHandlerState
*s
;
534 int index
= qdict_get_int(qdict
, "index");
537 QTAILQ_FOREACH(s
, &handlers
, node
) {
538 if (s
->id
!= index
) {
541 if (!(s
->handler
->mask
& (INPUT_EVENT_MASK_REL
|
542 INPUT_EVENT_MASK_ABS
))) {
543 error_report("Input device '%s' is not a mouse", s
->handler
->name
);
547 qemu_input_handler_activate(s
);
552 error_report("Mouse at index '%d' not found", index
);
555 qemu_input_check_mode_change();