VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMouseHandler.cpp@ 35742

Last change on this file since 35742 was 35742, checked in by vboxsync, 13 years ago

FE/Qt4: remove debug garbage

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 34.3 KB
Line 
1/* $Id: UIMouseHandler.cpp 35742 2011-01-27 16:15:05Z vboxsync $ */
2/** @file
3 *
4 * VBox frontends: Qt GUI ("VirtualBox"):
5 * UIMouseHandler class implementation
6 */
7
8/*
9 * Copyright (C) 2010 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20/* Global includes */
21#include <QDesktopWidget>
22#include <QMouseEvent>
23
24/* Local includes */
25#include "VBoxGlobal.h"
26#include "VBoxProblemReporter.h"
27#include "UIKeyboardHandler.h"
28#include "UIMouseHandler.h"
29#include "UISession.h"
30#include "UIMachineLogic.h"
31#include "UIMachineWindow.h"
32#include "UIMachineView.h"
33#include "UIFrameBuffer.h"
34
35#ifdef Q_WS_X11
36# include <X11/XKBlib.h>
37# ifdef KeyPress
38const int XFocusOut = FocusOut;
39const int XFocusIn = FocusIn;
40const int XKeyPress = KeyPress;
41const int XKeyRelease = KeyRelease;
42# undef KeyRelease
43# undef KeyPress
44# undef FocusOut
45# undef FocusIn
46# endif /* KeyPress */
47#endif /* Q_WS_X11 */
48
49#ifdef Q_WS_MAC
50# include "VBoxUtils-darwin.h"
51#endif /* Q_WS_MAC */
52
53/* Factory function to create mouse-handler: */
54UIMouseHandler* UIMouseHandler::create(UIMachineLogic *pMachineLogic,
55 UIVisualStateType visualStateType)
56{
57 /* Prepare mouse-handler: */
58 UIMouseHandler *pMouseHandler = 0;
59 /* Depending on visual-state type: */
60 switch (visualStateType)
61 {
62 /* For now all the states using common mouse-handler: */
63 case UIVisualStateType_Normal:
64 case UIVisualStateType_Fullscreen:
65 case UIVisualStateType_Seamless:
66 case UIVisualStateType_Scale:
67 pMouseHandler = new UIMouseHandler(pMachineLogic);
68 break;
69 default:
70 break;
71 }
72 /* Return prepared mouse-handler: */
73 return pMouseHandler;
74}
75
76/* Factory function to destroy mouse-handler: */
77void UIMouseHandler::destroy(UIMouseHandler *pMouseHandler)
78{
79 /* Delete mouse-handler: */
80 delete pMouseHandler;
81}
82
83/* Prepare listener for particular machine-window: */
84void UIMouseHandler::prepareListener(ulong uIndex, UIMachineWindow *pMachineWindow)
85{
86 /* If that window is NOT registered yet: */
87 if (!m_windows.contains(uIndex))
88 {
89 /* Register machine-window: */
90 m_windows.insert(uIndex, pMachineWindow->machineWindow());
91 /* Install event-filter for machine-window: */
92 m_windows[uIndex]->installEventFilter(this);
93 }
94
95 /* If that view is NOT registered yet: */
96 if (!m_views.contains(uIndex))
97 {
98 /* Register machine-view: */
99 m_views.insert(uIndex, pMachineWindow->machineView());
100 /* Install event-filter for machine-view: */
101 m_views[uIndex]->installEventFilter(this);
102 /* Make machine-view notify mouse-handler about resizeHintDone(): */
103 connect(m_views[uIndex], SIGNAL(resizeHintDone()), this, SLOT(sltMousePointerShapeChanged()));
104 }
105
106 /* If that viewport is NOT registered yet: */
107 if (!m_viewports.contains(uIndex))
108 {
109 /* Register machine-view-viewport: */
110 m_viewports.insert(uIndex, pMachineWindow->machineView()->viewport());
111 /* Install event-filter for machine-view-viewport: */
112 m_viewports[uIndex]->installEventFilter(this);
113 }
114}
115
116/* Cleanup listener for particular machine-window: */
117void UIMouseHandler::cleanupListener(ulong uIndex)
118{
119 /* Check if we should release mouse first: */
120 if ((int)uIndex == m_iMouseCaptureViewIndex)
121 releaseMouse();
122
123 /* If that window still registered: */
124 if (m_windows.contains(uIndex))
125 {
126 /* Unregister machine-window: */
127 m_windows.remove(uIndex);
128 }
129
130 /* If that view still registered: */
131 if (m_views.contains(uIndex))
132 {
133 /* Unregister machine-view: */
134 m_views.remove(uIndex);
135 }
136
137 /* If that viewport still registered: */
138 if (m_viewports.contains(uIndex))
139 {
140 /* Unregister machine-view-viewport: */
141 m_viewports.remove(uIndex);
142 }
143}
144
145void UIMouseHandler::captureMouse(ulong uScreenId)
146{
147 /* Do not try to capture mouse if its captured already: */
148 if (uisession()->isMouseCaptured())
149 return;
150
151 /* If such viewport exists: */
152 if (m_viewports.contains(uScreenId))
153 {
154 /* Store mouse-capturing state value: */
155 uisession()->setMouseCaptured(true);
156
157 /* Memorize the index of machine-view-viewport captured mouse: */
158 m_iMouseCaptureViewIndex = uScreenId;
159
160 /* Memorize the host position where the cursor was captured: */
161 m_capturedMousePos = QCursor::pos();
162
163 /* Acquiring visible viewport rectangle in global coodrinates: */
164 QRect visibleRectangle = m_viewports[m_iMouseCaptureViewIndex]->visibleRegion().boundingRect();
165 QPoint visibleRectanglePos = m_views[m_iMouseCaptureViewIndex]->mapToGlobal(m_viewports[m_iMouseCaptureViewIndex]->pos());
166 visibleRectangle.translate(visibleRectanglePos);
167 visibleRectangle = visibleRectangle.intersected(QApplication::desktop()->availableGeometry());
168
169#ifdef Q_WS_WIN
170 /* Move the mouse to the center of the visible area: */
171 m_lastMousePos = visibleRectangle.center();
172 QCursor::setPos(m_lastMousePos);
173 /* Update mouse clipping: */
174 updateMouseCursorClipping();
175#elif defined (Q_WS_MAC)
176 /* Grab all mouse events: */
177 ::darwinMouseGrab(m_viewports[m_iMouseCaptureViewIndex]);
178#else /* Q_WS_MAC */
179 /* Remember current mouse position: */
180 m_lastMousePos = QCursor::pos();
181 /* Grab all mouse events: */
182 m_viewports[m_iMouseCaptureViewIndex]->grabMouse();
183#endif /* !Q_WS_MAC */
184
185 /* Switch guest mouse to the relative mode: */
186 CMouse mouse = session().GetConsole().GetMouse();
187 mouse.PutMouseEvent(0, 0, 0, 0, 0);
188
189 /* Emit signal if required: */
190 emit mouseStateChanged(mouseState());
191 }
192}
193
194void UIMouseHandler::releaseMouse()
195{
196 /* Do not try to release mouse if its released already: */
197 if (!uisession()->isMouseCaptured())
198 return;
199
200 /* If such viewport exists: */
201 if (m_viewports.contains(m_iMouseCaptureViewIndex))
202 {
203 /* Store mouse-capturing state value: */
204 uisession()->setMouseCaptured(false);
205
206 /* Return the cursor to where it was when we captured it: */
207 QCursor::setPos(m_capturedMousePos);
208#ifdef Q_WS_WIN
209 /* Update mouse clipping: */
210 updateMouseCursorClipping();
211#elif defined(Q_WS_MAC)
212 /* Releasing grabbed mouse from that view: */
213 ::darwinMouseRelease(m_viewports[m_iMouseCaptureViewIndex]);
214#else /* Q_WS_MAC */
215 /* Releasing grabbed mouse from that view: */
216 m_viewports[m_iMouseCaptureViewIndex]->releaseMouse();
217#endif /* !Q_WS_MAC */
218 /* Reset mouse-capture index: */
219 m_iMouseCaptureViewIndex = -1;
220
221 /* Emit signal if required: */
222 emit mouseStateChanged(mouseState());
223 }
224}
225
226/* Setter for mouse-integration feature: */
227void UIMouseHandler::setMouseIntegrationEnabled(bool fEnabled)
228{
229 /* Do not do anything if its already done: */
230 if (uisession()->isMouseIntegrated() == fEnabled)
231 return;
232
233 /* Store mouse-integration state value: */
234 uisession()->setMouseIntegrated(fEnabled);
235
236 /* Reuse sltMouseCapabilityChanged() to update mouse state: */
237 sltMouseCapabilityChanged();
238}
239
240/* Current mouse state: */
241int UIMouseHandler::mouseState() const
242{
243 return (uisession()->isMouseCaptured() ? UIMouseStateType_MouseCaptured : 0) |
244 (uisession()->isMouseSupportsAbsolute() ? UIMouseStateType_MouseAbsolute : 0) |
245 (uisession()->isMouseIntegrated() ? 0 : UIMouseStateType_MouseAbsoluteDisabled);
246}
247
248#ifdef Q_WS_X11
249bool UIMouseHandler::x11EventFilter(XEvent *pEvent, ulong /* uScreenId */)
250{
251 /* Check if some system event should be filtered-out.
252 * Returning 'true' means filtering-out,
253 * Returning 'false' means passing event to Qt. */
254 bool fResult = false; /* Pass to Qt by default: */
255 switch (pEvent->type)
256 {
257 /* We have to handle XFocusOut right here as this event is not passed to UIMachineView::event().
258 * Handling this event is important for releasing the keyboard before the screen saver gets active.
259 * See public ticket #3894: Apparently this makes problems with newer versions of Qt
260 * and this hack is probably not necessary anymore. So disable it for Qt >= 4.5.0. */
261 case XFocusOut:
262 {
263 if (uisession()->isRunning())
264 {
265 if (VBoxGlobal::qtRTVersion() < ((4 << 16) | (5 << 8) | 0))
266 releaseMouse();
267 }
268 fResult = false;
269 }
270 default:
271 break;
272 }
273 /* Return result: */
274 return fResult;
275}
276#endif /* Q_WS_X11 */
277
278/* Machine state-change handler: */
279void UIMouseHandler::sltMachineStateChanged()
280{
281 /* Get machine state: */
282 KMachineState state = uisession()->machineState();
283 /* Handle particular machine states: */
284 switch (state)
285 {
286 case KMachineState_Paused:
287 case KMachineState_TeleportingPausedVM:
288 case KMachineState_Stuck:
289 {
290 /* Release the mouse: */
291 releaseMouse();
292 break;
293 }
294 default:
295 break;
296 }
297
298 /* Notify all listeners: */
299 emit mouseStateChanged(mouseState());
300}
301
302/* Mouse capability-change handler: */
303void UIMouseHandler::sltMouseCapabilityChanged()
304{
305 /* If mouse supports absolute pointing and mouse-integration activated: */
306 if (uisession()->isMouseSupportsAbsolute() && uisession()->isMouseIntegrated())
307 {
308 /* Release the mouse: */
309 releaseMouse();
310 /* Also we should switch guest mouse to the absolute mode: */
311 CMouse mouse = session().GetConsole().GetMouse();
312 mouse.PutMouseEventAbsolute(-1, -1, 0, 0, 0);
313 }
314#if 0 /* current team's decision is NOT to capture mouse on mouse-absolute mode loosing! */
315 /* If mouse-integration deactivated or mouse doesn't supports absolute pointing: */
316 else
317 {
318 /* Search for the machine-view focused now: */
319 int iFocusedView = -1;
320 QList<ulong> screenIds = m_views.keys();
321 for (int i = 0; i < screenIds.size(); ++i)
322 {
323 if (m_views[screenIds[i]]->hasFocus())
324 {
325 iFocusedView = screenIds[i];
326 break;
327 }
328 }
329 /* If there is no focused view but views are present we will use the first one: */
330 if (iFocusedView == -1 && !screenIds.isEmpty())
331 iFocusedView = screenIds[0];
332 /* Capture mouse using that view: */
333 if (iFocusedView != -1)
334 captureMouse(iFocusedView);
335 }
336#else /* but just to switch the guest mouse into relative mode! */
337 /* If mouse-integration deactivated or mouse doesn't supports absolute pointing: */
338 else
339 {
340 /* Switch guest mouse to the relative mode: */
341 CMouse mouse = session().GetConsole().GetMouse();
342 mouse.PutMouseEvent(0, 0, 0, 0, 0);
343 }
344#endif
345
346 /* Notify user about mouse supports or not absolute pointing if that method was called by signal: */
347 if (sender())
348 vboxProblem().remindAboutMouseIntegration(uisession()->isMouseSupportsAbsolute());
349
350 /* Notify all listeners: */
351 emit mouseStateChanged(mouseState());
352}
353
354/* Mouse pointer-shape-change handler: */
355void UIMouseHandler::sltMousePointerShapeChanged()
356{
357 /* First of all, we should check if the host pointer should be visible.
358 * We should hide host pointer in case of:
359 * 1. mouse is 'captured' or
360 * 2. mouse is 'not captured', 'integrated', 'absolute', host pointer is hidden by the guest. */
361 if (uisession()->isMouseCaptured() ||
362 (uisession()->isMouseIntegrated() &&
363 uisession()->isMouseSupportsAbsolute() &&
364 uisession()->isHidingHostPointer()))
365 {
366 QList<ulong> screenIds = m_viewports.keys();
367 for (int i = 0; i < screenIds.size(); ++i)
368 m_viewports[screenIds[i]]->setCursor(Qt::BlankCursor);
369 }
370
371 else
372
373 /* Otherwise we should show host pointer with guest shape assigned to it if:
374 * machine is NOT 'paused', mouse is 'integrated', 'absolute', valid pointer shape is present. */
375 if (!uisession()->isPaused() &&
376 uisession()->isMouseIntegrated() &&
377 uisession()->isMouseSupportsAbsolute() &&
378 uisession()->isValidPointerShapePresent())
379 {
380 QList<ulong> screenIds = m_viewports.keys();
381 for (int i = 0; i < screenIds.size(); ++i)
382 m_viewports[screenIds[i]]->setCursor(uisession()->cursor());
383 }
384
385 else
386
387 /* There could be other states covering such situations as:
388 * 1. machine is 'paused' or
389 * 2. mouse is 'not captured', 'integrated', 'not absolute' or
390 * 3. mouse is 'not captured', 'not integrated', 'absolute'.
391 * We have nothing to do with that except just unset the cursor. */
392 {
393 QList<ulong> screenIds = m_viewports.keys();
394 for (int i = 0; i < screenIds.size(); ++i)
395 m_viewports[screenIds[i]]->unsetCursor();
396 }
397}
398
399/* Mouse-handler constructor: */
400UIMouseHandler::UIMouseHandler(UIMachineLogic *pMachineLogic)
401 : QObject(pMachineLogic)
402 , m_pMachineLogic(pMachineLogic)
403 , m_iLastMouseWheelDelta(0)
404 , m_iMouseCaptureViewIndex(-1)
405{
406 /* Machine state-change updater: */
407 connect(uisession(), SIGNAL(sigMachineStateChange()), this, SLOT(sltMachineStateChanged()));
408
409 /* Mouse capability state-change updater: */
410 connect(uisession(), SIGNAL(sigMouseCapabilityChange()), this, SLOT(sltMouseCapabilityChanged()));
411
412 /* Mouse pointer shape state-change updaters: */
413 connect(uisession(), SIGNAL(sigMousePointerShapeChange()), this, SLOT(sltMousePointerShapeChanged()));
414 connect(this, SIGNAL(mouseStateChanged(int)), this, SLOT(sltMousePointerShapeChanged()));
415
416 /* Initialize: */
417 sltMachineStateChanged();
418 sltMousePointerShapeChanged();
419 sltMouseCapabilityChanged();
420}
421
422/* Mouse-handler destructor: */
423UIMouseHandler::~UIMouseHandler()
424{
425}
426
427/* Machine-logic getter: */
428UIMachineLogic* UIMouseHandler::machineLogic() const
429{
430 return m_pMachineLogic;
431}
432
433/* UI Session getter: */
434UISession* UIMouseHandler::uisession() const
435{
436 return machineLogic()->uisession();
437}
438
439/* Main Session getter: */
440CSession& UIMouseHandler::session() const
441{
442 return uisession()->session();
443}
444
445/* Event handler for registered machine-view(s): */
446bool UIMouseHandler::eventFilter(QObject *pWatched, QEvent *pEvent)
447{
448 /* If that object is of QWidget type: */
449 if (QWidget *pWatchedWidget = qobject_cast<QWidget*>(pWatched))
450 {
451 /* Check if that widget is in windows list: */
452 if (m_windows.values().contains(pWatchedWidget))
453 {
454#ifdef Q_WS_WIN
455 /* Handle window events: */
456 switch (pEvent->type())
457 {
458 case QEvent::Move:
459 {
460 /* Update mouse clipping if window was moved
461 * by Operating System desktop manager: */
462 updateMouseCursorClipping();
463 break;
464 }
465 default:
466 break;
467 }
468#endif /* Q_WS_WIN */
469 }
470
471 else
472
473 /* Check if that widget is of UIMachineView type: */
474 if (UIMachineView *pWatchedMachineView = qobject_cast<UIMachineView*>(pWatchedWidget))
475 {
476 /* Check if that widget is in views list: */
477 if (m_views.values().contains(pWatchedMachineView))
478 {
479 /* Handle view events: */
480 switch (pEvent->type())
481 {
482 case QEvent::FocusOut:
483 {
484 /* Release the mouse: */
485 releaseMouse();
486 break;
487 }
488 default:
489 break;
490 }
491 }
492 }
493
494 else
495
496 /* Check if that widget is in viewports list: */
497 if (m_viewports.values().contains(pWatchedWidget))
498 {
499 /* Get current watched widget screen id: */
500 ulong uScreenId = m_viewports.key(pWatchedWidget);
501 /* Handle viewport events: */
502 switch (pEvent->type())
503 {
504#ifdef Q_WS_MAC
505 case UIGrabMouseEvent::GrabMouseEvent:
506 {
507 UIGrabMouseEvent *pDeltaEvent = static_cast<UIGrabMouseEvent*>(pEvent);
508 QPoint gl = m_lastMousePos;
509 QPoint p = QPoint(pDeltaEvent->xDelta() + gl.x(),
510 pDeltaEvent->yDelta() + gl.y());
511 if (mouseEvent(pDeltaEvent->mouseEventType(), uScreenId,
512 m_viewports[uScreenId]->mapFromGlobal(p), p,
513 pDeltaEvent->buttons(),
514 pDeltaEvent->wheelDelta(), pDeltaEvent->orientation()))
515 return true;
516//
517// QMouseEvent *pNewMouseEvent = new QMouseEvent(QEvent::MouseMove,
518// m_viewports[uScreenId]->mapFromGlobal(p), p,
519// Qt::NoButton,
520// Qt::NoButton,
521// Qt::NoModifier);
522//
523//
524// /* Send that event to real destination: */
525// QApplication::postEvent(m_viewports[uScreenId], pNewMouseEvent);
526 return false;
527 break;
528 }
529#endif /* Q_WS_MAC */
530 case QEvent::MouseMove:
531 case QEvent::MouseButtonRelease:
532 {
533 /* Check if we should propagate this event to another window: */
534 QWidget *pHoveredWidget = QApplication::widgetAt(QCursor::pos());
535 if (pHoveredWidget && pHoveredWidget != pWatchedWidget && m_viewports.values().contains(pHoveredWidget))
536 {
537 /* Get current mouse-move event: */
538 QMouseEvent *pOldMouseEvent = static_cast<QMouseEvent*>(pEvent);
539
540 /* Prepare redirected mouse-move event: */
541 QMouseEvent *pNewMouseEvent = new QMouseEvent(pOldMouseEvent->type(),
542 pHoveredWidget->mapFromGlobal(pOldMouseEvent->globalPos()),
543 pOldMouseEvent->globalPos(),
544 pOldMouseEvent->button(),
545 pOldMouseEvent->buttons(),
546 pOldMouseEvent->modifiers());
547
548 /* Send that event to real destination: */
549 QApplication::postEvent(pHoveredWidget, pNewMouseEvent);
550
551 /* Filter out that event: */
552 return true;
553 }
554
555 /* Check if we should activate window under cursor: */
556 if (!uisession()->isMouseCaptured() &&
557 QApplication::activeWindow() &&
558 QApplication::activeWindow()->inherits("UIMachineWindow") &&
559 QApplication::activeWindow() != pWatchedWidget->window())
560 {
561 /* Activating hovered machine window: */
562 pWatchedWidget->window()->activateWindow();
563#ifdef Q_WS_X11
564 /* On X11 its not enough to just activate window if you
565 * want to raise it also, so we will make it separately: */
566 pWatchedWidget->window()->raise();
567#endif /* Q_WS_X11 */
568 }
569
570 /* This event should be also processed using next 'case': */
571 }
572 case QEvent::MouseButtonPress:
573 case QEvent::MouseButtonDblClick:
574 {
575 QMouseEvent *pMouseEvent = static_cast<QMouseEvent*>(pEvent);
576 m_iLastMouseWheelDelta = 0;
577 if (mouseEvent(pMouseEvent->type(), uScreenId,
578 pMouseEvent->pos(), pMouseEvent->globalPos(),
579 pMouseEvent->buttons(), 0, Qt::Horizontal))
580 return true;
581 break;
582 }
583 case QEvent::Wheel:
584 {
585 QWheelEvent *pWheelEvent = static_cast<QWheelEvent*>(pEvent);
586 /* There are pointing devices which send smaller values for the delta than 120.
587 * Here we sum them up until we are greater than 120. This allows to have finer control
588 * over the speed acceleration & enables such devices to send a valid wheel event to our
589 * guest mouse device at all: */
590 int iDelta = 0;
591 m_iLastMouseWheelDelta += pWheelEvent->delta();
592 if (qAbs(m_iLastMouseWheelDelta) >= 120)
593 {
594 iDelta = m_iLastMouseWheelDelta;
595 m_iLastMouseWheelDelta = m_iLastMouseWheelDelta % 120;
596 }
597 if (mouseEvent(pWheelEvent->type(), uScreenId,
598 pWheelEvent->pos(), pWheelEvent->globalPos(),
599#ifdef QT_MAC_USE_COCOA
600 /* Qt Cocoa is buggy. It always reports a left button pressed when the
601 * mouse wheel event occurs. A workaround is to ask the application which
602 * buttons are pressed currently: */
603 QApplication::mouseButtons(),
604#else /* QT_MAC_USE_COCOA */
605 pWheelEvent->buttons(),
606#endif /* !QT_MAC_USE_COCOA */
607 iDelta, pWheelEvent->orientation()))
608 return true;
609 break;
610 }
611#ifdef Q_WS_MAC
612 case QEvent::Leave:
613 {
614 /* Enable mouse event compression if we leave the VM view.
615 * This is necessary for having smooth resizing of the VM/other windows: */
616 ::darwinSetMouseCoalescingEnabled(true);
617 break;
618 }
619 case QEvent::Enter:
620 {
621 /* Disable mouse event compression if we enter the VM view.
622 * So all mouse events are registered in the VM.
623 * Only do this if the keyboard/mouse is grabbed
624 * (this is when we have a valid event handler): */
625 if (machineLogic()->keyboardHandler()->isKeyboardGrabbed())
626 darwinSetMouseCoalescingEnabled(false);
627 break;
628 }
629#endif /* Q_WS_MAC */
630#ifdef Q_WS_WIN
631 case QEvent::Resize:
632 {
633 /* Update mouse clipping: */
634 updateMouseCursorClipping();
635 break;
636 }
637#endif /* Q_WS_WIN */
638 default:
639 break;
640 }
641 }
642 }
643 return QObject::eventFilter(pWatched, pEvent);
644}
645
646/* Separate function to handle most of existing mouse-events: */
647bool UIMouseHandler::mouseEvent(int iEventType, ulong uScreenId,
648 const QPoint &relativePos, const QPoint &globalPos,
649 Qt::MouseButtons mouseButtons,
650 int wheelDelta, Qt::Orientation wheelDirection)
651{
652 /* Check if machine is still running: */
653 if (!uisession()->isRunning())
654 return true;
655
656 /* Check if such view & viewport are registered: */
657 if (!m_views.contains(uScreenId) || !m_viewports.contains(uScreenId))
658 return true;
659
660 int iMouseButtonsState = 0;
661 if (mouseButtons & Qt::LeftButton)
662 iMouseButtonsState |= KMouseButtonState_LeftButton;
663 if (mouseButtons & Qt::RightButton)
664 iMouseButtonsState |= KMouseButtonState_RightButton;
665 if (mouseButtons & Qt::MidButton)
666 iMouseButtonsState |= KMouseButtonState_MiddleButton;
667 if (mouseButtons & Qt::XButton1)
668 iMouseButtonsState |= KMouseButtonState_XButton1;
669 if (mouseButtons & Qt::XButton2)
670 iMouseButtonsState |= KMouseButtonState_XButton2;
671
672#ifdef Q_WS_MAC
673 /* Simulate the right click on host-key + left-mouse-button: */
674 if (machineLogic()->keyboardHandler()->isHostKeyPressed() &&
675 machineLogic()->keyboardHandler()->isHostKeyAlone() &&
676 iMouseButtonsState == KMouseButtonState_LeftButton)
677 iMouseButtonsState = KMouseButtonState_RightButton;
678#endif /* Q_WS_MAC */
679
680 int iWheelVertical = 0;
681 int iWheelHorizontal = 0;
682 if (wheelDirection == Qt::Vertical)
683 {
684 /* The absolute value of wheel delta is 120 units per every wheel move;
685 * positive deltas correspond to counterclockwise rotations (usually up),
686 * negative deltas correspond to clockwise (usually down). */
687 iWheelVertical = - (wheelDelta / 120);
688 }
689 else if (wheelDirection == Qt::Horizontal)
690 iWheelHorizontal = wheelDelta / 120;
691
692 if (uisession()->isMouseCaptured())
693 {
694#ifdef Q_WS_WIN
695 /* Send pending WM_PAINT events: */
696 ::UpdateWindow(m_viewports[uScreenId]->winId());
697#endif
698 CMouse mouse = session().GetConsole().GetMouse();
699 mouse.PutMouseEvent(globalPos.x() - m_lastMousePos.x(),
700 globalPos.y() - m_lastMousePos.y(),
701 iWheelVertical, iWheelHorizontal, iMouseButtonsState);
702
703#ifdef Q_WS_WIN
704 /* Bringing mouse to the opposite side to simulate the endless moving: */
705
706 /* Acquiring visible viewport rectangle in local coordinates: */
707 QRect viewportRectangle = m_viewports[uScreenId]->visibleRegion().boundingRect();
708 QPoint viewportRectangleGlobalPos = m_views[uScreenId]->mapToGlobal(m_viewports[uScreenId]->pos());
709 /* Shift viewport rectangle to global position to bound by available geometry: */
710 viewportRectangle.translate(viewportRectangleGlobalPos);
711 /* Acquiring viewport rectangle cropped by available geometry: */
712 viewportRectangle = viewportRectangle.intersected(QApplication::desktop()->availableGeometry());
713 /* Shift remaining viewport rectangle to local position as relative position is in local coordinates: */
714 viewportRectangle.translate(-viewportRectangleGlobalPos);
715
716 /* Get boundaries: */
717 int ix1 = viewportRectangle.left() + 1;
718 int iy1 = viewportRectangle.top() + 1;
719 int ix2 = viewportRectangle.right() - 1;
720 int iy2 = viewportRectangle.bottom() - 1;
721
722 /* Simulate infinite movement: */
723 QPoint p = relativePos;
724 if (relativePos.x() == ix1)
725 p.setX(ix2 - 1);
726 else if (relativePos.x() == ix2)
727 p.setX(ix1 + 1);
728 if (relativePos.y() == iy1)
729 p.setY(iy2 - 1);
730 else if (relativePos.y() == iy2)
731 p.setY(iy1 + 1);
732 if (p != relativePos)
733 {
734 m_lastMousePos = m_viewports[uScreenId]->mapToGlobal(p);
735 QCursor::setPos(m_lastMousePos);
736 }
737 else
738 m_lastMousePos = globalPos;
739#else /* Q_WS_WIN */
740 int iWe = QApplication::desktop()->width() - 1;
741 int iHe = QApplication::desktop()->height() - 1;
742 QPoint p = globalPos;
743 if (globalPos.x() == 0)
744 p.setX(iWe - 1);
745 else if (globalPos.x() == iWe)
746 p.setX( 1 );
747 if (globalPos.y() == 0)
748 p.setY(iHe - 1);
749 else if (globalPos.y() == iHe)
750 p.setY(1);
751
752 if (p != globalPos)
753 {
754 m_lastMousePos = p;
755 /* No need for cursor updating on the Mac, there is no one. */
756# ifndef Q_WS_MAC
757 QCursor::setPos(m_lastMousePos);
758# endif /* Q_WS_MAC */
759 }
760 else
761 m_lastMousePos = globalPos;
762#endif /* !Q_WS_WIN */
763 return true; /* stop further event handling */
764 }
765 else /* !uisession()->isMouseCaptured() */
766 {
767#if 0 // TODO: Move that to fullscreen event-handler:
768 if (vboxGlobal().vmRenderMode() != VBoxDefs::SDLMode)
769 {
770 /* try to automatically scroll the guest canvas if the
771 * mouse is on the screen border */
772 /// @todo (r=dmik) better use a timer for autoscroll
773 QRect scrGeo = QApplication::desktop()->screenGeometry (this);
774 int iDx = 0, iDy = 0;
775 if (scrGeo.width() < contentsWidth())
776 {
777 if (scrGeo.left() == globalPos.x()) iDx = -1;
778 if (scrGeo.right() == globalPos.x()) iDx = +1;
779 }
780 if (scrGeo.height() < contentsHeight())
781 {
782 if (scrGeo.top() == globalPos.y()) iDy = -1;
783 if (scrGeo.bottom() == globalPos.y()) iDy = +1;
784 }
785 if (iDx || iDy)
786 scrollBy(iDx, iDy);
787 }
788#endif
789
790 if (uisession()->isMouseSupportsAbsolute() && uisession()->isMouseIntegrated())
791 {
792 int iCw = m_views[uScreenId]->contentsWidth(), iCh = m_views[uScreenId]->contentsHeight();
793 int iVw = m_views[uScreenId]->visibleWidth(), iVh = m_views[uScreenId]->visibleHeight();
794
795 if (vboxGlobal().vmRenderMode() != VBoxDefs::SDLMode)
796 {
797 /* Try to automatically scroll the guest canvas if the
798 * mouse goes outside its visible part: */
799 int iDx = 0;
800 if (relativePos.x() > iVw) iDx = relativePos.x() - iVw;
801 else if (relativePos.x() < 0) iDx = relativePos.x();
802 int iDy = 0;
803 if (relativePos.y() > iVh) iDy = relativePos.y() - iVh;
804 else if (relativePos.y() < 0) iDy = relativePos.y();
805 if (iDx != 0 || iDy != 0) m_views[uScreenId]->scrollBy(iDx, iDy);
806 }
807
808 /* Get mouse-pointer location: */
809 QPoint cpnt = m_views[uScreenId]->viewportToContents(relativePos);
810
811 /* Determine scaling: */
812 UIFrameBuffer *pFrameBuffer = m_views[uScreenId]->frameBuffer();
813 QSize scaledSize = pFrameBuffer->scaledSize();
814 double xRatio = scaledSize.isValid() ? (double)pFrameBuffer->width() / (double)scaledSize.width() : 1;
815 double yRatio = scaledSize.isValid() ? (double)pFrameBuffer->height() / (double)scaledSize.height() : 1;
816 /* Set scaling if scale-factor is present: */
817 cpnt.setX(cpnt.x() * xRatio);
818 cpnt.setY(cpnt.y() * yRatio);
819
820 /* Bound coordinates: */
821 if (cpnt.x() < 0) cpnt.setX(0);
822 else if (cpnt.x() > iCw - 1) cpnt.setX(iCw - 1);
823 if (cpnt.y() < 0) cpnt.setY(0);
824 else if (cpnt.y() > iCh - 1) cpnt.setY(iCh - 1);
825
826 /* Determine shifting: */
827 CFramebuffer framebuffer;
828 LONG xShift = 0, yShift = 0;
829 session().GetConsole().GetDisplay().GetFramebuffer(uScreenId, framebuffer, xShift, yShift);
830 /* Set shifting: */
831 cpnt.setX(cpnt.x() + xShift);
832 cpnt.setY(cpnt.y() + yShift);
833
834 /* Post absolute mouse-event into guest: */
835 CMouse mouse = session().GetConsole().GetMouse();
836 mouse.PutMouseEventAbsolute(cpnt.x() + 1, cpnt.y() + 1, iWheelVertical, iWheelHorizontal, iMouseButtonsState);
837 return true;
838 }
839 else
840 {
841 if (m_views[uScreenId]->hasFocus() && (iEventType == QEvent::MouseButtonRelease && mouseButtons == Qt::NoButton))
842 {
843 if (uisession()->isPaused())
844 {
845 vboxProblem().remindAboutPausedVMInput();
846 }
847 else if (uisession()->isRunning())
848 {
849 /* Temporarily disable auto capture that will take place after this dialog is dismissed because
850 * the capture state is to be defined by the dialog result itself: */
851 uisession()->setAutoCaptureDisabled(true);
852 bool autoConfirmed = false;
853 bool ok = vboxProblem().confirmInputCapture(&autoConfirmed);
854 if (autoConfirmed)
855 uisession()->setAutoCaptureDisabled(false);
856 /* Otherwise, the disable flag will be reset in the next console view's focus in event (since
857 * may happen asynchronously on some platforms, after we return from this code): */
858 if (ok)
859 {
860#ifdef Q_WS_X11
861 /* Make sure that pending FocusOut events from the previous message box are handled,
862 * otherwise the mouse is immediately ungrabbed again: */
863 qApp->processEvents();
864#endif
865 machineLogic()->keyboardHandler()->captureKeyboard(uScreenId);
866 captureMouse(uScreenId);
867 }
868 }
869 }
870 }
871 }
872
873 return false;
874}
875
876#ifdef Q_WS_WIN
877/* This method is actually required only because under win-host
878 * we do not really grab the mouse in case of capturing it: */
879void UIMouseHandler::updateMouseCursorClipping()
880{
881 /* Check if such view && viewport are registered: */
882 if (!m_views.contains(m_iMouseCaptureViewIndex) || !m_viewports.contains(m_iMouseCaptureViewIndex))
883 return;
884
885 if (uisession()->isMouseCaptured())
886 {
887 /* Acquiring visible viewport rectangle: */
888 QRect viewportRectangle = m_viewports[m_iMouseCaptureViewIndex]->visibleRegion().boundingRect();
889 QPoint viewportRectangleGlobalPos = m_views[m_iMouseCaptureViewIndex]->mapToGlobal(m_viewports[m_iMouseCaptureViewIndex]->pos());
890 viewportRectangle.translate(viewportRectangleGlobalPos);
891 viewportRectangle = viewportRectangle.intersected(QApplication::desktop()->availableGeometry());
892 /* Prepare clipping area: */
893 RECT rect = { viewportRectangle.left() + 1, viewportRectangle.top() + 1, viewportRectangle.right(), viewportRectangle.bottom() };
894 ::ClipCursor(&rect);
895 }
896 else
897 {
898 ::ClipCursor(NULL);
899 }
900}
901#endif /* Q_WS_WIN */
902
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use