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
4#include "qevdevtouchmanager_p.h"
5#include "qevdevtouchhandler_p.h"
6
7#include <QtInputSupport/private/qevdevutil_p.h>
8
9#include <QStringList>
10#include <QGuiApplication>
11#include <QLoggingCategory>
12#include <QtDeviceDiscoverySupport/private/qdevicediscovery_p.h>
13#include <private/qguiapplication_p.h>
14#include <private/qinputdevicemanager_p_p.h>
15
16QT_BEGIN_NAMESPACE
17
18Q_DECLARE_LOGGING_CATEGORY(qLcEvdevTouch)
19
20QEvdevTouchManager::QEvdevTouchManager(const QString &key, const QString &specification, QObject *parent)
21 : QObject(parent)
22{
23 Q_UNUSED(key);
24
25 if (qEnvironmentVariableIsSet(varName: "QT_QPA_EVDEV_DEBUG"))
26 const_cast<QLoggingCategory &>(qLcEvdevTouch()).setEnabled(type: QtDebugMsg, enable: true);
27
28 QString spec = QString::fromLocal8Bit(ba: qgetenv(varName: "QT_QPA_EVDEV_TOUCHSCREEN_PARAMETERS"));
29
30 if (spec.isEmpty())
31 spec = specification;
32
33 auto parsed = QEvdevUtil::parseSpecification(specification: spec);
34 m_spec = std::move(parsed.spec);
35
36 for (const QString &device : std::as_const(t&: parsed.devices))
37 addDevice(deviceNode: device);
38
39 // when no devices specified, use device discovery to scan and monitor
40 if (parsed.devices.isEmpty()) {
41 qCDebug(qLcEvdevTouch, "evdevtouch: Using device discovery");
42 if (auto deviceDiscovery = QDeviceDiscovery::create(type: QDeviceDiscovery::Device_Touchpad | QDeviceDiscovery::Device_Touchscreen, parent: this)) {
43 const QStringList devices = deviceDiscovery->scanConnectedDevices();
44 for (const QString &device : devices)
45 addDevice(deviceNode: device);
46
47 connect(sender: deviceDiscovery, signal: &QDeviceDiscovery::deviceDetected,
48 context: this, slot: &QEvdevTouchManager::addDevice);
49 connect(sender: deviceDiscovery, signal: &QDeviceDiscovery::deviceRemoved,
50 context: this, slot: &QEvdevTouchManager::removeDevice);
51 }
52 }
53}
54
55QEvdevTouchManager::~QEvdevTouchManager()
56{
57}
58
59void QEvdevTouchManager::addDevice(const QString &deviceNode)
60{
61 qCDebug(qLcEvdevTouch, "evdevtouch: Adding device at %ls", qUtf16Printable(deviceNode));
62 auto handler = std::make_unique<QEvdevTouchScreenHandlerThread>(args: deviceNode, args&: m_spec);
63 if (handler) {
64 connect(sender: handler.get(), signal: &QEvdevTouchScreenHandlerThread::touchDeviceRegistered, context: this, slot: &QEvdevTouchManager::updateInputDeviceCount);
65 m_activeDevices.add(deviceNode, handler: std::move(handler));
66 } else {
67 qWarning(msg: "evdevtouch: Failed to open touch device %ls", qUtf16Printable(deviceNode));
68 }
69}
70
71void QEvdevTouchManager::removeDevice(const QString &deviceNode)
72{
73 if (m_activeDevices.remove(deviceNode)) {
74 qCDebug(qLcEvdevTouch, "evdevtouch: Removing device at %ls", qUtf16Printable(deviceNode));
75 updateInputDeviceCount();
76 }
77}
78
79void QEvdevTouchManager::updateInputDeviceCount()
80{
81 int registeredTouchDevices = 0;
82 for (const auto &device : m_activeDevices) {
83 if (device.handler->isPointingDeviceRegistered())
84 ++registeredTouchDevices;
85 }
86
87 qCDebug(qLcEvdevTouch, "evdevtouch: Updating QInputDeviceManager device count: %d touch devices, %d pending handler(s)",
88 registeredTouchDevices, m_activeDevices.count() - registeredTouchDevices);
89
90 QInputDeviceManagerPrivate::get(mgr: QGuiApplicationPrivate::inputDeviceManager())->setDeviceCount(
91 type: QInputDeviceManager::DeviceTypeTouch, count: registeredTouchDevices);
92}
93
94QT_END_NAMESPACE
95

source code of qtbase/src/platformsupport/input/evdevtouch/qevdevtouchmanager.cpp