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
25 #include "ui/console.h"
26 #include "hw/input/ps2.h"
27 #include "hw/i386/pc.h"
30 /* debug only vmmouse */
31 //#define DEBUG_VMMOUSE
33 /* VMMouse Commands */
34 #define VMMOUSE_GETVERSION 10
35 #define VMMOUSE_DATA 39
36 #define VMMOUSE_STATUS 40
37 #define VMMOUSE_COMMAND 41
39 #define VMMOUSE_READ_ID 0x45414552
40 #define VMMOUSE_DISABLE 0x000000f5
41 #define VMMOUSE_REQUEST_RELATIVE 0x4c455252
42 #define VMMOUSE_REQUEST_ABSOLUTE 0x53424152
44 #define VMMOUSE_QUEUE_SIZE 1024
46 #define VMMOUSE_VERSION 0x3442554a
49 #define DPRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
51 #define DPRINTF(fmt, ...) do { } while (0)
54 #define TYPE_VMMOUSE "vmmouse"
55 #define VMMOUSE(obj) OBJECT_CHECK(VMMouseState, (obj), TYPE_VMMOUSE)
57 typedef struct VMMouseState
61 uint32_t queue
[VMMOUSE_QUEUE_SIZE
];
66 QEMUPutMouseEntry
*entry
;
70 static uint32_t vmmouse_get_status(VMMouseState
*s
)
72 DPRINTF("vmmouse_get_status()\n");
73 return (s
->status
<< 16) | s
->nb_queue
;
76 static void vmmouse_mouse_event(void *opaque
, int x
, int y
, int dz
, int buttons_state
)
78 VMMouseState
*s
= opaque
;
81 if (s
->nb_queue
> (VMMOUSE_QUEUE_SIZE
- 4))
84 DPRINTF("vmmouse_mouse_event(%d, %d, %d, %d)\n",
85 x
, y
, dz
, buttons_state
);
87 if ((buttons_state
& MOUSE_EVENT_LBUTTON
))
89 if ((buttons_state
& MOUSE_EVENT_RBUTTON
))
91 if ((buttons_state
& MOUSE_EVENT_MBUTTON
))
99 s
->queue
[s
->nb_queue
++] = buttons
;
100 s
->queue
[s
->nb_queue
++] = x
;
101 s
->queue
[s
->nb_queue
++] = y
;
102 s
->queue
[s
->nb_queue
++] = dz
;
104 /* need to still generate PS2 events to notify driver to
106 i8042_isa_mouse_fake_event(s
->ps2_mouse
);
109 static void vmmouse_remove_handler(VMMouseState
*s
)
112 qemu_remove_mouse_event_handler(s
->entry
);
117 static void vmmouse_update_handler(VMMouseState
*s
, int absolute
)
119 if (s
->status
!= 0) {
122 if (s
->absolute
!= absolute
) {
123 s
->absolute
= absolute
;
124 vmmouse_remove_handler(s
);
126 if (s
->entry
== NULL
) {
127 s
->entry
= qemu_add_mouse_event_handler(vmmouse_mouse_event
,
130 qemu_activate_mouse_event_handler(s
->entry
);
134 static void vmmouse_read_id(VMMouseState
*s
)
136 DPRINTF("vmmouse_read_id()\n");
138 if (s
->nb_queue
== VMMOUSE_QUEUE_SIZE
)
141 s
->queue
[s
->nb_queue
++] = VMMOUSE_VERSION
;
145 static void vmmouse_request_relative(VMMouseState
*s
)
147 DPRINTF("vmmouse_request_relative()\n");
148 vmmouse_update_handler(s
, 0);
151 static void vmmouse_request_absolute(VMMouseState
*s
)
153 DPRINTF("vmmouse_request_absolute()\n");
154 vmmouse_update_handler(s
, 1);
157 static void vmmouse_disable(VMMouseState
*s
)
159 DPRINTF("vmmouse_disable()\n");
161 vmmouse_remove_handler(s
);
164 static void vmmouse_data(VMMouseState
*s
, uint32_t *data
, uint32_t size
)
168 DPRINTF("vmmouse_data(%d)\n", size
);
170 if (size
== 0 || size
> 6 || size
> s
->nb_queue
) {
171 printf("vmmouse: driver requested too much data %d\n", size
);
173 vmmouse_remove_handler(s
);
177 for (i
= 0; i
< size
; i
++)
178 data
[i
] = s
->queue
[i
];
182 memmove(s
->queue
, &s
->queue
[size
], sizeof(s
->queue
[0]) * s
->nb_queue
);
185 static uint32_t vmmouse_ioport_read(void *opaque
, uint32_t addr
)
187 VMMouseState
*s
= opaque
;
191 vmmouse_get_data(data
);
193 command
= data
[2] & 0xFFFF;
197 data
[0] = vmmouse_get_status(s
);
199 case VMMOUSE_COMMAND
:
201 case VMMOUSE_DISABLE
:
204 case VMMOUSE_READ_ID
:
207 case VMMOUSE_REQUEST_RELATIVE
:
208 vmmouse_request_relative(s
);
210 case VMMOUSE_REQUEST_ABSOLUTE
:
211 vmmouse_request_absolute(s
);
214 printf("vmmouse: unknown command %x\n", data
[1]);
219 vmmouse_data(s
, data
, data
[1]);
222 printf("vmmouse: unknown command %x\n", command
);
226 vmmouse_set_data(data
);
230 static int vmmouse_post_load(void *opaque
, int version_id
)
232 VMMouseState
*s
= opaque
;
234 vmmouse_remove_handler(s
);
235 vmmouse_update_handler(s
, s
->absolute
);
239 static const VMStateDescription vmstate_vmmouse
= {
242 .minimum_version_id
= 0,
243 .minimum_version_id_old
= 0,
244 .post_load
= vmmouse_post_load
,
245 .fields
= (VMStateField
[]) {
246 VMSTATE_INT32_EQUAL(queue_size
, VMMouseState
),
247 VMSTATE_UINT32_ARRAY(queue
, VMMouseState
, VMMOUSE_QUEUE_SIZE
),
248 VMSTATE_UINT16(nb_queue
, VMMouseState
),
249 VMSTATE_UINT16(status
, VMMouseState
),
250 VMSTATE_UINT8(absolute
, VMMouseState
),
251 VMSTATE_END_OF_LIST()
255 static void vmmouse_reset(DeviceState
*d
)
257 VMMouseState
*s
= VMMOUSE(d
);
259 s
->queue_size
= VMMOUSE_QUEUE_SIZE
;
264 static int vmmouse_initfn(ISADevice
*dev
)
266 VMMouseState
*s
= VMMOUSE(dev
);
268 DPRINTF("vmmouse_init\n");
270 vmport_register(VMMOUSE_STATUS
, vmmouse_ioport_read
, s
);
271 vmport_register(VMMOUSE_COMMAND
, vmmouse_ioport_read
, s
);
272 vmport_register(VMMOUSE_DATA
, vmmouse_ioport_read
, s
);
277 static Property vmmouse_properties
[] = {
278 DEFINE_PROP_PTR("ps2_mouse", VMMouseState
, ps2_mouse
),
279 DEFINE_PROP_END_OF_LIST(),
282 static void vmmouse_class_initfn(ObjectClass
*klass
, void *data
)
284 DeviceClass
*dc
= DEVICE_CLASS(klass
);
285 ISADeviceClass
*ic
= ISA_DEVICE_CLASS(klass
);
286 ic
->init
= vmmouse_initfn
;
288 dc
->reset
= vmmouse_reset
;
289 dc
->vmsd
= &vmstate_vmmouse
;
290 dc
->props
= vmmouse_properties
;
293 static const TypeInfo vmmouse_info
= {
294 .name
= TYPE_VMMOUSE
,
295 .parent
= TYPE_ISA_DEVICE
,
296 .instance_size
= sizeof(VMMouseState
),
297 .class_init
= vmmouse_class_initfn
,
300 static void vmmouse_register_types(void)
302 type_register_static(&vmmouse_info
);
305 type_init(vmmouse_register_types
)