]>
Commit | Line | Data |
---|---|---|
8f0056b7 PB |
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 | ||
8f0056b7 PB |
31 | static QEMUPutKBDEvent *qemu_put_kbd_event; |
32 | static void *qemu_put_kbd_event_opaque; | |
03a23a85 | 33 | static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers = QTAILQ_HEAD_INITIALIZER(led_handlers); |
6fef28ee AL |
34 | static QTAILQ_HEAD(, QEMUPutMouseEntry) mouse_handlers = |
35 | QTAILQ_HEAD_INITIALIZER(mouse_handlers); | |
7e581fb3 AL |
36 | static NotifierList mouse_mode_notifiers = |
37 | NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers); | |
8f0056b7 PB |
38 | |
39 | void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) | |
40 | { | |
41 | qemu_put_kbd_event_opaque = opaque; | |
42 | qemu_put_kbd_event = func; | |
43 | } | |
44 | ||
7e581fb3 AL |
45 | static void check_mode_change(void) |
46 | { | |
47 | static int current_is_absolute, current_has_absolute; | |
48 | int is_absolute; | |
49 | int has_absolute; | |
50 | ||
51 | is_absolute = kbd_mouse_is_absolute(); | |
52 | has_absolute = kbd_mouse_has_absolute(); | |
53 | ||
54 | if (is_absolute != current_is_absolute || | |
55 | has_absolute != current_has_absolute) { | |
56 | notifier_list_notify(&mouse_mode_notifiers); | |
57 | } | |
58 | ||
59 | current_is_absolute = is_absolute; | |
60 | current_has_absolute = has_absolute; | |
61 | } | |
62 | ||
8f0056b7 PB |
63 | QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, |
64 | void *opaque, int absolute, | |
65 | const char *name) | |
66 | { | |
6fef28ee AL |
67 | QEMUPutMouseEntry *s; |
68 | static int mouse_index = 0; | |
8f0056b7 PB |
69 | |
70 | s = qemu_mallocz(sizeof(QEMUPutMouseEntry)); | |
71 | ||
72 | s->qemu_put_mouse_event = func; | |
73 | s->qemu_put_mouse_event_opaque = opaque; | |
74 | s->qemu_put_mouse_event_absolute = absolute; | |
75 | s->qemu_put_mouse_event_name = qemu_strdup(name); | |
6fef28ee | 76 | s->index = mouse_index++; |
8f0056b7 | 77 | |
6fef28ee | 78 | QTAILQ_INSERT_TAIL(&mouse_handlers, s, node); |
8f0056b7 | 79 | |
7e581fb3 AL |
80 | check_mode_change(); |
81 | ||
8f0056b7 PB |
82 | return s; |
83 | } | |
84 | ||
6fef28ee | 85 | void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry) |
8f0056b7 | 86 | { |
6fef28ee AL |
87 | QTAILQ_REMOVE(&mouse_handlers, entry, node); |
88 | QTAILQ_INSERT_HEAD(&mouse_handlers, entry, node); | |
8f0056b7 | 89 | |
6fef28ee AL |
90 | check_mode_change(); |
91 | } | |
8f0056b7 | 92 | |
6fef28ee AL |
93 | void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry) |
94 | { | |
95 | QTAILQ_REMOVE(&mouse_handlers, entry, node); | |
8f0056b7 PB |
96 | |
97 | qemu_free(entry->qemu_put_mouse_event_name); | |
98 | qemu_free(entry); | |
7e581fb3 AL |
99 | |
100 | check_mode_change(); | |
8f0056b7 PB |
101 | } |
102 | ||
03a23a85 GH |
103 | QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func, |
104 | void *opaque) | |
105 | { | |
106 | QEMUPutLEDEntry *s; | |
107 | ||
108 | s = qemu_mallocz(sizeof(QEMUPutLEDEntry)); | |
109 | ||
110 | s->put_led = func; | |
111 | s->opaque = opaque; | |
112 | QTAILQ_INSERT_TAIL(&led_handlers, s, next); | |
113 | return s; | |
114 | } | |
115 | ||
116 | void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry) | |
117 | { | |
118 | if (entry == NULL) | |
119 | return; | |
120 | QTAILQ_REMOVE(&led_handlers, entry, next); | |
121 | qemu_free(entry); | |
122 | } | |
123 | ||
8f0056b7 PB |
124 | void kbd_put_keycode(int keycode) |
125 | { | |
126 | if (qemu_put_kbd_event) { | |
127 | qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); | |
128 | } | |
129 | } | |
130 | ||
03a23a85 GH |
131 | void kbd_put_ledstate(int ledstate) |
132 | { | |
133 | QEMUPutLEDEntry *cursor; | |
134 | ||
135 | QTAILQ_FOREACH(cursor, &led_handlers, next) { | |
136 | cursor->put_led(cursor->opaque, ledstate); | |
137 | } | |
138 | } | |
139 | ||
8f0056b7 PB |
140 | void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) |
141 | { | |
6fef28ee | 142 | QEMUPutMouseEntry *entry; |
8f0056b7 PB |
143 | QEMUPutMouseEvent *mouse_event; |
144 | void *mouse_event_opaque; | |
145 | int width; | |
146 | ||
6fef28ee | 147 | if (QTAILQ_EMPTY(&mouse_handlers)) { |
8f0056b7 PB |
148 | return; |
149 | } | |
150 | ||
6fef28ee AL |
151 | entry = QTAILQ_FIRST(&mouse_handlers); |
152 | ||
153 | mouse_event = entry->qemu_put_mouse_event; | |
154 | mouse_event_opaque = entry->qemu_put_mouse_event_opaque; | |
8f0056b7 PB |
155 | |
156 | if (mouse_event) { | |
157 | if (graphic_rotate) { | |
6fef28ee | 158 | if (entry->qemu_put_mouse_event_absolute) |
8f0056b7 PB |
159 | width = 0x7fff; |
160 | else | |
161 | width = graphic_width - 1; | |
162 | mouse_event(mouse_event_opaque, | |
6fef28ee | 163 | width - dy, dx, dz, buttons_state); |
8f0056b7 PB |
164 | } else |
165 | mouse_event(mouse_event_opaque, | |
6fef28ee | 166 | dx, dy, dz, buttons_state); |
8f0056b7 PB |
167 | } |
168 | } | |
169 | ||
170 | int kbd_mouse_is_absolute(void) | |
171 | { | |
6fef28ee | 172 | if (QTAILQ_EMPTY(&mouse_handlers)) { |
8f0056b7 | 173 | return 0; |
6fef28ee | 174 | } |
8f0056b7 | 175 | |
6fef28ee | 176 | return QTAILQ_FIRST(&mouse_handlers)->qemu_put_mouse_event_absolute; |
8f0056b7 PB |
177 | } |
178 | ||
eb2e259d AL |
179 | int kbd_mouse_has_absolute(void) |
180 | { | |
181 | QEMUPutMouseEntry *entry; | |
182 | ||
183 | QTAILQ_FOREACH(entry, &mouse_handlers, node) { | |
184 | if (entry->qemu_put_mouse_event_absolute) { | |
185 | return 1; | |
186 | } | |
187 | } | |
188 | ||
189 | return 0; | |
190 | } | |
191 | ||
8f0056b7 PB |
192 | static void info_mice_iter(QObject *data, void *opaque) |
193 | { | |
194 | QDict *mouse; | |
195 | Monitor *mon = opaque; | |
196 | ||
197 | mouse = qobject_to_qdict(data); | |
1aaee43c | 198 | monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n", |
8f0056b7 | 199 | (qdict_get_bool(mouse, "current") ? '*' : ' '), |
1aaee43c AL |
200 | qdict_get_int(mouse, "index"), qdict_get_str(mouse, "name"), |
201 | qdict_get_bool(mouse, "absolute") ? " (absolute)" : ""); | |
8f0056b7 PB |
202 | } |
203 | ||
204 | void do_info_mice_print(Monitor *mon, const QObject *data) | |
205 | { | |
206 | QList *mice_list; | |
207 | ||
208 | mice_list = qobject_to_qlist(data); | |
209 | if (qlist_empty(mice_list)) { | |
210 | monitor_printf(mon, "No mouse devices connected\n"); | |
211 | return; | |
212 | } | |
213 | ||
214 | qlist_iter(mice_list, info_mice_iter, mon); | |
215 | } | |
216 | ||
217 | /** | |
218 | * do_info_mice(): Show VM mice information | |
219 | * | |
220 | * Each mouse is represented by a QDict, the returned QObject is a QList of | |
221 | * all mice. | |
222 | * | |
223 | * The mouse QDict contains the following: | |
224 | * | |
225 | * - "name": mouse's name | |
226 | * - "index": mouse's index | |
227 | * - "current": true if this mouse is receiving events, false otherwise | |
1aaee43c | 228 | * - "absolute": true if the mouse generates absolute input events |
8f0056b7 PB |
229 | * |
230 | * Example: | |
231 | * | |
1aaee43c AL |
232 | * [ { "name": "QEMU Microsoft Mouse", "index": 0, "current": false, "absolute": false }, |
233 | * { "name": "QEMU PS/2 Mouse", "index": 1, "current": true, "absolute": true } ] | |
8f0056b7 PB |
234 | */ |
235 | void do_info_mice(Monitor *mon, QObject **ret_data) | |
236 | { | |
237 | QEMUPutMouseEntry *cursor; | |
238 | QList *mice_list; | |
6fef28ee | 239 | int current; |
8f0056b7 PB |
240 | |
241 | mice_list = qlist_new(); | |
242 | ||
6fef28ee | 243 | if (QTAILQ_EMPTY(&mouse_handlers)) { |
8f0056b7 PB |
244 | goto out; |
245 | } | |
246 | ||
6fef28ee AL |
247 | current = QTAILQ_FIRST(&mouse_handlers)->index; |
248 | ||
249 | QTAILQ_FOREACH(cursor, &mouse_handlers, node) { | |
8f0056b7 | 250 | QObject *obj; |
1aaee43c AL |
251 | obj = qobject_from_jsonf("{ 'name': %s," |
252 | " 'index': %d," | |
253 | " 'current': %i," | |
254 | " 'absolute': %i }", | |
8f0056b7 | 255 | cursor->qemu_put_mouse_event_name, |
6fef28ee | 256 | cursor->index, |
1aaee43c AL |
257 | cursor->index == current, |
258 | !!cursor->qemu_put_mouse_event_absolute); | |
8f0056b7 | 259 | qlist_append_obj(mice_list, obj); |
8f0056b7 PB |
260 | } |
261 | ||
262 | out: | |
263 | *ret_data = QOBJECT(mice_list); | |
264 | } | |
265 | ||
266 | void do_mouse_set(Monitor *mon, const QDict *qdict) | |
267 | { | |
268 | QEMUPutMouseEntry *cursor; | |
8f0056b7 | 269 | int index = qdict_get_int(qdict, "index"); |
6fef28ee | 270 | int found = 0; |
8f0056b7 | 271 | |
6fef28ee | 272 | if (QTAILQ_EMPTY(&mouse_handlers)) { |
8f0056b7 PB |
273 | monitor_printf(mon, "No mouse devices connected\n"); |
274 | return; | |
275 | } | |
276 | ||
6fef28ee AL |
277 | QTAILQ_FOREACH(cursor, &mouse_handlers, node) { |
278 | if (cursor->index == index) { | |
279 | found = 1; | |
280 | qemu_activate_mouse_event_handler(cursor); | |
281 | break; | |
282 | } | |
8f0056b7 PB |
283 | } |
284 | ||
6fef28ee | 285 | if (!found) { |
8f0056b7 | 286 | monitor_printf(mon, "Mouse at given index not found\n"); |
6fef28ee | 287 | } |
7e581fb3 AL |
288 | |
289 | check_mode_change(); | |
290 | } | |
291 | ||
292 | void qemu_add_mouse_mode_change_notifier(Notifier *notify) | |
293 | { | |
294 | notifier_list_add(&mouse_mode_notifiers, notify); | |
295 | } | |
296 | ||
297 | void qemu_remove_mouse_mode_change_notifier(Notifier *notify) | |
298 | { | |
299 | notifier_list_remove(&mouse_mode_notifiers, notify); | |
8f0056b7 | 300 | } |