target-arm: convert void helpers
[qemu/ar7.git] / input.c
blobc4b0619a7439f0c64921f1b7657f7946b1ac6389
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "sysemu.h"
26 #include "net.h"
27 #include "monitor.h"
28 #include "console.h"
29 #include "error.h"
30 #include "qmp-commands.h"
31 #include "qapi-types.h"
33 static QEMUPutKBDEvent *qemu_put_kbd_event;
34 static void *qemu_put_kbd_event_opaque;
35 static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers = QTAILQ_HEAD_INITIALIZER(led_handlers);
36 static QTAILQ_HEAD(, QEMUPutMouseEntry) mouse_handlers =
37 QTAILQ_HEAD_INITIALIZER(mouse_handlers);
38 static NotifierList mouse_mode_notifiers =
39 NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers);
41 static const int key_defs[] = {
42 [Q_KEY_CODE_SHIFT] = 0x2a,
43 [Q_KEY_CODE_SHIFT_R] = 0x36,
45 [Q_KEY_CODE_ALT] = 0x38,
46 [Q_KEY_CODE_ALT_R] = 0xb8,
47 [Q_KEY_CODE_ALTGR] = 0x64,
48 [Q_KEY_CODE_ALTGR_R] = 0xe4,
49 [Q_KEY_CODE_CTRL] = 0x1d,
50 [Q_KEY_CODE_CTRL_R] = 0x9d,
52 [Q_KEY_CODE_MENU] = 0xdd,
54 [Q_KEY_CODE_ESC] = 0x01,
56 [Q_KEY_CODE_1] = 0x02,
57 [Q_KEY_CODE_2] = 0x03,
58 [Q_KEY_CODE_3] = 0x04,
59 [Q_KEY_CODE_4] = 0x05,
60 [Q_KEY_CODE_5] = 0x06,
61 [Q_KEY_CODE_6] = 0x07,
62 [Q_KEY_CODE_7] = 0x08,
63 [Q_KEY_CODE_8] = 0x09,
64 [Q_KEY_CODE_9] = 0x0a,
65 [Q_KEY_CODE_0] = 0x0b,
66 [Q_KEY_CODE_MINUS] = 0x0c,
67 [Q_KEY_CODE_EQUAL] = 0x0d,
68 [Q_KEY_CODE_BACKSPACE] = 0x0e,
70 [Q_KEY_CODE_TAB] = 0x0f,
71 [Q_KEY_CODE_Q] = 0x10,
72 [Q_KEY_CODE_W] = 0x11,
73 [Q_KEY_CODE_E] = 0x12,
74 [Q_KEY_CODE_R] = 0x13,
75 [Q_KEY_CODE_T] = 0x14,
76 [Q_KEY_CODE_Y] = 0x15,
77 [Q_KEY_CODE_U] = 0x16,
78 [Q_KEY_CODE_I] = 0x17,
79 [Q_KEY_CODE_O] = 0x18,
80 [Q_KEY_CODE_P] = 0x19,
81 [Q_KEY_CODE_BRACKET_LEFT] = 0x1a,
82 [Q_KEY_CODE_BRACKET_RIGHT] = 0x1b,
83 [Q_KEY_CODE_RET] = 0x1c,
85 [Q_KEY_CODE_A] = 0x1e,
86 [Q_KEY_CODE_S] = 0x1f,
87 [Q_KEY_CODE_D] = 0x20,
88 [Q_KEY_CODE_F] = 0x21,
89 [Q_KEY_CODE_G] = 0x22,
90 [Q_KEY_CODE_H] = 0x23,
91 [Q_KEY_CODE_J] = 0x24,
92 [Q_KEY_CODE_K] = 0x25,
93 [Q_KEY_CODE_L] = 0x26,
94 [Q_KEY_CODE_SEMICOLON] = 0x27,
95 [Q_KEY_CODE_APOSTROPHE] = 0x28,
96 [Q_KEY_CODE_GRAVE_ACCENT] = 0x29,
98 [Q_KEY_CODE_BACKSLASH] = 0x2b,
99 [Q_KEY_CODE_Z] = 0x2c,
100 [Q_KEY_CODE_X] = 0x2d,
101 [Q_KEY_CODE_C] = 0x2e,
102 [Q_KEY_CODE_V] = 0x2f,
103 [Q_KEY_CODE_B] = 0x30,
104 [Q_KEY_CODE_N] = 0x31,
105 [Q_KEY_CODE_M] = 0x32,
106 [Q_KEY_CODE_COMMA] = 0x33,
107 [Q_KEY_CODE_DOT] = 0x34,
108 [Q_KEY_CODE_SLASH] = 0x35,
110 [Q_KEY_CODE_ASTERISK] = 0x37,
112 [Q_KEY_CODE_SPC] = 0x39,
113 [Q_KEY_CODE_CAPS_LOCK] = 0x3a,
114 [Q_KEY_CODE_F1] = 0x3b,
115 [Q_KEY_CODE_F2] = 0x3c,
116 [Q_KEY_CODE_F3] = 0x3d,
117 [Q_KEY_CODE_F4] = 0x3e,
118 [Q_KEY_CODE_F5] = 0x3f,
119 [Q_KEY_CODE_F6] = 0x40,
120 [Q_KEY_CODE_F7] = 0x41,
121 [Q_KEY_CODE_F8] = 0x42,
122 [Q_KEY_CODE_F9] = 0x43,
123 [Q_KEY_CODE_F10] = 0x44,
124 [Q_KEY_CODE_NUM_LOCK] = 0x45,
125 [Q_KEY_CODE_SCROLL_LOCK] = 0x46,
127 [Q_KEY_CODE_KP_DIVIDE] = 0xb5,
128 [Q_KEY_CODE_KP_MULTIPLY] = 0x37,
129 [Q_KEY_CODE_KP_SUBTRACT] = 0x4a,
130 [Q_KEY_CODE_KP_ADD] = 0x4e,
131 [Q_KEY_CODE_KP_ENTER] = 0x9c,
132 [Q_KEY_CODE_KP_DECIMAL] = 0x53,
133 [Q_KEY_CODE_SYSRQ] = 0x54,
135 [Q_KEY_CODE_KP_0] = 0x52,
136 [Q_KEY_CODE_KP_1] = 0x4f,
137 [Q_KEY_CODE_KP_2] = 0x50,
138 [Q_KEY_CODE_KP_3] = 0x51,
139 [Q_KEY_CODE_KP_4] = 0x4b,
140 [Q_KEY_CODE_KP_5] = 0x4c,
141 [Q_KEY_CODE_KP_6] = 0x4d,
142 [Q_KEY_CODE_KP_7] = 0x47,
143 [Q_KEY_CODE_KP_8] = 0x48,
144 [Q_KEY_CODE_KP_9] = 0x49,
146 [Q_KEY_CODE_LESS] = 0x56,
148 [Q_KEY_CODE_F11] = 0x57,
149 [Q_KEY_CODE_F12] = 0x58,
151 [Q_KEY_CODE_PRINT] = 0xb7,
153 [Q_KEY_CODE_HOME] = 0xc7,
154 [Q_KEY_CODE_PGUP] = 0xc9,
155 [Q_KEY_CODE_PGDN] = 0xd1,
156 [Q_KEY_CODE_END] = 0xcf,
158 [Q_KEY_CODE_LEFT] = 0xcb,
159 [Q_KEY_CODE_UP] = 0xc8,
160 [Q_KEY_CODE_DOWN] = 0xd0,
161 [Q_KEY_CODE_RIGHT] = 0xcd,
163 [Q_KEY_CODE_INSERT] = 0xd2,
164 [Q_KEY_CODE_DELETE] = 0xd3,
165 #ifdef NEED_CPU_H
166 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
167 [Q_KEY_CODE_STOP] = 0xf0,
168 [Q_KEY_CODE_AGAIN] = 0xf1,
169 [Q_KEY_CODE_PROPS] = 0xf2,
170 [Q_KEY_CODE_UNDO] = 0xf3,
171 [Q_KEY_CODE_FRONT] = 0xf4,
172 [Q_KEY_CODE_COPY] = 0xf5,
173 [Q_KEY_CODE_OPEN] = 0xf6,
174 [Q_KEY_CODE_PASTE] = 0xf7,
175 [Q_KEY_CODE_FIND] = 0xf8,
176 [Q_KEY_CODE_CUT] = 0xf9,
177 [Q_KEY_CODE_LF] = 0xfa,
178 [Q_KEY_CODE_HELP] = 0xfb,
179 [Q_KEY_CODE_META_L] = 0xfc,
180 [Q_KEY_CODE_META_R] = 0xfd,
181 [Q_KEY_CODE_COMPOSE] = 0xfe,
182 #endif
183 #endif
184 [Q_KEY_CODE_MAX] = 0,
187 int index_from_key(const char *key)
189 int i, keycode;
190 char *endp;
192 for (i = 0; QKeyCode_lookup[i] != NULL; i++) {
193 if (!strcmp(key, QKeyCode_lookup[i])) {
194 break;
198 if (strstart(key, "0x", NULL)) {
199 keycode = strtoul(key, &endp, 0);
200 if (*endp == '\0' && keycode >= 0x01 && keycode <= 0xff) {
201 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
202 if (keycode == key_defs[i]) {
203 break;
209 /* Return Q_KEY_CODE_MAX if the key is invalid */
210 return i;
213 int index_from_keycode(int code)
215 int i;
217 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
218 if (key_defs[i] == code) {
219 break;
223 /* Return Q_KEY_CODE_MAX if the code is invalid */
224 return i;
227 static QKeyCodeList *keycodes;
228 static QEMUTimer *key_timer;
230 static void release_keys(void *opaque)
232 int keycode;
233 QKeyCodeList *p;
235 for (p = keycodes; p != NULL; p = p->next) {
236 keycode = key_defs[p->value];
237 if (keycode & 0x80) {
238 kbd_put_keycode(0xe0);
240 kbd_put_keycode(keycode | 0x80);
242 qapi_free_QKeyCodeList(keycodes);
243 keycodes = NULL;
246 void qmp_send_key(QKeyCodeList *keys, bool has_hold_time, int64_t hold_time,
247 Error **errp)
249 int keycode;
250 QKeyCodeList *p, *keylist, *head = NULL, *tmp = NULL;
252 if (!key_timer) {
253 key_timer = qemu_new_timer_ns(vm_clock, release_keys, NULL);
256 if (keycodes != NULL) {
257 qemu_del_timer(key_timer);
258 release_keys(NULL);
260 if (!has_hold_time) {
261 hold_time = 100;
264 for (p = keys; p != NULL; p = p->next) {
265 keylist = g_malloc0(sizeof(*keylist));
266 keylist->value = p->value;
267 keylist->next = NULL;
269 if (!head) {
270 head = keylist;
272 if (tmp) {
273 tmp->next = keylist;
275 tmp = keylist;
277 /* key down events */
278 keycode = key_defs[p->value];
279 if (keycode & 0x80) {
280 kbd_put_keycode(0xe0);
282 kbd_put_keycode(keycode & 0x7f);
284 keycodes = head;
286 /* delayed key up events */
287 qemu_mod_timer(key_timer, qemu_get_clock_ns(vm_clock) +
288 muldiv64(get_ticks_per_sec(), hold_time, 1000));
291 void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
293 qemu_put_kbd_event_opaque = opaque;
294 qemu_put_kbd_event = func;
297 void qemu_remove_kbd_event_handler(void)
299 qemu_put_kbd_event_opaque = NULL;
300 qemu_put_kbd_event = NULL;
303 static void check_mode_change(void)
305 static int current_is_absolute, current_has_absolute;
306 int is_absolute;
307 int has_absolute;
309 is_absolute = kbd_mouse_is_absolute();
310 has_absolute = kbd_mouse_has_absolute();
312 if (is_absolute != current_is_absolute ||
313 has_absolute != current_has_absolute) {
314 notifier_list_notify(&mouse_mode_notifiers, NULL);
317 current_is_absolute = is_absolute;
318 current_has_absolute = has_absolute;
321 QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
322 void *opaque, int absolute,
323 const char *name)
325 QEMUPutMouseEntry *s;
326 static int mouse_index = 0;
328 s = g_malloc0(sizeof(QEMUPutMouseEntry));
330 s->qemu_put_mouse_event = func;
331 s->qemu_put_mouse_event_opaque = opaque;
332 s->qemu_put_mouse_event_absolute = absolute;
333 s->qemu_put_mouse_event_name = g_strdup(name);
334 s->index = mouse_index++;
336 QTAILQ_INSERT_TAIL(&mouse_handlers, s, node);
338 check_mode_change();
340 return s;
343 void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
345 QTAILQ_REMOVE(&mouse_handlers, entry, node);
346 QTAILQ_INSERT_HEAD(&mouse_handlers, entry, node);
348 check_mode_change();
351 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
353 QTAILQ_REMOVE(&mouse_handlers, entry, node);
355 g_free(entry->qemu_put_mouse_event_name);
356 g_free(entry);
358 check_mode_change();
361 QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
362 void *opaque)
364 QEMUPutLEDEntry *s;
366 s = g_malloc0(sizeof(QEMUPutLEDEntry));
368 s->put_led = func;
369 s->opaque = opaque;
370 QTAILQ_INSERT_TAIL(&led_handlers, s, next);
371 return s;
374 void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry)
376 if (entry == NULL)
377 return;
378 QTAILQ_REMOVE(&led_handlers, entry, next);
379 g_free(entry);
382 void kbd_put_keycode(int keycode)
384 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
385 return;
387 if (qemu_put_kbd_event) {
388 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
392 void kbd_put_ledstate(int ledstate)
394 QEMUPutLEDEntry *cursor;
396 QTAILQ_FOREACH(cursor, &led_handlers, next) {
397 cursor->put_led(cursor->opaque, ledstate);
401 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
403 QEMUPutMouseEntry *entry;
404 QEMUPutMouseEvent *mouse_event;
405 void *mouse_event_opaque;
406 int width, height;
408 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
409 return;
411 if (QTAILQ_EMPTY(&mouse_handlers)) {
412 return;
415 entry = QTAILQ_FIRST(&mouse_handlers);
417 mouse_event = entry->qemu_put_mouse_event;
418 mouse_event_opaque = entry->qemu_put_mouse_event_opaque;
420 if (mouse_event) {
421 if (entry->qemu_put_mouse_event_absolute) {
422 width = 0x7fff;
423 height = 0x7fff;
424 } else {
425 width = graphic_width - 1;
426 height = graphic_height - 1;
429 switch (graphic_rotate) {
430 case 0:
431 mouse_event(mouse_event_opaque,
432 dx, dy, dz, buttons_state);
433 break;
434 case 90:
435 mouse_event(mouse_event_opaque,
436 width - dy, dx, dz, buttons_state);
437 break;
438 case 180:
439 mouse_event(mouse_event_opaque,
440 width - dx, height - dy, dz, buttons_state);
441 break;
442 case 270:
443 mouse_event(mouse_event_opaque,
444 dy, height - dx, dz, buttons_state);
445 break;
450 int kbd_mouse_is_absolute(void)
452 if (QTAILQ_EMPTY(&mouse_handlers)) {
453 return 0;
456 return QTAILQ_FIRST(&mouse_handlers)->qemu_put_mouse_event_absolute;
459 int kbd_mouse_has_absolute(void)
461 QEMUPutMouseEntry *entry;
463 QTAILQ_FOREACH(entry, &mouse_handlers, node) {
464 if (entry->qemu_put_mouse_event_absolute) {
465 return 1;
469 return 0;
472 MouseInfoList *qmp_query_mice(Error **errp)
474 MouseInfoList *mice_list = NULL;
475 QEMUPutMouseEntry *cursor;
476 bool current = true;
478 QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
479 MouseInfoList *info = g_malloc0(sizeof(*info));
480 info->value = g_malloc0(sizeof(*info->value));
481 info->value->name = g_strdup(cursor->qemu_put_mouse_event_name);
482 info->value->index = cursor->index;
483 info->value->absolute = !!cursor->qemu_put_mouse_event_absolute;
484 info->value->current = current;
486 current = false;
488 info->next = mice_list;
489 mice_list = info;
492 return mice_list;
495 void do_mouse_set(Monitor *mon, const QDict *qdict)
497 QEMUPutMouseEntry *cursor;
498 int index = qdict_get_int(qdict, "index");
499 int found = 0;
501 if (QTAILQ_EMPTY(&mouse_handlers)) {
502 monitor_printf(mon, "No mouse devices connected\n");
503 return;
506 QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
507 if (cursor->index == index) {
508 found = 1;
509 qemu_activate_mouse_event_handler(cursor);
510 break;
514 if (!found) {
515 monitor_printf(mon, "Mouse at given index not found\n");
518 check_mode_change();
521 void qemu_add_mouse_mode_change_notifier(Notifier *notify)
523 notifier_list_add(&mouse_mode_notifiers, notify);
526 void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
528 notifier_remove(notify);