Debian Codegraph @

src/systeminfo/linux/qinputinfomanagerudev.cpp

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 Canonical, Ltd. and/or its subsidiary(-ies).
4 ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies).
5 ** Contact: http://www.qt-project.org/legal
6 **
7 ** This file is part of the QtSystems module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** Commercial License Usage
11 ** Licensees holding valid commercial Qt licenses may use this file in
12 ** accordance with the commercial license agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and The Qt Company. For licensing terms
15 ** and conditions see https://www.qt.io/terms-conditions. For further
16 ** information use the contact form at https://www.qt.io/contact-us.
17 **
18 ** GNU Lesser General Public License Usage
19 ** Alternatively, this file may be used under the terms of the GNU Lesser
20 ** General Public License version 3 as published by the Free Software
21 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
22 ** packaging of this file. Please review the following information to
23 ** ensure the GNU Lesser General Public License version 3 requirements
24 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25 **
26 ** GNU General Public License Usage
27 ** Alternatively, this file may be used under the terms of the GNU
28 ** General Public License version 2.0 or (at your option) the GNU General
29 ** Public license version 3 or any later version approved by the KDE Free
30 ** Qt Foundation. The licenses are as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32 ** included in the packaging of this file. Please review the following
33 ** information to ensure the GNU General Public License requirements will
34 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35 ** https://www.gnu.org/licenses/gpl-3.0.html.
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include "qinputinfomanagerudev_p.h"
42
43 #include <libudev.h>
44 #ifndef QT_NO_EVDEV
45 #include <libevdev/libevdev.h>
46 #endif
47 #include <fcntl.h>
48 #include <QDebug>
49 #include <QDir>
50 #include <QSocketNotifier>
51 #include <QTimer>
52
53 QInputInfoManagerUdev::QInputInfoManagerUdev(QObject *parent) :
54 QInputInfoManagerPrivate(parent),
55 udevice(0)
56 {
57 QTimer::singleShot(250,this,SLOT(init()));
58 }
59
60 QInputInfoManagerUdev::~QInputInfoManagerUdev()
61 {
62 udev_unref(udevice);
63 udev_monitor_unref(udevMonitor);
64 }
65
66 void QInputInfoManagerUdev::init()
67 {
68 if (!udevice)
69 udevice = udev_new();
70
71 udev_list_entry *devices;
72 udev_list_entry *dev_list_entry;
73 udev_device *dev;
74
75 QString subsystem = QStringLiteral("input");
76 struct udev_enumerate *enumerate = 0;
77
78 if (udevice) {
79
80 udevMonitor = udev_monitor_new_from_netlink(udevice, "udev");
81 if (udevMonitor) {
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());
85
86 udev_monitor_enable_receiving(udevMonitor);
87 notifierFd = udev_monitor_get_fd(udevMonitor);
88
89 notifier = new QSocketNotifier(notifierFd, QSocketNotifier::Read, this);
90 connect(notifier, SIGNAL(activated(int)), this, SLOT(onUDevChanges()));
91
92 udev_enumerate_scan_devices(enumerate);
93 devices = udev_enumerate_get_list_entry(enumerate);
94
95 udev_list_entry_foreach(dev_list_entry, devices) {
96 const char *path;
97 path = udev_list_entry_get_name(dev_list_entry);
98
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->identifier().isEmpty()) {
103 deviceMap.insert(iDevice->identifier(),iDevice);
104 }
105 }
106 udev_device_unref(dev);
107 }
108 udev_enumerate_unref(enumerate);
109 }
110 }
111 // udev_unref(udevice);
112 Q_FOREACH (const QString &devicePath, deviceMap.keys()) {
113 Q_EMIT deviceAdded(deviceMap.value(devicePath));
114 }
115 Q_EMIT ready();
116 }
117
118 QInputDevice::InputTypeFlags QInputInfoManagerUdev::getInputTypeFlags(struct udev_device *dev)
119 {
120 QInputDevice::InputTypeFlags flags = QInputDevice::UnknownType;
121 if (qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_KEY"), "1") == 0 ) {
122 flags |= QInputDevice::Button;
123 }
124 if (qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_MOUSE"), "1") == 0) {
125 flags |= QInputDevice::Mouse;
126 }
127 if (qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_TOUCHPAD"), "1") == 0) {
128 flags |= QInputDevice::TouchPad;
129 }
130 if (qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_TOUCHSCREEN"), "1") == 0
131 || qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_TABLET"), "1") == 0) {
132 flags |= QInputDevice::TouchScreen;
133 }
134 if (qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_KEYBOARD"), "1") == 0 ) {
135 flags |= QInputDevice::Keyboard;
136 }
137 if (!QString::fromLatin1(udev_device_get_property_value(dev, "SW")).isEmpty()) {
138 flags |= QInputDevice::Switch;
139 }
140
141 return flags;
142 }
143
144 QInputDevice *QInputInfoManagerUdev::addDevice(struct udev_device *udev)
145 {
146 QString eventPath = QString::fromLatin1(udev_device_get_sysname(udev));
147
148 if (eventPath.contains(QStringLiteral("event")))
149 eventPath.prepend(QStringLiteral("/dev/input/"));
150
151 if (deviceMap.contains(eventPath)) {
152 return Q_NULLPTR;
153 }
154 QInputDevice *inputDevice;
155 inputDevice = addUdevDevice(udev);
156 if (!inputDevice) {
157 return Q_NULLPTR;
158 }
159 eventPath = inputDevice->identifier();
160
161 int fd = open(eventPath.toLatin1(), O_RDONLY|O_NONBLOCK);
162 if (fd == -1) {
163 return inputDevice;
164 }
165 #ifndef QT_NO_EVDEV
166 struct libevdev *dev = NULL;
167 int rc = 1;
168 rc = libevdev_new_from_fd(fd, &dev);
169 if (rc < 0) {
170 qWarning() << "Failed to init libevdev ("<< strerror(-rc) << ")";
171 return Q_NULLPTR;
172 }
173
174 for (int i = 0; i < EV_MAX; i++) {
175 if (i == EV_KEY || i == EV_SW || i == EV_REL
176 || i == EV_REL || i == EV_ABS) {
177 for (int j = 0; j < libevdev_event_type_get_max(i); j++) {
178 if (libevdev_has_event_code(dev, i, j)) {
179 switch (i) {
180 case EV_KEY:
181 inputDevice->addButton(j);
182 break;
183 case EV_SW:
184 inputDevice->addSwitch(j);
185 break;
186 case EV_REL:
187 inputDevice->addRelativeAxis(j);
188 break;
189 case EV_ABS:
190 inputDevice->addAbsoluteAxis(j);
191 break;
192 };
193 }
194 }
195 }
196 }
197 #endif
198 return inputDevice;
199 }
200
201 void QInputInfoManagerUdev::addDetails(struct udev_device *)
202 {
203 }
204
205 void QInputInfoManagerUdev::removeDevice(const QString &path)
206 {
207 // this path is not a full evdev path
208 Q_FOREACH (const QString devicePath, deviceMap.keys()) {
209 if (devicePath.contains(path)) {
210 QInputDevice *removedDevice = deviceMap.take(devicePath);
211 removedDevice->deleteLater();
212 Q_EMIT deviceRemoved(devicePath);
213 }
214 }
215 }
216
217 QInputDevice *QInputInfoManagerUdev::addUdevDevice(struct udev_device *udev)
218 {
219 QInputDevice *iDevice;
220
221 struct udev_list_entry *list;
222 struct udev_list_entry *node;
223
224 list = udev_device_get_properties_list_entry (udev);
225 QString syspath = QString::fromLatin1(udev_device_get_syspath(udev));
226 QDir sysdir(syspath);
227
228 QStringList infoList = sysdir.entryList(QStringList() << QStringLiteral("event*"),QDir::Dirs);
229
230 if (infoList.count() > 0) {
231 QString token = infoList.at(0);
232
233 token.prepend(QStringLiteral("/dev/input/"));
234 iDevice = new QInputDevice(this);
235 iDevice->setIdentifier(token);
236 } else {
237 return Q_NULLPTR;
238 }
239 udev_list_entry_foreach (node, list) {
240
241 QString key = QString::fromLatin1(udev_list_entry_get_name(node));
242 QString value = QString::fromLatin1(udev_list_entry_get_value(node));
243
244 if (key == QStringLiteral("NAME")) {
245 iDevice->setName(value.remove(QStringLiteral("\"")));
246 }
247 }
248 iDevice->setTypes(getInputTypeFlags(udev));
249 return iDevice;
250 }
251
252 void QInputInfoManagerUdev::onUDevChanges()
253 {
254 if (!udevMonitor)
255 return;
256
257 udev_device *dev = udev_monitor_receive_device(udevMonitor);
258
259 if (dev) {
260 if (qstrcmp(udev_device_get_subsystem(dev), "input") == 0 ) {
261 QString eventPath = QString::fromLatin1(udev_device_get_sysname(dev));
262
263 QString action = QString::fromStdString(udev_device_get_action(dev));
264
265 if (!eventPath.contains(QStringLiteral("/dev/input/")))
266 eventPath.prepend(QStringLiteral("/dev/input/"));
267
268 if (action == QStringLiteral("add")) {
269 if (deviceMap.contains(eventPath)){
270 udev_device_unref(dev);
271 return;
272 }
273
274 QInputDevice *iDevice = addDevice(dev);
275 if (!iDevice) {
276 delete iDevice;
277 return;
278 }
279 iDevice->setTypes(getInputTypeFlags(dev));
280 udev_device_unref(dev);
281 deviceMap.insert(eventPath,iDevice);
282 Q_EMIT deviceAdded(deviceMap.value(eventPath));
283
284 } else if (action == QStringLiteral("remove")) {
285 removeDevice(eventPath);
286 }
287 }
288 }
289 }