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