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_input_send_event(int64_t console
, InputEventList
*events
,
131 con
= qemu_console_lookup_by_index(console
);
133 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
->kind
, con
)) {
146 error_setg(errp
, "Input handler not found for "
148 InputEventKind_lookup
[event
->kind
]);
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
->abs
->axis
== INPUT_AXIS_X
) {
167 evt
->abs
->axis
= INPUT_AXIS_Y
;
168 } else if (evt
->abs
->axis
== INPUT_AXIS_Y
) {
169 evt
->abs
->axis
= INPUT_AXIS_X
;
170 evt
->abs
->value
= INPUT_EVENT_ABS_SIZE
- 1 - evt
->abs
->value
;
174 evt
->abs
->value
= INPUT_EVENT_ABS_SIZE
- 1 - evt
->abs
->value
;
177 if (evt
->abs
->axis
== INPUT_AXIS_X
) {
178 evt
->abs
->axis
= INPUT_AXIS_Y
;
179 evt
->abs
->value
= INPUT_EVENT_ABS_SIZE
- 1 - evt
->abs
->value
;
180 } else if (evt
->abs
->axis
== INPUT_AXIS_Y
) {
181 evt
->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
->key
->key
->kind
) {
198 case KEY_VALUE_KIND_NUMBER
:
199 qcode
= qemu_input_key_number_to_qcode(evt
->key
->key
->number
);
200 name
= QKeyCode_lookup
[qcode
];
201 trace_input_event_key_number(idx
, evt
->key
->key
->number
,
202 name
, evt
->key
->down
);
204 case KEY_VALUE_KIND_QCODE
:
205 name
= QKeyCode_lookup
[evt
->key
->key
->qcode
];
206 trace_input_event_key_qcode(idx
, name
, evt
->key
->down
);
208 case KEY_VALUE_KIND_MAX
:
213 case INPUT_EVENT_KIND_BTN
:
214 name
= InputButton_lookup
[evt
->btn
->button
];
215 trace_input_event_btn(idx
, name
, evt
->btn
->down
);
217 case INPUT_EVENT_KIND_REL
:
218 name
= InputAxis_lookup
[evt
->rel
->axis
];
219 trace_input_event_rel(idx
, name
, evt
->rel
->value
);
221 case INPUT_EVENT_KIND_ABS
:
222 name
= InputAxis_lookup
[evt
->abs
->axis
];
223 trace_input_event_abs(idx
, name
, evt
->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(QemuConsole
*src
, InputEvent
*evt
)
300 QemuInputHandlerState
*s
;
302 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
306 qemu_input_event_trace(src
, evt
);
309 if (graphic_rotate
&& (evt
->kind
== INPUT_EVENT_KIND_ABS
)) {
310 qemu_input_transform_abs_rotate(evt
);
314 s
= qemu_input_find_handler(1 << evt
->kind
, src
);
318 s
->handler
->event(s
->dev
, src
, evt
);
322 void qemu_input_event_sync(void)
324 QemuInputHandlerState
*s
;
326 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED
)) {
330 trace_input_event_sync();
332 QTAILQ_FOREACH(s
, &handlers
, node
) {
336 if (s
->handler
->sync
) {
337 s
->handler
->sync(s
->dev
);
343 InputEvent
*qemu_input_event_new_key(KeyValue
*key
, bool down
)
345 InputEvent
*evt
= g_new0(InputEvent
, 1);
346 evt
->key
= g_new0(InputKeyEvent
, 1);
347 evt
->kind
= INPUT_EVENT_KIND_KEY
;
349 evt
->key
->down
= down
;
353 void qemu_input_event_send_key(QemuConsole
*src
, KeyValue
*key
, bool down
)
356 evt
= qemu_input_event_new_key(key
, down
);
357 if (QTAILQ_EMPTY(&kbd_queue
)) {
358 qemu_input_event_send(src
, evt
);
359 qemu_input_event_sync();
360 qapi_free_InputEvent(evt
);
362 qemu_input_queue_event(&kbd_queue
, src
, evt
);
363 qemu_input_queue_sync(&kbd_queue
);
367 void qemu_input_event_send_key_number(QemuConsole
*src
, int num
, bool down
)
369 KeyValue
*key
= g_new0(KeyValue
, 1);
370 key
->kind
= KEY_VALUE_KIND_NUMBER
;
372 qemu_input_event_send_key(src
, key
, down
);
375 void qemu_input_event_send_key_qcode(QemuConsole
*src
, QKeyCode q
, bool down
)
377 KeyValue
*key
= g_new0(KeyValue
, 1);
378 key
->kind
= KEY_VALUE_KIND_QCODE
;
380 qemu_input_event_send_key(src
, key
, down
);
383 void qemu_input_event_send_key_delay(uint32_t delay_ms
)
386 kbd_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
, qemu_input_queue_process
,
389 qemu_input_queue_delay(&kbd_queue
, kbd_timer
,
390 delay_ms
? delay_ms
: kbd_default_delay_ms
);
393 InputEvent
*qemu_input_event_new_btn(InputButton btn
, bool down
)
395 InputEvent
*evt
= g_new0(InputEvent
, 1);
396 evt
->btn
= g_new0(InputBtnEvent
, 1);
397 evt
->kind
= INPUT_EVENT_KIND_BTN
;
398 evt
->btn
->button
= btn
;
399 evt
->btn
->down
= down
;
403 void qemu_input_queue_btn(QemuConsole
*src
, InputButton btn
, bool down
)
406 evt
= qemu_input_event_new_btn(btn
, down
);
407 qemu_input_event_send(src
, evt
);
408 qapi_free_InputEvent(evt
);
411 void qemu_input_update_buttons(QemuConsole
*src
, uint32_t *button_map
,
412 uint32_t button_old
, uint32_t button_new
)
417 for (btn
= 0; btn
< INPUT_BUTTON_MAX
; btn
++) {
418 mask
= button_map
[btn
];
419 if ((button_old
& mask
) == (button_new
& mask
)) {
422 qemu_input_queue_btn(src
, btn
, button_new
& mask
);
426 bool qemu_input_is_absolute(void)
428 QemuInputHandlerState
*s
;
430 s
= qemu_input_find_handler(INPUT_EVENT_MASK_REL
| INPUT_EVENT_MASK_ABS
,
432 return (s
!= NULL
) && (s
->handler
->mask
& INPUT_EVENT_MASK_ABS
);
435 int qemu_input_scale_axis(int value
, int size_in
, int size_out
)
440 return (int64_t)value
* (size_out
- 1) / (size_in
- 1);
443 InputEvent
*qemu_input_event_new_move(InputEventKind kind
,
444 InputAxis axis
, int value
)
446 InputEvent
*evt
= g_new0(InputEvent
, 1);
447 InputMoveEvent
*move
= g_new0(InputMoveEvent
, 1);
456 void qemu_input_queue_rel(QemuConsole
*src
, InputAxis axis
, int value
)
459 evt
= qemu_input_event_new_move(INPUT_EVENT_KIND_REL
, axis
, value
);
460 qemu_input_event_send(src
, evt
);
461 qapi_free_InputEvent(evt
);
464 void qemu_input_queue_abs(QemuConsole
*src
, InputAxis axis
, int value
, int size
)
467 int scaled
= qemu_input_scale_axis(value
, size
, INPUT_EVENT_ABS_SIZE
);
468 evt
= qemu_input_event_new_move(INPUT_EVENT_KIND_ABS
, axis
, scaled
);
469 qemu_input_event_send(src
, evt
);
470 qapi_free_InputEvent(evt
);
473 void qemu_input_check_mode_change(void)
475 static int current_is_absolute
;
478 is_absolute
= qemu_input_is_absolute();
480 if (is_absolute
!= current_is_absolute
) {
481 trace_input_mouse_mode(is_absolute
);
482 notifier_list_notify(&mouse_mode_notifiers
, NULL
);
485 current_is_absolute
= is_absolute
;
488 void qemu_add_mouse_mode_change_notifier(Notifier
*notify
)
490 notifier_list_add(&mouse_mode_notifiers
, notify
);
493 void qemu_remove_mouse_mode_change_notifier(Notifier
*notify
)
495 notifier_remove(notify
);
498 MouseInfoList
*qmp_query_mice(Error
**errp
)
500 MouseInfoList
*mice_list
= NULL
;
502 QemuInputHandlerState
*s
;
505 QTAILQ_FOREACH(s
, &handlers
, node
) {
506 if (!(s
->handler
->mask
&
507 (INPUT_EVENT_MASK_REL
| INPUT_EVENT_MASK_ABS
))) {
511 info
= g_new0(MouseInfoList
, 1);
512 info
->value
= g_new0(MouseInfo
, 1);
513 info
->value
->index
= s
->id
;
514 info
->value
->name
= g_strdup(s
->handler
->name
);
515 info
->value
->absolute
= s
->handler
->mask
& INPUT_EVENT_MASK_ABS
;
516 info
->value
->current
= current
;
519 info
->next
= mice_list
;
526 void do_mouse_set(Monitor
*mon
, const QDict
*qdict
)
528 QemuInputHandlerState
*s
;
529 int index
= qdict_get_int(qdict
, "index");
532 QTAILQ_FOREACH(s
, &handlers
, node
) {
533 if (s
->id
!= index
) {
536 if (!(s
->handler
->mask
& (INPUT_EVENT_MASK_REL
|
537 INPUT_EVENT_MASK_ABS
))) {
538 error_report("Input device '%s' is not a mouse", s
->handler
->name
);
542 qemu_input_handler_activate(s
);
547 error_report("Mouse at index '%d' not found", index
);
550 qemu_input_check_mode_change();