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 "qapi/qapi-commands-ui.h"
27 #include "ui/console.h"
31 struct QEMUPutMouseEntry {
32 QEMUPutMouseEvent *qemu_put_mouse_event;
33 void *qemu_put_mouse_event_opaque;
34 int qemu_put_mouse_event_absolute;
38 QemuInputHandlerState *s;
39 int axis[INPUT_AXIS__MAX];
43 struct QEMUPutKbdEntry {
44 QEMUPutKBDEvent *put_kbd;
46 QemuInputHandlerState *s;
49 struct QEMUPutLEDEntry {
50 QEMUPutLEDEvent *put_led;
52 QTAILQ_ENTRY(QEMUPutLEDEntry) next;
55 static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers =
56 QTAILQ_HEAD_INITIALIZER(led_handlers);
58 int index_from_key(const char *key, size_t key_length)
62 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
63 if (!strncmp(key, QKeyCode_str(i), key_length) &&
64 !QKeyCode_str(i)[key_length]) {
69 /* Return Q_KEY_CODE__MAX if the key is invalid */
73 static KeyValue *copy_key_value(KeyValue *src)
75 KeyValue *dst = g_new(KeyValue, 1);
76 memcpy(dst, src, sizeof(*src));
77 if (dst->type == KEY_VALUE_KIND_NUMBER) {
78 QKeyCode code = qemu_input_key_number_to_qcode(dst->u.number.data);
79 dst->type = KEY_VALUE_KIND_QCODE;
80 dst->u.qcode.data = code;
85 void qmp_send_key(KeyValueList *keys, bool has_hold_time, int64_t hold_time,
93 hold_time = 0; /* use default */
96 for (p = keys; p != NULL; p = p->next) {
97 qemu_input_event_send_key(NULL, copy_key_value(p->value), true);
98 qemu_input_event_send_key_delay(hold_time);
99 up = g_realloc(up, sizeof(*up) * (count+1));
100 up[count] = copy_key_value(p->value);
105 qemu_input_event_send_key(NULL, up[count], false);
106 qemu_input_event_send_key_delay(hold_time);
111 static void legacy_kbd_event(DeviceState *dev, QemuConsole *src,
114 QEMUPutKbdEntry *entry = (QEMUPutKbdEntry *)dev;
115 int scancodes[3], i, count;
116 InputKeyEvent *key = evt->u.key.data;
118 if (!entry || !entry->put_kbd) {
121 count = qemu_input_key_value_to_scancode(key->key,
124 for (i = 0; i < count; i++) {
125 entry->put_kbd(entry->opaque, scancodes[i]);
129 static QemuInputHandler legacy_kbd_handler = {
130 .name = "legacy-kbd",
131 .mask = INPUT_EVENT_MASK_KEY,
132 .event = legacy_kbd_event,
135 QEMUPutKbdEntry *qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
137 QEMUPutKbdEntry *entry;
139 entry = g_new0(QEMUPutKbdEntry, 1);
140 entry->put_kbd = func;
141 entry->opaque = opaque;
142 entry->s = qemu_input_handler_register((DeviceState *)entry,
143 &legacy_kbd_handler);
144 qemu_input_handler_activate(entry->s);
148 static void legacy_mouse_event(DeviceState *dev, QemuConsole *src,
151 static const int bmap[INPUT_BUTTON__MAX] = {
152 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
153 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
154 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
156 QEMUPutMouseEntry *s = (QEMUPutMouseEntry *)dev;
158 InputMoveEvent *move;
161 case INPUT_EVENT_KIND_BTN:
162 btn = evt->u.btn.data;
164 s->buttons |= bmap[btn->button];
166 s->buttons &= ~bmap[btn->button];
168 if (btn->down && btn->button == INPUT_BUTTON_WHEEL_UP) {
169 s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque,
170 s->axis[INPUT_AXIS_X],
171 s->axis[INPUT_AXIS_Y],
175 if (btn->down && btn->button == INPUT_BUTTON_WHEEL_DOWN) {
176 s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque,
177 s->axis[INPUT_AXIS_X],
178 s->axis[INPUT_AXIS_Y],
183 case INPUT_EVENT_KIND_ABS:
184 move = evt->u.abs.data;
185 s->axis[move->axis] = move->value;
187 case INPUT_EVENT_KIND_REL:
188 move = evt->u.rel.data;
189 s->axis[move->axis] += move->value;
196 static void legacy_mouse_sync(DeviceState *dev)
198 QEMUPutMouseEntry *s = (QEMUPutMouseEntry *)dev;
200 s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque,
201 s->axis[INPUT_AXIS_X],
202 s->axis[INPUT_AXIS_Y],
206 if (!s->qemu_put_mouse_event_absolute) {
207 s->axis[INPUT_AXIS_X] = 0;
208 s->axis[INPUT_AXIS_Y] = 0;
212 QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
213 void *opaque, int absolute,
216 QEMUPutMouseEntry *s;
218 s = g_new0(QEMUPutMouseEntry, 1);
220 s->qemu_put_mouse_event = func;
221 s->qemu_put_mouse_event_opaque = opaque;
222 s->qemu_put_mouse_event_absolute = absolute;
225 s->h.mask = INPUT_EVENT_MASK_BTN |
226 (absolute ? INPUT_EVENT_MASK_ABS : INPUT_EVENT_MASK_REL);
227 s->h.event = legacy_mouse_event;
228 s->h.sync = legacy_mouse_sync;
229 s->s = qemu_input_handler_register((DeviceState *)s,
235 void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
237 qemu_input_handler_activate(entry->s);
240 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
242 qemu_input_handler_unregister(entry->s);
247 QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
252 s = g_new0(QEMUPutLEDEntry, 1);
256 QTAILQ_INSERT_TAIL(&led_handlers, s, next);
260 void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry)
264 QTAILQ_REMOVE(&led_handlers, entry, next);
268 void kbd_put_ledstate(int ledstate)
270 QEMUPutLEDEntry *cursor;
272 QTAILQ_FOREACH(cursor, &led_handlers, next) {
273 cursor->put_led(cursor->opaque, ledstate);