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