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) {
116 QRect screenRect = currentScreen->geometry();
117 qreal newX = (oldPos + movement).x();
118 qreal newY = (oldPos + movement).y();
119
120 if (newX <= screenRect.left() && newY < screenRect.top() + pointer->topBoundaryOffset()) { // top left corner
121 const auto distance = qSqrt(qPow(newX, 2) + qPow(newY- screenRect.top() - pointer->topBoundaryOffset(), 2));
122 Q_EMIT pushedTopLeftCorner(currentScreen, qAbs(distance), me->buttons());
123 m_pushing = true;
124 } else if (newX >= screenRect.right()-1 && newY < screenRect.top() + pointer->topBoundaryOffset()) { // top right corner
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());
127 m_pushing = true;
128 } else if (newX < 0 && newY >= screenRect.bottom()-1) { // bottom left corner
129 const auto distance = qSqrt(qPow(newX, 2) + qPow(newY-screenRect.bottom(), 2));
130 Q_EMIT pushedBottomLeftCorner(currentScreen, qAbs(distance), me->buttons());
131 m_pushing = true;
132 } else if (newX >= screenRect.right()-1 && newY >= screenRect.bottom()-1) { // bottom right corner
133 const auto distance = qSqrt(qPow(newX-screenRect.right(), 2) + qPow(newY-screenRect.bottom(), 2));
134 Q_EMIT pushedBottomRightCorner(currentScreen, qAbs(distance), me->buttons());
135 m_pushing = true;
136 } else if (newX < screenRect.left()) { // left edge
137 Q_EMIT pushedLeftBoundary(currentScreen, qAbs(newX), me->buttons());
138 m_pushing = true;
139 } else if (newX >= screenRect.right()) { // right edge
140 Q_EMIT pushedRightBoundary(currentScreen, newX - (screenRect.right() - 1), me->buttons());
141 m_pushing = true;
142 } else if (newY < screenRect.top() + pointer->topBoundaryOffset()) { // top edge
143 Q_EMIT pushedTopBoundary(currentScreen, qAbs(newY - screenRect.top() - pointer->topBoundaryOffset()), me->buttons());
144 m_pushing = true;
145 } else if (Q_LIKELY(newX > 0 && newX < screenRect.right()-1 && newY > 0 && newY < screenRect.bottom()-1)) { // normal pos, not pushing
146 if (m_pushing) {
147 Q_EMIT pushStopped(currentScreen);
148 m_pushing = false;
149 }
150 }
151 }
152
153 // Send the event
154 QMouseEvent eCopy(me->type(), me->localPos(), newPos, me->button(), me->buttons(), me->modifiers());
155 eCopy.setTimestamp(me->timestamp());
156 o->event(&eCopy);
157 return true;
158 }
159 // Adjust position for Wheel event the same way as mouse events
160 case QEvent::Wheel:
161 {
162 // if we don't have any pointers, filter all mouse events.
163 auto pointer = currentPointer();
164 if (!pointer || !pointer->window()) return true;
165
166 QWheelEvent* we = static_cast<QWheelEvent*>(e);
167
168 // Local position gives relative change of mouse pointer.
169 QPointF movement = we->position();
170
171 QPointF oldPos, newPos;
172
173 // XXX: see above
174 if (usesMapToGlobalCursorMapping()) {
175 // Because the movement is relative to the visual display, run both old and new
176 // pointer position through mapToGlobal() to take Shell's rotation into account,
177 // then recalculate the movement as a difference between them.
178 oldPos = pointer->parentItem()->mapToGlobal(pointer->position());
179 newPos = pointer->parentItem()->mapToGlobal(pointer->position() + movement);
180 movement = newPos - oldPos;
181 } else {
182 oldPos = pointer->window()->geometry().topLeft() + pointer->position();
183 }
184
185 // Adjust the position
186 newPos = adjustedPositionForMovement(oldPos, movement);
187
188 // Send the event
189 QWheelEvent eCopy(we->position(), newPos, we->pixelDelta(), we->angleDelta(), we->buttons(), we->modifiers(), we->phase(), we->inverted());
190 eCopy.setTimestamp(we->timestamp());
191 o->event(&eCopy);
192 return true;
193 }
194 default:
195 break;
196 }
197 return false;
198}
199
200QPointF InputDispatcherFilter::adjustedPositionForMovement(const QPointF &pt, const QPointF &movement) const
201{
202 QPointF adjusted = pt + movement;
203
204 auto screen = screenAt(adjusted); // first check if our move was to a screen with an enabled pointer.
205 if (screen) {
206 return adjusted;
207 } else if ((screen = screenAt(pt))) { // then check if our old position was valid
208 QRectF screenBounds = screen->geometry();
209 // bound the new position to the old 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));
212 } else {
213 auto screens = QGuiApplication::screens();
214
215 // center of first screen with a pointer.
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();
220 }
221 }
222 }
223 }
224 return adjusted;
225}
226
227QScreen *InputDispatcherFilter::screenAt(const QPointF &pt) const
228{
229 Q_FOREACH(MousePointer* pointer, m_pointers) {
230 if (!pointer->isEnabled()) continue;
231
232 QScreen* screen = pointer->screen();
233 if (screen && screen->geometry().contains(pt.toPoint()))
234 return screen;
235 }
236 return nullptr;
237}
238
239MousePointer *InputDispatcherFilter::currentPointer() const
240{
241 Q_FOREACH(MousePointer* pointer, m_pointers) {
242 if (!pointer->isEnabled()) continue;
243 return pointer;
244 }
245 return nullptr;
246}