VirtualBox

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

Last change on this file since 53517 was 52934, checked in by vboxsync, 10 years ago

Main: safearray cleanup, removed unnecessary SafeArray<->vector conversions

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