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;
45 QemuInputHandlerState
*qemu_input_handler_register(DeviceState
*dev
,
46 QemuInputHandler
*handler
)
48 QemuInputHandlerState
*s
= g_new0(QemuInputHandlerState
, 1);
54 QTAILQ_INSERT_TAIL(&handlers
, s
, node
);
56 qemu_input_check_mode_change();
60 void qemu_input_handler_activate(QemuInputHandlerState
*s
)
62 QTAILQ_REMOVE(&handlers
, s
, node
);
63 QTAILQ_INSERT_HEAD(&handlers
, s
, node
);
64 qemu_input_check_mode_change();
67 void qemu_input_handler_deactivate(QemuInputHandlerState
*s
)
69 QTAILQ_REMOVE(&handlers
, s
, node
);
70 QTAILQ_INSERT_TAIL(&handlers
, s
, node
);
71 qemu_input_check_mode_change();
74 void qemu_input_handler_unregister(QemuInputHandlerState
*s
)
76 QTAILQ_REMOVE(&handlers
, s
, node
);
78 qemu_input_check_mode_change();
81 void qemu_input_handler_bind(QemuInputHandlerState
*s
,
82 const char *device_id
, int head
,
88 dev
= qdev_find_recursive(sysbus_get_default(), device_id
);
90 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
91 "Device '%s' not found", device_id
);
95 con
= qemu_console_lookup_by_device(dev
, head
);
97 error_setg(errp
, "Device %s is not bound to a QemuConsole", device_id
);
104 static QemuInputHandlerState
*
105 qemu_input_find_handler(uint32_t mask
, QemuConsole
*con
)
107 QemuInputHandlerState
*s
;
109 QTAILQ_FOREACH(s
, &handlers
, node
) {
110 if (s
->con
== NULL
|| s
->con
!= con
) {
113 if (mask
& s
->handler
->mask
) {
118 QTAILQ_FOREACH(s
, &handlers
, node
) {
119 if (s
->con
!= NULL
) {
122 if (mask
& s
->handler
->mask
) {
129 void qmp_x_input_send_event(bool has_console
, int64_t console
,
130 InputEventList
*events
, Error
**errp
)
137 con
= qemu_console_lookup_by_index(console
);
139 error_setg(errp
, "console %" PRId64
" not found", console
);
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 switch (graphic_rotate
) {
173 if (evt
->u
.abs
->axis
== INPUT_AXIS_X
) {
174 evt
->u
.abs
->axis
= INPUT_AXIS_Y
;
175 } else if (evt
->u
.abs
->axis
== INPUT_AXIS_Y
) {
176 evt
->u
.abs
->axis
= INPUT_AXIS_X
;
177 evt
->u
.abs
->value
= INPUT_EVENT_ABS_SIZE
- 1 - evt
->u
.abs
->value
;
181 evt
->u
.abs
->value
= INPUT_EVENT_ABS_SIZE
- 1 - evt
->u
.abs
->value
;
184 if (evt
->u
.abs
->axis
== INPUT_AXIS_X
) {
185 evt
->u
.abs
->axis
= INPUT_AXIS_Y
;
186 evt
->u
.abs
->value
= INPUT_EVENT_ABS_SIZE
- 1 - evt
->u
.abs
->value
;
187 } else if (evt
->u
.abs
->axis
== INPUT_AXIS_Y
) {
188 evt
->u
.abs
->axis
= INPUT_AXIS_X
;
194 static void qemu_input_event_trace(QemuConsole
*src
, InputEvent
*evt
)
200 idx
= qemu_console_get_index(src
);
203 case INPUT_EVENT_KIND_KEY
:
204 switch (evt
->u
.key
->key
->type
) {
205 case KEY_VALUE_KIND_NUMBER
:
206 qcode
= qemu_input_key_number_to_qcode(evt
->u
.key
->key
->u
.number
);
207 name
= QKeyCode_lookup
[qcode
];
208 trace_input_event_key_number(idx
, evt
->u
.key
->key
->u
.number
,
209 name
, evt
->u
.key
->down
);
211 case KEY_VALUE_KIND_QCODE
:
212 name
= QKeyCode_lookup
[evt
->u
.key
->key
->u
.qcode
];
213 trace_input_event_key_qcode(idx
, name
, evt
->u
.key
->down
);
215 case KEY_VALUE_KIND__MAX
:
220 case INPUT_EVENT_KIND_BTN
:
221 name
= InputButton_lookup
[evt
->u
.btn
->button
];
222 trace_input_event_btn(idx
, name
, evt
->u
.btn
->down
);
224 case INPUT_EVENT_KIND_REL
:
225 name
= InputAxis_lookup
[evt
->u
.rel
->axis
];
226 trace_input_event_rel(idx
, name
, evt
->u
.rel
->value
);
228 case INPUT_EVENT_KIND_ABS
:
229 name
= InputAxis_lookup
[evt
->u
.abs
->axis
];
230 trace_input_event_abs(idx
, name
, evt
->u
.abs
->value
);
232 case INPUT_EVENT_KIND__MAX
:
238 static void qemu_input_queue_process(void *opaque
)
240 struct QemuInputEventQueueHead
*queue
= opaque
;
241 QemuInputEventQueue
*item
;
243 g_assert(!QTAILQ_EMPTY(queue
));
244 item
= QTAILQ_FIRST(queue
);
245 g_assert(item
->type
== QEMU_INPUT_QUEUE_DELAY
);
246 QTAILQ_REMOVE(queue
, item
, node
);
249 while (!QTAILQ_EMPTY(queue
)) {
250 item
= QTAILQ_FIRST(queue
);
251 switch (item
->type
) {
252 case QEMU_INPUT_QUEUE_DELAY
:
253 timer_mod(item
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
)
256 case QEMU_INPUT_QUEUE_EVENT
:
257 qemu_input_event_send(item
->src
, item
->evt
);
258 qapi_free_InputEvent(item
->evt
);
260 case QEMU_INPUT_QUEUE_SYNC
:
261 qemu_input_event_sync();
264 QTAILQ_REMOVE(queue
, item
, node
);
269 static void qemu_input_queue_delay(struct QemuInputEventQueueHead
*queue
,
270 QEMUTimer
*timer
, uint32_t delay_ms
)
272 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
273 bool start_timer
= QTAILQ_EMPTY(queue
);
275 item
->type
= QEMU_INPUT_QUEUE_DELAY
;
276 item
->delay_ms
= delay_ms
;
278 QTAILQ_INSERT_TAIL(queue
, item
, node
);
281 timer_mod(item
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
)
286 static void qemu_input_queue_event(struct QemuInputEventQueueHead
*queue
,
287 QemuConsole
*src
, InputEvent
*evt
)
289 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
291 item
->type
= QEMU_INPUT_QUEUE_EVENT
;
294 QTAILQ_INSERT_TAIL(queue
, item
, node
);
297 static void qemu_input_queue_sync(struct QemuInputEventQueueHead
*queue
)
299 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
301 item
->type
= QEMU_INPUT_QUEUE_SYNC
;
302 QTAILQ_INSERT_TAIL(queue
, item
, node
);
305 void qemu_input_event_send_impl(QemuConsole
*src
, InputEvent
*evt
)
307 QemuInputHandlerState
*s
;
309 qemu_input_event_trace(src
, evt
);
312 if (graphic_rotate
&& (evt
->type
== INPUT_EVENT_KIND_ABS
)) {
313 qemu_input_transform_abs_rotate(evt
);
317 s
= qemu_input_find_handler(1 << evt
->type
, src
);
321 s
->handler
->event(s
->dev
, src
, evt
);
325 void qemu_input_event_send(QemuConsole
*src
, InputEvent
*evt
)
327 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
331 replay_input_event(src
, evt
);
334 void qemu_input_event_sync_impl(void)
336 QemuInputHandlerState
*s
;
338 trace_input_event_sync();
340 QTAILQ_FOREACH(s
, &handlers
, node
) {
344 if (s
->handler
->sync
) {
345 s
->handler
->sync(s
->dev
);
351 void qemu_input_event_sync(void)
353 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
357 replay_input_sync_event();
360 InputEvent
*qemu_input_event_new_key(KeyValue
*key
, bool down
)
362 InputEvent
*evt
= g_new0(InputEvent
, 1);
363 evt
->u
.key
= g_new0(InputKeyEvent
, 1);
364 evt
->type
= INPUT_EVENT_KIND_KEY
;
365 evt
->u
.key
->key
= key
;
366 evt
->u
.key
->down
= down
;
370 void qemu_input_event_send_key(QemuConsole
*src
, KeyValue
*key
, bool down
)
373 evt
= qemu_input_event_new_key(key
, down
);
374 if (QTAILQ_EMPTY(&kbd_queue
)) {
375 qemu_input_event_send(src
, evt
);
376 qemu_input_event_sync();
377 qapi_free_InputEvent(evt
);
379 qemu_input_queue_event(&kbd_queue
, src
, evt
);
380 qemu_input_queue_sync(&kbd_queue
);
384 void qemu_input_event_send_key_number(QemuConsole
*src
, int num
, bool down
)
386 KeyValue
*key
= g_new0(KeyValue
, 1);
387 key
->type
= KEY_VALUE_KIND_NUMBER
;
389 qemu_input_event_send_key(src
, key
, down
);
392 void qemu_input_event_send_key_qcode(QemuConsole
*src
, QKeyCode q
, bool down
)
394 KeyValue
*key
= g_new0(KeyValue
, 1);
395 key
->type
= KEY_VALUE_KIND_QCODE
;
397 qemu_input_event_send_key(src
, key
, down
);
400 void qemu_input_event_send_key_delay(uint32_t delay_ms
)
403 kbd_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
, qemu_input_queue_process
,
406 qemu_input_queue_delay(&kbd_queue
, kbd_timer
,
407 delay_ms
? delay_ms
: kbd_default_delay_ms
);
410 InputEvent
*qemu_input_event_new_btn(InputButton btn
, bool down
)
412 InputEvent
*evt
= g_new0(InputEvent
, 1);
413 evt
->u
.btn
= g_new0(InputBtnEvent
, 1);
414 evt
->type
= INPUT_EVENT_KIND_BTN
;
415 evt
->u
.btn
->button
= btn
;
416 evt
->u
.btn
->down
= down
;
420 void qemu_input_queue_btn(QemuConsole
*src
, InputButton btn
, bool down
)
423 evt
= qemu_input_event_new_btn(btn
, down
);
424 qemu_input_event_send(src
, evt
);
425 qapi_free_InputEvent(evt
);
428 void qemu_input_update_buttons(QemuConsole
*src
, uint32_t *button_map
,
429 uint32_t button_old
, uint32_t button_new
)
434 for (btn
= 0; btn
< INPUT_BUTTON__MAX
; btn
++) {
435 mask
= button_map
[btn
];
436 if ((button_old
& mask
) == (button_new
& mask
)) {
439 qemu_input_queue_btn(src
, btn
, button_new
& mask
);
443 bool qemu_input_is_absolute(void)
445 QemuInputHandlerState
*s
;
447 s
= qemu_input_find_handler(INPUT_EVENT_MASK_REL
| INPUT_EVENT_MASK_ABS
,
449 return (s
!= NULL
) && (s
->handler
->mask
& INPUT_EVENT_MASK_ABS
);
452 int qemu_input_scale_axis(int value
, int size_in
, int size_out
)
457 return (int64_t)value
* (size_out
- 1) / (size_in
- 1);
460 InputEvent
*qemu_input_event_new_move(InputEventKind kind
,
461 InputAxis axis
, int value
)
463 InputEvent
*evt
= g_new0(InputEvent
, 1);
464 InputMoveEvent
*move
= g_new0(InputMoveEvent
, 1);
473 void qemu_input_queue_rel(QemuConsole
*src
, InputAxis axis
, int value
)
476 evt
= qemu_input_event_new_move(INPUT_EVENT_KIND_REL
, axis
, value
);
477 qemu_input_event_send(src
, evt
);
478 qapi_free_InputEvent(evt
);
481 void qemu_input_queue_abs(QemuConsole
*src
, InputAxis axis
, int value
, int size
)
484 int scaled
= qemu_input_scale_axis(value
, size
, INPUT_EVENT_ABS_SIZE
);
485 evt
= qemu_input_event_new_move(INPUT_EVENT_KIND_ABS
, axis
, scaled
);
486 qemu_input_event_send(src
, evt
);
487 qapi_free_InputEvent(evt
);
490 void qemu_input_check_mode_change(void)
492 static int current_is_absolute
;
495 is_absolute
= qemu_input_is_absolute();
497 if (is_absolute
!= current_is_absolute
) {
498 trace_input_mouse_mode(is_absolute
);
499 notifier_list_notify(&mouse_mode_notifiers
, NULL
);
502 current_is_absolute
= is_absolute
;
505 void qemu_add_mouse_mode_change_notifier(Notifier
*notify
)
507 notifier_list_add(&mouse_mode_notifiers
, notify
);
510 void qemu_remove_mouse_mode_change_notifier(Notifier
*notify
)
512 notifier_remove(notify
);
515 MouseInfoList
*qmp_query_mice(Error
**errp
)
517 MouseInfoList
*mice_list
= NULL
;
519 QemuInputHandlerState
*s
;
522 QTAILQ_FOREACH(s
, &handlers
, node
) {
523 if (!(s
->handler
->mask
&
524 (INPUT_EVENT_MASK_REL
| INPUT_EVENT_MASK_ABS
))) {
528 info
= g_new0(MouseInfoList
, 1);
529 info
->value
= g_new0(MouseInfo
, 1);
530 info
->value
->index
= s
->id
;
531 info
->value
->name
= g_strdup(s
->handler
->name
);
532 info
->value
->absolute
= s
->handler
->mask
& INPUT_EVENT_MASK_ABS
;
533 info
->value
->current
= current
;
536 info
->next
= mice_list
;
543 void hmp_mouse_set(Monitor
*mon
, const QDict
*qdict
)
545 QemuInputHandlerState
*s
;
546 int index
= qdict_get_int(qdict
, "index");
549 QTAILQ_FOREACH(s
, &handlers
, node
) {
550 if (s
->id
!= index
) {
553 if (!(s
->handler
->mask
& (INPUT_EVENT_MASK_REL
|
554 INPUT_EVENT_MASK_ABS
))) {
555 error_report("Input device '%s' is not a mouse", s
->handler
->name
);
559 qemu_input_handler_activate(s
);
564 error_report("Mouse at index '%d' not found", index
);
567 qemu_input_check_mode_change();