migration/block: Clean up BBs in block_save_complete()
[qemu/kevin.git] / ui / input.c
blob290ca9f54d47c6b60f329c7d9f9c5b58d20a57c1
1 #include "qemu/osdep.h"
2 #include "hw/qdev.h"
3 #include "sysemu/sysemu.h"
4 #include "qapi-types.h"
5 #include "qemu/error-report.h"
6 #include "qmp-commands.h"
7 #include "trace.h"
8 #include "ui/input.h"
9 #include "ui/console.h"
10 #include "sysemu/replay.h"
12 struct QemuInputHandlerState {
13 DeviceState *dev;
14 QemuInputHandler *handler;
15 int id;
16 int events;
17 QemuConsole *con;
18 QTAILQ_ENTRY(QemuInputHandlerState) node;
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;
35 static QTAILQ_HEAD(, QemuInputHandlerState) handlers =
36 QTAILQ_HEAD_INITIALIZER(handlers);
37 static NotifierList mouse_mode_notifiers =
38 NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers);
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 static uint32_t queue_count;
45 static uint32_t queue_limit = 1024;
47 QemuInputHandlerState *qemu_input_handler_register(DeviceState *dev,
48 QemuInputHandler *handler)
50 QemuInputHandlerState *s = g_new0(QemuInputHandlerState, 1);
51 static int id = 1;
53 s->dev = dev;
54 s->handler = handler;
55 s->id = id++;
56 QTAILQ_INSERT_TAIL(&handlers, s, node);
58 qemu_input_check_mode_change();
59 return s;
62 void qemu_input_handler_activate(QemuInputHandlerState *s)
64 QTAILQ_REMOVE(&handlers, s, node);
65 QTAILQ_INSERT_HEAD(&handlers, s, node);
66 qemu_input_check_mode_change();
69 void qemu_input_handler_deactivate(QemuInputHandlerState *s)
71 QTAILQ_REMOVE(&handlers, s, node);
72 QTAILQ_INSERT_TAIL(&handlers, s, node);
73 qemu_input_check_mode_change();
76 void qemu_input_handler_unregister(QemuInputHandlerState *s)
78 QTAILQ_REMOVE(&handlers, s, node);
79 g_free(s);
80 qemu_input_check_mode_change();
83 void qemu_input_handler_bind(QemuInputHandlerState *s,
84 const char *device_id, int head,
85 Error **errp)
87 QemuConsole *con;
88 Error *err = NULL;
90 con = qemu_console_lookup_by_device_name(device_id, head, &err);
91 if (err) {
92 error_propagate(errp, err);
93 return;
96 s->con = con;
99 static QemuInputHandlerState*
100 qemu_input_find_handler(uint32_t mask, QemuConsole *con)
102 QemuInputHandlerState *s;
104 QTAILQ_FOREACH(s, &handlers, node) {
105 if (s->con == NULL || s->con != con) {
106 continue;
108 if (mask & s->handler->mask) {
109 return s;
113 QTAILQ_FOREACH(s, &handlers, node) {
114 if (s->con != NULL) {
115 continue;
117 if (mask & s->handler->mask) {
118 return s;
121 return NULL;
124 void qmp_input_send_event(bool has_device, const char *device,
125 bool has_head, int64_t head,
126 InputEventList *events, Error **errp)
128 InputEventList *e;
129 QemuConsole *con;
130 Error *err = NULL;
132 con = NULL;
133 if (has_device) {
134 if (!has_head) {
135 head = 0;
137 con = qemu_console_lookup_by_device_name(device, head, &err);
138 if (err) {
139 error_propagate(errp, err);
140 return;
144 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
145 error_setg(errp, "VM not running");
146 return;
149 for (e = events; e != NULL; e = e->next) {
150 InputEvent *event = e->value;
152 if (!qemu_input_find_handler(1 << event->type, con)) {
153 error_setg(errp, "Input handler not found for "
154 "event type %s",
155 InputEventKind_lookup[event->type]);
156 return;
160 for (e = events; e != NULL; e = e->next) {
161 InputEvent *event = e->value;
163 qemu_input_event_send(con, event);
166 qemu_input_event_sync();
169 static int qemu_input_transform_invert_abs_value(int value)
171 return (int64_t)INPUT_EVENT_ABS_MAX - value + INPUT_EVENT_ABS_MIN;
174 static void qemu_input_transform_abs_rotate(InputEvent *evt)
176 InputMoveEvent *move = evt->u.abs.data;
177 switch (graphic_rotate) {
178 case 90:
179 if (move->axis == INPUT_AXIS_X) {
180 move->axis = INPUT_AXIS_Y;
181 } else if (move->axis == INPUT_AXIS_Y) {
182 move->axis = INPUT_AXIS_X;
183 move->value = qemu_input_transform_invert_abs_value(move->value);
185 break;
186 case 180:
187 move->value = qemu_input_transform_invert_abs_value(move->value);
188 break;
189 case 270:
190 if (move->axis == INPUT_AXIS_X) {
191 move->axis = INPUT_AXIS_Y;
192 move->value = qemu_input_transform_invert_abs_value(move->value);
193 } else if (move->axis == INPUT_AXIS_Y) {
194 move->axis = INPUT_AXIS_X;
196 break;
200 static void qemu_input_event_trace(QemuConsole *src, InputEvent *evt)
202 const char *name;
203 int qcode, idx = -1;
204 InputKeyEvent *key;
205 InputBtnEvent *btn;
206 InputMoveEvent *move;
208 if (src) {
209 idx = qemu_console_get_index(src);
211 switch (evt->type) {
212 case INPUT_EVENT_KIND_KEY:
213 key = evt->u.key.data;
214 switch (key->key->type) {
215 case KEY_VALUE_KIND_NUMBER:
216 qcode = qemu_input_key_number_to_qcode(key->key->u.number.data);
217 name = QKeyCode_lookup[qcode];
218 trace_input_event_key_number(idx, key->key->u.number.data,
219 name, key->down);
220 break;
221 case KEY_VALUE_KIND_QCODE:
222 name = QKeyCode_lookup[key->key->u.qcode.data];
223 trace_input_event_key_qcode(idx, name, key->down);
224 break;
225 case KEY_VALUE_KIND__MAX:
226 /* keep gcc happy */
227 break;
229 break;
230 case INPUT_EVENT_KIND_BTN:
231 btn = evt->u.btn.data;
232 name = InputButton_lookup[btn->button];
233 trace_input_event_btn(idx, name, btn->down);
234 break;
235 case INPUT_EVENT_KIND_REL:
236 move = evt->u.rel.data;
237 name = InputAxis_lookup[move->axis];
238 trace_input_event_rel(idx, name, move->value);
239 break;
240 case INPUT_EVENT_KIND_ABS:
241 move = evt->u.abs.data;
242 name = InputAxis_lookup[move->axis];
243 trace_input_event_abs(idx, name, move->value);
244 break;
245 case INPUT_EVENT_KIND__MAX:
246 /* keep gcc happy */
247 break;
251 static void qemu_input_queue_process(void *opaque)
253 struct QemuInputEventQueueHead *queue = opaque;
254 QemuInputEventQueue *item;
256 g_assert(!QTAILQ_EMPTY(queue));
257 item = QTAILQ_FIRST(queue);
258 g_assert(item->type == QEMU_INPUT_QUEUE_DELAY);
259 QTAILQ_REMOVE(queue, item, node);
260 g_free(item);
262 while (!QTAILQ_EMPTY(queue)) {
263 item = QTAILQ_FIRST(queue);
264 switch (item->type) {
265 case QEMU_INPUT_QUEUE_DELAY:
266 timer_mod(item->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL)
267 + item->delay_ms);
268 return;
269 case QEMU_INPUT_QUEUE_EVENT:
270 qemu_input_event_send(item->src, item->evt);
271 qapi_free_InputEvent(item->evt);
272 break;
273 case QEMU_INPUT_QUEUE_SYNC:
274 qemu_input_event_sync();
275 break;
277 QTAILQ_REMOVE(queue, item, node);
278 queue_count--;
279 g_free(item);
283 static void qemu_input_queue_delay(struct QemuInputEventQueueHead *queue,
284 QEMUTimer *timer, uint32_t delay_ms)
286 QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1);
287 bool start_timer = QTAILQ_EMPTY(queue);
289 item->type = QEMU_INPUT_QUEUE_DELAY;
290 item->delay_ms = delay_ms;
291 item->timer = timer;
292 QTAILQ_INSERT_TAIL(queue, item, node);
293 queue_count++;
295 if (start_timer) {
296 timer_mod(item->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL)
297 + item->delay_ms);
301 static void qemu_input_queue_event(struct QemuInputEventQueueHead *queue,
302 QemuConsole *src, InputEvent *evt)
304 QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1);
306 item->type = QEMU_INPUT_QUEUE_EVENT;
307 item->src = src;
308 item->evt = evt;
309 QTAILQ_INSERT_TAIL(queue, item, node);
310 queue_count++;
313 static void qemu_input_queue_sync(struct QemuInputEventQueueHead *queue)
315 QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1);
317 item->type = QEMU_INPUT_QUEUE_SYNC;
318 QTAILQ_INSERT_TAIL(queue, item, node);
319 queue_count++;
322 void qemu_input_event_send_impl(QemuConsole *src, InputEvent *evt)
324 QemuInputHandlerState *s;
326 qemu_input_event_trace(src, evt);
328 /* pre processing */
329 if (graphic_rotate && (evt->type == INPUT_EVENT_KIND_ABS)) {
330 qemu_input_transform_abs_rotate(evt);
333 /* send event */
334 s = qemu_input_find_handler(1 << evt->type, src);
335 if (!s) {
336 return;
338 s->handler->event(s->dev, src, evt);
339 s->events++;
342 void qemu_input_event_send(QemuConsole *src, InputEvent *evt)
344 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
345 return;
348 replay_input_event(src, evt);
351 void qemu_input_event_sync_impl(void)
353 QemuInputHandlerState *s;
355 trace_input_event_sync();
357 QTAILQ_FOREACH(s, &handlers, node) {
358 if (!s->events) {
359 continue;
361 if (s->handler->sync) {
362 s->handler->sync(s->dev);
364 s->events = 0;
368 void qemu_input_event_sync(void)
370 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
371 return;
374 replay_input_sync_event();
377 InputEvent *qemu_input_event_new_key(KeyValue *key, bool down)
379 InputEvent *evt = g_new0(InputEvent, 1);
380 evt->u.key.data = g_new0(InputKeyEvent, 1);
381 evt->type = INPUT_EVENT_KIND_KEY;
382 evt->u.key.data->key = key;
383 evt->u.key.data->down = down;
384 return evt;
387 void qemu_input_event_send_key(QemuConsole *src, KeyValue *key, bool down)
389 InputEvent *evt;
390 evt = qemu_input_event_new_key(key, down);
391 if (QTAILQ_EMPTY(&kbd_queue)) {
392 qemu_input_event_send(src, evt);
393 qemu_input_event_sync();
394 qapi_free_InputEvent(evt);
395 } else if (queue_count < queue_limit) {
396 qemu_input_queue_event(&kbd_queue, src, evt);
397 qemu_input_queue_sync(&kbd_queue);
401 void qemu_input_event_send_key_number(QemuConsole *src, int num, bool down)
403 KeyValue *key = g_new0(KeyValue, 1);
404 key->type = KEY_VALUE_KIND_NUMBER;
405 key->u.number.data = num;
406 qemu_input_event_send_key(src, key, down);
409 void qemu_input_event_send_key_qcode(QemuConsole *src, QKeyCode q, bool down)
411 KeyValue *key = g_new0(KeyValue, 1);
412 key->type = KEY_VALUE_KIND_QCODE;
413 key->u.qcode.data = q;
414 qemu_input_event_send_key(src, key, down);
417 void qemu_input_event_send_key_delay(uint32_t delay_ms)
419 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
420 return;
423 if (!kbd_timer) {
424 kbd_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, qemu_input_queue_process,
425 &kbd_queue);
427 if (queue_count < queue_limit) {
428 qemu_input_queue_delay(&kbd_queue, kbd_timer,
429 delay_ms ? delay_ms : kbd_default_delay_ms);
433 InputEvent *qemu_input_event_new_btn(InputButton btn, bool down)
435 InputEvent *evt = g_new0(InputEvent, 1);
436 evt->u.btn.data = g_new0(InputBtnEvent, 1);
437 evt->type = INPUT_EVENT_KIND_BTN;
438 evt->u.btn.data->button = btn;
439 evt->u.btn.data->down = down;
440 return evt;
443 void qemu_input_queue_btn(QemuConsole *src, InputButton btn, bool down)
445 InputEvent *evt;
446 evt = qemu_input_event_new_btn(btn, down);
447 qemu_input_event_send(src, evt);
448 qapi_free_InputEvent(evt);
451 void qemu_input_update_buttons(QemuConsole *src, uint32_t *button_map,
452 uint32_t button_old, uint32_t button_new)
454 InputButton btn;
455 uint32_t mask;
457 for (btn = 0; btn < INPUT_BUTTON__MAX; btn++) {
458 mask = button_map[btn];
459 if ((button_old & mask) == (button_new & mask)) {
460 continue;
462 qemu_input_queue_btn(src, btn, button_new & mask);
466 bool qemu_input_is_absolute(void)
468 QemuInputHandlerState *s;
470 s = qemu_input_find_handler(INPUT_EVENT_MASK_REL | INPUT_EVENT_MASK_ABS,
471 NULL);
472 return (s != NULL) && (s->handler->mask & INPUT_EVENT_MASK_ABS);
475 int qemu_input_scale_axis(int value,
476 int min_in, int max_in,
477 int min_out, int max_out)
479 int64_t range_in = (int64_t)max_in - min_in;
480 int64_t range_out = (int64_t)max_out - min_out;
482 if (range_in < 1) {
483 return min_out + range_out / 2;
485 return ((int64_t)value - min_in) * range_out / range_in + min_out;
488 InputEvent *qemu_input_event_new_move(InputEventKind kind,
489 InputAxis axis, int value)
491 InputEvent *evt = g_new0(InputEvent, 1);
492 InputMoveEvent *move = g_new0(InputMoveEvent, 1);
494 evt->type = kind;
495 evt->u.rel.data = move; /* evt->u.rel is the same as evt->u.abs */
496 move->axis = axis;
497 move->value = value;
498 return evt;
501 void qemu_input_queue_rel(QemuConsole *src, InputAxis axis, int value)
503 InputEvent *evt;
504 evt = qemu_input_event_new_move(INPUT_EVENT_KIND_REL, axis, value);
505 qemu_input_event_send(src, evt);
506 qapi_free_InputEvent(evt);
509 void qemu_input_queue_abs(QemuConsole *src, InputAxis axis, int value,
510 int min_in, int max_in)
512 InputEvent *evt;
513 int scaled = qemu_input_scale_axis(value, min_in, max_in,
514 INPUT_EVENT_ABS_MIN,
515 INPUT_EVENT_ABS_MAX);
516 evt = qemu_input_event_new_move(INPUT_EVENT_KIND_ABS, axis, scaled);
517 qemu_input_event_send(src, evt);
518 qapi_free_InputEvent(evt);
521 void qemu_input_check_mode_change(void)
523 static int current_is_absolute;
524 int is_absolute;
526 is_absolute = qemu_input_is_absolute();
528 if (is_absolute != current_is_absolute) {
529 trace_input_mouse_mode(is_absolute);
530 notifier_list_notify(&mouse_mode_notifiers, NULL);
533 current_is_absolute = is_absolute;
536 void qemu_add_mouse_mode_change_notifier(Notifier *notify)
538 notifier_list_add(&mouse_mode_notifiers, notify);
541 void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
543 notifier_remove(notify);
546 MouseInfoList *qmp_query_mice(Error **errp)
548 MouseInfoList *mice_list = NULL;
549 MouseInfoList *info;
550 QemuInputHandlerState *s;
551 bool current = true;
553 QTAILQ_FOREACH(s, &handlers, node) {
554 if (!(s->handler->mask &
555 (INPUT_EVENT_MASK_REL | INPUT_EVENT_MASK_ABS))) {
556 continue;
559 info = g_new0(MouseInfoList, 1);
560 info->value = g_new0(MouseInfo, 1);
561 info->value->index = s->id;
562 info->value->name = g_strdup(s->handler->name);
563 info->value->absolute = s->handler->mask & INPUT_EVENT_MASK_ABS;
564 info->value->current = current;
566 current = false;
567 info->next = mice_list;
568 mice_list = info;
571 return mice_list;
574 void hmp_mouse_set(Monitor *mon, const QDict *qdict)
576 QemuInputHandlerState *s;
577 int index = qdict_get_int(qdict, "index");
578 int found = 0;
580 QTAILQ_FOREACH(s, &handlers, node) {
581 if (s->id != index) {
582 continue;
584 if (!(s->handler->mask & (INPUT_EVENT_MASK_REL |
585 INPUT_EVENT_MASK_ABS))) {
586 error_report("Input device '%s' is not a mouse", s->handler->name);
587 return;
589 found = 1;
590 qemu_input_handler_activate(s);
591 break;
594 if (!found) {
595 error_report("Mouse at index '%d' not found", index);
598 qemu_input_check_mode_change();