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