blob: d2c3162f1ed616b18f2ce8d6796acfc61244177c [file] [log] [blame]
aurel32aa71cf82009-02-08 15:53:20 +00001/*
2 * QEMU Microsoft serial mouse emulation
3 *
4 * Copyright (c) 2008 Lubomir Rintel
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 */
Peter Maydell9c058332016-01-29 17:49:54 +000024#include "qemu/osdep.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010025#include "qemu-common.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020026#include "sysemu/char.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010027#include "ui/console.h"
Gerd Hoffmann96d7c072016-07-04 11:42:54 +020028#include "ui/input.h"
aurel32aa71cf82009-02-08 15:53:20 +000029
30#define MSMOUSE_LO6(n) ((n) & 0x3f)
31#define MSMOUSE_HI2(n) (((n) & 0xc0) >> 6)
32
Gerd Hoffmanncde8dcb2016-07-04 11:42:52 +020033typedef struct {
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +030034 Chardev parent;
Marc-André Lureau41ac54b2016-10-21 23:44:44 +030035
Gerd Hoffmann96d7c072016-07-04 11:42:54 +020036 QemuInputHandlerState *hs;
37 int axis[INPUT_AXIS__MAX];
38 bool btns[INPUT_BUTTON__MAX];
Gerd Hoffmannd7b7f5262016-07-04 11:42:55 +020039 bool btnc[INPUT_BUTTON__MAX];
Gerd Hoffmann57a4e3b2016-07-04 11:42:53 +020040 uint8_t outbuf[32];
41 int outlen;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +030042} MouseChardev;
Gerd Hoffmanncde8dcb2016-07-04 11:42:52 +020043
Marc-André Lureau777357d2016-12-07 18:39:10 +030044#define TYPE_CHARDEV_MSMOUSE "chardev-msmouse"
45#define MOUSE_CHARDEV(obj) \
46 OBJECT_CHECK(MouseChardev, (obj), TYPE_CHARDEV_MSMOUSE)
47
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +030048static void msmouse_chr_accept_input(Chardev *chr)
Gerd Hoffmann57a4e3b2016-07-04 11:42:53 +020049{
Marc-André Lureau777357d2016-12-07 18:39:10 +030050 MouseChardev *mouse = MOUSE_CHARDEV(chr);
Gerd Hoffmann57a4e3b2016-07-04 11:42:53 +020051 int len;
52
53 len = qemu_chr_be_can_write(chr);
54 if (len > mouse->outlen) {
55 len = mouse->outlen;
56 }
57 if (!len) {
58 return;
59 }
60
61 qemu_chr_be_write(chr, mouse->outbuf, len);
62 mouse->outlen -= len;
63 if (mouse->outlen) {
64 memmove(mouse->outbuf, mouse->outbuf + len, mouse->outlen);
65 }
66}
67
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +030068static void msmouse_queue_event(MouseChardev *mouse)
aurel32aa71cf82009-02-08 15:53:20 +000069{
aurel32aa71cf82009-02-08 15:53:20 +000070 unsigned char bytes[4] = { 0x40, 0x00, 0x00, 0x00 };
Gerd Hoffmannd7b7f5262016-07-04 11:42:55 +020071 int dx, dy, count = 3;
Gerd Hoffmann96d7c072016-07-04 11:42:54 +020072
73 dx = mouse->axis[INPUT_AXIS_X];
74 mouse->axis[INPUT_AXIS_X] = 0;
75
76 dy = mouse->axis[INPUT_AXIS_Y];
77 mouse->axis[INPUT_AXIS_Y] = 0;
aurel32aa71cf82009-02-08 15:53:20 +000078
79 /* Movement deltas */
80 bytes[0] |= (MSMOUSE_HI2(dy) << 2) | MSMOUSE_HI2(dx);
81 bytes[1] |= MSMOUSE_LO6(dx);
82 bytes[2] |= MSMOUSE_LO6(dy);
83
84 /* Buttons */
Gerd Hoffmann96d7c072016-07-04 11:42:54 +020085 bytes[0] |= (mouse->btns[INPUT_BUTTON_LEFT] ? 0x20 : 0x00);
86 bytes[0] |= (mouse->btns[INPUT_BUTTON_RIGHT] ? 0x10 : 0x00);
Gerd Hoffmannd7b7f5262016-07-04 11:42:55 +020087 if (mouse->btns[INPUT_BUTTON_MIDDLE] ||
88 mouse->btnc[INPUT_BUTTON_MIDDLE]) {
89 bytes[3] |= (mouse->btns[INPUT_BUTTON_MIDDLE] ? 0x20 : 0x00);
90 mouse->btnc[INPUT_BUTTON_MIDDLE] = false;
91 count = 4;
92 }
aurel32aa71cf82009-02-08 15:53:20 +000093
Gerd Hoffmannd7b7f5262016-07-04 11:42:55 +020094 if (mouse->outlen <= sizeof(mouse->outbuf) - count) {
95 memcpy(mouse->outbuf + mouse->outlen, bytes, count);
96 mouse->outlen += count;
Gerd Hoffmann57a4e3b2016-07-04 11:42:53 +020097 } else {
98 /* queue full -> drop event */
99 }
Gerd Hoffmann96d7c072016-07-04 11:42:54 +0200100}
Gerd Hoffmann57a4e3b2016-07-04 11:42:53 +0200101
Gerd Hoffmann96d7c072016-07-04 11:42:54 +0200102static void msmouse_input_event(DeviceState *dev, QemuConsole *src,
103 InputEvent *evt)
104{
Marc-André Lureau777357d2016-12-07 18:39:10 +0300105 MouseChardev *mouse = MOUSE_CHARDEV(dev);
Gerd Hoffmann96d7c072016-07-04 11:42:54 +0200106 InputMoveEvent *move;
107 InputBtnEvent *btn;
108
109 switch (evt->type) {
110 case INPUT_EVENT_KIND_REL:
111 move = evt->u.rel.data;
112 mouse->axis[move->axis] += move->value;
113 break;
114
115 case INPUT_EVENT_KIND_BTN:
116 btn = evt->u.btn.data;
117 mouse->btns[btn->button] = btn->down;
Gerd Hoffmannd7b7f5262016-07-04 11:42:55 +0200118 mouse->btnc[btn->button] = true;
Gerd Hoffmann96d7c072016-07-04 11:42:54 +0200119 break;
120
121 default:
122 /* keep gcc happy */
123 break;
124 }
125}
126
127static void msmouse_input_sync(DeviceState *dev)
128{
Marc-André Lureau777357d2016-12-07 18:39:10 +0300129 MouseChardev *mouse = MOUSE_CHARDEV(dev);
130 Chardev *chr = CHARDEV(dev);
Gerd Hoffmann96d7c072016-07-04 11:42:54 +0200131
132 msmouse_queue_event(mouse);
Marc-André Lureau41ac54b2016-10-21 23:44:44 +0300133 msmouse_chr_accept_input(chr);
aurel32aa71cf82009-02-08 15:53:20 +0000134}
135
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300136static int msmouse_chr_write(struct Chardev *s, const uint8_t *buf, int len)
aurel32aa71cf82009-02-08 15:53:20 +0000137{
138 /* Ignore writes to mouse port */
139 return len;
140}
141
Marc-André Lureau8955e892016-12-08 16:34:44 +0300142static void char_msmouse_finalize(Object *obj)
aurel32aa71cf82009-02-08 15:53:20 +0000143{
Marc-André Lureau8955e892016-12-08 16:34:44 +0300144 MouseChardev *mouse = MOUSE_CHARDEV(obj);
Gerd Hoffmanncde8dcb2016-07-04 11:42:52 +0200145
Gerd Hoffmann96d7c072016-07-04 11:42:54 +0200146 qemu_input_handler_unregister(mouse->hs);
aurel32aa71cf82009-02-08 15:53:20 +0000147}
148
Gerd Hoffmann96d7c072016-07-04 11:42:54 +0200149static QemuInputHandler msmouse_handler = {
150 .name = "QEMU Microsoft Mouse",
151 .mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_REL,
152 .event = msmouse_input_event,
153 .sync = msmouse_input_sync,
154};
155
Marc-André Lureau777357d2016-12-07 18:39:10 +0300156static void msmouse_chr_open(Chardev *chr,
157 ChardevBackend *backend,
158 bool *be_opened,
159 Error **errp)
aurel32aa71cf82009-02-08 15:53:20 +0000160{
Marc-André Lureau777357d2016-12-07 18:39:10 +0300161 MouseChardev *mouse = MOUSE_CHARDEV(chr);
aurel32aa71cf82009-02-08 15:53:20 +0000162
Marc-André Lureau82878da2016-10-22 13:09:43 +0300163 *be_opened = false;
Gerd Hoffmann96d7c072016-07-04 11:42:54 +0200164 mouse->hs = qemu_input_handler_register((DeviceState *)mouse,
165 &msmouse_handler);
aurel32aa71cf82009-02-08 15:53:20 +0000166}
Anthony Liguori5ab82112013-03-05 23:21:31 +0530167
Marc-André Lureau777357d2016-12-07 18:39:10 +0300168static void char_msmouse_class_init(ObjectClass *oc, void *data)
169{
170 ChardevClass *cc = CHARDEV_CLASS(oc);
171
172 cc->open = msmouse_chr_open;
173 cc->chr_write = msmouse_chr_write;
174 cc->chr_accept_input = msmouse_chr_accept_input;
Marc-André Lureau777357d2016-12-07 18:39:10 +0300175}
176
177static const TypeInfo char_msmouse_type_info = {
178 .name = TYPE_CHARDEV_MSMOUSE,
179 .parent = TYPE_CHARDEV,
180 .instance_size = sizeof(MouseChardev),
Marc-André Lureau8955e892016-12-08 16:34:44 +0300181 .instance_finalize = char_msmouse_finalize,
Marc-André Lureau777357d2016-12-07 18:39:10 +0300182 .class_init = char_msmouse_class_init,
183};
184
Anthony Liguori5ab82112013-03-05 23:21:31 +0530185static void register_types(void)
186{
Marc-André Lureau777357d2016-12-07 18:39:10 +0300187 type_register_static(&char_msmouse_type_info);
Anthony Liguori5ab82112013-03-05 23:21:31 +0530188}
189
190type_init(register_types);