blob: ed88cda6d6692fdfcf2d8e6c88531e3e2d111d02 [file] [log] [blame]
Peter Maydelle16f4c82016-01-29 17:49:51 +00001#include "qemu/osdep.h"
Gerd Hoffmann6f5943c2014-05-19 15:18:37 +02002#include "hw/qdev.h"
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +01003#include "sysemu/sysemu.h"
4#include "qapi-types.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +01005#include "qemu/error-report.h"
Gerd Hoffmanne842c682013-12-10 17:09:36 +01006#include "qmp-commands.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;
44
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +010045QemuInputHandlerState *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 Hoffmann4a33f452013-12-05 11:23:42 +010055
56 qemu_input_check_mode_change();
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +010057 return s;
58}
59
60void qemu_input_handler_activate(QemuInputHandlerState *s)
61{
62 QTAILQ_REMOVE(&handlers, s, node);
63 QTAILQ_INSERT_HEAD(&handlers, s, node);
Gerd Hoffmann4a33f452013-12-05 11:23:42 +010064 qemu_input_check_mode_change();
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +010065}
66
Gerd Hoffmann528728f2014-03-16 00:38:45 +010067void 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 Hoffmannc8b405b2013-11-27 10:35:26 +010074void qemu_input_handler_unregister(QemuInputHandlerState *s)
75{
76 QTAILQ_REMOVE(&handlers, s, node);
77 g_free(s);
Gerd Hoffmann4a33f452013-12-05 11:23:42 +010078 qemu_input_check_mode_change();
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +010079}
80
Gerd Hoffmann6f5943c2014-05-19 15:18:37 +020081void qemu_input_handler_bind(QemuInputHandlerState *s,
82 const char *device_id, int head,
83 Error **errp)
84{
Gerd Hoffmann6f5943c2014-05-19 15:18:37 +020085 QemuConsole *con;
Gerd Hoffmannf2c1d542016-01-12 11:45:43 +010086 Error *err = NULL;
Gerd Hoffmann6f5943c2014-05-19 15:18:37 +020087
Gerd Hoffmannf2c1d542016-01-12 11:45:43 +010088 con = qemu_console_lookup_by_device_name(device_id, head, &err);
89 if (err) {
90 error_propagate(errp, err);
Gerd Hoffmann6f5943c2014-05-19 15:18:37 +020091 return;
92 }
93
94 s->con = con;
95}
96
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +010097static QemuInputHandlerState*
Gerd Hoffmann6f5943c2014-05-19 15:18:37 +020098qemu_input_find_handler(uint32_t mask, QemuConsole *con)
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +010099{
100 QemuInputHandlerState *s;
101
102 QTAILQ_FOREACH(s, &handlers, node) {
Gerd Hoffmann6f5943c2014-05-19 15:18:37 +0200103 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 Hoffmannc8b405b2013-11-27 10:35:26 +0100115 if (mask & s->handler->mask) {
116 return s;
117 }
118 }
119 return NULL;
120}
121
Gerd Hoffmann6575ccd2016-01-12 12:39:33 +0100122void qmp_input_send_event(bool has_device, const char *device,
123 bool has_head, int64_t head,
124 InputEventList *events, Error **errp)
Marcelo Tosatti50c66172014-09-30 18:10:17 -0300125{
126 InputEventList *e;
127 QemuConsole *con;
Gerd Hoffmannb98d26e2016-01-12 12:11:14 +0100128 Error *err = NULL;
Marcelo Tosatti50c66172014-09-30 18:10:17 -0300129
Amos Kong51fc4472014-11-07 12:41:25 +0800130 con = NULL;
Gerd Hoffmannb98d26e2016-01-12 12:11:14 +0100131 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 Kong51fc4472014-11-07 12:41:25 +0800138 return;
139 }
Marcelo Tosatti50c66172014-09-30 18:10:17 -0300140 }
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 Blake568c73a2015-10-26 16:34:58 -0600150 if (!qemu_input_find_handler(1 << event->type, con)) {
Marcelo Tosatti50c66172014-09-30 18:10:17 -0300151 error_setg(errp, "Input handler not found for "
152 "event type %s",
Eric Blake568c73a2015-10-26 16:34:58 -0600153 InputEventKind_lookup[event->type]);
Marcelo Tosatti50c66172014-09-30 18:10:17 -0300154 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 Hoffmannd3535432013-11-28 11:29:33 +0100167static void qemu_input_transform_abs_rotate(InputEvent *evt)
168{
Eric Blake32bafa82016-03-17 16:48:37 -0600169 InputMoveEvent *move = evt->u.abs.data;
Gerd Hoffmannd3535432013-11-28 11:29:33 +0100170 switch (graphic_rotate) {
171 case 90:
Eric Blakeb5a1b442016-03-03 09:16:49 -0700172 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 Hoffmannd3535432013-11-28 11:29:33 +0100177 }
178 break;
179 case 180:
Eric Blakeb5a1b442016-03-03 09:16:49 -0700180 move->value = INPUT_EVENT_ABS_SIZE - 1 - move->value;
Gerd Hoffmannd3535432013-11-28 11:29:33 +0100181 break;
182 case 270:
Eric Blakeb5a1b442016-03-03 09:16:49 -0700183 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 Hoffmannd3535432013-11-28 11:29:33 +0100188 }
189 break;
190 }
191}
192
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100193static void qemu_input_event_trace(QemuConsole *src, InputEvent *evt)
194{
195 const char *name;
Gerd Hoffmann2386a902014-05-21 13:49:59 +0200196 int qcode, idx = -1;
Eric Blakeb5a1b442016-03-03 09:16:49 -0700197 InputKeyEvent *key;
198 InputBtnEvent *btn;
199 InputMoveEvent *move;
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100200
201 if (src) {
202 idx = qemu_console_get_index(src);
203 }
Eric Blake568c73a2015-10-26 16:34:58 -0600204 switch (evt->type) {
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100205 case INPUT_EVENT_KIND_KEY:
Eric Blake32bafa82016-03-17 16:48:37 -0600206 key = evt->u.key.data;
Eric Blakeb5a1b442016-03-03 09:16:49 -0700207 switch (key->key->type) {
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100208 case KEY_VALUE_KIND_NUMBER:
Eric Blake32bafa82016-03-17 16:48:37 -0600209 qcode = qemu_input_key_number_to_qcode(key->key->u.number.data);
Gerd Hoffmann2386a902014-05-21 13:49:59 +0200210 name = QKeyCode_lookup[qcode];
Eric Blake32bafa82016-03-17 16:48:37 -0600211 trace_input_event_key_number(idx, key->key->u.number.data,
Eric Blakeb5a1b442016-03-03 09:16:49 -0700212 name, key->down);
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100213 break;
214 case KEY_VALUE_KIND_QCODE:
Eric Blake32bafa82016-03-17 16:48:37 -0600215 name = QKeyCode_lookup[key->key->u.qcode.data];
Eric Blakeb5a1b442016-03-03 09:16:49 -0700216 trace_input_event_key_qcode(idx, name, key->down);
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100217 break;
Eric Blake7fb1cf12015-11-18 01:52:57 -0700218 case KEY_VALUE_KIND__MAX:
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100219 /* keep gcc happy */
220 break;
221 }
222 break;
223 case INPUT_EVENT_KIND_BTN:
Eric Blake32bafa82016-03-17 16:48:37 -0600224 btn = evt->u.btn.data;
Eric Blakeb5a1b442016-03-03 09:16:49 -0700225 name = InputButton_lookup[btn->button];
226 trace_input_event_btn(idx, name, btn->down);
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100227 break;
228 case INPUT_EVENT_KIND_REL:
Eric Blake32bafa82016-03-17 16:48:37 -0600229 move = evt->u.rel.data;
Eric Blakeb5a1b442016-03-03 09:16:49 -0700230 name = InputAxis_lookup[move->axis];
231 trace_input_event_rel(idx, name, move->value);
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100232 break;
233 case INPUT_EVENT_KIND_ABS:
Eric Blake32bafa82016-03-17 16:48:37 -0600234 move = evt->u.abs.data;
Eric Blakeb5a1b442016-03-03 09:16:49 -0700235 name = InputAxis_lookup[move->axis];
236 trace_input_event_abs(idx, name, move->value);
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100237 break;
Eric Blake7fb1cf12015-11-18 01:52:57 -0700238 case INPUT_EVENT_KIND__MAX:
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100239 /* keep gcc happy */
240 break;
241 }
242}
243
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200244static 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
275static 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
292static 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
303static 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 Dovgalyukee312992015-09-17 19:25:24 +0300311void qemu_input_event_send_impl(QemuConsole *src, InputEvent *evt)
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +0100312{
313 QemuInputHandlerState *s;
314
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100315 qemu_input_event_trace(src, evt);
316
Gerd Hoffmannd3535432013-11-28 11:29:33 +0100317 /* pre processing */
Eric Blake568c73a2015-10-26 16:34:58 -0600318 if (graphic_rotate && (evt->type == INPUT_EVENT_KIND_ABS)) {
Gerd Hoffmannd3535432013-11-28 11:29:33 +0100319 qemu_input_transform_abs_rotate(evt);
320 }
321
322 /* send event */
Eric Blake568c73a2015-10-26 16:34:58 -0600323 s = qemu_input_find_handler(1 << evt->type, src);
Gerd Hoffmannbdcc3a22014-03-31 16:07:30 +0200324 if (!s) {
325 return;
326 }
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +0100327 s->handler->event(s->dev, src, evt);
328 s->events++;
329}
330
Pavel Dovgalyukee312992015-09-17 19:25:24 +0300331void qemu_input_event_send(QemuConsole *src, InputEvent *evt)
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +0100332{
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +0100333 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
334 return;
335 }
336
Pavel Dovgalyukee312992015-09-17 19:25:24 +0300337 replay_input_event(src, evt);
338}
339
340void qemu_input_event_sync_impl(void)
341{
342 QemuInputHandlerState *s;
343
Gerd Hoffmannc43ce552013-12-04 15:20:05 +0100344 trace_input_event_sync();
345
Gerd Hoffmannc8b405b2013-11-27 10:35:26 +0100346 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 Hoffmann65671472013-11-27 11:38:47 +0100356
Pavel Dovgalyukee312992015-09-17 19:25:24 +0300357void 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 Hoffmann65671472013-11-27 11:38:47 +0100366InputEvent *qemu_input_event_new_key(KeyValue *key, bool down)
367{
368 InputEvent *evt = g_new0(InputEvent, 1);
Eric Blake32bafa82016-03-17 16:48:37 -0600369 evt->u.key.data = g_new0(InputKeyEvent, 1);
Eric Blake568c73a2015-10-26 16:34:58 -0600370 evt->type = INPUT_EVENT_KIND_KEY;
Eric Blake32bafa82016-03-17 16:48:37 -0600371 evt->u.key.data->key = key;
372 evt->u.key.data->down = down;
Gerd Hoffmann65671472013-11-27 11:38:47 +0100373 return evt;
374}
375
376void 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 Hoffmannbe1a7172014-05-28 13:02:40 +0200380 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 Hoffmann65671472013-11-27 11:38:47 +0100388}
389
390void qemu_input_event_send_key_number(QemuConsole *src, int num, bool down)
391{
392 KeyValue *key = g_new0(KeyValue, 1);
Eric Blake568c73a2015-10-26 16:34:58 -0600393 key->type = KEY_VALUE_KIND_NUMBER;
Eric Blake32bafa82016-03-17 16:48:37 -0600394 key->u.number.data = num;
Gerd Hoffmann65671472013-11-27 11:38:47 +0100395 qemu_input_event_send_key(src, key, down);
396}
397
398void qemu_input_event_send_key_qcode(QemuConsole *src, QKeyCode q, bool down)
399{
400 KeyValue *key = g_new0(KeyValue, 1);
Eric Blake568c73a2015-10-26 16:34:58 -0600401 key->type = KEY_VALUE_KIND_QCODE;
Eric Blake32bafa82016-03-17 16:48:37 -0600402 key->u.qcode.data = q;
Gerd Hoffmann65671472013-11-27 11:38:47 +0100403 qemu_input_event_send_key(src, key, down);
404}
Gerd Hoffmann43579402013-11-27 18:24:29 +0100405
Gerd Hoffmannbe1a7172014-05-28 13:02:40 +0200406void 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 Hoffmann43579402013-11-27 18:24:29 +0100416InputEvent *qemu_input_event_new_btn(InputButton btn, bool down)
417{
418 InputEvent *evt = g_new0(InputEvent, 1);
Eric Blake32bafa82016-03-17 16:48:37 -0600419 evt->u.btn.data = g_new0(InputBtnEvent, 1);
Eric Blake568c73a2015-10-26 16:34:58 -0600420 evt->type = INPUT_EVENT_KIND_BTN;
Eric Blake32bafa82016-03-17 16:48:37 -0600421 evt->u.btn.data->button = btn;
422 evt->u.btn.data->down = down;
Gerd Hoffmann43579402013-11-27 18:24:29 +0100423 return evt;
424}
425
426void 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
434void 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 Blake7fb1cf12015-11-18 01:52:57 -0700440 for (btn = 0; btn < INPUT_BUTTON__MAX; btn++) {
Gerd Hoffmann43579402013-11-27 18:24:29 +0100441 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 Hoffmann502c8db2013-11-28 11:31:09 +0100449bool qemu_input_is_absolute(void)
450{
451 QemuInputHandlerState *s;
452
Gerd Hoffmann6f5943c2014-05-19 15:18:37 +0200453 s = qemu_input_find_handler(INPUT_EVENT_MASK_REL | INPUT_EVENT_MASK_ABS,
454 NULL);
Gerd Hoffmann502c8db2013-11-28 11:31:09 +0100455 return (s != NULL) && (s->handler->mask & INPUT_EVENT_MASK_ABS);
456}
457
Gerd Hoffmann43579402013-11-27 18:24:29 +0100458int 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
466InputEvent *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 Blake568c73a2015-10-26 16:34:58 -0600472 evt->type = kind;
Eric Blake32bafa82016-03-17 16:48:37 -0600473 evt->u.rel.data = move; /* evt->u.rel is the same as evt->u.abs */
Gerd Hoffmann43579402013-11-27 18:24:29 +0100474 move->axis = axis;
475 move->value = value;
476 return evt;
477}
478
479void 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
487void 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 Hoffmann4a33f452013-12-05 11:23:42 +0100495
496void 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 Hoffmanna8dfb1c2013-12-05 11:24:14 +0100504 trace_input_mouse_mode(is_absolute);
Gerd Hoffmann4a33f452013-12-05 11:23:42 +0100505 notifier_list_notify(&mouse_mode_notifiers, NULL);
506 }
507
508 current_is_absolute = is_absolute;
509}
510
511void qemu_add_mouse_mode_change_notifier(Notifier *notify)
512{
513 notifier_list_add(&mouse_mode_notifiers, notify);
514}
515
516void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
517{
518 notifier_remove(notify);
519}
Gerd Hoffmanne842c682013-12-10 17:09:36 +0100520
521MouseInfoList *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 Hoffmann70b52f62013-12-10 17:16:03 +0100548
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100549void hmp_mouse_set(Monitor *mon, const QDict *qdict)
Gerd Hoffmann70b52f62013-12-10 17:16:03 +0100550{
551 QemuInputHandlerState *s;
552 int index = qdict_get_int(qdict, "index");
553 int found = 0;
554
555 QTAILQ_FOREACH(s, &handlers, node) {
Hani Benhabiles0419f782014-03-31 23:09:06 +0100556 if (s->id != index) {
557 continue;
Gerd Hoffmann70b52f62013-12-10 17:16:03 +0100558 }
Hani Benhabiles0419f782014-03-31 23:09:06 +0100559 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 Hoffmann70b52f62013-12-10 17:16:03 +0100567 }
568
569 if (!found) {
Hani Benhabiles0419f782014-03-31 23:09:06 +0100570 error_report("Mouse at index '%d' not found", index);
Gerd Hoffmann70b52f62013-12-10 17:16:03 +0100571 }
572
573 qemu_input_check_mode_change();
574}