xref: /openbmc/qemu/replay/replay-input.c (revision b917da4c)
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     InputKeyEvent *key;
51     InputBtnEvent *btn;
52     InputMoveEvent *move;
53     replay_put_dword(evt->type);
54 
55     switch (evt->type) {
56     case INPUT_EVENT_KIND_KEY:
57         key = evt->u.key;
58         replay_put_dword(key->key->type);
59 
60         switch (key->key->type) {
61         case KEY_VALUE_KIND_NUMBER:
62             replay_put_qword(key->key->u.number);
63             replay_put_byte(key->down);
64             break;
65         case KEY_VALUE_KIND_QCODE:
66             replay_put_dword(key->key->u.qcode);
67             replay_put_byte(key->down);
68             break;
69         case KEY_VALUE_KIND__MAX:
70             /* keep gcc happy */
71             break;
72         }
73         break;
74     case INPUT_EVENT_KIND_BTN:
75         btn = evt->u.btn;
76         replay_put_dword(btn->button);
77         replay_put_byte(btn->down);
78         break;
79     case INPUT_EVENT_KIND_REL:
80         move = evt->u.rel;
81         replay_put_dword(move->axis);
82         replay_put_qword(move->value);
83         break;
84     case INPUT_EVENT_KIND_ABS:
85         move = evt->u.abs;
86         replay_put_dword(move->axis);
87         replay_put_qword(move->value);
88         break;
89     case INPUT_EVENT_KIND__MAX:
90         /* keep gcc happy */
91         break;
92     }
93 }
94 
95 InputEvent *replay_read_input_event(void)
96 {
97     InputEvent evt;
98     KeyValue keyValue;
99     InputKeyEvent key;
100     key.key = &keyValue;
101     InputBtnEvent btn;
102     InputMoveEvent rel;
103     InputMoveEvent abs;
104 
105     evt.type = replay_get_dword();
106     switch (evt.type) {
107     case INPUT_EVENT_KIND_KEY:
108         evt.u.key = &key;
109         evt.u.key->key->type = replay_get_dword();
110 
111         switch (evt.u.key->key->type) {
112         case KEY_VALUE_KIND_NUMBER:
113             evt.u.key->key->u.number = replay_get_qword();
114             evt.u.key->down = replay_get_byte();
115             break;
116         case KEY_VALUE_KIND_QCODE:
117             evt.u.key->key->u.qcode = (QKeyCode)replay_get_dword();
118             evt.u.key->down = replay_get_byte();
119             break;
120         case KEY_VALUE_KIND__MAX:
121             /* keep gcc happy */
122             break;
123         }
124         break;
125     case INPUT_EVENT_KIND_BTN:
126         evt.u.btn = &btn;
127         evt.u.btn->button = (InputButton)replay_get_dword();
128         evt.u.btn->down = replay_get_byte();
129         break;
130     case INPUT_EVENT_KIND_REL:
131         evt.u.rel = &rel;
132         evt.u.rel->axis = (InputAxis)replay_get_dword();
133         evt.u.rel->value = replay_get_qword();
134         break;
135     case INPUT_EVENT_KIND_ABS:
136         evt.u.abs = &abs;
137         evt.u.abs->axis = (InputAxis)replay_get_dword();
138         evt.u.abs->value = replay_get_qword();
139         break;
140     case INPUT_EVENT_KIND__MAX:
141         /* keep gcc happy */
142         break;
143     }
144 
145     return qapi_clone_InputEvent(&evt);
146 }
147 
148 void replay_input_event(QemuConsole *src, InputEvent *evt)
149 {
150     if (replay_mode == REPLAY_MODE_PLAY) {
151         /* Nothing */
152     } else if (replay_mode == REPLAY_MODE_RECORD) {
153         replay_add_input_event(qapi_clone_InputEvent(evt));
154     } else {
155         qemu_input_event_send_impl(src, evt);
156     }
157 }
158 
159 void replay_input_sync_event(void)
160 {
161     if (replay_mode == REPLAY_MODE_PLAY) {
162         /* Nothing */
163     } else if (replay_mode == REPLAY_MODE_RECORD) {
164         replay_add_input_sync_event();
165     } else {
166         qemu_input_event_sync_impl();
167     }
168 }
169