blob: 6968b31781d0e2db3f78c454a85b16a5f6724496 [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"
Luiz Capitulinoe235cec2011-09-21 15:29:55 -030029#include "error.h"
30#include "qmp-commands.h"
Paolo Bonzini8f0056b2010-01-13 14:05:34 +010031
Paolo Bonzini8f0056b2010-01-13 14:05:34 +010032static QEMUPutKBDEvent *qemu_put_kbd_event;
33static void *qemu_put_kbd_event_opaque;
Gerd Hoffmann03a23a82010-02-26 17:17:36 +010034static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers = QTAILQ_HEAD_INITIALIZER(led_handlers);
Anthony Liguori6fef28e2010-03-09 20:52:22 -060035static QTAILQ_HEAD(, QEMUPutMouseEntry) mouse_handlers =
36 QTAILQ_HEAD_INITIALIZER(mouse_handlers);
Anthony Liguori7e581fb2010-03-09 13:25:00 -060037static NotifierList mouse_mode_notifiers =
38 NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers);
Paolo Bonzini8f0056b2010-01-13 14:05:34 +010039
40void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
41{
42 qemu_put_kbd_event_opaque = opaque;
43 qemu_put_kbd_event = func;
44}
45
Jes Sorensen46aaebf2010-06-08 15:12:18 +020046void qemu_remove_kbd_event_handler(void)
47{
48 qemu_put_kbd_event_opaque = NULL;
49 qemu_put_kbd_event = NULL;
50}
51
Anthony Liguori7e581fb2010-03-09 13:25:00 -060052static void check_mode_change(void)
53{
54 static int current_is_absolute, current_has_absolute;
55 int is_absolute;
56 int has_absolute;
57
58 is_absolute = kbd_mouse_is_absolute();
59 has_absolute = kbd_mouse_has_absolute();
60
61 if (is_absolute != current_is_absolute ||
62 has_absolute != current_has_absolute) {
Jan Kiszka9e8dd452011-06-20 14:06:26 +020063 notifier_list_notify(&mouse_mode_notifiers, NULL);
Anthony Liguori7e581fb2010-03-09 13:25:00 -060064 }
65
66 current_is_absolute = is_absolute;
67 current_has_absolute = has_absolute;
68}
69
Paolo Bonzini8f0056b2010-01-13 14:05:34 +010070QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
71 void *opaque, int absolute,
72 const char *name)
73{
Anthony Liguori6fef28e2010-03-09 20:52:22 -060074 QEMUPutMouseEntry *s;
75 static int mouse_index = 0;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +010076
Anthony Liguori7267c092011-08-20 22:09:37 -050077 s = g_malloc0(sizeof(QEMUPutMouseEntry));
Paolo Bonzini8f0056b2010-01-13 14:05:34 +010078
79 s->qemu_put_mouse_event = func;
80 s->qemu_put_mouse_event_opaque = opaque;
81 s->qemu_put_mouse_event_absolute = absolute;
Anthony Liguori7267c092011-08-20 22:09:37 -050082 s->qemu_put_mouse_event_name = g_strdup(name);
Anthony Liguori6fef28e2010-03-09 20:52:22 -060083 s->index = mouse_index++;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +010084
Anthony Liguori6fef28e2010-03-09 20:52:22 -060085 QTAILQ_INSERT_TAIL(&mouse_handlers, s, node);
Paolo Bonzini8f0056b2010-01-13 14:05:34 +010086
Anthony Liguori7e581fb2010-03-09 13:25:00 -060087 check_mode_change();
88
Paolo Bonzini8f0056b2010-01-13 14:05:34 +010089 return s;
90}
91
Anthony Liguori6fef28e2010-03-09 20:52:22 -060092void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
93{
94 QTAILQ_REMOVE(&mouse_handlers, entry, node);
95 QTAILQ_INSERT_HEAD(&mouse_handlers, entry, node);
96
97 check_mode_change();
98}
99
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100100void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
101{
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600102 QTAILQ_REMOVE(&mouse_handlers, entry, node);
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100103
Anthony Liguori7267c092011-08-20 22:09:37 -0500104 g_free(entry->qemu_put_mouse_event_name);
105 g_free(entry);
Anthony Liguori7e581fb2010-03-09 13:25:00 -0600106
107 check_mode_change();
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100108}
109
Gerd Hoffmann03a23a82010-02-26 17:17:36 +0100110QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
111 void *opaque)
112{
113 QEMUPutLEDEntry *s;
114
Anthony Liguori7267c092011-08-20 22:09:37 -0500115 s = g_malloc0(sizeof(QEMUPutLEDEntry));
Gerd Hoffmann03a23a82010-02-26 17:17:36 +0100116
117 s->put_led = func;
118 s->opaque = opaque;
119 QTAILQ_INSERT_TAIL(&led_handlers, s, next);
120 return s;
121}
122
123void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry)
124{
125 if (entry == NULL)
126 return;
127 QTAILQ_REMOVE(&led_handlers, entry, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500128 g_free(entry);
Gerd Hoffmann03a23a82010-02-26 17:17:36 +0100129}
130
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100131void kbd_put_keycode(int keycode)
132{
Luiz Capitulinoad02b962012-04-27 13:33:36 -0300133 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
Gerd Hoffmann99c7f872012-02-15 09:15:37 +0100134 return;
135 }
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100136 if (qemu_put_kbd_event) {
137 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
138 }
139}
140
Gerd Hoffmann03a23a82010-02-26 17:17:36 +0100141void kbd_put_ledstate(int ledstate)
142{
143 QEMUPutLEDEntry *cursor;
144
145 QTAILQ_FOREACH(cursor, &led_handlers, next) {
146 cursor->put_led(cursor->opaque, ledstate);
147 }
148}
149
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100150void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
151{
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600152 QEMUPutMouseEntry *entry;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100153 QEMUPutMouseEvent *mouse_event;
154 void *mouse_event_opaque;
Vasily Khoruzhick93128052011-06-17 13:04:36 +0300155 int width, height;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100156
Luiz Capitulinoad02b962012-04-27 13:33:36 -0300157 if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
Gerd Hoffmann99c7f872012-02-15 09:15:37 +0100158 return;
159 }
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600160 if (QTAILQ_EMPTY(&mouse_handlers)) {
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100161 return;
162 }
163
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600164 entry = QTAILQ_FIRST(&mouse_handlers);
165
166 mouse_event = entry->qemu_put_mouse_event;
167 mouse_event_opaque = entry->qemu_put_mouse_event_opaque;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100168
169 if (mouse_event) {
Vasily Khoruzhick93128052011-06-17 13:04:36 +0300170 if (entry->qemu_put_mouse_event_absolute) {
171 width = 0x7fff;
172 height = 0x7fff;
Brad Hards97697372011-04-09 12:11:36 +1000173 } else {
Vasily Khoruzhick93128052011-06-17 13:04:36 +0300174 width = graphic_width - 1;
175 height = graphic_height - 1;
176 }
177
178 switch (graphic_rotate) {
179 case 0:
180 mouse_event(mouse_event_opaque,
181 dx, dy, dz, buttons_state);
182 break;
183 case 90:
184 mouse_event(mouse_event_opaque,
185 width - dy, dx, dz, buttons_state);
186 break;
187 case 180:
188 mouse_event(mouse_event_opaque,
189 width - dx, height - dy, dz, buttons_state);
190 break;
191 case 270:
192 mouse_event(mouse_event_opaque,
193 dy, height - dx, dz, buttons_state);
194 break;
Brad Hards97697372011-04-09 12:11:36 +1000195 }
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100196 }
197}
198
199int kbd_mouse_is_absolute(void)
200{
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600201 if (QTAILQ_EMPTY(&mouse_handlers)) {
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100202 return 0;
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600203 }
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100204
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600205 return QTAILQ_FIRST(&mouse_handlers)->qemu_put_mouse_event_absolute;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100206}
207
Anthony Liguorieb2e2592010-03-09 14:26:40 -0600208int kbd_mouse_has_absolute(void)
209{
210 QEMUPutMouseEntry *entry;
211
212 QTAILQ_FOREACH(entry, &mouse_handlers, node) {
213 if (entry->qemu_put_mouse_event_absolute) {
214 return 1;
215 }
216 }
217
218 return 0;
219}
220
Luiz Capitulinoe235cec2011-09-21 15:29:55 -0300221MouseInfoList *qmp_query_mice(Error **errp)
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100222{
Luiz Capitulinoe235cec2011-09-21 15:29:55 -0300223 MouseInfoList *mice_list = NULL;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100224 QEMUPutMouseEntry *cursor;
Luiz Capitulinoe235cec2011-09-21 15:29:55 -0300225 bool current = true;
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600226
227 QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
Luiz Capitulinoe235cec2011-09-21 15:29:55 -0300228 MouseInfoList *info = g_malloc0(sizeof(*info));
229 info->value = g_malloc0(sizeof(*info->value));
230 info->value->name = g_strdup(cursor->qemu_put_mouse_event_name);
231 info->value->index = cursor->index;
232 info->value->absolute = !!cursor->qemu_put_mouse_event_absolute;
233 info->value->current = current;
234
235 current = false;
236
237 info->next = mice_list;
238 mice_list = info;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100239 }
240
Luiz Capitulinoe235cec2011-09-21 15:29:55 -0300241 return mice_list;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100242}
243
244void do_mouse_set(Monitor *mon, const QDict *qdict)
245{
246 QEMUPutMouseEntry *cursor;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100247 int index = qdict_get_int(qdict, "index");
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600248 int found = 0;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100249
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600250 if (QTAILQ_EMPTY(&mouse_handlers)) {
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100251 monitor_printf(mon, "No mouse devices connected\n");
252 return;
253 }
254
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600255 QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
256 if (cursor->index == index) {
257 found = 1;
258 qemu_activate_mouse_event_handler(cursor);
259 break;
260 }
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100261 }
262
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600263 if (!found) {
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100264 monitor_printf(mon, "Mouse at given index not found\n");
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600265 }
Anthony Liguori7e581fb2010-03-09 13:25:00 -0600266
267 check_mode_change();
268}
269
270void qemu_add_mouse_mode_change_notifier(Notifier *notify)
271{
272 notifier_list_add(&mouse_mode_notifiers, notify);
273}
274
275void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
276{
Paolo Bonzini31552522012-01-13 17:34:01 +0100277 notifier_remove(notify);
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100278}