This records user input (keyboard and mouse events) in record mode and replays these input events in replay mode.
Signed-off-by: Pavel Dovgalyuk <pavel.dovga...@ispras.ru> --- include/ui/input.h | 2 + replay/Makefile.objs | 1 replay/replay-events.c | 48 ++++++++++++++++++++ replay/replay-input.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++ replay/replay-internal.h | 11 ++++- replay/replay.h | 5 ++ ui/input.c | 80 ++++++++++++++++++++++++++-------- 7 files changed, 235 insertions(+), 20 deletions(-) create mode 100755 replay/replay-input.c diff --git a/include/ui/input.h b/include/ui/input.h index 5d5ac00..d06a12d 100644 --- a/include/ui/input.h +++ b/include/ui/input.h @@ -33,7 +33,9 @@ void qemu_input_handler_bind(QemuInputHandlerState *s, const char *device_id, int head, Error **errp); void qemu_input_event_send(QemuConsole *src, InputEvent *evt); +void qemu_input_event_send_impl(QemuConsole *src, InputEvent *evt); void qemu_input_event_sync(void); +void qemu_input_event_sync_impl(void); InputEvent *qemu_input_event_new_key(KeyValue *key, bool down); void qemu_input_event_send_key(QemuConsole *src, KeyValue *key, bool down); diff --git a/replay/Makefile.objs b/replay/Makefile.objs index 257c320..3936296 100755 --- a/replay/Makefile.objs +++ b/replay/Makefile.objs @@ -2,3 +2,4 @@ obj-y += replay.o obj-y += replay-internal.o obj-y += replay-events.o obj-y += replay-time.o +obj-y += replay-input.o diff --git a/replay/replay-events.c b/replay/replay-events.c index 4da5de0..308186b 100755 --- a/replay/replay-events.c +++ b/replay/replay-events.c @@ -13,6 +13,7 @@ #include "replay.h" #include "replay-internal.h" #include "block/thread-pool.h" +#include "ui/input.h" typedef struct Event { int event_kind; @@ -43,6 +44,16 @@ static void replay_run_event(Event *event) case REPLAY_ASYNC_EVENT_THREAD: thread_pool_work((ThreadPool *)event->opaque, event->opaque2); break; + case REPLAY_ASYNC_EVENT_INPUT: + qemu_input_event_send_impl(NULL, (InputEvent *)event->opaque); + /* Using local variables, when replaying. Do not free them. */ + if (replay_mode == REPLAY_MODE_RECORD) { + qapi_free_InputEvent((InputEvent *)event->opaque); + } + break; + case REPLAY_ASYNC_EVENT_INPUT_SYNC: + qemu_input_event_sync_impl(); + break; default: fprintf(stderr, "Replay: invalid async event ID (%d) in the queue\n", event->event_kind); @@ -136,6 +147,16 @@ void replay_add_thread_event(void *opaque, void *opaque2, uint64_t id) replay_add_event_internal(REPLAY_ASYNC_EVENT_THREAD, opaque, opaque2, id); } +void replay_add_input_event(struct InputEvent *event) +{ + replay_add_event_internal(REPLAY_ASYNC_EVENT_INPUT, event, NULL, 0); +} + +void replay_add_input_sync_event(void) +{ + replay_add_event_internal(REPLAY_ASYNC_EVENT_INPUT_SYNC, NULL, NULL, 0); +} + void replay_save_events(int opt) { qemu_mutex_lock(&lock); @@ -153,6 +174,9 @@ void replay_save_events(int opt) case REPLAY_ASYNC_EVENT_THREAD: replay_put_qword(event->id); break; + case REPLAY_ASYNC_EVENT_INPUT: + replay_save_input_event(event->opaque); + break; } } @@ -178,6 +202,7 @@ void replay_read_events(int opt) break; } /* Execute some events without searching them in the queue */ + Event e; switch (read_event_kind) { case REPLAY_ASYNC_EVENT_BH: case REPLAY_ASYNC_EVENT_THREAD: @@ -185,6 +210,29 @@ void replay_read_events(int opt) read_id = replay_get_qword(); } break; + case REPLAY_ASYNC_EVENT_INPUT: + e.event_kind = read_event_kind; + e.opaque = replay_read_input_event(); + + replay_run_event(&e); + + replay_has_unread_data = 0; + read_event_kind = -1; + read_opt = -1; + replay_fetch_data_kind(); + /* continue with the next event */ + continue; + case REPLAY_ASYNC_EVENT_INPUT_SYNC: + e.event_kind = read_event_kind; + e.opaque = 0; + replay_run_event(&e); + + replay_has_unread_data = 0; + read_event_kind = -1; + read_opt = -1; + replay_fetch_data_kind(); + /* continue with the next event */ + continue; default: fprintf(stderr, "Unknown ID %d of replay event\n", read_event_kind); exit(1); diff --git a/replay/replay-input.c b/replay/replay-input.c new file mode 100755 index 0000000..f5d1482 --- /dev/null +++ b/replay/replay-input.c @@ -0,0 +1,108 @@ +/* + * replay-input.c + * + * Copyright (c) 2010-2014 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. + * + */ + +#include "qemu-common.h" +#include "replay.h" +#include "replay-internal.h" +#include "ui/input.h" + +void replay_save_input_event(InputEvent *evt) +{ + replay_put_dword(evt->kind); + + switch (evt->kind) { + case INPUT_EVENT_KIND_KEY: + replay_put_dword(evt->key->key->kind); + + switch (evt->key->key->kind) { + case KEY_VALUE_KIND_NUMBER: + replay_put_qword(evt->key->key->number); + replay_put_byte(evt->key->down); + break; + case KEY_VALUE_KIND_QCODE: + replay_put_dword(evt->key->key->qcode); + replay_put_byte(evt->key->down); + break; + case KEY_VALUE_KIND_MAX: + /* keep gcc happy */ + break; + } + break; + case INPUT_EVENT_KIND_BTN: + replay_put_dword(evt->btn->button); + replay_put_byte(evt->btn->down); + break; + case INPUT_EVENT_KIND_REL: + replay_put_dword(evt->rel->axis); + replay_put_qword(evt->rel->value); + break; + case INPUT_EVENT_KIND_ABS: + replay_put_dword(evt->abs->axis); + replay_put_qword(evt->abs->value); + break; + case INPUT_EVENT_KIND_MAX: + /* keep gcc happy */ + break; + } +} + +InputEvent *replay_read_input_event(void) +{ + static InputEvent evt; + static KeyValue keyValue; + static InputKeyEvent key; + key.key = &keyValue; + static InputBtnEvent btn; + static InputMoveEvent rel; + static InputMoveEvent abs; + + evt.kind = replay_get_dword(); + switch (evt.kind) { + case INPUT_EVENT_KIND_KEY: + evt.key = &key; + evt.key->key->kind = replay_get_dword(); + + switch (evt.key->key->kind) { + case KEY_VALUE_KIND_NUMBER: + evt.key->key->number = replay_get_qword(); + evt.key->down = replay_get_byte(); + break; + case KEY_VALUE_KIND_QCODE: + evt.key->key->qcode = (QKeyCode)replay_get_dword(); + evt.key->down = replay_get_byte(); + break; + case KEY_VALUE_KIND_MAX: + /* keep gcc happy */ + break; + } + break; + case INPUT_EVENT_KIND_BTN: + evt.btn = &btn; + evt.btn->button = (InputButton)replay_get_dword(); + evt.btn->down = replay_get_byte(); + break; + case INPUT_EVENT_KIND_REL: + evt.rel = &rel; + evt.rel->axis = (InputAxis)replay_get_dword(); + evt.rel->value = replay_get_qword(); + break; + case INPUT_EVENT_KIND_ABS: + evt.abs = &abs; + evt.abs->axis = (InputAxis)replay_get_dword(); + evt.abs->value = replay_get_qword(); + break; + case INPUT_EVENT_KIND_MAX: + /* keep gcc happy */ + break; + } + + return &evt; +} diff --git a/replay/replay-internal.h b/replay/replay-internal.h index 142e09a..ac3ba82 100755 --- a/replay/replay-internal.h +++ b/replay/replay-internal.h @@ -41,7 +41,9 @@ #define REPLAY_ASYNC_EVENT_BH 0 #define REPLAY_ASYNC_EVENT_THREAD 1 -#define REPLAY_ASYNC_COUNT 2 +#define REPLAY_ASYNC_EVENT_INPUT 2 +#define REPLAY_ASYNC_EVENT_INPUT_SYNC 3 +#define REPLAY_ASYNC_COUNT 4 typedef struct ReplayState { /*! Cached clock values. */ @@ -120,4 +122,11 @@ void replay_read_events(int opt); /*! Adds specified async event to the queue */ void replay_add_event(int event_id, void *opaque); +/* Input events */ + +/*! Saves input event to the log */ +void replay_save_input_event(InputEvent *evt); +/*! Reads input event from the log */ +InputEvent *replay_read_input_event(void); + #endif diff --git a/replay/replay.h b/replay/replay.h index 9c40648..f8bf4c7 100755 --- a/replay/replay.h +++ b/replay/replay.h @@ -18,6 +18,7 @@ #include "qapi-types.h" struct QemuOpts; +struct InputEvent; /* replay clock kinds */ /* rdtsc */ @@ -110,5 +111,9 @@ void replay_disable_events(void); void replay_add_bh_event(void *bh, uint64_t id); /*! Adds thread event to the queue */ void replay_add_thread_event(void *pool, void *req, uint64_t id); +/*! Adds input event to the queue */ +void replay_add_input_event(struct InputEvent *event); +/*! Adds input sync event to the queue */ +void replay_add_input_sync_event(void); #endif diff --git a/ui/input.c b/ui/input.c index 002831e..4e49fe5 100644 --- a/ui/input.c +++ b/ui/input.c @@ -5,6 +5,7 @@ #include "trace.h" #include "ui/input.h" #include "ui/console.h" +#include "replay/replay.h" struct QemuInputHandlerState { DeviceState *dev; @@ -295,14 +296,10 @@ static void qemu_input_queue_sync(struct QemuInputEventQueueHead *queue) QTAILQ_INSERT_TAIL(queue, item, node); } -void qemu_input_event_send(QemuConsole *src, InputEvent *evt) +void qemu_input_event_send_impl(QemuConsole *src, InputEvent *evt) { QemuInputHandlerState *s; - if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) { - return; - } - qemu_input_event_trace(src, evt); /* pre processing */ @@ -319,14 +316,25 @@ void qemu_input_event_send(QemuConsole *src, InputEvent *evt) s->events++; } -void qemu_input_event_sync(void) +void qemu_input_event_send(QemuConsole *src, InputEvent *evt) { - QemuInputHandlerState *s; - if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) { return; } + if (replay_mode == REPLAY_MODE_PLAY) { + /* Nothing */ + } else if (replay_mode == REPLAY_MODE_RECORD) { + replay_add_input_event(evt); + } else { + qemu_input_event_send_impl(src, evt); + } +} + +void qemu_input_event_sync_impl(void) +{ + QemuInputHandlerState *s; + trace_input_event_sync(); QTAILQ_FOREACH(s, &handlers, node) { @@ -340,6 +348,21 @@ void qemu_input_event_sync(void) } } +void qemu_input_event_sync(void) +{ + if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) { + return; + } + + 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(); + } +} + InputEvent *qemu_input_event_new_key(KeyValue *key, bool down) { InputEvent *evt = g_new0(InputEvent, 1); @@ -353,14 +376,23 @@ InputEvent *qemu_input_event_new_key(KeyValue *key, bool down) void qemu_input_event_send_key(QemuConsole *src, KeyValue *key, bool down) { InputEvent *evt; - evt = qemu_input_event_new_key(key, down); - 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); + if (replay_mode != REPLAY_MODE_PLAY) { + evt = qemu_input_event_new_key(key, down); + if (QTAILQ_EMPTY(&kbd_queue)) { + qemu_input_event_send(src, evt); + qemu_input_event_sync(); + if (replay_mode != REPLAY_MODE_RECORD) { + qapi_free_InputEvent(evt); + } + } else { + if (replay_mode != REPLAY_MODE_NONE) { + fprintf(stderr, "Input queue is not supported " + "in record/replay mode\n"); + exit(1); + } + qemu_input_queue_event(&kbd_queue, src, evt); + qemu_input_queue_sync(&kbd_queue); + } } } @@ -386,6 +418,10 @@ void qemu_input_event_send_key_delay(uint32_t delay_ms) kbd_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, qemu_input_queue_process, &kbd_queue); } + if (replay_mode != REPLAY_MODE_NONE) { + fprintf(stderr, "Input queue is not supported in record/replay mode\n"); + exit(1); + } qemu_input_queue_delay(&kbd_queue, kbd_timer, delay_ms ? delay_ms : kbd_default_delay_ms); } @@ -405,7 +441,9 @@ 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); + if (replay_mode != REPLAY_MODE_RECORD) { + qapi_free_InputEvent(evt); + } } void qemu_input_update_buttons(QemuConsole *src, uint32_t *button_map, @@ -458,7 +496,9 @@ 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); + if (replay_mode != REPLAY_MODE_RECORD) { + qapi_free_InputEvent(evt); + } } void qemu_input_queue_abs(QemuConsole *src, InputAxis axis, int value, int size) @@ -467,7 +507,9 @@ void qemu_input_queue_abs(QemuConsole *src, InputAxis axis, int value, int size) 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); + if (replay_mode != REPLAY_MODE_RECORD) { + qapi_free_InputEvent(evt); + } } void qemu_input_check_mode_change(void)