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 "sysemu/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 CharDriverState *chr;
35 QemuInputHandlerState *hs;
36 int axis[INPUT_AXIS__MAX];
37 bool btns[INPUT_BUTTON__MAX];
38 bool btnc[INPUT_BUTTON__MAX];
39 uint8_t outbuf[32];
40 int outlen;
41} MouseState;
42
43static void msmouse_chr_accept_input(CharDriverState *chr)
44{
45 MouseState *mouse = chr->opaque;
46 int len;
47
48 len = qemu_chr_be_can_write(chr);
49 if (len > mouse->outlen) {
50 len = mouse->outlen;
51 }
52 if (!len) {
53 return;
54 }
55
56 qemu_chr_be_write(chr, mouse->outbuf, len);
57 mouse->outlen -= len;
58 if (mouse->outlen) {
59 memmove(mouse->outbuf, mouse->outbuf + len, mouse->outlen);
60 }
61}
62
63static void msmouse_queue_event(MouseState *mouse)
64{
65 unsigned char bytes[4] = { 0x40, 0x00, 0x00, 0x00 };
66 int dx, dy, count = 3;
67
68 dx = mouse->axis[INPUT_AXIS_X];
69 mouse->axis[INPUT_AXIS_X] = 0;
70
71 dy = mouse->axis[INPUT_AXIS_Y];
72 mouse->axis[INPUT_AXIS_Y] = 0;
73
74
75 bytes[0] |= (MSMOUSE_HI2(dy) << 2) | MSMOUSE_HI2(dx);
76 bytes[1] |= MSMOUSE_LO6(dx);
77 bytes[2] |= MSMOUSE_LO6(dy);
78
79
80 bytes[0] |= (mouse->btns[INPUT_BUTTON_LEFT] ? 0x20 : 0x00);
81 bytes[0] |= (mouse->btns[INPUT_BUTTON_RIGHT] ? 0x10 : 0x00);
82 if (mouse->btns[INPUT_BUTTON_MIDDLE] ||
83 mouse->btnc[INPUT_BUTTON_MIDDLE]) {
84 bytes[3] |= (mouse->btns[INPUT_BUTTON_MIDDLE] ? 0x20 : 0x00);
85 mouse->btnc[INPUT_BUTTON_MIDDLE] = false;
86 count = 4;
87 }
88
89 if (mouse->outlen <= sizeof(mouse->outbuf) - count) {
90 memcpy(mouse->outbuf + mouse->outlen, bytes, count);
91 mouse->outlen += count;
92 } else {
93
94 }
95}
96
97static void msmouse_input_event(DeviceState *dev, QemuConsole *src,
98 InputEvent *evt)
99{
100 MouseState *mouse = (MouseState *)dev;
101 InputMoveEvent *move;
102 InputBtnEvent *btn;
103
104 switch (evt->type) {
105 case INPUT_EVENT_KIND_REL:
106 move = evt->u.rel.data;
107 mouse->axis[move->axis] += move->value;
108 break;
109
110 case INPUT_EVENT_KIND_BTN:
111 btn = evt->u.btn.data;
112 mouse->btns[btn->button] = btn->down;
113 mouse->btnc[btn->button] = true;
114 break;
115
116 default:
117
118 break;
119 }
120}
121
122static void msmouse_input_sync(DeviceState *dev)
123{
124 MouseState *mouse = (MouseState *)dev;
125
126 msmouse_queue_event(mouse);
127 msmouse_chr_accept_input(mouse->chr);
128}
129
130static int msmouse_chr_write (struct CharDriverState *s, const uint8_t *buf, int len)
131{
132
133 return len;
134}
135
136static void msmouse_chr_close (struct CharDriverState *chr)
137{
138 MouseState *mouse = chr->opaque;
139
140 qemu_input_handler_unregister(mouse->hs);
141 g_free(mouse);
142 g_free(chr);
143}
144
145static QemuInputHandler msmouse_handler = {
146 .name = "QEMU Microsoft Mouse",
147 .mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_REL,
148 .event = msmouse_input_event,
149 .sync = msmouse_input_sync,
150};
151
152static CharDriverState *qemu_chr_open_msmouse(const char *id,
153 ChardevBackend *backend,
154 ChardevReturn *ret,
155 Error **errp)
156{
157 ChardevCommon *common = backend->u.msmouse.data;
158 MouseState *mouse;
159 CharDriverState *chr;
160
161 chr = qemu_chr_alloc(common, errp);
162 chr->chr_write = msmouse_chr_write;
163 chr->chr_close = msmouse_chr_close;
164 chr->chr_accept_input = msmouse_chr_accept_input;
165 chr->explicit_be_open = true;
166
167 mouse = g_new0(MouseState, 1);
168 mouse->hs = qemu_input_handler_register((DeviceState *)mouse,
169 &msmouse_handler);
170
171 mouse->chr = chr;
172 chr->opaque = mouse;
173
174 return chr;
175}
176
177static void register_types(void)
178{
179 register_char_driver("msmouse", CHARDEV_BACKEND_KIND_MSMOUSE, NULL,
180 qemu_chr_open_msmouse);
181}
182
183type_init(register_types);
184