blob: 8ac407dec48575581a60fd03e2d9b3dadb942218 [file] [log] [blame]
Peter Maydelle16f4c82016-01-29 17:49:51 +00001#include "qemu/osdep.h"
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +01002#include "sysemu/sysemu.h"
Markus Armbrustere688df62018-02-01 12:18:31 +01003#include "qapi/error.h"
Markus Armbruster9af23982018-02-11 10:36:01 +01004#include "qapi/qapi-commands-ui.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +01005#include "qapi/qmp/qdict.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +01006#include "qemu/error-report.h"
Gerd Hoffmannc43ce552013-12-04 15:20:05 +01007#include "trace.h"
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +01008#include "ui/input.h"
Gerd Hoffmannc43ce552013-12-04 15:20:05 +01009#include "ui/console.h"
Pavel Dovgalyukee312992015-09-17 19:25:24 +030010#include "sysemu/replay.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020011#include "sysemu/runstate.h"
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +010012
13struct QemuInputHandlerState {
14 DeviceState *dev;
15 QemuInputHandler *handler;
16 int id;
17 int events;
Gerd Hoffmann6f5943c2014-05-19 15:18:37 +020018 QemuConsole *con;
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +010019 QTAILQ_ENTRY(QemuInputHandlerState) node;
20};
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +020021
22typedef struct QemuInputEventQueue QemuInputEventQueue;
Paolo Bonzinif481ee22018-12-06 11:56:15 +010023typedef QTAILQ_HEAD(QemuInputEventQueueHead, QemuInputEventQueue)
24 QemuInputEventQueueHead;
25
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +020026struct QemuInputEventQueue {
27 enum {
28 QEMU_INPUT_QUEUE_DELAY = 1,
29 QEMU_INPUT_QUEUE_EVENT,
30 QEMU_INPUT_QUEUE_SYNC,
31 } type;
32 QEMUTimer *timer;
33 uint32_t delay_ms;
34 QemuConsole *src;
35 InputEvent *evt;
36 QTAILQ_ENTRY(QemuInputEventQueue) node;
37};
38
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +010039static QTAILQ_HEAD(, QemuInputHandlerState) handlers =
40 QTAILQ_HEAD_INITIALIZER(handlers);
Gerd Hoffmann4a33f452013-12-05 11:23:42 +010041static NotifierList mouse_mode_notifiers =
42 NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers);
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +010043
Paolo Bonzinif481ee22018-12-06 11:56:15 +010044static QemuInputEventQueueHead kbd_queue = QTAILQ_HEAD_INITIALIZER(kbd_queue);
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +020045static QEMUTimer *kbd_timer;
46static uint32_t kbd_default_delay_ms = 10;
Gerd Hoffmannfa18f362017-04-28 10:42:37 +020047static uint32_t queue_count;
48static uint32_t queue_limit = 1024;
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +020049
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +010050QemuInputHandlerState *qemu_input_handler_register(DeviceState *dev,
51 QemuInputHandler *handler)
52{
53 QemuInputHandlerState *s = g_new0(QemuInputHandlerState, 1);
54 static int id = 1;
55
56 s->dev = dev;
57 s->handler = handler;
58 s->id = id++;
59 QTAILQ_INSERT_TAIL(&handlers, s, node);
Gerd Hoffmann4a33f452013-12-05 11:23:42 +010060
61 qemu_input_check_mode_change();
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +010062 return s;
63}
64
65void qemu_input_handler_activate(QemuInputHandlerState *s)
66{
67 QTAILQ_REMOVE(&handlers, s, node);
68 QTAILQ_INSERT_HEAD(&handlers, s, node);
Gerd Hoffmann4a33f452013-12-05 11:23:42 +010069 qemu_input_check_mode_change();
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +010070}
71
Gerd Hoffmann528728f2014-03-16 00:38:45 +010072void qemu_input_handler_deactivate(QemuInputHandlerState *s)
73{
74 QTAILQ_REMOVE(&handlers, s, node);
75 QTAILQ_INSERT_TAIL(&handlers, s, node);
76 qemu_input_check_mode_change();
77}
78
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +010079void qemu_input_handler_unregister(QemuInputHandlerState *s)
80{
81 QTAILQ_REMOVE(&handlers, s, node);
82 g_free(s);
Gerd Hoffmann4a33f452013-12-05 11:23:42 +010083 qemu_input_check_mode_change();
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +010084}
85
Gerd Hoffmann6f5943c2014-05-19 15:18:37 +020086void qemu_input_handler_bind(QemuInputHandlerState *s,
87 const char *device_id, int head,
88 Error **errp)
89{
Gerd Hoffmann6f5943c2014-05-19 15:18:37 +020090 QemuConsole *con;
Gerd Hoffmannf2c1d542016-01-12 11:45:43 +010091 Error *err = NULL;
Gerd Hoffmann6f5943c2014-05-19 15:18:37 +020092
Gerd Hoffmannf2c1d542016-01-12 11:45:43 +010093 con = qemu_console_lookup_by_device_name(device_id, head, &err);
94 if (err) {
95 error_propagate(errp, err);
Gerd Hoffmann6f5943c2014-05-19 15:18:37 +020096 return;
97 }
98
99 s->con = con;
100}
101
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +0100102static QemuInputHandlerState*
Gerd Hoffmann6f5943c2014-05-19 15:18:37 +0200103qemu_input_find_handler(uint32_t mask, QemuConsole *con)
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +0100104{
105 QemuInputHandlerState *s;
106
107 QTAILQ_FOREACH(s, &handlers, node) {
Gerd Hoffmann6f5943c2014-05-19 15:18:37 +0200108 if (s->con == NULL || s->con != con) {
109 continue;
110 }
111 if (mask & s->handler->mask) {
112 return s;
113 }
114 }
115
116 QTAILQ_FOREACH(s, &handlers, node) {
117 if (s->con != NULL) {
118 continue;
119 }
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +0100120 if (mask & s->handler->mask) {
121 return s;
122 }
123 }
124 return NULL;
125}
126
Gerd Hoffmann6575ccd2016-01-12 12:39:33 +0100127void qmp_input_send_event(bool has_device, const char *device,
128 bool has_head, int64_t head,
129 InputEventList *events, Error **errp)
Marcelo Tosatti50c66172014-09-30 18:10:17 -0300130{
131 InputEventList *e;
132 QemuConsole *con;
Gerd Hoffmannb98d26e2016-01-12 12:11:14 +0100133 Error *err = NULL;
Marcelo Tosatti50c66172014-09-30 18:10:17 -0300134
Amos Kong51fc4472014-11-07 12:41:25 +0800135 con = NULL;
Gerd Hoffmannb98d26e2016-01-12 12:11:14 +0100136 if (has_device) {
137 if (!has_head) {
138 head = 0;
139 }
140 con = qemu_console_lookup_by_device_name(device, head, &err);
141 if (err) {
142 error_propagate(errp, err);
Amos Kong51fc4472014-11-07 12:41:25 +0800143 return;
144 }
Marcelo Tosatti50c66172014-09-30 18:10:17 -0300145 }
146
147 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
148 error_setg(errp, "VM not running");
149 return;
150 }
151
152 for (e = events; e != NULL; e = e->next) {
153 InputEvent *event = e->value;
154
Eric Blake568c73a2015-10-26 16:34:58 -0600155 if (!qemu_input_find_handler(1 << event->type, con)) {
Marcelo Tosatti50c66172014-09-30 18:10:17 -0300156 error_setg(errp, "Input handler not found for "
157 "event type %s",
Markus Armbruster977c7362017-08-24 10:46:08 +0200158 InputEventKind_str(event->type));
Marcelo Tosatti50c66172014-09-30 18:10:17 -0300159 return;
160 }
161 }
162
163 for (e = events; e != NULL; e = e->next) {
Daniel P. Berrangeaf07e5f2017-09-29 11:12:00 +0100164 InputEvent *evt = e->value;
Marcelo Tosatti50c66172014-09-30 18:10:17 -0300165
Daniel P. Berrangeaf07e5f2017-09-29 11:12:00 +0100166 if (evt->type == INPUT_EVENT_KIND_KEY &&
167 evt->u.key.data->key->type == KEY_VALUE_KIND_NUMBER) {
168 KeyValue *key = evt->u.key.data->key;
Daniel P. Berrange237925b2017-10-19 15:28:42 +0100169 QKeyCode code = qemu_input_key_number_to_qcode(key->u.number.data);
Daniel P. Berrangeaf07e5f2017-09-29 11:12:00 +0100170 qemu_input_event_send_key_qcode(con, code, evt->u.key.data->down);
171 } else {
172 qemu_input_event_send(con, evt);
173 }
Marcelo Tosatti50c66172014-09-30 18:10:17 -0300174 }
175
176 qemu_input_event_sync();
177}
178
Philippe Voinov9cfa7ab2017-05-05 15:39:52 +0200179static int qemu_input_transform_invert_abs_value(int value)
180{
181 return (int64_t)INPUT_EVENT_ABS_MAX - value + INPUT_EVENT_ABS_MIN;
182}
183
Gerd Hoffmannd3535432013-11-28 11:29:33 +0100184static void qemu_input_transform_abs_rotate(InputEvent *evt)
185{
Eric Blake32bafa82016-03-17 16:48:37 -0600186 InputMoveEvent *move = evt->u.abs.data;
Gerd Hoffmannd3535432013-11-28 11:29:33 +0100187 switch (graphic_rotate) {
188 case 90:
Eric Blakeb5a1b442016-03-03 09:16:49 -0700189 if (move->axis == INPUT_AXIS_X) {
190 move->axis = INPUT_AXIS_Y;
191 } else if (move->axis == INPUT_AXIS_Y) {
192 move->axis = INPUT_AXIS_X;
Philippe Voinov9cfa7ab2017-05-05 15:39:52 +0200193 move->value = qemu_input_transform_invert_abs_value(move->value);
Gerd Hoffmannd3535432013-11-28 11:29:33 +0100194 }
195 break;
196 case 180:
Philippe Voinov9cfa7ab2017-05-05 15:39:52 +0200197 move->value = qemu_input_transform_invert_abs_value(move->value);
Gerd Hoffmannd3535432013-11-28 11:29:33 +0100198 break;
199 case 270:
Eric Blakeb5a1b442016-03-03 09:16:49 -0700200 if (move->axis == INPUT_AXIS_X) {
201 move->axis = INPUT_AXIS_Y;
Philippe Voinov9cfa7ab2017-05-05 15:39:52 +0200202 move->value = qemu_input_transform_invert_abs_value(move->value);
Eric Blakeb5a1b442016-03-03 09:16:49 -0700203 } else if (move->axis == INPUT_AXIS_Y) {
204 move->axis = INPUT_AXIS_X;
Gerd Hoffmannd3535432013-11-28 11:29:33 +0100205 }
206 break;
207 }
208}
209
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100210static void qemu_input_event_trace(QemuConsole *src, InputEvent *evt)
211{
212 const char *name;
Gerd Hoffmann2386a902014-05-21 13:49:59 +0200213 int qcode, idx = -1;
Eric Blakeb5a1b442016-03-03 09:16:49 -0700214 InputKeyEvent *key;
215 InputBtnEvent *btn;
216 InputMoveEvent *move;
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100217
218 if (src) {
219 idx = qemu_console_get_index(src);
220 }
Eric Blake568c73a2015-10-26 16:34:58 -0600221 switch (evt->type) {
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100222 case INPUT_EVENT_KIND_KEY:
Eric Blake32bafa82016-03-17 16:48:37 -0600223 key = evt->u.key.data;
Eric Blakeb5a1b442016-03-03 09:16:49 -0700224 switch (key->key->type) {
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100225 case KEY_VALUE_KIND_NUMBER:
Eric Blake32bafa82016-03-17 16:48:37 -0600226 qcode = qemu_input_key_number_to_qcode(key->key->u.number.data);
Markus Armbruster977c7362017-08-24 10:46:08 +0200227 name = QKeyCode_str(qcode);
Eric Blake32bafa82016-03-17 16:48:37 -0600228 trace_input_event_key_number(idx, key->key->u.number.data,
Eric Blakeb5a1b442016-03-03 09:16:49 -0700229 name, key->down);
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100230 break;
231 case KEY_VALUE_KIND_QCODE:
Markus Armbruster977c7362017-08-24 10:46:08 +0200232 name = QKeyCode_str(key->key->u.qcode.data);
Eric Blakeb5a1b442016-03-03 09:16:49 -0700233 trace_input_event_key_qcode(idx, name, key->down);
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100234 break;
Eric Blake7fb1cf12015-11-18 01:52:57 -0700235 case KEY_VALUE_KIND__MAX:
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100236 /* keep gcc happy */
237 break;
238 }
239 break;
240 case INPUT_EVENT_KIND_BTN:
Eric Blake32bafa82016-03-17 16:48:37 -0600241 btn = evt->u.btn.data;
Markus Armbruster977c7362017-08-24 10:46:08 +0200242 name = InputButton_str(btn->button);
Eric Blakeb5a1b442016-03-03 09:16:49 -0700243 trace_input_event_btn(idx, name, btn->down);
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100244 break;
245 case INPUT_EVENT_KIND_REL:
Eric Blake32bafa82016-03-17 16:48:37 -0600246 move = evt->u.rel.data;
Markus Armbruster977c7362017-08-24 10:46:08 +0200247 name = InputAxis_str(move->axis);
Eric Blakeb5a1b442016-03-03 09:16:49 -0700248 trace_input_event_rel(idx, name, move->value);
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100249 break;
250 case INPUT_EVENT_KIND_ABS:
Eric Blake32bafa82016-03-17 16:48:37 -0600251 move = evt->u.abs.data;
Markus Armbruster977c7362017-08-24 10:46:08 +0200252 name = InputAxis_str(move->axis);
Eric Blakeb5a1b442016-03-03 09:16:49 -0700253 trace_input_event_abs(idx, name, move->value);
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100254 break;
Eric Blake7fb1cf12015-11-18 01:52:57 -0700255 case INPUT_EVENT_KIND__MAX:
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100256 /* keep gcc happy */
257 break;
258 }
259}
260
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200261static void qemu_input_queue_process(void *opaque)
262{
Paolo Bonzinif481ee22018-12-06 11:56:15 +0100263 QemuInputEventQueueHead *queue = opaque;
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200264 QemuInputEventQueue *item;
265
266 g_assert(!QTAILQ_EMPTY(queue));
267 item = QTAILQ_FIRST(queue);
268 g_assert(item->type == QEMU_INPUT_QUEUE_DELAY);
269 QTAILQ_REMOVE(queue, item, node);
Alexander Graf77b03592017-06-22 09:41:58 +0200270 queue_count--;
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200271 g_free(item);
272
273 while (!QTAILQ_EMPTY(queue)) {
274 item = QTAILQ_FIRST(queue);
275 switch (item->type) {
276 case QEMU_INPUT_QUEUE_DELAY:
Artem Pisarenko05ff8dc2018-10-17 14:24:18 +0600277 timer_mod(item->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL)
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200278 + item->delay_ms);
279 return;
280 case QEMU_INPUT_QUEUE_EVENT:
281 qemu_input_event_send(item->src, item->evt);
282 qapi_free_InputEvent(item->evt);
283 break;
284 case QEMU_INPUT_QUEUE_SYNC:
285 qemu_input_event_sync();
286 break;
287 }
288 QTAILQ_REMOVE(queue, item, node);
Gerd Hoffmannfa18f362017-04-28 10:42:37 +0200289 queue_count--;
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200290 g_free(item);
291 }
292}
293
Paolo Bonzinif481ee22018-12-06 11:56:15 +0100294static void qemu_input_queue_delay(QemuInputEventQueueHead *queue,
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200295 QEMUTimer *timer, uint32_t delay_ms)
296{
297 QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1);
298 bool start_timer = QTAILQ_EMPTY(queue);
299
300 item->type = QEMU_INPUT_QUEUE_DELAY;
301 item->delay_ms = delay_ms;
302 item->timer = timer;
303 QTAILQ_INSERT_TAIL(queue, item, node);
Gerd Hoffmannfa18f362017-04-28 10:42:37 +0200304 queue_count++;
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200305
306 if (start_timer) {
Artem Pisarenko05ff8dc2018-10-17 14:24:18 +0600307 timer_mod(item->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL)
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200308 + item->delay_ms);
309 }
310}
311
Paolo Bonzinif481ee22018-12-06 11:56:15 +0100312static void qemu_input_queue_event(QemuInputEventQueueHead *queue,
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200313 QemuConsole *src, InputEvent *evt)
314{
315 QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1);
316
317 item->type = QEMU_INPUT_QUEUE_EVENT;
318 item->src = src;
319 item->evt = evt;
320 QTAILQ_INSERT_TAIL(queue, item, node);
Gerd Hoffmannfa18f362017-04-28 10:42:37 +0200321 queue_count++;
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200322}
323
Paolo Bonzinif481ee22018-12-06 11:56:15 +0100324static void qemu_input_queue_sync(QemuInputEventQueueHead *queue)
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200325{
326 QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1);
327
328 item->type = QEMU_INPUT_QUEUE_SYNC;
329 QTAILQ_INSERT_TAIL(queue, item, node);
Gerd Hoffmannfa18f362017-04-28 10:42:37 +0200330 queue_count++;
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200331}
332
Pavel Dovgalyukee312992015-09-17 19:25:24 +0300333void qemu_input_event_send_impl(QemuConsole *src, InputEvent *evt)
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +0100334{
335 QemuInputHandlerState *s;
336
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100337 qemu_input_event_trace(src, evt);
338
Gerd Hoffmannd3535432013-11-28 11:29:33 +0100339 /* pre processing */
Eric Blake568c73a2015-10-26 16:34:58 -0600340 if (graphic_rotate && (evt->type == INPUT_EVENT_KIND_ABS)) {
Gerd Hoffmannd3535432013-11-28 11:29:33 +0100341 qemu_input_transform_abs_rotate(evt);
342 }
343
344 /* send event */
Eric Blake568c73a2015-10-26 16:34:58 -0600345 s = qemu_input_find_handler(1 << evt->type, src);
Gerd Hoffmannbdcc3a22014-03-31 16:07:30 +0200346 if (!s) {
347 return;
348 }
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +0100349 s->handler->event(s->dev, src, evt);
350 s->events++;
351}
352
Pavel Dovgalyukee312992015-09-17 19:25:24 +0300353void qemu_input_event_send(QemuConsole *src, InputEvent *evt)
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +0100354{
Daniel P. Berrangeaf07e5f2017-09-29 11:12:00 +0100355 /* Expect all parts of QEMU to send events with QCodes exclusively.
356 * Key numbers are only supported as end-user input via QMP */
357 assert(!(evt->type == INPUT_EVENT_KIND_KEY &&
358 evt->u.key.data->key->type == KEY_VALUE_KIND_NUMBER));
359
Daniel P. Berrange80b857f2017-10-19 15:28:47 +0100360
361 /*
362 * 'sysrq' was mistakenly added to hack around the fact that
363 * the ps2 driver was not generating correct scancodes sequences
364 * when 'alt+print' was pressed. This flaw is now fixed and the
365 * 'sysrq' key serves no further purpose. We normalize it to
366 * 'print', so that downstream receivers of the event don't
367 * neeed to deal with this mistake
368 */
369 if (evt->type == INPUT_EVENT_KIND_KEY &&
370 evt->u.key.data->key->u.qcode.data == Q_KEY_CODE_SYSRQ) {
371 evt->u.key.data->key->u.qcode.data = Q_KEY_CODE_PRINT;
372 }
373
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +0100374 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
375 return;
376 }
377
Pavel Dovgalyukee312992015-09-17 19:25:24 +0300378 replay_input_event(src, evt);
379}
380
381void qemu_input_event_sync_impl(void)
382{
383 QemuInputHandlerState *s;
384
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100385 trace_input_event_sync();
386
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +0100387 QTAILQ_FOREACH(s, &handlers, node) {
388 if (!s->events) {
389 continue;
390 }
391 if (s->handler->sync) {
392 s->handler->sync(s->dev);
393 }
394 s->events = 0;
395 }
396}
Gerd Hoffmann65671472013-11-27 11:38:47 +0100397
Pavel Dovgalyukee312992015-09-17 19:25:24 +0300398void qemu_input_event_sync(void)
399{
400 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
401 return;
402 }
403
404 replay_input_sync_event();
405}
406
Daniel P. Berrange3ad35e72017-09-29 11:12:01 +0100407static InputEvent *qemu_input_event_new_key(KeyValue *key, bool down)
Gerd Hoffmann65671472013-11-27 11:38:47 +0100408{
409 InputEvent *evt = g_new0(InputEvent, 1);
Eric Blake32bafa82016-03-17 16:48:37 -0600410 evt->u.key.data = g_new0(InputKeyEvent, 1);
Eric Blake568c73a2015-10-26 16:34:58 -0600411 evt->type = INPUT_EVENT_KIND_KEY;
Eric Blake32bafa82016-03-17 16:48:37 -0600412 evt->u.key.data->key = key;
413 evt->u.key.data->down = down;
Gerd Hoffmann65671472013-11-27 11:38:47 +0100414 return evt;
415}
416
417void qemu_input_event_send_key(QemuConsole *src, KeyValue *key, bool down)
418{
419 InputEvent *evt;
420 evt = qemu_input_event_new_key(key, down);
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200421 if (QTAILQ_EMPTY(&kbd_queue)) {
422 qemu_input_event_send(src, evt);
423 qemu_input_event_sync();
424 qapi_free_InputEvent(evt);
Gerd Hoffmannfa18f362017-04-28 10:42:37 +0200425 } else if (queue_count < queue_limit) {
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200426 qemu_input_queue_event(&kbd_queue, src, evt);
427 qemu_input_queue_sync(&kbd_queue);
linzhechengfca47742017-12-25 10:37:30 +0800428 } else {
429 qapi_free_InputEvent(evt);
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200430 }
Gerd Hoffmann65671472013-11-27 11:38:47 +0100431}
432
433void qemu_input_event_send_key_number(QemuConsole *src, int num, bool down)
434{
Daniel P. Berrangeaf07e5f2017-09-29 11:12:00 +0100435 QKeyCode code = qemu_input_key_number_to_qcode(num);
436 qemu_input_event_send_key_qcode(src, code, down);
Gerd Hoffmann65671472013-11-27 11:38:47 +0100437}
438
439void qemu_input_event_send_key_qcode(QemuConsole *src, QKeyCode q, bool down)
440{
441 KeyValue *key = g_new0(KeyValue, 1);
Eric Blake568c73a2015-10-26 16:34:58 -0600442 key->type = KEY_VALUE_KIND_QCODE;
Eric Blake32bafa82016-03-17 16:48:37 -0600443 key->u.qcode.data = q;
Gerd Hoffmann65671472013-11-27 11:38:47 +0100444 qemu_input_event_send_key(src, key, down);
445}
Gerd Hoffmann43579402013-11-27 18:24:29 +0100446
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200447void qemu_input_event_send_key_delay(uint32_t delay_ms)
448{
Marc-André Lureau05c66382017-04-25 17:05:20 +0400449 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
450 return;
451 }
452
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200453 if (!kbd_timer) {
Artem Pisarenkoe81f8672018-10-17 14:24:20 +0600454 kbd_timer = timer_new_full(NULL, QEMU_CLOCK_VIRTUAL,
455 SCALE_MS, QEMU_TIMER_ATTR_EXTERNAL,
456 qemu_input_queue_process, &kbd_queue);
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200457 }
Gerd Hoffmannfa18f362017-04-28 10:42:37 +0200458 if (queue_count < queue_limit) {
459 qemu_input_queue_delay(&kbd_queue, kbd_timer,
460 delay_ms ? delay_ms : kbd_default_delay_ms);
461 }
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200462}
463
Gerd Hoffmann43579402013-11-27 18:24:29 +0100464void qemu_input_queue_btn(QemuConsole *src, InputButton btn, bool down)
465{
Gerd Hoffmann7a1b46e2018-12-10 15:08:08 +0100466 InputBtnEvent bevt = {
467 .button = btn,
468 .down = down,
469 };
470 InputEvent evt = {
471 .type = INPUT_EVENT_KIND_BTN,
472 .u.btn.data = &bevt,
473 };
474
475 qemu_input_event_send(src, &evt);
Gerd Hoffmann43579402013-11-27 18:24:29 +0100476}
477
478void 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 Blake7fb1cf12015-11-18 01:52:57 -0700484 for (btn = 0; btn < INPUT_BUTTON__MAX; btn++) {
Gerd Hoffmann43579402013-11-27 18:24:29 +0100485 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 Hoffmann502c8db2013-11-28 11:31:09 +0100493bool qemu_input_is_absolute(void)
494{
495 QemuInputHandlerState *s;
496
Gerd Hoffmann6f5943c2014-05-19 15:18:37 +0200497 s = qemu_input_find_handler(INPUT_EVENT_MASK_REL | INPUT_EVENT_MASK_ABS,
498 NULL);
Gerd Hoffmann502c8db2013-11-28 11:31:09 +0100499 return (s != NULL) && (s->handler->mask & INPUT_EVENT_MASK_ABS);
500}
501
Philippe Voinov9cfa7ab2017-05-05 15:39:52 +0200502int qemu_input_scale_axis(int value,
503 int min_in, int max_in,
504 int min_out, int max_out)
Gerd Hoffmann43579402013-11-27 18:24:29 +0100505{
Philippe Voinov9cfa7ab2017-05-05 15:39:52 +0200506 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 Hoffmann43579402013-11-27 18:24:29 +0100511 }
Philippe Voinov9cfa7ab2017-05-05 15:39:52 +0200512 return ((int64_t)value - min_in) * range_out / range_in + min_out;
Gerd Hoffmann43579402013-11-27 18:24:29 +0100513}
514
Gerd Hoffmann43579402013-11-27 18:24:29 +0100515void qemu_input_queue_rel(QemuConsole *src, InputAxis axis, int value)
516{
Gerd Hoffmann7a1b46e2018-12-10 15:08:08 +0100517 InputMoveEvent move = {
518 .axis = axis,
519 .value = value,
520 };
521 InputEvent evt = {
522 .type = INPUT_EVENT_KIND_REL,
523 .u.rel.data = &move,
524 };
525
526 qemu_input_event_send(src, &evt);
Gerd Hoffmann43579402013-11-27 18:24:29 +0100527}
528
Philippe Voinov9cfa7ab2017-05-05 15:39:52 +0200529void qemu_input_queue_abs(QemuConsole *src, InputAxis axis, int value,
530 int min_in, int max_in)
Gerd Hoffmann43579402013-11-27 18:24:29 +0100531{
Gerd Hoffmann7a1b46e2018-12-10 15:08:08 +0100532 InputMoveEvent move = {
533 .axis = axis,
534 .value = qemu_input_scale_axis(value, min_in, max_in,
Philippe Voinov9cfa7ab2017-05-05 15:39:52 +0200535 INPUT_EVENT_ABS_MIN,
Gerd Hoffmann7a1b46e2018-12-10 15:08:08 +0100536 INPUT_EVENT_ABS_MAX),
537 };
538 InputEvent evt = {
539 .type = INPUT_EVENT_KIND_ABS,
540 .u.abs.data = &move,
541 };
542
543 qemu_input_event_send(src, &evt);
Gerd Hoffmann43579402013-11-27 18:24:29 +0100544}
Gerd Hoffmann4a33f452013-12-05 11:23:42 +0100545
546void qemu_input_check_mode_change(void)
547{
548 static int current_is_absolute;
549 int is_absolute;
550
551 is_absolute = qemu_input_is_absolute();
552
553 if (is_absolute != current_is_absolute) {
Gerd Hoffmanna8dfb1c2013-12-05 11:24:14 +0100554 trace_input_mouse_mode(is_absolute);
Gerd Hoffmann4a33f452013-12-05 11:23:42 +0100555 notifier_list_notify(&mouse_mode_notifiers, NULL);
556 }
557
558 current_is_absolute = is_absolute;
559}
560
561void qemu_add_mouse_mode_change_notifier(Notifier *notify)
562{
563 notifier_list_add(&mouse_mode_notifiers, notify);
564}
565
566void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
567{
568 notifier_remove(notify);
569}
Gerd Hoffmanne842c682013-12-10 17:09:36 +0100570
571MouseInfoList *qmp_query_mice(Error **errp)
572{
573 MouseInfoList *mice_list = NULL;
Eric Blake54aa3de2020-11-12 19:13:37 -0600574 MouseInfo *info;
Gerd Hoffmanne842c682013-12-10 17:09:36 +0100575 QemuInputHandlerState *s;
576 bool current = true;
577
578 QTAILQ_FOREACH(s, &handlers, node) {
579 if (!(s->handler->mask &
580 (INPUT_EVENT_MASK_REL | INPUT_EVENT_MASK_ABS))) {
581 continue;
582 }
583
Eric Blake54aa3de2020-11-12 19:13:37 -0600584 info = g_new0(MouseInfo, 1);
585 info->index = s->id;
586 info->name = g_strdup(s->handler->name);
587 info->absolute = s->handler->mask & INPUT_EVENT_MASK_ABS;
588 info->current = current;
Gerd Hoffmanne842c682013-12-10 17:09:36 +0100589
590 current = false;
Eric Blake54aa3de2020-11-12 19:13:37 -0600591 QAPI_LIST_PREPEND(mice_list, info);
Gerd Hoffmanne842c682013-12-10 17:09:36 +0100592 }
593
594 return mice_list;
595}
Gerd Hoffmann70b52f62013-12-10 17:16:03 +0100596
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100597void hmp_mouse_set(Monitor *mon, const QDict *qdict)
Gerd Hoffmann70b52f62013-12-10 17:16:03 +0100598{
599 QemuInputHandlerState *s;
600 int index = qdict_get_int(qdict, "index");
601 int found = 0;
602
603 QTAILQ_FOREACH(s, &handlers, node) {
Hani Benhabiles0419f782014-03-31 23:09:06 +0100604 if (s->id != index) {
605 continue;
Gerd Hoffmann70b52f62013-12-10 17:16:03 +0100606 }
Hani Benhabiles0419f782014-03-31 23:09:06 +0100607 if (!(s->handler->mask & (INPUT_EVENT_MASK_REL |
608 INPUT_EVENT_MASK_ABS))) {
609 error_report("Input device '%s' is not a mouse", s->handler->name);
610 return;
611 }
612 found = 1;
613 qemu_input_handler_activate(s);
614 break;
Gerd Hoffmann70b52f62013-12-10 17:16:03 +0100615 }
616
617 if (!found) {
Hani Benhabiles0419f782014-03-31 23:09:06 +0100618 error_report("Mouse at index '%d' not found", index);
Gerd Hoffmann70b52f62013-12-10 17:16:03 +0100619 }
620
621 qemu_input_check_mode_change();
622}