VirtualBox

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

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

Forgotten variable initialization.

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