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