root / hw / vmmouse.c @ 8d3a8c1e
History | View | Annotate | Download (7.5 kB)
1 | 548df2ac | ths | /*
|
---|---|---|---|
2 | 548df2ac | ths | * QEMU VMMouse emulation
|
3 | 5fafdf24 | ths | *
|
4 | 548df2ac | ths | * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
|
5 | 5fafdf24 | ths | *
|
6 | 548df2ac | ths | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 548df2ac | ths | * of this software and associated documentation files (the "Software"), to deal
|
8 | 548df2ac | ths | * in the Software without restriction, including without limitation the rights
|
9 | 548df2ac | ths | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 548df2ac | ths | * copies of the Software, and to permit persons to whom the Software is
|
11 | 548df2ac | ths | * furnished to do so, subject to the following conditions:
|
12 | 548df2ac | ths | *
|
13 | 548df2ac | ths | * The above copyright notice and this permission notice shall be included in
|
14 | 548df2ac | ths | * all copies or substantial portions of the Software.
|
15 | 548df2ac | ths | *
|
16 | 548df2ac | ths | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 548df2ac | ths | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 548df2ac | ths | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 548df2ac | ths | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 548df2ac | ths | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 548df2ac | ths | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 548df2ac | ths | * THE SOFTWARE.
|
23 | 548df2ac | ths | */
|
24 | 87ecb68b | pbrook | #include "hw.h" |
25 | 87ecb68b | pbrook | #include "console.h" |
26 | 87ecb68b | pbrook | #include "ps2.h" |
27 | 87ecb68b | pbrook | #include "pc.h" |
28 | 91c9e091 | Blue Swirl | #include "qdev.h" |
29 | 548df2ac | ths | |
30 | 548df2ac | ths | /* debug only vmmouse */
|
31 | 548df2ac | ths | //#define DEBUG_VMMOUSE
|
32 | 548df2ac | ths | |
33 | 548df2ac | ths | /* VMMouse Commands */
|
34 | 548df2ac | ths | #define VMMOUSE_GETVERSION 10 |
35 | 548df2ac | ths | #define VMMOUSE_DATA 39 |
36 | 548df2ac | ths | #define VMMOUSE_STATUS 40 |
37 | 548df2ac | ths | #define VMMOUSE_COMMAND 41 |
38 | 548df2ac | ths | |
39 | 548df2ac | ths | #define VMMOUSE_READ_ID 0x45414552 |
40 | 548df2ac | ths | #define VMMOUSE_DISABLE 0x000000f5 |
41 | 548df2ac | ths | #define VMMOUSE_REQUEST_RELATIVE 0x4c455252 |
42 | 548df2ac | ths | #define VMMOUSE_REQUEST_ABSOLUTE 0x53424152 |
43 | 548df2ac | ths | |
44 | 548df2ac | ths | #define VMMOUSE_QUEUE_SIZE 1024 |
45 | 548df2ac | ths | |
46 | 548df2ac | ths | #define VMMOUSE_VERSION 0x3442554a |
47 | 548df2ac | ths | |
48 | 548df2ac | ths | #ifdef DEBUG_VMMOUSE
|
49 | 548df2ac | ths | #define DPRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__) |
50 | 548df2ac | ths | #else
|
51 | 548df2ac | ths | #define DPRINTF(fmt, ...) do { } while (0) |
52 | 548df2ac | ths | #endif
|
53 | 548df2ac | ths | |
54 | 548df2ac | ths | typedef struct _VMMouseState |
55 | 548df2ac | ths | { |
56 | 91c9e091 | Blue Swirl | ISADevice dev; |
57 | 548df2ac | ths | uint32_t queue[VMMOUSE_QUEUE_SIZE]; |
58 | bcb58d9a | Juan Quintela | int32_t queue_size; |
59 | 548df2ac | ths | uint16_t nb_queue; |
60 | 548df2ac | ths | uint16_t status; |
61 | 548df2ac | ths | uint8_t absolute; |
62 | 548df2ac | ths | QEMUPutMouseEntry *entry; |
63 | 548df2ac | ths | void *ps2_mouse;
|
64 | 548df2ac | ths | } VMMouseState; |
65 | 548df2ac | ths | |
66 | 548df2ac | ths | static uint32_t vmmouse_get_status(VMMouseState *s)
|
67 | 548df2ac | ths | { |
68 | 548df2ac | ths | DPRINTF("vmmouse_get_status()\n");
|
69 | 548df2ac | ths | return (s->status << 16) | s->nb_queue; |
70 | 548df2ac | ths | } |
71 | 548df2ac | ths | |
72 | 548df2ac | ths | static void vmmouse_mouse_event(void *opaque, int x, int y, int dz, int buttons_state) |
73 | 548df2ac | ths | { |
74 | 548df2ac | ths | VMMouseState *s = opaque; |
75 | 548df2ac | ths | int buttons = 0; |
76 | 548df2ac | ths | |
77 | 548df2ac | ths | if (s->nb_queue > (VMMOUSE_QUEUE_SIZE - 4)) |
78 | 4ed59aa5 | ths | return;
|
79 | 548df2ac | ths | |
80 | 548df2ac | ths | DPRINTF("vmmouse_mouse_event(%d, %d, %d, %d)\n",
|
81 | 4ed59aa5 | ths | x, y, dz, buttons_state); |
82 | 548df2ac | ths | |
83 | 548df2ac | ths | if ((buttons_state & MOUSE_EVENT_LBUTTON))
|
84 | 4ed59aa5 | ths | buttons |= 0x20;
|
85 | 548df2ac | ths | if ((buttons_state & MOUSE_EVENT_RBUTTON))
|
86 | 4ed59aa5 | ths | buttons |= 0x10;
|
87 | 548df2ac | ths | if ((buttons_state & MOUSE_EVENT_MBUTTON))
|
88 | 4ed59aa5 | ths | buttons |= 0x08;
|
89 | 548df2ac | ths | |
90 | 548df2ac | ths | if (s->absolute) {
|
91 | 4ed59aa5 | ths | x <<= 1;
|
92 | 4ed59aa5 | ths | y <<= 1;
|
93 | 548df2ac | ths | } |
94 | 548df2ac | ths | |
95 | 548df2ac | ths | s->queue[s->nb_queue++] = buttons; |
96 | 548df2ac | ths | s->queue[s->nb_queue++] = x; |
97 | 548df2ac | ths | s->queue[s->nb_queue++] = y; |
98 | 548df2ac | ths | s->queue[s->nb_queue++] = dz; |
99 | 548df2ac | ths | |
100 | 548df2ac | ths | /* need to still generate PS2 events to notify driver to
|
101 | 548df2ac | ths | read from queue */
|
102 | 956a3e6b | Blue Swirl | i8042_isa_mouse_fake_event(s->ps2_mouse); |
103 | 548df2ac | ths | } |
104 | 548df2ac | ths | |
105 | cd496926 | Gerd Hoffmann | static void vmmouse_remove_handler(VMMouseState *s) |
106 | 548df2ac | ths | { |
107 | 548df2ac | ths | if (s->entry) {
|
108 | 4ed59aa5 | ths | qemu_remove_mouse_event_handler(s->entry); |
109 | 4ed59aa5 | ths | s->entry = NULL;
|
110 | 548df2ac | ths | } |
111 | cd496926 | Gerd Hoffmann | } |
112 | cd496926 | Gerd Hoffmann | |
113 | cd496926 | Gerd Hoffmann | static void vmmouse_update_handler(VMMouseState *s, int absolute) |
114 | cd496926 | Gerd Hoffmann | { |
115 | cd496926 | Gerd Hoffmann | if (s->status != 0) { |
116 | cd496926 | Gerd Hoffmann | return;
|
117 | cd496926 | Gerd Hoffmann | } |
118 | cd496926 | Gerd Hoffmann | if (s->absolute != absolute) {
|
119 | cd496926 | Gerd Hoffmann | s->absolute = absolute; |
120 | cd496926 | Gerd Hoffmann | vmmouse_remove_handler(s); |
121 | cd496926 | Gerd Hoffmann | } |
122 | cd496926 | Gerd Hoffmann | if (s->entry == NULL) { |
123 | 4ed59aa5 | ths | s->entry = qemu_add_mouse_event_handler(vmmouse_mouse_event, |
124 | 4ed59aa5 | ths | s, s->absolute, |
125 | 4ed59aa5 | ths | "vmmouse");
|
126 | cd496926 | Gerd Hoffmann | qemu_activate_mouse_event_handler(s->entry); |
127 | cd496926 | Gerd Hoffmann | } |
128 | 548df2ac | ths | } |
129 | 548df2ac | ths | |
130 | 548df2ac | ths | static void vmmouse_read_id(VMMouseState *s) |
131 | 548df2ac | ths | { |
132 | 548df2ac | ths | DPRINTF("vmmouse_read_id()\n");
|
133 | 548df2ac | ths | |
134 | 548df2ac | ths | if (s->nb_queue == VMMOUSE_QUEUE_SIZE)
|
135 | 4ed59aa5 | ths | return;
|
136 | 548df2ac | ths | |
137 | 548df2ac | ths | s->queue[s->nb_queue++] = VMMOUSE_VERSION; |
138 | 548df2ac | ths | s->status = 0;
|
139 | 548df2ac | ths | } |
140 | 548df2ac | ths | |
141 | 548df2ac | ths | static void vmmouse_request_relative(VMMouseState *s) |
142 | 548df2ac | ths | { |
143 | 548df2ac | ths | DPRINTF("vmmouse_request_relative()\n");
|
144 | cd496926 | Gerd Hoffmann | vmmouse_update_handler(s, 0);
|
145 | 548df2ac | ths | } |
146 | 548df2ac | ths | |
147 | 548df2ac | ths | static void vmmouse_request_absolute(VMMouseState *s) |
148 | 548df2ac | ths | { |
149 | 548df2ac | ths | DPRINTF("vmmouse_request_absolute()\n");
|
150 | cd496926 | Gerd Hoffmann | vmmouse_update_handler(s, 1);
|
151 | 548df2ac | ths | } |
152 | 548df2ac | ths | |
153 | 548df2ac | ths | static void vmmouse_disable(VMMouseState *s) |
154 | 548df2ac | ths | { |
155 | 548df2ac | ths | DPRINTF("vmmouse_disable()\n");
|
156 | 548df2ac | ths | s->status = 0xffff;
|
157 | cd496926 | Gerd Hoffmann | vmmouse_remove_handler(s); |
158 | 548df2ac | ths | } |
159 | 548df2ac | ths | |
160 | 548df2ac | ths | static void vmmouse_data(VMMouseState *s, uint32_t *data, uint32_t size) |
161 | 548df2ac | ths | { |
162 | 548df2ac | ths | int i;
|
163 | 548df2ac | ths | |
164 | 548df2ac | ths | DPRINTF("vmmouse_data(%d)\n", size);
|
165 | 548df2ac | ths | |
166 | 548df2ac | ths | if (size == 0 || size > 6 || size > s->nb_queue) { |
167 | 4ed59aa5 | ths | printf("vmmouse: driver requested too much data %d\n", size);
|
168 | 4ed59aa5 | ths | s->status = 0xffff;
|
169 | cd496926 | Gerd Hoffmann | vmmouse_remove_handler(s); |
170 | 4ed59aa5 | ths | return;
|
171 | 548df2ac | ths | } |
172 | 548df2ac | ths | |
173 | 548df2ac | ths | for (i = 0; i < size; i++) |
174 | 4ed59aa5 | ths | data[i] = s->queue[i]; |
175 | 548df2ac | ths | |
176 | 548df2ac | ths | s->nb_queue -= size; |
177 | 548df2ac | ths | if (s->nb_queue)
|
178 | 4ed59aa5 | ths | memmove(s->queue, &s->queue[size], sizeof(s->queue[0]) * s->nb_queue); |
179 | 548df2ac | ths | } |
180 | 548df2ac | ths | |
181 | 548df2ac | ths | static uint32_t vmmouse_ioport_read(void *opaque, uint32_t addr) |
182 | 548df2ac | ths | { |
183 | 548df2ac | ths | VMMouseState *s = opaque; |
184 | 548df2ac | ths | uint32_t data[6];
|
185 | 548df2ac | ths | uint16_t command; |
186 | 548df2ac | ths | |
187 | 548df2ac | ths | vmmouse_get_data(data); |
188 | 548df2ac | ths | |
189 | 548df2ac | ths | command = data[2] & 0xFFFF; |
190 | 548df2ac | ths | |
191 | 548df2ac | ths | switch (command) {
|
192 | 548df2ac | ths | case VMMOUSE_STATUS:
|
193 | 4ed59aa5 | ths | data[0] = vmmouse_get_status(s);
|
194 | 4ed59aa5 | ths | break;
|
195 | 548df2ac | ths | case VMMOUSE_COMMAND:
|
196 | 4ed59aa5 | ths | switch (data[1]) { |
197 | 4ed59aa5 | ths | case VMMOUSE_DISABLE:
|
198 | 4ed59aa5 | ths | vmmouse_disable(s); |
199 | 4ed59aa5 | ths | break;
|
200 | 4ed59aa5 | ths | case VMMOUSE_READ_ID:
|
201 | 4ed59aa5 | ths | vmmouse_read_id(s); |
202 | 4ed59aa5 | ths | break;
|
203 | 4ed59aa5 | ths | case VMMOUSE_REQUEST_RELATIVE:
|
204 | 4ed59aa5 | ths | vmmouse_request_relative(s); |
205 | 4ed59aa5 | ths | break;
|
206 | 4ed59aa5 | ths | case VMMOUSE_REQUEST_ABSOLUTE:
|
207 | 4ed59aa5 | ths | vmmouse_request_absolute(s); |
208 | 4ed59aa5 | ths | break;
|
209 | 4ed59aa5 | ths | default:
|
210 | 4ed59aa5 | ths | printf("vmmouse: unknown command %x\n", data[1]); |
211 | 4ed59aa5 | ths | break;
|
212 | 4ed59aa5 | ths | } |
213 | 4ed59aa5 | ths | break;
|
214 | 548df2ac | ths | case VMMOUSE_DATA:
|
215 | 4ed59aa5 | ths | vmmouse_data(s, data, data[1]);
|
216 | 4ed59aa5 | ths | break;
|
217 | 548df2ac | ths | default:
|
218 | 4ed59aa5 | ths | printf("vmmouse: unknown command %x\n", command);
|
219 | 4ed59aa5 | ths | break;
|
220 | 548df2ac | ths | } |
221 | 548df2ac | ths | |
222 | 548df2ac | ths | vmmouse_set_data(data); |
223 | 548df2ac | ths | return data[0]; |
224 | 548df2ac | ths | } |
225 | 548df2ac | ths | |
226 | e59fb374 | Juan Quintela | static int vmmouse_post_load(void *opaque, int version_id) |
227 | 548df2ac | ths | { |
228 | 548df2ac | ths | VMMouseState *s = opaque; |
229 | 548df2ac | ths | |
230 | cd496926 | Gerd Hoffmann | vmmouse_remove_handler(s); |
231 | cd496926 | Gerd Hoffmann | vmmouse_update_handler(s, s->absolute); |
232 | 548df2ac | ths | return 0; |
233 | 548df2ac | ths | } |
234 | 548df2ac | ths | |
235 | bcb58d9a | Juan Quintela | static const VMStateDescription vmstate_vmmouse = { |
236 | bcb58d9a | Juan Quintela | .name = "vmmouse",
|
237 | bcb58d9a | Juan Quintela | .version_id = 0,
|
238 | bcb58d9a | Juan Quintela | .minimum_version_id = 0,
|
239 | bcb58d9a | Juan Quintela | .minimum_version_id_old = 0,
|
240 | bcb58d9a | Juan Quintela | .post_load = vmmouse_post_load, |
241 | bcb58d9a | Juan Quintela | .fields = (VMStateField []) { |
242 | bcb58d9a | Juan Quintela | VMSTATE_INT32_EQUAL(queue_size, VMMouseState), |
243 | bcb58d9a | Juan Quintela | VMSTATE_UINT32_ARRAY(queue, VMMouseState, VMMOUSE_QUEUE_SIZE), |
244 | bcb58d9a | Juan Quintela | VMSTATE_UINT16(nb_queue, VMMouseState), |
245 | bcb58d9a | Juan Quintela | VMSTATE_UINT16(status, VMMouseState), |
246 | bcb58d9a | Juan Quintela | VMSTATE_UINT8(absolute, VMMouseState), |
247 | bcb58d9a | Juan Quintela | VMSTATE_END_OF_LIST() |
248 | bcb58d9a | Juan Quintela | } |
249 | bcb58d9a | Juan Quintela | }; |
250 | bcb58d9a | Juan Quintela | |
251 | 91c9e091 | Blue Swirl | static void vmmouse_reset(DeviceState *d) |
252 | 548df2ac | ths | { |
253 | 91c9e091 | Blue Swirl | VMMouseState *s = container_of(d, VMMouseState, dev.qdev); |
254 | 548df2ac | ths | |
255 | 91c9e091 | Blue Swirl | s->status = 0xffff;
|
256 | 8668f61d | Blue Swirl | s->queue_size = VMMOUSE_QUEUE_SIZE; |
257 | 91c9e091 | Blue Swirl | } |
258 | 548df2ac | ths | |
259 | 91c9e091 | Blue Swirl | static int vmmouse_initfn(ISADevice *dev) |
260 | 91c9e091 | Blue Swirl | { |
261 | 91c9e091 | Blue Swirl | VMMouseState *s = DO_UPCAST(VMMouseState, dev, dev); |
262 | 548df2ac | ths | |
263 | 91c9e091 | Blue Swirl | DPRINTF("vmmouse_init\n");
|
264 | 548df2ac | ths | |
265 | 93342807 | ths | vmport_register(VMMOUSE_STATUS, vmmouse_ioport_read, s); |
266 | 93342807 | ths | vmport_register(VMMOUSE_COMMAND, vmmouse_ioport_read, s); |
267 | 93342807 | ths | vmport_register(VMMOUSE_DATA, vmmouse_ioport_read, s); |
268 | 548df2ac | ths | |
269 | 91c9e091 | Blue Swirl | return 0; |
270 | 91c9e091 | Blue Swirl | } |
271 | 91c9e091 | Blue Swirl | |
272 | 91c9e091 | Blue Swirl | static ISADeviceInfo vmmouse_info = {
|
273 | 91c9e091 | Blue Swirl | .init = vmmouse_initfn, |
274 | 91c9e091 | Blue Swirl | .qdev.name = "vmmouse",
|
275 | 91c9e091 | Blue Swirl | .qdev.size = sizeof(VMMouseState),
|
276 | ff90d503 | Jan Kiszka | .qdev.vmsd = &vmstate_vmmouse, |
277 | 91c9e091 | Blue Swirl | .qdev.no_user = 1,
|
278 | 91c9e091 | Blue Swirl | .qdev.reset = vmmouse_reset, |
279 | 91c9e091 | Blue Swirl | .qdev.props = (Property[]) { |
280 | 91c9e091 | Blue Swirl | DEFINE_PROP_PTR("ps2_mouse", VMMouseState, ps2_mouse),
|
281 | 91c9e091 | Blue Swirl | DEFINE_PROP_END_OF_LIST(), |
282 | 91c9e091 | Blue Swirl | } |
283 | 91c9e091 | Blue Swirl | }; |
284 | 91c9e091 | Blue Swirl | |
285 | 91c9e091 | Blue Swirl | static void vmmouse_dev_register(void) |
286 | 91c9e091 | Blue Swirl | { |
287 | 91c9e091 | Blue Swirl | isa_qdev_register(&vmmouse_info); |
288 | 548df2ac | ths | } |
289 | 91c9e091 | Blue Swirl | device_init(vmmouse_dev_register) |