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 #include <linux/input.h>
51 #include <fcntl.h>
52 
53 /* android (and perhaps some other linux-derived stuff) don't define everything
54  * in linux/input.h, so we'll need to do that ourselves.
55  */
56 #ifndef KEY_CNT
57 #define KEY_CNT                 (KEY_MAX+1)
58 #endif
59 #ifndef REL_CNT
60 #define REL_CNT                 (REL_MAX+1)
61 #endif
62 #ifndef ABS_CNT
63 #define ABS_CNT                 (ABS_MAX+1)
64 #endif
65 #ifndef ABS_MT_POSITION_X
66 #define ABS_MT_POSITION_X       0x35
67 #endif
68 #ifndef ABS_MT_POSITION_Y
69 #define ABS_MT_POSITION_Y       0x36
70 #endif
71 
72 #define LONG_BITS (sizeof(long) * 8 )
73 #define LONG_FIELD_SIZE(bits) ((bits / LONG_BITS) + 1)
74 
testBit(long bit,const long * field)75 static bool testBit(long bit, const long *field)
76 {
77     return (field[bit / LONG_BITS] >> bit % LONG_BITS) & 1;
78 }
79 
80 QT_BEGIN_NAMESPACE
81 
82 Q_LOGGING_CATEGORY(lcDD, "qt.qpa.input")
83 
create(QDeviceTypes types,QObject * parent)84 QDeviceDiscovery *QDeviceDiscovery::create(QDeviceTypes types, QObject *parent)
85 {
86     return new QDeviceDiscoveryStatic(types, parent);
87 }
88 
QDeviceDiscoveryStatic(QDeviceTypes types,QObject * parent)89 QDeviceDiscoveryStatic::QDeviceDiscoveryStatic(QDeviceTypes types, QObject *parent)
90     : QDeviceDiscovery(types, parent)
91 {
92     qCDebug(lcDD) << "static device discovery for type" << types;
93 }
94 
scanConnectedDevices()95 QStringList QDeviceDiscoveryStatic::scanConnectedDevices()
96 {
97     QStringList devices;
98     QDir dir;
99     dir.setFilter(QDir::System);
100 
101     // check for input devices
102     if (m_types & Device_InputMask) {
103         dir.setPath(QString::fromLatin1(QT_EVDEV_DEVICE_PATH));
104         const auto deviceFiles = dir.entryList();
105         for (const QString &deviceFile : deviceFiles) {
106             QString absoluteFilePath = dir.absolutePath() + QLatin1Char('/') + deviceFile;
107             if (checkDeviceType(absoluteFilePath))
108                 devices << absoluteFilePath;
109         }
110     }
111 
112     // check for drm devices
113     if (m_types & Device_VideoMask) {
114         dir.setPath(QString::fromLatin1(QT_DRM_DEVICE_PATH));
115         const auto deviceFiles = dir.entryList();
116         for (const QString &deviceFile : deviceFiles) {
117             QString absoluteFilePath = dir.absolutePath() + QLatin1Char('/') + deviceFile;
118             if (checkDeviceType(absoluteFilePath))
119                 devices << absoluteFilePath;
120         }
121     }
122 
123     qCDebug(lcDD) << "Found matching devices" << devices;
124 
125     return devices;
126 }
127 
checkDeviceType(const QString & device)128 bool QDeviceDiscoveryStatic::checkDeviceType(const QString &device)
129 {
130     int fd = QT_OPEN(device.toLocal8Bit().constData(), O_RDONLY | O_NDELAY, 0);
131     if (Q_UNLIKELY(fd == -1)) {
132         qWarning() << "Device discovery cannot open device" << device;
133         return false;
134     }
135 
136     qCDebug(lcDD) << "doing static device discovery for " << device;
137 
138     if ((m_types & Device_DRM) && device.contains(QLatin1String(QT_DRM_DEVICE_PREFIX))) {
139         QT_CLOSE(fd);
140         return true;
141     }
142 
143     long bitsAbs[LONG_FIELD_SIZE(ABS_CNT)];
144     long bitsKey[LONG_FIELD_SIZE(KEY_CNT)];
145     long bitsRel[LONG_FIELD_SIZE(REL_CNT)];
146     memset(bitsAbs, 0, sizeof(bitsAbs));
147     memset(bitsKey, 0, sizeof(bitsKey));
148     memset(bitsRel, 0, sizeof(bitsRel));
149 
150     ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(bitsAbs)), bitsAbs);
151     ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(bitsKey)), bitsKey);
152     ioctl(fd, EVIOCGBIT(EV_REL, sizeof(bitsRel)), bitsRel);
153 
154     QT_CLOSE(fd);
155 
156     if ((m_types & Device_Keyboard)) {
157         if (testBit(KEY_Q, bitsKey)) {
158             qCDebug(lcDD) << "Found keyboard at" << device;
159             return true;
160         }
161     }
162 
163     if ((m_types & Device_Mouse)) {
164         if (testBit(REL_X, bitsRel) && testBit(REL_Y, bitsRel) && testBit(BTN_MOUSE, bitsKey)) {
165             qCDebug(lcDD) << "Found mouse at" << device;
166             return true;
167         }
168     }
169 
170     if ((m_types & (Device_Touchpad | Device_Touchscreen))) {
171         if (testBit(ABS_X, bitsAbs) && testBit(ABS_Y, bitsAbs)) {
172             if ((m_types & Device_Touchpad) && testBit(BTN_TOOL_FINGER, bitsKey)) {
173                 qCDebug(lcDD) << "Found touchpad at" << device;
174                 return true;
175             } else if ((m_types & Device_Touchscreen) && testBit(BTN_TOUCH, bitsKey)) {
176                 qCDebug(lcDD) << "Found touchscreen at" << device;
177                 return true;
178             } else if ((m_types & Device_Tablet) && (testBit(BTN_STYLUS, bitsKey) || testBit(BTN_TOOL_PEN, bitsKey))) {
179                 qCDebug(lcDD) << "Found tablet at" << device;
180                 return true;
181             }
182         } else if (testBit(ABS_MT_POSITION_X, bitsAbs) &&
183                    testBit(ABS_MT_POSITION_Y, bitsAbs)) {
184             qCDebug(lcDD) << "Found new-style touchscreen at" << device;
185             return true;
186         }
187     }
188 
189     if ((m_types & Device_Joystick)) {
190         if (testBit(BTN_A, bitsKey) || testBit(BTN_TRIGGER, bitsKey) || testBit(ABS_RX, bitsAbs)) {
191             qCDebug(lcDD) << "Found joystick/gamepad at" << device;
192             return true;
193         }
194     }
195 
196     return false;
197 }
198 
199 QT_END_NAMESPACE
200