xref: /openbmc/qemu/ui/input.c (revision 8c1f72da)
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 
33 struct 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 
45 struct QEMUPutKbdEntry {
46     QEMUPutKBDEvent *put_kbd;
47     void *opaque;
48     QTAILQ_ENTRY(QEMUPutKbdEntry) next;
49 };
50 
51 struct QEMUPutLEDEntry {
52     QEMUPutLEDEvent *put_led;
53     void *opaque;
54     QTAILQ_ENTRY(QEMUPutLEDEntry) next;
55 };
56 
57 static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers =
58     QTAILQ_HEAD_INITIALIZER(led_handlers);
59 static QTAILQ_HEAD(, QEMUPutKbdEntry) kbd_handlers =
60     QTAILQ_HEAD_INITIALIZER(kbd_handlers);
61 static QTAILQ_HEAD(, QEMUPutMouseEntry) mouse_handlers =
62     QTAILQ_HEAD_INITIALIZER(mouse_handlers);
63 static NotifierList mouse_mode_notifiers =
64     NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers);
65 
66 static const int key_defs[] = {
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 
212 int index_from_key(const char *key)
213 {
214     int i;
215 
216     for (i = 0; QKeyCode_lookup[i] != NULL; i++) {
217         if (!strcmp(key, QKeyCode_lookup[i])) {
218             break;
219         }
220     }
221 
222     /* Return Q_KEY_CODE_MAX if the key is invalid */
223     return i;
224 }
225 
226 int 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 
240 static int *keycodes;
241 static int keycodes_size;
242 static QEMUTimer *key_timer;
243 
244 static 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 
254 static void free_keycodes(void)
255 {
256     g_free(keycodes);
257     keycodes = NULL;
258     keycodes_size = 0;
259 }
260 
261 static void release_keys(void *opaque)
262 {
263     while (keycodes_size > 0) {
264         if (keycodes[--keycodes_size] & SCANCODE_GREY) {
265             kbd_put_keycode(SCANCODE_EMUL0);
266         }
267         kbd_put_keycode(keycodes[keycodes_size] | SCANCODE_UP);
268     }
269 
270     free_keycodes();
271 }
272 
273 void qmp_send_key(KeyValueList *keys, bool has_hold_time, int64_t hold_time,
274                   Error **errp)
275 {
276     int keycode;
277     KeyValueList *p;
278 
279     if (!key_timer) {
280         key_timer = qemu_new_timer_ns(vm_clock, release_keys, NULL);
281     }
282 
283     if (keycodes != NULL) {
284         qemu_del_timer(key_timer);
285         release_keys(NULL);
286     }
287 
288     if (!has_hold_time) {
289         hold_time = 100;
290     }
291 
292     for (p = keys; p != NULL; p = p->next) {
293         /* key down events */
294         keycode = keycode_from_keyvalue(p->value);
295         if (keycode < 0x01 || keycode > 0xff) {
296             error_setg(errp, "invalid hex keycode 0x%x", keycode);
297             free_keycodes();
298             return;
299         }
300 
301         if (keycode & SCANCODE_GREY) {
302             kbd_put_keycode(SCANCODE_EMUL0);
303         }
304         kbd_put_keycode(keycode & SCANCODE_KEYCODEMASK);
305 
306         keycodes = g_realloc(keycodes, sizeof(int) * (keycodes_size + 1));
307         keycodes[keycodes_size++] = keycode;
308     }
309 
310     /* delayed key up events */
311     qemu_mod_timer(key_timer, qemu_get_clock_ns(vm_clock) +
312                    muldiv64(get_ticks_per_sec(), hold_time, 1000));
313 }
314 
315 QEMUPutKbdEntry *qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
316 {
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;
324 }
325 
326 void qemu_remove_kbd_event_handler(QEMUPutKbdEntry *entry)
327 {
328     QTAILQ_REMOVE(&kbd_handlers, entry, next);
329 }
330 
331 static 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) {
342         notifier_list_notify(&mouse_mode_notifiers, NULL);
343     }
344 
345     current_is_absolute = is_absolute;
346     current_has_absolute = has_absolute;
347 }
348 
349 QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
350                                                 void *opaque, int absolute,
351                                                 const char *name)
352 {
353     QEMUPutMouseEntry *s;
354     static int mouse_index = 0;
355 
356     s = g_malloc0(sizeof(QEMUPutMouseEntry));
357 
358     s->qemu_put_mouse_event = func;
359     s->qemu_put_mouse_event_opaque = opaque;
360     s->qemu_put_mouse_event_absolute = absolute;
361     s->qemu_put_mouse_event_name = g_strdup(name);
362     s->index = mouse_index++;
363 
364     QTAILQ_INSERT_TAIL(&mouse_handlers, s, node);
365 
366     check_mode_change();
367 
368     return s;
369 }
370 
371 void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
372 {
373     QTAILQ_REMOVE(&mouse_handlers, entry, node);
374     QTAILQ_INSERT_HEAD(&mouse_handlers, entry, node);
375 
376     check_mode_change();
377 }
378 
379 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
380 {
381     QTAILQ_REMOVE(&mouse_handlers, entry, node);
382 
383     g_free(entry->qemu_put_mouse_event_name);
384     g_free(entry);
385 
386     check_mode_change();
387 }
388 
389 QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
390                                             void *opaque)
391 {
392     QEMUPutLEDEntry *s;
393 
394     s = g_malloc0(sizeof(QEMUPutLEDEntry));
395 
396     s->put_led = func;
397     s->opaque = opaque;
398     QTAILQ_INSERT_TAIL(&led_handlers, s, next);
399     return s;
400 }
401 
402 void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry)
403 {
404     if (entry == NULL)
405         return;
406     QTAILQ_REMOVE(&led_handlers, entry, next);
407     g_free(entry);
408 }
409 
410 void kbd_put_keycode(int keycode)
411 {
412     QEMUPutKbdEntry *entry = QTAILQ_FIRST(&kbd_handlers);
413 
414     if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
415         return;
416     }
417     if (entry) {
418         entry->put_kbd(entry->opaque, keycode);
419     }
420 }
421 
422 void 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 
431 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
432 {
433     QEMUPutMouseEntry *entry;
434     QEMUPutMouseEvent *mouse_event;
435     void *mouse_event_opaque;
436     int width, height;
437 
438     if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
439         return;
440     }
441     if (QTAILQ_EMPTY(&mouse_handlers)) {
442         return;
443     }
444 
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;
449 
450     if (mouse_event) {
451         if (entry->qemu_put_mouse_event_absolute) {
452             width = 0x7fff;
453             height = 0x7fff;
454         } else {
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;
476         }
477     }
478 }
479 
480 int kbd_mouse_is_absolute(void)
481 {
482     if (QTAILQ_EMPTY(&mouse_handlers)) {
483         return 0;
484     }
485 
486     return QTAILQ_FIRST(&mouse_handlers)->qemu_put_mouse_event_absolute;
487 }
488 
489 int 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 
502 MouseInfoList *qmp_query_mice(Error **errp)
503 {
504     MouseInfoList *mice_list = NULL;
505     QEMUPutMouseEntry *cursor;
506     bool current = true;
507 
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;
515 
516         current = false;
517 
518         info->next = mice_list;
519         mice_list = info;
520     }
521 
522     return mice_list;
523 }
524 
525 void do_mouse_set(Monitor *mon, const QDict *qdict)
526 {
527     QEMUPutMouseEntry *cursor;
528     int index = qdict_get_int(qdict, "index");
529     int found = 0;
530 
531     if (QTAILQ_EMPTY(&mouse_handlers)) {
532         monitor_printf(mon, "No mouse devices connected\n");
533         return;
534     }
535 
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         }
542     }
543 
544     if (!found) {
545         monitor_printf(mon, "Mouse at given index not found\n");
546     }
547 
548     check_mode_change();
549 }
550 
551 void qemu_add_mouse_mode_change_notifier(Notifier *notify)
552 {
553     notifier_list_add(&mouse_mode_notifiers, notify);
554 }
555 
556 void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
557 {
558     notifier_remove(notify);
559 }
560