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