Lomiri
Loading...
Searching...
No Matches
InputDispatcherFilter.cpp
1/*
2 * Copyright (C) 2016 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License version 3, as published by
6 * the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include "InputDispatcherFilter.h"
18#include "MousePointer.h"
19
20#include <QDebug>
21#include <QEvent>
22#include <QGuiApplication>
23#include <QQuickWindow>
24#include <QScreen>
25#include <QtMath>
26#include <qpa/qplatformnativeinterface.h>
27#include <qpa/qplatformscreen.h>
28
29InputDispatcherFilter *InputDispatcherFilter::instance()
30{
31 static InputDispatcherFilter filter;
32 return &filter;
33}
34
35InputDispatcherFilter::InputDispatcherFilter(QObject *parent)
36 : QObject(parent)
37 , m_pushing(false)
38{
39 QPlatformNativeInterface *ni = QGuiApplication::platformNativeInterface();
40 m_inputDispatcher = static_cast<QObject*>(ni->nativeResourceForIntegration("InputDispatcher"));
41 if (m_inputDispatcher) {
42 m_inputDispatcher->installEventFilter(this);
43 }
44}
45
46void InputDispatcherFilter::registerPointer(MousePointer *pointer)
47{
48 // allow first registered pointer to be visible.
49 m_pointers.insert(pointer);
50}
51
52void InputDispatcherFilter::unregisterPointer(MousePointer *pointer)
53{
54 m_pointers.remove(pointer);
55}
56
57static bool usesMapToGlobalCursorMapping() {
58 static bool ret = qEnvironmentVariableIsSet("LOMIRI_USES_MAP_TO_GLOBAL_CURSOR_MAPPING");
59 return ret;
60}
61
62bool InputDispatcherFilter::eventFilter(QObject *o, QEvent *e)
63{
64 if (o != m_inputDispatcher) return false;
65
66 switch (e->type()) {
67 case QEvent::MouseMove:
68 case QEvent::MouseButtonPress:
69 case QEvent::MouseButtonRelease:
70 {
71 // if we don't have any pointers, filter all mouse events.
72 auto pointer = currentPointer();
73 if (!pointer || !pointer->window()) return true;
74
75 if (!pointer->parentItem()) {
76 qDebug() << "Huh? a MousePointer without a parent?";
77 return true;
78 }
79
80 QMouseEvent* me = static_cast<QMouseEvent*>(e);
81
82 // Local position gives relative change of mouse pointer.
83 QPointF movement = me->localPos();
84
85 QPointF oldPos, newPos;
86
87 // "Feature-flag" the use of mapToGlobal() because apparently this
88 // causes problem on some devices.
89 // XXX: flag must matches between us and QtMir, or things will break.
90 if (usesMapToGlobalCursorMapping()) {
91 // Because the movement is relative to the visual display, run both old and new
92 // pointer position through mapToGlobal() to take Shell's rotation into account,
93 // then recalculate the movement as a difference between them.
94 if (qEnvironmentVariableIsSet("LOMIRI_RUNNING_IN_VM")) {
95 // Get the position directly from Mir
96 oldPos = me->screenPos();
97 } else {
98 oldPos = pointer->parentItem()->mapToGlobal(pointer->position());
99 }
100 newPos = pointer->parentItem()->mapToGlobal(pointer->position() + movement);
101 movement = newPos - oldPos;
102 } else {
103 if (qEnvironmentVariableIsSet("LOMIRI_RUNNING_IN_VM")) {
104 // Get the position directly from Mir
105 oldPos = me->screenPos();
106 } else {
107 oldPos = pointer->window()->geometry().topLeft() + pointer->position();
108 }
109 }
110
111 // Adjust the position
112 newPos = adjustedPositionForMovement(oldPos, movement);
113
114 QScreen* currentScreen = screenAt(newPos);
115 if (currentScreen && currentScreen != pointer->screen()) {
116 Q_EMIT screenEntered(currentScreen);
117 }
118 if (currentScreen) {
119 QRect screenRect = currentScreen->geometry();
120 qreal newX = (oldPos + movement).x();
121 qreal newY = (oldPos + movement).y();
122
123 if (newX <= screenRect.left() && newY < screenRect.top() + pointer->topBoundaryOffset()) { // top left corner
124 const auto distance = qSqrt(qPow(newX, 2) + qPow(newY- screenRect.top() - pointer->topBoundaryOffset(), 2));
125 Q_EMIT pushedTopLeftCorner(currentScreen, qAbs(distance), me->buttons());
126 m_pushing = true;
127 } else if (newX >= screenRect.right()-1 && newY < screenRect.top() + pointer->topBoundaryOffset()) { // top right corner
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());
130 m_pushing = true;
131 } else if (newX < 0 && newY >= screenRect.bottom()-1) { // bottom left corner
132 const auto distance = qSqrt(qPow(newX, 2) + qPow(newY-screenRect.bottom(), 2));
133 Q_EMIT pushedBottomLeftCorner(currentScreen, qAbs(distance), me->buttons());
134 m_pushing = true;
135 } else if (newX >= screenRect.right()-1 && newY >= screenRect.bottom()-1) { // bottom right corner
136 const auto distance = qSqrt(qPow(newX-screenRect.right(), 2) + qPow(newY-screenRect.bottom(), 2));
137 Q_EMIT pushedBottomRightCorner(currentScreen, qAbs(distance), me->buttons());
138 m_pushing = true;
139 } else if (newX < screenRect.left()) { // left edge
140 Q_EMIT pushedLeftBoundary(currentScreen, qAbs(newX), me->buttons());
141 m_pushing = true;
142 } else if (newX >= screenRect.right()) { // right edge
143 Q_EMIT pushedRightBoundary(currentScreen, newX - (screenRect.right() - 1), me->buttons());
144 m_pushing = true;
145 } else if (newY < screenRect.top() + pointer->topBoundaryOffset()) { // top edge
146 Q_EMIT pushedTopBoundary(currentScreen, qAbs(newY - screenRect.top() - pointer->topBoundaryOffset()), me->buttons());
147 m_pushing = true;
148 } else if (Q_LIKELY(newX > 0 && newX < screenRect.right()-1 && newY > 0 && newY < screenRect.bottom()-1)) { // normal pos, not pushing
149 if (m_pushing) {
150 Q_EMIT pushStopped(currentScreen);
151 m_pushing = false;
152 }
153 }
154 }
155
156 // Send the event
157 QMouseEvent eCopy(me->type(), me->localPos(), newPos, me->button(), me->buttons(), me->modifiers());
158 eCopy.setTimestamp(me->timestamp());
159 o->event(&eCopy);
160 return true;
161 }
162 // Adjust position for Wheel event the same way as mouse events
163 case QEvent::Wheel:
164 {
165 // if we don't have any pointers, filter all mouse events.
166 auto pointer = currentPointer();
167 if (!pointer || !pointer->window()) return true;
168
169 QWheelEvent* we = static_cast<QWheelEvent*>(e);
170
171 // Local position gives relative change of mouse pointer.
172 QPointF movement = we->position();
173
174 QPointF oldPos, newPos;
175
176 // XXX: see above
177 if (usesMapToGlobalCursorMapping()) {
178 // Because the movement is relative to the visual display, run both old and new
179 // pointer position through mapToGlobal() to take Shell's rotation into account,
180 // then recalculate the movement as a difference between them.
181 oldPos = pointer->parentItem()->mapToGlobal(pointer->position());
182 newPos = pointer->parentItem()->mapToGlobal(pointer->position() + movement);
183 movement = newPos - oldPos;
184 } else {
185 oldPos = pointer->window()->geometry().topLeft() + pointer->position();
186 }
187
188 // Adjust the position
189 newPos = adjustedPositionForMovement(oldPos, movement);
190
191 // Send the event
192 QWheelEvent eCopy(we->position(), newPos, we->pixelDelta(), we->angleDelta(), we->buttons(), we->modifiers(), we->phase(), we->inverted());
193 eCopy.setTimestamp(we->timestamp());
194 o->event(&eCopy);
195 return true;
196 }
197 default:
198 break;
199 }
200 return false;
201}
202
203QPointF InputDispatcherFilter::adjustedPositionForMovement(const QPointF &pt, const QPointF &movement) const
204{
205 QPointF adjusted = pt + movement;
206
207 auto screen = screenAt(adjusted); // first check if our move was to a screen with an enabled pointer.
208 if (screen) {
209 return adjusted;
210 } else if ((screen = screenAt(pt))) { // then check if our old position was valid
211 QRectF screenBounds = screen->geometry();
212 // bound the new position to the old 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));
215 } else {
216 auto screens = QGuiApplication::screens();
217
218 // center of first screen with a pointer.
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();
223 }
224 }
225 }
226 }
227 return adjusted;
228}
229
230QScreen *InputDispatcherFilter::screenAt(const QPointF &pt) const
231{
232 Q_FOREACH(MousePointer* pointer, m_pointers) {
233 QScreen* screen = pointer->screen();
234 if (screen && screen->geometry().contains(pt.toPoint()))
235 return screen;
236 }
237 return nullptr;
238}
239
240MousePointer *InputDispatcherFilter::currentPointer() const
241{
242 Q_FOREACH(MousePointer* pointer, m_pointers) {
243 if (!pointer->isEnabled()) continue;
244 return pointer;
245 }
246 return nullptr;
247}