VirtualBox

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

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

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