2
0

input.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 (qemu_put_kbd_event) {
  110. qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
  111. }
  112. }
  113. void kbd_put_ledstate(int ledstate)
  114. {
  115. QEMUPutLEDEntry *cursor;
  116. QTAILQ_FOREACH(cursor, &led_handlers, next) {
  117. cursor->put_led(cursor->opaque, ledstate);
  118. }
  119. }
  120. void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
  121. {
  122. QEMUPutMouseEntry *entry;
  123. QEMUPutMouseEvent *mouse_event;
  124. void *mouse_event_opaque;
  125. int width, height;
  126. if (QTAILQ_EMPTY(&mouse_handlers)) {
  127. return;
  128. }
  129. entry = QTAILQ_FIRST(&mouse_handlers);
  130. mouse_event = entry->qemu_put_mouse_event;
  131. mouse_event_opaque = entry->qemu_put_mouse_event_opaque;
  132. if (mouse_event) {
  133. if (entry->qemu_put_mouse_event_absolute) {
  134. width = 0x7fff;
  135. height = 0x7fff;
  136. } else {
  137. width = graphic_width - 1;
  138. height = graphic_height - 1;
  139. }
  140. switch (graphic_rotate) {
  141. case 0:
  142. mouse_event(mouse_event_opaque,
  143. dx, dy, dz, buttons_state);
  144. break;
  145. case 90:
  146. mouse_event(mouse_event_opaque,
  147. width - dy, dx, dz, buttons_state);
  148. break;
  149. case 180:
  150. mouse_event(mouse_event_opaque,
  151. width - dx, height - dy, dz, buttons_state);
  152. break;
  153. case 270:
  154. mouse_event(mouse_event_opaque,
  155. dy, height - dx, dz, buttons_state);
  156. break;
  157. }
  158. }
  159. }
  160. int kbd_mouse_is_absolute(void)
  161. {
  162. if (QTAILQ_EMPTY(&mouse_handlers)) {
  163. return 0;
  164. }
  165. return QTAILQ_FIRST(&mouse_handlers)->qemu_put_mouse_event_absolute;
  166. }
  167. int kbd_mouse_has_absolute(void)
  168. {
  169. QEMUPutMouseEntry *entry;
  170. QTAILQ_FOREACH(entry, &mouse_handlers, node) {
  171. if (entry->qemu_put_mouse_event_absolute) {
  172. return 1;
  173. }
  174. }
  175. return 0;
  176. }
  177. MouseInfoList *qmp_query_mice(Error **errp)
  178. {
  179. MouseInfoList *mice_list = NULL;
  180. QEMUPutMouseEntry *cursor;
  181. bool current = true;
  182. QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
  183. MouseInfoList *info = g_malloc0(sizeof(*info));
  184. info->value = g_malloc0(sizeof(*info->value));
  185. info->value->name = g_strdup(cursor->qemu_put_mouse_event_name);
  186. info->value->index = cursor->index;
  187. info->value->absolute = !!cursor->qemu_put_mouse_event_absolute;
  188. info->value->current = current;
  189. current = false;
  190. info->next = mice_list;
  191. mice_list = info;
  192. }
  193. return mice_list;
  194. }
  195. void do_mouse_set(Monitor *mon, const QDict *qdict)
  196. {
  197. QEMUPutMouseEntry *cursor;
  198. int index = qdict_get_int(qdict, "index");
  199. int found = 0;
  200. if (QTAILQ_EMPTY(&mouse_handlers)) {
  201. monitor_printf(mon, "No mouse devices connected\n");
  202. return;
  203. }
  204. QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
  205. if (cursor->index == index) {
  206. found = 1;
  207. qemu_activate_mouse_event_handler(cursor);
  208. break;
  209. }
  210. }
  211. if (!found) {
  212. monitor_printf(mon, "Mouse at given index not found\n");
  213. }
  214. check_mode_change();
  215. }
  216. void qemu_add_mouse_mode_change_notifier(Notifier *notify)
  217. {
  218. notifier_list_add(&mouse_mode_notifiers, notify);
  219. }
  220. void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
  221. {
  222. notifier_list_remove(&mouse_mode_notifiers, notify);
  223. }