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 <QEvent>
21#include <QGuiApplication>
22#include <QQuickWindow>
23#include <QScreen>
24#include <QtMath>
25#include <qpa/qplatformnativeinterface.h>
26#include <qpa/qplatformscreen.h>
27
28InputDispatcherFilter *InputDispatcherFilter::instance()
29{
30 static InputDispatcherFilter filter;
31 return &filter;
32}
33
34InputDispatcherFilter::InputDispatcherFilter(QObject *parent)
35 : QObject(parent)
36 , m_pushing(false)
37{
38 QPlatformNativeInterface *ni = QGuiApplication::platformNativeInterface();
39 m_inputDispatcher = static_cast<QObject*>(ni->nativeResourceForIntegration("InputDispatcher"));
40 if (m_inputDispatcher) {
41 m_inputDispatcher->installEventFilter(this);
42 }
43}
44
45void InputDispatcherFilter::registerPointer(MousePointer *pointer)
46{
47 // allow first registered pointer to be visible.
48 m_pointers.insert(pointer);
49}
50
51void InputDispatcherFilter::unregisterPointer(MousePointer *pointer)
52{
53 m_pointers.remove(pointer);
54}
55
56bool InputDispatcherFilter::eventFilter(QObject *o, QEvent *e)
57{
58 if (o != m_inputDispatcher) return false;
59
60 switch (e->type()) {
61 case QEvent::MouseMove:
62 case QEvent::MouseButtonPress:
63 case QEvent::MouseButtonRelease:
64 {
65 // if we don't have any pointers, filter all mouse events.
66 auto pointer = currentPointer();
67 if (!pointer || !pointer->window()) return true;
68
69 QMouseEvent* me = static_cast<QMouseEvent*>(e);
70
71 // Local position gives relative change of mouse pointer.
72 QPointF movement = me->localPos();
73
74 QPointF oldPos;
75 // Adjust the position
76 if (qEnvironmentVariableIsSet("LOMIRI_RUNNING_IN_VM")) {
77 // Get the position directly from Mir
78 oldPos = me->screenPos();
79 }
80 else {
81 oldPos = pointer->window()->geometry().topLeft() + pointer->position();
82 }
83 QPointF newPos = adjustedPositionForMovement(oldPos, movement);
84
85 QScreen* currentScreen = screenAt(newPos);
86 if (currentScreen) {
87 QRect screenRect = currentScreen->geometry();
88 qreal newX = (oldPos + movement).x();
89 qreal newY = (oldPos + movement).y();
90
91 if (newX <= screenRect.left() && newY < screenRect.top() + pointer->topBoundaryOffset()) { // top left corner
92 const auto distance = qSqrt(qPow(newX, 2) + qPow(newY- screenRect.top() - pointer->topBoundaryOffset(), 2));
93 Q_EMIT pushedTopLeftCorner(currentScreen, qAbs(distance), me->buttons());
94 m_pushing = true;
95 } else if (newX >= screenRect.right()-1 && newY < screenRect.top() + pointer->topBoundaryOffset()) { // top right corner
96 const auto distance = qSqrt(qPow(newX-screenRect.right(), 2) + qPow(newY - screenRect.top() - pointer->topBoundaryOffset(), 2));
97 Q_EMIT pushedTopRightCorner(currentScreen, qAbs(distance), me->buttons());
98 m_pushing = true;
99 } else if (newX < 0 && newY >= screenRect.bottom()-1) { // bottom left corner
100 const auto distance = qSqrt(qPow(newX, 2) + qPow(newY-screenRect.bottom(), 2));
101 Q_EMIT pushedBottomLeftCorner(currentScreen, qAbs(distance), me->buttons());
102 m_pushing = true;
103 } else if (newX >= screenRect.right()-1 && newY >= screenRect.bottom()-1) { // bottom right corner
104 const auto distance = qSqrt(qPow(newX-screenRect.right(), 2) + qPow(newY-screenRect.bottom(), 2));
105 Q_EMIT pushedBottomRightCorner(currentScreen, qAbs(distance), me->buttons());
106 m_pushing = true;
107 } else if (newX < screenRect.left()) { // left edge
108 Q_EMIT pushedLeftBoundary(currentScreen, qAbs(newX), me->buttons());
109 m_pushing = true;
110 } else if (newX >= screenRect.right()) { // right edge
111 Q_EMIT pushedRightBoundary(currentScreen, newX - (screenRect.right() - 1), me->buttons());
112 m_pushing = true;
113 } else if (newY < screenRect.top() + pointer->topBoundaryOffset()) { // top edge
114 Q_EMIT pushedTopBoundary(currentScreen, qAbs(newY - screenRect.top() - pointer->topBoundaryOffset()), me->buttons());
115 m_pushing = true;
116 } else if (Q_LIKELY(newX > 0 && newX < screenRect.right()-1 && newY > 0 && newY < screenRect.bottom()-1)) { // normal pos, not pushing
117 if (m_pushing) {
118 Q_EMIT pushStopped(currentScreen);
119 m_pushing = false;
120 }
121 }
122 }
123
124 // Send the event
125 QMouseEvent eCopy(me->type(), me->localPos(), newPos, me->button(), me->buttons(), me->modifiers());
126 eCopy.setTimestamp(me->timestamp());
127 o->event(&eCopy);
128 return true;
129 }
130 // Adjust position for Wheel event the same way as mouse events
131 case QEvent::Wheel:
132 {
133 // if we don't have any pointers, filter all mouse events.
134 auto pointer = currentPointer();
135 if (!pointer || !pointer->window()) return true;
136
137 QWheelEvent* we = static_cast<QWheelEvent*>(e);
138
139 // Local position gives relative change of mouse pointer.
140 QPointF movement = we->position();
141
142 // Adjust the position
143 QPointF oldPos = pointer->window()->geometry().topLeft() + pointer->position();
144 QPointF newPos = adjustedPositionForMovement(oldPos, movement);
145
146 // Send the event
147 QWheelEvent eCopy(we->position(), newPos, we->pixelDelta(), we->angleDelta(), we->buttons(), we->modifiers(), we->phase(), we->inverted());
148 eCopy.setTimestamp(we->timestamp());
149 o->event(&eCopy);
150 return true;
151 }
152 default:
153 break;
154 }
155 return false;
156}
157
158QPointF InputDispatcherFilter::adjustedPositionForMovement(const QPointF &pt, const QPointF &movement) const
159{
160 QPointF adjusted = pt + movement;
161
162 auto screen = screenAt(adjusted); // first check if our move was to a screen with an enabled pointer.
163 if (screen) {
164 return adjusted;
165 } else if ((screen = screenAt(pt))) { // then check if our old position was valid
166 QRectF screenBounds = screen->geometry();
167 // bound the new position to the old screen geometry
168 adjusted.rx() = qMax(screenBounds.left(), qMin(adjusted.x(), screenBounds.right()-1));
169 adjusted.ry() = qMax(screenBounds.top(), qMin(adjusted.y(), screenBounds.bottom()-1));
170 } else {
171 auto screens = QGuiApplication::screens();
172
173 // center of first screen with a pointer.
174 Q_FOREACH(QScreen* screen, screens) {
175 Q_FOREACH(MousePointer* pointer, m_pointers) {
176 if (pointer->isEnabled() && pointer->screen() == screen) {
177 return screen->geometry().center();
178 }
179 }
180 }
181 }
182 return adjusted;
183}
184
185QScreen *InputDispatcherFilter::screenAt(const QPointF &pt) const
186{
187 Q_FOREACH(MousePointer* pointer, m_pointers) {
188 if (!pointer->isEnabled()) continue;
189
190 QScreen* screen = pointer->screen();
191 if (screen && screen->geometry().contains(pt.toPoint()))
192 return screen;
193 }
194 return nullptr;
195}
196
197MousePointer *InputDispatcherFilter::currentPointer() const
198{
199 Q_FOREACH(MousePointer* pointer, m_pointers) {
200 if (!pointer->isEnabled()) continue;
201 return pointer;
202 }
203 return nullptr;
204}