VirtualBox

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

Last change on this file since 97656 was 97558, checked in by vboxsync, 2 years ago

Backed out r153813, r153818: Main: src-client: MouseImpl: Provide guest's absolute pointing mouse device with buttons state when mouse intergration is ON, bugref:10285.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 42.5 KB
Line 
1/* $Id: MouseImpl.cpp 97558 2022-11-15 22:04:54Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#define LOG_GROUP LOG_GROUP_MAIN_MOUSE
29#include "LoggingNew.h"
30
31#include <iprt/cpp/utils.h>
32
33#include "MouseImpl.h"
34#include "DisplayImpl.h"
35#include "VMMDev.h"
36#include "MousePointerShapeWrap.h"
37#include "VBoxEvents.h"
38
39#include <VBox/vmm/pdmdrv.h>
40#include <VBox/VMMDev.h>
41#include <VBox/err.h>
42
43
44class ATL_NO_VTABLE MousePointerShape:
45 public MousePointerShapeWrap
46{
47public:
48
49 DECLARE_COMMON_CLASS_METHODS(MousePointerShape)
50
51 HRESULT FinalConstruct();
52 void FinalRelease();
53
54 /* Public initializer/uninitializer for internal purposes only. */
55 HRESULT init(ComObjPtr<Mouse> pMouse,
56 bool fVisible, bool fAlpha,
57 uint32_t hotX, uint32_t hotY,
58 uint32_t width, uint32_t height,
59 const uint8_t *pu8Shape, uint32_t cbShape);
60 void uninit();
61
62private:
63 // wrapped IMousePointerShape properties
64 virtual HRESULT getVisible(BOOL *aVisible);
65 virtual HRESULT getAlpha(BOOL *aAlpha);
66 virtual HRESULT getHotX(ULONG *aHotX);
67 virtual HRESULT getHotY(ULONG *aHotY);
68 virtual HRESULT getWidth(ULONG *aWidth);
69 virtual HRESULT getHeight(ULONG *aHeight);
70 virtual HRESULT getShape(std::vector<BYTE> &aShape);
71
72 struct Data
73 {
74 ComObjPtr<Mouse> pMouse;
75 bool fVisible;
76 bool fAlpha;
77 uint32_t hotX;
78 uint32_t hotY;
79 uint32_t width;
80 uint32_t height;
81 std::vector<BYTE> shape;
82 };
83
84 Data m;
85};
86
87/*
88 * MousePointerShape implementation.
89 */
90DEFINE_EMPTY_CTOR_DTOR(MousePointerShape)
91
92HRESULT MousePointerShape::FinalConstruct()
93{
94 return BaseFinalConstruct();
95}
96
97void MousePointerShape::FinalRelease()
98{
99 uninit();
100
101 BaseFinalRelease();
102}
103
104HRESULT MousePointerShape::init(ComObjPtr<Mouse> pMouse,
105 bool fVisible, bool fAlpha,
106 uint32_t hotX, uint32_t hotY,
107 uint32_t width, uint32_t height,
108 const uint8_t *pu8Shape, uint32_t cbShape)
109{
110 LogFlowThisFunc(("v %d, a %d, h %d,%d, %dx%d, cb %d\n",
111 fVisible, fAlpha, hotX, hotY, width, height, cbShape));
112
113 /* Enclose the state transition NotReady->InInit->Ready */
114 AutoInitSpan autoInitSpan(this);
115 AssertReturn(autoInitSpan.isOk(), E_FAIL);
116
117 m.pMouse = pMouse;
118 m.fVisible = fVisible;
119 m.fAlpha = fAlpha;
120 m.hotX = hotX;
121 m.hotY = hotY;
122 m.width = width;
123 m.height = height;
124 m.shape.resize(cbShape);
125 if (cbShape)
126 {
127 memcpy(&m.shape.front(), pu8Shape, cbShape);
128 }
129
130 /* Confirm a successful initialization */
131 autoInitSpan.setSucceeded();
132
133 return S_OK;
134}
135
136void MousePointerShape::uninit()
137{
138 LogFlowThisFunc(("\n"));
139
140 /* Enclose the state transition Ready->InUninit->NotReady */
141 AutoUninitSpan autoUninitSpan(this);
142 if (autoUninitSpan.uninitDone())
143 return;
144
145 m.pMouse.setNull();
146}
147
148HRESULT MousePointerShape::getVisible(BOOL *aVisible)
149{
150 *aVisible = m.fVisible;
151 return S_OK;
152}
153
154HRESULT MousePointerShape::getAlpha(BOOL *aAlpha)
155{
156 *aAlpha = m.fAlpha;
157 return S_OK;
158}
159
160HRESULT MousePointerShape::getHotX(ULONG *aHotX)
161{
162 *aHotX = m.hotX;
163 return S_OK;
164}
165
166HRESULT MousePointerShape::getHotY(ULONG *aHotY)
167{
168 *aHotY = m.hotY;
169 return S_OK;
170}
171
172HRESULT MousePointerShape::getWidth(ULONG *aWidth)
173{
174 *aWidth = m.width;
175 return S_OK;
176}
177
178HRESULT MousePointerShape::getHeight(ULONG *aHeight)
179{
180 *aHeight = m.height;
181 return S_OK;
182}
183
184HRESULT MousePointerShape::getShape(std::vector<BYTE> &aShape)
185{
186 aShape.resize(m.shape.size());
187 if (m.shape.size())
188 memcpy(&aShape.front(), &m.shape.front(), aShape.size());
189 return S_OK;
190}
191
192
193/** @name Mouse device capabilities bitfield
194 * @{ */
195enum
196{
197 /** The mouse device can do relative reporting */
198 MOUSE_DEVCAP_RELATIVE = 1,
199 /** The mouse device can do absolute reporting */
200 MOUSE_DEVCAP_ABSOLUTE = 2,
201 /** The mouse device can do absolute multi-touch reporting */
202 MOUSE_DEVCAP_MT_ABSOLUTE = 4,
203 /** The mouse device can do relative multi-touch reporting */
204 MOUSE_DEVCAP_MT_RELATIVE = 8,
205};
206/** @} */
207
208
209/**
210 * Mouse driver instance data.
211 */
212struct DRVMAINMOUSE
213{
214 /** Pointer to the mouse object. */
215 Mouse *pMouse;
216 /** Pointer to the driver instance structure. */
217 PPDMDRVINS pDrvIns;
218 /** Pointer to the mouse port interface of the driver/device above us. */
219 PPDMIMOUSEPORT pUpPort;
220 /** Our mouse connector interface. */
221 PDMIMOUSECONNECTOR IConnector;
222 /** The capabilities of this device. */
223 uint32_t u32DevCaps;
224};
225
226
227// constructor / destructor
228/////////////////////////////////////////////////////////////////////////////
229
230Mouse::Mouse()
231 : mParent(NULL)
232{
233}
234
235Mouse::~Mouse()
236{
237}
238
239
240HRESULT Mouse::FinalConstruct()
241{
242 RT_ZERO(mpDrv);
243 RT_ZERO(mPointerData);
244 mcLastX = 0x8000;
245 mcLastY = 0x8000;
246 mfLastButtons = 0;
247 mfVMMDevGuestCaps = 0;
248 return BaseFinalConstruct();
249}
250
251void Mouse::FinalRelease()
252{
253 uninit();
254 BaseFinalRelease();
255}
256
257// public methods only for internal purposes
258/////////////////////////////////////////////////////////////////////////////
259
260/**
261 * Initializes the mouse object.
262 *
263 * @returns COM result indicator
264 * @param parent handle of our parent object
265 */
266HRESULT Mouse::init (ConsoleMouseInterface *parent)
267{
268 LogFlowThisFunc(("\n"));
269
270 ComAssertRet(parent, E_INVALIDARG);
271
272 /* Enclose the state transition NotReady->InInit->Ready */
273 AutoInitSpan autoInitSpan(this);
274 AssertReturn(autoInitSpan.isOk(), E_FAIL);
275
276 unconst(mParent) = parent;
277
278 unconst(mEventSource).createObject();
279 HRESULT hrc = mEventSource->init();
280 AssertComRCReturnRC(hrc);
281
282 ComPtr<IEvent> ptrEvent;
283 hrc = ::CreateGuestMouseEvent(ptrEvent.asOutParam(), mEventSource,
284 (GuestMouseEventMode_T)0, 0 /*x*/, 0 /*y*/, 0 /*z*/, 0 /*w*/, 0 /*buttons*/);
285 AssertComRCReturnRC(hrc);
286 mMouseEvent.init(ptrEvent, mEventSource);
287
288 /* Confirm a successful initialization */
289 autoInitSpan.setSucceeded();
290
291 return S_OK;
292}
293
294/**
295 * Uninitializes the instance and sets the ready flag to FALSE.
296 * Called either from FinalRelease() or by the parent when it gets destroyed.
297 */
298void Mouse::uninit()
299{
300 LogFlowThisFunc(("\n"));
301
302 /* Enclose the state transition Ready->InUninit->NotReady */
303 AutoUninitSpan autoUninitSpan(this);
304 if (autoUninitSpan.uninitDone())
305 return;
306
307 for (unsigned i = 0; i < MOUSE_MAX_DEVICES; ++i)
308 {
309 if (mpDrv[i])
310 mpDrv[i]->pMouse = NULL;
311 mpDrv[i] = NULL;
312 }
313
314 mPointerShape.setNull();
315
316 RTMemFree(mPointerData.pu8Shape);
317 mPointerData.pu8Shape = NULL;
318 mPointerData.cbShape = 0;
319
320 mMouseEvent.uninit();
321 unconst(mEventSource).setNull();
322 unconst(mParent) = NULL;
323}
324
325void Mouse::updateMousePointerShape(bool fVisible, bool fAlpha,
326 uint32_t hotX, uint32_t hotY,
327 uint32_t width, uint32_t height,
328 const uint8_t *pu8Shape, uint32_t cbShape)
329{
330 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
331
332 RTMemFree(mPointerData.pu8Shape);
333 mPointerData.pu8Shape = NULL;
334 mPointerData.cbShape = 0;
335
336 mPointerData.fVisible = fVisible;
337 mPointerData.fAlpha = fAlpha;
338 mPointerData.hotX = hotX;
339 mPointerData.hotY = hotY;
340 mPointerData.width = width;
341 mPointerData.height = height;
342 if (cbShape)
343 {
344 mPointerData.pu8Shape = (uint8_t *)RTMemDup(pu8Shape, cbShape);
345 if (mPointerData.pu8Shape)
346 {
347 mPointerData.cbShape = cbShape;
348 }
349 }
350
351 mPointerShape.setNull();
352}
353
354// IMouse properties
355/////////////////////////////////////////////////////////////////////////////
356
357/** Report the front-end's mouse handling capabilities to the VMM device and
358 * thus to the guest.
359 * @note all calls out of this object are made with no locks held! */
360HRESULT Mouse::i_updateVMMDevMouseCaps(uint32_t fCapsAdded,
361 uint32_t fCapsRemoved)
362{
363 VMMDevMouseInterface *pVMMDev = mParent->i_getVMMDevMouseInterface();
364 if (!pVMMDev)
365 return E_FAIL; /* No assertion, as the front-ends can send events
366 * at all sorts of inconvenient times. */
367 DisplayMouseInterface *pDisplay = mParent->i_getDisplayMouseInterface();
368 if (pDisplay == NULL)
369 return E_FAIL;
370 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
371 if (!pVMMDevPort)
372 return E_FAIL; /* same here */
373
374 int vrc = pVMMDevPort->pfnUpdateMouseCapabilities(pVMMDevPort, fCapsAdded,
375 fCapsRemoved);
376 if (RT_FAILURE(vrc))
377 return E_FAIL;
378 return pDisplay->i_reportHostCursorCapabilities(fCapsAdded, fCapsRemoved);
379}
380
381/**
382 * Returns whether the currently active device portfolio can accept absolute
383 * mouse events.
384 *
385 * @returns COM status code
386 * @param aAbsoluteSupported address of result variable
387 */
388HRESULT Mouse::getAbsoluteSupported(BOOL *aAbsoluteSupported)
389{
390 *aAbsoluteSupported = i_supportsAbs();
391 return S_OK;
392}
393
394/**
395 * Returns whether the currently active device portfolio can accept relative
396 * mouse events.
397 *
398 * @returns COM status code
399 * @param aRelativeSupported address of result variable
400 */
401HRESULT Mouse::getRelativeSupported(BOOL *aRelativeSupported)
402{
403 *aRelativeSupported = i_supportsRel();
404 return S_OK;
405}
406
407/**
408 * Returns whether the currently active device portfolio can accept multi-touch
409 * touchscreen events.
410 *
411 * @returns COM status code
412 * @param aTouchScreenSupported address of result variable
413 */
414HRESULT Mouse::getTouchScreenSupported(BOOL *aTouchScreenSupported)
415{
416 *aTouchScreenSupported = i_supportsTS();
417 return S_OK;
418}
419
420/**
421 * Returns whether the currently active device portfolio can accept multi-touch
422 * touchpad events.
423 *
424 * @returns COM status code
425 * @param aTouchPadSupported address of result variable
426 */
427HRESULT Mouse::getTouchPadSupported(BOOL *aTouchPadSupported)
428{
429 *aTouchPadSupported = i_supportsTP();
430 return S_OK;
431}
432
433/**
434 * Returns whether the guest can currently switch to drawing the mouse cursor
435 * itself if it is asked to by the front-end.
436 *
437 * @returns COM status code
438 * @param aNeedsHostCursor address of result variable
439 */
440HRESULT Mouse::getNeedsHostCursor(BOOL *aNeedsHostCursor)
441{
442 *aNeedsHostCursor = i_guestNeedsHostCursor();
443 return S_OK;
444}
445
446HRESULT Mouse::getPointerShape(ComPtr<IMousePointerShape> &aPointerShape)
447{
448 HRESULT hr = S_OK;
449
450 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
451
452 if (mPointerShape.isNull())
453 {
454 ComObjPtr<MousePointerShape> obj;
455 hr = obj.createObject();
456 if (SUCCEEDED(hr))
457 {
458 hr = obj->init(this, mPointerData.fVisible, mPointerData.fAlpha,
459 mPointerData.hotX, mPointerData.hotY,
460 mPointerData.width, mPointerData.height,
461 mPointerData.pu8Shape, mPointerData.cbShape);
462 }
463
464 if (SUCCEEDED(hr))
465 {
466 mPointerShape = obj;
467 }
468 }
469
470 if (SUCCEEDED(hr))
471 {
472 aPointerShape = mPointerShape;
473 }
474
475 return hr;
476}
477
478// IMouse methods
479/////////////////////////////////////////////////////////////////////////////
480
481/** Converts a bitfield containing information about mouse buttons currently
482 * held down from the format used by the front-end to the format used by PDM
483 * and the emulated pointing devices. */
484static uint32_t i_mouseButtonsToPDM(LONG buttonState)
485{
486 uint32_t fButtons = 0;
487 if (buttonState & MouseButtonState_LeftButton)
488 fButtons |= PDMIMOUSEPORT_BUTTON_LEFT;
489 if (buttonState & MouseButtonState_RightButton)
490 fButtons |= PDMIMOUSEPORT_BUTTON_RIGHT;
491 if (buttonState & MouseButtonState_MiddleButton)
492 fButtons |= PDMIMOUSEPORT_BUTTON_MIDDLE;
493 if (buttonState & MouseButtonState_XButton1)
494 fButtons |= PDMIMOUSEPORT_BUTTON_X1;
495 if (buttonState & MouseButtonState_XButton2)
496 fButtons |= PDMIMOUSEPORT_BUTTON_X2;
497 return fButtons;
498}
499
500HRESULT Mouse::getEventSource(ComPtr<IEventSource> &aEventSource)
501{
502 // no need to lock - lifetime constant
503 mEventSource.queryInterfaceTo(aEventSource.asOutParam());
504 return S_OK;
505}
506
507/**
508 * Send a relative pointer event to the relative device we deem most
509 * appropriate.
510 *
511 * @returns COM status code
512 */
513HRESULT Mouse::i_reportRelEventToMouseDev(int32_t dx, int32_t dy, int32_t dz,
514 int32_t dw, uint32_t fButtons)
515{
516 if (dx || dy || dz || dw || fButtons != mfLastButtons)
517 {
518 PPDMIMOUSEPORT pUpPort = NULL;
519 {
520 AutoReadLock aLock(this COMMA_LOCKVAL_SRC_POS);
521
522 for (unsigned i = 0; !pUpPort && i < MOUSE_MAX_DEVICES; ++i)
523 {
524 if (mpDrv[i] && (mpDrv[i]->u32DevCaps & MOUSE_DEVCAP_RELATIVE))
525 pUpPort = mpDrv[i]->pUpPort;
526 }
527 }
528 if (!pUpPort)
529 return S_OK;
530
531 int vrc = pUpPort->pfnPutEvent(pUpPort, dx, dy, dz, dw, fButtons);
532
533 if (RT_FAILURE(vrc))
534 return setErrorBoth(VBOX_E_IPRT_ERROR, vrc,
535 tr("Could not send the mouse event to the virtual mouse (%Rrc)"),
536 vrc);
537 mfLastButtons = fButtons;
538 }
539 return S_OK;
540}
541
542
543/**
544 * Send an absolute pointer event to the emulated absolute device we deem most
545 * appropriate.
546 *
547 * @returns COM status code
548 */
549HRESULT Mouse::i_reportAbsEventToMouseDev(int32_t x, int32_t y,
550 int32_t dz, int32_t dw, uint32_t fButtons)
551{
552 if ( x < VMMDEV_MOUSE_RANGE_MIN
553 || x > VMMDEV_MOUSE_RANGE_MAX)
554 return S_OK;
555 if ( y < VMMDEV_MOUSE_RANGE_MIN
556 || y > VMMDEV_MOUSE_RANGE_MAX)
557 return S_OK;
558 if ( x != mcLastX || y != mcLastY
559 || dz || dw || fButtons != mfLastButtons)
560 {
561 PPDMIMOUSEPORT pUpPort = NULL;
562 {
563 AutoReadLock aLock(this COMMA_LOCKVAL_SRC_POS);
564
565 for (unsigned i = 0; !pUpPort && i < MOUSE_MAX_DEVICES; ++i)
566 {
567 if (mpDrv[i] && (mpDrv[i]->u32DevCaps & MOUSE_DEVCAP_ABSOLUTE))
568 pUpPort = mpDrv[i]->pUpPort;
569 }
570 }
571 if (!pUpPort)
572 return S_OK;
573
574 int vrc = pUpPort->pfnPutEventAbs(pUpPort, x, y, dz,
575 dw, fButtons);
576 if (RT_FAILURE(vrc))
577 return setErrorBoth(VBOX_E_IPRT_ERROR, vrc,
578 tr("Could not send the mouse event to the virtual mouse (%Rrc)"),
579 vrc);
580 mfLastButtons = fButtons;
581
582 }
583 return S_OK;
584}
585
586HRESULT Mouse::i_reportMultiTouchEventToDevice(uint8_t cContacts,
587 const uint64_t *pau64Contacts,
588 bool fTouchScreen,
589 uint32_t u32ScanTime)
590{
591 HRESULT hrc = S_OK;
592
593 int match = fTouchScreen ? MOUSE_DEVCAP_MT_ABSOLUTE : MOUSE_DEVCAP_MT_RELATIVE;
594 PPDMIMOUSEPORT pUpPort = NULL;
595 {
596 AutoReadLock aLock(this COMMA_LOCKVAL_SRC_POS);
597
598 unsigned i;
599 for (i = 0; i < MOUSE_MAX_DEVICES; ++i)
600 {
601 if ( mpDrv[i]
602 && (mpDrv[i]->u32DevCaps & match))
603 {
604 pUpPort = mpDrv[i]->pUpPort;
605 break;
606 }
607 }
608 }
609
610 if (pUpPort)
611 {
612 int vrc = pUpPort->pfnPutEventTouchScreen(pUpPort, cContacts, pau64Contacts, u32ScanTime);
613 if (RT_FAILURE(vrc))
614 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc,
615 tr("Could not send the multi-touch event to the virtual device (%Rrc)"),
616 vrc);
617 }
618 else
619 {
620 hrc = E_UNEXPECTED;
621 }
622
623 return hrc;
624}
625
626
627/**
628 * Send an absolute position event to the VMM device.
629 * @note all calls out of this object are made with no locks held!
630 *
631 * @returns COM status code
632 */
633HRESULT Mouse::i_reportAbsEventToVMMDev(int32_t x, int32_t y)
634{
635 VMMDevMouseInterface *pVMMDev = mParent->i_getVMMDevMouseInterface();
636 ComAssertRet(pVMMDev, E_FAIL);
637 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
638 ComAssertRet(pVMMDevPort, E_FAIL);
639
640 if (x != mcLastX || y != mcLastY)
641 {
642 int vrc = pVMMDevPort->pfnSetAbsoluteMouse(pVMMDevPort,
643 x, y);
644 if (RT_FAILURE(vrc))
645 return setErrorBoth(VBOX_E_IPRT_ERROR, vrc,
646 tr("Could not send the mouse event to the virtual mouse (%Rrc)"),
647 vrc);
648 }
649 return S_OK;
650}
651
652
653/**
654 * Send an absolute pointer event to a pointing device (the VMM device if
655 * possible or whatever emulated absolute device seems best to us if not).
656 *
657 * @returns COM status code
658 */
659HRESULT Mouse::i_reportAbsEventToInputDevices(int32_t x, int32_t y, int32_t dz, int32_t dw, uint32_t fButtons,
660 bool fUsesVMMDevEvent)
661{
662 HRESULT hrc;
663 /** If we are using the VMMDev to report absolute position but without
664 * VMMDev IRQ support then we need to send a small "jiggle" to the emulated
665 * relative mouse device to alert the guest to changes. */
666 LONG cJiggle = 0;
667
668 if (i_vmmdevCanAbs())
669 {
670 /*
671 * Send the absolute mouse position to the VMM device.
672 */
673 if (x != mcLastX || y != mcLastY)
674 {
675 hrc = i_reportAbsEventToVMMDev(x, y);
676 cJiggle = !fUsesVMMDevEvent;
677 }
678 hrc = i_reportRelEventToMouseDev(cJiggle, 0, dz, dw, fButtons);
679 }
680 else
681 hrc = i_reportAbsEventToMouseDev(x, y, dz, dw, fButtons);
682
683 mcLastX = x;
684 mcLastY = y;
685 return hrc;
686}
687
688
689/**
690 * Send an absolute position event to the display device.
691 * @note all calls out of this object are made with no locks held!
692 * @param x Cursor X position in pixels relative to the first screen, where
693 * (1, 1) is the upper left corner.
694 * @param y Cursor Y position in pixels relative to the first screen, where
695 * (1, 1) is the upper left corner.
696 */
697HRESULT Mouse::i_reportAbsEventToDisplayDevice(int32_t x, int32_t y)
698{
699 DisplayMouseInterface *pDisplay = mParent->i_getDisplayMouseInterface();
700 ComAssertRet(pDisplay, E_FAIL);
701
702 if (x != mcLastX || y != mcLastY)
703 {
704 pDisplay->i_reportHostCursorPosition(x - 1, y - 1, false);
705 }
706 return S_OK;
707}
708
709
710void Mouse::i_fireMouseEvent(bool fAbsolute, LONG x, LONG y, LONG dz, LONG dw,
711 LONG fButtons)
712{
713 /* If mouse button is pressed, we generate new event, to avoid reusable events coalescing and thus
714 dropping key press events */
715 GuestMouseEventMode_T mode;
716 if (fAbsolute)
717 mode = GuestMouseEventMode_Absolute;
718 else
719 mode = GuestMouseEventMode_Relative;
720
721 if (fButtons != 0)
722 ::FireGuestMouseEvent(mEventSource, mode, x, y, dz, dw, fButtons);
723 else
724 {
725 ComPtr<IEvent> ptrEvent;
726 mMouseEvent.getEvent(ptrEvent.asOutParam());
727 ReinitGuestMouseEvent(ptrEvent, mode, x, y, dz, dw, fButtons);
728 mMouseEvent.fire(0);
729 }
730}
731
732void Mouse::i_fireMultiTouchEvent(uint8_t cContacts,
733 const LONG64 *paContacts,
734 bool fTouchScreen,
735 uint32_t u32ScanTime)
736{
737 com::SafeArray<SHORT> xPositions(cContacts);
738 com::SafeArray<SHORT> yPositions(cContacts);
739 com::SafeArray<USHORT> contactIds(cContacts);
740 com::SafeArray<USHORT> contactFlags(cContacts);
741
742 uint8_t i;
743 for (i = 0; i < cContacts; i++)
744 {
745 uint32_t u32Lo = RT_LO_U32(paContacts[i]);
746 uint32_t u32Hi = RT_HI_U32(paContacts[i]);
747 xPositions[i] = (int16_t)u32Lo;
748 yPositions[i] = (int16_t)(u32Lo >> 16);
749 contactIds[i] = RT_BYTE1(u32Hi);
750 contactFlags[i] = RT_BYTE2(u32Hi);
751 }
752
753 ::FireGuestMultiTouchEvent(mEventSource, cContacts, ComSafeArrayAsInParam(xPositions), ComSafeArrayAsInParam(yPositions),
754 ComSafeArrayAsInParam(contactIds), ComSafeArrayAsInParam(contactFlags), fTouchScreen, u32ScanTime);
755}
756
757/**
758 * Send a relative mouse event to the guest.
759 * @note the VMMDev capability change is so that the guest knows we are sending
760 * real events over the PS/2 device and not dummy events to signal the
761 * arrival of new absolute pointer data
762 *
763 * @returns COM status code
764 * @param dx X movement.
765 * @param dy Y movement.
766 * @param dz Z movement.
767 * @param dw Mouse wheel movement.
768 * @param aButtonState The mouse button state.
769 */
770HRESULT Mouse::putMouseEvent(LONG dx, LONG dy, LONG dz, LONG dw,
771 LONG aButtonState)
772{
773 LogRel3(("%s: dx=%d, dy=%d, dz=%d, dw=%d\n", __PRETTY_FUNCTION__,
774 dx, dy, dz, dw));
775
776 uint32_t fButtonsAdj = i_mouseButtonsToPDM(aButtonState);
777 /* Make sure that the guest knows that we are sending real movement
778 * events to the PS/2 device and not just dummy wake-up ones. */
779 i_updateVMMDevMouseCaps(0, VMMDEV_MOUSE_HOST_WANTS_ABSOLUTE);
780 HRESULT hrc = i_reportRelEventToMouseDev(dx, dy, dz, dw, fButtonsAdj);
781
782 i_fireMouseEvent(false, dx, dy, dz, dw, aButtonState);
783
784 return hrc;
785}
786
787/**
788 * Convert an (X, Y) value pair in screen co-ordinates (starting from 1) to a
789 * value from VMMDEV_MOUSE_RANGE_MIN to VMMDEV_MOUSE_RANGE_MAX. Sets the
790 * optional validity value to false if the pair is not on an active screen and
791 * to true otherwise.
792 * @note since guests with recent versions of X.Org use a different method
793 * to everyone else to map the valuator value to a screen pixel (they
794 * multiply by the screen dimension, do a floating point divide by
795 * the valuator maximum and round the result, while everyone else
796 * does truncating integer operations) we adjust the value we send
797 * so that it maps to the right pixel both when the result is rounded
798 * and when it is truncated.
799 *
800 * @returns COM status value
801 */
802HRESULT Mouse::i_convertDisplayRes(LONG x, LONG y, int32_t *pxAdj, int32_t *pyAdj,
803 bool *pfValid)
804{
805 AssertPtrReturn(pxAdj, E_POINTER);
806 AssertPtrReturn(pyAdj, E_POINTER);
807 AssertPtrNullReturn(pfValid, E_POINTER);
808 DisplayMouseInterface *pDisplay = mParent->i_getDisplayMouseInterface();
809 ComAssertRet(pDisplay, E_FAIL);
810 /** The amount to add to the result (multiplied by the screen width/height)
811 * to compensate for differences in guest methods for mapping back to
812 * pixels */
813 enum { ADJUST_RANGE = - 3 * VMMDEV_MOUSE_RANGE / 4 };
814
815 if (pfValid)
816 *pfValid = true;
817 if (!(mfVMMDevGuestCaps & VMMDEV_MOUSE_NEW_PROTOCOL) && !pDisplay->i_isInputMappingSet())
818 {
819 ULONG displayWidth, displayHeight;
820 ULONG ulDummy;
821 LONG lDummy;
822 /* Takes the display lock */
823 HRESULT hrc = pDisplay->i_getScreenResolution(0, &displayWidth,
824 &displayHeight, &ulDummy, &lDummy, &lDummy);
825 if (FAILED(hrc))
826 return hrc;
827
828 *pxAdj = displayWidth ? (x * VMMDEV_MOUSE_RANGE + ADJUST_RANGE)
829 / (LONG) displayWidth: 0;
830 *pyAdj = displayHeight ? (y * VMMDEV_MOUSE_RANGE + ADJUST_RANGE)
831 / (LONG) displayHeight: 0;
832 }
833 else
834 {
835 int32_t x1, y1, x2, y2;
836 /* Takes the display lock */
837 pDisplay->i_getFramebufferDimensions(&x1, &y1, &x2, &y2);
838 *pxAdj = x1 < x2 ? ((x - x1) * VMMDEV_MOUSE_RANGE + ADJUST_RANGE)
839 / (x2 - x1) : 0;
840 *pyAdj = y1 < y2 ? ((y - y1) * VMMDEV_MOUSE_RANGE + ADJUST_RANGE)
841 / (y2 - y1) : 0;
842 if ( *pxAdj < VMMDEV_MOUSE_RANGE_MIN
843 || *pxAdj > VMMDEV_MOUSE_RANGE_MAX
844 || *pyAdj < VMMDEV_MOUSE_RANGE_MIN
845 || *pyAdj > VMMDEV_MOUSE_RANGE_MAX)
846 if (pfValid)
847 *pfValid = false;
848 }
849 return S_OK;
850}
851
852
853/**
854 * Send an absolute mouse event to the VM. This requires either VirtualBox-
855 * specific drivers installed in the guest or absolute pointing device
856 * emulation.
857 * @note the VMMDev capability change is so that the guest knows we are sending
858 * dummy events over the PS/2 device to signal the arrival of new
859 * absolute pointer data, and not pointer real movement data
860 * @note all calls out of this object are made with no locks held!
861 *
862 * @returns COM status code
863 * @param x X position (pixel), starting from 1
864 * @param y Y position (pixel), starting from 1
865 * @param dz Z movement
866 * @param dw mouse wheel movement
867 * @param aButtonState The mouse button state
868 */
869HRESULT Mouse::putMouseEventAbsolute(LONG x, LONG y, LONG dz, LONG dw,
870 LONG aButtonState)
871{
872 LogRel3(("%s: x=%d, y=%d, dz=%d, dw=%d, fButtons=0x%x\n",
873 __PRETTY_FUNCTION__, x, y, dz, dw, aButtonState));
874
875 DisplayMouseInterface *pDisplay = mParent->i_getDisplayMouseInterface();
876 ComAssertRet(pDisplay, E_FAIL);
877 int32_t xAdj, yAdj;
878 uint32_t fButtonsAdj;
879 bool fValid;
880
881 /* If we are doing old-style (IRQ-less) absolute reporting to the VMM
882 * device then make sure the guest is aware of it, so that it knows to
883 * ignore relative movement on the PS/2 device. */
884 i_updateVMMDevMouseCaps(VMMDEV_MOUSE_HOST_WANTS_ABSOLUTE, 0);
885 /* Detect out-of-range. */
886 if (x == 0x7FFFFFFF && y == 0x7FFFFFFF)
887 {
888 pDisplay->i_reportHostCursorPosition(0, 0, true);
889 return S_OK;
890 }
891 /* Detect "report-only" (-1, -1). This is not ideal, as in theory the
892 * front-end could be sending negative values relative to the primary
893 * screen. */
894 if (x == -1 && y == -1)
895 return S_OK;
896 /** @todo the front end should do this conversion to avoid races */
897 /** @note Or maybe not... races are pretty inherent in everything done in
898 * this object and not really bad as far as I can see. */
899 HRESULT hrc = i_convertDisplayRes(x, y, &xAdj, &yAdj, &fValid);
900 if (FAILED(hrc)) return hrc;
901
902 fButtonsAdj = i_mouseButtonsToPDM(aButtonState);
903 if (fValid)
904 {
905 hrc = i_reportAbsEventToInputDevices(xAdj, yAdj, dz, dw, fButtonsAdj,
906 RT_BOOL(mfVMMDevGuestCaps & VMMDEV_MOUSE_NEW_PROTOCOL));
907 if (FAILED(hrc)) return hrc;
908
909 i_fireMouseEvent(true, x, y, dz, dw, aButtonState);
910 }
911 hrc = i_reportAbsEventToDisplayDevice(x, y);
912
913 return hrc;
914}
915
916/**
917 * Send a multi-touch event. This requires multi-touch pointing device emulation.
918 * @note all calls out of this object are made with no locks held!
919 *
920 * @returns COM status code.
921 * @param aCount Number of contacts.
922 * @param aContacts Information about each contact.
923 * @param aIsTouchscreen Distinguishes between touchscreen and touchpad events.
924 * @param aScanTime Timestamp.
925 */
926HRESULT Mouse::putEventMultiTouch(LONG aCount,
927 const std::vector<LONG64> &aContacts,
928 BOOL aIsTouchscreen,
929 ULONG aScanTime)
930{
931 LogRel3(("%s: aCount %d(actual %d), aScanTime %u\n",
932 __FUNCTION__, aCount, aContacts.size(), aScanTime));
933
934 HRESULT hrc = S_OK;
935
936 if ((LONG)aContacts.size() >= aCount)
937 {
938 const LONG64 *paContacts = aCount > 0? &aContacts.front(): NULL;
939
940 hrc = i_putEventMultiTouch(aCount, paContacts, aIsTouchscreen, aScanTime);
941 }
942 else
943 {
944 hrc = E_INVALIDARG;
945 }
946
947 return hrc;
948}
949
950/**
951 * Send a multi-touch event. Version for scripting languages.
952 *
953 * @returns COM status code.
954 * @param aCount Number of contacts.
955 * @param aContacts Information about each contact.
956 * @param aIsTouchscreen Distinguishes between touchscreen and touchpad events.
957 * @param aScanTime Timestamp.
958 */
959HRESULT Mouse::putEventMultiTouchString(LONG aCount,
960 const com::Utf8Str &aContacts,
961 BOOL aIsTouchscreen,
962 ULONG aScanTime)
963{
964 /** @todo implement: convert the string to LONG64 array and call putEventMultiTouch. */
965 NOREF(aCount);
966 NOREF(aContacts);
967 NOREF(aIsTouchscreen);
968 NOREF(aScanTime);
969 return E_NOTIMPL;
970}
971
972
973// private methods
974/////////////////////////////////////////////////////////////////////////////
975
976/* Used by PutEventMultiTouch and PutEventMultiTouchString. */
977HRESULT Mouse::i_putEventMultiTouch(LONG aCount,
978 const LONG64 *paContacts,
979 BOOL aIsTouchscreen,
980 ULONG aScanTime)
981{
982 if (aCount >= 256)
983 return E_INVALIDARG;
984
985 HRESULT hrc = S_OK;
986
987 /* Touch events in the touchscreen variant are currently mapped to the
988 * primary monitor, because the emulated USB touchscreen device is
989 * associated with one (normally the primary) screen in the guest.
990 * In the future this could/should be extended to multi-screen support. */
991 ULONG uScreenId = 0;
992
993 ULONG cWidth = 0;
994 ULONG cHeight = 0;
995 LONG xOrigin = 0;
996 LONG yOrigin = 0;
997
998 if (aIsTouchscreen)
999 {
1000 DisplayMouseInterface *pDisplay = mParent->i_getDisplayMouseInterface();
1001 ComAssertRet(pDisplay, E_FAIL);
1002 ULONG cBPP = 0;
1003 hrc = pDisplay->i_getScreenResolution(uScreenId, &cWidth, &cHeight, &cBPP, &xOrigin, &yOrigin);
1004 NOREF(cBPP);
1005 ComAssertComRCRetRC(hrc);
1006 }
1007
1008 uint64_t* pau64Contacts = NULL;
1009 uint8_t cContacts = 0;
1010
1011 /* Deliver 0 contacts too, touch device may use this to reset the state. */
1012 if (aCount > 0)
1013 {
1014 /* Create a copy with converted coords. */
1015 pau64Contacts = (uint64_t *)RTMemTmpAlloc(aCount * sizeof(uint64_t));
1016 if (pau64Contacts)
1017 {
1018 if (aIsTouchscreen)
1019 {
1020 int32_t x1 = xOrigin;
1021 int32_t y1 = yOrigin;
1022 int32_t x2 = x1 + cWidth;
1023 int32_t y2 = y1 + cHeight;
1024
1025 LogRel3(("%s: screen [%d] %d,%d %d,%d\n",
1026 __FUNCTION__, uScreenId, x1, y1, x2, y2));
1027
1028 LONG i;
1029 for (i = 0; i < aCount; i++)
1030 {
1031 uint32_t u32Lo = RT_LO_U32(paContacts[i]);
1032 uint32_t u32Hi = RT_HI_U32(paContacts[i]);
1033 int32_t x = (int16_t)u32Lo;
1034 int32_t y = (int16_t)(u32Lo >> 16);
1035 uint8_t contactId = RT_BYTE1(u32Hi);
1036 bool fInContact = (RT_BYTE2(u32Hi) & 0x1) != 0;
1037 bool fInRange = (RT_BYTE2(u32Hi) & 0x2) != 0;
1038
1039 LogRel3(("%s: touchscreen [%d] %d,%d id %d, inContact %d, inRange %d\n",
1040 __FUNCTION__, i, x, y, contactId, fInContact, fInRange));
1041
1042 /* x1,y1 are inclusive and x2,y2 are exclusive,
1043 * while x,y start from 1 and are inclusive.
1044 */
1045 if (x <= x1 || x > x2 || y <= y1 || y > y2)
1046 {
1047 /* Out of range. Skip the contact. */
1048 continue;
1049 }
1050
1051 int32_t xAdj = x1 < x2? ((x - 1 - x1) * VMMDEV_MOUSE_RANGE) / (x2 - x1) : 0;
1052 int32_t yAdj = y1 < y2? ((y - 1 - y1) * VMMDEV_MOUSE_RANGE) / (y2 - y1) : 0;
1053
1054 bool fValid = ( xAdj >= VMMDEV_MOUSE_RANGE_MIN
1055 && xAdj <= VMMDEV_MOUSE_RANGE_MAX
1056 && yAdj >= VMMDEV_MOUSE_RANGE_MIN
1057 && yAdj <= VMMDEV_MOUSE_RANGE_MAX);
1058
1059 if (fValid)
1060 {
1061 uint8_t fu8 = (uint8_t)( (fInContact? 0x01: 0x00)
1062 | (fInRange? 0x02: 0x00));
1063 pau64Contacts[cContacts] = RT_MAKE_U64_FROM_U16((uint16_t)xAdj,
1064 (uint16_t)yAdj,
1065 RT_MAKE_U16(contactId, fu8),
1066 0);
1067 cContacts++;
1068 }
1069 }
1070 }
1071 else
1072 {
1073 LONG i;
1074 for (i = 0; i < aCount; i++)
1075 {
1076 uint32_t u32Lo = RT_LO_U32(paContacts[i]);
1077 uint32_t u32Hi = RT_HI_U32(paContacts[i]);
1078 uint16_t x = (uint16_t)u32Lo;
1079 uint16_t y = (uint16_t)(u32Lo >> 16);
1080 uint8_t contactId = RT_BYTE1(u32Hi);
1081 bool fInContact = (RT_BYTE2(u32Hi) & 0x1) != 0;
1082
1083 LogRel3(("%s: touchpad [%d] %#04x,%#04x id %d, inContact %d\n",
1084 __FUNCTION__, i, x, y, contactId, fInContact));
1085
1086 uint8_t fu8 = (uint8_t)(fInContact? 0x01: 0x00);
1087
1088 pau64Contacts[cContacts] = RT_MAKE_U64_FROM_U16(x, y,
1089 RT_MAKE_U16(contactId, fu8),
1090 0);
1091 cContacts++;
1092 }
1093 }
1094 }
1095 else
1096 {
1097 hrc = E_OUTOFMEMORY;
1098 }
1099 }
1100
1101 if (SUCCEEDED(hrc))
1102 {
1103 hrc = i_reportMultiTouchEventToDevice(cContacts, cContacts? pau64Contacts: NULL, !!aIsTouchscreen, (uint32_t)aScanTime);
1104
1105 /* Send the original contact information. */
1106 i_fireMultiTouchEvent(cContacts, cContacts? paContacts: NULL, !!aIsTouchscreen, (uint32_t)aScanTime);
1107 }
1108
1109 RTMemTmpFree(pau64Contacts);
1110
1111 return hrc;
1112}
1113
1114
1115/** Does the guest currently rely on the host to draw the mouse cursor or
1116 * can it switch to doing it itself in software? */
1117bool Mouse::i_guestNeedsHostCursor(void)
1118{
1119 return RT_BOOL(mfVMMDevGuestCaps & VMMDEV_MOUSE_GUEST_NEEDS_HOST_CURSOR);
1120}
1121
1122
1123/**
1124 * Gets the combined capabilities of all currently enabled devices.
1125 *
1126 * @returns Combination of MOUSE_DEVCAP_XXX
1127 */
1128uint32_t Mouse::i_getDeviceCaps(void)
1129{
1130 uint32_t fCaps = 0;
1131 AutoReadLock aLock(this COMMA_LOCKVAL_SRC_POS);
1132 for (unsigned i = 0; i < MOUSE_MAX_DEVICES; ++i)
1133 if (mpDrv[i])
1134 fCaps |= mpDrv[i]->u32DevCaps;
1135 return fCaps;
1136}
1137
1138
1139/** Does the VMM device currently support absolute reporting? */
1140bool Mouse::i_vmmdevCanAbs(void)
1141{
1142 /* This requires the VMMDev cap and a relative device, which supposedly
1143 consumes these. As seen in @bugref{10285} this isn't quite as clear cut. */
1144 return (mfVMMDevGuestCaps & VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE)
1145 && (i_getDeviceCaps() & MOUSE_DEVCAP_RELATIVE);
1146}
1147
1148
1149/** Does any device currently support absolute reporting w/o help from VMMDev? */
1150bool Mouse::i_deviceCanAbs(void)
1151{
1152 return RT_BOOL(i_getDeviceCaps() & MOUSE_DEVCAP_ABSOLUTE);
1153}
1154
1155
1156/** Can we currently send relative events to the guest? */
1157bool Mouse::i_supportsRel(void)
1158{
1159 return RT_BOOL(i_getDeviceCaps() & MOUSE_DEVCAP_RELATIVE);
1160}
1161
1162
1163/** Can we currently send absolute events to the guest (including via VMMDev)? */
1164bool Mouse::i_supportsAbs(uint32_t fCaps) const
1165{
1166 return (fCaps & MOUSE_DEVCAP_ABSOLUTE)
1167 || /* inlined i_vmmdevCanAbs() to avoid unnecessary i_getDeviceCaps call: */
1168 ( (mfVMMDevGuestCaps & VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE)
1169 && (fCaps & MOUSE_DEVCAP_RELATIVE));
1170}
1171
1172
1173/** Can we currently send absolute events to the guest? */
1174bool Mouse::i_supportsAbs(void)
1175{
1176 return Mouse::i_supportsAbs(i_getDeviceCaps());
1177}
1178
1179
1180/** Can we currently send multi-touch events (touchscreen variant) to the guest? */
1181bool Mouse::i_supportsTS(void)
1182{
1183 return RT_BOOL(i_getDeviceCaps() & MOUSE_DEVCAP_MT_ABSOLUTE);
1184}
1185
1186
1187/** Can we currently send multi-touch events (touchpad variant) to the guest? */
1188bool Mouse::i_supportsTP(void)
1189{
1190 return RT_BOOL(i_getDeviceCaps() & MOUSE_DEVCAP_MT_RELATIVE);
1191}
1192
1193
1194/** Check what sort of reporting can be done using the devices currently
1195 * enabled (including the VMM device) and notify the guest and the front-end.
1196 */
1197void Mouse::i_sendMouseCapsNotifications(void)
1198{
1199 bool fRelDev, fTSDev, fTPDev, fCanAbs, fNeedsHostCursor;
1200 {
1201 AutoReadLock aLock(this COMMA_LOCKVAL_SRC_POS);
1202
1203 uint32_t const fCaps = i_getDeviceCaps();
1204 fRelDev = RT_BOOL(fCaps & MOUSE_DEVCAP_RELATIVE);
1205 fTSDev = RT_BOOL(fCaps & MOUSE_DEVCAP_MT_ABSOLUTE);
1206 fTPDev = RT_BOOL(fCaps & MOUSE_DEVCAP_MT_RELATIVE);
1207 fCanAbs = i_supportsAbs(fCaps);
1208 fNeedsHostCursor = i_guestNeedsHostCursor();
1209 }
1210 mParent->i_onMouseCapabilityChange(fCanAbs, fRelDev, fTSDev, fTPDev, fNeedsHostCursor);
1211}
1212
1213
1214/**
1215 * @interface_method_impl{PDMIMOUSECONNECTOR,pfnReportModes}
1216 */
1217DECLCALLBACK(void) Mouse::i_mouseReportModes(PPDMIMOUSECONNECTOR pInterface, bool fRelative,
1218 bool fAbsolute, bool fMTAbsolute, bool fMTRelative)
1219{
1220 PDRVMAINMOUSE pDrv = RT_FROM_MEMBER(pInterface, DRVMAINMOUSE, IConnector);
1221 if (fRelative)
1222 pDrv->u32DevCaps |= MOUSE_DEVCAP_RELATIVE;
1223 else
1224 pDrv->u32DevCaps &= ~MOUSE_DEVCAP_RELATIVE;
1225 if (fAbsolute)
1226 pDrv->u32DevCaps |= MOUSE_DEVCAP_ABSOLUTE;
1227 else
1228 pDrv->u32DevCaps &= ~MOUSE_DEVCAP_ABSOLUTE;
1229 if (fMTAbsolute)
1230 pDrv->u32DevCaps |= MOUSE_DEVCAP_MT_ABSOLUTE;
1231 else
1232 pDrv->u32DevCaps &= ~MOUSE_DEVCAP_MT_ABSOLUTE;
1233 if (fMTRelative)
1234 pDrv->u32DevCaps |= MOUSE_DEVCAP_MT_RELATIVE;
1235 else
1236 pDrv->u32DevCaps &= ~MOUSE_DEVCAP_MT_RELATIVE;
1237
1238 pDrv->pMouse->i_sendMouseCapsNotifications();
1239}
1240
1241
1242/**
1243 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
1244 */
1245DECLCALLBACK(void *) Mouse::i_drvQueryInterface(PPDMIBASE pInterface, const char *pszIID)
1246{
1247 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
1248 PDRVMAINMOUSE pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
1249
1250 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
1251 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUSECONNECTOR, &pDrv->IConnector);
1252 return NULL;
1253}
1254
1255
1256/**
1257 * Destruct a mouse driver instance.
1258 *
1259 * @returns VBox status code.
1260 * @param pDrvIns The driver instance data.
1261 */
1262DECLCALLBACK(void) Mouse::i_drvDestruct(PPDMDRVINS pDrvIns)
1263{
1264 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
1265 PDRVMAINMOUSE pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
1266 LogFlow(("Mouse::drvDestruct: iInstance=%d\n", pDrvIns->iInstance));
1267
1268 if (pThis->pMouse)
1269 {
1270 AutoWriteLock mouseLock(pThis->pMouse COMMA_LOCKVAL_SRC_POS);
1271 for (unsigned cDev = 0; cDev < MOUSE_MAX_DEVICES; ++cDev)
1272 if (pThis->pMouse->mpDrv[cDev] == pThis)
1273 {
1274 pThis->pMouse->mpDrv[cDev] = NULL;
1275 break;
1276 }
1277 }
1278}
1279
1280
1281/**
1282 * Construct a mouse driver instance.
1283 *
1284 * @copydoc FNPDMDRVCONSTRUCT
1285 */
1286DECLCALLBACK(int) Mouse::i_drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
1287{
1288 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
1289 RT_NOREF(fFlags, pCfg);
1290 PDRVMAINMOUSE pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
1291 LogFlow(("drvMainMouse_Construct: iInstance=%d\n", pDrvIns->iInstance));
1292
1293 /*
1294 * Validate configuration.
1295 */
1296 PDMDRV_VALIDATE_CONFIG_RETURN(pDrvIns, "", "");
1297 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
1298 ("Configuration error: Not possible to attach anything to this driver!\n"),
1299 VERR_PDM_DRVINS_NO_ATTACH);
1300
1301 /*
1302 * IBase.
1303 */
1304 pDrvIns->IBase.pfnQueryInterface = Mouse::i_drvQueryInterface;
1305
1306 pThis->IConnector.pfnReportModes = Mouse::i_mouseReportModes;
1307
1308 /*
1309 * Get the IMousePort interface of the above driver/device.
1310 */
1311 pThis->pUpPort = (PPDMIMOUSEPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMIMOUSEPORT_IID);
1312 if (!pThis->pUpPort)
1313 {
1314 AssertMsgFailed(("Configuration error: No mouse port interface above!\n"));
1315 return VERR_PDM_MISSING_INTERFACE_ABOVE;
1316 }
1317
1318 /*
1319 * Get the Mouse object pointer and update the mpDrv member.
1320 */
1321 com::Guid uuid(COM_IIDOF(IMouse));
1322 IMouse *pIMouse = (IMouse *)PDMDrvHlpQueryGenericUserObject(pDrvIns, uuid.raw());
1323 if (!pIMouse)
1324 {
1325 AssertMsgFailed(("Configuration error: No/bad Mouse object!\n"));
1326 return VERR_NOT_FOUND;
1327 }
1328 pThis->pMouse = static_cast<Mouse *>(pIMouse);
1329
1330 unsigned cDev;
1331 {
1332 AutoWriteLock mouseLock(pThis->pMouse COMMA_LOCKVAL_SRC_POS);
1333
1334 for (cDev = 0; cDev < MOUSE_MAX_DEVICES; ++cDev)
1335 if (!pThis->pMouse->mpDrv[cDev])
1336 {
1337 pThis->pMouse->mpDrv[cDev] = pThis;
1338 break;
1339 }
1340 }
1341 if (cDev == MOUSE_MAX_DEVICES)
1342 return VERR_NO_MORE_HANDLES;
1343
1344 return VINF_SUCCESS;
1345}
1346
1347
1348/**
1349 * Main mouse driver registration record.
1350 */
1351const PDMDRVREG Mouse::DrvReg =
1352{
1353 /* u32Version */
1354 PDM_DRVREG_VERSION,
1355 /* szName */
1356 "MainMouse",
1357 /* szRCMod */
1358 "",
1359 /* szR0Mod */
1360 "",
1361 /* pszDescription */
1362 "Main mouse driver (Main as in the API).",
1363 /* fFlags */
1364 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1365 /* fClass. */
1366 PDM_DRVREG_CLASS_MOUSE,
1367 /* cMaxInstances */
1368 ~0U,
1369 /* cbInstance */
1370 sizeof(DRVMAINMOUSE),
1371 /* pfnConstruct */
1372 Mouse::i_drvConstruct,
1373 /* pfnDestruct */
1374 Mouse::i_drvDestruct,
1375 /* pfnRelocate */
1376 NULL,
1377 /* pfnIOCtl */
1378 NULL,
1379 /* pfnPowerOn */
1380 NULL,
1381 /* pfnReset */
1382 NULL,
1383 /* pfnSuspend */
1384 NULL,
1385 /* pfnResume */
1386 NULL,
1387 /* pfnAttach */
1388 NULL,
1389 /* pfnDetach */
1390 NULL,
1391 /* pfnPowerOff */
1392 NULL,
1393 /* pfnSoftReset */
1394 NULL,
1395 /* u32EndVersion */
1396 PDM_DRVREG_VERSION
1397};
1398/* 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