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