MouseImpl.cpp revision d578610de5e6c148ec4136f7eb3401767c772648
/* $Id$ */
/** @file
* VirtualBox COM class implementation
*/
/*
* Copyright (C) 2006-2012 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#include "MouseImpl.h"
#include "DisplayImpl.h"
#include "VMMDev.h"
#include "AutoCaller.h"
#include "Logging.h"
/** @name Mouse device capabilities bitfield
* @{ */
enum
{
/** The mouse device can do relative reporting */
/** The mouse device can do absolute reporting */
/** The mouse device can do absolute reporting */
};
/** @} */
/**
* Mouse driver instance data.
*/
struct DRVMAINMOUSE
{
/** Pointer to the mouse object. */
/** Pointer to the driver instance structure. */
/** Our mouse connector interface. */
/** The capabilities of this device. */
};
// constructor / destructor
/////////////////////////////////////////////////////////////////////////////
{
}
{
}
{
mcLastX = 0x8000;
mcLastY = 0x8000;
mfLastButtons = 0;
mfVMMDevGuestCaps = 0;
return BaseFinalConstruct();
}
void Mouse::FinalRelease()
{
uninit();
}
// public methods only for internal purposes
/////////////////////////////////////////////////////////////////////////////
/**
* Initializes the mouse object.
*
* @returns COM result indicator
* @param parent handle of our parent object
*/
{
LogFlowThisFunc(("\n"));
/* Enclose the state transition NotReady->InInit->Ready */
AutoInitSpan autoInitSpan(this);
0, 0, 0, 0, 0);
/* Confirm a successful initialization */
return S_OK;
}
/**
* Uninitializes the instance and sets the ready flag to FALSE.
* Called either from FinalRelease() or by the parent when it gets destroyed.
*/
{
LogFlowThisFunc(("\n"));
/* Enclose the state transition Ready->InUninit->NotReady */
AutoUninitSpan autoUninitSpan(this);
if (autoUninitSpan.uninitDone())
return;
for (unsigned i = 0; i < MOUSE_MAX_DEVICES; ++i)
{
if (mpDrv[i])
}
}
// IMouse properties
/////////////////////////////////////////////////////////////////////////////
/** Report the front-end's mouse handling capabilities to the VMM device and
* thus to the guest.
* @note all calls out of this object are made with no locks held! */
{
if (!pVMMDev)
return E_FAIL; /* No assertion, as the front-ends can send events
* at all sorts of inconvenient times. */
if (!pVMMDevPort)
return E_FAIL; /* same here */
}
/**
* Returns whether the currently active device portfolio can accept absolute
* mouse events.
*
* @returns COM status code
* @param absoluteSupported address of result variable
*/
{
if (!absoluteSupported)
return E_POINTER;
AutoCaller autoCaller(this);
*absoluteSupported = supportsAbs();
return S_OK;
}
/**
* Returns whether the currently active device portfolio can accept relative
* mouse events.
*
* @returns COM status code
* @param relativeSupported address of result variable
*/
{
if (!relativeSupported)
return E_POINTER;
AutoCaller autoCaller(this);
*relativeSupported = supportsRel();
return S_OK;
}
/**
* Returns whether the currently active device portfolio can accept multi-touch
* mouse events.
*
* @returns COM status code
* @param multiTouchSupported address of result variable
*/
{
if (!multiTouchSupported)
return E_POINTER;
AutoCaller autoCaller(this);
return S_OK;
}
/**
* Returns whether the guest can currently switch to drawing the mouse cursor
* itself if it is asked to by the front-end.
*
* @returns COM status code
* @param pfNeedsHostCursor address of result variable
*/
{
if (!pfNeedsHostCursor)
return E_POINTER;
AutoCaller autoCaller(this);
return S_OK;
}
// IMouse methods
/////////////////////////////////////////////////////////////////////////////
/** Converts a bitfield containing information about mouse buttons currently
* held down from the format used by the front-end to the format used by PDM
* and the emulated pointing devices. */
{
return fButtons;
}
{
AutoCaller autoCaller(this);
// no need to lock - lifetime constant
return S_OK;
}
/**
* Send a relative pointer event to the relative device we deem most
* appropriate.
*
* @returns COM status code
*/
{
{
{
for (unsigned i = 0; !pUpPort && i < MOUSE_MAX_DEVICES; ++i)
{
}
}
if (!pUpPort)
return S_OK;
if (RT_FAILURE(vrc))
return setError(VBOX_E_IPRT_ERROR,
tr("Could not send the mouse event to the virtual mouse (%Rrc)"),
vrc);
}
return S_OK;
}
/**
* Send an absolute pointer event to the emulated absolute device we deem most
* appropriate.
*
* @returns COM status code
*/
{
if ( x < VMMDEV_MOUSE_RANGE_MIN
|| x > VMMDEV_MOUSE_RANGE_MAX)
return S_OK;
if ( y < VMMDEV_MOUSE_RANGE_MIN
|| y > VMMDEV_MOUSE_RANGE_MAX)
return S_OK;
{
{
for (unsigned i = 0; !pUpPort && i < MOUSE_MAX_DEVICES; ++i)
{
}
}
if (!pUpPort)
return S_OK;
if (RT_FAILURE(vrc))
return setError(VBOX_E_IPRT_ERROR,
tr("Could not send the mouse event to the virtual mouse (%Rrc)"),
vrc);
}
return S_OK;
}
/**
* Send a multi-touch event to the first enabled emulated multi-touch device.
*
* @returns COM status code
*/
{
if ( x < VMMDEV_MOUSE_RANGE_MIN
|| x > VMMDEV_MOUSE_RANGE_MAX)
return S_OK;
if ( y < VMMDEV_MOUSE_RANGE_MIN
|| y > VMMDEV_MOUSE_RANGE_MAX)
return S_OK;
{
for (unsigned i = 0; !pUpPort && i < MOUSE_MAX_DEVICES; ++i)
{
if ( mpDrv[i]
}
}
if (!pUpPort)
return S_OK;
if (RT_FAILURE(vrc))
return setError(VBOX_E_IPRT_ERROR,
tr("Could not send the touch event to the virtual device (%Rrc)"),
vrc);
return S_OK;
}
/**
* Send an absolute position event to the VMM device.
* @note all calls out of this object are made with no locks held!
*
* @returns COM status code
*/
{
{
x, y);
if (RT_FAILURE(vrc))
return setError(VBOX_E_IPRT_ERROR,
tr("Could not send the mouse event to the virtual mouse (%Rrc)"),
vrc);
}
return S_OK;
}
/**
* Send an absolute pointer event to a pointing device (the VMM device if
* possible or whatever emulated absolute device seems best to us if not).
*
* @returns COM status code
*/
bool fUsesVMMDevEvent)
{
/** If we are using the VMMDev to report absolute position but without
* VMMDev IRQ support then we need to send a small "jiggle" to the emulated
* relative mouse device to alert the guest to changes. */
if (vmmdevCanAbs())
{
/*
* Send the absolute mouse position to the VMM device.
*/
{
rc = reportAbsEventToVMMDev(x, y);
}
}
else
mcLastX = x;
mcLastY = y;
return rc;
}
{
/* If mouse button is pressed, we generate new event, to avoid reusable events coalescing and thus
dropping key press events */
if (fButtons != 0)
{
}
else
{
mMouseEvent.fire(0);
}
}
/**
* Send a relative mouse event to the guest.
* @note the VMMDev capability change is so that the guest knows we are sending
* real events over the PS/2 device and not dummy events to signal the
* arrival of new absolute pointer data
*
* @returns COM status code
* @param dx X movement
* @param dy Y movement
* @param dz Z movement
* @param fButtons The mouse button state
*/
{
AutoCaller autoCaller(this);
/* Make sure that the guest knows that we are sending real movement
* events to the PS/2 device and not just dummy wake-up ones. */
return rc;
}
/**
* Convert an (X, Y) value pair in screen co-ordinates (starting from 1) to a
* value from VMMDEV_MOUSE_RANGE_MIN to VMMDEV_MOUSE_RANGE_MAX. Sets the
* optional validity value to false if the pair is not on an active screen and
* to true otherwise.
* @note since guests with recent versions of X.Org use a different method
* to everyone else to map the valuator value to a screen pixel (they
* multiply by the screen dimension, do a floating point divide by
* the valuator maximum and round the result, while everyone else
* does truncating integer operations) we adjust the value we send
* so that it maps to the right pixel both when the result is rounded
* and when it is truncated.
*
* @returns COM status value
*/
bool *pfValid)
{
* to compensate for differences in guest methods for mapping back to
* pixels */
if (pfValid)
*pfValid = true;
if (!(mfVMMDevGuestCaps & VMMDEV_MOUSE_NEW_PROTOCOL))
{
/* Takes the display lock */
&displayHeight, NULL);
return rc;
/ (LONG) displayWidth: 0;
/ (LONG) displayHeight: 0;
}
else
{
/* Takes the display lock */
if ( *pxAdj < VMMDEV_MOUSE_RANGE_MIN
|| *pxAdj > VMMDEV_MOUSE_RANGE_MAX
|| *pyAdj < VMMDEV_MOUSE_RANGE_MIN
|| *pyAdj > VMMDEV_MOUSE_RANGE_MAX)
if (pfValid)
*pfValid = false;
}
return S_OK;
}
/**
* Send an absolute mouse event to the VM. This requires either VirtualBox-
* specific drivers installed in the guest or absolute pointing device
* emulation.
* @note the VMMDev capability change is so that the guest knows we are sending
* dummy events over the PS/2 device to signal the arrival of new
* absolute pointer data, and not pointer real movement data
* @note all calls out of this object are made with no locks held!
*
* @returns COM status code
* @param x X position (pixel), starting from 1
* @param y Y position (pixel), starting from 1
* @param dz Z movement
* @param fButtons The mouse button state
*/
{
AutoCaller autoCaller(this);
LogRel3(("%s: x=%d, y=%d, dz=%d, dw=%d, fButtons=0x%x\n",
bool fValid;
/** @todo the front end should do this conversion to avoid races */
/** @note Or maybe not... races are pretty inherent in everything done in
* this object and not really bad as far as I can see. */
/* If we are doing old-style (IRQ-less) absolute reporting to the VMM
* device then make sure the guest is aware of it, so that it knows to
* ignore relative movement on the PS/2 device. */
if (fValid)
{
}
return rc;
}
/**
* @interface_method_impl{IMouse,putMouseEventMultiTouch}
*/
{
AutoCaller autoCaller(this);
LogRel3(("%s: x=%d, y=%d, cContact=%d, fContact=%RTBool\n",
bool fValid;
/** @todo the front end should do this conversion to avoid races */
/** @note Or maybe not... races are pretty inherent in everything done in
* this object and not really bad as far as I can see. */
if (fValid)
{
}
return rc;
}
// private methods
/////////////////////////////////////////////////////////////////////////////
/** Does the guest currently rely on the host to draw the mouse cursor or
* can it switch to doing it itself in software? */
bool Mouse::guestNeedsHostCursor(void)
{
}
/** Check what sort of reporting can be done using the devices currently
* enabled. Does not consider the VMM device. */
{
bool fAbsDev = false;
bool fRelDev = false;
bool fMTDev = false;
for (unsigned i = 0; i < MOUSE_MAX_DEVICES; ++i)
if (mpDrv[i])
{
fAbsDev = true;
fRelDev = true;
fMTDev = true;
}
if (pfAbs)
if (pfRel)
}
/** Does the VMM device currently support absolute reporting? */
bool Mouse::vmmdevCanAbs(void)
{
bool fRelDev;
return (mfVMMDevGuestCaps & VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE)
&& fRelDev;
}
/** Does the VMM device currently support absolute reporting? */
bool Mouse::deviceCanAbs(void)
{
bool fAbsDev;
return fAbsDev;
}
/** Can we currently send relative events to the guest? */
bool Mouse::supportsRel(void)
{
bool fRelDev;
return fRelDev;
}
/** Can we currently send absolute events to the guest? */
bool Mouse::supportsAbs(void)
{
bool fAbsDev;
return fAbsDev || vmmdevCanAbs();
}
/** Can we currently send absolute events to the guest? */
bool Mouse::supportsMT(void)
{
bool fMTDev;
return fMTDev;
}
/** Check what sort of reporting can be done using the devices currently
* enabled (including the VMM device) and notify the guest and the front-end.
*/
void Mouse::sendMouseCapsNotifications(void)
{
{
fCanAbs = supportsAbs();
}
if (fAbsDev)
else
/** @todo this call takes the Console lock in order to update the cached
* callback data atomically. However I can't see any sign that the cached
* data is ever used again. */
}
/**
* @interface_method_impl{PDMIMOUSECONNECTOR,pfnReportModes}
* A virtual device is notifying us about its current state and capabilities
*/
DECLCALLBACK(void) Mouse::mouseReportModes(PPDMIMOUSECONNECTOR pInterface, bool fRel, bool fAbs, bool fMT)
{
if (fRel)
else
if (fAbs)
else
if (fMT)
else
}
/**
* @interface_method_impl{PDMIBASE,pfnQueryInterface}
*/
{
return NULL;
}
/**
* Destruct a mouse driver instance.
*
* @returns VBox status.
* @param pDrvIns The driver instance data.
*/
{
{
{
break;
}
}
}
/**
* Construct a mouse driver instance.
*
* @copydoc FNPDMDRVCONSTRUCT
*/
{
/*
* Validate configuration.
*/
("Configuration error: Not possible to attach anything to this driver!\n"),
/*
* IBase.
*/
/*
*/
pThis->pUpPort = (PPDMIMOUSEPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMIMOUSEPORT_IID);
{
AssertMsgFailed(("Configuration error: No mouse port interface above!\n"));
return VERR_PDM_MISSING_INTERFACE_ABOVE;
}
/*
* Get the Mouse object pointer and update the mpDrv member.
*/
void *pv;
if (RT_FAILURE(rc))
{
return rc;
}
unsigned cDev;
{
{
break;
}
}
if (cDev == MOUSE_MAX_DEVICES)
return VERR_NO_MORE_HANDLES;
return VINF_SUCCESS;
}
/**
* Main mouse driver registration record.
*/
{
/* u32Version */
/* szName */
"MainMouse",
/* szRCMod */
"",
/* szR0Mod */
"",
/* pszDescription */
"Main mouse driver (Main as in the API).",
/* fFlags */
/* fClass. */
/* cMaxInstances */
~0U,
/* cbInstance */
sizeof(DRVMAINMOUSE),
/* pfnConstruct */
/* pfnDestruct */
/* pfnRelocate */
NULL,
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
NULL,
/* pfnSuspend */
NULL,
/* pfnResume */
NULL,
/* pfnAttach */
NULL,
/* pfnDetach */
NULL,
/* pfnPowerOff */
NULL,
/* pfnSoftReset */
NULL,
/* u32EndVersion */
};
/* vi: set tabstop=4 shiftwidth=4 expandtab: */