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