console: add & use qemu_console_lookup_by_device_name
[qemu.git] / ui / input.c
blobfce99bab403ab2023756d2ee63b03cc7e52b7cdc
1 #include "qemu/osdep.h"
2 #include "hw/qdev.h"
3 #include "sysemu/sysemu.h"
4 #include "qapi-types.h"
5 #include "qemu/error-report.h"
6 #include "qmp-commands.h"
7 #include "trace.h"
8 #include "ui/input.h"
9 #include "ui/console.h"
10 #include "sysemu/replay.h"
12 struct QemuInputHandlerState {
13 DeviceState *dev;
14 QemuInputHandler *handler;
15 int id;
16 int events;
17 QemuConsole *con;
18 QTAILQ_ENTRY(QemuInputHandlerState) node;
21 typedef struct QemuInputEventQueue QemuInputEventQueue;
22 struct QemuInputEventQueue {
23 enum {
24 QEMU_INPUT_QUEUE_DELAY = 1,
25 QEMU_INPUT_QUEUE_EVENT,
26 QEMU_INPUT_QUEUE_SYNC,
27 } type;
28 QEMUTimer *timer;
29 uint32_t delay_ms;
30 QemuConsole *src;
31 InputEvent *evt;
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);
49 static int id = 1;
51 s->dev = dev;
52 s->handler = handler;
53 s->id = id++;
54 QTAILQ_INSERT_TAIL(&handlers, s, node);
56 qemu_input_check_mode_change();
57 return s;
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);
77 g_free(s);
78 qemu_input_check_mode_change();
81 void qemu_input_handler_bind(QemuInputHandlerState *s,
82 const char *device_id, int head,
83 Error **errp)
85 QemuConsole *con;
86 Error *err = NULL;
88 con = qemu_console_lookup_by_device_name(device_id, head, &err);
89 if (err) {
90 error_propagate(errp, err);
91 return;
94 s->con = con;
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) {
104 continue;
106 if (mask & s->handler->mask) {
107 return s;
111 QTAILQ_FOREACH(s, &handlers, node) {
112 if (s->con != NULL) {
113 continue;
115 if (mask & s->handler->mask) {
116 return s;
119 return NULL;
122 void qmp_x_input_send_event(bool has_console, int64_t console,
123 InputEventList *events, Error **errp)
125 InputEventList *e;
126 QemuConsole *con;
128 con = NULL;
129 if (has_console) {
130 con = qemu_console_lookup_by_index(console);
131 if (!con) {
132 error_setg(errp, "console %" PRId64 " not found", console);
133 return;
137 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
138 error_setg(errp, "VM not running");
139 return;
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 "
147 "event type %s",
148 InputEventKind_lookup[event->type]);
149 return;
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) {
165 case 90:
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;
172 break;
173 case 180:
174 evt->u.abs->value = INPUT_EVENT_ABS_SIZE - 1 - evt->u.abs->value;
175 break;
176 case 270:
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;
183 break;
187 static void qemu_input_event_trace(QemuConsole *src, InputEvent *evt)
189 const char *name;
190 int qcode, idx = -1;
192 if (src) {
193 idx = qemu_console_get_index(src);
195 switch (evt->type) {
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);
203 break;
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);
207 break;
208 case KEY_VALUE_KIND__MAX:
209 /* keep gcc happy */
210 break;
212 break;
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);
216 break;
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);
220 break;
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);
224 break;
225 case INPUT_EVENT_KIND__MAX:
226 /* keep gcc happy */
227 break;
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);
240 g_free(item);
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)
247 + item->delay_ms);
248 return;
249 case QEMU_INPUT_QUEUE_EVENT:
250 qemu_input_event_send(item->src, item->evt);
251 qapi_free_InputEvent(item->evt);
252 break;
253 case QEMU_INPUT_QUEUE_SYNC:
254 qemu_input_event_sync();
255 break;
257 QTAILQ_REMOVE(queue, item, node);
258 g_free(item);
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;
270 item->timer = timer;
271 QTAILQ_INSERT_TAIL(queue, item, node);
273 if (start_timer) {
274 timer_mod(item->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL)
275 + item->delay_ms);
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;
285 item->src = src;
286 item->evt = evt;
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);
304 /* pre processing */
305 if (graphic_rotate && (evt->type == INPUT_EVENT_KIND_ABS)) {
306 qemu_input_transform_abs_rotate(evt);
309 /* send event */
310 s = qemu_input_find_handler(1 << evt->type, src);
311 if (!s) {
312 return;
314 s->handler->event(s->dev, src, evt);
315 s->events++;
318 void qemu_input_event_send(QemuConsole *src, InputEvent *evt)
320 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
321 return;
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) {
334 if (!s->events) {
335 continue;
337 if (s->handler->sync) {
338 s->handler->sync(s->dev);
340 s->events = 0;
344 void qemu_input_event_sync(void)
346 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
347 return;
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;
360 return evt;
363 void qemu_input_event_send_key(QemuConsole *src, KeyValue *key, bool down)
365 InputEvent *evt;
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);
371 } else {
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;
381 key->u.number = num;
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;
389 key->u.qcode = q;
390 qemu_input_event_send_key(src, key, down);
393 void qemu_input_event_send_key_delay(uint32_t delay_ms)
395 if (!kbd_timer) {
396 kbd_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, qemu_input_queue_process,
397 &kbd_queue);
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;
410 return evt;
413 void qemu_input_queue_btn(QemuConsole *src, InputButton btn, bool down)
415 InputEvent *evt;
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)
424 InputButton btn;
425 uint32_t mask;
427 for (btn = 0; btn < INPUT_BUTTON__MAX; btn++) {
428 mask = button_map[btn];
429 if ((button_old & mask) == (button_new & mask)) {
430 continue;
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,
441 NULL);
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)
447 if (size_in < 2) {
448 return size_out / 2;
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);
459 evt->type = kind;
460 evt->u.data = move;
461 move->axis = axis;
462 move->value = value;
463 return evt;
466 void qemu_input_queue_rel(QemuConsole *src, InputAxis axis, int value)
468 InputEvent *evt;
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)
476 InputEvent *evt;
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;
486 int 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;
511 MouseInfoList *info;
512 QemuInputHandlerState *s;
513 bool current = true;
515 QTAILQ_FOREACH(s, &handlers, node) {
516 if (!(s->handler->mask &
517 (INPUT_EVENT_MASK_REL | INPUT_EVENT_MASK_ABS))) {
518 continue;
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;
528 current = false;
529 info->next = mice_list;
530 mice_list = info;
533 return mice_list;
536 void hmp_mouse_set(Monitor *mon, const QDict *qdict)
538 QemuInputHandlerState *s;
539 int index = qdict_get_int(qdict, "index");
540 int found = 0;
542 QTAILQ_FOREACH(s, &handlers, node) {
543 if (s->id != index) {
544 continue;
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);
549 return;
551 found = 1;
552 qemu_input_handler_activate(s);
553 break;
556 if (!found) {
557 error_report("Mouse at index '%d' not found", index);
560 qemu_input_check_mode_change();