blob: 8a281d655ce79718b4f5f4630e4541478d09ba33 [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
Juha Riihimäkic1042302012-02-17 13:41:17 +000032static QTAILQ_HEAD(, QEMUPutKBDEntry) kbd_handlers =
33 QTAILQ_HEAD_INITIALIZER(kbd_handlers);
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{
Juha Riihimäkic1042302012-02-17 13:41:17 +000042 QEMUPutKBDEntry *s;
43
44 if (func != NULL) {
45 s = g_malloc0(sizeof(QEMUPutKBDEntry));
46
47 s->put_kbd_event = func;
48 s->opaque = opaque;
49
50 QTAILQ_INSERT_TAIL(&kbd_handlers, s, next);
51 }
Paolo Bonzini8f0056b2010-01-13 14:05:34 +010052}
53
Juha Riihimäkic1042302012-02-17 13:41:17 +000054void qemu_remove_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
Jes Sorensen46aaebf2010-06-08 15:12:18 +020055{
Juha Riihimäkic1042302012-02-17 13:41:17 +000056 QEMUPutKBDEntry *cursor, *cursor_next;
57 if (func != NULL) {
58 QTAILQ_FOREACH_SAFE(cursor, &kbd_handlers, next, cursor_next) {
59 if (cursor->put_kbd_event == func && cursor->opaque == opaque) {
60 QTAILQ_REMOVE(&kbd_handlers, cursor, next);
61 }
62 }
63 }
Jes Sorensen46aaebf2010-06-08 15:12:18 +020064}
65
Anthony Liguori7e581fb2010-03-09 13:25:00 -060066static void check_mode_change(void)
67{
68 static int current_is_absolute, current_has_absolute;
69 int is_absolute;
70 int has_absolute;
71
72 is_absolute = kbd_mouse_is_absolute();
73 has_absolute = kbd_mouse_has_absolute();
74
75 if (is_absolute != current_is_absolute ||
76 has_absolute != current_has_absolute) {
Jan Kiszka9e8dd452011-06-20 14:06:26 +020077 notifier_list_notify(&mouse_mode_notifiers, NULL);
Anthony Liguori7e581fb2010-03-09 13:25:00 -060078 }
79
80 current_is_absolute = is_absolute;
81 current_has_absolute = has_absolute;
82}
83
Paolo Bonzini8f0056b2010-01-13 14:05:34 +010084QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
85 void *opaque, int absolute,
86 const char *name)
87{
Anthony Liguori6fef28e2010-03-09 20:52:22 -060088 QEMUPutMouseEntry *s;
89 static int mouse_index = 0;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +010090
Anthony Liguori7267c092011-08-20 22:09:37 -050091 s = g_malloc0(sizeof(QEMUPutMouseEntry));
Paolo Bonzini8f0056b2010-01-13 14:05:34 +010092
93 s->qemu_put_mouse_event = func;
94 s->qemu_put_mouse_event_opaque = opaque;
95 s->qemu_put_mouse_event_absolute = absolute;
Anthony Liguori7267c092011-08-20 22:09:37 -050096 s->qemu_put_mouse_event_name = g_strdup(name);
Anthony Liguori6fef28e2010-03-09 20:52:22 -060097 s->index = mouse_index++;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +010098
Anthony Liguori6fef28e2010-03-09 20:52:22 -060099 QTAILQ_INSERT_TAIL(&mouse_handlers, s, node);
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100100
Anthony Liguori7e581fb2010-03-09 13:25:00 -0600101 check_mode_change();
102
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100103 return s;
104}
105
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600106void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
107{
108 QTAILQ_REMOVE(&mouse_handlers, entry, node);
109 QTAILQ_INSERT_HEAD(&mouse_handlers, entry, node);
110
111 check_mode_change();
112}
113
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100114void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
115{
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600116 QTAILQ_REMOVE(&mouse_handlers, entry, node);
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100117
Anthony Liguori7267c092011-08-20 22:09:37 -0500118 g_free(entry->qemu_put_mouse_event_name);
119 g_free(entry);
Anthony Liguori7e581fb2010-03-09 13:25:00 -0600120
121 check_mode_change();
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100122}
123
Gerd Hoffmann03a23a82010-02-26 17:17:36 +0100124QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
125 void *opaque)
126{
127 QEMUPutLEDEntry *s;
128
Anthony Liguori7267c092011-08-20 22:09:37 -0500129 s = g_malloc0(sizeof(QEMUPutLEDEntry));
Gerd Hoffmann03a23a82010-02-26 17:17:36 +0100130
131 s->put_led = func;
132 s->opaque = opaque;
133 QTAILQ_INSERT_TAIL(&led_handlers, s, next);
134 return s;
135}
136
137void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry)
138{
139 if (entry == NULL)
140 return;
141 QTAILQ_REMOVE(&led_handlers, entry, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500142 g_free(entry);
Gerd Hoffmann03a23a82010-02-26 17:17:36 +0100143}
144
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100145void kbd_put_keycode(int keycode)
146{
Juha Riihimäkic1042302012-02-17 13:41:17 +0000147 QEMUPutKBDEntry *cursor;
Gerd Hoffmann99c7f872012-02-15 09:15:37 +0100148 if (!runstate_is_running()) {
149 return;
150 }
Juha Riihimäkic1042302012-02-17 13:41:17 +0000151 QTAILQ_FOREACH(cursor, &kbd_handlers, next) {
152 cursor->put_kbd_event(cursor->opaque, keycode);
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100153 }
154}
155
Gerd Hoffmann03a23a82010-02-26 17:17:36 +0100156void kbd_put_ledstate(int ledstate)
157{
158 QEMUPutLEDEntry *cursor;
159
160 QTAILQ_FOREACH(cursor, &led_handlers, next) {
161 cursor->put_led(cursor->opaque, ledstate);
162 }
163}
164
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100165void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
166{
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600167 QEMUPutMouseEntry *entry;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100168 QEMUPutMouseEvent *mouse_event;
169 void *mouse_event_opaque;
Vasily Khoruzhick93128052011-06-17 13:04:36 +0300170 int width, height;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100171
Gerd Hoffmann99c7f872012-02-15 09:15:37 +0100172 if (!runstate_is_running()) {
173 return;
174 }
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600175 if (QTAILQ_EMPTY(&mouse_handlers)) {
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100176 return;
177 }
178
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600179 entry = QTAILQ_FIRST(&mouse_handlers);
180
181 mouse_event = entry->qemu_put_mouse_event;
182 mouse_event_opaque = entry->qemu_put_mouse_event_opaque;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100183
184 if (mouse_event) {
Vasily Khoruzhick93128052011-06-17 13:04:36 +0300185 if (entry->qemu_put_mouse_event_absolute) {
186 width = 0x7fff;
187 height = 0x7fff;
Brad Hards97697372011-04-09 12:11:36 +1000188 } else {
Vasily Khoruzhick93128052011-06-17 13:04:36 +0300189 width = graphic_width - 1;
190 height = graphic_height - 1;
191 }
192
193 switch (graphic_rotate) {
194 case 0:
195 mouse_event(mouse_event_opaque,
196 dx, dy, dz, buttons_state);
197 break;
198 case 90:
199 mouse_event(mouse_event_opaque,
200 width - dy, dx, dz, buttons_state);
201 break;
202 case 180:
203 mouse_event(mouse_event_opaque,
204 width - dx, height - dy, dz, buttons_state);
205 break;
206 case 270:
207 mouse_event(mouse_event_opaque,
208 dy, height - dx, dz, buttons_state);
209 break;
Brad Hards97697372011-04-09 12:11:36 +1000210 }
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100211 }
212}
213
214int kbd_mouse_is_absolute(void)
215{
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600216 if (QTAILQ_EMPTY(&mouse_handlers)) {
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100217 return 0;
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600218 }
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100219
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600220 return QTAILQ_FIRST(&mouse_handlers)->qemu_put_mouse_event_absolute;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100221}
222
Anthony Liguorieb2e2592010-03-09 14:26:40 -0600223int kbd_mouse_has_absolute(void)
224{
225 QEMUPutMouseEntry *entry;
226
227 QTAILQ_FOREACH(entry, &mouse_handlers, node) {
228 if (entry->qemu_put_mouse_event_absolute) {
229 return 1;
230 }
231 }
232
233 return 0;
234}
235
Luiz Capitulinoe235cec2011-09-21 15:29:55 -0300236MouseInfoList *qmp_query_mice(Error **errp)
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100237{
Luiz Capitulinoe235cec2011-09-21 15:29:55 -0300238 MouseInfoList *mice_list = NULL;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100239 QEMUPutMouseEntry *cursor;
Luiz Capitulinoe235cec2011-09-21 15:29:55 -0300240 bool current = true;
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600241
242 QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
Luiz Capitulinoe235cec2011-09-21 15:29:55 -0300243 MouseInfoList *info = g_malloc0(sizeof(*info));
244 info->value = g_malloc0(sizeof(*info->value));
245 info->value->name = g_strdup(cursor->qemu_put_mouse_event_name);
246 info->value->index = cursor->index;
247 info->value->absolute = !!cursor->qemu_put_mouse_event_absolute;
248 info->value->current = current;
249
250 current = false;
251
252 info->next = mice_list;
253 mice_list = info;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100254 }
255
Luiz Capitulinoe235cec2011-09-21 15:29:55 -0300256 return mice_list;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100257}
258
259void do_mouse_set(Monitor *mon, const QDict *qdict)
260{
261 QEMUPutMouseEntry *cursor;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100262 int index = qdict_get_int(qdict, "index");
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600263 int found = 0;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100264
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600265 if (QTAILQ_EMPTY(&mouse_handlers)) {
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100266 monitor_printf(mon, "No mouse devices connected\n");
267 return;
268 }
269
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600270 QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
271 if (cursor->index == index) {
272 found = 1;
273 qemu_activate_mouse_event_handler(cursor);
274 break;
275 }
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100276 }
277
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600278 if (!found) {
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100279 monitor_printf(mon, "Mouse at given index not found\n");
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600280 }
Anthony Liguori7e581fb2010-03-09 13:25:00 -0600281
282 check_mode_change();
283}
284
285void qemu_add_mouse_mode_change_notifier(Notifier *notify)
286{
287 notifier_list_add(&mouse_mode_notifiers, notify);
288}
289
290void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
291{
Paolo Bonzini31552522012-01-13 17:34:01 +0100292 notifier_remove(notify);
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100293}