blob: 549654e26a328ac93a6927fb0cc83c8724770535 [file] [log] [blame]
Paolo Bonzini8f0056b2010-01-13 14:05:34 +01001/*
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
Peter Maydelle16f4c82016-01-29 17:49:51 +000025#include "qemu/osdep.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010026#include "qapi/qapi-commands-ui.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010027#include "sysemu/sysemu.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010028#include "ui/console.h"
Michael S. Tsirkin0041e9a2018-05-03 22:51:00 +030029#include "keymaps.h"
Gerd Hoffmann9784e572013-11-27 11:59:25 +010030#include "ui/input.h"
Paolo Bonzini8f0056b2010-01-13 14:05:34 +010031
Gerd Hoffmann72711ef2013-04-24 12:08:37 +020032struct QEMUPutMouseEntry {
33 QEMUPutMouseEvent *qemu_put_mouse_event;
34 void *qemu_put_mouse_event_opaque;
35 int qemu_put_mouse_event_absolute;
Gerd Hoffmannedd85a32013-11-27 17:41:40 +010036
37 /* new input core */
38 QemuInputHandler h;
39 QemuInputHandlerState *s;
Eric Blake7fb1cf12015-11-18 01:52:57 -070040 int axis[INPUT_AXIS__MAX];
Gerd Hoffmannedd85a32013-11-27 17:41:40 +010041 int buttons;
Gerd Hoffmann72711ef2013-04-24 12:08:37 +020042};
43
Gerd Hoffmann5a375322013-04-24 12:08:38 +020044struct QEMUPutKbdEntry {
45 QEMUPutKBDEvent *put_kbd;
46 void *opaque;
Gerd Hoffmann9784e572013-11-27 11:59:25 +010047 QemuInputHandlerState *s;
Gerd Hoffmann5a375322013-04-24 12:08:38 +020048};
49
Gerd Hoffmann72711ef2013-04-24 12:08:37 +020050struct QEMUPutLEDEntry {
51 QEMUPutLEDEvent *put_led;
52 void *opaque;
53 QTAILQ_ENTRY(QEMUPutLEDEntry) next;
54};
55
Gerd Hoffmann5a375322013-04-24 12:08:38 +020056static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers =
57 QTAILQ_HEAD_INITIALIZER(led_handlers);
Paolo Bonzini8f0056b2010-01-13 14:05:34 +010058
Wolfgang Bumiller64ffbe02016-01-13 09:09:58 +010059int index_from_key(const char *key, size_t key_length)
Amos Kong1048c882012-08-31 10:56:25 +080060{
Luiz Capitulino9d537c92012-09-20 14:47:02 -030061 int i;
Amos Kong1048c882012-08-31 10:56:25 +080062
Markus Armbruster1c236ba2017-08-24 10:46:06 +020063 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +020064 if (!strncmp(key, QKeyCode_str(i), key_length) &&
65 !QKeyCode_str(i)[key_length]) {
Amos Kong1048c882012-08-31 10:56:25 +080066 break;
67 }
68 }
69
Eric Blake7fb1cf12015-11-18 01:52:57 -070070 /* Return Q_KEY_CODE__MAX if the key is invalid */
Amos Kong1048c882012-08-31 10:56:25 +080071 return i;
72}
73
Gerd Hoffmannce53f2f2014-04-29 13:17:43 +020074static KeyValue *copy_key_value(KeyValue *src)
75{
76 KeyValue *dst = g_new(KeyValue, 1);
77 memcpy(dst, src, sizeof(*src));
Daniel P. Berrange1d5b8d72017-10-19 15:28:41 +010078 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 }
Gerd Hoffmannce53f2f2014-04-29 13:17:43 +020083 return dst;
Amos Konge4c8f002012-08-31 10:56:26 +080084}
85
Luiz Capitulino9f328972012-09-20 14:19:47 -030086void qmp_send_key(KeyValueList *keys, bool has_hold_time, int64_t hold_time,
Amos Konge4c8f002012-08-31 10:56:26 +080087 Error **errp)
88{
Luiz Capitulino9f328972012-09-20 14:19:47 -030089 KeyValueList *p;
Gerd Hoffmanne37f2022014-09-26 10:02:16 +020090 KeyValue **up = NULL;
91 int count = 0;
Amos Konge4c8f002012-08-31 10:56:26 +080092
Amos Konge4c8f002012-08-31 10:56:26 +080093 if (!has_hold_time) {
Gerd Hoffmann2e377f12014-05-28 13:03:37 +020094 hold_time = 0; /* use default */
Amos Konge4c8f002012-08-31 10:56:26 +080095 }
96
97 for (p = keys; p != NULL; p = p->next) {
Gerd Hoffmannce53f2f2014-04-29 13:17:43 +020098 qemu_input_event_send_key(NULL, copy_key_value(p->value), true);
Gerd Hoffmann2e377f12014-05-28 13:03:37 +020099 qemu_input_event_send_key_delay(hold_time);
Gerd Hoffmanne37f2022014-09-26 10:02:16 +0200100 up = g_realloc(up, sizeof(*up) * (count+1));
101 up[count] = copy_key_value(p->value);
102 count++;
Amos Konge4c8f002012-08-31 10:56:26 +0800103 }
Gerd Hoffmanne37f2022014-09-26 10:02:16 +0200104 while (count) {
105 count--;
106 qemu_input_event_send_key(NULL, up[count], false);
Gerd Hoffmann2e377f12014-05-28 13:03:37 +0200107 qemu_input_event_send_key_delay(hold_time);
108 }
Gerd Hoffmanne37f2022014-09-26 10:02:16 +0200109 g_free(up);
Amos Konge4c8f002012-08-31 10:56:26 +0800110}
111
Gerd Hoffmann9784e572013-11-27 11:59:25 +0100112static void legacy_kbd_event(DeviceState *dev, QemuConsole *src,
113 InputEvent *evt)
114{
115 QEMUPutKbdEntry *entry = (QEMUPutKbdEntry *)dev;
Gerd Hoffmann02aa76c2014-03-11 12:15:39 +0100116 int scancodes[3], i, count;
Eric Blake32bafa82016-03-17 16:48:37 -0600117 InputKeyEvent *key = evt->u.key.data;
Gerd Hoffmann9784e572013-11-27 11:59:25 +0100118
119 if (!entry || !entry->put_kbd) {
120 return;
121 }
Eric Blakeb5a1b442016-03-03 09:16:49 -0700122 count = qemu_input_key_value_to_scancode(key->key,
123 key->down,
Gerd Hoffmann02aa76c2014-03-11 12:15:39 +0100124 scancodes);
125 for (i = 0; i < count; i++) {
126 entry->put_kbd(entry->opaque, scancodes[i]);
Gerd Hoffmann9784e572013-11-27 11:59:25 +0100127 }
Gerd Hoffmann9784e572013-11-27 11:59:25 +0100128}
129
130static QemuInputHandler legacy_kbd_handler = {
131 .name = "legacy-kbd",
132 .mask = INPUT_EVENT_MASK_KEY,
133 .event = legacy_kbd_event,
134};
135
Gerd Hoffmann5a375322013-04-24 12:08:38 +0200136QEMUPutKbdEntry *qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100137{
Gerd Hoffmann5a375322013-04-24 12:08:38 +0200138 QEMUPutKbdEntry *entry;
139
Gerd Hoffmann9784e572013-11-27 11:59:25 +0100140 entry = g_new0(QEMUPutKbdEntry, 1);
Gerd Hoffmann5a375322013-04-24 12:08:38 +0200141 entry->put_kbd = func;
142 entry->opaque = opaque;
Gerd Hoffmann9784e572013-11-27 11:59:25 +0100143 entry->s = qemu_input_handler_register((DeviceState *)entry,
144 &legacy_kbd_handler);
Gerd Hoffmann7f5e07d2014-03-11 14:08:31 +0100145 qemu_input_handler_activate(entry->s);
Gerd Hoffmann5a375322013-04-24 12:08:38 +0200146 return entry;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100147}
148
Gerd Hoffmannedd85a32013-11-27 17:41:40 +0100149static void legacy_mouse_event(DeviceState *dev, QemuConsole *src,
150 InputEvent *evt)
151{
Eric Blake7fb1cf12015-11-18 01:52:57 -0700152 static const int bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannedd85a32013-11-27 17:41:40 +0100153 [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;
Eric Blakeb5a1b442016-03-03 09:16:49 -0700158 InputBtnEvent *btn;
159 InputMoveEvent *move;
Gerd Hoffmannedd85a32013-11-27 17:41:40 +0100160
Eric Blake568c73a2015-10-26 16:34:58 -0600161 switch (evt->type) {
Gerd Hoffmannedd85a32013-11-27 17:41:40 +0100162 case INPUT_EVENT_KIND_BTN:
Eric Blake32bafa82016-03-17 16:48:37 -0600163 btn = evt->u.btn.data;
Eric Blakeb5a1b442016-03-03 09:16:49 -0700164 if (btn->down) {
165 s->buttons |= bmap[btn->button];
Gerd Hoffmannedd85a32013-11-27 17:41:40 +0100166 } else {
Eric Blakeb5a1b442016-03-03 09:16:49 -0700167 s->buttons &= ~bmap[btn->button];
Gerd Hoffmannedd85a32013-11-27 17:41:40 +0100168 }
Eric Blakeb5a1b442016-03-03 09:16:49 -0700169 if (btn->down && btn->button == INPUT_BUTTON_WHEEL_UP) {
Gerd Hoffmanndbb2a132014-03-10 09:31:01 +0100170 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 }
Eric Blakeb5a1b442016-03-03 09:16:49 -0700176 if (btn->down && btn->button == INPUT_BUTTON_WHEEL_DOWN) {
Gerd Hoffmanndbb2a132014-03-10 09:31:01 +0100177 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 }
Gerd Hoffmannedd85a32013-11-27 17:41:40 +0100183 break;
184 case INPUT_EVENT_KIND_ABS:
Eric Blake32bafa82016-03-17 16:48:37 -0600185 move = evt->u.abs.data;
Eric Blakeb5a1b442016-03-03 09:16:49 -0700186 s->axis[move->axis] = move->value;
Gerd Hoffmannedd85a32013-11-27 17:41:40 +0100187 break;
188 case INPUT_EVENT_KIND_REL:
Eric Blake32bafa82016-03-17 16:48:37 -0600189 move = evt->u.rel.data;
Eric Blakeb5a1b442016-03-03 09:16:49 -0700190 s->axis[move->axis] += move->value;
Gerd Hoffmannedd85a32013-11-27 17:41:40 +0100191 break;
192 default:
193 break;
194 }
195}
196
197static void legacy_mouse_sync(DeviceState *dev)
198{
199 QEMUPutMouseEntry *s = (QEMUPutMouseEntry *)dev;
200
201 s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque,
202 s->axis[INPUT_AXIS_X],
203 s->axis[INPUT_AXIS_Y],
204 0,
205 s->buttons);
206
207 if (!s->qemu_put_mouse_event_absolute) {
208 s->axis[INPUT_AXIS_X] = 0;
209 s->axis[INPUT_AXIS_Y] = 0;
210 }
211}
212
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100213QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
214 void *opaque, int absolute,
215 const char *name)
216{
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600217 QEMUPutMouseEntry *s;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100218
Markus Armbrusterfedf0d32015-11-03 17:12:03 +0100219 s = g_new0(QEMUPutMouseEntry, 1);
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100220
221 s->qemu_put_mouse_event = func;
222 s->qemu_put_mouse_event_opaque = opaque;
223 s->qemu_put_mouse_event_absolute = absolute;
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100224
Gerd Hoffmannedd85a32013-11-27 17:41:40 +0100225 s->h.name = name;
226 s->h.mask = INPUT_EVENT_MASK_BTN |
227 (absolute ? INPUT_EVENT_MASK_ABS : INPUT_EVENT_MASK_REL);
228 s->h.event = legacy_mouse_event;
229 s->h.sync = legacy_mouse_sync;
230 s->s = qemu_input_handler_register((DeviceState *)s,
231 &s->h);
232
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100233 return s;
234}
235
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600236void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
237{
Gerd Hoffmannedd85a32013-11-27 17:41:40 +0100238 qemu_input_handler_activate(entry->s);
Anthony Liguori6fef28e2010-03-09 20:52:22 -0600239}
240
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100241void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
242{
Gerd Hoffmannedd85a32013-11-27 17:41:40 +0100243 qemu_input_handler_unregister(entry->s);
244
Anthony Liguori7267c092011-08-20 22:09:37 -0500245 g_free(entry);
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100246}
247
Gerd Hoffmann03a23a82010-02-26 17:17:36 +0100248QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
249 void *opaque)
250{
251 QEMUPutLEDEntry *s;
252
Markus Armbrusterfedf0d32015-11-03 17:12:03 +0100253 s = g_new0(QEMUPutLEDEntry, 1);
Gerd Hoffmann03a23a82010-02-26 17:17:36 +0100254
255 s->put_led = func;
256 s->opaque = opaque;
257 QTAILQ_INSERT_TAIL(&led_handlers, s, next);
258 return s;
259}
260
261void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry)
262{
263 if (entry == NULL)
264 return;
265 QTAILQ_REMOVE(&led_handlers, entry, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500266 g_free(entry);
Gerd Hoffmann03a23a82010-02-26 17:17:36 +0100267}
268
Gerd Hoffmann03a23a82010-02-26 17:17:36 +0100269void kbd_put_ledstate(int ledstate)
270{
271 QEMUPutLEDEntry *cursor;
272
273 QTAILQ_FOREACH(cursor, &led_handlers, next) {
274 cursor->put_led(cursor->opaque, ledstate);
275 }
276}