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 con
= qemu_console_lookup_by_device_name(device_id
, head
, &err
);
90 error_propagate(errp
, err
);
97 static QemuInputHandlerState
*
98 qemu_input_find_handler(uint32_t mask
, QemuConsole
*con
)
100 QemuInputHandlerState
*s
;
102 QTAILQ_FOREACH(s
, &handlers
, node
) {
103 if (s
->con
== NULL
|| s
->con
!= con
) {
106 if (mask
& s
->handler
->mask
) {
111 QTAILQ_FOREACH(s
, &handlers
, node
) {
112 if (s
->con
!= NULL
) {
115 if (mask
& s
->handler
->mask
) {
122 void qmp_x_input_send_event(bool has_console
, int64_t console
,
123 InputEventList
*events
, Error
**errp
)
130 con
= qemu_console_lookup_by_index(console
);
132 error_setg(errp
, "console %" PRId64
" not found", console
);
137 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
138 error_setg(errp
, "VM not running");
142 for (e
= events
; e
!= NULL
; e
= e
->next
) {
143 InputEvent
*event
= e
->value
;
145 if (!qemu_input_find_handler(1 << event
->type
, con
)) {
146 error_setg(errp
, "Input handler not found for "
148 InputEventKind_lookup
[event
->type
]);
153 for (e
= events
; e
!= NULL
; e
= e
->next
) {
154 InputEvent
*event
= e
->value
;
156 qemu_input_event_send(con
, event
);
159 qemu_input_event_sync();
162 static void qemu_input_transform_abs_rotate(InputEvent
*evt
)
164 switch (graphic_rotate
) {
166 if (evt
->u
.abs
->axis
== INPUT_AXIS_X
) {
167 evt
->u
.abs
->axis
= INPUT_AXIS_Y
;
168 } else if (evt
->u
.abs
->axis
== INPUT_AXIS_Y
) {
169 evt
->u
.abs
->axis
= INPUT_AXIS_X
;
170 evt
->u
.abs
->value
= INPUT_EVENT_ABS_SIZE
- 1 - evt
->u
.abs
->value
;
174 evt
->u
.abs
->value
= INPUT_EVENT_ABS_SIZE
- 1 - evt
->u
.abs
->value
;
177 if (evt
->u
.abs
->axis
== INPUT_AXIS_X
) {
178 evt
->u
.abs
->axis
= INPUT_AXIS_Y
;
179 evt
->u
.abs
->value
= INPUT_EVENT_ABS_SIZE
- 1 - evt
->u
.abs
->value
;
180 } else if (evt
->u
.abs
->axis
== INPUT_AXIS_Y
) {
181 evt
->u
.abs
->axis
= INPUT_AXIS_X
;
187 static void qemu_input_event_trace(QemuConsole
*src
, InputEvent
*evt
)
193 idx
= qemu_console_get_index(src
);
196 case INPUT_EVENT_KIND_KEY
:
197 switch (evt
->u
.key
->key
->type
) {
198 case KEY_VALUE_KIND_NUMBER
:
199 qcode
= qemu_input_key_number_to_qcode(evt
->u
.key
->key
->u
.number
);
200 name
= QKeyCode_lookup
[qcode
];
201 trace_input_event_key_number(idx
, evt
->u
.key
->key
->u
.number
,
202 name
, evt
->u
.key
->down
);
204 case KEY_VALUE_KIND_QCODE
:
205 name
= QKeyCode_lookup
[evt
->u
.key
->key
->u
.qcode
];
206 trace_input_event_key_qcode(idx
, name
, evt
->u
.key
->down
);
208 case KEY_VALUE_KIND__MAX
:
213 case INPUT_EVENT_KIND_BTN
:
214 name
= InputButton_lookup
[evt
->u
.btn
->button
];
215 trace_input_event_btn(idx
, name
, evt
->u
.btn
->down
);
217 case INPUT_EVENT_KIND_REL
:
218 name
= InputAxis_lookup
[evt
->u
.rel
->axis
];
219 trace_input_event_rel(idx
, name
, evt
->u
.rel
->value
);
221 case INPUT_EVENT_KIND_ABS
:
222 name
= InputAxis_lookup
[evt
->u
.abs
->axis
];
223 trace_input_event_abs(idx
, name
, evt
->u
.abs
->value
);
225 case INPUT_EVENT_KIND__MAX
:
231 static void qemu_input_queue_process(void *opaque
)
233 struct QemuInputEventQueueHead
*queue
= opaque
;
234 QemuInputEventQueue
*item
;
236 g_assert(!QTAILQ_EMPTY(queue
));
237 item
= QTAILQ_FIRST(queue
);
238 g_assert(item
->type
== QEMU_INPUT_QUEUE_DELAY
);
239 QTAILQ_REMOVE(queue
, item
, node
);
242 while (!QTAILQ_EMPTY(queue
)) {
243 item
= QTAILQ_FIRST(queue
);
244 switch (item
->type
) {
245 case QEMU_INPUT_QUEUE_DELAY
:
246 timer_mod(item
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
)
249 case QEMU_INPUT_QUEUE_EVENT
:
250 qemu_input_event_send(item
->src
, item
->evt
);
251 qapi_free_InputEvent(item
->evt
);
253 case QEMU_INPUT_QUEUE_SYNC
:
254 qemu_input_event_sync();
257 QTAILQ_REMOVE(queue
, item
, node
);
262 static void qemu_input_queue_delay(struct QemuInputEventQueueHead
*queue
,
263 QEMUTimer
*timer
, uint32_t delay_ms
)
265 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
266 bool start_timer
= QTAILQ_EMPTY(queue
);
268 item
->type
= QEMU_INPUT_QUEUE_DELAY
;
269 item
->delay_ms
= delay_ms
;
271 QTAILQ_INSERT_TAIL(queue
, item
, node
);
274 timer_mod(item
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
)
279 static void qemu_input_queue_event(struct QemuInputEventQueueHead
*queue
,
280 QemuConsole
*src
, InputEvent
*evt
)
282 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
284 item
->type
= QEMU_INPUT_QUEUE_EVENT
;
287 QTAILQ_INSERT_TAIL(queue
, item
, node
);
290 static void qemu_input_queue_sync(struct QemuInputEventQueueHead
*queue
)
292 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
294 item
->type
= QEMU_INPUT_QUEUE_SYNC
;
295 QTAILQ_INSERT_TAIL(queue
, item
, node
);
298 void qemu_input_event_send_impl(QemuConsole
*src
, InputEvent
*evt
)
300 QemuInputHandlerState
*s
;
302 qemu_input_event_trace(src
, evt
);
305 if (graphic_rotate
&& (evt
->type
== INPUT_EVENT_KIND_ABS
)) {
306 qemu_input_transform_abs_rotate(evt
);
310 s
= qemu_input_find_handler(1 << evt
->type
, src
);
314 s
->handler
->event(s
->dev
, src
, evt
);
318 void qemu_input_event_send(QemuConsole
*src
, InputEvent
*evt
)
320 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
324 replay_input_event(src
, evt
);
327 void qemu_input_event_sync_impl(void)
329 QemuInputHandlerState
*s
;
331 trace_input_event_sync();
333 QTAILQ_FOREACH(s
, &handlers
, node
) {
337 if (s
->handler
->sync
) {
338 s
->handler
->sync(s
->dev
);
344 void qemu_input_event_sync(void)
346 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
350 replay_input_sync_event();
353 InputEvent
*qemu_input_event_new_key(KeyValue
*key
, bool down
)
355 InputEvent
*evt
= g_new0(InputEvent
, 1);
356 evt
->u
.key
= g_new0(InputKeyEvent
, 1);
357 evt
->type
= INPUT_EVENT_KIND_KEY
;
358 evt
->u
.key
->key
= key
;
359 evt
->u
.key
->down
= down
;
363 void qemu_input_event_send_key(QemuConsole
*src
, KeyValue
*key
, bool down
)
366 evt
= qemu_input_event_new_key(key
, down
);
367 if (QTAILQ_EMPTY(&kbd_queue
)) {
368 qemu_input_event_send(src
, evt
);
369 qemu_input_event_sync();
370 qapi_free_InputEvent(evt
);
372 qemu_input_queue_event(&kbd_queue
, src
, evt
);
373 qemu_input_queue_sync(&kbd_queue
);
377 void qemu_input_event_send_key_number(QemuConsole
*src
, int num
, bool down
)
379 KeyValue
*key
= g_new0(KeyValue
, 1);
380 key
->type
= KEY_VALUE_KIND_NUMBER
;
382 qemu_input_event_send_key(src
, key
, down
);
385 void qemu_input_event_send_key_qcode(QemuConsole
*src
, QKeyCode q
, bool down
)
387 KeyValue
*key
= g_new0(KeyValue
, 1);
388 key
->type
= KEY_VALUE_KIND_QCODE
;
390 qemu_input_event_send_key(src
, key
, down
);
393 void qemu_input_event_send_key_delay(uint32_t delay_ms
)
396 kbd_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
, qemu_input_queue_process
,
399 qemu_input_queue_delay(&kbd_queue
, kbd_timer
,
400 delay_ms
? delay_ms
: kbd_default_delay_ms
);
403 InputEvent
*qemu_input_event_new_btn(InputButton btn
, bool down
)
405 InputEvent
*evt
= g_new0(InputEvent
, 1);
406 evt
->u
.btn
= g_new0(InputBtnEvent
, 1);
407 evt
->type
= INPUT_EVENT_KIND_BTN
;
408 evt
->u
.btn
->button
= btn
;
409 evt
->u
.btn
->down
= down
;
413 void qemu_input_queue_btn(QemuConsole
*src
, InputButton btn
, bool down
)
416 evt
= qemu_input_event_new_btn(btn
, down
);
417 qemu_input_event_send(src
, evt
);
418 qapi_free_InputEvent(evt
);
421 void qemu_input_update_buttons(QemuConsole
*src
, uint32_t *button_map
,
422 uint32_t button_old
, uint32_t button_new
)
427 for (btn
= 0; btn
< INPUT_BUTTON__MAX
; btn
++) {
428 mask
= button_map
[btn
];
429 if ((button_old
& mask
) == (button_new
& mask
)) {
432 qemu_input_queue_btn(src
, btn
, button_new
& mask
);
436 bool qemu_input_is_absolute(void)
438 QemuInputHandlerState
*s
;
440 s
= qemu_input_find_handler(INPUT_EVENT_MASK_REL
| INPUT_EVENT_MASK_ABS
,
442 return (s
!= NULL
) && (s
->handler
->mask
& INPUT_EVENT_MASK_ABS
);
445 int qemu_input_scale_axis(int value
, int size_in
, int size_out
)
450 return (int64_t)value
* (size_out
- 1) / (size_in
- 1);
453 InputEvent
*qemu_input_event_new_move(InputEventKind kind
,
454 InputAxis axis
, int value
)
456 InputEvent
*evt
= g_new0(InputEvent
, 1);
457 InputMoveEvent
*move
= g_new0(InputMoveEvent
, 1);
466 void qemu_input_queue_rel(QemuConsole
*src
, InputAxis axis
, int value
)
469 evt
= qemu_input_event_new_move(INPUT_EVENT_KIND_REL
, axis
, value
);
470 qemu_input_event_send(src
, evt
);
471 qapi_free_InputEvent(evt
);
474 void qemu_input_queue_abs(QemuConsole
*src
, InputAxis axis
, int value
, int size
)
477 int scaled
= qemu_input_scale_axis(value
, size
, INPUT_EVENT_ABS_SIZE
);
478 evt
= qemu_input_event_new_move(INPUT_EVENT_KIND_ABS
, axis
, scaled
);
479 qemu_input_event_send(src
, evt
);
480 qapi_free_InputEvent(evt
);
483 void qemu_input_check_mode_change(void)
485 static int current_is_absolute
;
488 is_absolute
= qemu_input_is_absolute();
490 if (is_absolute
!= current_is_absolute
) {
491 trace_input_mouse_mode(is_absolute
);
492 notifier_list_notify(&mouse_mode_notifiers
, NULL
);
495 current_is_absolute
= is_absolute
;
498 void qemu_add_mouse_mode_change_notifier(Notifier
*notify
)
500 notifier_list_add(&mouse_mode_notifiers
, notify
);
503 void qemu_remove_mouse_mode_change_notifier(Notifier
*notify
)
505 notifier_remove(notify
);
508 MouseInfoList
*qmp_query_mice(Error
**errp
)
510 MouseInfoList
*mice_list
= NULL
;
512 QemuInputHandlerState
*s
;
515 QTAILQ_FOREACH(s
, &handlers
, node
) {
516 if (!(s
->handler
->mask
&
517 (INPUT_EVENT_MASK_REL
| INPUT_EVENT_MASK_ABS
))) {
521 info
= g_new0(MouseInfoList
, 1);
522 info
->value
= g_new0(MouseInfo
, 1);
523 info
->value
->index
= s
->id
;
524 info
->value
->name
= g_strdup(s
->handler
->name
);
525 info
->value
->absolute
= s
->handler
->mask
& INPUT_EVENT_MASK_ABS
;
526 info
->value
->current
= current
;
529 info
->next
= mice_list
;
536 void hmp_mouse_set(Monitor
*mon
, const QDict
*qdict
)
538 QemuInputHandlerState
*s
;
539 int index
= qdict_get_int(qdict
, "index");
542 QTAILQ_FOREACH(s
, &handlers
, node
) {
543 if (s
->id
!= index
) {
546 if (!(s
->handler
->mask
& (INPUT_EVENT_MASK_REL
|
547 INPUT_EVENT_MASK_ABS
))) {
548 error_report("Input device '%s' is not a mouse", s
->handler
->name
);
552 qemu_input_handler_activate(s
);
557 error_report("Mouse at index '%d' not found", index
);
560 qemu_input_check_mode_change();