Peter Maydell | e16f4c8 | 2016-01-29 17:49:51 +0000 | [diff] [blame] | 1 | #include "qemu/osdep.h" |
Gerd Hoffmann | c8b405b | 2013-11-27 10:35:26 +0100 | [diff] [blame] | 2 | #include "sysemu/sysemu.h" |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 3 | #include "qapi/error.h" |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 4 | #include "qapi/qapi-commands-ui.h" |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 5 | #include "qapi/qmp/qdict.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 6 | #include "qemu/error-report.h" |
Gerd Hoffmann | c43ce55 | 2013-12-04 15:20:05 +0100 | [diff] [blame] | 7 | #include "trace.h" |
Gerd Hoffmann | c8b405b | 2013-11-27 10:35:26 +0100 | [diff] [blame] | 8 | #include "ui/input.h" |
Gerd Hoffmann | c43ce55 | 2013-12-04 15:20:05 +0100 | [diff] [blame] | 9 | #include "ui/console.h" |
Pavel Dovgalyuk | ee31299 | 2015-09-17 19:25:24 +0300 | [diff] [blame] | 10 | #include "sysemu/replay.h" |
Gerd Hoffmann | c8b405b | 2013-11-27 10:35:26 +0100 | [diff] [blame] | 11 | |
| 12 | struct QemuInputHandlerState { |
| 13 | DeviceState *dev; |
| 14 | QemuInputHandler *handler; |
| 15 | int id; |
| 16 | int events; |
Gerd Hoffmann | 6f5943c | 2014-05-19 15:18:37 +0200 | [diff] [blame] | 17 | QemuConsole *con; |
Gerd Hoffmann | c8b405b | 2013-11-27 10:35:26 +0100 | [diff] [blame] | 18 | QTAILQ_ENTRY(QemuInputHandlerState) node; |
| 19 | }; |
Gerd Hoffmann | be1a717 | 2014-05-28 13:02:40 +0200 | [diff] [blame] | 20 | |
| 21 | typedef struct QemuInputEventQueue QemuInputEventQueue; |
| 22 | struct QemuInputEventQueue { |
| 23 | enum { |
| 24 | QEMU_INPUT_QUEUE_DELAY = 1, |
| 25 | QEMU_INPUT_QUEUE_EVENT, |
| 26 | QEMU_INPUT_QUEUE_SYNC, |
| 27 | } type; |
| 28 | QEMUTimer *timer; |
| 29 | uint32_t delay_ms; |
| 30 | QemuConsole *src; |
| 31 | InputEvent *evt; |
| 32 | QTAILQ_ENTRY(QemuInputEventQueue) node; |
| 33 | }; |
| 34 | |
Gerd Hoffmann | c8b405b | 2013-11-27 10:35:26 +0100 | [diff] [blame] | 35 | static QTAILQ_HEAD(, QemuInputHandlerState) handlers = |
| 36 | QTAILQ_HEAD_INITIALIZER(handlers); |
Gerd Hoffmann | 4a33f45 | 2013-12-05 11:23:42 +0100 | [diff] [blame] | 37 | static NotifierList mouse_mode_notifiers = |
| 38 | NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers); |
Gerd Hoffmann | c8b405b | 2013-11-27 10:35:26 +0100 | [diff] [blame] | 39 | |
Gerd Hoffmann | be1a717 | 2014-05-28 13:02:40 +0200 | [diff] [blame] | 40 | static QTAILQ_HEAD(QemuInputEventQueueHead, QemuInputEventQueue) kbd_queue = |
| 41 | QTAILQ_HEAD_INITIALIZER(kbd_queue); |
| 42 | static QEMUTimer *kbd_timer; |
| 43 | static uint32_t kbd_default_delay_ms = 10; |
Gerd Hoffmann | fa18f36 | 2017-04-28 10:42:37 +0200 | [diff] [blame] | 44 | static uint32_t queue_count; |
| 45 | static uint32_t queue_limit = 1024; |
Gerd Hoffmann | be1a717 | 2014-05-28 13:02:40 +0200 | [diff] [blame] | 46 | |
Gerd Hoffmann | c8b405b | 2013-11-27 10:35:26 +0100 | [diff] [blame] | 47 | QemuInputHandlerState *qemu_input_handler_register(DeviceState *dev, |
| 48 | QemuInputHandler *handler) |
| 49 | { |
| 50 | QemuInputHandlerState *s = g_new0(QemuInputHandlerState, 1); |
| 51 | static int id = 1; |
| 52 | |
| 53 | s->dev = dev; |
| 54 | s->handler = handler; |
| 55 | s->id = id++; |
| 56 | QTAILQ_INSERT_TAIL(&handlers, s, node); |
Gerd Hoffmann | 4a33f45 | 2013-12-05 11:23:42 +0100 | [diff] [blame] | 57 | |
| 58 | qemu_input_check_mode_change(); |
Gerd Hoffmann | c8b405b | 2013-11-27 10:35:26 +0100 | [diff] [blame] | 59 | return s; |
| 60 | } |
| 61 | |
| 62 | void qemu_input_handler_activate(QemuInputHandlerState *s) |
| 63 | { |
| 64 | QTAILQ_REMOVE(&handlers, s, node); |
| 65 | QTAILQ_INSERT_HEAD(&handlers, s, node); |
Gerd Hoffmann | 4a33f45 | 2013-12-05 11:23:42 +0100 | [diff] [blame] | 66 | qemu_input_check_mode_change(); |
Gerd Hoffmann | c8b405b | 2013-11-27 10:35:26 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Gerd Hoffmann | 528728f | 2014-03-16 00:38:45 +0100 | [diff] [blame] | 69 | void qemu_input_handler_deactivate(QemuInputHandlerState *s) |
| 70 | { |
| 71 | QTAILQ_REMOVE(&handlers, s, node); |
| 72 | QTAILQ_INSERT_TAIL(&handlers, s, node); |
| 73 | qemu_input_check_mode_change(); |
| 74 | } |
| 75 | |
Gerd Hoffmann | c8b405b | 2013-11-27 10:35:26 +0100 | [diff] [blame] | 76 | void qemu_input_handler_unregister(QemuInputHandlerState *s) |
| 77 | { |
| 78 | QTAILQ_REMOVE(&handlers, s, node); |
| 79 | g_free(s); |
Gerd Hoffmann | 4a33f45 | 2013-12-05 11:23:42 +0100 | [diff] [blame] | 80 | qemu_input_check_mode_change(); |
Gerd Hoffmann | c8b405b | 2013-11-27 10:35:26 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Gerd Hoffmann | 6f5943c | 2014-05-19 15:18:37 +0200 | [diff] [blame] | 83 | void qemu_input_handler_bind(QemuInputHandlerState *s, |
| 84 | const char *device_id, int head, |
| 85 | Error **errp) |
| 86 | { |
Gerd Hoffmann | 6f5943c | 2014-05-19 15:18:37 +0200 | [diff] [blame] | 87 | QemuConsole *con; |
Gerd Hoffmann | f2c1d54 | 2016-01-12 11:45:43 +0100 | [diff] [blame] | 88 | Error *err = NULL; |
Gerd Hoffmann | 6f5943c | 2014-05-19 15:18:37 +0200 | [diff] [blame] | 89 | |
Gerd Hoffmann | f2c1d54 | 2016-01-12 11:45:43 +0100 | [diff] [blame] | 90 | con = qemu_console_lookup_by_device_name(device_id, head, &err); |
| 91 | if (err) { |
| 92 | error_propagate(errp, err); |
Gerd Hoffmann | 6f5943c | 2014-05-19 15:18:37 +0200 | [diff] [blame] | 93 | return; |
| 94 | } |
| 95 | |
| 96 | s->con = con; |
| 97 | } |
| 98 | |
Gerd Hoffmann | c8b405b | 2013-11-27 10:35:26 +0100 | [diff] [blame] | 99 | static QemuInputHandlerState* |
Gerd Hoffmann | 6f5943c | 2014-05-19 15:18:37 +0200 | [diff] [blame] | 100 | qemu_input_find_handler(uint32_t mask, QemuConsole *con) |
Gerd Hoffmann | c8b405b | 2013-11-27 10:35:26 +0100 | [diff] [blame] | 101 | { |
| 102 | QemuInputHandlerState *s; |
| 103 | |
| 104 | QTAILQ_FOREACH(s, &handlers, node) { |
Gerd Hoffmann | 6f5943c | 2014-05-19 15:18:37 +0200 | [diff] [blame] | 105 | if (s->con == NULL || s->con != con) { |
| 106 | continue; |
| 107 | } |
| 108 | if (mask & s->handler->mask) { |
| 109 | return s; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | QTAILQ_FOREACH(s, &handlers, node) { |
| 114 | if (s->con != NULL) { |
| 115 | continue; |
| 116 | } |
Gerd Hoffmann | c8b405b | 2013-11-27 10:35:26 +0100 | [diff] [blame] | 117 | if (mask & s->handler->mask) { |
| 118 | return s; |
| 119 | } |
| 120 | } |
| 121 | return NULL; |
| 122 | } |
| 123 | |
Gerd Hoffmann | 6575ccd | 2016-01-12 12:39:33 +0100 | [diff] [blame] | 124 | void qmp_input_send_event(bool has_device, const char *device, |
| 125 | bool has_head, int64_t head, |
| 126 | InputEventList *events, Error **errp) |
Marcelo Tosatti | 50c6617 | 2014-09-30 18:10:17 -0300 | [diff] [blame] | 127 | { |
| 128 | InputEventList *e; |
| 129 | QemuConsole *con; |
Gerd Hoffmann | b98d26e | 2016-01-12 12:11:14 +0100 | [diff] [blame] | 130 | Error *err = NULL; |
Marcelo Tosatti | 50c6617 | 2014-09-30 18:10:17 -0300 | [diff] [blame] | 131 | |
Amos Kong | 51fc447 | 2014-11-07 12:41:25 +0800 | [diff] [blame] | 132 | con = NULL; |
Gerd Hoffmann | b98d26e | 2016-01-12 12:11:14 +0100 | [diff] [blame] | 133 | if (has_device) { |
| 134 | if (!has_head) { |
| 135 | head = 0; |
| 136 | } |
| 137 | con = qemu_console_lookup_by_device_name(device, head, &err); |
| 138 | if (err) { |
| 139 | error_propagate(errp, err); |
Amos Kong | 51fc447 | 2014-11-07 12:41:25 +0800 | [diff] [blame] | 140 | return; |
| 141 | } |
Marcelo Tosatti | 50c6617 | 2014-09-30 18:10:17 -0300 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) { |
| 145 | error_setg(errp, "VM not running"); |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | for (e = events; e != NULL; e = e->next) { |
| 150 | InputEvent *event = e->value; |
| 151 | |
Eric Blake | 568c73a | 2015-10-26 16:34:58 -0600 | [diff] [blame] | 152 | if (!qemu_input_find_handler(1 << event->type, con)) { |
Marcelo Tosatti | 50c6617 | 2014-09-30 18:10:17 -0300 | [diff] [blame] | 153 | error_setg(errp, "Input handler not found for " |
| 154 | "event type %s", |
Markus Armbruster | 977c736 | 2017-08-24 10:46:08 +0200 | [diff] [blame] | 155 | InputEventKind_str(event->type)); |
Marcelo Tosatti | 50c6617 | 2014-09-30 18:10:17 -0300 | [diff] [blame] | 156 | return; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | for (e = events; e != NULL; e = e->next) { |
Daniel P. Berrange | af07e5f | 2017-09-29 11:12:00 +0100 | [diff] [blame] | 161 | InputEvent *evt = e->value; |
Marcelo Tosatti | 50c6617 | 2014-09-30 18:10:17 -0300 | [diff] [blame] | 162 | |
Daniel P. Berrange | af07e5f | 2017-09-29 11:12:00 +0100 | [diff] [blame] | 163 | if (evt->type == INPUT_EVENT_KIND_KEY && |
| 164 | evt->u.key.data->key->type == KEY_VALUE_KIND_NUMBER) { |
| 165 | KeyValue *key = evt->u.key.data->key; |
Daniel P. Berrange | 237925b | 2017-10-19 15:28:42 +0100 | [diff] [blame] | 166 | QKeyCode code = qemu_input_key_number_to_qcode(key->u.number.data); |
Daniel P. Berrange | af07e5f | 2017-09-29 11:12:00 +0100 | [diff] [blame] | 167 | qemu_input_event_send_key_qcode(con, code, evt->u.key.data->down); |
| 168 | } else { |
| 169 | qemu_input_event_send(con, evt); |
| 170 | } |
Marcelo Tosatti | 50c6617 | 2014-09-30 18:10:17 -0300 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | qemu_input_event_sync(); |
| 174 | } |
| 175 | |
Philippe Voinov | 9cfa7ab | 2017-05-05 15:39:52 +0200 | [diff] [blame] | 176 | static int qemu_input_transform_invert_abs_value(int value) |
| 177 | { |
| 178 | return (int64_t)INPUT_EVENT_ABS_MAX - value + INPUT_EVENT_ABS_MIN; |
| 179 | } |
| 180 | |
Gerd Hoffmann | d353543 | 2013-11-28 11:29:33 +0100 | [diff] [blame] | 181 | static void qemu_input_transform_abs_rotate(InputEvent *evt) |
| 182 | { |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 183 | InputMoveEvent *move = evt->u.abs.data; |
Gerd Hoffmann | d353543 | 2013-11-28 11:29:33 +0100 | [diff] [blame] | 184 | switch (graphic_rotate) { |
| 185 | case 90: |
Eric Blake | b5a1b44 | 2016-03-03 09:16:49 -0700 | [diff] [blame] | 186 | if (move->axis == INPUT_AXIS_X) { |
| 187 | move->axis = INPUT_AXIS_Y; |
| 188 | } else if (move->axis == INPUT_AXIS_Y) { |
| 189 | move->axis = INPUT_AXIS_X; |
Philippe Voinov | 9cfa7ab | 2017-05-05 15:39:52 +0200 | [diff] [blame] | 190 | move->value = qemu_input_transform_invert_abs_value(move->value); |
Gerd Hoffmann | d353543 | 2013-11-28 11:29:33 +0100 | [diff] [blame] | 191 | } |
| 192 | break; |
| 193 | case 180: |
Philippe Voinov | 9cfa7ab | 2017-05-05 15:39:52 +0200 | [diff] [blame] | 194 | move->value = qemu_input_transform_invert_abs_value(move->value); |
Gerd Hoffmann | d353543 | 2013-11-28 11:29:33 +0100 | [diff] [blame] | 195 | break; |
| 196 | case 270: |
Eric Blake | b5a1b44 | 2016-03-03 09:16:49 -0700 | [diff] [blame] | 197 | if (move->axis == INPUT_AXIS_X) { |
| 198 | move->axis = INPUT_AXIS_Y; |
Philippe Voinov | 9cfa7ab | 2017-05-05 15:39:52 +0200 | [diff] [blame] | 199 | move->value = qemu_input_transform_invert_abs_value(move->value); |
Eric Blake | b5a1b44 | 2016-03-03 09:16:49 -0700 | [diff] [blame] | 200 | } else if (move->axis == INPUT_AXIS_Y) { |
| 201 | move->axis = INPUT_AXIS_X; |
Gerd Hoffmann | d353543 | 2013-11-28 11:29:33 +0100 | [diff] [blame] | 202 | } |
| 203 | break; |
| 204 | } |
| 205 | } |
| 206 | |
Gerd Hoffmann | c43ce55 | 2013-12-04 15:20:05 +0100 | [diff] [blame] | 207 | static void qemu_input_event_trace(QemuConsole *src, InputEvent *evt) |
| 208 | { |
| 209 | const char *name; |
Gerd Hoffmann | 2386a90 | 2014-05-21 13:49:59 +0200 | [diff] [blame] | 210 | int qcode, idx = -1; |
Eric Blake | b5a1b44 | 2016-03-03 09:16:49 -0700 | [diff] [blame] | 211 | InputKeyEvent *key; |
| 212 | InputBtnEvent *btn; |
| 213 | InputMoveEvent *move; |
Gerd Hoffmann | c43ce55 | 2013-12-04 15:20:05 +0100 | [diff] [blame] | 214 | |
| 215 | if (src) { |
| 216 | idx = qemu_console_get_index(src); |
| 217 | } |
Eric Blake | 568c73a | 2015-10-26 16:34:58 -0600 | [diff] [blame] | 218 | switch (evt->type) { |
Gerd Hoffmann | c43ce55 | 2013-12-04 15:20:05 +0100 | [diff] [blame] | 219 | case INPUT_EVENT_KIND_KEY: |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 220 | key = evt->u.key.data; |
Eric Blake | b5a1b44 | 2016-03-03 09:16:49 -0700 | [diff] [blame] | 221 | switch (key->key->type) { |
Gerd Hoffmann | c43ce55 | 2013-12-04 15:20:05 +0100 | [diff] [blame] | 222 | case KEY_VALUE_KIND_NUMBER: |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 223 | qcode = qemu_input_key_number_to_qcode(key->key->u.number.data); |
Markus Armbruster | 977c736 | 2017-08-24 10:46:08 +0200 | [diff] [blame] | 224 | name = QKeyCode_str(qcode); |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 225 | trace_input_event_key_number(idx, key->key->u.number.data, |
Eric Blake | b5a1b44 | 2016-03-03 09:16:49 -0700 | [diff] [blame] | 226 | name, key->down); |
Gerd Hoffmann | c43ce55 | 2013-12-04 15:20:05 +0100 | [diff] [blame] | 227 | break; |
| 228 | case KEY_VALUE_KIND_QCODE: |
Markus Armbruster | 977c736 | 2017-08-24 10:46:08 +0200 | [diff] [blame] | 229 | name = QKeyCode_str(key->key->u.qcode.data); |
Eric Blake | b5a1b44 | 2016-03-03 09:16:49 -0700 | [diff] [blame] | 230 | trace_input_event_key_qcode(idx, name, key->down); |
Gerd Hoffmann | c43ce55 | 2013-12-04 15:20:05 +0100 | [diff] [blame] | 231 | break; |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 232 | case KEY_VALUE_KIND__MAX: |
Gerd Hoffmann | c43ce55 | 2013-12-04 15:20:05 +0100 | [diff] [blame] | 233 | /* keep gcc happy */ |
| 234 | break; |
| 235 | } |
| 236 | break; |
| 237 | case INPUT_EVENT_KIND_BTN: |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 238 | btn = evt->u.btn.data; |
Markus Armbruster | 977c736 | 2017-08-24 10:46:08 +0200 | [diff] [blame] | 239 | name = InputButton_str(btn->button); |
Eric Blake | b5a1b44 | 2016-03-03 09:16:49 -0700 | [diff] [blame] | 240 | trace_input_event_btn(idx, name, btn->down); |
Gerd Hoffmann | c43ce55 | 2013-12-04 15:20:05 +0100 | [diff] [blame] | 241 | break; |
| 242 | case INPUT_EVENT_KIND_REL: |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 243 | move = evt->u.rel.data; |
Markus Armbruster | 977c736 | 2017-08-24 10:46:08 +0200 | [diff] [blame] | 244 | name = InputAxis_str(move->axis); |
Eric Blake | b5a1b44 | 2016-03-03 09:16:49 -0700 | [diff] [blame] | 245 | trace_input_event_rel(idx, name, move->value); |
Gerd Hoffmann | c43ce55 | 2013-12-04 15:20:05 +0100 | [diff] [blame] | 246 | break; |
| 247 | case INPUT_EVENT_KIND_ABS: |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 248 | move = evt->u.abs.data; |
Markus Armbruster | 977c736 | 2017-08-24 10:46:08 +0200 | [diff] [blame] | 249 | name = InputAxis_str(move->axis); |
Eric Blake | b5a1b44 | 2016-03-03 09:16:49 -0700 | [diff] [blame] | 250 | trace_input_event_abs(idx, name, move->value); |
Gerd Hoffmann | c43ce55 | 2013-12-04 15:20:05 +0100 | [diff] [blame] | 251 | break; |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 252 | case INPUT_EVENT_KIND__MAX: |
Gerd Hoffmann | c43ce55 | 2013-12-04 15:20:05 +0100 | [diff] [blame] | 253 | /* keep gcc happy */ |
| 254 | break; |
| 255 | } |
| 256 | } |
| 257 | |
Gerd Hoffmann | be1a717 | 2014-05-28 13:02:40 +0200 | [diff] [blame] | 258 | static void qemu_input_queue_process(void *opaque) |
| 259 | { |
| 260 | struct QemuInputEventQueueHead *queue = opaque; |
| 261 | QemuInputEventQueue *item; |
| 262 | |
| 263 | g_assert(!QTAILQ_EMPTY(queue)); |
| 264 | item = QTAILQ_FIRST(queue); |
| 265 | g_assert(item->type == QEMU_INPUT_QUEUE_DELAY); |
| 266 | QTAILQ_REMOVE(queue, item, node); |
Alexander Graf | 77b0359 | 2017-06-22 09:41:58 +0200 | [diff] [blame] | 267 | queue_count--; |
Gerd Hoffmann | be1a717 | 2014-05-28 13:02:40 +0200 | [diff] [blame] | 268 | g_free(item); |
| 269 | |
| 270 | while (!QTAILQ_EMPTY(queue)) { |
| 271 | item = QTAILQ_FIRST(queue); |
| 272 | switch (item->type) { |
| 273 | case QEMU_INPUT_QUEUE_DELAY: |
| 274 | timer_mod(item->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) |
| 275 | + item->delay_ms); |
| 276 | return; |
| 277 | case QEMU_INPUT_QUEUE_EVENT: |
| 278 | qemu_input_event_send(item->src, item->evt); |
| 279 | qapi_free_InputEvent(item->evt); |
| 280 | break; |
| 281 | case QEMU_INPUT_QUEUE_SYNC: |
| 282 | qemu_input_event_sync(); |
| 283 | break; |
| 284 | } |
| 285 | QTAILQ_REMOVE(queue, item, node); |
Gerd Hoffmann | fa18f36 | 2017-04-28 10:42:37 +0200 | [diff] [blame] | 286 | queue_count--; |
Gerd Hoffmann | be1a717 | 2014-05-28 13:02:40 +0200 | [diff] [blame] | 287 | g_free(item); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | static void qemu_input_queue_delay(struct QemuInputEventQueueHead *queue, |
| 292 | QEMUTimer *timer, uint32_t delay_ms) |
| 293 | { |
| 294 | QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1); |
| 295 | bool start_timer = QTAILQ_EMPTY(queue); |
| 296 | |
| 297 | item->type = QEMU_INPUT_QUEUE_DELAY; |
| 298 | item->delay_ms = delay_ms; |
| 299 | item->timer = timer; |
| 300 | QTAILQ_INSERT_TAIL(queue, item, node); |
Gerd Hoffmann | fa18f36 | 2017-04-28 10:42:37 +0200 | [diff] [blame] | 301 | queue_count++; |
Gerd Hoffmann | be1a717 | 2014-05-28 13:02:40 +0200 | [diff] [blame] | 302 | |
| 303 | if (start_timer) { |
| 304 | timer_mod(item->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) |
| 305 | + item->delay_ms); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | static void qemu_input_queue_event(struct QemuInputEventQueueHead *queue, |
| 310 | QemuConsole *src, InputEvent *evt) |
| 311 | { |
| 312 | QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1); |
| 313 | |
| 314 | item->type = QEMU_INPUT_QUEUE_EVENT; |
| 315 | item->src = src; |
| 316 | item->evt = evt; |
| 317 | QTAILQ_INSERT_TAIL(queue, item, node); |
Gerd Hoffmann | fa18f36 | 2017-04-28 10:42:37 +0200 | [diff] [blame] | 318 | queue_count++; |
Gerd Hoffmann | be1a717 | 2014-05-28 13:02:40 +0200 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | static void qemu_input_queue_sync(struct QemuInputEventQueueHead *queue) |
| 322 | { |
| 323 | QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1); |
| 324 | |
| 325 | item->type = QEMU_INPUT_QUEUE_SYNC; |
| 326 | QTAILQ_INSERT_TAIL(queue, item, node); |
Gerd Hoffmann | fa18f36 | 2017-04-28 10:42:37 +0200 | [diff] [blame] | 327 | queue_count++; |
Gerd Hoffmann | be1a717 | 2014-05-28 13:02:40 +0200 | [diff] [blame] | 328 | } |
| 329 | |
Pavel Dovgalyuk | ee31299 | 2015-09-17 19:25:24 +0300 | [diff] [blame] | 330 | void qemu_input_event_send_impl(QemuConsole *src, InputEvent *evt) |
Gerd Hoffmann | c8b405b | 2013-11-27 10:35:26 +0100 | [diff] [blame] | 331 | { |
| 332 | QemuInputHandlerState *s; |
| 333 | |
Gerd Hoffmann | c43ce55 | 2013-12-04 15:20:05 +0100 | [diff] [blame] | 334 | qemu_input_event_trace(src, evt); |
| 335 | |
Gerd Hoffmann | d353543 | 2013-11-28 11:29:33 +0100 | [diff] [blame] | 336 | /* pre processing */ |
Eric Blake | 568c73a | 2015-10-26 16:34:58 -0600 | [diff] [blame] | 337 | if (graphic_rotate && (evt->type == INPUT_EVENT_KIND_ABS)) { |
Gerd Hoffmann | d353543 | 2013-11-28 11:29:33 +0100 | [diff] [blame] | 338 | qemu_input_transform_abs_rotate(evt); |
| 339 | } |
| 340 | |
| 341 | /* send event */ |
Eric Blake | 568c73a | 2015-10-26 16:34:58 -0600 | [diff] [blame] | 342 | s = qemu_input_find_handler(1 << evt->type, src); |
Gerd Hoffmann | bdcc3a2 | 2014-03-31 16:07:30 +0200 | [diff] [blame] | 343 | if (!s) { |
| 344 | return; |
| 345 | } |
Gerd Hoffmann | c8b405b | 2013-11-27 10:35:26 +0100 | [diff] [blame] | 346 | s->handler->event(s->dev, src, evt); |
| 347 | s->events++; |
| 348 | } |
| 349 | |
Pavel Dovgalyuk | ee31299 | 2015-09-17 19:25:24 +0300 | [diff] [blame] | 350 | void qemu_input_event_send(QemuConsole *src, InputEvent *evt) |
Gerd Hoffmann | c8b405b | 2013-11-27 10:35:26 +0100 | [diff] [blame] | 351 | { |
Daniel P. Berrange | af07e5f | 2017-09-29 11:12:00 +0100 | [diff] [blame] | 352 | /* Expect all parts of QEMU to send events with QCodes exclusively. |
| 353 | * Key numbers are only supported as end-user input via QMP */ |
| 354 | assert(!(evt->type == INPUT_EVENT_KIND_KEY && |
| 355 | evt->u.key.data->key->type == KEY_VALUE_KIND_NUMBER)); |
| 356 | |
Daniel P. Berrange | 80b857f | 2017-10-19 15:28:47 +0100 | [diff] [blame] | 357 | |
| 358 | /* |
| 359 | * 'sysrq' was mistakenly added to hack around the fact that |
| 360 | * the ps2 driver was not generating correct scancodes sequences |
| 361 | * when 'alt+print' was pressed. This flaw is now fixed and the |
| 362 | * 'sysrq' key serves no further purpose. We normalize it to |
| 363 | * 'print', so that downstream receivers of the event don't |
| 364 | * neeed to deal with this mistake |
| 365 | */ |
| 366 | if (evt->type == INPUT_EVENT_KIND_KEY && |
| 367 | evt->u.key.data->key->u.qcode.data == Q_KEY_CODE_SYSRQ) { |
| 368 | evt->u.key.data->key->u.qcode.data = Q_KEY_CODE_PRINT; |
| 369 | } |
| 370 | |
Gerd Hoffmann | c8b405b | 2013-11-27 10:35:26 +0100 | [diff] [blame] | 371 | if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) { |
| 372 | return; |
| 373 | } |
| 374 | |
Pavel Dovgalyuk | ee31299 | 2015-09-17 19:25:24 +0300 | [diff] [blame] | 375 | replay_input_event(src, evt); |
| 376 | } |
| 377 | |
| 378 | void qemu_input_event_sync_impl(void) |
| 379 | { |
| 380 | QemuInputHandlerState *s; |
| 381 | |
Gerd Hoffmann | c43ce55 | 2013-12-04 15:20:05 +0100 | [diff] [blame] | 382 | trace_input_event_sync(); |
| 383 | |
Gerd Hoffmann | c8b405b | 2013-11-27 10:35:26 +0100 | [diff] [blame] | 384 | QTAILQ_FOREACH(s, &handlers, node) { |
| 385 | if (!s->events) { |
| 386 | continue; |
| 387 | } |
| 388 | if (s->handler->sync) { |
| 389 | s->handler->sync(s->dev); |
| 390 | } |
| 391 | s->events = 0; |
| 392 | } |
| 393 | } |
Gerd Hoffmann | 6567147 | 2013-11-27 11:38:47 +0100 | [diff] [blame] | 394 | |
Pavel Dovgalyuk | ee31299 | 2015-09-17 19:25:24 +0300 | [diff] [blame] | 395 | void qemu_input_event_sync(void) |
| 396 | { |
| 397 | if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) { |
| 398 | return; |
| 399 | } |
| 400 | |
| 401 | replay_input_sync_event(); |
| 402 | } |
| 403 | |
Daniel P. Berrange | 3ad35e7 | 2017-09-29 11:12:01 +0100 | [diff] [blame] | 404 | static InputEvent *qemu_input_event_new_key(KeyValue *key, bool down) |
Gerd Hoffmann | 6567147 | 2013-11-27 11:38:47 +0100 | [diff] [blame] | 405 | { |
| 406 | InputEvent *evt = g_new0(InputEvent, 1); |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 407 | evt->u.key.data = g_new0(InputKeyEvent, 1); |
Eric Blake | 568c73a | 2015-10-26 16:34:58 -0600 | [diff] [blame] | 408 | evt->type = INPUT_EVENT_KIND_KEY; |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 409 | evt->u.key.data->key = key; |
| 410 | evt->u.key.data->down = down; |
Gerd Hoffmann | 6567147 | 2013-11-27 11:38:47 +0100 | [diff] [blame] | 411 | return evt; |
| 412 | } |
| 413 | |
| 414 | void qemu_input_event_send_key(QemuConsole *src, KeyValue *key, bool down) |
| 415 | { |
| 416 | InputEvent *evt; |
| 417 | evt = qemu_input_event_new_key(key, down); |
Gerd Hoffmann | be1a717 | 2014-05-28 13:02:40 +0200 | [diff] [blame] | 418 | if (QTAILQ_EMPTY(&kbd_queue)) { |
| 419 | qemu_input_event_send(src, evt); |
| 420 | qemu_input_event_sync(); |
| 421 | qapi_free_InputEvent(evt); |
Gerd Hoffmann | fa18f36 | 2017-04-28 10:42:37 +0200 | [diff] [blame] | 422 | } else if (queue_count < queue_limit) { |
Gerd Hoffmann | be1a717 | 2014-05-28 13:02:40 +0200 | [diff] [blame] | 423 | qemu_input_queue_event(&kbd_queue, src, evt); |
| 424 | qemu_input_queue_sync(&kbd_queue); |
linzhecheng | fca4774 | 2017-12-25 10:37:30 +0800 | [diff] [blame] | 425 | } else { |
| 426 | qapi_free_InputEvent(evt); |
Gerd Hoffmann | be1a717 | 2014-05-28 13:02:40 +0200 | [diff] [blame] | 427 | } |
Gerd Hoffmann | 6567147 | 2013-11-27 11:38:47 +0100 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | void qemu_input_event_send_key_number(QemuConsole *src, int num, bool down) |
| 431 | { |
Daniel P. Berrange | af07e5f | 2017-09-29 11:12:00 +0100 | [diff] [blame] | 432 | QKeyCode code = qemu_input_key_number_to_qcode(num); |
| 433 | qemu_input_event_send_key_qcode(src, code, down); |
Gerd Hoffmann | 6567147 | 2013-11-27 11:38:47 +0100 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | void qemu_input_event_send_key_qcode(QemuConsole *src, QKeyCode q, bool down) |
| 437 | { |
| 438 | KeyValue *key = g_new0(KeyValue, 1); |
Eric Blake | 568c73a | 2015-10-26 16:34:58 -0600 | [diff] [blame] | 439 | key->type = KEY_VALUE_KIND_QCODE; |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 440 | key->u.qcode.data = q; |
Gerd Hoffmann | 6567147 | 2013-11-27 11:38:47 +0100 | [diff] [blame] | 441 | qemu_input_event_send_key(src, key, down); |
| 442 | } |
Gerd Hoffmann | 4357940 | 2013-11-27 18:24:29 +0100 | [diff] [blame] | 443 | |
Gerd Hoffmann | be1a717 | 2014-05-28 13:02:40 +0200 | [diff] [blame] | 444 | void qemu_input_event_send_key_delay(uint32_t delay_ms) |
| 445 | { |
Marc-André Lureau | 05c6638 | 2017-04-25 17:05:20 +0400 | [diff] [blame] | 446 | if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) { |
| 447 | return; |
| 448 | } |
| 449 | |
Gerd Hoffmann | be1a717 | 2014-05-28 13:02:40 +0200 | [diff] [blame] | 450 | if (!kbd_timer) { |
| 451 | kbd_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, qemu_input_queue_process, |
| 452 | &kbd_queue); |
| 453 | } |
Gerd Hoffmann | fa18f36 | 2017-04-28 10:42:37 +0200 | [diff] [blame] | 454 | if (queue_count < queue_limit) { |
| 455 | qemu_input_queue_delay(&kbd_queue, kbd_timer, |
| 456 | delay_ms ? delay_ms : kbd_default_delay_ms); |
| 457 | } |
Gerd Hoffmann | be1a717 | 2014-05-28 13:02:40 +0200 | [diff] [blame] | 458 | } |
| 459 | |
Gerd Hoffmann | 4357940 | 2013-11-27 18:24:29 +0100 | [diff] [blame] | 460 | InputEvent *qemu_input_event_new_btn(InputButton btn, bool down) |
| 461 | { |
| 462 | InputEvent *evt = g_new0(InputEvent, 1); |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 463 | evt->u.btn.data = g_new0(InputBtnEvent, 1); |
Eric Blake | 568c73a | 2015-10-26 16:34:58 -0600 | [diff] [blame] | 464 | evt->type = INPUT_EVENT_KIND_BTN; |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 465 | evt->u.btn.data->button = btn; |
| 466 | evt->u.btn.data->down = down; |
Gerd Hoffmann | 4357940 | 2013-11-27 18:24:29 +0100 | [diff] [blame] | 467 | return evt; |
| 468 | } |
| 469 | |
| 470 | void qemu_input_queue_btn(QemuConsole *src, InputButton btn, bool down) |
| 471 | { |
| 472 | InputEvent *evt; |
| 473 | evt = qemu_input_event_new_btn(btn, down); |
| 474 | qemu_input_event_send(src, evt); |
| 475 | qapi_free_InputEvent(evt); |
| 476 | } |
| 477 | |
| 478 | void qemu_input_update_buttons(QemuConsole *src, uint32_t *button_map, |
| 479 | uint32_t button_old, uint32_t button_new) |
| 480 | { |
| 481 | InputButton btn; |
| 482 | uint32_t mask; |
| 483 | |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 484 | for (btn = 0; btn < INPUT_BUTTON__MAX; btn++) { |
Gerd Hoffmann | 4357940 | 2013-11-27 18:24:29 +0100 | [diff] [blame] | 485 | mask = button_map[btn]; |
| 486 | if ((button_old & mask) == (button_new & mask)) { |
| 487 | continue; |
| 488 | } |
| 489 | qemu_input_queue_btn(src, btn, button_new & mask); |
| 490 | } |
| 491 | } |
| 492 | |
Gerd Hoffmann | 502c8db | 2013-11-28 11:31:09 +0100 | [diff] [blame] | 493 | bool qemu_input_is_absolute(void) |
| 494 | { |
| 495 | QemuInputHandlerState *s; |
| 496 | |
Gerd Hoffmann | 6f5943c | 2014-05-19 15:18:37 +0200 | [diff] [blame] | 497 | s = qemu_input_find_handler(INPUT_EVENT_MASK_REL | INPUT_EVENT_MASK_ABS, |
| 498 | NULL); |
Gerd Hoffmann | 502c8db | 2013-11-28 11:31:09 +0100 | [diff] [blame] | 499 | return (s != NULL) && (s->handler->mask & INPUT_EVENT_MASK_ABS); |
| 500 | } |
| 501 | |
Philippe Voinov | 9cfa7ab | 2017-05-05 15:39:52 +0200 | [diff] [blame] | 502 | int qemu_input_scale_axis(int value, |
| 503 | int min_in, int max_in, |
| 504 | int min_out, int max_out) |
Gerd Hoffmann | 4357940 | 2013-11-27 18:24:29 +0100 | [diff] [blame] | 505 | { |
Philippe Voinov | 9cfa7ab | 2017-05-05 15:39:52 +0200 | [diff] [blame] | 506 | int64_t range_in = (int64_t)max_in - min_in; |
| 507 | int64_t range_out = (int64_t)max_out - min_out; |
| 508 | |
| 509 | if (range_in < 1) { |
| 510 | return min_out + range_out / 2; |
Gerd Hoffmann | 4357940 | 2013-11-27 18:24:29 +0100 | [diff] [blame] | 511 | } |
Philippe Voinov | 9cfa7ab | 2017-05-05 15:39:52 +0200 | [diff] [blame] | 512 | return ((int64_t)value - min_in) * range_out / range_in + min_out; |
Gerd Hoffmann | 4357940 | 2013-11-27 18:24:29 +0100 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | InputEvent *qemu_input_event_new_move(InputEventKind kind, |
| 516 | InputAxis axis, int value) |
| 517 | { |
| 518 | InputEvent *evt = g_new0(InputEvent, 1); |
| 519 | InputMoveEvent *move = g_new0(InputMoveEvent, 1); |
| 520 | |
Eric Blake | 568c73a | 2015-10-26 16:34:58 -0600 | [diff] [blame] | 521 | evt->type = kind; |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 522 | evt->u.rel.data = move; /* evt->u.rel is the same as evt->u.abs */ |
Gerd Hoffmann | 4357940 | 2013-11-27 18:24:29 +0100 | [diff] [blame] | 523 | move->axis = axis; |
| 524 | move->value = value; |
| 525 | return evt; |
| 526 | } |
| 527 | |
| 528 | void qemu_input_queue_rel(QemuConsole *src, InputAxis axis, int value) |
| 529 | { |
| 530 | InputEvent *evt; |
| 531 | evt = qemu_input_event_new_move(INPUT_EVENT_KIND_REL, axis, value); |
| 532 | qemu_input_event_send(src, evt); |
| 533 | qapi_free_InputEvent(evt); |
| 534 | } |
| 535 | |
Philippe Voinov | 9cfa7ab | 2017-05-05 15:39:52 +0200 | [diff] [blame] | 536 | void qemu_input_queue_abs(QemuConsole *src, InputAxis axis, int value, |
| 537 | int min_in, int max_in) |
Gerd Hoffmann | 4357940 | 2013-11-27 18:24:29 +0100 | [diff] [blame] | 538 | { |
| 539 | InputEvent *evt; |
Philippe Voinov | 9cfa7ab | 2017-05-05 15:39:52 +0200 | [diff] [blame] | 540 | int scaled = qemu_input_scale_axis(value, min_in, max_in, |
| 541 | INPUT_EVENT_ABS_MIN, |
| 542 | INPUT_EVENT_ABS_MAX); |
Gerd Hoffmann | 4357940 | 2013-11-27 18:24:29 +0100 | [diff] [blame] | 543 | evt = qemu_input_event_new_move(INPUT_EVENT_KIND_ABS, axis, scaled); |
| 544 | qemu_input_event_send(src, evt); |
| 545 | qapi_free_InputEvent(evt); |
| 546 | } |
Gerd Hoffmann | 4a33f45 | 2013-12-05 11:23:42 +0100 | [diff] [blame] | 547 | |
| 548 | void qemu_input_check_mode_change(void) |
| 549 | { |
| 550 | static int current_is_absolute; |
| 551 | int is_absolute; |
| 552 | |
| 553 | is_absolute = qemu_input_is_absolute(); |
| 554 | |
| 555 | if (is_absolute != current_is_absolute) { |
Gerd Hoffmann | a8dfb1c | 2013-12-05 11:24:14 +0100 | [diff] [blame] | 556 | trace_input_mouse_mode(is_absolute); |
Gerd Hoffmann | 4a33f45 | 2013-12-05 11:23:42 +0100 | [diff] [blame] | 557 | notifier_list_notify(&mouse_mode_notifiers, NULL); |
| 558 | } |
| 559 | |
| 560 | current_is_absolute = is_absolute; |
| 561 | } |
| 562 | |
| 563 | void qemu_add_mouse_mode_change_notifier(Notifier *notify) |
| 564 | { |
| 565 | notifier_list_add(&mouse_mode_notifiers, notify); |
| 566 | } |
| 567 | |
| 568 | void qemu_remove_mouse_mode_change_notifier(Notifier *notify) |
| 569 | { |
| 570 | notifier_remove(notify); |
| 571 | } |
Gerd Hoffmann | e842c68 | 2013-12-10 17:09:36 +0100 | [diff] [blame] | 572 | |
| 573 | MouseInfoList *qmp_query_mice(Error **errp) |
| 574 | { |
| 575 | MouseInfoList *mice_list = NULL; |
| 576 | MouseInfoList *info; |
| 577 | QemuInputHandlerState *s; |
| 578 | bool current = true; |
| 579 | |
| 580 | QTAILQ_FOREACH(s, &handlers, node) { |
| 581 | if (!(s->handler->mask & |
| 582 | (INPUT_EVENT_MASK_REL | INPUT_EVENT_MASK_ABS))) { |
| 583 | continue; |
| 584 | } |
| 585 | |
| 586 | info = g_new0(MouseInfoList, 1); |
| 587 | info->value = g_new0(MouseInfo, 1); |
| 588 | info->value->index = s->id; |
| 589 | info->value->name = g_strdup(s->handler->name); |
| 590 | info->value->absolute = s->handler->mask & INPUT_EVENT_MASK_ABS; |
| 591 | info->value->current = current; |
| 592 | |
| 593 | current = false; |
| 594 | info->next = mice_list; |
| 595 | mice_list = info; |
| 596 | } |
| 597 | |
| 598 | return mice_list; |
| 599 | } |
Gerd Hoffmann | 70b52f6 | 2013-12-10 17:16:03 +0100 | [diff] [blame] | 600 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 601 | void hmp_mouse_set(Monitor *mon, const QDict *qdict) |
Gerd Hoffmann | 70b52f6 | 2013-12-10 17:16:03 +0100 | [diff] [blame] | 602 | { |
| 603 | QemuInputHandlerState *s; |
| 604 | int index = qdict_get_int(qdict, "index"); |
| 605 | int found = 0; |
| 606 | |
| 607 | QTAILQ_FOREACH(s, &handlers, node) { |
Hani Benhabiles | 0419f78 | 2014-03-31 23:09:06 +0100 | [diff] [blame] | 608 | if (s->id != index) { |
| 609 | continue; |
Gerd Hoffmann | 70b52f6 | 2013-12-10 17:16:03 +0100 | [diff] [blame] | 610 | } |
Hani Benhabiles | 0419f78 | 2014-03-31 23:09:06 +0100 | [diff] [blame] | 611 | if (!(s->handler->mask & (INPUT_EVENT_MASK_REL | |
| 612 | INPUT_EVENT_MASK_ABS))) { |
| 613 | error_report("Input device '%s' is not a mouse", s->handler->name); |
| 614 | return; |
| 615 | } |
| 616 | found = 1; |
| 617 | qemu_input_handler_activate(s); |
| 618 | break; |
Gerd Hoffmann | 70b52f6 | 2013-12-10 17:16:03 +0100 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | if (!found) { |
Hani Benhabiles | 0419f78 | 2014-03-31 23:09:06 +0100 | [diff] [blame] | 622 | error_report("Mouse at index '%d' not found", index); |
Gerd Hoffmann | 70b52f6 | 2013-12-10 17:16:03 +0100 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | qemu_input_check_mode_change(); |
| 626 | } |