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 "qemu-common.h" 9 #include "qemu/config-file.h" 10 #include "qemu/sockets.h" 11 #include "sysemu/sysemu.h" 12 #include "ui/input.h" 13 14 #include <sys/ioctl.h> 15 #include "standard-headers/linux/input.h" 16 17 static int linux_to_qcode[KEY_CNT] = { 18 [KEY_ESC] = Q_KEY_CODE_ESC, 19 [KEY_1] = Q_KEY_CODE_1, 20 [KEY_2] = Q_KEY_CODE_2, 21 [KEY_3] = Q_KEY_CODE_3, 22 [KEY_4] = Q_KEY_CODE_4, 23 [KEY_5] = Q_KEY_CODE_5, 24 [KEY_6] = Q_KEY_CODE_6, 25 [KEY_7] = Q_KEY_CODE_7, 26 [KEY_8] = Q_KEY_CODE_8, 27 [KEY_9] = Q_KEY_CODE_9, 28 [KEY_0] = Q_KEY_CODE_0, 29 [KEY_MINUS] = Q_KEY_CODE_MINUS, 30 [KEY_EQUAL] = Q_KEY_CODE_EQUAL, 31 [KEY_BACKSPACE] = Q_KEY_CODE_BACKSPACE, 32 [KEY_TAB] = Q_KEY_CODE_TAB, 33 [KEY_Q] = Q_KEY_CODE_Q, 34 [KEY_W] = Q_KEY_CODE_W, 35 [KEY_E] = Q_KEY_CODE_E, 36 [KEY_R] = Q_KEY_CODE_R, 37 [KEY_T] = Q_KEY_CODE_T, 38 [KEY_Y] = Q_KEY_CODE_Y, 39 [KEY_U] = Q_KEY_CODE_U, 40 [KEY_I] = Q_KEY_CODE_I, 41 [KEY_O] = Q_KEY_CODE_O, 42 [KEY_P] = Q_KEY_CODE_P, 43 [KEY_LEFTBRACE] = Q_KEY_CODE_BRACKET_LEFT, 44 [KEY_RIGHTBRACE] = Q_KEY_CODE_BRACKET_RIGHT, 45 [KEY_ENTER] = Q_KEY_CODE_RET, 46 [KEY_LEFTCTRL] = Q_KEY_CODE_CTRL, 47 [KEY_A] = Q_KEY_CODE_A, 48 [KEY_S] = Q_KEY_CODE_S, 49 [KEY_D] = Q_KEY_CODE_D, 50 [KEY_F] = Q_KEY_CODE_F, 51 [KEY_G] = Q_KEY_CODE_G, 52 [KEY_H] = Q_KEY_CODE_H, 53 [KEY_J] = Q_KEY_CODE_J, 54 [KEY_K] = Q_KEY_CODE_K, 55 [KEY_L] = Q_KEY_CODE_L, 56 [KEY_SEMICOLON] = Q_KEY_CODE_SEMICOLON, 57 [KEY_APOSTROPHE] = Q_KEY_CODE_APOSTROPHE, 58 [KEY_GRAVE] = Q_KEY_CODE_GRAVE_ACCENT, 59 [KEY_LEFTSHIFT] = Q_KEY_CODE_SHIFT, 60 [KEY_BACKSLASH] = Q_KEY_CODE_BACKSLASH, 61 [KEY_102ND] = Q_KEY_CODE_LESS, 62 [KEY_Z] = Q_KEY_CODE_Z, 63 [KEY_X] = Q_KEY_CODE_X, 64 [KEY_C] = Q_KEY_CODE_C, 65 [KEY_V] = Q_KEY_CODE_V, 66 [KEY_B] = Q_KEY_CODE_B, 67 [KEY_N] = Q_KEY_CODE_N, 68 [KEY_M] = Q_KEY_CODE_M, 69 [KEY_COMMA] = Q_KEY_CODE_COMMA, 70 [KEY_DOT] = Q_KEY_CODE_DOT, 71 [KEY_SLASH] = Q_KEY_CODE_SLASH, 72 [KEY_RIGHTSHIFT] = Q_KEY_CODE_SHIFT_R, 73 [KEY_LEFTALT] = Q_KEY_CODE_ALT, 74 [KEY_SPACE] = Q_KEY_CODE_SPC, 75 [KEY_CAPSLOCK] = Q_KEY_CODE_CAPS_LOCK, 76 [KEY_F1] = Q_KEY_CODE_F1, 77 [KEY_F2] = Q_KEY_CODE_F2, 78 [KEY_F3] = Q_KEY_CODE_F3, 79 [KEY_F4] = Q_KEY_CODE_F4, 80 [KEY_F5] = Q_KEY_CODE_F5, 81 [KEY_F6] = Q_KEY_CODE_F6, 82 [KEY_F7] = Q_KEY_CODE_F7, 83 [KEY_F8] = Q_KEY_CODE_F8, 84 [KEY_F9] = Q_KEY_CODE_F9, 85 [KEY_F10] = Q_KEY_CODE_F10, 86 [KEY_NUMLOCK] = Q_KEY_CODE_NUM_LOCK, 87 [KEY_SCROLLLOCK] = Q_KEY_CODE_SCROLL_LOCK, 88 [KEY_KP0] = Q_KEY_CODE_KP_0, 89 [KEY_KP1] = Q_KEY_CODE_KP_1, 90 [KEY_KP2] = Q_KEY_CODE_KP_2, 91 [KEY_KP3] = Q_KEY_CODE_KP_3, 92 [KEY_KP4] = Q_KEY_CODE_KP_4, 93 [KEY_KP5] = Q_KEY_CODE_KP_5, 94 [KEY_KP6] = Q_KEY_CODE_KP_6, 95 [KEY_KP7] = Q_KEY_CODE_KP_7, 96 [KEY_KP8] = Q_KEY_CODE_KP_8, 97 [KEY_KP9] = Q_KEY_CODE_KP_9, 98 [KEY_KPMINUS] = Q_KEY_CODE_KP_SUBTRACT, 99 [KEY_KPPLUS] = Q_KEY_CODE_KP_ADD, 100 [KEY_KPDOT] = Q_KEY_CODE_KP_DECIMAL, 101 [KEY_KPENTER] = Q_KEY_CODE_KP_ENTER, 102 [KEY_KPSLASH] = Q_KEY_CODE_KP_DIVIDE, 103 [KEY_KPASTERISK] = Q_KEY_CODE_KP_MULTIPLY, 104 [KEY_F11] = Q_KEY_CODE_F11, 105 [KEY_F12] = Q_KEY_CODE_F12, 106 [KEY_RIGHTCTRL] = Q_KEY_CODE_CTRL_R, 107 [KEY_SYSRQ] = Q_KEY_CODE_SYSRQ, 108 [KEY_RIGHTALT] = Q_KEY_CODE_ALT_R, 109 [KEY_HOME] = Q_KEY_CODE_HOME, 110 [KEY_UP] = Q_KEY_CODE_UP, 111 [KEY_PAGEUP] = Q_KEY_CODE_PGUP, 112 [KEY_LEFT] = Q_KEY_CODE_LEFT, 113 [KEY_RIGHT] = Q_KEY_CODE_RIGHT, 114 [KEY_END] = Q_KEY_CODE_END, 115 [KEY_DOWN] = Q_KEY_CODE_DOWN, 116 [KEY_PAGEDOWN] = Q_KEY_CODE_PGDN, 117 [KEY_INSERT] = Q_KEY_CODE_INSERT, 118 [KEY_DELETE] = Q_KEY_CODE_DELETE, 119 [KEY_LEFTMETA] = Q_KEY_CODE_META_L, 120 [KEY_RIGHTMETA] = Q_KEY_CODE_META_R, 121 [KEY_MENU] = Q_KEY_CODE_MENU, 122 }; 123 124 static int qemu_input_linux_to_qcode(unsigned int lnx) 125 { 126 assert(lnx < KEY_CNT); 127 return linux_to_qcode[lnx]; 128 } 129 130 typedef struct InputLinux InputLinux; 131 132 struct InputLinux { 133 const char *evdev; 134 int fd; 135 bool repeat; 136 bool grab_request; 137 bool grab_active; 138 bool grab_all; 139 bool keydown[KEY_CNT]; 140 int keycount; 141 int wheel; 142 QTAILQ_ENTRY(InputLinux) next; 143 }; 144 145 static QTAILQ_HEAD(, InputLinux) inputs = QTAILQ_HEAD_INITIALIZER(inputs); 146 147 static void input_linux_toggle_grab(InputLinux *il) 148 { 149 intptr_t request = !il->grab_active; 150 InputLinux *item; 151 int rc; 152 153 rc = ioctl(il->fd, EVIOCGRAB, request); 154 if (rc < 0) { 155 return; 156 } 157 il->grab_active = !il->grab_active; 158 159 if (!il->grab_all) { 160 return; 161 } 162 QTAILQ_FOREACH(item, &inputs, next) { 163 if (item == il || item->grab_all) { 164 /* avoid endless loops */ 165 continue; 166 } 167 if (item->grab_active != il->grab_active) { 168 input_linux_toggle_grab(item); 169 } 170 } 171 } 172 173 static void input_linux_event_keyboard(void *opaque) 174 { 175 InputLinux *il = opaque; 176 struct input_event event; 177 int rc; 178 179 for (;;) { 180 rc = read(il->fd, &event, sizeof(event)); 181 if (rc != sizeof(event)) { 182 if (rc < 0 && errno != EAGAIN) { 183 fprintf(stderr, "%s: read: %s\n", __func__, strerror(errno)); 184 qemu_set_fd_handler(il->fd, NULL, NULL, NULL); 185 close(il->fd); 186 } 187 break; 188 } 189 190 switch (event.type) { 191 case EV_KEY: 192 if (event.value > 2 || (event.value > 1 && !il->repeat)) { 193 /* 194 * ignore autorepeat + unknown key events 195 * 0 == up, 1 == down, 2 == autorepeat, other == undefined 196 */ 197 continue; 198 } 199 /* keep track of key state */ 200 if (!il->keydown[event.code] && event.value) { 201 il->keydown[event.code] = true; 202 il->keycount++; 203 } 204 if (il->keydown[event.code] && !event.value) { 205 il->keydown[event.code] = false; 206 il->keycount--; 207 } 208 209 /* send event to guest when grab is active */ 210 if (il->grab_active) { 211 int qcode = qemu_input_linux_to_qcode(event.code); 212 qemu_input_event_send_key_qcode(NULL, qcode, event.value); 213 } 214 215 /* hotkey -> record switch request ... */ 216 if (il->keydown[KEY_LEFTCTRL] && 217 il->keydown[KEY_RIGHTCTRL]) { 218 il->grab_request = true; 219 } 220 221 /* 222 * ... and do the switch when all keys are lifted, so we 223 * confuse neither guest nor host with keys which seem to 224 * be stuck due to missing key-up events. 225 */ 226 if (il->grab_request && !il->keycount) { 227 il->grab_request = false; 228 input_linux_toggle_grab(il); 229 } 230 break; 231 } 232 } 233 } 234 235 static void input_linux_event_mouse_button(int button) 236 { 237 qemu_input_queue_btn(NULL, button, true); 238 qemu_input_event_sync(); 239 qemu_input_queue_btn(NULL, button, false); 240 qemu_input_event_sync(); 241 } 242 243 static void input_linux_event_mouse(void *opaque) 244 { 245 InputLinux *il = opaque; 246 struct input_event event; 247 int rc; 248 249 for (;;) { 250 rc = read(il->fd, &event, sizeof(event)); 251 if (rc != sizeof(event)) { 252 if (rc < 0 && errno != EAGAIN) { 253 fprintf(stderr, "%s: read: %s\n", __func__, strerror(errno)); 254 qemu_set_fd_handler(il->fd, NULL, NULL, NULL); 255 close(il->fd); 256 } 257 break; 258 } 259 260 /* only send event to guest when grab is active */ 261 if (!il->grab_active) { 262 continue; 263 } 264 265 switch (event.type) { 266 case EV_KEY: 267 switch (event.code) { 268 case BTN_LEFT: 269 qemu_input_queue_btn(NULL, INPUT_BUTTON_LEFT, event.value); 270 break; 271 case BTN_RIGHT: 272 qemu_input_queue_btn(NULL, INPUT_BUTTON_RIGHT, event.value); 273 break; 274 case BTN_MIDDLE: 275 qemu_input_queue_btn(NULL, INPUT_BUTTON_MIDDLE, event.value); 276 break; 277 case BTN_GEAR_UP: 278 qemu_input_queue_btn(NULL, INPUT_BUTTON_WHEEL_UP, event.value); 279 break; 280 case BTN_GEAR_DOWN: 281 qemu_input_queue_btn(NULL, INPUT_BUTTON_WHEEL_DOWN, 282 event.value); 283 break; 284 }; 285 break; 286 case EV_REL: 287 switch (event.code) { 288 case REL_X: 289 qemu_input_queue_rel(NULL, INPUT_AXIS_X, event.value); 290 break; 291 case REL_Y: 292 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, event.value); 293 break; 294 case REL_WHEEL: 295 il->wheel = event.value; 296 break; 297 } 298 break; 299 case EV_SYN: 300 qemu_input_event_sync(); 301 if (il->wheel != 0) { 302 input_linux_event_mouse_button((il->wheel > 0) 303 ? INPUT_BUTTON_WHEEL_UP 304 : INPUT_BUTTON_WHEEL_DOWN); 305 il->wheel = 0; 306 } 307 break; 308 } 309 } 310 } 311 312 int input_linux_init(void *opaque, QemuOpts *opts, Error **errp) 313 { 314 InputLinux *il = g_new0(InputLinux, 1); 315 uint32_t evtmap; 316 int rc, ver; 317 318 il->evdev = qemu_opt_get(opts, "evdev"); 319 il->grab_all = qemu_opt_get_bool(opts, "grab-all", false); 320 il->repeat = qemu_opt_get_bool(opts, "repeat", false); 321 322 if (!il->evdev) { 323 error_setg(errp, "no input device specified"); 324 goto err_free; 325 } 326 327 il->fd = open(il->evdev, O_RDWR); 328 if (il->fd < 0) { 329 error_setg_file_open(errp, errno, il->evdev); 330 goto err_free; 331 } 332 qemu_set_nonblock(il->fd); 333 334 rc = ioctl(il->fd, EVIOCGVERSION, &ver); 335 if (rc < 0) { 336 error_setg(errp, "%s: is not an evdev device", il->evdev); 337 goto err_close; 338 } 339 340 rc = ioctl(il->fd, EVIOCGBIT(0, sizeof(evtmap)), &evtmap); 341 342 if (evtmap & (1 << EV_REL)) { 343 /* has relative axis -> assume mouse */ 344 qemu_set_fd_handler(il->fd, input_linux_event_mouse, NULL, il); 345 } else if (evtmap & (1 << EV_ABS)) { 346 /* has absolute axis -> not supported */ 347 error_setg(errp, "tablet/touchscreen not supported"); 348 goto err_close; 349 } else if (evtmap & (1 << EV_KEY)) { 350 /* has keys/buttons (and no axis) -> assume keyboard */ 351 qemu_set_fd_handler(il->fd, input_linux_event_keyboard, NULL, il); 352 } else { 353 /* Huh? What is this? */ 354 error_setg(errp, "unknown kind of input device"); 355 goto err_close; 356 } 357 input_linux_toggle_grab(il); 358 QTAILQ_INSERT_TAIL(&inputs, il, next); 359 return 0; 360 361 err_close: 362 close(il->fd); 363 err_free: 364 g_free(il); 365 return -1; 366 } 367 368 static QemuOptsList qemu_input_linux_opts = { 369 .name = "input-linux", 370 .head = QTAILQ_HEAD_INITIALIZER(qemu_input_linux_opts.head), 371 .implied_opt_name = "evdev", 372 .desc = { 373 { 374 .name = "evdev", 375 .type = QEMU_OPT_STRING, 376 },{ 377 .name = "grab-all", 378 .type = QEMU_OPT_BOOL, 379 },{ 380 .name = "repeat", 381 .type = QEMU_OPT_BOOL, 382 }, 383 { /* end of list */ } 384 }, 385 }; 386 387 static void input_linux_register_config(void) 388 { 389 qemu_add_opts(&qemu_input_linux_opts); 390 } 391 machine_init(input_linux_register_config); 392