]> git.proxmox.com Git - qemu.git/blob - input.c
Rewrite mouse handlers to use QTAILQ and to have an activation function
[qemu.git] / input.c
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 QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers = QTAILQ_HEAD_INITIALIZER(led_handlers);
35 static QTAILQ_HEAD(, QEMUPutMouseEntry) mouse_handlers =
36 QTAILQ_HEAD_INITIALIZER(mouse_handlers);
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;
49 static int mouse_index = 0;
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);
57 s->index = mouse_index++;
58
59 QTAILQ_INSERT_TAIL(&mouse_handlers, s, node);
60
61 return s;
62 }
63
64 void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
65 {
66 QTAILQ_REMOVE(&mouse_handlers, entry, node);
67 QTAILQ_INSERT_HEAD(&mouse_handlers, entry, node);
68
69 check_mode_change();
70 }
71
72 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
73 {
74 QTAILQ_REMOVE(&mouse_handlers, entry, node);
75
76 qemu_free(entry->qemu_put_mouse_event_name);
77 qemu_free(entry);
78 }
79
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
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
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
117 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
118 {
119 QEMUPutMouseEntry *entry;
120 QEMUPutMouseEvent *mouse_event;
121 void *mouse_event_opaque;
122 int width;
123
124 if (QTAILQ_EMPTY(&mouse_handlers)) {
125 return;
126 }
127
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;
132
133 if (mouse_event) {
134 if (graphic_rotate) {
135 if (entry->qemu_put_mouse_event_absolute)
136 width = 0x7fff;
137 else
138 width = graphic_width - 1;
139 mouse_event(mouse_event_opaque,
140 width - dy, dx, dz, buttons_state);
141 } else
142 mouse_event(mouse_event_opaque,
143 dx, dy, dz, buttons_state);
144 }
145 }
146
147 int kbd_mouse_is_absolute(void)
148 {
149 if (QTAILQ_EMPTY(&mouse_handlers)) {
150 return 0;
151 }
152
153 return QTAILQ_FIRST(&mouse_handlers)->qemu_put_mouse_event_absolute;
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;
201 int current;
202
203 mice_list = qlist_new();
204
205 if (QTAILQ_EMPTY(&mouse_handlers)) {
206 goto out;
207 }
208
209 current = QTAILQ_FIRST(&mouse_handlers)->index;
210
211 QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
212 QObject *obj;
213 obj = qobject_from_jsonf("{ 'name': %s, 'index': %d, 'current': %i }",
214 cursor->qemu_put_mouse_event_name,
215 cursor->index,
216 cursor->index == current);
217 qlist_append_obj(mice_list, obj);
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;
227 int index = qdict_get_int(qdict, "index");
228 int found = 0;
229
230 if (QTAILQ_EMPTY(&mouse_handlers)) {
231 monitor_printf(mon, "No mouse devices connected\n");
232 return;
233 }
234
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 }
241 }
242
243 if (!found) {
244 monitor_printf(mon, "Mouse at given index not found\n");
245 }
246 }