mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-29 08:13:44 +01:00
support for more than 5 batteries
This commit is contained in:
@@ -74,6 +74,8 @@ static void defaultConfig(FFconfig* config)
|
||||
ffStrbufInitA(&config->libWayland, 1);
|
||||
|
||||
ffStrbufInitA(&config->diskFolders, 1);
|
||||
|
||||
ffStrbufInitA(&config->batteryDir, 1);
|
||||
}
|
||||
|
||||
void ffInitInstance(FFinstance* instance)
|
||||
|
||||
@@ -115,6 +115,7 @@ static inline void printHelp()
|
||||
"\n"
|
||||
"Module specific options:\n"
|
||||
" --disk-folders <folders>: A colon seperated list of folder paths for the disk output. Default is \"/:/home\"\n"
|
||||
" --battery-dir <folder>: The directory were the battery folders are in. Standard: /sys/class/power_supply/\n"
|
||||
"\n"
|
||||
"Parsing is not case sensetive. E.g. \"--lib-PCI\" is equal to \"--Lib-Pci\"\n"
|
||||
"If an value starts with an ?, it is optional. \"true\" will be used if not set.\n"
|
||||
@@ -667,6 +668,8 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
optionParseString(key, value, &instance->config.libWayland);
|
||||
else if(strcasecmp(key, "--disk-folders") == 0)
|
||||
optionParseString(key, value, &instance->config.diskFolders);
|
||||
else if(strcasecmp(key, "--battery-dir") == 0)
|
||||
optionParseString(key, value, &instance->config.batteryDir);
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Error: unknown option: %s\n", key);
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include "fastfetch_config.h"
|
||||
|
||||
#include "util/FFstrbuf.h"
|
||||
#include "util/FFlist.h"
|
||||
|
||||
#define UNUSED(...) (void)(__VA_ARGS__)
|
||||
|
||||
@@ -97,6 +98,8 @@ typedef struct FFconfig
|
||||
|
||||
FFstrbuf diskFolders;
|
||||
|
||||
FFstrbuf batteryDir;
|
||||
|
||||
} FFconfig;
|
||||
|
||||
typedef struct FFOSResult
|
||||
|
||||
+68
-38
@@ -1,40 +1,43 @@
|
||||
#include "fastfetch.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
|
||||
#define FF_BATTERY_MODULE_NAME "Battery"
|
||||
#define FF_BATTERY_NUM_FORMAT_ARGS 5
|
||||
|
||||
static void printBattery(FFinstance* instance, uint8_t index)
|
||||
static void printBattery(FFinstance* instance, FFstrbuf* dir, uint32_t index)
|
||||
{
|
||||
uint32_t dirLength = dir->length;
|
||||
|
||||
FF_STRBUF_CREATE(manufactor);
|
||||
char manufactorPath[42];
|
||||
sprintf(manufactorPath, "/sys/class/power_supply/BAT%hhu/manufacturer", index);
|
||||
ffGetFileContent(manufactorPath, &manufactor);
|
||||
ffStrbufAppendS(dir, "/manufacturer");
|
||||
ffGetFileContent(dir->chars, &manufactor);
|
||||
ffStrbufSubstrBefore(dir, dirLength);
|
||||
|
||||
FF_STRBUF_CREATE(model);
|
||||
char modelPath[40];
|
||||
sprintf(modelPath, "/sys/class/power_supply/BAT%hhu/model_name", index);
|
||||
ffGetFileContent(modelPath, &model);
|
||||
ffStrbufAppendS(dir, "/model_name");
|
||||
ffGetFileContent(dir->chars, &model);
|
||||
ffStrbufSubstrBefore(dir, dirLength);
|
||||
|
||||
FF_STRBUF_CREATE(technology);
|
||||
char technologyPath[40];
|
||||
sprintf(technologyPath, "/sys/class/power_supply/BAT%hhu/technology", index);
|
||||
ffGetFileContent(technologyPath, &technology);
|
||||
ffStrbufAppendS(dir, "/technology");
|
||||
ffGetFileContent(dir->chars, &technology);
|
||||
ffStrbufSubstrBefore(dir, dirLength);
|
||||
|
||||
FF_STRBUF_CREATE(capacity);
|
||||
char capacityPath[38];
|
||||
sprintf(capacityPath, "/sys/class/power_supply/BAT%hhu/capacity", index);
|
||||
ffGetFileContent(capacityPath, &capacity);
|
||||
ffStrbufAppendS(dir, "/capacity");
|
||||
ffGetFileContent(dir->chars, &capacity);
|
||||
ffStrbufSubstrBefore(dir, dirLength);
|
||||
|
||||
FF_STRBUF_CREATE(status);
|
||||
char statusPath[36];
|
||||
sprintf(statusPath, "/sys/class/power_supply/BAT%hhu/status", index);
|
||||
ffGetFileContent(statusPath, &status);
|
||||
ffStrbufAppendS(dir, "/status");
|
||||
ffGetFileContent(dir->chars, &status);
|
||||
ffStrbufSubstrBefore(dir, dirLength);
|
||||
|
||||
if(manufactor.length == 0 && model.length == 0 && technology.length == 0 && capacity.length == 0 && status.length == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_BATTERY_MODULE_NAME, index, &instance->config.batteryKey, &instance->config.batteryFormat, FF_BATTERY_NUM_FORMAT_ARGS, "No file in /sys/class/power_supply/BAT%hhu/ could be read or all battery options are disabled", index);
|
||||
ffPrintError(instance, FF_BATTERY_MODULE_NAME, index, &instance->config.batteryKey, &instance->config.batteryFormat, FF_BATTERY_NUM_FORMAT_ARGS, "No file in %s could be read or all battery options are disabled", dir->chars);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -87,34 +90,61 @@ static void printBattery(FFinstance* instance, uint8_t index)
|
||||
ffStrbufDestroy(&status);
|
||||
}
|
||||
|
||||
#define FF_BATTERY_MAX_INDEX 5
|
||||
|
||||
void ffPrintBattery(FFinstance* instance)
|
||||
{
|
||||
uint8_t batteryCounter = 0;
|
||||
uint8_t batteryIndicies[FF_BATTERY_MAX_INDEX];
|
||||
FFstrbuf baseDir;
|
||||
ffStrbufInitA(&baseDir, 64);
|
||||
if(instance->config.batteryDir.length > 0)
|
||||
ffStrbufAppend(&baseDir, &instance->config.batteryDir);
|
||||
else
|
||||
ffStrbufAppendS(&baseDir, "/sys/class/power_supply/");
|
||||
uint32_t baseDirLength = baseDir.length;
|
||||
|
||||
for(uint8_t i = 0; i < FF_BATTERY_MAX_INDEX; i++)
|
||||
DIR* dirp = opendir(baseDir.chars);
|
||||
if(dirp == NULL)
|
||||
{
|
||||
char path[30];
|
||||
sprintf(path, "/sys/class/power_supply/BAT%i", i);
|
||||
|
||||
DIR* dir = opendir(path);
|
||||
if(dir != NULL)
|
||||
{
|
||||
batteryIndicies[batteryCounter++] = i;
|
||||
closedir(dir);
|
||||
}
|
||||
}
|
||||
|
||||
if(batteryCounter == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_BATTERY_MODULE_NAME, 0, &instance->config.batteryKey, &instance->config.batteryFormat, FF_BATTERY_NUM_FORMAT_ARGS, "/sys/class/power_supply/ doesn't contain any BAT* folder");
|
||||
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;
|
||||
}
|
||||
|
||||
for(uint8_t i = 0; i < batteryCounter; i++)
|
||||
FFlist dirs;
|
||||
ffListInitA(&dirs, sizeof(FFstrbuf), 4);
|
||||
|
||||
struct dirent* entry;
|
||||
|
||||
while((entry = readdir(dirp)) != NULL)
|
||||
{
|
||||
printBattery(instance, batteryIndicies[i]);
|
||||
ffStrbufAppendS(&baseDir, entry->d_name);
|
||||
ffStrbufAppendS(&baseDir, "/capacity");
|
||||
|
||||
if(access(baseDir.chars, F_OK) == 0)
|
||||
{
|
||||
FFstrbuf* name = ffListAdd(&dirs);
|
||||
ffStrbufInit(name);
|
||||
ffStrbufSetS(name, entry->d_name);
|
||||
}
|
||||
|
||||
ffStrbufSubstrBefore(&baseDir, baseDirLength);
|
||||
}
|
||||
|
||||
if(dirs.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 BAT* folder", baseDir.chars);
|
||||
ffListDestroy(&dirs);
|
||||
ffStrbufDestroy(&baseDir);
|
||||
return;
|
||||
}
|
||||
|
||||
for(uint32_t i = 0; i < dirs.length; i++)
|
||||
{
|
||||
FFstrbuf* name = ffListGet(&dirs, i);
|
||||
ffStrbufAppend(&baseDir, name);
|
||||
printBattery(instance, &baseDir, dirs.length == 1 ? 0 : i);
|
||||
ffStrbufSubstrBefore(&baseDir, baseDirLength);
|
||||
ffStrbufDestroy(name);
|
||||
}
|
||||
|
||||
ffListDestroy(&dirs);
|
||||
ffStrbufDestroy(&baseDir);
|
||||
}
|
||||
|
||||
+4
-1
@@ -24,7 +24,10 @@ void* ffListGet(FFlist* list, uint32_t index)
|
||||
void* ffListAdd(FFlist* list)
|
||||
{
|
||||
if(list->length == list->capacity)
|
||||
list->data = realloc(list->data, list->capacity * 2 * list->elementSize);
|
||||
{
|
||||
list->capacity = list->capacity * 2 * list->elementSize;
|
||||
list->data = realloc(list->data, list->capacity);
|
||||
}
|
||||
void* adress = list->data + (list->length * list->elementSize);
|
||||
++list->length;
|
||||
return adress;
|
||||
|
||||
Reference in New Issue
Block a user