]> Git Repo - qemu.git/blob - ui/input-linux.c
Include qemu/main-loop.h less
[qemu.git] / ui / input-linux.c
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 "sysemu/sysemu.h"
14 #include "ui/input.h"
15 #include "qom/object_interfaces.h"
16 #include "sysemu/iothread.h"
17 #include "block/aio.h"
18
19 #include <sys/ioctl.h>
20 #include "standard-headers/linux/input.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 #define INPUT_LINUX(obj) \
35     OBJECT_CHECK(InputLinux, (obj), TYPE_INPUT_LINUX)
36 #define INPUT_LINUX_GET_CLASS(obj) \
37     OBJECT_GET_CLASS(InputLinuxClass, (obj), TYPE_INPUT_LINUX)
38 #define INPUT_LINUX_CLASS(klass) \
39     OBJECT_CLASS_CHECK(InputLinuxClass, (klass), TYPE_INPUT_LINUX)
40
41 typedef struct InputLinux InputLinux;
42 typedef struct InputLinuxClass InputLinuxClass;
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_META_META:
118         return il->keydown[KEY_LEFTMETA] &&
119             il->keydown[KEY_RIGHTMETA];
120
121     case GRAB_TOGGLE_KEYS_SCROLLLOCK:
122         return il->keydown[KEY_SCROLLLOCK];
123
124     case GRAB_TOGGLE_KEYS_CTRL_SCROLLLOCK:
125         return (il->keydown[KEY_LEFTCTRL] ||
126                 il->keydown[KEY_RIGHTCTRL]) &&
127             il->keydown[KEY_SCROLLLOCK];
128
129     case GRAB_TOGGLE_KEYS__MAX:
130         /* avoid gcc error */
131         break;
132     }
133     return false;
134 }
135
136 static bool input_linux_should_skip(InputLinux *il,
137                                     struct input_event *event)
138 {
139     return (il->grab_toggle == GRAB_TOGGLE_KEYS_SCROLLLOCK ||
140             il->grab_toggle == GRAB_TOGGLE_KEYS_CTRL_SCROLLLOCK) &&
141             event->code == KEY_SCROLLLOCK;
142 }
143
144 static void input_linux_handle_keyboard(InputLinux *il,
145                                         struct input_event *event)
146 {
147     if (event->type == EV_KEY) {
148         if (event->value > 2 || (event->value > 1 && !il->repeat)) {
149             /*
150              * ignore autorepeat + unknown key events
151              * 0 == up, 1 == down, 2 == autorepeat, other == undefined
152              */
153             return;
154         }
155         if (event->code >= KEY_CNT) {
156             /*
157              * Should not happen.  But better safe than sorry,
158              * and we make Coverity happy too.
159              */
160             return;
161         }
162
163         /* keep track of key state */
164         if (!il->keydown[event->code] && event->value) {
165             il->keydown[event->code] = true;
166             il->keycount++;
167         }
168         if (il->keydown[event->code] && !event->value) {
169             il->keydown[event->code] = false;
170             il->keycount--;
171         }
172
173         /* send event to guest when grab is active */
174         if (il->grab_active && !input_linux_should_skip(il, event)) {
175             int qcode = qemu_input_linux_to_qcode(event->code);
176             qemu_input_event_send_key_qcode(NULL, qcode, event->value);
177         }
178
179         /* hotkey -> record switch request ... */
180         if (input_linux_check_toggle(il)) {
181             il->grab_request = true;
182         }
183
184         /*
185          * ... and do the switch when all keys are lifted, so we
186          * confuse neither guest nor host with keys which seem to
187          * be stuck due to missing key-up events.
188          */
189         if (il->grab_request && !il->keycount) {
190             il->grab_request = false;
191             input_linux_toggle_grab(il);
192         }
193     }
194 }
195
196 static void input_linux_event_mouse_button(int button)
197 {
198     qemu_input_queue_btn(NULL, button, true);
199     qemu_input_event_sync();
200     qemu_input_queue_btn(NULL, button, false);
201     qemu_input_event_sync();
202 }
203
204 static void input_linux_handle_mouse(InputLinux *il, struct input_event *event)
205 {
206     if (!il->grab_active) {
207         return;
208     }
209
210     switch (event->type) {
211     case EV_KEY:
212         switch (event->code) {
213         case BTN_LEFT:
214             qemu_input_queue_btn(NULL, INPUT_BUTTON_LEFT, event->value);
215             break;
216         case BTN_RIGHT:
217             qemu_input_queue_btn(NULL, INPUT_BUTTON_RIGHT, event->value);
218             break;
219         case BTN_MIDDLE:
220             qemu_input_queue_btn(NULL, INPUT_BUTTON_MIDDLE, event->value);
221             break;
222         case BTN_GEAR_UP:
223             qemu_input_queue_btn(NULL, INPUT_BUTTON_WHEEL_UP, event->value);
224             break;
225         case BTN_GEAR_DOWN:
226             qemu_input_queue_btn(NULL, INPUT_BUTTON_WHEEL_DOWN,
227                                  event->value);
228             break;
229         case BTN_SIDE:
230             qemu_input_queue_btn(NULL, INPUT_BUTTON_SIDE, event->value);
231             break;
232         case BTN_EXTRA:
233             qemu_input_queue_btn(NULL, INPUT_BUTTON_EXTRA, event->value);
234             break;
235         };
236         break;
237     case EV_REL:
238         switch (event->code) {
239         case REL_X:
240             qemu_input_queue_rel(NULL, INPUT_AXIS_X, event->value);
241             break;
242         case REL_Y:
243             qemu_input_queue_rel(NULL, INPUT_AXIS_Y, event->value);
244             break;
245         case REL_WHEEL:
246             il->wheel = event->value;
247             break;
248         }
249         break;
250     case EV_ABS:
251         switch (event->code) {
252         case ABS_X:
253             qemu_input_queue_abs(NULL, INPUT_AXIS_X, event->value,
254                                  il->abs_x_min, il->abs_x_max);
255             break;
256         case ABS_Y:
257             qemu_input_queue_abs(NULL, INPUT_AXIS_Y, event->value,
258                                  il->abs_y_min, il->abs_y_max);
259             break;
260         }
261         break;
262     case EV_SYN:
263         qemu_input_event_sync();
264         if (il->wheel != 0) {
265             input_linux_event_mouse_button((il->wheel > 0)
266                                            ? INPUT_BUTTON_WHEEL_UP
267                                            : INPUT_BUTTON_WHEEL_DOWN);
268             il->wheel = 0;
269         }
270         break;
271     }
272 }
273
274 static void input_linux_event(void *opaque)
275 {
276     InputLinux *il = opaque;
277     int rc;
278     int read_size;
279     uint8_t *p = (uint8_t *)&il->event;
280
281     for (;;) {
282         read_size = sizeof(il->event) - il->read_offset;
283         rc = read(il->fd, &p[il->read_offset], read_size);
284         if (rc != read_size) {
285             if (rc < 0 && errno != EAGAIN) {
286                 fprintf(stderr, "%s: read: %s\n", __func__, strerror(errno));
287                 qemu_set_fd_handler(il->fd, NULL, NULL, NULL);
288                 close(il->fd);
289             } else if (rc > 0) {
290                 il->read_offset += rc;
291             }
292             break;
293         }
294         il->read_offset = 0;
295
296         if (il->num_keys) {
297             input_linux_handle_keyboard(il, &il->event);
298         }
299         if ((il->has_rel_x || il->has_abs_x) && il->num_btns) {
300             input_linux_handle_mouse(il, &il->event);
301         }
302     }
303 }
304
305 static void input_linux_complete(UserCreatable *uc, Error **errp)
306 {
307     InputLinux *il = INPUT_LINUX(uc);
308     uint8_t evtmap, relmap, absmap;
309     uint8_t keymap[KEY_CNT / 8], keystate[KEY_CNT / 8];
310     unsigned int i;
311     int rc, ver;
312     struct input_absinfo absinfo;
313
314     if (!il->evdev) {
315         error_setg(errp, "no input device specified");
316         return;
317     }
318
319     il->fd = open(il->evdev, O_RDWR);
320     if (il->fd < 0)  {
321         error_setg_file_open(errp, errno, il->evdev);
322         return;
323     }
324     qemu_set_nonblock(il->fd);
325
326     rc = ioctl(il->fd, EVIOCGVERSION, &ver);
327     if (rc < 0) {
328         error_setg(errp, "%s: is not an evdev device", il->evdev);
329         goto err_close;
330     }
331
332     rc = ioctl(il->fd, EVIOCGBIT(0, sizeof(evtmap)), &evtmap);
333     if (rc < 0) {
334         error_setg(errp, "%s: failed to read event bits", il->evdev);
335         goto err_close;
336     }
337
338     if (evtmap & (1 << EV_REL)) {
339         relmap = 0;
340         rc = ioctl(il->fd, EVIOCGBIT(EV_REL, sizeof(relmap)), &relmap);
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 (absmap & (1 << ABS_X)) {
350             il->has_abs_x = true;
351             rc = ioctl(il->fd, EVIOCGABS(ABS_X), &absinfo);
352             il->abs_x_min = absinfo.minimum;
353             il->abs_x_max = absinfo.maximum;
354             rc = ioctl(il->fd, EVIOCGABS(ABS_Y), &absinfo);
355             il->abs_y_min = absinfo.minimum;
356             il->abs_y_max = absinfo.maximum;
357         }
358     }
359
360     if (evtmap & (1 << EV_KEY)) {
361         memset(keymap, 0, sizeof(keymap));
362         rc = ioctl(il->fd, EVIOCGBIT(EV_KEY, sizeof(keymap)), keymap);
363         rc = ioctl(il->fd, EVIOCGKEY(sizeof(keystate)), keystate);
364         for (i = 0; i < KEY_CNT; i++) {
365             if (keymap[i / 8] & (1 << (i % 8))) {
366                 if (linux_is_button(i)) {
367                     il->num_btns++;
368                 } else {
369                     il->num_keys++;
370                 }
371                 if (keystate[i / 8] & (1 << (i % 8))) {
372                     il->keydown[i] = true;
373                     il->keycount++;
374                 }
375             }
376         }
377     }
378
379     qemu_set_fd_handler(il->fd, input_linux_event, NULL, il);
380     if (il->keycount) {
381         /* delay grab until all keys are released */
382         il->grab_request = true;
383     } else {
384         input_linux_toggle_grab(il);
385     }
386     QTAILQ_INSERT_TAIL(&inputs, il, next);
387     il->initialized = true;
388     return;
389
390 err_close:
391     close(il->fd);
392     return;
393 }
394
395 static void input_linux_instance_finalize(Object *obj)
396 {
397     InputLinux *il = INPUT_LINUX(obj);
398
399     if (il->initialized) {
400         QTAILQ_REMOVE(&inputs, il, next);
401         close(il->fd);
402     }
403     g_free(il->evdev);
404 }
405
406 static char *input_linux_get_evdev(Object *obj, Error **errp)
407 {
408     InputLinux *il = INPUT_LINUX(obj);
409
410     return g_strdup(il->evdev);
411 }
412
413 static void input_linux_set_evdev(Object *obj, const char *value,
414                                   Error **errp)
415 {
416     InputLinux *il = INPUT_LINUX(obj);
417
418     if (il->evdev) {
419         error_setg(errp, "evdev property already set");
420         return;
421     }
422     il->evdev = g_strdup(value);
423 }
424
425 static bool input_linux_get_grab_all(Object *obj, Error **errp)
426 {
427     InputLinux *il = INPUT_LINUX(obj);
428
429     return il->grab_all;
430 }
431
432 static void input_linux_set_grab_all(Object *obj, bool value,
433                                    Error **errp)
434 {
435     InputLinux *il = INPUT_LINUX(obj);
436
437     il->grab_all = value;
438 }
439
440 static bool input_linux_get_repeat(Object *obj, Error **errp)
441 {
442     InputLinux *il = INPUT_LINUX(obj);
443
444     return il->repeat;
445 }
446
447 static void input_linux_set_repeat(Object *obj, bool value,
448                                    Error **errp)
449 {
450     InputLinux *il = INPUT_LINUX(obj);
451
452     il->repeat = value;
453 }
454
455 static int input_linux_get_grab_toggle(Object *obj, Error **errp)
456 {
457     InputLinux *il = INPUT_LINUX(obj);
458
459     return il->grab_toggle;
460 }
461
462 static void input_linux_set_grab_toggle(Object *obj, int value,
463                                        Error **errp)
464 {
465     InputLinux *il = INPUT_LINUX(obj);
466
467     il->grab_toggle = value;
468 }
469
470 static void input_linux_instance_init(Object *obj)
471 {
472     object_property_add_str(obj, "evdev",
473                             input_linux_get_evdev,
474                             input_linux_set_evdev, NULL);
475     object_property_add_bool(obj, "grab_all",
476                              input_linux_get_grab_all,
477                              input_linux_set_grab_all, NULL);
478     object_property_add_bool(obj, "repeat",
479                              input_linux_get_repeat,
480                              input_linux_set_repeat, NULL);
481     object_property_add_enum(obj, "grab-toggle", "GrabToggleKeys",
482                              &GrabToggleKeys_lookup,
483                              input_linux_get_grab_toggle,
484                              input_linux_set_grab_toggle, NULL);
485 }
486
487 static void input_linux_class_init(ObjectClass *oc, void *data)
488 {
489     UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
490
491     ucc->complete = input_linux_complete;
492 }
493
494 static const TypeInfo input_linux_info = {
495     .name = TYPE_INPUT_LINUX,
496     .parent = TYPE_OBJECT,
497     .class_size = sizeof(InputLinuxClass),
498     .class_init = input_linux_class_init,
499     .instance_size = sizeof(InputLinux),
500     .instance_init = input_linux_instance_init,
501     .instance_finalize = input_linux_instance_finalize,
502     .interfaces = (InterfaceInfo[]) {
503         { TYPE_USER_CREATABLE },
504         { }
505     }
506 };
507
508 static void register_types(void)
509 {
510     type_register_static(&input_linux_info);
511 }
512
513 type_init(register_types);
This page took 0.053648 seconds and 4 git commands to generate.