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 || !pointer->window())
return true;
69 QMouseEvent* me =
static_cast<QMouseEvent*
>(e);
72 QPointF movement = me->localPos();
76 if (qEnvironmentVariableIsSet(
"LOMIRI_RUNNING_IN_VM")) {
78 oldPos = me->screenPos();
81 oldPos = pointer->window()->geometry().topLeft() + pointer->position();
83 QPointF newPos = adjustedPositionForMovement(oldPos, movement);
85 QScreen* currentScreen = screenAt(newPos);
87 QRect screenRect = currentScreen->geometry();
88 qreal newX = (oldPos + movement).x();
89 qreal newY = (oldPos + movement).y();
91 if (newX <= screenRect.left() && newY < screenRect.top() + pointer->topBoundaryOffset()) {
92 const auto distance = qSqrt(qPow(newX, 2) + qPow(newY- screenRect.top() - pointer->topBoundaryOffset(), 2));
93 Q_EMIT pushedTopLeftCorner(currentScreen, qAbs(distance), me->buttons());
95 }
else if (newX >= screenRect.right()-1 && newY < screenRect.top() + pointer->topBoundaryOffset()) {
96 const auto distance = qSqrt(qPow(newX-screenRect.right(), 2) + qPow(newY - screenRect.top() - pointer->topBoundaryOffset(), 2));
97 Q_EMIT pushedTopRightCorner(currentScreen, qAbs(distance), me->buttons());
99 }
else if (newX < 0 && newY >= screenRect.bottom()-1) {
100 const auto distance = qSqrt(qPow(newX, 2) + qPow(newY-screenRect.bottom(), 2));
101 Q_EMIT pushedBottomLeftCorner(currentScreen, qAbs(distance), me->buttons());
103 }
else if (newX >= screenRect.right()-1 && newY >= screenRect.bottom()-1) {
104 const auto distance = qSqrt(qPow(newX-screenRect.right(), 2) + qPow(newY-screenRect.bottom(), 2));
105 Q_EMIT pushedBottomRightCorner(currentScreen, qAbs(distance), me->buttons());
107 }
else if (newX < screenRect.left()) {
108 Q_EMIT pushedLeftBoundary(currentScreen, qAbs(newX), me->buttons());
110 }
else if (newX >= screenRect.right()) {
111 Q_EMIT pushedRightBoundary(currentScreen, newX - (screenRect.right() - 1), me->buttons());
113 }
else if (newY < screenRect.top() + pointer->topBoundaryOffset()) {
114 Q_EMIT pushedTopBoundary(currentScreen, qAbs(newY - screenRect.top() - pointer->topBoundaryOffset()), me->buttons());
116 }
else if (Q_LIKELY(newX > 0 && newX < screenRect.right()-1 && newY > 0 && newY < screenRect.bottom()-1)) {
118 Q_EMIT pushStopped(currentScreen);
125 QMouseEvent eCopy(me->type(), me->localPos(), newPos, me->button(), me->buttons(), me->modifiers());
126 eCopy.setTimestamp(me->timestamp());
134 auto pointer = currentPointer();
135 if (!pointer || !pointer->window())
return true;
137 QWheelEvent* we =
static_cast<QWheelEvent*
>(e);
140 QPointF movement = we->position();
143 QPointF oldPos = pointer->window()->geometry().topLeft() + pointer->position();
144 QPointF newPos = adjustedPositionForMovement(oldPos, movement);
147 QWheelEvent eCopy(we->position(), newPos, we->pixelDelta(), we->angleDelta(), we->buttons(), we->modifiers(), we->phase(), we->inverted());
148 eCopy.setTimestamp(we->timestamp());
158QPointF InputDispatcherFilter::adjustedPositionForMovement(
const QPointF &pt,
const QPointF &movement)
const
160 QPointF adjusted = pt + movement;
162 auto screen = screenAt(adjusted);
165 }
else if ((screen = screenAt(pt))) {
166 QRectF screenBounds = screen->geometry();
168 adjusted.rx() = qMax(screenBounds.left(), qMin(adjusted.x(), screenBounds.right()-1));
169 adjusted.ry() = qMax(screenBounds.top(), qMin(adjusted.y(), screenBounds.bottom()-1));
171 auto screens = QGuiApplication::screens();
174 Q_FOREACH(QScreen* screen, screens) {
175 Q_FOREACH(MousePointer* pointer, m_pointers) {
176 if (pointer->isEnabled() && pointer->screen() == screen) {
177 return screen->geometry().center();
185QScreen *InputDispatcherFilter::screenAt(
const QPointF &pt)
const
187 Q_FOREACH(MousePointer* pointer, m_pointers) {
188 if (!pointer->isEnabled())
continue;
190 QScreen* screen = pointer->screen();
191 if (screen && screen->geometry().contains(pt.toPoint()))
197MousePointer *InputDispatcherFilter::currentPointer()
const
199 Q_FOREACH(MousePointer* pointer, m_pointers) {
200 if (!pointer->isEnabled())
continue;