2 #include "sysemu/sysemu.h"
3 #include "qapi-types.h"
4 #include "qmp-commands.h"
7 #include "ui/console.h"
9 struct QemuInputHandlerState
{
11 QemuInputHandler
*handler
;
15 QTAILQ_ENTRY(QemuInputHandlerState
) node
;
18 typedef struct QemuInputEventQueue QemuInputEventQueue
;
19 struct QemuInputEventQueue
{
21 QEMU_INPUT_QUEUE_DELAY
= 1,
22 QEMU_INPUT_QUEUE_EVENT
,
23 QEMU_INPUT_QUEUE_SYNC
,
29 QTAILQ_ENTRY(QemuInputEventQueue
) node
;
32 static QTAILQ_HEAD(, QemuInputHandlerState
) handlers
=
33 QTAILQ_HEAD_INITIALIZER(handlers
);
34 static NotifierList mouse_mode_notifiers
=
35 NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers
);
37 static QTAILQ_HEAD(QemuInputEventQueueHead
, QemuInputEventQueue
) kbd_queue
=
38 QTAILQ_HEAD_INITIALIZER(kbd_queue
);
39 static QEMUTimer
*kbd_timer
;
40 static uint32_t kbd_default_delay_ms
= 10;
42 QemuInputHandlerState
*qemu_input_handler_register(DeviceState
*dev
,
43 QemuInputHandler
*handler
)
45 QemuInputHandlerState
*s
= g_new0(QemuInputHandlerState
, 1);
51 QTAILQ_INSERT_TAIL(&handlers
, s
, node
);
53 qemu_input_check_mode_change();
57 void qemu_input_handler_activate(QemuInputHandlerState
*s
)
59 QTAILQ_REMOVE(&handlers
, s
, node
);
60 QTAILQ_INSERT_HEAD(&handlers
, s
, node
);
61 qemu_input_check_mode_change();
64 void qemu_input_handler_deactivate(QemuInputHandlerState
*s
)
66 QTAILQ_REMOVE(&handlers
, s
, node
);
67 QTAILQ_INSERT_TAIL(&handlers
, s
, node
);
68 qemu_input_check_mode_change();
71 void qemu_input_handler_unregister(QemuInputHandlerState
*s
)
73 QTAILQ_REMOVE(&handlers
, s
, node
);
75 qemu_input_check_mode_change();
78 void qemu_input_handler_bind(QemuInputHandlerState
*s
,
79 const char *device_id
, int head
,
85 dev
= qdev_find_recursive(sysbus_get_default(), device_id
);
87 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device_id
);
91 con
= qemu_console_lookup_by_device(dev
, head
);
93 error_setg(errp
, "Device %s is not bound to a QemuConsole", device_id
);
100 static QemuInputHandlerState
*
101 qemu_input_find_handler(uint32_t mask
, QemuConsole
*con
)
103 QemuInputHandlerState
*s
;
105 QTAILQ_FOREACH(s
, &handlers
, node
) {
106 if (s
->con
== NULL
|| s
->con
!= con
) {
109 if (mask
& s
->handler
->mask
) {
114 QTAILQ_FOREACH(s
, &handlers
, node
) {
115 if (s
->con
!= NULL
) {
118 if (mask
& s
->handler
->mask
) {
125 void qmp_x_input_send_event(bool has_console
, int64_t console
,
126 InputEventList
*events
, Error
**errp
)
133 con
= qemu_console_lookup_by_index(console
);
135 error_setg(errp
, "console %" PRId64
" not found", console
);
140 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
141 error_setg(errp
, "VM not running");
145 for (e
= events
; e
!= NULL
; e
= e
->next
) {
146 InputEvent
*event
= e
->value
;
148 if (!qemu_input_find_handler(1 << event
->kind
, con
)) {
149 error_setg(errp
, "Input handler not found for "
151 InputEventKind_lookup
[event
->kind
]);
156 for (e
= events
; e
!= NULL
; e
= e
->next
) {
157 InputEvent
*event
= e
->value
;
159 qemu_input_event_send(con
, event
);
162 qemu_input_event_sync();
165 static void qemu_input_transform_abs_rotate(InputEvent
*evt
)
167 switch (graphic_rotate
) {
169 if (evt
->abs
->axis
== INPUT_AXIS_X
) {
170 evt
->abs
->axis
= INPUT_AXIS_Y
;
171 } else if (evt
->abs
->axis
== INPUT_AXIS_Y
) {
172 evt
->abs
->axis
= INPUT_AXIS_X
;
173 evt
->abs
->value
= INPUT_EVENT_ABS_SIZE
- 1 - evt
->abs
->value
;
177 evt
->abs
->value
= INPUT_EVENT_ABS_SIZE
- 1 - evt
->abs
->value
;
180 if (evt
->abs
->axis
== INPUT_AXIS_X
) {
181 evt
->abs
->axis
= INPUT_AXIS_Y
;
182 evt
->abs
->value
= INPUT_EVENT_ABS_SIZE
- 1 - evt
->abs
->value
;
183 } else if (evt
->abs
->axis
== INPUT_AXIS_Y
) {
184 evt
->abs
->axis
= INPUT_AXIS_X
;
190 static void qemu_input_event_trace(QemuConsole
*src
, InputEvent
*evt
)
196 idx
= qemu_console_get_index(src
);
199 case INPUT_EVENT_KIND_KEY
:
200 switch (evt
->key
->key
->kind
) {
201 case KEY_VALUE_KIND_NUMBER
:
202 qcode
= qemu_input_key_number_to_qcode(evt
->key
->key
->number
);
203 name
= QKeyCode_lookup
[qcode
];
204 trace_input_event_key_number(idx
, evt
->key
->key
->number
,
205 name
, evt
->key
->down
);
207 case KEY_VALUE_KIND_QCODE
:
208 name
= QKeyCode_lookup
[evt
->key
->key
->qcode
];
209 trace_input_event_key_qcode(idx
, name
, evt
->key
->down
);
211 case KEY_VALUE_KIND_MAX
:
216 case INPUT_EVENT_KIND_BTN
:
217 name
= InputButton_lookup
[evt
->btn
->button
];
218 trace_input_event_btn(idx
, name
, evt
->btn
->down
);
220 case INPUT_EVENT_KIND_REL
:
221 name
= InputAxis_lookup
[evt
->rel
->axis
];
222 trace_input_event_rel(idx
, name
, evt
->rel
->value
);
224 case INPUT_EVENT_KIND_ABS
:
225 name
= InputAxis_lookup
[evt
->abs
->axis
];
226 trace_input_event_abs(idx
, name
, evt
->abs
->value
);
228 case INPUT_EVENT_KIND_MAX
:
234 static void qemu_input_queue_process(void *opaque
)
236 struct QemuInputEventQueueHead
*queue
= opaque
;
237 QemuInputEventQueue
*item
;
239 g_assert(!QTAILQ_EMPTY(queue
));
240 item
= QTAILQ_FIRST(queue
);
241 g_assert(item
->type
== QEMU_INPUT_QUEUE_DELAY
);
242 QTAILQ_REMOVE(queue
, item
, node
);
245 while (!QTAILQ_EMPTY(queue
)) {
246 item
= QTAILQ_FIRST(queue
);
247 switch (item
->type
) {
248 case QEMU_INPUT_QUEUE_DELAY
:
249 timer_mod(item
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
)
252 case QEMU_INPUT_QUEUE_EVENT
:
253 qemu_input_event_send(item
->src
, item
->evt
);
254 qapi_free_InputEvent(item
->evt
);
256 case QEMU_INPUT_QUEUE_SYNC
:
257 qemu_input_event_sync();
260 QTAILQ_REMOVE(queue
, item
, node
);
265 static void qemu_input_queue_delay(struct QemuInputEventQueueHead
*queue
,
266 QEMUTimer
*timer
, uint32_t delay_ms
)
268 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
269 bool start_timer
= QTAILQ_EMPTY(queue
);
271 item
->type
= QEMU_INPUT_QUEUE_DELAY
;
272 item
->delay_ms
= delay_ms
;
274 QTAILQ_INSERT_TAIL(queue
, item
, node
);
277 timer_mod(item
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
)
282 static void qemu_input_queue_event(struct QemuInputEventQueueHead
*queue
,
283 QemuConsole
*src
, InputEvent
*evt
)
285 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
287 item
->type
= QEMU_INPUT_QUEUE_EVENT
;
290 QTAILQ_INSERT_TAIL(queue
, item
, node
);
293 static void qemu_input_queue_sync(struct QemuInputEventQueueHead
*queue
)
295 QemuInputEventQueue
*item
= g_new0(QemuInputEventQueue
, 1);
297 item
->type
= QEMU_INPUT_QUEUE_SYNC
;
298 QTAILQ_INSERT_TAIL(queue
, item
, node
);
301 void qemu_input_event_send(QemuConsole
*src
, InputEvent
*evt
)
303 QemuInputHandlerState
*s
;
305 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
309 qemu_input_event_trace(src
, evt
);
312 if (graphic_rotate
&& (evt
->kind
== INPUT_EVENT_KIND_ABS
)) {
313 qemu_input_transform_abs_rotate(evt
);
317 s
= qemu_input_find_handler(1 << evt
->kind
, src
);
321 s
->handler
->event(s
->dev
, src
, evt
);
325 void qemu_input_event_sync(void)
327 QemuInputHandlerState
*s
;
329 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
333 trace_input_event_sync();
335 QTAILQ_FOREACH(s
, &handlers
, node
) {
339 if (s
->handler
->sync
) {
340 s
->handler
->sync(s
->dev
);
346 InputEvent
*qemu_input_event_new_key(KeyValue
*key
, bool down
)
348 InputEvent
*evt
= g_new0(InputEvent
, 1);
349 evt
->key
= g_new0(InputKeyEvent
, 1);
350 evt
->kind
= INPUT_EVENT_KIND_KEY
;
352 evt
->key
->down
= down
;
356 void qemu_input_event_send_key(QemuConsole
*src
, KeyValue
*key
, bool down
)
359 evt
= qemu_input_event_new_key(key
, down
);
360 if (QTAILQ_EMPTY(&kbd_queue
)) {
361 qemu_input_event_send(src
, evt
);
362 qemu_input_event_sync();
363 qapi_free_InputEvent(evt
);
365 qemu_input_queue_event(&kbd_queue
, src
, evt
);
366 qemu_input_queue_sync(&kbd_queue
);
370 void qemu_input_event_send_key_number(QemuConsole
*src
, int num
, bool down
)
372 KeyValue
*key
= g_new0(KeyValue
, 1);
373 key
->kind
= KEY_VALUE_KIND_NUMBER
;
375 qemu_input_event_send_key(src
, key
, down
);
378 void qemu_input_event_send_key_qcode(QemuConsole
*src
, QKeyCode q
, bool down
)
380 KeyValue
*key
= g_new0(KeyValue
, 1);
381 key
->kind
= KEY_VALUE_KIND_QCODE
;
383 qemu_input_event_send_key(src
, key
, down
);
386 void qemu_input_event_send_key_delay(uint32_t delay_ms
)
389 kbd_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
, qemu_input_queue_process
,
392 qemu_input_queue_delay(&kbd_queue
, kbd_timer
,
393 delay_ms
? delay_ms
: kbd_default_delay_ms
);
396 InputEvent
*qemu_input_event_new_btn(InputButton btn
, bool down
)
398 InputEvent
*evt
= g_new0(InputEvent
, 1);
399 evt
->btn
= g_new0(InputBtnEvent
, 1);
400 evt
->kind
= INPUT_EVENT_KIND_BTN
;
401 evt
->btn
->button
= btn
;
402 evt
->btn
->down
= down
;
406 void qemu_input_queue_btn(QemuConsole
*src
, InputButton btn
, bool down
)
409 evt
= qemu_input_event_new_btn(btn
, down
);
410 qemu_input_event_send(src
, evt
);
411 qapi_free_InputEvent(evt
);
414 void qemu_input_update_buttons(QemuConsole
*src
, uint32_t *button_map
,
415 uint32_t button_old
, uint32_t button_new
)
420 for (btn
= 0; btn
< INPUT_BUTTON_MAX
; btn
++) {
421 mask
= button_map
[btn
];
422 if ((button_old
& mask
) == (button_new
& mask
)) {
425 qemu_input_queue_btn(src
, btn
, button_new
& mask
);
429 bool qemu_input_is_absolute(void)
431 QemuInputHandlerState
*s
;
433 s
= qemu_input_find_handler(INPUT_EVENT_MASK_REL
| INPUT_EVENT_MASK_ABS
,
435 return (s
!= NULL
) && (s
->handler
->mask
& INPUT_EVENT_MASK_ABS
);
438 int qemu_input_scale_axis(int value
, int size_in
, int size_out
)
443 return (int64_t)value
* (size_out
- 1) / (size_in
- 1);
446 InputEvent
*qemu_input_event_new_move(InputEventKind kind
,
447 InputAxis axis
, int value
)
449 InputEvent
*evt
= g_new0(InputEvent
, 1);
450 InputMoveEvent
*move
= g_new0(InputMoveEvent
, 1);
459 void qemu_input_queue_rel(QemuConsole
*src
, InputAxis axis
, int value
)
462 evt
= qemu_input_event_new_move(INPUT_EVENT_KIND_REL
, axis
, value
);
463 qemu_input_event_send(src
, evt
);
464 qapi_free_InputEvent(evt
);
467 void qemu_input_queue_abs(QemuConsole
*src
, InputAxis axis
, int value
, int size
)
470 int scaled
= qemu_input_scale_axis(value
, size
, INPUT_EVENT_ABS_SIZE
);
471 evt
= qemu_input_event_new_move(INPUT_EVENT_KIND_ABS
, axis
, scaled
);
472 qemu_input_event_send(src
, evt
);
473 qapi_free_InputEvent(evt
);
476 void qemu_input_check_mode_change(void)
478 static int current_is_absolute
;
481 is_absolute
= qemu_input_is_absolute();
483 if (is_absolute
!= current_is_absolute
) {
484 trace_input_mouse_mode(is_absolute
);
485 notifier_list_notify(&mouse_mode_notifiers
, NULL
);
488 current_is_absolute
= is_absolute
;
491 void qemu_add_mouse_mode_change_notifier(Notifier
*notify
)
493 notifier_list_add(&mouse_mode_notifiers
, notify
);
496 void qemu_remove_mouse_mode_change_notifier(Notifier
*notify
)
498 notifier_remove(notify
);
501 MouseInfoList
*qmp_query_mice(Error
**errp
)
503 MouseInfoList
*mice_list
= NULL
;
505 QemuInputHandlerState
*s
;
508 QTAILQ_FOREACH(s
, &handlers
, node
) {
509 if (!(s
->handler
->mask
&
510 (INPUT_EVENT_MASK_REL
| INPUT_EVENT_MASK_ABS
))) {
514 info
= g_new0(MouseInfoList
, 1);
515 info
->value
= g_new0(MouseInfo
, 1);
516 info
->value
->index
= s
->id
;
517 info
->value
->name
= g_strdup(s
->handler
->name
);
518 info
->value
->absolute
= s
->handler
->mask
& INPUT_EVENT_MASK_ABS
;
519 info
->value
->current
= current
;
522 info
->next
= mice_list
;
529 void hmp_mouse_set(Monitor
*mon
, const QDict
*qdict
)
531 QemuInputHandlerState
*s
;
532 int index
= qdict_get_int(qdict
, "index");
535 QTAILQ_FOREACH(s
, &handlers
, node
) {
536 if (s
->id
!= index
) {
539 if (!(s
->handler
->mask
& (INPUT_EVENT_MASK_REL
|
540 INPUT_EVENT_MASK_ABS
))) {
541 error_report("Input device '%s' is not a mouse", s
->handler
->name
);
545 qemu_input_handler_activate(s
);
550 error_report("Mouse at index '%d' not found", index
);
553 qemu_input_check_mode_change();