2010-01-13 14:05:34 +01:00
|
|
|
/*
|
|
|
|
* QEMU System Emulator
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003-2008 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2012-12-17 18:20:04 +01:00
|
|
|
#include "sysemu/sysemu.h"
|
2012-12-17 18:19:49 +01:00
|
|
|
#include "monitor/monitor.h"
|
2012-11-28 12:06:30 +01:00
|
|
|
#include "ui/console.h"
|
2012-12-17 18:19:43 +01:00
|
|
|
#include "qapi/error.h"
|
2011-09-21 20:29:55 +02:00
|
|
|
#include "qmp-commands.h"
|
2012-08-31 04:56:26 +02:00
|
|
|
#include "qapi-types.h"
|
2013-05-16 07:19:47 +02:00
|
|
|
#include "ui/keymaps.h"
|
2013-11-27 11:59:25 +01:00
|
|
|
#include "ui/input.h"
|
2010-01-13 14:05:34 +01:00
|
|
|
|
2013-04-24 12:08:37 +02:00
|
|
|
struct QEMUPutMouseEntry {
|
|
|
|
QEMUPutMouseEvent *qemu_put_mouse_event;
|
|
|
|
void *qemu_put_mouse_event_opaque;
|
|
|
|
int qemu_put_mouse_event_absolute;
|
2013-11-27 17:41:40 +01:00
|
|
|
|
|
|
|
/* new input core */
|
|
|
|
QemuInputHandler h;
|
|
|
|
QemuInputHandlerState *s;
|
|
|
|
int axis[INPUT_AXIS_MAX];
|
|
|
|
int buttons;
|
2013-04-24 12:08:37 +02:00
|
|
|
};
|
|
|
|
|
2013-04-24 12:08:38 +02:00
|
|
|
struct QEMUPutKbdEntry {
|
|
|
|
QEMUPutKBDEvent *put_kbd;
|
|
|
|
void *opaque;
|
2013-11-27 11:59:25 +01:00
|
|
|
QemuInputHandlerState *s;
|
2013-04-24 12:08:38 +02:00
|
|
|
};
|
|
|
|
|
2013-04-24 12:08:37 +02:00
|
|
|
struct QEMUPutLEDEntry {
|
|
|
|
QEMUPutLEDEvent *put_led;
|
|
|
|
void *opaque;
|
|
|
|
QTAILQ_ENTRY(QEMUPutLEDEntry) next;
|
|
|
|
};
|
|
|
|
|
2013-04-24 12:08:38 +02:00
|
|
|
static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers =
|
|
|
|
QTAILQ_HEAD_INITIALIZER(led_handlers);
|
2010-03-10 03:52:22 +01:00
|
|
|
static QTAILQ_HEAD(, QEMUPutMouseEntry) mouse_handlers =
|
|
|
|
QTAILQ_HEAD_INITIALIZER(mouse_handlers);
|
2010-01-13 14:05:34 +01:00
|
|
|
|
2012-08-31 04:56:25 +02:00
|
|
|
int index_from_key(const char *key)
|
|
|
|
{
|
2012-09-20 19:47:02 +02:00
|
|
|
int i;
|
2012-08-31 04:56:25 +02:00
|
|
|
|
|
|
|
for (i = 0; QKeyCode_lookup[i] != NULL; i++) {
|
|
|
|
if (!strcmp(key, QKeyCode_lookup[i])) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return Q_KEY_CODE_MAX if the key is invalid */
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2014-04-29 13:17:43 +02:00
|
|
|
static KeyValue **keyvalues;
|
|
|
|
static int keyvalues_size;
|
2012-08-31 04:56:26 +02:00
|
|
|
static QEMUTimer *key_timer;
|
|
|
|
|
2014-04-29 13:17:43 +02:00
|
|
|
static void free_keyvalues(void)
|
2012-09-20 19:19:47 +02:00
|
|
|
{
|
2014-04-29 13:17:43 +02:00
|
|
|
g_free(keyvalues);
|
|
|
|
keyvalues = NULL;
|
|
|
|
keyvalues_size = 0;
|
2012-09-20 19:19:47 +02:00
|
|
|
}
|
|
|
|
|
2012-08-31 04:56:26 +02:00
|
|
|
static void release_keys(void *opaque)
|
|
|
|
{
|
2014-04-29 13:17:43 +02:00
|
|
|
while (keyvalues_size > 0) {
|
|
|
|
qemu_input_event_send_key(NULL, keyvalues[--keyvalues_size],
|
|
|
|
false);
|
2012-08-31 04:56:26 +02:00
|
|
|
}
|
2012-09-20 18:44:28 +02:00
|
|
|
|
2014-04-29 13:17:43 +02:00
|
|
|
free_keyvalues();
|
|
|
|
}
|
|
|
|
|
|
|
|
static KeyValue *copy_key_value(KeyValue *src)
|
|
|
|
{
|
|
|
|
KeyValue *dst = g_new(KeyValue, 1);
|
|
|
|
memcpy(dst, src, sizeof(*src));
|
|
|
|
return dst;
|
2012-08-31 04:56:26 +02:00
|
|
|
}
|
|
|
|
|
2012-09-20 19:19:47 +02:00
|
|
|
void qmp_send_key(KeyValueList *keys, bool has_hold_time, int64_t hold_time,
|
2012-08-31 04:56:26 +02:00
|
|
|
Error **errp)
|
|
|
|
{
|
2012-09-20 19:19:47 +02:00
|
|
|
KeyValueList *p;
|
2012-08-31 04:56:26 +02:00
|
|
|
|
|
|
|
if (!key_timer) {
|
2013-08-21 17:03:08 +02:00
|
|
|
key_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, release_keys, NULL);
|
2012-08-31 04:56:26 +02:00
|
|
|
}
|
|
|
|
|
2014-04-29 13:17:43 +02:00
|
|
|
if (keyvalues != NULL) {
|
2013-08-21 17:03:08 +02:00
|
|
|
timer_del(key_timer);
|
2012-08-31 04:56:26 +02:00
|
|
|
release_keys(NULL);
|
|
|
|
}
|
2012-09-20 18:44:28 +02:00
|
|
|
|
2012-08-31 04:56:26 +02:00
|
|
|
if (!has_hold_time) {
|
|
|
|
hold_time = 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (p = keys; p != NULL; p = p->next) {
|
2014-04-29 13:17:43 +02:00
|
|
|
qemu_input_event_send_key(NULL, copy_key_value(p->value), true);
|
2012-09-20 18:44:28 +02:00
|
|
|
|
2014-04-29 13:17:43 +02:00
|
|
|
keyvalues = g_realloc(keyvalues, sizeof(KeyValue *) *
|
|
|
|
(keyvalues_size + 1));
|
|
|
|
keyvalues[keyvalues_size++] = copy_key_value(p->value);
|
2012-08-31 04:56:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* delayed key up events */
|
2013-08-21 17:03:08 +02:00
|
|
|
timer_mod(key_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
|
2014-04-29 13:17:43 +02:00
|
|
|
muldiv64(get_ticks_per_sec(), hold_time, 1000));
|
2012-08-31 04:56:26 +02:00
|
|
|
}
|
|
|
|
|
2013-11-27 11:59:25 +01:00
|
|
|
static void legacy_kbd_event(DeviceState *dev, QemuConsole *src,
|
|
|
|
InputEvent *evt)
|
|
|
|
{
|
|
|
|
QEMUPutKbdEntry *entry = (QEMUPutKbdEntry *)dev;
|
2014-03-11 12:15:39 +01:00
|
|
|
int scancodes[3], i, count;
|
2013-11-27 11:59:25 +01:00
|
|
|
|
|
|
|
if (!entry || !entry->put_kbd) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-11 12:15:39 +01:00
|
|
|
count = qemu_input_key_value_to_scancode(evt->key->key,
|
|
|
|
evt->key->down,
|
|
|
|
scancodes);
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
entry->put_kbd(entry->opaque, scancodes[i]);
|
2013-11-27 11:59:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static QemuInputHandler legacy_kbd_handler = {
|
|
|
|
.name = "legacy-kbd",
|
|
|
|
.mask = INPUT_EVENT_MASK_KEY,
|
|
|
|
.event = legacy_kbd_event,
|
|
|
|
};
|
|
|
|
|
2013-04-24 12:08:38 +02:00
|
|
|
QEMUPutKbdEntry *qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
|
2010-01-13 14:05:34 +01:00
|
|
|
{
|
2013-04-24 12:08:38 +02:00
|
|
|
QEMUPutKbdEntry *entry;
|
|
|
|
|
2013-11-27 11:59:25 +01:00
|
|
|
entry = g_new0(QEMUPutKbdEntry, 1);
|
2013-04-24 12:08:38 +02:00
|
|
|
entry->put_kbd = func;
|
|
|
|
entry->opaque = opaque;
|
2013-11-27 11:59:25 +01:00
|
|
|
entry->s = qemu_input_handler_register((DeviceState *)entry,
|
|
|
|
&legacy_kbd_handler);
|
2014-03-11 14:08:31 +01:00
|
|
|
qemu_input_handler_activate(entry->s);
|
2013-04-24 12:08:38 +02:00
|
|
|
return entry;
|
2010-01-13 14:05:34 +01:00
|
|
|
}
|
|
|
|
|
2013-04-24 12:08:38 +02:00
|
|
|
void qemu_remove_kbd_event_handler(QEMUPutKbdEntry *entry)
|
2010-06-08 15:12:18 +02:00
|
|
|
{
|
2013-11-27 11:59:25 +01:00
|
|
|
qemu_input_handler_unregister(entry->s);
|
|
|
|
g_free(entry);
|
2010-06-08 15:12:18 +02:00
|
|
|
}
|
|
|
|
|
2013-11-27 17:41:40 +01:00
|
|
|
static void legacy_mouse_event(DeviceState *dev, QemuConsole *src,
|
|
|
|
InputEvent *evt)
|
|
|
|
{
|
|
|
|
static const int bmap[INPUT_BUTTON_MAX] = {
|
|
|
|
[INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
|
|
|
|
[INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
|
|
|
|
[INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
|
|
|
|
};
|
|
|
|
QEMUPutMouseEntry *s = (QEMUPutMouseEntry *)dev;
|
|
|
|
|
|
|
|
switch (evt->kind) {
|
|
|
|
case INPUT_EVENT_KIND_BTN:
|
|
|
|
if (evt->btn->down) {
|
|
|
|
s->buttons |= bmap[evt->btn->button];
|
|
|
|
} else {
|
|
|
|
s->buttons &= ~bmap[evt->btn->button];
|
|
|
|
}
|
2014-03-10 09:31:01 +01:00
|
|
|
if (evt->btn->down && evt->btn->button == INPUT_BUTTON_WHEEL_UP) {
|
|
|
|
s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque,
|
|
|
|
s->axis[INPUT_AXIS_X],
|
|
|
|
s->axis[INPUT_AXIS_Y],
|
|
|
|
-1,
|
|
|
|
s->buttons);
|
|
|
|
}
|
|
|
|
if (evt->btn->down && evt->btn->button == INPUT_BUTTON_WHEEL_DOWN) {
|
|
|
|
s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque,
|
|
|
|
s->axis[INPUT_AXIS_X],
|
|
|
|
s->axis[INPUT_AXIS_Y],
|
|
|
|
1,
|
|
|
|
s->buttons);
|
|
|
|
}
|
2013-11-27 17:41:40 +01:00
|
|
|
break;
|
|
|
|
case INPUT_EVENT_KIND_ABS:
|
|
|
|
s->axis[evt->abs->axis] = evt->abs->value;
|
|
|
|
break;
|
|
|
|
case INPUT_EVENT_KIND_REL:
|
|
|
|
s->axis[evt->rel->axis] += evt->rel->value;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void legacy_mouse_sync(DeviceState *dev)
|
|
|
|
{
|
|
|
|
QEMUPutMouseEntry *s = (QEMUPutMouseEntry *)dev;
|
|
|
|
|
|
|
|
s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque,
|
|
|
|
s->axis[INPUT_AXIS_X],
|
|
|
|
s->axis[INPUT_AXIS_Y],
|
|
|
|
0,
|
|
|
|
s->buttons);
|
|
|
|
|
|
|
|
if (!s->qemu_put_mouse_event_absolute) {
|
|
|
|
s->axis[INPUT_AXIS_X] = 0;
|
|
|
|
s->axis[INPUT_AXIS_Y] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-13 14:05:34 +01:00
|
|
|
QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
|
|
|
|
void *opaque, int absolute,
|
|
|
|
const char *name)
|
|
|
|
{
|
2010-03-10 03:52:22 +01:00
|
|
|
QEMUPutMouseEntry *s;
|
2010-01-13 14:05:34 +01:00
|
|
|
|
2011-08-21 05:09:37 +02:00
|
|
|
s = g_malloc0(sizeof(QEMUPutMouseEntry));
|
2010-01-13 14:05:34 +01:00
|
|
|
|
|
|
|
s->qemu_put_mouse_event = func;
|
|
|
|
s->qemu_put_mouse_event_opaque = opaque;
|
|
|
|
s->qemu_put_mouse_event_absolute = absolute;
|
|
|
|
|
2013-11-27 17:41:40 +01:00
|
|
|
s->h.name = name;
|
|
|
|
s->h.mask = INPUT_EVENT_MASK_BTN |
|
|
|
|
(absolute ? INPUT_EVENT_MASK_ABS : INPUT_EVENT_MASK_REL);
|
|
|
|
s->h.event = legacy_mouse_event;
|
|
|
|
s->h.sync = legacy_mouse_sync;
|
|
|
|
s->s = qemu_input_handler_register((DeviceState *)s,
|
|
|
|
&s->h);
|
|
|
|
|
2010-01-13 14:05:34 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2010-03-10 03:52:22 +01:00
|
|
|
void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
|
2010-01-13 14:05:34 +01:00
|
|
|
{
|
2013-11-27 17:41:40 +01:00
|
|
|
qemu_input_handler_activate(entry->s);
|
2010-03-10 03:52:22 +01:00
|
|
|
}
|
2010-01-13 14:05:34 +01:00
|
|
|
|
2010-03-10 03:52:22 +01:00
|
|
|
void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
|
|
|
|
{
|
2013-11-27 17:41:40 +01:00
|
|
|
qemu_input_handler_unregister(entry->s);
|
|
|
|
|
2011-08-21 05:09:37 +02:00
|
|
|
g_free(entry);
|
2010-01-13 14:05:34 +01:00
|
|
|
}
|
|
|
|
|
2010-02-26 17:17:36 +01:00
|
|
|
QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
QEMUPutLEDEntry *s;
|
|
|
|
|
2011-08-21 05:09:37 +02:00
|
|
|
s = g_malloc0(sizeof(QEMUPutLEDEntry));
|
2010-02-26 17:17:36 +01:00
|
|
|
|
|
|
|
s->put_led = func;
|
|
|
|
s->opaque = opaque;
|
|
|
|
QTAILQ_INSERT_TAIL(&led_handlers, s, next);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry)
|
|
|
|
{
|
|
|
|
if (entry == NULL)
|
|
|
|
return;
|
|
|
|
QTAILQ_REMOVE(&led_handlers, entry, next);
|
2011-08-21 05:09:37 +02:00
|
|
|
g_free(entry);
|
2010-02-26 17:17:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void kbd_put_ledstate(int ledstate)
|
|
|
|
{
|
|
|
|
QEMUPutLEDEntry *cursor;
|
|
|
|
|
|
|
|
QTAILQ_FOREACH(cursor, &led_handlers, next) {
|
|
|
|
cursor->put_led(cursor->opaque, ledstate);
|
|
|
|
}
|
|
|
|
}
|