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 if (qEnvironmentVariableIsSet(
"LOMIRI_RUNNING_IN_VM")) {
105 oldPos = me->screenPos();
107 oldPos = pointer->window()->geometry().topLeft() + pointer->position();
112 newPos = adjustedPositionForMovement(oldPos, movement);
114 QScreen* currentScreen = screenAt(newPos);
116 QRect screenRect = currentScreen->geometry();
117 qreal newX = (oldPos + movement).x();
118 qreal newY = (oldPos + movement).y();
120 if (newX <= screenRect.left() && newY < screenRect.top() + pointer->topBoundaryOffset()) {
121 const auto distance = qSqrt(qPow(newX, 2) + qPow(newY- screenRect.top() - pointer->topBoundaryOffset(), 2));
122 Q_EMIT pushedTopLeftCorner(currentScreen, qAbs(distance), me->buttons());
124 }
else if (newX >= screenRect.right()-1 && newY < screenRect.top() + pointer->topBoundaryOffset()) {
125 const auto distance = qSqrt(qPow(newX-screenRect.right(), 2) + qPow(newY - screenRect.top() - pointer->topBoundaryOffset(), 2));
126 Q_EMIT pushedTopRightCorner(currentScreen, qAbs(distance), me->buttons());
128 }
else if (newX < 0 && newY >= screenRect.bottom()-1) {
129 const auto distance = qSqrt(qPow(newX, 2) + qPow(newY-screenRect.bottom(), 2));
130 Q_EMIT pushedBottomLeftCorner(currentScreen, qAbs(distance), me->buttons());
132 }
else if (newX >= screenRect.right()-1 && newY >= screenRect.bottom()-1) {
133 const auto distance = qSqrt(qPow(newX-screenRect.right(), 2) + qPow(newY-screenRect.bottom(), 2));
134 Q_EMIT pushedBottomRightCorner(currentScreen, qAbs(distance), me->buttons());
136 }
else if (newX < screenRect.left()) {
137 Q_EMIT pushedLeftBoundary(currentScreen, qAbs(newX), me->buttons());
139 }
else if (newX >= screenRect.right()) {
140 Q_EMIT pushedRightBoundary(currentScreen, newX - (screenRect.right() - 1), me->buttons());
142 }
else if (newY < screenRect.top() + pointer->topBoundaryOffset()) {
143 Q_EMIT pushedTopBoundary(currentScreen, qAbs(newY - screenRect.top() - pointer->topBoundaryOffset()), me->buttons());
145 }
else if (Q_LIKELY(newX > 0 && newX < screenRect.right()-1 && newY > 0 && newY < screenRect.bottom()-1)) {
147 Q_EMIT pushStopped(currentScreen);
154 QMouseEvent eCopy(me->type(), me->localPos(), newPos, me->button(), me->buttons(), me->modifiers());
155 eCopy.setTimestamp(me->timestamp());
163 auto pointer = currentPointer();
164 if (!pointer || !pointer->window())
return true;
166 QWheelEvent* we =
static_cast<QWheelEvent*
>(e);
169 QPointF movement = we->position();
171 QPointF oldPos, newPos;
174 if (usesMapToGlobalCursorMapping()) {
178 oldPos = pointer->parentItem()->mapToGlobal(pointer->position());
179 newPos = pointer->parentItem()->mapToGlobal(pointer->position() + movement);
180 movement = newPos - oldPos;
182 oldPos = pointer->window()->geometry().topLeft() + pointer->position();
186 newPos = adjustedPositionForMovement(oldPos, movement);
189 QWheelEvent eCopy(we->position(), newPos, we->pixelDelta(), we->angleDelta(), we->buttons(), we->modifiers(), we->phase(), we->inverted());
190 eCopy.setTimestamp(we->timestamp());
200QPointF InputDispatcherFilter::adjustedPositionForMovement(
const QPointF &pt,
const QPointF &movement)
const
202 QPointF adjusted = pt + movement;
204 auto screen = screenAt(adjusted);
207 }
else if ((screen = screenAt(pt))) {
208 QRectF screenBounds = screen->geometry();
210 adjusted.rx() = qMax(screenBounds.left(), qMin(adjusted.x(), screenBounds.right()-1));
211 adjusted.ry() = qMax(screenBounds.top(), qMin(adjusted.y(), screenBounds.bottom()-1));
213 auto screens = QGuiApplication::screens();
216 Q_FOREACH(QScreen* screen, screens) {
217 Q_FOREACH(MousePointer* pointer, m_pointers) {
218 if (pointer->isEnabled() && pointer->screen() == screen) {
219 return screen->geometry().center();
227QScreen *InputDispatcherFilter::screenAt(
const QPointF &pt)
const
229 Q_FOREACH(MousePointer* pointer, m_pointers) {
230 if (!pointer->isEnabled())
continue;
232 QScreen* screen = pointer->screen();
233 if (screen && screen->geometry().contains(pt.toPoint()))
239MousePointer *InputDispatcherFilter::currentPointer()
const
241 Q_FOREACH(MousePointer* pointer, m_pointers) {
242 if (!pointer->isEnabled())
continue;