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
31 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
32 static void *qemu_put_kbd_event_opaque
;
33 static QTAILQ_HEAD(, QEMUPutLEDEntry
) led_handlers
= QTAILQ_HEAD_INITIALIZER(led_handlers
);
34 static QTAILQ_HEAD(, QEMUPutMouseEntry
) mouse_handlers
=
35 QTAILQ_HEAD_INITIALIZER(mouse_handlers
);
36 static NotifierList mouse_mode_notifiers
=
37 NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers
);
39 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
41 qemu_put_kbd_event_opaque
= opaque
;
42 qemu_put_kbd_event
= func
;
45 void qemu_remove_kbd_event_handler(void)
47 qemu_put_kbd_event_opaque
= NULL
;
48 qemu_put_kbd_event
= NULL
;
51 static void check_mode_change(void)
53 static int current_is_absolute
, current_has_absolute
;
57 is_absolute
= kbd_mouse_is_absolute();
58 has_absolute
= kbd_mouse_has_absolute();
60 if (is_absolute
!= current_is_absolute
||
61 has_absolute
!= current_has_absolute
) {
62 notifier_list_notify(&mouse_mode_notifiers
);
65 current_is_absolute
= is_absolute
;
66 current_has_absolute
= has_absolute
;
69 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
70 void *opaque
, int absolute
,
74 static int mouse_index
= 0;
76 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
78 s
->qemu_put_mouse_event
= func
;
79 s
->qemu_put_mouse_event_opaque
= opaque
;
80 s
->qemu_put_mouse_event_absolute
= absolute
;
81 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
82 s
->index
= mouse_index
++;
84 QTAILQ_INSERT_TAIL(&mouse_handlers
, s
, node
);
91 void qemu_activate_mouse_event_handler(QEMUPutMouseEntry
*entry
)
93 QTAILQ_REMOVE(&mouse_handlers
, entry
, node
);
94 QTAILQ_INSERT_HEAD(&mouse_handlers
, entry
, node
);
99 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
101 QTAILQ_REMOVE(&mouse_handlers
, entry
, node
);
103 qemu_free(entry
->qemu_put_mouse_event_name
);
109 QEMUPutLEDEntry
*qemu_add_led_event_handler(QEMUPutLEDEvent
*func
,
114 s
= qemu_mallocz(sizeof(QEMUPutLEDEntry
));
118 QTAILQ_INSERT_TAIL(&led_handlers
, s
, next
);
122 void qemu_remove_led_event_handler(QEMUPutLEDEntry
*entry
)
126 QTAILQ_REMOVE(&led_handlers
, entry
, next
);
130 void kbd_put_keycode(int keycode
)
132 if (qemu_put_kbd_event
) {
133 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
137 void kbd_put_ledstate(int ledstate
)
139 QEMUPutLEDEntry
*cursor
;
141 QTAILQ_FOREACH(cursor
, &led_handlers
, next
) {
142 cursor
->put_led(cursor
->opaque
, ledstate
);
146 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
148 QEMUPutMouseEntry
*entry
;
149 QEMUPutMouseEvent
*mouse_event
;
150 void *mouse_event_opaque
;
153 if (QTAILQ_EMPTY(&mouse_handlers
)) {
157 entry
= QTAILQ_FIRST(&mouse_handlers
);
159 mouse_event
= entry
->qemu_put_mouse_event
;
160 mouse_event_opaque
= entry
->qemu_put_mouse_event_opaque
;
163 if (graphic_rotate
) {
164 if (entry
->qemu_put_mouse_event_absolute
)
167 width
= graphic_width
- 1;
168 mouse_event(mouse_event_opaque
,
169 width
- dy
, dx
, dz
, buttons_state
);
171 mouse_event(mouse_event_opaque
,
172 dx
, dy
, dz
, buttons_state
);
176 int kbd_mouse_is_absolute(void)
178 if (QTAILQ_EMPTY(&mouse_handlers
)) {
182 return QTAILQ_FIRST(&mouse_handlers
)->qemu_put_mouse_event_absolute
;
185 int kbd_mouse_has_absolute(void)
187 QEMUPutMouseEntry
*entry
;
189 QTAILQ_FOREACH(entry
, &mouse_handlers
, node
) {
190 if (entry
->qemu_put_mouse_event_absolute
) {
198 static void info_mice_iter(QObject
*data
, void *opaque
)
201 Monitor
*mon
= opaque
;
203 mouse
= qobject_to_qdict(data
);
204 monitor_printf(mon
, "%c Mouse #%" PRId64
": %s%s\n",
205 (qdict_get_bool(mouse
, "current") ? '*' : ' '),
206 qdict_get_int(mouse
, "index"), qdict_get_str(mouse
, "name"),
207 qdict_get_bool(mouse
, "absolute") ? " (absolute)" : "");
210 void do_info_mice_print(Monitor
*mon
, const QObject
*data
)
214 mice_list
= qobject_to_qlist(data
);
215 if (qlist_empty(mice_list
)) {
216 monitor_printf(mon
, "No mouse devices connected\n");
220 qlist_iter(mice_list
, info_mice_iter
, mon
);
223 void do_info_mice(Monitor
*mon
, QObject
**ret_data
)
225 QEMUPutMouseEntry
*cursor
;
229 mice_list
= qlist_new();
231 if (QTAILQ_EMPTY(&mouse_handlers
)) {
235 current
= QTAILQ_FIRST(&mouse_handlers
)->index
;
237 QTAILQ_FOREACH(cursor
, &mouse_handlers
, node
) {
239 obj
= qobject_from_jsonf("{ 'name': %s,"
243 cursor
->qemu_put_mouse_event_name
,
245 cursor
->index
== current
,
246 !!cursor
->qemu_put_mouse_event_absolute
);
247 qlist_append_obj(mice_list
, obj
);
251 *ret_data
= QOBJECT(mice_list
);
254 void do_mouse_set(Monitor
*mon
, const QDict
*qdict
)
256 QEMUPutMouseEntry
*cursor
;
257 int index
= qdict_get_int(qdict
, "index");
260 if (QTAILQ_EMPTY(&mouse_handlers
)) {
261 monitor_printf(mon
, "No mouse devices connected\n");
265 QTAILQ_FOREACH(cursor
, &mouse_handlers
, node
) {
266 if (cursor
->index
== index
) {
268 qemu_activate_mouse_event_handler(cursor
);
274 monitor_printf(mon
, "Mouse at given index not found\n");
280 void qemu_add_mouse_mode_change_notifier(Notifier
*notify
)
282 notifier_list_add(&mouse_mode_notifiers
, notify
);
285 void qemu_remove_mouse_mode_change_notifier(Notifier
*notify
)
287 notifier_list_remove(&mouse_mode_notifiers
, notify
);