VirtualBox

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

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

Main and Devices: absolute mouse support: do not send a mouse movement event if only the button state has changed

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.0 KB
Line 
1/* $Id: MouseImpl.cpp 19614 2009-05-12 12:27:21Z 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 vrc = mpDrv->pUpPort->pfnPutEvent(mpDrv->pUpPort, 0, 0, dz,
327 fButtons);
328 else
329 vrc = mpDrv->pUpPort->pfnPutEvent(mpDrv->pUpPort, 1, 1, dz,
330 fButtons);
331 mLastAbsX = mouseXAbs;
332 mLastAbsY = mouseYAbs;
333 if (RT_FAILURE (vrc))
334 rc = setError (VBOX_E_IPRT_ERROR,
335 tr ("Could not send the mouse event to the virtual mouse (%Rrc)"),
336 vrc);
337 }
338
339 return rc;
340}
341
342// private methods
343/////////////////////////////////////////////////////////////////////////////
344
345/**
346 * Queries an interface to the driver.
347 *
348 * @returns Pointer to interface.
349 * @returns NULL if the interface was not supported by the driver.
350 * @param pInterface Pointer to this interface structure.
351 * @param enmInterface The requested interface identification.
352 */
353DECLCALLBACK(void *) Mouse::drvQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
354{
355 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
356 PDRVMAINMOUSE pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
357 switch (enmInterface)
358 {
359 case PDMINTERFACE_BASE:
360 return &pDrvIns->IBase;
361 case PDMINTERFACE_MOUSE_CONNECTOR:
362 return &pDrv->Connector;
363 default:
364 return NULL;
365 }
366}
367
368
369/**
370 * Destruct a mouse driver instance.
371 *
372 * @returns VBox status.
373 * @param pDrvIns The driver instance data.
374 */
375DECLCALLBACK(void) Mouse::drvDestruct(PPDMDRVINS pDrvIns)
376{
377 PDRVMAINMOUSE pData = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
378 LogFlow(("Mouse::drvDestruct: iInstance=%d\n", pDrvIns->iInstance));
379 if (pData->pMouse)
380 {
381 AutoWriteLock mouseLock (pData->pMouse);
382 pData->pMouse->mpDrv = NULL;
383 }
384}
385
386
387/**
388 * Construct a mouse driver instance.
389 *
390 * @returns VBox status.
391 * @param pDrvIns The driver instance data.
392 * If the registration structure is needed, pDrvIns->pDrvReg points to it.
393 * @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
394 * of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
395 * iInstance it's expected to be used a bit in this function.
396 */
397DECLCALLBACK(int) Mouse::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
398{
399 PDRVMAINMOUSE pData = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
400 LogFlow(("drvMainMouse_Construct: iInstance=%d\n", pDrvIns->iInstance));
401
402 /*
403 * Validate configuration.
404 */
405 if (!CFGMR3AreValuesValid(pCfgHandle, "Object\0"))
406 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
407 PPDMIBASE pBaseIgnore;
408 int rc = pDrvIns->pDrvHlp->pfnAttach(pDrvIns, &pBaseIgnore);
409 if (rc != VERR_PDM_NO_ATTACHED_DRIVER)
410 {
411 AssertMsgFailed(("Configuration error: Not possible to attach anything to this driver!\n"));
412 return VERR_PDM_DRVINS_NO_ATTACH;
413 }
414
415 /*
416 * IBase.
417 */
418 pDrvIns->IBase.pfnQueryInterface = Mouse::drvQueryInterface;
419
420 /*
421 * Get the IMousePort interface of the above driver/device.
422 */
423 pData->pUpPort = (PPDMIMOUSEPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_MOUSE_PORT);
424 if (!pData->pUpPort)
425 {
426 AssertMsgFailed(("Configuration error: No mouse port interface above!\n"));
427 return VERR_PDM_MISSING_INTERFACE_ABOVE;
428 }
429
430 /*
431 * Get the Mouse object pointer and update the mpDrv member.
432 */
433 void *pv;
434 rc = CFGMR3QueryPtr(pCfgHandle, "Object", &pv);
435 if (RT_FAILURE(rc))
436 {
437 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
438 return rc;
439 }
440 pData->pMouse = (Mouse *)pv; /** @todo Check this cast! */
441 pData->pMouse->mpDrv = pData;
442
443 return VINF_SUCCESS;
444}
445
446
447/**
448 * Main mouse driver registration record.
449 */
450const PDMDRVREG Mouse::DrvReg =
451{
452 /* u32Version */
453 PDM_DRVREG_VERSION,
454 /* szDriverName */
455 "MainMouse",
456 /* pszDescription */
457 "Main mouse driver (Main as in the API).",
458 /* fFlags */
459 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
460 /* fClass. */
461 PDM_DRVREG_CLASS_MOUSE,
462 /* cMaxInstances */
463 ~0,
464 /* cbInstance */
465 sizeof(DRVMAINMOUSE),
466 /* pfnConstruct */
467 Mouse::drvConstruct,
468 /* pfnDestruct */
469 Mouse::drvDestruct,
470 /* pfnIOCtl */
471 NULL,
472 /* pfnPowerOn */
473 NULL,
474 /* pfnReset */
475 NULL,
476 /* pfnSuspend */
477 NULL,
478 /* pfnResume */
479 NULL,
480 /* pfnDetach */
481 NULL
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