1 | /* $Id: MouseImpl.cpp 26173 2010-02-02 21:11:09Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox frontends: Basic Frontend (BFE):
|
---|
4 | * Implementation of Mouse class
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | *
|
---|
18 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
19 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
20 | * additional information or have any questions.
|
---|
21 | */
|
---|
22 |
|
---|
23 | #ifdef VBOXBFE_WITHOUT_COM
|
---|
24 | # include "COMDefs.h"
|
---|
25 | #else
|
---|
26 | # include <VBox/com/defs.h>
|
---|
27 | #endif
|
---|
28 | #include <VBox/pdm.h>
|
---|
29 | #include <VBox/cfgm.h>
|
---|
30 | #include <VBox/err.h>
|
---|
31 | #include <iprt/assert.h>
|
---|
32 | #include <VBox/log.h>
|
---|
33 | #include <iprt/asm.h>
|
---|
34 | #include <iprt/uuid.h>
|
---|
35 | #include <VBox/VMMDev.h>
|
---|
36 | #include "MouseImpl.h"
|
---|
37 | #include "DisplayImpl.h"
|
---|
38 | #include "VMMDevInterface.h"
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Mouse driver instance data.
|
---|
42 | */
|
---|
43 | typedef struct DRVMAINMOUSE
|
---|
44 | {
|
---|
45 | /** Pointer to the associated mouse driver. */
|
---|
46 | Mouse *mpDrv;
|
---|
47 | /** Pointer to the driver instance structure. */
|
---|
48 | PPDMDRVINS pDrvIns;
|
---|
49 | /** Pointer to the mouse port interface of the driver/device above us. */
|
---|
50 | PPDMIMOUSEPORT pUpPort;
|
---|
51 | /** Our mouse connector interface. */
|
---|
52 | PDMIMOUSECONNECTOR Connector;
|
---|
53 | } DRVMAINMOUSE, *PDRVMAINMOUSE;
|
---|
54 |
|
---|
55 |
|
---|
56 | // IMouse methods
|
---|
57 | /////////////////////////////////////////////////////////////////////////////
|
---|
58 |
|
---|
59 | int Mouse::setAbsoluteCoordinates(bool a_fAbsolute)
|
---|
60 | {
|
---|
61 | this->fAbsolute = a_fAbsolute;
|
---|
62 | return S_OK;
|
---|
63 | }
|
---|
64 |
|
---|
65 | int Mouse::setNeedsHostCursor(bool a_fNeedsHostCursor)
|
---|
66 | {
|
---|
67 | this->fNeedsHostCursor = a_fNeedsHostCursor;
|
---|
68 | return S_OK;
|
---|
69 | }
|
---|
70 |
|
---|
71 | int Mouse::setHostCursor(bool enable)
|
---|
72 | {
|
---|
73 | uHostCaps = enable ? 0 : VMMDEV_MOUSE_HOST_CANNOT_HWPOINTER;
|
---|
74 | gVMMDev->SetMouseCapabilities(uHostCaps);
|
---|
75 | return S_OK;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Send a mouse event.
|
---|
80 | *
|
---|
81 | * @returns COM status code
|
---|
82 | * @param dx X movement
|
---|
83 | * @param dy Y movement
|
---|
84 | * @param dz Z movement
|
---|
85 | * @param buttonState The mouse button state
|
---|
86 | */
|
---|
87 | int Mouse::PutMouseEvent(LONG dx, LONG dy, LONG dz, LONG buttonState)
|
---|
88 | {
|
---|
89 | uint32_t mouseCaps;
|
---|
90 | gVMMDev->QueryMouseCapabilities(&mouseCaps);
|
---|
91 |
|
---|
92 | /*
|
---|
93 | * This method being called implies that the host no
|
---|
94 | * longer wants to use absolute coordinates. If the VMM
|
---|
95 | * device isn't aware of that yet, tell it.
|
---|
96 | */
|
---|
97 | if (mouseCaps & VMMDEV_MOUSE_HOST_CAN_ABSOLUTE)
|
---|
98 | {
|
---|
99 | gVMMDev->SetMouseCapabilities(uHostCaps);
|
---|
100 | }
|
---|
101 |
|
---|
102 | uint32_t fButtons = 0;
|
---|
103 | if (buttonState & PDMIMOUSEPORT_BUTTON_LEFT)
|
---|
104 | fButtons |= PDMIMOUSEPORT_BUTTON_LEFT;
|
---|
105 | if (buttonState & PDMIMOUSEPORT_BUTTON_RIGHT)
|
---|
106 | fButtons |= PDMIMOUSEPORT_BUTTON_RIGHT;
|
---|
107 | if (buttonState & PDMIMOUSEPORT_BUTTON_MIDDLE)
|
---|
108 | fButtons |= PDMIMOUSEPORT_BUTTON_MIDDLE;
|
---|
109 |
|
---|
110 | int vrc = mpDrv->pUpPort->pfnPutEvent(mpDrv->pUpPort, dx, dy, dz, 0 /* Horizontal wheel */, fButtons);
|
---|
111 | if (RT_FAILURE (vrc))
|
---|
112 | return E_FAIL;
|
---|
113 |
|
---|
114 | return S_OK;
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Send an absolute mouse event to the VM. This only works
|
---|
119 | * when the required guest support has been installed.
|
---|
120 | *
|
---|
121 | * @returns COM status code
|
---|
122 | * @param x X position (pixel)
|
---|
123 | * @param y Y position (pixel)
|
---|
124 | * @param dz Z movement
|
---|
125 | * @param buttonState The mouse button state
|
---|
126 | */
|
---|
127 | int Mouse::PutMouseEventAbsolute(LONG x, LONG y, LONG dz, LONG buttonState)
|
---|
128 | {
|
---|
129 | uint32_t mouseCaps;
|
---|
130 | gVMMDev->QueryMouseCapabilities(&mouseCaps);
|
---|
131 |
|
---|
132 | /*
|
---|
133 | * This method being called implies that the host no
|
---|
134 | * longer wants to use absolute coordinates. If the VMM
|
---|
135 | * device isn't aware of that yet, tell it.
|
---|
136 | */
|
---|
137 | if (!(mouseCaps & VMMDEV_MOUSE_HOST_CAN_ABSOLUTE))
|
---|
138 | {
|
---|
139 | gVMMDev->SetMouseCapabilities(uHostCaps | VMMDEV_MOUSE_HOST_CAN_ABSOLUTE);
|
---|
140 | }
|
---|
141 |
|
---|
142 | ULONG displayWidth;
|
---|
143 | ULONG displayHeight;
|
---|
144 | displayHeight = gDisplay->getHeight();
|
---|
145 | displayWidth = gDisplay->getWidth();
|
---|
146 |
|
---|
147 | uint32_t mouseXAbs = (x * 0xFFFF) / displayWidth;
|
---|
148 | uint32_t mouseYAbs = (y * 0xFFFF) / displayHeight;
|
---|
149 |
|
---|
150 | /*
|
---|
151 | * Send the absolute mouse position to the VMM device
|
---|
152 | */
|
---|
153 | int vrc = gVMMDev->SetAbsoluteMouse(mouseXAbs, mouseYAbs);
|
---|
154 | AssertRC(vrc);
|
---|
155 |
|
---|
156 | // check if the guest actually wants absolute mouse positions
|
---|
157 | if (mouseCaps & VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE)
|
---|
158 | {
|
---|
159 | uint32_t fButtons = 0;
|
---|
160 | if (buttonState & PDMIMOUSEPORT_BUTTON_LEFT)
|
---|
161 | fButtons |= PDMIMOUSEPORT_BUTTON_LEFT;
|
---|
162 | if (buttonState & PDMIMOUSEPORT_BUTTON_RIGHT)
|
---|
163 | fButtons |= PDMIMOUSEPORT_BUTTON_RIGHT;
|
---|
164 | if (buttonState & PDMIMOUSEPORT_BUTTON_MIDDLE)
|
---|
165 | fButtons |= PDMIMOUSEPORT_BUTTON_MIDDLE;
|
---|
166 |
|
---|
167 | vrc = mpDrv->pUpPort->pfnPutEvent(mpDrv->pUpPort, 1, 1, dz, 0 /* Horizontal wheel */, fButtons);
|
---|
168 | if (RT_FAILURE (vrc))
|
---|
169 | return E_FAIL;
|
---|
170 | }
|
---|
171 |
|
---|
172 | return S_OK;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /////////////////////////////////////////////////////////////////////////////
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * @interface_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
179 | */
|
---|
180 | DECLCALLBACK(void *) Mouse::drvQueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
181 | {
|
---|
182 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
|
---|
183 | PDRVMAINMOUSE pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
|
---|
184 | if (RTUuidCompare2Strs(pszIID, PDMIBASE_IID) == 0)
|
---|
185 | return &pDrvIns->IBase;
|
---|
186 | if (RTUuidCompare2Strs(pszIID, PDMIMOUSECONNECTOR_IID) == 0)
|
---|
187 | return &pDrv->Connector;
|
---|
188 | return NULL;
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Destruct a mouse driver instance.
|
---|
194 | *
|
---|
195 | * @returns VBox status.
|
---|
196 | * @param pDrvIns The driver instance data.
|
---|
197 | */
|
---|
198 | DECLCALLBACK(void) Mouse::drvDestruct(PPDMDRVINS pDrvIns)
|
---|
199 | {
|
---|
200 | //PDRVMAINMOUSE pData = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
|
---|
201 | LogFlow(("Mouse::drvDestruct: iInstance=%d\n", pDrvIns->iInstance));
|
---|
202 | }
|
---|
203 |
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Construct a mouse driver instance.
|
---|
207 | *
|
---|
208 | * @copydoc FNPDMDRVCONSTRUCT
|
---|
209 | */
|
---|
210 | DECLCALLBACK(int) Mouse::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
|
---|
211 | {
|
---|
212 | PDRVMAINMOUSE pData = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
|
---|
213 | LogFlow(("drvMainMouse_Construct: iInstance=%d\n", pDrvIns->iInstance));
|
---|
214 |
|
---|
215 | /*
|
---|
216 | * Validate configuration.
|
---|
217 | */
|
---|
218 | if (!CFGMR3AreValuesValid(pCfg, "Object\0"))
|
---|
219 | return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
|
---|
220 | AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
|
---|
221 | ("Configuration error: Not possible to attach anything to this driver!\n"),
|
---|
222 | VERR_PDM_DRVINS_NO_ATTACH);
|
---|
223 |
|
---|
224 | /*
|
---|
225 | * IBase.
|
---|
226 | */
|
---|
227 | pDrvIns->IBase.pfnQueryInterface = Mouse::drvQueryInterface;
|
---|
228 |
|
---|
229 | /*
|
---|
230 | * Get the IMousePort interface of the above driver/device.
|
---|
231 | */
|
---|
232 | pData->pUpPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIMOUSEPORT);
|
---|
233 | if (!pData->pUpPort)
|
---|
234 | {
|
---|
235 | AssertMsgFailed(("Configuration error: No mouse port interface above!\n"));
|
---|
236 | return VERR_PDM_MISSING_INTERFACE_ABOVE;
|
---|
237 | }
|
---|
238 |
|
---|
239 | /*
|
---|
240 | * Get the Mouse object pointer and update the mpDrv member.
|
---|
241 | */
|
---|
242 | void *pv;
|
---|
243 | int rc = CFGMR3QueryPtr(pCfg, "Object", &pv);
|
---|
244 | if (RT_FAILURE(rc))
|
---|
245 | {
|
---|
246 | AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
|
---|
247 | return rc;
|
---|
248 | }
|
---|
249 |
|
---|
250 | pData->mpDrv = (Mouse*)pv; /** @todo Check this cast! */
|
---|
251 | pData->mpDrv->mpDrv = pData;
|
---|
252 |
|
---|
253 | return VINF_SUCCESS;
|
---|
254 | }
|
---|
255 |
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * Main mouse driver registration record.
|
---|
259 | */
|
---|
260 | const PDMDRVREG Mouse::DrvReg =
|
---|
261 | {
|
---|
262 | /* u32Version */
|
---|
263 | PDM_DRVREG_VERSION,
|
---|
264 | /* szName */
|
---|
265 | "MainMouse",
|
---|
266 | /* szRCMod */
|
---|
267 | "",
|
---|
268 | /* szR0Mod */
|
---|
269 | "",
|
---|
270 | /* pszDescription */
|
---|
271 | "Main mouse driver (Main as in the API).",
|
---|
272 | /* fFlags */
|
---|
273 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
274 | /* fClass. */
|
---|
275 | PDM_DRVREG_CLASS_MOUSE,
|
---|
276 | /* cMaxInstances */
|
---|
277 | ~0,
|
---|
278 | /* cbInstance */
|
---|
279 | sizeof(DRVMAINMOUSE),
|
---|
280 | /* pfnConstruct */
|
---|
281 | Mouse::drvConstruct,
|
---|
282 | /* pfnDestruct */
|
---|
283 | Mouse::drvDestruct,
|
---|
284 | /* pfnRelocate */
|
---|
285 | NULL,
|
---|
286 | /* pfnIOCtl */
|
---|
287 | NULL,
|
---|
288 | /* pfnPowerOn */
|
---|
289 | NULL,
|
---|
290 | /* pfnReset */
|
---|
291 | NULL,
|
---|
292 | /* pfnSuspend */
|
---|
293 | NULL,
|
---|
294 | /* pfnResume */
|
---|
295 | NULL,
|
---|
296 | /* pfnAttach */
|
---|
297 | NULL,
|
---|
298 | /* pfnDetach */
|
---|
299 | NULL,
|
---|
300 | /* pfnPowerOff */
|
---|
301 | NULL,
|
---|
302 | /* pfnSoftReset */
|
---|
303 | NULL,
|
---|
304 | /* u32EndVersion */
|
---|
305 | PDM_DRVREG_VERSION
|
---|
306 | };
|
---|
307 |
|
---|