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
29#define MSMOUSE_LO6(n) ((n) & 0x3f)
30#define MSMOUSE_HI2(n) (((n) & 0xc0) >> 6)
31
32static void msmouse_event(void *opaque,
33 int dx, int dy, int dz, int buttons_state)
34{
35 CharDriverState *chr = (CharDriverState *)opaque;
36
37 unsigned char bytes[4] = { 0x40, 0x00, 0x00, 0x00 };
38
39
40 bytes[0] |= (MSMOUSE_HI2(dy) << 2) | MSMOUSE_HI2(dx);
41 bytes[1] |= MSMOUSE_LO6(dx);
42 bytes[2] |= MSMOUSE_LO6(dy);
43
44
45 bytes[0] |= (buttons_state & 0x01 ? 0x20 : 0x00);
46 bytes[0] |= (buttons_state & 0x02 ? 0x10 : 0x00);
47 bytes[3] |= (buttons_state & 0x04 ? 0x20 : 0x00);
48
49
50
51
52 qemu_chr_be_write(chr, bytes, 4);
53}
54
55static int msmouse_chr_write (struct CharDriverState *s, const uint8_t *buf, int len)
56{
57
58 return len;
59}
60
61static void msmouse_chr_close (struct CharDriverState *chr)
62{
63 g_free (chr);
64}
65
66static CharDriverState *qemu_chr_open_msmouse(const char *id,
67 ChardevBackend *backend,
68 ChardevReturn *ret,
69 Error **errp)
70{
71 ChardevCommon *common = backend->u.msmouse.data;
72 CharDriverState *chr;
73
74 chr = qemu_chr_alloc(common, errp);
75 if (!chr) {
76 return NULL;
77 }
78 chr->chr_write = msmouse_chr_write;
79 chr->chr_close = msmouse_chr_close;
80 chr->explicit_be_open = true;
81
82 qemu_add_mouse_event_handler(msmouse_event, chr, 0, "QEMU Microsoft Mouse");
83
84 return chr;
85}
86
87static void register_types(void)
88{
89 register_char_driver("msmouse", CHARDEV_BACKEND_KIND_MSMOUSE, NULL,
90 qemu_chr_open_msmouse);
91}
92
93type_init(register_types);
94