VirtualBox

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

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

Main: defer creation of the VMMDev instance in Console to Console::powerUpThread so that the VMMDev with the HGCM thread only gets created for session/console pairs any more which actually power up a VM; this avoids creating dozens of HGCM threads for every single session even if it never starts a VM; some more Console code cleanup while we're at it

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.9 KB
Line 
1/* $Id: MouseImpl.cpp 32851 2010-09-30 15:12:55Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2006-2008 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include <iprt/cpp/utils.h>
19
20#include "MouseImpl.h"
21#include "DisplayImpl.h"
22#include "VMMDev.h"
23
24#include "AutoCaller.h"
25#include "Logging.h"
26
27#include <VBox/pdmdrv.h>
28
29#include <iprt/asm.h>
30
31#include <VBox/VMMDev.h>
32
33/** @name Mouse device capabilities bitfield
34 * @{ */
35enum
36{
37 /** The mouse device can do relative reporting */
38 MOUSE_DEVCAP_RELATIVE = 1,
39 /** The mouse device can do absolute reporting */
40 MOUSE_DEVCAP_ABSOLUTE = 2
41};
42/** @} */
43
44/**
45 * Mouse driver instance data.
46 */
47struct DRVMAINMOUSE
48{
49 /** Pointer to the mouse object. */
50 Mouse *pMouse;
51 /** Pointer to the driver instance structure. */
52 PPDMDRVINS pDrvIns;
53 /** Pointer to the mouse port interface of the driver/device above us. */
54 PPDMIMOUSEPORT pUpPort;
55 /** Our mouse connector interface. */
56 PDMIMOUSECONNECTOR IConnector;
57 /** The capabilities of this device. */
58 uint32_t u32DevCaps;
59};
60
61
62// constructor / destructor
63/////////////////////////////////////////////////////////////////////////////
64
65Mouse::Mouse()
66 : mParent(NULL)
67{
68}
69
70Mouse::~Mouse()
71{
72}
73
74
75HRESULT Mouse::FinalConstruct()
76{
77 RT_ZERO(mpDrv);
78 mfVMMDevCanAbs = false;
79 mfVMMDevNeedsHostCursor = false;
80 mLastAbsX = 0x8000;
81 mLastAbsY = 0x8000;
82 mLastButtons = 0;
83 return S_OK;
84}
85
86void Mouse::FinalRelease()
87{
88 uninit();
89}
90
91// public methods only for internal purposes
92/////////////////////////////////////////////////////////////////////////////
93
94/**
95 * Initializes the mouse object.
96 *
97 * @returns COM result indicator
98 * @param parent handle of our parent object
99 */
100HRESULT Mouse::init (Console *parent)
101{
102 LogFlowThisFunc(("\n"));
103
104 ComAssertRet(parent, E_INVALIDARG);
105
106 /* Enclose the state transition NotReady->InInit->Ready */
107 AutoInitSpan autoInitSpan(this);
108 AssertReturn(autoInitSpan.isOk(), E_FAIL);
109
110 unconst(mParent) = parent;
111
112 mfHostCaps = 0;
113
114 /* Confirm a successful initialization */
115 autoInitSpan.setSucceeded();
116
117 return S_OK;
118}
119
120/**
121 * Uninitializes the instance and sets the ready flag to FALSE.
122 * Called either from FinalRelease() or by the parent when it gets destroyed.
123 */
124void Mouse::uninit()
125{
126 LogFlowThisFunc(("\n"));
127
128 /* Enclose the state transition Ready->InUninit->NotReady */
129 AutoUninitSpan autoUninitSpan(this);
130 if (autoUninitSpan.uninitDone())
131 return;
132
133 for (unsigned i = 0; i < MOUSE_MAX_DEVICES; ++i)
134 {
135 if (mpDrv[i])
136 mpDrv[i]->pMouse = NULL;
137 mpDrv[i] = NULL;
138 }
139
140#ifdef VBOXBFE_WITHOUT_COM
141 mParent = NULL;
142#else
143 unconst(mParent) = NULL;
144#endif
145}
146
147
148// IMouse properties
149/////////////////////////////////////////////////////////////////////////////
150
151/** Query the VMM device for the Guest Additions's (and the host front-end's)
152 * mouse handling capabilities.
153 * @note all calls out of this object are made with no locks held! */
154HRESULT Mouse::getVMMDevMouseCaps(uint32_t *pfCaps)
155{
156 AssertPtrReturn(pfCaps, E_POINTER);
157 /** @todo does getting the VMMDev and the VMMDevPort like this guarantee
158 * they won't go away while we are using them? */
159 VMMDev *pVMMDev = mParent->getVMMDev();
160 ComAssertRet(pVMMDev, E_FAIL);
161 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
162 ComAssertRet(pVMMDevPort, E_FAIL);
163
164 int rc = pVMMDevPort->pfnQueryMouseCapabilities(pVMMDevPort, pfCaps);
165 return RT_SUCCESS(rc) ? S_OK : E_FAIL;
166}
167
168/** Report the front-end's mouse handling capabilities to the VMM device and
169 * thus to the guest.
170 * @note all calls out of this object are made with no locks held! */
171HRESULT Mouse::setVMMDevMouseCaps(uint32_t fCaps)
172{
173 VMMDev *pVMMDev = mParent->getVMMDev();
174 ComAssertRet(pVMMDev, E_FAIL);
175 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
176 ComAssertRet(pVMMDevPort, E_FAIL);
177
178 int rc = pVMMDevPort->pfnSetMouseCapabilities(pVMMDevPort, fCaps);
179 return RT_SUCCESS(rc) ? S_OK : E_FAIL;
180}
181
182/**
183 * Returns whether the current setup can accept absolute mouse events, either
184 * because an emulated absolute pointing device is active or because the Guest
185 * Additions are.
186 *
187 * @returns COM status code
188 * @param absoluteSupported address of result variable
189 */
190STDMETHODIMP Mouse::COMGETTER(AbsoluteSupported) (BOOL *absoluteSupported)
191{
192 if (!absoluteSupported)
193 return E_POINTER;
194
195 AutoCaller autoCaller(this);
196 if (FAILED(autoCaller.rc())) return autoCaller.rc();
197
198 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
199 bool fAbs = false;
200
201 if (mfVMMDevCanAbs)
202 fAbs = TRUE;
203
204 for (unsigned i = 0; i < MOUSE_MAX_DEVICES; ++i)
205 if (mpDrv[i] && (mpDrv[i]->u32DevCaps & MOUSE_DEVCAP_ABSOLUTE))
206 fAbs = TRUE;
207
208 *absoluteSupported = fAbs;
209 return S_OK;
210}
211
212/**
213 * Returns whether the current setup can accept relative mouse events, that is,
214 * whether an emulated relative pointing device is active.
215 *
216 * @returns COM status code
217 * @param relativeSupported address of result variable
218 */
219STDMETHODIMP Mouse::COMGETTER(RelativeSupported) (BOOL *relativeSupported)
220{
221 if (!relativeSupported)
222 return E_POINTER;
223
224 AutoCaller autoCaller(this);
225 if (FAILED(autoCaller.rc())) return autoCaller.rc();
226
227 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
228 bool fRel = false;
229
230 for (unsigned i = 0; i < MOUSE_MAX_DEVICES; ++i)
231 if (mpDrv[i] && (mpDrv[i]->u32DevCaps & MOUSE_DEVCAP_RELATIVE))
232 fRel = TRUE;
233
234 *relativeSupported = fRel;
235 return S_OK;
236}
237
238/**
239 * Returns whether the guest can currently switch to drawing the mouse cursor
240 * itself if it is asked to by the front-end.
241 *
242 * @returns COM status code
243 * @param pfNeedsHostCursor address of result variable
244 */
245STDMETHODIMP Mouse::COMGETTER(NeedsHostCursor) (BOOL *pfNeedsHostCursor)
246{
247 if (!pfNeedsHostCursor)
248 return E_POINTER;
249
250 AutoCaller autoCaller(this);
251 if (FAILED(autoCaller.rc())) return autoCaller.rc();
252
253 *pfNeedsHostCursor = mfVMMDevNeedsHostCursor;
254 return S_OK;
255}
256
257// IMouse methods
258/////////////////////////////////////////////////////////////////////////////
259
260/** Converts a bitfield containing information about mouse buttons currently
261 * held down from the format used by the front-end to the format used by PDM
262 * and the emulated pointing devices. */
263static uint32_t mouseButtonsToPDM(LONG buttonState)
264{
265 uint32_t fButtons = 0;
266 if (buttonState & MouseButtonState_LeftButton)
267 fButtons |= PDMIMOUSEPORT_BUTTON_LEFT;
268 if (buttonState & MouseButtonState_RightButton)
269 fButtons |= PDMIMOUSEPORT_BUTTON_RIGHT;
270 if (buttonState & MouseButtonState_MiddleButton)
271 fButtons |= PDMIMOUSEPORT_BUTTON_MIDDLE;
272 if (buttonState & MouseButtonState_XButton1)
273 fButtons |= PDMIMOUSEPORT_BUTTON_X1;
274 if (buttonState & MouseButtonState_XButton2)
275 fButtons |= PDMIMOUSEPORT_BUTTON_X2;
276 return fButtons;
277}
278
279
280/**
281 * Send a relative pointer event to the relative device we deem most
282 * appropriate.
283 *
284 * @returns COM status code
285 */
286HRESULT Mouse::reportRelEventToMouseDev(int32_t dx, int32_t dy, int32_t dz,
287 int32_t dw, uint32_t fButtons)
288{
289 if (dx || dy || dz || dw || fButtons != mLastButtons)
290 {
291 PPDMIMOUSEPORT pUpPort = NULL;
292 {
293 AutoReadLock aLock(this COMMA_LOCKVAL_SRC_POS);
294
295 for (unsigned i = 0; !pUpPort && i < MOUSE_MAX_DEVICES; ++i)
296 {
297 if (mpDrv[i] && (mpDrv[i]->u32DevCaps & MOUSE_DEVCAP_RELATIVE))
298 pUpPort = mpDrv[i]->pUpPort;
299 }
300 }
301 if (!pUpPort)
302 return S_OK;
303
304 int vrc = pUpPort->pfnPutEvent(pUpPort, dx, dy, dz, dw, fButtons);
305
306 if (RT_FAILURE(vrc))
307 return setError(VBOX_E_IPRT_ERROR,
308 tr("Could not send the mouse event to the virtual mouse (%Rrc)"),
309 vrc);
310 mLastButtons = fButtons;
311 }
312 return S_OK;
313}
314
315
316/**
317 * Send an absolute pointer event to the emulated absolute device we deem most
318 * appropriate.
319 *
320 * @returns COM status code
321 */
322HRESULT Mouse::reportAbsEventToMouseDev(uint32_t mouseXAbs, uint32_t mouseYAbs,
323 int32_t dz, int32_t dw, uint32_t fButtons)
324{
325 if ( mouseXAbs != mLastAbsX || mouseYAbs != mLastAbsY
326 || dz || dw || fButtons != mLastButtons)
327 {
328 PPDMIMOUSEPORT pUpPort = NULL;
329 {
330 AutoReadLock aLock(this COMMA_LOCKVAL_SRC_POS);
331
332 for (unsigned i = 0; !pUpPort && i < MOUSE_MAX_DEVICES; ++i)
333 {
334 if (mpDrv[i] && (mpDrv[i]->u32DevCaps & MOUSE_DEVCAP_ABSOLUTE))
335 pUpPort = mpDrv[i]->pUpPort;
336 }
337 }
338 if (!pUpPort)
339 return S_OK;
340
341 int vrc = pUpPort->pfnPutEventAbs(pUpPort, mouseXAbs, mouseYAbs, dz,
342 dw, fButtons);
343 if (RT_FAILURE(vrc))
344 return setError(VBOX_E_IPRT_ERROR,
345 tr("Could not send the mouse event to the virtual mouse (%Rrc)"),
346 vrc);
347 mLastButtons = fButtons;
348 }
349 return S_OK;
350}
351
352
353/**
354 * Send an absolute position event to the VMM device.
355 * @note all calls out of this object are made with no locks held!
356 *
357 * @returns COM status code
358 */
359HRESULT Mouse::reportAbsEventToVMMDev(uint32_t mouseXAbs, uint32_t mouseYAbs)
360{
361 VMMDev *pVMMDev = mParent->getVMMDev();
362 ComAssertRet(pVMMDev, E_FAIL);
363 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
364 ComAssertRet(pVMMDevPort, E_FAIL);
365
366 if (mouseXAbs != mLastAbsX || mouseYAbs != mLastAbsY)
367 {
368 int vrc = pVMMDevPort->pfnSetAbsoluteMouse(pVMMDevPort,
369 mouseXAbs, mouseYAbs);
370 if (RT_FAILURE(vrc))
371 return setError(VBOX_E_IPRT_ERROR,
372 tr("Could not send the mouse event to the virtual mouse (%Rrc)"),
373 vrc);
374 }
375 return S_OK;
376}
377
378
379/**
380 * Send an absolute pointer event to a pointing device (the VMM device if
381 * possible or whatever emulated absolute device seems best to us if not).
382 *
383 * @returns COM status code
384 */
385HRESULT Mouse::reportAbsEvent(uint32_t mouseXAbs, uint32_t mouseYAbs,
386 int32_t dz, int32_t dw, uint32_t fButtons,
387 bool fUsesVMMDevEvent)
388{
389 HRESULT rc;
390 /** If we are using the VMMDev to report absolute position but without
391 * VMMDev IRQ support then we need to send a small "jiggle" to the emulated
392 * relative mouse device to alert the guest to changes. */
393 LONG cJiggle = 0;
394
395 if (mfVMMDevCanAbs)
396 {
397 /*
398 * Send the absolute mouse position to the VMM device.
399 */
400 if (mouseXAbs != mLastAbsX || mouseYAbs != mLastAbsY)
401 {
402 rc = reportAbsEventToVMMDev(mouseXAbs, mouseYAbs);
403 cJiggle = 1;
404 }
405 rc = reportRelEventToMouseDev(cJiggle, 0, dz, dw, fButtons);
406 }
407 else
408 rc = reportAbsEventToMouseDev(mouseXAbs, mouseYAbs, dz, dw, fButtons);
409
410 mLastAbsX = mouseXAbs;
411 mLastAbsY = mouseYAbs;
412 return rc;
413}
414
415/**
416 * Send a relative mouse event to the guest.
417 * @note the VMMDev capability change is so that the guest knows we are sending
418 * real events over the PS/2 device and not dummy events to signal the
419 * arrival of new absolute pointer data
420 *
421 * @returns COM status code
422 * @param dx X movement
423 * @param dy Y movement
424 * @param dz Z movement
425 * @param buttonState The mouse button state
426 */
427STDMETHODIMP Mouse::PutMouseEvent(LONG dx, LONG dy, LONG dz, LONG dw, LONG buttonState)
428{
429 HRESULT rc;
430 /** Do we need to send updated capabilities to the VMM device? */
431 bool fUpdateCaps = FALSE;
432 uint32_t fButtons;
433
434 AutoCaller autoCaller(this);
435 if (FAILED(autoCaller.rc())) return autoCaller.rc();
436
437 {
438 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
439
440 LogRel3(("%s: dx=%d, dy=%d, dz=%d, dw=%d\n", __PRETTY_FUNCTION__,
441 dx, dy, dz, dw));
442 /* Make sure that the guest knows that we are sending real movement
443 * events to the PS/2 device and not just dummy wake-up ones. */
444 if (mfHostCaps & VMMDEV_MOUSE_HOST_WANTS_ABSOLUTE)
445 {
446 mfHostCaps &= ~VMMDEV_MOUSE_HOST_WANTS_ABSOLUTE;
447 fUpdateCaps = TRUE;
448 }
449
450 fButtons = mouseButtonsToPDM(buttonState);
451 }
452 /** @note we drop the lock before calling out of the object! */
453 if (fUpdateCaps)
454 setVMMDevMouseCaps(mfHostCaps);
455 rc = reportRelEventToMouseDev(dx, dy, dz, dw, fButtons);
456
457 return rc;
458}
459
460/**
461 * Convert an (X, Y) value pair in screen co-ordinates (starting from 1) to a
462 * value from 0 to 0xffff.
463 *
464 * @returns COM status value
465 */
466HRESULT Mouse::convertDisplayRes(LONG x, LONG y, uint32_t *pcX, uint32_t *pcY)
467{
468 AssertPtrReturn(pcX, E_POINTER);
469 AssertPtrReturn(pcY, E_POINTER);
470 Display *pDisplay = mParent->getDisplay();
471 ComAssertRet(pDisplay, E_FAIL);
472
473 ULONG displayWidth, displayHeight;
474 /* Takes the display lock */
475 HRESULT rc = pDisplay->GetScreenResolution (0, &displayWidth, &displayHeight,
476 NULL);
477 if (FAILED(rc))
478 return rc;
479
480 *pcX = displayWidth ? ((x - 1) * 0xFFFF) / displayWidth: 0;
481 *pcY = displayHeight ? ((y - 1) * 0xFFFF) / displayHeight: 0;
482 return S_OK;
483}
484
485
486/**
487 * Send an absolute mouse event to the VM. This requires either VirtualBox-
488 * specific drivers installed in the guest or absolute pointing device
489 * emulation.
490 * @note the VMMDev capability change is so that the guest knows we are sending
491 * dummy events over the PS/2 device to signal the arrival of new
492 * absolute pointer data, and not pointer real movement data
493 * @note all calls out of this object are made with no locks held!
494 *
495 * @returns COM status code
496 * @param x X position (pixel), starting from 1
497 * @param y Y position (pixel), starting from 1
498 * @param dz Z movement
499 * @param buttonState The mouse button state
500 */
501STDMETHODIMP Mouse::PutMouseEventAbsolute(LONG x, LONG y, LONG dz, LONG dw,
502 LONG buttonState)
503{
504 AutoCaller autoCaller(this);
505 if (FAILED(autoCaller.rc())) return autoCaller.rc();
506
507 LogRel3(("%s: x=%d, y=%d, dz=%d, dw=%d, buttonState=0x%x\n",
508 __PRETTY_FUNCTION__, x, y, dz, dw, buttonState));
509
510 uint32_t mouseXAbs, mouseYAbs;
511 /** Do we need to send updated capabilities to the VMM device? */
512 bool fUpdateCaps = FALSE;
513
514 /** @todo the front end should do this conversion to avoid races */
515 /** @note Or maybe not... races are pretty inherent in everything done in
516 * this object and not really bad as far as I can see. */
517 HRESULT rc = convertDisplayRes(x, y, &mouseXAbs, &mouseYAbs);
518 if (FAILED(rc)) return rc;
519
520 /** @todo multi-monitor Windows guests expect this to be unbounded.
521 * Understand the issues involved and fix for the rest. */
522 /* if (mouseXAbs > 0xffff)
523 mouseXAbs = mLastAbsX;
524 if (mouseYAbs > 0xffff)
525 mouseYAbs = mLastAbsY; */
526
527 uint32_t mouseCaps;
528 rc = getVMMDevMouseCaps(&mouseCaps);
529 if (FAILED(rc)) return rc;
530 uint32_t fButtons = mouseButtonsToPDM(buttonState);
531
532 /* If we are doing old-style (IRQ-less) absolute reporting to the VMM
533 * device then make sure the guest is aware of it, so that it knows to
534 * ignore relative movement on the PS/2 device. */
535 {
536 AutoWriteLock aLock(this COMMA_LOCKVAL_SRC_POS);
537
538 if (mfVMMDevCanAbs && !(mfHostCaps & VMMDEV_MOUSE_HOST_WANTS_ABSOLUTE))
539 {
540 mfHostCaps |= VMMDEV_MOUSE_HOST_WANTS_ABSOLUTE;
541 fUpdateCaps = TRUE;
542 }
543 }
544 /** @note we drop the lock again before calling out! */
545 if (fUpdateCaps)
546 setVMMDevMouseCaps(mfHostCaps);
547
548 rc = reportAbsEvent(mouseXAbs, mouseYAbs, dz, dw, fButtons,
549 mouseCaps & VMMDEV_MOUSE_GUEST_USES_EVENT);
550
551 return rc;
552}
553
554// private methods
555/////////////////////////////////////////////////////////////////////////////
556
557
558/** Work out what mouse capabilities the guest and the front-end have to offer,
559 * based on the state of the available emulated devices and the capabilities
560 * the guest has signalled to the VMM device, and notify the guest and the
561 * Console respectively about what the other can do. */
562void Mouse::sendMouseCapsNotifications(void)
563{
564 bool fAbsDev = false;
565 bool fRelDev = false;
566 uint32_t u32MouseCaps;
567
568 {
569 AutoWriteLock aLock(this COMMA_LOCKVAL_SRC_POS);
570
571 for (unsigned i = 0; i < MOUSE_MAX_DEVICES; ++i)
572 if (mpDrv[i])
573 {
574 if (mpDrv[i]->u32DevCaps & MOUSE_DEVCAP_ABSOLUTE)
575 fAbsDev = true;
576 if (mpDrv[i]->u32DevCaps & MOUSE_DEVCAP_RELATIVE)
577 fRelDev = true;
578 }
579 if (fAbsDev && !(mfHostCaps & VMMDEV_MOUSE_HOST_HAS_ABS_DEV))
580 mfHostCaps |= VMMDEV_MOUSE_HOST_HAS_ABS_DEV;
581 if (!fAbsDev && (mfHostCaps & VMMDEV_MOUSE_HOST_HAS_ABS_DEV))
582 mfHostCaps &= ~VMMDEV_MOUSE_HOST_HAS_ABS_DEV;
583 }
584 /** @note we drop the lock again before calling out! */
585 if (SUCCEEDED(getVMMDevMouseCaps(&u32MouseCaps)))
586 mfVMMDevCanAbs = (u32MouseCaps & VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE)
587 && fRelDev;
588 else
589 mfVMMDevCanAbs = false;
590 /** @todo this call takes the Console lock in order to update the cached
591 * callback data atomically. However I can't see any sign that the cached
592 * data is ever used again. */
593 mParent->onMouseCapabilityChange(fAbsDev || mfVMMDevCanAbs, fRelDev,
594 mfVMMDevNeedsHostCursor);
595 /** @todo if this gets called during device initialisation we get an
596 * error due to VMMDev not being initialised yet. */
597 setVMMDevMouseCaps(mfHostCaps);
598}
599
600
601/**
602 * @interface_method_impl{PDMIMOUSECONNECTOR,pfnReportModes}
603 * A virtual device is notifying us about its current state and capabilities
604 */
605DECLCALLBACK(void) Mouse::mouseReportModes(PPDMIMOUSECONNECTOR pInterface, bool fRel, bool fAbs)
606{
607 PDRVMAINMOUSE pDrv = RT_FROM_MEMBER(pInterface, DRVMAINMOUSE, IConnector);
608 if (fRel)
609 pDrv->u32DevCaps |= MOUSE_DEVCAP_RELATIVE;
610 else
611 pDrv->u32DevCaps &= ~MOUSE_DEVCAP_RELATIVE;
612 if (fAbs)
613 pDrv->u32DevCaps |= MOUSE_DEVCAP_ABSOLUTE;
614 else
615 pDrv->u32DevCaps &= ~MOUSE_DEVCAP_ABSOLUTE;
616
617 pDrv->pMouse->sendMouseCapsNotifications();
618}
619
620
621/**
622 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
623 */
624DECLCALLBACK(void *) Mouse::drvQueryInterface(PPDMIBASE pInterface, const char *pszIID)
625{
626 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
627 PDRVMAINMOUSE pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
628
629 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
630 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUSECONNECTOR, &pDrv->IConnector);
631 return NULL;
632}
633
634
635/**
636 * Destruct a mouse driver instance.
637 *
638 * @returns VBox status.
639 * @param pDrvIns The driver instance data.
640 */
641DECLCALLBACK(void) Mouse::drvDestruct(PPDMDRVINS pDrvIns)
642{
643 PDRVMAINMOUSE pData = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
644 LogFlow(("Mouse::drvDestruct: iInstance=%d\n", pDrvIns->iInstance));
645 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
646
647 if (pData->pMouse)
648 {
649 AutoWriteLock mouseLock(pData->pMouse COMMA_LOCKVAL_SRC_POS);
650 for (unsigned cDev = 0; cDev < MOUSE_MAX_DEVICES; ++cDev)
651 if (pData->pMouse->mpDrv[cDev] == pData)
652 {
653 pData->pMouse->mpDrv[cDev] = NULL;
654 break;
655 }
656 }
657}
658
659
660/**
661 * Construct a mouse driver instance.
662 *
663 * @copydoc FNPDMDRVCONSTRUCT
664 */
665DECLCALLBACK(int) Mouse::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
666{
667 PDRVMAINMOUSE pData = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
668 LogFlow(("drvMainMouse_Construct: iInstance=%d\n", pDrvIns->iInstance));
669 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
670
671 /*
672 * Validate configuration.
673 */
674 if (!CFGMR3AreValuesValid(pCfg, "Object\0"))
675 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
676 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
677 ("Configuration error: Not possible to attach anything to this driver!\n"),
678 VERR_PDM_DRVINS_NO_ATTACH);
679
680 /*
681 * IBase.
682 */
683 pDrvIns->IBase.pfnQueryInterface = Mouse::drvQueryInterface;
684
685 pData->IConnector.pfnReportModes = Mouse::mouseReportModes;
686
687 /*
688 * Get the IMousePort interface of the above driver/device.
689 */
690 pData->pUpPort = (PPDMIMOUSEPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMIMOUSEPORT_IID);
691 if (!pData->pUpPort)
692 {
693 AssertMsgFailed(("Configuration error: No mouse port interface above!\n"));
694 return VERR_PDM_MISSING_INTERFACE_ABOVE;
695 }
696
697 /*
698 * Get the Mouse object pointer and update the mpDrv member.
699 */
700 void *pv;
701 int rc = CFGMR3QueryPtr(pCfg, "Object", &pv);
702 if (RT_FAILURE(rc))
703 {
704 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
705 return rc;
706 }
707 pData->pMouse = (Mouse *)pv; /** @todo Check this cast! */
708 unsigned cDev;
709 {
710 AutoReadLock mouseLock(pData->pMouse COMMA_LOCKVAL_SRC_POS);
711
712 for (cDev = 0; cDev < MOUSE_MAX_DEVICES; ++cDev)
713 if (!pData->pMouse->mpDrv[cDev])
714 {
715 pData->pMouse->mpDrv[cDev] = pData;
716 break;
717 }
718 }
719 if (cDev == MOUSE_MAX_DEVICES)
720 return VERR_NO_MORE_HANDLES;
721
722 return VINF_SUCCESS;
723}
724
725
726/**
727 * Main mouse driver registration record.
728 */
729const PDMDRVREG Mouse::DrvReg =
730{
731 /* u32Version */
732 PDM_DRVREG_VERSION,
733 /* szName */
734 "MainMouse",
735 /* szRCMod */
736 "",
737 /* szR0Mod */
738 "",
739 /* pszDescription */
740 "Main mouse driver (Main as in the API).",
741 /* fFlags */
742 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
743 /* fClass. */
744 PDM_DRVREG_CLASS_MOUSE,
745 /* cMaxInstances */
746 ~0,
747 /* cbInstance */
748 sizeof(DRVMAINMOUSE),
749 /* pfnConstruct */
750 Mouse::drvConstruct,
751 /* pfnDestruct */
752 Mouse::drvDestruct,
753 /* pfnRelocate */
754 NULL,
755 /* pfnIOCtl */
756 NULL,
757 /* pfnPowerOn */
758 NULL,
759 /* pfnReset */
760 NULL,
761 /* pfnSuspend */
762 NULL,
763 /* pfnResume */
764 NULL,
765 /* pfnAttach */
766 NULL,
767 /* pfnDetach */
768 NULL,
769 /* pfnPowerOff */
770 NULL,
771 /* pfnSoftReset */
772 NULL,
773 /* u32EndVersion */
774 PDM_DRVREG_VERSION
775};
776/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use