target-sparc: fix --enable-debug build for 64 bit host
[qemu/aliguori-queue.git] / input.c
blob955b9ab04dcf3d980c649ffaebf581a06b7fc07a
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 QEMUPutMouseEntry *qemu_put_mouse_event_head;
35 static QEMUPutMouseEntry *qemu_put_mouse_event_current;
37 void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
39 qemu_put_kbd_event_opaque = opaque;
40 qemu_put_kbd_event = func;
43 QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
44 void *opaque, int absolute,
45 const char *name)
47 QEMUPutMouseEntry *s, *cursor;
49 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
51 s->qemu_put_mouse_event = func;
52 s->qemu_put_mouse_event_opaque = opaque;
53 s->qemu_put_mouse_event_absolute = absolute;
54 s->qemu_put_mouse_event_name = qemu_strdup(name);
55 s->next = NULL;
57 if (!qemu_put_mouse_event_head) {
58 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
59 return s;
62 cursor = qemu_put_mouse_event_head;
63 while (cursor->next != NULL)
64 cursor = cursor->next;
66 cursor->next = s;
67 qemu_put_mouse_event_current = s;
69 return s;
72 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
74 QEMUPutMouseEntry *prev = NULL, *cursor;
76 if (!qemu_put_mouse_event_head || entry == NULL)
77 return;
79 cursor = qemu_put_mouse_event_head;
80 while (cursor != NULL && cursor != entry) {
81 prev = cursor;
82 cursor = cursor->next;
85 if (cursor == NULL) // does not exist or list empty
86 return;
87 else if (prev == NULL) { // entry is head
88 qemu_put_mouse_event_head = cursor->next;
89 if (qemu_put_mouse_event_current == entry)
90 qemu_put_mouse_event_current = cursor->next;
91 qemu_free(entry->qemu_put_mouse_event_name);
92 qemu_free(entry);
93 return;
96 prev->next = entry->next;
98 if (qemu_put_mouse_event_current == entry)
99 qemu_put_mouse_event_current = prev;
101 qemu_free(entry->qemu_put_mouse_event_name);
102 qemu_free(entry);
105 void kbd_put_keycode(int keycode)
107 if (qemu_put_kbd_event) {
108 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
112 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
114 QEMUPutMouseEvent *mouse_event;
115 void *mouse_event_opaque;
116 int width;
118 if (!qemu_put_mouse_event_current) {
119 return;
122 mouse_event =
123 qemu_put_mouse_event_current->qemu_put_mouse_event;
124 mouse_event_opaque =
125 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
127 if (mouse_event) {
128 if (graphic_rotate) {
129 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
130 width = 0x7fff;
131 else
132 width = graphic_width - 1;
133 mouse_event(mouse_event_opaque,
134 width - dy, dx, dz, buttons_state);
135 } else
136 mouse_event(mouse_event_opaque,
137 dx, dy, dz, buttons_state);
141 int kbd_mouse_is_absolute(void)
143 if (!qemu_put_mouse_event_current)
144 return 0;
146 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
149 static void info_mice_iter(QObject *data, void *opaque)
151 QDict *mouse;
152 Monitor *mon = opaque;
154 mouse = qobject_to_qdict(data);
155 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s\n",
156 (qdict_get_bool(mouse, "current") ? '*' : ' '),
157 qdict_get_int(mouse, "index"), qdict_get_str(mouse, "name"));
160 void do_info_mice_print(Monitor *mon, const QObject *data)
162 QList *mice_list;
164 mice_list = qobject_to_qlist(data);
165 if (qlist_empty(mice_list)) {
166 monitor_printf(mon, "No mouse devices connected\n");
167 return;
170 qlist_iter(mice_list, info_mice_iter, mon);
174 * do_info_mice(): Show VM mice information
176 * Each mouse is represented by a QDict, the returned QObject is a QList of
177 * all mice.
179 * The mouse QDict contains the following:
181 * - "name": mouse's name
182 * - "index": mouse's index
183 * - "current": true if this mouse is receiving events, false otherwise
185 * Example:
187 * [ { "name": "QEMU Microsoft Mouse", "index": 0, "current": false },
188 * { "name": "QEMU PS/2 Mouse", "index": 1, "current": true } ]
190 void do_info_mice(Monitor *mon, QObject **ret_data)
192 QEMUPutMouseEntry *cursor;
193 QList *mice_list;
194 int index = 0;
196 mice_list = qlist_new();
198 if (!qemu_put_mouse_event_head) {
199 goto out;
202 cursor = qemu_put_mouse_event_head;
203 while (cursor != NULL) {
204 QObject *obj;
205 obj = qobject_from_jsonf("{ 'name': %s, 'index': %d, 'current': %i }",
206 cursor->qemu_put_mouse_event_name,
207 index, cursor == qemu_put_mouse_event_current);
208 qlist_append_obj(mice_list, obj);
209 index++;
210 cursor = cursor->next;
213 out:
214 *ret_data = QOBJECT(mice_list);
217 void do_mouse_set(Monitor *mon, const QDict *qdict)
219 QEMUPutMouseEntry *cursor;
220 int i = 0;
221 int index = qdict_get_int(qdict, "index");
223 if (!qemu_put_mouse_event_head) {
224 monitor_printf(mon, "No mouse devices connected\n");
225 return;
228 cursor = qemu_put_mouse_event_head;
229 while (cursor != NULL && index != i) {
230 i++;
231 cursor = cursor->next;
234 if (cursor != NULL)
235 qemu_put_mouse_event_current = cursor;
236 else
237 monitor_printf(mon, "Mouse at given index not found\n");