]> git.proxmox.com Git - qemu.git/blame - ui/input.c
Update version for 1.7.0-rc1 release
[qemu.git] / ui / input.c
CommitLineData
8f0056b7
PB
1/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24
9c17d615 25#include "sysemu/sysemu.h"
83c9089e 26#include "monitor/monitor.h"
28ecbaee 27#include "ui/console.h"
7b1b5d19 28#include "qapi/error.h"
e235cec3 29#include "qmp-commands.h"
e4c8f004 30#include "qapi-types.h"
b2d1674b 31#include "ui/keymaps.h"
8f0056b7 32
72711efb
GH
33struct QEMUPutMouseEntry {
34 QEMUPutMouseEvent *qemu_put_mouse_event;
35 void *qemu_put_mouse_event_opaque;
36 int qemu_put_mouse_event_absolute;
37 char *qemu_put_mouse_event_name;
38
39 int index;
40
41 /* used internally by qemu for handling mice */
42 QTAILQ_ENTRY(QEMUPutMouseEntry) node;
43};
44
5a37532d
GH
45struct QEMUPutKbdEntry {
46 QEMUPutKBDEvent *put_kbd;
47 void *opaque;
48 QTAILQ_ENTRY(QEMUPutKbdEntry) next;
49};
50
72711efb
GH
51struct QEMUPutLEDEntry {
52 QEMUPutLEDEvent *put_led;
53 void *opaque;
54 QTAILQ_ENTRY(QEMUPutLEDEntry) next;
55};
56
5a37532d
GH
57static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers =
58 QTAILQ_HEAD_INITIALIZER(led_handlers);
59static QTAILQ_HEAD(, QEMUPutKbdEntry) kbd_handlers =
60 QTAILQ_HEAD_INITIALIZER(kbd_handlers);
6fef28ee
AL
61static QTAILQ_HEAD(, QEMUPutMouseEntry) mouse_handlers =
62 QTAILQ_HEAD_INITIALIZER(mouse_handlers);
5a37532d 63static NotifierList mouse_mode_notifiers =
7e581fb3 64 NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers);
8f0056b7 65
e4c8f004 66static const int key_defs[] = {
1048c88f
AK
67 [Q_KEY_CODE_SHIFT] = 0x2a,
68 [Q_KEY_CODE_SHIFT_R] = 0x36,
69
70 [Q_KEY_CODE_ALT] = 0x38,
71 [Q_KEY_CODE_ALT_R] = 0xb8,
72 [Q_KEY_CODE_ALTGR] = 0x64,
73 [Q_KEY_CODE_ALTGR_R] = 0xe4,
74 [Q_KEY_CODE_CTRL] = 0x1d,
75 [Q_KEY_CODE_CTRL_R] = 0x9d,
76
77 [Q_KEY_CODE_MENU] = 0xdd,
78
79 [Q_KEY_CODE_ESC] = 0x01,
80
81 [Q_KEY_CODE_1] = 0x02,
82 [Q_KEY_CODE_2] = 0x03,
83 [Q_KEY_CODE_3] = 0x04,
84 [Q_KEY_CODE_4] = 0x05,
85 [Q_KEY_CODE_5] = 0x06,
86 [Q_KEY_CODE_6] = 0x07,
87 [Q_KEY_CODE_7] = 0x08,
88 [Q_KEY_CODE_8] = 0x09,
89 [Q_KEY_CODE_9] = 0x0a,
90 [Q_KEY_CODE_0] = 0x0b,
91 [Q_KEY_CODE_MINUS] = 0x0c,
92 [Q_KEY_CODE_EQUAL] = 0x0d,
93 [Q_KEY_CODE_BACKSPACE] = 0x0e,
94
95 [Q_KEY_CODE_TAB] = 0x0f,
96 [Q_KEY_CODE_Q] = 0x10,
97 [Q_KEY_CODE_W] = 0x11,
98 [Q_KEY_CODE_E] = 0x12,
99 [Q_KEY_CODE_R] = 0x13,
100 [Q_KEY_CODE_T] = 0x14,
101 [Q_KEY_CODE_Y] = 0x15,
102 [Q_KEY_CODE_U] = 0x16,
103 [Q_KEY_CODE_I] = 0x17,
104 [Q_KEY_CODE_O] = 0x18,
105 [Q_KEY_CODE_P] = 0x19,
106 [Q_KEY_CODE_BRACKET_LEFT] = 0x1a,
107 [Q_KEY_CODE_BRACKET_RIGHT] = 0x1b,
108 [Q_KEY_CODE_RET] = 0x1c,
109
110 [Q_KEY_CODE_A] = 0x1e,
111 [Q_KEY_CODE_S] = 0x1f,
112 [Q_KEY_CODE_D] = 0x20,
113 [Q_KEY_CODE_F] = 0x21,
114 [Q_KEY_CODE_G] = 0x22,
115 [Q_KEY_CODE_H] = 0x23,
116 [Q_KEY_CODE_J] = 0x24,
117 [Q_KEY_CODE_K] = 0x25,
118 [Q_KEY_CODE_L] = 0x26,
119 [Q_KEY_CODE_SEMICOLON] = 0x27,
120 [Q_KEY_CODE_APOSTROPHE] = 0x28,
121 [Q_KEY_CODE_GRAVE_ACCENT] = 0x29,
122
123 [Q_KEY_CODE_BACKSLASH] = 0x2b,
124 [Q_KEY_CODE_Z] = 0x2c,
125 [Q_KEY_CODE_X] = 0x2d,
126 [Q_KEY_CODE_C] = 0x2e,
127 [Q_KEY_CODE_V] = 0x2f,
128 [Q_KEY_CODE_B] = 0x30,
129 [Q_KEY_CODE_N] = 0x31,
130 [Q_KEY_CODE_M] = 0x32,
131 [Q_KEY_CODE_COMMA] = 0x33,
132 [Q_KEY_CODE_DOT] = 0x34,
133 [Q_KEY_CODE_SLASH] = 0x35,
134
135 [Q_KEY_CODE_ASTERISK] = 0x37,
136
137 [Q_KEY_CODE_SPC] = 0x39,
138 [Q_KEY_CODE_CAPS_LOCK] = 0x3a,
139 [Q_KEY_CODE_F1] = 0x3b,
140 [Q_KEY_CODE_F2] = 0x3c,
141 [Q_KEY_CODE_F3] = 0x3d,
142 [Q_KEY_CODE_F4] = 0x3e,
143 [Q_KEY_CODE_F5] = 0x3f,
144 [Q_KEY_CODE_F6] = 0x40,
145 [Q_KEY_CODE_F7] = 0x41,
146 [Q_KEY_CODE_F8] = 0x42,
147 [Q_KEY_CODE_F9] = 0x43,
148 [Q_KEY_CODE_F10] = 0x44,
149 [Q_KEY_CODE_NUM_LOCK] = 0x45,
150 [Q_KEY_CODE_SCROLL_LOCK] = 0x46,
151
152 [Q_KEY_CODE_KP_DIVIDE] = 0xb5,
153 [Q_KEY_CODE_KP_MULTIPLY] = 0x37,
154 [Q_KEY_CODE_KP_SUBTRACT] = 0x4a,
155 [Q_KEY_CODE_KP_ADD] = 0x4e,
156 [Q_KEY_CODE_KP_ENTER] = 0x9c,
157 [Q_KEY_CODE_KP_DECIMAL] = 0x53,
158 [Q_KEY_CODE_SYSRQ] = 0x54,
159
160 [Q_KEY_CODE_KP_0] = 0x52,
161 [Q_KEY_CODE_KP_1] = 0x4f,
162 [Q_KEY_CODE_KP_2] = 0x50,
163 [Q_KEY_CODE_KP_3] = 0x51,
164 [Q_KEY_CODE_KP_4] = 0x4b,
165 [Q_KEY_CODE_KP_5] = 0x4c,
166 [Q_KEY_CODE_KP_6] = 0x4d,
167 [Q_KEY_CODE_KP_7] = 0x47,
168 [Q_KEY_CODE_KP_8] = 0x48,
169 [Q_KEY_CODE_KP_9] = 0x49,
170
171 [Q_KEY_CODE_LESS] = 0x56,
172
173 [Q_KEY_CODE_F11] = 0x57,
174 [Q_KEY_CODE_F12] = 0x58,
175
176 [Q_KEY_CODE_PRINT] = 0xb7,
177
178 [Q_KEY_CODE_HOME] = 0xc7,
179 [Q_KEY_CODE_PGUP] = 0xc9,
180 [Q_KEY_CODE_PGDN] = 0xd1,
181 [Q_KEY_CODE_END] = 0xcf,
182
183 [Q_KEY_CODE_LEFT] = 0xcb,
184 [Q_KEY_CODE_UP] = 0xc8,
185 [Q_KEY_CODE_DOWN] = 0xd0,
186 [Q_KEY_CODE_RIGHT] = 0xcd,
187
188 [Q_KEY_CODE_INSERT] = 0xd2,
189 [Q_KEY_CODE_DELETE] = 0xd3,
190#ifdef NEED_CPU_H
191#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
192 [Q_KEY_CODE_STOP] = 0xf0,
193 [Q_KEY_CODE_AGAIN] = 0xf1,
194 [Q_KEY_CODE_PROPS] = 0xf2,
195 [Q_KEY_CODE_UNDO] = 0xf3,
196 [Q_KEY_CODE_FRONT] = 0xf4,
197 [Q_KEY_CODE_COPY] = 0xf5,
198 [Q_KEY_CODE_OPEN] = 0xf6,
199 [Q_KEY_CODE_PASTE] = 0xf7,
200 [Q_KEY_CODE_FIND] = 0xf8,
201 [Q_KEY_CODE_CUT] = 0xf9,
202 [Q_KEY_CODE_LF] = 0xfa,
203 [Q_KEY_CODE_HELP] = 0xfb,
204 [Q_KEY_CODE_META_L] = 0xfc,
205 [Q_KEY_CODE_META_R] = 0xfd,
206 [Q_KEY_CODE_COMPOSE] = 0xfe,
207#endif
208#endif
209 [Q_KEY_CODE_MAX] = 0,
210};
211
212int index_from_key(const char *key)
213{
9d537c90 214 int i;
1048c88f
AK
215
216 for (i = 0; QKeyCode_lookup[i] != NULL; i++) {
217 if (!strcmp(key, QKeyCode_lookup[i])) {
218 break;
219 }
220 }
221
1048c88f
AK
222 /* Return Q_KEY_CODE_MAX if the key is invalid */
223 return i;
224}
225
226int index_from_keycode(int code)
227{
228 int i;
229
230 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
231 if (key_defs[i] == code) {
232 break;
233 }
234 }
235
236 /* Return Q_KEY_CODE_MAX if the code is invalid */
237 return i;
238}
239
05a3543d
LC
240static int *keycodes;
241static int keycodes_size;
e4c8f004
AK
242static QEMUTimer *key_timer;
243
9f328977
LC
244static int keycode_from_keyvalue(const KeyValue *value)
245{
246 if (value->kind == KEY_VALUE_KIND_QCODE) {
247 return key_defs[value->qcode];
248 } else {
249 assert(value->kind == KEY_VALUE_KIND_NUMBER);
250 return value->number;
251 }
252}
253
254static void free_keycodes(void)
255{
256 g_free(keycodes);
257 keycodes = NULL;
258 keycodes_size = 0;
259}
260
e4c8f004
AK
261static void release_keys(void *opaque)
262{
153d02e3 263 while (keycodes_size > 0) {
b2d1674b
AK
264 if (keycodes[--keycodes_size] & SCANCODE_GREY) {
265 kbd_put_keycode(SCANCODE_EMUL0);
e4c8f004 266 }
b2d1674b 267 kbd_put_keycode(keycodes[keycodes_size] | SCANCODE_UP);
e4c8f004 268 }
05a3543d 269
9f328977 270 free_keycodes();
e4c8f004
AK
271}
272
9f328977 273void qmp_send_key(KeyValueList *keys, bool has_hold_time, int64_t hold_time,
e4c8f004
AK
274 Error **errp)
275{
276 int keycode;
9f328977 277 KeyValueList *p;
e4c8f004
AK
278
279 if (!key_timer) {
bc72ad67 280 key_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, release_keys, NULL);
e4c8f004
AK
281 }
282
283 if (keycodes != NULL) {
bc72ad67 284 timer_del(key_timer);
e4c8f004
AK
285 release_keys(NULL);
286 }
05a3543d 287
e4c8f004
AK
288 if (!has_hold_time) {
289 hold_time = 100;
290 }
291
292 for (p = keys; p != NULL; p = p->next) {
e4c8f004 293 /* key down events */
9f328977
LC
294 keycode = keycode_from_keyvalue(p->value);
295 if (keycode < 0x01 || keycode > 0xff) {
312fd5f2 296 error_setg(errp, "invalid hex keycode 0x%x", keycode);
9f328977
LC
297 free_keycodes();
298 return;
299 }
300
b2d1674b
AK
301 if (keycode & SCANCODE_GREY) {
302 kbd_put_keycode(SCANCODE_EMUL0);
e4c8f004 303 }
b2d1674b 304 kbd_put_keycode(keycode & SCANCODE_KEYCODEMASK);
05a3543d
LC
305
306 keycodes = g_realloc(keycodes, sizeof(int) * (keycodes_size + 1));
307 keycodes[keycodes_size++] = keycode;
e4c8f004 308 }
e4c8f004
AK
309
310 /* delayed key up events */
bc72ad67 311 timer_mod(key_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
e4c8f004
AK
312 muldiv64(get_ticks_per_sec(), hold_time, 1000));
313}
314
5a37532d 315QEMUPutKbdEntry *qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
8f0056b7 316{
5a37532d
GH
317 QEMUPutKbdEntry *entry;
318
319 entry = g_malloc0(sizeof(QEMUPutKbdEntry));
320 entry->put_kbd = func;
321 entry->opaque = opaque;
322 QTAILQ_INSERT_HEAD(&kbd_handlers, entry, next);
323 return entry;
8f0056b7
PB
324}
325
5a37532d 326void qemu_remove_kbd_event_handler(QEMUPutKbdEntry *entry)
46aaebff 327{
5a37532d 328 QTAILQ_REMOVE(&kbd_handlers, entry, next);
46aaebff
JS
329}
330
7e581fb3
AL
331static void check_mode_change(void)
332{
333 static int current_is_absolute, current_has_absolute;
334 int is_absolute;
335 int has_absolute;
336
337 is_absolute = kbd_mouse_is_absolute();
338 has_absolute = kbd_mouse_has_absolute();
339
340 if (is_absolute != current_is_absolute ||
341 has_absolute != current_has_absolute) {
9e8dd451 342 notifier_list_notify(&mouse_mode_notifiers, NULL);
7e581fb3
AL
343 }
344
345 current_is_absolute = is_absolute;
346 current_has_absolute = has_absolute;
347}
348
8f0056b7
PB
349QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
350 void *opaque, int absolute,
351 const char *name)
352{
6fef28ee
AL
353 QEMUPutMouseEntry *s;
354 static int mouse_index = 0;
8f0056b7 355
7267c094 356 s = g_malloc0(sizeof(QEMUPutMouseEntry));
8f0056b7
PB
357
358 s->qemu_put_mouse_event = func;
359 s->qemu_put_mouse_event_opaque = opaque;
360 s->qemu_put_mouse_event_absolute = absolute;
7267c094 361 s->qemu_put_mouse_event_name = g_strdup(name);
6fef28ee 362 s->index = mouse_index++;
8f0056b7 363
6fef28ee 364 QTAILQ_INSERT_TAIL(&mouse_handlers, s, node);
8f0056b7 365
7e581fb3
AL
366 check_mode_change();
367
8f0056b7
PB
368 return s;
369}
370
6fef28ee 371void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
8f0056b7 372{
6fef28ee
AL
373 QTAILQ_REMOVE(&mouse_handlers, entry, node);
374 QTAILQ_INSERT_HEAD(&mouse_handlers, entry, node);
8f0056b7 375
6fef28ee
AL
376 check_mode_change();
377}
8f0056b7 378
6fef28ee
AL
379void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
380{
381 QTAILQ_REMOVE(&mouse_handlers, entry, node);
8f0056b7 382
7267c094
AL
383 g_free(entry->qemu_put_mouse_event_name);
384 g_free(entry);
7e581fb3
AL
385
386 check_mode_change();
8f0056b7
PB
387}
388
03a23a85
GH
389QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
390 void *opaque)
391{
392 QEMUPutLEDEntry *s;
393
7267c094 394 s = g_malloc0(sizeof(QEMUPutLEDEntry));
03a23a85
GH
395
396 s->put_led = func;
397 s->opaque = opaque;
398 QTAILQ_INSERT_TAIL(&led_handlers, s, next);
399 return s;
400}
401
402void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry)
403{
404 if (entry == NULL)
405 return;
406 QTAILQ_REMOVE(&led_handlers, entry, next);
7267c094 407 g_free(entry);
03a23a85
GH
408}
409
8f0056b7
PB
410void kbd_put_keycode(int keycode)
411{
5a37532d
GH
412 QEMUPutKbdEntry *entry = QTAILQ_FIRST(&kbd_handlers);
413
ad02b96a 414 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
99c7f878
GH
415 return;
416 }
5a37532d
GH
417 if (entry) {
418 entry->put_kbd(entry->opaque, keycode);
8f0056b7
PB
419 }
420}
421
03a23a85
GH
422void kbd_put_ledstate(int ledstate)
423{
424 QEMUPutLEDEntry *cursor;
425
426 QTAILQ_FOREACH(cursor, &led_handlers, next) {
427 cursor->put_led(cursor->opaque, ledstate);
428 }
429}
430
8f0056b7
PB
431void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
432{
6fef28ee 433 QEMUPutMouseEntry *entry;
8f0056b7
PB
434 QEMUPutMouseEvent *mouse_event;
435 void *mouse_event_opaque;
9312805d 436 int width, height;
8f0056b7 437
ad02b96a 438 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
99c7f878
GH
439 return;
440 }
6fef28ee 441 if (QTAILQ_EMPTY(&mouse_handlers)) {
8f0056b7
PB
442 return;
443 }
444
6fef28ee
AL
445 entry = QTAILQ_FIRST(&mouse_handlers);
446
447 mouse_event = entry->qemu_put_mouse_event;
448 mouse_event_opaque = entry->qemu_put_mouse_event_opaque;
8f0056b7
PB
449
450 if (mouse_event) {
9312805d
VK
451 if (entry->qemu_put_mouse_event_absolute) {
452 width = 0x7fff;
453 height = 0x7fff;
97697373 454 } else {
9312805d
VK
455 width = graphic_width - 1;
456 height = graphic_height - 1;
457 }
458
459 switch (graphic_rotate) {
460 case 0:
461 mouse_event(mouse_event_opaque,
462 dx, dy, dz, buttons_state);
463 break;
464 case 90:
465 mouse_event(mouse_event_opaque,
466 width - dy, dx, dz, buttons_state);
467 break;
468 case 180:
469 mouse_event(mouse_event_opaque,
470 width - dx, height - dy, dz, buttons_state);
471 break;
472 case 270:
473 mouse_event(mouse_event_opaque,
474 dy, height - dx, dz, buttons_state);
475 break;
97697373 476 }
8f0056b7
PB
477 }
478}
479
480int kbd_mouse_is_absolute(void)
481{
6fef28ee 482 if (QTAILQ_EMPTY(&mouse_handlers)) {
8f0056b7 483 return 0;
6fef28ee 484 }
8f0056b7 485
6fef28ee 486 return QTAILQ_FIRST(&mouse_handlers)->qemu_put_mouse_event_absolute;
8f0056b7
PB
487}
488
eb2e259d
AL
489int kbd_mouse_has_absolute(void)
490{
491 QEMUPutMouseEntry *entry;
492
493 QTAILQ_FOREACH(entry, &mouse_handlers, node) {
494 if (entry->qemu_put_mouse_event_absolute) {
495 return 1;
496 }
497 }
498
499 return 0;
500}
501
e235cec3 502MouseInfoList *qmp_query_mice(Error **errp)
8f0056b7 503{
e235cec3 504 MouseInfoList *mice_list = NULL;
8f0056b7 505 QEMUPutMouseEntry *cursor;
e235cec3 506 bool current = true;
8f0056b7 507
e235cec3
LC
508 QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
509 MouseInfoList *info = g_malloc0(sizeof(*info));
510 info->value = g_malloc0(sizeof(*info->value));
511 info->value->name = g_strdup(cursor->qemu_put_mouse_event_name);
512 info->value->index = cursor->index;
513 info->value->absolute = !!cursor->qemu_put_mouse_event_absolute;
514 info->value->current = current;
8f0056b7 515
e235cec3 516 current = false;
6fef28ee 517
e235cec3
LC
518 info->next = mice_list;
519 mice_list = info;
8f0056b7
PB
520 }
521
e235cec3 522 return mice_list;
8f0056b7
PB
523}
524
525void do_mouse_set(Monitor *mon, const QDict *qdict)
526{
527 QEMUPutMouseEntry *cursor;
8f0056b7 528 int index = qdict_get_int(qdict, "index");
6fef28ee 529 int found = 0;
8f0056b7 530
6fef28ee 531 if (QTAILQ_EMPTY(&mouse_handlers)) {
8f0056b7
PB
532 monitor_printf(mon, "No mouse devices connected\n");
533 return;
534 }
535
6fef28ee
AL
536 QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
537 if (cursor->index == index) {
538 found = 1;
539 qemu_activate_mouse_event_handler(cursor);
540 break;
541 }
8f0056b7
PB
542 }
543
6fef28ee 544 if (!found) {
8f0056b7 545 monitor_printf(mon, "Mouse at given index not found\n");
6fef28ee 546 }
7e581fb3
AL
547
548 check_mode_change();
549}
550
551void qemu_add_mouse_mode_change_notifier(Notifier *notify)
552{
553 notifier_list_add(&mouse_mode_notifiers, notify);
554}
555
556void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
557{
31552529 558 notifier_remove(notify);
8f0056b7 559}