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