Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qdevicediscovery_static.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QStringList>
7#include <QCoreApplication>
8#include <QObject>
9#include <QHash>
10#include <QDir>
11#include <QLoggingCategory>
12#include <QtCore/private/qcore_unix_p.h>
13
14#ifdef Q_OS_FREEBSD
15#include <dev/evdev/input.h>
16#else
17#include <linux/input.h>
18#endif
19#include <fcntl.h>
20
21/* android (and perhaps some other linux-derived stuff) don't define everything
22 * in linux/input.h, so we'll need to do that ourselves.
23 */
24#ifndef KEY_CNT
25#define KEY_CNT (KEY_MAX+1)
26#endif
27#ifndef REL_CNT
28#define REL_CNT (REL_MAX+1)
29#endif
30#ifndef ABS_CNT
31#define ABS_CNT (ABS_MAX+1)
32#endif
33#ifndef ABS_MT_POSITION_X
34#define ABS_MT_POSITION_X 0x35
35#endif
36#ifndef ABS_MT_POSITION_Y
37#define ABS_MT_POSITION_Y 0x36
38#endif
39
40#define LONG_BITS (sizeof(long) * 8 )
41#define LONG_FIELD_SIZE(bits) ((bits / LONG_BITS) + 1)
42
43static bool testBit(long bit, const long *field)
44{
45 return (field[bit / LONG_BITS] >> bit % LONG_BITS) & 1;
46}
47
49
50using namespace Qt::StringLiterals;
51
52Q_LOGGING_CATEGORY(lcDD, "qt.qpa.input")
53
55{
56 return new QDeviceDiscoveryStatic(types, parent);
57}
58
60 : QDeviceDiscovery(types, parent)
61{
62 qCDebug(lcDD) << "static device discovery for type" << types;
63}
64
66{
68 QDir dir;
69 dir.setFilter(QDir::System);
70
71 // check for input devices
74 const auto deviceFiles = dir.entryList();
75 for (const QString &deviceFile : deviceFiles) {
76 QString absoluteFilePath = dir.absolutePath() + u'/' + deviceFile;
77 if (checkDeviceType(absoluteFilePath))
79 }
80 }
81
82 // check for drm devices
85 const auto deviceFiles = dir.entryList();
86 for (const QString &deviceFile : deviceFiles) {
87 QString absoluteFilePath = dir.absolutePath() + u'/' + deviceFile;
88 if (checkDeviceType(absoluteFilePath))
90 }
91 }
92
93 qCDebug(lcDD) << "Found matching devices" << devices;
94
95 return devices;
96}
97
98bool QDeviceDiscoveryStatic::checkDeviceType(const QString &device)
99{
100 int fd = QT_OPEN(device.toLocal8Bit().constData(), O_RDONLY | O_NDELAY, 0);
101 if (Q_UNLIKELY(fd == -1)) {
102 qWarning() << "Device discovery cannot open device" << device;
103 return false;
104 }
105
106 qCDebug(lcDD) << "doing static device discovery for " << device;
107
108 if ((m_types & Device_DRM) && device.contains(QT_DRM_DEVICE_PREFIX ""_L1)) {
109 QT_CLOSE(fd);
110 return true;
111 }
112
113 long bitsAbs[LONG_FIELD_SIZE(ABS_CNT)];
114 long bitsKey[LONG_FIELD_SIZE(KEY_CNT)];
115 long bitsRel[LONG_FIELD_SIZE(REL_CNT)];
116 memset(bitsAbs, 0, sizeof(bitsAbs));
117 memset(bitsKey, 0, sizeof(bitsKey));
118 memset(bitsRel, 0, sizeof(bitsRel));
119
120 ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(bitsAbs)), bitsAbs);
121 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(bitsKey)), bitsKey);
122 ioctl(fd, EVIOCGBIT(EV_REL, sizeof(bitsRel)), bitsRel);
123
124 QT_CLOSE(fd);
125
126 if ((m_types & Device_Keyboard)) {
127 if (testBit(KEY_Q, bitsKey)) {
128 qCDebug(lcDD) << "Found keyboard at" << device;
129 return true;
130 }
131 }
132
133 if ((m_types & Device_Mouse)) {
134 if (testBit(REL_X, bitsRel) && testBit(REL_Y, bitsRel) && testBit(BTN_MOUSE, bitsKey)) {
135 qCDebug(lcDD) << "Found mouse at" << device;
136 return true;
137 }
138 }
139
141 if (testBit(ABS_X, bitsAbs) && testBit(ABS_Y, bitsAbs)) {
142 if ((m_types & Device_Touchpad) && testBit(BTN_TOOL_FINGER, bitsKey)) {
143 qCDebug(lcDD) << "Found touchpad at" << device;
144 return true;
145 } else if ((m_types & Device_Touchscreen) && testBit(BTN_TOUCH, bitsKey)) {
146 qCDebug(lcDD) << "Found touchscreen at" << device;
147 return true;
148 } else if ((m_types & Device_Tablet) && (testBit(BTN_STYLUS, bitsKey) || testBit(BTN_TOOL_PEN, bitsKey))) {
149 qCDebug(lcDD) << "Found tablet at" << device;
150 return true;
151 }
152 } else if (testBit(ABS_MT_POSITION_X, bitsAbs) &&
153 testBit(ABS_MT_POSITION_Y, bitsAbs)) {
154 qCDebug(lcDD) << "Found new-style touchscreen at" << device;
155 return true;
156 }
157 }
158
159 if ((m_types & Device_Joystick)) {
160 if (testBit(BTN_A, bitsKey) || testBit(BTN_TRIGGER, bitsKey) || testBit(ABS_RX, bitsAbs)) {
161 qCDebug(lcDD) << "Found joystick/gamepad at" << device;
162 return true;
163 }
164 }
165
166 return false;
167}
168
IOBluetoothDevice * device
QDeviceDiscoveryStatic(QDeviceTypes types, QObject *parent=nullptr)
QStringList scanConnectedDevices() override
\inmodule QtCore
Definition qdir.h:20
@ System
Definition qdir.h:36
\inmodule QtCore
Definition qobject.h:103
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5871
Combined button and popup list for selecting options.
#define Q_UNLIKELY(x)
#define QT_OPEN
#define QT_CLOSE
#define QT_DRM_DEVICE_PATH
#define QT_EVDEV_DEVICE_PATH
#define QT_DRM_DEVICE_PREFIX
#define ABS_CNT
#define ABS_MT_POSITION_X
static bool testBit(long bit, const long *field)
#define REL_CNT
#define LONG_BITS
#define ABS_MT_POSITION_Y
#define LONG_FIELD_SIZE(bits)
#define KEY_CNT
EGLDeviceEXT * devices
#define qWarning
Definition qlogging.h:166
#define Q_LOGGING_CATEGORY(name,...)
#define qCDebug(category,...)
GLsizei GLenum GLenum * types
GLuint64 GLenum GLint fd
static QString absoluteFilePath(const Options *options, const QString &relativeFileName)
Definition main.cpp:1902
QString dir
[11]
view create()