msmouse.c 5.51 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
/*
 * QEMU Microsoft serial mouse emulation
 *
 * Copyright (c) 2008 Lubomir Rintel
 *
 * 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.
 */
24
#include "qemu/osdep.h"
25
#include "qemu-common.h"
26
#include "sysemu/char.h"
27
#include "ui/console.h"
28
#include "ui/input.h"
29 30 31 32

#define MSMOUSE_LO6(n) ((n) & 0x3f)
#define MSMOUSE_HI2(n) (((n) & 0xc0) >> 6)

33
typedef struct {
34
    Chardev parent;
35

36 37 38
    QemuInputHandlerState *hs;
    int axis[INPUT_AXIS__MAX];
    bool btns[INPUT_BUTTON__MAX];
39
    bool btnc[INPUT_BUTTON__MAX];
40 41
    uint8_t outbuf[32];
    int outlen;
42
} MouseChardev;
43

Marc-André Lureau's avatar
Marc-André Lureau committed
44 45 46 47
#define TYPE_CHARDEV_MSMOUSE "chardev-msmouse"
#define MOUSE_CHARDEV(obj)                                      \
    OBJECT_CHECK(MouseChardev, (obj), TYPE_CHARDEV_MSMOUSE)

48
static void msmouse_chr_accept_input(Chardev *chr)
49
{
Marc-André Lureau's avatar
Marc-André Lureau committed
50
    MouseChardev *mouse = MOUSE_CHARDEV(chr);
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    int len;

    len = qemu_chr_be_can_write(chr);
    if (len > mouse->outlen) {
        len = mouse->outlen;
    }
    if (!len) {
        return;
    }

    qemu_chr_be_write(chr, mouse->outbuf, len);
    mouse->outlen -= len;
    if (mouse->outlen) {
        memmove(mouse->outbuf, mouse->outbuf + len, mouse->outlen);
    }
}

68
static void msmouse_queue_event(MouseChardev *mouse)
69 70
{
    unsigned char bytes[4] = { 0x40, 0x00, 0x00, 0x00 };
71
    int dx, dy, count = 3;
72 73 74 75 76 77

    dx = mouse->axis[INPUT_AXIS_X];
    mouse->axis[INPUT_AXIS_X] = 0;

    dy = mouse->axis[INPUT_AXIS_Y];
    mouse->axis[INPUT_AXIS_Y] = 0;
78 79 80 81 82 83 84

    /* Movement deltas */
    bytes[0] |= (MSMOUSE_HI2(dy) << 2) | MSMOUSE_HI2(dx);
    bytes[1] |= MSMOUSE_LO6(dx);
    bytes[2] |= MSMOUSE_LO6(dy);

    /* Buttons */
85 86
    bytes[0] |= (mouse->btns[INPUT_BUTTON_LEFT]   ? 0x20 : 0x00);
    bytes[0] |= (mouse->btns[INPUT_BUTTON_RIGHT]  ? 0x10 : 0x00);
87 88 89 90 91 92 93 94 95 96
    if (mouse->btns[INPUT_BUTTON_MIDDLE] ||
        mouse->btnc[INPUT_BUTTON_MIDDLE]) {
        bytes[3] |= (mouse->btns[INPUT_BUTTON_MIDDLE] ? 0x20 : 0x00);
        mouse->btnc[INPUT_BUTTON_MIDDLE] = false;
        count = 4;
    }

    if (mouse->outlen <= sizeof(mouse->outbuf) - count) {
        memcpy(mouse->outbuf + mouse->outlen, bytes, count);
        mouse->outlen += count;
97 98 99
    } else {
        /* queue full -> drop event */
    }
100
}
101

102 103 104
static void msmouse_input_event(DeviceState *dev, QemuConsole *src,
                                InputEvent *evt)
{
Marc-André Lureau's avatar
Marc-André Lureau committed
105
    MouseChardev *mouse = MOUSE_CHARDEV(dev);
106 107 108 109 110 111 112 113 114 115 116 117
    InputMoveEvent *move;
    InputBtnEvent *btn;

    switch (evt->type) {
    case INPUT_EVENT_KIND_REL:
        move = evt->u.rel.data;
        mouse->axis[move->axis] += move->value;
        break;

    case INPUT_EVENT_KIND_BTN:
        btn = evt->u.btn.data;
        mouse->btns[btn->button] = btn->down;
118
        mouse->btnc[btn->button] = true;
119 120 121 122 123 124 125 126 127 128
        break;

    default:
        /* keep gcc happy */
        break;
    }
}

static void msmouse_input_sync(DeviceState *dev)
{
Marc-André Lureau's avatar
Marc-André Lureau committed
129 130
    MouseChardev *mouse = MOUSE_CHARDEV(dev);
    Chardev *chr = CHARDEV(dev);
131 132

    msmouse_queue_event(mouse);
133
    msmouse_chr_accept_input(chr);
134 135
}

136
static int msmouse_chr_write(struct Chardev *s, const uint8_t *buf, int len)
137 138 139 140 141
{
    /* Ignore writes to mouse port */
    return len;
}

142
static void char_msmouse_finalize(Object *obj)
143
{
144
    MouseChardev *mouse = MOUSE_CHARDEV(obj);
145

146
    qemu_input_handler_unregister(mouse->hs);
147 148
}

149 150 151 152 153 154 155
static QemuInputHandler msmouse_handler = {
    .name  = "QEMU Microsoft Mouse",
    .mask  = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_REL,
    .event = msmouse_input_event,
    .sync  = msmouse_input_sync,
};

Marc-André Lureau's avatar
Marc-André Lureau committed
156 157 158 159
static void msmouse_chr_open(Chardev *chr,
                             ChardevBackend *backend,
                             bool *be_opened,
                             Error **errp)
160
{
Marc-André Lureau's avatar
Marc-André Lureau committed
161
    MouseChardev *mouse = MOUSE_CHARDEV(chr);
162

163
    *be_opened = false;
164 165
    mouse->hs = qemu_input_handler_register((DeviceState *)mouse,
                                            &msmouse_handler);
Marc-André Lureau's avatar
Marc-André Lureau committed
166
}
167

Marc-André Lureau's avatar
Marc-André Lureau committed
168 169 170
static void char_msmouse_class_init(ObjectClass *oc, void *data)
{
    ChardevClass *cc = CHARDEV_CLASS(oc);
171

Marc-André Lureau's avatar
Marc-André Lureau committed
172 173 174
    cc->open = msmouse_chr_open;
    cc->chr_write = msmouse_chr_write;
    cc->chr_accept_input = msmouse_chr_accept_input;
175
}
176

Marc-André Lureau's avatar
Marc-André Lureau committed
177 178 179 180
static const TypeInfo char_msmouse_type_info = {
    .name = TYPE_CHARDEV_MSMOUSE,
    .parent = TYPE_CHARDEV,
    .instance_size = sizeof(MouseChardev),
181
    .instance_finalize = char_msmouse_finalize,
Marc-André Lureau's avatar
Marc-André Lureau committed
182 183 184
    .class_init = char_msmouse_class_init,
};

185 186
static void register_types(void)
{
187 188 189
    static const CharDriver driver = {
        .kind = CHARDEV_BACKEND_KIND_MSMOUSE,
    };
Marc-André Lureau's avatar
Marc-André Lureau committed
190

191
    register_char_driver(&driver);
Marc-André Lureau's avatar
Marc-André Lureau committed
192
    type_register_static(&char_msmouse_type_info);
193 194 195
}

type_init(register_types);