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