VirtualBox

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

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

IMouse::PointerShape

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 37.8 KB
Line 
1/* $Id: MouseImpl.cpp 52921 2014-10-02 06:36:54Z 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 com::SafeArray <LONG64> arrayContacts(aContacts);
864
865 LogRel3(("%s: aCount %d(actual %d), aScanTime %u\n",
866 __FUNCTION__, aCount, arrayContacts.size(), aScanTime));
867
868 HRESULT rc = S_OK;
869
870 if ((LONG)arrayContacts.size() >= aCount)
871 {
872 LONG64* paContacts = arrayContacts.raw();
873
874 rc = i_putEventMultiTouch(aCount, paContacts, aScanTime);
875 }
876 else
877 {
878 rc = E_INVALIDARG;
879 }
880
881 return rc;
882}
883
884/**
885 * Send a multi-touch event. Version for scripting languages.
886 *
887 * @returns COM status code.
888 * @param aCount Number of contacts.
889 * @param aContacts Information about each contact.
890 * @param aScanTime Timestamp.
891 */
892HRESULT Mouse::putEventMultiTouchString(LONG aCount,
893 const com::Utf8Str &aContacts,
894 ULONG aScanTime)
895{
896 /** @todo implement: convert the string to LONG64 array and call putEventMultiTouch. */
897 NOREF(aCount);
898 NOREF(aContacts);
899 NOREF(aScanTime);
900 return E_NOTIMPL;
901}
902
903
904// private methods
905/////////////////////////////////////////////////////////////////////////////
906
907/* Used by PutEventMultiTouch and PutEventMultiTouchString. */
908HRESULT Mouse::i_putEventMultiTouch(LONG aCount,
909 LONG64 *paContacts,
910 ULONG aScanTime)
911{
912 if (aCount >= 256)
913 {
914 return E_INVALIDARG;
915 }
916
917 DisplayMouseInterface *pDisplay = mParent->i_getDisplayMouseInterface();
918 ComAssertRet(pDisplay, E_FAIL);
919
920 /* Touch events are mapped to the primary monitor, because the emulated USB
921 * touchscreen device is associated with one (normally the primary) screen in the guest.
922 */
923 ULONG uScreenId = 0;
924
925 ULONG cWidth = 0;
926 ULONG cHeight = 0;
927 ULONG cBPP = 0;
928 LONG xOrigin = 0;
929 LONG yOrigin = 0;
930 HRESULT rc = pDisplay->i_getScreenResolution(uScreenId, &cWidth, &cHeight, &cBPP, &xOrigin, &yOrigin);
931 NOREF(cBPP);
932 ComAssertComRCRetRC(rc);
933
934 uint64_t* pau64Contacts = NULL;
935 uint8_t cContacts = 0;
936
937 /* Deliver 0 contacts too, touch device may use this to reset the state. */
938 if (aCount > 0)
939 {
940 /* Create a copy with converted coords. */
941 pau64Contacts = (uint64_t *)RTMemTmpAlloc(aCount * sizeof(uint64_t));
942 if (pau64Contacts)
943 {
944 int32_t x1 = xOrigin;
945 int32_t y1 = yOrigin;
946 int32_t x2 = x1 + cWidth;
947 int32_t y2 = y1 + cHeight;
948
949 LogRel3(("%s: screen [%d] %d,%d %d,%d\n",
950 __FUNCTION__, uScreenId, x1, y1, x2, y2));
951
952 LONG i;
953 for (i = 0; i < aCount; i++)
954 {
955 uint32_t u32Lo = RT_LO_U32(paContacts[i]);
956 uint32_t u32Hi = RT_HI_U32(paContacts[i]);
957 int32_t x = (int16_t)u32Lo;
958 int32_t y = (int16_t)(u32Lo >> 16);
959 uint8_t contactId = RT_BYTE1(u32Hi);
960 bool fInContact = (RT_BYTE2(u32Hi) & 0x1) != 0;
961 bool fInRange = (RT_BYTE2(u32Hi) & 0x2) != 0;
962
963 LogRel3(("%s: [%d] %d,%d id %d, inContact %d, inRange %d\n",
964 __FUNCTION__, i, x, y, contactId, fInContact, fInRange));
965
966 /* x1,y1 are inclusive and x2,y2 are exclusive,
967 * while x,y start from 1 and are inclusive.
968 */
969 if (x <= x1 || x > x2 || y <= y1 || y > y2)
970 {
971 /* Out of range. Skip the contact. */
972 continue;
973 }
974
975 int32_t xAdj = x1 < x2? ((x - 1 - x1) * VMMDEV_MOUSE_RANGE) / (x2 - x1) : 0;
976 int32_t yAdj = y1 < y2? ((y - 1 - y1) * VMMDEV_MOUSE_RANGE) / (y2 - y1) : 0;
977
978 bool fValid = ( xAdj >= VMMDEV_MOUSE_RANGE_MIN
979 && xAdj <= VMMDEV_MOUSE_RANGE_MAX
980 && yAdj >= VMMDEV_MOUSE_RANGE_MIN
981 && yAdj <= VMMDEV_MOUSE_RANGE_MAX);
982
983 if (fValid)
984 {
985 uint8_t fu8 = (fInContact? 0x01: 0x00)
986 | (fInRange? 0x02: 0x00);
987 pau64Contacts[cContacts] = RT_MAKE_U64_FROM_U16((uint16_t)xAdj,
988 (uint16_t)yAdj,
989 RT_MAKE_U16(contactId, fu8),
990 0);
991 cContacts++;
992 }
993 }
994 }
995 else
996 {
997 rc = E_OUTOFMEMORY;
998 }
999 }
1000
1001 if (SUCCEEDED(rc))
1002 {
1003 rc = i_reportMultiTouchEventToDevice(cContacts, cContacts? pau64Contacts: NULL, (uint32_t)aScanTime);
1004
1005 /* Send the original contact information. */
1006 i_fireMultiTouchEvent(cContacts, cContacts? paContacts: NULL, (uint32_t)aScanTime);
1007 }
1008
1009 RTMemTmpFree(pau64Contacts);
1010
1011 return rc;
1012}
1013
1014
1015/** Does the guest currently rely on the host to draw the mouse cursor or
1016 * can it switch to doing it itself in software? */
1017bool Mouse::i_guestNeedsHostCursor(void)
1018{
1019 return RT_BOOL(mfVMMDevGuestCaps & VMMDEV_MOUSE_GUEST_NEEDS_HOST_CURSOR);
1020}
1021
1022
1023/** Check what sort of reporting can be done using the devices currently
1024 * enabled. Does not consider the VMM device. */
1025void Mouse::i_getDeviceCaps(bool *pfAbs, bool *pfRel, bool *pfMT)
1026{
1027 bool fAbsDev = false;
1028 bool fRelDev = false;
1029 bool fMTDev = false;
1030
1031 AutoReadLock aLock(this COMMA_LOCKVAL_SRC_POS);
1032
1033 for (unsigned i = 0; i < MOUSE_MAX_DEVICES; ++i)
1034 if (mpDrv[i])
1035 {
1036 if (mpDrv[i]->u32DevCaps & MOUSE_DEVCAP_ABSOLUTE)
1037 fAbsDev = true;
1038 if (mpDrv[i]->u32DevCaps & MOUSE_DEVCAP_RELATIVE)
1039 fRelDev = true;
1040 if (mpDrv[i]->u32DevCaps & MOUSE_DEVCAP_MULTI_TOUCH)
1041 fMTDev = true;
1042 }
1043 if (pfAbs)
1044 *pfAbs = fAbsDev;
1045 if (pfRel)
1046 *pfRel = fRelDev;
1047 if (pfMT)
1048 *pfMT = fMTDev;
1049}
1050
1051
1052/** Does the VMM device currently support absolute reporting? */
1053bool Mouse::i_vmmdevCanAbs(void)
1054{
1055 bool fRelDev;
1056
1057 i_getDeviceCaps(NULL, &fRelDev, NULL);
1058 return (mfVMMDevGuestCaps & VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE)
1059 && fRelDev;
1060}
1061
1062
1063/** Does the VMM device currently support absolute reporting? */
1064bool Mouse::i_deviceCanAbs(void)
1065{
1066 bool fAbsDev;
1067
1068 i_getDeviceCaps(&fAbsDev, NULL, NULL);
1069 return fAbsDev;
1070}
1071
1072
1073/** Can we currently send relative events to the guest? */
1074bool Mouse::i_supportsRel(void)
1075{
1076 bool fRelDev;
1077
1078 i_getDeviceCaps(NULL, &fRelDev, NULL);
1079 return fRelDev;
1080}
1081
1082
1083/** Can we currently send absolute events to the guest? */
1084bool Mouse::i_supportsAbs(void)
1085{
1086 bool fAbsDev;
1087
1088 i_getDeviceCaps(&fAbsDev, NULL, NULL);
1089 return fAbsDev || i_vmmdevCanAbs();
1090}
1091
1092
1093/** Can we currently send absolute events to the guest? */
1094bool Mouse::i_supportsMT(void)
1095{
1096 bool fMTDev;
1097
1098 i_getDeviceCaps(NULL, NULL, &fMTDev);
1099 return fMTDev;
1100}
1101
1102
1103/** Check what sort of reporting can be done using the devices currently
1104 * enabled (including the VMM device) and notify the guest and the front-end.
1105 */
1106void Mouse::i_sendMouseCapsNotifications(void)
1107{
1108 bool fAbsDev, fRelDev, fMTDev, fCanAbs, fNeedsHostCursor;
1109
1110 {
1111 AutoReadLock aLock(this COMMA_LOCKVAL_SRC_POS);
1112
1113 i_getDeviceCaps(&fAbsDev, &fRelDev, &fMTDev);
1114 fCanAbs = i_supportsAbs();
1115 fNeedsHostCursor = i_guestNeedsHostCursor();
1116 }
1117 if (fAbsDev)
1118 i_updateVMMDevMouseCaps(VMMDEV_MOUSE_HOST_HAS_ABS_DEV, 0);
1119 else
1120 i_updateVMMDevMouseCaps(0, VMMDEV_MOUSE_HOST_HAS_ABS_DEV);
1121 /** @todo this call takes the Console lock in order to update the cached
1122 * callback data atomically. However I can't see any sign that the cached
1123 * data is ever used again. */
1124 mParent->i_onMouseCapabilityChange(fCanAbs, fRelDev, fMTDev, fNeedsHostCursor);
1125}
1126
1127
1128/**
1129 * @interface_method_impl{PDMIMOUSECONNECTOR,pfnReportModes}
1130 * A virtual device is notifying us about its current state and capabilities
1131 */
1132DECLCALLBACK(void) Mouse::i_mouseReportModes(PPDMIMOUSECONNECTOR pInterface, bool fRel, bool fAbs, bool fMT)
1133{
1134 PDRVMAINMOUSE pDrv = RT_FROM_MEMBER(pInterface, DRVMAINMOUSE, IConnector);
1135 if (fRel)
1136 pDrv->u32DevCaps |= MOUSE_DEVCAP_RELATIVE;
1137 else
1138 pDrv->u32DevCaps &= ~MOUSE_DEVCAP_RELATIVE;
1139 if (fAbs)
1140 pDrv->u32DevCaps |= MOUSE_DEVCAP_ABSOLUTE;
1141 else
1142 pDrv->u32DevCaps &= ~MOUSE_DEVCAP_ABSOLUTE;
1143 if (fMT)
1144 pDrv->u32DevCaps |= MOUSE_DEVCAP_MULTI_TOUCH;
1145 else
1146 pDrv->u32DevCaps &= ~MOUSE_DEVCAP_MULTI_TOUCH;
1147
1148 pDrv->pMouse->i_sendMouseCapsNotifications();
1149}
1150
1151
1152/**
1153 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
1154 */
1155DECLCALLBACK(void *) Mouse::i_drvQueryInterface(PPDMIBASE pInterface, const char *pszIID)
1156{
1157 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
1158 PDRVMAINMOUSE pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
1159
1160 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
1161 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUSECONNECTOR, &pDrv->IConnector);
1162 return NULL;
1163}
1164
1165
1166/**
1167 * Destruct a mouse driver instance.
1168 *
1169 * @returns VBox status.
1170 * @param pDrvIns The driver instance data.
1171 */
1172DECLCALLBACK(void) Mouse::i_drvDestruct(PPDMDRVINS pDrvIns)
1173{
1174 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
1175 PDRVMAINMOUSE pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
1176 LogFlow(("Mouse::drvDestruct: iInstance=%d\n", pDrvIns->iInstance));
1177
1178 if (pThis->pMouse)
1179 {
1180 AutoWriteLock mouseLock(pThis->pMouse COMMA_LOCKVAL_SRC_POS);
1181 for (unsigned cDev = 0; cDev < MOUSE_MAX_DEVICES; ++cDev)
1182 if (pThis->pMouse->mpDrv[cDev] == pThis)
1183 {
1184 pThis->pMouse->mpDrv[cDev] = NULL;
1185 break;
1186 }
1187 }
1188}
1189
1190
1191/**
1192 * Construct a mouse driver instance.
1193 *
1194 * @copydoc FNPDMDRVCONSTRUCT
1195 */
1196DECLCALLBACK(int) Mouse::i_drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
1197{
1198 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
1199 PDRVMAINMOUSE pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
1200 LogFlow(("drvMainMouse_Construct: iInstance=%d\n", pDrvIns->iInstance));
1201
1202 /*
1203 * Validate configuration.
1204 */
1205 if (!CFGMR3AreValuesValid(pCfg, "Object\0"))
1206 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
1207 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
1208 ("Configuration error: Not possible to attach anything to this driver!\n"),
1209 VERR_PDM_DRVINS_NO_ATTACH);
1210
1211 /*
1212 * IBase.
1213 */
1214 pDrvIns->IBase.pfnQueryInterface = Mouse::i_drvQueryInterface;
1215
1216 pThis->IConnector.pfnReportModes = Mouse::i_mouseReportModes;
1217
1218 /*
1219 * Get the IMousePort interface of the above driver/device.
1220 */
1221 pThis->pUpPort = (PPDMIMOUSEPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMIMOUSEPORT_IID);
1222 if (!pThis->pUpPort)
1223 {
1224 AssertMsgFailed(("Configuration error: No mouse port interface above!\n"));
1225 return VERR_PDM_MISSING_INTERFACE_ABOVE;
1226 }
1227
1228 /*
1229 * Get the Mouse object pointer and update the mpDrv member.
1230 */
1231 void *pv;
1232 int rc = CFGMR3QueryPtr(pCfg, "Object", &pv);
1233 if (RT_FAILURE(rc))
1234 {
1235 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
1236 return rc;
1237 }
1238 pThis->pMouse = (Mouse *)pv; /** @todo Check this cast! */
1239 unsigned cDev;
1240 {
1241 AutoReadLock mouseLock(pThis->pMouse COMMA_LOCKVAL_SRC_POS);
1242
1243 for (cDev = 0; cDev < MOUSE_MAX_DEVICES; ++cDev)
1244 if (!pThis->pMouse->mpDrv[cDev])
1245 {
1246 pThis->pMouse->mpDrv[cDev] = pThis;
1247 break;
1248 }
1249 }
1250 if (cDev == MOUSE_MAX_DEVICES)
1251 return VERR_NO_MORE_HANDLES;
1252
1253 return VINF_SUCCESS;
1254}
1255
1256
1257/**
1258 * Main mouse driver registration record.
1259 */
1260const PDMDRVREG Mouse::DrvReg =
1261{
1262 /* u32Version */
1263 PDM_DRVREG_VERSION,
1264 /* szName */
1265 "MainMouse",
1266 /* szRCMod */
1267 "",
1268 /* szR0Mod */
1269 "",
1270 /* pszDescription */
1271 "Main mouse driver (Main as in the API).",
1272 /* fFlags */
1273 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1274 /* fClass. */
1275 PDM_DRVREG_CLASS_MOUSE,
1276 /* cMaxInstances */
1277 ~0U,
1278 /* cbInstance */
1279 sizeof(DRVMAINMOUSE),
1280 /* pfnConstruct */
1281 Mouse::i_drvConstruct,
1282 /* pfnDestruct */
1283 Mouse::i_drvDestruct,
1284 /* pfnRelocate */
1285 NULL,
1286 /* pfnIOCtl */
1287 NULL,
1288 /* pfnPowerOn */
1289 NULL,
1290 /* pfnReset */
1291 NULL,
1292 /* pfnSuspend */
1293 NULL,
1294 /* pfnResume */
1295 NULL,
1296 /* pfnAttach */
1297 NULL,
1298 /* pfnDetach */
1299 NULL,
1300 /* pfnPowerOff */
1301 NULL,
1302 /* pfnSoftReset */
1303 NULL,
1304 /* u32EndVersion */
1305 PDM_DRVREG_VERSION
1306};
1307/* 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