]> git.proxmox.com Git - mirror_qemu.git/blame - ui/input-legacy.c
Merge remote-tracking branch 'remotes/maxreitz/tags/pull-block-for-peter-2016-02...
[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
9c17d615 25#include "sysemu/sysemu.h"
28ecbaee 26#include "ui/console.h"
7b1b5d19 27#include "qapi/error.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
1048c88f
AK
60int index_from_key(const char *key)
61{
9d537c90 62 int i;
1048c88f
AK
63
64 for (i = 0; QKeyCode_lookup[i] != NULL; i++) {
65 if (!strcmp(key, QKeyCode_lookup[i])) {
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));
78 return dst;
e4c8f004
AK
79}
80
9f328977 81void qmp_send_key(KeyValueList *keys, bool has_hold_time, int64_t hold_time,
e4c8f004
AK
82 Error **errp)
83{
9f328977 84 KeyValueList *p;
e37f2024
GH
85 KeyValue **up = NULL;
86 int count = 0;
e4c8f004 87
e4c8f004 88 if (!has_hold_time) {
2e377f17 89 hold_time = 0; /* use default */
e4c8f004
AK
90 }
91
92 for (p = keys; p != NULL; p = p->next) {
ce53f2f9 93 qemu_input_event_send_key(NULL, copy_key_value(p->value), true);
2e377f17 94 qemu_input_event_send_key_delay(hold_time);
e37f2024
GH
95 up = g_realloc(up, sizeof(*up) * (count+1));
96 up[count] = copy_key_value(p->value);
97 count++;
2e377f17 98 }
e37f2024
GH
99 while (count) {
100 count--;
101 qemu_input_event_send_key(NULL, up[count], false);
2e377f17 102 qemu_input_event_send_key_delay(hold_time);
e4c8f004 103 }
e37f2024 104 g_free(up);
e4c8f004
AK
105}
106
9784e579
GH
107static void legacy_kbd_event(DeviceState *dev, QemuConsole *src,
108 InputEvent *evt)
109{
110 QEMUPutKbdEntry *entry = (QEMUPutKbdEntry *)dev;
02aa76c2 111 int scancodes[3], i, count;
9784e579
GH
112
113 if (!entry || !entry->put_kbd) {
114 return;
115 }
568c73a4
EB
116 count = qemu_input_key_value_to_scancode(evt->u.key->key,
117 evt->u.key->down,
02aa76c2
GH
118 scancodes);
119 for (i = 0; i < count; i++) {
120 entry->put_kbd(entry->opaque, scancodes[i]);
9784e579 121 }
9784e579
GH
122}
123
124static QemuInputHandler legacy_kbd_handler = {
125 .name = "legacy-kbd",
126 .mask = INPUT_EVENT_MASK_KEY,
127 .event = legacy_kbd_event,
128};
129
5a37532d 130QEMUPutKbdEntry *qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
8f0056b7 131{
5a37532d
GH
132 QEMUPutKbdEntry *entry;
133
9784e579 134 entry = g_new0(QEMUPutKbdEntry, 1);
5a37532d
GH
135 entry->put_kbd = func;
136 entry->opaque = opaque;
9784e579
GH
137 entry->s = qemu_input_handler_register((DeviceState *)entry,
138 &legacy_kbd_handler);
7f5e07d9 139 qemu_input_handler_activate(entry->s);
5a37532d 140 return entry;
8f0056b7
PB
141}
142
edd85a3d
GH
143static void legacy_mouse_event(DeviceState *dev, QemuConsole *src,
144 InputEvent *evt)
145{
7fb1cf16 146 static const int bmap[INPUT_BUTTON__MAX] = {
edd85a3d
GH
147 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
148 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
149 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
150 };
151 QEMUPutMouseEntry *s = (QEMUPutMouseEntry *)dev;
152
568c73a4 153 switch (evt->type) {
edd85a3d 154 case INPUT_EVENT_KIND_BTN:
568c73a4
EB
155 if (evt->u.btn->down) {
156 s->buttons |= bmap[evt->u.btn->button];
edd85a3d 157 } else {
568c73a4 158 s->buttons &= ~bmap[evt->u.btn->button];
edd85a3d 159 }
d20a580b 160 if (evt->u.btn->down && evt->u.btn->button == INPUT_BUTTON_WHEELUP) {
dbb2a132
GH
161 s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque,
162 s->axis[INPUT_AXIS_X],
163 s->axis[INPUT_AXIS_Y],
164 -1,
165 s->buttons);
166 }
568c73a4 167 if (evt->u.btn->down &&
d20a580b 168 evt->u.btn->button == INPUT_BUTTON_WHEELDOWN) {
dbb2a132
GH
169 s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque,
170 s->axis[INPUT_AXIS_X],
171 s->axis[INPUT_AXIS_Y],
172 1,
173 s->buttons);
174 }
edd85a3d
GH
175 break;
176 case INPUT_EVENT_KIND_ABS:
568c73a4 177 s->axis[evt->u.abs->axis] = evt->u.abs->value;
edd85a3d
GH
178 break;
179 case INPUT_EVENT_KIND_REL:
568c73a4 180 s->axis[evt->u.rel->axis] += evt->u.rel->value;
edd85a3d
GH
181 break;
182 default:
183 break;
184 }
185}
186
187static void legacy_mouse_sync(DeviceState *dev)
188{
189 QEMUPutMouseEntry *s = (QEMUPutMouseEntry *)dev;
190
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 0,
195 s->buttons);
196
197 if (!s->qemu_put_mouse_event_absolute) {
198 s->axis[INPUT_AXIS_X] = 0;
199 s->axis[INPUT_AXIS_Y] = 0;
200 }
201}
202
8f0056b7
PB
203QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
204 void *opaque, int absolute,
205 const char *name)
206{
6fef28ee 207 QEMUPutMouseEntry *s;
8f0056b7 208
fedf0d35 209 s = g_new0(QEMUPutMouseEntry, 1);
8f0056b7
PB
210
211 s->qemu_put_mouse_event = func;
212 s->qemu_put_mouse_event_opaque = opaque;
213 s->qemu_put_mouse_event_absolute = absolute;
8f0056b7 214
edd85a3d
GH
215 s->h.name = name;
216 s->h.mask = INPUT_EVENT_MASK_BTN |
217 (absolute ? INPUT_EVENT_MASK_ABS : INPUT_EVENT_MASK_REL);
218 s->h.event = legacy_mouse_event;
219 s->h.sync = legacy_mouse_sync;
220 s->s = qemu_input_handler_register((DeviceState *)s,
221 &s->h);
222
8f0056b7
PB
223 return s;
224}
225
6fef28ee 226void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
8f0056b7 227{
edd85a3d 228 qemu_input_handler_activate(entry->s);
6fef28ee 229}
8f0056b7 230
6fef28ee
AL
231void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
232{
edd85a3d
GH
233 qemu_input_handler_unregister(entry->s);
234
7267c094 235 g_free(entry);
8f0056b7
PB
236}
237
03a23a85
GH
238QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
239 void *opaque)
240{
241 QEMUPutLEDEntry *s;
242
fedf0d35 243 s = g_new0(QEMUPutLEDEntry, 1);
03a23a85
GH
244
245 s->put_led = func;
246 s->opaque = opaque;
247 QTAILQ_INSERT_TAIL(&led_handlers, s, next);
248 return s;
249}
250
251void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry)
252{
253 if (entry == NULL)
254 return;
255 QTAILQ_REMOVE(&led_handlers, entry, next);
7267c094 256 g_free(entry);
03a23a85
GH
257}
258
03a23a85
GH
259void kbd_put_ledstate(int ledstate)
260{
261 QEMUPutLEDEntry *cursor;
262
263 QTAILQ_FOREACH(cursor, &led_handlers, next) {
264 cursor->put_led(cursor->opaque, ledstate);
265 }
266}