]> git.ipfire.org Git - thirdparty/qemu.git/blame - chardev/msmouse.c
Merge remote-tracking branch 'remotes/bonzini/tags/for-upstream' into staging
[thirdparty/qemu.git] / chardev / msmouse.c
CommitLineData
aa71cf80
AJ
1/*
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 */
0b8fa32f 24
9c058332 25#include "qemu/osdep.h"
0b8fa32f 26#include "qemu/module.h"
8228e353 27#include "chardev/char.h"
28ecbaee 28#include "ui/console.h"
96d7c072 29#include "ui/input.h"
aa71cf80
AJ
30
31#define MSMOUSE_LO6(n) ((n) & 0x3f)
32#define MSMOUSE_HI2(n) (((n) & 0xc0) >> 6)
33
cde8dcbc 34typedef struct {
0ec7b3e7 35 Chardev parent;
41ac54b2 36
96d7c072
GH
37 QemuInputHandlerState *hs;
38 int axis[INPUT_AXIS__MAX];
39 bool btns[INPUT_BUTTON__MAX];
d7b7f526 40 bool btnc[INPUT_BUTTON__MAX];
57a4e3b9
GH
41 uint8_t outbuf[32];
42 int outlen;
0ec7b3e7 43} MouseChardev;
cde8dcbc 44
777357d7
MAL
45#define TYPE_CHARDEV_MSMOUSE "chardev-msmouse"
46#define MOUSE_CHARDEV(obj) \
47 OBJECT_CHECK(MouseChardev, (obj), TYPE_CHARDEV_MSMOUSE)
48
0ec7b3e7 49static void msmouse_chr_accept_input(Chardev *chr)
57a4e3b9 50{
777357d7 51 MouseChardev *mouse = MOUSE_CHARDEV(chr);
57a4e3b9
GH
52 int len;
53
54 len = qemu_chr_be_can_write(chr);
55 if (len > mouse->outlen) {
56 len = mouse->outlen;
57 }
58 if (!len) {
59 return;
60 }
61
62 qemu_chr_be_write(chr, mouse->outbuf, len);
63 mouse->outlen -= len;
64 if (mouse->outlen) {
65 memmove(mouse->outbuf, mouse->outbuf + len, mouse->outlen);
66 }
67}
68
0ec7b3e7 69static void msmouse_queue_event(MouseChardev *mouse)
aa71cf80 70{
aa71cf80 71 unsigned char bytes[4] = { 0x40, 0x00, 0x00, 0x00 };
d7b7f526 72 int dx, dy, count = 3;
96d7c072
GH
73
74 dx = mouse->axis[INPUT_AXIS_X];
75 mouse->axis[INPUT_AXIS_X] = 0;
76
77 dy = mouse->axis[INPUT_AXIS_Y];
78 mouse->axis[INPUT_AXIS_Y] = 0;
aa71cf80
AJ
79
80 /* Movement deltas */
81 bytes[0] |= (MSMOUSE_HI2(dy) << 2) | MSMOUSE_HI2(dx);
82 bytes[1] |= MSMOUSE_LO6(dx);
83 bytes[2] |= MSMOUSE_LO6(dy);
84
85 /* Buttons */
96d7c072
GH
86 bytes[0] |= (mouse->btns[INPUT_BUTTON_LEFT] ? 0x20 : 0x00);
87 bytes[0] |= (mouse->btns[INPUT_BUTTON_RIGHT] ? 0x10 : 0x00);
d7b7f526
GH
88 if (mouse->btns[INPUT_BUTTON_MIDDLE] ||
89 mouse->btnc[INPUT_BUTTON_MIDDLE]) {
90 bytes[3] |= (mouse->btns[INPUT_BUTTON_MIDDLE] ? 0x20 : 0x00);
91 mouse->btnc[INPUT_BUTTON_MIDDLE] = false;
92 count = 4;
93 }
94
95 if (mouse->outlen <= sizeof(mouse->outbuf) - count) {
96 memcpy(mouse->outbuf + mouse->outlen, bytes, count);
97 mouse->outlen += count;
57a4e3b9
GH
98 } else {
99 /* queue full -> drop event */
100 }
96d7c072 101}
57a4e3b9 102
96d7c072
GH
103static void msmouse_input_event(DeviceState *dev, QemuConsole *src,
104 InputEvent *evt)
105{
777357d7 106 MouseChardev *mouse = MOUSE_CHARDEV(dev);
96d7c072
GH
107 InputMoveEvent *move;
108 InputBtnEvent *btn;
109
110 switch (evt->type) {
111 case INPUT_EVENT_KIND_REL:
112 move = evt->u.rel.data;
113 mouse->axis[move->axis] += move->value;
114 break;
115
116 case INPUT_EVENT_KIND_BTN:
117 btn = evt->u.btn.data;
118 mouse->btns[btn->button] = btn->down;
d7b7f526 119 mouse->btnc[btn->button] = true;
96d7c072
GH
120 break;
121
122 default:
123 /* keep gcc happy */
124 break;
125 }
126}
127
128static void msmouse_input_sync(DeviceState *dev)
129{
777357d7
MAL
130 MouseChardev *mouse = MOUSE_CHARDEV(dev);
131 Chardev *chr = CHARDEV(dev);
96d7c072
GH
132
133 msmouse_queue_event(mouse);
41ac54b2 134 msmouse_chr_accept_input(chr);
aa71cf80
AJ
135}
136
0ec7b3e7 137static int msmouse_chr_write(struct Chardev *s, const uint8_t *buf, int len)
aa71cf80
AJ
138{
139 /* Ignore writes to mouse port */
140 return len;
141}
142
8955e891 143static void char_msmouse_finalize(Object *obj)
aa71cf80 144{
8955e891 145 MouseChardev *mouse = MOUSE_CHARDEV(obj);
cde8dcbc 146
96d7c072 147 qemu_input_handler_unregister(mouse->hs);
aa71cf80
AJ
148}
149
96d7c072
GH
150static QemuInputHandler msmouse_handler = {
151 .name = "QEMU Microsoft Mouse",
152 .mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_REL,
153 .event = msmouse_input_event,
154 .sync = msmouse_input_sync,
155};
156
777357d7
MAL
157static void msmouse_chr_open(Chardev *chr,
158 ChardevBackend *backend,
159 bool *be_opened,
160 Error **errp)
aa71cf80 161{
777357d7 162 MouseChardev *mouse = MOUSE_CHARDEV(chr);
aa71cf80 163
82878dac 164 *be_opened = false;
96d7c072
GH
165 mouse->hs = qemu_input_handler_register((DeviceState *)mouse,
166 &msmouse_handler);
777357d7 167}
cde8dcbc 168
777357d7
MAL
169static void char_msmouse_class_init(ObjectClass *oc, void *data)
170{
171 ChardevClass *cc = CHARDEV_CLASS(oc);
aa71cf80 172
777357d7
MAL
173 cc->open = msmouse_chr_open;
174 cc->chr_write = msmouse_chr_write;
175 cc->chr_accept_input = msmouse_chr_accept_input;
aa71cf80 176}
5ab8211b 177
777357d7
MAL
178static const TypeInfo char_msmouse_type_info = {
179 .name = TYPE_CHARDEV_MSMOUSE,
180 .parent = TYPE_CHARDEV,
181 .instance_size = sizeof(MouseChardev),
8955e891 182 .instance_finalize = char_msmouse_finalize,
777357d7
MAL
183 .class_init = char_msmouse_class_init,
184};
185
5ab8211b
AL
186static void register_types(void)
187{
777357d7 188 type_register_static(&char_msmouse_type_info);
5ab8211b
AL
189}
190
191type_init(register_types);