input-legacy.c 8.5 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 "monitor/monitor.h"
27
#include "ui/console.h"
28
#include "qapi/error.h"
L
Luiz Capitulino 已提交
29
#include "qmp-commands.h"
A
Amos Kong 已提交
30
#include "qapi-types.h"
31
#include "ui/keymaps.h"
32
#include "ui/input.h"
33

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

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

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

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

58 59
static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers =
    QTAILQ_HEAD_INITIALIZER(led_handlers);
60 61
static QTAILQ_HEAD(, QEMUPutMouseEntry) mouse_handlers =
    QTAILQ_HEAD_INITIALIZER(mouse_handlers);
62

63 64
int index_from_key(const char *key)
{
65
    int i;
66 67 68 69 70 71 72 73 74 75 76

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

77 78
static int *keycodes;
static int keycodes_size;
A
Amos Kong 已提交
79 80
static QEMUTimer *key_timer;

81 82 83 84 85 86 87
static void free_keycodes(void)
{
    g_free(keycodes);
    keycodes = NULL;
    keycodes_size = 0;
}

A
Amos Kong 已提交
88 89
static void release_keys(void *opaque)
{
90
    while (keycodes_size > 0) {
91 92
        qemu_input_event_send_key_number(NULL, keycodes[--keycodes_size],
                                         false);
A
Amos Kong 已提交
93
    }
94

95
    free_keycodes();
A
Amos Kong 已提交
96 97
}

98
void qmp_send_key(KeyValueList *keys, bool has_hold_time, int64_t hold_time,
A
Amos Kong 已提交
99 100 101
                  Error **errp)
{
    int keycode;
102
    KeyValueList *p;
A
Amos Kong 已提交
103 104

    if (!key_timer) {
105
        key_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, release_keys, NULL);
A
Amos Kong 已提交
106 107 108
    }

    if (keycodes != NULL) {
109
        timer_del(key_timer);
A
Amos Kong 已提交
110 111
        release_keys(NULL);
    }
112

A
Amos Kong 已提交
113 114 115 116 117 118
    if (!has_hold_time) {
        hold_time = 100;
    }

    for (p = keys; p != NULL; p = p->next) {
        /* key down events */
G
Gerd Hoffmann 已提交
119
        keycode = qemu_input_key_value_to_number(p->value);
120
        if (keycode < 0x01 || keycode > 0xff) {
121
            error_setg(errp, "invalid hex keycode 0x%x", keycode);
122 123 124 125
            free_keycodes();
            return;
        }

126
        qemu_input_event_send_key_number(NULL, keycode, true);
127 128 129

        keycodes = g_realloc(keycodes, sizeof(int) * (keycodes_size + 1));
        keycodes[keycodes_size++] = keycode;
A
Amos Kong 已提交
130 131 132
    }

    /* delayed key up events */
133
    timer_mod(key_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
A
Amos Kong 已提交
134 135 136
                   muldiv64(get_ticks_per_sec(), hold_time, 1000));
}

137 138 139 140
static void legacy_kbd_event(DeviceState *dev, QemuConsole *src,
                             InputEvent *evt)
{
    QEMUPutKbdEntry *entry = (QEMUPutKbdEntry *)dev;
G
Gerd Hoffmann 已提交
141
    int scancodes[3], i, count;
142 143 144 145

    if (!entry || !entry->put_kbd) {
        return;
    }
G
Gerd Hoffmann 已提交
146 147 148 149 150
    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]);
151 152 153 154 155 156 157 158 159
    }
}

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

160
QEMUPutKbdEntry *qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
161
{
162 163
    QEMUPutKbdEntry *entry;

164
    entry = g_new0(QEMUPutKbdEntry, 1);
165 166
    entry->put_kbd = func;
    entry->opaque = opaque;
167 168
    entry->s = qemu_input_handler_register((DeviceState *)entry,
                                           &legacy_kbd_handler);
G
Gerd Hoffmann 已提交
169
    qemu_input_handler_activate(entry->s);
170
    return entry;
171 172
}

173
void qemu_remove_kbd_event_handler(QEMUPutKbdEntry *entry)
174
{
175 176
    qemu_input_handler_unregister(entry->s);
    g_free(entry);
177 178
}

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
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];
        }
196 197 198 199 200 201 202 203 204 205 206 207 208 209
        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);
        }
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
        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;
    }
}

238 239 240 241
QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
                                                void *opaque, int absolute,
                                                const char *name)
{
242
    QEMUPutMouseEntry *s;
243

244
    s = g_malloc0(sizeof(QEMUPutMouseEntry));
245 246 247 248 249

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

250 251 252 253 254 255 256 257
    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);

258 259 260
    return s;
}

261
void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
262
{
263
    qemu_input_handler_activate(entry->s);
264
}
265

266 267
void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
{
268 269
    qemu_input_handler_unregister(entry->s);

270
    g_free(entry);
271 272
}

G
Gerd Hoffmann 已提交
273 274 275 276 277
QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
                                            void *opaque)
{
    QEMUPutLEDEntry *s;

278
    s = g_malloc0(sizeof(QEMUPutLEDEntry));
G
Gerd Hoffmann 已提交
279 280 281 282 283 284 285 286 287 288 289 290

    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);
291
    g_free(entry);
G
Gerd Hoffmann 已提交
292 293 294 295 296 297 298 299 300 301
}

void kbd_put_ledstate(int ledstate)
{
    QEMUPutLEDEntry *cursor;

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