input: limit kbd queue depth
[qemu.git] / ui / input.c
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"
11
12 struct QemuInputHandlerState {
13 DeviceState *dev;
14 QemuInputHandler *handler;
15 int id;
16 int events;
17 QemuConsole *con;
18 QTAILQ_ENTRY(QemuInputHandlerState) node;
19 };
20
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;
33 };
34
35 static QTAILQ_HEAD(, QemuInputHandlerState) handlers =
36 QTAILQ_HEAD_INITIALIZER(handlers);
37 static NotifierList mouse_mode_notifiers =
38 NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers);
39
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;
46
47 QemuInputHandlerState *qemu_input_handler_register(DeviceState *dev,
48 QemuInputHandler *handler)
49 {
50 QemuInputHandlerState *s = g_new0(QemuInputHandlerState, 1);
51 static int id = 1;
52
53 s->dev = dev;
54 s->handler = handler;
55 s->id = id++;
56 QTAILQ_INSERT_TAIL(&handlers, s, node);
57
58 qemu_input_check_mode_change();
59 return s;
60 }
61
62 void qemu_input_handler_activate(QemuInputHandlerState *s)
63 {
64 QTAILQ_REMOVE(&handlers, s, node);
65 QTAILQ_INSERT_HEAD(&handlers, s, node);
66 qemu_input_check_mode_change();
67 }
68
69 void qemu_input_handler_deactivate(QemuInputHandlerState *s)
70 {
71 QTAILQ_REMOVE(&handlers, s, node);
72 QTAILQ_INSERT_TAIL(&handlers, s, node);
73 qemu_input_check_mode_change();
74 }
75
76 void qemu_input_handler_unregister(QemuInputHandlerState *s)
77 {
78 QTAILQ_REMOVE(&handlers, s, node);
79 g_free(s);
80 qemu_input_check_mode_change();
81 }
82
83 void qemu_input_handler_bind(QemuInputHandlerState *s,
84 const char *device_id, int head,
85 Error **errp)
86 {
87 QemuConsole *con;
88 Error *err = NULL;
89
90 con = qemu_console_lookup_by_device_name(device_id, head, &err);
91 if (err) {
92 error_propagate(errp, err);
93 return;
94 }
95
96 s->con = con;
97 }
98
99 static QemuInputHandlerState*
100 qemu_input_find_handler(uint32_t mask, QemuConsole *con)
101 {
102 QemuInputHandlerState *s;
103
104 QTAILQ_FOREACH(s, &handlers, node) {
105 if (s->con == NULL || s->con != con) {
106 continue;
107 }
108 if (mask & s->handler->mask) {
109 return s;
110 }
111 }
112
113 QTAILQ_FOREACH(s, &handlers, node) {
114 if (s->con != NULL) {
115 continue;
116 }
117 if (mask & s->handler->mask) {
118 return s;
119 }
120 }
121 return NULL;
122 }
123
124 void qmp_input_send_event(bool has_device, const char *device,
125 bool has_head, int64_t head,
126 InputEventList *events, Error **errp)
127 {
128 InputEventList *e;
129 QemuConsole *con;
130 Error *err = NULL;
131
132 con = NULL;
133 if (has_device) {
134 if (!has_head) {
135 head = 0;
136 }
137 con = qemu_console_lookup_by_device_name(device, head, &err);
138 if (err) {
139 error_propagate(errp, err);
140 return;
141 }
142 }
143
144 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
145 error_setg(errp, "VM not running");
146 return;
147 }
148
149 for (e = events; e != NULL; e = e->next) {
150 InputEvent *event = e->value;
151
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;
157 }
158 }
159
160 for (e = events; e != NULL; e = e->next) {
161 InputEvent *event = e->value;
162
163 qemu_input_event_send(con, event);
164 }
165
166 qemu_input_event_sync();
167 }
168
169 static void qemu_input_transform_abs_rotate(InputEvent *evt)
170 {
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;
179 }
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;
190 }
191 break;
192 }
193 }
194
195 static void qemu_input_event_trace(QemuConsole *src, InputEvent *evt)
196 {
197 const char *name;
198 int qcode, idx = -1;
199 InputKeyEvent *key;
200 InputBtnEvent *btn;
201 InputMoveEvent *move;
202
203 if (src) {
204 idx = qemu_console_get_index(src);
205 }
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;
223 }
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;
243 }
244 }
245
246 static void qemu_input_queue_process(void *opaque)
247 {
248 struct QemuInputEventQueueHead *queue = opaque;
249 QemuInputEventQueue *item;
250
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);
256
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;
271 }
272 QTAILQ_REMOVE(queue, item, node);
273 queue_count--;
274 g_free(item);
275 }
276 }
277
278 static void qemu_input_queue_delay(struct QemuInputEventQueueHead *queue,
279 QEMUTimer *timer, uint32_t delay_ms)
280 {
281 QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1);
282 bool start_timer = QTAILQ_EMPTY(queue);
283
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++;
289
290 if (start_timer) {
291 timer_mod(item->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL)
292 + item->delay_ms);
293 }
294 }
295
296 static void qemu_input_queue_event(struct QemuInputEventQueueHead *queue,
297 QemuConsole *src, InputEvent *evt)
298 {
299 QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1);
300
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++;
306 }
307
308 static void qemu_input_queue_sync(struct QemuInputEventQueueHead *queue)
309 {
310 QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1);
311
312 item->type = QEMU_INPUT_QUEUE_SYNC;
313 QTAILQ_INSERT_TAIL(queue, item, node);
314 queue_count++;
315 }
316
317 void qemu_input_event_send_impl(QemuConsole *src, InputEvent *evt)
318 {
319 QemuInputHandlerState *s;
320
321 qemu_input_event_trace(src, evt);
322
323 /* pre processing */
324 if (graphic_rotate && (evt->type == INPUT_EVENT_KIND_ABS)) {
325 qemu_input_transform_abs_rotate(evt);
326 }
327
328 /* send event */
329 s = qemu_input_find_handler(1 << evt->type, src);
330 if (!s) {
331 return;
332 }
333 s->handler->event(s->dev, src, evt);
334 s->events++;
335 }
336
337 void qemu_input_event_send(QemuConsole *src, InputEvent *evt)
338 {
339 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
340 return;
341 }
342
343 replay_input_event(src, evt);
344 }
345
346 void qemu_input_event_sync_impl(void)
347 {
348 QemuInputHandlerState *s;
349
350 trace_input_event_sync();
351
352 QTAILQ_FOREACH(s, &handlers, node) {
353 if (!s->events) {
354 continue;
355 }
356 if (s->handler->sync) {
357 s->handler->sync(s->dev);
358 }
359 s->events = 0;
360 }
361 }
362
363 void qemu_input_event_sync(void)
364 {
365 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
366 return;
367 }
368
369 replay_input_sync_event();
370 }
371
372 InputEvent *qemu_input_event_new_key(KeyValue *key, bool down)
373 {
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;
380 }
381
382 void qemu_input_event_send_key(QemuConsole *src, KeyValue *key, bool down)
383 {
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);
393 }
394 }
395
396 void qemu_input_event_send_key_number(QemuConsole *src, int num, bool down)
397 {
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);
402 }
403
404 void qemu_input_event_send_key_qcode(QemuConsole *src, QKeyCode q, bool down)
405 {
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);
410 }
411
412 void qemu_input_event_send_key_delay(uint32_t delay_ms)
413 {
414 if (!kbd_timer) {
415 kbd_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, qemu_input_queue_process,
416 &kbd_queue);
417 }
418 if (queue_count < queue_limit) {
419 qemu_input_queue_delay(&kbd_queue, kbd_timer,
420 delay_ms ? delay_ms : kbd_default_delay_ms);
421 }
422 }
423
424 InputEvent *qemu_input_event_new_btn(InputButton btn, bool down)
425 {
426 InputEvent *evt = g_new0(InputEvent, 1);
427 evt->u.btn.data = g_new0(InputBtnEvent, 1);
428 evt->type = INPUT_EVENT_KIND_BTN;
429 evt->u.btn.data->button = btn;
430 evt->u.btn.data->down = down;
431 return evt;
432 }
433
434 void qemu_input_queue_btn(QemuConsole *src, InputButton btn, bool down)
435 {
436 InputEvent *evt;
437 evt = qemu_input_event_new_btn(btn, down);
438 qemu_input_event_send(src, evt);
439 qapi_free_InputEvent(evt);
440 }
441
442 void qemu_input_update_buttons(QemuConsole *src, uint32_t *button_map,
443 uint32_t button_old, uint32_t button_new)
444 {
445 InputButton btn;
446 uint32_t mask;
447
448 for (btn = 0; btn < INPUT_BUTTON__MAX; btn++) {
449 mask = button_map[btn];
450 if ((button_old & mask) == (button_new & mask)) {
451 continue;
452 }
453 qemu_input_queue_btn(src, btn, button_new & mask);
454 }
455 }
456
457 bool qemu_input_is_absolute(void)
458 {
459 QemuInputHandlerState *s;
460
461 s = qemu_input_find_handler(INPUT_EVENT_MASK_REL | INPUT_EVENT_MASK_ABS,
462 NULL);
463 return (s != NULL) && (s->handler->mask & INPUT_EVENT_MASK_ABS);
464 }
465
466 int qemu_input_scale_axis(int value, int size_in, int size_out)
467 {
468 if (size_in < 2) {
469 return size_out / 2;
470 }
471 return (int64_t)value * (size_out - 1) / (size_in - 1);
472 }
473
474 InputEvent *qemu_input_event_new_move(InputEventKind kind,
475 InputAxis axis, int value)
476 {
477 InputEvent *evt = g_new0(InputEvent, 1);
478 InputMoveEvent *move = g_new0(InputMoveEvent, 1);
479
480 evt->type = kind;
481 evt->u.rel.data = move; /* evt->u.rel is the same as evt->u.abs */
482 move->axis = axis;
483 move->value = value;
484 return evt;
485 }
486
487 void qemu_input_queue_rel(QemuConsole *src, InputAxis axis, int value)
488 {
489 InputEvent *evt;
490 evt = qemu_input_event_new_move(INPUT_EVENT_KIND_REL, axis, value);
491 qemu_input_event_send(src, evt);
492 qapi_free_InputEvent(evt);
493 }
494
495 void qemu_input_queue_abs(QemuConsole *src, InputAxis axis, int value, int size)
496 {
497 InputEvent *evt;
498 int scaled = qemu_input_scale_axis(value, size, INPUT_EVENT_ABS_SIZE);
499 evt = qemu_input_event_new_move(INPUT_EVENT_KIND_ABS, axis, scaled);
500 qemu_input_event_send(src, evt);
501 qapi_free_InputEvent(evt);
502 }
503
504 void qemu_input_check_mode_change(void)
505 {
506 static int current_is_absolute;
507 int is_absolute;
508
509 is_absolute = qemu_input_is_absolute();
510
511 if (is_absolute != current_is_absolute) {
512 trace_input_mouse_mode(is_absolute);
513 notifier_list_notify(&mouse_mode_notifiers, NULL);
514 }
515
516 current_is_absolute = is_absolute;
517 }
518
519 void qemu_add_mouse_mode_change_notifier(Notifier *notify)
520 {
521 notifier_list_add(&mouse_mode_notifiers, notify);
522 }
523
524 void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
525 {
526 notifier_remove(notify);
527 }
528
529 MouseInfoList *qmp_query_mice(Error **errp)
530 {
531 MouseInfoList *mice_list = NULL;
532 MouseInfoList *info;
533 QemuInputHandlerState *s;
534 bool current = true;
535
536 QTAILQ_FOREACH(s, &handlers, node) {
537 if (!(s->handler->mask &
538 (INPUT_EVENT_MASK_REL | INPUT_EVENT_MASK_ABS))) {
539 continue;
540 }
541
542 info = g_new0(MouseInfoList, 1);
543 info->value = g_new0(MouseInfo, 1);
544 info->value->index = s->id;
545 info->value->name = g_strdup(s->handler->name);
546 info->value->absolute = s->handler->mask & INPUT_EVENT_MASK_ABS;
547 info->value->current = current;
548
549 current = false;
550 info->next = mice_list;
551 mice_list = info;
552 }
553
554 return mice_list;
555 }
556
557 void hmp_mouse_set(Monitor *mon, const QDict *qdict)
558 {
559 QemuInputHandlerState *s;
560 int index = qdict_get_int(qdict, "index");
561 int found = 0;
562
563 QTAILQ_FOREACH(s, &handlers, node) {
564 if (s->id != index) {
565 continue;
566 }
567 if (!(s->handler->mask & (INPUT_EVENT_MASK_REL |
568 INPUT_EVENT_MASK_ABS))) {
569 error_report("Input device '%s' is not a mouse", s->handler->name);
570 return;
571 }
572 found = 1;
573 qemu_input_handler_activate(s);
574 break;
575 }
576
577 if (!found) {
578 error_report("Mouse at index '%d' not found", index);
579 }
580
581 qemu_input_check_mode_change();
582 }