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 QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
35 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
36 static QTAILQ_HEAD(, QEMUPutLEDEntry
) led_handlers
= QTAILQ_HEAD_INITIALIZER(led_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
,
48 QEMUPutMouseEntry
*s
, *cursor
;
50 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
52 s
->qemu_put_mouse_event
= func
;
53 s
->qemu_put_mouse_event_opaque
= opaque
;
54 s
->qemu_put_mouse_event_absolute
= absolute
;
55 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
58 if (!qemu_put_mouse_event_head
) {
59 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
63 cursor
= qemu_put_mouse_event_head
;
64 while (cursor
->next
!= NULL
)
65 cursor
= cursor
->next
;
68 qemu_put_mouse_event_current
= s
;
73 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
75 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
77 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
80 cursor
= qemu_put_mouse_event_head
;
81 while (cursor
!= NULL
&& cursor
!= entry
) {
83 cursor
= cursor
->next
;
86 if (cursor
== NULL
) // does not exist or list empty
88 else if (prev
== NULL
) { // entry is head
89 qemu_put_mouse_event_head
= cursor
->next
;
90 if (qemu_put_mouse_event_current
== entry
)
91 qemu_put_mouse_event_current
= cursor
->next
;
92 qemu_free(entry
->qemu_put_mouse_event_name
);
97 prev
->next
= entry
->next
;
99 if (qemu_put_mouse_event_current
== entry
)
100 qemu_put_mouse_event_current
= prev
;
102 qemu_free(entry
->qemu_put_mouse_event_name
);
106 QEMUPutLEDEntry
*qemu_add_led_event_handler(QEMUPutLEDEvent
*func
,
111 s
= qemu_mallocz(sizeof(QEMUPutLEDEntry
));
115 QTAILQ_INSERT_TAIL(&led_handlers
, s
, next
);
119 void qemu_remove_led_event_handler(QEMUPutLEDEntry
*entry
)
123 QTAILQ_REMOVE(&led_handlers
, entry
, next
);
127 void kbd_put_keycode(int keycode
)
129 if (qemu_put_kbd_event
) {
130 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
134 void kbd_put_ledstate(int ledstate
)
136 QEMUPutLEDEntry
*cursor
;
138 QTAILQ_FOREACH(cursor
, &led_handlers
, next
) {
139 cursor
->put_led(cursor
->opaque
, ledstate
);
143 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
145 QEMUPutMouseEvent
*mouse_event
;
146 void *mouse_event_opaque
;
149 if (!qemu_put_mouse_event_current
) {
154 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
156 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
159 if (graphic_rotate
) {
160 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
163 width
= graphic_width
- 1;
164 mouse_event(mouse_event_opaque
,
165 width
- dy
, dx
, dz
, buttons_state
);
167 mouse_event(mouse_event_opaque
,
168 dx
, dy
, dz
, buttons_state
);
172 int kbd_mouse_is_absolute(void)
174 if (!qemu_put_mouse_event_current
)
177 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
180 static void info_mice_iter(QObject
*data
, void *opaque
)
183 Monitor
*mon
= opaque
;
185 mouse
= qobject_to_qdict(data
);
186 monitor_printf(mon
, "%c Mouse #%" PRId64
": %s\n",
187 (qdict_get_bool(mouse
, "current") ? '*' : ' '),
188 qdict_get_int(mouse
, "index"), qdict_get_str(mouse
, "name"));
191 void do_info_mice_print(Monitor
*mon
, const QObject
*data
)
195 mice_list
= qobject_to_qlist(data
);
196 if (qlist_empty(mice_list
)) {
197 monitor_printf(mon
, "No mouse devices connected\n");
201 qlist_iter(mice_list
, info_mice_iter
, mon
);
205 * do_info_mice(): Show VM mice information
207 * Each mouse is represented by a QDict, the returned QObject is a QList of
210 * The mouse QDict contains the following:
212 * - "name": mouse's name
213 * - "index": mouse's index
214 * - "current": true if this mouse is receiving events, false otherwise
218 * [ { "name": "QEMU Microsoft Mouse", "index": 0, "current": false },
219 * { "name": "QEMU PS/2 Mouse", "index": 1, "current": true } ]
221 void do_info_mice(Monitor
*mon
, QObject
**ret_data
)
223 QEMUPutMouseEntry
*cursor
;
227 mice_list
= qlist_new();
229 if (!qemu_put_mouse_event_head
) {
233 cursor
= qemu_put_mouse_event_head
;
234 while (cursor
!= NULL
) {
236 obj
= qobject_from_jsonf("{ 'name': %s, 'index': %d, 'current': %i }",
237 cursor
->qemu_put_mouse_event_name
,
238 index
, cursor
== qemu_put_mouse_event_current
);
239 qlist_append_obj(mice_list
, obj
);
241 cursor
= cursor
->next
;
245 *ret_data
= QOBJECT(mice_list
);
248 void do_mouse_set(Monitor
*mon
, const QDict
*qdict
)
250 QEMUPutMouseEntry
*cursor
;
252 int index
= qdict_get_int(qdict
, "index");
254 if (!qemu_put_mouse_event_head
) {
255 monitor_printf(mon
, "No mouse devices connected\n");
259 cursor
= qemu_put_mouse_event_head
;
260 while (cursor
!= NULL
&& index
!= i
) {
262 cursor
= cursor
->next
;
266 qemu_put_mouse_event_current
= cursor
;
268 monitor_printf(mon
, "Mouse at given index not found\n");