Files
fastfetch/src/modules/battery.c
T

183 lines
5.3 KiB
C
Raw Normal View History

2021-02-18 22:25:36 +01:00
#include "fastfetch.h"
2021-04-24 13:05:43 +02:00
#include <unistd.h>
2021-03-03 21:03:12 +01:00
#include <dirent.h>
2021-04-15 18:59:26 +02:00
#define FF_BATTERY_MODULE_NAME "Battery"
#define FF_BATTERY_NUM_FORMAT_ARGS 5
2021-04-03 21:51:50 +02:00
2021-12-31 14:12:44 +01:00
typedef struct BatteryResult
{
FFstrbuf manufacturer;
FFstrbuf modelName;
FFstrbuf technology;
FFstrbuf capacity;
FFstrbuf status;
} BatteryResult;
static void parseBattery(FFstrbuf* dir, FFlist* results)
2021-02-18 22:25:36 +01:00
{
2021-04-24 13:05:43 +02:00
uint32_t dirLength = dir->length;
2021-12-31 14:12:44 +01:00
FFstrbuf testBatteryBuffer;
ffStrbufInit(&testBatteryBuffer);
2021-03-03 21:03:12 +01:00
2021-12-31 14:12:44 +01:00
//type must exist and be "Battery"
ffStrbufAppendS(dir, "/type");
ffGetFileContent(dir->chars, &testBatteryBuffer);
2021-04-24 13:05:43 +02:00
ffStrbufSubstrBefore(dir, dirLength);
2021-03-03 21:03:12 +01:00
2021-12-31 14:12:44 +01:00
if(ffStrbufIgnCaseCompS(&testBatteryBuffer, "Battery") != 0)
{
ffStrbufDestroy(&testBatteryBuffer);
return;
}
2021-02-18 22:25:36 +01:00
2021-12-31 14:12:44 +01:00
//scope may not exist or must not be "Device"
ffStrbufAppendS(dir, "/scope");
ffGetFileContent(dir->chars, &testBatteryBuffer);
2021-04-24 13:05:43 +02:00
ffStrbufSubstrBefore(dir, dirLength);
2021-02-18 22:25:36 +01:00
2021-12-31 14:12:44 +01:00
if(ffStrbufIgnCaseCompS(&testBatteryBuffer, "Device") == 0)
{
ffStrbufDestroy(&testBatteryBuffer);
return;
}
ffStrbufDestroy(&testBatteryBuffer);
BatteryResult* result = ffListAdd(results);
2021-03-03 21:03:12 +01:00
2021-12-31 14:12:44 +01:00
//capacity must exist and be not empty
ffStrbufInit(&result->capacity);
ffStrbufAppendS(dir, "/capacity");
ffGetFileContent(dir->chars, &result->capacity);
ffStrbufSubstrBefore(dir, dirLength);
2021-05-05 19:52:50 +02:00
2021-12-31 14:12:44 +01:00
if(result->capacity.length == 0)
2021-04-15 18:59:26 +02:00
{
2021-12-31 14:12:44 +01:00
ffStrbufDestroy(&result->capacity);
--results->length;
2021-04-03 21:51:50 +02:00
return;
2021-04-03 11:39:59 +02:00
}
2021-03-01 19:53:20 +01:00
2021-12-31 14:12:44 +01:00
//At this point, we have a battery. Try to get as much values as possible.
ffStrbufInit(&result->manufacturer);
ffStrbufAppendS(dir, "/manufacturer");
ffGetFileContent(dir->chars, &result->manufacturer);
ffStrbufSubstrBefore(dir, dirLength);
ffStrbufInit(&result->modelName);
ffStrbufAppendS(dir, "/model_name");
ffGetFileContent(dir->chars, &result->modelName);
ffStrbufSubstrBefore(dir, dirLength);
ffStrbufInit(&result->technology);
ffStrbufAppendS(dir, "/technology");
ffGetFileContent(dir->chars, &result->technology);
ffStrbufSubstrBefore(dir, dirLength);
ffStrbufInit(&result->status);
ffStrbufAppendS(dir, "/status");
ffGetFileContent(dir->chars, &result->status);
ffStrbufSubstrBefore(dir, dirLength);
}
static void printBattery(FFinstance* instance, const BatteryResult* result, uint8_t index)
{
2021-03-23 19:48:52 +01:00
if(instance->config.batteryFormat.length == 0)
2021-03-04 20:49:12 +01:00
{
2021-04-15 18:59:26 +02:00
ffPrintLogoAndKey(instance, FF_BATTERY_MODULE_NAME, index, &instance->config.batteryKey);
2021-12-31 14:12:44 +01:00
bool showStatus =
result->status.length > 0 &&
ffStrbufIgnCaseCompS(&result->status, "Unknown") != 0;
2021-05-30 21:37:27 +02:00
2021-12-31 14:12:44 +01:00
if(result->capacity.length > 0)
2021-03-19 20:57:07 +01:00
{
2021-12-31 14:12:44 +01:00
ffStrbufWriteTo(&result->capacity, stdout);
2021-05-30 21:35:04 +02:00
putchar('%');
2021-03-27 18:14:19 +01:00
2021-05-30 21:37:27 +02:00
if(showStatus)
2021-06-26 11:39:42 +02:00
fputs(" [", stdout);
2021-03-19 20:57:07 +01:00
}
2021-05-30 21:35:04 +02:00
2021-05-30 21:37:27 +02:00
if(showStatus)
2021-06-26 11:39:42 +02:00
{
2021-12-31 14:12:44 +01:00
ffStrbufWriteTo(&result->status, stdout);
2021-05-30 21:35:04 +02:00
2021-12-31 14:12:44 +01:00
if(result->capacity.length > 0)
2021-06-26 11:39:42 +02:00
putchar(']');
}
2021-05-30 21:35:04 +02:00
putchar('\n');
2021-02-19 16:23:41 +01:00
}
2021-03-01 19:53:20 +01:00
else
2021-02-19 16:23:41 +01:00
{
2021-04-15 18:59:26 +02:00
ffPrintFormatString(instance, FF_BATTERY_MODULE_NAME, index, &instance->config.batteryKey, &instance->config.batteryFormat, NULL, FF_BATTERY_NUM_FORMAT_ARGS, (FFformatarg[]){
2021-12-31 14:12:44 +01:00
{FF_FORMAT_ARG_TYPE_STRBUF, &result->manufacturer},
{FF_FORMAT_ARG_TYPE_STRBUF, &result->modelName},
{FF_FORMAT_ARG_TYPE_STRBUF, &result->technology},
{FF_FORMAT_ARG_TYPE_STRBUF, &result->capacity},
{FF_FORMAT_ARG_TYPE_STRBUF, &result->status}
2021-04-15 18:59:26 +02:00
});
2021-02-19 16:23:41 +01:00
}
2021-03-03 21:03:12 +01:00
}
2021-02-18 22:25:36 +01:00
2021-03-03 21:03:12 +01:00
void ffPrintBattery(FFinstance* instance)
{
2021-04-24 13:05:43 +02:00
FFstrbuf baseDir;
ffStrbufInitA(&baseDir, 64);
if(instance->config.batteryDir.length > 0)
2021-04-24 14:37:40 +02:00
{
2021-04-24 13:05:43 +02:00
ffStrbufAppend(&baseDir, &instance->config.batteryDir);
2021-12-31 14:12:44 +01:00
if(!ffStrbufEndsWithC(&baseDir, '/'))
2021-04-24 14:37:40 +02:00
ffStrbufAppendC(&baseDir, '/');
}
2021-04-24 13:05:43 +02:00
else
2021-12-31 14:12:44 +01:00
{
2021-04-24 13:05:43 +02:00
ffStrbufAppendS(&baseDir, "/sys/class/power_supply/");
2021-12-31 14:12:44 +01:00
}
2021-04-24 14:37:40 +02:00
2021-04-24 13:05:43 +02:00
uint32_t baseDirLength = baseDir.length;
2021-04-15 18:59:26 +02:00
2021-04-24 13:05:43 +02:00
DIR* dirp = opendir(baseDir.chars);
if(dirp == NULL)
2021-03-03 21:03:12 +01:00
{
2021-04-24 13:05:43 +02:00
ffPrintError(instance, FF_BATTERY_MODULE_NAME, 0, &instance->config.batteryKey, &instance->config.batteryFormat, FF_BATTERY_NUM_FORMAT_ARGS, "opendir(\"%s\") == NULL", baseDir.chars);
ffStrbufDestroy(&baseDir);
return;
}
2021-12-31 14:12:44 +01:00
FFlist results;
ffListInitA(&results, sizeof(BatteryResult), 4);
2021-03-03 21:03:12 +01:00
2021-04-24 13:05:43 +02:00
struct dirent* entry;
while((entry = readdir(dirp)) != NULL)
{
ffStrbufAppendS(&baseDir, entry->d_name);
2021-12-31 14:12:44 +01:00
parseBattery(&baseDir, &results);
2021-04-24 13:05:43 +02:00
ffStrbufSubstrBefore(&baseDir, baseDirLength);
2021-03-03 21:03:12 +01:00
}
2021-04-03 11:39:59 +02:00
2021-12-31 14:12:44 +01:00
closedir(dirp);
2021-04-03 11:39:59 +02:00
2021-12-31 14:12:44 +01:00
for(uint8_t i = 0; i < (uint8_t) results.length; i++)
2021-04-15 18:59:26 +02:00
{
2021-12-31 14:12:44 +01:00
BatteryResult* result = ffListGet(&results, i);
printBattery(instance, result, i);
ffStrbufDestroy(&result->manufacturer);
ffStrbufDestroy(&result->modelName);
ffStrbufDestroy(&result->technology);
ffStrbufDestroy(&result->capacity);
ffStrbufDestroy(&result->status);
2021-04-03 11:39:59 +02:00
}
2021-04-24 13:05:43 +02:00
2021-12-31 14:12:44 +01:00
if(results.length == 0)
ffPrintError(instance, FF_BATTERY_MODULE_NAME, 0, &instance->config.batteryKey, &instance->config.batteryFormat, FF_BATTERY_NUM_FORMAT_ARGS, "%s doesn't contain any battery folder", baseDir.chars);
ffListDestroy(&results);
2021-04-24 13:05:43 +02:00
ffStrbufDestroy(&baseDir);
2021-03-27 18:14:19 +01:00
}