integrator/cp: Model CP control registers as sysbus device
[qemu/qmp-unstable.git] / ui / input.c
blobeeeabe844c08b872fcece8c595f4fb3ac0b80e5b
1 #include "hw/qdev.h"
2 #include "sysemu/sysemu.h"
3 #include "qapi-types.h"
4 #include "qmp-commands.h"
5 #include "trace.h"
6 #include "ui/input.h"
7 #include "ui/console.h"
9 struct QemuInputHandlerState {
10 DeviceState *dev;
11 QemuInputHandler *handler;
12 int id;
13 int events;
14 QemuConsole *con;
15 QTAILQ_ENTRY(QemuInputHandlerState) node;
18 typedef struct QemuInputEventQueue QemuInputEventQueue;
19 struct QemuInputEventQueue {
20 enum {
21 QEMU_INPUT_QUEUE_DELAY = 1,
22 QEMU_INPUT_QUEUE_EVENT,
23 QEMU_INPUT_QUEUE_SYNC,
24 } type;
25 QEMUTimer *timer;
26 uint32_t delay_ms;
27 QemuConsole *src;
28 InputEvent *evt;
29 QTAILQ_ENTRY(QemuInputEventQueue) node;
32 static QTAILQ_HEAD(, QemuInputHandlerState) handlers =
33 QTAILQ_HEAD_INITIALIZER(handlers);
34 static NotifierList mouse_mode_notifiers =
35 NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers);
37 static QTAILQ_HEAD(QemuInputEventQueueHead, QemuInputEventQueue) kbd_queue =
38 QTAILQ_HEAD_INITIALIZER(kbd_queue);
39 static QEMUTimer *kbd_timer;
40 static uint32_t kbd_default_delay_ms = 10;
42 QemuInputHandlerState *qemu_input_handler_register(DeviceState *dev,
43 QemuInputHandler *handler)
45 QemuInputHandlerState *s = g_new0(QemuInputHandlerState, 1);
46 static int id = 1;
48 s->dev = dev;
49 s->handler = handler;
50 s->id = id++;
51 QTAILQ_INSERT_TAIL(&handlers, s, node);
53 qemu_input_check_mode_change();
54 return s;
57 void qemu_input_handler_activate(QemuInputHandlerState *s)
59 QTAILQ_REMOVE(&handlers, s, node);
60 QTAILQ_INSERT_HEAD(&handlers, s, node);
61 qemu_input_check_mode_change();
64 void qemu_input_handler_deactivate(QemuInputHandlerState *s)
66 QTAILQ_REMOVE(&handlers, s, node);
67 QTAILQ_INSERT_TAIL(&handlers, s, node);
68 qemu_input_check_mode_change();
71 void qemu_input_handler_unregister(QemuInputHandlerState *s)
73 QTAILQ_REMOVE(&handlers, s, node);
74 g_free(s);
75 qemu_input_check_mode_change();
78 void qemu_input_handler_bind(QemuInputHandlerState *s,
79 const char *device_id, int head,
80 Error **errp)
82 DeviceState *dev;
83 QemuConsole *con;
85 dev = qdev_find_recursive(sysbus_get_default(), device_id);
86 if (dev == NULL) {
87 error_set(errp, QERR_DEVICE_NOT_FOUND, device_id);
88 return;
91 con = qemu_console_lookup_by_device(dev, head);
92 if (con == NULL) {
93 error_setg(errp, "Device %s is not bound to a QemuConsole", device_id);
94 return;
97 s->con = con;
100 static QemuInputHandlerState*
101 qemu_input_find_handler(uint32_t mask, QemuConsole *con)
103 QemuInputHandlerState *s;
105 QTAILQ_FOREACH(s, &handlers, node) {
106 if (s->con == NULL || s->con != con) {
107 continue;
109 if (mask & s->handler->mask) {
110 return s;
114 QTAILQ_FOREACH(s, &handlers, node) {
115 if (s->con != NULL) {
116 continue;
118 if (mask & s->handler->mask) {
119 return s;
122 return NULL;
125 void qmp_x_input_send_event(bool has_console, int64_t console,
126 InputEventList *events, Error **errp)
128 InputEventList *e;
129 QemuConsole *con;
131 con = NULL;
132 if (has_console) {
133 con = qemu_console_lookup_by_index(console);
134 if (!con) {
135 error_setg(errp, "console %" PRId64 " not found", console);
136 return;
140 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
141 error_setg(errp, "VM not running");
142 return;
145 for (e = events; e != NULL; e = e->next) {
146 InputEvent *event = e->value;
148 if (!qemu_input_find_handler(1 << event->kind, con)) {
149 error_setg(errp, "Input handler not found for "
150 "event type %s",
151 InputEventKind_lookup[event->kind]);
152 return;
156 for (e = events; e != NULL; e = e->next) {
157 InputEvent *event = e->value;
159 qemu_input_event_send(con, event);
162 qemu_input_event_sync();
165 static void qemu_input_transform_abs_rotate(InputEvent *evt)
167 switch (graphic_rotate) {
168 case 90:
169 if (evt->abs->axis == INPUT_AXIS_X) {
170 evt->abs->axis = INPUT_AXIS_Y;
171 } else if (evt->abs->axis == INPUT_AXIS_Y) {
172 evt->abs->axis = INPUT_AXIS_X;
173 evt->abs->value = INPUT_EVENT_ABS_SIZE - 1 - evt->abs->value;
175 break;
176 case 180:
177 evt->abs->value = INPUT_EVENT_ABS_SIZE - 1 - evt->abs->value;
178 break;
179 case 270:
180 if (evt->abs->axis == INPUT_AXIS_X) {
181 evt->abs->axis = INPUT_AXIS_Y;
182 evt->abs->value = INPUT_EVENT_ABS_SIZE - 1 - evt->abs->value;
183 } else if (evt->abs->axis == INPUT_AXIS_Y) {
184 evt->abs->axis = INPUT_AXIS_X;
186 break;
190 static void qemu_input_event_trace(QemuConsole *src, InputEvent *evt)
192 const char *name;
193 int qcode, idx = -1;
195 if (src) {
196 idx = qemu_console_get_index(src);
198 switch (evt->kind) {
199 case INPUT_EVENT_KIND_KEY:
200 switch (evt->key->key->kind) {
201 case KEY_VALUE_KIND_NUMBER:
202 qcode = qemu_input_key_number_to_qcode(evt->key->key->number);
203 name = QKeyCode_lookup[qcode];
204 trace_input_event_key_number(idx, evt->key->key->number,
205 name, evt->key->down);
206 break;
207 case KEY_VALUE_KIND_QCODE:
208 name = QKeyCode_lookup[evt->key->key->qcode];
209 trace_input_event_key_qcode(idx, name, evt->key->down);
210 break;
211 case KEY_VALUE_KIND_MAX:
212 /* keep gcc happy */
213 break;
215 break;
216 case INPUT_EVENT_KIND_BTN:
217 name = InputButton_lookup[evt->btn->button];
218 trace_input_event_btn(idx, name, evt->btn->down);
219 break;
220 case INPUT_EVENT_KIND_REL:
221 name = InputAxis_lookup[evt->rel->axis];
222 trace_input_event_rel(idx, name, evt->rel->value);
223 break;
224 case INPUT_EVENT_KIND_ABS:
225 name = InputAxis_lookup[evt->abs->axis];
226 trace_input_event_abs(idx, name, evt->abs->value);
227 break;
228 case INPUT_EVENT_KIND_MAX:
229 /* keep gcc happy */
230 break;
234 static void qemu_input_queue_process(void *opaque)
236 struct QemuInputEventQueueHead *queue = opaque;
237 QemuInputEventQueue *item;
239 g_assert(!QTAILQ_EMPTY(queue));
240 item = QTAILQ_FIRST(queue);
241 g_assert(item->type == QEMU_INPUT_QUEUE_DELAY);
242 QTAILQ_REMOVE(queue, item, node);
243 g_free(item);
245 while (!QTAILQ_EMPTY(queue)) {
246 item = QTAILQ_FIRST(queue);
247 switch (item->type) {
248 case QEMU_INPUT_QUEUE_DELAY:
249 timer_mod(item->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL)
250 + item->delay_ms);
251 return;
252 case QEMU_INPUT_QUEUE_EVENT:
253 qemu_input_event_send(item->src, item->evt);
254 qapi_free_InputEvent(item->evt);
255 break;
256 case QEMU_INPUT_QUEUE_SYNC:
257 qemu_input_event_sync();
258 break;
260 QTAILQ_REMOVE(queue, item, node);
261 g_free(item);
265 static void qemu_input_queue_delay(struct QemuInputEventQueueHead *queue,
266 QEMUTimer *timer, uint32_t delay_ms)
268 QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1);
269 bool start_timer = QTAILQ_EMPTY(queue);
271 item->type = QEMU_INPUT_QUEUE_DELAY;
272 item->delay_ms = delay_ms;
273 item->timer = timer;
274 QTAILQ_INSERT_TAIL(queue, item, node);
276 if (start_timer) {
277 timer_mod(item->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL)
278 + item->delay_ms);
282 static void qemu_input_queue_event(struct QemuInputEventQueueHead *queue,
283 QemuConsole *src, InputEvent *evt)
285 QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1);
287 item->type = QEMU_INPUT_QUEUE_EVENT;
288 item->src = src;
289 item->evt = evt;
290 QTAILQ_INSERT_TAIL(queue, item, node);
293 static void qemu_input_queue_sync(struct QemuInputEventQueueHead *queue)
295 QemuInputEventQueue *item = g_new0(QemuInputEventQueue, 1);
297 item->type = QEMU_INPUT_QUEUE_SYNC;
298 QTAILQ_INSERT_TAIL(queue, item, node);
301 void qemu_input_event_send(QemuConsole *src, InputEvent *evt)
303 QemuInputHandlerState *s;
305 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
306 return;
309 qemu_input_event_trace(src, evt);
311 /* pre processing */
312 if (graphic_rotate && (evt->kind == INPUT_EVENT_KIND_ABS)) {
313 qemu_input_transform_abs_rotate(evt);
316 /* send event */
317 s = qemu_input_find_handler(1 << evt->kind, src);
318 if (!s) {
319 return;
321 s->handler->event(s->dev, src, evt);
322 s->events++;
325 void qemu_input_event_sync(void)
327 QemuInputHandlerState *s;
329 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
330 return;
333 trace_input_event_sync();
335 QTAILQ_FOREACH(s, &handlers, node) {
336 if (!s->events) {
337 continue;
339 if (s->handler->sync) {
340 s->handler->sync(s->dev);
342 s->events = 0;
346 InputEvent *qemu_input_event_new_key(KeyValue *key, bool down)
348 InputEvent *evt = g_new0(InputEvent, 1);
349 evt->key = g_new0(InputKeyEvent, 1);
350 evt->kind = INPUT_EVENT_KIND_KEY;
351 evt->key->key = key;
352 evt->key->down = down;
353 return evt;
356 void qemu_input_event_send_key(QemuConsole *src, KeyValue *key, bool down)
358 InputEvent *evt;
359 evt = qemu_input_event_new_key(key, down);
360 if (QTAILQ_EMPTY(&kbd_queue)) {
361 qemu_input_event_send(src, evt);
362 qemu_input_event_sync();
363 qapi_free_InputEvent(evt);
364 } else {
365 qemu_input_queue_event(&kbd_queue, src, evt);
366 qemu_input_queue_sync(&kbd_queue);
370 void qemu_input_event_send_key_number(QemuConsole *src, int num, bool down)
372 KeyValue *key = g_new0(KeyValue, 1);
373 key->kind = KEY_VALUE_KIND_NUMBER;
374 key->number = num;
375 qemu_input_event_send_key(src, key, down);
378 void qemu_input_event_send_key_qcode(QemuConsole *src, QKeyCode q, bool down)
380 KeyValue *key = g_new0(KeyValue, 1);
381 key->kind = KEY_VALUE_KIND_QCODE;
382 key->qcode = q;
383 qemu_input_event_send_key(src, key, down);
386 void qemu_input_event_send_key_delay(uint32_t delay_ms)
388 if (!kbd_timer) {
389 kbd_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, qemu_input_queue_process,
390 &kbd_queue);
392 qemu_input_queue_delay(&kbd_queue, kbd_timer,
393 delay_ms ? delay_ms : kbd_default_delay_ms);
396 InputEvent *qemu_input_event_new_btn(InputButton btn, bool down)
398 InputEvent *evt = g_new0(InputEvent, 1);
399 evt->btn = g_new0(InputBtnEvent, 1);
400 evt->kind = INPUT_EVENT_KIND_BTN;
401 evt->btn->button = btn;
402 evt->btn->down = down;
403 return evt;
406 void qemu_input_queue_btn(QemuConsole *src, InputButton btn, bool down)
408 InputEvent *evt;
409 evt = qemu_input_event_new_btn(btn, down);
410 qemu_input_event_send(src, evt);
411 qapi_free_InputEvent(evt);
414 void qemu_input_update_buttons(QemuConsole *src, uint32_t *button_map,
415 uint32_t button_old, uint32_t button_new)
417 InputButton btn;
418 uint32_t mask;
420 for (btn = 0; btn < INPUT_BUTTON_MAX; btn++) {
421 mask = button_map[btn];
422 if ((button_old & mask) == (button_new & mask)) {
423 continue;
425 qemu_input_queue_btn(src, btn, button_new & mask);
429 bool qemu_input_is_absolute(void)
431 QemuInputHandlerState *s;
433 s = qemu_input_find_handler(INPUT_EVENT_MASK_REL | INPUT_EVENT_MASK_ABS,
434 NULL);
435 return (s != NULL) && (s->handler->mask & INPUT_EVENT_MASK_ABS);
438 int qemu_input_scale_axis(int value, int size_in, int size_out)
440 if (size_in < 2) {
441 return size_out / 2;
443 return (int64_t)value * (size_out - 1) / (size_in - 1);
446 InputEvent *qemu_input_event_new_move(InputEventKind kind,
447 InputAxis axis, int value)
449 InputEvent *evt = g_new0(InputEvent, 1);
450 InputMoveEvent *move = g_new0(InputMoveEvent, 1);
452 evt->kind = kind;
453 evt->data = move;
454 move->axis = axis;
455 move->value = value;
456 return evt;
459 void qemu_input_queue_rel(QemuConsole *src, InputAxis axis, int value)
461 InputEvent *evt;
462 evt = qemu_input_event_new_move(INPUT_EVENT_KIND_REL, axis, value);
463 qemu_input_event_send(src, evt);
464 qapi_free_InputEvent(evt);
467 void qemu_input_queue_abs(QemuConsole *src, InputAxis axis, int value, int size)
469 InputEvent *evt;
470 int scaled = qemu_input_scale_axis(value, size, INPUT_EVENT_ABS_SIZE);
471 evt = qemu_input_event_new_move(INPUT_EVENT_KIND_ABS, axis, scaled);
472 qemu_input_event_send(src, evt);
473 qapi_free_InputEvent(evt);
476 void qemu_input_check_mode_change(void)
478 static int current_is_absolute;
479 int is_absolute;
481 is_absolute = qemu_input_is_absolute();
483 if (is_absolute != current_is_absolute) {
484 trace_input_mouse_mode(is_absolute);
485 notifier_list_notify(&mouse_mode_notifiers, NULL);
488 current_is_absolute = is_absolute;
491 void qemu_add_mouse_mode_change_notifier(Notifier *notify)
493 notifier_list_add(&mouse_mode_notifiers, notify);
496 void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
498 notifier_remove(notify);
501 MouseInfoList *qmp_query_mice(Error **errp)
503 MouseInfoList *mice_list = NULL;
504 MouseInfoList *info;
505 QemuInputHandlerState *s;
506 bool current = true;
508 QTAILQ_FOREACH(s, &handlers, node) {
509 if (!(s->handler->mask &
510 (INPUT_EVENT_MASK_REL | INPUT_EVENT_MASK_ABS))) {
511 continue;
514 info = g_new0(MouseInfoList, 1);
515 info->value = g_new0(MouseInfo, 1);
516 info->value->index = s->id;
517 info->value->name = g_strdup(s->handler->name);
518 info->value->absolute = s->handler->mask & INPUT_EVENT_MASK_ABS;
519 info->value->current = current;
521 current = false;
522 info->next = mice_list;
523 mice_list = info;
526 return mice_list;
529 void hmp_mouse_set(Monitor *mon, const QDict *qdict)
531 QemuInputHandlerState *s;
532 int index = qdict_get_int(qdict, "index");
533 int found = 0;
535 QTAILQ_FOREACH(s, &handlers, node) {
536 if (s->id != index) {
537 continue;
539 if (!(s->handler->mask & (INPUT_EVENT_MASK_REL |
540 INPUT_EVENT_MASK_ABS))) {
541 error_report("Input device '%s' is not a mouse", s->handler->name);
542 return;
544 found = 1;
545 qemu_input_handler_activate(s);
546 break;
549 if (!found) {
550 error_report("Mouse at index '%d' not found", index);
553 qemu_input_check_mode_change();