VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/MouseImpl.cpp@ 35753

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

Main. QT/FE: fix long standing COM issue

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette