2022-09-16 11:03:46 +08:00
|
|
|
#include "fastfetch.h"
|
2023-01-28 12:40:12 +01:00
|
|
|
#include "common/io/io.h"
|
2022-09-16 11:03:46 +08:00
|
|
|
#include "battery.h"
|
2023-06-19 16:07:23 +08:00
|
|
|
#include "util/stringUtils.h"
|
2022-09-16 11:03:46 +08:00
|
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
|
|
static void parseBattery(FFstrbuf* dir, FFlist* results)
|
|
|
|
|
{
|
|
|
|
|
uint32_t dirLength = dir->length;
|
|
|
|
|
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY testBatteryBuffer = ffStrbufCreate();
|
2022-09-16 11:03:46 +08:00
|
|
|
|
|
|
|
|
//type must exist and be "Battery"
|
|
|
|
|
ffStrbufAppendS(dir, "/type");
|
|
|
|
|
ffReadFileBuffer(dir->chars, &testBatteryBuffer);
|
|
|
|
|
ffStrbufSubstrBefore(dir, dirLength);
|
|
|
|
|
|
|
|
|
|
if(ffStrbufIgnCaseCompS(&testBatteryBuffer, "Battery") != 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
//scope may not exist or must not be "Device"
|
|
|
|
|
ffStrbufAppendS(dir, "/scope");
|
|
|
|
|
ffReadFileBuffer(dir->chars, &testBatteryBuffer);
|
|
|
|
|
ffStrbufSubstrBefore(dir, dirLength);
|
|
|
|
|
|
|
|
|
|
if(ffStrbufIgnCaseCompS(&testBatteryBuffer, "Device") == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
BatteryResult* result = ffListAdd(results);
|
|
|
|
|
|
|
|
|
|
//capacity must exist and be not empty
|
|
|
|
|
ffStrbufAppendS(dir, "/capacity");
|
2022-10-18 22:55:28 +08:00
|
|
|
bool available = ffReadFileBuffer(dir->chars, &testBatteryBuffer);
|
2022-09-16 11:03:46 +08:00
|
|
|
ffStrbufSubstrBefore(dir, dirLength);
|
2022-10-18 22:55:28 +08:00
|
|
|
if(available)
|
|
|
|
|
result->capacity = ffStrbufToDouble(&testBatteryBuffer);
|
2023-04-15 15:29:54 +08:00
|
|
|
|
2022-10-18 22:55:28 +08:00
|
|
|
if(!available)
|
2022-09-16 11:03:46 +08:00
|
|
|
{
|
2022-10-18 22:55:28 +08:00
|
|
|
result->capacity = 0.0/0.0;
|
2022-09-16 11:03:46 +08:00
|
|
|
--results->length;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//At this point, we have a battery. Try to get as much values as possible.
|
|
|
|
|
|
|
|
|
|
ffStrbufInit(&result->manufacturer);
|
|
|
|
|
ffStrbufAppendS(dir, "/manufacturer");
|
|
|
|
|
ffReadFileBuffer(dir->chars, &result->manufacturer);
|
|
|
|
|
ffStrbufSubstrBefore(dir, dirLength);
|
|
|
|
|
|
|
|
|
|
ffStrbufInit(&result->modelName);
|
|
|
|
|
ffStrbufAppendS(dir, "/model_name");
|
|
|
|
|
ffReadFileBuffer(dir->chars, &result->modelName);
|
|
|
|
|
ffStrbufSubstrBefore(dir, dirLength);
|
|
|
|
|
|
|
|
|
|
ffStrbufInit(&result->technology);
|
|
|
|
|
ffStrbufAppendS(dir, "/technology");
|
|
|
|
|
ffReadFileBuffer(dir->chars, &result->technology);
|
|
|
|
|
ffStrbufSubstrBefore(dir, dirLength);
|
|
|
|
|
|
|
|
|
|
ffStrbufInit(&result->status);
|
|
|
|
|
ffStrbufAppendS(dir, "/status");
|
|
|
|
|
ffReadFileBuffer(dir->chars, &result->status);
|
|
|
|
|
ffStrbufSubstrBefore(dir, dirLength);
|
2022-09-25 15:23:00 +08:00
|
|
|
|
|
|
|
|
result->temperature = FF_BATTERY_TEMP_UNSET;
|
2022-09-16 11:03:46 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
const char* ffDetectBattery(FFBatteryOptions* options, FFlist* results)
|
2023-03-07 16:13:53 +08:00
|
|
|
{
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY baseDir = ffStrbufCreateA(64);
|
|
|
|
|
|
2023-06-12 09:20:39 +08:00
|
|
|
if(options->dir.length > 0)
|
2022-09-16 11:03:46 +08:00
|
|
|
{
|
2023-06-12 09:20:39 +08:00
|
|
|
ffStrbufAppend(&baseDir, &options->dir);
|
2022-09-17 00:34:37 +08:00
|
|
|
ffStrbufEnsureEndsWithC(&baseDir, '/');
|
2022-09-16 11:03:46 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ffStrbufAppendS(&baseDir, "/sys/class/power_supply/");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t baseDirLength = baseDir.length;
|
|
|
|
|
|
|
|
|
|
DIR* dirp = opendir(baseDir.chars);
|
|
|
|
|
if(dirp == NULL)
|
|
|
|
|
return "opendir(batteryDir) == NULL";
|
|
|
|
|
|
|
|
|
|
struct dirent* entry;
|
|
|
|
|
while((entry = readdir(dirp)) != NULL)
|
|
|
|
|
{
|
2023-06-19 16:07:23 +08:00
|
|
|
if(ffStrEquals(entry->d_name, ".") || ffStrEquals(entry->d_name, ".."))
|
2023-01-09 10:45:51 +08:00
|
|
|
continue;
|
|
|
|
|
|
2022-09-16 11:03:46 +08:00
|
|
|
ffStrbufAppendS(&baseDir, entry->d_name);
|
|
|
|
|
parseBattery(&baseDir, results);
|
|
|
|
|
ffStrbufSubstrBefore(&baseDir, baseDirLength);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
closedir(dirp);
|
|
|
|
|
|
2023-04-15 15:29:54 +08:00
|
|
|
if(results->length == 0)
|
2022-09-16 11:03:46 +08:00
|
|
|
return "batteryDir doesn't contain any battery folder";
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|