qobject: Use simpler QDict/QList scalar insertion macros
[qemu/ar7.git] / ui / input.c
blob830f912f99f33c63ca9cb7cff68b16cae3623e17
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;
44 static uint32_t queue_count;
45 static uint32_t queue_limit = 1024;
47 QemuInputHandlerState *qemu_input_handler_register(DeviceState *dev,
48 QemuInputHandler *handler)
50 QemuInputHandlerState *s = g_new0(QemuInputHandlerState, 1);
51 static int id = 1;
53 s->dev = dev;
54 s->handler = handler;
55 s->id = id++;
56 QTAILQ_INSERT_TAIL(&handlers, s, node);
58 qemu_input_check_mode_change();
59 return s;
62 void qemu_input_handler_activate(QemuInputHandlerState *s)
64 QTAILQ_REMOVE(&handlers, s, node);
65 QTAILQ_INSERT_HEAD(&handlers, s, node);
66 qemu_input_check_mode_change();
69 void qemu_input_handler_deactivate(QemuInputHandlerState *s)
71 QTAILQ_REMOVE(&handlers, s, node);
72 QTAILQ_INSERT_TAIL(&handlers, s, node);
73 qemu_input_check_mode_change();
76 void qemu_input_handler_unregister(QemuInputHandlerState *s)
78 QTAILQ_REMOVE(&handlers, s, node);
79 g_free(s);
80 qemu_input_check_mode_change();
83 void qemu_input_handler_bind(QemuInputHandlerState *s,
84 const char *device_id, int head,
85 Error **errp)
87 QemuConsole *con;
88 Error *err = NULL;
90 con = qemu_console_lookup_by_device_name(device_id, head, &err);
91 if (err) {
92 error_propagate(errp, err);
93 return;
96 s->con = con;
99 static QemuInputHandlerState*
100 qemu_input_find_handler(uint32_t mask, QemuConsole *con)
102 QemuInputHandlerState *s;
104 QTAILQ_FOREACH(s, &handlers, node) {
105 if (s->con == NULL || s->con != con) {
106 continue;
108 if (mask & s->handler->mask) {
109 return s;
113 QTAILQ_FOREACH(s, &handlers, node) {
114 if (s->con != NULL) {
115 continue;
117 if (mask & s->handler->mask) {
118 return s;
121 return NULL;
124 void qmp_input_send_event(bool has_device, const char *device,
125 bool has_head, int64_t head,
126 InputEventList *events, Error **errp)
128 InputEventList *e;
129 QemuConsole *con;
130 Error *err = NULL;
132 con = NULL;
133 if (has_device) {
134 if (!has_head) {
135 head = 0;
137 con = qemu_console_lookup_by_device_name(device, head, &err);
138 if (err) {
139 error_propagate(errp, err);
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 InputMoveEvent *move = evt->u.abs.data;
172 switch (graphic_rotate) {
173 case 90:
174 if (move->axis == INPUT_AXIS_X) {
175 move->axis = INPUT_AXIS_Y;
176 } else if (move->axis == INPUT_AXIS_Y) {
177 move->axis = INPUT_AXIS_X;
178 move->value = INPUT_EVENT_ABS_SIZE - 1 - move->value;
180 break;
181 case 180:
182 move->value = INPUT_EVENT_ABS_SIZE - 1 - move->value;
183 break;
184 case 270:
185 if (move->axis == INPUT_AXIS_X) {
186 move->axis = INPUT_AXIS_Y;
187 move->value = INPUT_EVENT_ABS_SIZE - 1 - move->value;
188 } else if (move->axis == INPUT_AXIS_Y) {
189 move->axis = INPUT_AXIS_X;
191 break;
195 static void qemu_input_event_trace(QemuConsole *src, InputEvent *evt)
197 const char *name;
198 int qcode, idx = -1;
199 InputKeyEvent *key;
200 InputBtnEvent *btn;
201 InputMoveEvent *move;
203 if (src) {
204 idx = qemu_console_get_index(src);
206 switch (evt->type) {
207 case INPUT_EVENT_KIND_KEY:
208 key = evt->u.key.data;
209 switch (key->key->type) {
210 case KEY_VALUE_KIND_NUMBER:
211 qcode = qemu_input_key_number_to_qcode(key->key->u.number.data);
212 name = QKeyCode_lookup[qcode];
213 trace_input_event_key_number(idx, key->key->u.number.data,
214 name, key->down);
215 break;
216 case KEY_VALUE_KIND_QCODE:
217 name = QKeyCode_lookup[key->key->u.qcode.data];
218 trace_input_event_key_qcode(idx, name, key->down);
219 break;
220 case KEY_VALUE_KIND__MAX:
221 /* keep gcc happy */
222 break;
224 break;
225 case INPUT_EVENT_KIND_BTN:
226 btn = evt->u.btn.data;
227 name = InputButton_lookup[btn->button];
228 trace_input_event_btn(idx, name, btn->down);
229 break;
230 case INPUT_EVENT_KIND_REL:
231 move = evt->u.rel.data;
232 name = InputAxis_lookup[move->axis];
233 trace_input_event_rel(idx, name, move->value);
234 break;
235 case INPUT_EVENT_KIND_ABS:
236 move = evt->u.abs.data;
237 name = InputAxis_lookup[move->axis];
238 trace_input_event_abs(idx, name, move->value);
239 break;
240 case INPUT_EVENT_KIND__MAX:
241 /* keep gcc happy */
242 break;
246 static void qemu_input_queue_process(void *opaque)
248 struct QemuInputEventQueueHead *queue = opaque;
249 QemuInputEventQueue *item;
251 g_assert(!QTAILQ_EMPTY(queue));
252 item = QTAILQ_FIRST(queue);
253 g_assert(item->type == QEMU_INPUT_QUEUE_DELAY);
254 QTAILQ_REMOVE(queue, item, node);
255 g_free(item);
257 while (!QTAILQ_EMPTY(queue)) {
258 item = QTAILQ_FIRST(queue);
259 switch (item->type) {
260 case QEMU_INPUT_QUEUE_DELAY:
261 timer_mod(item->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL)
262 + item->delay_ms);
263 return;
264 case QEMU_INPUT_QUEUE_EVENT:
265 qemu_input_event_send(item->src, item->evt);
266 qapi_free_InputEvent(item->evt);
267 break;
268 case QEMU_INPUT_QUEUE_SYNC:
269 qemu_input_event_sync();
270 break;
272 QTAILQ_REMOVE(queue, item, node);
273 queue_count--;
274 g_free(item);
278 static void qemu_input_queue_delay(struct QemuInputEventQueueHead *queue,
279 QEMUTimer *timer, uint32_t delay_ms)
281 QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1);
282 bool start_timer = QTAILQ_EMPTY(queue);
284 item->type = QEMU_INPUT_QUEUE_DELAY;
285 item->delay_ms = delay_ms;
286 item->timer = timer;
287 QTAILQ_INSERT_TAIL(queue, item, node);
288 queue_count++;
290 if (start_timer) {
291 timer_mod(item->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL)
292 + item->delay_ms);
296 static void qemu_input_queue_event(struct QemuInputEventQueueHead *queue,
297 QemuConsole *src, InputEvent *evt)
299 QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1);
301 item->type = QEMU_INPUT_QUEUE_EVENT;
302 item->src = src;
303 item->evt = evt;
304 QTAILQ_INSERT_TAIL(queue, item, node);
305 queue_count++;
308 static void qemu_input_queue_sync(struct QemuInputEventQueueHead *queue)
310 QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1);
312 item->type = QEMU_INPUT_QUEUE_SYNC;
313 QTAILQ_INSERT_TAIL(queue, item, node);
314 queue_count++;
317 void qemu_input_event_send_impl(QemuConsole *src, InputEvent *evt)
319 QemuInputHandlerState *s;
321 qemu_input_event_trace(src, evt);
323 /* pre processing */
324 if (graphic_rotate && (evt->type == INPUT_EVENT_KIND_ABS)) {
325 qemu_input_transform_abs_rotate(evt);
328 /* send event */
329 s = qemu_input_find_handler(1 << evt->type, src);
330 if (!s) {
331 return;
333 s->handler->event(s->dev, src, evt);
334 s->events++;
337 void qemu_input_event_send(QemuConsole *src, InputEvent *evt)
339 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
340 return;
343 replay_input_event(src, evt);
346 void qemu_input_event_sync_impl(void)
348 QemuInputHandlerState *s;
350 trace_input_event_sync();
352 QTAILQ_FOREACH(s, &handlers, node) {
353 if (!s->events) {
354 continue;
356 if (s->handler->sync) {
357 s->handler->sync(s->dev);
359 s->events = 0;
363 void qemu_input_event_sync(void)
365 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
366 return;
369 replay_input_sync_event();
372 InputEvent *qemu_input_event_new_key(KeyValue *key, bool down)
374 InputEvent *evt = g_new0(InputEvent, 1);
375 evt->u.key.data = g_new0(InputKeyEvent, 1);
376 evt->type = INPUT_EVENT_KIND_KEY;
377 evt->u.key.data->key = key;
378 evt->u.key.data->down = down;
379 return evt;
382 void qemu_input_event_send_key(QemuConsole *src, KeyValue *key, bool down)
384 InputEvent *evt;
385 evt = qemu_input_event_new_key(key, down);
386 if (QTAILQ_EMPTY(&kbd_queue)) {
387 qemu_input_event_send(src, evt);
388 qemu_input_event_sync();
389 qapi_free_InputEvent(evt);
390 } else if (queue_count < queue_limit) {
391 qemu_input_queue_event(&kbd_queue, src, evt);
392 qemu_input_queue_sync(&kbd_queue);
396 void qemu_input_event_send_key_number(QemuConsole *src, int num, bool down)
398 KeyValue *key = g_new0(KeyValue, 1);
399 key->type = KEY_VALUE_KIND_NUMBER;
400 key->u.number.data = num;
401 qemu_input_event_send_key(src, key, down);
404 void qemu_input_event_send_key_qcode(QemuConsole *src, QKeyCode q, bool down)
406 KeyValue *key = g_new0(KeyValue, 1);
407 key->type = KEY_VALUE_KIND_QCODE;
408 key->u.qcode.data = q;
409 qemu_input_event_send_key(src, key, down);
412 void qemu_input_event_send_key_delay(uint32_t delay_ms)
414 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
415 return;
418 if (!kbd_timer) {
419 kbd_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, qemu_input_queue_process,
420 &kbd_queue);
422 if (queue_count < queue_limit) {
423 qemu_input_queue_delay(&kbd_queue, kbd_timer,
424 delay_ms ? delay_ms : kbd_default_delay_ms);
428 InputEvent *qemu_input_event_new_btn(InputButton btn, bool down)
430 InputEvent *evt = g_new0(InputEvent, 1);
431 evt->u.btn.data = g_new0(InputBtnEvent, 1);
432 evt->type = INPUT_EVENT_KIND_BTN;
433 evt->u.btn.data->button = btn;
434 evt->u.btn.data->down = down;
435 return evt;
438 void qemu_input_queue_btn(QemuConsole *src, InputButton btn, bool down)
440 InputEvent *evt;
441 evt = qemu_input_event_new_btn(btn, down);
442 qemu_input_event_send(src, evt);
443 qapi_free_InputEvent(evt);
446 void qemu_input_update_buttons(QemuConsole *src, uint32_t *button_map,
447 uint32_t button_old, uint32_t button_new)
449 InputButton btn;
450 uint32_t mask;
452 for (btn = 0; btn < INPUT_BUTTON__MAX; btn++) {
453 mask = button_map[btn];
454 if ((button_old & mask) == (button_new & mask)) {
455 continue;
457 qemu_input_queue_btn(src, btn, button_new & mask);
461 bool qemu_input_is_absolute(void)
463 QemuInputHandlerState *s;
465 s = qemu_input_find_handler(INPUT_EVENT_MASK_REL | INPUT_EVENT_MASK_ABS,
466 NULL);
467 return (s != NULL) && (s->handler->mask & INPUT_EVENT_MASK_ABS);
470 int qemu_input_scale_axis(int value, int size_in, int size_out)
472 if (size_in < 2) {
473 return size_out / 2;
475 return (int64_t)value * (size_out - 1) / (size_in - 1);
478 InputEvent *qemu_input_event_new_move(InputEventKind kind,
479 InputAxis axis, int value)
481 InputEvent *evt = g_new0(InputEvent, 1);
482 InputMoveEvent *move = g_new0(InputMoveEvent, 1);
484 evt->type = kind;
485 evt->u.rel.data = move; /* evt->u.rel is the same as evt->u.abs */
486 move->axis = axis;
487 move->value = value;
488 return evt;
491 void qemu_input_queue_rel(QemuConsole *src, InputAxis axis, int value)
493 InputEvent *evt;
494 evt = qemu_input_event_new_move(INPUT_EVENT_KIND_REL, axis, value);
495 qemu_input_event_send(src, evt);
496 qapi_free_InputEvent(evt);
499 void qemu_input_queue_abs(QemuConsole *src, InputAxis axis, int value, int size)
501 InputEvent *evt;
502 int scaled = qemu_input_scale_axis(value, size, INPUT_EVENT_ABS_SIZE);
503 evt = qemu_input_event_new_move(INPUT_EVENT_KIND_ABS, axis, scaled);
504 qemu_input_event_send(src, evt);
505 qapi_free_InputEvent(evt);
508 void qemu_input_check_mode_change(void)
510 static int current_is_absolute;
511 int is_absolute;
513 is_absolute = qemu_input_is_absolute();
515 if (is_absolute != current_is_absolute) {
516 trace_input_mouse_mode(is_absolute);
517 notifier_list_notify(&mouse_mode_notifiers, NULL);
520 current_is_absolute = is_absolute;
523 void qemu_add_mouse_mode_change_notifier(Notifier *notify)
525 notifier_list_add(&mouse_mode_notifiers, notify);
528 void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
530 notifier_remove(notify);
533 MouseInfoList *qmp_query_mice(Error **errp)
535 MouseInfoList *mice_list = NULL;
536 MouseInfoList *info;
537 QemuInputHandlerState *s;
538 bool current = true;
540 QTAILQ_FOREACH(s, &handlers, node) {
541 if (!(s->handler->mask &
542 (INPUT_EVENT_MASK_REL | INPUT_EVENT_MASK_ABS))) {
543 continue;
546 info = g_new0(MouseInfoList, 1);
547 info->value = g_new0(MouseInfo, 1);
548 info->value->index = s->id;
549 info->value->name = g_strdup(s->handler->name);
550 info->value->absolute = s->handler->mask & INPUT_EVENT_MASK_ABS;
551 info->value->current = current;
553 current = false;
554 info->next = mice_list;
555 mice_list = info;
558 return mice_list;
561 void hmp_mouse_set(Monitor *mon, const QDict *qdict)
563 QemuInputHandlerState *s;
564 int index = qdict_get_int(qdict, "index");
565 int found = 0;
567 QTAILQ_FOREACH(s, &handlers, node) {
568 if (s->id != index) {
569 continue;
571 if (!(s->handler->mask & (INPUT_EVENT_MASK_REL |
572 INPUT_EVENT_MASK_ABS))) {
573 error_report("Input device '%s' is not a mouse", s->handler->name);
574 return;
576 found = 1;
577 qemu_input_handler_activate(s);
578 break;
581 if (!found) {
582 error_report("Mouse at index '%d' not found", index);
585 qemu_input_check_mode_change();