2 * QEMU VMMouse emulation
4 * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
29 /* debug only vmmouse */
30 //#define DEBUG_VMMOUSE
32 /* VMMouse Commands */
33 #define VMMOUSE_GETVERSION 10
34 #define VMMOUSE_DATA 39
35 #define VMMOUSE_STATUS 40
36 #define VMMOUSE_COMMAND 41
38 #define VMMOUSE_READ_ID 0x45414552
39 #define VMMOUSE_DISABLE 0x000000f5
40 #define VMMOUSE_REQUEST_RELATIVE 0x4c455252
41 #define VMMOUSE_REQUEST_ABSOLUTE 0x53424152
43 #define VMMOUSE_QUEUE_SIZE 1024
45 #define VMMOUSE_VERSION 0x3442554a
48 #define DPRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
50 #define DPRINTF(fmt, ...) do { } while (0)
53 typedef struct _VMMouseState
55 uint32_t queue
[VMMOUSE_QUEUE_SIZE
];
60 QEMUPutMouseEntry
*entry
;
64 static uint32_t vmmouse_get_status(VMMouseState
*s
)
66 DPRINTF("vmmouse_get_status()\n");
67 return (s
->status
<< 16) | s
->nb_queue
;
70 static void vmmouse_mouse_event(void *opaque
, int x
, int y
, int dz
, int buttons_state
)
72 VMMouseState
*s
= opaque
;
75 if (s
->nb_queue
> (VMMOUSE_QUEUE_SIZE
- 4))
78 DPRINTF("vmmouse_mouse_event(%d, %d, %d, %d)\n",
79 x
, y
, dz
, buttons_state
);
81 if ((buttons_state
& MOUSE_EVENT_LBUTTON
))
83 if ((buttons_state
& MOUSE_EVENT_RBUTTON
))
85 if ((buttons_state
& MOUSE_EVENT_MBUTTON
))
93 s
->queue
[s
->nb_queue
++] = buttons
;
94 s
->queue
[s
->nb_queue
++] = x
;
95 s
->queue
[s
->nb_queue
++] = y
;
96 s
->queue
[s
->nb_queue
++] = dz
;
98 /* need to still generate PS2 events to notify driver to
100 i8042_isa_mouse_fake_event(s
->ps2_mouse
);
103 static void vmmouse_update_handler(VMMouseState
*s
)
106 qemu_remove_mouse_event_handler(s
->entry
);
110 s
->entry
= qemu_add_mouse_event_handler(vmmouse_mouse_event
,
115 static void vmmouse_read_id(VMMouseState
*s
)
117 DPRINTF("vmmouse_read_id()\n");
119 if (s
->nb_queue
== VMMOUSE_QUEUE_SIZE
)
122 s
->queue
[s
->nb_queue
++] = VMMOUSE_VERSION
;
124 vmmouse_update_handler(s
);
127 static void vmmouse_request_relative(VMMouseState
*s
)
129 DPRINTF("vmmouse_request_relative()\n");
131 vmmouse_update_handler(s
);
134 static void vmmouse_request_absolute(VMMouseState
*s
)
136 DPRINTF("vmmouse_request_absolute()\n");
138 vmmouse_update_handler(s
);
141 static void vmmouse_disable(VMMouseState
*s
)
143 DPRINTF("vmmouse_disable()\n");
145 vmmouse_update_handler(s
);
148 static void vmmouse_data(VMMouseState
*s
, uint32_t *data
, uint32_t size
)
152 DPRINTF("vmmouse_data(%d)\n", size
);
154 if (size
== 0 || size
> 6 || size
> s
->nb_queue
) {
155 printf("vmmouse: driver requested too much data %d\n", size
);
157 vmmouse_update_handler(s
);
161 for (i
= 0; i
< size
; i
++)
162 data
[i
] = s
->queue
[i
];
166 memmove(s
->queue
, &s
->queue
[size
], sizeof(s
->queue
[0]) * s
->nb_queue
);
169 static void vmmouse_get_data(uint32_t *data
)
171 CPUState
*env
= cpu_single_env
;
173 data
[0] = env
->regs
[R_EAX
]; data
[1] = env
->regs
[R_EBX
];
174 data
[2] = env
->regs
[R_ECX
]; data
[3] = env
->regs
[R_EDX
];
175 data
[4] = env
->regs
[R_ESI
]; data
[5] = env
->regs
[R_EDI
];
177 DPRINTF("get_data = {%x, %x, %x, %x, %x, %x}\n",
178 data
[0], data
[1], data
[2], data
[3], data
[4], data
[5]);
181 static void vmmouse_set_data(const uint32_t *data
)
183 CPUState
*env
= cpu_single_env
;
185 DPRINTF("set_data = {%x, %x, %x, %x, %x, %x}\n",
186 data
[0], data
[1], data
[2], data
[3], data
[4], data
[5]);
188 env
->regs
[R_EAX
] = data
[0]; env
->regs
[R_EBX
] = data
[1];
189 env
->regs
[R_ECX
] = data
[2]; env
->regs
[R_EDX
] = data
[3];
190 env
->regs
[R_ESI
] = data
[4]; env
->regs
[R_EDI
] = data
[5];
193 static uint32_t vmmouse_ioport_read(void *opaque
, uint32_t addr
)
195 VMMouseState
*s
= opaque
;
199 vmmouse_get_data(data
);
201 command
= data
[2] & 0xFFFF;
205 data
[0] = vmmouse_get_status(s
);
207 case VMMOUSE_COMMAND
:
209 case VMMOUSE_DISABLE
:
212 case VMMOUSE_READ_ID
:
215 case VMMOUSE_REQUEST_RELATIVE
:
216 vmmouse_request_relative(s
);
218 case VMMOUSE_REQUEST_ABSOLUTE
:
219 vmmouse_request_absolute(s
);
222 printf("vmmouse: unknown command %x\n", data
[1]);
227 vmmouse_data(s
, data
, data
[1]);
230 printf("vmmouse: unknown command %x\n", command
);
234 vmmouse_set_data(data
);
238 static int vmmouse_post_load(void *opaque
, int version_id
)
240 VMMouseState
*s
= opaque
;
242 vmmouse_update_handler(s
);
246 static const VMStateDescription vmstate_vmmouse
= {
249 .minimum_version_id
= 0,
250 .minimum_version_id_old
= 0,
251 .post_load
= vmmouse_post_load
,
252 .fields
= (VMStateField
[]) {
253 VMSTATE_INT32_EQUAL(queue_size
, VMMouseState
),
254 VMSTATE_UINT32_ARRAY(queue
, VMMouseState
, VMMOUSE_QUEUE_SIZE
),
255 VMSTATE_UINT16(nb_queue
, VMMouseState
),
256 VMSTATE_UINT16(status
, VMMouseState
),
257 VMSTATE_UINT8(absolute
, VMMouseState
),
258 VMSTATE_END_OF_LIST()
262 void *vmmouse_init(void *m
)
264 VMMouseState
*s
= NULL
;
266 DPRINTF("vmmouse_init\n");
268 s
= qemu_mallocz(sizeof(VMMouseState
));
272 s
->queue_size
= VMMOUSE_QUEUE_SIZE
;
274 vmport_register(VMMOUSE_STATUS
, vmmouse_ioport_read
, s
);
275 vmport_register(VMMOUSE_COMMAND
, vmmouse_ioport_read
, s
);
276 vmport_register(VMMOUSE_DATA
, vmmouse_ioport_read
, s
);
277 vmstate_register(NULL
, 0, &vmstate_vmmouse
, s
);