input.c 15.1 KB
Newer Older
P
Peter Maydell 已提交
1
#include "qemu/osdep.h"
2
#include "hw/qdev.h"
3 4
#include "sysemu/sysemu.h"
#include "qapi-types.h"
5
#include "qemu/error-report.h"
6
#include "qmp-commands.h"
G
Gerd Hoffmann 已提交
7
#include "trace.h"
8
#include "ui/input.h"
G
Gerd Hoffmann 已提交
9
#include "ui/console.h"
10
#include "sysemu/replay.h"
11 12 13 14 15 16

struct QemuInputHandlerState {
    DeviceState       *dev;
    QemuInputHandler  *handler;
    int               id;
    int               events;
17
    QemuConsole       *con;
18 19
    QTAILQ_ENTRY(QemuInputHandlerState) node;
};
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

typedef struct QemuInputEventQueue QemuInputEventQueue;
struct QemuInputEventQueue {
    enum {
        QEMU_INPUT_QUEUE_DELAY = 1,
        QEMU_INPUT_QUEUE_EVENT,
        QEMU_INPUT_QUEUE_SYNC,
    } type;
    QEMUTimer *timer;
    uint32_t delay_ms;
    QemuConsole *src;
    InputEvent *evt;
    QTAILQ_ENTRY(QemuInputEventQueue) node;
};

35 36
static QTAILQ_HEAD(, QemuInputHandlerState) handlers =
    QTAILQ_HEAD_INITIALIZER(handlers);
37 38
static NotifierList mouse_mode_notifiers =
    NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers);
39

40 41 42 43 44
static QTAILQ_HEAD(QemuInputEventQueueHead, QemuInputEventQueue) kbd_queue =
    QTAILQ_HEAD_INITIALIZER(kbd_queue);
static QEMUTimer *kbd_timer;
static uint32_t kbd_default_delay_ms = 10;

45 46 47 48 49 50 51 52 53 54
QemuInputHandlerState *qemu_input_handler_register(DeviceState *dev,
                                                   QemuInputHandler *handler)
{
    QemuInputHandlerState *s = g_new0(QemuInputHandlerState, 1);
    static int id = 1;

    s->dev = dev;
    s->handler = handler;
    s->id = id++;
    QTAILQ_INSERT_TAIL(&handlers, s, node);
55 56

    qemu_input_check_mode_change();
57 58 59 60 61 62 63
    return s;
}

void qemu_input_handler_activate(QemuInputHandlerState *s)
{
    QTAILQ_REMOVE(&handlers, s, node);
    QTAILQ_INSERT_HEAD(&handlers, s, node);
64
    qemu_input_check_mode_change();
65 66
}

67 68 69 70 71 72 73
void qemu_input_handler_deactivate(QemuInputHandlerState *s)
{
    QTAILQ_REMOVE(&handlers, s, node);
    QTAILQ_INSERT_TAIL(&handlers, s, node);
    qemu_input_check_mode_change();
}

74 75 76 77
void qemu_input_handler_unregister(QemuInputHandlerState *s)
{
    QTAILQ_REMOVE(&handlers, s, node);
    g_free(s);
78
    qemu_input_check_mode_change();
79 80
}

81 82 83 84 85
void qemu_input_handler_bind(QemuInputHandlerState *s,
                             const char *device_id, int head,
                             Error **errp)
{
    QemuConsole *con;
86
    Error *err = NULL;
87

88 89 90
    con = qemu_console_lookup_by_device_name(device_id, head, &err);
    if (err) {
        error_propagate(errp, err);
91 92 93 94 95 96
        return;
    }

    s->con = con;
}

97
static QemuInputHandlerState*
98
qemu_input_find_handler(uint32_t mask, QemuConsole *con)
99 100 101 102
{
    QemuInputHandlerState *s;

    QTAILQ_FOREACH(s, &handlers, node) {
103 104 105 106 107 108 109 110 111 112 113 114
        if (s->con == NULL || s->con != con) {
            continue;
        }
        if (mask & s->handler->mask) {
            return s;
        }
    }

    QTAILQ_FOREACH(s, &handlers, node) {
        if (s->con != NULL) {
            continue;
        }
115 116 117 118 119 120 121
        if (mask & s->handler->mask) {
            return s;
        }
    }
    return NULL;
}

122 123 124
void qmp_input_send_event(bool has_device, const char *device,
                          bool has_head, int64_t head,
                          InputEventList *events, Error **errp)
M
Marcelo Tosatti 已提交
125 126 127
{
    InputEventList *e;
    QemuConsole *con;
128
    Error *err = NULL;
M
Marcelo Tosatti 已提交
129

130
    con = NULL;
131 132 133 134 135 136 137
    if (has_device) {
        if (!has_head) {
            head = 0;
        }
        con = qemu_console_lookup_by_device_name(device, head, &err);
        if (err) {
            error_propagate(errp, err);
138 139
            return;
        }
M
Marcelo Tosatti 已提交
140 141 142 143 144 145 146 147 148 149
    }

    if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
        error_setg(errp, "VM not running");
        return;
    }

    for (e = events; e != NULL; e = e->next) {
        InputEvent *event = e->value;

150
        if (!qemu_input_find_handler(1 << event->type, con)) {
M
Marcelo Tosatti 已提交
151 152
            error_setg(errp, "Input handler not found for "
                             "event type %s",
153
                            InputEventKind_lookup[event->type]);
M
Marcelo Tosatti 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166
            return;
        }
    }

    for (e = events; e != NULL; e = e->next) {
        InputEvent *event = e->value;

        qemu_input_event_send(con, event);
    }

    qemu_input_event_sync();
}

167 168
static void qemu_input_transform_abs_rotate(InputEvent *evt)
{
169
    InputMoveEvent *move = evt->u.abs;
170 171
    switch (graphic_rotate) {
    case 90:
172 173 174 175 176
        if (move->axis == INPUT_AXIS_X) {
            move->axis = INPUT_AXIS_Y;
        } else if (move->axis == INPUT_AXIS_Y) {
            move->axis = INPUT_AXIS_X;
            move->value = INPUT_EVENT_ABS_SIZE - 1 - move->value;
177 178 179
        }
        break;
    case 180:
180
        move->value = INPUT_EVENT_ABS_SIZE - 1 - move->value;
181 182
        break;
    case 270:
183 184 185 186 187
        if (move->axis == INPUT_AXIS_X) {
            move->axis = INPUT_AXIS_Y;
            move->value = INPUT_EVENT_ABS_SIZE - 1 - move->value;
        } else if (move->axis == INPUT_AXIS_Y) {
            move->axis = INPUT_AXIS_X;
188 189 190 191 192
        }
        break;
    }
}

G
Gerd Hoffmann 已提交
193 194 195
static void qemu_input_event_trace(QemuConsole *src, InputEvent *evt)
{
    const char *name;
196
    int qcode, idx = -1;
197 198 199
    InputKeyEvent *key;
    InputBtnEvent *btn;
    InputMoveEvent *move;
G
Gerd Hoffmann 已提交
200 201 202 203

    if (src) {
        idx = qemu_console_get_index(src);
    }
204
    switch (evt->type) {
G
Gerd Hoffmann 已提交
205
    case INPUT_EVENT_KIND_KEY:
206 207
        key = evt->u.key;
        switch (key->key->type) {
G
Gerd Hoffmann 已提交
208
        case KEY_VALUE_KIND_NUMBER:
209
            qcode = qemu_input_key_number_to_qcode(key->key->u.number);
210
            name = QKeyCode_lookup[qcode];
211 212
            trace_input_event_key_number(idx, key->key->u.number,
                                         name, key->down);
G
Gerd Hoffmann 已提交
213 214
            break;
        case KEY_VALUE_KIND_QCODE:
215 216
            name = QKeyCode_lookup[key->key->u.qcode];
            trace_input_event_key_qcode(idx, name, key->down);
G
Gerd Hoffmann 已提交
217
            break;
218
        case KEY_VALUE_KIND__MAX:
G
Gerd Hoffmann 已提交
219 220 221 222 223
            /* keep gcc happy */
            break;
        }
        break;
    case INPUT_EVENT_KIND_BTN:
224 225 226
        btn = evt->u.btn;
        name = InputButton_lookup[btn->button];
        trace_input_event_btn(idx, name, btn->down);
G
Gerd Hoffmann 已提交
227 228
        break;
    case INPUT_EVENT_KIND_REL:
229 230 231
        move = evt->u.rel;
        name = InputAxis_lookup[move->axis];
        trace_input_event_rel(idx, name, move->value);
G
Gerd Hoffmann 已提交
232 233
        break;
    case INPUT_EVENT_KIND_ABS:
234 235 236
        move = evt->u.abs;
        name = InputAxis_lookup[move->axis];
        trace_input_event_abs(idx, name, move->value);
G
Gerd Hoffmann 已提交
237
        break;
238
    case INPUT_EVENT_KIND__MAX:
G
Gerd Hoffmann 已提交
239 240 241 242 243
        /* keep gcc happy */
        break;
    }
}

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
static void qemu_input_queue_process(void *opaque)
{
    struct QemuInputEventQueueHead *queue = opaque;
    QemuInputEventQueue *item;

    g_assert(!QTAILQ_EMPTY(queue));
    item = QTAILQ_FIRST(queue);
    g_assert(item->type == QEMU_INPUT_QUEUE_DELAY);
    QTAILQ_REMOVE(queue, item, node);
    g_free(item);

    while (!QTAILQ_EMPTY(queue)) {
        item = QTAILQ_FIRST(queue);
        switch (item->type) {
        case QEMU_INPUT_QUEUE_DELAY:
            timer_mod(item->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL)
                      + item->delay_ms);
            return;
        case QEMU_INPUT_QUEUE_EVENT:
            qemu_input_event_send(item->src, item->evt);
            qapi_free_InputEvent(item->evt);
            break;
        case QEMU_INPUT_QUEUE_SYNC:
            qemu_input_event_sync();
            break;
        }
        QTAILQ_REMOVE(queue, item, node);
        g_free(item);
    }
}

static void qemu_input_queue_delay(struct QemuInputEventQueueHead *queue,
                                   QEMUTimer *timer, uint32_t delay_ms)
{
    QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1);
    bool start_timer = QTAILQ_EMPTY(queue);

    item->type = QEMU_INPUT_QUEUE_DELAY;
    item->delay_ms = delay_ms;
    item->timer = timer;
    QTAILQ_INSERT_TAIL(queue, item, node);

    if (start_timer) {
        timer_mod(item->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL)
                  + item->delay_ms);
    }
}

static void qemu_input_queue_event(struct QemuInputEventQueueHead *queue,
                                   QemuConsole *src, InputEvent *evt)
{
    QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1);

    item->type = QEMU_INPUT_QUEUE_EVENT;
    item->src = src;
    item->evt = evt;
    QTAILQ_INSERT_TAIL(queue, item, node);
}

static void qemu_input_queue_sync(struct QemuInputEventQueueHead *queue)
{
    QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1);

    item->type = QEMU_INPUT_QUEUE_SYNC;
    QTAILQ_INSERT_TAIL(queue, item, node);
}

311
void qemu_input_event_send_impl(QemuConsole *src, InputEvent *evt)
312 313 314
{
    QemuInputHandlerState *s;

G
Gerd Hoffmann 已提交
315 316
    qemu_input_event_trace(src, evt);

317
    /* pre processing */
318
    if (graphic_rotate && (evt->type == INPUT_EVENT_KIND_ABS)) {
319 320 321 322
            qemu_input_transform_abs_rotate(evt);
    }

    /* send event */
323
    s = qemu_input_find_handler(1 << evt->type, src);
G
Gerd Hoffmann 已提交
324 325 326
    if (!s) {
        return;
    }
327 328 329 330
    s->handler->event(s->dev, src, evt);
    s->events++;
}

331
void qemu_input_event_send(QemuConsole *src, InputEvent *evt)
332 333 334 335 336
{
    if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
        return;
    }

337 338 339 340 341 342 343
    replay_input_event(src, evt);
}

void qemu_input_event_sync_impl(void)
{
    QemuInputHandlerState *s;

G
Gerd Hoffmann 已提交
344 345
    trace_input_event_sync();

346 347 348 349 350 351 352 353 354 355
    QTAILQ_FOREACH(s, &handlers, node) {
        if (!s->events) {
            continue;
        }
        if (s->handler->sync) {
            s->handler->sync(s->dev);
        }
        s->events = 0;
    }
}
356

357 358 359 360 361 362 363 364 365
void qemu_input_event_sync(void)
{
    if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
        return;
    }

    replay_input_sync_event();
}

366 367 368
InputEvent *qemu_input_event_new_key(KeyValue *key, bool down)
{
    InputEvent *evt = g_new0(InputEvent, 1);
369 370 371 372
    evt->u.key = g_new0(InputKeyEvent, 1);
    evt->type = INPUT_EVENT_KIND_KEY;
    evt->u.key->key = key;
    evt->u.key->down = down;
373 374 375 376 377 378 379
    return evt;
}

void qemu_input_event_send_key(QemuConsole *src, KeyValue *key, bool down)
{
    InputEvent *evt;
    evt = qemu_input_event_new_key(key, down);
380 381 382 383 384 385 386 387
    if (QTAILQ_EMPTY(&kbd_queue)) {
        qemu_input_event_send(src, evt);
        qemu_input_event_sync();
        qapi_free_InputEvent(evt);
    } else {
        qemu_input_queue_event(&kbd_queue, src, evt);
        qemu_input_queue_sync(&kbd_queue);
    }
388 389 390 391 392
}

void qemu_input_event_send_key_number(QemuConsole *src, int num, bool down)
{
    KeyValue *key = g_new0(KeyValue, 1);
393 394
    key->type = KEY_VALUE_KIND_NUMBER;
    key->u.number = num;
395 396 397 398 399 400
    qemu_input_event_send_key(src, key, down);
}

void qemu_input_event_send_key_qcode(QemuConsole *src, QKeyCode q, bool down)
{
    KeyValue *key = g_new0(KeyValue, 1);
401 402
    key->type = KEY_VALUE_KIND_QCODE;
    key->u.qcode = q;
403 404
    qemu_input_event_send_key(src, key, down);
}
405

406 407 408 409 410 411 412 413 414 415
void qemu_input_event_send_key_delay(uint32_t delay_ms)
{
    if (!kbd_timer) {
        kbd_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, qemu_input_queue_process,
                                 &kbd_queue);
    }
    qemu_input_queue_delay(&kbd_queue, kbd_timer,
                           delay_ms ? delay_ms : kbd_default_delay_ms);
}

416 417 418
InputEvent *qemu_input_event_new_btn(InputButton btn, bool down)
{
    InputEvent *evt = g_new0(InputEvent, 1);
419 420 421 422
    evt->u.btn = g_new0(InputBtnEvent, 1);
    evt->type = INPUT_EVENT_KIND_BTN;
    evt->u.btn->button = btn;
    evt->u.btn->down = down;
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
    return evt;
}

void qemu_input_queue_btn(QemuConsole *src, InputButton btn, bool down)
{
    InputEvent *evt;
    evt = qemu_input_event_new_btn(btn, down);
    qemu_input_event_send(src, evt);
    qapi_free_InputEvent(evt);
}

void qemu_input_update_buttons(QemuConsole *src, uint32_t *button_map,
                               uint32_t button_old, uint32_t button_new)
{
    InputButton btn;
    uint32_t mask;

440
    for (btn = 0; btn < INPUT_BUTTON__MAX; btn++) {
441 442 443 444 445 446 447 448
        mask = button_map[btn];
        if ((button_old & mask) == (button_new & mask)) {
            continue;
        }
        qemu_input_queue_btn(src, btn, button_new & mask);
    }
}

449 450 451 452
bool qemu_input_is_absolute(void)
{
    QemuInputHandlerState *s;

453 454
    s = qemu_input_find_handler(INPUT_EVENT_MASK_REL | INPUT_EVENT_MASK_ABS,
                                NULL);
455 456 457
    return (s != NULL) && (s->handler->mask & INPUT_EVENT_MASK_ABS);
}

458 459 460 461 462 463 464 465 466 467 468 469 470 471
int qemu_input_scale_axis(int value, int size_in, int size_out)
{
    if (size_in < 2) {
        return size_out / 2;
    }
    return (int64_t)value * (size_out - 1) / (size_in - 1);
}

InputEvent *qemu_input_event_new_move(InputEventKind kind,
                                      InputAxis axis, int value)
{
    InputEvent *evt = g_new0(InputEvent, 1);
    InputMoveEvent *move = g_new0(InputMoveEvent, 1);

472
    evt->type = kind;
473
    evt->u.rel = move; /* evt->u.rel is the same as evt->u.abs */
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
    move->axis = axis;
    move->value = value;
    return evt;
}

void qemu_input_queue_rel(QemuConsole *src, InputAxis axis, int value)
{
    InputEvent *evt;
    evt = qemu_input_event_new_move(INPUT_EVENT_KIND_REL, axis, value);
    qemu_input_event_send(src, evt);
    qapi_free_InputEvent(evt);
}

void qemu_input_queue_abs(QemuConsole *src, InputAxis axis, int value, int size)
{
    InputEvent *evt;
    int scaled = qemu_input_scale_axis(value, size, INPUT_EVENT_ABS_SIZE);
    evt = qemu_input_event_new_move(INPUT_EVENT_KIND_ABS, axis, scaled);
    qemu_input_event_send(src, evt);
    qapi_free_InputEvent(evt);
}
495 496 497 498 499 500 501 502 503

void qemu_input_check_mode_change(void)
{
    static int current_is_absolute;
    int is_absolute;

    is_absolute = qemu_input_is_absolute();

    if (is_absolute != current_is_absolute) {
504
        trace_input_mouse_mode(is_absolute);
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
        notifier_list_notify(&mouse_mode_notifiers, NULL);
    }

    current_is_absolute = is_absolute;
}

void qemu_add_mouse_mode_change_notifier(Notifier *notify)
{
    notifier_list_add(&mouse_mode_notifiers, notify);
}

void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
{
    notifier_remove(notify);
}
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547

MouseInfoList *qmp_query_mice(Error **errp)
{
    MouseInfoList *mice_list = NULL;
    MouseInfoList *info;
    QemuInputHandlerState *s;
    bool current = true;

    QTAILQ_FOREACH(s, &handlers, node) {
        if (!(s->handler->mask &
              (INPUT_EVENT_MASK_REL | INPUT_EVENT_MASK_ABS))) {
            continue;
        }

        info = g_new0(MouseInfoList, 1);
        info->value = g_new0(MouseInfo, 1);
        info->value->index = s->id;
        info->value->name = g_strdup(s->handler->name);
        info->value->absolute = s->handler->mask & INPUT_EVENT_MASK_ABS;
        info->value->current = current;

        current = false;
        info->next = mice_list;
        mice_list = info;
    }

    return mice_list;
}
548

549
void hmp_mouse_set(Monitor *mon, const QDict *qdict)
550 551 552 553 554 555
{
    QemuInputHandlerState *s;
    int index = qdict_get_int(qdict, "index");
    int found = 0;

    QTAILQ_FOREACH(s, &handlers, node) {
556 557
        if (s->id != index) {
            continue;
558
        }
559 560 561 562 563 564 565 566
        if (!(s->handler->mask & (INPUT_EVENT_MASK_REL |
                                  INPUT_EVENT_MASK_ABS))) {
            error_report("Input device '%s' is not a mouse", s->handler->name);
            return;
        }
        found = 1;
        qemu_input_handler_activate(s);
        break;
567 568 569
    }

    if (!found) {
570
        error_report("Mouse at index '%d' not found", index);
571 572 573 574
    }

    qemu_input_check_mode_change();
}