on 1ea9117 help sign in
unity8/plugins/Unity/InputInfo/linux/qinputdeviceinfo_linux.cpp
292 lines 10 kB history raw
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****************************************************************************/
42#include "qinputdeviceinfo_linux_p.h"
44#include <libudev.h>
45#include <libevdev/libevdev.h>
46#include <fcntl.h>
47#include <QDebug>
48#include <QSocketNotifier>
49#include <QTimer>
50#include <QDir>
52QInputDeviceManagerPrivate::QInputDeviceManagerPrivate(QObject *parent) :
53 QObject(parent),
54 currentFilter(QInputDevice::Unknown),
55 udevMonitor(0),
56 udevice(0)
58 QTimer::singleShot(250,this,SLOT(init()));
61QInputDeviceManagerPrivate::~QInputDeviceManagerPrivate()
63 udev_unref(udevice);
64 udev_monitor_unref(udevMonitor);
67void QInputDeviceManagerPrivate::init()
69 if (!udevice)
70 udevice = udev_new();
72 udev_list_entry *devices;
73 udev_list_entry *dev_list_entry;
74 udev_device *dev;
76 QString subsystem = QStringLiteral("input");
77 struct udev_enumerate *enumerate = 0;
79 if (udevice) {
81 udevMonitor = udev_monitor_new_from_netlink(udevice, "udev");
82 udev_monitor_filter_add_match_subsystem_devtype(udevMonitor, subsystem.toLatin1(), NULL);
83 enumerate = udev_enumerate_new(udevice);
84 udev_enumerate_add_match_subsystem(enumerate, subsystem.toLatin1());
86 udev_monitor_enable_receiving(udevMonitor);
87 notifierFd = udev_monitor_get_fd(udevMonitor);
89 notifier = new QSocketNotifier(notifierFd, QSocketNotifier::Read, this);
90 connect(notifier, SIGNAL(activated(int)), this, SLOT(onUDevChanges()));
92 udev_enumerate_scan_devices(enumerate);
93 devices = udev_enumerate_get_list_entry(enumerate);
95 udev_list_entry_foreach(dev_list_entry, devices) {
96 const char *path;
97 path = udev_list_entry_get_name(dev_list_entry);
99 dev = udev_device_new_from_syspath(udevice, path);
100 if (qstrcmp(udev_device_get_subsystem(dev), "input") == 0 ) {
101 QInputDevice *iDevice = addDevice(dev);
102 if (iDevice && !iDevice->devicePath().isEmpty()) {
103 deviceMap.insert(iDevice->devicePath(),iDevice);
104 }
105 }
106 udev_device_unref(dev);
107 }
108 udev_enumerate_unref(enumerate);
109 }
110 // udev_unref(udevice);
111 Q_FOREACH (const QString &devicePath, deviceMap.keys()) {
112 Q_EMIT deviceAdded(devicePath);
113 }
114 Q_EMIT ready();
117QInputDevice::InputTypeFlags QInputDeviceManagerPrivate::getInputTypeFlags(struct udev_device *dev)
119 QInputDevice::InputTypeFlags flags = QInputDevice::Unknown;
120 if (qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_KEY"), "1") == 0 ) {
121 flags |= QInputDevice::Button;
122 }
123 if (qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_MOUSE"), "1") == 0) {
124 flags |= QInputDevice::Mouse;
125 }
126 if (qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_TOUCHPAD"), "1") == 0) {
127 flags |= QInputDevice::TouchPad;
128 }
129 if (qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_TOUCHSCREEN"), "1") == 0
130 || qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_TABLET"), "1") == 0) {
131 flags |= QInputDevice::TouchScreen;
132 }
133 if (qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_KEYBOARD"), "1") == 0 ) {
134 flags |= QInputDevice::Keyboard;
135 }
136 if (!QString::fromLatin1(udev_device_get_property_value(dev, "SW")).isEmpty()) {
137 flags |= QInputDevice::Switch;
138 }
140 return flags;
143QInputDevice *QInputDeviceManagerPrivate::addDevice(struct udev_device *udev)
145 QString eventPath = QString::fromLatin1(udev_device_get_sysname(udev));
147 if (eventPath.contains(QStringLiteral("event")))
148 eventPath.prepend(QStringLiteral("/dev/input/"));
150 if (deviceMap.contains(eventPath)) {
151 return Q_NULLPTR;
152 }
153 struct libevdev *dev = NULL;
154 int fd;
155 int rc = 1;
156 QInputDevice *inputDevice;
157 inputDevice = addUdevDevice(udev);
158 if (!inputDevice) {
159 return Q_NULLPTR;
160 }
161 eventPath = inputDevice->devicePath();
163 qDebug() << "Input device added:" << inputDevice->name() << inputDevice->devicePath() << inputDevice->type();
165 fd = open(eventPath.toLatin1(), O_RDONLY|O_NONBLOCK);
166 if (fd == -1) {
167 return inputDevice;
168 }
169 rc = libevdev_new_from_fd(fd, &dev);
170 if (rc < 0) {
171 qWarning() << "Failed to init libevdev ("<< strerror(-rc) << ")";
172 return Q_NULLPTR;
173 }
175 for (int i = 0; i < EV_MAX; i++) {
176 if (i == EV_KEY || i == EV_SW || i == EV_REL
177 || i == EV_REL || i == EV_ABS) {
178 for (int j = 0; j < libevdev_event_type_get_max(i); j++) {
179 if (libevdev_has_event_code(dev, i, j)) {
180 switch (i) {
181 case EV_KEY:
182 inputDevice->addButton(j);
183 break;
184 case EV_SW:
185 inputDevice->addSwitch(j);
186 break;
187 case EV_REL:
188 inputDevice->addRelativeAxis(j);
189 break;
190 case EV_ABS:
191 inputDevice->addAbsoluteAxis(j);
192 break;
193 };
194 }
195 }
196 }
197 }
199 return inputDevice;
202void QInputDeviceManagerPrivate::addDetails(struct udev_device *)
206void QInputDeviceManagerPrivate::removeDevice(const QString &path)
208 // this path is not a full evdev path
209 Q_FOREACH (const QString devicePath, deviceMap.keys()) {
210 if (devicePath.contains(path)) {
211 qDebug() << "Input device removed:" << deviceMap.value(devicePath)->name() << devicePath << deviceMap.value(devicePath)->type();
212 deviceMap.remove(devicePath);
213 Q_EMIT deviceRemoved(devicePath);
214 }
215 }
218QInputDevice *QInputDeviceManagerPrivate::addUdevDevice(struct udev_device *udev)
220 QInputDevice *iDevice;
222 struct udev_list_entry *list;
223 struct udev_list_entry *node;
225 list = udev_device_get_properties_list_entry (udev);
226 QString syspath = QString::fromLatin1(udev_device_get_syspath(udev));
227 QDir sysdir(syspath);
229 QStringList infoList = sysdir.entryList(QStringList() << QStringLiteral("event*"),QDir::Dirs);
231 if (infoList.count() > 0) {
232 QString token = infoList.at(0);
234 token.prepend(QStringLiteral("/dev/input/"));
235 iDevice = new QInputDevice(this);
236 iDevice->setDevicePath(token);
237 } else {
238 return Q_NULLPTR;
239 }
240 udev_list_entry_foreach (node, list) {
242 QString key = QString::fromLatin1(udev_list_entry_get_name(node));
243 QString value = QString::fromLatin1(udev_list_entry_get_value(node));
245 if (key == QStringLiteral("NAME")) {
246 iDevice->setName(value.remove(QStringLiteral("\"")));
247 }
248 }
249 iDevice->setType(getInputTypeFlags(udev));
250 return iDevice;
253void QInputDeviceManagerPrivate::onUDevChanges()
255 if (!udevMonitor)
256 return;
258 udev_device *dev = udev_monitor_receive_device(udevMonitor);
260 if (dev) {
261 if (qstrcmp(udev_device_get_subsystem(dev), "input") == 0 ) {
262 QString eventPath = QString::fromLatin1(udev_device_get_sysname(dev));
264 QString action = QString::fromStdString(udev_device_get_action(dev));
266 if (!eventPath.contains(QStringLiteral("/dev/input/")))
267 eventPath.prepend(QStringLiteral("/dev/input/"));
269 if (action == QStringLiteral("add")) {
270 if (deviceMap.contains(eventPath)){
271 udev_device_unref(dev);
272 return;
273 }
275 QInputDevice *iDevice = addDevice(dev);
276 if (!iDevice) {
277 delete iDevice;
278 return;
279 }
280 iDevice->setType(getInputTypeFlags(dev));
281 udev_device_unref(dev);
283 deviceMap.insert(eventPath,iDevice);
285 Q_EMIT deviceAdded(eventPath);
287 } else if (action == QStringLiteral("remove")) {
288 removeDevice(eventPath);
289 }
290 }
291 }