xref: /openbmc/qemu/ui/input.c (revision e6d34aee)
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"
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_str(event->type));
156             return;
157         }
158     }
159 
160     for (e = events; e != NULL; e = e->next) {
161         InputEvent *evt = e->value;
162 
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);
170         }
171     }
172 
173     qemu_input_event_sync();
174 }
175 
176 static int qemu_input_transform_invert_abs_value(int value)
177 {
178   return (int64_t)INPUT_EVENT_ABS_MAX - value + INPUT_EVENT_ABS_MIN;
179 }
180 
181 static void qemu_input_transform_abs_rotate(InputEvent *evt)
182 {
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);
191         }
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;
202         }
203         break;
204     }
205 }
206 
207 static void qemu_input_event_trace(QemuConsole *src, InputEvent *evt)
208 {
209     const char *name;
210     int qcode, idx = -1;
211     InputKeyEvent *key;
212     InputBtnEvent *btn;
213     InputMoveEvent *move;
214 
215     if (src) {
216         idx = qemu_console_get_index(src);
217     }
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;
235         }
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;
255     }
256 }
257 
258 static void qemu_input_queue_process(void *opaque)
259 {
260     struct QemuInputEventQueueHead *queue = opaque;
261     QemuInputEventQueue *item;
262 
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);
269 
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;
284         }
285         QTAILQ_REMOVE(queue, item, node);
286         queue_count--;
287         g_free(item);
288     }
289 }
290 
291 static void qemu_input_queue_delay(struct QemuInputEventQueueHead *queue,
292                                    QEMUTimer *timer, uint32_t delay_ms)
293 {
294     QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1);
295     bool start_timer = QTAILQ_EMPTY(queue);
296 
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++;
302 
303     if (start_timer) {
304         timer_mod(item->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL)
305                   + item->delay_ms);
306     }
307 }
308 
309 static void qemu_input_queue_event(struct QemuInputEventQueueHead *queue,
310                                    QemuConsole *src, InputEvent *evt)
311 {
312     QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1);
313 
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++;
319 }
320 
321 static void qemu_input_queue_sync(struct QemuInputEventQueueHead *queue)
322 {
323     QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1);
324 
325     item->type = QEMU_INPUT_QUEUE_SYNC;
326     QTAILQ_INSERT_TAIL(queue, item, node);
327     queue_count++;
328 }
329 
330 void qemu_input_event_send_impl(QemuConsole *src, InputEvent *evt)
331 {
332     QemuInputHandlerState *s;
333 
334     qemu_input_event_trace(src, evt);
335 
336     /* pre processing */
337     if (graphic_rotate && (evt->type == INPUT_EVENT_KIND_ABS)) {
338             qemu_input_transform_abs_rotate(evt);
339     }
340 
341     /* send event */
342     s = qemu_input_find_handler(1 << evt->type, src);
343     if (!s) {
344         return;
345     }
346     s->handler->event(s->dev, src, evt);
347     s->events++;
348 }
349 
350 void qemu_input_event_send(QemuConsole *src, InputEvent *evt)
351 {
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));
356 
357 
358     /*
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
365      */
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;
369     }
370 
371     if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
372         return;
373     }
374 
375     replay_input_event(src, evt);
376 }
377 
378 void qemu_input_event_sync_impl(void)
379 {
380     QemuInputHandlerState *s;
381 
382     trace_input_event_sync();
383 
384     QTAILQ_FOREACH(s, &handlers, node) {
385         if (!s->events) {
386             continue;
387         }
388         if (s->handler->sync) {
389             s->handler->sync(s->dev);
390         }
391         s->events = 0;
392     }
393 }
394 
395 void qemu_input_event_sync(void)
396 {
397     if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
398         return;
399     }
400 
401     replay_input_sync_event();
402 }
403 
404 static InputEvent *qemu_input_event_new_key(KeyValue *key, bool down)
405 {
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;
412 }
413 
414 void qemu_input_event_send_key(QemuConsole *src, KeyValue *key, bool down)
415 {
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);
427     }
428 }
429 
430 void qemu_input_event_send_key_number(QemuConsole *src, int num, bool down)
431 {
432     QKeyCode code = qemu_input_key_number_to_qcode(num);
433     qemu_input_event_send_key_qcode(src, code, down);
434 }
435 
436 void qemu_input_event_send_key_qcode(QemuConsole *src, QKeyCode q, bool down)
437 {
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);
442 }
443 
444 void qemu_input_event_send_key_delay(uint32_t delay_ms)
445 {
446     if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
447         return;
448     }
449 
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);
454     }
455     if (queue_count < queue_limit) {
456         qemu_input_queue_delay(&kbd_queue, kbd_timer,
457                                delay_ms ? delay_ms : kbd_default_delay_ms);
458     }
459 }
460 
461 InputEvent *qemu_input_event_new_btn(InputButton btn, bool down)
462 {
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;
469 }
470 
471 void qemu_input_queue_btn(QemuConsole *src, InputButton btn, bool down)
472 {
473     InputEvent *evt;
474     evt = qemu_input_event_new_btn(btn, down);
475     qemu_input_event_send(src, evt);
476     qapi_free_InputEvent(evt);
477 }
478 
479 void qemu_input_update_buttons(QemuConsole *src, uint32_t *button_map,
480                                uint32_t button_old, uint32_t button_new)
481 {
482     InputButton btn;
483     uint32_t mask;
484 
485     for (btn = 0; btn < INPUT_BUTTON__MAX; btn++) {
486         mask = button_map[btn];
487         if ((button_old & mask) == (button_new & mask)) {
488             continue;
489         }
490         qemu_input_queue_btn(src, btn, button_new & mask);
491     }
492 }
493 
494 bool qemu_input_is_absolute(void)
495 {
496     QemuInputHandlerState *s;
497 
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);
501 }
502 
503 int qemu_input_scale_axis(int value,
504                           int min_in, int max_in,
505                           int min_out, int max_out)
506 {
507     int64_t range_in = (int64_t)max_in - min_in;
508     int64_t range_out = (int64_t)max_out - min_out;
509 
510     if (range_in < 1) {
511         return min_out + range_out / 2;
512     }
513     return ((int64_t)value - min_in) * range_out / range_in + min_out;
514 }
515 
516 InputEvent *qemu_input_event_new_move(InputEventKind kind,
517                                       InputAxis axis, int value)
518 {
519     InputEvent *evt = g_new0(InputEvent, 1);
520     InputMoveEvent *move = g_new0(InputMoveEvent, 1);
521 
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;
527 }
528 
529 void qemu_input_queue_rel(QemuConsole *src, InputAxis axis, int value)
530 {
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);
535 }
536 
537 void qemu_input_queue_abs(QemuConsole *src, InputAxis axis, int value,
538                           int min_in, int max_in)
539 {
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);
547 }
548 
549 void qemu_input_check_mode_change(void)
550 {
551     static int current_is_absolute;
552     int is_absolute;
553 
554     is_absolute = qemu_input_is_absolute();
555 
556     if (is_absolute != current_is_absolute) {
557         trace_input_mouse_mode(is_absolute);
558         notifier_list_notify(&mouse_mode_notifiers, NULL);
559     }
560 
561     current_is_absolute = is_absolute;
562 }
563 
564 void qemu_add_mouse_mode_change_notifier(Notifier *notify)
565 {
566     notifier_list_add(&mouse_mode_notifiers, notify);
567 }
568 
569 void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
570 {
571     notifier_remove(notify);
572 }
573 
574 MouseInfoList *qmp_query_mice(Error **errp)
575 {
576     MouseInfoList *mice_list = NULL;
577     MouseInfoList *info;
578     QemuInputHandlerState *s;
579     bool current = true;
580 
581     QTAILQ_FOREACH(s, &handlers, node) {
582         if (!(s->handler->mask &
583               (INPUT_EVENT_MASK_REL | INPUT_EVENT_MASK_ABS))) {
584             continue;
585         }
586 
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;
593 
594         current = false;
595         info->next = mice_list;
596         mice_list = info;
597     }
598 
599     return mice_list;
600 }
601 
602 void hmp_mouse_set(Monitor *mon, const QDict *qdict)
603 {
604     QemuInputHandlerState *s;
605     int index = qdict_get_int(qdict, "index");
606     int found = 0;
607 
608     QTAILQ_FOREACH(s, &handlers, node) {
609         if (s->id != index) {
610             continue;
611         }
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;
616         }
617         found = 1;
618         qemu_input_handler_activate(s);
619         break;
620     }
621 
622     if (!found) {
623         error_report("Mouse at index '%d' not found", index);
624     }
625 
626     qemu_input_check_mode_change();
627 }
628