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

source code of qtbase/src/platformsupport/input/evdevtablet/qevdevtabletmanager.cpp