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
32 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
33 static void *qemu_put_kbd_event_opaque
;
34 static QTAILQ_HEAD(, QEMUPutLEDEntry
) led_handlers
= QTAILQ_HEAD_INITIALIZER(led_handlers
);
35 static QTAILQ_HEAD(, QEMUPutMouseEntry
) mouse_handlers
=
36 QTAILQ_HEAD_INITIALIZER(mouse_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
,
49 static int mouse_index
= 0;
51 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
53 s
->qemu_put_mouse_event
= func
;
54 s
->qemu_put_mouse_event_opaque
= opaque
;
55 s
->qemu_put_mouse_event_absolute
= absolute
;
56 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
57 s
->index
= mouse_index
++;
59 QTAILQ_INSERT_TAIL(&mouse_handlers
, s
, node
);
64 void qemu_activate_mouse_event_handler(QEMUPutMouseEntry
*entry
)
66 QTAILQ_REMOVE(&mouse_handlers
, entry
, node
);
67 QTAILQ_INSERT_HEAD(&mouse_handlers
, entry
, node
);
72 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
74 QTAILQ_REMOVE(&mouse_handlers
, entry
, node
);
76 qemu_free(entry
->qemu_put_mouse_event_name
);
80 QEMUPutLEDEntry
*qemu_add_led_event_handler(QEMUPutLEDEvent
*func
,
85 s
= qemu_mallocz(sizeof(QEMUPutLEDEntry
));
89 QTAILQ_INSERT_TAIL(&led_handlers
, s
, next
);
93 void qemu_remove_led_event_handler(QEMUPutLEDEntry
*entry
)
97 QTAILQ_REMOVE(&led_handlers
, entry
, next
);
101 void kbd_put_keycode(int keycode
)
103 if (qemu_put_kbd_event
) {
104 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
108 void kbd_put_ledstate(int ledstate
)
110 QEMUPutLEDEntry
*cursor
;
112 QTAILQ_FOREACH(cursor
, &led_handlers
, next
) {
113 cursor
->put_led(cursor
->opaque
, ledstate
);
117 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
119 QEMUPutMouseEntry
*entry
;
120 QEMUPutMouseEvent
*mouse_event
;
121 void *mouse_event_opaque
;
124 if (QTAILQ_EMPTY(&mouse_handlers
)) {
128 entry
= QTAILQ_FIRST(&mouse_handlers
);
130 mouse_event
= entry
->qemu_put_mouse_event
;
131 mouse_event_opaque
= entry
->qemu_put_mouse_event_opaque
;
134 if (graphic_rotate
) {
135 if (entry
->qemu_put_mouse_event_absolute
)
138 width
= graphic_width
- 1;
139 mouse_event(mouse_event_opaque
,
140 width
- dy
, dx
, dz
, buttons_state
);
142 mouse_event(mouse_event_opaque
,
143 dx
, dy
, dz
, buttons_state
);
147 int kbd_mouse_is_absolute(void)
149 if (QTAILQ_EMPTY(&mouse_handlers
)) {
153 return QTAILQ_FIRST(&mouse_handlers
)->qemu_put_mouse_event_absolute
;
156 int kbd_mouse_has_absolute(void)
158 QEMUPutMouseEntry
*entry
;
160 QTAILQ_FOREACH(entry
, &mouse_handlers
, node
) {
161 if (entry
->qemu_put_mouse_event_absolute
) {
169 static void info_mice_iter(QObject
*data
, void *opaque
)
172 Monitor
*mon
= opaque
;
174 mouse
= qobject_to_qdict(data
);
175 monitor_printf(mon
, "%c Mouse #%" PRId64
": %s\n",
176 (qdict_get_bool(mouse
, "current") ? '*' : ' '),
177 qdict_get_int(mouse
, "index"), qdict_get_str(mouse
, "name"));
180 void do_info_mice_print(Monitor
*mon
, const QObject
*data
)
184 mice_list
= qobject_to_qlist(data
);
185 if (qlist_empty(mice_list
)) {
186 monitor_printf(mon
, "No mouse devices connected\n");
190 qlist_iter(mice_list
, info_mice_iter
, mon
);
194 * do_info_mice(): Show VM mice information
196 * Each mouse is represented by a QDict, the returned QObject is a QList of
199 * The mouse QDict contains the following:
201 * - "name": mouse's name
202 * - "index": mouse's index
203 * - "current": true if this mouse is receiving events, false otherwise
207 * [ { "name": "QEMU Microsoft Mouse", "index": 0, "current": false },
208 * { "name": "QEMU PS/2 Mouse", "index": 1, "current": true } ]
210 void do_info_mice(Monitor
*mon
, QObject
**ret_data
)
212 QEMUPutMouseEntry
*cursor
;
216 mice_list
= qlist_new();
218 if (QTAILQ_EMPTY(&mouse_handlers
)) {
222 current
= QTAILQ_FIRST(&mouse_handlers
)->index
;
224 QTAILQ_FOREACH(cursor
, &mouse_handlers
, node
) {
226 obj
= qobject_from_jsonf("{ 'name': %s, 'index': %d, 'current': %i }",
227 cursor
->qemu_put_mouse_event_name
,
229 cursor
->index
== current
);
230 qlist_append_obj(mice_list
, obj
);
234 *ret_data
= QOBJECT(mice_list
);
237 void do_mouse_set(Monitor
*mon
, const QDict
*qdict
)
239 QEMUPutMouseEntry
*cursor
;
240 int index
= qdict_get_int(qdict
, "index");
243 if (QTAILQ_EMPTY(&mouse_handlers
)) {
244 monitor_printf(mon
, "No mouse devices connected\n");
248 QTAILQ_FOREACH(cursor
, &mouse_handlers
, node
) {
249 if (cursor
->index
== index
) {
251 qemu_activate_mouse_event_handler(cursor
);
257 monitor_printf(mon
, "Mouse at given index not found\n");