| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "cpufreqqmlitem.h" |
| 5 | |
| 6 | #include "core/qmlcomponentregistry.h" |
| 7 | #include "cpufreq.h" |
| 8 | #include <QQmlApplicationEngine> |
| 9 | #include <QtGlobal> |
| 10 | #include <QtQml> |
| 11 | #include <memory> |
| 12 | |
| 13 | char const *const CPUFreqQMLItem::trStrings[] = { |
| 14 | QT_TRANSLATE_NOOP("ControlModeQMLItem", "CPU_CPUFREQ"), |
| 15 | |
| 16 | // XXX add cpufreq scaling governors here |
| 17 | QT_TRANSLATE_NOOP("CPUFreqQMLItem", "performance"), |
| 18 | QT_TRANSLATE_NOOP("CPUFreqQMLItem", "powersave"), |
| 19 | QT_TRANSLATE_NOOP("CPUFreqQMLItem", "schedutil"), |
| 20 | QT_TRANSLATE_NOOP("CPUFreqQMLItem", "ondemand"), |
| 21 | QT_TRANSLATE_NOOP("CPUFreqQMLItem", "conservative"), |
| 22 | |
| 23 | // XXX add CPU EPP available hints here |
| 24 | QT_TRANSLATE_NOOP("CPUFreqQMLItem", "default"), |
| 25 | QT_TRANSLATE_NOOP("CPUFreqQMLItem", "performance"), |
| 26 | QT_TRANSLATE_NOOP("CPUFreqQMLItem", "balance_performance"), |
| 27 | QT_TRANSLATE_NOOP("CPUFreqQMLItem", "balance_power"), |
| 28 | QT_TRANSLATE_NOOP("CPUFreqQMLItem", "power"), |
| 29 | }; |
| 30 | |
| 31 | class CPUFreqQMLItem::Initializer final |
| 32 | : public QMLItem::Initializer |
| 33 | , public CPUFreq::Exporter |
| 34 | { |
| 35 | public: |
| 36 | Initializer(IQMLComponentFactory const &qmlComponentFactory, |
| 37 | QQmlApplicationEngine &qmlEngine, CPUFreqQMLItem &qmlItem) noexcept |
| 38 | : QMLItem::Initializer(qmlComponentFactory, qmlEngine) |
| 39 | , outer_(qmlItem) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 44 | provideExporter(Item const &) override |
| 45 | { |
| 46 | return {}; |
| 47 | } |
| 48 | |
| 49 | void takeActive(bool active) override; |
| 50 | |
| 51 | void takeCPUFreqScalingGovernor(std::string const &governor) override; |
| 52 | void |
| 53 | takeCPUFreqScalingGovernors(std::vector<std::string> const &governors) override; |
| 54 | |
| 55 | void takeCPUFreqEPPHint(std::optional<std::string> const &hint) override; |
| 56 | void takeCPUFreqEPPHints( |
| 57 | std::optional<std::vector<std::string>> const &hints) override; |
| 58 | |
| 59 | private: |
| 60 | CPUFreqQMLItem &outer_; |
| 61 | }; |
| 62 | |
| 63 | void CPUFreqQMLItem::Initializer::takeActive(bool active) |
| 64 | { |
| 65 | outer_.takeActive(active); |
| 66 | } |
| 67 | |
| 68 | void CPUFreqQMLItem::Initializer::takeCPUFreqScalingGovernor( |
| 69 | std::string const &governor) |
| 70 | { |
| 71 | outer_.takeCPUFreqScalingGovernor(governor); |
| 72 | } |
| 73 | |
| 74 | void CPUFreqQMLItem::Initializer::takeCPUFreqScalingGovernors( |
| 75 | std::vector<std::string> const &governors) |
| 76 | { |
| 77 | outer_.takeCPUFreqScalingGovernors(governors); |
| 78 | } |
| 79 | |
| 80 | void CPUFreqQMLItem::Initializer::takeCPUFreqEPPHint( |
| 81 | std::optional<std::string> const &hint) |
| 82 | { |
| 83 | outer_.takeCPUFreqEPPHint(hint); |
| 84 | } |
| 85 | |
| 86 | void CPUFreqQMLItem::Initializer::takeCPUFreqEPPHints( |
| 87 | std::optional<std::vector<std::string>> const &hints) |
| 88 | { |
| 89 | outer_.takeCPUFreqEPPHints(hints); |
| 90 | } |
| 91 | |
| 92 | CPUFreqQMLItem::CPUFreqQMLItem() noexcept |
| 93 | { |
| 94 | setName(tr(CPUFreq::ItemID.data())); |
| 95 | } |
| 96 | |
| 97 | void CPUFreqQMLItem::changeScalingGovernor(QString const &governor) |
| 98 | { |
| 99 | auto newScalingGovernor = governor.toStdString(); |
| 100 | if (scalingGovernor_ != newScalingGovernor) { |
| 101 | std::swap(scalingGovernor_, newScalingGovernor); |
| 102 | emit scalingGovernorChanged(governor); |
| 103 | emit toggleEppHint(enableEpp_ && scalingGovernor_ == eppScalingGovernor_); |
| 104 | emit settingsChanged(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void CPUFreqQMLItem::changeEPPHint(QString const &hint) |
| 109 | { |
| 110 | auto newHint = hint.toStdString(); |
| 111 | if (eppHint_ && eppHint_ != newHint) { |
| 112 | std::swap(*eppHint_, newHint); |
| 113 | emit eppHintChanged(hint); |
| 114 | emit settingsChanged(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void CPUFreqQMLItem::activate(bool active) |
| 119 | { |
| 120 | takeActive(active); |
| 121 | } |
| 122 | |
| 123 | std::optional<std::reference_wrapper<Importable::Importer>> |
| 124 | CPUFreqQMLItem::provideImporter(Item const &) |
| 125 | { |
| 126 | return {}; |
| 127 | } |
| 128 | |
| 129 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 130 | CPUFreqQMLItem::provideExporter(Item const &) |
| 131 | { |
| 132 | return {}; |
| 133 | } |
| 134 | |
| 135 | bool CPUFreqQMLItem::provideActive() const |
| 136 | { |
| 137 | return active_; |
| 138 | } |
| 139 | |
| 140 | std::string const &CPUFreqQMLItem::provideCPUFreqScalingGovernor() const |
| 141 | { |
| 142 | return scalingGovernor_; |
| 143 | } |
| 144 | |
| 145 | std::optional<std::string> const &CPUFreqQMLItem::provideCPUFreqEPPHint() const |
| 146 | { |
| 147 | return eppHint_; |
| 148 | } |
| 149 | |
| 150 | void CPUFreqQMLItem::takeActive(bool active) |
| 151 | { |
| 152 | active_ = active; |
| 153 | setVisible(active); |
| 154 | } |
| 155 | |
| 156 | void CPUFreqQMLItem::takeCPUFreqScalingGovernor(std::string const &governor) |
| 157 | { |
| 158 | if (scalingGovernor_ != governor) { |
| 159 | scalingGovernor_ = governor; |
| 160 | emit scalingGovernorChanged(QString::fromStdString(scalingGovernor_)); |
| 161 | emit toggleEppHint(enableEpp_ && scalingGovernor_ == eppScalingGovernor_); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void CPUFreqQMLItem::takeCPUFreqEPPHint(std::optional<std::string> const &hint) |
| 166 | { |
| 167 | if (hint && eppHint_ != hint) { |
| 168 | eppHint_ = hint; |
| 169 | emit eppHintChanged(QString::fromStdString(*eppHint_)); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | std::unique_ptr<Exportable::Exporter> |
| 174 | CPUFreqQMLItem::initializer(IQMLComponentFactory const &qmlComponentFactory, |
| 175 | QQmlApplicationEngine &qmlEngine) |
| 176 | { |
| 177 | return std::make_unique<CPUFreqQMLItem::Initializer>(qmlComponentFactory, |
| 178 | qmlEngine, *this); |
| 179 | } |
| 180 | |
| 181 | void CPUFreqQMLItem::takeCPUFreqScalingGovernors( |
| 182 | std::vector<std::string> const &governors) |
| 183 | { |
| 184 | QList<QString> governorTextVector; |
| 185 | for (auto governor : governors) { |
| 186 | governorTextVector.push_back(QString::fromStdString(governor)); |
| 187 | governorTextVector.push_back(tr(governor.data())); |
| 188 | } |
| 189 | emit scalingGovernorsChanged(governorTextVector); |
| 190 | } |
| 191 | |
| 192 | void CPUFreqQMLItem::takeCPUFreqEPPHints( |
| 193 | std::optional<std::vector<std::string>> const &hints) |
| 194 | { |
| 195 | if (!hints) |
| 196 | return; |
| 197 | |
| 198 | enableEpp_ = true; |
| 199 | |
| 200 | QList<QString> hintTextVector; |
| 201 | for (auto hint : *hints) { |
| 202 | hintTextVector.push_back(QString::fromStdString(hint)); |
| 203 | hintTextVector.push_back(tr(hint.data())); |
| 204 | } |
| 205 | emit eppHintsChanged(hintTextVector); |
| 206 | } |
| 207 | |
| 208 | bool CPUFreqQMLItem::register_() |
| 209 | { |
| 210 | QMLComponentRegistry::addQMLTypeRegisterer([]() { |
| 211 | qmlRegisterType<CPUFreqQMLItem>("CoreCtrl.UIComponents", 1, 0, |
| 212 | CPUFreq::ItemID.data()); |
| 213 | }); |
| 214 | |
| 215 | QMLComponentRegistry::addQMLItemProvider( |
| 216 | CPUFreq::ItemID, [](QQmlApplicationEngine &engine) { |
| 217 | QQmlComponent component(&engine, |
| 218 | QStringLiteral("qrc:/qml/CPUFreqForm.qml")); |
| 219 | return qobject_cast<QMLItem *>(component.create()); |
| 220 | }); |
| 221 | |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | bool const CPUFreqQMLItem::registered_ = CPUFreqQMLItem::register_(); |