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