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