VirtualBox

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

Last change on this file since 47245 was 47245, checked in by vboxsync, 12 years ago

Main/Mouse: updated multi-touch protocol for in-contact informatin.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 28.1 KB
Line 
1/* $Id: MouseImpl.cpp 47245 2013-07-19 08:01:57Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2006-2012 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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 /** The mouse device can do absolute reporting */
41 MOUSE_DEVCAP_MULTI_TOUCH = 4
42};
43/** @} */
44
45
46/**
47 * Mouse driver instance data.
48 */
49struct DRVMAINMOUSE
50{
51 /** Pointer to the mouse object. */
52 Mouse *pMouse;
53 /** Pointer to the driver instance structure. */
54 PPDMDRVINS pDrvIns;
55 /** Pointer to the mouse port interface of the driver/device above us. */
56 PPDMIMOUSEPORT pUpPort;
57 /** Our mouse connector interface. */
58 PDMIMOUSECONNECTOR IConnector;
59 /** The capabilities of this device. */
60 uint32_t u32DevCaps;
61};
62
63
64// constructor / destructor
65/////////////////////////////////////////////////////////////////////////////
66
67Mouse::Mouse()
68 : mParent(NULL)
69{
70}
71
72Mouse::~Mouse()
73{
74}
75
76
77HRESULT Mouse::FinalConstruct()
78{
79 RT_ZERO(mpDrv);
80 mcLastX = 0x8000;
81 mcLastY = 0x8000;
82 mfLastButtons = 0;
83 mfVMMDevGuestCaps = 0;
84 return BaseFinalConstruct();
85}
86
87void Mouse::FinalRelease()
88{
89 uninit();
90 BaseFinalRelease();
91}
92
93// public methods only for internal purposes
94/////////////////////////////////////////////////////////////////////////////
95
96/**
97 * Initializes the mouse object.
98 *
99 * @returns COM result indicator
100 * @param parent handle of our parent object
101 */
102HRESULT Mouse::init (ConsoleMouseInterface *parent)
103{
104 LogFlowThisFunc(("\n"));
105
106 ComAssertRet(parent, E_INVALIDARG);
107
108 /* Enclose the state transition NotReady->InInit->Ready */
109 AutoInitSpan autoInitSpan(this);
110 AssertReturn(autoInitSpan.isOk(), E_FAIL);
111
112 unconst(mParent) = parent;
113
114 unconst(mEventSource).createObject();
115 HRESULT rc = mEventSource->init(static_cast<IMouse*>(this));
116 AssertComRCReturnRC(rc);
117 mMouseEvent.init(mEventSource, VBoxEventType_OnGuestMouse,
118 0, 0, 0, 0, 0);
119
120 /* Confirm a successful initialization */
121 autoInitSpan.setSucceeded();
122
123 return S_OK;
124}
125
126/**
127 * Uninitializes the instance and sets the ready flag to FALSE.
128 * Called either from FinalRelease() or by the parent when it gets destroyed.
129 */
130void Mouse::uninit()
131{
132 LogFlowThisFunc(("\n"));
133
134 /* Enclose the state transition Ready->InUninit->NotReady */
135 AutoUninitSpan autoUninitSpan(this);
136 if (autoUninitSpan.uninitDone())
137 return;
138
139 for (unsigned i = 0; i < MOUSE_MAX_DEVICES; ++i)
140 {
141 if (mpDrv[i])
142 mpDrv[i]->pMouse = NULL;
143 mpDrv[i] = NULL;
144 }
145
146 mMouseEvent.uninit();
147 unconst(mEventSource).setNull();
148 unconst(mParent) = NULL;
149}
150
151
152// IMouse properties
153/////////////////////////////////////////////////////////////////////////////
154
155/** Report the front-end's mouse handling capabilities to the VMM device and
156 * thus to the guest.
157 * @note all calls out of this object are made with no locks held! */
158HRESULT Mouse::updateVMMDevMouseCaps(uint32_t fCapsAdded,
159 uint32_t fCapsRemoved)
160{
161 VMMDevMouseInterface *pVMMDev = mParent->getVMMDevMouseInterface();
162 if (!pVMMDev)
163 return E_FAIL; /* No assertion, as the front-ends can send events
164 * at all sorts of inconvenient times. */
165 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
166 if (!pVMMDevPort)
167 return E_FAIL; /* same here */
168
169 int rc = pVMMDevPort->pfnUpdateMouseCapabilities(pVMMDevPort, fCapsAdded,
170 fCapsRemoved);
171 return RT_SUCCESS(rc) ? S_OK : E_FAIL;
172}
173
174/**
175 * Returns whether the currently active device portfolio can accept absolute
176 * mouse events.
177 *
178 * @returns COM status code
179 * @param absoluteSupported address of result variable
180 */
181STDMETHODIMP Mouse::COMGETTER(AbsoluteSupported) (BOOL *absoluteSupported)
182{
183 if (!absoluteSupported)
184 return E_POINTER;
185
186 AutoCaller autoCaller(this);
187 if (FAILED(autoCaller.rc())) return autoCaller.rc();
188
189 *absoluteSupported = supportsAbs();
190 return S_OK;
191}
192
193/**
194 * Returns whether the currently active device portfolio can accept relative
195 * mouse events.
196 *
197 * @returns COM status code
198 * @param relativeSupported address of result variable
199 */
200STDMETHODIMP Mouse::COMGETTER(RelativeSupported) (BOOL *relativeSupported)
201{
202 if (!relativeSupported)
203 return E_POINTER;
204
205 AutoCaller autoCaller(this);
206 if (FAILED(autoCaller.rc())) return autoCaller.rc();
207
208 *relativeSupported = supportsRel();
209 return S_OK;
210}
211
212/**
213 * Returns whether the currently active device portfolio can accept multi-touch
214 * mouse events.
215 *
216 * @returns COM status code
217 * @param multiTouchSupported address of result variable
218 */
219STDMETHODIMP Mouse::COMGETTER(MultiTouchSupported) (BOOL *multiTouchSupported)
220{
221 if (!multiTouchSupported)
222 return E_POINTER;
223
224 AutoCaller autoCaller(this);
225 if (FAILED(autoCaller.rc())) return autoCaller.rc();
226
227 *multiTouchSupported = FALSE;
228 return S_OK;
229}
230
231/**
232 * Returns whether the guest can currently switch to drawing the mouse cursor
233 * itself if it is asked to by the front-end.
234 *
235 * @returns COM status code
236 * @param pfNeedsHostCursor address of result variable
237 */
238STDMETHODIMP Mouse::COMGETTER(NeedsHostCursor) (BOOL *pfNeedsHostCursor)
239{
240 if (!pfNeedsHostCursor)
241 return E_POINTER;
242
243 AutoCaller autoCaller(this);
244 if (FAILED(autoCaller.rc())) return autoCaller.rc();
245
246 *pfNeedsHostCursor = guestNeedsHostCursor();
247 return S_OK;
248}
249
250// IMouse methods
251/////////////////////////////////////////////////////////////////////////////
252
253/** Converts a bitfield containing information about mouse buttons currently
254 * held down from the format used by the front-end to the format used by PDM
255 * and the emulated pointing devices. */
256static uint32_t mouseButtonsToPDM(LONG buttonState)
257{
258 uint32_t fButtons = 0;
259 if (buttonState & MouseButtonState_LeftButton)
260 fButtons |= PDMIMOUSEPORT_BUTTON_LEFT;
261 if (buttonState & MouseButtonState_RightButton)
262 fButtons |= PDMIMOUSEPORT_BUTTON_RIGHT;
263 if (buttonState & MouseButtonState_MiddleButton)
264 fButtons |= PDMIMOUSEPORT_BUTTON_MIDDLE;
265 if (buttonState & MouseButtonState_XButton1)
266 fButtons |= PDMIMOUSEPORT_BUTTON_X1;
267 if (buttonState & MouseButtonState_XButton2)
268 fButtons |= PDMIMOUSEPORT_BUTTON_X2;
269 return fButtons;
270}
271
272STDMETHODIMP Mouse::COMGETTER(EventSource)(IEventSource ** aEventSource)
273{
274 CheckComArgOutPointerValid(aEventSource);
275
276 AutoCaller autoCaller(this);
277 if (FAILED(autoCaller.rc())) return autoCaller.rc();
278
279 // no need to lock - lifetime constant
280 mEventSource.queryInterfaceTo(aEventSource);
281
282 return S_OK;
283}
284
285/**
286 * Send a relative pointer event to the relative device we deem most
287 * appropriate.
288 *
289 * @returns COM status code
290 */
291HRESULT Mouse::reportRelEventToMouseDev(int32_t dx, int32_t dy, int32_t dz,
292 int32_t dw, uint32_t fButtons)
293{
294 if (dx || dy || dz || dw || fButtons != mfLastButtons)
295 {
296 PPDMIMOUSEPORT pUpPort = NULL;
297 {
298 AutoReadLock aLock(this COMMA_LOCKVAL_SRC_POS);
299
300 for (unsigned i = 0; !pUpPort && i < MOUSE_MAX_DEVICES; ++i)
301 {
302 if (mpDrv[i] && (mpDrv[i]->u32DevCaps & MOUSE_DEVCAP_RELATIVE))
303 pUpPort = mpDrv[i]->pUpPort;
304 }
305 }
306 if (!pUpPort)
307 return S_OK;
308
309 int vrc = pUpPort->pfnPutEvent(pUpPort, dx, dy, dz, dw, fButtons);
310
311 if (RT_FAILURE(vrc))
312 return setError(VBOX_E_IPRT_ERROR,
313 tr("Could not send the mouse event to the virtual mouse (%Rrc)"),
314 vrc);
315 mfLastButtons = fButtons;
316 }
317 return S_OK;
318}
319
320
321/**
322 * Send an absolute pointer event to the emulated absolute device we deem most
323 * appropriate.
324 *
325 * @returns COM status code
326 */
327HRESULT Mouse::reportAbsEventToMouseDev(int32_t x, int32_t y,
328 int32_t dz, int32_t dw, uint32_t fButtons)
329{
330 if ( x < VMMDEV_MOUSE_RANGE_MIN
331 || x > VMMDEV_MOUSE_RANGE_MAX)
332 return S_OK;
333 if ( y < VMMDEV_MOUSE_RANGE_MIN
334 || y > VMMDEV_MOUSE_RANGE_MAX)
335 return S_OK;
336 if ( x != mcLastX || y != mcLastY
337 || dz || dw || fButtons != mfLastButtons)
338 {
339 PPDMIMOUSEPORT pUpPort = NULL;
340 {
341 AutoReadLock aLock(this COMMA_LOCKVAL_SRC_POS);
342
343 for (unsigned i = 0; !pUpPort && i < MOUSE_MAX_DEVICES; ++i)
344 {
345 if (mpDrv[i] && (mpDrv[i]->u32DevCaps & MOUSE_DEVCAP_ABSOLUTE))
346 pUpPort = mpDrv[i]->pUpPort;
347 }
348 }
349 if (!pUpPort)
350 return S_OK;
351
352 int vrc = pUpPort->pfnPutEventAbs(pUpPort, x, y, dz,
353 dw, fButtons);
354 if (RT_FAILURE(vrc))
355 return setError(VBOX_E_IPRT_ERROR,
356 tr("Could not send the mouse event to the virtual mouse (%Rrc)"),
357 vrc);
358 mfLastButtons = fButtons;
359
360 }
361 return S_OK;
362}
363
364
365/**
366 * Send a multi-touch event to the first enabled emulated multi-touch device.
367 *
368 * @returns COM status code
369 */
370HRESULT Mouse::reportMTEventToMouseDev(int32_t x, int32_t y,
371 uint32_t cContact, uint32_t fContact)
372{
373 if ( x < VMMDEV_MOUSE_RANGE_MIN
374 || x > VMMDEV_MOUSE_RANGE_MAX)
375 return S_OK;
376 if ( y < VMMDEV_MOUSE_RANGE_MIN
377 || y > VMMDEV_MOUSE_RANGE_MAX)
378 return S_OK;
379 PPDMIMOUSEPORT pUpPort = NULL;
380 {
381 AutoReadLock aLock(this COMMA_LOCKVAL_SRC_POS);
382
383 for (unsigned i = 0; !pUpPort && i < MOUSE_MAX_DEVICES; ++i)
384 {
385 if ( mpDrv[i]
386 && (mpDrv[i]->u32DevCaps & MOUSE_DEVCAP_MULTI_TOUCH))
387 pUpPort = mpDrv[i]->pUpPort;
388 }
389 }
390 if (!pUpPort)
391 return S_OK;
392
393 int vrc = pUpPort->pfnPutEventMT(pUpPort, x, y,
394 cContact, fContact);
395 if (RT_FAILURE(vrc))
396 return setError(VBOX_E_IPRT_ERROR,
397 tr("Could not send the touch event to the virtual device (%Rrc)"),
398 vrc);
399 return S_OK;
400}
401
402
403/**
404 * Send an absolute position event to the VMM device.
405 * @note all calls out of this object are made with no locks held!
406 *
407 * @returns COM status code
408 */
409HRESULT Mouse::reportAbsEventToVMMDev(int32_t x, int32_t y)
410{
411 VMMDevMouseInterface *pVMMDev = mParent->getVMMDevMouseInterface();
412 ComAssertRet(pVMMDev, E_FAIL);
413 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
414 ComAssertRet(pVMMDevPort, E_FAIL);
415
416 if (x != mcLastX || y != mcLastY)
417 {
418 int vrc = pVMMDevPort->pfnSetAbsoluteMouse(pVMMDevPort,
419 x, y);
420 if (RT_FAILURE(vrc))
421 return setError(VBOX_E_IPRT_ERROR,
422 tr("Could not send the mouse event to the virtual mouse (%Rrc)"),
423 vrc);
424 }
425 return S_OK;
426}
427
428
429/**
430 * Send an absolute pointer event to a pointing device (the VMM device if
431 * possible or whatever emulated absolute device seems best to us if not).
432 *
433 * @returns COM status code
434 */
435HRESULT Mouse::reportAbsEvent(int32_t x, int32_t y,
436 int32_t dz, int32_t dw, uint32_t fButtons,
437 bool fUsesVMMDevEvent)
438{
439 HRESULT rc = S_OK;
440 /** If we are using the VMMDev to report absolute position but without
441 * VMMDev IRQ support then we need to send a small "jiggle" to the emulated
442 * relative mouse device to alert the guest to changes. */
443 LONG cJiggle = 0;
444
445 /*
446 * Send the absolute mouse position to the device.
447 */
448 if (x != mcLastX || y != mcLastY)
449 {
450 if (vmmdevCanAbs())
451 {
452 rc = reportAbsEventToVMMDev(x, y);
453 cJiggle = !fUsesVMMDevEvent;
454 }
455 else
456 {
457 rc = reportAbsEventToMouseDev(x, y, 0, 0, fButtons);
458 fButtons = 0;
459 }
460 }
461 if (SUCCEEDED(rc))
462 rc = reportRelEventToMouseDev(cJiggle, 0, dz, dw, fButtons);
463
464 mcLastX = x;
465 mcLastY = y;
466 return rc;
467}
468
469void Mouse::fireMouseEvent(bool fAbsolute, LONG x, LONG y, LONG dz, LONG dw,
470 LONG cContact, LONG fButtons)
471{
472 /* If mouse button is pressed, we generate new event, to avoid reusable events coalescing and thus
473 dropping key press events */
474 if (fButtons != 0)
475 {
476 VBoxEventDesc evDesc;
477 evDesc.init(mEventSource, VBoxEventType_OnGuestMouse, fAbsolute, x, y,
478 dz, dw, cContact, fButtons);
479 evDesc.fire(0);
480 }
481 else
482 {
483 mMouseEvent.reinit(VBoxEventType_OnGuestMouse, fAbsolute, x, y, dz, dw,
484 cContact, fButtons);
485 mMouseEvent.fire(0);
486 }
487}
488
489
490/**
491 * Send a relative mouse event to the guest.
492 * @note the VMMDev capability change is so that the guest knows we are sending
493 * real events over the PS/2 device and not dummy events to signal the
494 * arrival of new absolute pointer data
495 *
496 * @returns COM status code
497 * @param dx X movement
498 * @param dy Y movement
499 * @param dz Z movement
500 * @param fButtons The mouse button state
501 */
502STDMETHODIMP Mouse::PutMouseEvent(LONG dx, LONG dy, LONG dz, LONG dw,
503 LONG fButtons)
504{
505 HRESULT rc;
506 uint32_t fButtonsAdj;
507
508 AutoCaller autoCaller(this);
509 if (FAILED(autoCaller.rc())) return autoCaller.rc();
510 LogRel3(("%s: dx=%d, dy=%d, dz=%d, dw=%d\n", __PRETTY_FUNCTION__,
511 dx, dy, dz, dw));
512
513 fButtonsAdj = mouseButtonsToPDM(fButtons);
514 /* Make sure that the guest knows that we are sending real movement
515 * events to the PS/2 device and not just dummy wake-up ones. */
516 updateVMMDevMouseCaps(0, VMMDEV_MOUSE_HOST_WANTS_ABSOLUTE);
517 rc = reportRelEventToMouseDev(dx, dy, dz, dw, fButtonsAdj);
518
519 fireMouseEvent(false, dx, dy, dz, dw, 0, fButtons);
520
521 return rc;
522}
523
524/**
525 * Convert an (X, Y) value pair in screen co-ordinates (starting from 1) to a
526 * value from VMMDEV_MOUSE_RANGE_MIN to VMMDEV_MOUSE_RANGE_MAX. Sets the
527 * optional validity value to false if the pair is not on an active screen and
528 * to true otherwise.
529 * @note since guests with recent versions of X.Org use a different method
530 * to everyone else to map the valuator value to a screen pixel (they
531 * multiply by the screen dimension, do a floating point divide by
532 * the valuator maximum and round the result, while everyone else
533 * does truncating integer operations) we adjust the value we send
534 * so that it maps to the right pixel both when the result is rounded
535 * and when it is truncated.
536 *
537 * @returns COM status value
538 */
539HRESULT Mouse::convertDisplayRes(LONG x, LONG y, int32_t *pxAdj, int32_t *pyAdj,
540 bool *pfValid)
541{
542 AssertPtrReturn(pxAdj, E_POINTER);
543 AssertPtrReturn(pyAdj, E_POINTER);
544 AssertPtrNullReturn(pfValid, E_POINTER);
545 DisplayMouseInterface *pDisplay = mParent->getDisplayMouseInterface();
546 ComAssertRet(pDisplay, E_FAIL);
547 /** The amount to add to the result (multiplied by the screen width/height)
548 * to compensate for differences in guest methods for mapping back to
549 * pixels */
550 enum { ADJUST_RANGE = - 3 * VMMDEV_MOUSE_RANGE / 4 };
551
552 if (pfValid)
553 *pfValid = true;
554 if (!(mfVMMDevGuestCaps & VMMDEV_MOUSE_NEW_PROTOCOL))
555 {
556 ULONG displayWidth, displayHeight;
557 /* Takes the display lock */
558 HRESULT rc = pDisplay->getScreenResolution(0, &displayWidth,
559 &displayHeight, NULL);
560 if (FAILED(rc))
561 return rc;
562
563 *pxAdj = displayWidth ? (x * VMMDEV_MOUSE_RANGE + ADJUST_RANGE)
564 / (LONG) displayWidth: 0;
565 *pyAdj = displayHeight ? (y * VMMDEV_MOUSE_RANGE + ADJUST_RANGE)
566 / (LONG) displayHeight: 0;
567 }
568 else
569 {
570 int32_t x1, y1, x2, y2;
571 /* Takes the display lock */
572 pDisplay->getFramebufferDimensions(&x1, &y1, &x2, &y2);
573 *pxAdj = x1 < x2 ? ((x - x1) * VMMDEV_MOUSE_RANGE + ADJUST_RANGE)
574 / (x2 - x1) : 0;
575 *pyAdj = y1 < y2 ? ((y - y1) * VMMDEV_MOUSE_RANGE + ADJUST_RANGE)
576 / (y2 - y1) : 0;
577 if ( *pxAdj < VMMDEV_MOUSE_RANGE_MIN
578 || *pxAdj > VMMDEV_MOUSE_RANGE_MAX
579 || *pyAdj < VMMDEV_MOUSE_RANGE_MIN
580 || *pyAdj > VMMDEV_MOUSE_RANGE_MAX)
581 if (pfValid)
582 *pfValid = false;
583 }
584 return S_OK;
585}
586
587
588/**
589 * Send an absolute mouse event to the VM. This requires either VirtualBox-
590 * specific drivers installed in the guest or absolute pointing device
591 * emulation.
592 * @note the VMMDev capability change is so that the guest knows we are sending
593 * dummy events over the PS/2 device to signal the arrival of new
594 * absolute pointer data, and not pointer real movement data
595 * @note all calls out of this object are made with no locks held!
596 *
597 * @returns COM status code
598 * @param x X position (pixel), starting from 1
599 * @param y Y position (pixel), starting from 1
600 * @param dz Z movement
601 * @param fButtons The mouse button state
602 */
603STDMETHODIMP Mouse::PutMouseEventAbsolute(LONG x, LONG y, LONG dz, LONG dw,
604 LONG fButtons)
605{
606 AutoCaller autoCaller(this);
607 if (FAILED(autoCaller.rc())) return autoCaller.rc();
608
609 LogRel3(("%s: x=%d, y=%d, dz=%d, dw=%d, fButtons=0x%x\n",
610 __PRETTY_FUNCTION__, x, y, dz, dw, fButtons));
611
612 int32_t xAdj, yAdj;
613 uint32_t fButtonsAdj;
614 bool fValid;
615
616 /** @todo the front end should do this conversion to avoid races */
617 /** @note Or maybe not... races are pretty inherent in everything done in
618 * this object and not really bad as far as I can see. */
619 HRESULT rc = convertDisplayRes(x, y, &xAdj, &yAdj, &fValid);
620 if (FAILED(rc)) return rc;
621
622 fButtonsAdj = mouseButtonsToPDM(fButtons);
623 /* If we are doing old-style (IRQ-less) absolute reporting to the VMM
624 * device then make sure the guest is aware of it, so that it knows to
625 * ignore relative movement on the PS/2 device. */
626 updateVMMDevMouseCaps(VMMDEV_MOUSE_HOST_WANTS_ABSOLUTE, 0);
627 if (fValid)
628 {
629 rc = reportAbsEvent(xAdj, yAdj, dz, dw, fButtonsAdj,
630 RT_BOOL( mfVMMDevGuestCaps
631 & VMMDEV_MOUSE_NEW_PROTOCOL));
632
633 fireMouseEvent(true, x, y, dz, dw, 0, fButtons);
634 }
635
636 return rc;
637}
638
639/**
640 * @interface_method_impl{IMouse,putMouseEventMultiTouch}
641 */
642STDMETHODIMP Mouse::PutMouseEventMultiTouch(LONG x, LONG y, LONG cContact,
643 LONG contactState)
644{
645 AutoCaller autoCaller(this);
646 if (FAILED(autoCaller.rc())) return autoCaller.rc();
647
648 LogRel3(("%s: x=%d, y=%d, cContact=%d, contactState=%d\n",
649 __PRETTY_FUNCTION__, x, y, cContact, contactState));
650
651 int32_t xAdj, yAdj;
652 bool fValid;
653
654 /** @todo the front end should do this conversion to avoid races */
655 /** @note Or maybe not... races are pretty inherent in everything done in
656 * this object and not really bad as far as I can see. */
657 HRESULT rc = convertDisplayRes(x, y, &xAdj, &yAdj, &fValid);
658 if (FAILED(rc)) return rc;
659
660 if (fValid)
661 {
662 rc = reportMTEventToMouseDev(xAdj, yAdj, cContact, contactState);
663
664 fireMouseEvent(true, x, y, 0, 0, cContact, contactState);
665 }
666
667 return rc;
668}
669
670// private methods
671/////////////////////////////////////////////////////////////////////////////
672
673
674/** Does the guest currently rely on the host to draw the mouse cursor or
675 * can it switch to doing it itself in software? */
676bool Mouse::guestNeedsHostCursor(void)
677{
678 return RT_BOOL(mfVMMDevGuestCaps & VMMDEV_MOUSE_GUEST_NEEDS_HOST_CURSOR);
679}
680
681
682/** Check what sort of reporting can be done using the devices currently
683 * enabled. Does not consider the VMM device. */
684void Mouse::getDeviceCaps(bool *pfAbs, bool *pfRel, bool *pfMT)
685{
686 bool fAbsDev = false;
687 bool fRelDev = false;
688 bool fMTDev = false;
689
690 AutoReadLock aLock(this COMMA_LOCKVAL_SRC_POS);
691
692 for (unsigned i = 0; i < MOUSE_MAX_DEVICES; ++i)
693 if (mpDrv[i])
694 {
695 if (mpDrv[i]->u32DevCaps & MOUSE_DEVCAP_ABSOLUTE)
696 fAbsDev = true;
697 if (mpDrv[i]->u32DevCaps & MOUSE_DEVCAP_RELATIVE)
698 fRelDev = true;
699 if (mpDrv[i]->u32DevCaps & MOUSE_DEVCAP_MULTI_TOUCH)
700 fMTDev = true;
701 }
702 if (pfAbs)
703 *pfAbs = fAbsDev;
704 if (pfRel)
705 *pfRel = fRelDev;
706}
707
708
709/** Does the VMM device currently support absolute reporting? */
710bool Mouse::vmmdevCanAbs(void)
711{
712 bool fRelDev;
713
714 getDeviceCaps(NULL, &fRelDev, NULL);
715 return (mfVMMDevGuestCaps & VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE)
716 && fRelDev;
717}
718
719
720/** Does the VMM device currently support absolute reporting? */
721bool Mouse::deviceCanAbs(void)
722{
723 bool fAbsDev;
724
725 getDeviceCaps(&fAbsDev, NULL, NULL);
726 return fAbsDev;
727}
728
729
730/** Can we currently send relative events to the guest? */
731bool Mouse::supportsRel(void)
732{
733 bool fRelDev;
734
735 getDeviceCaps(NULL, &fRelDev, NULL);
736 return fRelDev;
737}
738
739
740/** Can we currently send absolute events to the guest? */
741bool Mouse::supportsAbs(void)
742{
743 bool fAbsDev;
744
745 getDeviceCaps(&fAbsDev, NULL, NULL);
746 return fAbsDev || vmmdevCanAbs();
747}
748
749
750/** Can we currently send absolute events to the guest? */
751bool Mouse::supportsMT(void)
752{
753 bool fMTDev;
754
755 getDeviceCaps(NULL, NULL, &fMTDev);
756 return fMTDev;
757}
758
759
760/** Check what sort of reporting can be done using the devices currently
761 * enabled (including the VMM device) and notify the guest and the front-end.
762 */
763void Mouse::sendMouseCapsNotifications(void)
764{
765 bool fAbsDev, fRelDev, fMTDev, fCanAbs, fNeedsHostCursor;
766
767 {
768 AutoReadLock aLock(this COMMA_LOCKVAL_SRC_POS);
769
770 getDeviceCaps(&fAbsDev, &fRelDev, &fMTDev);
771 fCanAbs = supportsAbs();
772 fNeedsHostCursor = guestNeedsHostCursor();
773 }
774 if (fAbsDev)
775 updateVMMDevMouseCaps(VMMDEV_MOUSE_HOST_HAS_ABS_DEV, 0);
776 else
777 updateVMMDevMouseCaps(0, VMMDEV_MOUSE_HOST_HAS_ABS_DEV);
778 /** @todo this call takes the Console lock in order to update the cached
779 * callback data atomically. However I can't see any sign that the cached
780 * data is ever used again. */
781 mParent->onMouseCapabilityChange(fCanAbs, fRelDev, fMTDev, fNeedsHostCursor);
782}
783
784
785/**
786 * @interface_method_impl{PDMIMOUSECONNECTOR,pfnReportModes}
787 * A virtual device is notifying us about its current state and capabilities
788 */
789DECLCALLBACK(void) Mouse::mouseReportModes(PPDMIMOUSECONNECTOR pInterface, bool fRel, bool fAbs, bool fMT)
790{
791 PDRVMAINMOUSE pDrv = RT_FROM_MEMBER(pInterface, DRVMAINMOUSE, IConnector);
792 if (fRel)
793 pDrv->u32DevCaps |= MOUSE_DEVCAP_RELATIVE;
794 else
795 pDrv->u32DevCaps &= ~MOUSE_DEVCAP_RELATIVE;
796 if (fAbs)
797 pDrv->u32DevCaps |= MOUSE_DEVCAP_ABSOLUTE;
798 else
799 pDrv->u32DevCaps &= ~MOUSE_DEVCAP_ABSOLUTE;
800 if (fMT)
801 pDrv->u32DevCaps |= MOUSE_DEVCAP_MULTI_TOUCH;
802 else
803 pDrv->u32DevCaps &= ~MOUSE_DEVCAP_MULTI_TOUCH;
804
805 pDrv->pMouse->sendMouseCapsNotifications();
806}
807
808
809/**
810 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
811 */
812DECLCALLBACK(void *) Mouse::drvQueryInterface(PPDMIBASE pInterface, const char *pszIID)
813{
814 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
815 PDRVMAINMOUSE pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
816
817 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
818 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUSECONNECTOR, &pDrv->IConnector);
819 return NULL;
820}
821
822
823/**
824 * Destruct a mouse driver instance.
825 *
826 * @returns VBox status.
827 * @param pDrvIns The driver instance data.
828 */
829DECLCALLBACK(void) Mouse::drvDestruct(PPDMDRVINS pDrvIns)
830{
831 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
832 PDRVMAINMOUSE pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
833 LogFlow(("Mouse::drvDestruct: iInstance=%d\n", pDrvIns->iInstance));
834
835 if (pThis->pMouse)
836 {
837 AutoWriteLock mouseLock(pThis->pMouse COMMA_LOCKVAL_SRC_POS);
838 for (unsigned cDev = 0; cDev < MOUSE_MAX_DEVICES; ++cDev)
839 if (pThis->pMouse->mpDrv[cDev] == pThis)
840 {
841 pThis->pMouse->mpDrv[cDev] = NULL;
842 break;
843 }
844 }
845}
846
847
848/**
849 * Construct a mouse driver instance.
850 *
851 * @copydoc FNPDMDRVCONSTRUCT
852 */
853DECLCALLBACK(int) Mouse::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
854{
855 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
856 PDRVMAINMOUSE pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
857 LogFlow(("drvMainMouse_Construct: iInstance=%d\n", pDrvIns->iInstance));
858
859 /*
860 * Validate configuration.
861 */
862 if (!CFGMR3AreValuesValid(pCfg, "Object\0"))
863 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
864 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
865 ("Configuration error: Not possible to attach anything to this driver!\n"),
866 VERR_PDM_DRVINS_NO_ATTACH);
867
868 /*
869 * IBase.
870 */
871 pDrvIns->IBase.pfnQueryInterface = Mouse::drvQueryInterface;
872
873 pThis->IConnector.pfnReportModes = Mouse::mouseReportModes;
874
875 /*
876 * Get the IMousePort interface of the above driver/device.
877 */
878 pThis->pUpPort = (PPDMIMOUSEPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMIMOUSEPORT_IID);
879 if (!pThis->pUpPort)
880 {
881 AssertMsgFailed(("Configuration error: No mouse port interface above!\n"));
882 return VERR_PDM_MISSING_INTERFACE_ABOVE;
883 }
884
885 /*
886 * Get the Mouse object pointer and update the mpDrv member.
887 */
888 void *pv;
889 int rc = CFGMR3QueryPtr(pCfg, "Object", &pv);
890 if (RT_FAILURE(rc))
891 {
892 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
893 return rc;
894 }
895 pThis->pMouse = (Mouse *)pv; /** @todo Check this cast! */
896 unsigned cDev;
897 {
898 AutoReadLock mouseLock(pThis->pMouse COMMA_LOCKVAL_SRC_POS);
899
900 for (cDev = 0; cDev < MOUSE_MAX_DEVICES; ++cDev)
901 if (!pThis->pMouse->mpDrv[cDev])
902 {
903 pThis->pMouse->mpDrv[cDev] = pThis;
904 break;
905 }
906 }
907 if (cDev == MOUSE_MAX_DEVICES)
908 return VERR_NO_MORE_HANDLES;
909
910 return VINF_SUCCESS;
911}
912
913
914/**
915 * Main mouse driver registration record.
916 */
917const PDMDRVREG Mouse::DrvReg =
918{
919 /* u32Version */
920 PDM_DRVREG_VERSION,
921 /* szName */
922 "MainMouse",
923 /* szRCMod */
924 "",
925 /* szR0Mod */
926 "",
927 /* pszDescription */
928 "Main mouse driver (Main as in the API).",
929 /* fFlags */
930 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
931 /* fClass. */
932 PDM_DRVREG_CLASS_MOUSE,
933 /* cMaxInstances */
934 ~0U,
935 /* cbInstance */
936 sizeof(DRVMAINMOUSE),
937 /* pfnConstruct */
938 Mouse::drvConstruct,
939 /* pfnDestruct */
940 Mouse::drvDestruct,
941 /* pfnRelocate */
942 NULL,
943 /* pfnIOCtl */
944 NULL,
945 /* pfnPowerOn */
946 NULL,
947 /* pfnReset */
948 NULL,
949 /* pfnSuspend */
950 NULL,
951 /* pfnResume */
952 NULL,
953 /* pfnAttach */
954 NULL,
955 /* pfnDetach */
956 NULL,
957 /* pfnPowerOff */
958 NULL,
959 /* pfnSoftReset */
960 NULL,
961 /* u32EndVersion */
962 PDM_DRVREG_VERSION
963};
964/* 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