stream: Switch stream_populate() to byte-based
[qemu/kevin.git] / ui / input.c
blobaf05f063685c064630bce636441c9ec89256de7d
1 #include "qemu/osdep.h"
2 #include "sysemu/sysemu.h"
3 #include "qapi-types.h"
4 #include "qemu/error-report.h"
5 #include "qmp-commands.h"
6 #include "trace.h"
7 #include "ui/input.h"
8 #include "ui/console.h"
9 #include "sysemu/replay.h"
11 struct QemuInputHandlerState {
12 DeviceState *dev;
13 QemuInputHandler *handler;
14 int id;
15 int events;
16 QemuConsole *con;
17 QTAILQ_ENTRY(QemuInputHandlerState) node;
20 typedef struct QemuInputEventQueue QemuInputEventQueue;
21 struct QemuInputEventQueue {
22 enum {
23 QEMU_INPUT_QUEUE_DELAY = 1,
24 QEMU_INPUT_QUEUE_EVENT,
25 QEMU_INPUT_QUEUE_SYNC,
26 } type;
27 QEMUTimer *timer;
28 uint32_t delay_ms;
29 QemuConsole *src;
30 InputEvent *evt;
31 QTAILQ_ENTRY(QemuInputEventQueue) node;
34 static QTAILQ_HEAD(, QemuInputHandlerState) handlers =
35 QTAILQ_HEAD_INITIALIZER(handlers);
36 static NotifierList mouse_mode_notifiers =
37 NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers);
39 static QTAILQ_HEAD(QemuInputEventQueueHead, QemuInputEventQueue) kbd_queue =
40 QTAILQ_HEAD_INITIALIZER(kbd_queue);
41 static QEMUTimer *kbd_timer;
42 static uint32_t kbd_default_delay_ms = 10;
43 static uint32_t queue_count;
44 static uint32_t queue_limit = 1024;
46 QemuInputHandlerState *qemu_input_handler_register(DeviceState *dev,
47 QemuInputHandler *handler)
49 QemuInputHandlerState *s = g_new0(QemuInputHandlerState, 1);
50 static int id = 1;
52 s->dev = dev;
53 s->handler = handler;
54 s->id = id++;
55 QTAILQ_INSERT_TAIL(&handlers, s, node);
57 qemu_input_check_mode_change();
58 return s;
61 void qemu_input_handler_activate(QemuInputHandlerState *s)
63 QTAILQ_REMOVE(&handlers, s, node);
64 QTAILQ_INSERT_HEAD(&handlers, s, node);
65 qemu_input_check_mode_change();
68 void qemu_input_handler_deactivate(QemuInputHandlerState *s)
70 QTAILQ_REMOVE(&handlers, s, node);
71 QTAILQ_INSERT_TAIL(&handlers, s, node);
72 qemu_input_check_mode_change();
75 void qemu_input_handler_unregister(QemuInputHandlerState *s)
77 QTAILQ_REMOVE(&handlers, s, node);
78 g_free(s);
79 qemu_input_check_mode_change();
82 void qemu_input_handler_bind(QemuInputHandlerState *s,
83 const char *device_id, int head,
84 Error **errp)
86 QemuConsole *con;
87 Error *err = NULL;
89 con = qemu_console_lookup_by_device_name(device_id, head, &err);
90 if (err) {
91 error_propagate(errp, err);
92 return;
95 s->con = con;
98 static QemuInputHandlerState*
99 qemu_input_find_handler(uint32_t mask, QemuConsole *con)
101 QemuInputHandlerState *s;
103 QTAILQ_FOREACH(s, &handlers, node) {
104 if (s->con == NULL || s->con != con) {
105 continue;
107 if (mask & s->handler->mask) {
108 return s;
112 QTAILQ_FOREACH(s, &handlers, node) {
113 if (s->con != NULL) {
114 continue;
116 if (mask & s->handler->mask) {
117 return s;
120 return NULL;
123 void qmp_input_send_event(bool has_device, const char *device,
124 bool has_head, int64_t head,
125 InputEventList *events, Error **errp)
127 InputEventList *e;
128 QemuConsole *con;
129 Error *err = NULL;
131 con = NULL;
132 if (has_device) {
133 if (!has_head) {
134 head = 0;
136 con = qemu_console_lookup_by_device_name(device, head, &err);
137 if (err) {
138 error_propagate(errp, err);
139 return;
143 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
144 error_setg(errp, "VM not running");
145 return;
148 for (e = events; e != NULL; e = e->next) {
149 InputEvent *event = e->value;
151 if (!qemu_input_find_handler(1 << event->type, con)) {
152 error_setg(errp, "Input handler not found for "
153 "event type %s",
154 InputEventKind_lookup[event->type]);
155 return;
159 for (e = events; e != NULL; e = e->next) {
160 InputEvent *event = e->value;
162 qemu_input_event_send(con, event);
165 qemu_input_event_sync();
168 static int qemu_input_transform_invert_abs_value(int value)
170 return (int64_t)INPUT_EVENT_ABS_MAX - value + INPUT_EVENT_ABS_MIN;
173 static void qemu_input_transform_abs_rotate(InputEvent *evt)
175 InputMoveEvent *move = evt->u.abs.data;
176 switch (graphic_rotate) {
177 case 90:
178 if (move->axis == INPUT_AXIS_X) {
179 move->axis = INPUT_AXIS_Y;
180 } else if (move->axis == INPUT_AXIS_Y) {
181 move->axis = INPUT_AXIS_X;
182 move->value = qemu_input_transform_invert_abs_value(move->value);
184 break;
185 case 180:
186 move->value = qemu_input_transform_invert_abs_value(move->value);
187 break;
188 case 270:
189 if (move->axis == INPUT_AXIS_X) {
190 move->axis = INPUT_AXIS_Y;
191 move->value = qemu_input_transform_invert_abs_value(move->value);
192 } else if (move->axis == INPUT_AXIS_Y) {
193 move->axis = INPUT_AXIS_X;
195 break;
199 static void qemu_input_event_trace(QemuConsole *src, InputEvent *evt)
201 const char *name;
202 int qcode, idx = -1;
203 InputKeyEvent *key;
204 InputBtnEvent *btn;
205 InputMoveEvent *move;
207 if (src) {
208 idx = qemu_console_get_index(src);
210 switch (evt->type) {
211 case INPUT_EVENT_KIND_KEY:
212 key = evt->u.key.data;
213 switch (key->key->type) {
214 case KEY_VALUE_KIND_NUMBER:
215 qcode = qemu_input_key_number_to_qcode(key->key->u.number.data);
216 name = QKeyCode_lookup[qcode];
217 trace_input_event_key_number(idx, key->key->u.number.data,
218 name, key->down);
219 break;
220 case KEY_VALUE_KIND_QCODE:
221 name = QKeyCode_lookup[key->key->u.qcode.data];
222 trace_input_event_key_qcode(idx, name, key->down);
223 break;
224 case KEY_VALUE_KIND__MAX:
225 /* keep gcc happy */
226 break;
228 break;
229 case INPUT_EVENT_KIND_BTN:
230 btn = evt->u.btn.data;
231 name = InputButton_lookup[btn->button];
232 trace_input_event_btn(idx, name, btn->down);
233 break;
234 case INPUT_EVENT_KIND_REL:
235 move = evt->u.rel.data;
236 name = InputAxis_lookup[move->axis];
237 trace_input_event_rel(idx, name, move->value);
238 break;
239 case INPUT_EVENT_KIND_ABS:
240 move = evt->u.abs.data;
241 name = InputAxis_lookup[move->axis];
242 trace_input_event_abs(idx, name, move->value);
243 break;
244 case INPUT_EVENT_KIND__MAX:
245 /* keep gcc happy */
246 break;
250 static void qemu_input_queue_process(void *opaque)
252 struct QemuInputEventQueueHead *queue = opaque;
253 QemuInputEventQueue *item;
255 g_assert(!QTAILQ_EMPTY(queue));
256 item = QTAILQ_FIRST(queue);
257 g_assert(item->type == QEMU_INPUT_QUEUE_DELAY);
258 QTAILQ_REMOVE(queue, item, node);
259 queue_count--;
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();