Files
fastfetch/src/modules/battery.c
T

104 lines
3.0 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>
static void printBattery(FFinstance* instance, uint8_t index)
2021-02-18 22:25:36 +01:00
{
2021-03-03 21:03:12 +01:00
char manufactor[128];
2021-03-19 20:57:07 +01:00
char manufactorPath[42];
sprintf(manufactorPath, "/sys/class/power_supply/BAT%i/manufacturer", index);
ffGetFileContent(manufactorPath, manufactor, sizeof(manufactor));
2021-03-03 21:03:12 +01:00
char model[128];
2021-03-19 20:57:07 +01:00
char modelPath[40];
sprintf(modelPath, "/sys/class/power_supply/BAT%i/model_name", index);
ffGetFileContent(modelPath, model, sizeof(model));
2021-03-03 21:03:12 +01:00
2021-03-01 19:53:20 +01:00
char technology[32];
2021-03-19 20:57:07 +01:00
char technologyPath[40];
sprintf(technologyPath, "/sys/class/power_supply/BAT%i/technology", index);
ffGetFileContent(technologyPath, technology, sizeof(technology));
2021-02-18 22:25:36 +01:00
2021-03-01 19:53:20 +01:00
char capacity[32];
2021-03-19 20:57:07 +01:00
char capacityPath[38];
sprintf(capacityPath, "/sys/class/power_supply/BAT%i/capacity", index);
ffGetFileContent(capacityPath, capacity, sizeof(capacity));
2021-02-18 22:25:36 +01:00
2021-03-01 19:53:20 +01:00
char status[32];
2021-03-19 20:57:07 +01:00
char statusPath[36];
sprintf(statusPath, "/sys/class/power_supply/BAT%i/status", index);
ffGetFileContent(statusPath, status, sizeof(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-19 20:57:07 +01:00
if(manufactor[0] == '\0' && model[0] == '\0' && technology[0] == '\0' && capacity[0] == '\0' && status[0] == '\0' && ffStrbufIsEmpty(&instance->config.batteryFormat))
2021-02-19 16:23:41 +01:00
{
2021-03-03 21:03:12 +01:00
ffPrintError(instance, key, "No file in /sys/class/power_supply/BAT0/ could be read or all battery options are disabled");
2021-02-18 22:25:36 +01:00
return;
2021-02-19 16:23:41 +01:00
}
2021-03-01 19:53:20 +01:00
2021-03-03 21:03:12 +01:00
ffPrintLogoAndKey(instance, key);
2021-03-19 20:57:07 +01:00
if(ffStrbufIsEmpty(&instance->config.batteryFormat))
2021-03-04 20:49:12 +01:00
{
2021-03-19 20:57:07 +01:00
if(manufactor[0] != '\0')
printf("%s ", manufactor);
2021-03-01 19:53:20 +01:00
2021-03-19 20:57:07 +01:00
if(model[0] != '\0')
printf("%s ", model);
2021-03-03 21:03:12 +01:00
2021-03-19 20:57:07 +01:00
if(technology[0] != '\0')
printf("(%s) ", technology);
2021-02-18 22:25:36 +01:00
2021-03-19 20:57:07 +01:00
if(capacity[0] != '\0')
{
printf("[%s%%", capacity);
if(status[0] == '\0')
puts("]");
else
printf("; %s]\n", status);
}
2021-03-01 19:53:20 +01:00
else
2021-03-19 20:57:07 +01:00
{
if(status[0] != '\0')
printf("%s", status);
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-19 20:57:07 +01:00
FFstrbuf battery;
ffStrbufInit(&battery);
ffParseFormatString(&battery, &instance->config.batteryFormat, 5,
(FFformatarg){FF_FORMAT_ARG_TYPE_STRING, manufactor},
(FFformatarg){FF_FORMAT_ARG_TYPE_STRING, model},
(FFformatarg){FF_FORMAT_ARG_TYPE_STRING, technology},
(FFformatarg){FF_FORMAT_ARG_TYPE_STRING, capacity},
(FFformatarg){FF_FORMAT_ARG_TYPE_STRING, status}
);
ffStrbufWriteTo(&battery, stdout);
putchar('\n');
ffStrbufDestroy(&battery);
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)
{
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
}