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