VirtualBox

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

Last change on this file since 25860 was 25860, checked in by vboxsync, 14 years ago

Main: cleanup: get rid of VirtualBoxBaseProto, move AutoCaller*/*Span* classes out of VirtualBoxBaseProto class scope and into separate header; move CombinedProgress into separate header (it's only used by Console any more)

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

© 2023 Oracle
ContactPrivacy policyTerms of Use