Merge commit '666daa68234b5b1758652633cab07d5ca6046a5b' into upstream-merge
[qemu-kvm/amd-iommu.git] / input.c
blob651442d793a8228d78968ca1ab5928f819cc0db6
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"
31 static QEMUPutKBDEvent *qemu_put_kbd_event;
32 static void *qemu_put_kbd_event_opaque;
33 static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers = QTAILQ_HEAD_INITIALIZER(led_handlers);
34 static QTAILQ_HEAD(, QEMUPutMouseEntry) mouse_handlers =
35 QTAILQ_HEAD_INITIALIZER(mouse_handlers);
36 static NotifierList mouse_mode_notifiers =
37 NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers);
39 void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
41 qemu_put_kbd_event_opaque = opaque;
42 qemu_put_kbd_event = func;
45 static void check_mode_change(void)
47 static int current_is_absolute, current_has_absolute;
48 int is_absolute;
49 int has_absolute;
51 is_absolute = kbd_mouse_is_absolute();
52 has_absolute = kbd_mouse_has_absolute();
54 if (is_absolute != current_is_absolute ||
55 has_absolute != current_has_absolute) {
56 notifier_list_notify(&mouse_mode_notifiers);
59 current_is_absolute = is_absolute;
60 current_has_absolute = has_absolute;
63 QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
64 void *opaque, int absolute,
65 const char *name)
67 QEMUPutMouseEntry *s;
68 static int mouse_index = 0;
70 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
72 s->qemu_put_mouse_event = func;
73 s->qemu_put_mouse_event_opaque = opaque;
74 s->qemu_put_mouse_event_absolute = absolute;
75 s->qemu_put_mouse_event_name = qemu_strdup(name);
76 s->index = mouse_index++;
78 QTAILQ_INSERT_TAIL(&mouse_handlers, s, node);
80 check_mode_change();
82 return s;
85 void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
87 QTAILQ_REMOVE(&mouse_handlers, entry, node);
88 QTAILQ_INSERT_HEAD(&mouse_handlers, entry, node);
90 check_mode_change();
93 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
95 QTAILQ_REMOVE(&mouse_handlers, entry, node);
97 qemu_free(entry->qemu_put_mouse_event_name);
98 qemu_free(entry);
100 check_mode_change();
103 QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
104 void *opaque)
106 QEMUPutLEDEntry *s;
108 s = qemu_mallocz(sizeof(QEMUPutLEDEntry));
110 s->put_led = func;
111 s->opaque = opaque;
112 QTAILQ_INSERT_TAIL(&led_handlers, s, next);
113 return s;
116 void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry)
118 if (entry == NULL)
119 return;
120 QTAILQ_REMOVE(&led_handlers, entry, next);
121 qemu_free(entry);
124 void kbd_put_keycode(int keycode)
126 if (qemu_put_kbd_event) {
127 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
131 void kbd_put_ledstate(int ledstate)
133 QEMUPutLEDEntry *cursor;
135 QTAILQ_FOREACH(cursor, &led_handlers, next) {
136 cursor->put_led(cursor->opaque, ledstate);
140 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
142 QEMUPutMouseEntry *entry;
143 QEMUPutMouseEvent *mouse_event;
144 void *mouse_event_opaque;
145 int width;
147 if (QTAILQ_EMPTY(&mouse_handlers)) {
148 return;
151 entry = QTAILQ_FIRST(&mouse_handlers);
153 mouse_event = entry->qemu_put_mouse_event;
154 mouse_event_opaque = entry->qemu_put_mouse_event_opaque;
156 if (mouse_event) {
157 if (graphic_rotate) {
158 if (entry->qemu_put_mouse_event_absolute)
159 width = 0x7fff;
160 else
161 width = graphic_width - 1;
162 mouse_event(mouse_event_opaque,
163 width - dy, dx, dz, buttons_state);
164 } else
165 mouse_event(mouse_event_opaque,
166 dx, dy, dz, buttons_state);
170 int kbd_mouse_is_absolute(void)
172 if (QTAILQ_EMPTY(&mouse_handlers)) {
173 return 0;
176 return QTAILQ_FIRST(&mouse_handlers)->qemu_put_mouse_event_absolute;
179 int kbd_mouse_has_absolute(void)
181 QEMUPutMouseEntry *entry;
183 QTAILQ_FOREACH(entry, &mouse_handlers, node) {
184 if (entry->qemu_put_mouse_event_absolute) {
185 return 1;
189 return 0;
192 static void info_mice_iter(QObject *data, void *opaque)
194 QDict *mouse;
195 Monitor *mon = opaque;
197 mouse = qobject_to_qdict(data);
198 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
199 (qdict_get_bool(mouse, "current") ? '*' : ' '),
200 qdict_get_int(mouse, "index"), qdict_get_str(mouse, "name"),
201 qdict_get_bool(mouse, "absolute") ? " (absolute)" : "");
204 void do_info_mice_print(Monitor *mon, const QObject *data)
206 QList *mice_list;
208 mice_list = qobject_to_qlist(data);
209 if (qlist_empty(mice_list)) {
210 monitor_printf(mon, "No mouse devices connected\n");
211 return;
214 qlist_iter(mice_list, info_mice_iter, mon);
217 void do_info_mice(Monitor *mon, QObject **ret_data)
219 QEMUPutMouseEntry *cursor;
220 QList *mice_list;
221 int current;
223 mice_list = qlist_new();
225 if (QTAILQ_EMPTY(&mouse_handlers)) {
226 goto out;
229 current = QTAILQ_FIRST(&mouse_handlers)->index;
231 QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
232 QObject *obj;
233 obj = qobject_from_jsonf("{ 'name': %s,"
234 " 'index': %d,"
235 " 'current': %i,"
236 " 'absolute': %i }",
237 cursor->qemu_put_mouse_event_name,
238 cursor->index,
239 cursor->index == current,
240 !!cursor->qemu_put_mouse_event_absolute);
241 qlist_append_obj(mice_list, obj);
244 out:
245 *ret_data = QOBJECT(mice_list);
248 void do_mouse_set(Monitor *mon, const QDict *qdict)
250 QEMUPutMouseEntry *cursor;
251 int index = qdict_get_int(qdict, "index");
252 int found = 0;
254 if (QTAILQ_EMPTY(&mouse_handlers)) {
255 monitor_printf(mon, "No mouse devices connected\n");
256 return;
259 QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
260 if (cursor->index == index) {
261 found = 1;
262 qemu_activate_mouse_event_handler(cursor);
263 break;
267 if (!found) {
268 monitor_printf(mon, "Mouse at given index not found\n");
271 check_mode_change();
274 void qemu_add_mouse_mode_change_notifier(Notifier *notify)
276 notifier_list_add(&mouse_mode_notifiers, notify);
279 void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
281 notifier_list_remove(&mouse_mode_notifiers, notify);