1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the plugins 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 The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qdevicediscovery_static_p.h"
41
42 #include <QStringList>
43 #include <QCoreApplication>
44 #include <QObject>
45 #include <QHash>
46 #include <QDir>
47 #include <QLoggingCategory>
48 #include <QtCore/private/qcore_unix_p.h>
49
50 #ifdef Q_OS_FREEBSD
51 #include <dev/evdev/input.h>
52 #else
53 #include <linux/input.h>
54 #endif
55 #include <fcntl.h>
56
57 /* android (and perhaps some other linux-derived stuff) don't define everything
58 * in linux/input.h, so we'll need to do that ourselves.
59 */
60 #ifndef KEY_CNT
61 #define KEY_CNT (KEY_MAX+1)
62 #endif
63 #ifndef REL_CNT
64 #define REL_CNT (REL_MAX+1)
65 #endif
66 #ifndef ABS_CNT
67 #define ABS_CNT (ABS_MAX+1)
68 #endif
69 #ifndef ABS_MT_POSITION_X
70 #define ABS_MT_POSITION_X 0x35
71 #endif
72 #ifndef ABS_MT_POSITION_Y
73 #define ABS_MT_POSITION_Y 0x36
74 #endif
75
76 #define LONG_BITS (sizeof(long) * 8 )
77 #define LONG_FIELD_SIZE(bits) ((bits / LONG_BITS) + 1)
78
testBit(long bit,const long * field)79 static bool testBit(long bit, const long *field)
80 {
81 return (field[bit / LONG_BITS] >> bit % LONG_BITS) & 1;
82 }
83
84 QT_BEGIN_NAMESPACE
85
86 Q_LOGGING_CATEGORY(lcDD, "qt.qpa.input")
87
create(QDeviceTypes types,QObject * parent)88 QDeviceDiscovery *QDeviceDiscovery::create(QDeviceTypes types, QObject *parent)
89 {
90 return new QDeviceDiscoveryStatic(types, parent);
91 }
92
QDeviceDiscoveryStatic(QDeviceTypes types,QObject * parent)93 QDeviceDiscoveryStatic::QDeviceDiscoveryStatic(QDeviceTypes types, QObject *parent)
94 : QDeviceDiscovery(types, parent)
95 {
96 qCDebug(lcDD) << "static device discovery for type" << types;
97 }
98
scanConnectedDevices()99 QStringList QDeviceDiscoveryStatic::scanConnectedDevices()
100 {
101 QStringList devices;
102 QDir dir;
103 dir.setFilter(QDir::System);
104
105 // check for input devices
106 if (m_types & Device_InputMask) {
107 dir.setPath(QString::fromLatin1(QT_EVDEV_DEVICE_PATH));
108 const auto deviceFiles = dir.entryList();
109 for (const QString &deviceFile : deviceFiles) {
110 QString absoluteFilePath = dir.absolutePath() + QLatin1Char('/') + deviceFile;
111 if (checkDeviceType(absoluteFilePath))
112 devices << absoluteFilePath;
113 }
114 }
115
116 // check for drm devices
117 if (m_types & Device_VideoMask) {
118 dir.setPath(QString::fromLatin1(QT_DRM_DEVICE_PATH));
119 const auto deviceFiles = dir.entryList();
120 for (const QString &deviceFile : deviceFiles) {
121 QString absoluteFilePath = dir.absolutePath() + QLatin1Char('/') + deviceFile;
122 if (checkDeviceType(absoluteFilePath))
123 devices << absoluteFilePath;
124 }
125 }
126
127 qCDebug(lcDD) << "Found matching devices" << devices;
128
129 return devices;
130 }
131
checkDeviceType(const QString & device)132 bool QDeviceDiscoveryStatic::checkDeviceType(const QString &device)
133 {
134 int fd = QT_OPEN(device.toLocal8Bit().constData(), O_RDONLY | O_NDELAY, 0);
135 if (Q_UNLIKELY(fd == -1)) {
136 qWarning() << "Device discovery cannot open device" << device;
137 return false;
138 }
139
140 qCDebug(lcDD) << "doing static device discovery for " << device;
141
142 if ((m_types & Device_DRM) && device.contains(QLatin1String(QT_DRM_DEVICE_PREFIX))) {
143 QT_CLOSE(fd);
144 return true;
145 }
146
147 long bitsAbs[LONG_FIELD_SIZE(ABS_CNT)];
148 long bitsKey[LONG_FIELD_SIZE(KEY_CNT)];
149 long bitsRel[LONG_FIELD_SIZE(REL_CNT)];
150 memset(bitsAbs, 0, sizeof(bitsAbs));
151 memset(bitsKey, 0, sizeof(bitsKey));
152 memset(bitsRel, 0, sizeof(bitsRel));
153
154 ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(bitsAbs)), bitsAbs);
155 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(bitsKey)), bitsKey);
156 ioctl(fd, EVIOCGBIT(EV_REL, sizeof(bitsRel)), bitsRel);
157
158 QT_CLOSE(fd);
159
160 if ((m_types & Device_Keyboard)) {
161 if (testBit(KEY_Q, bitsKey)) {
162 qCDebug(lcDD) << "Found keyboard at" << device;
163 return true;
164 }
165 }
166
167 if ((m_types & Device_Mouse)) {
168 if (testBit(REL_X, bitsRel) && testBit(REL_Y, bitsRel) && testBit(BTN_MOUSE, bitsKey)) {
169 qCDebug(lcDD) << "Found mouse at" << device;
170 return true;
171 }
172 }
173
174 if ((m_types & (Device_Touchpad | Device_Touchscreen))) {
175 if (testBit(ABS_X, bitsAbs) && testBit(ABS_Y, bitsAbs)) {
176 if ((m_types & Device_Touchpad) && testBit(BTN_TOOL_FINGER, bitsKey)) {
177 qCDebug(lcDD) << "Found touchpad at" << device;
178 return true;
179 } else if ((m_types & Device_Touchscreen) && testBit(BTN_TOUCH, bitsKey)) {
180 qCDebug(lcDD) << "Found touchscreen at" << device;
181 return true;
182 } else if ((m_types & Device_Tablet) && (testBit(BTN_STYLUS, bitsKey) || testBit(BTN_TOOL_PEN, bitsKey))) {
183 qCDebug(lcDD) << "Found tablet at" << device;
184 return true;
185 }
186 } else if (testBit(ABS_MT_POSITION_X, bitsAbs) &&
187 testBit(ABS_MT_POSITION_Y, bitsAbs)) {
188 qCDebug(lcDD) << "Found new-style touchscreen at" << device;
189 return true;
190 }
191 }
192
193 if ((m_types & Device_Joystick)) {
194 if (testBit(BTN_A, bitsKey) || testBit(BTN_TRIGGER, bitsKey) || testBit(ABS_RX, bitsAbs)) {
195 qCDebug(lcDD) << "Found joystick/gamepad at" << device;
196 return true;
197 }
198 }
199
200 return false;
201 }
202
203 QT_END_NAMESPACE
204