qapi: Add missing JSON files in build dependencies
[qemu/armbru.git] / ui / input.c
blobbdcb974a89a0e30636dedf5f85e18e9e1e2c5004
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 DeviceState *dev;
86 QemuConsole *con;
88 dev = qdev_find_recursive(sysbus_get_default(), device_id);
89 if (dev == NULL) {
90 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
91 "Device '%s' not found", device_id);
92 return;
95 con = qemu_console_lookup_by_device(dev, head);
96 if (con == NULL) {
97 error_setg(errp, "Device %s is not bound to a QemuConsole", device_id);
98 return;
101 s->con = con;
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) {
111 continue;
113 if (mask & s->handler->mask) {
114 return s;
118 QTAILQ_FOREACH(s, &handlers, node) {
119 if (s->con != NULL) {
120 continue;
122 if (mask & s->handler->mask) {
123 return s;
126 return NULL;
129 void qmp_x_input_send_event(bool has_console, int64_t console,
130 InputEventList *events, Error **errp)
132 InputEventList *e;
133 QemuConsole *con;
135 con = NULL;
136 if (has_console) {
137 con = qemu_console_lookup_by_index(console);
138 if (!con) {
139 error_setg(errp, "console %" PRId64 " not found", console);
140 return;
144 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
145 error_setg(errp, "VM not running");
146 return;
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 "
154 "event type %s",
155 InputEventKind_lookup[event->type]);
156 return;
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) {
172 case 90:
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;
179 break;
180 case 180:
181 evt->u.abs->value = INPUT_EVENT_ABS_SIZE - 1 - evt->u.abs->value;
182 break;
183 case 270:
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;
190 break;
194 static void qemu_input_event_trace(QemuConsole *src, InputEvent *evt)
196 const char *name;
197 int qcode, idx = -1;
199 if (src) {
200 idx = qemu_console_get_index(src);
202 switch (evt->type) {
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);
210 break;
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);
214 break;
215 case KEY_VALUE_KIND__MAX:
216 /* keep gcc happy */
217 break;
219 break;
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);
223 break;
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);
227 break;
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);
231 break;
232 case INPUT_EVENT_KIND__MAX:
233 /* keep gcc happy */
234 break;
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);
247 g_free(item);
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)
254 + item->delay_ms);
255 return;
256 case QEMU_INPUT_QUEUE_EVENT:
257 qemu_input_event_send(item->src, item->evt);
258 qapi_free_InputEvent(item->evt);
259 break;
260 case QEMU_INPUT_QUEUE_SYNC:
261 qemu_input_event_sync();
262 break;
264 QTAILQ_REMOVE(queue, item, node);
265 g_free(item);
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;
277 item->timer = timer;
278 QTAILQ_INSERT_TAIL(queue, item, node);
280 if (start_timer) {
281 timer_mod(item->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL)
282 + item->delay_ms);
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;
292 item->src = src;
293 item->evt = evt;
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);
311 /* pre processing */
312 if (graphic_rotate && (evt->type == INPUT_EVENT_KIND_ABS)) {
313 qemu_input_transform_abs_rotate(evt);
316 /* send event */
317 s = qemu_input_find_handler(1 << evt->type, src);
318 if (!s) {
319 return;
321 s->handler->event(s->dev, src, evt);
322 s->events++;
325 void qemu_input_event_send(QemuConsole *src, InputEvent *evt)
327 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
328 return;
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) {
341 if (!s->events) {
342 continue;
344 if (s->handler->sync) {
345 s->handler->sync(s->dev);
347 s->events = 0;
351 void qemu_input_event_sync(void)
353 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
354 return;
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;
367 return evt;
370 void qemu_input_event_send_key(QemuConsole *src, KeyValue *key, bool down)
372 InputEvent *evt;
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);
378 } else {
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;
388 key->u.number = num;
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;
396 key->u.qcode = q;
397 qemu_input_event_send_key(src, key, down);
400 void qemu_input_event_send_key_delay(uint32_t delay_ms)
402 if (!kbd_timer) {
403 kbd_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, qemu_input_queue_process,
404 &kbd_queue);
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;
417 return evt;
420 void qemu_input_queue_btn(QemuConsole *src, InputButton btn, bool down)
422 InputEvent *evt;
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)
431 InputButton btn;
432 uint32_t mask;
434 for (btn = 0; btn < INPUT_BUTTON__MAX; btn++) {
435 mask = button_map[btn];
436 if ((button_old & mask) == (button_new & mask)) {
437 continue;
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,
448 NULL);
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)
454 if (size_in < 2) {
455 return size_out / 2;
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);
466 evt->type = kind;
467 evt->u.data = move;
468 move->axis = axis;
469 move->value = value;
470 return evt;
473 void qemu_input_queue_rel(QemuConsole *src, InputAxis axis, int value)
475 InputEvent *evt;
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)
483 InputEvent *evt;
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;
493 int 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;
518 MouseInfoList *info;
519 QemuInputHandlerState *s;
520 bool current = true;
522 QTAILQ_FOREACH(s, &handlers, node) {
523 if (!(s->handler->mask &
524 (INPUT_EVENT_MASK_REL | INPUT_EVENT_MASK_ABS))) {
525 continue;
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;
535 current = false;
536 info->next = mice_list;
537 mice_list = info;
540 return mice_list;
543 void hmp_mouse_set(Monitor *mon, const QDict *qdict)
545 QemuInputHandlerState *s;
546 int index = qdict_get_int(qdict, "index");
547 int found = 0;
549 QTAILQ_FOREACH(s, &handlers, node) {
550 if (s->id != index) {
551 continue;
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);
556 return;
558 found = 1;
559 qemu_input_handler_activate(s);
560 break;
563 if (!found) {
564 error_report("Mouse at index '%d' not found", index);
567 qemu_input_check_mode_change();