File indexing completed on 2024-05-19 05:30:19

0001 /*
0002  * SPDX-FileCopyrightText: 2021 David Redondo <kde@david-redondo.de>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "lmsensors.h"
0008 
0009 #include <systemstats/SensorContainer.h>
0010 #include <systemstats/SensorObject.h>
0011 #include <systemstats/SensorsFeatureSensor.h>
0012 
0013 #include <KLocalizedString>
0014 #include <KPluginFactory>
0015 #include <array>
0016 
0017 #include <sensors/sensors.h>
0018 
0019 LmSensorsPlugin::LmSensorsPlugin(QObject *parent, const QVariantList &args)
0020     : KSysGuard::SensorPlugin(parent, args)
0021 {
0022     auto container = new KSysGuard::SensorContainer(QStringLiteral("lmsensors"), i18n( "Hardware Sensors" ), this);
0023 
0024     const std::array<QByteArray, 3> blacklist{"coretemp","k10temp","amdgpu"}; //already handled by other plugins
0025     int chipNumber = 0;
0026     while (const sensors_chip_name * const chipName = sensors_get_detected_chips(nullptr, &chipNumber)) {
0027         if (std::find(blacklist.cbegin(), blacklist.cend(), chipName->prefix) != blacklist.cend()) {
0028             continue;
0029         }
0030         int requiredBytes  = sensors_snprintf_chip_name(nullptr, 0, chipName) + 1;
0031         QByteArray name;
0032         name.resize(requiredBytes);
0033         sensors_snprintf_chip_name(name.data(), name.size(), chipName);
0034 
0035         // In some cases, sensor names may end with `\x00`. trimmed() unfortunately does not get rid of that
0036         // for us so convert them to spaces so trimmed will remove them.
0037         name.replace('\x00', ' ');
0038         name = name.trimmed();
0039 
0040         const QString nameString = QString::fromUtf8(name);
0041         KSysGuard::SensorObject *sensorObject = container->object(nameString);
0042         if (!sensorObject) {
0043             sensorObject = new KSysGuard::SensorObject(nameString, nameString, container);
0044         }
0045         int featureNumber = 0;
0046         while (const sensors_feature * const feature = sensors_get_features(chipName, &featureNumber)) {
0047             const QString id = QString::fromUtf8(feature->name);
0048             if (sensorObject->sensor(id)) {
0049                 continue;
0050             }
0051             if (auto sensor = KSysGuard::makeSensorsFeatureSensor(id, chipName, feature, sensorObject)) {
0052                 m_sensors.push_back(sensor);
0053             }
0054         }
0055     }
0056 }
0057 
0058 LmSensorsPlugin::~LmSensorsPlugin()
0059 {
0060 }
0061 
0062 
0063 QString LmSensorsPlugin::providerName() const
0064 {
0065     return QStringLiteral("lmsensors");
0066 }
0067 
0068 void LmSensorsPlugin::update()
0069 {
0070     for (auto sensor : std::as_const(m_sensors)) {
0071         sensor->update();
0072     }
0073 }
0074 
0075 K_PLUGIN_CLASS_WITH_JSON(LmSensorsPlugin, "metadata.json")
0076 #include "lmsensors.moc"
0077 
0078 #include "moc_lmsensors.cpp"