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
30 #include "qmp-commands.h"
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
);
37 static NotifierList mouse_mode_notifiers
=
38 NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers
);
40 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
42 qemu_put_kbd_event_opaque
= opaque
;
43 qemu_put_kbd_event
= func
;
46 void qemu_remove_kbd_event_handler(void)
48 qemu_put_kbd_event_opaque
= NULL
;
49 qemu_put_kbd_event
= NULL
;
52 static void check_mode_change(void)
54 static int current_is_absolute
, current_has_absolute
;
58 is_absolute
= kbd_mouse_is_absolute();
59 has_absolute
= kbd_mouse_has_absolute();
61 if (is_absolute
!= current_is_absolute
||
62 has_absolute
!= current_has_absolute
) {
63 notifier_list_notify(&mouse_mode_notifiers
, NULL
);
66 current_is_absolute
= is_absolute
;
67 current_has_absolute
= has_absolute
;
70 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
71 void *opaque
, int absolute
,
75 static int mouse_index
= 0;
77 s
= g_malloc0(sizeof(QEMUPutMouseEntry
));
79 s
->qemu_put_mouse_event
= func
;
80 s
->qemu_put_mouse_event_opaque
= opaque
;
81 s
->qemu_put_mouse_event_absolute
= absolute
;
82 s
->qemu_put_mouse_event_name
= g_strdup(name
);
83 s
->index
= mouse_index
++;
85 QTAILQ_INSERT_TAIL(&mouse_handlers
, s
, node
);
92 void qemu_activate_mouse_event_handler(QEMUPutMouseEntry
*entry
)
94 QTAILQ_REMOVE(&mouse_handlers
, entry
, node
);
95 QTAILQ_INSERT_HEAD(&mouse_handlers
, entry
, node
);
100 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
102 QTAILQ_REMOVE(&mouse_handlers
, entry
, node
);
104 g_free(entry
->qemu_put_mouse_event_name
);
110 QEMUPutLEDEntry
*qemu_add_led_event_handler(QEMUPutLEDEvent
*func
,
115 s
= g_malloc0(sizeof(QEMUPutLEDEntry
));
119 QTAILQ_INSERT_TAIL(&led_handlers
, s
, next
);
123 void qemu_remove_led_event_handler(QEMUPutLEDEntry
*entry
)
127 QTAILQ_REMOVE(&led_handlers
, entry
, next
);
131 void kbd_put_keycode(int keycode
)
133 if (!runstate_is_running()) {
136 if (qemu_put_kbd_event
) {
137 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
141 void kbd_put_ledstate(int ledstate
)
143 QEMUPutLEDEntry
*cursor
;
145 QTAILQ_FOREACH(cursor
, &led_handlers
, next
) {
146 cursor
->put_led(cursor
->opaque
, ledstate
);
150 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
152 QEMUPutMouseEntry
*entry
;
153 QEMUPutMouseEvent
*mouse_event
;
154 void *mouse_event_opaque
;
157 if (!runstate_is_running()) {
160 if (QTAILQ_EMPTY(&mouse_handlers
)) {
164 entry
= QTAILQ_FIRST(&mouse_handlers
);
166 mouse_event
= entry
->qemu_put_mouse_event
;
167 mouse_event_opaque
= entry
->qemu_put_mouse_event_opaque
;
170 if (entry
->qemu_put_mouse_event_absolute
) {
174 width
= graphic_width
- 1;
175 height
= graphic_height
- 1;
178 switch (graphic_rotate
) {
180 mouse_event(mouse_event_opaque
,
181 dx
, dy
, dz
, buttons_state
);
184 mouse_event(mouse_event_opaque
,
185 width
- dy
, dx
, dz
, buttons_state
);
188 mouse_event(mouse_event_opaque
,
189 width
- dx
, height
- dy
, dz
, buttons_state
);
192 mouse_event(mouse_event_opaque
,
193 dy
, height
- dx
, dz
, buttons_state
);
199 int kbd_mouse_is_absolute(void)
201 if (QTAILQ_EMPTY(&mouse_handlers
)) {
205 return QTAILQ_FIRST(&mouse_handlers
)->qemu_put_mouse_event_absolute
;
208 int kbd_mouse_has_absolute(void)
210 QEMUPutMouseEntry
*entry
;
212 QTAILQ_FOREACH(entry
, &mouse_handlers
, node
) {
213 if (entry
->qemu_put_mouse_event_absolute
) {
221 MouseInfoList
*qmp_query_mice(Error
**errp
)
223 MouseInfoList
*mice_list
= NULL
;
224 QEMUPutMouseEntry
*cursor
;
227 QTAILQ_FOREACH(cursor
, &mouse_handlers
, node
) {
228 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
;
237 info
->next
= mice_list
;
244 void do_mouse_set(Monitor
*mon
, const QDict
*qdict
)
246 QEMUPutMouseEntry
*cursor
;
247 int index
= qdict_get_int(qdict
, "index");
250 if (QTAILQ_EMPTY(&mouse_handlers
)) {
251 monitor_printf(mon
, "No mouse devices connected\n");
255 QTAILQ_FOREACH(cursor
, &mouse_handlers
, node
) {
256 if (cursor
->index
== index
) {
258 qemu_activate_mouse_event_handler(cursor
);
264 monitor_printf(mon
, "Mouse at given index not found\n");
270 void qemu_add_mouse_mode_change_notifier(Notifier
*notify
)
272 notifier_list_add(&mouse_mode_notifiers
, notify
);
275 void qemu_remove_mouse_mode_change_notifier(Notifier
*notify
)
277 notifier_remove(notify
);