VirtualBox

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

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

6813 src-client/MouseImpl.cpp fixed

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