replay-input.c 4.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * replay-input.c
 *
 * Copyright (c) 2010-2015 Institute for System Programming
 *                         of the Russian Academy of Sciences.
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 *
 */

P
Peter Maydell 已提交
12
#include "qemu/osdep.h"
13
#include "qapi/error.h"
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#include "qemu-common.h"
#include "sysemu/replay.h"
#include "replay-internal.h"
#include "qemu/notify.h"
#include "ui/input.h"
#include "qapi/qmp-output-visitor.h"
#include "qapi/qmp-input-visitor.h"
#include "qapi-visit.h"

static InputEvent *qapi_clone_InputEvent(InputEvent *src)
{
    QmpOutputVisitor *qov;
    QmpInputVisitor *qiv;
    Visitor *ov, *iv;
    QObject *obj;
    InputEvent *dst = NULL;

    qov = qmp_output_visitor_new();
    ov = qmp_output_get_visitor(qov);
33
    visit_type_InputEvent(ov, NULL, &src, &error_abort);
34 35 36 37 38 39
    obj = qmp_output_get_qobject(qov);
    qmp_output_visitor_cleanup(qov);
    if (!obj) {
        return NULL;
    }

40
    qiv = qmp_input_visitor_new(obj, true);
41
    iv = qmp_input_get_visitor(qiv);
42
    visit_type_InputEvent(iv, NULL, &dst, &error_abort);
43 44 45 46 47 48 49 50
    qmp_input_visitor_cleanup(qiv);
    qobject_decref(obj);

    return dst;
}

void replay_save_input_event(InputEvent *evt)
{
51 52 53
    InputKeyEvent *key;
    InputBtnEvent *btn;
    InputMoveEvent *move;
54 55 56 57
    replay_put_dword(evt->type);

    switch (evt->type) {
    case INPUT_EVENT_KIND_KEY:
58
        key = evt->u.key.data;
59
        replay_put_dword(key->key->type);
60

61
        switch (key->key->type) {
62
        case KEY_VALUE_KIND_NUMBER:
63
            replay_put_qword(key->key->u.number.data);
64
            replay_put_byte(key->down);
65 66
            break;
        case KEY_VALUE_KIND_QCODE:
67
            replay_put_dword(key->key->u.qcode.data);
68
            replay_put_byte(key->down);
69
            break;
70
        case KEY_VALUE_KIND__MAX:
71 72 73 74 75
            /* keep gcc happy */
            break;
        }
        break;
    case INPUT_EVENT_KIND_BTN:
76
        btn = evt->u.btn.data;
77 78
        replay_put_dword(btn->button);
        replay_put_byte(btn->down);
79 80
        break;
    case INPUT_EVENT_KIND_REL:
81
        move = evt->u.rel.data;
82 83
        replay_put_dword(move->axis);
        replay_put_qword(move->value);
84 85
        break;
    case INPUT_EVENT_KIND_ABS:
86
        move = evt->u.abs.data;
87 88
        replay_put_dword(move->axis);
        replay_put_qword(move->value);
89
        break;
90
    case INPUT_EVENT_KIND__MAX:
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
        /* keep gcc happy */
        break;
    }
}

InputEvent *replay_read_input_event(void)
{
    InputEvent evt;
    KeyValue keyValue;
    InputKeyEvent key;
    key.key = &keyValue;
    InputBtnEvent btn;
    InputMoveEvent rel;
    InputMoveEvent abs;

    evt.type = replay_get_dword();
    switch (evt.type) {
    case INPUT_EVENT_KIND_KEY:
109 110
        evt.u.key.data = &key;
        evt.u.key.data->key->type = replay_get_dword();
111

112
        switch (evt.u.key.data->key->type) {
113
        case KEY_VALUE_KIND_NUMBER:
114 115
            evt.u.key.data->key->u.number.data = replay_get_qword();
            evt.u.key.data->down = replay_get_byte();
116 117
            break;
        case KEY_VALUE_KIND_QCODE:
118 119
            evt.u.key.data->key->u.qcode.data = (QKeyCode)replay_get_dword();
            evt.u.key.data->down = replay_get_byte();
120
            break;
121
        case KEY_VALUE_KIND__MAX:
122 123 124 125 126
            /* keep gcc happy */
            break;
        }
        break;
    case INPUT_EVENT_KIND_BTN:
127 128 129
        evt.u.btn.data = &btn;
        evt.u.btn.data->button = (InputButton)replay_get_dword();
        evt.u.btn.data->down = replay_get_byte();
130 131
        break;
    case INPUT_EVENT_KIND_REL:
132 133 134
        evt.u.rel.data = &rel;
        evt.u.rel.data->axis = (InputAxis)replay_get_dword();
        evt.u.rel.data->value = replay_get_qword();
135 136
        break;
    case INPUT_EVENT_KIND_ABS:
137 138 139
        evt.u.abs.data = &abs;
        evt.u.abs.data->axis = (InputAxis)replay_get_dword();
        evt.u.abs.data->value = replay_get_qword();
140
        break;
141
    case INPUT_EVENT_KIND__MAX:
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
        /* keep gcc happy */
        break;
    }

    return qapi_clone_InputEvent(&evt);
}

void replay_input_event(QemuConsole *src, InputEvent *evt)
{
    if (replay_mode == REPLAY_MODE_PLAY) {
        /* Nothing */
    } else if (replay_mode == REPLAY_MODE_RECORD) {
        replay_add_input_event(qapi_clone_InputEvent(evt));
    } else {
        qemu_input_event_send_impl(src, evt);
    }
}

void replay_input_sync_event(void)
{
    if (replay_mode == REPLAY_MODE_PLAY) {
        /* Nothing */
    } else if (replay_mode == REPLAY_MODE_RECORD) {
        replay_add_input_sync_event();
    } else {
        qemu_input_event_sync_impl();
    }
}