1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtGui module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include "qevdevmousemanager_p.h"
44 #include <QStringList>
45 #include <QGuiApplication>
47 #include <QtGui/qwindowsysteminterface.h>
49 //#define QT_QPA_MOUSEMANAGER_DEBUG
51 #ifdef QT_QPA_MOUSEMANAGER_DEBUG
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)
62 QStringList args = specification.split(QLatin1Char(':'));
65 foreach (const QString &arg, args) {
66 if (arg.startsWith(QLatin1String("/dev/"))) {
67 // if device is specified try to use it
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();
77 // build new specification without /dev/ elements
78 m_spec = args.join(QLatin1String(":"));
80 // add all mice for devices specified in the argument list
81 foreach (const QString &device, devices)
84 if (devices.isEmpty()) {
85 #ifdef QT_QPA_MOUSEMANAGER_DEBUG
86 qWarning() << "Use device discovery";
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) {
97 connect(m_deviceDiscovery, SIGNAL(deviceDetected(QString)), this, SLOT(addMouse(QString)));
98 connect(m_deviceDiscovery, SIGNAL(deviceRemoved(QString)), this, SLOT(removeMouse(QString)));
103 QEvdevMouseManager::~QEvdevMouseManager()
109 void QEvdevMouseManager::handleMouseEvent(int x, int y, Qt::MouseButtons buttons)
111 // update current absolute coordinates
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;
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;
127 QPoint pos(m_x + m_xoffset, m_y + m_yoffset);
128 QWindowSystemInterface::handleMouseEvent(0, pos, pos, buttons);
130 #ifdef QT_QPA_MOUSEMANAGER_DEBUG
131 qDebug("mouse event %d %d %d", pos.x(), pos.y(), int(buttons));
135 void QEvdevMouseManager::handleWheelEvent(int delta, Qt::Orientation orientation)
137 QPoint pos(m_x + m_xoffset, m_y + m_yoffset);
138 QWindowSystemInterface::handleWheelEvent(0, pos, pos, delta, orientation);
140 #ifdef QT_QPA_MOUSEMANAGER_DEBUG
141 qDebug("mouse wheel event %dx%d %d %d", pos.x(), pos.y(), delta, int(orientation));
145 void QEvdevMouseManager::addMouse(const QString &deviceNode)
147 #ifdef QT_QPA_MOUSEMANAGER_DEBUG
148 qWarning() << "Adding mouse at" << deviceNode;
151 QEvdevMouseHandler *handler;
152 handler = QEvdevMouseHandler::create(deviceNode, m_spec);
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);
158 qWarning("Failed to open mouse");
162 void QEvdevMouseManager::removeMouse(const QString &deviceNode)
164 if (m_mice.contains(deviceNode)) {
165 #ifdef QT_QPA_MOUSEMANAGER_DEBUG
166 qWarning() << "Removing mouse at" << deviceNode;
168 QEvdevMouseHandler *handler = m_mice.value(deviceNode);
169 m_mice.remove(deviceNode);