VirtualBox

source: vbox/trunk/src/VBox/Main/MouseImpl.cpp@ 22277

Last change on this file since 22277 was 22277, checked in by vboxsync, 15 years ago

PDMDRVREG change (big changeset).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.8 KB
Line 
1/* $Id: MouseImpl.cpp 22277 2009-08-16 21:12:50Z vboxsync $ */
2
3/** @file
4 *
5 * VirtualBox COM class implementation
6 */
7
8/*
9 * Copyright (C) 2006-2008 Sun Microsystems, Inc.
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
20 * Clara, CA 95054 USA or visit http://www.sun.com if you need
21 * additional information or have any questions.
22 */
23
24#include "MouseImpl.h"
25#include "DisplayImpl.h"
26#include "VMMDev.h"
27
28#include "Logging.h"
29
30#include <VBox/pdmdrv.h>
31#include <iprt/asm.h>
32#include <VBox/VMMDev.h>
33
34/**
35 * Mouse driver instance data.
36 */
37typedef struct DRVMAINMOUSE
38{
39 /** Pointer to the mouse object. */
40 Mouse *pMouse;
41 /** Pointer to the driver instance structure. */
42 PPDMDRVINS pDrvIns;
43 /** Pointer to the mouse port interface of the driver/device above us. */
44 PPDMIMOUSEPORT pUpPort;
45 /** Our mouse connector interface. */
46 PDMIMOUSECONNECTOR Connector;
47} DRVMAINMOUSE, *PDRVMAINMOUSE;
48
49/** Converts PDMIMOUSECONNECTOR pointer to a DRVMAINMOUSE pointer. */
50#define PDMIMOUSECONNECTOR_2_MAINMOUSE(pInterface) ( (PDRVMAINMOUSE) ((uintptr_t)pInterface - OFFSETOF(DRVMAINMOUSE, Connector)) )
51
52
53// constructor / destructor
54/////////////////////////////////////////////////////////////////////////////
55
56DEFINE_EMPTY_CTOR_DTOR (Mouse)
57
58HRESULT Mouse::FinalConstruct()
59{
60 mpDrv = NULL;
61 mLastAbsX = 0;
62 mLastAbsY = 0;
63 return S_OK;
64}
65
66void Mouse::FinalRelease()
67{
68 uninit();
69}
70
71// public methods only for internal purposes
72/////////////////////////////////////////////////////////////////////////////
73
74/**
75 * Initializes the mouse object.
76 *
77 * @returns COM result indicator
78 * @param parent handle of our parent object
79 */
80HRESULT Mouse::init (Console *parent)
81{
82 LogFlowThisFunc(("\n"));
83
84 ComAssertRet (parent, E_INVALIDARG);
85
86 /* Enclose the state transition NotReady->InInit->Ready */
87 AutoInitSpan autoInitSpan(this);
88 AssertReturn(autoInitSpan.isOk(), E_FAIL);
89
90 unconst(mParent) = parent;
91
92#ifdef RT_OS_L4
93 /* L4 console has no own mouse cursor */
94 uHostCaps = VMMDEV_MOUSE_HOST_CANNOT_HWPOINTER;
95#else
96 uHostCaps = 0;
97#endif
98
99 /* Confirm a successful initialization */
100 autoInitSpan.setSucceeded();
101
102 return S_OK;
103}
104
105/**
106 * Uninitializes the instance and sets the ready flag to FALSE.
107 * Called either from FinalRelease() or by the parent when it gets destroyed.
108 */
109void Mouse::uninit()
110{
111 LogFlowThisFunc(("\n"));
112
113 /* Enclose the state transition Ready->InUninit->NotReady */
114 AutoUninitSpan autoUninitSpan(this);
115 if (autoUninitSpan.uninitDone())
116 return;
117
118 if (mpDrv)
119 mpDrv->pMouse = NULL;
120 mpDrv = NULL;
121
122 unconst(mParent).setNull();
123}
124
125// IMouse properties
126/////////////////////////////////////////////////////////////////////////////
127
128/**
129 * Returns whether the current setup can accept absolute mouse
130 * events.
131 *
132 * @returns COM status code
133 * @param absoluteSupported address of result variable
134 */
135STDMETHODIMP Mouse::COMGETTER(AbsoluteSupported) (BOOL *absoluteSupported)
136{
137 if (!absoluteSupported)
138 return E_POINTER;
139
140 AutoCaller autoCaller(this);
141 CheckComRCReturnRC(autoCaller.rc());
142
143 AutoWriteLock alock(this);
144
145 CHECK_CONSOLE_DRV (mpDrv);
146
147 ComAssertRet (mParent->getVMMDev(), E_FAIL);
148 ComAssertRet (mParent->getVMMDev()->getVMMDevPort(), E_FAIL);
149
150 *absoluteSupported = FALSE;
151 uint32_t mouseCaps;
152 mParent->getVMMDev()->getVMMDevPort()->pfnQueryMouseCapabilities(mParent->getVMMDev()->getVMMDevPort(), &mouseCaps);
153 *absoluteSupported = mouseCaps & VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE;
154
155 return S_OK;
156}
157
158/**
159 * Returns whether the current setup can accept relative mouse
160 * events.
161 *
162 * @returns COM status code
163 * @param absoluteSupported address of result variable
164 */
165STDMETHODIMP Mouse::COMGETTER(NeedsHostCursor) (BOOL *needsHostCursor)
166{
167 if (!needsHostCursor)
168 return E_POINTER;
169
170 AutoCaller autoCaller(this);
171 CheckComRCReturnRC(autoCaller.rc());
172
173 AutoWriteLock alock(this);
174
175 CHECK_CONSOLE_DRV (mpDrv);
176
177 ComAssertRet (mParent->getVMMDev(), E_FAIL);
178 ComAssertRet (mParent->getVMMDev()->getVMMDevPort(), E_FAIL);
179
180 *needsHostCursor = FALSE;
181 uint32_t mouseCaps;
182 mParent->getVMMDev()->getVMMDevPort()->pfnQueryMouseCapabilities(mParent->getVMMDev()->getVMMDevPort(), &mouseCaps);
183 *needsHostCursor = mouseCaps & VMMDEV_MOUSE_GUEST_NEEDS_HOST_CURSOR;
184
185 return S_OK;
186}
187
188// IMouse methods
189/////////////////////////////////////////////////////////////////////////////
190
191/**
192 * Send a mouse event.
193 *
194 * @returns COM status code
195 * @param dx X movement
196 * @param dy Y movement
197 * @param dz Z movement
198 * @param buttonState The mouse button state
199 */
200STDMETHODIMP Mouse::PutMouseEvent(LONG dx, LONG dy, LONG dz, LONG buttonState)
201{
202 HRESULT rc = S_OK;
203
204 AutoCaller autoCaller(this);
205 CheckComRCReturnRC(autoCaller.rc());
206
207 AutoWriteLock alock(this);
208
209 CHECK_CONSOLE_DRV (mpDrv);
210
211 ComAssertRet (mParent->getVMMDev(), E_FAIL);
212 ComAssertRet (mParent->getVMMDev()->getVMMDevPort(), E_FAIL);
213
214 uint32_t mouseCaps;
215 mParent->getVMMDev()->getVMMDevPort()
216 ->pfnQueryMouseCapabilities(mParent->getVMMDev()->getVMMDevPort(),
217 &mouseCaps);
218 /*
219 * This method being called implies that the host no
220 * longer wants to use absolute coordinates. If the VMM
221 * device isn't aware of that yet, tell it.
222 */
223 if (mouseCaps & VMMDEV_MOUSE_HOST_CAN_ABSOLUTE)
224 {
225 mParent->getVMMDev()->getVMMDevPort()->pfnSetMouseCapabilities(
226 mParent->getVMMDev()->getVMMDevPort(), uHostCaps);
227 }
228
229 uint32_t fButtons = 0;
230 if (buttonState & MouseButtonState_LeftButton)
231 fButtons |= PDMIMOUSEPORT_BUTTON_LEFT;
232 if (buttonState & MouseButtonState_RightButton)
233 fButtons |= PDMIMOUSEPORT_BUTTON_RIGHT;
234 if (buttonState & MouseButtonState_MiddleButton)
235 fButtons |= PDMIMOUSEPORT_BUTTON_MIDDLE;
236
237 int vrc = mpDrv->pUpPort->pfnPutEvent(mpDrv->pUpPort, dx, dy, dz, fButtons);
238 if (RT_FAILURE(vrc))
239 rc = setError (VBOX_E_IPRT_ERROR,
240 tr ("Could not send the mouse event to the virtual mouse (%Rrc)"),
241 vrc);
242
243 return rc;
244}
245
246/**
247 * Send an absolute mouse event to the VM. This only works
248 * when the required guest support has been installed.
249 *
250 * @returns COM status code
251 * @param x X position (pixel)
252 * @param y Y position (pixel)
253 * @param dz Z movement
254 * @param buttonState The mouse button state
255 */
256STDMETHODIMP Mouse::PutMouseEventAbsolute(LONG x, LONG y, LONG dz,
257 LONG buttonState)
258{
259 HRESULT rc = S_OK;
260
261 AutoCaller autoCaller(this);
262 CheckComRCReturnRC(autoCaller.rc());
263
264 AutoWriteLock alock(this);
265
266 CHECK_CONSOLE_DRV (mpDrv);
267
268 ComAssertRet (mParent->getVMMDev(), E_FAIL);
269 ComAssertRet (mParent->getVMMDev()->getVMMDevPort(), E_FAIL);
270
271 uint32_t mouseCaps;
272 mParent->getVMMDev()->getVMMDevPort()
273 ->pfnQueryMouseCapabilities(mParent->getVMMDev()->getVMMDevPort(),
274 &mouseCaps);
275 /*
276 * This method being called implies that the host wants
277 * to use absolute coordinates. If the VMM device isn't
278 * aware of that yet, tell it.
279 */
280 if (!(mouseCaps & VMMDEV_MOUSE_HOST_CAN_ABSOLUTE))
281 {
282 mParent->getVMMDev()->getVMMDevPort()->pfnSetMouseCapabilities(
283 mParent->getVMMDev()->getVMMDevPort(),
284 uHostCaps | VMMDEV_MOUSE_HOST_CAN_ABSOLUTE);
285 }
286
287 Display *pDisplay = mParent->getDisplay();
288 ComAssertRet (pDisplay, E_FAIL);
289
290 ULONG displayWidth;
291 ULONG displayHeight;
292 rc = pDisplay->COMGETTER(Width)(&displayWidth);
293 ComAssertComRCRet (rc, rc);
294 rc = pDisplay->COMGETTER(Height)(&displayHeight);
295 ComAssertComRCRet (rc, rc);
296
297 uint32_t mouseXAbs = displayWidth? (x * 0xFFFF) / displayWidth: 0;
298 uint32_t mouseYAbs = displayHeight? (y * 0xFFFF) / displayHeight: 0;
299
300 /*
301 * Send the absolute mouse position to the VMM device.
302 */
303 int vrc = mParent->getVMMDev()->getVMMDevPort()
304 ->pfnSetAbsoluteMouse(mParent->getVMMDev()->getVMMDevPort(),
305 mouseXAbs, mouseYAbs);
306 ComAssertRCRet (vrc, E_FAIL);
307
308 // Check if the guest actually wants absolute mouse positions.
309 if (mouseCaps & VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE)
310 {
311 uint32_t fButtons = 0;
312 if (buttonState & MouseButtonState_LeftButton)
313 fButtons |= PDMIMOUSEPORT_BUTTON_LEFT;
314 if (buttonState & MouseButtonState_RightButton)
315 fButtons |= PDMIMOUSEPORT_BUTTON_RIGHT;
316 if (buttonState & MouseButtonState_MiddleButton)
317 fButtons |= PDMIMOUSEPORT_BUTTON_MIDDLE;
318
319 /* This is a workaround. In order to alert the Guest Additions to the
320 * fact that the absolute pointer position has changed, we send a
321 * a minute movement event to the PS/2 mouse device. But in order
322 * to avoid the mouse jiggling every time the use clicks, we check to
323 * see if the position has really changed since the last mouse event.
324 */
325 if ( ((mLastAbsX == mouseXAbs) && (mLastAbsY == mouseYAbs))
326 || (mouseCaps & VMMDEV_MOUSE_GUEST_USES_VMMDEV))
327 vrc = mpDrv->pUpPort->pfnPutEvent(mpDrv->pUpPort, 0, 0, dz,
328 fButtons);
329 else
330 vrc = mpDrv->pUpPort->pfnPutEvent(mpDrv->pUpPort, 1, 1, dz,
331 fButtons);
332 mLastAbsX = mouseXAbs;
333 mLastAbsY = mouseYAbs;
334 if (RT_FAILURE(vrc))
335 rc = setError (VBOX_E_IPRT_ERROR,
336 tr ("Could not send the mouse event to the virtual mouse (%Rrc)"),
337 vrc);
338 }
339
340 return rc;
341}
342
343// private methods
344/////////////////////////////////////////////////////////////////////////////
345
346/**
347 * Queries an interface to the driver.
348 *
349 * @returns Pointer to interface.
350 * @returns NULL if the interface was not supported by the driver.
351 * @param pInterface Pointer to this interface structure.
352 * @param enmInterface The requested interface identification.
353 */
354DECLCALLBACK(void *) Mouse::drvQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
355{
356 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
357 PDRVMAINMOUSE pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
358 switch (enmInterface)
359 {
360 case PDMINTERFACE_BASE:
361 return &pDrvIns->IBase;
362 case PDMINTERFACE_MOUSE_CONNECTOR:
363 return &pDrv->Connector;
364 default:
365 return NULL;
366 }
367}
368
369
370/**
371 * Destruct a mouse driver instance.
372 *
373 * @returns VBox status.
374 * @param pDrvIns The driver instance data.
375 */
376DECLCALLBACK(void) Mouse::drvDestruct(PPDMDRVINS pDrvIns)
377{
378 PDRVMAINMOUSE pData = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
379 LogFlow(("Mouse::drvDestruct: iInstance=%d\n", pDrvIns->iInstance));
380 if (pData->pMouse)
381 {
382 AutoWriteLock mouseLock (pData->pMouse);
383 pData->pMouse->mpDrv = NULL;
384 }
385}
386
387
388/**
389 * Construct a mouse driver instance.
390 *
391 * @copydoc FNPDMDRVCONSTRUCT
392 */
393DECLCALLBACK(int) Mouse::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle, uint32_t fFlags)
394{
395 PDRVMAINMOUSE pData = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
396 LogFlow(("drvMainMouse_Construct: iInstance=%d\n", pDrvIns->iInstance));
397
398 /*
399 * Validate configuration.
400 */
401 if (!CFGMR3AreValuesValid(pCfgHandle, "Object\0"))
402 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
403 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
404 ("Configuration error: Not possible to attach anything to this driver!\n"),
405 VERR_PDM_DRVINS_NO_ATTACH);
406
407 /*
408 * IBase.
409 */
410 pDrvIns->IBase.pfnQueryInterface = Mouse::drvQueryInterface;
411
412 /*
413 * Get the IMousePort interface of the above driver/device.
414 */
415 pData->pUpPort = (PPDMIMOUSEPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_MOUSE_PORT);
416 if (!pData->pUpPort)
417 {
418 AssertMsgFailed(("Configuration error: No mouse port interface above!\n"));
419 return VERR_PDM_MISSING_INTERFACE_ABOVE;
420 }
421
422 /*
423 * Get the Mouse object pointer and update the mpDrv member.
424 */
425 void *pv;
426 int rc = CFGMR3QueryPtr(pCfgHandle, "Object", &pv);
427 if (RT_FAILURE(rc))
428 {
429 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
430 return rc;
431 }
432 pData->pMouse = (Mouse *)pv; /** @todo Check this cast! */
433 pData->pMouse->mpDrv = pData;
434
435 return VINF_SUCCESS;
436}
437
438
439/**
440 * Main mouse driver registration record.
441 */
442const PDMDRVREG Mouse::DrvReg =
443{
444 /* u32Version */
445 PDM_DRVREG_VERSION,
446 /* szDriverName */
447 "MainMouse",
448 /* pszDescription */
449 "Main mouse driver (Main as in the API).",
450 /* fFlags */
451 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
452 /* fClass. */
453 PDM_DRVREG_CLASS_MOUSE,
454 /* cMaxInstances */
455 ~0,
456 /* cbInstance */
457 sizeof(DRVMAINMOUSE),
458 /* pfnConstruct */
459 Mouse::drvConstruct,
460 /* pfnDestruct */
461 Mouse::drvDestruct,
462 /* pfnIOCtl */
463 NULL,
464 /* pfnPowerOn */
465 NULL,
466 /* pfnReset */
467 NULL,
468 /* pfnSuspend */
469 NULL,
470 /* pfnResume */
471 NULL,
472 /* pfnAttach */
473 NULL,
474 /* pfnDetach */
475 NULL,
476 /* pfnPowerOff */
477 NULL,
478 /* pfnSoftReset */
479 NULL,
480 /* u32EndVersion */
481 PDM_DRVREG_VERSION
482};
483/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette