Add kbd_mouse_has_absolute()
[qemu/mdroth.git] / input.c
blob8d5a14d2da51f118b7770be1cbdd216d9e54fc9b
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "sysemu.h"
26 #include "net.h"
27 #include "monitor.h"
28 #include "console.h"
29 #include "qjson.h"
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);
38 void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
40 qemu_put_kbd_event_opaque = opaque;
41 qemu_put_kbd_event = func;
44 QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
45 void *opaque, int absolute,
46 const char *name)
48 QEMUPutMouseEntry *s;
49 static int mouse_index = 0;
51 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
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++;
59 QTAILQ_INSERT_TAIL(&mouse_handlers, s, node);
61 return s;
64 void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
66 QTAILQ_REMOVE(&mouse_handlers, entry, node);
67 QTAILQ_INSERT_HEAD(&mouse_handlers, entry, node);
69 check_mode_change();
72 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
74 QTAILQ_REMOVE(&mouse_handlers, entry, node);
76 qemu_free(entry->qemu_put_mouse_event_name);
77 qemu_free(entry);
80 QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
81 void *opaque)
83 QEMUPutLEDEntry *s;
85 s = qemu_mallocz(sizeof(QEMUPutLEDEntry));
87 s->put_led = func;
88 s->opaque = opaque;
89 QTAILQ_INSERT_TAIL(&led_handlers, s, next);
90 return s;
93 void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry)
95 if (entry == NULL)
96 return;
97 QTAILQ_REMOVE(&led_handlers, entry, next);
98 qemu_free(entry);
101 void kbd_put_keycode(int keycode)
103 if (qemu_put_kbd_event) {
104 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
108 void kbd_put_ledstate(int ledstate)
110 QEMUPutLEDEntry *cursor;
112 QTAILQ_FOREACH(cursor, &led_handlers, next) {
113 cursor->put_led(cursor->opaque, ledstate);
117 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
119 QEMUPutMouseEntry *entry;
120 QEMUPutMouseEvent *mouse_event;
121 void *mouse_event_opaque;
122 int width;
124 if (QTAILQ_EMPTY(&mouse_handlers)) {
125 return;
128 entry = QTAILQ_FIRST(&mouse_handlers);
130 mouse_event = entry->qemu_put_mouse_event;
131 mouse_event_opaque = entry->qemu_put_mouse_event_opaque;
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);
147 int kbd_mouse_is_absolute(void)
149 if (QTAILQ_EMPTY(&mouse_handlers)) {
150 return 0;
153 return QTAILQ_FIRST(&mouse_handlers)->qemu_put_mouse_event_absolute;
156 int kbd_mouse_has_absolute(void)
158 QEMUPutMouseEntry *entry;
160 QTAILQ_FOREACH(entry, &mouse_handlers, node) {
161 if (entry->qemu_put_mouse_event_absolute) {
162 return 1;
166 return 0;
169 static void info_mice_iter(QObject *data, void *opaque)
171 QDict *mouse;
172 Monitor *mon = opaque;
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"));
180 void do_info_mice_print(Monitor *mon, const QObject *data)
182 QList *mice_list;
184 mice_list = qobject_to_qlist(data);
185 if (qlist_empty(mice_list)) {
186 monitor_printf(mon, "No mouse devices connected\n");
187 return;
190 qlist_iter(mice_list, info_mice_iter, mon);
194 * do_info_mice(): Show VM mice information
196 * Each mouse is represented by a QDict, the returned QObject is a QList of
197 * all mice.
199 * The mouse QDict contains the following:
201 * - "name": mouse's name
202 * - "index": mouse's index
203 * - "current": true if this mouse is receiving events, false otherwise
205 * Example:
207 * [ { "name": "QEMU Microsoft Mouse", "index": 0, "current": false },
208 * { "name": "QEMU PS/2 Mouse", "index": 1, "current": true } ]
210 void do_info_mice(Monitor *mon, QObject **ret_data)
212 QEMUPutMouseEntry *cursor;
213 QList *mice_list;
214 int current;
216 mice_list = qlist_new();
218 if (QTAILQ_EMPTY(&mouse_handlers)) {
219 goto out;
222 current = QTAILQ_FIRST(&mouse_handlers)->index;
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);
233 out:
234 *ret_data = QOBJECT(mice_list);
237 void do_mouse_set(Monitor *mon, const QDict *qdict)
239 QEMUPutMouseEntry *cursor;
240 int index = qdict_get_int(qdict, "index");
241 int found = 0;
243 if (QTAILQ_EMPTY(&mouse_handlers)) {
244 monitor_printf(mon, "No mouse devices connected\n");
245 return;
248 QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
249 if (cursor->index == index) {
250 found = 1;
251 qemu_activate_mouse_event_handler(cursor);
252 break;
256 if (!found) {
257 monitor_printf(mon, "Mouse at given index not found\n");