@@ -0,0 +1,249 @@
1+/****************************************************************************
2+**
3+** Copyright (C) 2014 Canonical, Ltd. and/or its subsidiary(-ies).
4+** Contact: http://www.qt-project.org/legal
5+**
6+** This file is part of the QtSystems module of the Qt Toolkit.
7+**
8+** $QT_BEGIN_LICENSE:LGPL$
9+** Commercial License Usage
10+** Licensees holding valid commercial Qt licenses may use this file in
11+** accordance with the commercial license agreement provided with the
12+** Software or, alternatively, in accordance with the terms contained in
13+** a written agreement between you and Digia. For licensing terms and
14+** conditions see http://qt.digia.com/licensing. For further information
15+** use the contact form at http://qt.digia.com/contact-us.
16+**
17+** GNU Lesser General Public License Usage
18+** Alternatively, this file may be used under the terms of the GNU Lesser
19+** General Public License version 2.1 as published by the Free Software
20+** Foundation and appearing in the file LICENSE.LGPL included in the
21+** packaging of this file. Please review the following information to
22+** ensure the GNU Lesser General Public License version 2.1 requirements
23+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24+**
25+** In addition, as a special exception, Digia gives you certain additional
26+** rights. These rights are described in the Digia Qt LGPL Exception
27+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28+**
29+** GNU General Public License Usage
30+** Alternatively, this file may be used under the terms of the GNU
31+** General Public License version 3.0 as published by the Free Software
32+** Foundation and appearing in the file LICENSE.GPL included in the
33+** packaging of this file. Please review the following information to
34+** ensure the GNU General Public License version 3.0 requirements will be
35+** met: http://www.gnu.org/copyleft/gpl.html.
36+**
37+**
38+** $QT_END_LICENSE$
39+**
40+****************************************************************************/
41+
42+#include "qinputdeviceinfo_linux_p.h"
43+
44+#include <libudev.h>
45+#include <libevdev/libevdev.h>
46+#include <fcntl.h>
47+#include <QDebug>
48+#include <QSocketNotifier>
49+#include <QTimer>
50+
51+QInputDeviceInfoPrivate::QInputDeviceInfoPrivate(QObject *parent) :
52+ QObject(parent)
53+ , udev(0)
54+{
55+ QTimer::singleShot(250,this,SLOT(init()));
56+}
57+
58+void QInputDeviceInfoPrivate::init()
59+{
60+ if (!udev)
61+ udev = udev_new();
62+
63+ struct udev_list_entry *devices, *dev_list_entry;
64+ struct udev_device *dev;
65+
66+ QString subsystem = QStringLiteral("input");
67+ struct udev_enumerate *enumerate = 0;
68+
69+ if (udev) {
70+
71+ udevMonitor = udev_monitor_new_from_netlink(udev, "udev");
72+ udev_monitor_filter_add_match_subsystem_devtype(udevMonitor, subsystem.toLatin1(), NULL);
73+ enumerate = udev_enumerate_new(udev);
74+ udev_enumerate_add_match_subsystem(enumerate, subsystem.toLatin1());
75+
76+
77+ udev_monitor_enable_receiving(udevMonitor);
78+ notifierFd = udev_monitor_get_fd(udevMonitor);
79+
80+ notifier = new QSocketNotifier(notifierFd, QSocketNotifier::Read, this);
81+ connect(notifier, SIGNAL(activated(int)), this, SLOT(onUDevChanges()));
82+
83+
84+ udev_enumerate_scan_devices(enumerate);
85+ devices = udev_enumerate_get_list_entry(enumerate);
86+
87+ udev_list_entry_foreach(dev_list_entry, devices) {
88+ const char *path;
89+ path = udev_list_entry_get_name(dev_list_entry);
90+
91+ dev = udev_device_new_from_syspath(udev, path);
92+
93+ QString eventPath = QString::fromLatin1(udev_device_get_sysname(dev));
94+
95+ if (qstrcmp(udev_device_get_subsystem(dev), "input") == 0 ) {
96+
97+ if (eventPath.contains(QStringLiteral("event"))) {
98+ eventPath.prepend(QStringLiteral("/dev/input/"));
99+
100+ QInputDevice *iDevice = addDevice(eventPath);
101+ if (!iDevice)
102+ continue;
103+
104+ qDebug() << "*** ADDING DEVICE" << eventPath;
105+
106+ iDevice->setTypes(getInputTypes(dev));
107+
108+ if (iDevice->switches().count() > 0 && iDevice->buttons().count() == 0)
109+ iDevice->setTypes(iDevice->types() | QInputDeviceInfo::Switch);
110+
111+ if (iDevice->buttons().count() > 0 && iDevice->types() == QInputDeviceInfo::Unknown)
112+ iDevice->setTypes(iDevice->types() | QInputDeviceInfo::Button);
113+
114+ deviceList.append(iDevice);
115+ deviceMap.insert(eventPath,iDevice);
116+ Q_EMIT newDevice(eventPath);
117+
118+ }
119+ }
120+ }
121+ udev_enumerate_unref(enumerate);
122+ }
123+ Q_EMIT ready();
124+}
125+
126+QInputDeviceInfo::InputTypes QInputDeviceInfoPrivate::getInputTypes( struct udev_device *dev)
127+{
128+ qDebug() << "******* have input type Keyboard" << udev_device_get_property_value(dev, "ID_INPUT_KEYBOARD");
129+ qDebug() << "******* have input type Mouse" << udev_device_get_property_value(dev, "ID_INPUT_MOUSE");
130+ qDebug() << "******* have input type Touchpad" << udev_device_get_property_value(dev, "ID_INPUT_TOUCHPAD");
131+ qDebug() << "******* have input type Touchscreen" << udev_device_get_property_value(dev, "ID_INPUT_TOUCHSCREEN");
132+ qDebug() << "******* have input type Tablet" << udev_device_get_property_value(dev, "ID_INPUT_TABLET");
133+ qDebug() << "******* have input type joystick" << udev_device_get_property_value(dev, "ID_INPUT_JOYSTICK");
134+ QInputDeviceInfo::InputTypes types = QInputDeviceInfo::Unknown;
135+ if (qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_KEYBOARD"), "1") == 0 )
136+ types |= QInputDeviceInfo::Keyboard;
137+
138+ if (qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_MOUSE"), "1") == 0)
139+ types |= QInputDeviceInfo::Mouse;
140+
141+ if (qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_TOUCHPAD"), "1") == 0)
142+ types |= QInputDeviceInfo::TouchPad;
143+
144+ if (qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_TOUCHSCREEN"), "1") == 0
145+ || qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_TABLET"), "1") == 0)
146+ types |= QInputDeviceInfo::TouchScreen;
147+
148+ return types;
149+}
150+
151+QInputDevice *QInputDeviceInfoPrivate::addDevice(const QString &path)
152+{
153+ QInputDevice *inputDevice = new QInputDevice(this);
154+
155+ struct libevdev *dev = NULL;
156+ int fd;
157+ int rc = 1;
158+ fd = open(path.toLatin1(), O_RDONLY|O_NONBLOCK);
159+
160+ if (fd == -1) {
161+ qDebug() << "Failed to open";
162+ return inputDevice;
163+ }
164+ rc = libevdev_new_from_fd(fd, &dev);
165+ if (rc < 0) {
166+ qDebug() << "Failed to init libevdev ("<< strerror(-rc) << ")";
167+ return inputDevice;
168+ }
169+
170+ inputDevice->setName(QString::fromLatin1(libevdev_get_name(dev)));
171+ inputDevice->setDevicePath(path);
172+ for (int i = 0; i < EV_MAX; i++) {
173+ if (i == EV_KEY || i == EV_SW || i == EV_REL
174+ || i == EV_REL || i == EV_ABS) {
175+ for (int j = 0; j < libevdev_event_type_get_max(i); j++) {
176+ if (libevdev_has_event_code(dev, i, j)) {
177+ switch (i) {
178+ case EV_KEY:
179+ inputDevice->addButton(j);
180+ break;
181+ case EV_SW:
182+ inputDevice->addSwitch(j);
183+ break;
184+ case EV_REL:
185+ inputDevice->addRelativeAxis(j);
186+ break;
187+ case EV_ABS:
188+ inputDevice->addAbsoluteAxis(j);
189+ break;
190+ };
191+ }
192+ }
193+ }
194+ }
195+ return inputDevice;
196+}
197+
198+void QInputDeviceInfoPrivate::removeDevice(const QString &path)
199+{
200+ for (int i = 0; i < deviceList.size(); ++i) {
201+ if (deviceList.at(i)->devicePath() == path) {
202+ delete deviceList.takeAt(i);
203+ deviceMap.remove(path);
204+ Q_EMIT deviceRemoved(path);
205+ }
206+ }
207+}
208+
209+void QInputDeviceInfoPrivate::onUDevChanges()
210+{
211+ struct udev_device *dev = udev_monitor_receive_device(udevMonitor);
212+ if (dev) {
213+ if (qstrcmp(udev_device_get_subsystem(dev), "input") == 0 ) {
214+ QString eventPath = QString::fromLatin1(udev_device_get_sysname(dev));
215+
216+ if (eventPath.contains(QStringLiteral("input")))
217+ return;
218+
219+ QString action = QString::fromStdString(udev_device_get_action(dev));
220+
221+ if (!eventPath.contains(QStringLiteral("/dev/input/")))
222+ eventPath.prepend(QStringLiteral("/dev/input/"));
223+
224+ if (action == QStringLiteral("add")) {
225+
226+ QInputDevice *iDevice = addDevice(eventPath);
227+ if (!iDevice)
228+ return;
229+
230+ iDevice->setTypes(getInputTypes(dev));
231+ udev_device_unref(dev);
232+
233+ if (iDevice->switches().count() > 0 && iDevice->buttons().count() == 0)
234+ iDevice->setTypes(iDevice->types() | QInputDeviceInfo::Switch);
235+
236+ if (iDevice->buttons().count() > 0 && iDevice->types() == QInputDeviceInfo::Unknown)
237+ iDevice->setTypes(iDevice->types() | QInputDeviceInfo::Button);
238+
239+ deviceList.append(iDevice);
240+ deviceMap.insert(eventPath,iDevice);
241+
242+ Q_EMIT newDevice(eventPath);
243+
244+ } else if (action == QStringLiteral("remove")) {
245+ removeDevice(eventPath);
246+ }
247+ }
248+ }
249+}