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