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 return entry; 337 } 338 339 void qemu_remove_kbd_event_handler(QEMUPutKbdEntry *entry) 340 { 341 qemu_input_handler_unregister(entry->s); 342 g_free(entry); 343 } 344 345 static void legacy_mouse_event(DeviceState *dev, QemuConsole *src, 346 InputEvent *evt) 347 { 348 static const int bmap[INPUT_BUTTON_MAX] = { 349 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON, 350 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON, 351 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON, 352 }; 353 QEMUPutMouseEntry *s = (QEMUPutMouseEntry *)dev; 354 355 switch (evt->kind) { 356 case INPUT_EVENT_KIND_BTN: 357 if (evt->btn->down) { 358 s->buttons |= bmap[evt->btn->button]; 359 } else { 360 s->buttons &= ~bmap[evt->btn->button]; 361 } 362 if (evt->btn->down && evt->btn->button == INPUT_BUTTON_WHEEL_UP) { 363 s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque, 364 s->axis[INPUT_AXIS_X], 365 s->axis[INPUT_AXIS_Y], 366 -1, 367 s->buttons); 368 } 369 if (evt->btn->down && evt->btn->button == INPUT_BUTTON_WHEEL_DOWN) { 370 s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque, 371 s->axis[INPUT_AXIS_X], 372 s->axis[INPUT_AXIS_Y], 373 1, 374 s->buttons); 375 } 376 break; 377 case INPUT_EVENT_KIND_ABS: 378 s->axis[evt->abs->axis] = evt->abs->value; 379 break; 380 case INPUT_EVENT_KIND_REL: 381 s->axis[evt->rel->axis] += evt->rel->value; 382 break; 383 default: 384 break; 385 } 386 } 387 388 static void legacy_mouse_sync(DeviceState *dev) 389 { 390 QEMUPutMouseEntry *s = (QEMUPutMouseEntry *)dev; 391 392 s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque, 393 s->axis[INPUT_AXIS_X], 394 s->axis[INPUT_AXIS_Y], 395 0, 396 s->buttons); 397 398 if (!s->qemu_put_mouse_event_absolute) { 399 s->axis[INPUT_AXIS_X] = 0; 400 s->axis[INPUT_AXIS_Y] = 0; 401 } 402 } 403 404 QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, 405 void *opaque, int absolute, 406 const char *name) 407 { 408 QEMUPutMouseEntry *s; 409 410 s = g_malloc0(sizeof(QEMUPutMouseEntry)); 411 412 s->qemu_put_mouse_event = func; 413 s->qemu_put_mouse_event_opaque = opaque; 414 s->qemu_put_mouse_event_absolute = absolute; 415 416 s->h.name = name; 417 s->h.mask = INPUT_EVENT_MASK_BTN | 418 (absolute ? INPUT_EVENT_MASK_ABS : INPUT_EVENT_MASK_REL); 419 s->h.event = legacy_mouse_event; 420 s->h.sync = legacy_mouse_sync; 421 s->s = qemu_input_handler_register((DeviceState *)s, 422 &s->h); 423 424 return s; 425 } 426 427 void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry) 428 { 429 qemu_input_handler_activate(entry->s); 430 } 431 432 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry) 433 { 434 qemu_input_handler_unregister(entry->s); 435 436 g_free(entry); 437 } 438 439 QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func, 440 void *opaque) 441 { 442 QEMUPutLEDEntry *s; 443 444 s = g_malloc0(sizeof(QEMUPutLEDEntry)); 445 446 s->put_led = func; 447 s->opaque = opaque; 448 QTAILQ_INSERT_TAIL(&led_handlers, s, next); 449 return s; 450 } 451 452 void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry) 453 { 454 if (entry == NULL) 455 return; 456 QTAILQ_REMOVE(&led_handlers, entry, next); 457 g_free(entry); 458 } 459 460 void kbd_put_ledstate(int ledstate) 461 { 462 QEMUPutLEDEntry *cursor; 463 464 QTAILQ_FOREACH(cursor, &led_handlers, next) { 465 cursor->put_led(cursor->opaque, ledstate); 466 } 467 } 468