2
0

input-legacy.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. int index_from_key(const char *key)
  57. {
  58. int i;
  59. for (i = 0; QKeyCode_lookup[i] != NULL; i++) {
  60. if (!strcmp(key, QKeyCode_lookup[i])) {
  61. break;
  62. }
  63. }
  64. /* Return Q_KEY_CODE_MAX if the key is invalid */
  65. return i;
  66. }
  67. static KeyValue *copy_key_value(KeyValue *src)
  68. {
  69. KeyValue *dst = g_new(KeyValue, 1);
  70. memcpy(dst, src, sizeof(*src));
  71. return dst;
  72. }
  73. void qmp_send_key(KeyValueList *keys, bool has_hold_time, int64_t hold_time,
  74. Error **errp)
  75. {
  76. KeyValueList *p;
  77. KeyValue **up = NULL;
  78. int count = 0;
  79. if (!has_hold_time) {
  80. hold_time = 0; /* use default */
  81. }
  82. for (p = keys; p != NULL; p = p->next) {
  83. qemu_input_event_send_key(NULL, copy_key_value(p->value), true);
  84. qemu_input_event_send_key_delay(hold_time);
  85. up = g_realloc(up, sizeof(*up) * (count+1));
  86. up[count] = copy_key_value(p->value);
  87. count++;
  88. }
  89. while (count) {
  90. count--;
  91. qemu_input_event_send_key(NULL, up[count], false);
  92. qemu_input_event_send_key_delay(hold_time);
  93. }
  94. g_free(up);
  95. }
  96. static void legacy_kbd_event(DeviceState *dev, QemuConsole *src,
  97. InputEvent *evt)
  98. {
  99. QEMUPutKbdEntry *entry = (QEMUPutKbdEntry *)dev;
  100. int scancodes[3], i, count;
  101. if (!entry || !entry->put_kbd) {
  102. return;
  103. }
  104. count = qemu_input_key_value_to_scancode(evt->key->key,
  105. evt->key->down,
  106. scancodes);
  107. for (i = 0; i < count; i++) {
  108. entry->put_kbd(entry->opaque, scancodes[i]);
  109. }
  110. }
  111. static QemuInputHandler legacy_kbd_handler = {
  112. .name = "legacy-kbd",
  113. .mask = INPUT_EVENT_MASK_KEY,
  114. .event = legacy_kbd_event,
  115. };
  116. QEMUPutKbdEntry *qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
  117. {
  118. QEMUPutKbdEntry *entry;
  119. entry = g_new0(QEMUPutKbdEntry, 1);
  120. entry->put_kbd = func;
  121. entry->opaque = opaque;
  122. entry->s = qemu_input_handler_register((DeviceState *)entry,
  123. &legacy_kbd_handler);
  124. qemu_input_handler_activate(entry->s);
  125. return entry;
  126. }
  127. void qemu_remove_kbd_event_handler(QEMUPutKbdEntry *entry)
  128. {
  129. qemu_input_handler_unregister(entry->s);
  130. g_free(entry);
  131. }
  132. static void legacy_mouse_event(DeviceState *dev, QemuConsole *src,
  133. InputEvent *evt)
  134. {
  135. static const int bmap[INPUT_BUTTON_MAX] = {
  136. [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
  137. [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
  138. [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
  139. };
  140. QEMUPutMouseEntry *s = (QEMUPutMouseEntry *)dev;
  141. switch (evt->kind) {
  142. case INPUT_EVENT_KIND_BTN:
  143. if (evt->btn->down) {
  144. s->buttons |= bmap[evt->btn->button];
  145. } else {
  146. s->buttons &= ~bmap[evt->btn->button];
  147. }
  148. if (evt->btn->down && evt->btn->button == INPUT_BUTTON_WHEEL_UP) {
  149. s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque,
  150. s->axis[INPUT_AXIS_X],
  151. s->axis[INPUT_AXIS_Y],
  152. -1,
  153. s->buttons);
  154. }
  155. if (evt->btn->down && evt->btn->button == INPUT_BUTTON_WHEEL_DOWN) {
  156. s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque,
  157. s->axis[INPUT_AXIS_X],
  158. s->axis[INPUT_AXIS_Y],
  159. 1,
  160. s->buttons);
  161. }
  162. break;
  163. case INPUT_EVENT_KIND_ABS:
  164. s->axis[evt->abs->axis] = evt->abs->value;
  165. break;
  166. case INPUT_EVENT_KIND_REL:
  167. s->axis[evt->rel->axis] += evt->rel->value;
  168. break;
  169. default:
  170. break;
  171. }
  172. }
  173. static void legacy_mouse_sync(DeviceState *dev)
  174. {
  175. QEMUPutMouseEntry *s = (QEMUPutMouseEntry *)dev;
  176. s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque,
  177. s->axis[INPUT_AXIS_X],
  178. s->axis[INPUT_AXIS_Y],
  179. 0,
  180. s->buttons);
  181. if (!s->qemu_put_mouse_event_absolute) {
  182. s->axis[INPUT_AXIS_X] = 0;
  183. s->axis[INPUT_AXIS_Y] = 0;
  184. }
  185. }
  186. QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
  187. void *opaque, int absolute,
  188. const char *name)
  189. {
  190. QEMUPutMouseEntry *s;
  191. s = g_malloc0(sizeof(QEMUPutMouseEntry));
  192. s->qemu_put_mouse_event = func;
  193. s->qemu_put_mouse_event_opaque = opaque;
  194. s->qemu_put_mouse_event_absolute = absolute;
  195. s->h.name = name;
  196. s->h.mask = INPUT_EVENT_MASK_BTN |
  197. (absolute ? INPUT_EVENT_MASK_ABS : INPUT_EVENT_MASK_REL);
  198. s->h.event = legacy_mouse_event;
  199. s->h.sync = legacy_mouse_sync;
  200. s->s = qemu_input_handler_register((DeviceState *)s,
  201. &s->h);
  202. return s;
  203. }
  204. void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
  205. {
  206. qemu_input_handler_activate(entry->s);
  207. }
  208. void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
  209. {
  210. qemu_input_handler_unregister(entry->s);
  211. g_free(entry);
  212. }
  213. QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
  214. void *opaque)
  215. {
  216. QEMUPutLEDEntry *s;
  217. s = g_malloc0(sizeof(QEMUPutLEDEntry));
  218. s->put_led = func;
  219. s->opaque = opaque;
  220. QTAILQ_INSERT_TAIL(&led_handlers, s, next);
  221. return s;
  222. }
  223. void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry)
  224. {
  225. if (entry == NULL)
  226. return;
  227. QTAILQ_REMOVE(&led_handlers, entry, next);
  228. g_free(entry);
  229. }
  230. void kbd_put_ledstate(int ledstate)
  231. {
  232. QEMUPutLEDEntry *cursor;
  233. QTAILQ_FOREACH(cursor, &led_handlers, next) {
  234. cursor->put_led(cursor->opaque, ledstate);
  235. }
  236. }