blob: e2f7c92a71f6e7c0f70ddb0e7c09b2fd64c2f5cf [file] [log] [blame]
Paolo Bonzini8f0056b2010-01-13 14:05:34 +01001/*
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
Paolo Bonzini8f0056b2010-01-13 14:05:34 +010031static QEMUPutKBDEvent *qemu_put_kbd_event;
32static void *qemu_put_kbd_event_opaque;
Gerd Hoffmann03a23a82010-02-26 17:17:36 +010033static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers = QTAILQ_HEAD_INITIALIZER(led_handlers);
Anthony Liguori6fef28e2010-03-09 20:52:22 -060034static QTAILQ_HEAD(, QEMUPutMouseEntry) mouse_handlers =
35 QTAILQ_HEAD_INITIALIZER(mouse_handlers);
Anthony Liguori7e581fb2010-03-09 13:25:00 -060036static NotifierList mouse_mode_notifiers =
37 NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers);
Paolo Bonzini8f0056b2010-01-13 14:05:34 +010038
39void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
40{
41 qemu_put_kbd_event_opaque = opaque;
42 qemu_put_kbd_event = func;
43}
44
Jes Sorensen46aaebf2010-06-08 15:12:18 +020045void qemu_remove_kbd_event_handler(void)
46{
47 qemu_put_kbd_event_opaque = NULL;
48 qemu_put_kbd_event = NULL;
49}
50
Anthony Liguori7e581fb2010-03-09 13:25:00 -060051static void check_mode_change(void)
52{
53 static int current_is_absolute, current_has_absolute;
54 int is_absolute;
55 int has_absolute;
56
57 is_absolute = kbd_mouse_is_absolute();
58 has_absolute = kbd_mouse_has_absolute();
59
60 if (is_absolute != current_is_absolute ||
61 has_absolute != current_has_absolute) {
Jan Kiszka9e8dd452011-06-20 14:06:26 +020062 notifier_list_notify(&mouse_mode_notifiers, NULL);
Anthony Liguori7e581fb2010-03-09 13:25:00 -060063 }
64
65 current_is_absolute = is_absolute;
66 current_has_absolute = has_absolute;
67}
68
Paolo Bonzini8f0056b2010-01-13 14:05:34 +010069QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
70 void *opaque, int absolute,
71 const char *name)
72{
Anthony Liguori6fef28e2010-03-09 20:52:22 -060073 QEMUPutMouseEntry *s;
74 static int mouse_index = 0;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +010075
Anthony Liguori7267c092011-08-20 22:09:37 -050076 s = g_malloc0(sizeof(QEMUPutMouseEntry));
Paolo Bonzini8f0056b2010-01-13 14:05:34 +010077
78 s->qemu_put_mouse_event = func;
79 s->qemu_put_mouse_event_opaque = opaque;
80 s->qemu_put_mouse_event_absolute = absolute;
Anthony Liguori7267c092011-08-20 22:09:37 -050081 s->qemu_put_mouse_event_name = g_strdup(name);
Anthony Liguori6fef28e2010-03-09 20:52:22 -060082 s->index = mouse_index++;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +010083
Anthony Liguori6fef28e2010-03-09 20:52:22 -060084 QTAILQ_INSERT_TAIL(&mouse_handlers, s, node);
Paolo Bonzini8f0056b2010-01-13 14:05:34 +010085
Anthony Liguori7e581fb2010-03-09 13:25:00 -060086 check_mode_change();
87
Paolo Bonzini8f0056b2010-01-13 14:05:34 +010088 return s;
89}
90
Anthony Liguori6fef28e2010-03-09 20:52:22 -060091void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
92{
93 QTAILQ_REMOVE(&mouse_handlers, entry, node);
94 QTAILQ_INSERT_HEAD(&mouse_handlers, entry, node);
95
96 check_mode_change();
97}
98
Paolo Bonzini8f0056b2010-01-13 14:05:34 +010099void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
100{
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600101 QTAILQ_REMOVE(&mouse_handlers, entry, node);
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100102
Anthony Liguori7267c092011-08-20 22:09:37 -0500103 g_free(entry->qemu_put_mouse_event_name);
104 g_free(entry);
Anthony Liguori7e581fb2010-03-09 13:25:00 -0600105
106 check_mode_change();
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100107}
108
Gerd Hoffmann03a23a82010-02-26 17:17:36 +0100109QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
110 void *opaque)
111{
112 QEMUPutLEDEntry *s;
113
Anthony Liguori7267c092011-08-20 22:09:37 -0500114 s = g_malloc0(sizeof(QEMUPutLEDEntry));
Gerd Hoffmann03a23a82010-02-26 17:17:36 +0100115
116 s->put_led = func;
117 s->opaque = opaque;
118 QTAILQ_INSERT_TAIL(&led_handlers, s, next);
119 return s;
120}
121
122void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry)
123{
124 if (entry == NULL)
125 return;
126 QTAILQ_REMOVE(&led_handlers, entry, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500127 g_free(entry);
Gerd Hoffmann03a23a82010-02-26 17:17:36 +0100128}
129
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100130void kbd_put_keycode(int keycode)
131{
132 if (qemu_put_kbd_event) {
133 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
134 }
135}
136
Gerd Hoffmann03a23a82010-02-26 17:17:36 +0100137void kbd_put_ledstate(int ledstate)
138{
139 QEMUPutLEDEntry *cursor;
140
141 QTAILQ_FOREACH(cursor, &led_handlers, next) {
142 cursor->put_led(cursor->opaque, ledstate);
143 }
144}
145
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100146void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
147{
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600148 QEMUPutMouseEntry *entry;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100149 QEMUPutMouseEvent *mouse_event;
150 void *mouse_event_opaque;
Vasily Khoruzhick93128052011-06-17 13:04:36 +0300151 int width, height;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100152
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600153 if (QTAILQ_EMPTY(&mouse_handlers)) {
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100154 return;
155 }
156
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600157 entry = QTAILQ_FIRST(&mouse_handlers);
158
159 mouse_event = entry->qemu_put_mouse_event;
160 mouse_event_opaque = entry->qemu_put_mouse_event_opaque;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100161
162 if (mouse_event) {
Vasily Khoruzhick93128052011-06-17 13:04:36 +0300163 if (entry->qemu_put_mouse_event_absolute) {
164 width = 0x7fff;
165 height = 0x7fff;
Brad Hards97697372011-04-09 12:11:36 +1000166 } else {
Vasily Khoruzhick93128052011-06-17 13:04:36 +0300167 width = graphic_width - 1;
168 height = graphic_height - 1;
169 }
170
171 switch (graphic_rotate) {
172 case 0:
173 mouse_event(mouse_event_opaque,
174 dx, dy, dz, buttons_state);
175 break;
176 case 90:
177 mouse_event(mouse_event_opaque,
178 width - dy, dx, dz, buttons_state);
179 break;
180 case 180:
181 mouse_event(mouse_event_opaque,
182 width - dx, height - dy, dz, buttons_state);
183 break;
184 case 270:
185 mouse_event(mouse_event_opaque,
186 dy, height - dx, dz, buttons_state);
187 break;
Brad Hards97697372011-04-09 12:11:36 +1000188 }
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100189 }
190}
191
192int kbd_mouse_is_absolute(void)
193{
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600194 if (QTAILQ_EMPTY(&mouse_handlers)) {
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100195 return 0;
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600196 }
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100197
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600198 return QTAILQ_FIRST(&mouse_handlers)->qemu_put_mouse_event_absolute;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100199}
200
Anthony Liguorieb2e2592010-03-09 14:26:40 -0600201int kbd_mouse_has_absolute(void)
202{
203 QEMUPutMouseEntry *entry;
204
205 QTAILQ_FOREACH(entry, &mouse_handlers, node) {
206 if (entry->qemu_put_mouse_event_absolute) {
207 return 1;
208 }
209 }
210
211 return 0;
212}
213
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100214static void info_mice_iter(QObject *data, void *opaque)
215{
216 QDict *mouse;
217 Monitor *mon = opaque;
218
219 mouse = qobject_to_qdict(data);
Anthony Liguori1aaee432010-03-09 20:58:07 -0600220 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100221 (qdict_get_bool(mouse, "current") ? '*' : ' '),
Anthony Liguori1aaee432010-03-09 20:58:07 -0600222 qdict_get_int(mouse, "index"), qdict_get_str(mouse, "name"),
223 qdict_get_bool(mouse, "absolute") ? " (absolute)" : "");
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100224}
225
226void do_info_mice_print(Monitor *mon, const QObject *data)
227{
228 QList *mice_list;
229
230 mice_list = qobject_to_qlist(data);
231 if (qlist_empty(mice_list)) {
232 monitor_printf(mon, "No mouse devices connected\n");
233 return;
234 }
235
236 qlist_iter(mice_list, info_mice_iter, mon);
237}
238
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100239void do_info_mice(Monitor *mon, QObject **ret_data)
240{
241 QEMUPutMouseEntry *cursor;
242 QList *mice_list;
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600243 int current;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100244
245 mice_list = qlist_new();
246
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600247 if (QTAILQ_EMPTY(&mouse_handlers)) {
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100248 goto out;
249 }
250
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600251 current = QTAILQ_FIRST(&mouse_handlers)->index;
252
253 QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100254 QObject *obj;
Anthony Liguori1aaee432010-03-09 20:58:07 -0600255 obj = qobject_from_jsonf("{ 'name': %s,"
256 " 'index': %d,"
257 " 'current': %i,"
258 " 'absolute': %i }",
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100259 cursor->qemu_put_mouse_event_name,
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600260 cursor->index,
Anthony Liguori1aaee432010-03-09 20:58:07 -0600261 cursor->index == current,
262 !!cursor->qemu_put_mouse_event_absolute);
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100263 qlist_append_obj(mice_list, obj);
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100264 }
265
266out:
267 *ret_data = QOBJECT(mice_list);
268}
269
270void do_mouse_set(Monitor *mon, const QDict *qdict)
271{
272 QEMUPutMouseEntry *cursor;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100273 int index = qdict_get_int(qdict, "index");
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600274 int found = 0;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100275
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600276 if (QTAILQ_EMPTY(&mouse_handlers)) {
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100277 monitor_printf(mon, "No mouse devices connected\n");
278 return;
279 }
280
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600281 QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
282 if (cursor->index == index) {
283 found = 1;
284 qemu_activate_mouse_event_handler(cursor);
285 break;
286 }
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100287 }
288
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600289 if (!found) {
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100290 monitor_printf(mon, "Mouse at given index not found\n");
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600291 }
Anthony Liguori7e581fb2010-03-09 13:25:00 -0600292
293 check_mode_change();
294}
295
296void qemu_add_mouse_mode_change_notifier(Notifier *notify)
297{
298 notifier_list_add(&mouse_mode_notifiers, notify);
299}
300
301void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
302{
303 notifier_list_remove(&mouse_mode_notifiers, notify);
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100304}