]> git.proxmox.com Git - mirror_qemu.git/blob - ui/input-legacy.c
input: keyboard: switch legacy handlers to new core
[mirror_qemu.git] / ui / input-legacy.c
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/sysemu.h"
26 #include "monitor/monitor.h"
27 #include "ui/console.h"
28 #include "qapi/error.h"
29 #include "qmp-commands.h"
30 #include "qapi-types.h"
31 #include "ui/keymaps.h"
32 #include "ui/input.h"
33
34 struct QEMUPutMouseEntry {
35 QEMUPutMouseEvent *qemu_put_mouse_event;
36 void *qemu_put_mouse_event_opaque;
37 int qemu_put_mouse_event_absolute;
38 char *qemu_put_mouse_event_name;
39
40 int index;
41
42 /* used internally by qemu for handling mice */
43 QTAILQ_ENTRY(QEMUPutMouseEntry) node;
44 };
45
46 struct QEMUPutKbdEntry {
47 QEMUPutKBDEvent *put_kbd;
48 void *opaque;
49 QemuInputHandlerState *s;
50 };
51
52 struct QEMUPutLEDEntry {
53 QEMUPutLEDEvent *put_led;
54 void *opaque;
55 QTAILQ_ENTRY(QEMUPutLEDEntry) next;
56 };
57
58 static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers =
59 QTAILQ_HEAD_INITIALIZER(led_handlers);
60 static QTAILQ_HEAD(, QEMUPutMouseEntry) mouse_handlers =
61 QTAILQ_HEAD_INITIALIZER(mouse_handlers);
62 static NotifierList mouse_mode_notifiers =
63 NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers);
64
65 static const int key_defs[] = {
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
211 int index_from_key(const char *key)
212 {
213 int i;
214
215 for (i = 0; QKeyCode_lookup[i] != NULL; i++) {
216 if (!strcmp(key, QKeyCode_lookup[i])) {
217 break;
218 }
219 }
220
221 /* Return Q_KEY_CODE_MAX if the key is invalid */
222 return i;
223 }
224
225 int 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
239 static int *keycodes;
240 static int keycodes_size;
241 static QEMUTimer *key_timer;
242
243 static 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
253 static void free_keycodes(void)
254 {
255 g_free(keycodes);
256 keycodes = NULL;
257 keycodes_size = 0;
258 }
259
260 static void release_keys(void *opaque)
261 {
262 while (keycodes_size > 0) {
263 if (keycodes[--keycodes_size] & SCANCODE_GREY) {
264 kbd_put_keycode(SCANCODE_EMUL0);
265 }
266 kbd_put_keycode(keycodes[keycodes_size] | SCANCODE_UP);
267 }
268
269 free_keycodes();
270 }
271
272 void qmp_send_key(KeyValueList *keys, bool has_hold_time, int64_t hold_time,
273 Error **errp)
274 {
275 int keycode;
276 KeyValueList *p;
277
278 if (!key_timer) {
279 key_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, release_keys, NULL);
280 }
281
282 if (keycodes != NULL) {
283 timer_del(key_timer);
284 release_keys(NULL);
285 }
286
287 if (!has_hold_time) {
288 hold_time = 100;
289 }
290
291 for (p = keys; p != NULL; p = p->next) {
292 /* key down events */
293 keycode = keycode_from_keyvalue(p->value);
294 if (keycode < 0x01 || keycode > 0xff) {
295 error_setg(errp, "invalid hex keycode 0x%x", keycode);
296 free_keycodes();
297 return;
298 }
299
300 if (keycode & SCANCODE_GREY) {
301 kbd_put_keycode(SCANCODE_EMUL0);
302 }
303 kbd_put_keycode(keycode & SCANCODE_KEYCODEMASK);
304
305 keycodes = g_realloc(keycodes, sizeof(int) * (keycodes_size + 1));
306 keycodes[keycodes_size++] = keycode;
307 }
308
309 /* delayed key up events */
310 timer_mod(key_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
311 muldiv64(get_ticks_per_sec(), hold_time, 1000));
312 }
313
314 static void legacy_kbd_event(DeviceState *dev, QemuConsole *src,
315 InputEvent *evt)
316 {
317 QEMUPutKbdEntry *entry = (QEMUPutKbdEntry *)dev;
318 int keycode = keycode_from_keyvalue(evt->key->key);
319
320 if (!entry || !entry->put_kbd) {
321 return;
322 }
323 if (evt->key->key->kind == KEY_VALUE_KIND_QCODE &&
324 evt->key->key->qcode == Q_KEY_CODE_PAUSE) {
325 /* specific case */
326 int v = evt->key->down ? 0 : 0x80;
327 entry->put_kbd(entry->opaque, 0xe1);
328 entry->put_kbd(entry->opaque, 0x1d | v);
329 entry->put_kbd(entry->opaque, 0x45 | v);
330 return;
331 }
332 if (keycode & SCANCODE_GREY) {
333 entry->put_kbd(entry->opaque, SCANCODE_EMUL0);
334 keycode &= ~SCANCODE_GREY;
335 }
336 if (!evt->key->down) {
337 keycode |= SCANCODE_UP;
338 }
339 entry->put_kbd(entry->opaque, keycode);
340 }
341
342 static QemuInputHandler legacy_kbd_handler = {
343 .name = "legacy-kbd",
344 .mask = INPUT_EVENT_MASK_KEY,
345 .event = legacy_kbd_event,
346 };
347
348 QEMUPutKbdEntry *qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
349 {
350 QEMUPutKbdEntry *entry;
351
352 entry = g_new0(QEMUPutKbdEntry, 1);
353 entry->put_kbd = func;
354 entry->opaque = opaque;
355 entry->s = qemu_input_handler_register((DeviceState *)entry,
356 &legacy_kbd_handler);
357 return entry;
358 }
359
360 void qemu_remove_kbd_event_handler(QEMUPutKbdEntry *entry)
361 {
362 qemu_input_handler_unregister(entry->s);
363 g_free(entry);
364 }
365
366 static void check_mode_change(void)
367 {
368 static int current_is_absolute, current_has_absolute;
369 int is_absolute;
370 int has_absolute;
371
372 is_absolute = kbd_mouse_is_absolute();
373 has_absolute = kbd_mouse_has_absolute();
374
375 if (is_absolute != current_is_absolute ||
376 has_absolute != current_has_absolute) {
377 notifier_list_notify(&mouse_mode_notifiers, NULL);
378 }
379
380 current_is_absolute = is_absolute;
381 current_has_absolute = has_absolute;
382 }
383
384 QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
385 void *opaque, int absolute,
386 const char *name)
387 {
388 QEMUPutMouseEntry *s;
389 static int mouse_index = 0;
390
391 s = g_malloc0(sizeof(QEMUPutMouseEntry));
392
393 s->qemu_put_mouse_event = func;
394 s->qemu_put_mouse_event_opaque = opaque;
395 s->qemu_put_mouse_event_absolute = absolute;
396 s->qemu_put_mouse_event_name = g_strdup(name);
397 s->index = mouse_index++;
398
399 QTAILQ_INSERT_TAIL(&mouse_handlers, s, node);
400
401 check_mode_change();
402
403 return s;
404 }
405
406 void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
407 {
408 QTAILQ_REMOVE(&mouse_handlers, entry, node);
409 QTAILQ_INSERT_HEAD(&mouse_handlers, entry, node);
410
411 check_mode_change();
412 }
413
414 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
415 {
416 QTAILQ_REMOVE(&mouse_handlers, entry, node);
417
418 g_free(entry->qemu_put_mouse_event_name);
419 g_free(entry);
420
421 check_mode_change();
422 }
423
424 QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
425 void *opaque)
426 {
427 QEMUPutLEDEntry *s;
428
429 s = g_malloc0(sizeof(QEMUPutLEDEntry));
430
431 s->put_led = func;
432 s->opaque = opaque;
433 QTAILQ_INSERT_TAIL(&led_handlers, s, next);
434 return s;
435 }
436
437 void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry)
438 {
439 if (entry == NULL)
440 return;
441 QTAILQ_REMOVE(&led_handlers, entry, next);
442 g_free(entry);
443 }
444
445 void kbd_put_keycode(int keycode)
446 {
447 static bool emul0;
448 bool up;
449
450 if (keycode == SCANCODE_EMUL0) {
451 emul0 = true;
452 return;
453 }
454 if (keycode & SCANCODE_UP) {
455 keycode &= ~SCANCODE_UP;
456 up = true;
457 } else {
458 up = false;
459 }
460 if (emul0) {
461 keycode |= SCANCODE_GREY;
462 emul0 = false;
463 }
464
465 qemu_input_event_send_key_number(NULL, keycode, !up);
466 }
467
468 void kbd_put_ledstate(int ledstate)
469 {
470 QEMUPutLEDEntry *cursor;
471
472 QTAILQ_FOREACH(cursor, &led_handlers, next) {
473 cursor->put_led(cursor->opaque, ledstate);
474 }
475 }
476
477 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
478 {
479 QEMUPutMouseEntry *entry;
480 QEMUPutMouseEvent *mouse_event;
481 void *mouse_event_opaque;
482 int width, height;
483
484 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
485 return;
486 }
487 if (QTAILQ_EMPTY(&mouse_handlers)) {
488 return;
489 }
490
491 entry = QTAILQ_FIRST(&mouse_handlers);
492
493 mouse_event = entry->qemu_put_mouse_event;
494 mouse_event_opaque = entry->qemu_put_mouse_event_opaque;
495
496 if (mouse_event) {
497 if (entry->qemu_put_mouse_event_absolute) {
498 width = 0x7fff;
499 height = 0x7fff;
500 } else {
501 width = graphic_width - 1;
502 height = graphic_height - 1;
503 }
504
505 switch (graphic_rotate) {
506 case 0:
507 mouse_event(mouse_event_opaque,
508 dx, dy, dz, buttons_state);
509 break;
510 case 90:
511 mouse_event(mouse_event_opaque,
512 width - dy, dx, dz, buttons_state);
513 break;
514 case 180:
515 mouse_event(mouse_event_opaque,
516 width - dx, height - dy, dz, buttons_state);
517 break;
518 case 270:
519 mouse_event(mouse_event_opaque,
520 dy, height - dx, dz, buttons_state);
521 break;
522 }
523 }
524 }
525
526 int kbd_mouse_is_absolute(void)
527 {
528 if (QTAILQ_EMPTY(&mouse_handlers)) {
529 return 0;
530 }
531
532 return QTAILQ_FIRST(&mouse_handlers)->qemu_put_mouse_event_absolute;
533 }
534
535 int kbd_mouse_has_absolute(void)
536 {
537 QEMUPutMouseEntry *entry;
538
539 QTAILQ_FOREACH(entry, &mouse_handlers, node) {
540 if (entry->qemu_put_mouse_event_absolute) {
541 return 1;
542 }
543 }
544
545 return 0;
546 }
547
548 MouseInfoList *qmp_query_mice(Error **errp)
549 {
550 MouseInfoList *mice_list = NULL;
551 QEMUPutMouseEntry *cursor;
552 bool current = true;
553
554 QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
555 MouseInfoList *info = g_malloc0(sizeof(*info));
556 info->value = g_malloc0(sizeof(*info->value));
557 info->value->name = g_strdup(cursor->qemu_put_mouse_event_name);
558 info->value->index = cursor->index;
559 info->value->absolute = !!cursor->qemu_put_mouse_event_absolute;
560 info->value->current = current;
561
562 current = false;
563
564 info->next = mice_list;
565 mice_list = info;
566 }
567
568 return mice_list;
569 }
570
571 void do_mouse_set(Monitor *mon, const QDict *qdict)
572 {
573 QEMUPutMouseEntry *cursor;
574 int index = qdict_get_int(qdict, "index");
575 int found = 0;
576
577 if (QTAILQ_EMPTY(&mouse_handlers)) {
578 monitor_printf(mon, "No mouse devices connected\n");
579 return;
580 }
581
582 QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
583 if (cursor->index == index) {
584 found = 1;
585 qemu_activate_mouse_event_handler(cursor);
586 break;
587 }
588 }
589
590 if (!found) {
591 monitor_printf(mon, "Mouse at given index not found\n");
592 }
593
594 check_mode_change();
595 }
596
597 void qemu_add_mouse_mode_change_notifier(Notifier *notify)
598 {
599 notifier_list_add(&mouse_mode_notifiers, notify);
600 }
601
602 void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
603 {
604 notifier_remove(notify);
605 }