input.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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.h"
  25. #include "net.h"
  26. #include "monitor.h"
  27. #include "console.h"
  28. #include "error.h"
  29. #include "qmp-commands.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. void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
  38. {
  39. qemu_put_kbd_event_opaque = opaque;
  40. qemu_put_kbd_event = func;
  41. }
  42. void qemu_remove_kbd_event_handler(void)
  43. {
  44. qemu_put_kbd_event_opaque = NULL;
  45. qemu_put_kbd_event = NULL;
  46. }
  47. static void check_mode_change(void)
  48. {
  49. static int current_is_absolute, current_has_absolute;
  50. int is_absolute;
  51. int has_absolute;
  52. is_absolute = kbd_mouse_is_absolute();
  53. has_absolute = kbd_mouse_has_absolute();
  54. if (is_absolute != current_is_absolute ||
  55. has_absolute != current_has_absolute) {
  56. notifier_list_notify(&mouse_mode_notifiers, NULL);
  57. }
  58. current_is_absolute = is_absolute;
  59. current_has_absolute = has_absolute;
  60. }
  61. QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
  62. void *opaque, int absolute,
  63. const char *name)
  64. {
  65. QEMUPutMouseEntry *s;
  66. static int mouse_index = 0;
  67. s = g_malloc0(sizeof(QEMUPutMouseEntry));
  68. s->qemu_put_mouse_event = func;
  69. s->qemu_put_mouse_event_opaque = opaque;
  70. s->qemu_put_mouse_event_absolute = absolute;
  71. s->qemu_put_mouse_event_name = g_strdup(name);
  72. s->index = mouse_index++;
  73. QTAILQ_INSERT_TAIL(&mouse_handlers, s, node);
  74. check_mode_change();
  75. return s;
  76. }
  77. void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
  78. {
  79. QTAILQ_REMOVE(&mouse_handlers, entry, node);
  80. QTAILQ_INSERT_HEAD(&mouse_handlers, entry, node);
  81. check_mode_change();
  82. }
  83. void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
  84. {
  85. QTAILQ_REMOVE(&mouse_handlers, entry, node);
  86. g_free(entry->qemu_put_mouse_event_name);
  87. g_free(entry);
  88. check_mode_change();
  89. }
  90. QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
  91. void *opaque)
  92. {
  93. QEMUPutLEDEntry *s;
  94. s = g_malloc0(sizeof(QEMUPutLEDEntry));
  95. s->put_led = func;
  96. s->opaque = opaque;
  97. QTAILQ_INSERT_TAIL(&led_handlers, s, next);
  98. return s;
  99. }
  100. void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry)
  101. {
  102. if (entry == NULL)
  103. return;
  104. QTAILQ_REMOVE(&led_handlers, entry, next);
  105. g_free(entry);
  106. }
  107. void kbd_put_keycode(int keycode)
  108. {
  109. if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
  110. return;
  111. }
  112. if (qemu_put_kbd_event) {
  113. qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
  114. }
  115. }
  116. void kbd_put_ledstate(int ledstate)
  117. {
  118. QEMUPutLEDEntry *cursor;
  119. QTAILQ_FOREACH(cursor, &led_handlers, next) {
  120. cursor->put_led(cursor->opaque, ledstate);
  121. }
  122. }
  123. void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
  124. {
  125. QEMUPutMouseEntry *entry;
  126. QEMUPutMouseEvent *mouse_event;
  127. void *mouse_event_opaque;
  128. int width, height;
  129. if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
  130. return;
  131. }
  132. if (QTAILQ_EMPTY(&mouse_handlers)) {
  133. return;
  134. }
  135. entry = QTAILQ_FIRST(&mouse_handlers);
  136. mouse_event = entry->qemu_put_mouse_event;
  137. mouse_event_opaque = entry->qemu_put_mouse_event_opaque;
  138. if (mouse_event) {
  139. if (entry->qemu_put_mouse_event_absolute) {
  140. width = 0x7fff;
  141. height = 0x7fff;
  142. } else {
  143. width = graphic_width - 1;
  144. height = graphic_height - 1;
  145. }
  146. switch (graphic_rotate) {
  147. case 0:
  148. mouse_event(mouse_event_opaque,
  149. dx, dy, dz, buttons_state);
  150. break;
  151. case 90:
  152. mouse_event(mouse_event_opaque,
  153. width - dy, dx, dz, buttons_state);
  154. break;
  155. case 180:
  156. mouse_event(mouse_event_opaque,
  157. width - dx, height - dy, dz, buttons_state);
  158. break;
  159. case 270:
  160. mouse_event(mouse_event_opaque,
  161. dy, height - dx, dz, buttons_state);
  162. break;
  163. }
  164. }
  165. }
  166. int kbd_mouse_is_absolute(void)
  167. {
  168. if (QTAILQ_EMPTY(&mouse_handlers)) {
  169. return 0;
  170. }
  171. return QTAILQ_FIRST(&mouse_handlers)->qemu_put_mouse_event_absolute;
  172. }
  173. int kbd_mouse_has_absolute(void)
  174. {
  175. QEMUPutMouseEntry *entry;
  176. QTAILQ_FOREACH(entry, &mouse_handlers, node) {
  177. if (entry->qemu_put_mouse_event_absolute) {
  178. return 1;
  179. }
  180. }
  181. return 0;
  182. }
  183. MouseInfoList *qmp_query_mice(Error **errp)
  184. {
  185. MouseInfoList *mice_list = NULL;
  186. QEMUPutMouseEntry *cursor;
  187. bool current = true;
  188. QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
  189. MouseInfoList *info = g_malloc0(sizeof(*info));
  190. info->value = g_malloc0(sizeof(*info->value));
  191. info->value->name = g_strdup(cursor->qemu_put_mouse_event_name);
  192. info->value->index = cursor->index;
  193. info->value->absolute = !!cursor->qemu_put_mouse_event_absolute;
  194. info->value->current = current;
  195. current = false;
  196. info->next = mice_list;
  197. mice_list = info;
  198. }
  199. return mice_list;
  200. }
  201. void do_mouse_set(Monitor *mon, const QDict *qdict)
  202. {
  203. QEMUPutMouseEntry *cursor;
  204. int index = qdict_get_int(qdict, "index");
  205. int found = 0;
  206. if (QTAILQ_EMPTY(&mouse_handlers)) {
  207. monitor_printf(mon, "No mouse devices connected\n");
  208. return;
  209. }
  210. QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
  211. if (cursor->index == index) {
  212. found = 1;
  213. qemu_activate_mouse_event_handler(cursor);
  214. break;
  215. }
  216. }
  217. if (!found) {
  218. monitor_printf(mon, "Mouse at given index not found\n");
  219. }
  220. check_mode_change();
  221. }
  222. void qemu_add_mouse_mode_change_notifier(Notifier *notify)
  223. {
  224. notifier_list_add(&mouse_mode_notifiers, notify);
  225. }
  226. void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
  227. {
  228. notifier_remove(notify);
  229. }