xref: /qemu/ui/input-linux.c (revision db1015e92e04835c9eb50c29625fe566d1202dbd)
1 /*
2  * This work is licensed under the terms of the GNU GPL, version 2 or
3  * (at your option) any later version.  See the COPYING file in the
4  * top-level directory.
5  */
6 
7 #include "qemu/osdep.h"
8 #include "qapi/error.h"
9 #include "qemu/config-file.h"
10 #include "qemu/main-loop.h"
11 #include "qemu/module.h"
12 #include "qemu/sockets.h"
13 #include "ui/input.h"
14 #include "qom/object_interfaces.h"
15 #include "sysemu/iothread.h"
16 #include "block/aio.h"
17 
18 #include <sys/ioctl.h>
19 #include "standard-headers/linux/input.h"
20 #include "qom/object.h"
21 
22 static bool linux_is_button(unsigned int lnx)
23 {
24     if (lnx < 0x100) {
25         return false;
26     }
27     if (lnx >= 0x160 && lnx < 0x2c0) {
28         return false;
29     }
30     return true;
31 }
32 
33 #define TYPE_INPUT_LINUX "input-linux"
34 typedef struct InputLinux InputLinux;
35 typedef struct InputLinuxClass InputLinuxClass;
36 #define INPUT_LINUX(obj) \
37     OBJECT_CHECK(InputLinux, (obj), TYPE_INPUT_LINUX)
38 #define INPUT_LINUX_GET_CLASS(obj) \
39     OBJECT_GET_CLASS(InputLinuxClass, (obj), TYPE_INPUT_LINUX)
40 #define INPUT_LINUX_CLASS(klass) \
41     OBJECT_CLASS_CHECK(InputLinuxClass, (klass), TYPE_INPUT_LINUX)
42 
43 
44 struct InputLinux {
45     Object parent;
46 
47     char        *evdev;
48     int         fd;
49     bool        repeat;
50     bool        grab_request;
51     bool        grab_active;
52     bool        grab_all;
53     bool        keydown[KEY_CNT];
54     int         keycount;
55     int         wheel;
56     bool        initialized;
57 
58     bool        has_rel_x;
59     bool        has_abs_x;
60     int         num_keys;
61     int         num_btns;
62     int         abs_x_min;
63     int         abs_x_max;
64     int         abs_y_min;
65     int         abs_y_max;
66     struct input_event event;
67     int         read_offset;
68 
69     enum GrabToggleKeys grab_toggle;
70 
71     QTAILQ_ENTRY(InputLinux) next;
72 };
73 
74 struct InputLinuxClass {
75     ObjectClass parent_class;
76 };
77 
78 static QTAILQ_HEAD(, InputLinux) inputs = QTAILQ_HEAD_INITIALIZER(inputs);
79 
80 static void input_linux_toggle_grab(InputLinux *il)
81 {
82     intptr_t request = !il->grab_active;
83     InputLinux *item;
84     int rc;
85 
86     rc = ioctl(il->fd, EVIOCGRAB, request);
87     if (rc < 0) {
88         return;
89     }
90     il->grab_active = !il->grab_active;
91 
92     if (!il->grab_all) {
93         return;
94     }
95     QTAILQ_FOREACH(item, &inputs, next) {
96         if (item == il || item->grab_all) {
97             /* avoid endless loops */
98             continue;
99         }
100         if (item->grab_active != il->grab_active) {
101             input_linux_toggle_grab(item);
102         }
103     }
104 }
105 
106 static bool input_linux_check_toggle(InputLinux *il)
107 {
108     switch (il->grab_toggle) {
109     case GRAB_TOGGLE_KEYS_CTRL_CTRL:
110         return il->keydown[KEY_LEFTCTRL] &&
111             il->keydown[KEY_RIGHTCTRL];
112 
113     case GRAB_TOGGLE_KEYS_ALT_ALT:
114         return il->keydown[KEY_LEFTALT] &&
115             il->keydown[KEY_RIGHTALT];
116 
117     case GRAB_TOGGLE_KEYS_SHIFT_SHIFT:
118         return il->keydown[KEY_LEFTSHIFT] &&
119             il->keydown[KEY_RIGHTSHIFT];
120 
121     case GRAB_TOGGLE_KEYS_META_META:
122         return il->keydown[KEY_LEFTMETA] &&
123             il->keydown[KEY_RIGHTMETA];
124 
125     case GRAB_TOGGLE_KEYS_SCROLLLOCK:
126         return il->keydown[KEY_SCROLLLOCK];
127 
128     case GRAB_TOGGLE_KEYS_CTRL_SCROLLLOCK:
129         return (il->keydown[KEY_LEFTCTRL] ||
130                 il->keydown[KEY_RIGHTCTRL]) &&
131             il->keydown[KEY_SCROLLLOCK];
132 
133     case GRAB_TOGGLE_KEYS__MAX:
134         /* avoid gcc error */
135         break;
136     }
137     return false;
138 }
139 
140 static bool input_linux_should_skip(InputLinux *il,
141                                     struct input_event *event)
142 {
143     return (il->grab_toggle == GRAB_TOGGLE_KEYS_SCROLLLOCK ||
144             il->grab_toggle == GRAB_TOGGLE_KEYS_CTRL_SCROLLLOCK) &&
145             event->code == KEY_SCROLLLOCK;
146 }
147 
148 static void input_linux_handle_keyboard(InputLinux *il,
149                                         struct input_event *event)
150 {
151     if (event->type == EV_KEY) {
152         if (event->value > 2 || (event->value > 1 && !il->repeat)) {
153             /*
154              * ignore autorepeat + unknown key events
155              * 0 == up, 1 == down, 2 == autorepeat, other == undefined
156              */
157             return;
158         }
159         if (event->code >= KEY_CNT) {
160             /*
161              * Should not happen.  But better safe than sorry,
162              * and we make Coverity happy too.
163              */
164             return;
165         }
166 
167         /* keep track of key state */
168         if (!il->keydown[event->code] && event->value) {
169             il->keydown[event->code] = true;
170             il->keycount++;
171         }
172         if (il->keydown[event->code] && !event->value) {
173             il->keydown[event->code] = false;
174             il->keycount--;
175         }
176 
177         /* send event to guest when grab is active */
178         if (il->grab_active && !input_linux_should_skip(il, event)) {
179             int qcode = qemu_input_linux_to_qcode(event->code);
180             qemu_input_event_send_key_qcode(NULL, qcode, event->value);
181         }
182 
183         /* hotkey -> record switch request ... */
184         if (input_linux_check_toggle(il)) {
185             il->grab_request = true;
186         }
187 
188         /*
189          * ... and do the switch when all keys are lifted, so we
190          * confuse neither guest nor host with keys which seem to
191          * be stuck due to missing key-up events.
192          */
193         if (il->grab_request && !il->keycount) {
194             il->grab_request = false;
195             input_linux_toggle_grab(il);
196         }
197     }
198 }
199 
200 static void input_linux_event_mouse_button(int button)
201 {
202     qemu_input_queue_btn(NULL, button, true);
203     qemu_input_event_sync();
204     qemu_input_queue_btn(NULL, button, false);
205     qemu_input_event_sync();
206 }
207 
208 static void input_linux_handle_mouse(InputLinux *il, struct input_event *event)
209 {
210     if (!il->grab_active) {
211         return;
212     }
213 
214     switch (event->type) {
215     case EV_KEY:
216         switch (event->code) {
217         case BTN_LEFT:
218             qemu_input_queue_btn(NULL, INPUT_BUTTON_LEFT, event->value);
219             break;
220         case BTN_RIGHT:
221             qemu_input_queue_btn(NULL, INPUT_BUTTON_RIGHT, event->value);
222             break;
223         case BTN_MIDDLE:
224             qemu_input_queue_btn(NULL, INPUT_BUTTON_MIDDLE, event->value);
225             break;
226         case BTN_GEAR_UP:
227             qemu_input_queue_btn(NULL, INPUT_BUTTON_WHEEL_UP, event->value);
228             break;
229         case BTN_GEAR_DOWN:
230             qemu_input_queue_btn(NULL, INPUT_BUTTON_WHEEL_DOWN,
231                                  event->value);
232             break;
233         case BTN_SIDE:
234             qemu_input_queue_btn(NULL, INPUT_BUTTON_SIDE, event->value);
235             break;
236         case BTN_EXTRA:
237             qemu_input_queue_btn(NULL, INPUT_BUTTON_EXTRA, event->value);
238             break;
239         };
240         break;
241     case EV_REL:
242         switch (event->code) {
243         case REL_X:
244             qemu_input_queue_rel(NULL, INPUT_AXIS_X, event->value);
245             break;
246         case REL_Y:
247             qemu_input_queue_rel(NULL, INPUT_AXIS_Y, event->value);
248             break;
249         case REL_WHEEL:
250             il->wheel = event->value;
251             break;
252         }
253         break;
254     case EV_ABS:
255         switch (event->code) {
256         case ABS_X:
257             qemu_input_queue_abs(NULL, INPUT_AXIS_X, event->value,
258                                  il->abs_x_min, il->abs_x_max);
259             break;
260         case ABS_Y:
261             qemu_input_queue_abs(NULL, INPUT_AXIS_Y, event->value,
262                                  il->abs_y_min, il->abs_y_max);
263             break;
264         }
265         break;
266     case EV_SYN:
267         qemu_input_event_sync();
268         if (il->wheel != 0) {
269             input_linux_event_mouse_button((il->wheel > 0)
270                                            ? INPUT_BUTTON_WHEEL_UP
271                                            : INPUT_BUTTON_WHEEL_DOWN);
272             il->wheel = 0;
273         }
274         break;
275     }
276 }
277 
278 static void input_linux_event(void *opaque)
279 {
280     InputLinux *il = opaque;
281     int rc;
282     int read_size;
283     uint8_t *p = (uint8_t *)&il->event;
284 
285     for (;;) {
286         read_size = sizeof(il->event) - il->read_offset;
287         rc = read(il->fd, &p[il->read_offset], read_size);
288         if (rc != read_size) {
289             if (rc < 0 && errno != EAGAIN) {
290                 fprintf(stderr, "%s: read: %s\n", __func__, strerror(errno));
291                 qemu_set_fd_handler(il->fd, NULL, NULL, NULL);
292                 close(il->fd);
293             } else if (rc > 0) {
294                 il->read_offset += rc;
295             }
296             break;
297         }
298         il->read_offset = 0;
299 
300         if (il->num_keys) {
301             input_linux_handle_keyboard(il, &il->event);
302         }
303         if ((il->has_rel_x || il->has_abs_x) && il->num_btns) {
304             input_linux_handle_mouse(il, &il->event);
305         }
306     }
307 }
308 
309 static void input_linux_complete(UserCreatable *uc, Error **errp)
310 {
311     InputLinux *il = INPUT_LINUX(uc);
312     uint8_t evtmap, relmap, absmap;
313     uint8_t keymap[KEY_CNT / 8], keystate[KEY_CNT / 8];
314     unsigned int i;
315     int rc, ver;
316     struct input_absinfo absinfo;
317 
318     if (!il->evdev) {
319         error_setg(errp, "no input device specified");
320         return;
321     }
322 
323     il->fd = open(il->evdev, O_RDWR);
324     if (il->fd < 0)  {
325         error_setg_file_open(errp, errno, il->evdev);
326         return;
327     }
328     qemu_set_nonblock(il->fd);
329 
330     rc = ioctl(il->fd, EVIOCGVERSION, &ver);
331     if (rc < 0) {
332         error_setg(errp, "%s: is not an evdev device", il->evdev);
333         goto err_close;
334     }
335 
336     rc = ioctl(il->fd, EVIOCGBIT(0, sizeof(evtmap)), &evtmap);
337     if (rc < 0) {
338         goto err_read_event_bits;
339     }
340 
341     if (evtmap & (1 << EV_REL)) {
342         relmap = 0;
343         rc = ioctl(il->fd, EVIOCGBIT(EV_REL, sizeof(relmap)), &relmap);
344         if (rc < 0) {
345             goto err_read_event_bits;
346         }
347         if (relmap & (1 << REL_X)) {
348             il->has_rel_x = true;
349         }
350     }
351 
352     if (evtmap & (1 << EV_ABS)) {
353         absmap = 0;
354         rc = ioctl(il->fd, EVIOCGBIT(EV_ABS, sizeof(absmap)), &absmap);
355         if (rc < 0) {
356             goto err_read_event_bits;
357         }
358         if (absmap & (1 << ABS_X)) {
359             il->has_abs_x = true;
360             rc = ioctl(il->fd, EVIOCGABS(ABS_X), &absinfo);
361             if (rc < 0) {
362                 error_setg(errp, "%s: failed to get get absolute X value",
363                            il->evdev);
364                 goto err_close;
365             }
366             il->abs_x_min = absinfo.minimum;
367             il->abs_x_max = absinfo.maximum;
368             rc = ioctl(il->fd, EVIOCGABS(ABS_Y), &absinfo);
369             if (rc < 0) {
370                 error_setg(errp, "%s: failed to get get absolute Y value",
371                            il->evdev);
372                 goto err_close;
373             }
374             il->abs_y_min = absinfo.minimum;
375             il->abs_y_max = absinfo.maximum;
376         }
377     }
378 
379     if (evtmap & (1 << EV_KEY)) {
380         memset(keymap, 0, sizeof(keymap));
381         rc = ioctl(il->fd, EVIOCGBIT(EV_KEY, sizeof(keymap)), keymap);
382         if (rc < 0) {
383             goto err_read_event_bits;
384         }
385         rc = ioctl(il->fd, EVIOCGKEY(sizeof(keystate)), keystate);
386         if (rc < 0) {
387             error_setg(errp, "%s: failed to get global key state", il->evdev);
388             goto err_close;
389         }
390         for (i = 0; i < KEY_CNT; i++) {
391             if (keymap[i / 8] & (1 << (i % 8))) {
392                 if (linux_is_button(i)) {
393                     il->num_btns++;
394                 } else {
395                     il->num_keys++;
396                 }
397                 if (keystate[i / 8] & (1 << (i % 8))) {
398                     il->keydown[i] = true;
399                     il->keycount++;
400                 }
401             }
402         }
403     }
404 
405     qemu_set_fd_handler(il->fd, input_linux_event, NULL, il);
406     if (il->keycount) {
407         /* delay grab until all keys are released */
408         il->grab_request = true;
409     } else {
410         input_linux_toggle_grab(il);
411     }
412     QTAILQ_INSERT_TAIL(&inputs, il, next);
413     il->initialized = true;
414     return;
415 
416 err_read_event_bits:
417     error_setg(errp, "%s: failed to read event bits", il->evdev);
418 
419 err_close:
420     close(il->fd);
421     return;
422 }
423 
424 static void input_linux_instance_finalize(Object *obj)
425 {
426     InputLinux *il = INPUT_LINUX(obj);
427 
428     if (il->initialized) {
429         QTAILQ_REMOVE(&inputs, il, next);
430         close(il->fd);
431     }
432     g_free(il->evdev);
433 }
434 
435 static char *input_linux_get_evdev(Object *obj, Error **errp)
436 {
437     InputLinux *il = INPUT_LINUX(obj);
438 
439     return g_strdup(il->evdev);
440 }
441 
442 static void input_linux_set_evdev(Object *obj, const char *value,
443                                   Error **errp)
444 {
445     InputLinux *il = INPUT_LINUX(obj);
446 
447     if (il->evdev) {
448         error_setg(errp, "evdev property already set");
449         return;
450     }
451     il->evdev = g_strdup(value);
452 }
453 
454 static bool input_linux_get_grab_all(Object *obj, Error **errp)
455 {
456     InputLinux *il = INPUT_LINUX(obj);
457 
458     return il->grab_all;
459 }
460 
461 static void input_linux_set_grab_all(Object *obj, bool value,
462                                    Error **errp)
463 {
464     InputLinux *il = INPUT_LINUX(obj);
465 
466     il->grab_all = value;
467 }
468 
469 static bool input_linux_get_repeat(Object *obj, Error **errp)
470 {
471     InputLinux *il = INPUT_LINUX(obj);
472 
473     return il->repeat;
474 }
475 
476 static void input_linux_set_repeat(Object *obj, bool value,
477                                    Error **errp)
478 {
479     InputLinux *il = INPUT_LINUX(obj);
480 
481     il->repeat = value;
482 }
483 
484 static int input_linux_get_grab_toggle(Object *obj, Error **errp)
485 {
486     InputLinux *il = INPUT_LINUX(obj);
487 
488     return il->grab_toggle;
489 }
490 
491 static void input_linux_set_grab_toggle(Object *obj, int value,
492                                        Error **errp)
493 {
494     InputLinux *il = INPUT_LINUX(obj);
495 
496     il->grab_toggle = value;
497 }
498 
499 static void input_linux_instance_init(Object *obj)
500 {
501     object_property_add_str(obj, "evdev",
502                             input_linux_get_evdev,
503                             input_linux_set_evdev);
504     object_property_add_bool(obj, "grab_all",
505                              input_linux_get_grab_all,
506                              input_linux_set_grab_all);
507     object_property_add_bool(obj, "repeat",
508                              input_linux_get_repeat,
509                              input_linux_set_repeat);
510     object_property_add_enum(obj, "grab-toggle", "GrabToggleKeys",
511                              &GrabToggleKeys_lookup,
512                              input_linux_get_grab_toggle,
513                              input_linux_set_grab_toggle);
514 }
515 
516 static void input_linux_class_init(ObjectClass *oc, void *data)
517 {
518     UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
519 
520     ucc->complete = input_linux_complete;
521 }
522 
523 static const TypeInfo input_linux_info = {
524     .name = TYPE_INPUT_LINUX,
525     .parent = TYPE_OBJECT,
526     .class_size = sizeof(InputLinuxClass),
527     .class_init = input_linux_class_init,
528     .instance_size = sizeof(InputLinux),
529     .instance_init = input_linux_instance_init,
530     .instance_finalize = input_linux_instance_finalize,
531     .interfaces = (InterfaceInfo[]) {
532         { TYPE_USER_CREATABLE },
533         { }
534     }
535 };
536 
537 static void register_types(void)
538 {
539     type_register_static(&input_linux_info);
540 }
541 
542 type_init(register_types);
543