Revert "Move QWindowSystemInterface out of qpa."
[profile/ivi/qtbase.git] / src / platformsupport / input / evdevmouse / qevdevmousemanager.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qevdevmousemanager_p.h"
43
44 #include <QStringList>
45 #include <QGuiApplication>
46 #include <QScreen>
47 #include <qpa/qwindowsysteminterface.h>
48
49 //#define QT_QPA_MOUSEMANAGER_DEBUG
50
51 #ifdef QT_QPA_MOUSEMANAGER_DEBUG
52 #include <QDebug>
53 #endif
54
55 QT_BEGIN_NAMESPACE
56
57 QEvdevMouseManager::QEvdevMouseManager(const QString &key, const QString &specification, QObject *parent)
58     : QObject(parent), m_x(0), m_y(0), m_xoffset(0), m_yoffset(0)
59 {
60     Q_UNUSED(key);
61
62     QStringList args = specification.split(QLatin1Char(':'));
63     QStringList devices;
64
65     foreach (const QString &arg, args) {
66         if (arg.startsWith(QLatin1String("/dev/"))) {
67             // if device is specified try to use it
68             devices.append(arg);
69             args.removeAll(arg);
70         } else if (arg.startsWith(QLatin1String("xoffset="))) {
71             m_xoffset = arg.mid(8).toInt();
72         } else if (arg.startsWith(QLatin1String("yoffset="))) {
73             m_yoffset = arg.mid(8).toInt();
74         }
75     }
76
77     // build new specification without /dev/ elements
78     m_spec = args.join(QLatin1String(":"));
79
80     // add all mice for devices specified in the argument list
81     foreach (const QString &device, devices)
82         addMouse(device);
83
84     if (devices.isEmpty()) {
85 #ifdef QT_QPA_MOUSEMANAGER_DEBUG
86         qWarning() << "Use device discovery";
87 #endif
88
89         m_deviceDiscovery = QDeviceDiscovery::create(QDeviceDiscovery::Device_Mouse | QDeviceDiscovery::Device_Touchpad, this);
90         if (m_deviceDiscovery) {
91             // scan and add already connected keyboards
92             QStringList devices = m_deviceDiscovery->scanConnectedDevices();
93             foreach (const QString &device, devices) {
94                 addMouse(device);
95             }
96
97             connect(m_deviceDiscovery, SIGNAL(deviceDetected(QString)), this, SLOT(addMouse(QString)));
98             connect(m_deviceDiscovery, SIGNAL(deviceRemoved(QString)), this, SLOT(removeMouse(QString)));
99         }
100     }
101 }
102
103 QEvdevMouseManager::~QEvdevMouseManager()
104 {
105     qDeleteAll(m_mice);
106     m_mice.clear();
107 }
108
109 void QEvdevMouseManager::handleMouseEvent(int x, int y, Qt::MouseButtons buttons)
110 {
111     // update current absolute coordinates
112     m_x += x;
113     m_y += y;
114
115     // clamp to screen geometry
116     QRect g = QGuiApplication::primaryScreen()->virtualGeometry();
117     if (m_x + m_xoffset < g.left())
118         m_x = g.left() - m_xoffset;
119     else if (m_x + m_xoffset > g.right())
120         m_x = g.right() - m_xoffset;
121
122     if (m_y + m_yoffset < g.top())
123         m_y = g.top() - m_yoffset;
124     else if (m_y + m_yoffset > g.bottom())
125         m_y = g.bottom() - m_yoffset;
126
127     QPoint pos(m_x + m_xoffset, m_y + m_yoffset);
128     QWindowSystemInterface::handleMouseEvent(0, pos, pos, buttons);
129
130 #ifdef QT_QPA_MOUSEMANAGER_DEBUG
131     qDebug("mouse event %d %d %d", pos.x(), pos.y(), int(buttons));
132 #endif
133 }
134
135 void QEvdevMouseManager::handleWheelEvent(int delta, Qt::Orientation orientation)
136 {
137     QPoint pos(m_x + m_xoffset, m_y + m_yoffset);
138     QWindowSystemInterface::handleWheelEvent(0, pos, pos, delta, orientation);
139
140 #ifdef QT_QPA_MOUSEMANAGER_DEBUG
141     qDebug("mouse wheel event %dx%d %d %d", pos.x(), pos.y(), delta, int(orientation));
142 #endif
143 }
144
145 void QEvdevMouseManager::addMouse(const QString &deviceNode)
146 {
147 #ifdef QT_QPA_MOUSEMANAGER_DEBUG
148     qWarning() << "Adding mouse at" << deviceNode;
149 #endif
150
151     QEvdevMouseHandler *handler;
152     handler = QEvdevMouseHandler::create(deviceNode, m_spec);
153     if (handler) {
154         connect(handler, SIGNAL(handleMouseEvent(int,int,Qt::MouseButtons)), this, SLOT(handleMouseEvent(int,int,Qt::MouseButtons)));
155         connect(handler, SIGNAL(handleWheelEvent(int,Qt::Orientation)), this, SLOT(handleWheelEvent(int,Qt::Orientation)));
156         m_mice.insert(deviceNode, handler);
157     } else {
158         qWarning("Failed to open mouse");
159     }
160 }
161
162 void QEvdevMouseManager::removeMouse(const QString &deviceNode)
163 {
164     if (m_mice.contains(deviceNode)) {
165 #ifdef QT_QPA_MOUSEMANAGER_DEBUG
166         qWarning() << "Removing mouse at" << deviceNode;
167 #endif
168         QEvdevMouseHandler *handler = m_mice.value(deviceNode);
169         m_mice.remove(deviceNode);
170         delete handler;
171     }
172 }
173
174 QT_END_NAMESPACE