VirtualBox

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

Last change on this file was 99739, checked in by vboxsync, 13 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
RevLine 
[13607]1/* $Id: MouseImpl.cpp 99739 2023-05-11 01:01:08Z vboxsync $ */
[1]2/** @file
3 * VirtualBox COM class implementation
4 */
5
6/*
[98103]7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
[1]8 *
[96407]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
[1]26 */
27
[67914]28#define LOG_GROUP LOG_GROUP_MAIN_MOUSE
29#include "LoggingNew.h"
30
[30681]31#include <iprt/cpp/utils.h>
32
[1]33#include "MouseImpl.h"
34#include "DisplayImpl.h"
35#include "VMMDev.h"
[52921]36#include "MousePointerShapeWrap.h"
[85286]37#include "VBoxEvents.h"
[1]38
[35346]39#include <VBox/vmm/pdmdrv.h>
[33758]40#include <VBox/VMMDev.h>
[76760]41#include <VBox/err.h>
[30681]42
43
[52921]44class ATL_NO_VTABLE MousePointerShape:
45 public MousePointerShapeWrap
46{
47public:
48
[90828]49 DECLARE_COMMON_CLASS_METHODS(MousePointerShape)
[52921]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());
[59858]187 if (m.shape.size())
188 memcpy(&aShape.front(), &m.shape.front(), aShape.size());
[52921]189 return S_OK;
190}
191
192
[27060]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 */
[47174]200 MOUSE_DEVCAP_ABSOLUTE = 2,
[95271]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,
[27060]205};
206/** @} */
207
[36161]208
[1]209/**
210 * Mouse driver instance data.
211 */
[27060]212struct DRVMAINMOUSE
[1]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. */
[25985]221 PDMIMOUSECONNECTOR IConnector;
[27060]222 /** The capabilities of this device. */
223 uint32_t u32DevCaps;
224};
[1]225
[30764]226
[1]227// constructor / destructor
228/////////////////////////////////////////////////////////////////////////////
229
[27607]230Mouse::Mouse()
231 : mParent(NULL)
232{
233}
[13606]234
[27607]235Mouse::~Mouse()
236{
237}
238
239
[1]240HRESULT Mouse::FinalConstruct()
241{
[27060]242 RT_ZERO(mpDrv);
[52921]243 RT_ZERO(mPointerData);
[47208]244 mcLastX = 0x8000;
245 mcLastY = 0x8000;
[33758]246 mfLastButtons = 0;
[33779]247 mfVMMDevGuestCaps = 0;
[35638]248 return BaseFinalConstruct();
[1]249}
250
251void Mouse::FinalRelease()
252{
[13606]253 uninit();
[35638]254 BaseFinalRelease();
[1]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 */
[47190]266HRESULT Mouse::init (ConsoleMouseInterface *parent)
[1]267{
[21878]268 LogFlowThisFunc(("\n"));
[1]269
[26235]270 ComAssertRet(parent, E_INVALIDARG);
[1]271
[13606]272 /* Enclose the state transition NotReady->InInit->Ready */
[21878]273 AutoInitSpan autoInitSpan(this);
274 AssertReturn(autoInitSpan.isOk(), E_FAIL);
[1]275
[13606]276 unconst(mParent) = parent;
[1]277
[33061]278 unconst(mEventSource).createObject();
[94924]279 HRESULT hrc = mEventSource->init();
280 AssertComRCReturnRC(hrc);
[33061]281
[85286]282 ComPtr<IEvent> ptrEvent;
[94924]283 hrc = ::CreateGuestMouseEvent(ptrEvent.asOutParam(), mEventSource,
284 (GuestMouseEventMode_T)0, 0 /*x*/, 0 /*y*/, 0 /*z*/, 0 /*w*/, 0 /*buttons*/);
285 AssertComRCReturnRC(hrc);
[85286]286 mMouseEvent.init(ptrEvent, mEventSource);
287
[13606]288 /* Confirm a successful initialization */
289 autoInitSpan.setSucceeded();
290
[1]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{
[21878]300 LogFlowThisFunc(("\n"));
[1]301
[13606]302 /* Enclose the state transition Ready->InUninit->NotReady */
[21878]303 AutoUninitSpan autoUninitSpan(this);
[13606]304 if (autoUninitSpan.uninitDone())
305 return;
[1]306
[27060]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 }
[1]313
[52921]314 mPointerShape.setNull();
315
316 RTMemFree(mPointerData.pu8Shape);
317 mPointerData.pu8Shape = NULL;
318 mPointerData.cbShape = 0;
319
[33061]320 mMouseEvent.uninit();
321 unconst(mEventSource).setNull();
[27607]322 unconst(mParent) = NULL;
[1]323}
324
[52921]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);
[26624]331
[52921]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
[1]354// IMouse properties
355/////////////////////////////////////////////////////////////////////////////
356
[32828]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! */
[50613]360HRESULT Mouse::i_updateVMMDevMouseCaps(uint32_t fCapsAdded,
361 uint32_t fCapsRemoved)
[26624]362{
[51612]363 VMMDevMouseInterface *pVMMDev = mParent->i_getVMMDevMouseInterface();
[33758]364 if (!pVMMDev)
365 return E_FAIL; /* No assertion, as the front-ends can send events
366 * at all sorts of inconvenient times. */
[53965]367 DisplayMouseInterface *pDisplay = mParent->i_getDisplayMouseInterface();
368 if (pDisplay == NULL)
369 return E_FAIL;
[26624]370 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
[41131]371 if (!pVMMDevPort)
372 return E_FAIL; /* same here */
[26624]373
[94924]374 int vrc = pVMMDevPort->pfnUpdateMouseCapabilities(pVMMDevPort, fCapsAdded,
375 fCapsRemoved);
376 if (RT_FAILURE(vrc))
[53965]377 return E_FAIL;
378 return pDisplay->i_reportHostCursorCapabilities(fCapsAdded, fCapsRemoved);
[26624]379}
380
[1]381/**
[47174]382 * Returns whether the currently active device portfolio can accept absolute
383 * mouse events.
[1]384 *
385 * @returns COM status code
[65103]386 * @param aAbsoluteSupported address of result variable
[1]387 */
[50613]388HRESULT Mouse::getAbsoluteSupported(BOOL *aAbsoluteSupported)
[1]389{
[50613]390 *aAbsoluteSupported = i_supportsAbs();
[1]391 return S_OK;
392}
393
394/**
[47174]395 * Returns whether the currently active device portfolio can accept relative
396 * mouse events.
[26782]397 *
398 * @returns COM status code
[65103]399 * @param aRelativeSupported address of result variable
[26782]400 */
[50613]401HRESULT Mouse::getRelativeSupported(BOOL *aRelativeSupported)
[26782]402{
[50613]403 *aRelativeSupported = i_supportsRel();
[26782]404 return S_OK;
405}
406
407/**
[47174]408 * Returns whether the currently active device portfolio can accept multi-touch
[95368]409 * touchscreen events.
[47174]410 *
411 * @returns COM status code
[95368]412 * @param aTouchScreenSupported address of result variable
[47174]413 */
[95368]414HRESULT Mouse::getTouchScreenSupported(BOOL *aTouchScreenSupported)
[47174]415{
[95368]416 *aTouchScreenSupported = i_supportsTS();
[47174]417 return S_OK;
418}
419
420/**
[95368]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/**
[32828]434 * Returns whether the guest can currently switch to drawing the mouse cursor
435 * itself if it is asked to by the front-end.
[1]436 *
437 * @returns COM status code
[65103]438 * @param aNeedsHostCursor address of result variable
[1]439 */
[50613]440HRESULT Mouse::getNeedsHostCursor(BOOL *aNeedsHostCursor)
[1]441{
[50613]442 *aNeedsHostCursor = i_guestNeedsHostCursor();
[1]443 return S_OK;
444}
445
[52921]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
[1]478// IMouse methods
479/////////////////////////////////////////////////////////////////////////////
480
[32828]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. */
[50613]484static uint32_t i_mouseButtonsToPDM(LONG buttonState)
[26624]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
[50613]500HRESULT Mouse::getEventSource(ComPtr<IEventSource> &aEventSource)
[33061]501{
502 // no need to lock - lifetime constant
[50613]503 mEventSource.queryInterfaceTo(aEventSource.asOutParam());
[33061]504 return S_OK;
505}
506
[1]507/**
[32828]508 * Send a relative pointer event to the relative device we deem most
509 * appropriate.
[26624]510 *
511 * @returns COM status code
512 */
[50613]513HRESULT Mouse::i_reportRelEventToMouseDev(int32_t dx, int32_t dy, int32_t dz,
514 int32_t dw, uint32_t fButtons)
[26624]515{
[33758]516 if (dx || dy || dz || dw || fButtons != mfLastButtons)
[26624]517 {
[27060]518 PPDMIMOUSEPORT pUpPort = NULL;
519 {
[32817]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 }
[27060]527 }
528 if (!pUpPort)
[27129]529 return S_OK;
[27060]530
[26624]531 int vrc = pUpPort->pfnPutEvent(pUpPort, dx, dy, dz, dw, fButtons);
532
533 if (RT_FAILURE(vrc))
[73003]534 return setErrorBoth(VBOX_E_IPRT_ERROR, vrc,
535 tr("Could not send the mouse event to the virtual mouse (%Rrc)"),
536 vrc);
[33758]537 mfLastButtons = fButtons;
[26624]538 }
539 return S_OK;
540}
541
542
543/**
[32828]544 * Send an absolute pointer event to the emulated absolute device we deem most
545 * appropriate.
[26624]546 *
547 * @returns COM status code
548 */
[50613]549HRESULT Mouse::i_reportAbsEventToMouseDev(int32_t x, int32_t y,
550 int32_t dz, int32_t dw, uint32_t fButtons)
[26624]551{
[47208]552 if ( x < VMMDEV_MOUSE_RANGE_MIN
553 || x > VMMDEV_MOUSE_RANGE_MAX)
[36161]554 return S_OK;
[47208]555 if ( y < VMMDEV_MOUSE_RANGE_MIN
556 || y > VMMDEV_MOUSE_RANGE_MAX)
[36161]557 return S_OK;
[47208]558 if ( x != mcLastX || y != mcLastY
[33758]559 || dz || dw || fButtons != mfLastButtons)
[26624]560 {
[27060]561 PPDMIMOUSEPORT pUpPort = NULL;
562 {
[32817]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 }
[27060]570 }
571 if (!pUpPort)
[27129]572 return S_OK;
[27060]573
[47208]574 int vrc = pUpPort->pfnPutEventAbs(pUpPort, x, y, dz,
[27060]575 dw, fButtons);
[26624]576 if (RT_FAILURE(vrc))
[73003]577 return setErrorBoth(VBOX_E_IPRT_ERROR, vrc,
578 tr("Could not send the mouse event to the virtual mouse (%Rrc)"),
579 vrc);
[33758]580 mfLastButtons = fButtons;
[33075]581
[26624]582 }
583 return S_OK;
584}
585
[50613]586HRESULT Mouse::i_reportMultiTouchEventToDevice(uint8_t cContacts,
587 const uint64_t *pau64Contacts,
[95368]588 bool fTouchScreen,
[50613]589 uint32_t u32ScanTime)
[47571]590{
591 HRESULT hrc = S_OK;
[26624]592
[95368]593 int match = fTouchScreen ? MOUSE_DEVCAP_MT_ABSOLUTE : MOUSE_DEVCAP_MT_RELATIVE;
[47208]594 PPDMIMOUSEPORT pUpPort = NULL;
595 {
596 AutoReadLock aLock(this COMMA_LOCKVAL_SRC_POS);
597
[47571]598 unsigned i;
599 for (i = 0; i < MOUSE_MAX_DEVICES; ++i)
[47208]600 {
601 if ( mpDrv[i]
[95368]602 && (mpDrv[i]->u32DevCaps & match))
[47571]603 {
[47208]604 pUpPort = mpDrv[i]->pUpPort;
[47571]605 break;
606 }
[47208]607 }
608 }
609
[47571]610 if (pUpPort)
611 {
[95271]612 int vrc = pUpPort->pfnPutEventTouchScreen(pUpPort, cContacts, pau64Contacts, u32ScanTime);
[47571]613 if (RT_FAILURE(vrc))
[73003]614 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc,
615 tr("Could not send the multi-touch event to the virtual device (%Rrc)"),
616 vrc);
[47571]617 }
618 else
619 {
620 hrc = E_UNEXPECTED;
621 }
622
623 return hrc;
[47174]624}
625
626
627/**
[26624]628 * Send an absolute position event to the VMM device.
[32828]629 * @note all calls out of this object are made with no locks held!
[26624]630 *
631 * @returns COM status code
632 */
[97790]633HRESULT Mouse::i_reportAbsEventToVMMDev(int32_t x, int32_t y, int32_t dz, int32_t dw, uint32_t fButtons)
[26624]634{
[51612]635 VMMDevMouseInterface *pVMMDev = mParent->i_getVMMDevMouseInterface();
[26624]636 ComAssertRet(pVMMDev, E_FAIL);
637 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
638 ComAssertRet(pVMMDevPort, E_FAIL);
639
[97790]640 if (x != mcLastX || y != mcLastY || dz || dw || fButtons != mfLastButtons)
[26624]641 {
[97790]642 int vrc = pVMMDevPort->pfnSetAbsoluteMouse(pVMMDevPort, x, y, dz, dw, fButtons);
[26624]643 if (RT_FAILURE(vrc))
[73003]644 return setErrorBoth(VBOX_E_IPRT_ERROR, vrc,
645 tr("Could not send the mouse event to the virtual mouse (%Rrc)"),
646 vrc);
[26624]647 }
648 return S_OK;
649}
650
[32828]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 */
[53965]658HRESULT Mouse::i_reportAbsEventToInputDevices(int32_t x, int32_t y, int32_t dz, int32_t dw, uint32_t fButtons,
659 bool fUsesVMMDevEvent)
[32817]660{
[97791]661 HRESULT hrc = S_OK;
[97558]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
[50613]667 if (i_vmmdevCanAbs())
[32817]668 {
[47841]669 /*
670 * Send the absolute mouse position to the VMM device.
671 */
[97790]672 if (x != mcLastX || y != mcLastY || dz || dw || fButtons != mfLastButtons)
[32817]673 {
[97790]674 hrc = i_reportAbsEventToVMMDev(x, y, dz, dw, fButtons);
[33261]675 cJiggle = !fUsesVMMDevEvent;
[32817]676 }
[97790]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);
[32817]684 }
[47841]685 else
[94924]686 hrc = i_reportAbsEventToMouseDev(x, y, dz, dw, fButtons);
[32817]687
[47208]688 mcLastX = x;
689 mcLastY = y;
[97790]690 mfLastButtons = fButtons;
[94924]691 return hrc;
[32817]692}
693
[53965]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
[77485]708 if (x != mcLastX || y != mcLastY)
[53965]709 {
[77485]710 pDisplay->i_reportHostCursorPosition(x - 1, y - 1, false);
[53965]711 }
712 return S_OK;
713}
714
715
[50613]716void Mouse::i_fireMouseEvent(bool fAbsolute, LONG x, LONG y, LONG dz, LONG dw,
717 LONG fButtons)
[35871]718{
719 /* If mouse button is pressed, we generate new event, to avoid reusable events coalescing and thus
720 dropping key press events */
[47805]721 GuestMouseEventMode_T mode;
722 if (fAbsolute)
723 mode = GuestMouseEventMode_Absolute;
724 else
725 mode = GuestMouseEventMode_Relative;
726
[47215]727 if (fButtons != 0)
[85300]728 ::FireGuestMouseEvent(mEventSource, mode, x, y, dz, dw, fButtons);
[35871]729 else
730 {
[85286]731 ComPtr<IEvent> ptrEvent;
732 mMouseEvent.getEvent(ptrEvent.asOutParam());
733 ReinitGuestMouseEvent(ptrEvent, mode, x, y, dz, dw, fButtons);
[35871]734 mMouseEvent.fire(0);
735 }
736}
737
[50613]738void Mouse::i_fireMultiTouchEvent(uint8_t cContacts,
739 const LONG64 *paContacts,
[95368]740 bool fTouchScreen,
[50613]741 uint32_t u32ScanTime)
[47848]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);
[35871]747
[47848]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
[85300]759 ::FireGuestMultiTouchEvent(mEventSource, cContacts, ComSafeArrayAsInParam(xPositions), ComSafeArrayAsInParam(yPositions),
[95368]760 ComSafeArrayAsInParam(contactIds), ComSafeArrayAsInParam(contactFlags), fTouchScreen, u32ScanTime);
[47848]761}
762
[26624]763/**
[32828]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
[1]768 *
769 * @returns COM status code
[65103]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.
[1]775 */
[50613]776HRESULT Mouse::putMouseEvent(LONG dx, LONG dy, LONG dz, LONG dw,
777 LONG aButtonState)
[1]778{
[33758]779 LogRel3(("%s: dx=%d, dy=%d, dz=%d, dw=%d\n", __PRETTY_FUNCTION__,
[32817]780 dx, dy, dz, dw));
781
[94924]782 uint32_t fButtonsAdj = i_mouseButtonsToPDM(aButtonState);
[33758]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. */
[50613]785 i_updateVMMDevMouseCaps(0, VMMDEV_MOUSE_HOST_WANTS_ABSOLUTE);
[94924]786 HRESULT hrc = i_reportRelEventToMouseDev(dx, dy, dz, dw, fButtonsAdj);
[1]787
[50613]788 i_fireMouseEvent(false, dx, dy, dz, dw, aButtonState);
[33075]789
[94924]790 return hrc;
[1]791}
792
793/**
[32817]794 * Convert an (X, Y) value pair in screen co-ordinates (starting from 1) to a
[40310]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.
[38590]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.
[26624]805 *
806 * @returns COM status value
807 */
[50613]808HRESULT Mouse::i_convertDisplayRes(LONG x, LONG y, int32_t *pxAdj, int32_t *pyAdj,
809 bool *pfValid)
[26624]810{
[47215]811 AssertPtrReturn(pxAdj, E_POINTER);
812 AssertPtrReturn(pyAdj, E_POINTER);
[35212]813 AssertPtrNullReturn(pfValid, E_POINTER);
[51612]814 DisplayMouseInterface *pDisplay = mParent->i_getDisplayMouseInterface();
[26624]815 ComAssertRet(pDisplay, E_FAIL);
[38590]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 */
[40310]819 enum { ADJUST_RANGE = - 3 * VMMDEV_MOUSE_RANGE / 4 };
[26624]820
[35212]821 if (pfValid)
822 *pfValid = true;
[54495]823 if (!(mfVMMDevGuestCaps & VMMDEV_MOUSE_NEW_PROTOCOL) && !pDisplay->i_isInputMappingSet())
[34754]824 {
825 ULONG displayWidth, displayHeight;
[52064]826 ULONG ulDummy;
827 LONG lDummy;
[34754]828 /* Takes the display lock */
[94924]829 HRESULT hrc = pDisplay->i_getScreenResolution(0, &displayWidth,
830 &displayHeight, &ulDummy, &lDummy, &lDummy);
831 if (FAILED(hrc))
832 return hrc;
[26624]833
[47215]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;
[34754]838 }
839 else
840 {
841 int32_t x1, y1, x2, y2;
[34835]842 /* Takes the display lock */
[52064]843 pDisplay->i_getFramebufferDimensions(&x1, &y1, &x2, &y2);
[47215]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)
[35212]852 if (pfValid)
853 *pfValid = false;
[34754]854 }
[26624]855 return S_OK;
856}
857
858
859/**
[32817]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.
[32828]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!
[1]867 *
868 * @returns COM status code
[65103]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
[1]874 */
[50613]875HRESULT Mouse::putMouseEventAbsolute(LONG x, LONG y, LONG dz, LONG dw,
876 LONG aButtonState)
[1]877{
[47215]878 LogRel3(("%s: x=%d, y=%d, dz=%d, dw=%d, fButtons=0x%x\n",
[50613]879 __PRETTY_FUNCTION__, x, y, dz, dw, aButtonState));
[1]880
[77485]881 DisplayMouseInterface *pDisplay = mParent->i_getDisplayMouseInterface();
882 ComAssertRet(pDisplay, E_FAIL);
[47208]883 int32_t xAdj, yAdj;
[47215]884 uint32_t fButtonsAdj;
[35212]885 bool fValid;
[32817]886
[77485]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;
[27208]902 /** @todo the front end should do this conversion to avoid races */
[32817]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. */
[94924]905 HRESULT hrc = i_convertDisplayRes(x, y, &xAdj, &yAdj, &fValid);
906 if (FAILED(hrc)) return hrc;
[27160]907
[50613]908 fButtonsAdj = i_mouseButtonsToPDM(aButtonState);
[35212]909 if (fValid)
910 {
[94924]911 hrc = i_reportAbsEventToInputDevices(xAdj, yAdj, dz, dw, fButtonsAdj,
912 RT_BOOL(mfVMMDevGuestCaps & VMMDEV_MOUSE_NEW_PROTOCOL));
913 if (FAILED(hrc)) return hrc;
[27060]914
[50613]915 i_fireMouseEvent(true, x, y, dz, dw, aButtonState);
[35212]916 }
[94924]917 hrc = i_reportAbsEventToDisplayDevice(x, y);
[33075]918
[94924]919 return hrc;
[1]920}
921
[47208]922/**
[47571]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.
[95368]929 * @param aIsTouchscreen Distinguishes between touchscreen and touchpad events.
[47571]930 * @param aScanTime Timestamp.
[47208]931 */
[50613]932HRESULT Mouse::putEventMultiTouch(LONG aCount,
933 const std::vector<LONG64> &aContacts,
[95368]934 BOOL aIsTouchscreen,
[50613]935 ULONG aScanTime)
[47208]936{
[47571]937 LogRel3(("%s: aCount %d(actual %d), aScanTime %u\n",
[52934]938 __FUNCTION__, aCount, aContacts.size(), aScanTime));
[47208]939
[94924]940 HRESULT hrc = S_OK;
[47208]941
[52934]942 if ((LONG)aContacts.size() >= aCount)
[47208]943 {
[52934]944 const LONG64 *paContacts = aCount > 0? &aContacts.front(): NULL;
[47208]945
[95368]946 hrc = i_putEventMultiTouch(aCount, paContacts, aIsTouchscreen, aScanTime);
[47208]947 }
[47571]948 else
949 {
[94924]950 hrc = E_INVALIDARG;
[47571]951 }
[47208]952
[94924]953 return hrc;
[47208]954}
955
[47571]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.
[95368]962 * @param aIsTouchscreen Distinguishes between touchscreen and touchpad events.
[47571]963 * @param aScanTime Timestamp.
964 */
[50613]965HRESULT Mouse::putEventMultiTouchString(LONG aCount,
966 const com::Utf8Str &aContacts,
[95368]967 BOOL aIsTouchscreen,
[50613]968 ULONG aScanTime)
[47571]969{
970 /** @todo implement: convert the string to LONG64 array and call putEventMultiTouch. */
971 NOREF(aCount);
972 NOREF(aContacts);
[95368]973 NOREF(aIsTouchscreen);
[47571]974 NOREF(aScanTime);
975 return E_NOTIMPL;
976}
977
978
[1]979// private methods
980/////////////////////////////////////////////////////////////////////////////
981
[47571]982/* Used by PutEventMultiTouch and PutEventMultiTouchString. */
[50613]983HRESULT Mouse::i_putEventMultiTouch(LONG aCount,
[52934]984 const LONG64 *paContacts,
[95368]985 BOOL aIsTouchscreen,
[50613]986 ULONG aScanTime)
[47571]987{
988 if (aCount >= 256)
[96909]989 return E_INVALIDARG;
[26650]990
[95368]991 HRESULT hrc = S_OK;
[47571]992
[95368]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. */
[47795]997 ULONG uScreenId = 0;
[47571]998
[47795]999 ULONG cWidth = 0;
1000 ULONG cHeight = 0;
1001 LONG xOrigin = 0;
1002 LONG yOrigin = 0;
1003
[95368]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
[47571]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 {
[95368]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;
[47571]1030
[95368]1031 LogRel3(("%s: screen [%d] %d,%d %d,%d\n",
1032 __FUNCTION__, uScreenId, x1, y1, x2, y2));
[47795]1033
[95368]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;
[47571]1044
[95368]1045 LogRel3(("%s: touchscreen [%d] %d,%d id %d, inContact %d, inRange %d\n",
1046 __FUNCTION__, i, x, y, contactId, fInContact, fInRange));
[47571]1047
[95368]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 }
[96909]1076 }
1077 else
1078 {
[95368]1079 LONG i;
1080 for (i = 0; i < aCount; i++)
[47795]1081 {
[95368]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;
[47571]1088
[95368]1089 LogRel3(("%s: touchpad [%d] %#04x,%#04x id %d, inContact %d\n",
1090 __FUNCTION__, i, x, y, contactId, fInContact));
[47795]1091
[95368]1092 uint8_t fu8 = (uint8_t)(fInContact? 0x01: 0x00);
[47571]1093
[95368]1094 pau64Contacts[cContacts] = RT_MAKE_U64_FROM_U16(x, y,
[47571]1095 RT_MAKE_U16(contactId, fu8),
[95368]1096 0);
[47571]1097 cContacts++;
1098 }
1099 }
1100 }
1101 else
1102 {
[94924]1103 hrc = E_OUTOFMEMORY;
[47571]1104 }
1105 }
1106
[94924]1107 if (SUCCEEDED(hrc))
[47571]1108 {
[95368]1109 hrc = i_reportMultiTouchEventToDevice(cContacts, cContacts? pau64Contacts: NULL, !!aIsTouchscreen, (uint32_t)aScanTime);
[47571]1110
[47848]1111 /* Send the original contact information. */
[95368]1112 i_fireMultiTouchEvent(cContacts, cContacts? paContacts: NULL, !!aIsTouchscreen, (uint32_t)aScanTime);
[47571]1113 }
1114
1115 RTMemTmpFree(pau64Contacts);
1116
[94924]1117 return hrc;
[47571]1118}
1119
1120
[33758]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? */
[50613]1123bool Mouse::i_guestNeedsHostCursor(void)
[26782]1124{
[33758]1125 return RT_BOOL(mfVMMDevGuestCaps & VMMDEV_MOUSE_GUEST_NEEDS_HOST_CURSOR);
1126}
1127
1128
[96909]1129/**
1130 * Gets the combined capabilities of all currently enabled devices.
[65103]1131 *
[96909]1132 * @returns Combination of MOUSE_DEVCAP_XXX
[65103]1133 */
[96909]1134uint32_t Mouse::i_getDeviceCaps(void)
[33758]1135{
[96909]1136 uint32_t fCaps = 0;
[33758]1137 AutoReadLock aLock(this COMMA_LOCKVAL_SRC_POS);
1138 for (unsigned i = 0; i < MOUSE_MAX_DEVICES; ++i)
1139 if (mpDrv[i])
[96909]1140 fCaps |= mpDrv[i]->u32DevCaps;
1141 return fCaps;
[33758]1142}
1143
1144
1145/** Does the VMM device currently support absolute reporting? */
[50613]1146bool Mouse::i_vmmdevCanAbs(void)
[33758]1147{
[96909]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);
[33758]1152}
1153
1154
[96909]1155/** Does any device currently support absolute reporting w/o help from VMMDev? */
[50613]1156bool Mouse::i_deviceCanAbs(void)
[33758]1157{
[96909]1158 return RT_BOOL(i_getDeviceCaps() & MOUSE_DEVCAP_ABSOLUTE);
[33758]1159}
1160
1161
1162/** Can we currently send relative events to the guest? */
[50613]1163bool Mouse::i_supportsRel(void)
[33758]1164{
[96909]1165 return RT_BOOL(i_getDeviceCaps() & MOUSE_DEVCAP_RELATIVE);
1166}
[33758]1167
[96909]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));
[33758]1176}
1177
1178
1179/** Can we currently send absolute events to the guest? */
[50613]1180bool Mouse::i_supportsAbs(void)
[33758]1181{
[96909]1182 return Mouse::i_supportsAbs(i_getDeviceCaps());
[33758]1183}
1184
1185
[95369]1186/** Can we currently send multi-touch events (touchscreen variant) to the guest? */
[95368]1187bool Mouse::i_supportsTS(void)
[47174]1188{
[96909]1189 return RT_BOOL(i_getDeviceCaps() & MOUSE_DEVCAP_MT_ABSOLUTE);
[47174]1190}
1191
1192
[95369]1193/** Can we currently send multi-touch events (touchpad variant) to the guest? */
1194bool Mouse::i_supportsTP(void)
1195{
[96909]1196 return RT_BOOL(i_getDeviceCaps() & MOUSE_DEVCAP_MT_RELATIVE);
[95369]1197}
1198
1199
[33758]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 */
[50613]1203void Mouse::i_sendMouseCapsNotifications(void)
[33758]1204{
[95368]1205 bool fRelDev, fTSDev, fTPDev, fCanAbs, fNeedsHostCursor;
[32817]1206 {
[33758]1207 AutoReadLock aLock(this COMMA_LOCKVAL_SRC_POS);
[32817]1208
[96909]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);
[50613]1214 fNeedsHostCursor = i_guestNeedsHostCursor();
[32817]1215 }
[95368]1216 mParent->i_onMouseCapabilityChange(fCanAbs, fRelDev, fTSDev, fTPDev, fNeedsHostCursor);
[26782]1217}
1218
1219
[1]1220/**
[26935]1221 * @interface_method_impl{PDMIMOUSECONNECTOR,pfnReportModes}
[26650]1222 */
[65103]1223DECLCALLBACK(void) Mouse::i_mouseReportModes(PPDMIMOUSECONNECTOR pInterface, bool fRelative,
[95271]1224 bool fAbsolute, bool fMTAbsolute, bool fMTRelative)
[26650]1225{
1226 PDRVMAINMOUSE pDrv = RT_FROM_MEMBER(pInterface, DRVMAINMOUSE, IConnector);
[65103]1227 if (fRelative)
[27060]1228 pDrv->u32DevCaps |= MOUSE_DEVCAP_RELATIVE;
[26935]1229 else
[27060]1230 pDrv->u32DevCaps &= ~MOUSE_DEVCAP_RELATIVE;
[65103]1231 if (fAbsolute)
[27060]1232 pDrv->u32DevCaps |= MOUSE_DEVCAP_ABSOLUTE;
[26650]1233 else
[27060]1234 pDrv->u32DevCaps &= ~MOUSE_DEVCAP_ABSOLUTE;
[95271]1235 if (fMTAbsolute)
1236 pDrv->u32DevCaps |= MOUSE_DEVCAP_MT_ABSOLUTE;
[47174]1237 else
[95271]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;
[26650]1243
[50613]1244 pDrv->pMouse->i_sendMouseCapsNotifications();
[26650]1245}
1246
1247
1248/**
[25966]1249 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
[1]1250 */
[50613]1251DECLCALLBACK(void *) Mouse::i_drvQueryInterface(PPDMIBASE pInterface, const char *pszIID)
[1]1252{
[25966]1253 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
1254 PDRVMAINMOUSE pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
[25985]1255
1256 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
1257 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUSECONNECTOR, &pDrv->IConnector);
[25966]1258 return NULL;
[1]1259}
1260
1261
1262/**
1263 * Destruct a mouse driver instance.
1264 *
1265 * @param pDrvIns The driver instance data.
1266 */
[50613]1267DECLCALLBACK(void) Mouse::i_drvDestruct(PPDMDRVINS pDrvIns)
[1]1268{
[45029]1269 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
[45030]1270 PDRVMAINMOUSE pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
[1]1271 LogFlow(("Mouse::drvDestruct: iInstance=%d\n", pDrvIns->iInstance));
[26001]1272
[45030]1273 if (pThis->pMouse)
[1]1274 {
[45030]1275 AutoWriteLock mouseLock(pThis->pMouse COMMA_LOCKVAL_SRC_POS);
[27208]1276 for (unsigned cDev = 0; cDev < MOUSE_MAX_DEVICES; ++cDev)
[45030]1277 if (pThis->pMouse->mpDrv[cDev] == pThis)
[27208]1278 {
[45030]1279 pThis->pMouse->mpDrv[cDev] = NULL;
[27208]1280 break;
1281 }
[1]1282 }
1283}
1284
1285
1286/**
1287 * Construct a mouse driver instance.
1288 *
[22277]1289 * @copydoc FNPDMDRVCONSTRUCT
[1]1290 */
[50613]1291DECLCALLBACK(int) Mouse::i_drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
[1]1292{
[45029]1293 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
[93444]1294 RT_NOREF(fFlags, pCfg);
[45030]1295 PDRVMAINMOUSE pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
[1]1296 LogFlow(("drvMainMouse_Construct: iInstance=%d\n", pDrvIns->iInstance));
1297
1298 /*
1299 * Validate configuration.
1300 */
[93444]1301 PDMDRV_VALIDATE_CONFIG_RETURN(pDrvIns, "", "");
[25149]1302 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
[22277]1303 ("Configuration error: Not possible to attach anything to this driver!\n"),
1304 VERR_PDM_DRVINS_NO_ATTACH);
[1]1305
1306 /*
1307 * IBase.
1308 */
[50613]1309 pDrvIns->IBase.pfnQueryInterface = Mouse::i_drvQueryInterface;
[1]1310
[50613]1311 pThis->IConnector.pfnReportModes = Mouse::i_mouseReportModes;
[26624]1312
[1]1313 /*
1314 * Get the IMousePort interface of the above driver/device.
1315 */
[45030]1316 pThis->pUpPort = (PPDMIMOUSEPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMIMOUSEPORT_IID);
1317 if (!pThis->pUpPort)
[1]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 */
[89951]1326 com::Guid uuid(COM_IIDOF(IMouse));
1327 IMouse *pIMouse = (IMouse *)PDMDrvHlpQueryGenericUserObject(pDrvIns, uuid.raw());
1328 if (!pIMouse)
[1]1329 {
[89951]1330 AssertMsgFailed(("Configuration error: No/bad Mouse object!\n"));
1331 return VERR_NOT_FOUND;
[1]1332 }
[89951]1333 pThis->pMouse = static_cast<Mouse *>(pIMouse);
1334
[27060]1335 unsigned cDev;
[32817]1336 {
[59995]1337 AutoWriteLock mouseLock(pThis->pMouse COMMA_LOCKVAL_SRC_POS);
[32817]1338
1339 for (cDev = 0; cDev < MOUSE_MAX_DEVICES; ++cDev)
[47841]1340 if (!pThis->pMouse->mpDrv[cDev])
[32817]1341 {
[47841]1342 pThis->pMouse->mpDrv[cDev] = pThis;
[32817]1343 break;
1344 }
1345 }
[27060]1346 if (cDev == MOUSE_MAX_DEVICES)
1347 return VERR_NO_MORE_HANDLES;
[1]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,
[26166]1360 /* szName */
[1]1361 "MainMouse",
[25893]1362 /* szRCMod */
1363 "",
1364 /* szR0Mod */
1365 "",
[1]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 */
[40282]1373 ~0U,
[1]1374 /* cbInstance */
1375 sizeof(DRVMAINMOUSE),
1376 /* pfnConstruct */
[50613]1377 Mouse::i_drvConstruct,
[1]1378 /* pfnDestruct */
[50613]1379 Mouse::i_drvDestruct,
[25893]1380 /* pfnRelocate */
1381 NULL,
[1]1382 /* pfnIOCtl */
1383 NULL,
1384 /* pfnPowerOn */
1385 NULL,
1386 /* pfnReset */
1387 NULL,
1388 /* pfnSuspend */
1389 NULL,
1390 /* pfnResume */
1391 NULL,
[22277]1392 /* pfnAttach */
1393 NULL,
[1]1394 /* pfnDetach */
[25149]1395 NULL,
[22277]1396 /* pfnPowerOff */
[25149]1397 NULL,
[22277]1398 /* pfnSoftReset */
1399 NULL,
1400 /* u32EndVersion */
1401 PDM_DRVREG_VERSION
[1]1402};
[14772]1403/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use