]> git.proxmox.com Git - mirror_qemu.git/blame - ui/input-legacy.c
Merge tag 'pull-aspeed-20240201' of https://github.com/legoater/qemu into staging
[mirror_qemu.git] / ui / input-legacy.c
CommitLineData
8f0056b7
PB
1/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
24
e16f4c87 25#include "qemu/osdep.h"
17f6315e 26#include "qemu/log.h"
9af23989 27#include "qapi/qapi-commands-ui.h"
28ecbaee 28#include "ui/console.h"
0041e9a0 29#include "keymaps.h"
9784e579 30#include "ui/input.h"
8f0056b7 31
72711efb
GH
32struct QEMUPutMouseEntry {
33 QEMUPutMouseEvent *qemu_put_mouse_event;
34 void *qemu_put_mouse_event_opaque;
35 int qemu_put_mouse_event_absolute;
edd85a3d
GH
36
37 /* new input core */
38 QemuInputHandler h;
39 QemuInputHandlerState *s;
7fb1cf16 40 int axis[INPUT_AXIS__MAX];
edd85a3d 41 int buttons;
72711efb
GH
42};
43
5a37532d
GH
44struct QEMUPutKbdEntry {
45 QEMUPutKBDEvent *put_kbd;
46 void *opaque;
9784e579 47 QemuInputHandlerState *s;
5a37532d
GH
48};
49
72711efb
GH
50struct QEMUPutLEDEntry {
51 QEMUPutLEDEvent *put_led;
52 void *opaque;
53 QTAILQ_ENTRY(QEMUPutLEDEntry) next;
54};
55
5a37532d
GH
56static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers =
57 QTAILQ_HEAD_INITIALIZER(led_handlers);
8f0056b7 58
64ffbe04 59int index_from_key(const char *key, size_t key_length)
1048c88f 60{
9d537c90 61 int i;
1048c88f 62
1c236ba5 63 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
977c736f
MA
64 if (!strncmp(key, QKeyCode_str(i), key_length) &&
65 !QKeyCode_str(i)[key_length]) {
1048c88f
AK
66 break;
67 }
68 }
69
7fb1cf16 70 /* Return Q_KEY_CODE__MAX if the key is invalid */
1048c88f
AK
71 return i;
72}
73
ce53f2f9
GH
74static KeyValue *copy_key_value(KeyValue *src)
75{
76 KeyValue *dst = g_new(KeyValue, 1);
77 memcpy(dst, src, sizeof(*src));
1d5b8d77
DB
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;
82 }
ce53f2f9 83 return dst;
e4c8f004
AK
84}
85
9f328977 86void qmp_send_key(KeyValueList *keys, bool has_hold_time, int64_t hold_time,
e4c8f004
AK
87 Error **errp)
88{
9f328977 89 KeyValueList *p;
e37f2024
GH
90 KeyValue **up = NULL;
91 int count = 0;
e4c8f004 92
e4c8f004 93 if (!has_hold_time) {
2e377f17 94 hold_time = 0; /* use default */
e4c8f004
AK
95 }
96
97 for (p = keys; p != NULL; p = p->next) {
ce53f2f9 98 qemu_input_event_send_key(NULL, copy_key_value(p->value), true);
2e377f17 99 qemu_input_event_send_key_delay(hold_time);
e37f2024
GH
100 up = g_realloc(up, sizeof(*up) * (count+1));
101 up[count] = copy_key_value(p->value);
102 count++;
2e377f17 103 }
e37f2024
GH
104 while (count) {
105 count--;
106 qemu_input_event_send_key(NULL, up[count], false);
2e377f17 107 qemu_input_event_send_key_delay(hold_time);
e4c8f004 108 }
e37f2024 109 g_free(up);
e4c8f004
AK
110}
111
9784e579
GH
112static void legacy_kbd_event(DeviceState *dev, QemuConsole *src,
113 InputEvent *evt)
114{
115 QEMUPutKbdEntry *entry = (QEMUPutKbdEntry *)dev;
02aa76c2 116 int scancodes[3], i, count;
32bafa8f 117 InputKeyEvent *key = evt->u.key.data;
9784e579
GH
118
119 if (!entry || !entry->put_kbd) {
120 return;
121 }
b5a1b443
EB
122 count = qemu_input_key_value_to_scancode(key->key,
123 key->down,
02aa76c2
GH
124 scancodes);
125 for (i = 0; i < count; i++) {
126 entry->put_kbd(entry->opaque, scancodes[i]);
9784e579 127 }
9784e579
GH
128}
129
b1be65f6 130static const QemuInputHandler legacy_kbd_handler = {
9784e579
GH
131 .name = "legacy-kbd",
132 .mask = INPUT_EVENT_MASK_KEY,
133 .event = legacy_kbd_event,
134};
135
5a37532d 136QEMUPutKbdEntry *qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
8f0056b7 137{
5a37532d
GH
138 QEMUPutKbdEntry *entry;
139
9784e579 140 entry = g_new0(QEMUPutKbdEntry, 1);
5a37532d
GH
141 entry->put_kbd = func;
142 entry->opaque = opaque;
9784e579
GH
143 entry->s = qemu_input_handler_register((DeviceState *)entry,
144 &legacy_kbd_handler);
7f5e07d9 145 qemu_input_handler_activate(entry->s);
5a37532d 146 return entry;
8f0056b7
PB
147}
148
edd85a3d
GH
149static void legacy_mouse_event(DeviceState *dev, QemuConsole *src,
150 InputEvent *evt)
151{
7fb1cf16 152 static const int bmap[INPUT_BUTTON__MAX] = {
edd85a3d
GH
153 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
154 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
155 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
156 };
157 QEMUPutMouseEntry *s = (QEMUPutMouseEntry *)dev;
b5a1b443
EB
158 InputBtnEvent *btn;
159 InputMoveEvent *move;
edd85a3d 160
568c73a4 161 switch (evt->type) {
edd85a3d 162 case INPUT_EVENT_KIND_BTN:
32bafa8f 163 btn = evt->u.btn.data;
b5a1b443
EB
164 if (btn->down) {
165 s->buttons |= bmap[btn->button];
edd85a3d 166 } else {
b5a1b443 167 s->buttons &= ~bmap[btn->button];
edd85a3d 168 }
b5a1b443 169 if (btn->down && btn->button == INPUT_BUTTON_WHEEL_UP) {
dbb2a132
GH
170 s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque,
171 s->axis[INPUT_AXIS_X],
172 s->axis[INPUT_AXIS_Y],
173 -1,
174 s->buttons);
175 }
b5a1b443 176 if (btn->down && btn->button == INPUT_BUTTON_WHEEL_DOWN) {
dbb2a132
GH
177 s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque,
178 s->axis[INPUT_AXIS_X],
179 s->axis[INPUT_AXIS_Y],
180 1,
181 s->buttons);
182 }
17f6315e
DP
183 if (btn->down && btn->button == INPUT_BUTTON_WHEEL_RIGHT) {
184 s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque,
185 s->axis[INPUT_AXIS_X],
186 s->axis[INPUT_AXIS_Y],
187 -2,
188 s->buttons);
189 }
190 if (btn->down && btn->button == INPUT_BUTTON_WHEEL_LEFT) {
191 s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque,
192 s->axis[INPUT_AXIS_X],
193 s->axis[INPUT_AXIS_Y],
194 2,
195 s->buttons);
196 }
edd85a3d
GH
197 break;
198 case INPUT_EVENT_KIND_ABS:
32bafa8f 199 move = evt->u.abs.data;
b5a1b443 200 s->axis[move->axis] = move->value;
edd85a3d
GH
201 break;
202 case INPUT_EVENT_KIND_REL:
32bafa8f 203 move = evt->u.rel.data;
b5a1b443 204 s->axis[move->axis] += move->value;
edd85a3d
GH
205 break;
206 default:
207 break;
208 }
209}
210
211static void legacy_mouse_sync(DeviceState *dev)
212{
213 QEMUPutMouseEntry *s = (QEMUPutMouseEntry *)dev;
214
215 s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque,
216 s->axis[INPUT_AXIS_X],
217 s->axis[INPUT_AXIS_Y],
218 0,
219 s->buttons);
220
221 if (!s->qemu_put_mouse_event_absolute) {
222 s->axis[INPUT_AXIS_X] = 0;
223 s->axis[INPUT_AXIS_Y] = 0;
224 }
225}
226
8f0056b7
PB
227QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
228 void *opaque, int absolute,
229 const char *name)
230{
6fef28ee 231 QEMUPutMouseEntry *s;
8f0056b7 232
fedf0d35 233 s = g_new0(QEMUPutMouseEntry, 1);
8f0056b7
PB
234
235 s->qemu_put_mouse_event = func;
236 s->qemu_put_mouse_event_opaque = opaque;
237 s->qemu_put_mouse_event_absolute = absolute;
8f0056b7 238
edd85a3d
GH
239 s->h.name = name;
240 s->h.mask = INPUT_EVENT_MASK_BTN |
241 (absolute ? INPUT_EVENT_MASK_ABS : INPUT_EVENT_MASK_REL);
242 s->h.event = legacy_mouse_event;
243 s->h.sync = legacy_mouse_sync;
244 s->s = qemu_input_handler_register((DeviceState *)s,
245 &s->h);
246
8f0056b7
PB
247 return s;
248}
249
6fef28ee 250void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
8f0056b7 251{
edd85a3d 252 qemu_input_handler_activate(entry->s);
6fef28ee 253}
8f0056b7 254
6fef28ee
AL
255void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
256{
edd85a3d
GH
257 qemu_input_handler_unregister(entry->s);
258
7267c094 259 g_free(entry);
8f0056b7
PB
260}
261
03a23a85
GH
262QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
263 void *opaque)
264{
265 QEMUPutLEDEntry *s;
266
fedf0d35 267 s = g_new0(QEMUPutLEDEntry, 1);
03a23a85
GH
268
269 s->put_led = func;
270 s->opaque = opaque;
271 QTAILQ_INSERT_TAIL(&led_handlers, s, next);
272 return s;
273}
274
275void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry)
276{
277 if (entry == NULL)
278 return;
279 QTAILQ_REMOVE(&led_handlers, entry, next);
7267c094 280 g_free(entry);
03a23a85
GH
281}
282
03a23a85
GH
283void kbd_put_ledstate(int ledstate)
284{
285 QEMUPutLEDEntry *cursor;
286
287 QTAILQ_FOREACH(cursor, &led_handlers, next) {
288 cursor->put_led(cursor->opaque, ledstate);
289 }
290}