1/****************************************************************************
3** Copyright (C) 2014 Canonical, Ltd. and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
6** This file is part of the QtSystems module of the Qt Toolkit.
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.
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.
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.
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.
40****************************************************************************/
42#include "qinputdeviceinfo_linux_p.h"
45#include <libevdev/libevdev.h>
48#include <QSocketNotifier>
51QInputDeviceInfoPrivate::QInputDeviceInfoPrivate(QObject *parent) :
55 QTimer::singleShot(250, this, &QInputDeviceInfoPrivate::init);
58void QInputDeviceInfoPrivate::init()
63 struct udev_list_entry *devices, *dev_list_entry;
64 struct udev_device *dev;
66 QString subsystem = QStringLiteral("input");
67 struct udev_enumerate *enumerate = 0;
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());
77 udev_monitor_enable_receiving(udevMonitor);
78 notifierFd = udev_monitor_get_fd(udevMonitor);
80 notifier = new QSocketNotifier(notifierFd, QSocketNotifier::Read, this);
81 connect(notifier, &QSocketNotifier::activated, this, &QInputDeviceInfoPrivate::onUDevChanges);
84 udev_enumerate_scan_devices(enumerate);
85 devices = udev_enumerate_get_list_entry(enumerate);
87 udev_list_entry_foreach(dev_list_entry, devices) {
89 path = udev_list_entry_get_name(dev_list_entry);
91 dev = udev_device_new_from_syspath(udev, path);
93 QString eventPath = QString::fromLatin1(udev_device_get_sysname(dev));
95 if (qstrcmp(udev_device_get_subsystem(dev), "input") == 0 ) {
97 if (eventPath.contains(QStringLiteral("event"))) {
98 eventPath.prepend(QStringLiteral("/dev/input/"));
100 QInputDevice *iDevice = addDevice(eventPath);
104 iDevice->setTypes(getInputTypes(dev));
106 if (iDevice->switches().count() > 0 && iDevice->buttons().count() == 0)
107 iDevice->setTypes(iDevice->types() | QInputDeviceInfo::Switch);
109 if (iDevice->buttons().count() > 0 && iDevice->types() == QInputDeviceInfo::Unknown)
110 iDevice->setTypes(iDevice->types() | QInputDeviceInfo::Button);
112 deviceList.append(iDevice);
113 deviceMap.insert(eventPath,iDevice);
114 Q_EMIT newDevice(eventPath);
119 udev_enumerate_unref(enumerate);
124QInputDeviceInfo::InputTypes QInputDeviceInfoPrivate::getInputTypes( struct udev_device *dev)
126 QInputDeviceInfo::InputTypes types = QInputDeviceInfo::Unknown;
127 if (qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_KEYBOARD"), "1") == 0 )
128 types |= QInputDeviceInfo::Keyboard;
130 if (qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_MOUSE"), "1") == 0)
131 types |= QInputDeviceInfo::Mouse;
133 if (qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_TOUCHPAD"), "1") == 0)
134 types |= QInputDeviceInfo::TouchPad;
136 if (qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_TOUCHSCREEN"), "1") == 0
137 || qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_TABLET"), "1") == 0)
138 types |= QInputDeviceInfo::TouchScreen;
143QInputDevice *QInputDeviceInfoPrivate::addDevice(const QString &path)
145 QInputDevice *inputDevice = new QInputDevice(this);
146 inputDevice->setDevicePath(path);
148 struct libevdev *dev = NULL;
151 fd = open(path.toLatin1(), O_RDONLY|O_NONBLOCK);
156 rc = libevdev_new_from_fd(fd, &dev);
158 qWarning() << "Failed to init libevdev ("<< strerror(-rc) << ")";
162 inputDevice->setName(QString::fromLatin1(libevdev_get_name(dev)));
163 for (int i = 0; i < EV_MAX; i++) {
164 if (i == EV_KEY || i == EV_SW || i == EV_REL
165 || i == EV_REL || i == EV_ABS) {
166 for (int j = 0; j < libevdev_event_type_get_max(i); j++) {
167 if (libevdev_has_event_code(dev, i, j)) {
170 inputDevice->addButton(j);
173 inputDevice->addSwitch(j);
176 inputDevice->addRelativeAxis(j);
179 inputDevice->addAbsoluteAxis(j);
189void QInputDeviceInfoPrivate::removeDevice(const QString &path)
191 for (int i = 0; i < deviceList.size(); ++i) {
192 if (deviceList.at(i)->devicePath() == path) {
193 delete deviceList.takeAt(i);
194 deviceMap.remove(path);
195 Q_EMIT deviceRemoved(path);
200void QInputDeviceInfoPrivate::onUDevChanges()
202 struct udev_device *dev = udev_monitor_receive_device(udevMonitor);
204 if (qstrcmp(udev_device_get_subsystem(dev), "input") == 0 ) {
205 QString eventPath = QString::fromLatin1(udev_device_get_sysname(dev));
207 if (eventPath.contains(QStringLiteral("input")))
210 QString action = QString::fromStdString(udev_device_get_action(dev));
212 if (!eventPath.contains(QStringLiteral("/dev/input/")))
213 eventPath.prepend(QStringLiteral("/dev/input/"));
215 if (action == QStringLiteral("add")) {
217 QInputDevice *iDevice = addDevice(eventPath);
221 iDevice->setTypes(getInputTypes(dev));
222 udev_device_unref(dev);
224 if (iDevice->switches().count() > 0 && iDevice->buttons().count() == 0)
225 iDevice->setTypes(iDevice->types() | QInputDeviceInfo::Switch);
227 if (iDevice->buttons().count() > 0 && iDevice->types() == QInputDeviceInfo::Unknown)
228 iDevice->setTypes(iDevice->types() | QInputDeviceInfo::Button);
230 deviceList.append(iDevice);
231 deviceMap.insert(eventPath,iDevice);
233 Q_EMIT newDevice(eventPath);
235 } else if (action == QStringLiteral("remove")) {
236 removeDevice(eventPath);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list