Files
fastfetch/src/modules/battery.c
T

141 lines
3.9 KiB
C
Raw Normal View History

2021-02-18 22:25:36 +01:00
#include "fastfetch.h"
2021-03-03 21:03:12 +01:00
#include <dirent.h>
2021-04-03 21:51:50 +02:00
static void getKey(FFinstance* instance, FFstrbuf* key, uint8_t counter, bool showCounter)
{
if(instance->config.batteryKey.length == 0)
{
if(showCounter)
ffStrbufAppendF(key, "Battery %hhu", counter);
else
ffStrbufSetS(key, "Battery");
}
else
{
ffParseFormatString(key, &instance->config.batteryKey, 1,
(FFformatarg){FF_FORMAT_ARG_TYPE_UINT8, &counter}
);
}
}
2021-03-03 21:03:12 +01:00
static void printBattery(FFinstance* instance, uint8_t index)
2021-02-18 22:25:36 +01:00
{
FF_STRBUF_CREATE(manufactor);
2021-03-19 20:57:07 +01:00
char manufactorPath[42];
2021-04-03 11:39:59 +02:00
sprintf(manufactorPath, "/sys/class/power_supply/BAT%hhu/manufacturer", index);
ffGetFileContent(manufactorPath, &manufactor);
2021-03-03 21:03:12 +01:00
FF_STRBUF_CREATE(model);
2021-03-19 20:57:07 +01:00
char modelPath[40];
2021-04-03 11:39:59 +02:00
sprintf(modelPath, "/sys/class/power_supply/BAT%hhu/model_name", index);
ffGetFileContent(modelPath, &model);
2021-03-03 21:03:12 +01:00
FF_STRBUF_CREATE(technology);
2021-03-19 20:57:07 +01:00
char technologyPath[40];
2021-04-03 11:39:59 +02:00
sprintf(technologyPath, "/sys/class/power_supply/BAT%hhu/technology", index);
ffGetFileContent(technologyPath, &technology);
2021-02-18 22:25:36 +01:00
FF_STRBUF_CREATE(capacity);
2021-03-19 20:57:07 +01:00
char capacityPath[38];
2021-04-03 11:39:59 +02:00
sprintf(capacityPath, "/sys/class/power_supply/BAT%hhu/capacity", index);
ffGetFileContent(capacityPath, &capacity);
2021-02-18 22:25:36 +01:00
FF_STRBUF_CREATE(status);
2021-03-19 20:57:07 +01:00
char statusPath[36];
2021-04-03 11:39:59 +02:00
sprintf(statusPath, "/sys/class/power_supply/BAT%hhu/status", index);
ffGetFileContent(statusPath, &status);
2021-03-03 21:03:12 +01:00
2021-04-03 11:39:59 +02:00
FF_STRBUF_CREATE(key);
2021-04-03 21:51:50 +02:00
getKey(instance, &key, index + 1, true);
if(
manufactor.length == 0 &&
model.length == 0 &&
technology.length == 0 &&
capacity.length == 0 &&
status.length == 0
) {
ffPrintError(instance, &key, NULL, "No file in /sys/class/power_supply/BAT%hhu/ could be read or all battery options are disabled", index);
ffStrbufDestroy(&key);
return;
2021-04-03 11:39:59 +02:00
}
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-04-03 11:39:59 +02:00
ffPrintLogoAndKey(instance, &key, NULL);
2021-03-23 19:48:52 +01:00
if(manufactor.length > 0)
printf("%s ", manufactor.chars);
2021-03-01 19:53:20 +01:00
2021-03-23 19:48:52 +01:00
if(model.length > 0)
printf("%s ", model.chars);
2021-03-03 21:03:12 +01:00
2021-03-23 19:48:52 +01:00
if(technology.length > 0)
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
{
printf("[%s%%", capacity.chars);
2021-03-27 18:14:19 +01:00
2021-03-23 19:48:52 +01:00
if(status.length == 0)
2021-03-19 20:57:07 +01:00
puts("]");
else
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)
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-04-03 11:39:59 +02:00
ffPrintFormatString(instance, &key, NULL, &instance->config.batteryFormat, 5,
(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
}
ffStrbufDestroy(&manufactor);
ffStrbufDestroy(&model);
ffStrbufDestroy(&technology);
ffStrbufDestroy(&capacity);
ffStrbufDestroy(&status);
2021-04-03 11:39:59 +02:00
ffStrbufDestroy(&key);
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-03 11:39:59 +02:00
uint8_t batteryCounter = 0;
2021-03-03 21:03:12 +01:00
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)
{
2021-04-03 11:39:59 +02:00
printBattery(instance, batteryCounter++);
2021-03-03 21:03:12 +01:00
closedir(dir);
}
}
2021-04-03 11:39:59 +02:00
if(batteryCounter == 0)
{
if(!instance->config.showErrors)
return;
FF_STRBUF_CREATE(key);
2021-04-03 21:51:50 +02:00
getKey(instance, &key, 1, false);
2021-04-03 11:39:59 +02:00
ffPrintError(instance, &key, NULL, "No battery found in /sys/class/power_supply/");
ffStrbufDestroy(&key);
}
2021-03-27 18:14:19 +01:00
}