Merge remote branch 'mst/pci' into staging
[qemu/aliguori-queue.git] / input.c
blobbaaa4c60c6b880d081e35a2636708da4f15920f7
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;
36 static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers = QTAILQ_HEAD_INITIALIZER(led_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, *cursor;
50 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
52 s->qemu_put_mouse_event = func;
53 s->qemu_put_mouse_event_opaque = opaque;
54 s->qemu_put_mouse_event_absolute = absolute;
55 s->qemu_put_mouse_event_name = qemu_strdup(name);
56 s->next = NULL;
58 if (!qemu_put_mouse_event_head) {
59 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
60 return s;
63 cursor = qemu_put_mouse_event_head;
64 while (cursor->next != NULL)
65 cursor = cursor->next;
67 cursor->next = s;
68 qemu_put_mouse_event_current = s;
70 return s;
73 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
75 QEMUPutMouseEntry *prev = NULL, *cursor;
77 if (!qemu_put_mouse_event_head || entry == NULL)
78 return;
80 cursor = qemu_put_mouse_event_head;
81 while (cursor != NULL && cursor != entry) {
82 prev = cursor;
83 cursor = cursor->next;
86 if (cursor == NULL) // does not exist or list empty
87 return;
88 else if (prev == NULL) { // entry is head
89 qemu_put_mouse_event_head = cursor->next;
90 if (qemu_put_mouse_event_current == entry)
91 qemu_put_mouse_event_current = cursor->next;
92 qemu_free(entry->qemu_put_mouse_event_name);
93 qemu_free(entry);
94 return;
97 prev->next = entry->next;
99 if (qemu_put_mouse_event_current == entry)
100 qemu_put_mouse_event_current = prev;
102 qemu_free(entry->qemu_put_mouse_event_name);
103 qemu_free(entry);
106 QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
107 void *opaque)
109 QEMUPutLEDEntry *s;
111 s = qemu_mallocz(sizeof(QEMUPutLEDEntry));
113 s->put_led = func;
114 s->opaque = opaque;
115 QTAILQ_INSERT_TAIL(&led_handlers, s, next);
116 return s;
119 void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry)
121 if (entry == NULL)
122 return;
123 QTAILQ_REMOVE(&led_handlers, entry, next);
124 qemu_free(entry);
127 void kbd_put_keycode(int keycode)
129 if (qemu_put_kbd_event) {
130 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
134 void kbd_put_ledstate(int ledstate)
136 QEMUPutLEDEntry *cursor;
138 QTAILQ_FOREACH(cursor, &led_handlers, next) {
139 cursor->put_led(cursor->opaque, ledstate);
143 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
145 QEMUPutMouseEvent *mouse_event;
146 void *mouse_event_opaque;
147 int width;
149 if (!qemu_put_mouse_event_current) {
150 return;
153 mouse_event =
154 qemu_put_mouse_event_current->qemu_put_mouse_event;
155 mouse_event_opaque =
156 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
158 if (mouse_event) {
159 if (graphic_rotate) {
160 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
161 width = 0x7fff;
162 else
163 width = graphic_width - 1;
164 mouse_event(mouse_event_opaque,
165 width - dy, dx, dz, buttons_state);
166 } else
167 mouse_event(mouse_event_opaque,
168 dx, dy, dz, buttons_state);
172 int kbd_mouse_is_absolute(void)
174 if (!qemu_put_mouse_event_current)
175 return 0;
177 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
180 static void info_mice_iter(QObject *data, void *opaque)
182 QDict *mouse;
183 Monitor *mon = opaque;
185 mouse = qobject_to_qdict(data);
186 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s\n",
187 (qdict_get_bool(mouse, "current") ? '*' : ' '),
188 qdict_get_int(mouse, "index"), qdict_get_str(mouse, "name"));
191 void do_info_mice_print(Monitor *mon, const QObject *data)
193 QList *mice_list;
195 mice_list = qobject_to_qlist(data);
196 if (qlist_empty(mice_list)) {
197 monitor_printf(mon, "No mouse devices connected\n");
198 return;
201 qlist_iter(mice_list, info_mice_iter, mon);
205 * do_info_mice(): Show VM mice information
207 * Each mouse is represented by a QDict, the returned QObject is a QList of
208 * all mice.
210 * The mouse QDict contains the following:
212 * - "name": mouse's name
213 * - "index": mouse's index
214 * - "current": true if this mouse is receiving events, false otherwise
216 * Example:
218 * [ { "name": "QEMU Microsoft Mouse", "index": 0, "current": false },
219 * { "name": "QEMU PS/2 Mouse", "index": 1, "current": true } ]
221 void do_info_mice(Monitor *mon, QObject **ret_data)
223 QEMUPutMouseEntry *cursor;
224 QList *mice_list;
225 int index = 0;
227 mice_list = qlist_new();
229 if (!qemu_put_mouse_event_head) {
230 goto out;
233 cursor = qemu_put_mouse_event_head;
234 while (cursor != NULL) {
235 QObject *obj;
236 obj = qobject_from_jsonf("{ 'name': %s, 'index': %d, 'current': %i }",
237 cursor->qemu_put_mouse_event_name,
238 index, cursor == qemu_put_mouse_event_current);
239 qlist_append_obj(mice_list, obj);
240 index++;
241 cursor = cursor->next;
244 out:
245 *ret_data = QOBJECT(mice_list);
248 void do_mouse_set(Monitor *mon, const QDict *qdict)
250 QEMUPutMouseEntry *cursor;
251 int i = 0;
252 int index = qdict_get_int(qdict, "index");
254 if (!qemu_put_mouse_event_head) {
255 monitor_printf(mon, "No mouse devices connected\n");
256 return;
259 cursor = qemu_put_mouse_event_head;
260 while (cursor != NULL && index != i) {
261 i++;
262 cursor = cursor->next;
265 if (cursor != NULL)
266 qemu_put_mouse_event_current = cursor;
267 else
268 monitor_printf(mon, "Mouse at given index not found\n");