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
24 #include "qemu/osdep.h"
26 #include "ui/console.h"
27 #include "hw/input/ps2.h"
28 #include "hw/i386/pc.h"
31 /* debug only vmmouse */
32 //#define DEBUG_VMMOUSE
34 /* VMMouse Commands */
35 #define VMMOUSE_GETVERSION 10
36 #define VMMOUSE_DATA 39
37 #define VMMOUSE_STATUS 40
38 #define VMMOUSE_COMMAND 41
40 #define VMMOUSE_READ_ID 0x45414552
41 #define VMMOUSE_DISABLE 0x000000f5
42 #define VMMOUSE_REQUEST_RELATIVE 0x4c455252
43 #define VMMOUSE_REQUEST_ABSOLUTE 0x53424152
45 #define VMMOUSE_QUEUE_SIZE 1024
47 #define VMMOUSE_VERSION 0x3442554a
50 #define DPRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
52 #define DPRINTF(fmt, ...) do { } while (0)
55 #define TYPE_VMMOUSE "vmmouse"
56 #define VMMOUSE(obj) OBJECT_CHECK(VMMouseState, (obj), TYPE_VMMOUSE)
58 typedef struct VMMouseState
62 uint32_t queue
[VMMOUSE_QUEUE_SIZE
];
67 QEMUPutMouseEntry
*entry
;
71 static uint32_t vmmouse_get_status(VMMouseState
*s
)
73 DPRINTF("vmmouse_get_status()\n");
74 return (s
->status
<< 16) | s
->nb_queue
;
77 static void vmmouse_mouse_event(void *opaque
, int x
, int y
, int dz
, int buttons_state
)
79 VMMouseState
*s
= opaque
;
82 if (s
->nb_queue
> (VMMOUSE_QUEUE_SIZE
- 4))
85 DPRINTF("vmmouse_mouse_event(%d, %d, %d, %d)\n",
86 x
, y
, dz
, buttons_state
);
88 if ((buttons_state
& MOUSE_EVENT_LBUTTON
))
90 if ((buttons_state
& MOUSE_EVENT_RBUTTON
))
92 if ((buttons_state
& MOUSE_EVENT_MBUTTON
))
100 s
->queue
[s
->nb_queue
++] = buttons
;
101 s
->queue
[s
->nb_queue
++] = x
;
102 s
->queue
[s
->nb_queue
++] = y
;
103 s
->queue
[s
->nb_queue
++] = dz
;
105 /* need to still generate PS2 events to notify driver to
107 i8042_isa_mouse_fake_event(s
->ps2_mouse
);
110 static void vmmouse_remove_handler(VMMouseState
*s
)
113 qemu_remove_mouse_event_handler(s
->entry
);
118 static void vmmouse_update_handler(VMMouseState
*s
, int absolute
)
120 if (s
->status
!= 0) {
123 if (s
->absolute
!= absolute
) {
124 s
->absolute
= absolute
;
125 vmmouse_remove_handler(s
);
127 if (s
->entry
== NULL
) {
128 s
->entry
= qemu_add_mouse_event_handler(vmmouse_mouse_event
,
131 qemu_activate_mouse_event_handler(s
->entry
);
135 static void vmmouse_read_id(VMMouseState
*s
)
137 DPRINTF("vmmouse_read_id()\n");
139 if (s
->nb_queue
== VMMOUSE_QUEUE_SIZE
)
142 s
->queue
[s
->nb_queue
++] = VMMOUSE_VERSION
;
146 static void vmmouse_request_relative(VMMouseState
*s
)
148 DPRINTF("vmmouse_request_relative()\n");
149 vmmouse_update_handler(s
, 0);
152 static void vmmouse_request_absolute(VMMouseState
*s
)
154 DPRINTF("vmmouse_request_absolute()\n");
155 vmmouse_update_handler(s
, 1);
158 static void vmmouse_disable(VMMouseState
*s
)
160 DPRINTF("vmmouse_disable()\n");
162 vmmouse_remove_handler(s
);
165 static void vmmouse_data(VMMouseState
*s
, uint32_t *data
, uint32_t size
)
169 DPRINTF("vmmouse_data(%d)\n", size
);
171 if (size
== 0 || size
> 6 || size
> s
->nb_queue
) {
172 printf("vmmouse: driver requested too much data %d\n", size
);
174 vmmouse_remove_handler(s
);
178 for (i
= 0; i
< size
; i
++)
179 data
[i
] = s
->queue
[i
];
183 memmove(s
->queue
, &s
->queue
[size
], sizeof(s
->queue
[0]) * s
->nb_queue
);
186 static uint32_t vmmouse_ioport_read(void *opaque
, uint32_t addr
)
188 VMMouseState
*s
= opaque
;
192 vmmouse_get_data(data
);
194 command
= data
[2] & 0xFFFF;
198 data
[0] = vmmouse_get_status(s
);
200 case VMMOUSE_COMMAND
:
202 case VMMOUSE_DISABLE
:
205 case VMMOUSE_READ_ID
:
208 case VMMOUSE_REQUEST_RELATIVE
:
209 vmmouse_request_relative(s
);
211 case VMMOUSE_REQUEST_ABSOLUTE
:
212 vmmouse_request_absolute(s
);
215 printf("vmmouse: unknown command %x\n", data
[1]);
220 vmmouse_data(s
, data
, data
[1]);
223 printf("vmmouse: unknown command %x\n", command
);
227 vmmouse_set_data(data
);
231 static int vmmouse_post_load(void *opaque
, int version_id
)
233 VMMouseState
*s
= opaque
;
235 vmmouse_remove_handler(s
);
236 vmmouse_update_handler(s
, s
->absolute
);
240 static const VMStateDescription vmstate_vmmouse
= {
243 .minimum_version_id
= 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 void vmmouse_realizefn(DeviceState
*dev
, Error
**errp
)
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
);
275 static Property vmmouse_properties
[] = {
276 DEFINE_PROP_PTR("ps2_mouse", VMMouseState
, ps2_mouse
),
277 DEFINE_PROP_END_OF_LIST(),
280 static void vmmouse_class_initfn(ObjectClass
*klass
, void *data
)
282 DeviceClass
*dc
= DEVICE_CLASS(klass
);
284 dc
->realize
= vmmouse_realizefn
;
285 dc
->reset
= vmmouse_reset
;
286 dc
->vmsd
= &vmstate_vmmouse
;
287 dc
->props
= vmmouse_properties
;
288 /* Reason: pointer property "ps2_mouse" */
289 dc
->cannot_instantiate_with_device_add_yet
= true;
292 static const TypeInfo vmmouse_info
= {
293 .name
= TYPE_VMMOUSE
,
294 .parent
= TYPE_ISA_DEVICE
,
295 .instance_size
= sizeof(VMMouseState
),
296 .class_init
= vmmouse_class_initfn
,
299 static void vmmouse_register_types(void)
301 type_register_static(&vmmouse_info
);
304 type_init(vmmouse_register_types
)