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