1 | /* $Id: MouseImpl.cpp 44528 2013-02-04 14:27:54Z 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 | * @{ */
|
---|
34 | enum
|
---|
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 | };
|
---|
41 | /** @} */
|
---|
42 |
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Mouse driver instance data.
|
---|
46 | */
|
---|
47 | struct DRVMAINMOUSE
|
---|
48 | {
|
---|
49 | /** Pointer to the mouse object. */
|
---|
50 | Mouse *pMouse;
|
---|
51 | /** Pointer to the driver instance structure. */
|
---|
52 | PPDMDRVINS pDrvIns;
|
---|
53 | /** Pointer to the mouse port interface of the driver/device above us. */
|
---|
54 | PPDMIMOUSEPORT pUpPort;
|
---|
55 | /** Our mouse connector interface. */
|
---|
56 | PDMIMOUSECONNECTOR IConnector;
|
---|
57 | /** The capabilities of this device. */
|
---|
58 | uint32_t u32DevCaps;
|
---|
59 | };
|
---|
60 |
|
---|
61 |
|
---|
62 | // constructor / destructor
|
---|
63 | /////////////////////////////////////////////////////////////////////////////
|
---|
64 |
|
---|
65 | Mouse::Mouse()
|
---|
66 | : mParent(NULL)
|
---|
67 | {
|
---|
68 | }
|
---|
69 |
|
---|
70 | Mouse::~Mouse()
|
---|
71 | {
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | HRESULT Mouse::FinalConstruct()
|
---|
76 | {
|
---|
77 | RT_ZERO(mpDrv);
|
---|
78 | mcLastAbsX = 0x8000;
|
---|
79 | mcLastAbsY = 0x8000;
|
---|
80 | mfLastButtons = 0;
|
---|
81 | mfVMMDevGuestCaps = 0;
|
---|
82 | return BaseFinalConstruct();
|
---|
83 | }
|
---|
84 |
|
---|
85 | void Mouse::FinalRelease()
|
---|
86 | {
|
---|
87 | uninit();
|
---|
88 | BaseFinalRelease();
|
---|
89 | }
|
---|
90 |
|
---|
91 | // public methods only for internal purposes
|
---|
92 | /////////////////////////////////////////////////////////////////////////////
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Initializes the mouse object.
|
---|
96 | *
|
---|
97 | * @returns COM result indicator
|
---|
98 | * @param parent handle of our parent object
|
---|
99 | */
|
---|
100 | HRESULT Mouse::init (Console *parent)
|
---|
101 | {
|
---|
102 | LogFlowThisFunc(("\n"));
|
---|
103 |
|
---|
104 | ComAssertRet(parent, E_INVALIDARG);
|
---|
105 |
|
---|
106 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
107 | AutoInitSpan autoInitSpan(this);
|
---|
108 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
109 |
|
---|
110 | unconst(mParent) = parent;
|
---|
111 |
|
---|
112 | #ifndef VBOXBFE_WITHOUT_COM
|
---|
113 | unconst(mEventSource).createObject();
|
---|
114 | HRESULT rc = mEventSource->init(static_cast<IMouse*>(this));
|
---|
115 | AssertComRCReturnRC(rc);
|
---|
116 | mMouseEvent.init(mEventSource, VBoxEventType_OnGuestMouse,
|
---|
117 | 0, 0, 0, 0, 0);
|
---|
118 | #endif
|
---|
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 | */
|
---|
130 | void 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 | #ifdef VBOXBFE_WITHOUT_COM
|
---|
147 | mParent = NULL;
|
---|
148 | #else
|
---|
149 | mMouseEvent.uninit();
|
---|
150 | unconst(mEventSource).setNull();
|
---|
151 | unconst(mParent) = NULL;
|
---|
152 | #endif
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | // IMouse properties
|
---|
157 | /////////////////////////////////////////////////////////////////////////////
|
---|
158 |
|
---|
159 | /** Report the front-end's mouse handling capabilities to the VMM device and
|
---|
160 | * thus to the guest.
|
---|
161 | * @note all calls out of this object are made with no locks held! */
|
---|
162 | HRESULT Mouse::updateVMMDevMouseCaps(uint32_t fCapsAdded,
|
---|
163 | uint32_t fCapsRemoved)
|
---|
164 | {
|
---|
165 | VMMDev *pVMMDev = mParent->getVMMDev();
|
---|
166 | if (!pVMMDev)
|
---|
167 | return E_FAIL; /* No assertion, as the front-ends can send events
|
---|
168 | * at all sorts of inconvenient times. */
|
---|
169 | PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
|
---|
170 | if (!pVMMDevPort)
|
---|
171 | return E_FAIL; /* same here */
|
---|
172 |
|
---|
173 | int rc = pVMMDevPort->pfnUpdateMouseCapabilities(pVMMDevPort, fCapsAdded,
|
---|
174 | fCapsRemoved);
|
---|
175 | return RT_SUCCESS(rc) ? S_OK : E_FAIL;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Returns whether the current setup can accept absolute mouse events, either
|
---|
180 | * because an emulated absolute pointing device is active or because the Guest
|
---|
181 | * Additions are.
|
---|
182 | *
|
---|
183 | * @returns COM status code
|
---|
184 | * @param absoluteSupported address of result variable
|
---|
185 | */
|
---|
186 | STDMETHODIMP Mouse::COMGETTER(AbsoluteSupported) (BOOL *absoluteSupported)
|
---|
187 | {
|
---|
188 | if (!absoluteSupported)
|
---|
189 | return E_POINTER;
|
---|
190 |
|
---|
191 | AutoCaller autoCaller(this);
|
---|
192 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
193 |
|
---|
194 | *absoluteSupported = supportsAbs();
|
---|
195 | return S_OK;
|
---|
196 | }
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Returns whether the current setup can accept relative mouse events, that is,
|
---|
200 | * whether an emulated relative pointing device is active.
|
---|
201 | *
|
---|
202 | * @returns COM status code
|
---|
203 | * @param relativeSupported address of result variable
|
---|
204 | */
|
---|
205 | STDMETHODIMP Mouse::COMGETTER(RelativeSupported) (BOOL *relativeSupported)
|
---|
206 | {
|
---|
207 | if (!relativeSupported)
|
---|
208 | return E_POINTER;
|
---|
209 |
|
---|
210 | AutoCaller autoCaller(this);
|
---|
211 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
212 |
|
---|
213 | *relativeSupported = supportsRel();
|
---|
214 | return S_OK;
|
---|
215 | }
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Returns whether the guest can currently switch to drawing the mouse cursor
|
---|
219 | * itself if it is asked to by the front-end.
|
---|
220 | *
|
---|
221 | * @returns COM status code
|
---|
222 | * @param pfNeedsHostCursor address of result variable
|
---|
223 | */
|
---|
224 | STDMETHODIMP Mouse::COMGETTER(NeedsHostCursor) (BOOL *pfNeedsHostCursor)
|
---|
225 | {
|
---|
226 | if (!pfNeedsHostCursor)
|
---|
227 | return E_POINTER;
|
---|
228 |
|
---|
229 | AutoCaller autoCaller(this);
|
---|
230 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
231 |
|
---|
232 | *pfNeedsHostCursor = guestNeedsHostCursor();
|
---|
233 | return S_OK;
|
---|
234 | }
|
---|
235 |
|
---|
236 | // IMouse methods
|
---|
237 | /////////////////////////////////////////////////////////////////////////////
|
---|
238 |
|
---|
239 | /** Converts a bitfield containing information about mouse buttons currently
|
---|
240 | * held down from the format used by the front-end to the format used by PDM
|
---|
241 | * and the emulated pointing devices. */
|
---|
242 | static uint32_t mouseButtonsToPDM(LONG buttonState)
|
---|
243 | {
|
---|
244 | uint32_t fButtons = 0;
|
---|
245 | if (buttonState & MouseButtonState_LeftButton)
|
---|
246 | fButtons |= PDMIMOUSEPORT_BUTTON_LEFT;
|
---|
247 | if (buttonState & MouseButtonState_RightButton)
|
---|
248 | fButtons |= PDMIMOUSEPORT_BUTTON_RIGHT;
|
---|
249 | if (buttonState & MouseButtonState_MiddleButton)
|
---|
250 | fButtons |= PDMIMOUSEPORT_BUTTON_MIDDLE;
|
---|
251 | if (buttonState & MouseButtonState_XButton1)
|
---|
252 | fButtons |= PDMIMOUSEPORT_BUTTON_X1;
|
---|
253 | if (buttonState & MouseButtonState_XButton2)
|
---|
254 | fButtons |= PDMIMOUSEPORT_BUTTON_X2;
|
---|
255 | return fButtons;
|
---|
256 | }
|
---|
257 |
|
---|
258 | #ifndef VBOXBFE_WITHOUT_COM
|
---|
259 | STDMETHODIMP Mouse::COMGETTER(EventSource)(IEventSource ** aEventSource)
|
---|
260 | {
|
---|
261 | CheckComArgOutPointerValid(aEventSource);
|
---|
262 |
|
---|
263 | AutoCaller autoCaller(this);
|
---|
264 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
265 |
|
---|
266 | // no need to lock - lifetime constant
|
---|
267 | mEventSource.queryInterfaceTo(aEventSource);
|
---|
268 |
|
---|
269 | return S_OK;
|
---|
270 | }
|
---|
271 | #endif
|
---|
272 |
|
---|
273 | /**
|
---|
274 | * Send a relative pointer event to the relative device we deem most
|
---|
275 | * appropriate.
|
---|
276 | *
|
---|
277 | * @returns COM status code
|
---|
278 | */
|
---|
279 | HRESULT Mouse::reportRelEventToMouseDev(int32_t dx, int32_t dy, int32_t dz,
|
---|
280 | int32_t dw, uint32_t fButtons)
|
---|
281 | {
|
---|
282 | if (dx || dy || dz || dw || fButtons != mfLastButtons)
|
---|
283 | {
|
---|
284 | PPDMIMOUSEPORT pUpPort = NULL;
|
---|
285 | {
|
---|
286 | AutoReadLock aLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
287 |
|
---|
288 | for (unsigned i = 0; !pUpPort && i < MOUSE_MAX_DEVICES; ++i)
|
---|
289 | {
|
---|
290 | if (mpDrv[i] && (mpDrv[i]->u32DevCaps & MOUSE_DEVCAP_RELATIVE))
|
---|
291 | pUpPort = mpDrv[i]->pUpPort;
|
---|
292 | }
|
---|
293 | }
|
---|
294 | if (!pUpPort)
|
---|
295 | return S_OK;
|
---|
296 |
|
---|
297 | int vrc = pUpPort->pfnPutEvent(pUpPort, dx, dy, dz, dw, fButtons);
|
---|
298 |
|
---|
299 | if (RT_FAILURE(vrc))
|
---|
300 | return setError(VBOX_E_IPRT_ERROR,
|
---|
301 | tr("Could not send the mouse event to the virtual mouse (%Rrc)"),
|
---|
302 | vrc);
|
---|
303 | mfLastButtons = fButtons;
|
---|
304 | }
|
---|
305 | return S_OK;
|
---|
306 | }
|
---|
307 |
|
---|
308 |
|
---|
309 | /**
|
---|
310 | * Send an absolute pointer event to the emulated absolute device we deem most
|
---|
311 | * appropriate.
|
---|
312 | *
|
---|
313 | * @returns COM status code
|
---|
314 | */
|
---|
315 | HRESULT Mouse::reportAbsEventToMouseDev(int32_t mouseXAbs, int32_t mouseYAbs,
|
---|
316 | int32_t dz, int32_t dw, uint32_t fButtons)
|
---|
317 | {
|
---|
318 | if ( mouseXAbs < VMMDEV_MOUSE_RANGE_MIN
|
---|
319 | || mouseXAbs > VMMDEV_MOUSE_RANGE_MAX)
|
---|
320 | return S_OK;
|
---|
321 | if ( mouseYAbs < VMMDEV_MOUSE_RANGE_MIN
|
---|
322 | || mouseYAbs > VMMDEV_MOUSE_RANGE_MAX)
|
---|
323 | return S_OK;
|
---|
324 | if ( mouseXAbs != mcLastAbsX || mouseYAbs != mcLastAbsY
|
---|
325 | || dz || dw || fButtons != mfLastButtons)
|
---|
326 | {
|
---|
327 | PPDMIMOUSEPORT pUpPort = NULL;
|
---|
328 | {
|
---|
329 | AutoReadLock aLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
330 |
|
---|
331 | for (unsigned i = 0; !pUpPort && i < MOUSE_MAX_DEVICES; ++i)
|
---|
332 | {
|
---|
333 | if (mpDrv[i] && (mpDrv[i]->u32DevCaps & MOUSE_DEVCAP_ABSOLUTE))
|
---|
334 | pUpPort = mpDrv[i]->pUpPort;
|
---|
335 | }
|
---|
336 | }
|
---|
337 | if (!pUpPort)
|
---|
338 | return S_OK;
|
---|
339 |
|
---|
340 | int vrc = pUpPort->pfnPutEventAbs(pUpPort, mouseXAbs, mouseYAbs, dz,
|
---|
341 | dw, fButtons);
|
---|
342 | if (RT_FAILURE(vrc))
|
---|
343 | return setError(VBOX_E_IPRT_ERROR,
|
---|
344 | tr("Could not send the mouse event to the virtual mouse (%Rrc)"),
|
---|
345 | vrc);
|
---|
346 | mfLastButtons = fButtons;
|
---|
347 |
|
---|
348 | }
|
---|
349 | return S_OK;
|
---|
350 | }
|
---|
351 |
|
---|
352 |
|
---|
353 | /**
|
---|
354 | * Send an absolute position event to the VMM device.
|
---|
355 | * @note all calls out of this object are made with no locks held!
|
---|
356 | *
|
---|
357 | * @returns COM status code
|
---|
358 | */
|
---|
359 | HRESULT Mouse::reportAbsEventToVMMDev(int32_t mouseXAbs, int32_t mouseYAbs)
|
---|
360 | {
|
---|
361 | VMMDev *pVMMDev = mParent->getVMMDev();
|
---|
362 | ComAssertRet(pVMMDev, E_FAIL);
|
---|
363 | PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
|
---|
364 | ComAssertRet(pVMMDevPort, E_FAIL);
|
---|
365 |
|
---|
366 | if (mouseXAbs != mcLastAbsX || mouseYAbs != mcLastAbsY)
|
---|
367 | {
|
---|
368 | int vrc = pVMMDevPort->pfnSetAbsoluteMouse(pVMMDevPort,
|
---|
369 | mouseXAbs, mouseYAbs);
|
---|
370 | if (RT_FAILURE(vrc))
|
---|
371 | return setError(VBOX_E_IPRT_ERROR,
|
---|
372 | tr("Could not send the mouse event to the virtual mouse (%Rrc)"),
|
---|
373 | vrc);
|
---|
374 | }
|
---|
375 | return S_OK;
|
---|
376 | }
|
---|
377 |
|
---|
378 |
|
---|
379 | /**
|
---|
380 | * Send an absolute pointer event to a pointing device (the VMM device if
|
---|
381 | * possible or whatever emulated absolute device seems best to us if not).
|
---|
382 | *
|
---|
383 | * @returns COM status code
|
---|
384 | */
|
---|
385 | HRESULT Mouse::reportAbsEvent(int32_t mouseXAbs, int32_t mouseYAbs,
|
---|
386 | int32_t dz, int32_t dw, uint32_t fButtons,
|
---|
387 | bool fUsesVMMDevEvent)
|
---|
388 | {
|
---|
389 | HRESULT rc;
|
---|
390 | /** If we are using the VMMDev to report absolute position but without
|
---|
391 | * VMMDev IRQ support then we need to send a small "jiggle" to the emulated
|
---|
392 | * relative mouse device to alert the guest to changes. */
|
---|
393 | LONG cJiggle = 0;
|
---|
394 |
|
---|
395 | if (vmmdevCanAbs())
|
---|
396 | {
|
---|
397 | /*
|
---|
398 | * Send the absolute mouse position to the VMM device.
|
---|
399 | */
|
---|
400 | if (mouseXAbs != mcLastAbsX || mouseYAbs != mcLastAbsY)
|
---|
401 | {
|
---|
402 | rc = reportAbsEventToVMMDev(mouseXAbs, mouseYAbs);
|
---|
403 | cJiggle = !fUsesVMMDevEvent;
|
---|
404 | }
|
---|
405 | rc = reportRelEventToMouseDev(cJiggle, 0, dz, dw, fButtons);
|
---|
406 | }
|
---|
407 | else
|
---|
408 | rc = reportAbsEventToMouseDev(mouseXAbs, mouseYAbs, dz, dw, fButtons);
|
---|
409 |
|
---|
410 | mcLastAbsX = mouseXAbs;
|
---|
411 | mcLastAbsY = mouseYAbs;
|
---|
412 | return rc;
|
---|
413 | }
|
---|
414 |
|
---|
415 | #ifndef VBOXBFE_WITHOUT_COM
|
---|
416 | void Mouse::fireMouseEvent(bool fAbsolute, LONG x, LONG y, LONG dz, LONG dw, LONG Buttons)
|
---|
417 | {
|
---|
418 | /* If mouse button is pressed, we generate new event, to avoid reusable events coalescing and thus
|
---|
419 | dropping key press events */
|
---|
420 | if (Buttons != 0)
|
---|
421 | {
|
---|
422 | VBoxEventDesc evDesc;
|
---|
423 | evDesc.init(mEventSource, VBoxEventType_OnGuestMouse, fAbsolute, x, y, dz, dw, Buttons);
|
---|
424 | evDesc.fire(0);
|
---|
425 | }
|
---|
426 | else
|
---|
427 | {
|
---|
428 | mMouseEvent.reinit(VBoxEventType_OnGuestMouse, fAbsolute, x, y, dz, dw, Buttons);
|
---|
429 | mMouseEvent.fire(0);
|
---|
430 | }
|
---|
431 | }
|
---|
432 | #endif
|
---|
433 |
|
---|
434 |
|
---|
435 | /**
|
---|
436 | * Send a relative mouse event to the guest.
|
---|
437 | * @note the VMMDev capability change is so that the guest knows we are sending
|
---|
438 | * real events over the PS/2 device and not dummy events to signal the
|
---|
439 | * arrival of new absolute pointer data
|
---|
440 | *
|
---|
441 | * @returns COM status code
|
---|
442 | * @param dx X movement
|
---|
443 | * @param dy Y movement
|
---|
444 | * @param dz Z movement
|
---|
445 | * @param buttonState The mouse button state
|
---|
446 | */
|
---|
447 | STDMETHODIMP Mouse::PutMouseEvent(LONG dx, LONG dy, LONG dz, LONG dw, LONG buttonState)
|
---|
448 | {
|
---|
449 | HRESULT rc;
|
---|
450 | uint32_t fButtons;
|
---|
451 |
|
---|
452 | AutoCaller autoCaller(this);
|
---|
453 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
454 | LogRel3(("%s: dx=%d, dy=%d, dz=%d, dw=%d\n", __PRETTY_FUNCTION__,
|
---|
455 | dx, dy, dz, dw));
|
---|
456 |
|
---|
457 | fButtons = mouseButtonsToPDM(buttonState);
|
---|
458 | /* Make sure that the guest knows that we are sending real movement
|
---|
459 | * events to the PS/2 device and not just dummy wake-up ones. */
|
---|
460 | updateVMMDevMouseCaps(0, VMMDEV_MOUSE_HOST_WANTS_ABSOLUTE);
|
---|
461 | rc = reportRelEventToMouseDev(dx, dy, dz, dw, fButtons);
|
---|
462 |
|
---|
463 | fireMouseEvent(false, dx, dy, dz, dw, buttonState);
|
---|
464 |
|
---|
465 | return rc;
|
---|
466 | }
|
---|
467 |
|
---|
468 | /**
|
---|
469 | * Convert an (X, Y) value pair in screen co-ordinates (starting from 1) to a
|
---|
470 | * value from VMMDEV_MOUSE_RANGE_MIN to VMMDEV_MOUSE_RANGE_MAX. Sets the
|
---|
471 | * optional validity value to false if the pair is not on an active screen and
|
---|
472 | * to true otherwise.
|
---|
473 | * @note since guests with recent versions of X.Org use a different method
|
---|
474 | * to everyone else to map the valuator value to a screen pixel (they
|
---|
475 | * multiply by the screen dimension, do a floating point divide by
|
---|
476 | * the valuator maximum and round the result, while everyone else
|
---|
477 | * does truncating integer operations) we adjust the value we send
|
---|
478 | * so that it maps to the right pixel both when the result is rounded
|
---|
479 | * and when it is truncated.
|
---|
480 | *
|
---|
481 | * @returns COM status value
|
---|
482 | */
|
---|
483 | HRESULT Mouse::convertDisplayRes(LONG x, LONG y, int32_t *pcX, int32_t *pcY,
|
---|
484 | bool *pfValid)
|
---|
485 | {
|
---|
486 | AssertPtrReturn(pcX, E_POINTER);
|
---|
487 | AssertPtrReturn(pcY, E_POINTER);
|
---|
488 | AssertPtrNullReturn(pfValid, E_POINTER);
|
---|
489 | Display *pDisplay = mParent->getDisplay();
|
---|
490 | ComAssertRet(pDisplay, E_FAIL);
|
---|
491 | /** The amount to add to the result (multiplied by the screen width/height)
|
---|
492 | * to compensate for differences in guest methods for mapping back to
|
---|
493 | * pixels */
|
---|
494 | enum { ADJUST_RANGE = - 3 * VMMDEV_MOUSE_RANGE / 4 };
|
---|
495 |
|
---|
496 | if (pfValid)
|
---|
497 | *pfValid = true;
|
---|
498 | if (!(mfVMMDevGuestCaps & VMMDEV_MOUSE_NEW_PROTOCOL))
|
---|
499 | {
|
---|
500 | ULONG displayWidth, displayHeight;
|
---|
501 | /* Takes the display lock */
|
---|
502 | HRESULT rc = pDisplay->GetScreenResolution(0, &displayWidth,
|
---|
503 | &displayHeight, NULL);
|
---|
504 | if (FAILED(rc))
|
---|
505 | return rc;
|
---|
506 |
|
---|
507 | *pcX = displayWidth ? (x * VMMDEV_MOUSE_RANGE + ADJUST_RANGE)
|
---|
508 | / (LONG) displayWidth: 0;
|
---|
509 | *pcY = displayHeight ? (y * VMMDEV_MOUSE_RANGE + ADJUST_RANGE)
|
---|
510 | / (LONG) displayHeight: 0;
|
---|
511 | }
|
---|
512 | else
|
---|
513 | {
|
---|
514 | int32_t x1, y1, x2, y2;
|
---|
515 | /* Takes the display lock */
|
---|
516 | pDisplay->getFramebufferDimensions(&x1, &y1, &x2, &y2);
|
---|
517 | *pcX = x1 < x2 ? ((x - x1) * VMMDEV_MOUSE_RANGE + ADJUST_RANGE)
|
---|
518 | / (x2 - x1) : 0;
|
---|
519 | *pcY = y1 < y2 ? ((y - y1) * VMMDEV_MOUSE_RANGE + ADJUST_RANGE)
|
---|
520 | / (y2 - y1) : 0;
|
---|
521 | if ( *pcX < VMMDEV_MOUSE_RANGE_MIN || *pcX > VMMDEV_MOUSE_RANGE_MAX
|
---|
522 | || *pcY < VMMDEV_MOUSE_RANGE_MIN || *pcY > VMMDEV_MOUSE_RANGE_MAX)
|
---|
523 | if (pfValid)
|
---|
524 | *pfValid = false;
|
---|
525 | }
|
---|
526 | return S_OK;
|
---|
527 | }
|
---|
528 |
|
---|
529 |
|
---|
530 | /**
|
---|
531 | * Send an absolute mouse event to the VM. This requires either VirtualBox-
|
---|
532 | * specific drivers installed in the guest or absolute pointing device
|
---|
533 | * emulation.
|
---|
534 | * @note the VMMDev capability change is so that the guest knows we are sending
|
---|
535 | * dummy events over the PS/2 device to signal the arrival of new
|
---|
536 | * absolute pointer data, and not pointer real movement data
|
---|
537 | * @note all calls out of this object are made with no locks held!
|
---|
538 | *
|
---|
539 | * @returns COM status code
|
---|
540 | * @param x X position (pixel), starting from 1
|
---|
541 | * @param y Y position (pixel), starting from 1
|
---|
542 | * @param dz Z movement
|
---|
543 | * @param buttonState The mouse button state
|
---|
544 | */
|
---|
545 | STDMETHODIMP Mouse::PutMouseEventAbsolute(LONG x, LONG y, LONG dz, LONG dw,
|
---|
546 | LONG buttonState)
|
---|
547 | {
|
---|
548 | AutoCaller autoCaller(this);
|
---|
549 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
550 |
|
---|
551 | LogRel3(("%s: x=%d, y=%d, dz=%d, dw=%d, buttonState=0x%x\n",
|
---|
552 | __PRETTY_FUNCTION__, x, y, dz, dw, buttonState));
|
---|
553 |
|
---|
554 | int32_t mouseXAbs, mouseYAbs;
|
---|
555 | uint32_t fButtons;
|
---|
556 | bool fValid;
|
---|
557 |
|
---|
558 | /** @todo the front end should do this conversion to avoid races */
|
---|
559 | /** @note Or maybe not... races are pretty inherent in everything done in
|
---|
560 | * this object and not really bad as far as I can see. */
|
---|
561 | HRESULT rc = convertDisplayRes(x, y, &mouseXAbs, &mouseYAbs, &fValid);
|
---|
562 | if (FAILED(rc)) return rc;
|
---|
563 |
|
---|
564 | fButtons = mouseButtonsToPDM(buttonState);
|
---|
565 | /* If we are doing old-style (IRQ-less) absolute reporting to the VMM
|
---|
566 | * device then make sure the guest is aware of it, so that it knows to
|
---|
567 | * ignore relative movement on the PS/2 device. */
|
---|
568 | updateVMMDevMouseCaps(VMMDEV_MOUSE_HOST_WANTS_ABSOLUTE, 0);
|
---|
569 | if (fValid)
|
---|
570 | {
|
---|
571 | rc = reportAbsEvent(mouseXAbs, mouseYAbs, dz, dw, fButtons,
|
---|
572 | RT_BOOL( mfVMMDevGuestCaps
|
---|
573 | & VMMDEV_MOUSE_NEW_PROTOCOL));
|
---|
574 |
|
---|
575 | fireMouseEvent(true, x, y, dz, dw, buttonState);
|
---|
576 | }
|
---|
577 |
|
---|
578 | return rc;
|
---|
579 | }
|
---|
580 |
|
---|
581 | // private methods
|
---|
582 | /////////////////////////////////////////////////////////////////////////////
|
---|
583 |
|
---|
584 |
|
---|
585 | /** Does the guest currently rely on the host to draw the mouse cursor or
|
---|
586 | * can it switch to doing it itself in software? */
|
---|
587 | bool Mouse::guestNeedsHostCursor(void)
|
---|
588 | {
|
---|
589 | return RT_BOOL(mfVMMDevGuestCaps & VMMDEV_MOUSE_GUEST_NEEDS_HOST_CURSOR);
|
---|
590 | }
|
---|
591 |
|
---|
592 |
|
---|
593 | /** Check what sort of reporting can be done using the devices currently
|
---|
594 | * enabled. Does not consider the VMM device. */
|
---|
595 | void Mouse::getDeviceCaps(bool *pfAbs, bool *pfRel)
|
---|
596 | {
|
---|
597 | bool fAbsDev = false;
|
---|
598 | bool fRelDev = false;
|
---|
599 |
|
---|
600 | AutoReadLock aLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
601 |
|
---|
602 | for (unsigned i = 0; i < MOUSE_MAX_DEVICES; ++i)
|
---|
603 | if (mpDrv[i])
|
---|
604 | {
|
---|
605 | if (mpDrv[i]->u32DevCaps & MOUSE_DEVCAP_ABSOLUTE)
|
---|
606 | fAbsDev = true;
|
---|
607 | if (mpDrv[i]->u32DevCaps & MOUSE_DEVCAP_RELATIVE)
|
---|
608 | fRelDev = true;
|
---|
609 | }
|
---|
610 | if (pfAbs)
|
---|
611 | *pfAbs = fAbsDev;
|
---|
612 | if (pfRel)
|
---|
613 | *pfRel = fRelDev;
|
---|
614 | }
|
---|
615 |
|
---|
616 |
|
---|
617 | /** Does the VMM device currently support absolute reporting? */
|
---|
618 | bool Mouse::vmmdevCanAbs(void)
|
---|
619 | {
|
---|
620 | bool fRelDev;
|
---|
621 |
|
---|
622 | getDeviceCaps(NULL, &fRelDev);
|
---|
623 | return (mfVMMDevGuestCaps & VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE)
|
---|
624 | && fRelDev;
|
---|
625 | }
|
---|
626 |
|
---|
627 |
|
---|
628 | /** Does the VMM device currently support absolute reporting? */
|
---|
629 | bool Mouse::deviceCanAbs(void)
|
---|
630 | {
|
---|
631 | bool fAbsDev;
|
---|
632 |
|
---|
633 | getDeviceCaps(&fAbsDev, NULL);
|
---|
634 | return fAbsDev;
|
---|
635 | }
|
---|
636 |
|
---|
637 |
|
---|
638 | /** Can we currently send relative events to the guest? */
|
---|
639 | bool Mouse::supportsRel(void)
|
---|
640 | {
|
---|
641 | bool fRelDev;
|
---|
642 |
|
---|
643 | getDeviceCaps(NULL, &fRelDev);
|
---|
644 | return fRelDev;
|
---|
645 | }
|
---|
646 |
|
---|
647 |
|
---|
648 | /** Can we currently send absolute events to the guest? */
|
---|
649 | bool Mouse::supportsAbs(void)
|
---|
650 | {
|
---|
651 | bool fAbsDev;
|
---|
652 |
|
---|
653 | getDeviceCaps(&fAbsDev, NULL);
|
---|
654 | return fAbsDev || vmmdevCanAbs();
|
---|
655 | }
|
---|
656 |
|
---|
657 |
|
---|
658 | /** Check what sort of reporting can be done using the devices currently
|
---|
659 | * enabled (including the VMM device) and notify the guest and the front-end.
|
---|
660 | */
|
---|
661 | void Mouse::sendMouseCapsNotifications(void)
|
---|
662 | {
|
---|
663 | bool fAbsDev, fRelDev, fCanAbs, fNeedsHostCursor;
|
---|
664 |
|
---|
665 | {
|
---|
666 | AutoReadLock aLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
667 |
|
---|
668 | getDeviceCaps(&fAbsDev, &fRelDev);
|
---|
669 | fCanAbs = supportsAbs();
|
---|
670 | fNeedsHostCursor = guestNeedsHostCursor();
|
---|
671 | }
|
---|
672 | if (fAbsDev)
|
---|
673 | updateVMMDevMouseCaps(VMMDEV_MOUSE_HOST_HAS_ABS_DEV, 0);
|
---|
674 | else
|
---|
675 | updateVMMDevMouseCaps(0, VMMDEV_MOUSE_HOST_HAS_ABS_DEV);
|
---|
676 | /** @todo this call takes the Console lock in order to update the cached
|
---|
677 | * callback data atomically. However I can't see any sign that the cached
|
---|
678 | * data is ever used again. */
|
---|
679 | mParent->onMouseCapabilityChange(fCanAbs, fRelDev, fNeedsHostCursor);
|
---|
680 | }
|
---|
681 |
|
---|
682 |
|
---|
683 | /**
|
---|
684 | * @interface_method_impl{PDMIMOUSECONNECTOR,pfnReportModes}
|
---|
685 | * A virtual device is notifying us about its current state and capabilities
|
---|
686 | */
|
---|
687 | DECLCALLBACK(void) Mouse::mouseReportModes(PPDMIMOUSECONNECTOR pInterface, bool fRel, bool fAbs)
|
---|
688 | {
|
---|
689 | PDRVMAINMOUSE pDrv = RT_FROM_MEMBER(pInterface, DRVMAINMOUSE, IConnector);
|
---|
690 | if (fRel)
|
---|
691 | pDrv->u32DevCaps |= MOUSE_DEVCAP_RELATIVE;
|
---|
692 | else
|
---|
693 | pDrv->u32DevCaps &= ~MOUSE_DEVCAP_RELATIVE;
|
---|
694 | if (fAbs)
|
---|
695 | pDrv->u32DevCaps |= MOUSE_DEVCAP_ABSOLUTE;
|
---|
696 | else
|
---|
697 | pDrv->u32DevCaps &= ~MOUSE_DEVCAP_ABSOLUTE;
|
---|
698 |
|
---|
699 | pDrv->pMouse->sendMouseCapsNotifications();
|
---|
700 | }
|
---|
701 |
|
---|
702 |
|
---|
703 | /**
|
---|
704 | * @interface_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
705 | */
|
---|
706 | DECLCALLBACK(void *) Mouse::drvQueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
707 | {
|
---|
708 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
|
---|
709 | PDRVMAINMOUSE pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
|
---|
710 |
|
---|
711 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
|
---|
712 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUSECONNECTOR, &pDrv->IConnector);
|
---|
713 | return NULL;
|
---|
714 | }
|
---|
715 |
|
---|
716 |
|
---|
717 | /**
|
---|
718 | * Destruct a mouse driver instance.
|
---|
719 | *
|
---|
720 | * @returns VBox status.
|
---|
721 | * @param pDrvIns The driver instance data.
|
---|
722 | */
|
---|
723 | DECLCALLBACK(void) Mouse::drvDestruct(PPDMDRVINS pDrvIns)
|
---|
724 | {
|
---|
725 | PDRVMAINMOUSE pData = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
|
---|
726 | LogFlow(("Mouse::drvDestruct: iInstance=%d\n", pDrvIns->iInstance));
|
---|
727 | PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
|
---|
728 |
|
---|
729 | if (pData->pMouse)
|
---|
730 | {
|
---|
731 | AutoWriteLock mouseLock(pData->pMouse COMMA_LOCKVAL_SRC_POS);
|
---|
732 | for (unsigned cDev = 0; cDev < MOUSE_MAX_DEVICES; ++cDev)
|
---|
733 | if (pData->pMouse->mpDrv[cDev] == pData)
|
---|
734 | {
|
---|
735 | pData->pMouse->mpDrv[cDev] = NULL;
|
---|
736 | break;
|
---|
737 | }
|
---|
738 | }
|
---|
739 | }
|
---|
740 |
|
---|
741 |
|
---|
742 | /**
|
---|
743 | * Construct a mouse driver instance.
|
---|
744 | *
|
---|
745 | * @copydoc FNPDMDRVCONSTRUCT
|
---|
746 | */
|
---|
747 | DECLCALLBACK(int) Mouse::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
|
---|
748 | {
|
---|
749 | PDRVMAINMOUSE pData = PDMINS_2_DATA(pDrvIns, PDRVMAINMOUSE);
|
---|
750 | LogFlow(("drvMainMouse_Construct: iInstance=%d\n", pDrvIns->iInstance));
|
---|
751 | PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
|
---|
752 |
|
---|
753 | /*
|
---|
754 | * Validate configuration.
|
---|
755 | */
|
---|
756 | if (!CFGMR3AreValuesValid(pCfg, "Object\0"))
|
---|
757 | return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
|
---|
758 | AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
|
---|
759 | ("Configuration error: Not possible to attach anything to this driver!\n"),
|
---|
760 | VERR_PDM_DRVINS_NO_ATTACH);
|
---|
761 |
|
---|
762 | /*
|
---|
763 | * IBase.
|
---|
764 | */
|
---|
765 | pDrvIns->IBase.pfnQueryInterface = Mouse::drvQueryInterface;
|
---|
766 |
|
---|
767 | pData->IConnector.pfnReportModes = Mouse::mouseReportModes;
|
---|
768 |
|
---|
769 | /*
|
---|
770 | * Get the IMousePort interface of the above driver/device.
|
---|
771 | */
|
---|
772 | pData->pUpPort = (PPDMIMOUSEPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMIMOUSEPORT_IID);
|
---|
773 | if (!pData->pUpPort)
|
---|
774 | {
|
---|
775 | AssertMsgFailed(("Configuration error: No mouse port interface above!\n"));
|
---|
776 | return VERR_PDM_MISSING_INTERFACE_ABOVE;
|
---|
777 | }
|
---|
778 |
|
---|
779 | /*
|
---|
780 | * Get the Mouse object pointer and update the mpDrv member.
|
---|
781 | */
|
---|
782 | void *pv;
|
---|
783 | int rc = CFGMR3QueryPtr(pCfg, "Object", &pv);
|
---|
784 | if (RT_FAILURE(rc))
|
---|
785 | {
|
---|
786 | AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
|
---|
787 | return rc;
|
---|
788 | }
|
---|
789 | pData->pMouse = (Mouse *)pv; /** @todo Check this cast! */
|
---|
790 | unsigned cDev;
|
---|
791 | {
|
---|
792 | AutoReadLock mouseLock(pData->pMouse COMMA_LOCKVAL_SRC_POS);
|
---|
793 |
|
---|
794 | for (cDev = 0; cDev < MOUSE_MAX_DEVICES; ++cDev)
|
---|
795 | if (!pData->pMouse->mpDrv[cDev])
|
---|
796 | {
|
---|
797 | pData->pMouse->mpDrv[cDev] = pData;
|
---|
798 | break;
|
---|
799 | }
|
---|
800 | }
|
---|
801 | if (cDev == MOUSE_MAX_DEVICES)
|
---|
802 | return VERR_NO_MORE_HANDLES;
|
---|
803 |
|
---|
804 | return VINF_SUCCESS;
|
---|
805 | }
|
---|
806 |
|
---|
807 |
|
---|
808 | /**
|
---|
809 | * Main mouse driver registration record.
|
---|
810 | */
|
---|
811 | const PDMDRVREG Mouse::DrvReg =
|
---|
812 | {
|
---|
813 | /* u32Version */
|
---|
814 | PDM_DRVREG_VERSION,
|
---|
815 | /* szName */
|
---|
816 | "MainMouse",
|
---|
817 | /* szRCMod */
|
---|
818 | "",
|
---|
819 | /* szR0Mod */
|
---|
820 | "",
|
---|
821 | /* pszDescription */
|
---|
822 | "Main mouse driver (Main as in the API).",
|
---|
823 | /* fFlags */
|
---|
824 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
825 | /* fClass. */
|
---|
826 | PDM_DRVREG_CLASS_MOUSE,
|
---|
827 | /* cMaxInstances */
|
---|
828 | ~0U,
|
---|
829 | /* cbInstance */
|
---|
830 | sizeof(DRVMAINMOUSE),
|
---|
831 | /* pfnConstruct */
|
---|
832 | Mouse::drvConstruct,
|
---|
833 | /* pfnDestruct */
|
---|
834 | Mouse::drvDestruct,
|
---|
835 | /* pfnRelocate */
|
---|
836 | NULL,
|
---|
837 | /* pfnIOCtl */
|
---|
838 | NULL,
|
---|
839 | /* pfnPowerOn */
|
---|
840 | NULL,
|
---|
841 | /* pfnReset */
|
---|
842 | NULL,
|
---|
843 | /* pfnSuspend */
|
---|
844 | NULL,
|
---|
845 | /* pfnResume */
|
---|
846 | NULL,
|
---|
847 | /* pfnAttach */
|
---|
848 | NULL,
|
---|
849 | /* pfnDetach */
|
---|
850 | NULL,
|
---|
851 | /* pfnPowerOff */
|
---|
852 | NULL,
|
---|
853 | /* pfnSoftReset */
|
---|
854 | NULL,
|
---|
855 | /* u32EndVersion */
|
---|
856 | PDM_DRVREG_VERSION
|
---|
857 | };
|
---|
858 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|