VirtualBox

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

Last change on this file since 52888 was 52821, checked in by vboxsync, 11 years ago

Main: warning, cleanup.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette