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