17#include "InputDispatcherFilter.h"
18#include "MousePointer.h"
21#include <QGuiApplication>
22#include <QQuickWindow>
25#include <qpa/qplatformnativeinterface.h>
26#include <qpa/qplatformscreen.h>
28InputDispatcherFilter *InputDispatcherFilter::instance()
30 static InputDispatcherFilter filter;
34InputDispatcherFilter::InputDispatcherFilter(QObject *parent)
38 QPlatformNativeInterface *ni = QGuiApplication::platformNativeInterface();
39 m_inputDispatcher =
static_cast<QObject*
>(ni->nativeResourceForIntegration(
"InputDispatcher"));
40 if (m_inputDispatcher) {
41 m_inputDispatcher->installEventFilter(
this);
45void InputDispatcherFilter::registerPointer(MousePointer *pointer)
48 m_pointers.insert(pointer);
51void InputDispatcherFilter::unregisterPointer(MousePointer *pointer)
53 m_pointers.remove(pointer);
56bool InputDispatcherFilter::eventFilter(QObject *o, QEvent *e)
58 if (o != m_inputDispatcher)
return false;
61 case QEvent::MouseMove:
62 case QEvent::MouseButtonPress:
63 case QEvent::MouseButtonRelease:
66 auto pointer = currentPointer();
67 if (!pointer)
return true;
69 QMouseEvent* me =
static_cast<QMouseEvent*
>(e);
72 QPointF movement = me->localPos();
75 QPointF oldPos = pointer->window()->geometry().topLeft() + pointer->position();
76 QPointF newPos = adjustedPositionForMovement(oldPos, movement);
78 QScreen* currentScreen = screenAt(newPos);
80 QRect screenRect = currentScreen->geometry();
81 qreal newX = (oldPos + movement).x();
82 qreal newY = (oldPos + movement).y();
84 if (newX <= screenRect.left() && newY < screenRect.top() + pointer->topBoundaryOffset()) {
85 const auto distance = qSqrt(qPow(newX, 2) + qPow(newY- screenRect.top() - pointer->topBoundaryOffset(), 2));
86 Q_EMIT pushedTopLeftCorner(currentScreen, qAbs(distance), me->buttons());
88 }
else if (newX >= screenRect.right()-1 && newY < screenRect.top() + pointer->topBoundaryOffset()) {
89 const auto distance = qSqrt(qPow(newX-screenRect.right(), 2) + qPow(newY - screenRect.top() - pointer->topBoundaryOffset(), 2));
90 Q_EMIT pushedTopRightCorner(currentScreen, qAbs(distance), me->buttons());
92 }
else if (newX < 0 && newY >= screenRect.bottom()-1) {
93 const auto distance = qSqrt(qPow(newX, 2) + qPow(newY-screenRect.bottom(), 2));
94 Q_EMIT pushedBottomLeftCorner(currentScreen, qAbs(distance), me->buttons());
96 }
else if (newX >= screenRect.right()-1 && newY >= screenRect.bottom()-1) {
97 const auto distance = qSqrt(qPow(newX-screenRect.right(), 2) + qPow(newY-screenRect.bottom(), 2));
98 Q_EMIT pushedBottomRightCorner(currentScreen, qAbs(distance), me->buttons());
100 }
else if (newX < screenRect.left()) {
101 Q_EMIT pushedLeftBoundary(currentScreen, qAbs(newX), me->buttons());
103 }
else if (newX >= screenRect.right()) {
104 Q_EMIT pushedRightBoundary(currentScreen, newX - (screenRect.right() - 1), me->buttons());
106 }
else if (newY < screenRect.top() + pointer->topBoundaryOffset()) {
107 Q_EMIT pushedTopBoundary(currentScreen, qAbs(newY - screenRect.top() - pointer->topBoundaryOffset()), me->buttons());
109 }
else if (Q_LIKELY(newX > 0 && newX < screenRect.right()-1 && newY > 0 && newY < screenRect.bottom()-1)) {
111 Q_EMIT pushStopped(currentScreen);
118 QMouseEvent eCopy(me->type(), me->localPos(), newPos, me->button(), me->buttons(), me->modifiers());
119 eCopy.setTimestamp(me->timestamp());
129QPointF InputDispatcherFilter::adjustedPositionForMovement(
const QPointF &pt,
const QPointF &movement)
const
131 QPointF adjusted = pt + movement;
133 auto screen = screenAt(adjusted);
136 }
else if ((screen = screenAt(pt))) {
137 QRectF screenBounds = screen->geometry();
139 adjusted.rx() = qMax(screenBounds.left(), qMin(adjusted.x(), screenBounds.right()-1));
140 adjusted.ry() = qMax(screenBounds.top(), qMin(adjusted.y(), screenBounds.bottom()-1));
142 auto screens = QGuiApplication::screens();
145 Q_FOREACH(QScreen* screen, screens) {
146 Q_FOREACH(MousePointer* pointer, m_pointers) {
147 if (pointer->isEnabled() && pointer->screen() == screen) {
148 return screen->geometry().center();
156QScreen *InputDispatcherFilter::screenAt(
const QPointF &pt)
const
158 Q_FOREACH(MousePointer* pointer, m_pointers) {
159 if (!pointer->isEnabled())
continue;
161 QScreen* screen = pointer->screen();
162 if (screen && screen->geometry().contains(pt.toPoint()))
168MousePointer *InputDispatcherFilter::currentPointer()
const
170 Q_FOREACH(MousePointer* pointer, m_pointers) {
171 if (!pointer->isEnabled())
continue;