Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 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 | |
| 25 | #include "sysemu.h" |
| 26 | #include "net.h" |
| 27 | #include "monitor.h" |
| 28 | #include "console.h" |
| 29 | #include "qjson.h" |
| 30 | |
| 31 | |
| 32 | static QEMUPutKBDEvent *qemu_put_kbd_event; |
| 33 | static void *qemu_put_kbd_event_opaque; |
| 34 | static QEMUPutMouseEntry *qemu_put_mouse_event_head; |
| 35 | static QEMUPutMouseEntry *qemu_put_mouse_event_current; |
Gerd Hoffmann | 03a23a8 | 2010-02-26 17:17:36 +0100 | [diff] [blame^] | 36 | static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers = QTAILQ_HEAD_INITIALIZER(led_handlers); |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 37 | |
| 38 | void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) |
| 39 | { |
| 40 | qemu_put_kbd_event_opaque = opaque; |
| 41 | qemu_put_kbd_event = func; |
| 42 | } |
| 43 | |
| 44 | QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, |
| 45 | void *opaque, int absolute, |
| 46 | const char *name) |
| 47 | { |
| 48 | QEMUPutMouseEntry *s, *cursor; |
| 49 | |
| 50 | s = qemu_mallocz(sizeof(QEMUPutMouseEntry)); |
| 51 | |
| 52 | s->qemu_put_mouse_event = func; |
| 53 | s->qemu_put_mouse_event_opaque = opaque; |
| 54 | s->qemu_put_mouse_event_absolute = absolute; |
| 55 | s->qemu_put_mouse_event_name = qemu_strdup(name); |
| 56 | s->next = NULL; |
| 57 | |
| 58 | if (!qemu_put_mouse_event_head) { |
| 59 | qemu_put_mouse_event_head = qemu_put_mouse_event_current = s; |
| 60 | return s; |
| 61 | } |
| 62 | |
| 63 | cursor = qemu_put_mouse_event_head; |
| 64 | while (cursor->next != NULL) |
| 65 | cursor = cursor->next; |
| 66 | |
| 67 | cursor->next = s; |
| 68 | qemu_put_mouse_event_current = s; |
| 69 | |
| 70 | return s; |
| 71 | } |
| 72 | |
| 73 | void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry) |
| 74 | { |
| 75 | QEMUPutMouseEntry *prev = NULL, *cursor; |
| 76 | |
| 77 | if (!qemu_put_mouse_event_head || entry == NULL) |
| 78 | return; |
| 79 | |
| 80 | cursor = qemu_put_mouse_event_head; |
| 81 | while (cursor != NULL && cursor != entry) { |
| 82 | prev = cursor; |
| 83 | cursor = cursor->next; |
| 84 | } |
| 85 | |
| 86 | if (cursor == NULL) // does not exist or list empty |
| 87 | return; |
| 88 | else if (prev == NULL) { // entry is head |
| 89 | qemu_put_mouse_event_head = cursor->next; |
| 90 | if (qemu_put_mouse_event_current == entry) |
| 91 | qemu_put_mouse_event_current = cursor->next; |
| 92 | qemu_free(entry->qemu_put_mouse_event_name); |
| 93 | qemu_free(entry); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | prev->next = entry->next; |
| 98 | |
| 99 | if (qemu_put_mouse_event_current == entry) |
| 100 | qemu_put_mouse_event_current = prev; |
| 101 | |
| 102 | qemu_free(entry->qemu_put_mouse_event_name); |
| 103 | qemu_free(entry); |
| 104 | } |
| 105 | |
Gerd Hoffmann | 03a23a8 | 2010-02-26 17:17:36 +0100 | [diff] [blame^] | 106 | QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func, |
| 107 | void *opaque) |
| 108 | { |
| 109 | QEMUPutLEDEntry *s; |
| 110 | |
| 111 | s = qemu_mallocz(sizeof(QEMUPutLEDEntry)); |
| 112 | |
| 113 | s->put_led = func; |
| 114 | s->opaque = opaque; |
| 115 | QTAILQ_INSERT_TAIL(&led_handlers, s, next); |
| 116 | return s; |
| 117 | } |
| 118 | |
| 119 | void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry) |
| 120 | { |
| 121 | if (entry == NULL) |
| 122 | return; |
| 123 | QTAILQ_REMOVE(&led_handlers, entry, next); |
| 124 | qemu_free(entry); |
| 125 | } |
| 126 | |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 127 | void kbd_put_keycode(int keycode) |
| 128 | { |
| 129 | if (qemu_put_kbd_event) { |
| 130 | qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); |
| 131 | } |
| 132 | } |
| 133 | |
Gerd Hoffmann | 03a23a8 | 2010-02-26 17:17:36 +0100 | [diff] [blame^] | 134 | void kbd_put_ledstate(int ledstate) |
| 135 | { |
| 136 | QEMUPutLEDEntry *cursor; |
| 137 | |
| 138 | QTAILQ_FOREACH(cursor, &led_handlers, next) { |
| 139 | cursor->put_led(cursor->opaque, ledstate); |
| 140 | } |
| 141 | } |
| 142 | |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 143 | void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) |
| 144 | { |
| 145 | QEMUPutMouseEvent *mouse_event; |
| 146 | void *mouse_event_opaque; |
| 147 | int width; |
| 148 | |
| 149 | if (!qemu_put_mouse_event_current) { |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | mouse_event = |
| 154 | qemu_put_mouse_event_current->qemu_put_mouse_event; |
| 155 | mouse_event_opaque = |
| 156 | qemu_put_mouse_event_current->qemu_put_mouse_event_opaque; |
| 157 | |
| 158 | if (mouse_event) { |
| 159 | if (graphic_rotate) { |
| 160 | if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute) |
| 161 | width = 0x7fff; |
| 162 | else |
| 163 | width = graphic_width - 1; |
| 164 | mouse_event(mouse_event_opaque, |
| 165 | width - dy, dx, dz, buttons_state); |
| 166 | } else |
| 167 | mouse_event(mouse_event_opaque, |
| 168 | dx, dy, dz, buttons_state); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | int kbd_mouse_is_absolute(void) |
| 173 | { |
| 174 | if (!qemu_put_mouse_event_current) |
| 175 | return 0; |
| 176 | |
| 177 | return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute; |
| 178 | } |
| 179 | |
| 180 | static void info_mice_iter(QObject *data, void *opaque) |
| 181 | { |
| 182 | QDict *mouse; |
| 183 | Monitor *mon = opaque; |
| 184 | |
| 185 | mouse = qobject_to_qdict(data); |
| 186 | monitor_printf(mon, "%c Mouse #%" PRId64 ": %s\n", |
| 187 | (qdict_get_bool(mouse, "current") ? '*' : ' '), |
| 188 | qdict_get_int(mouse, "index"), qdict_get_str(mouse, "name")); |
| 189 | } |
| 190 | |
| 191 | void do_info_mice_print(Monitor *mon, const QObject *data) |
| 192 | { |
| 193 | QList *mice_list; |
| 194 | |
| 195 | mice_list = qobject_to_qlist(data); |
| 196 | if (qlist_empty(mice_list)) { |
| 197 | monitor_printf(mon, "No mouse devices connected\n"); |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | qlist_iter(mice_list, info_mice_iter, mon); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * do_info_mice(): Show VM mice information |
| 206 | * |
| 207 | * Each mouse is represented by a QDict, the returned QObject is a QList of |
| 208 | * all mice. |
| 209 | * |
| 210 | * The mouse QDict contains the following: |
| 211 | * |
| 212 | * - "name": mouse's name |
| 213 | * - "index": mouse's index |
| 214 | * - "current": true if this mouse is receiving events, false otherwise |
| 215 | * |
| 216 | * Example: |
| 217 | * |
| 218 | * [ { "name": "QEMU Microsoft Mouse", "index": 0, "current": false }, |
| 219 | * { "name": "QEMU PS/2 Mouse", "index": 1, "current": true } ] |
| 220 | */ |
| 221 | void do_info_mice(Monitor *mon, QObject **ret_data) |
| 222 | { |
| 223 | QEMUPutMouseEntry *cursor; |
| 224 | QList *mice_list; |
| 225 | int index = 0; |
| 226 | |
| 227 | mice_list = qlist_new(); |
| 228 | |
| 229 | if (!qemu_put_mouse_event_head) { |
| 230 | goto out; |
| 231 | } |
| 232 | |
| 233 | cursor = qemu_put_mouse_event_head; |
| 234 | while (cursor != NULL) { |
| 235 | QObject *obj; |
| 236 | obj = qobject_from_jsonf("{ 'name': %s, 'index': %d, 'current': %i }", |
| 237 | cursor->qemu_put_mouse_event_name, |
| 238 | index, cursor == qemu_put_mouse_event_current); |
| 239 | qlist_append_obj(mice_list, obj); |
| 240 | index++; |
| 241 | cursor = cursor->next; |
| 242 | } |
| 243 | |
| 244 | out: |
| 245 | *ret_data = QOBJECT(mice_list); |
| 246 | } |
| 247 | |
| 248 | void do_mouse_set(Monitor *mon, const QDict *qdict) |
| 249 | { |
| 250 | QEMUPutMouseEntry *cursor; |
| 251 | int i = 0; |
| 252 | int index = qdict_get_int(qdict, "index"); |
| 253 | |
| 254 | if (!qemu_put_mouse_event_head) { |
| 255 | monitor_printf(mon, "No mouse devices connected\n"); |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | cursor = qemu_put_mouse_event_head; |
| 260 | while (cursor != NULL && index != i) { |
| 261 | i++; |
| 262 | cursor = cursor->next; |
| 263 | } |
| 264 | |
| 265 | if (cursor != NULL) |
| 266 | qemu_put_mouse_event_current = cursor; |
| 267 | else |
| 268 | monitor_printf(mon, "Mouse at given index not found\n"); |
| 269 | } |