Changeset View
Changeset View
Standalone View
Standalone View
kded/control.cpp
- This file was added.
1 | /******************************************************************** | ||||
---|---|---|---|---|---|
2 | Copyright 2019 Roman Gilg <subdiff@gmail.com> | ||||
3 | | ||||
4 | This program is free software; you can redistribute it and/or modify | ||||
5 | it under the terms of the GNU General Public License as published by | ||||
6 | the Free Software Foundation; either version 2 of the License, or | ||||
7 | (at your option) any later version. | ||||
8 | | ||||
9 | This program is distributed in the hope that it will be useful, | ||||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | GNU General Public License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU General Public License | ||||
15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
16 | *********************************************************************/ | ||||
17 | #include "control.h" | ||||
18 | #include "config.h" | ||||
19 | | ||||
20 | #include "kscreen_daemon_debug.h" | ||||
21 | #include "generator.h" | ||||
22 | | ||||
23 | #include <QStringList> | ||||
24 | #include <QFile> | ||||
25 | #include <QStringBuilder> | ||||
26 | #include <QJsonDocument> | ||||
27 | #include <QDir> | ||||
28 | #include <QLoggingCategory> | ||||
29 | | ||||
30 | #include <kscreen/output.h> | ||||
31 | #include <kscreen/edid.h> | ||||
32 | | ||||
33 | QString Control::s_dirName = QStringLiteral("control/"); | ||||
34 | | ||||
35 | QString Control::dirPath() | ||||
36 | { | ||||
37 | return Config::dirPath() % s_dirName; | ||||
38 | } | ||||
39 | | ||||
40 | QString Control::outputFilePath(const QString &hash) | ||||
41 | { | ||||
42 | const QString dir = dirPath() % QStringLiteral("outputs/"); | ||||
43 | if (!QDir().mkpath(dir)) { | ||||
44 | return QString(); | ||||
45 | } | ||||
46 | return dir % hash; | ||||
47 | } | ||||
48 | | ||||
49 | QString Control::configFilePath(const QString &hash) | ||||
50 | { | ||||
51 | const QString dir = dirPath() % QStringLiteral("configs/"); | ||||
52 | if (!QDir().mkpath(dir)) { | ||||
53 | return QString(); | ||||
54 | } | ||||
55 | return dir % hash; | ||||
56 | } | ||||
57 | | ||||
58 | Control::OutputRetention Control::convertVariantToOutputRetention(QVariant variant) | ||||
59 | { | ||||
60 | if (variant.canConvert<int>()) { | ||||
61 | const auto retention = variant.toInt(); | ||||
62 | if (retention == (int)OutputRetention::Global) { | ||||
63 | return OutputRetention::Global; | ||||
64 | } | ||||
65 | if (retention == (int)OutputRetention::Individual) { | ||||
66 | return OutputRetention::Individual; | ||||
67 | } | ||||
68 | } | ||||
69 | return OutputRetention::Undefined; | ||||
70 | } | ||||
71 | | ||||
72 | QMap<QString, Control::OutputRetention> Control::readInOutputRetentionValues(const QString &configId) | ||||
73 | { | ||||
74 | // qDebug() << "Looking for control file:" << configId; | ||||
75 | QFile file(configFilePath(configId)); | ||||
76 | if (!file.open(QIODevice::ReadOnly)) { | ||||
77 | qCDebug(KSCREEN_KDED) << "Failed to open file" << file.fileName(); | ||||
78 | return QMap<QString, Control::OutputRetention>(); | ||||
79 | } | ||||
80 | | ||||
81 | QJsonDocument parser; | ||||
82 | const QVariantMap controlInfo = parser.fromJson(file.readAll()).toVariant().toMap(); | ||||
83 | const QVariantList outputsInfo = controlInfo[QStringLiteral("outputs")].toList(); | ||||
84 | QMap<QString, Control::OutputRetention> retentions; | ||||
85 | | ||||
86 | for (const auto variantInfo : outputsInfo) { | ||||
87 | const QVariantMap info = variantInfo.toMap(); | ||||
88 | | ||||
89 | // TODO: this does not yet consider the output name (i.e. connector). Necessary? | ||||
90 | const QString outputHash = info[QStringLiteral("id")].toString(); | ||||
91 | if (outputHash.isEmpty()) { | ||||
92 | continue; | ||||
93 | } | ||||
94 | retentions[outputHash] = convertVariantToOutputRetention(info[QStringLiteral("retention")]); | ||||
95 | } | ||||
96 | | ||||
97 | return retentions; | ||||
98 | } | ||||
99 | | ||||
100 | Control::OutputRetention Control::getOutputRetention(const QString &outputId, const QMap<QString, Control::OutputRetention> &retentions) | ||||
101 | { | ||||
102 | if (retentions.contains(outputId)) { | ||||
103 | return retentions[outputId]; | ||||
104 | } | ||||
105 | // info for output not found | ||||
106 | return OutputRetention::Undefined; | ||||
107 | } |