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