Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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
34#define ABS_MT_POSITION_X 0x35
35#endif
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
48QT_BEGIN_NAMESPACE
49
50using namespace Qt::StringLiterals;
51
52Q_STATIC_LOGGING_CATEGORY(lcDD, "qt.qpa.input")
53
54QDeviceDiscovery *QDeviceDiscovery::create(QDeviceTypes types, QObject *parent)
55{
56 return new QDeviceDiscoveryStatic(types, parent);
57}
58
59QDeviceDiscoveryStatic::QDeviceDiscoveryStatic(QDeviceTypes types, QObject *parent)
60 : QDeviceDiscovery(types, parent)
61{
62 qCDebug(lcDD) << "static device discovery for type" << types;
63}
64
65QStringList QDeviceDiscoveryStatic::scanConnectedDevices()
66{
67 QStringList devices;
68
69 auto addDevices = [this, &devices](const char *path) {
70 for (const auto &entry : QDirListing(QString::fromLatin1(path))) {
71 QString absoluteFilePath = entry.absoluteFilePath();
72 if (checkDeviceType(absoluteFilePath))
73 devices.emplace_back(std::move(absoluteFilePath));
74 }
75 };
76
77 // check for input devices
78 if (m_types & Device_InputMask)
79 addDevices(QT_EVDEV_DEVICE_PATH);
80
81 // check for drm devices
82 if (m_types & Device_VideoMask)
83 addDevices(QT_DRM_DEVICE_PATH);
84
85 qCDebug(lcDD) << "Found matching devices" << devices;
86
87 return devices;
88}
89
90bool QDeviceDiscoveryStatic::checkDeviceType(const QString &device)
91{
92 int fd = QT_OPEN(device.toLocal8Bit().constData(), O_RDONLY | O_NDELAY, 0);
93 if (Q_UNLIKELY(fd == -1)) {
94 qWarning() << "Device discovery cannot open device" << device;
95 return false;
96 }
97
98 qCDebug(lcDD) << "doing static device discovery for " << device;
99
100 if ((m_types & Device_DRM) && device.contains(QT_DRM_DEVICE_PREFIX ""_L1)) {
101 QT_CLOSE(fd);
102 return true;
103 }
104
105 long bitsAbs[LONG_FIELD_SIZE(ABS_CNT)];
106 long bitsKey[LONG_FIELD_SIZE(KEY_CNT)];
107 long bitsRel[LONG_FIELD_SIZE(REL_CNT)];
108 memset(bitsAbs, 0, sizeof(bitsAbs));
109 memset(bitsKey, 0, sizeof(bitsKey));
110 memset(bitsRel, 0, sizeof(bitsRel));
111
112 ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(bitsAbs)), bitsAbs);
113 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(bitsKey)), bitsKey);
114 ioctl(fd, EVIOCGBIT(EV_REL, sizeof(bitsRel)), bitsRel);
115
116 QT_CLOSE(fd);
117
118 if ((m_types & Device_Keyboard)) {
119 if (testBit(KEY_Q, bitsKey)) {
120 qCDebug(lcDD) << "Found keyboard at" << device;
121 return true;
122 }
123 }
124
125 if ((m_types & Device_Mouse)) {
126 if (testBit(REL_X, bitsRel) && testBit(REL_Y, bitsRel) && testBit(BTN_MOUSE, bitsKey)) {
127 qCDebug(lcDD) << "Found mouse at" << device;
128 return true;
129 }
130 }
131
132 if ((m_types & (Device_Touchpad | Device_Touchscreen))) {
133 if (testBit(ABS_X, bitsAbs) && testBit(ABS_Y, bitsAbs)) {
134 if ((m_types & Device_Touchpad) && testBit(BTN_TOOL_FINGER, bitsKey)) {
135 qCDebug(lcDD) << "Found touchpad at" << device;
136 return true;
137 } else if ((m_types & Device_Touchscreen) && testBit(BTN_TOUCH, bitsKey)) {
138 qCDebug(lcDD) << "Found touchscreen at" << device;
139 return true;
140 } else if ((m_types & Device_Tablet) && (testBit(BTN_STYLUS, bitsKey) || testBit(BTN_TOOL_PEN, bitsKey))) {
141 qCDebug(lcDD) << "Found tablet at" << device;
142 return true;
143 }
144 } else if (testBit(ABS_MT_POSITION_X, bitsAbs) &&
145 testBit(ABS_MT_POSITION_Y, bitsAbs)) {
146 qCDebug(lcDD) << "Found new-style touchscreen at" << device;
147 return true;
148 }
149 }
150
151 if ((m_types & Device_Joystick)) {
152 if (testBit(BTN_A, bitsKey) || testBit(BTN_TRIGGER, bitsKey) || testBit(ABS_RX, bitsAbs)) {
153 qCDebug(lcDD) << "Found joystick/gamepad at" << device;
154 return true;
155 }
156 }
157
158 return false;
159}
160
161QT_END_NAMESPACE
Q_STATIC_LOGGING_CATEGORY(lcAccessibilityCore, "qt.accessibility.core")
#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
#define BTN_TOUCH
#define EV_ABS
#define ABS_Y
#define EV_KEY
#define ABS_X