]>
Commit | Line | Data |
---|---|---|
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 | */ | |
9c058332 | 24 | #include "qemu/osdep.h" |
28ecbaee | 25 | #include "qemu-common.h" |
8228e353 | 26 | #include "chardev/char.h" |
28ecbaee | 27 | #include "ui/console.h" |
96d7c072 | 28 | #include "ui/input.h" |
aa71cf80 AJ |
29 | |
30 | #define MSMOUSE_LO6(n) ((n) & 0x3f) | |
31 | #define MSMOUSE_HI2(n) (((n) & 0xc0) >> 6) | |
32 | ||
cde8dcbc | 33 | typedef struct { |
0ec7b3e7 | 34 | Chardev parent; |
41ac54b2 | 35 | |
96d7c072 GH |
36 | QemuInputHandlerState *hs; |
37 | int axis[INPUT_AXIS__MAX]; | |
38 | bool btns[INPUT_BUTTON__MAX]; | |
d7b7f526 | 39 | bool btnc[INPUT_BUTTON__MAX]; |
57a4e3b9 GH |
40 | uint8_t outbuf[32]; |
41 | int outlen; | |
0ec7b3e7 | 42 | } MouseChardev; |
cde8dcbc | 43 | |
777357d7 MAL |
44 | #define TYPE_CHARDEV_MSMOUSE "chardev-msmouse" |
45 | #define MOUSE_CHARDEV(obj) \ | |
46 | OBJECT_CHECK(MouseChardev, (obj), TYPE_CHARDEV_MSMOUSE) | |
47 | ||
0ec7b3e7 | 48 | static void msmouse_chr_accept_input(Chardev *chr) |
57a4e3b9 | 49 | { |
777357d7 | 50 | MouseChardev *mouse = MOUSE_CHARDEV(chr); |
57a4e3b9 GH |
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 | ||
0ec7b3e7 | 68 | static void msmouse_queue_event(MouseChardev *mouse) |
aa71cf80 | 69 | { |
aa71cf80 | 70 | unsigned char bytes[4] = { 0x40, 0x00, 0x00, 0x00 }; |
d7b7f526 | 71 | int dx, dy, count = 3; |
96d7c072 GH |
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; | |
aa71cf80 AJ |
78 | |
79 | /* Movement deltas */ | |
80 | bytes[0] |= (MSMOUSE_HI2(dy) << 2) | MSMOUSE_HI2(dx); | |
81 | bytes[1] |= MSMOUSE_LO6(dx); | |
82 | bytes[2] |= MSMOUSE_LO6(dy); | |
83 | ||
84 | /* Buttons */ | |
96d7c072 GH |
85 | bytes[0] |= (mouse->btns[INPUT_BUTTON_LEFT] ? 0x20 : 0x00); |
86 | bytes[0] |= (mouse->btns[INPUT_BUTTON_RIGHT] ? 0x10 : 0x00); | |
d7b7f526 GH |
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; | |
57a4e3b9 GH |
97 | } else { |
98 | /* queue full -> drop event */ | |
99 | } | |
96d7c072 | 100 | } |
57a4e3b9 | 101 | |
96d7c072 GH |
102 | static void msmouse_input_event(DeviceState *dev, QemuConsole *src, |
103 | InputEvent *evt) | |
104 | { | |
777357d7 | 105 | MouseChardev *mouse = MOUSE_CHARDEV(dev); |
96d7c072 GH |
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; | |
d7b7f526 | 118 | mouse->btnc[btn->button] = true; |
96d7c072 GH |
119 | break; |
120 | ||
121 | default: | |
122 | /* keep gcc happy */ | |
123 | break; | |
124 | } | |
125 | } | |
126 | ||
127 | static void msmouse_input_sync(DeviceState *dev) | |
128 | { | |
777357d7 MAL |
129 | MouseChardev *mouse = MOUSE_CHARDEV(dev); |
130 | Chardev *chr = CHARDEV(dev); | |
96d7c072 GH |
131 | |
132 | msmouse_queue_event(mouse); | |
41ac54b2 | 133 | msmouse_chr_accept_input(chr); |
aa71cf80 AJ |
134 | } |
135 | ||
0ec7b3e7 | 136 | static int msmouse_chr_write(struct Chardev *s, const uint8_t *buf, int len) |
aa71cf80 AJ |
137 | { |
138 | /* Ignore writes to mouse port */ | |
139 | return len; | |
140 | } | |
141 | ||
8955e891 | 142 | static void char_msmouse_finalize(Object *obj) |
aa71cf80 | 143 | { |
8955e891 | 144 | MouseChardev *mouse = MOUSE_CHARDEV(obj); |
cde8dcbc | 145 | |
96d7c072 | 146 | qemu_input_handler_unregister(mouse->hs); |
aa71cf80 AJ |
147 | } |
148 | ||
96d7c072 GH |
149 | static 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 | ||
777357d7 MAL |
156 | static void msmouse_chr_open(Chardev *chr, |
157 | ChardevBackend *backend, | |
158 | bool *be_opened, | |
159 | Error **errp) | |
aa71cf80 | 160 | { |
777357d7 | 161 | MouseChardev *mouse = MOUSE_CHARDEV(chr); |
aa71cf80 | 162 | |
82878dac | 163 | *be_opened = false; |
96d7c072 GH |
164 | mouse->hs = qemu_input_handler_register((DeviceState *)mouse, |
165 | &msmouse_handler); | |
777357d7 | 166 | } |
cde8dcbc | 167 | |
777357d7 MAL |
168 | static void char_msmouse_class_init(ObjectClass *oc, void *data) |
169 | { | |
170 | ChardevClass *cc = CHARDEV_CLASS(oc); | |
aa71cf80 | 171 | |
777357d7 MAL |
172 | cc->open = msmouse_chr_open; |
173 | cc->chr_write = msmouse_chr_write; | |
174 | cc->chr_accept_input = msmouse_chr_accept_input; | |
aa71cf80 | 175 | } |
5ab8211b | 176 | |
777357d7 MAL |
177 | static const TypeInfo char_msmouse_type_info = { |
178 | .name = TYPE_CHARDEV_MSMOUSE, | |
179 | .parent = TYPE_CHARDEV, | |
180 | .instance_size = sizeof(MouseChardev), | |
8955e891 | 181 | .instance_finalize = char_msmouse_finalize, |
777357d7 MAL |
182 | .class_init = char_msmouse_class_init, |
183 | }; | |
184 | ||
5ab8211b AL |
185 | static void register_types(void) |
186 | { | |
777357d7 | 187 | type_register_static(&char_msmouse_type_info); |
5ab8211b AL |
188 | } |
189 | ||
190 | type_init(register_types); |