VirtualBox

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

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

FE/Qt, Main/Console+Mouse: Make compatible with Qt6, and add some API change bits forgotten in previous change. bugref:9891

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