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 "qoutputmapping_p.h" |
5 | #include <QFile> |
6 | #include <QFileInfo> |
7 | #include <QVariantMap> |
8 | #include <QJsonDocument> |
9 | #include <QJsonObject> |
10 | #include <QJsonArray> |
11 | |
12 | QT_BEGIN_NAMESPACE |
13 | |
14 | using namespace Qt::StringLiterals; |
15 | |
16 | static QOutputMapping *s_outputMapping = nullptr; |
17 | |
18 | QOutputMapping *QOutputMapping::get() |
19 | { |
20 | if (!s_outputMapping) |
21 | s_outputMapping = new QDefaultOutputMapping; |
22 | |
23 | return s_outputMapping; |
24 | } |
25 | |
26 | bool QOutputMapping::load() |
27 | { |
28 | return false; |
29 | } |
30 | |
31 | QString QOutputMapping::screenNameForDeviceNode(const QString &deviceNode) |
32 | { |
33 | Q_UNUSED(deviceNode); |
34 | return QString(); |
35 | } |
36 | |
37 | #ifdef Q_OS_WEBOS |
38 | QWindow *QOutputMapping::windowForDeviceNode(const QString &deviceNode) |
39 | { |
40 | Q_UNUSED(deviceNode); |
41 | return nullptr; |
42 | } |
43 | |
44 | void QOutputMapping::set(QOutputMapping *mapping) |
45 | { |
46 | if (s_outputMapping) |
47 | delete s_outputMapping; |
48 | |
49 | s_outputMapping = mapping; |
50 | } |
51 | #endif // Q_OS_WEBOS |
52 | |
53 | bool QDefaultOutputMapping::load() |
54 | { |
55 | static QByteArray configFile = qgetenv(varName: "QT_QPA_EGLFS_KMS_CONFIG"); |
56 | if (configFile.isEmpty()) |
57 | return false; |
58 | |
59 | QFile file(QString::fromUtf8(ba: configFile)); |
60 | if (!file.open(flags: QFile::ReadOnly)) { |
61 | qWarning(msg: "touch input support: Failed to open %s", configFile.constData()); |
62 | return false; |
63 | } |
64 | |
65 | const QJsonDocument doc = QJsonDocument::fromJson(json: file.readAll()); |
66 | if (!doc.isObject()) { |
67 | qWarning(msg: "touch input support: Failed to parse %s", configFile.constData()); |
68 | return false; |
69 | } |
70 | |
71 | // What we are interested is the virtualIndex and touchDevice properties for |
72 | // each element in the outputs array. |
73 | const QJsonArray outputs = doc.object().value(key: "outputs"_L1).toArray(); |
74 | for (int i = 0; i < outputs.size(); ++i) { |
75 | const QVariantMap output = outputs.at(i).toObject().toVariantMap(); |
76 | if (!output.contains(QStringLiteral("touchDevice"))) |
77 | continue; |
78 | if (!output.contains(QStringLiteral("name"))) { |
79 | qWarning(msg: "evdevtouch: Output %d specifies touchDevice but not name, this is wrong", i); |
80 | continue; |
81 | } |
82 | QFileInfo deviceNode(output.value(QStringLiteral("touchDevice")).toString()); |
83 | const QString &screenName = output.value(QStringLiteral("name")).toString(); |
84 | m_screenTable.insert(key: deviceNode.canonicalFilePath(), value: screenName); |
85 | } |
86 | |
87 | return true; |
88 | } |
89 | |
90 | QString QDefaultOutputMapping::screenNameForDeviceNode(const QString &deviceNode) |
91 | { |
92 | return m_screenTable.value(key: deviceNode); |
93 | } |
94 | |
95 | QT_END_NAMESPACE |
96 |