17#include "InputDispatcherFilter.h"
18#include "MousePointer.h"
22#include <QGuiApplication>
23#include <QQuickWindow>
26#include <qpa/qplatformnativeinterface.h>
27#include <qpa/qplatformscreen.h>
29InputDispatcherFilter *InputDispatcherFilter::instance()
31 static InputDispatcherFilter filter;
35InputDispatcherFilter::InputDispatcherFilter(QObject *parent)
39 QPlatformNativeInterface *ni = QGuiApplication::platformNativeInterface();
40 m_inputDispatcher =
static_cast<QObject*
>(ni->nativeResourceForIntegration(
"InputDispatcher"));
41 if (m_inputDispatcher) {
42 m_inputDispatcher->installEventFilter(
this);
46void InputDispatcherFilter::registerPointer(MousePointer *pointer)
49 m_pointers.insert(pointer);
52void InputDispatcherFilter::unregisterPointer(MousePointer *pointer)
54 m_pointers.remove(pointer);
57static bool usesMapToGlobalCursorMapping() {
58 static bool ret = qEnvironmentVariableIsSet(
"LOMIRI_USES_MAP_TO_GLOBAL_CURSOR_MAPPING");
62bool InputDispatcherFilter::eventFilter(QObject *o, QEvent *e)
64 if (o != m_inputDispatcher)
return false;
67 case QEvent::MouseMove:
68 case QEvent::MouseButtonPress:
69 case QEvent::MouseButtonRelease:
72 auto pointer = currentPointer();
73 if (!pointer || !pointer->window())
return true;
75 if (!pointer->parentItem()) {
76 qDebug() <<
"Huh? a MousePointer without a parent?";
80 QMouseEvent* me =
static_cast<QMouseEvent*
>(e);
83 QPointF movement = me->localPos();
85 QPointF oldPos, newPos;
90 if (usesMapToGlobalCursorMapping()) {
94 if (qEnvironmentVariableIsSet(
"LOMIRI_RUNNING_IN_VM")) {
96 oldPos = me->screenPos();
98 oldPos = pointer->parentItem()->mapToGlobal(pointer->position());
100 newPos = pointer->parentItem()->mapToGlobal(pointer->position() + movement);
101 movement = newPos - oldPos;
103 oldPos = pointer->window()->geometry().topLeft() + pointer->position();
107 newPos = adjustedPositionForMovement(oldPos, movement);
109 QScreen* currentScreen = screenAt(newPos);
111 QRect screenRect = currentScreen->geometry();
112 qreal newX = (oldPos + movement).x();
113 qreal newY = (oldPos + movement).y();
115 if (newX <= screenRect.left() && newY < screenRect.top() + pointer->topBoundaryOffset()) {
116 const auto distance = qSqrt(qPow(newX, 2) + qPow(newY- screenRect.top() - pointer->topBoundaryOffset(), 2));
117 Q_EMIT pushedTopLeftCorner(currentScreen, qAbs(distance), me->buttons());
119 }
else if (newX >= screenRect.right()-1 && newY < screenRect.top() + pointer->topBoundaryOffset()) {
120 const auto distance = qSqrt(qPow(newX-screenRect.right(), 2) + qPow(newY - screenRect.top() - pointer->topBoundaryOffset(), 2));
121 Q_EMIT pushedTopRightCorner(currentScreen, qAbs(distance), me->buttons());
123 }
else if (newX < 0 && newY >= screenRect.bottom()-1) {
124 const auto distance = qSqrt(qPow(newX, 2) + qPow(newY-screenRect.bottom(), 2));
125 Q_EMIT pushedBottomLeftCorner(currentScreen, qAbs(distance), me->buttons());
127 }
else if (newX >= screenRect.right()-1 && newY >= screenRect.bottom()-1) {
128 const auto distance = qSqrt(qPow(newX-screenRect.right(), 2) + qPow(newY-screenRect.bottom(), 2));
129 Q_EMIT pushedBottomRightCorner(currentScreen, qAbs(distance), me->buttons());
131 }
else if (newX < screenRect.left()) {
132 Q_EMIT pushedLeftBoundary(currentScreen, qAbs(newX), me->buttons());
134 }
else if (newX >= screenRect.right()) {
135 Q_EMIT pushedRightBoundary(currentScreen, newX - (screenRect.right() - 1), me->buttons());
137 }
else if (newY < screenRect.top() + pointer->topBoundaryOffset()) {
138 Q_EMIT pushedTopBoundary(currentScreen, qAbs(newY - screenRect.top() - pointer->topBoundaryOffset()), me->buttons());
140 }
else if (Q_LIKELY(newX > 0 && newX < screenRect.right()-1 && newY > 0 && newY < screenRect.bottom()-1)) {
142 Q_EMIT pushStopped(currentScreen);
149 QMouseEvent eCopy(me->type(), me->localPos(), newPos, me->button(), me->buttons(), me->modifiers());
150 eCopy.setTimestamp(me->timestamp());
158 auto pointer = currentPointer();
159 if (!pointer || !pointer->window())
return true;
161 QWheelEvent* we =
static_cast<QWheelEvent*
>(e);
164 QPointF movement = we->position();
166 QPointF oldPos, newPos;
169 if (usesMapToGlobalCursorMapping()) {
173 oldPos = pointer->parentItem()->mapToGlobal(pointer->position());
174 newPos = pointer->parentItem()->mapToGlobal(pointer->position() + movement);
175 movement = newPos - oldPos;
177 oldPos = pointer->window()->geometry().topLeft() + pointer->position();
181 newPos = adjustedPositionForMovement(oldPos, movement);
184 QWheelEvent eCopy(we->position(), newPos, we->pixelDelta(), we->angleDelta(), we->buttons(), we->modifiers(), we->phase(), we->inverted());
185 eCopy.setTimestamp(we->timestamp());
195QPointF InputDispatcherFilter::adjustedPositionForMovement(
const QPointF &pt,
const QPointF &movement)
const
197 QPointF adjusted = pt + movement;
199 auto screen = screenAt(adjusted);
202 }
else if ((screen = screenAt(pt))) {
203 QRectF screenBounds = screen->geometry();
205 adjusted.rx() = qMax(screenBounds.left(), qMin(adjusted.x(), screenBounds.right()-1));
206 adjusted.ry() = qMax(screenBounds.top(), qMin(adjusted.y(), screenBounds.bottom()-1));
208 auto screens = QGuiApplication::screens();
211 Q_FOREACH(QScreen* screen, screens) {
212 Q_FOREACH(MousePointer* pointer, m_pointers) {
213 if (pointer->isEnabled() && pointer->screen() == screen) {
214 return screen->geometry().center();
222QScreen *InputDispatcherFilter::screenAt(
const QPointF &pt)
const
224 Q_FOREACH(MousePointer* pointer, m_pointers) {
225 if (!pointer->isEnabled())
continue;
227 QScreen* screen = pointer->screen();
228 if (screen && screen->geometry().contains(pt.toPoint()))
234MousePointer *InputDispatcherFilter::currentPointer()
const
236 Q_FOREACH(MousePointer* pointer, m_pointers) {
237 if (!pointer->isEnabled())
continue;