]> git.proxmox.com Git - mirror_qemu.git/blame - input.c
input: qmp_send_key(): simplify
[mirror_qemu.git] / 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
25#include "sysemu.h"
26#include "net.h"
27#include "monitor.h"
28#include "console.h"
e235cec3
LC
29#include "error.h"
30#include "qmp-commands.h"
e4c8f004 31#include "qapi-types.h"
8f0056b7 32
8f0056b7
PB
33static QEMUPutKBDEvent *qemu_put_kbd_event;
34static void *qemu_put_kbd_event_opaque;
03a23a85 35static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers = QTAILQ_HEAD_INITIALIZER(led_handlers);
6fef28ee
AL
36static QTAILQ_HEAD(, QEMUPutMouseEntry) mouse_handlers =
37 QTAILQ_HEAD_INITIALIZER(mouse_handlers);
7e581fb3
AL
38static NotifierList mouse_mode_notifiers =
39 NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers);
8f0056b7 40
e4c8f004 41static const int key_defs[] = {
1048c88f
AK
42 [Q_KEY_CODE_SHIFT] = 0x2a,
43 [Q_KEY_CODE_SHIFT_R] = 0x36,
44
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,
51
52 [Q_KEY_CODE_MENU] = 0xdd,
53
54 [Q_KEY_CODE_ESC] = 0x01,
55
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,
69
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,
84
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,
97
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,
109
110 [Q_KEY_CODE_ASTERISK] = 0x37,
111
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,
126
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,
134
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,
145
146 [Q_KEY_CODE_LESS] = 0x56,
147
148 [Q_KEY_CODE_F11] = 0x57,
149 [Q_KEY_CODE_F12] = 0x58,
150
151 [Q_KEY_CODE_PRINT] = 0xb7,
152
153 [Q_KEY_CODE_HOME] = 0xc7,
154 [Q_KEY_CODE_PGUP] = 0xc9,
155 [Q_KEY_CODE_PGDN] = 0xd1,
156 [Q_KEY_CODE_END] = 0xcf,
157
158 [Q_KEY_CODE_LEFT] = 0xcb,
159 [Q_KEY_CODE_UP] = 0xc8,
160 [Q_KEY_CODE_DOWN] = 0xd0,
161 [Q_KEY_CODE_RIGHT] = 0xcd,
162
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,
185};
186
187int index_from_key(const char *key)
188{
189 int i, keycode;
190 char *endp;
191
192 for (i = 0; QKeyCode_lookup[i] != NULL; i++) {
193 if (!strcmp(key, QKeyCode_lookup[i])) {
194 break;
195 }
196 }
197
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;
204 }
205 }
206 }
207 }
208
209 /* Return Q_KEY_CODE_MAX if the key is invalid */
210 return i;
211}
212
213int index_from_keycode(int code)
214{
215 int i;
216
217 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
218 if (key_defs[i] == code) {
219 break;
220 }
221 }
222
223 /* Return Q_KEY_CODE_MAX if the code is invalid */
224 return i;
225}
226
05a3543d
LC
227static int *keycodes;
228static int keycodes_size;
e4c8f004
AK
229static QEMUTimer *key_timer;
230
231static void release_keys(void *opaque)
232{
05a3543d 233 int i;
e4c8f004 234
05a3543d
LC
235 for (i = 0; i < keycodes_size; i++) {
236 if (keycodes[i] & 0x80) {
e4c8f004
AK
237 kbd_put_keycode(0xe0);
238 }
05a3543d 239 kbd_put_keycode(keycodes[i]| 0x80);
e4c8f004 240 }
05a3543d
LC
241
242 g_free(keycodes);
e4c8f004 243 keycodes = NULL;
05a3543d 244 keycodes_size = 0;
e4c8f004
AK
245}
246
247void qmp_send_key(QKeyCodeList *keys, bool has_hold_time, int64_t hold_time,
248 Error **errp)
249{
250 int keycode;
05a3543d 251 QKeyCodeList *p;
e4c8f004
AK
252
253 if (!key_timer) {
254 key_timer = qemu_new_timer_ns(vm_clock, release_keys, NULL);
255 }
256
257 if (keycodes != NULL) {
258 qemu_del_timer(key_timer);
259 release_keys(NULL);
260 }
05a3543d 261
e4c8f004
AK
262 if (!has_hold_time) {
263 hold_time = 100;
264 }
265
266 for (p = keys; p != NULL; p = p->next) {
e4c8f004
AK
267 /* key down events */
268 keycode = key_defs[p->value];
269 if (keycode & 0x80) {
270 kbd_put_keycode(0xe0);
271 }
272 kbd_put_keycode(keycode & 0x7f);
05a3543d
LC
273
274 keycodes = g_realloc(keycodes, sizeof(int) * (keycodes_size + 1));
275 keycodes[keycodes_size++] = keycode;
e4c8f004 276 }
e4c8f004
AK
277
278 /* delayed key up events */
279 qemu_mod_timer(key_timer, qemu_get_clock_ns(vm_clock) +
280 muldiv64(get_ticks_per_sec(), hold_time, 1000));
281}
282
8f0056b7
PB
283void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
284{
285 qemu_put_kbd_event_opaque = opaque;
286 qemu_put_kbd_event = func;
287}
288
46aaebff
JS
289void qemu_remove_kbd_event_handler(void)
290{
291 qemu_put_kbd_event_opaque = NULL;
292 qemu_put_kbd_event = NULL;
293}
294
7e581fb3
AL
295static void check_mode_change(void)
296{
297 static int current_is_absolute, current_has_absolute;
298 int is_absolute;
299 int has_absolute;
300
301 is_absolute = kbd_mouse_is_absolute();
302 has_absolute = kbd_mouse_has_absolute();
303
304 if (is_absolute != current_is_absolute ||
305 has_absolute != current_has_absolute) {
9e8dd451 306 notifier_list_notify(&mouse_mode_notifiers, NULL);
7e581fb3
AL
307 }
308
309 current_is_absolute = is_absolute;
310 current_has_absolute = has_absolute;
311}
312
8f0056b7
PB
313QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
314 void *opaque, int absolute,
315 const char *name)
316{
6fef28ee
AL
317 QEMUPutMouseEntry *s;
318 static int mouse_index = 0;
8f0056b7 319
7267c094 320 s = g_malloc0(sizeof(QEMUPutMouseEntry));
8f0056b7
PB
321
322 s->qemu_put_mouse_event = func;
323 s->qemu_put_mouse_event_opaque = opaque;
324 s->qemu_put_mouse_event_absolute = absolute;
7267c094 325 s->qemu_put_mouse_event_name = g_strdup(name);
6fef28ee 326 s->index = mouse_index++;
8f0056b7 327
6fef28ee 328 QTAILQ_INSERT_TAIL(&mouse_handlers, s, node);
8f0056b7 329
7e581fb3
AL
330 check_mode_change();
331
8f0056b7
PB
332 return s;
333}
334
6fef28ee 335void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
8f0056b7 336{
6fef28ee
AL
337 QTAILQ_REMOVE(&mouse_handlers, entry, node);
338 QTAILQ_INSERT_HEAD(&mouse_handlers, entry, node);
8f0056b7 339
6fef28ee
AL
340 check_mode_change();
341}
8f0056b7 342
6fef28ee
AL
343void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
344{
345 QTAILQ_REMOVE(&mouse_handlers, entry, node);
8f0056b7 346
7267c094
AL
347 g_free(entry->qemu_put_mouse_event_name);
348 g_free(entry);
7e581fb3
AL
349
350 check_mode_change();
8f0056b7
PB
351}
352
03a23a85
GH
353QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
354 void *opaque)
355{
356 QEMUPutLEDEntry *s;
357
7267c094 358 s = g_malloc0(sizeof(QEMUPutLEDEntry));
03a23a85
GH
359
360 s->put_led = func;
361 s->opaque = opaque;
362 QTAILQ_INSERT_TAIL(&led_handlers, s, next);
363 return s;
364}
365
366void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry)
367{
368 if (entry == NULL)
369 return;
370 QTAILQ_REMOVE(&led_handlers, entry, next);
7267c094 371 g_free(entry);
03a23a85
GH
372}
373
8f0056b7
PB
374void kbd_put_keycode(int keycode)
375{
ad02b96a 376 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
99c7f878
GH
377 return;
378 }
8f0056b7
PB
379 if (qemu_put_kbd_event) {
380 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
381 }
382}
383
03a23a85
GH
384void kbd_put_ledstate(int ledstate)
385{
386 QEMUPutLEDEntry *cursor;
387
388 QTAILQ_FOREACH(cursor, &led_handlers, next) {
389 cursor->put_led(cursor->opaque, ledstate);
390 }
391}
392
8f0056b7
PB
393void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
394{
6fef28ee 395 QEMUPutMouseEntry *entry;
8f0056b7
PB
396 QEMUPutMouseEvent *mouse_event;
397 void *mouse_event_opaque;
9312805d 398 int width, height;
8f0056b7 399
ad02b96a 400 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
99c7f878
GH
401 return;
402 }
6fef28ee 403 if (QTAILQ_EMPTY(&mouse_handlers)) {
8f0056b7
PB
404 return;
405 }
406
6fef28ee
AL
407 entry = QTAILQ_FIRST(&mouse_handlers);
408
409 mouse_event = entry->qemu_put_mouse_event;
410 mouse_event_opaque = entry->qemu_put_mouse_event_opaque;
8f0056b7
PB
411
412 if (mouse_event) {
9312805d
VK
413 if (entry->qemu_put_mouse_event_absolute) {
414 width = 0x7fff;
415 height = 0x7fff;
97697373 416 } else {
9312805d
VK
417 width = graphic_width - 1;
418 height = graphic_height - 1;
419 }
420
421 switch (graphic_rotate) {
422 case 0:
423 mouse_event(mouse_event_opaque,
424 dx, dy, dz, buttons_state);
425 break;
426 case 90:
427 mouse_event(mouse_event_opaque,
428 width - dy, dx, dz, buttons_state);
429 break;
430 case 180:
431 mouse_event(mouse_event_opaque,
432 width - dx, height - dy, dz, buttons_state);
433 break;
434 case 270:
435 mouse_event(mouse_event_opaque,
436 dy, height - dx, dz, buttons_state);
437 break;
97697373 438 }
8f0056b7
PB
439 }
440}
441
442int kbd_mouse_is_absolute(void)
443{
6fef28ee 444 if (QTAILQ_EMPTY(&mouse_handlers)) {
8f0056b7 445 return 0;
6fef28ee 446 }
8f0056b7 447
6fef28ee 448 return QTAILQ_FIRST(&mouse_handlers)->qemu_put_mouse_event_absolute;
8f0056b7
PB
449}
450
eb2e259d
AL
451int kbd_mouse_has_absolute(void)
452{
453 QEMUPutMouseEntry *entry;
454
455 QTAILQ_FOREACH(entry, &mouse_handlers, node) {
456 if (entry->qemu_put_mouse_event_absolute) {
457 return 1;
458 }
459 }
460
461 return 0;
462}
463
e235cec3 464MouseInfoList *qmp_query_mice(Error **errp)
8f0056b7 465{
e235cec3 466 MouseInfoList *mice_list = NULL;
8f0056b7 467 QEMUPutMouseEntry *cursor;
e235cec3 468 bool current = true;
8f0056b7 469
e235cec3
LC
470 QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
471 MouseInfoList *info = g_malloc0(sizeof(*info));
472 info->value = g_malloc0(sizeof(*info->value));
473 info->value->name = g_strdup(cursor->qemu_put_mouse_event_name);
474 info->value->index = cursor->index;
475 info->value->absolute = !!cursor->qemu_put_mouse_event_absolute;
476 info->value->current = current;
8f0056b7 477
e235cec3 478 current = false;
6fef28ee 479
e235cec3
LC
480 info->next = mice_list;
481 mice_list = info;
8f0056b7
PB
482 }
483
e235cec3 484 return mice_list;
8f0056b7
PB
485}
486
487void do_mouse_set(Monitor *mon, const QDict *qdict)
488{
489 QEMUPutMouseEntry *cursor;
8f0056b7 490 int index = qdict_get_int(qdict, "index");
6fef28ee 491 int found = 0;
8f0056b7 492
6fef28ee 493 if (QTAILQ_EMPTY(&mouse_handlers)) {
8f0056b7
PB
494 monitor_printf(mon, "No mouse devices connected\n");
495 return;
496 }
497
6fef28ee
AL
498 QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
499 if (cursor->index == index) {
500 found = 1;
501 qemu_activate_mouse_event_handler(cursor);
502 break;
503 }
8f0056b7
PB
504 }
505
6fef28ee 506 if (!found) {
8f0056b7 507 monitor_printf(mon, "Mouse at given index not found\n");
6fef28ee 508 }
7e581fb3
AL
509
510 check_mode_change();
511}
512
513void qemu_add_mouse_mode_change_notifier(Notifier *notify)
514{
515 notifier_list_add(&mouse_mode_notifiers, notify);
516}
517
518void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
519{
31552529 520 notifier_remove(notify);
8f0056b7 521}