2021-02-18 22:25:36 +01:00
|
|
|
#include "fastfetch.h"
|
|
|
|
|
|
2021-03-03 21:03:12 +01:00
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
|
|
static void printBattery(FFinstance* instance, uint8_t index)
|
2021-02-18 22:25:36 +01:00
|
|
|
{
|
2021-03-21 16:52:17 +01:00
|
|
|
FF_STRBUF_CREATE(manufactor);
|
2021-03-19 20:57:07 +01:00
|
|
|
char manufactorPath[42];
|
|
|
|
|
sprintf(manufactorPath, "/sys/class/power_supply/BAT%i/manufacturer", index);
|
2021-03-21 16:52:17 +01:00
|
|
|
ffGetFileContent(manufactorPath, &manufactor);
|
2021-03-03 21:03:12 +01:00
|
|
|
|
2021-03-21 16:52:17 +01:00
|
|
|
FF_STRBUF_CREATE(model);
|
2021-03-19 20:57:07 +01:00
|
|
|
char modelPath[40];
|
|
|
|
|
sprintf(modelPath, "/sys/class/power_supply/BAT%i/model_name", index);
|
2021-03-21 16:52:17 +01:00
|
|
|
ffGetFileContent(modelPath, &model);
|
2021-03-03 21:03:12 +01:00
|
|
|
|
2021-03-21 16:52:17 +01:00
|
|
|
FF_STRBUF_CREATE(technology);
|
2021-03-19 20:57:07 +01:00
|
|
|
char technologyPath[40];
|
|
|
|
|
sprintf(technologyPath, "/sys/class/power_supply/BAT%i/technology", index);
|
2021-03-21 16:52:17 +01:00
|
|
|
ffGetFileContent(technologyPath, &technology);
|
2021-02-18 22:25:36 +01:00
|
|
|
|
2021-03-21 16:52:17 +01:00
|
|
|
FF_STRBUF_CREATE(capacity);
|
2021-03-19 20:57:07 +01:00
|
|
|
char capacityPath[38];
|
|
|
|
|
sprintf(capacityPath, "/sys/class/power_supply/BAT%i/capacity", index);
|
2021-03-21 16:52:17 +01:00
|
|
|
ffGetFileContent(capacityPath, &capacity);
|
2021-02-18 22:25:36 +01:00
|
|
|
|
2021-03-21 16:52:17 +01:00
|
|
|
FF_STRBUF_CREATE(status);
|
2021-03-19 20:57:07 +01:00
|
|
|
char statusPath[36];
|
|
|
|
|
sprintf(statusPath, "/sys/class/power_supply/BAT%i/status", index);
|
2021-03-21 16:52:17 +01:00
|
|
|
ffGetFileContent(statusPath, &status);
|
2021-03-03 21:03:12 +01:00
|
|
|
|
|
|
|
|
char key[10];
|
|
|
|
|
sprintf(key, "Battery %i", index);
|
2021-03-01 19:53:20 +01:00
|
|
|
|
2021-03-23 19:48:52 +01:00
|
|
|
if(instance->config.batteryFormat.length == 0)
|
2021-03-04 20:49:12 +01:00
|
|
|
{
|
2021-03-27 15:39:56 +01:00
|
|
|
if(
|
|
|
|
|
manufactor.length == 0 &&
|
|
|
|
|
model.length == 0 &&
|
|
|
|
|
technology.length == 0 &&
|
|
|
|
|
capacity.length == 0 &&
|
|
|
|
|
status.length == 0
|
|
|
|
|
) {
|
|
|
|
|
ffPrintError(instance, key, "No file in /sys/class/power_supply/BAT0/ could be read or all battery options are disabled");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ffPrintLogoAndKey(instance, key);
|
|
|
|
|
|
2021-03-23 19:48:52 +01:00
|
|
|
if(manufactor.length > 0)
|
2021-03-21 16:52:17 +01:00
|
|
|
printf("%s ", manufactor.chars);
|
2021-03-01 19:53:20 +01:00
|
|
|
|
2021-03-23 19:48:52 +01:00
|
|
|
if(model.length > 0)
|
2021-03-21 16:52:17 +01:00
|
|
|
printf("%s ", manufactor.chars);
|
2021-03-03 21:03:12 +01:00
|
|
|
|
2021-03-23 19:48:52 +01:00
|
|
|
if(technology.length > 0)
|
2021-03-21 16:52:17 +01:00
|
|
|
printf("(%s) ", technology.chars);
|
2021-02-18 22:25:36 +01:00
|
|
|
|
2021-03-23 19:48:52 +01:00
|
|
|
if(capacity.length > 0)
|
2021-03-19 20:57:07 +01:00
|
|
|
{
|
2021-03-21 16:52:17 +01:00
|
|
|
printf("[%s%%", capacity.chars);
|
2021-03-19 20:57:07 +01:00
|
|
|
|
2021-03-23 19:48:52 +01:00
|
|
|
if(status.length == 0)
|
2021-03-19 20:57:07 +01:00
|
|
|
puts("]");
|
|
|
|
|
else
|
2021-03-21 16:52:17 +01:00
|
|
|
printf("; %s]\n", status.chars);
|
2021-03-19 20:57:07 +01:00
|
|
|
}
|
2021-03-01 19:53:20 +01:00
|
|
|
else
|
2021-03-19 20:57:07 +01:00
|
|
|
{
|
2021-03-23 19:48:52 +01:00
|
|
|
if(status.length > 0)
|
2021-03-21 16:52:17 +01:00
|
|
|
printf("[%s]", status.chars);
|
2021-03-19 20:57:07 +01:00
|
|
|
else
|
|
|
|
|
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-03-27 15:39:56 +01:00
|
|
|
ffPrintFormatString(instance, key, &instance->config.batteryFormat, 5,
|
2021-03-21 16:52:17 +01:00
|
|
|
(FFformatarg){FF_FORMAT_ARG_TYPE_STRBUF, &manufactor},
|
|
|
|
|
(FFformatarg){FF_FORMAT_ARG_TYPE_STRBUF, &model},
|
|
|
|
|
(FFformatarg){FF_FORMAT_ARG_TYPE_STRBUF, &technology},
|
|
|
|
|
(FFformatarg){FF_FORMAT_ARG_TYPE_STRBUF, &capacity},
|
|
|
|
|
(FFformatarg){FF_FORMAT_ARG_TYPE_STRBUF, &status}
|
2021-03-19 20:57:07 +01:00
|
|
|
);
|
2021-02-19 16:23:41 +01:00
|
|
|
}
|
2021-03-21 16:52:17 +01:00
|
|
|
|
|
|
|
|
ffStrbufDestroy(&manufactor);
|
|
|
|
|
ffStrbufDestroy(&model);
|
|
|
|
|
ffStrbufDestroy(&technology);
|
|
|
|
|
ffStrbufDestroy(&capacity);
|
|
|
|
|
ffStrbufDestroy(&status);
|
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)
|
|
|
|
|
{
|
|
|
|
|
for(uint8_t i = 0; i < 5; i++)
|
|
|
|
|
{
|
|
|
|
|
char path[30];
|
|
|
|
|
sprintf(path, "/sys/class/power_supply/BAT%i", i);
|
|
|
|
|
|
|
|
|
|
DIR* dir = opendir(path);
|
|
|
|
|
if(dir != NULL)
|
|
|
|
|
{
|
|
|
|
|
printBattery(instance, i);
|
|
|
|
|
closedir(dir);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-18 22:25:36 +01:00
|
|
|
}
|