on fc161c2 help sign in
unity8/plugins/Cursor/InputDispatcherFilter.cpp
146 lines 5.1 kB history raw
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 */
17#include "InputDispatcherFilter.h"
18#include "MousePointer.h"
20#include <QEvent>
21#include <QGuiApplication>
22#include <QScreen>
23#include <qpa/qplatformnativeinterface.h>
24#include <qpa/qplatformscreen.h>
26InputDispatcherFilter *InputDispatcherFilter::instance()
28 static InputDispatcherFilter filter;
29 return &filter;
32InputDispatcherFilter::InputDispatcherFilter(QObject *parent)
33 : QObject(parent)
35 QPlatformNativeInterface *ni = QGuiApplication::platformNativeInterface();
36 m_inputDispatcher = static_cast<QObject*>(ni->nativeResourceForIntegration("InputDispatcher"));
37 if (m_inputDispatcher) {
38 m_inputDispatcher->installEventFilter(this);
39 }
42void InputDispatcherFilter::registerPointer(MousePointer *pointer)
44 // allow first registered pointer to be visible.
45 pointer->setVisible(m_pointers.count() == 0);
47 m_pointers.insert(pointer);
48 connect(pointer, &MousePointer::mouseMoved, this, [this, pointer]() {
49 Q_FOREACH(auto p, m_pointers) {
50 p->setVisible(p == pointer);
51 }
52 });
55void InputDispatcherFilter::unregisterPointer(MousePointer *pointer)
57 m_pointers.remove(pointer);
58 disconnect(pointer, &MousePointer::mouseMoved, this, 0);
61void InputDispatcherFilter::setPosition(const QPointF &pos)
63 mousePosition = pos;
66bool InputDispatcherFilter::eventFilter(QObject *o, QEvent *e)
68 if (o != m_inputDispatcher) return false;
70 switch (e->type()) {
71 case QEvent::MouseMove:
72 case QEvent::MouseButtonPress:
73 case QEvent::MouseButtonRelease:
74 {
75 // FIXME - removed all input filtering for now - this was for extended display
76 return false;
78// QMouseEvent* me = static_cast<QMouseEvent*>(e);
80// // Local position gives relative change of mouse pointer.
81// QPointF localPos = me->localPos();
82// QPointF globalPos = me->screenPos();
84// // Adjust the position
85// QPointF oldPos(mousePosition.isNull() ? globalPos : mousePosition);
86// QPointF newPos = adjustedPositionForMovement(oldPos, localPos);
88// QScreen* currentScreen = screenAt(newPos);
89// if (currentScreen) {
90// QRect screenRect = currentScreen->geometry();
91// qreal unadjustedX = (oldPos + localPos).x();
92// if (unadjustedX < screenRect.left()) {
93// Q_EMIT pushedLeftBoundary(currentScreen, qAbs(unadjustedX - screenRect.left()), me->buttons());
94// } else if (unadjustedX > screenRect.right()) {
95// Q_EMIT pushedRightBoundary(currentScreen, qAbs(unadjustedX - screenRect.right()), me->buttons());
96// }
97// }
99// // Send the event
100// QMouseEvent eCopy(me->type(), me->localPos(), newPos, me->button(), me->buttons(), me->modifiers());
101// eCopy.setTimestamp(me->timestamp());
102// o->event(&eCopy);
103// return true;
104 }
105 default:
106 break;
107 }
108 return false;
111QPointF InputDispatcherFilter::adjustedPositionForMovement(const QPointF &pt, const QPointF &movement) const
113 QPointF adjusted = pt + movement;
115 auto screen = screenAt(adjusted); // first check if our move was to a valid screen.
116 if (screen) {
117 return adjusted;
118 } else if ((screen = screenAt(pt))) { // then check if our old position was valid
119 QRectF screenBounds = screen->geometry();
120 // bound the new position to the old screen geometry
121 adjusted.rx() = qMax(screenBounds.left(), qMin(adjusted.x(), screenBounds.right()-1));
122 adjusted.ry() = qMax(screenBounds.top(), qMin(adjusted.y(), screenBounds.bottom()-1));
123 } else {
124 auto screens = QGuiApplication::screens();
126 // center of first screen with a pointer.
127 Q_FOREACH(QScreen* screen, screens) {
128 Q_FOREACH(MousePointer* pointer, m_pointers) {
129 if (pointer->screen() == screen) {
130 return screen->geometry().center();
131 }
132 }
133 }
134 }
135 return adjusted;
138QScreen *InputDispatcherFilter::screenAt(const QPointF &pt) const
140 Q_FOREACH(MousePointer* pointer, m_pointers) {
141 QScreen* screen = pointer->screen();
142 if (screen && screen->geometry().contains(pt.toPoint()))
143 return screen;
144 }
145 return nullptr;