From c5c3cecb935b86713e2b1fef741b9cf7ee2acccc Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Sat, 24 Apr 2021 13:05:43 +0200 Subject: [PATCH] support for more than 5 batteries --- CMakeLists.txt | 1 + completions/bash | 1 + src/common/init.c | 2 + src/fastfetch.c | 3 ++ src/fastfetch.h | 3 ++ src/modules/battery.c | 106 +++++++++++++++++++++++++++--------------- src/util/FFlist.c | 5 +- 7 files changed, 82 insertions(+), 39 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f5873abc3..28a6f23fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,6 +57,7 @@ link_libraries( set(SRCS src/util/FFstrbuf.c + src/util/FFlist.c src/common/init.c src/common/threading.c src/common/io.c diff --git a/completions/bash b/completions/bash index 17fd8806f..e15309b0d 100644 --- a/completions/bash +++ b/completions/bash @@ -180,6 +180,7 @@ __fastfetch_completion() "--lib-Xrandr" "--lib-DConf" "--lib-wayland" + "--battery-dir" ) local FF_OPTIONS_LOGO=( diff --git a/src/common/init.c b/src/common/init.c index 67fc42b49..f154c866d 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -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) diff --git a/src/fastfetch.c b/src/fastfetch.c index 6bb7cd297..5c373d81c 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -115,6 +115,7 @@ static inline void printHelp() "\n" "Module specific options:\n" " --disk-folders : A colon seperated list of folder paths for the disk output. Default is \"/:/home\"\n" + " --battery-dir : 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); diff --git a/src/fastfetch.h b/src/fastfetch.h index 56bf1148c..937ac4206 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -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 diff --git a/src/modules/battery.c b/src/modules/battery.c index b322fba96..18b361d34 100644 --- a/src/modules/battery.c +++ b/src/modules/battery.c @@ -1,40 +1,43 @@ #include "fastfetch.h" +#include #include #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); } diff --git a/src/util/FFlist.c b/src/util/FFlist.c index c624e0711..1af070eb3 100644 --- a/src/util/FFlist.c +++ b/src/util/FFlist.c @@ -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;