1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "qemu/osdep.h"
25#include "qemu-common.h"
26#include "chardev/char.h"
27#include "ui/console.h"
28#include "ui/input.h"
29
30#define MSMOUSE_LO6(n) ((n) & 0x3f)
31#define MSMOUSE_HI2(n) (((n) & 0xc0) >> 6)
32
33typedef struct {
34 Chardev parent;
35
36 QemuInputHandlerState *hs;
37 int axis[INPUT_AXIS__MAX];
38 bool btns[INPUT_BUTTON__MAX];
39 bool btnc[INPUT_BUTTON__MAX];
40 uint8_t outbuf[32];
41 int outlen;
42} MouseChardev;
43
44#define TYPE_CHARDEV_MSMOUSE "chardev-msmouse"
45#define MOUSE_CHARDEV(obj) \
46 OBJECT_CHECK(MouseChardev, (obj), TYPE_CHARDEV_MSMOUSE)
47
48static void msmouse_chr_accept_input(Chardev *chr)
49{
50 MouseChardev *mouse = MOUSE_CHARDEV(chr);
51 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
68static void msmouse_queue_event(MouseChardev *mouse)
69{
70 unsigned char bytes[4] = { 0x40, 0x00, 0x00, 0x00 };
71 int dx, dy, count = 3;
72
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;
78
79
80 bytes[0] |= (MSMOUSE_HI2(dy) << 2) | MSMOUSE_HI2(dx);
81 bytes[1] |= MSMOUSE_LO6(dx);
82 bytes[2] |= MSMOUSE_LO6(dy);
83
84
85 bytes[0] |= (mouse->btns[INPUT_BUTTON_LEFT] ? 0x20 : 0x00);
86 bytes[0] |= (mouse->btns[INPUT_BUTTON_RIGHT] ? 0x10 : 0x00);
87 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 }
93
94 if (mouse->outlen <= sizeof(mouse->outbuf) - count) {
95 memcpy(mouse->outbuf + mouse->outlen, bytes, count);
96 mouse->outlen += count;
97 } else {
98
99 }
100}
101
102static void msmouse_input_event(DeviceState *dev, QemuConsole *src,
103 InputEvent *evt)
104{
105 MouseChardev *mouse = MOUSE_CHARDEV(dev);
106 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;
118 mouse->btnc[btn->button] = true;
119 break;
120
121 default:
122
123 break;
124 }
125}
126
127static void msmouse_input_sync(DeviceState *dev)
128{
129 MouseChardev *mouse = MOUSE_CHARDEV(dev);
130 Chardev *chr = CHARDEV(dev);
131
132 msmouse_queue_event(mouse);
133 msmouse_chr_accept_input(chr);
134}
135
136static int msmouse_chr_write(struct Chardev *s, const uint8_t *buf, int len)
137{
138
139 return len;
140}
141
142static void char_msmouse_finalize(Object *obj)
143{
144 MouseChardev *mouse = MOUSE_CHARDEV(obj);
145
146 qemu_input_handler_unregister(mouse->hs);
147}
148
149static 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
156static void msmouse_chr_open(Chardev *chr,
157 ChardevBackend *backend,
158 bool *be_opened,
159 Error **errp)
160{
161 MouseChardev *mouse = MOUSE_CHARDEV(chr);
162
163 *be_opened = false;
164 mouse->hs = qemu_input_handler_register((DeviceState *)mouse,
165 &msmouse_handler);
166}
167
168static 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;
175}
176
177static const TypeInfo char_msmouse_type_info = {
178 .name = TYPE_CHARDEV_MSMOUSE,
179 .parent = TYPE_CHARDEV,
180 .instance_size = sizeof(MouseChardev),
181 .instance_finalize = char_msmouse_finalize,
182 .class_init = char_msmouse_class_init,
183};
184
185static void register_types(void)
186{
187 type_register_static(&char_msmouse_type_info);
188}
189
190type_init(register_types);
191