xref: /openbmc/qemu/ui/input-linux.c (revision 0c0c1fd9)
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-common.h"
10 #include "qemu/config-file.h"
11 #include "qemu/sockets.h"
12 #include "sysemu/sysemu.h"
13 #include "ui/input.h"
14 #include "qom/object_interfaces.h"
15 
16 #include <sys/ioctl.h>
17 #include "standard-headers/linux/input.h"
18 
19 static int linux_to_qcode[KEY_CNT] = {
20     [KEY_ESC]            = Q_KEY_CODE_ESC,
21     [KEY_1]              = Q_KEY_CODE_1,
22     [KEY_2]              = Q_KEY_CODE_2,
23     [KEY_3]              = Q_KEY_CODE_3,
24     [KEY_4]              = Q_KEY_CODE_4,
25     [KEY_5]              = Q_KEY_CODE_5,
26     [KEY_6]              = Q_KEY_CODE_6,
27     [KEY_7]              = Q_KEY_CODE_7,
28     [KEY_8]              = Q_KEY_CODE_8,
29     [KEY_9]              = Q_KEY_CODE_9,
30     [KEY_0]              = Q_KEY_CODE_0,
31     [KEY_MINUS]          = Q_KEY_CODE_MINUS,
32     [KEY_EQUAL]          = Q_KEY_CODE_EQUAL,
33     [KEY_BACKSPACE]      = Q_KEY_CODE_BACKSPACE,
34     [KEY_TAB]            = Q_KEY_CODE_TAB,
35     [KEY_Q]              = Q_KEY_CODE_Q,
36     [KEY_W]              = Q_KEY_CODE_W,
37     [KEY_E]              = Q_KEY_CODE_E,
38     [KEY_R]              = Q_KEY_CODE_R,
39     [KEY_T]              = Q_KEY_CODE_T,
40     [KEY_Y]              = Q_KEY_CODE_Y,
41     [KEY_U]              = Q_KEY_CODE_U,
42     [KEY_I]              = Q_KEY_CODE_I,
43     [KEY_O]              = Q_KEY_CODE_O,
44     [KEY_P]              = Q_KEY_CODE_P,
45     [KEY_LEFTBRACE]      = Q_KEY_CODE_BRACKET_LEFT,
46     [KEY_RIGHTBRACE]     = Q_KEY_CODE_BRACKET_RIGHT,
47     [KEY_ENTER]          = Q_KEY_CODE_RET,
48     [KEY_LEFTCTRL]       = Q_KEY_CODE_CTRL,
49     [KEY_A]              = Q_KEY_CODE_A,
50     [KEY_S]              = Q_KEY_CODE_S,
51     [KEY_D]              = Q_KEY_CODE_D,
52     [KEY_F]              = Q_KEY_CODE_F,
53     [KEY_G]              = Q_KEY_CODE_G,
54     [KEY_H]              = Q_KEY_CODE_H,
55     [KEY_J]              = Q_KEY_CODE_J,
56     [KEY_K]              = Q_KEY_CODE_K,
57     [KEY_L]              = Q_KEY_CODE_L,
58     [KEY_SEMICOLON]      = Q_KEY_CODE_SEMICOLON,
59     [KEY_APOSTROPHE]     = Q_KEY_CODE_APOSTROPHE,
60     [KEY_GRAVE]          = Q_KEY_CODE_GRAVE_ACCENT,
61     [KEY_LEFTSHIFT]      = Q_KEY_CODE_SHIFT,
62     [KEY_BACKSLASH]      = Q_KEY_CODE_BACKSLASH,
63     [KEY_102ND]          = Q_KEY_CODE_LESS,
64     [KEY_Z]              = Q_KEY_CODE_Z,
65     [KEY_X]              = Q_KEY_CODE_X,
66     [KEY_C]              = Q_KEY_CODE_C,
67     [KEY_V]              = Q_KEY_CODE_V,
68     [KEY_B]              = Q_KEY_CODE_B,
69     [KEY_N]              = Q_KEY_CODE_N,
70     [KEY_M]              = Q_KEY_CODE_M,
71     [KEY_COMMA]          = Q_KEY_CODE_COMMA,
72     [KEY_DOT]            = Q_KEY_CODE_DOT,
73     [KEY_SLASH]          = Q_KEY_CODE_SLASH,
74     [KEY_RIGHTSHIFT]     = Q_KEY_CODE_SHIFT_R,
75     [KEY_LEFTALT]        = Q_KEY_CODE_ALT,
76     [KEY_SPACE]          = Q_KEY_CODE_SPC,
77     [KEY_CAPSLOCK]       = Q_KEY_CODE_CAPS_LOCK,
78     [KEY_F1]             = Q_KEY_CODE_F1,
79     [KEY_F2]             = Q_KEY_CODE_F2,
80     [KEY_F3]             = Q_KEY_CODE_F3,
81     [KEY_F4]             = Q_KEY_CODE_F4,
82     [KEY_F5]             = Q_KEY_CODE_F5,
83     [KEY_F6]             = Q_KEY_CODE_F6,
84     [KEY_F7]             = Q_KEY_CODE_F7,
85     [KEY_F8]             = Q_KEY_CODE_F8,
86     [KEY_F9]             = Q_KEY_CODE_F9,
87     [KEY_F10]            = Q_KEY_CODE_F10,
88     [KEY_NUMLOCK]        = Q_KEY_CODE_NUM_LOCK,
89     [KEY_SCROLLLOCK]     = Q_KEY_CODE_SCROLL_LOCK,
90     [KEY_KP0]            = Q_KEY_CODE_KP_0,
91     [KEY_KP1]            = Q_KEY_CODE_KP_1,
92     [KEY_KP2]            = Q_KEY_CODE_KP_2,
93     [KEY_KP3]            = Q_KEY_CODE_KP_3,
94     [KEY_KP4]            = Q_KEY_CODE_KP_4,
95     [KEY_KP5]            = Q_KEY_CODE_KP_5,
96     [KEY_KP6]            = Q_KEY_CODE_KP_6,
97     [KEY_KP7]            = Q_KEY_CODE_KP_7,
98     [KEY_KP8]            = Q_KEY_CODE_KP_8,
99     [KEY_KP9]            = Q_KEY_CODE_KP_9,
100     [KEY_KPMINUS]        = Q_KEY_CODE_KP_SUBTRACT,
101     [KEY_KPPLUS]         = Q_KEY_CODE_KP_ADD,
102     [KEY_KPDOT]          = Q_KEY_CODE_KP_DECIMAL,
103     [KEY_KPENTER]        = Q_KEY_CODE_KP_ENTER,
104     [KEY_KPSLASH]        = Q_KEY_CODE_KP_DIVIDE,
105     [KEY_KPASTERISK]     = Q_KEY_CODE_KP_MULTIPLY,
106     [KEY_F11]            = Q_KEY_CODE_F11,
107     [KEY_F12]            = Q_KEY_CODE_F12,
108     [KEY_RIGHTCTRL]      = Q_KEY_CODE_CTRL_R,
109     [KEY_SYSRQ]          = Q_KEY_CODE_SYSRQ,
110     [KEY_RIGHTALT]       = Q_KEY_CODE_ALT_R,
111     [KEY_HOME]           = Q_KEY_CODE_HOME,
112     [KEY_UP]             = Q_KEY_CODE_UP,
113     [KEY_PAGEUP]         = Q_KEY_CODE_PGUP,
114     [KEY_LEFT]           = Q_KEY_CODE_LEFT,
115     [KEY_RIGHT]          = Q_KEY_CODE_RIGHT,
116     [KEY_END]            = Q_KEY_CODE_END,
117     [KEY_DOWN]           = Q_KEY_CODE_DOWN,
118     [KEY_PAGEDOWN]       = Q_KEY_CODE_PGDN,
119     [KEY_INSERT]         = Q_KEY_CODE_INSERT,
120     [KEY_DELETE]         = Q_KEY_CODE_DELETE,
121     [KEY_LEFTMETA]       = Q_KEY_CODE_META_L,
122     [KEY_RIGHTMETA]      = Q_KEY_CODE_META_R,
123     [KEY_MENU]           = Q_KEY_CODE_MENU,
124 };
125 
126 static int qemu_input_linux_to_qcode(unsigned int lnx)
127 {
128     assert(lnx < KEY_CNT);
129     return linux_to_qcode[lnx];
130 }
131 
132 static bool linux_is_button(unsigned int lnx)
133 {
134     if (lnx < 0x100) {
135         return false;
136     }
137     if (lnx >= 0x160 && lnx < 0x2c0) {
138         return false;
139     }
140     return true;
141 }
142 
143 #define TYPE_INPUT_LINUX "input-linux"
144 #define INPUT_LINUX(obj) \
145     OBJECT_CHECK(InputLinux, (obj), TYPE_INPUT_LINUX)
146 #define INPUT_LINUX_GET_CLASS(obj) \
147     OBJECT_GET_CLASS(InputLinuxClass, (obj), TYPE_INPUT_LINUX)
148 #define INPUT_LINUX_CLASS(klass) \
149     OBJECT_CLASS_CHECK(InputLinuxClass, (klass), TYPE_INPUT_LINUX)
150 
151 typedef struct InputLinux InputLinux;
152 typedef struct InputLinuxClass InputLinuxClass;
153 
154 struct InputLinux {
155     Object parent;
156 
157     char        *evdev;
158     int         fd;
159     bool        repeat;
160     bool        grab_request;
161     bool        grab_active;
162     bool        grab_all;
163     bool        keydown[KEY_CNT];
164     int         keycount;
165     int         wheel;
166     bool        initialized;
167 
168     bool        has_rel_x;
169     bool        has_abs_x;
170     int         num_keys;
171     int         num_btns;
172 
173     QTAILQ_ENTRY(InputLinux) next;
174 };
175 
176 struct InputLinuxClass {
177     ObjectClass parent_class;
178 };
179 
180 static QTAILQ_HEAD(, InputLinux) inputs = QTAILQ_HEAD_INITIALIZER(inputs);
181 
182 static void input_linux_toggle_grab(InputLinux *il)
183 {
184     intptr_t request = !il->grab_active;
185     InputLinux *item;
186     int rc;
187 
188     rc = ioctl(il->fd, EVIOCGRAB, request);
189     if (rc < 0) {
190         return;
191     }
192     il->grab_active = !il->grab_active;
193 
194     if (!il->grab_all) {
195         return;
196     }
197     QTAILQ_FOREACH(item, &inputs, next) {
198         if (item == il || item->grab_all) {
199             /* avoid endless loops */
200             continue;
201         }
202         if (item->grab_active != il->grab_active) {
203             input_linux_toggle_grab(item);
204         }
205     }
206 }
207 
208 static void input_linux_handle_keyboard(InputLinux *il,
209                                         struct input_event *event)
210 {
211     if (event->type == EV_KEY) {
212         if (event->value > 2 || (event->value > 1 && !il->repeat)) {
213             /*
214              * ignore autorepeat + unknown key events
215              * 0 == up, 1 == down, 2 == autorepeat, other == undefined
216              */
217             return;
218         }
219         if (event->code >= KEY_CNT) {
220             /*
221              * Should not happen.  But better safe than sorry,
222              * and we make Coverity happy too.
223              */
224             return;
225         }
226 
227         /* keep track of key state */
228         if (!il->keydown[event->code] && event->value) {
229             il->keydown[event->code] = true;
230             il->keycount++;
231         }
232         if (il->keydown[event->code] && !event->value) {
233             il->keydown[event->code] = false;
234             il->keycount--;
235         }
236 
237         /* send event to guest when grab is active */
238         if (il->grab_active) {
239             int qcode = qemu_input_linux_to_qcode(event->code);
240             qemu_input_event_send_key_qcode(NULL, qcode, event->value);
241         }
242 
243         /* hotkey -> record switch request ... */
244         if (il->keydown[KEY_LEFTCTRL] &&
245             il->keydown[KEY_RIGHTCTRL]) {
246             il->grab_request = true;
247         }
248 
249         /*
250          * ... and do the switch when all keys are lifted, so we
251          * confuse neither guest nor host with keys which seem to
252          * be stuck due to missing key-up events.
253          */
254         if (il->grab_request && !il->keycount) {
255             il->grab_request = false;
256             input_linux_toggle_grab(il);
257         }
258     }
259 }
260 
261 static void input_linux_event_mouse_button(int button)
262 {
263     qemu_input_queue_btn(NULL, button, true);
264     qemu_input_event_sync();
265     qemu_input_queue_btn(NULL, button, false);
266     qemu_input_event_sync();
267 }
268 
269 static void input_linux_handle_mouse(InputLinux *il, struct input_event *event)
270 {
271     if (!il->grab_active) {
272         return;
273     }
274 
275     switch (event->type) {
276     case EV_KEY:
277         switch (event->code) {
278         case BTN_LEFT:
279             qemu_input_queue_btn(NULL, INPUT_BUTTON_LEFT, event->value);
280             break;
281         case BTN_RIGHT:
282             qemu_input_queue_btn(NULL, INPUT_BUTTON_RIGHT, event->value);
283             break;
284         case BTN_MIDDLE:
285             qemu_input_queue_btn(NULL, INPUT_BUTTON_MIDDLE, event->value);
286             break;
287         case BTN_GEAR_UP:
288             qemu_input_queue_btn(NULL, INPUT_BUTTON_WHEEL_UP, event->value);
289             break;
290         case BTN_GEAR_DOWN:
291             qemu_input_queue_btn(NULL, INPUT_BUTTON_WHEEL_DOWN,
292                                  event->value);
293             break;
294         };
295         break;
296     case EV_REL:
297         switch (event->code) {
298         case REL_X:
299             qemu_input_queue_rel(NULL, INPUT_AXIS_X, event->value);
300             break;
301         case REL_Y:
302             qemu_input_queue_rel(NULL, INPUT_AXIS_Y, event->value);
303             break;
304         case REL_WHEEL:
305             il->wheel = event->value;
306             break;
307         }
308         break;
309     case EV_SYN:
310         qemu_input_event_sync();
311         if (il->wheel != 0) {
312             input_linux_event_mouse_button((il->wheel > 0)
313                                            ? INPUT_BUTTON_WHEEL_UP
314                                            : INPUT_BUTTON_WHEEL_DOWN);
315             il->wheel = 0;
316         }
317         break;
318     }
319 }
320 
321 static void input_linux_event(void *opaque)
322 {
323     InputLinux *il = opaque;
324     struct input_event event;
325     int rc;
326 
327     for (;;) {
328         rc = read(il->fd, &event, sizeof(event));
329         if (rc != sizeof(event)) {
330             if (rc < 0 && errno != EAGAIN) {
331                 fprintf(stderr, "%s: read: %s\n", __func__, strerror(errno));
332                 qemu_set_fd_handler(il->fd, NULL, NULL, NULL);
333                 close(il->fd);
334             }
335             break;
336         }
337 
338         if (il->num_keys) {
339             input_linux_handle_keyboard(il, &event);
340         }
341         if (il->has_rel_x && il->num_btns) {
342             input_linux_handle_mouse(il, &event);
343         }
344     }
345 }
346 
347 static void input_linux_complete(UserCreatable *uc, Error **errp)
348 {
349     InputLinux *il = INPUT_LINUX(uc);
350     uint8_t evtmap, relmap, absmap, keymap[KEY_CNT / 8];
351     unsigned int i;
352     int rc, ver;
353 
354     if (!il->evdev) {
355         error_setg(errp, "no input device specified");
356         return;
357     }
358 
359     il->fd = open(il->evdev, O_RDWR);
360     if (il->fd < 0)  {
361         error_setg_file_open(errp, errno, il->evdev);
362         return;
363     }
364     qemu_set_nonblock(il->fd);
365 
366     rc = ioctl(il->fd, EVIOCGVERSION, &ver);
367     if (rc < 0) {
368         error_setg(errp, "%s: is not an evdev device", il->evdev);
369         goto err_close;
370     }
371 
372     rc = ioctl(il->fd, EVIOCGBIT(0, sizeof(evtmap)), &evtmap);
373     if (rc < 0) {
374         error_setg(errp, "%s: failed to read event bits", il->evdev);
375         goto err_close;
376     }
377 
378     if (evtmap & (1 << EV_REL)) {
379         relmap = 0;
380         rc = ioctl(il->fd, EVIOCGBIT(EV_REL, sizeof(relmap)), &relmap);
381         if (relmap & (1 << REL_X)) {
382             il->has_rel_x = true;
383         }
384     }
385 
386     if (evtmap & (1 << EV_ABS)) {
387         absmap = 0;
388         rc = ioctl(il->fd, EVIOCGBIT(EV_ABS, sizeof(absmap)), &absmap);
389         if (absmap & (1 << ABS_X)) {
390             il->has_abs_x = true;
391         }
392     }
393 
394     if (evtmap & (1 << EV_KEY)) {
395         memset(keymap, 0, sizeof(keymap));
396         rc = ioctl(il->fd, EVIOCGBIT(EV_KEY, sizeof(keymap)), keymap);
397         for (i = 0; i < KEY_CNT; i++) {
398             if (keymap[i / 8] & (1 << (i % 8))) {
399                 if (linux_is_button(i)) {
400                     il->num_btns++;
401                 } else {
402                     il->num_keys++;
403                 }
404             }
405         }
406     }
407 
408     qemu_set_fd_handler(il->fd, input_linux_event, NULL, il);
409     input_linux_toggle_grab(il);
410     QTAILQ_INSERT_TAIL(&inputs, il, next);
411     il->initialized = true;
412     return;
413 
414 err_close:
415     close(il->fd);
416     return;
417 }
418 
419 static void input_linux_instance_finalize(Object *obj)
420 {
421     InputLinux *il = INPUT_LINUX(obj);
422 
423     if (il->initialized) {
424         QTAILQ_REMOVE(&inputs, il, next);
425         close(il->fd);
426     }
427     g_free(il->evdev);
428 }
429 
430 static char *input_linux_get_evdev(Object *obj, Error **errp)
431 {
432     InputLinux *il = INPUT_LINUX(obj);
433 
434     return g_strdup(il->evdev);
435 }
436 
437 static void input_linux_set_evdev(Object *obj, const char *value,
438                                   Error **errp)
439 {
440     InputLinux *il = INPUT_LINUX(obj);
441 
442     if (il->evdev) {
443         error_setg(errp, "evdev property already set");
444         return;
445     }
446     il->evdev = g_strdup(value);
447 }
448 
449 static bool input_linux_get_grab_all(Object *obj, Error **errp)
450 {
451     InputLinux *il = INPUT_LINUX(obj);
452 
453     return il->grab_all;
454 }
455 
456 static void input_linux_set_grab_all(Object *obj, bool value,
457                                    Error **errp)
458 {
459     InputLinux *il = INPUT_LINUX(obj);
460 
461     il->grab_all = value;
462 }
463 
464 static bool input_linux_get_repeat(Object *obj, Error **errp)
465 {
466     InputLinux *il = INPUT_LINUX(obj);
467 
468     return il->repeat;
469 }
470 
471 static void input_linux_set_repeat(Object *obj, bool value,
472                                    Error **errp)
473 {
474     InputLinux *il = INPUT_LINUX(obj);
475 
476     il->repeat = value;
477 }
478 
479 static void input_linux_instance_init(Object *obj)
480 {
481     object_property_add_str(obj, "evdev",
482                             input_linux_get_evdev,
483                             input_linux_set_evdev, NULL);
484     object_property_add_bool(obj, "grab_all",
485                              input_linux_get_grab_all,
486                              input_linux_set_grab_all, NULL);
487     object_property_add_bool(obj, "repeat",
488                              input_linux_get_repeat,
489                              input_linux_set_repeat, NULL);
490 }
491 
492 static void input_linux_class_init(ObjectClass *oc, void *data)
493 {
494     UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
495 
496     ucc->complete = input_linux_complete;
497 }
498 
499 static const TypeInfo input_linux_info = {
500     .name = TYPE_INPUT_LINUX,
501     .parent = TYPE_OBJECT,
502     .class_size = sizeof(InputLinuxClass),
503     .class_init = input_linux_class_init,
504     .instance_size = sizeof(InputLinux),
505     .instance_init = input_linux_instance_init,
506     .instance_finalize = input_linux_instance_finalize,
507     .interfaces = (InterfaceInfo[]) {
508         { TYPE_USER_CREATABLE },
509         { }
510     }
511 };
512 
513 static void register_types(void)
514 {
515     type_register_static(&input_linux_info);
516 }
517 
518 type_init(register_types);
519