xref: /openbmc/qemu/ui/input.c (revision c39f95dc)
1 #include "qemu/osdep.h"
2 #include "sysemu/sysemu.h"
3 #include "qapi-types.h"
4 #include "qemu/error-report.h"
5 #include "qmp-commands.h"
6 #include "trace.h"
7 #include "ui/input.h"
8 #include "ui/console.h"
9 #include "sysemu/replay.h"
10 
11 struct QemuInputHandlerState {
12     DeviceState       *dev;
13     QemuInputHandler  *handler;
14     int               id;
15     int               events;
16     QemuConsole       *con;
17     QTAILQ_ENTRY(QemuInputHandlerState) node;
18 };
19 
20 typedef struct QemuInputEventQueue QemuInputEventQueue;
21 struct QemuInputEventQueue {
22     enum {
23         QEMU_INPUT_QUEUE_DELAY = 1,
24         QEMU_INPUT_QUEUE_EVENT,
25         QEMU_INPUT_QUEUE_SYNC,
26     } type;
27     QEMUTimer *timer;
28     uint32_t delay_ms;
29     QemuConsole *src;
30     InputEvent *evt;
31     QTAILQ_ENTRY(QemuInputEventQueue) node;
32 };
33 
34 static QTAILQ_HEAD(, QemuInputHandlerState) handlers =
35     QTAILQ_HEAD_INITIALIZER(handlers);
36 static NotifierList mouse_mode_notifiers =
37     NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers);
38 
39 static QTAILQ_HEAD(QemuInputEventQueueHead, QemuInputEventQueue) kbd_queue =
40     QTAILQ_HEAD_INITIALIZER(kbd_queue);
41 static QEMUTimer *kbd_timer;
42 static uint32_t kbd_default_delay_ms = 10;
43 static uint32_t queue_count;
44 static uint32_t queue_limit = 1024;
45 
46 QemuInputHandlerState *qemu_input_handler_register(DeviceState *dev,
47                                                    QemuInputHandler *handler)
48 {
49     QemuInputHandlerState *s = g_new0(QemuInputHandlerState, 1);
50     static int id = 1;
51 
52     s->dev = dev;
53     s->handler = handler;
54     s->id = id++;
55     QTAILQ_INSERT_TAIL(&handlers, s, node);
56 
57     qemu_input_check_mode_change();
58     return s;
59 }
60 
61 void qemu_input_handler_activate(QemuInputHandlerState *s)
62 {
63     QTAILQ_REMOVE(&handlers, s, node);
64     QTAILQ_INSERT_HEAD(&handlers, s, node);
65     qemu_input_check_mode_change();
66 }
67 
68 void qemu_input_handler_deactivate(QemuInputHandlerState *s)
69 {
70     QTAILQ_REMOVE(&handlers, s, node);
71     QTAILQ_INSERT_TAIL(&handlers, s, node);
72     qemu_input_check_mode_change();
73 }
74 
75 void qemu_input_handler_unregister(QemuInputHandlerState *s)
76 {
77     QTAILQ_REMOVE(&handlers, s, node);
78     g_free(s);
79     qemu_input_check_mode_change();
80 }
81 
82 void qemu_input_handler_bind(QemuInputHandlerState *s,
83                              const char *device_id, int head,
84                              Error **errp)
85 {
86     QemuConsole *con;
87     Error *err = NULL;
88 
89     con = qemu_console_lookup_by_device_name(device_id, head, &err);
90     if (err) {
91         error_propagate(errp, err);
92         return;
93     }
94 
95     s->con = con;
96 }
97 
98 static QemuInputHandlerState*
99 qemu_input_find_handler(uint32_t mask, QemuConsole *con)
100 {
101     QemuInputHandlerState *s;
102 
103     QTAILQ_FOREACH(s, &handlers, node) {
104         if (s->con == NULL || s->con != con) {
105             continue;
106         }
107         if (mask & s->handler->mask) {
108             return s;
109         }
110     }
111 
112     QTAILQ_FOREACH(s, &handlers, node) {
113         if (s->con != NULL) {
114             continue;
115         }
116         if (mask & s->handler->mask) {
117             return s;
118         }
119     }
120     return NULL;
121 }
122 
123 void qmp_input_send_event(bool has_device, const char *device,
124                           bool has_head, int64_t head,
125                           InputEventList *events, Error **errp)
126 {
127     InputEventList *e;
128     QemuConsole *con;
129     Error *err = NULL;
130 
131     con = NULL;
132     if (has_device) {
133         if (!has_head) {
134             head = 0;
135         }
136         con = qemu_console_lookup_by_device_name(device, head, &err);
137         if (err) {
138             error_propagate(errp, err);
139             return;
140         }
141     }
142 
143     if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
144         error_setg(errp, "VM not running");
145         return;
146     }
147 
148     for (e = events; e != NULL; e = e->next) {
149         InputEvent *event = e->value;
150 
151         if (!qemu_input_find_handler(1 << event->type, con)) {
152             error_setg(errp, "Input handler not found for "
153                              "event type %s",
154                             InputEventKind_str(event->type));
155             return;
156         }
157     }
158 
159     for (e = events; e != NULL; e = e->next) {
160         InputEvent *evt = e->value;
161 
162         if (evt->type == INPUT_EVENT_KIND_KEY &&
163             evt->u.key.data->key->type == KEY_VALUE_KIND_NUMBER) {
164             KeyValue *key = evt->u.key.data->key;
165             QKeyCode code = qemu_input_key_number_to_qcode(key->u.qcode.data);
166             qemu_input_event_send_key_qcode(con, code, evt->u.key.data->down);
167         } else {
168             qemu_input_event_send(con, evt);
169         }
170     }
171 
172     qemu_input_event_sync();
173 }
174 
175 static int qemu_input_transform_invert_abs_value(int value)
176 {
177   return (int64_t)INPUT_EVENT_ABS_MAX - value + INPUT_EVENT_ABS_MIN;
178 }
179 
180 static void qemu_input_transform_abs_rotate(InputEvent *evt)
181 {
182     InputMoveEvent *move = evt->u.abs.data;
183     switch (graphic_rotate) {
184     case 90:
185         if (move->axis == INPUT_AXIS_X) {
186             move->axis = INPUT_AXIS_Y;
187         } else if (move->axis == INPUT_AXIS_Y) {
188             move->axis = INPUT_AXIS_X;
189             move->value = qemu_input_transform_invert_abs_value(move->value);
190         }
191         break;
192     case 180:
193         move->value = qemu_input_transform_invert_abs_value(move->value);
194         break;
195     case 270:
196         if (move->axis == INPUT_AXIS_X) {
197             move->axis = INPUT_AXIS_Y;
198             move->value = qemu_input_transform_invert_abs_value(move->value);
199         } else if (move->axis == INPUT_AXIS_Y) {
200             move->axis = INPUT_AXIS_X;
201         }
202         break;
203     }
204 }
205 
206 static void qemu_input_event_trace(QemuConsole *src, InputEvent *evt)
207 {
208     const char *name;
209     int qcode, idx = -1;
210     InputKeyEvent *key;
211     InputBtnEvent *btn;
212     InputMoveEvent *move;
213 
214     if (src) {
215         idx = qemu_console_get_index(src);
216     }
217     switch (evt->type) {
218     case INPUT_EVENT_KIND_KEY:
219         key = evt->u.key.data;
220         switch (key->key->type) {
221         case KEY_VALUE_KIND_NUMBER:
222             qcode = qemu_input_key_number_to_qcode(key->key->u.number.data);
223             name = QKeyCode_str(qcode);
224             trace_input_event_key_number(idx, key->key->u.number.data,
225                                          name, key->down);
226             break;
227         case KEY_VALUE_KIND_QCODE:
228             name = QKeyCode_str(key->key->u.qcode.data);
229             trace_input_event_key_qcode(idx, name, key->down);
230             break;
231         case KEY_VALUE_KIND__MAX:
232             /* keep gcc happy */
233             break;
234         }
235         break;
236     case INPUT_EVENT_KIND_BTN:
237         btn = evt->u.btn.data;
238         name = InputButton_str(btn->button);
239         trace_input_event_btn(idx, name, btn->down);
240         break;
241     case INPUT_EVENT_KIND_REL:
242         move = evt->u.rel.data;
243         name = InputAxis_str(move->axis);
244         trace_input_event_rel(idx, name, move->value);
245         break;
246     case INPUT_EVENT_KIND_ABS:
247         move = evt->u.abs.data;
248         name = InputAxis_str(move->axis);
249         trace_input_event_abs(idx, name, move->value);
250         break;
251     case INPUT_EVENT_KIND__MAX:
252         /* keep gcc happy */
253         break;
254     }
255 }
256 
257 static void qemu_input_queue_process(void *opaque)
258 {
259     struct QemuInputEventQueueHead *queue = opaque;
260     QemuInputEventQueue *item;
261 
262     g_assert(!QTAILQ_EMPTY(queue));
263     item = QTAILQ_FIRST(queue);
264     g_assert(item->type == QEMU_INPUT_QUEUE_DELAY);
265     QTAILQ_REMOVE(queue, item, node);
266     queue_count--;
267     g_free(item);
268 
269     while (!QTAILQ_EMPTY(queue)) {
270         item = QTAILQ_FIRST(queue);
271         switch (item->type) {
272         case QEMU_INPUT_QUEUE_DELAY:
273             timer_mod(item->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL)
274                       + item->delay_ms);
275             return;
276         case QEMU_INPUT_QUEUE_EVENT:
277             qemu_input_event_send(item->src, item->evt);
278             qapi_free_InputEvent(item->evt);
279             break;
280         case QEMU_INPUT_QUEUE_SYNC:
281             qemu_input_event_sync();
282             break;
283         }
284         QTAILQ_REMOVE(queue, item, node);
285         queue_count--;
286         g_free(item);
287     }
288 }
289 
290 static void qemu_input_queue_delay(struct QemuInputEventQueueHead *queue,
291                                    QEMUTimer *timer, uint32_t delay_ms)
292 {
293     QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1);
294     bool start_timer = QTAILQ_EMPTY(queue);
295 
296     item->type = QEMU_INPUT_QUEUE_DELAY;
297     item->delay_ms = delay_ms;
298     item->timer = timer;
299     QTAILQ_INSERT_TAIL(queue, item, node);
300     queue_count++;
301 
302     if (start_timer) {
303         timer_mod(item->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL)
304                   + item->delay_ms);
305     }
306 }
307 
308 static void qemu_input_queue_event(struct QemuInputEventQueueHead *queue,
309                                    QemuConsole *src, InputEvent *evt)
310 {
311     QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1);
312 
313     item->type = QEMU_INPUT_QUEUE_EVENT;
314     item->src = src;
315     item->evt = evt;
316     QTAILQ_INSERT_TAIL(queue, item, node);
317     queue_count++;
318 }
319 
320 static void qemu_input_queue_sync(struct QemuInputEventQueueHead *queue)
321 {
322     QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1);
323 
324     item->type = QEMU_INPUT_QUEUE_SYNC;
325     QTAILQ_INSERT_TAIL(queue, item, node);
326     queue_count++;
327 }
328 
329 void qemu_input_event_send_impl(QemuConsole *src, InputEvent *evt)
330 {
331     QemuInputHandlerState *s;
332 
333     qemu_input_event_trace(src, evt);
334 
335     /* pre processing */
336     if (graphic_rotate && (evt->type == INPUT_EVENT_KIND_ABS)) {
337             qemu_input_transform_abs_rotate(evt);
338     }
339 
340     /* send event */
341     s = qemu_input_find_handler(1 << evt->type, src);
342     if (!s) {
343         return;
344     }
345     s->handler->event(s->dev, src, evt);
346     s->events++;
347 }
348 
349 void qemu_input_event_send(QemuConsole *src, InputEvent *evt)
350 {
351     /* Expect all parts of QEMU to send events with QCodes exclusively.
352      * Key numbers are only supported as end-user input via QMP */
353     assert(!(evt->type == INPUT_EVENT_KIND_KEY &&
354              evt->u.key.data->key->type == KEY_VALUE_KIND_NUMBER));
355 
356     if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
357         return;
358     }
359 
360     replay_input_event(src, evt);
361 }
362 
363 void qemu_input_event_sync_impl(void)
364 {
365     QemuInputHandlerState *s;
366 
367     trace_input_event_sync();
368 
369     QTAILQ_FOREACH(s, &handlers, node) {
370         if (!s->events) {
371             continue;
372         }
373         if (s->handler->sync) {
374             s->handler->sync(s->dev);
375         }
376         s->events = 0;
377     }
378 }
379 
380 void qemu_input_event_sync(void)
381 {
382     if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
383         return;
384     }
385 
386     replay_input_sync_event();
387 }
388 
389 static InputEvent *qemu_input_event_new_key(KeyValue *key, bool down)
390 {
391     InputEvent *evt = g_new0(InputEvent, 1);
392     evt->u.key.data = g_new0(InputKeyEvent, 1);
393     evt->type = INPUT_EVENT_KIND_KEY;
394     evt->u.key.data->key = key;
395     evt->u.key.data->down = down;
396     return evt;
397 }
398 
399 void qemu_input_event_send_key(QemuConsole *src, KeyValue *key, bool down)
400 {
401     InputEvent *evt;
402     evt = qemu_input_event_new_key(key, down);
403     if (QTAILQ_EMPTY(&kbd_queue)) {
404         qemu_input_event_send(src, evt);
405         qemu_input_event_sync();
406         qapi_free_InputEvent(evt);
407     } else if (queue_count < queue_limit) {
408         qemu_input_queue_event(&kbd_queue, src, evt);
409         qemu_input_queue_sync(&kbd_queue);
410     }
411 }
412 
413 void qemu_input_event_send_key_number(QemuConsole *src, int num, bool down)
414 {
415     QKeyCode code = qemu_input_key_number_to_qcode(num);
416     qemu_input_event_send_key_qcode(src, code, down);
417 }
418 
419 void qemu_input_event_send_key_qcode(QemuConsole *src, QKeyCode q, bool down)
420 {
421     KeyValue *key = g_new0(KeyValue, 1);
422     key->type = KEY_VALUE_KIND_QCODE;
423     key->u.qcode.data = q;
424     qemu_input_event_send_key(src, key, down);
425 }
426 
427 void qemu_input_event_send_key_delay(uint32_t delay_ms)
428 {
429     if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
430         return;
431     }
432 
433     if (!kbd_timer) {
434         kbd_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, qemu_input_queue_process,
435                                  &kbd_queue);
436     }
437     if (queue_count < queue_limit) {
438         qemu_input_queue_delay(&kbd_queue, kbd_timer,
439                                delay_ms ? delay_ms : kbd_default_delay_ms);
440     }
441 }
442 
443 InputEvent *qemu_input_event_new_btn(InputButton btn, bool down)
444 {
445     InputEvent *evt = g_new0(InputEvent, 1);
446     evt->u.btn.data = g_new0(InputBtnEvent, 1);
447     evt->type = INPUT_EVENT_KIND_BTN;
448     evt->u.btn.data->button = btn;
449     evt->u.btn.data->down = down;
450     return evt;
451 }
452 
453 void qemu_input_queue_btn(QemuConsole *src, InputButton btn, bool down)
454 {
455     InputEvent *evt;
456     evt = qemu_input_event_new_btn(btn, down);
457     qemu_input_event_send(src, evt);
458     qapi_free_InputEvent(evt);
459 }
460 
461 void qemu_input_update_buttons(QemuConsole *src, uint32_t *button_map,
462                                uint32_t button_old, uint32_t button_new)
463 {
464     InputButton btn;
465     uint32_t mask;
466 
467     for (btn = 0; btn < INPUT_BUTTON__MAX; btn++) {
468         mask = button_map[btn];
469         if ((button_old & mask) == (button_new & mask)) {
470             continue;
471         }
472         qemu_input_queue_btn(src, btn, button_new & mask);
473     }
474 }
475 
476 bool qemu_input_is_absolute(void)
477 {
478     QemuInputHandlerState *s;
479 
480     s = qemu_input_find_handler(INPUT_EVENT_MASK_REL | INPUT_EVENT_MASK_ABS,
481                                 NULL);
482     return (s != NULL) && (s->handler->mask & INPUT_EVENT_MASK_ABS);
483 }
484 
485 int qemu_input_scale_axis(int value,
486                           int min_in, int max_in,
487                           int min_out, int max_out)
488 {
489     int64_t range_in = (int64_t)max_in - min_in;
490     int64_t range_out = (int64_t)max_out - min_out;
491 
492     if (range_in < 1) {
493         return min_out + range_out / 2;
494     }
495     return ((int64_t)value - min_in) * range_out / range_in + min_out;
496 }
497 
498 InputEvent *qemu_input_event_new_move(InputEventKind kind,
499                                       InputAxis axis, int value)
500 {
501     InputEvent *evt = g_new0(InputEvent, 1);
502     InputMoveEvent *move = g_new0(InputMoveEvent, 1);
503 
504     evt->type = kind;
505     evt->u.rel.data = move; /* evt->u.rel is the same as evt->u.abs */
506     move->axis = axis;
507     move->value = value;
508     return evt;
509 }
510 
511 void qemu_input_queue_rel(QemuConsole *src, InputAxis axis, int value)
512 {
513     InputEvent *evt;
514     evt = qemu_input_event_new_move(INPUT_EVENT_KIND_REL, axis, value);
515     qemu_input_event_send(src, evt);
516     qapi_free_InputEvent(evt);
517 }
518 
519 void qemu_input_queue_abs(QemuConsole *src, InputAxis axis, int value,
520                           int min_in, int max_in)
521 {
522     InputEvent *evt;
523     int scaled = qemu_input_scale_axis(value, min_in, max_in,
524                                        INPUT_EVENT_ABS_MIN,
525                                        INPUT_EVENT_ABS_MAX);
526     evt = qemu_input_event_new_move(INPUT_EVENT_KIND_ABS, axis, scaled);
527     qemu_input_event_send(src, evt);
528     qapi_free_InputEvent(evt);
529 }
530 
531 void qemu_input_check_mode_change(void)
532 {
533     static int current_is_absolute;
534     int is_absolute;
535 
536     is_absolute = qemu_input_is_absolute();
537 
538     if (is_absolute != current_is_absolute) {
539         trace_input_mouse_mode(is_absolute);
540         notifier_list_notify(&mouse_mode_notifiers, NULL);
541     }
542 
543     current_is_absolute = is_absolute;
544 }
545 
546 void qemu_add_mouse_mode_change_notifier(Notifier *notify)
547 {
548     notifier_list_add(&mouse_mode_notifiers, notify);
549 }
550 
551 void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
552 {
553     notifier_remove(notify);
554 }
555 
556 MouseInfoList *qmp_query_mice(Error **errp)
557 {
558     MouseInfoList *mice_list = NULL;
559     MouseInfoList *info;
560     QemuInputHandlerState *s;
561     bool current = true;
562 
563     QTAILQ_FOREACH(s, &handlers, node) {
564         if (!(s->handler->mask &
565               (INPUT_EVENT_MASK_REL | INPUT_EVENT_MASK_ABS))) {
566             continue;
567         }
568 
569         info = g_new0(MouseInfoList, 1);
570         info->value = g_new0(MouseInfo, 1);
571         info->value->index = s->id;
572         info->value->name = g_strdup(s->handler->name);
573         info->value->absolute = s->handler->mask & INPUT_EVENT_MASK_ABS;
574         info->value->current = current;
575 
576         current = false;
577         info->next = mice_list;
578         mice_list = info;
579     }
580 
581     return mice_list;
582 }
583 
584 void hmp_mouse_set(Monitor *mon, const QDict *qdict)
585 {
586     QemuInputHandlerState *s;
587     int index = qdict_get_int(qdict, "index");
588     int found = 0;
589 
590     QTAILQ_FOREACH(s, &handlers, node) {
591         if (s->id != index) {
592             continue;
593         }
594         if (!(s->handler->mask & (INPUT_EVENT_MASK_REL |
595                                   INPUT_EVENT_MASK_ABS))) {
596             error_report("Input device '%s' is not a mouse", s->handler->name);
597             return;
598         }
599         found = 1;
600         qemu_input_handler_activate(s);
601         break;
602     }
603 
604     if (!found) {
605         error_report("Mouse at index '%d' not found", index);
606     }
607 
608     qemu_input_check_mode_change();
609 }
610