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 QMouseEvent* me = static_cast<QMouseEvent*>(e);
66
67 // Local position gives relative change of mouse pointer.
68 QPointF movement = me->localPos();
69
70 // Adjust the position
71 // Get the pos from mir
72 QPointF oldPos = me->screenPos();
73 QPointF newPos = adjustedPositionForMovement(oldPos, movement);
74
75 // if we don't have any pointers, filter all mouse events.
76 auto pointer = currentPointer(newPos);
77 if (!pointer || !pointer->window()) return true;
78
79 // Activate the current pointer
80 for (MousePointer* p : m_pointers)
81 p->setEnabled(false);
82 pointer->setEnabled(pointer->isActive());
83
84 QScreen* currentScreen = screenAt(newPos);
85 if (currentScreen) {
86 QRect screenRect = currentScreen->geometry();
87 qreal newX = (oldPos + movement).x();
88 qreal newY = (oldPos + movement).y();
89
90 if (newX <= screenRect.left() && newY < screenRect.top() + pointer->topBoundaryOffset()) { // top left corner
91 const auto distance = qSqrt(qPow(newX, 2) + qPow(newY- screenRect.top() - pointer->topBoundaryOffset(), 2));
92 Q_EMIT pushedTopLeftCorner(currentScreen, qAbs(distance), me->buttons());
93 m_pushing = true;
94 } else if (newX >= screenRect.right()-1 && newY < screenRect.top() + pointer->topBoundaryOffset()) { // top right corner
95 const auto distance = qSqrt(qPow(newX-screenRect.right(), 2) + qPow(newY - screenRect.top() - pointer->topBoundaryOffset(), 2));
96 Q_EMIT pushedTopRightCorner(currentScreen, qAbs(distance), me->buttons());
97 m_pushing = true;
98 } else if (newX < 0 && newY >= screenRect.bottom()-1) { // bottom left corner
99 const auto distance = qSqrt(qPow(newX, 2) + qPow(newY-screenRect.bottom(), 2));
100 Q_EMIT pushedBottomLeftCorner(currentScreen, qAbs(distance), me->buttons());
101 m_pushing = true;
102 } else if (newX >= screenRect.right()-1 && newY >= screenRect.bottom()-1) { // bottom right corner
103 const auto distance = qSqrt(qPow(newX-screenRect.right(), 2) + qPow(newY-screenRect.bottom(), 2));
104 Q_EMIT pushedBottomRightCorner(currentScreen, qAbs(distance), me->buttons());
105 m_pushing = true;
106 } else if (newX < screenRect.left()) { // left edge
107 Q_EMIT pushedLeftBoundary(currentScreen, qAbs(newX), me->buttons());
108 m_pushing = true;
109 } else if (newX >= screenRect.right()) { // right edge
110 Q_EMIT pushedRightBoundary(currentScreen, newX - (screenRect.right() - 1), me->buttons());
111 m_pushing = true;
112 } else if (newY < screenRect.top() + pointer->topBoundaryOffset()) { // top edge
113 Q_EMIT pushedTopBoundary(currentScreen, qAbs(newY - screenRect.top() - pointer->topBoundaryOffset()), me->buttons());
114 m_pushing = true;
115 } else if (Q_LIKELY(newX > 0 && newX < screenRect.right()-1 && newY > 0 && newY < screenRect.bottom()-1)) { // normal pos, not pushing
116 if (m_pushing) {
117 Q_EMIT pushStopped(currentScreen);
118 m_pushing = false;
119 }
120 }
121 }
122
123 // Send the event
124 QMouseEvent eCopy(me->type(), me->localPos(), newPos, me->button(), me->buttons(), me->modifiers());
125 eCopy.setTimestamp(me->timestamp());
126 o->event(&eCopy);
127 return true;
128 }
129 default:
130 break;
131 }
132 return false;
133}
134
135QPointF InputDispatcherFilter::adjustedPositionForMovement(const QPointF &pt, const QPointF &movement) const
136{
137 QPointF adjusted = pt + movement;
138 MousePointer* pointer = nullptr;
139
140 auto screen = screenAt(adjusted); // first check if our move was to a screen with an enabled pointer.
141 if (screen) {
142 return adjusted;
143 } else if ((screen = screenAt(pt))) { // then check if our old position was valid
144 QRectF screenBounds = screen->geometry();
145 // bound the new position to the old screen geometry
146 adjusted.rx() = qMax(screenBounds.left(), qMin(adjusted.x(), screenBounds.right()-1));
147 adjusted.ry() = qMax(screenBounds.top(), qMin(adjusted.y(), screenBounds.bottom()-1));
148 } else if ((pointer = currentPointer(pt)) && pointer->centerAdjust()) {
149 auto screens = QGuiApplication::screens();
150
151 // center of first screen with a pointer.
152 Q_FOREACH(QScreen* screen, screens) {
153 Q_FOREACH(MousePointer* pointer, m_pointers) {
154 if (pointer->screen() == screen) {
155 pointer->setCenterAdjust(false);
156 return screen->geometry().center();
157 }
158 }
159 }
160 }
161 return adjusted;
162}
163
164QScreen *InputDispatcherFilter::screenAt(const QPointF &pt) const
165{
166 Q_FOREACH(MousePointer* pointer, m_pointers) {
167 QScreen* screen = pointer->screen();
168 if (screen && screen->geometry().contains(pt.toPoint()))
169 return screen;
170 }
171 return nullptr;
172}
173
174MousePointer *InputDispatcherFilter::currentPointer(const QPointF& point) const
175{
176 Q_FOREACH(MousePointer* pointer, m_pointers) {
177 QScreen* screen = pointer->screen();
178 if (screen && screen->geometry().contains(point.toPoint()))
179 return pointer;
180 }
181 return nullptr;
182}