| 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 "qevdevtabletmanager_p.h" |
| 5 | #include "qevdevtablethandler_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 | Q_DECLARE_LOGGING_CATEGORY(qLcEvdevTablet) |
| 19 | |
| 20 | QEvdevTabletManager::QEvdevTabletManager(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 &>(qLcEvdevTablet()).setEnabled(type: QtDebugMsg, enable: true); |
| 27 | |
| 28 | QString spec = QString::fromLocal8Bit(ba: qgetenv(varName: "QT_QPA_EVDEV_TABLET_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(qLcEvdevTablet, "evdevtablet: Using device discovery" ); |
| 42 | if (auto deviceDiscovery = QDeviceDiscovery::create(type: QDeviceDiscovery::Device_Tablet, 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: &QEvdevTabletManager::addDevice); |
| 49 | connect(sender: deviceDiscovery, signal: &QDeviceDiscovery::deviceRemoved, |
| 50 | context: this, slot: &QEvdevTabletManager::removeDevice); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | QEvdevTabletManager::~QEvdevTabletManager() |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | void QEvdevTabletManager::addDevice(const QString &deviceNode) |
| 60 | { |
| 61 | qCDebug(qLcEvdevTablet, "Adding device at %ls" , qUtf16Printable(deviceNode)); |
| 62 | auto handler = std::make_unique<QEvdevTabletHandlerThread>(args: deviceNode, args&: m_spec); |
| 63 | if (handler) { |
| 64 | m_activeDevices.add(deviceNode, handler: std::move(handler)); |
| 65 | updateDeviceCount(); |
| 66 | } else { |
| 67 | qWarning(msg: "evdevtablet: Failed to open tablet device %ls" , qUtf16Printable(deviceNode)); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void QEvdevTabletManager::removeDevice(const QString &deviceNode) |
| 72 | { |
| 73 | if (m_activeDevices.remove(deviceNode)) { |
| 74 | qCDebug(qLcEvdevTablet, "Removing device at %ls" , qUtf16Printable(deviceNode)); |
| 75 | updateDeviceCount(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void QEvdevTabletManager::updateDeviceCount() |
| 80 | { |
| 81 | QInputDeviceManagerPrivate::get(mgr: QGuiApplicationPrivate::inputDeviceManager())->setDeviceCount( |
| 82 | type: QInputDeviceManager::DeviceTypeTablet, count: m_activeDevices.count()); |
| 83 | } |
| 84 | |
| 85 | QT_END_NAMESPACE |
| 86 | |