1 /* 2 * replay-input.c 3 * 4 * Copyright (c) 2010-2015 Institute for System Programming 5 * of the Russian Academy of Sciences. 6 * 7 * This work is licensed under the terms of the GNU GPL, version 2 or later. 8 * See the COPYING file in the top-level directory. 9 * 10 */ 11 12 #include "qemu/osdep.h" 13 #include "qemu-common.h" 14 #include "sysemu/replay.h" 15 #include "replay-internal.h" 16 #include "qemu/notify.h" 17 #include "ui/input.h" 18 #include "qapi/qmp-output-visitor.h" 19 #include "qapi/qmp-input-visitor.h" 20 #include "qapi-visit.h" 21 22 static InputEvent *qapi_clone_InputEvent(InputEvent *src) 23 { 24 QmpOutputVisitor *qov; 25 QmpInputVisitor *qiv; 26 Visitor *ov, *iv; 27 QObject *obj; 28 InputEvent *dst = NULL; 29 30 qov = qmp_output_visitor_new(); 31 ov = qmp_output_get_visitor(qov); 32 visit_type_InputEvent(ov, NULL, &src, &error_abort); 33 obj = qmp_output_get_qobject(qov); 34 qmp_output_visitor_cleanup(qov); 35 if (!obj) { 36 return NULL; 37 } 38 39 qiv = qmp_input_visitor_new(obj); 40 iv = qmp_input_get_visitor(qiv); 41 visit_type_InputEvent(iv, NULL, &dst, &error_abort); 42 qmp_input_visitor_cleanup(qiv); 43 qobject_decref(obj); 44 45 return dst; 46 } 47 48 void replay_save_input_event(InputEvent *evt) 49 { 50 replay_put_dword(evt->type); 51 52 switch (evt->type) { 53 case INPUT_EVENT_KIND_KEY: 54 replay_put_dword(evt->u.key->key->type); 55 56 switch (evt->u.key->key->type) { 57 case KEY_VALUE_KIND_NUMBER: 58 replay_put_qword(evt->u.key->key->u.number); 59 replay_put_byte(evt->u.key->down); 60 break; 61 case KEY_VALUE_KIND_QCODE: 62 replay_put_dword(evt->u.key->key->u.qcode); 63 replay_put_byte(evt->u.key->down); 64 break; 65 case KEY_VALUE_KIND__MAX: 66 /* keep gcc happy */ 67 break; 68 } 69 break; 70 case INPUT_EVENT_KIND_BTN: 71 replay_put_dword(evt->u.btn->button); 72 replay_put_byte(evt->u.btn->down); 73 break; 74 case INPUT_EVENT_KIND_REL: 75 replay_put_dword(evt->u.rel->axis); 76 replay_put_qword(evt->u.rel->value); 77 break; 78 case INPUT_EVENT_KIND_ABS: 79 replay_put_dword(evt->u.abs->axis); 80 replay_put_qword(evt->u.abs->value); 81 break; 82 case INPUT_EVENT_KIND__MAX: 83 /* keep gcc happy */ 84 break; 85 } 86 } 87 88 InputEvent *replay_read_input_event(void) 89 { 90 InputEvent evt; 91 KeyValue keyValue; 92 InputKeyEvent key; 93 key.key = &keyValue; 94 InputBtnEvent btn; 95 InputMoveEvent rel; 96 InputMoveEvent abs; 97 98 evt.type = replay_get_dword(); 99 switch (evt.type) { 100 case INPUT_EVENT_KIND_KEY: 101 evt.u.key = &key; 102 evt.u.key->key->type = replay_get_dword(); 103 104 switch (evt.u.key->key->type) { 105 case KEY_VALUE_KIND_NUMBER: 106 evt.u.key->key->u.number = replay_get_qword(); 107 evt.u.key->down = replay_get_byte(); 108 break; 109 case KEY_VALUE_KIND_QCODE: 110 evt.u.key->key->u.qcode = (QKeyCode)replay_get_dword(); 111 evt.u.key->down = replay_get_byte(); 112 break; 113 case KEY_VALUE_KIND__MAX: 114 /* keep gcc happy */ 115 break; 116 } 117 break; 118 case INPUT_EVENT_KIND_BTN: 119 evt.u.btn = &btn; 120 evt.u.btn->button = (InputButton)replay_get_dword(); 121 evt.u.btn->down = replay_get_byte(); 122 break; 123 case INPUT_EVENT_KIND_REL: 124 evt.u.rel = &rel; 125 evt.u.rel->axis = (InputAxis)replay_get_dword(); 126 evt.u.rel->value = replay_get_qword(); 127 break; 128 case INPUT_EVENT_KIND_ABS: 129 evt.u.abs = &abs; 130 evt.u.abs->axis = (InputAxis)replay_get_dword(); 131 evt.u.abs->value = replay_get_qword(); 132 break; 133 case INPUT_EVENT_KIND__MAX: 134 /* keep gcc happy */ 135 break; 136 } 137 138 return qapi_clone_InputEvent(&evt); 139 } 140 141 void replay_input_event(QemuConsole *src, InputEvent *evt) 142 { 143 if (replay_mode == REPLAY_MODE_PLAY) { 144 /* Nothing */ 145 } else if (replay_mode == REPLAY_MODE_RECORD) { 146 replay_add_input_event(qapi_clone_InputEvent(evt)); 147 } else { 148 qemu_input_event_send_impl(src, evt); 149 } 150 } 151 152 void replay_input_sync_event(void) 153 { 154 if (replay_mode == REPLAY_MODE_PLAY) { 155 /* Nothing */ 156 } else if (replay_mode == REPLAY_MODE_RECORD) { 157 replay_add_input_sync_event(); 158 } else { 159 qemu_input_event_sync_impl(); 160 } 161 } 162