docker: use HTTPS git URL for virglrenderer
[qemu/ar7.git] / ui / input.c
blob7c9a4109c474f89d4cbd392fecebd894ccd57f12
1 #include "qemu/osdep.h"
2 #include "sysemu/sysemu.h"
3 #include "qapi/error.h"
4 #include "qapi/qapi-commands-ui.h"
5 #include "qapi/qmp/qdict.h"
6 #include "qemu/error-report.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_str(event->type));
156 return;
160 for (e = events; e != NULL; e = e->next) {
161 InputEvent *evt = e->value;
163 if (evt->type == INPUT_EVENT_KIND_KEY &&
164 evt->u.key.data->key->type == KEY_VALUE_KIND_NUMBER) {
165 KeyValue *key = evt->u.key.data->key;
166 QKeyCode code = qemu_input_key_number_to_qcode(key->u.number.data);
167 qemu_input_event_send_key_qcode(con, code, evt->u.key.data->down);
168 } else {
169 qemu_input_event_send(con, evt);
173 qemu_input_event_sync();
176 static int qemu_input_transform_invert_abs_value(int value)
178 return (int64_t)INPUT_EVENT_ABS_MAX - value + INPUT_EVENT_ABS_MIN;
181 static void qemu_input_transform_abs_rotate(InputEvent *evt)
183 InputMoveEvent *move = evt->u.abs.data;
184 switch (graphic_rotate) {
185 case 90:
186 if (move->axis == INPUT_AXIS_X) {
187 move->axis = INPUT_AXIS_Y;
188 } else if (move->axis == INPUT_AXIS_Y) {
189 move->axis = INPUT_AXIS_X;
190 move->value = qemu_input_transform_invert_abs_value(move->value);
192 break;
193 case 180:
194 move->value = qemu_input_transform_invert_abs_value(move->value);
195 break;
196 case 270:
197 if (move->axis == INPUT_AXIS_X) {
198 move->axis = INPUT_AXIS_Y;
199 move->value = qemu_input_transform_invert_abs_value(move->value);
200 } else if (move->axis == INPUT_AXIS_Y) {
201 move->axis = INPUT_AXIS_X;
203 break;
207 static void qemu_input_event_trace(QemuConsole *src, InputEvent *evt)
209 const char *name;
210 int qcode, idx = -1;
211 InputKeyEvent *key;
212 InputBtnEvent *btn;
213 InputMoveEvent *move;
215 if (src) {
216 idx = qemu_console_get_index(src);
218 switch (evt->type) {
219 case INPUT_EVENT_KIND_KEY:
220 key = evt->u.key.data;
221 switch (key->key->type) {
222 case KEY_VALUE_KIND_NUMBER:
223 qcode = qemu_input_key_number_to_qcode(key->key->u.number.data);
224 name = QKeyCode_str(qcode);
225 trace_input_event_key_number(idx, key->key->u.number.data,
226 name, key->down);
227 break;
228 case KEY_VALUE_KIND_QCODE:
229 name = QKeyCode_str(key->key->u.qcode.data);
230 trace_input_event_key_qcode(idx, name, key->down);
231 break;
232 case KEY_VALUE_KIND__MAX:
233 /* keep gcc happy */
234 break;
236 break;
237 case INPUT_EVENT_KIND_BTN:
238 btn = evt->u.btn.data;
239 name = InputButton_str(btn->button);
240 trace_input_event_btn(idx, name, btn->down);
241 break;
242 case INPUT_EVENT_KIND_REL:
243 move = evt->u.rel.data;
244 name = InputAxis_str(move->axis);
245 trace_input_event_rel(idx, name, move->value);
246 break;
247 case INPUT_EVENT_KIND_ABS:
248 move = evt->u.abs.data;
249 name = InputAxis_str(move->axis);
250 trace_input_event_abs(idx, name, move->value);
251 break;
252 case INPUT_EVENT_KIND__MAX:
253 /* keep gcc happy */
254 break;
258 static void qemu_input_queue_process(void *opaque)
260 struct QemuInputEventQueueHead *queue = opaque;
261 QemuInputEventQueue *item;
263 g_assert(!QTAILQ_EMPTY(queue));
264 item = QTAILQ_FIRST(queue);
265 g_assert(item->type == QEMU_INPUT_QUEUE_DELAY);
266 QTAILQ_REMOVE(queue, item, node);
267 queue_count--;
268 g_free(item);
270 while (!QTAILQ_EMPTY(queue)) {
271 item = QTAILQ_FIRST(queue);
272 switch (item->type) {
273 case QEMU_INPUT_QUEUE_DELAY:
274 timer_mod(item->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL)
275 + item->delay_ms);
276 return;
277 case QEMU_INPUT_QUEUE_EVENT:
278 qemu_input_event_send(item->src, item->evt);
279 qapi_free_InputEvent(item->evt);
280 break;
281 case QEMU_INPUT_QUEUE_SYNC:
282 qemu_input_event_sync();
283 break;
285 QTAILQ_REMOVE(queue, item, node);
286 queue_count--;
287 g_free(item);
291 static void qemu_input_queue_delay(struct QemuInputEventQueueHead *queue,
292 QEMUTimer *timer, uint32_t delay_ms)
294 QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1);
295 bool start_timer = QTAILQ_EMPTY(queue);
297 item->type = QEMU_INPUT_QUEUE_DELAY;
298 item->delay_ms = delay_ms;
299 item->timer = timer;
300 QTAILQ_INSERT_TAIL(queue, item, node);
301 queue_count++;
303 if (start_timer) {
304 timer_mod(item->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL)
305 + item->delay_ms);
309 static void qemu_input_queue_event(struct QemuInputEventQueueHead *queue,
310 QemuConsole *src, InputEvent *evt)
312 QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1);
314 item->type = QEMU_INPUT_QUEUE_EVENT;
315 item->src = src;
316 item->evt = evt;
317 QTAILQ_INSERT_TAIL(queue, item, node);
318 queue_count++;
321 static void qemu_input_queue_sync(struct QemuInputEventQueueHead *queue)
323 QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1);
325 item->type = QEMU_INPUT_QUEUE_SYNC;
326 QTAILQ_INSERT_TAIL(queue, item, node);
327 queue_count++;
330 void qemu_input_event_send_impl(QemuConsole *src, InputEvent *evt)
332 QemuInputHandlerState *s;
334 qemu_input_event_trace(src, evt);
336 /* pre processing */
337 if (graphic_rotate && (evt->type == INPUT_EVENT_KIND_ABS)) {
338 qemu_input_transform_abs_rotate(evt);
341 /* send event */
342 s = qemu_input_find_handler(1 << evt->type, src);
343 if (!s) {
344 return;
346 s->handler->event(s->dev, src, evt);
347 s->events++;
350 void qemu_input_event_send(QemuConsole *src, InputEvent *evt)
352 /* Expect all parts of QEMU to send events with QCodes exclusively.
353 * Key numbers are only supported as end-user input via QMP */
354 assert(!(evt->type == INPUT_EVENT_KIND_KEY &&
355 evt->u.key.data->key->type == KEY_VALUE_KIND_NUMBER));
359 * 'sysrq' was mistakenly added to hack around the fact that
360 * the ps2 driver was not generating correct scancodes sequences
361 * when 'alt+print' was pressed. This flaw is now fixed and the
362 * 'sysrq' key serves no further purpose. We normalize it to
363 * 'print', so that downstream receivers of the event don't
364 * neeed to deal with this mistake
366 if (evt->type == INPUT_EVENT_KIND_KEY &&
367 evt->u.key.data->key->u.qcode.data == Q_KEY_CODE_SYSRQ) {
368 evt->u.key.data->key->u.qcode.data = Q_KEY_CODE_PRINT;
371 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
372 return;
375 replay_input_event(src, evt);
378 void qemu_input_event_sync_impl(void)
380 QemuInputHandlerState *s;
382 trace_input_event_sync();
384 QTAILQ_FOREACH(s, &handlers, node) {
385 if (!s->events) {
386 continue;
388 if (s->handler->sync) {
389 s->handler->sync(s->dev);
391 s->events = 0;
395 void qemu_input_event_sync(void)
397 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
398 return;
401 replay_input_sync_event();
404 static InputEvent *qemu_input_event_new_key(KeyValue *key, bool down)
406 InputEvent *evt = g_new0(InputEvent, 1);
407 evt->u.key.data = g_new0(InputKeyEvent, 1);
408 evt->type = INPUT_EVENT_KIND_KEY;
409 evt->u.key.data->key = key;
410 evt->u.key.data->down = down;
411 return evt;
414 void qemu_input_event_send_key(QemuConsole *src, KeyValue *key, bool down)
416 InputEvent *evt;
417 evt = qemu_input_event_new_key(key, down);
418 if (QTAILQ_EMPTY(&kbd_queue)) {
419 qemu_input_event_send(src, evt);
420 qemu_input_event_sync();
421 qapi_free_InputEvent(evt);
422 } else if (queue_count < queue_limit) {
423 qemu_input_queue_event(&kbd_queue, src, evt);
424 qemu_input_queue_sync(&kbd_queue);
425 } else {
426 qapi_free_InputEvent(evt);
430 void qemu_input_event_send_key_number(QemuConsole *src, int num, bool down)
432 QKeyCode code = qemu_input_key_number_to_qcode(num);
433 qemu_input_event_send_key_qcode(src, code, down);
436 void qemu_input_event_send_key_qcode(QemuConsole *src, QKeyCode q, bool down)
438 KeyValue *key = g_new0(KeyValue, 1);
439 key->type = KEY_VALUE_KIND_QCODE;
440 key->u.qcode.data = q;
441 qemu_input_event_send_key(src, key, down);
444 void qemu_input_event_send_key_delay(uint32_t delay_ms)
446 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
447 return;
450 if (!kbd_timer) {
451 kbd_timer = timer_new_full(NULL, QEMU_CLOCK_VIRTUAL,
452 SCALE_MS, QEMU_TIMER_ATTR_EXTERNAL,
453 qemu_input_queue_process, &kbd_queue);
455 if (queue_count < queue_limit) {
456 qemu_input_queue_delay(&kbd_queue, kbd_timer,
457 delay_ms ? delay_ms : kbd_default_delay_ms);
461 InputEvent *qemu_input_event_new_btn(InputButton btn, bool down)
463 InputEvent *evt = g_new0(InputEvent, 1);
464 evt->u.btn.data = g_new0(InputBtnEvent, 1);
465 evt->type = INPUT_EVENT_KIND_BTN;
466 evt->u.btn.data->button = btn;
467 evt->u.btn.data->down = down;
468 return evt;
471 void qemu_input_queue_btn(QemuConsole *src, InputButton btn, bool down)
473 InputEvent *evt;
474 evt = qemu_input_event_new_btn(btn, down);
475 qemu_input_event_send(src, evt);
476 qapi_free_InputEvent(evt);
479 void qemu_input_update_buttons(QemuConsole *src, uint32_t *button_map,
480 uint32_t button_old, uint32_t button_new)
482 InputButton btn;
483 uint32_t mask;
485 for (btn = 0; btn < INPUT_BUTTON__MAX; btn++) {
486 mask = button_map[btn];
487 if ((button_old & mask) == (button_new & mask)) {
488 continue;
490 qemu_input_queue_btn(src, btn, button_new & mask);
494 bool qemu_input_is_absolute(void)
496 QemuInputHandlerState *s;
498 s = qemu_input_find_handler(INPUT_EVENT_MASK_REL | INPUT_EVENT_MASK_ABS,
499 NULL);
500 return (s != NULL) && (s->handler->mask & INPUT_EVENT_MASK_ABS);
503 int qemu_input_scale_axis(int value,
504 int min_in, int max_in,
505 int min_out, int max_out)
507 int64_t range_in = (int64_t)max_in - min_in;
508 int64_t range_out = (int64_t)max_out - min_out;
510 if (range_in < 1) {
511 return min_out + range_out / 2;
513 return ((int64_t)value - min_in) * range_out / range_in + min_out;
516 InputEvent *qemu_input_event_new_move(InputEventKind kind,
517 InputAxis axis, int value)
519 InputEvent *evt = g_new0(InputEvent, 1);
520 InputMoveEvent *move = g_new0(InputMoveEvent, 1);
522 evt->type = kind;
523 evt->u.rel.data = move; /* evt->u.rel is the same as evt->u.abs */
524 move->axis = axis;
525 move->value = value;
526 return evt;
529 void qemu_input_queue_rel(QemuConsole *src, InputAxis axis, int value)
531 InputEvent *evt;
532 evt = qemu_input_event_new_move(INPUT_EVENT_KIND_REL, axis, value);
533 qemu_input_event_send(src, evt);
534 qapi_free_InputEvent(evt);
537 void qemu_input_queue_abs(QemuConsole *src, InputAxis axis, int value,
538 int min_in, int max_in)
540 InputEvent *evt;
541 int scaled = qemu_input_scale_axis(value, min_in, max_in,
542 INPUT_EVENT_ABS_MIN,
543 INPUT_EVENT_ABS_MAX);
544 evt = qemu_input_event_new_move(INPUT_EVENT_KIND_ABS, axis, scaled);
545 qemu_input_event_send(src, evt);
546 qapi_free_InputEvent(evt);
549 void qemu_input_check_mode_change(void)
551 static int current_is_absolute;
552 int is_absolute;
554 is_absolute = qemu_input_is_absolute();
556 if (is_absolute != current_is_absolute) {
557 trace_input_mouse_mode(is_absolute);
558 notifier_list_notify(&mouse_mode_notifiers, NULL);
561 current_is_absolute = is_absolute;
564 void qemu_add_mouse_mode_change_notifier(Notifier *notify)
566 notifier_list_add(&mouse_mode_notifiers, notify);
569 void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
571 notifier_remove(notify);
574 MouseInfoList *qmp_query_mice(Error **errp)
576 MouseInfoList *mice_list = NULL;
577 MouseInfoList *info;
578 QemuInputHandlerState *s;
579 bool current = true;
581 QTAILQ_FOREACH(s, &handlers, node) {
582 if (!(s->handler->mask &
583 (INPUT_EVENT_MASK_REL | INPUT_EVENT_MASK_ABS))) {
584 continue;
587 info = g_new0(MouseInfoList, 1);
588 info->value = g_new0(MouseInfo, 1);
589 info->value->index = s->id;
590 info->value->name = g_strdup(s->handler->name);
591 info->value->absolute = s->handler->mask & INPUT_EVENT_MASK_ABS;
592 info->value->current = current;
594 current = false;
595 info->next = mice_list;
596 mice_list = info;
599 return mice_list;
602 void hmp_mouse_set(Monitor *mon, const QDict *qdict)
604 QemuInputHandlerState *s;
605 int index = qdict_get_int(qdict, "index");
606 int found = 0;
608 QTAILQ_FOREACH(s, &handlers, node) {
609 if (s->id != index) {
610 continue;
612 if (!(s->handler->mask & (INPUT_EVENT_MASK_REL |
613 INPUT_EVENT_MASK_ABS))) {
614 error_report("Input device '%s' is not a mouse", s->handler->name);
615 return;
617 found = 1;
618 qemu_input_handler_activate(s);
619 break;
622 if (!found) {
623 error_report("Mouse at index '%d' not found", index);
626 qemu_input_check_mode_change();