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
25 #include "qemu/osdep.h"
26 #include "sysemu/sysemu.h"
27 #include "ui/console.h"
28 #include "qmp-commands.h"
29 #include "ui/keymaps.h"
32 struct QEMUPutMouseEntry
{
33 QEMUPutMouseEvent
*qemu_put_mouse_event
;
34 void *qemu_put_mouse_event_opaque
;
35 int qemu_put_mouse_event_absolute
;
39 QemuInputHandlerState
*s
;
40 int axis
[INPUT_AXIS__MAX
];
44 struct QEMUPutKbdEntry
{
45 QEMUPutKBDEvent
*put_kbd
;
47 QemuInputHandlerState
*s
;
50 struct QEMUPutLEDEntry
{
51 QEMUPutLEDEvent
*put_led
;
53 QTAILQ_ENTRY(QEMUPutLEDEntry
) next
;
56 static QTAILQ_HEAD(, QEMUPutLEDEntry
) led_handlers
=
57 QTAILQ_HEAD_INITIALIZER(led_handlers
);
59 int index_from_key(const char *key
, size_t key_length
)
63 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
64 if (!strncmp(key
, QKeyCode_str(i
), key_length
) &&
65 !QKeyCode_str(i
)[key_length
]) {
70 /* Return Q_KEY_CODE__MAX if the key is invalid */
74 static KeyValue
*copy_key_value(KeyValue
*src
)
76 KeyValue
*dst
= g_new(KeyValue
, 1);
77 memcpy(dst
, src
, sizeof(*src
));
78 if (dst
->type
== KEY_VALUE_KIND_NUMBER
) {
79 QKeyCode code
= qemu_input_key_number_to_qcode(dst
->u
.number
.data
);
80 dst
->type
= KEY_VALUE_KIND_QCODE
;
81 dst
->u
.qcode
.data
= code
;
86 void qmp_send_key(KeyValueList
*keys
, bool has_hold_time
, int64_t hold_time
,
94 hold_time
= 0; /* use default */
97 for (p
= keys
; p
!= NULL
; p
= p
->next
) {
98 qemu_input_event_send_key(NULL
, copy_key_value(p
->value
), true);
99 qemu_input_event_send_key_delay(hold_time
);
100 up
= g_realloc(up
, sizeof(*up
) * (count
+1));
101 up
[count
] = copy_key_value(p
->value
);
106 qemu_input_event_send_key(NULL
, up
[count
], false);
107 qemu_input_event_send_key_delay(hold_time
);
112 static void legacy_kbd_event(DeviceState
*dev
, QemuConsole
*src
,
115 QEMUPutKbdEntry
*entry
= (QEMUPutKbdEntry
*)dev
;
116 int scancodes
[3], i
, count
;
117 InputKeyEvent
*key
= evt
->u
.key
.data
;
119 if (!entry
|| !entry
->put_kbd
) {
122 count
= qemu_input_key_value_to_scancode(key
->key
,
125 for (i
= 0; i
< count
; i
++) {
126 entry
->put_kbd(entry
->opaque
, scancodes
[i
]);
130 static QemuInputHandler legacy_kbd_handler
= {
131 .name
= "legacy-kbd",
132 .mask
= INPUT_EVENT_MASK_KEY
,
133 .event
= legacy_kbd_event
,
136 QEMUPutKbdEntry
*qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
138 QEMUPutKbdEntry
*entry
;
140 entry
= g_new0(QEMUPutKbdEntry
, 1);
141 entry
->put_kbd
= func
;
142 entry
->opaque
= opaque
;
143 entry
->s
= qemu_input_handler_register((DeviceState
*)entry
,
144 &legacy_kbd_handler
);
145 qemu_input_handler_activate(entry
->s
);
149 static void legacy_mouse_event(DeviceState
*dev
, QemuConsole
*src
,
152 static const int bmap
[INPUT_BUTTON__MAX
] = {
153 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
154 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
155 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
157 QEMUPutMouseEntry
*s
= (QEMUPutMouseEntry
*)dev
;
159 InputMoveEvent
*move
;
162 case INPUT_EVENT_KIND_BTN
:
163 btn
= evt
->u
.btn
.data
;
165 s
->buttons
|= bmap
[btn
->button
];
167 s
->buttons
&= ~bmap
[btn
->button
];
169 if (btn
->down
&& btn
->button
== INPUT_BUTTON_WHEEL_UP
) {
170 s
->qemu_put_mouse_event(s
->qemu_put_mouse_event_opaque
,
171 s
->axis
[INPUT_AXIS_X
],
172 s
->axis
[INPUT_AXIS_Y
],
176 if (btn
->down
&& btn
->button
== INPUT_BUTTON_WHEEL_DOWN
) {
177 s
->qemu_put_mouse_event(s
->qemu_put_mouse_event_opaque
,
178 s
->axis
[INPUT_AXIS_X
],
179 s
->axis
[INPUT_AXIS_Y
],
184 case INPUT_EVENT_KIND_ABS
:
185 move
= evt
->u
.abs
.data
;
186 s
->axis
[move
->axis
] = move
->value
;
188 case INPUT_EVENT_KIND_REL
:
189 move
= evt
->u
.rel
.data
;
190 s
->axis
[move
->axis
] += move
->value
;
197 static void legacy_mouse_sync(DeviceState
*dev
)
199 QEMUPutMouseEntry
*s
= (QEMUPutMouseEntry
*)dev
;
201 s
->qemu_put_mouse_event(s
->qemu_put_mouse_event_opaque
,
202 s
->axis
[INPUT_AXIS_X
],
203 s
->axis
[INPUT_AXIS_Y
],
207 if (!s
->qemu_put_mouse_event_absolute
) {
208 s
->axis
[INPUT_AXIS_X
] = 0;
209 s
->axis
[INPUT_AXIS_Y
] = 0;
213 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
214 void *opaque
, int absolute
,
217 QEMUPutMouseEntry
*s
;
219 s
= g_new0(QEMUPutMouseEntry
, 1);
221 s
->qemu_put_mouse_event
= func
;
222 s
->qemu_put_mouse_event_opaque
= opaque
;
223 s
->qemu_put_mouse_event_absolute
= absolute
;
226 s
->h
.mask
= INPUT_EVENT_MASK_BTN
|
227 (absolute
? INPUT_EVENT_MASK_ABS
: INPUT_EVENT_MASK_REL
);
228 s
->h
.event
= legacy_mouse_event
;
229 s
->h
.sync
= legacy_mouse_sync
;
230 s
->s
= qemu_input_handler_register((DeviceState
*)s
,
236 void qemu_activate_mouse_event_handler(QEMUPutMouseEntry
*entry
)
238 qemu_input_handler_activate(entry
->s
);
241 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
243 qemu_input_handler_unregister(entry
->s
);
248 QEMUPutLEDEntry
*qemu_add_led_event_handler(QEMUPutLEDEvent
*func
,
253 s
= g_new0(QEMUPutLEDEntry
, 1);
257 QTAILQ_INSERT_TAIL(&led_handlers
, s
, next
);
261 void qemu_remove_led_event_handler(QEMUPutLEDEntry
*entry
)
265 QTAILQ_REMOVE(&led_handlers
, entry
, next
);
269 void kbd_put_ledstate(int ledstate
)
271 QEMUPutLEDEntry
*cursor
;
273 QTAILQ_FOREACH(cursor
, &led_handlers
, next
) {
274 cursor
->put_led(cursor
->opaque
, ledstate
);