blob: eb9231dcdb988fa9c0ca2d61f2ae5ebfd932ab43 [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 */
Markus Armbruster0b8fa322019-05-23 16:35:07 +020024
Peter Maydell9c058332016-01-29 17:49:54 +000025#include "qemu/osdep.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020026#include "qemu/module.h"
Marc-André Lureau8228e352017-01-26 17:19:46 +040027#include "chardev/char.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010028#include "ui/console.h"
Gerd Hoffmann96d7c072016-07-04 11:42:54 +020029#include "ui/input.h"
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040030#include "qom/object.h"
aurel32aa71cf82009-02-08 15:53:20 +000031
32#define MSMOUSE_LO6(n) ((n) & 0x3f)
33#define MSMOUSE_HI2(n) (((n) & 0xc0) >> 6)
34
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040035struct MouseChardev {
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +030036 Chardev parent;
Marc-André Lureau41ac54b2016-10-21 23:44:44 +030037
Gerd Hoffmann96d7c072016-07-04 11:42:54 +020038 QemuInputHandlerState *hs;
39 int axis[INPUT_AXIS__MAX];
40 bool btns[INPUT_BUTTON__MAX];
Gerd Hoffmannd7b7f5262016-07-04 11:42:55 +020041 bool btnc[INPUT_BUTTON__MAX];
Gerd Hoffmann57a4e3b2016-07-04 11:42:53 +020042 uint8_t outbuf[32];
43 int outlen;
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040044};
45typedef struct MouseChardev MouseChardev;
Gerd Hoffmanncde8dcb2016-07-04 11:42:52 +020046
Marc-André Lureau777357d2016-12-07 18:39:10 +030047#define TYPE_CHARDEV_MSMOUSE "chardev-msmouse"
Eduardo Habkost8110fa12020-08-31 17:07:33 -040048DECLARE_INSTANCE_CHECKER(MouseChardev, MOUSE_CHARDEV,
49 TYPE_CHARDEV_MSMOUSE)
Marc-André Lureau777357d2016-12-07 18:39:10 +030050
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +030051static void msmouse_chr_accept_input(Chardev *chr)
Gerd Hoffmann57a4e3b2016-07-04 11:42:53 +020052{
Marc-André Lureau777357d2016-12-07 18:39:10 +030053 MouseChardev *mouse = MOUSE_CHARDEV(chr);
Gerd Hoffmann57a4e3b2016-07-04 11:42:53 +020054 int len;
55
56 len = qemu_chr_be_can_write(chr);
57 if (len > mouse->outlen) {
58 len = mouse->outlen;
59 }
60 if (!len) {
61 return;
62 }
63
64 qemu_chr_be_write(chr, mouse->outbuf, len);
65 mouse->outlen -= len;
66 if (mouse->outlen) {
67 memmove(mouse->outbuf, mouse->outbuf + len, mouse->outlen);
68 }
69}
70
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +030071static void msmouse_queue_event(MouseChardev *mouse)
aurel32aa71cf82009-02-08 15:53:20 +000072{
aurel32aa71cf82009-02-08 15:53:20 +000073 unsigned char bytes[4] = { 0x40, 0x00, 0x00, 0x00 };
Gerd Hoffmannd7b7f5262016-07-04 11:42:55 +020074 int dx, dy, count = 3;
Gerd Hoffmann96d7c072016-07-04 11:42:54 +020075
76 dx = mouse->axis[INPUT_AXIS_X];
77 mouse->axis[INPUT_AXIS_X] = 0;
78
79 dy = mouse->axis[INPUT_AXIS_Y];
80 mouse->axis[INPUT_AXIS_Y] = 0;
aurel32aa71cf82009-02-08 15:53:20 +000081
82 /* Movement deltas */
83 bytes[0] |= (MSMOUSE_HI2(dy) << 2) | MSMOUSE_HI2(dx);
84 bytes[1] |= MSMOUSE_LO6(dx);
85 bytes[2] |= MSMOUSE_LO6(dy);
86
87 /* Buttons */
Gerd Hoffmann96d7c072016-07-04 11:42:54 +020088 bytes[0] |= (mouse->btns[INPUT_BUTTON_LEFT] ? 0x20 : 0x00);
89 bytes[0] |= (mouse->btns[INPUT_BUTTON_RIGHT] ? 0x10 : 0x00);
Gerd Hoffmannd7b7f5262016-07-04 11:42:55 +020090 if (mouse->btns[INPUT_BUTTON_MIDDLE] ||
91 mouse->btnc[INPUT_BUTTON_MIDDLE]) {
92 bytes[3] |= (mouse->btns[INPUT_BUTTON_MIDDLE] ? 0x20 : 0x00);
93 mouse->btnc[INPUT_BUTTON_MIDDLE] = false;
94 count = 4;
95 }
aurel32aa71cf82009-02-08 15:53:20 +000096
Gerd Hoffmannd7b7f5262016-07-04 11:42:55 +020097 if (mouse->outlen <= sizeof(mouse->outbuf) - count) {
98 memcpy(mouse->outbuf + mouse->outlen, bytes, count);
99 mouse->outlen += count;
Gerd Hoffmann57a4e3b2016-07-04 11:42:53 +0200100 } else {
101 /* queue full -> drop event */
102 }
Gerd Hoffmann96d7c072016-07-04 11:42:54 +0200103}
Gerd Hoffmann57a4e3b2016-07-04 11:42:53 +0200104
Gerd Hoffmann96d7c072016-07-04 11:42:54 +0200105static void msmouse_input_event(DeviceState *dev, QemuConsole *src,
106 InputEvent *evt)
107{
Marc-André Lureau777357d2016-12-07 18:39:10 +0300108 MouseChardev *mouse = MOUSE_CHARDEV(dev);
Gerd Hoffmann96d7c072016-07-04 11:42:54 +0200109 InputMoveEvent *move;
110 InputBtnEvent *btn;
111
112 switch (evt->type) {
113 case INPUT_EVENT_KIND_REL:
114 move = evt->u.rel.data;
115 mouse->axis[move->axis] += move->value;
116 break;
117
118 case INPUT_EVENT_KIND_BTN:
119 btn = evt->u.btn.data;
120 mouse->btns[btn->button] = btn->down;
Gerd Hoffmannd7b7f5262016-07-04 11:42:55 +0200121 mouse->btnc[btn->button] = true;
Gerd Hoffmann96d7c072016-07-04 11:42:54 +0200122 break;
123
124 default:
125 /* keep gcc happy */
126 break;
127 }
128}
129
130static void msmouse_input_sync(DeviceState *dev)
131{
Marc-André Lureau777357d2016-12-07 18:39:10 +0300132 MouseChardev *mouse = MOUSE_CHARDEV(dev);
133 Chardev *chr = CHARDEV(dev);
Gerd Hoffmann96d7c072016-07-04 11:42:54 +0200134
135 msmouse_queue_event(mouse);
Marc-André Lureau41ac54b2016-10-21 23:44:44 +0300136 msmouse_chr_accept_input(chr);
aurel32aa71cf82009-02-08 15:53:20 +0000137}
138
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300139static int msmouse_chr_write(struct Chardev *s, const uint8_t *buf, int len)
aurel32aa71cf82009-02-08 15:53:20 +0000140{
141 /* Ignore writes to mouse port */
142 return len;
143}
144
Marc-André Lureau8955e892016-12-08 16:34:44 +0300145static void char_msmouse_finalize(Object *obj)
aurel32aa71cf82009-02-08 15:53:20 +0000146{
Marc-André Lureau8955e892016-12-08 16:34:44 +0300147 MouseChardev *mouse = MOUSE_CHARDEV(obj);
Gerd Hoffmanncde8dcb2016-07-04 11:42:52 +0200148
Gerd Hoffmann96d7c072016-07-04 11:42:54 +0200149 qemu_input_handler_unregister(mouse->hs);
aurel32aa71cf82009-02-08 15:53:20 +0000150}
151
Gerd Hoffmann96d7c072016-07-04 11:42:54 +0200152static QemuInputHandler msmouse_handler = {
153 .name = "QEMU Microsoft Mouse",
154 .mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_REL,
155 .event = msmouse_input_event,
156 .sync = msmouse_input_sync,
157};
158
Marc-André Lureau777357d2016-12-07 18:39:10 +0300159static void msmouse_chr_open(Chardev *chr,
160 ChardevBackend *backend,
161 bool *be_opened,
162 Error **errp)
aurel32aa71cf82009-02-08 15:53:20 +0000163{
Marc-André Lureau777357d2016-12-07 18:39:10 +0300164 MouseChardev *mouse = MOUSE_CHARDEV(chr);
aurel32aa71cf82009-02-08 15:53:20 +0000165
Marc-André Lureau82878da2016-10-22 13:09:43 +0300166 *be_opened = false;
Gerd Hoffmann96d7c072016-07-04 11:42:54 +0200167 mouse->hs = qemu_input_handler_register((DeviceState *)mouse,
168 &msmouse_handler);
aurel32aa71cf82009-02-08 15:53:20 +0000169}
Anthony Liguori5ab82112013-03-05 23:21:31 +0530170
Marc-André Lureau777357d2016-12-07 18:39:10 +0300171static void char_msmouse_class_init(ObjectClass *oc, void *data)
172{
173 ChardevClass *cc = CHARDEV_CLASS(oc);
174
175 cc->open = msmouse_chr_open;
176 cc->chr_write = msmouse_chr_write;
177 cc->chr_accept_input = msmouse_chr_accept_input;
Marc-André Lureau777357d2016-12-07 18:39:10 +0300178}
179
180static const TypeInfo char_msmouse_type_info = {
181 .name = TYPE_CHARDEV_MSMOUSE,
182 .parent = TYPE_CHARDEV,
183 .instance_size = sizeof(MouseChardev),
Marc-André Lureau8955e892016-12-08 16:34:44 +0300184 .instance_finalize = char_msmouse_finalize,
Marc-André Lureau777357d2016-12-07 18:39:10 +0300185 .class_init = char_msmouse_class_init,
186};
187
Anthony Liguori5ab82112013-03-05 23:21:31 +0530188static void register_types(void)
189{
Marc-André Lureau777357d2016-12-07 18:39:10 +0300190 type_register_static(&char_msmouse_type_info);
Anthony Liguori5ab82112013-03-05 23:21:31 +0530191}
192
193type_init(register_types);