]> git.proxmox.com Git - qemu.git/blob - input.c
Add kbd_mouse_has_absolute()
[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 int kbd_mouse_has_absolute(void)
157 {
158 QEMUPutMouseEntry *entry;
159
160 QTAILQ_FOREACH(entry, &mouse_handlers, node) {
161 if (entry->qemu_put_mouse_event_absolute) {
162 return 1;
163 }
164 }
165
166 return 0;
167 }
168
169 static void info_mice_iter(QObject *data, void *opaque)
170 {
171 QDict *mouse;
172 Monitor *mon = opaque;
173
174 mouse = qobject_to_qdict(data);
175 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s\n",
176 (qdict_get_bool(mouse, "current") ? '*' : ' '),
177 qdict_get_int(mouse, "index"), qdict_get_str(mouse, "name"));
178 }
179
180 void do_info_mice_print(Monitor *mon, const QObject *data)
181 {
182 QList *mice_list;
183
184 mice_list = qobject_to_qlist(data);
185 if (qlist_empty(mice_list)) {
186 monitor_printf(mon, "No mouse devices connected\n");
187 return;
188 }
189
190 qlist_iter(mice_list, info_mice_iter, mon);
191 }
192
193 /**
194 * do_info_mice(): Show VM mice information
195 *
196 * Each mouse is represented by a QDict, the returned QObject is a QList of
197 * all mice.
198 *
199 * The mouse QDict contains the following:
200 *
201 * - "name": mouse's name
202 * - "index": mouse's index
203 * - "current": true if this mouse is receiving events, false otherwise
204 *
205 * Example:
206 *
207 * [ { "name": "QEMU Microsoft Mouse", "index": 0, "current": false },
208 * { "name": "QEMU PS/2 Mouse", "index": 1, "current": true } ]
209 */
210 void do_info_mice(Monitor *mon, QObject **ret_data)
211 {
212 QEMUPutMouseEntry *cursor;
213 QList *mice_list;
214 int current;
215
216 mice_list = qlist_new();
217
218 if (QTAILQ_EMPTY(&mouse_handlers)) {
219 goto out;
220 }
221
222 current = QTAILQ_FIRST(&mouse_handlers)->index;
223
224 QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
225 QObject *obj;
226 obj = qobject_from_jsonf("{ 'name': %s, 'index': %d, 'current': %i }",
227 cursor->qemu_put_mouse_event_name,
228 cursor->index,
229 cursor->index == current);
230 qlist_append_obj(mice_list, obj);
231 }
232
233 out:
234 *ret_data = QOBJECT(mice_list);
235 }
236
237 void do_mouse_set(Monitor *mon, const QDict *qdict)
238 {
239 QEMUPutMouseEntry *cursor;
240 int index = qdict_get_int(qdict, "index");
241 int found = 0;
242
243 if (QTAILQ_EMPTY(&mouse_handlers)) {
244 monitor_printf(mon, "No mouse devices connected\n");
245 return;
246 }
247
248 QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
249 if (cursor->index == index) {
250 found = 1;
251 qemu_activate_mouse_event_handler(cursor);
252 break;
253 }
254 }
255
256 if (!found) {
257 monitor_printf(mon, "Mouse at given index not found\n");
258 }
259 }