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);
115 if (currentScreen && currentScreen != pointer->screen()) {
116 Q_EMIT screenEntered(currentScreen);
119 QRect screenRect = currentScreen->geometry();
120 qreal newX = (oldPos + movement).x();
121 qreal newY = (oldPos + movement).y();
123 if (newX <= screenRect.left() && newY < screenRect.top() + pointer->topBoundaryOffset()) {
124 const auto distance = qSqrt(qPow(newX, 2) + qPow(newY- screenRect.top() - pointer->topBoundaryOffset(), 2));
125 Q_EMIT pushedTopLeftCorner(currentScreen, qAbs(distance), me->buttons());
127 }
else if (newX >= screenRect.right()-1 && newY < screenRect.top() + pointer->topBoundaryOffset()) {
128 const auto distance = qSqrt(qPow(newX-screenRect.right(), 2) + qPow(newY - screenRect.top() - pointer->topBoundaryOffset(), 2));
129 Q_EMIT pushedTopRightCorner(currentScreen, qAbs(distance), me->buttons());
131 }
else if (newX < 0 && newY >= screenRect.bottom()-1) {
132 const auto distance = qSqrt(qPow(newX, 2) + qPow(newY-screenRect.bottom(), 2));
133 Q_EMIT pushedBottomLeftCorner(currentScreen, qAbs(distance), me->buttons());
135 }
else if (newX >= screenRect.right()-1 && newY >= screenRect.bottom()-1) {
136 const auto distance = qSqrt(qPow(newX-screenRect.right(), 2) + qPow(newY-screenRect.bottom(), 2));
137 Q_EMIT pushedBottomRightCorner(currentScreen, qAbs(distance), me->buttons());
139 }
else if (newX < screenRect.left()) {
140 Q_EMIT pushedLeftBoundary(currentScreen, qAbs(newX), me->buttons());
142 }
else if (newX >= screenRect.right()) {
143 Q_EMIT pushedRightBoundary(currentScreen, newX - (screenRect.right() - 1), me->buttons());
145 }
else if (newY < screenRect.top() + pointer->topBoundaryOffset()) {
146 Q_EMIT pushedTopBoundary(currentScreen, qAbs(newY - screenRect.top() - pointer->topBoundaryOffset()), me->buttons());
148 }
else if (Q_LIKELY(newX > 0 && newX < screenRect.right()-1 && newY > 0 && newY < screenRect.bottom()-1)) {
150 Q_EMIT pushStopped(currentScreen);
157 QMouseEvent eCopy(me->type(), me->localPos(), newPos, me->button(), me->buttons(), me->modifiers());
158 eCopy.setTimestamp(me->timestamp());
166 auto pointer = currentPointer();
167 if (!pointer || !pointer->window())
return true;
169 QWheelEvent* we =
static_cast<QWheelEvent*
>(e);
172 QPointF movement = we->position();
174 QPointF oldPos, newPos;
177 if (usesMapToGlobalCursorMapping()) {
181 oldPos = pointer->parentItem()->mapToGlobal(pointer->position());
182 newPos = pointer->parentItem()->mapToGlobal(pointer->position() + movement);
183 movement = newPos - oldPos;
185 oldPos = pointer->window()->geometry().topLeft() + pointer->position();
189 newPos = adjustedPositionForMovement(oldPos, movement);
192 QWheelEvent eCopy(we->position(), newPos, we->pixelDelta(), we->angleDelta(), we->buttons(), we->modifiers(), we->phase(), we->inverted());
193 eCopy.setTimestamp(we->timestamp());
203QPointF InputDispatcherFilter::adjustedPositionForMovement(
const QPointF &pt,
const QPointF &movement)
const
205 QPointF adjusted = pt + movement;
207 auto screen = screenAt(adjusted);
210 }
else if ((screen = screenAt(pt))) {
211 QRectF screenBounds = screen->geometry();
213 adjusted.rx() = qMax(screenBounds.left(), qMin(adjusted.x(), screenBounds.right()-1));
214 adjusted.ry() = qMax(screenBounds.top(), qMin(adjusted.y(), screenBounds.bottom()-1));
216 auto screens = QGuiApplication::screens();
219 Q_FOREACH(QScreen* screen, screens) {
220 Q_FOREACH(MousePointer* pointer, m_pointers) {
221 if (pointer->isEnabled() && pointer->screen() == screen) {
222 return screen->geometry().center();
230QScreen *InputDispatcherFilter::screenAt(
const QPointF &pt)
const
232 Q_FOREACH(MousePointer* pointer, m_pointers) {
233 QScreen* screen = pointer->screen();
234 if (screen && screen->geometry().contains(pt.toPoint()))
240MousePointer *InputDispatcherFilter::currentPointer()
const
242 Q_FOREACH(MousePointer* pointer, m_pointers) {
243 if (!pointer->isEnabled())
continue;