Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging
[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
39 /* new input core */
40 QemuInputHandler h;
41 QemuInputHandlerState *s;
42 int axis[INPUT_AXIS_MAX];
43 int buttons;
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
63 static const int key_defs[] = {
64 [Q_KEY_CODE_SHIFT] = 0x2a,
65 [Q_KEY_CODE_SHIFT_R] = 0x36,
66
67 [Q_KEY_CODE_ALT] = 0x38,
68 [Q_KEY_CODE_ALT_R] = 0xb8,
69 [Q_KEY_CODE_ALTGR] = 0x64,
70 [Q_KEY_CODE_ALTGR_R] = 0xe4,
71 [Q_KEY_CODE_CTRL] = 0x1d,
72 [Q_KEY_CODE_CTRL_R] = 0x9d,
73
74 [Q_KEY_CODE_MENU] = 0xdd,
75
76 [Q_KEY_CODE_ESC] = 0x01,
77
78 [Q_KEY_CODE_1] = 0x02,
79 [Q_KEY_CODE_2] = 0x03,
80 [Q_KEY_CODE_3] = 0x04,
81 [Q_KEY_CODE_4] = 0x05,
82 [Q_KEY_CODE_5] = 0x06,
83 [Q_KEY_CODE_6] = 0x07,
84 [Q_KEY_CODE_7] = 0x08,
85 [Q_KEY_CODE_8] = 0x09,
86 [Q_KEY_CODE_9] = 0x0a,
87 [Q_KEY_CODE_0] = 0x0b,
88 [Q_KEY_CODE_MINUS] = 0x0c,
89 [Q_KEY_CODE_EQUAL] = 0x0d,
90 [Q_KEY_CODE_BACKSPACE] = 0x0e,
91
92 [Q_KEY_CODE_TAB] = 0x0f,
93 [Q_KEY_CODE_Q] = 0x10,
94 [Q_KEY_CODE_W] = 0x11,
95 [Q_KEY_CODE_E] = 0x12,
96 [Q_KEY_CODE_R] = 0x13,
97 [Q_KEY_CODE_T] = 0x14,
98 [Q_KEY_CODE_Y] = 0x15,
99 [Q_KEY_CODE_U] = 0x16,
100 [Q_KEY_CODE_I] = 0x17,
101 [Q_KEY_CODE_O] = 0x18,
102 [Q_KEY_CODE_P] = 0x19,
103 [Q_KEY_CODE_BRACKET_LEFT] = 0x1a,
104 [Q_KEY_CODE_BRACKET_RIGHT] = 0x1b,
105 [Q_KEY_CODE_RET] = 0x1c,
106
107 [Q_KEY_CODE_A] = 0x1e,
108 [Q_KEY_CODE_S] = 0x1f,
109 [Q_KEY_CODE_D] = 0x20,
110 [Q_KEY_CODE_F] = 0x21,
111 [Q_KEY_CODE_G] = 0x22,
112 [Q_KEY_CODE_H] = 0x23,
113 [Q_KEY_CODE_J] = 0x24,
114 [Q_KEY_CODE_K] = 0x25,
115 [Q_KEY_CODE_L] = 0x26,
116 [Q_KEY_CODE_SEMICOLON] = 0x27,
117 [Q_KEY_CODE_APOSTROPHE] = 0x28,
118 [Q_KEY_CODE_GRAVE_ACCENT] = 0x29,
119
120 [Q_KEY_CODE_BACKSLASH] = 0x2b,
121 [Q_KEY_CODE_Z] = 0x2c,
122 [Q_KEY_CODE_X] = 0x2d,
123 [Q_KEY_CODE_C] = 0x2e,
124 [Q_KEY_CODE_V] = 0x2f,
125 [Q_KEY_CODE_B] = 0x30,
126 [Q_KEY_CODE_N] = 0x31,
127 [Q_KEY_CODE_M] = 0x32,
128 [Q_KEY_CODE_COMMA] = 0x33,
129 [Q_KEY_CODE_DOT] = 0x34,
130 [Q_KEY_CODE_SLASH] = 0x35,
131
132 [Q_KEY_CODE_ASTERISK] = 0x37,
133
134 [Q_KEY_CODE_SPC] = 0x39,
135 [Q_KEY_CODE_CAPS_LOCK] = 0x3a,
136 [Q_KEY_CODE_F1] = 0x3b,
137 [Q_KEY_CODE_F2] = 0x3c,
138 [Q_KEY_CODE_F3] = 0x3d,
139 [Q_KEY_CODE_F4] = 0x3e,
140 [Q_KEY_CODE_F5] = 0x3f,
141 [Q_KEY_CODE_F6] = 0x40,
142 [Q_KEY_CODE_F7] = 0x41,
143 [Q_KEY_CODE_F8] = 0x42,
144 [Q_KEY_CODE_F9] = 0x43,
145 [Q_KEY_CODE_F10] = 0x44,
146 [Q_KEY_CODE_NUM_LOCK] = 0x45,
147 [Q_KEY_CODE_SCROLL_LOCK] = 0x46,
148
149 [Q_KEY_CODE_KP_DIVIDE] = 0xb5,
150 [Q_KEY_CODE_KP_MULTIPLY] = 0x37,
151 [Q_KEY_CODE_KP_SUBTRACT] = 0x4a,
152 [Q_KEY_CODE_KP_ADD] = 0x4e,
153 [Q_KEY_CODE_KP_ENTER] = 0x9c,
154 [Q_KEY_CODE_KP_DECIMAL] = 0x53,
155 [Q_KEY_CODE_SYSRQ] = 0x54,
156
157 [Q_KEY_CODE_KP_0] = 0x52,
158 [Q_KEY_CODE_KP_1] = 0x4f,
159 [Q_KEY_CODE_KP_2] = 0x50,
160 [Q_KEY_CODE_KP_3] = 0x51,
161 [Q_KEY_CODE_KP_4] = 0x4b,
162 [Q_KEY_CODE_KP_5] = 0x4c,
163 [Q_KEY_CODE_KP_6] = 0x4d,
164 [Q_KEY_CODE_KP_7] = 0x47,
165 [Q_KEY_CODE_KP_8] = 0x48,
166 [Q_KEY_CODE_KP_9] = 0x49,
167
168 [Q_KEY_CODE_LESS] = 0x56,
169
170 [Q_KEY_CODE_F11] = 0x57,
171 [Q_KEY_CODE_F12] = 0x58,
172
173 [Q_KEY_CODE_PRINT] = 0xb7,
174
175 [Q_KEY_CODE_HOME] = 0xc7,
176 [Q_KEY_CODE_PGUP] = 0xc9,
177 [Q_KEY_CODE_PGDN] = 0xd1,
178 [Q_KEY_CODE_END] = 0xcf,
179
180 [Q_KEY_CODE_LEFT] = 0xcb,
181 [Q_KEY_CODE_UP] = 0xc8,
182 [Q_KEY_CODE_DOWN] = 0xd0,
183 [Q_KEY_CODE_RIGHT] = 0xcd,
184
185 [Q_KEY_CODE_INSERT] = 0xd2,
186 [Q_KEY_CODE_DELETE] = 0xd3,
187 #ifdef NEED_CPU_H
188 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
189 [Q_KEY_CODE_STOP] = 0xf0,
190 [Q_KEY_CODE_AGAIN] = 0xf1,
191 [Q_KEY_CODE_PROPS] = 0xf2,
192 [Q_KEY_CODE_UNDO] = 0xf3,
193 [Q_KEY_CODE_FRONT] = 0xf4,
194 [Q_KEY_CODE_COPY] = 0xf5,
195 [Q_KEY_CODE_OPEN] = 0xf6,
196 [Q_KEY_CODE_PASTE] = 0xf7,
197 [Q_KEY_CODE_FIND] = 0xf8,
198 [Q_KEY_CODE_CUT] = 0xf9,
199 [Q_KEY_CODE_LF] = 0xfa,
200 [Q_KEY_CODE_HELP] = 0xfb,
201 [Q_KEY_CODE_META_L] = 0xfc,
202 [Q_KEY_CODE_META_R] = 0xfd,
203 [Q_KEY_CODE_COMPOSE] = 0xfe,
204 #endif
205 #endif
206 [Q_KEY_CODE_MAX] = 0,
207 };
208
209 int index_from_key(const char *key)
210 {
211 int i;
212
213 for (i = 0; QKeyCode_lookup[i] != NULL; i++) {
214 if (!strcmp(key, QKeyCode_lookup[i])) {
215 break;
216 }
217 }
218
219 /* Return Q_KEY_CODE_MAX if the key is invalid */
220 return i;
221 }
222
223 static int *keycodes;
224 static int keycodes_size;
225 static QEMUTimer *key_timer;
226
227 static int keycode_from_keyvalue(const KeyValue *value)
228 {
229 if (value->kind == KEY_VALUE_KIND_QCODE) {
230 return key_defs[value->qcode];
231 } else {
232 assert(value->kind == KEY_VALUE_KIND_NUMBER);
233 return value->number;
234 }
235 }
236
237 static void free_keycodes(void)
238 {
239 g_free(keycodes);
240 keycodes = NULL;
241 keycodes_size = 0;
242 }
243
244 static void release_keys(void *opaque)
245 {
246 while (keycodes_size > 0) {
247 qemu_input_event_send_key_number(NULL, keycodes[--keycodes_size],
248 false);
249 }
250
251 free_keycodes();
252 }
253
254 void qmp_send_key(KeyValueList *keys, bool has_hold_time, int64_t hold_time,
255 Error **errp)
256 {
257 int keycode;
258 KeyValueList *p;
259
260 if (!key_timer) {
261 key_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, release_keys, NULL);
262 }
263
264 if (keycodes != NULL) {
265 timer_del(key_timer);
266 release_keys(NULL);
267 }
268
269 if (!has_hold_time) {
270 hold_time = 100;
271 }
272
273 for (p = keys; p != NULL; p = p->next) {
274 /* key down events */
275 keycode = keycode_from_keyvalue(p->value);
276 if (keycode < 0x01 || keycode > 0xff) {
277 error_setg(errp, "invalid hex keycode 0x%x", keycode);
278 free_keycodes();
279 return;
280 }
281
282 qemu_input_event_send_key_number(NULL, keycode, true);
283
284 keycodes = g_realloc(keycodes, sizeof(int) * (keycodes_size + 1));
285 keycodes[keycodes_size++] = keycode;
286 }
287
288 /* delayed key up events */
289 timer_mod(key_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
290 muldiv64(get_ticks_per_sec(), hold_time, 1000));
291 }
292
293 static void legacy_kbd_event(DeviceState *dev, QemuConsole *src,
294 InputEvent *evt)
295 {
296 QEMUPutKbdEntry *entry = (QEMUPutKbdEntry *)dev;
297 int keycode = keycode_from_keyvalue(evt->key->key);
298
299 if (!entry || !entry->put_kbd) {
300 return;
301 }
302 if (evt->key->key->kind == KEY_VALUE_KIND_QCODE &&
303 evt->key->key->qcode == Q_KEY_CODE_PAUSE) {
304 /* specific case */
305 int v = evt->key->down ? 0 : 0x80;
306 entry->put_kbd(entry->opaque, 0xe1);
307 entry->put_kbd(entry->opaque, 0x1d | v);
308 entry->put_kbd(entry->opaque, 0x45 | v);
309 return;
310 }
311 if (keycode & SCANCODE_GREY) {
312 entry->put_kbd(entry->opaque, SCANCODE_EMUL0);
313 keycode &= ~SCANCODE_GREY;
314 }
315 if (!evt->key->down) {
316 keycode |= SCANCODE_UP;
317 }
318 entry->put_kbd(entry->opaque, keycode);
319 }
320
321 static QemuInputHandler legacy_kbd_handler = {
322 .name = "legacy-kbd",
323 .mask = INPUT_EVENT_MASK_KEY,
324 .event = legacy_kbd_event,
325 };
326
327 QEMUPutKbdEntry *qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
328 {
329 QEMUPutKbdEntry *entry;
330
331 entry = g_new0(QEMUPutKbdEntry, 1);
332 entry->put_kbd = func;
333 entry->opaque = opaque;
334 entry->s = qemu_input_handler_register((DeviceState *)entry,
335 &legacy_kbd_handler);
336 qemu_input_handler_activate(entry->s);
337 return entry;
338 }
339
340 void qemu_remove_kbd_event_handler(QEMUPutKbdEntry *entry)
341 {
342 qemu_input_handler_unregister(entry->s);
343 g_free(entry);
344 }
345
346 static void legacy_mouse_event(DeviceState *dev, QemuConsole *src,
347 InputEvent *evt)
348 {
349 static const int bmap[INPUT_BUTTON_MAX] = {
350 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
351 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
352 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
353 };
354 QEMUPutMouseEntry *s = (QEMUPutMouseEntry *)dev;
355
356 switch (evt->kind) {
357 case INPUT_EVENT_KIND_BTN:
358 if (evt->btn->down) {
359 s->buttons |= bmap[evt->btn->button];
360 } else {
361 s->buttons &= ~bmap[evt->btn->button];
362 }
363 if (evt->btn->down && evt->btn->button == INPUT_BUTTON_WHEEL_UP) {
364 s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque,
365 s->axis[INPUT_AXIS_X],
366 s->axis[INPUT_AXIS_Y],
367 -1,
368 s->buttons);
369 }
370 if (evt->btn->down && evt->btn->button == INPUT_BUTTON_WHEEL_DOWN) {
371 s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque,
372 s->axis[INPUT_AXIS_X],
373 s->axis[INPUT_AXIS_Y],
374 1,
375 s->buttons);
376 }
377 break;
378 case INPUT_EVENT_KIND_ABS:
379 s->axis[evt->abs->axis] = evt->abs->value;
380 break;
381 case INPUT_EVENT_KIND_REL:
382 s->axis[evt->rel->axis] += evt->rel->value;
383 break;
384 default:
385 break;
386 }
387 }
388
389 static void legacy_mouse_sync(DeviceState *dev)
390 {
391 QEMUPutMouseEntry *s = (QEMUPutMouseEntry *)dev;
392
393 s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque,
394 s->axis[INPUT_AXIS_X],
395 s->axis[INPUT_AXIS_Y],
396 0,
397 s->buttons);
398
399 if (!s->qemu_put_mouse_event_absolute) {
400 s->axis[INPUT_AXIS_X] = 0;
401 s->axis[INPUT_AXIS_Y] = 0;
402 }
403 }
404
405 QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
406 void *opaque, int absolute,
407 const char *name)
408 {
409 QEMUPutMouseEntry *s;
410
411 s = g_malloc0(sizeof(QEMUPutMouseEntry));
412
413 s->qemu_put_mouse_event = func;
414 s->qemu_put_mouse_event_opaque = opaque;
415 s->qemu_put_mouse_event_absolute = absolute;
416
417 s->h.name = name;
418 s->h.mask = INPUT_EVENT_MASK_BTN |
419 (absolute ? INPUT_EVENT_MASK_ABS : INPUT_EVENT_MASK_REL);
420 s->h.event = legacy_mouse_event;
421 s->h.sync = legacy_mouse_sync;
422 s->s = qemu_input_handler_register((DeviceState *)s,
423 &s->h);
424
425 return s;
426 }
427
428 void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
429 {
430 qemu_input_handler_activate(entry->s);
431 }
432
433 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
434 {
435 qemu_input_handler_unregister(entry->s);
436
437 g_free(entry);
438 }
439
440 QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
441 void *opaque)
442 {
443 QEMUPutLEDEntry *s;
444
445 s = g_malloc0(sizeof(QEMUPutLEDEntry));
446
447 s->put_led = func;
448 s->opaque = opaque;
449 QTAILQ_INSERT_TAIL(&led_handlers, s, next);
450 return s;
451 }
452
453 void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry)
454 {
455 if (entry == NULL)
456 return;
457 QTAILQ_REMOVE(&led_handlers, entry, next);
458 g_free(entry);
459 }
460
461 void kbd_put_ledstate(int ledstate)
462 {
463 QEMUPutLEDEntry *cursor;
464
465 QTAILQ_FOREACH(cursor, &led_handlers, next) {
466 cursor->put_led(cursor->opaque, ledstate);
467 }
468 }