input-legacy.c 13 KB

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