VirtualBox

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

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

Main: IDisplay converted to use API wrappers.

  • 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 52064 2014-07-16 21:23:55Z 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
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
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->i_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->i_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 ULONG ulDummy;
557 LONG lDummy;
558 /* Takes the display lock */
559 HRESULT rc = pDisplay->i_getScreenResolution(0, &displayWidth,
560 &displayHeight, &ulDummy, &lDummy, &lDummy);
561 if (FAILED(rc))
562 return rc;
563
564 *pxAdj = displayWidth ? (x * VMMDEV_MOUSE_RANGE + ADJUST_RANGE)
565 / (LONG) displayWidth: 0;
566 *pyAdj = displayHeight ? (y * VMMDEV_MOUSE_RANGE + ADJUST_RANGE)
567 / (LONG) displayHeight: 0;
568 }
569 else
570 {
571 int32_t x1, y1, x2, y2;
572 /* Takes the display lock */
573 pDisplay->i_getFramebufferDimensions(&x1, &y1, &x2, &y2);
574 *pxAdj = x1 < x2 ? ((x - x1) * VMMDEV_MOUSE_RANGE + ADJUST_RANGE)
575 / (x2 - x1) : 0;
576 *pyAdj = y1 < y2 ? ((y - y1) * VMMDEV_MOUSE_RANGE + ADJUST_RANGE)
577 / (y2 - y1) : 0;
578 if ( *pxAdj < VMMDEV_MOUSE_RANGE_MIN
579 || *pxAdj > VMMDEV_MOUSE_RANGE_MAX
580 || *pyAdj < VMMDEV_MOUSE_RANGE_MIN
581 || *pyAdj > VMMDEV_MOUSE_RANGE_MAX)
582 if (pfValid)
583 *pfValid = false;
584 }
585 return S_OK;
586}
587
588
589/**
590 * Send an absolute mouse event to the VM. This requires either VirtualBox-
591 * specific drivers installed in the guest or absolute pointing device
592 * emulation.
593 * @note the VMMDev capability change is so that the guest knows we are sending
594 * dummy events over the PS/2 device to signal the arrival of new
595 * absolute pointer data, and not pointer real movement data
596 * @note all calls out of this object are made with no locks held!
597 *
598 * @returns COM status code
599 * @param x X position (pixel), starting from 1
600 * @param y Y position (pixel), starting from 1
601 * @param dz Z movement
602 * @param fButtons The mouse button state
603 */
604HRESULT Mouse::putMouseEventAbsolute(LONG x, LONG y, LONG dz, LONG dw,
605 LONG aButtonState)
606{
607 LogRel3(("%s: x=%d, y=%d, dz=%d, dw=%d, fButtons=0x%x\n",
608 __PRETTY_FUNCTION__, x, y, dz, dw, aButtonState));
609
610 int32_t xAdj, yAdj;
611 uint32_t fButtonsAdj;
612 bool fValid;
613
614 /** @todo the front end should do this conversion to avoid races */
615 /** @note Or maybe not... races are pretty inherent in everything done in
616 * this object and not really bad as far as I can see. */
617 HRESULT rc = i_convertDisplayRes(x, y, &xAdj, &yAdj, &fValid);
618 if (FAILED(rc)) return rc;
619
620 fButtonsAdj = i_mouseButtonsToPDM(aButtonState);
621 /* If we are doing old-style (IRQ-less) absolute reporting to the VMM
622 * device then make sure the guest is aware of it, so that it knows to
623 * ignore relative movement on the PS/2 device. */
624 i_updateVMMDevMouseCaps(VMMDEV_MOUSE_HOST_WANTS_ABSOLUTE, 0);
625 if (fValid)
626 {
627 rc = i_reportAbsEvent(xAdj, yAdj, dz, dw, fButtonsAdj,
628 RT_BOOL( mfVMMDevGuestCaps
629 & VMMDEV_MOUSE_NEW_PROTOCOL));
630
631 i_fireMouseEvent(true, x, y, dz, dw, aButtonState);
632 }
633
634 return rc;
635}
636
637/**
638 * Send a multi-touch event. This requires multi-touch pointing device emulation.
639 * @note all calls out of this object are made with no locks held!
640 *
641 * @returns COM status code.
642 * @param aCount Number of contacts.
643 * @param aContacts Information about each contact.
644 * @param aScanTime Timestamp.
645 */
646HRESULT Mouse::putEventMultiTouch(LONG aCount,
647 const std::vector<LONG64> &aContacts,
648 ULONG aScanTime)
649{
650 com::SafeArray <LONG64> arrayContacts(aContacts);
651
652 LogRel3(("%s: aCount %d(actual %d), aScanTime %u\n",
653 __FUNCTION__, aCount, arrayContacts.size(), aScanTime));
654
655 HRESULT rc = S_OK;
656
657 if ((LONG)arrayContacts.size() >= aCount)
658 {
659 LONG64* paContacts = arrayContacts.raw();
660
661 rc = i_putEventMultiTouch(aCount, paContacts, aScanTime);
662 }
663 else
664 {
665 rc = E_INVALIDARG;
666 }
667
668 return rc;
669}
670
671/**
672 * Send a multi-touch event. Version for scripting languages.
673 *
674 * @returns COM status code.
675 * @param aCount Number of contacts.
676 * @param aContacts Information about each contact.
677 * @param aScanTime Timestamp.
678 */
679HRESULT Mouse::putEventMultiTouchString(LONG aCount,
680 const com::Utf8Str &aContacts,
681 ULONG aScanTime)
682{
683 /** @todo implement: convert the string to LONG64 array and call putEventMultiTouch. */
684 NOREF(aCount);
685 NOREF(aContacts);
686 NOREF(aScanTime);
687 return E_NOTIMPL;
688}
689
690
691// private methods
692/////////////////////////////////////////////////////////////////////////////
693
694/* Used by PutEventMultiTouch and PutEventMultiTouchString. */
695HRESULT Mouse::i_putEventMultiTouch(LONG aCount,
696 LONG64 *paContacts,
697 ULONG aScanTime)
698{
699 if (aCount >= 256)
700 {
701 return E_INVALIDARG;
702 }
703
704 DisplayMouseInterface *pDisplay = mParent->i_getDisplayMouseInterface();
705 ComAssertRet(pDisplay, E_FAIL);
706
707 /* Touch events are mapped to the primary monitor, because the emulated USB
708 * touchscreen device is associated with one (normally the primary) screen in the guest.
709 */
710 ULONG uScreenId = 0;
711
712 ULONG cWidth = 0;
713 ULONG cHeight = 0;
714 ULONG cBPP = 0;
715 LONG xOrigin = 0;
716 LONG yOrigin = 0;
717 HRESULT rc = pDisplay->i_getScreenResolution(uScreenId, &cWidth, &cHeight, &cBPP, &xOrigin, &yOrigin);
718 NOREF(cBPP);
719 ComAssertComRCRetRC(rc);
720
721 uint64_t* pau64Contacts = NULL;
722 uint8_t cContacts = 0;
723
724 /* Deliver 0 contacts too, touch device may use this to reset the state. */
725 if (aCount > 0)
726 {
727 /* Create a copy with converted coords. */
728 pau64Contacts = (uint64_t *)RTMemTmpAlloc(aCount * sizeof(uint64_t));
729 if (pau64Contacts)
730 {
731 int32_t x1 = xOrigin;
732 int32_t y1 = yOrigin;
733 int32_t x2 = x1 + cWidth;
734 int32_t y2 = y1 + cHeight;
735
736 LogRel3(("%s: screen [%d] %d,%d %d,%d\n",
737 __FUNCTION__, uScreenId, x1, y1, x2, y2));
738
739 LONG i;
740 for (i = 0; i < aCount; i++)
741 {
742 uint32_t u32Lo = RT_LO_U32(paContacts[i]);
743 uint32_t u32Hi = RT_HI_U32(paContacts[i]);
744 int32_t x = (int16_t)u32Lo;
745 int32_t y = (int16_t)(u32Lo >> 16);
746 uint8_t contactId = RT_BYTE1(u32Hi);
747 bool fInContact = (RT_BYTE2(u32Hi) & 0x1) != 0;
748 bool fInRange = (RT_BYTE2(u32Hi) & 0x2) != 0;
749
750 LogRel3(("%s: [%d] %d,%d id %d, inContact %d, inRange %d\n",
751 __FUNCTION__, i, x, y, contactId, fInContact, fInRange));
752
753 /* x1,y1 are inclusive and x2,y2 are exclusive,
754 * while x,y start from 1 and are inclusive.
755 */
756 if (x <= x1 || x > x2 || y <= y1 || y > y2)
757 {
758 /* Out of range. Skip the contact. */
759 continue;
760 }
761
762 int32_t xAdj = x1 < x2? ((x - 1 - x1) * VMMDEV_MOUSE_RANGE) / (x2 - x1) : 0;
763 int32_t yAdj = y1 < y2? ((y - 1 - y1) * VMMDEV_MOUSE_RANGE) / (y2 - y1) : 0;
764
765 bool fValid = ( xAdj >= VMMDEV_MOUSE_RANGE_MIN
766 && xAdj <= VMMDEV_MOUSE_RANGE_MAX
767 && yAdj >= VMMDEV_MOUSE_RANGE_MIN
768 && yAdj <= VMMDEV_MOUSE_RANGE_MAX);
769
770 if (fValid)
771 {
772 uint8_t fu8 = (fInContact? 0x01: 0x00)
773 | (fInRange? 0x02: 0x00);
774 pau64Contacts[cContacts] = RT_MAKE_U64_FROM_U16((uint16_t)xAdj,
775 (uint16_t)yAdj,
776 RT_MAKE_U16(contactId, fu8),
777 0);
778 cContacts++;
779 }
780 }
781 }
782 else
783 {
784 rc = E_OUTOFMEMORY;
785 }
786 }
787
788 if (SUCCEEDED(rc))
789 {
790 rc = i_reportMultiTouchEventToDevice(cContacts, cContacts? pau64Contacts: NULL, (uint32_t)aScanTime);
791
792 /* Send the original contact information. */
793 i_fireMultiTouchEvent(cContacts, cContacts? paContacts: NULL, (uint32_t)aScanTime);
794 }
795
796 RTMemTmpFree(pau64Contacts);
797
798 return rc;
799}
800
801
802/** Does the guest currently rely on the host to draw the mouse cursor or
803 * can it switch to doing it itself in software? */
804bool Mouse::i_guestNeedsHostCursor(void)
805{
806 return RT_BOOL(mfVMMDevGuestCaps & VMMDEV_MOUSE_GUEST_NEEDS_HOST_CURSOR);
807}
808
809
810/** Check what sort of reporting can be done using the devices currently
811 * enabled. Does not consider the VMM device. */
812void Mouse::i_getDeviceCaps(bool *pfAbs, bool *pfRel, bool *pfMT)
813{
814 bool fAbsDev = false;
815 bool fRelDev = false;
816 bool fMTDev = false;
817
818 AutoReadLock aLock(this COMMA_LOCKVAL_SRC_POS);
819
820 for (unsigned i = 0; i < MOUSE_MAX_DEVICES; ++i)
821 if (mpDrv[i])
822 {
823 if (mpDrv[i]->u32DevCaps & MOUSE_DEVCAP_ABSOLUTE)
824 fAbsDev = true;
825 if (mpDrv[i]->u32DevCaps & MOUSE_DEVCAP_RELATIVE)
826 fRelDev = true;
827 if (mpDrv[i]->u32DevCaps & MOUSE_DEVCAP_MULTI_TOUCH)
828 fMTDev = true;
829 }
830 if (pfAbs)
831 *pfAbs = fAbsDev;
832 if (pfRel)
833 *pfRel = fRelDev;
834 if (pfMT)
835 *pfMT = fMTDev;
836}
837
838
839/** Does the VMM device currently support absolute reporting? */
840bool Mouse::i_vmmdevCanAbs(void)
841{
842 bool fRelDev;
843
844 i_getDeviceCaps(NULL, &fRelDev, NULL);
845 return (mfVMMDevGuestCaps & VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE)
846 && fRelDev;
847}
848
849
850/** Does the VMM device currently support absolute reporting? */
851bool Mouse::i_deviceCanAbs(void)
852{
853 bool fAbsDev;
854
855 i_getDeviceCaps(&fAbsDev, NULL, NULL);
856 return fAbsDev;
857}
858
859
860/** Can we currently send relative events to the guest? */
861bool Mouse::i_supportsRel(void)
862{
863 bool fRelDev;
864
865 i_getDeviceCaps(NULL, &fRelDev, NULL);
866 return fRelDev;
867}
868
869
870/** Can we currently send absolute events to the guest? */
871bool Mouse::i_supportsAbs(void)
872{
873 bool fAbsDev;
874
875 i_getDeviceCaps(&fAbsDev, NULL, NULL);
876 return fAbsDev || i_vmmdevCanAbs();
877}
878
879
880/** Can we currently send absolute events to the guest? */
881bool Mouse::i_supportsMT(void)
882{
883 bool fMTDev;
884
885 i_getDeviceCaps(NULL, NULL, &fMTDev);
886 return fMTDev;
887}
888
889
890/** Check what sort of reporting can be done using the devices currently
891 * enabled (including the VMM device) and notify the guest and the front-end.
892 */
893void Mouse::i_sendMouseCapsNotifications(void)
894{
895 bool fAbsDev, fRelDev, fMTDev, fCanAbs, fNeedsHostCursor;
896
897 {
898 AutoReadLock aLock(this COMMA_LOCKVAL_SRC_POS);
899
900 i_getDeviceCaps(&fAbsDev, &fRelDev, &fMTDev);
901 fCanAbs = i_supportsAbs();
902 fNeedsHostCursor = i_guestNeedsHostCursor();
903 }
904 if (fAbsDev)
905 i_updateVMMDevMouseCaps(VMMDEV_MOUSE_HOST_HAS_ABS_DEV, 0);
906 else
907 i_updateVMMDevMouseCaps(0, VMMDEV_MOUSE_HOST_HAS_ABS_DEV);
908 /** @todo this call takes the Console lock in order to update the cached
909 * callback data atomically. However I can't see any sign that the cached
910 * data is ever used again. */
911 mParent->i_onMouseCapabilityChange(fCanAbs, fRelDev, fMTDev, fNeedsHostCursor);
912}
913
914
915/**
916 * @interface_method_impl{PDMIMOUSECONNECTOR,pfnReportModes}
917 * A virtual device is notifying us about its current state and capabilities
918 */
919DECLCALLBACK(void) Mouse::i_mouseReportModes(PPDMIMOUSECONNECTOR pInterface, bool fRel, bool fAbs, bool fMT)
920{
921 PDRVMAINMOUSE pDrv = RT_FROM_MEMBER(pInterface, DRVMAINMOUSE, IConnector);
922 if (fRel)
923 pDrv->u32DevCaps |= MOUSE_DEVCAP_RELATIVE;
924 else
925 pDrv->u32DevCaps &= ~MOUSE_DEVCAP_RELATIVE;
926 if (fAbs)
927 pDrv->u32DevCaps |= MOUSE_DEVCAP_ABSOLUTE;
928 else
929 pDrv->u32DevCaps &= ~MOUSE_DEVCAP_ABSOLUTE;
930 if (fMT)
931 pDrv->u32DevCaps |= MOUSE_DEVCAP_MULTI_TOUCH;
932 else
933 pDrv->u32DevCaps &= ~MOUSE_DEVCAP_MULTI_TOUCH;
934
935 pDrv->pMouse->i_sendMouseCapsNotifications();
936}
937
938
939/**
940 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
941 */
942DECLCALLBACK(void *) Mouse::i_drvQueryInterface(PPDMIBASE pInterface, const char *pszIID)
943{
944 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
945 PDRVMAINMOUSE pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
946
947 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
948 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUSECONNECTOR, &pDrv->IConnector);
949 return NULL;
950}
951
952
953/**
954 * Destruct a mouse driver instance.
955 *
956 * @returns VBox status.
957 * @param pDrvIns The driver instance data.
958 */
959DECLCALLBACK(void) Mouse::i_drvDestruct(PPDMDRVINS pDrvIns)
960{
961 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
962 PDRVMAINMOUSE pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
963 LogFlow(("Mouse::drvDestruct: iInstance=%d\n", pDrvIns->iInstance));
964
965 if (pThis->pMouse)
966 {
967 AutoWriteLock mouseLock(pThis->pMouse COMMA_LOCKVAL_SRC_POS);
968 for (unsigned cDev = 0; cDev < MOUSE_MAX_DEVICES; ++cDev)
969 if (pThis->pMouse->mpDrv[cDev] == pThis)
970 {
971 pThis->pMouse->mpDrv[cDev] = NULL;
972 break;
973 }
974 }
975}
976
977
978/**
979 * Construct a mouse driver instance.
980 *
981 * @copydoc FNPDMDRVCONSTRUCT
982 */
983DECLCALLBACK(int) Mouse::i_drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
984{
985 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
986 PDRVMAINMOUSE pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
987 LogFlow(("drvMainMouse_Construct: iInstance=%d\n", pDrvIns->iInstance));
988
989 /*
990 * Validate configuration.
991 */
992 if (!CFGMR3AreValuesValid(pCfg, "Object\0"))
993 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
994 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
995 ("Configuration error: Not possible to attach anything to this driver!\n"),
996 VERR_PDM_DRVINS_NO_ATTACH);
997
998 /*
999 * IBase.
1000 */
1001 pDrvIns->IBase.pfnQueryInterface = Mouse::i_drvQueryInterface;
1002
1003 pThis->IConnector.pfnReportModes = Mouse::i_mouseReportModes;
1004
1005 /*
1006 * Get the IMousePort interface of the above driver/device.
1007 */
1008 pThis->pUpPort = (PPDMIMOUSEPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMIMOUSEPORT_IID);
1009 if (!pThis->pUpPort)
1010 {
1011 AssertMsgFailed(("Configuration error: No mouse port interface above!\n"));
1012 return VERR_PDM_MISSING_INTERFACE_ABOVE;
1013 }
1014
1015 /*
1016 * Get the Mouse object pointer and update the mpDrv member.
1017 */
1018 void *pv;
1019 int rc = CFGMR3QueryPtr(pCfg, "Object", &pv);
1020 if (RT_FAILURE(rc))
1021 {
1022 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
1023 return rc;
1024 }
1025 pThis->pMouse = (Mouse *)pv; /** @todo Check this cast! */
1026 unsigned cDev;
1027 {
1028 AutoReadLock mouseLock(pThis->pMouse COMMA_LOCKVAL_SRC_POS);
1029
1030 for (cDev = 0; cDev < MOUSE_MAX_DEVICES; ++cDev)
1031 if (!pThis->pMouse->mpDrv[cDev])
1032 {
1033 pThis->pMouse->mpDrv[cDev] = pThis;
1034 break;
1035 }
1036 }
1037 if (cDev == MOUSE_MAX_DEVICES)
1038 return VERR_NO_MORE_HANDLES;
1039
1040 return VINF_SUCCESS;
1041}
1042
1043
1044/**
1045 * Main mouse driver registration record.
1046 */
1047const PDMDRVREG Mouse::DrvReg =
1048{
1049 /* u32Version */
1050 PDM_DRVREG_VERSION,
1051 /* szName */
1052 "MainMouse",
1053 /* szRCMod */
1054 "",
1055 /* szR0Mod */
1056 "",
1057 /* pszDescription */
1058 "Main mouse driver (Main as in the API).",
1059 /* fFlags */
1060 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1061 /* fClass. */
1062 PDM_DRVREG_CLASS_MOUSE,
1063 /* cMaxInstances */
1064 ~0U,
1065 /* cbInstance */
1066 sizeof(DRVMAINMOUSE),
1067 /* pfnConstruct */
1068 Mouse::i_drvConstruct,
1069 /* pfnDestruct */
1070 Mouse::i_drvDestruct,
1071 /* pfnRelocate */
1072 NULL,
1073 /* pfnIOCtl */
1074 NULL,
1075 /* pfnPowerOn */
1076 NULL,
1077 /* pfnReset */
1078 NULL,
1079 /* pfnSuspend */
1080 NULL,
1081 /* pfnResume */
1082 NULL,
1083 /* pfnAttach */
1084 NULL,
1085 /* pfnDetach */
1086 NULL,
1087 /* pfnPowerOff */
1088 NULL,
1089 /* pfnSoftReset */
1090 NULL,
1091 /* u32EndVersion */
1092 PDM_DRVREG_VERSION
1093};
1094/* 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