root / hw / vmmouse.c @ 9ba2f660
History | View | Annotate | Download (7.4 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 | 548df2ac | ths | |
29 | 548df2ac | ths | /* debug only vmmouse */
|
30 | 548df2ac | ths | //#define DEBUG_VMMOUSE
|
31 | 548df2ac | ths | |
32 | 548df2ac | ths | /* VMMouse Commands */
|
33 | 548df2ac | ths | #define VMMOUSE_GETVERSION 10 |
34 | 548df2ac | ths | #define VMMOUSE_DATA 39 |
35 | 548df2ac | ths | #define VMMOUSE_STATUS 40 |
36 | 548df2ac | ths | #define VMMOUSE_COMMAND 41 |
37 | 548df2ac | ths | |
38 | 548df2ac | ths | #define VMMOUSE_READ_ID 0x45414552 |
39 | 548df2ac | ths | #define VMMOUSE_DISABLE 0x000000f5 |
40 | 548df2ac | ths | #define VMMOUSE_REQUEST_RELATIVE 0x4c455252 |
41 | 548df2ac | ths | #define VMMOUSE_REQUEST_ABSOLUTE 0x53424152 |
42 | 548df2ac | ths | |
43 | 548df2ac | ths | #define VMMOUSE_QUEUE_SIZE 1024 |
44 | 548df2ac | ths | |
45 | 548df2ac | ths | #define VMMOUSE_VERSION 0x3442554a |
46 | 548df2ac | ths | |
47 | 548df2ac | ths | #ifdef DEBUG_VMMOUSE
|
48 | 548df2ac | ths | #define DPRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__) |
49 | 548df2ac | ths | #else
|
50 | 548df2ac | ths | #define DPRINTF(fmt, ...) do { } while (0) |
51 | 548df2ac | ths | #endif
|
52 | 548df2ac | ths | |
53 | 548df2ac | ths | typedef struct _VMMouseState |
54 | 548df2ac | ths | { |
55 | 548df2ac | ths | uint32_t queue[VMMOUSE_QUEUE_SIZE]; |
56 | bcb58d9a | Juan Quintela | int32_t queue_size; |
57 | 548df2ac | ths | uint16_t nb_queue; |
58 | 548df2ac | ths | uint16_t status; |
59 | 548df2ac | ths | uint8_t absolute; |
60 | 548df2ac | ths | QEMUPutMouseEntry *entry; |
61 | 548df2ac | ths | void *ps2_mouse;
|
62 | 548df2ac | ths | } VMMouseState; |
63 | 548df2ac | ths | |
64 | 548df2ac | ths | static uint32_t vmmouse_get_status(VMMouseState *s)
|
65 | 548df2ac | ths | { |
66 | 548df2ac | ths | DPRINTF("vmmouse_get_status()\n");
|
67 | 548df2ac | ths | return (s->status << 16) | s->nb_queue; |
68 | 548df2ac | ths | } |
69 | 548df2ac | ths | |
70 | 548df2ac | ths | static void vmmouse_mouse_event(void *opaque, int x, int y, int dz, int buttons_state) |
71 | 548df2ac | ths | { |
72 | 548df2ac | ths | VMMouseState *s = opaque; |
73 | 548df2ac | ths | int buttons = 0; |
74 | 548df2ac | ths | |
75 | 548df2ac | ths | if (s->nb_queue > (VMMOUSE_QUEUE_SIZE - 4)) |
76 | 4ed59aa5 | ths | return;
|
77 | 548df2ac | ths | |
78 | 548df2ac | ths | DPRINTF("vmmouse_mouse_event(%d, %d, %d, %d)\n",
|
79 | 4ed59aa5 | ths | x, y, dz, buttons_state); |
80 | 548df2ac | ths | |
81 | 548df2ac | ths | if ((buttons_state & MOUSE_EVENT_LBUTTON))
|
82 | 4ed59aa5 | ths | buttons |= 0x20;
|
83 | 548df2ac | ths | if ((buttons_state & MOUSE_EVENT_RBUTTON))
|
84 | 4ed59aa5 | ths | buttons |= 0x10;
|
85 | 548df2ac | ths | if ((buttons_state & MOUSE_EVENT_MBUTTON))
|
86 | 4ed59aa5 | ths | buttons |= 0x08;
|
87 | 548df2ac | ths | |
88 | 548df2ac | ths | if (s->absolute) {
|
89 | 4ed59aa5 | ths | x <<= 1;
|
90 | 4ed59aa5 | ths | y <<= 1;
|
91 | 548df2ac | ths | } |
92 | 548df2ac | ths | |
93 | 548df2ac | ths | s->queue[s->nb_queue++] = buttons; |
94 | 548df2ac | ths | s->queue[s->nb_queue++] = x; |
95 | 548df2ac | ths | s->queue[s->nb_queue++] = y; |
96 | 548df2ac | ths | s->queue[s->nb_queue++] = dz; |
97 | 548df2ac | ths | |
98 | 548df2ac | ths | /* need to still generate PS2 events to notify driver to
|
99 | 548df2ac | ths | read from queue */
|
100 | 548df2ac | ths | ps2_mouse_fake_event(s->ps2_mouse); |
101 | 548df2ac | ths | } |
102 | 548df2ac | ths | |
103 | 548df2ac | ths | static void vmmouse_update_handler(VMMouseState *s) |
104 | 548df2ac | ths | { |
105 | 548df2ac | ths | if (s->entry) {
|
106 | 4ed59aa5 | ths | qemu_remove_mouse_event_handler(s->entry); |
107 | 4ed59aa5 | ths | s->entry = NULL;
|
108 | 548df2ac | ths | } |
109 | 548df2ac | ths | if (s->status == 0) |
110 | 4ed59aa5 | ths | s->entry = qemu_add_mouse_event_handler(vmmouse_mouse_event, |
111 | 4ed59aa5 | ths | s, s->absolute, |
112 | 4ed59aa5 | ths | "vmmouse");
|
113 | 548df2ac | ths | } |
114 | 548df2ac | ths | |
115 | 548df2ac | ths | static void vmmouse_read_id(VMMouseState *s) |
116 | 548df2ac | ths | { |
117 | 548df2ac | ths | DPRINTF("vmmouse_read_id()\n");
|
118 | 548df2ac | ths | |
119 | 548df2ac | ths | if (s->nb_queue == VMMOUSE_QUEUE_SIZE)
|
120 | 4ed59aa5 | ths | return;
|
121 | 548df2ac | ths | |
122 | 548df2ac | ths | s->queue[s->nb_queue++] = VMMOUSE_VERSION; |
123 | 548df2ac | ths | s->status = 0;
|
124 | 548df2ac | ths | vmmouse_update_handler(s); |
125 | 548df2ac | ths | } |
126 | 548df2ac | ths | |
127 | 548df2ac | ths | static void vmmouse_request_relative(VMMouseState *s) |
128 | 548df2ac | ths | { |
129 | 548df2ac | ths | DPRINTF("vmmouse_request_relative()\n");
|
130 | 548df2ac | ths | s->absolute = 0;
|
131 | 548df2ac | ths | vmmouse_update_handler(s); |
132 | 548df2ac | ths | } |
133 | 548df2ac | ths | |
134 | 548df2ac | ths | static void vmmouse_request_absolute(VMMouseState *s) |
135 | 548df2ac | ths | { |
136 | 548df2ac | ths | DPRINTF("vmmouse_request_absolute()\n");
|
137 | 548df2ac | ths | s->absolute = 1;
|
138 | 548df2ac | ths | vmmouse_update_handler(s); |
139 | 548df2ac | ths | } |
140 | 548df2ac | ths | |
141 | 548df2ac | ths | static void vmmouse_disable(VMMouseState *s) |
142 | 548df2ac | ths | { |
143 | 548df2ac | ths | DPRINTF("vmmouse_disable()\n");
|
144 | 548df2ac | ths | s->status = 0xffff;
|
145 | 548df2ac | ths | vmmouse_update_handler(s); |
146 | 548df2ac | ths | } |
147 | 548df2ac | ths | |
148 | 548df2ac | ths | static void vmmouse_data(VMMouseState *s, uint32_t *data, uint32_t size) |
149 | 548df2ac | ths | { |
150 | 548df2ac | ths | int i;
|
151 | 548df2ac | ths | |
152 | 548df2ac | ths | DPRINTF("vmmouse_data(%d)\n", size);
|
153 | 548df2ac | ths | |
154 | 548df2ac | ths | if (size == 0 || size > 6 || size > s->nb_queue) { |
155 | 4ed59aa5 | ths | printf("vmmouse: driver requested too much data %d\n", size);
|
156 | 4ed59aa5 | ths | s->status = 0xffff;
|
157 | 4ed59aa5 | ths | vmmouse_update_handler(s); |
158 | 4ed59aa5 | ths | return;
|
159 | 548df2ac | ths | } |
160 | 548df2ac | ths | |
161 | 548df2ac | ths | for (i = 0; i < size; i++) |
162 | 4ed59aa5 | ths | data[i] = s->queue[i]; |
163 | 548df2ac | ths | |
164 | 548df2ac | ths | s->nb_queue -= size; |
165 | 548df2ac | ths | if (s->nb_queue)
|
166 | 4ed59aa5 | ths | memmove(s->queue, &s->queue[size], sizeof(s->queue[0]) * s->nb_queue); |
167 | 548df2ac | ths | } |
168 | 548df2ac | ths | |
169 | 548df2ac | ths | static void vmmouse_get_data(uint32_t *data) |
170 | 548df2ac | ths | { |
171 | 548df2ac | ths | CPUState *env = cpu_single_env; |
172 | 548df2ac | ths | |
173 | 548df2ac | ths | data[0] = env->regs[R_EAX]; data[1] = env->regs[R_EBX]; |
174 | 548df2ac | ths | data[2] = env->regs[R_ECX]; data[3] = env->regs[R_EDX]; |
175 | 548df2ac | ths | data[4] = env->regs[R_ESI]; data[5] = env->regs[R_EDI]; |
176 | 548df2ac | ths | |
177 | 548df2ac | ths | DPRINTF("get_data = {%x, %x, %x, %x, %x, %x}\n",
|
178 | 4ed59aa5 | ths | data[0], data[1], data[2], data[3], data[4], data[5]); |
179 | 548df2ac | ths | } |
180 | 548df2ac | ths | |
181 | 548df2ac | ths | static void vmmouse_set_data(const uint32_t *data) |
182 | 548df2ac | ths | { |
183 | 548df2ac | ths | CPUState *env = cpu_single_env; |
184 | 548df2ac | ths | |
185 | 548df2ac | ths | DPRINTF("set_data = {%x, %x, %x, %x, %x, %x}\n",
|
186 | 4ed59aa5 | ths | data[0], data[1], data[2], data[3], data[4], data[5]); |
187 | 548df2ac | ths | |
188 | 548df2ac | ths | env->regs[R_EAX] = data[0]; env->regs[R_EBX] = data[1]; |
189 | 548df2ac | ths | env->regs[R_ECX] = data[2]; env->regs[R_EDX] = data[3]; |
190 | 548df2ac | ths | env->regs[R_ESI] = data[4]; env->regs[R_EDI] = data[5]; |
191 | 548df2ac | ths | } |
192 | 548df2ac | ths | |
193 | 548df2ac | ths | static uint32_t vmmouse_ioport_read(void *opaque, uint32_t addr) |
194 | 548df2ac | ths | { |
195 | 548df2ac | ths | VMMouseState *s = opaque; |
196 | 548df2ac | ths | uint32_t data[6];
|
197 | 548df2ac | ths | uint16_t command; |
198 | 548df2ac | ths | |
199 | 548df2ac | ths | vmmouse_get_data(data); |
200 | 548df2ac | ths | |
201 | 548df2ac | ths | command = data[2] & 0xFFFF; |
202 | 548df2ac | ths | |
203 | 548df2ac | ths | switch (command) {
|
204 | 548df2ac | ths | case VMMOUSE_STATUS:
|
205 | 4ed59aa5 | ths | data[0] = vmmouse_get_status(s);
|
206 | 4ed59aa5 | ths | break;
|
207 | 548df2ac | ths | case VMMOUSE_COMMAND:
|
208 | 4ed59aa5 | ths | switch (data[1]) { |
209 | 4ed59aa5 | ths | case VMMOUSE_DISABLE:
|
210 | 4ed59aa5 | ths | vmmouse_disable(s); |
211 | 4ed59aa5 | ths | break;
|
212 | 4ed59aa5 | ths | case VMMOUSE_READ_ID:
|
213 | 4ed59aa5 | ths | vmmouse_read_id(s); |
214 | 4ed59aa5 | ths | break;
|
215 | 4ed59aa5 | ths | case VMMOUSE_REQUEST_RELATIVE:
|
216 | 4ed59aa5 | ths | vmmouse_request_relative(s); |
217 | 4ed59aa5 | ths | break;
|
218 | 4ed59aa5 | ths | case VMMOUSE_REQUEST_ABSOLUTE:
|
219 | 4ed59aa5 | ths | vmmouse_request_absolute(s); |
220 | 4ed59aa5 | ths | break;
|
221 | 4ed59aa5 | ths | default:
|
222 | 4ed59aa5 | ths | printf("vmmouse: unknown command %x\n", data[1]); |
223 | 4ed59aa5 | ths | break;
|
224 | 4ed59aa5 | ths | } |
225 | 4ed59aa5 | ths | break;
|
226 | 548df2ac | ths | case VMMOUSE_DATA:
|
227 | 4ed59aa5 | ths | vmmouse_data(s, data, data[1]);
|
228 | 4ed59aa5 | ths | break;
|
229 | 548df2ac | ths | default:
|
230 | 4ed59aa5 | ths | printf("vmmouse: unknown command %x\n", command);
|
231 | 4ed59aa5 | ths | break;
|
232 | 548df2ac | ths | } |
233 | 548df2ac | ths | |
234 | 548df2ac | ths | vmmouse_set_data(data); |
235 | 548df2ac | ths | return data[0]; |
236 | 548df2ac | ths | } |
237 | 548df2ac | ths | |
238 | e59fb374 | Juan Quintela | static int vmmouse_post_load(void *opaque, int version_id) |
239 | 548df2ac | ths | { |
240 | 548df2ac | ths | VMMouseState *s = opaque; |
241 | 548df2ac | ths | |
242 | 548df2ac | ths | vmmouse_update_handler(s); |
243 | 548df2ac | ths | return 0; |
244 | 548df2ac | ths | } |
245 | 548df2ac | ths | |
246 | bcb58d9a | Juan Quintela | static const VMStateDescription vmstate_vmmouse = { |
247 | bcb58d9a | Juan Quintela | .name = "vmmouse",
|
248 | bcb58d9a | Juan Quintela | .version_id = 0,
|
249 | bcb58d9a | Juan Quintela | .minimum_version_id = 0,
|
250 | bcb58d9a | Juan Quintela | .minimum_version_id_old = 0,
|
251 | bcb58d9a | Juan Quintela | .post_load = vmmouse_post_load, |
252 | bcb58d9a | Juan Quintela | .fields = (VMStateField []) { |
253 | bcb58d9a | Juan Quintela | VMSTATE_INT32_EQUAL(queue_size, VMMouseState), |
254 | bcb58d9a | Juan Quintela | VMSTATE_UINT32_ARRAY(queue, VMMouseState, VMMOUSE_QUEUE_SIZE), |
255 | bcb58d9a | Juan Quintela | VMSTATE_UINT16(nb_queue, VMMouseState), |
256 | bcb58d9a | Juan Quintela | VMSTATE_UINT16(status, VMMouseState), |
257 | bcb58d9a | Juan Quintela | VMSTATE_UINT8(absolute, VMMouseState), |
258 | bcb58d9a | Juan Quintela | VMSTATE_END_OF_LIST() |
259 | bcb58d9a | Juan Quintela | } |
260 | bcb58d9a | Juan Quintela | }; |
261 | bcb58d9a | Juan Quintela | |
262 | 548df2ac | ths | void *vmmouse_init(void *m) |
263 | 548df2ac | ths | { |
264 | 548df2ac | ths | VMMouseState *s = NULL;
|
265 | 548df2ac | ths | |
266 | 548df2ac | ths | DPRINTF("vmmouse_init\n");
|
267 | 548df2ac | ths | |
268 | 548df2ac | ths | s = qemu_mallocz(sizeof(VMMouseState));
|
269 | 548df2ac | ths | |
270 | 548df2ac | ths | s->status = 0xffff;
|
271 | 548df2ac | ths | s->ps2_mouse = m; |
272 | bcb58d9a | Juan Quintela | s->queue_size = VMMOUSE_QUEUE_SIZE; |
273 | 548df2ac | ths | |
274 | 93342807 | ths | vmport_register(VMMOUSE_STATUS, vmmouse_ioport_read, s); |
275 | 93342807 | ths | vmport_register(VMMOUSE_COMMAND, vmmouse_ioport_read, s); |
276 | 93342807 | ths | vmport_register(VMMOUSE_DATA, vmmouse_ioport_read, s); |
277 | bcb58d9a | Juan Quintela | vmstate_register(0, &vmstate_vmmouse, s);
|
278 | 548df2ac | ths | |
279 | 548df2ac | ths | return s;
|
280 | 548df2ac | ths | } |