2
0

input.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 "qjson.h"
  29. static QEMUPutKBDEvent *qemu_put_kbd_event;
  30. static void *qemu_put_kbd_event_opaque;
  31. static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers = QTAILQ_HEAD_INITIALIZER(led_handlers);
  32. static QTAILQ_HEAD(, QEMUPutMouseEntry) mouse_handlers =
  33. QTAILQ_HEAD_INITIALIZER(mouse_handlers);
  34. static NotifierList mouse_mode_notifiers =
  35. NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers);
  36. void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
  37. {
  38. qemu_put_kbd_event_opaque = opaque;
  39. qemu_put_kbd_event = func;
  40. }
  41. void qemu_remove_kbd_event_handler(void)
  42. {
  43. qemu_put_kbd_event_opaque = NULL;
  44. qemu_put_kbd_event = NULL;
  45. }
  46. static void check_mode_change(void)
  47. {
  48. static int current_is_absolute, current_has_absolute;
  49. int is_absolute;
  50. int has_absolute;
  51. is_absolute = kbd_mouse_is_absolute();
  52. has_absolute = kbd_mouse_has_absolute();
  53. if (is_absolute != current_is_absolute ||
  54. has_absolute != current_has_absolute) {
  55. notifier_list_notify(&mouse_mode_notifiers, NULL);
  56. }
  57. current_is_absolute = is_absolute;
  58. current_has_absolute = has_absolute;
  59. }
  60. QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
  61. void *opaque, int absolute,
  62. const char *name)
  63. {
  64. QEMUPutMouseEntry *s;
  65. static int mouse_index = 0;
  66. s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
  67. s->qemu_put_mouse_event = func;
  68. s->qemu_put_mouse_event_opaque = opaque;
  69. s->qemu_put_mouse_event_absolute = absolute;
  70. s->qemu_put_mouse_event_name = qemu_strdup(name);
  71. s->index = mouse_index++;
  72. QTAILQ_INSERT_TAIL(&mouse_handlers, s, node);
  73. check_mode_change();
  74. return s;
  75. }
  76. void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
  77. {
  78. QTAILQ_REMOVE(&mouse_handlers, entry, node);
  79. QTAILQ_INSERT_HEAD(&mouse_handlers, entry, node);
  80. check_mode_change();
  81. }
  82. void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
  83. {
  84. QTAILQ_REMOVE(&mouse_handlers, entry, node);
  85. qemu_free(entry->qemu_put_mouse_event_name);
  86. qemu_free(entry);
  87. check_mode_change();
  88. }
  89. QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
  90. void *opaque)
  91. {
  92. QEMUPutLEDEntry *s;
  93. s = qemu_mallocz(sizeof(QEMUPutLEDEntry));
  94. s->put_led = func;
  95. s->opaque = opaque;
  96. QTAILQ_INSERT_TAIL(&led_handlers, s, next);
  97. return s;
  98. }
  99. void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry)
  100. {
  101. if (entry == NULL)
  102. return;
  103. QTAILQ_REMOVE(&led_handlers, entry, next);
  104. qemu_free(entry);
  105. }
  106. void kbd_put_keycode(int keycode)
  107. {
  108. if (qemu_put_kbd_event) {
  109. qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
  110. }
  111. }
  112. void kbd_put_ledstate(int ledstate)
  113. {
  114. QEMUPutLEDEntry *cursor;
  115. QTAILQ_FOREACH(cursor, &led_handlers, next) {
  116. cursor->put_led(cursor->opaque, ledstate);
  117. }
  118. }
  119. void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
  120. {
  121. QEMUPutMouseEntry *entry;
  122. QEMUPutMouseEvent *mouse_event;
  123. void *mouse_event_opaque;
  124. int width, height;
  125. if (QTAILQ_EMPTY(&mouse_handlers)) {
  126. return;
  127. }
  128. entry = QTAILQ_FIRST(&mouse_handlers);
  129. mouse_event = entry->qemu_put_mouse_event;
  130. mouse_event_opaque = entry->qemu_put_mouse_event_opaque;
  131. if (mouse_event) {
  132. if (entry->qemu_put_mouse_event_absolute) {
  133. width = 0x7fff;
  134. height = 0x7fff;
  135. } else {
  136. width = graphic_width - 1;
  137. height = graphic_height - 1;
  138. }
  139. switch (graphic_rotate) {
  140. case 0:
  141. mouse_event(mouse_event_opaque,
  142. dx, dy, dz, buttons_state);
  143. break;
  144. case 90:
  145. mouse_event(mouse_event_opaque,
  146. width - dy, dx, dz, buttons_state);
  147. break;
  148. case 180:
  149. mouse_event(mouse_event_opaque,
  150. width - dx, height - dy, dz, buttons_state);
  151. break;
  152. case 270:
  153. mouse_event(mouse_event_opaque,
  154. dy, height - dx, dz, buttons_state);
  155. break;
  156. }
  157. }
  158. }
  159. int kbd_mouse_is_absolute(void)
  160. {
  161. if (QTAILQ_EMPTY(&mouse_handlers)) {
  162. return 0;
  163. }
  164. return QTAILQ_FIRST(&mouse_handlers)->qemu_put_mouse_event_absolute;
  165. }
  166. int kbd_mouse_has_absolute(void)
  167. {
  168. QEMUPutMouseEntry *entry;
  169. QTAILQ_FOREACH(entry, &mouse_handlers, node) {
  170. if (entry->qemu_put_mouse_event_absolute) {
  171. return 1;
  172. }
  173. }
  174. return 0;
  175. }
  176. static void info_mice_iter(QObject *data, void *opaque)
  177. {
  178. QDict *mouse;
  179. Monitor *mon = opaque;
  180. mouse = qobject_to_qdict(data);
  181. monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
  182. (qdict_get_bool(mouse, "current") ? '*' : ' '),
  183. qdict_get_int(mouse, "index"), qdict_get_str(mouse, "name"),
  184. qdict_get_bool(mouse, "absolute") ? " (absolute)" : "");
  185. }
  186. void do_info_mice_print(Monitor *mon, const QObject *data)
  187. {
  188. QList *mice_list;
  189. mice_list = qobject_to_qlist(data);
  190. if (qlist_empty(mice_list)) {
  191. monitor_printf(mon, "No mouse devices connected\n");
  192. return;
  193. }
  194. qlist_iter(mice_list, info_mice_iter, mon);
  195. }
  196. void do_info_mice(Monitor *mon, QObject **ret_data)
  197. {
  198. QEMUPutMouseEntry *cursor;
  199. QList *mice_list;
  200. int current;
  201. mice_list = qlist_new();
  202. if (QTAILQ_EMPTY(&mouse_handlers)) {
  203. goto out;
  204. }
  205. current = QTAILQ_FIRST(&mouse_handlers)->index;
  206. QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
  207. QObject *obj;
  208. obj = qobject_from_jsonf("{ 'name': %s,"
  209. " 'index': %d,"
  210. " 'current': %i,"
  211. " 'absolute': %i }",
  212. cursor->qemu_put_mouse_event_name,
  213. cursor->index,
  214. cursor->index == current,
  215. !!cursor->qemu_put_mouse_event_absolute);
  216. qlist_append_obj(mice_list, obj);
  217. }
  218. out:
  219. *ret_data = QOBJECT(mice_list);
  220. }
  221. void do_mouse_set(Monitor *mon, const QDict *qdict)
  222. {
  223. QEMUPutMouseEntry *cursor;
  224. int index = qdict_get_int(qdict, "index");
  225. int found = 0;
  226. if (QTAILQ_EMPTY(&mouse_handlers)) {
  227. monitor_printf(mon, "No mouse devices connected\n");
  228. return;
  229. }
  230. QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
  231. if (cursor->index == index) {
  232. found = 1;
  233. qemu_activate_mouse_event_handler(cursor);
  234. break;
  235. }
  236. }
  237. if (!found) {
  238. monitor_printf(mon, "Mouse at given index not found\n");
  239. }
  240. check_mode_change();
  241. }
  242. void qemu_add_mouse_mode_change_notifier(Notifier *notify)
  243. {
  244. notifier_list_add(&mouse_mode_notifiers, notify);
  245. }
  246. void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
  247. {
  248. notifier_list_remove(&mouse_mode_notifiers, notify);
  249. }