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