VirtualBox

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

Last change on this file since 20818 was 19654, checked in by vboxsync, 16 years ago

Main and Devices/absolute mouse: do not send movement events to the virutal PS/2 device if the guest can read them from VMMDev

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.1 KB
Line 
1/* $Id: MouseImpl.cpp 19654 2009-05-13 12:30:58Z 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/VBoxDev.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_MOUSEHOSTCANNOTHWPOINTER;
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_MOUSEGUESTWANTSABS;
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_MOUSEGUESTNEEDSHOSTCUR;
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_MOUSEHOSTWANTSABS)
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_MOUSEHOSTWANTSABS))
281 {
282 mParent->getVMMDev()->getVMMDevPort()->pfnSetMouseCapabilities(
283 mParent->getVMMDev()->getVMMDevPort(),
284 uHostCaps | VMMDEV_MOUSEHOSTWANTSABS);
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_MOUSEGUESTWANTSABS)
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 & VBOXGUEST_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 * @returns VBox status.
392 * @param pDrvIns The driver instance data.
393 * If the registration structure is needed, pDrvIns->pDrvReg points to it.
394 * @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
395 * of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
396 * iInstance it's expected to be used a bit in this function.
397 */
398DECLCALLBACK(int) Mouse::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
399{
400 PDRVMAINMOUSE pData = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
401 LogFlow(("drvMainMouse_Construct: iInstance=%d\n", pDrvIns->iInstance));
402
403 /*
404 * Validate configuration.
405 */
406 if (!CFGMR3AreValuesValid(pCfgHandle, "Object\0"))
407 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
408 PPDMIBASE pBaseIgnore;
409 int rc = pDrvIns->pDrvHlp->pfnAttach(pDrvIns, &pBaseIgnore);
410 if (rc != VERR_PDM_NO_ATTACHED_DRIVER)
411 {
412 AssertMsgFailed(("Configuration error: Not possible to attach anything to this driver!\n"));
413 return VERR_PDM_DRVINS_NO_ATTACH;
414 }
415
416 /*
417 * IBase.
418 */
419 pDrvIns->IBase.pfnQueryInterface = Mouse::drvQueryInterface;
420
421 /*
422 * Get the IMousePort interface of the above driver/device.
423 */
424 pData->pUpPort = (PPDMIMOUSEPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_MOUSE_PORT);
425 if (!pData->pUpPort)
426 {
427 AssertMsgFailed(("Configuration error: No mouse port interface above!\n"));
428 return VERR_PDM_MISSING_INTERFACE_ABOVE;
429 }
430
431 /*
432 * Get the Mouse object pointer and update the mpDrv member.
433 */
434 void *pv;
435 rc = CFGMR3QueryPtr(pCfgHandle, "Object", &pv);
436 if (RT_FAILURE(rc))
437 {
438 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
439 return rc;
440 }
441 pData->pMouse = (Mouse *)pv; /** @todo Check this cast! */
442 pData->pMouse->mpDrv = pData;
443
444 return VINF_SUCCESS;
445}
446
447
448/**
449 * Main mouse driver registration record.
450 */
451const PDMDRVREG Mouse::DrvReg =
452{
453 /* u32Version */
454 PDM_DRVREG_VERSION,
455 /* szDriverName */
456 "MainMouse",
457 /* pszDescription */
458 "Main mouse driver (Main as in the API).",
459 /* fFlags */
460 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
461 /* fClass. */
462 PDM_DRVREG_CLASS_MOUSE,
463 /* cMaxInstances */
464 ~0,
465 /* cbInstance */
466 sizeof(DRVMAINMOUSE),
467 /* pfnConstruct */
468 Mouse::drvConstruct,
469 /* pfnDestruct */
470 Mouse::drvDestruct,
471 /* pfnIOCtl */
472 NULL,
473 /* pfnPowerOn */
474 NULL,
475 /* pfnReset */
476 NULL,
477 /* pfnSuspend */
478 NULL,
479 /* pfnResume */
480 NULL,
481 /* pfnDetach */
482 NULL
483};
484/* 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