input.c 13 KB

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