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 "qapi-types.h"
30 #include "ui/keymaps.h"
33 struct QEMUPutMouseEntry {
34 QEMUPutMouseEvent *qemu_put_mouse_event;
35 void *qemu_put_mouse_event_opaque;
36 int qemu_put_mouse_event_absolute;
40 QemuInputHandlerState *s;
41 int axis[INPUT_AXIS__MAX];
45 struct QEMUPutKbdEntry {
46 QEMUPutKBDEvent *put_kbd;
48 QemuInputHandlerState *s;
51 struct QEMUPutLEDEntry {
52 QEMUPutLEDEvent *put_led;
54 QTAILQ_ENTRY(QEMUPutLEDEntry) next;
57 static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers =
58 QTAILQ_HEAD_INITIALIZER(led_handlers);
60 int index_from_key(const char *key, size_t key_length)
64 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
65 if (!strncmp(key, QKeyCode_str(i), key_length) &&
66 !QKeyCode_str(i)[key_length]) {
71 /* Return Q_KEY_CODE__MAX if the key is invalid */
75 static KeyValue *copy_key_value(KeyValue *src)
77 KeyValue *dst = g_new(KeyValue, 1);
78 memcpy(dst, src, sizeof(*src));
79 if (dst->type == KEY_VALUE_KIND_NUMBER) {
80 QKeyCode code = qemu_input_key_number_to_qcode(dst->u.number.data);
81 dst->type = KEY_VALUE_KIND_QCODE;
82 dst->u.qcode.data = code;
87 void qmp_send_key(KeyValueList *keys, bool has_hold_time, int64_t hold_time,
95 hold_time = 0; /* use default */
98 for (p = keys; p != NULL; p = p->next) {
99 qemu_input_event_send_key(NULL, copy_key_value(p->value), true);
100 qemu_input_event_send_key_delay(hold_time);
101 up = g_realloc(up, sizeof(*up) * (count+1));
102 up[count] = copy_key_value(p->value);
107 qemu_input_event_send_key(NULL, up[count], false);
108 qemu_input_event_send_key_delay(hold_time);
113 static void legacy_kbd_event(DeviceState *dev, QemuConsole *src,
116 QEMUPutKbdEntry *entry = (QEMUPutKbdEntry *)dev;
117 int scancodes[3], i, count;
118 InputKeyEvent *key = evt->u.key.data;
120 if (!entry || !entry->put_kbd) {
123 count = qemu_input_key_value_to_scancode(key->key,
126 for (i = 0; i < count; i++) {
127 entry->put_kbd(entry->opaque, scancodes[i]);
131 static QemuInputHandler legacy_kbd_handler = {
132 .name = "legacy-kbd",
133 .mask = INPUT_EVENT_MASK_KEY,
134 .event = legacy_kbd_event,
137 QEMUPutKbdEntry *qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
139 QEMUPutKbdEntry *entry;
141 entry = g_new0(QEMUPutKbdEntry, 1);
142 entry->put_kbd = func;
143 entry->opaque = opaque;
144 entry->s = qemu_input_handler_register((DeviceState *)entry,
145 &legacy_kbd_handler);
146 qemu_input_handler_activate(entry->s);
150 static void legacy_mouse_event(DeviceState *dev, QemuConsole *src,
153 static const int bmap[INPUT_BUTTON__MAX] = {
154 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
155 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
156 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
158 QEMUPutMouseEntry *s = (QEMUPutMouseEntry *)dev;
160 InputMoveEvent *move;
163 case INPUT_EVENT_KIND_BTN:
164 btn = evt->u.btn.data;
166 s->buttons |= bmap[btn->button];
168 s->buttons &= ~bmap[btn->button];
170 if (btn->down && btn->button == INPUT_BUTTON_WHEEL_UP) {
171 s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque,
172 s->axis[INPUT_AXIS_X],
173 s->axis[INPUT_AXIS_Y],
177 if (btn->down && btn->button == INPUT_BUTTON_WHEEL_DOWN) {
178 s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque,
179 s->axis[INPUT_AXIS_X],
180 s->axis[INPUT_AXIS_Y],
185 case INPUT_EVENT_KIND_ABS:
186 move = evt->u.abs.data;
187 s->axis[move->axis] = move->value;
189 case INPUT_EVENT_KIND_REL:
190 move = evt->u.rel.data;
191 s->axis[move->axis] += move->value;
198 static void legacy_mouse_sync(DeviceState *dev)
200 QEMUPutMouseEntry *s = (QEMUPutMouseEntry *)dev;
202 s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque,
203 s->axis[INPUT_AXIS_X],
204 s->axis[INPUT_AXIS_Y],
208 if (!s->qemu_put_mouse_event_absolute) {
209 s->axis[INPUT_AXIS_X] = 0;
210 s->axis[INPUT_AXIS_Y] = 0;
214 QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
215 void *opaque, int absolute,
218 QEMUPutMouseEntry *s;
220 s = g_new0(QEMUPutMouseEntry, 1);
222 s->qemu_put_mouse_event = func;
223 s->qemu_put_mouse_event_opaque = opaque;
224 s->qemu_put_mouse_event_absolute = absolute;
227 s->h.mask = INPUT_EVENT_MASK_BTN |
228 (absolute ? INPUT_EVENT_MASK_ABS : INPUT_EVENT_MASK_REL);
229 s->h.event = legacy_mouse_event;
230 s->h.sync = legacy_mouse_sync;
231 s->s = qemu_input_handler_register((DeviceState *)s,
237 void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
239 qemu_input_handler_activate(entry->s);
242 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
244 qemu_input_handler_unregister(entry->s);
249 QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
254 s = g_new0(QEMUPutLEDEntry, 1);
258 QTAILQ_INSERT_TAIL(&led_handlers, s, next);
262 void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry)
266 QTAILQ_REMOVE(&led_handlers, entry, next);
270 void kbd_put_ledstate(int ledstate)
272 QEMUPutLEDEntry *cursor;
274 QTAILQ_FOREACH(cursor, &led_handlers, next) {
275 cursor->put_led(cursor->opaque, ledstate);