input-legacy.c 7.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * 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.
 */

25
#include "sysemu/sysemu.h"
26
#include "ui/console.h"
27
#include "qapi/error.h"
L
Luiz Capitulino 已提交
28
#include "qmp-commands.h"
A
Amos Kong 已提交
29
#include "qapi-types.h"
30
#include "ui/keymaps.h"
31
#include "ui/input.h"
32

33 34 35 36
struct QEMUPutMouseEntry {
    QEMUPutMouseEvent *qemu_put_mouse_event;
    void *qemu_put_mouse_event_opaque;
    int qemu_put_mouse_event_absolute;
37 38 39 40 41 42

    /* new input core */
    QemuInputHandler h;
    QemuInputHandlerState *s;
    int axis[INPUT_AXIS_MAX];
    int buttons;
43 44
};

45 46 47
struct QEMUPutKbdEntry {
    QEMUPutKBDEvent *put_kbd;
    void *opaque;
48
    QemuInputHandlerState *s;
49 50
};

51 52 53 54 55 56
struct QEMUPutLEDEntry {
    QEMUPutLEDEvent *put_led;
    void *opaque;
    QTAILQ_ENTRY(QEMUPutLEDEntry) next;
};

57 58
static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers =
    QTAILQ_HEAD_INITIALIZER(led_handlers);
59

60 61
int index_from_key(const char *key)
{
62
    int i;
63 64 65 66 67 68 69 70 71 72 73

    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;
}

74 75 76 77 78
static KeyValue *copy_key_value(KeyValue *src)
{
    KeyValue *dst = g_new(KeyValue, 1);
    memcpy(dst, src, sizeof(*src));
    return dst;
A
Amos Kong 已提交
79 80
}

81
void qmp_send_key(KeyValueList *keys, bool has_hold_time, int64_t hold_time,
A
Amos Kong 已提交
82 83
                  Error **errp)
{
84
    KeyValueList *p;
85 86
    KeyValue **up = NULL;
    int count = 0;
A
Amos Kong 已提交
87 88

    if (!has_hold_time) {
89
        hold_time = 0; /* use default */
A
Amos Kong 已提交
90 91 92
    }

    for (p = keys; p != NULL; p = p->next) {
93
        qemu_input_event_send_key(NULL, copy_key_value(p->value), true);
94
        qemu_input_event_send_key_delay(hold_time);
95 96 97
        up = g_realloc(up, sizeof(*up) * (count+1));
        up[count] = copy_key_value(p->value);
        count++;
98
    }
99 100 101
    while (count) {
        count--;
        qemu_input_event_send_key(NULL, up[count], false);
102
        qemu_input_event_send_key_delay(hold_time);
A
Amos Kong 已提交
103
    }
104
    g_free(up);
A
Amos Kong 已提交
105 106
}

107 108 109 110
static void legacy_kbd_event(DeviceState *dev, QemuConsole *src,
                             InputEvent *evt)
{
    QEMUPutKbdEntry *entry = (QEMUPutKbdEntry *)dev;
G
Gerd Hoffmann 已提交
111
    int scancodes[3], i, count;
112 113 114 115

    if (!entry || !entry->put_kbd) {
        return;
    }
116 117
    count = qemu_input_key_value_to_scancode(evt->u.key->key,
                                             evt->u.key->down,
G
Gerd Hoffmann 已提交
118 119 120
                                             scancodes);
    for (i = 0; i < count; i++) {
        entry->put_kbd(entry->opaque, scancodes[i]);
121 122 123 124 125 126 127 128 129
    }
}

static QemuInputHandler legacy_kbd_handler = {
    .name  = "legacy-kbd",
    .mask  = INPUT_EVENT_MASK_KEY,
    .event = legacy_kbd_event,
};

130
QEMUPutKbdEntry *qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
131
{
132 133
    QEMUPutKbdEntry *entry;

134
    entry = g_new0(QEMUPutKbdEntry, 1);
135 136
    entry->put_kbd = func;
    entry->opaque = opaque;
137 138
    entry->s = qemu_input_handler_register((DeviceState *)entry,
                                           &legacy_kbd_handler);
G
Gerd Hoffmann 已提交
139
    qemu_input_handler_activate(entry->s);
140
    return entry;
141 142
}

143 144 145 146 147 148 149 150 151 152
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;

153
    switch (evt->type) {
154
    case INPUT_EVENT_KIND_BTN:
155 156
        if (evt->u.btn->down) {
            s->buttons |= bmap[evt->u.btn->button];
157
        } else {
158
            s->buttons &= ~bmap[evt->u.btn->button];
159
        }
160
        if (evt->u.btn->down && evt->u.btn->button == INPUT_BUTTON_WHEEL_UP) {
161 162 163 164 165 166
            s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque,
                                    s->axis[INPUT_AXIS_X],
                                    s->axis[INPUT_AXIS_Y],
                                    -1,
                                    s->buttons);
        }
167 168
        if (evt->u.btn->down &&
            evt->u.btn->button == INPUT_BUTTON_WHEEL_DOWN) {
169 170 171 172 173 174
            s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque,
                                    s->axis[INPUT_AXIS_X],
                                    s->axis[INPUT_AXIS_Y],
                                    1,
                                    s->buttons);
        }
175 176
        break;
    case INPUT_EVENT_KIND_ABS:
177
        s->axis[evt->u.abs->axis] = evt->u.abs->value;
178 179
        break;
    case INPUT_EVENT_KIND_REL:
180
        s->axis[evt->u.rel->axis] += evt->u.rel->value;
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
        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;
    }
}

203 204 205 206
QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
                                                void *opaque, int absolute,
                                                const char *name)
{
207
    QEMUPutMouseEntry *s;
208

209
    s = g_malloc0(sizeof(QEMUPutMouseEntry));
210 211 212 213 214

    s->qemu_put_mouse_event = func;
    s->qemu_put_mouse_event_opaque = opaque;
    s->qemu_put_mouse_event_absolute = absolute;

215 216 217 218 219 220 221 222
    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);

223 224 225
    return s;
}

226
void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
227
{
228
    qemu_input_handler_activate(entry->s);
229
}
230

231 232
void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
{
233 234
    qemu_input_handler_unregister(entry->s);

235
    g_free(entry);
236 237
}

G
Gerd Hoffmann 已提交
238 239 240 241 242
QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
                                            void *opaque)
{
    QEMUPutLEDEntry *s;

243
    s = g_malloc0(sizeof(QEMUPutLEDEntry));
G
Gerd Hoffmann 已提交
244 245 246 247 248 249 250 251 252 253 254 255

    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);
256
    g_free(entry);
G
Gerd Hoffmann 已提交
257 258 259 260 261 262 263 264 265 266
}

void kbd_put_ledstate(int ledstate)
{
    QEMUPutLEDEntry *cursor;

    QTAILQ_FOREACH(cursor, &led_handlers, next) {
        cursor->put_led(cursor->opaque, ledstate);
    }
}