blob: 51b1019252c6d913859f8b62372529bc68114d42 [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"
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +010011
12struct QemuInputHandlerState {
13 DeviceState *dev;
14 QemuInputHandler *handler;
15 int id;
16 int events;
Gerd Hoffmann6f5943c2014-05-19 15:18:37 +020017 QemuConsole *con;
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +010018 QTAILQ_ENTRY(QemuInputHandlerState) node;
19};
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +020020
21typedef struct QemuInputEventQueue QemuInputEventQueue;
22struct 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 Hoffmannc8b405b2013-11-27 10:35:26 +010035static QTAILQ_HEAD(, QemuInputHandlerState) handlers =
36 QTAILQ_HEAD_INITIALIZER(handlers);
Gerd Hoffmann4a33f452013-12-05 11:23:42 +010037static NotifierList mouse_mode_notifiers =
38 NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers);
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +010039
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +020040static QTAILQ_HEAD(QemuInputEventQueueHead, QemuInputEventQueue) kbd_queue =
41 QTAILQ_HEAD_INITIALIZER(kbd_queue);
42static QEMUTimer *kbd_timer;
43static uint32_t kbd_default_delay_ms = 10;
Gerd Hoffmannfa18f362017-04-28 10:42:37 +020044static uint32_t queue_count;
45static uint32_t queue_limit = 1024;
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +020046
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +010047QemuInputHandlerState *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 Hoffmann4a33f452013-12-05 11:23:42 +010057
58 qemu_input_check_mode_change();
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +010059 return s;
60}
61
62void qemu_input_handler_activate(QemuInputHandlerState *s)
63{
64 QTAILQ_REMOVE(&handlers, s, node);
65 QTAILQ_INSERT_HEAD(&handlers, s, node);
Gerd Hoffmann4a33f452013-12-05 11:23:42 +010066 qemu_input_check_mode_change();
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +010067}
68
Gerd Hoffmann528728f2014-03-16 00:38:45 +010069void 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 Hoffmannc8b405b2013-11-27 10:35:26 +010076void qemu_input_handler_unregister(QemuInputHandlerState *s)
77{
78 QTAILQ_REMOVE(&handlers, s, node);
79 g_free(s);
Gerd Hoffmann4a33f452013-12-05 11:23:42 +010080 qemu_input_check_mode_change();
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +010081}
82
Gerd Hoffmann6f5943c2014-05-19 15:18:37 +020083void qemu_input_handler_bind(QemuInputHandlerState *s,
84 const char *device_id, int head,
85 Error **errp)
86{
Gerd Hoffmann6f5943c2014-05-19 15:18:37 +020087 QemuConsole *con;
Gerd Hoffmannf2c1d542016-01-12 11:45:43 +010088 Error *err = NULL;
Gerd Hoffmann6f5943c2014-05-19 15:18:37 +020089
Gerd Hoffmannf2c1d542016-01-12 11:45:43 +010090 con = qemu_console_lookup_by_device_name(device_id, head, &err);
91 if (err) {
92 error_propagate(errp, err);
Gerd Hoffmann6f5943c2014-05-19 15:18:37 +020093 return;
94 }
95
96 s->con = con;
97}
98
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +010099static QemuInputHandlerState*
Gerd Hoffmann6f5943c2014-05-19 15:18:37 +0200100qemu_input_find_handler(uint32_t mask, QemuConsole *con)
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +0100101{
102 QemuInputHandlerState *s;
103
104 QTAILQ_FOREACH(s, &handlers, node) {
Gerd Hoffmann6f5943c2014-05-19 15:18:37 +0200105 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 Hoffmannc8b405b2013-11-27 10:35:26 +0100117 if (mask & s->handler->mask) {
118 return s;
119 }
120 }
121 return NULL;
122}
123
Gerd Hoffmann6575ccd2016-01-12 12:39:33 +0100124void qmp_input_send_event(bool has_device, const char *device,
125 bool has_head, int64_t head,
126 InputEventList *events, Error **errp)
Marcelo Tosatti50c66172014-09-30 18:10:17 -0300127{
128 InputEventList *e;
129 QemuConsole *con;
Gerd Hoffmannb98d26e2016-01-12 12:11:14 +0100130 Error *err = NULL;
Marcelo Tosatti50c66172014-09-30 18:10:17 -0300131
Amos Kong51fc4472014-11-07 12:41:25 +0800132 con = NULL;
Gerd Hoffmannb98d26e2016-01-12 12:11:14 +0100133 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 Kong51fc4472014-11-07 12:41:25 +0800140 return;
141 }
Marcelo Tosatti50c66172014-09-30 18:10:17 -0300142 }
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 Blake568c73a2015-10-26 16:34:58 -0600152 if (!qemu_input_find_handler(1 << event->type, con)) {
Marcelo Tosatti50c66172014-09-30 18:10:17 -0300153 error_setg(errp, "Input handler not found for "
154 "event type %s",
Markus Armbruster977c7362017-08-24 10:46:08 +0200155 InputEventKind_str(event->type));
Marcelo Tosatti50c66172014-09-30 18:10:17 -0300156 return;
157 }
158 }
159
160 for (e = events; e != NULL; e = e->next) {
Daniel P. Berrangeaf07e5f2017-09-29 11:12:00 +0100161 InputEvent *evt = e->value;
Marcelo Tosatti50c66172014-09-30 18:10:17 -0300162
Daniel P. Berrangeaf07e5f2017-09-29 11:12:00 +0100163 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. Berrange237925b2017-10-19 15:28:42 +0100166 QKeyCode code = qemu_input_key_number_to_qcode(key->u.number.data);
Daniel P. Berrangeaf07e5f2017-09-29 11:12:00 +0100167 qemu_input_event_send_key_qcode(con, code, evt->u.key.data->down);
168 } else {
169 qemu_input_event_send(con, evt);
170 }
Marcelo Tosatti50c66172014-09-30 18:10:17 -0300171 }
172
173 qemu_input_event_sync();
174}
175
Philippe Voinov9cfa7ab2017-05-05 15:39:52 +0200176static 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 Hoffmannd3535432013-11-28 11:29:33 +0100181static void qemu_input_transform_abs_rotate(InputEvent *evt)
182{
Eric Blake32bafa82016-03-17 16:48:37 -0600183 InputMoveEvent *move = evt->u.abs.data;
Gerd Hoffmannd3535432013-11-28 11:29:33 +0100184 switch (graphic_rotate) {
185 case 90:
Eric Blakeb5a1b442016-03-03 09:16:49 -0700186 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 Voinov9cfa7ab2017-05-05 15:39:52 +0200190 move->value = qemu_input_transform_invert_abs_value(move->value);
Gerd Hoffmannd3535432013-11-28 11:29:33 +0100191 }
192 break;
193 case 180:
Philippe Voinov9cfa7ab2017-05-05 15:39:52 +0200194 move->value = qemu_input_transform_invert_abs_value(move->value);
Gerd Hoffmannd3535432013-11-28 11:29:33 +0100195 break;
196 case 270:
Eric Blakeb5a1b442016-03-03 09:16:49 -0700197 if (move->axis == INPUT_AXIS_X) {
198 move->axis = INPUT_AXIS_Y;
Philippe Voinov9cfa7ab2017-05-05 15:39:52 +0200199 move->value = qemu_input_transform_invert_abs_value(move->value);
Eric Blakeb5a1b442016-03-03 09:16:49 -0700200 } else if (move->axis == INPUT_AXIS_Y) {
201 move->axis = INPUT_AXIS_X;
Gerd Hoffmannd3535432013-11-28 11:29:33 +0100202 }
203 break;
204 }
205}
206
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100207static void qemu_input_event_trace(QemuConsole *src, InputEvent *evt)
208{
209 const char *name;
Gerd Hoffmann2386a902014-05-21 13:49:59 +0200210 int qcode, idx = -1;
Eric Blakeb5a1b442016-03-03 09:16:49 -0700211 InputKeyEvent *key;
212 InputBtnEvent *btn;
213 InputMoveEvent *move;
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100214
215 if (src) {
216 idx = qemu_console_get_index(src);
217 }
Eric Blake568c73a2015-10-26 16:34:58 -0600218 switch (evt->type) {
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100219 case INPUT_EVENT_KIND_KEY:
Eric Blake32bafa82016-03-17 16:48:37 -0600220 key = evt->u.key.data;
Eric Blakeb5a1b442016-03-03 09:16:49 -0700221 switch (key->key->type) {
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100222 case KEY_VALUE_KIND_NUMBER:
Eric Blake32bafa82016-03-17 16:48:37 -0600223 qcode = qemu_input_key_number_to_qcode(key->key->u.number.data);
Markus Armbruster977c7362017-08-24 10:46:08 +0200224 name = QKeyCode_str(qcode);
Eric Blake32bafa82016-03-17 16:48:37 -0600225 trace_input_event_key_number(idx, key->key->u.number.data,
Eric Blakeb5a1b442016-03-03 09:16:49 -0700226 name, key->down);
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100227 break;
228 case KEY_VALUE_KIND_QCODE:
Markus Armbruster977c7362017-08-24 10:46:08 +0200229 name = QKeyCode_str(key->key->u.qcode.data);
Eric Blakeb5a1b442016-03-03 09:16:49 -0700230 trace_input_event_key_qcode(idx, name, key->down);
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100231 break;
Eric Blake7fb1cf12015-11-18 01:52:57 -0700232 case KEY_VALUE_KIND__MAX:
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100233 /* keep gcc happy */
234 break;
235 }
236 break;
237 case INPUT_EVENT_KIND_BTN:
Eric Blake32bafa82016-03-17 16:48:37 -0600238 btn = evt->u.btn.data;
Markus Armbruster977c7362017-08-24 10:46:08 +0200239 name = InputButton_str(btn->button);
Eric Blakeb5a1b442016-03-03 09:16:49 -0700240 trace_input_event_btn(idx, name, btn->down);
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100241 break;
242 case INPUT_EVENT_KIND_REL:
Eric Blake32bafa82016-03-17 16:48:37 -0600243 move = evt->u.rel.data;
Markus Armbruster977c7362017-08-24 10:46:08 +0200244 name = InputAxis_str(move->axis);
Eric Blakeb5a1b442016-03-03 09:16:49 -0700245 trace_input_event_rel(idx, name, move->value);
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100246 break;
247 case INPUT_EVENT_KIND_ABS:
Eric Blake32bafa82016-03-17 16:48:37 -0600248 move = evt->u.abs.data;
Markus Armbruster977c7362017-08-24 10:46:08 +0200249 name = InputAxis_str(move->axis);
Eric Blakeb5a1b442016-03-03 09:16:49 -0700250 trace_input_event_abs(idx, name, move->value);
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100251 break;
Eric Blake7fb1cf12015-11-18 01:52:57 -0700252 case INPUT_EVENT_KIND__MAX:
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100253 /* keep gcc happy */
254 break;
255 }
256}
257
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200258static 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 Graf77b03592017-06-22 09:41:58 +0200267 queue_count--;
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200268 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 Hoffmannfa18f362017-04-28 10:42:37 +0200286 queue_count--;
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200287 g_free(item);
288 }
289}
290
291static 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 Hoffmannfa18f362017-04-28 10:42:37 +0200301 queue_count++;
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200302
303 if (start_timer) {
304 timer_mod(item->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL)
305 + item->delay_ms);
306 }
307}
308
309static 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 Hoffmannfa18f362017-04-28 10:42:37 +0200318 queue_count++;
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200319}
320
321static 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 Hoffmannfa18f362017-04-28 10:42:37 +0200327 queue_count++;
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200328}
329
Pavel Dovgalyukee312992015-09-17 19:25:24 +0300330void qemu_input_event_send_impl(QemuConsole *src, InputEvent *evt)
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +0100331{
332 QemuInputHandlerState *s;
333
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100334 qemu_input_event_trace(src, evt);
335
Gerd Hoffmannd3535432013-11-28 11:29:33 +0100336 /* pre processing */
Eric Blake568c73a2015-10-26 16:34:58 -0600337 if (graphic_rotate && (evt->type == INPUT_EVENT_KIND_ABS)) {
Gerd Hoffmannd3535432013-11-28 11:29:33 +0100338 qemu_input_transform_abs_rotate(evt);
339 }
340
341 /* send event */
Eric Blake568c73a2015-10-26 16:34:58 -0600342 s = qemu_input_find_handler(1 << evt->type, src);
Gerd Hoffmannbdcc3a22014-03-31 16:07:30 +0200343 if (!s) {
344 return;
345 }
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +0100346 s->handler->event(s->dev, src, evt);
347 s->events++;
348}
349
Pavel Dovgalyukee312992015-09-17 19:25:24 +0300350void qemu_input_event_send(QemuConsole *src, InputEvent *evt)
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +0100351{
Daniel P. Berrangeaf07e5f2017-09-29 11:12:00 +0100352 /* 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. Berrange80b857f2017-10-19 15:28:47 +0100357
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 Hoffmannc8b405b2013-11-27 10:35:26 +0100371 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
372 return;
373 }
374
Pavel Dovgalyukee312992015-09-17 19:25:24 +0300375 replay_input_event(src, evt);
376}
377
378void qemu_input_event_sync_impl(void)
379{
380 QemuInputHandlerState *s;
381
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100382 trace_input_event_sync();
383
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +0100384 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 Hoffmann65671472013-11-27 11:38:47 +0100394
Pavel Dovgalyukee312992015-09-17 19:25:24 +0300395void 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. Berrange3ad35e72017-09-29 11:12:01 +0100404static InputEvent *qemu_input_event_new_key(KeyValue *key, bool down)
Gerd Hoffmann65671472013-11-27 11:38:47 +0100405{
406 InputEvent *evt = g_new0(InputEvent, 1);
Eric Blake32bafa82016-03-17 16:48:37 -0600407 evt->u.key.data = g_new0(InputKeyEvent, 1);
Eric Blake568c73a2015-10-26 16:34:58 -0600408 evt->type = INPUT_EVENT_KIND_KEY;
Eric Blake32bafa82016-03-17 16:48:37 -0600409 evt->u.key.data->key = key;
410 evt->u.key.data->down = down;
Gerd Hoffmann65671472013-11-27 11:38:47 +0100411 return evt;
412}
413
414void 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 Hoffmannbe1a7172014-05-28 13:02:40 +0200418 if (QTAILQ_EMPTY(&kbd_queue)) {
419 qemu_input_event_send(src, evt);
420 qemu_input_event_sync();
421 qapi_free_InputEvent(evt);
Gerd Hoffmannfa18f362017-04-28 10:42:37 +0200422 } else if (queue_count < queue_limit) {
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200423 qemu_input_queue_event(&kbd_queue, src, evt);
424 qemu_input_queue_sync(&kbd_queue);
linzhechengfca47742017-12-25 10:37:30 +0800425 } else {
426 qapi_free_InputEvent(evt);
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200427 }
Gerd Hoffmann65671472013-11-27 11:38:47 +0100428}
429
430void qemu_input_event_send_key_number(QemuConsole *src, int num, bool down)
431{
Daniel P. Berrangeaf07e5f2017-09-29 11:12:00 +0100432 QKeyCode code = qemu_input_key_number_to_qcode(num);
433 qemu_input_event_send_key_qcode(src, code, down);
Gerd Hoffmann65671472013-11-27 11:38:47 +0100434}
435
436void qemu_input_event_send_key_qcode(QemuConsole *src, QKeyCode q, bool down)
437{
438 KeyValue *key = g_new0(KeyValue, 1);
Eric Blake568c73a2015-10-26 16:34:58 -0600439 key->type = KEY_VALUE_KIND_QCODE;
Eric Blake32bafa82016-03-17 16:48:37 -0600440 key->u.qcode.data = q;
Gerd Hoffmann65671472013-11-27 11:38:47 +0100441 qemu_input_event_send_key(src, key, down);
442}
Gerd Hoffmann43579402013-11-27 18:24:29 +0100443
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200444void qemu_input_event_send_key_delay(uint32_t delay_ms)
445{
Marc-André Lureau05c66382017-04-25 17:05:20 +0400446 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
447 return;
448 }
449
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200450 if (!kbd_timer) {
451 kbd_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, qemu_input_queue_process,
452 &kbd_queue);
453 }
Gerd Hoffmannfa18f362017-04-28 10:42:37 +0200454 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 Hoffmannbe1a7172014-05-28 13:02:40 +0200458}
459
Gerd Hoffmann43579402013-11-27 18:24:29 +0100460InputEvent *qemu_input_event_new_btn(InputButton btn, bool down)
461{
462 InputEvent *evt = g_new0(InputEvent, 1);
Eric Blake32bafa82016-03-17 16:48:37 -0600463 evt->u.btn.data = g_new0(InputBtnEvent, 1);
Eric Blake568c73a2015-10-26 16:34:58 -0600464 evt->type = INPUT_EVENT_KIND_BTN;
Eric Blake32bafa82016-03-17 16:48:37 -0600465 evt->u.btn.data->button = btn;
466 evt->u.btn.data->down = down;
Gerd Hoffmann43579402013-11-27 18:24:29 +0100467 return evt;
468}
469
470void 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
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
515InputEvent *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 Blake568c73a2015-10-26 16:34:58 -0600521 evt->type = kind;
Eric Blake32bafa82016-03-17 16:48:37 -0600522 evt->u.rel.data = move; /* evt->u.rel is the same as evt->u.abs */
Gerd Hoffmann43579402013-11-27 18:24:29 +0100523 move->axis = axis;
524 move->value = value;
525 return evt;
526}
527
528void 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 Voinov9cfa7ab2017-05-05 15:39:52 +0200536void qemu_input_queue_abs(QemuConsole *src, InputAxis axis, int value,
537 int min_in, int max_in)
Gerd Hoffmann43579402013-11-27 18:24:29 +0100538{
539 InputEvent *evt;
Philippe Voinov9cfa7ab2017-05-05 15:39:52 +0200540 int scaled = qemu_input_scale_axis(value, min_in, max_in,
541 INPUT_EVENT_ABS_MIN,
542 INPUT_EVENT_ABS_MAX);
Gerd Hoffmann43579402013-11-27 18:24:29 +0100543 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 Hoffmann4a33f452013-12-05 11:23:42 +0100547
548void 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 Hoffmanna8dfb1c2013-12-05 11:24:14 +0100556 trace_input_mouse_mode(is_absolute);
Gerd Hoffmann4a33f452013-12-05 11:23:42 +0100557 notifier_list_notify(&mouse_mode_notifiers, NULL);
558 }
559
560 current_is_absolute = is_absolute;
561}
562
563void qemu_add_mouse_mode_change_notifier(Notifier *notify)
564{
565 notifier_list_add(&mouse_mode_notifiers, notify);
566}
567
568void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
569{
570 notifier_remove(notify);
571}
Gerd Hoffmanne842c682013-12-10 17:09:36 +0100572
573MouseInfoList *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 Hoffmann70b52f62013-12-10 17:16:03 +0100600
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100601void hmp_mouse_set(Monitor *mon, const QDict *qdict)
Gerd Hoffmann70b52f62013-12-10 17:16:03 +0100602{
603 QemuInputHandlerState *s;
604 int index = qdict_get_int(qdict, "index");
605 int found = 0;
606
607 QTAILQ_FOREACH(s, &handlers, node) {
Hani Benhabiles0419f782014-03-31 23:09:06 +0100608 if (s->id != index) {
609 continue;
Gerd Hoffmann70b52f62013-12-10 17:16:03 +0100610 }
Hani Benhabiles0419f782014-03-31 23:09:06 +0100611 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 Hoffmann70b52f62013-12-10 17:16:03 +0100619 }
620
621 if (!found) {
Hani Benhabiles0419f782014-03-31 23:09:06 +0100622 error_report("Mouse at index '%d' not found", index);
Gerd Hoffmann70b52f62013-12-10 17:16:03 +0100623 }
624
625 qemu_input_check_mode_change();
626}