1 #include "qemu/osdep.h" 2 #include "hw/qdev.h" 3 #include "sysemu/sysemu.h" 4 #include "qapi-types.h" 5 #include "qemu/error-report.h" 6 #include "qmp-commands.h" 7 #include "trace.h" 8 #include "ui/input.h" 9 #include "ui/console.h" 10 #include "sysemu/replay.h" 11 12 struct QemuInputHandlerState { 13 DeviceState *dev; 14 QemuInputHandler *handler; 15 int id; 16 int events; 17 QemuConsole *con; 18 QTAILQ_ENTRY(QemuInputHandlerState) node; 19 }; 20 21 typedef struct QemuInputEventQueue QemuInputEventQueue; 22 struct QemuInputEventQueue { 23 enum { 24 QEMU_INPUT_QUEUE_DELAY = 1, 25 QEMU_INPUT_QUEUE_EVENT, 26 QEMU_INPUT_QUEUE_SYNC, 27 } type; 28 QEMUTimer *timer; 29 uint32_t delay_ms; 30 QemuConsole *src; 31 InputEvent *evt; 32 QTAILQ_ENTRY(QemuInputEventQueue) node; 33 }; 34 35 static QTAILQ_HEAD(, QemuInputHandlerState) handlers = 36 QTAILQ_HEAD_INITIALIZER(handlers); 37 static NotifierList mouse_mode_notifiers = 38 NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers); 39 40 static QTAILQ_HEAD(QemuInputEventQueueHead, QemuInputEventQueue) kbd_queue = 41 QTAILQ_HEAD_INITIALIZER(kbd_queue); 42 static QEMUTimer *kbd_timer; 43 static uint32_t kbd_default_delay_ms = 10; 44 45 QemuInputHandlerState *qemu_input_handler_register(DeviceState *dev, 46 QemuInputHandler *handler) 47 { 48 QemuInputHandlerState *s = g_new0(QemuInputHandlerState, 1); 49 static int id = 1; 50 51 s->dev = dev; 52 s->handler = handler; 53 s->id = id++; 54 QTAILQ_INSERT_TAIL(&handlers, s, node); 55 56 qemu_input_check_mode_change(); 57 return s; 58 } 59 60 void qemu_input_handler_activate(QemuInputHandlerState *s) 61 { 62 QTAILQ_REMOVE(&handlers, s, node); 63 QTAILQ_INSERT_HEAD(&handlers, s, node); 64 qemu_input_check_mode_change(); 65 } 66 67 void qemu_input_handler_deactivate(QemuInputHandlerState *s) 68 { 69 QTAILQ_REMOVE(&handlers, s, node); 70 QTAILQ_INSERT_TAIL(&handlers, s, node); 71 qemu_input_check_mode_change(); 72 } 73 74 void qemu_input_handler_unregister(QemuInputHandlerState *s) 75 { 76 QTAILQ_REMOVE(&handlers, s, node); 77 g_free(s); 78 qemu_input_check_mode_change(); 79 } 80 81 void qemu_input_handler_bind(QemuInputHandlerState *s, 82 const char *device_id, int head, 83 Error **errp) 84 { 85 QemuConsole *con; 86 Error *err = NULL; 87 88 con = qemu_console_lookup_by_device_name(device_id, head, &err); 89 if (err) { 90 error_propagate(errp, err); 91 return; 92 } 93 94 s->con = con; 95 } 96 97 static QemuInputHandlerState* 98 qemu_input_find_handler(uint32_t mask, QemuConsole *con) 99 { 100 QemuInputHandlerState *s; 101 102 QTAILQ_FOREACH(s, &handlers, node) { 103 if (s->con == NULL || s->con != con) { 104 continue; 105 } 106 if (mask & s->handler->mask) { 107 return s; 108 } 109 } 110 111 QTAILQ_FOREACH(s, &handlers, node) { 112 if (s->con != NULL) { 113 continue; 114 } 115 if (mask & s->handler->mask) { 116 return s; 117 } 118 } 119 return NULL; 120 } 121 122 void qmp_input_send_event(bool has_device, const char *device, 123 bool has_head, int64_t head, 124 InputEventList *events, Error **errp) 125 { 126 InputEventList *e; 127 QemuConsole *con; 128 Error *err = NULL; 129 130 con = NULL; 131 if (has_device) { 132 if (!has_head) { 133 head = 0; 134 } 135 con = qemu_console_lookup_by_device_name(device, head, &err); 136 if (err) { 137 error_propagate(errp, err); 138 return; 139 } 140 } 141 142 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) { 143 error_setg(errp, "VM not running"); 144 return; 145 } 146 147 for (e = events; e != NULL; e = e->next) { 148 InputEvent *event = e->value; 149 150 if (!qemu_input_find_handler(1 << event->type, con)) { 151 error_setg(errp, "Input handler not found for " 152 "event type %s", 153 InputEventKind_lookup[event->type]); 154 return; 155 } 156 } 157 158 for (e = events; e != NULL; e = e->next) { 159 InputEvent *event = e->value; 160 161 qemu_input_event_send(con, event); 162 } 163 164 qemu_input_event_sync(); 165 } 166 167 static void qemu_input_transform_abs_rotate(InputEvent *evt) 168 { 169 switch (graphic_rotate) { 170 case 90: 171 if (evt->u.abs->axis == INPUT_AXIS_X) { 172 evt->u.abs->axis = INPUT_AXIS_Y; 173 } else if (evt->u.abs->axis == INPUT_AXIS_Y) { 174 evt->u.abs->axis = INPUT_AXIS_X; 175 evt->u.abs->value = INPUT_EVENT_ABS_SIZE - 1 - evt->u.abs->value; 176 } 177 break; 178 case 180: 179 evt->u.abs->value = INPUT_EVENT_ABS_SIZE - 1 - evt->u.abs->value; 180 break; 181 case 270: 182 if (evt->u.abs->axis == INPUT_AXIS_X) { 183 evt->u.abs->axis = INPUT_AXIS_Y; 184 evt->u.abs->value = INPUT_EVENT_ABS_SIZE - 1 - evt->u.abs->value; 185 } else if (evt->u.abs->axis == INPUT_AXIS_Y) { 186 evt->u.abs->axis = INPUT_AXIS_X; 187 } 188 break; 189 } 190 } 191 192 static void qemu_input_event_trace(QemuConsole *src, InputEvent *evt) 193 { 194 const char *name; 195 int qcode, idx = -1; 196 197 if (src) { 198 idx = qemu_console_get_index(src); 199 } 200 switch (evt->type) { 201 case INPUT_EVENT_KIND_KEY: 202 switch (evt->u.key->key->type) { 203 case KEY_VALUE_KIND_NUMBER: 204 qcode = qemu_input_key_number_to_qcode(evt->u.key->key->u.number); 205 name = QKeyCode_lookup[qcode]; 206 trace_input_event_key_number(idx, evt->u.key->key->u.number, 207 name, evt->u.key->down); 208 break; 209 case KEY_VALUE_KIND_QCODE: 210 name = QKeyCode_lookup[evt->u.key->key->u.qcode]; 211 trace_input_event_key_qcode(idx, name, evt->u.key->down); 212 break; 213 case KEY_VALUE_KIND__MAX: 214 /* keep gcc happy */ 215 break; 216 } 217 break; 218 case INPUT_EVENT_KIND_BTN: 219 name = InputButton_lookup[evt->u.btn->button]; 220 trace_input_event_btn(idx, name, evt->u.btn->down); 221 break; 222 case INPUT_EVENT_KIND_REL: 223 name = InputAxis_lookup[evt->u.rel->axis]; 224 trace_input_event_rel(idx, name, evt->u.rel->value); 225 break; 226 case INPUT_EVENT_KIND_ABS: 227 name = InputAxis_lookup[evt->u.abs->axis]; 228 trace_input_event_abs(idx, name, evt->u.abs->value); 229 break; 230 case INPUT_EVENT_KIND__MAX: 231 /* keep gcc happy */ 232 break; 233 } 234 } 235 236 static void qemu_input_queue_process(void *opaque) 237 { 238 struct QemuInputEventQueueHead *queue = opaque; 239 QemuInputEventQueue *item; 240 241 g_assert(!QTAILQ_EMPTY(queue)); 242 item = QTAILQ_FIRST(queue); 243 g_assert(item->type == QEMU_INPUT_QUEUE_DELAY); 244 QTAILQ_REMOVE(queue, item, node); 245 g_free(item); 246 247 while (!QTAILQ_EMPTY(queue)) { 248 item = QTAILQ_FIRST(queue); 249 switch (item->type) { 250 case QEMU_INPUT_QUEUE_DELAY: 251 timer_mod(item->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) 252 + item->delay_ms); 253 return; 254 case QEMU_INPUT_QUEUE_EVENT: 255 qemu_input_event_send(item->src, item->evt); 256 qapi_free_InputEvent(item->evt); 257 break; 258 case QEMU_INPUT_QUEUE_SYNC: 259 qemu_input_event_sync(); 260 break; 261 } 262 QTAILQ_REMOVE(queue, item, node); 263 g_free(item); 264 } 265 } 266 267 static void qemu_input_queue_delay(struct QemuInputEventQueueHead *queue, 268 QEMUTimer *timer, uint32_t delay_ms) 269 { 270 QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1); 271 bool start_timer = QTAILQ_EMPTY(queue); 272 273 item->type = QEMU_INPUT_QUEUE_DELAY; 274 item->delay_ms = delay_ms; 275 item->timer = timer; 276 QTAILQ_INSERT_TAIL(queue, item, node); 277 278 if (start_timer) { 279 timer_mod(item->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) 280 + item->delay_ms); 281 } 282 } 283 284 static void qemu_input_queue_event(struct QemuInputEventQueueHead *queue, 285 QemuConsole *src, InputEvent *evt) 286 { 287 QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1); 288 289 item->type = QEMU_INPUT_QUEUE_EVENT; 290 item->src = src; 291 item->evt = evt; 292 QTAILQ_INSERT_TAIL(queue, item, node); 293 } 294 295 static void qemu_input_queue_sync(struct QemuInputEventQueueHead *queue) 296 { 297 QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1); 298 299 item->type = QEMU_INPUT_QUEUE_SYNC; 300 QTAILQ_INSERT_TAIL(queue, item, node); 301 } 302 303 void qemu_input_event_send_impl(QemuConsole *src, InputEvent *evt) 304 { 305 QemuInputHandlerState *s; 306 307 qemu_input_event_trace(src, evt); 308 309 /* pre processing */ 310 if (graphic_rotate && (evt->type == INPUT_EVENT_KIND_ABS)) { 311 qemu_input_transform_abs_rotate(evt); 312 } 313 314 /* send event */ 315 s = qemu_input_find_handler(1 << evt->type, src); 316 if (!s) { 317 return; 318 } 319 s->handler->event(s->dev, src, evt); 320 s->events++; 321 } 322 323 void qemu_input_event_send(QemuConsole *src, InputEvent *evt) 324 { 325 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) { 326 return; 327 } 328 329 replay_input_event(src, evt); 330 } 331 332 void qemu_input_event_sync_impl(void) 333 { 334 QemuInputHandlerState *s; 335 336 trace_input_event_sync(); 337 338 QTAILQ_FOREACH(s, &handlers, node) { 339 if (!s->events) { 340 continue; 341 } 342 if (s->handler->sync) { 343 s->handler->sync(s->dev); 344 } 345 s->events = 0; 346 } 347 } 348 349 void qemu_input_event_sync(void) 350 { 351 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) { 352 return; 353 } 354 355 replay_input_sync_event(); 356 } 357 358 InputEvent *qemu_input_event_new_key(KeyValue *key, bool down) 359 { 360 InputEvent *evt = g_new0(InputEvent, 1); 361 evt->u.key = g_new0(InputKeyEvent, 1); 362 evt->type = INPUT_EVENT_KIND_KEY; 363 evt->u.key->key = key; 364 evt->u.key->down = down; 365 return evt; 366 } 367 368 void qemu_input_event_send_key(QemuConsole *src, KeyValue *key, bool down) 369 { 370 InputEvent *evt; 371 evt = qemu_input_event_new_key(key, down); 372 if (QTAILQ_EMPTY(&kbd_queue)) { 373 qemu_input_event_send(src, evt); 374 qemu_input_event_sync(); 375 qapi_free_InputEvent(evt); 376 } else { 377 qemu_input_queue_event(&kbd_queue, src, evt); 378 qemu_input_queue_sync(&kbd_queue); 379 } 380 } 381 382 void qemu_input_event_send_key_number(QemuConsole *src, int num, bool down) 383 { 384 KeyValue *key = g_new0(KeyValue, 1); 385 key->type = KEY_VALUE_KIND_NUMBER; 386 key->u.number = num; 387 qemu_input_event_send_key(src, key, down); 388 } 389 390 void qemu_input_event_send_key_qcode(QemuConsole *src, QKeyCode q, bool down) 391 { 392 KeyValue *key = g_new0(KeyValue, 1); 393 key->type = KEY_VALUE_KIND_QCODE; 394 key->u.qcode = q; 395 qemu_input_event_send_key(src, key, down); 396 } 397 398 void qemu_input_event_send_key_delay(uint32_t delay_ms) 399 { 400 if (!kbd_timer) { 401 kbd_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, qemu_input_queue_process, 402 &kbd_queue); 403 } 404 qemu_input_queue_delay(&kbd_queue, kbd_timer, 405 delay_ms ? delay_ms : kbd_default_delay_ms); 406 } 407 408 InputEvent *qemu_input_event_new_btn(InputButton btn, bool down) 409 { 410 InputEvent *evt = g_new0(InputEvent, 1); 411 evt->u.btn = g_new0(InputBtnEvent, 1); 412 evt->type = INPUT_EVENT_KIND_BTN; 413 evt->u.btn->button = btn; 414 evt->u.btn->down = down; 415 return evt; 416 } 417 418 void qemu_input_queue_btn(QemuConsole *src, InputButton btn, bool down) 419 { 420 InputEvent *evt; 421 evt = qemu_input_event_new_btn(btn, down); 422 qemu_input_event_send(src, evt); 423 qapi_free_InputEvent(evt); 424 } 425 426 void qemu_input_update_buttons(QemuConsole *src, uint32_t *button_map, 427 uint32_t button_old, uint32_t button_new) 428 { 429 InputButton btn; 430 uint32_t mask; 431 432 for (btn = 0; btn < INPUT_BUTTON__MAX; btn++) { 433 mask = button_map[btn]; 434 if ((button_old & mask) == (button_new & mask)) { 435 continue; 436 } 437 qemu_input_queue_btn(src, btn, button_new & mask); 438 } 439 } 440 441 bool qemu_input_is_absolute(void) 442 { 443 QemuInputHandlerState *s; 444 445 s = qemu_input_find_handler(INPUT_EVENT_MASK_REL | INPUT_EVENT_MASK_ABS, 446 NULL); 447 return (s != NULL) && (s->handler->mask & INPUT_EVENT_MASK_ABS); 448 } 449 450 int qemu_input_scale_axis(int value, int size_in, int size_out) 451 { 452 if (size_in < 2) { 453 return size_out / 2; 454 } 455 return (int64_t)value * (size_out - 1) / (size_in - 1); 456 } 457 458 InputEvent *qemu_input_event_new_move(InputEventKind kind, 459 InputAxis axis, int value) 460 { 461 InputEvent *evt = g_new0(InputEvent, 1); 462 InputMoveEvent *move = g_new0(InputMoveEvent, 1); 463 464 evt->type = kind; 465 evt->u.data = move; 466 move->axis = axis; 467 move->value = value; 468 return evt; 469 } 470 471 void qemu_input_queue_rel(QemuConsole *src, InputAxis axis, int value) 472 { 473 InputEvent *evt; 474 evt = qemu_input_event_new_move(INPUT_EVENT_KIND_REL, axis, value); 475 qemu_input_event_send(src, evt); 476 qapi_free_InputEvent(evt); 477 } 478 479 void qemu_input_queue_abs(QemuConsole *src, InputAxis axis, int value, int size) 480 { 481 InputEvent *evt; 482 int scaled = qemu_input_scale_axis(value, size, INPUT_EVENT_ABS_SIZE); 483 evt = qemu_input_event_new_move(INPUT_EVENT_KIND_ABS, axis, scaled); 484 qemu_input_event_send(src, evt); 485 qapi_free_InputEvent(evt); 486 } 487 488 void qemu_input_check_mode_change(void) 489 { 490 static int current_is_absolute; 491 int is_absolute; 492 493 is_absolute = qemu_input_is_absolute(); 494 495 if (is_absolute != current_is_absolute) { 496 trace_input_mouse_mode(is_absolute); 497 notifier_list_notify(&mouse_mode_notifiers, NULL); 498 } 499 500 current_is_absolute = is_absolute; 501 } 502 503 void qemu_add_mouse_mode_change_notifier(Notifier *notify) 504 { 505 notifier_list_add(&mouse_mode_notifiers, notify); 506 } 507 508 void qemu_remove_mouse_mode_change_notifier(Notifier *notify) 509 { 510 notifier_remove(notify); 511 } 512 513 MouseInfoList *qmp_query_mice(Error **errp) 514 { 515 MouseInfoList *mice_list = NULL; 516 MouseInfoList *info; 517 QemuInputHandlerState *s; 518 bool current = true; 519 520 QTAILQ_FOREACH(s, &handlers, node) { 521 if (!(s->handler->mask & 522 (INPUT_EVENT_MASK_REL | INPUT_EVENT_MASK_ABS))) { 523 continue; 524 } 525 526 info = g_new0(MouseInfoList, 1); 527 info->value = g_new0(MouseInfo, 1); 528 info->value->index = s->id; 529 info->value->name = g_strdup(s->handler->name); 530 info->value->absolute = s->handler->mask & INPUT_EVENT_MASK_ABS; 531 info->value->current = current; 532 533 current = false; 534 info->next = mice_list; 535 mice_list = info; 536 } 537 538 return mice_list; 539 } 540 541 void hmp_mouse_set(Monitor *mon, const QDict *qdict) 542 { 543 QemuInputHandlerState *s; 544 int index = qdict_get_int(qdict, "index"); 545 int found = 0; 546 547 QTAILQ_FOREACH(s, &handlers, node) { 548 if (s->id != index) { 549 continue; 550 } 551 if (!(s->handler->mask & (INPUT_EVENT_MASK_REL | 552 INPUT_EVENT_MASK_ABS))) { 553 error_report("Input device '%s' is not a mouse", s->handler->name); 554 return; 555 } 556 found = 1; 557 qemu_input_handler_activate(s); 558 break; 559 } 560 561 if (!found) { 562 error_report("Mouse at index '%d' not found", index); 563 } 564 565 qemu_input_check_mode_change(); 566 } 567