on 795acac help sign in
unity8/plugins/Cursor/InputDispatcherFilter.cpp
143 lines 4.9 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 QMouseEvent* me = static_cast<QMouseEvent*>(e);
77 // Local position gives relative change of mouse pointer.
78 QPointF localPos = me->localPos();
79 QPointF globalPos = me->screenPos();
81 // Adjust the position
82 QPointF oldPos(mousePosition.isNull() ? globalPos : mousePosition);
83 QPointF newPos = adjustedPositionForMovement(oldPos, localPos);
85 QScreen* currentScreen = screenAt(newPos);
86 if (currentScreen) {
87 QRect screenRect = currentScreen->geometry();
88 qreal unadjustedX = (oldPos + localPos).x();
89 if (unadjustedX < screenRect.left()) {
90 Q_EMIT pushedLeftBoundary(currentScreen, qAbs(unadjustedX - screenRect.left()), me->buttons());
91 } else if (unadjustedX > screenRect.right()) {
92 Q_EMIT pushedRightBoundary(currentScreen, qAbs(unadjustedX - screenRect.right()), me->buttons());
93 }
94 }
96 // Send the event
97 QMouseEvent eCopy(me->type(), me->localPos(), newPos, me->button(), me->buttons(), me->modifiers());
98 eCopy.setTimestamp(me->timestamp());
99 o->event(&eCopy);
100 return true;
101 }
102 default:
103 break;
104 }
105 return false;
108QPointF InputDispatcherFilter::adjustedPositionForMovement(const QPointF &pt, const QPointF &movement) const
110 QPointF adjusted = pt + movement;
112 auto screen = screenAt(adjusted); // first check if our move was to a valid screen.
113 if (screen) {
114 return adjusted;
115 } else if ((screen = screenAt(pt))) { // then check if our old position was valid
116 QRectF screenBounds = screen->geometry();
117 // bound the new position to the old screen geometry
118 adjusted.rx() = qMax(screenBounds.left(), qMin(adjusted.x(), screenBounds.right()-1));
119 adjusted.ry() = qMax(screenBounds.top(), qMin(adjusted.y(), screenBounds.bottom()-1));
120 } else {
121 auto screens = QGuiApplication::screens();
123 // center of first screen with a pointer.
124 Q_FOREACH(QScreen* screen, screens) {
125 Q_FOREACH(MousePointer* pointer, m_pointers) {
126 if (pointer->screen() == screen) {
127 return screen->geometry().center();
128 }
129 }
130 }
131 }
132 return adjusted;
135QScreen *InputDispatcherFilter::screenAt(const QPointF &pt) const
137 Q_FOREACH(MousePointer* pointer, m_pointers) {
138 QScreen* screen = pointer->screen();
139 if (screen && screen->geometry().contains(pt.toPoint()))
140 return screen;
141 }
142 return nullptr;