VirtualBox

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

Last change on this file since 100842 was 99739, checked in by vboxsync, 20 months ago

*: doxygen corrections (mostly about removing @returns from functions returning void).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 42.9 KB
Line 
1/* $Id: MouseImpl.cpp 99739 2023-05-11 01:01:08Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2006-2023 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, int32_t dz, int32_t dw, uint32_t fButtons)
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 || dz || dw || fButtons != mfLastButtons)
641 {
642 int vrc = pVMMDevPort->pfnSetAbsoluteMouse(pVMMDevPort, x, y, dz, dw, fButtons);
643 if (RT_FAILURE(vrc))
644 return setErrorBoth(VBOX_E_IPRT_ERROR, vrc,
645 tr("Could not send the mouse event to the virtual mouse (%Rrc)"),
646 vrc);
647 }
648 return S_OK;
649}
650
651
652/**
653 * Send an absolute pointer event to a pointing device (the VMM device if
654 * possible or whatever emulated absolute device seems best to us if not).
655 *
656 * @returns COM status code
657 */
658HRESULT Mouse::i_reportAbsEventToInputDevices(int32_t x, int32_t y, int32_t dz, int32_t dw, uint32_t fButtons,
659 bool fUsesVMMDevEvent)
660{
661 HRESULT hrc = S_OK;
662 /** If we are using the VMMDev to report absolute position but without
663 * VMMDev IRQ support then we need to send a small "jiggle" to the emulated
664 * relative mouse device to alert the guest to changes. */
665 LONG cJiggle = 0;
666
667 if (i_vmmdevCanAbs())
668 {
669 /*
670 * Send the absolute mouse position to the VMM device.
671 */
672 if (x != mcLastX || y != mcLastY || dz || dw || fButtons != mfLastButtons)
673 {
674 hrc = i_reportAbsEventToVMMDev(x, y, dz, dw, fButtons);
675 cJiggle = !fUsesVMMDevEvent;
676 }
677
678 /* If guest cannot yet read full mouse state from DevVMM (i.e.,
679 * only 'x' and 'y' coordinates will be read) we need to pass buttons
680 * state as well as horizontal and vertical wheel movement over ever-present PS/2
681 * emulated mouse device. */
682 if (!(mfVMMDevGuestCaps & VMMDEV_MOUSE_GUEST_USES_FULL_STATE_PROTOCOL))
683 hrc = i_reportRelEventToMouseDev(cJiggle, 0, dz, dw, fButtons);
684 }
685 else
686 hrc = i_reportAbsEventToMouseDev(x, y, dz, dw, fButtons);
687
688 mcLastX = x;
689 mcLastY = y;
690 mfLastButtons = fButtons;
691 return hrc;
692}
693
694
695/**
696 * Send an absolute position event to the display device.
697 * @note all calls out of this object are made with no locks held!
698 * @param x Cursor X position in pixels relative to the first screen, where
699 * (1, 1) is the upper left corner.
700 * @param y Cursor Y position in pixels relative to the first screen, where
701 * (1, 1) is the upper left corner.
702 */
703HRESULT Mouse::i_reportAbsEventToDisplayDevice(int32_t x, int32_t y)
704{
705 DisplayMouseInterface *pDisplay = mParent->i_getDisplayMouseInterface();
706 ComAssertRet(pDisplay, E_FAIL);
707
708 if (x != mcLastX || y != mcLastY)
709 {
710 pDisplay->i_reportHostCursorPosition(x - 1, y - 1, false);
711 }
712 return S_OK;
713}
714
715
716void Mouse::i_fireMouseEvent(bool fAbsolute, LONG x, LONG y, LONG dz, LONG dw,
717 LONG fButtons)
718{
719 /* If mouse button is pressed, we generate new event, to avoid reusable events coalescing and thus
720 dropping key press events */
721 GuestMouseEventMode_T mode;
722 if (fAbsolute)
723 mode = GuestMouseEventMode_Absolute;
724 else
725 mode = GuestMouseEventMode_Relative;
726
727 if (fButtons != 0)
728 ::FireGuestMouseEvent(mEventSource, mode, x, y, dz, dw, fButtons);
729 else
730 {
731 ComPtr<IEvent> ptrEvent;
732 mMouseEvent.getEvent(ptrEvent.asOutParam());
733 ReinitGuestMouseEvent(ptrEvent, mode, x, y, dz, dw, fButtons);
734 mMouseEvent.fire(0);
735 }
736}
737
738void Mouse::i_fireMultiTouchEvent(uint8_t cContacts,
739 const LONG64 *paContacts,
740 bool fTouchScreen,
741 uint32_t u32ScanTime)
742{
743 com::SafeArray<SHORT> xPositions(cContacts);
744 com::SafeArray<SHORT> yPositions(cContacts);
745 com::SafeArray<USHORT> contactIds(cContacts);
746 com::SafeArray<USHORT> contactFlags(cContacts);
747
748 uint8_t i;
749 for (i = 0; i < cContacts; i++)
750 {
751 uint32_t u32Lo = RT_LO_U32(paContacts[i]);
752 uint32_t u32Hi = RT_HI_U32(paContacts[i]);
753 xPositions[i] = (int16_t)u32Lo;
754 yPositions[i] = (int16_t)(u32Lo >> 16);
755 contactIds[i] = RT_BYTE1(u32Hi);
756 contactFlags[i] = RT_BYTE2(u32Hi);
757 }
758
759 ::FireGuestMultiTouchEvent(mEventSource, cContacts, ComSafeArrayAsInParam(xPositions), ComSafeArrayAsInParam(yPositions),
760 ComSafeArrayAsInParam(contactIds), ComSafeArrayAsInParam(contactFlags), fTouchScreen, u32ScanTime);
761}
762
763/**
764 * Send a relative mouse event to the guest.
765 * @note the VMMDev capability change is so that the guest knows we are sending
766 * real events over the PS/2 device and not dummy events to signal the
767 * arrival of new absolute pointer data
768 *
769 * @returns COM status code
770 * @param dx X movement.
771 * @param dy Y movement.
772 * @param dz Z movement.
773 * @param dw Mouse wheel movement.
774 * @param aButtonState The mouse button state.
775 */
776HRESULT Mouse::putMouseEvent(LONG dx, LONG dy, LONG dz, LONG dw,
777 LONG aButtonState)
778{
779 LogRel3(("%s: dx=%d, dy=%d, dz=%d, dw=%d\n", __PRETTY_FUNCTION__,
780 dx, dy, dz, dw));
781
782 uint32_t fButtonsAdj = i_mouseButtonsToPDM(aButtonState);
783 /* Make sure that the guest knows that we are sending real movement
784 * events to the PS/2 device and not just dummy wake-up ones. */
785 i_updateVMMDevMouseCaps(0, VMMDEV_MOUSE_HOST_WANTS_ABSOLUTE);
786 HRESULT hrc = i_reportRelEventToMouseDev(dx, dy, dz, dw, fButtonsAdj);
787
788 i_fireMouseEvent(false, dx, dy, dz, dw, aButtonState);
789
790 return hrc;
791}
792
793/**
794 * Convert an (X, Y) value pair in screen co-ordinates (starting from 1) to a
795 * value from VMMDEV_MOUSE_RANGE_MIN to VMMDEV_MOUSE_RANGE_MAX. Sets the
796 * optional validity value to false if the pair is not on an active screen and
797 * to true otherwise.
798 * @note since guests with recent versions of X.Org use a different method
799 * to everyone else to map the valuator value to a screen pixel (they
800 * multiply by the screen dimension, do a floating point divide by
801 * the valuator maximum and round the result, while everyone else
802 * does truncating integer operations) we adjust the value we send
803 * so that it maps to the right pixel both when the result is rounded
804 * and when it is truncated.
805 *
806 * @returns COM status value
807 */
808HRESULT Mouse::i_convertDisplayRes(LONG x, LONG y, int32_t *pxAdj, int32_t *pyAdj,
809 bool *pfValid)
810{
811 AssertPtrReturn(pxAdj, E_POINTER);
812 AssertPtrReturn(pyAdj, E_POINTER);
813 AssertPtrNullReturn(pfValid, E_POINTER);
814 DisplayMouseInterface *pDisplay = mParent->i_getDisplayMouseInterface();
815 ComAssertRet(pDisplay, E_FAIL);
816 /** The amount to add to the result (multiplied by the screen width/height)
817 * to compensate for differences in guest methods for mapping back to
818 * pixels */
819 enum { ADJUST_RANGE = - 3 * VMMDEV_MOUSE_RANGE / 4 };
820
821 if (pfValid)
822 *pfValid = true;
823 if (!(mfVMMDevGuestCaps & VMMDEV_MOUSE_NEW_PROTOCOL) && !pDisplay->i_isInputMappingSet())
824 {
825 ULONG displayWidth, displayHeight;
826 ULONG ulDummy;
827 LONG lDummy;
828 /* Takes the display lock */
829 HRESULT hrc = pDisplay->i_getScreenResolution(0, &displayWidth,
830 &displayHeight, &ulDummy, &lDummy, &lDummy);
831 if (FAILED(hrc))
832 return hrc;
833
834 *pxAdj = displayWidth ? (x * VMMDEV_MOUSE_RANGE + ADJUST_RANGE)
835 / (LONG) displayWidth: 0;
836 *pyAdj = displayHeight ? (y * VMMDEV_MOUSE_RANGE + ADJUST_RANGE)
837 / (LONG) displayHeight: 0;
838 }
839 else
840 {
841 int32_t x1, y1, x2, y2;
842 /* Takes the display lock */
843 pDisplay->i_getFramebufferDimensions(&x1, &y1, &x2, &y2);
844 *pxAdj = x1 < x2 ? ((x - x1) * VMMDEV_MOUSE_RANGE + ADJUST_RANGE)
845 / (x2 - x1) : 0;
846 *pyAdj = y1 < y2 ? ((y - y1) * VMMDEV_MOUSE_RANGE + ADJUST_RANGE)
847 / (y2 - y1) : 0;
848 if ( *pxAdj < VMMDEV_MOUSE_RANGE_MIN
849 || *pxAdj > VMMDEV_MOUSE_RANGE_MAX
850 || *pyAdj < VMMDEV_MOUSE_RANGE_MIN
851 || *pyAdj > VMMDEV_MOUSE_RANGE_MAX)
852 if (pfValid)
853 *pfValid = false;
854 }
855 return S_OK;
856}
857
858
859/**
860 * Send an absolute mouse event to the VM. This requires either VirtualBox-
861 * specific drivers installed in the guest or absolute pointing device
862 * emulation.
863 * @note the VMMDev capability change is so that the guest knows we are sending
864 * dummy events over the PS/2 device to signal the arrival of new
865 * absolute pointer data, and not pointer real movement data
866 * @note all calls out of this object are made with no locks held!
867 *
868 * @returns COM status code
869 * @param x X position (pixel), starting from 1
870 * @param y Y position (pixel), starting from 1
871 * @param dz Z movement
872 * @param dw mouse wheel movement
873 * @param aButtonState The mouse button state
874 */
875HRESULT Mouse::putMouseEventAbsolute(LONG x, LONG y, LONG dz, LONG dw,
876 LONG aButtonState)
877{
878 LogRel3(("%s: x=%d, y=%d, dz=%d, dw=%d, fButtons=0x%x\n",
879 __PRETTY_FUNCTION__, x, y, dz, dw, aButtonState));
880
881 DisplayMouseInterface *pDisplay = mParent->i_getDisplayMouseInterface();
882 ComAssertRet(pDisplay, E_FAIL);
883 int32_t xAdj, yAdj;
884 uint32_t fButtonsAdj;
885 bool fValid;
886
887 /* If we are doing old-style (IRQ-less) absolute reporting to the VMM
888 * device then make sure the guest is aware of it, so that it knows to
889 * ignore relative movement on the PS/2 device. */
890 i_updateVMMDevMouseCaps(VMMDEV_MOUSE_HOST_WANTS_ABSOLUTE, 0);
891 /* Detect out-of-range. */
892 if (x == 0x7FFFFFFF && y == 0x7FFFFFFF)
893 {
894 pDisplay->i_reportHostCursorPosition(0, 0, true);
895 return S_OK;
896 }
897 /* Detect "report-only" (-1, -1). This is not ideal, as in theory the
898 * front-end could be sending negative values relative to the primary
899 * screen. */
900 if (x == -1 && y == -1)
901 return S_OK;
902 /** @todo the front end should do this conversion to avoid races */
903 /** @note Or maybe not... races are pretty inherent in everything done in
904 * this object and not really bad as far as I can see. */
905 HRESULT hrc = i_convertDisplayRes(x, y, &xAdj, &yAdj, &fValid);
906 if (FAILED(hrc)) return hrc;
907
908 fButtonsAdj = i_mouseButtonsToPDM(aButtonState);
909 if (fValid)
910 {
911 hrc = i_reportAbsEventToInputDevices(xAdj, yAdj, dz, dw, fButtonsAdj,
912 RT_BOOL(mfVMMDevGuestCaps & VMMDEV_MOUSE_NEW_PROTOCOL));
913 if (FAILED(hrc)) return hrc;
914
915 i_fireMouseEvent(true, x, y, dz, dw, aButtonState);
916 }
917 hrc = i_reportAbsEventToDisplayDevice(x, y);
918
919 return hrc;
920}
921
922/**
923 * Send a multi-touch event. This requires multi-touch pointing device emulation.
924 * @note all calls out of this object are made with no locks held!
925 *
926 * @returns COM status code.
927 * @param aCount Number of contacts.
928 * @param aContacts Information about each contact.
929 * @param aIsTouchscreen Distinguishes between touchscreen and touchpad events.
930 * @param aScanTime Timestamp.
931 */
932HRESULT Mouse::putEventMultiTouch(LONG aCount,
933 const std::vector<LONG64> &aContacts,
934 BOOL aIsTouchscreen,
935 ULONG aScanTime)
936{
937 LogRel3(("%s: aCount %d(actual %d), aScanTime %u\n",
938 __FUNCTION__, aCount, aContacts.size(), aScanTime));
939
940 HRESULT hrc = S_OK;
941
942 if ((LONG)aContacts.size() >= aCount)
943 {
944 const LONG64 *paContacts = aCount > 0? &aContacts.front(): NULL;
945
946 hrc = i_putEventMultiTouch(aCount, paContacts, aIsTouchscreen, aScanTime);
947 }
948 else
949 {
950 hrc = E_INVALIDARG;
951 }
952
953 return hrc;
954}
955
956/**
957 * Send a multi-touch event. Version for scripting languages.
958 *
959 * @returns COM status code.
960 * @param aCount Number of contacts.
961 * @param aContacts Information about each contact.
962 * @param aIsTouchscreen Distinguishes between touchscreen and touchpad events.
963 * @param aScanTime Timestamp.
964 */
965HRESULT Mouse::putEventMultiTouchString(LONG aCount,
966 const com::Utf8Str &aContacts,
967 BOOL aIsTouchscreen,
968 ULONG aScanTime)
969{
970 /** @todo implement: convert the string to LONG64 array and call putEventMultiTouch. */
971 NOREF(aCount);
972 NOREF(aContacts);
973 NOREF(aIsTouchscreen);
974 NOREF(aScanTime);
975 return E_NOTIMPL;
976}
977
978
979// private methods
980/////////////////////////////////////////////////////////////////////////////
981
982/* Used by PutEventMultiTouch and PutEventMultiTouchString. */
983HRESULT Mouse::i_putEventMultiTouch(LONG aCount,
984 const LONG64 *paContacts,
985 BOOL aIsTouchscreen,
986 ULONG aScanTime)
987{
988 if (aCount >= 256)
989 return E_INVALIDARG;
990
991 HRESULT hrc = S_OK;
992
993 /* Touch events in the touchscreen variant are currently mapped to the
994 * primary monitor, because the emulated USB touchscreen device is
995 * associated with one (normally the primary) screen in the guest.
996 * In the future this could/should be extended to multi-screen support. */
997 ULONG uScreenId = 0;
998
999 ULONG cWidth = 0;
1000 ULONG cHeight = 0;
1001 LONG xOrigin = 0;
1002 LONG yOrigin = 0;
1003
1004 if (aIsTouchscreen)
1005 {
1006 DisplayMouseInterface *pDisplay = mParent->i_getDisplayMouseInterface();
1007 ComAssertRet(pDisplay, E_FAIL);
1008 ULONG cBPP = 0;
1009 hrc = pDisplay->i_getScreenResolution(uScreenId, &cWidth, &cHeight, &cBPP, &xOrigin, &yOrigin);
1010 NOREF(cBPP);
1011 ComAssertComRCRetRC(hrc);
1012 }
1013
1014 uint64_t* pau64Contacts = NULL;
1015 uint8_t cContacts = 0;
1016
1017 /* Deliver 0 contacts too, touch device may use this to reset the state. */
1018 if (aCount > 0)
1019 {
1020 /* Create a copy with converted coords. */
1021 pau64Contacts = (uint64_t *)RTMemTmpAlloc(aCount * sizeof(uint64_t));
1022 if (pau64Contacts)
1023 {
1024 if (aIsTouchscreen)
1025 {
1026 int32_t x1 = xOrigin;
1027 int32_t y1 = yOrigin;
1028 int32_t x2 = x1 + cWidth;
1029 int32_t y2 = y1 + cHeight;
1030
1031 LogRel3(("%s: screen [%d] %d,%d %d,%d\n",
1032 __FUNCTION__, uScreenId, x1, y1, x2, y2));
1033
1034 LONG i;
1035 for (i = 0; i < aCount; i++)
1036 {
1037 uint32_t u32Lo = RT_LO_U32(paContacts[i]);
1038 uint32_t u32Hi = RT_HI_U32(paContacts[i]);
1039 int32_t x = (int16_t)u32Lo;
1040 int32_t y = (int16_t)(u32Lo >> 16);
1041 uint8_t contactId = RT_BYTE1(u32Hi);
1042 bool fInContact = (RT_BYTE2(u32Hi) & 0x1) != 0;
1043 bool fInRange = (RT_BYTE2(u32Hi) & 0x2) != 0;
1044
1045 LogRel3(("%s: touchscreen [%d] %d,%d id %d, inContact %d, inRange %d\n",
1046 __FUNCTION__, i, x, y, contactId, fInContact, fInRange));
1047
1048 /* x1,y1 are inclusive and x2,y2 are exclusive,
1049 * while x,y start from 1 and are inclusive.
1050 */
1051 if (x <= x1 || x > x2 || y <= y1 || y > y2)
1052 {
1053 /* Out of range. Skip the contact. */
1054 continue;
1055 }
1056
1057 int32_t xAdj = x1 < x2? ((x - 1 - x1) * VMMDEV_MOUSE_RANGE) / (x2 - x1) : 0;
1058 int32_t yAdj = y1 < y2? ((y - 1 - y1) * VMMDEV_MOUSE_RANGE) / (y2 - y1) : 0;
1059
1060 bool fValid = ( xAdj >= VMMDEV_MOUSE_RANGE_MIN
1061 && xAdj <= VMMDEV_MOUSE_RANGE_MAX
1062 && yAdj >= VMMDEV_MOUSE_RANGE_MIN
1063 && yAdj <= VMMDEV_MOUSE_RANGE_MAX);
1064
1065 if (fValid)
1066 {
1067 uint8_t fu8 = (uint8_t)( (fInContact? 0x01: 0x00)
1068 | (fInRange? 0x02: 0x00));
1069 pau64Contacts[cContacts] = RT_MAKE_U64_FROM_U16((uint16_t)xAdj,
1070 (uint16_t)yAdj,
1071 RT_MAKE_U16(contactId, fu8),
1072 0);
1073 cContacts++;
1074 }
1075 }
1076 }
1077 else
1078 {
1079 LONG i;
1080 for (i = 0; i < aCount; i++)
1081 {
1082 uint32_t u32Lo = RT_LO_U32(paContacts[i]);
1083 uint32_t u32Hi = RT_HI_U32(paContacts[i]);
1084 uint16_t x = (uint16_t)u32Lo;
1085 uint16_t y = (uint16_t)(u32Lo >> 16);
1086 uint8_t contactId = RT_BYTE1(u32Hi);
1087 bool fInContact = (RT_BYTE2(u32Hi) & 0x1) != 0;
1088
1089 LogRel3(("%s: touchpad [%d] %#04x,%#04x id %d, inContact %d\n",
1090 __FUNCTION__, i, x, y, contactId, fInContact));
1091
1092 uint8_t fu8 = (uint8_t)(fInContact? 0x01: 0x00);
1093
1094 pau64Contacts[cContacts] = RT_MAKE_U64_FROM_U16(x, y,
1095 RT_MAKE_U16(contactId, fu8),
1096 0);
1097 cContacts++;
1098 }
1099 }
1100 }
1101 else
1102 {
1103 hrc = E_OUTOFMEMORY;
1104 }
1105 }
1106
1107 if (SUCCEEDED(hrc))
1108 {
1109 hrc = i_reportMultiTouchEventToDevice(cContacts, cContacts? pau64Contacts: NULL, !!aIsTouchscreen, (uint32_t)aScanTime);
1110
1111 /* Send the original contact information. */
1112 i_fireMultiTouchEvent(cContacts, cContacts? paContacts: NULL, !!aIsTouchscreen, (uint32_t)aScanTime);
1113 }
1114
1115 RTMemTmpFree(pau64Contacts);
1116
1117 return hrc;
1118}
1119
1120
1121/** Does the guest currently rely on the host to draw the mouse cursor or
1122 * can it switch to doing it itself in software? */
1123bool Mouse::i_guestNeedsHostCursor(void)
1124{
1125 return RT_BOOL(mfVMMDevGuestCaps & VMMDEV_MOUSE_GUEST_NEEDS_HOST_CURSOR);
1126}
1127
1128
1129/**
1130 * Gets the combined capabilities of all currently enabled devices.
1131 *
1132 * @returns Combination of MOUSE_DEVCAP_XXX
1133 */
1134uint32_t Mouse::i_getDeviceCaps(void)
1135{
1136 uint32_t fCaps = 0;
1137 AutoReadLock aLock(this COMMA_LOCKVAL_SRC_POS);
1138 for (unsigned i = 0; i < MOUSE_MAX_DEVICES; ++i)
1139 if (mpDrv[i])
1140 fCaps |= mpDrv[i]->u32DevCaps;
1141 return fCaps;
1142}
1143
1144
1145/** Does the VMM device currently support absolute reporting? */
1146bool Mouse::i_vmmdevCanAbs(void)
1147{
1148 /* This requires the VMMDev cap and a relative device, which supposedly
1149 consumes these. As seen in @bugref{10285} this isn't quite as clear cut. */
1150 return (mfVMMDevGuestCaps & VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE)
1151 && (i_getDeviceCaps() & MOUSE_DEVCAP_RELATIVE);
1152}
1153
1154
1155/** Does any device currently support absolute reporting w/o help from VMMDev? */
1156bool Mouse::i_deviceCanAbs(void)
1157{
1158 return RT_BOOL(i_getDeviceCaps() & MOUSE_DEVCAP_ABSOLUTE);
1159}
1160
1161
1162/** Can we currently send relative events to the guest? */
1163bool Mouse::i_supportsRel(void)
1164{
1165 return RT_BOOL(i_getDeviceCaps() & MOUSE_DEVCAP_RELATIVE);
1166}
1167
1168
1169/** Can we currently send absolute events to the guest (including via VMMDev)? */
1170bool Mouse::i_supportsAbs(uint32_t fCaps) const
1171{
1172 return (fCaps & MOUSE_DEVCAP_ABSOLUTE)
1173 || /* inlined i_vmmdevCanAbs() to avoid unnecessary i_getDeviceCaps call: */
1174 ( (mfVMMDevGuestCaps & VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE)
1175 && (fCaps & MOUSE_DEVCAP_RELATIVE));
1176}
1177
1178
1179/** Can we currently send absolute events to the guest? */
1180bool Mouse::i_supportsAbs(void)
1181{
1182 return Mouse::i_supportsAbs(i_getDeviceCaps());
1183}
1184
1185
1186/** Can we currently send multi-touch events (touchscreen variant) to the guest? */
1187bool Mouse::i_supportsTS(void)
1188{
1189 return RT_BOOL(i_getDeviceCaps() & MOUSE_DEVCAP_MT_ABSOLUTE);
1190}
1191
1192
1193/** Can we currently send multi-touch events (touchpad variant) to the guest? */
1194bool Mouse::i_supportsTP(void)
1195{
1196 return RT_BOOL(i_getDeviceCaps() & MOUSE_DEVCAP_MT_RELATIVE);
1197}
1198
1199
1200/** Check what sort of reporting can be done using the devices currently
1201 * enabled (including the VMM device) and notify the guest and the front-end.
1202 */
1203void Mouse::i_sendMouseCapsNotifications(void)
1204{
1205 bool fRelDev, fTSDev, fTPDev, fCanAbs, fNeedsHostCursor;
1206 {
1207 AutoReadLock aLock(this COMMA_LOCKVAL_SRC_POS);
1208
1209 uint32_t const fCaps = i_getDeviceCaps();
1210 fRelDev = RT_BOOL(fCaps & MOUSE_DEVCAP_RELATIVE);
1211 fTSDev = RT_BOOL(fCaps & MOUSE_DEVCAP_MT_ABSOLUTE);
1212 fTPDev = RT_BOOL(fCaps & MOUSE_DEVCAP_MT_RELATIVE);
1213 fCanAbs = i_supportsAbs(fCaps);
1214 fNeedsHostCursor = i_guestNeedsHostCursor();
1215 }
1216 mParent->i_onMouseCapabilityChange(fCanAbs, fRelDev, fTSDev, fTPDev, fNeedsHostCursor);
1217}
1218
1219
1220/**
1221 * @interface_method_impl{PDMIMOUSECONNECTOR,pfnReportModes}
1222 */
1223DECLCALLBACK(void) Mouse::i_mouseReportModes(PPDMIMOUSECONNECTOR pInterface, bool fRelative,
1224 bool fAbsolute, bool fMTAbsolute, bool fMTRelative)
1225{
1226 PDRVMAINMOUSE pDrv = RT_FROM_MEMBER(pInterface, DRVMAINMOUSE, IConnector);
1227 if (fRelative)
1228 pDrv->u32DevCaps |= MOUSE_DEVCAP_RELATIVE;
1229 else
1230 pDrv->u32DevCaps &= ~MOUSE_DEVCAP_RELATIVE;
1231 if (fAbsolute)
1232 pDrv->u32DevCaps |= MOUSE_DEVCAP_ABSOLUTE;
1233 else
1234 pDrv->u32DevCaps &= ~MOUSE_DEVCAP_ABSOLUTE;
1235 if (fMTAbsolute)
1236 pDrv->u32DevCaps |= MOUSE_DEVCAP_MT_ABSOLUTE;
1237 else
1238 pDrv->u32DevCaps &= ~MOUSE_DEVCAP_MT_ABSOLUTE;
1239 if (fMTRelative)
1240 pDrv->u32DevCaps |= MOUSE_DEVCAP_MT_RELATIVE;
1241 else
1242 pDrv->u32DevCaps &= ~MOUSE_DEVCAP_MT_RELATIVE;
1243
1244 pDrv->pMouse->i_sendMouseCapsNotifications();
1245}
1246
1247
1248/**
1249 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
1250 */
1251DECLCALLBACK(void *) Mouse::i_drvQueryInterface(PPDMIBASE pInterface, const char *pszIID)
1252{
1253 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
1254 PDRVMAINMOUSE pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
1255
1256 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
1257 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUSECONNECTOR, &pDrv->IConnector);
1258 return NULL;
1259}
1260
1261
1262/**
1263 * Destruct a mouse driver instance.
1264 *
1265 * @param pDrvIns The driver instance data.
1266 */
1267DECLCALLBACK(void) Mouse::i_drvDestruct(PPDMDRVINS pDrvIns)
1268{
1269 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
1270 PDRVMAINMOUSE pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
1271 LogFlow(("Mouse::drvDestruct: iInstance=%d\n", pDrvIns->iInstance));
1272
1273 if (pThis->pMouse)
1274 {
1275 AutoWriteLock mouseLock(pThis->pMouse COMMA_LOCKVAL_SRC_POS);
1276 for (unsigned cDev = 0; cDev < MOUSE_MAX_DEVICES; ++cDev)
1277 if (pThis->pMouse->mpDrv[cDev] == pThis)
1278 {
1279 pThis->pMouse->mpDrv[cDev] = NULL;
1280 break;
1281 }
1282 }
1283}
1284
1285
1286/**
1287 * Construct a mouse driver instance.
1288 *
1289 * @copydoc FNPDMDRVCONSTRUCT
1290 */
1291DECLCALLBACK(int) Mouse::i_drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
1292{
1293 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
1294 RT_NOREF(fFlags, pCfg);
1295 PDRVMAINMOUSE pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
1296 LogFlow(("drvMainMouse_Construct: iInstance=%d\n", pDrvIns->iInstance));
1297
1298 /*
1299 * Validate configuration.
1300 */
1301 PDMDRV_VALIDATE_CONFIG_RETURN(pDrvIns, "", "");
1302 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
1303 ("Configuration error: Not possible to attach anything to this driver!\n"),
1304 VERR_PDM_DRVINS_NO_ATTACH);
1305
1306 /*
1307 * IBase.
1308 */
1309 pDrvIns->IBase.pfnQueryInterface = Mouse::i_drvQueryInterface;
1310
1311 pThis->IConnector.pfnReportModes = Mouse::i_mouseReportModes;
1312
1313 /*
1314 * Get the IMousePort interface of the above driver/device.
1315 */
1316 pThis->pUpPort = (PPDMIMOUSEPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMIMOUSEPORT_IID);
1317 if (!pThis->pUpPort)
1318 {
1319 AssertMsgFailed(("Configuration error: No mouse port interface above!\n"));
1320 return VERR_PDM_MISSING_INTERFACE_ABOVE;
1321 }
1322
1323 /*
1324 * Get the Mouse object pointer and update the mpDrv member.
1325 */
1326 com::Guid uuid(COM_IIDOF(IMouse));
1327 IMouse *pIMouse = (IMouse *)PDMDrvHlpQueryGenericUserObject(pDrvIns, uuid.raw());
1328 if (!pIMouse)
1329 {
1330 AssertMsgFailed(("Configuration error: No/bad Mouse object!\n"));
1331 return VERR_NOT_FOUND;
1332 }
1333 pThis->pMouse = static_cast<Mouse *>(pIMouse);
1334
1335 unsigned cDev;
1336 {
1337 AutoWriteLock mouseLock(pThis->pMouse COMMA_LOCKVAL_SRC_POS);
1338
1339 for (cDev = 0; cDev < MOUSE_MAX_DEVICES; ++cDev)
1340 if (!pThis->pMouse->mpDrv[cDev])
1341 {
1342 pThis->pMouse->mpDrv[cDev] = pThis;
1343 break;
1344 }
1345 }
1346 if (cDev == MOUSE_MAX_DEVICES)
1347 return VERR_NO_MORE_HANDLES;
1348
1349 return VINF_SUCCESS;
1350}
1351
1352
1353/**
1354 * Main mouse driver registration record.
1355 */
1356const PDMDRVREG Mouse::DrvReg =
1357{
1358 /* u32Version */
1359 PDM_DRVREG_VERSION,
1360 /* szName */
1361 "MainMouse",
1362 /* szRCMod */
1363 "",
1364 /* szR0Mod */
1365 "",
1366 /* pszDescription */
1367 "Main mouse driver (Main as in the API).",
1368 /* fFlags */
1369 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1370 /* fClass. */
1371 PDM_DRVREG_CLASS_MOUSE,
1372 /* cMaxInstances */
1373 ~0U,
1374 /* cbInstance */
1375 sizeof(DRVMAINMOUSE),
1376 /* pfnConstruct */
1377 Mouse::i_drvConstruct,
1378 /* pfnDestruct */
1379 Mouse::i_drvDestruct,
1380 /* pfnRelocate */
1381 NULL,
1382 /* pfnIOCtl */
1383 NULL,
1384 /* pfnPowerOn */
1385 NULL,
1386 /* pfnReset */
1387 NULL,
1388 /* pfnSuspend */
1389 NULL,
1390 /* pfnResume */
1391 NULL,
1392 /* pfnAttach */
1393 NULL,
1394 /* pfnDetach */
1395 NULL,
1396 /* pfnPowerOff */
1397 NULL,
1398 /* pfnSoftReset */
1399 NULL,
1400 /* u32EndVersion */
1401 PDM_DRVREG_VERSION
1402};
1403/* 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