From a0f54329031df3a776cd68e7c81c56574481c9be Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Sun, 16 Jan 2022 16:33:15 +0100 Subject: [PATCH] Temp detection --- src/detection/temps.c | 113 ++++++++++++++++++++++++++++++++++++++++++ src/fastfetch.h | 15 ++++++ 2 files changed, 128 insertions(+) create mode 100644 src/detection/temps.c diff --git a/src/detection/temps.c b/src/detection/temps.c new file mode 100644 index 000000000..cb923e45f --- /dev/null +++ b/src/detection/temps.c @@ -0,0 +1,113 @@ +#include "fastfetch.h" + +#include +#include +#include +#include + +static bool isTempFile(const char* name) +{ + return + name[0] == 't' && + name[1] == 'e' && + name[2] == 'm' && + name[3] == 'p' && + name[4] >= '0' && + name[4] <= '9' && + strncmp(name + 5, "_input", 6) == 0; +} + +static bool parseHwmonDir(FFstrbuf* dir, FFTempValue* value) +{ + DIR* dirp = opendir(dir->chars); + if(dirp == NULL) + return false; + + uint32_t dirLength = dir->length; + + struct dirent* dirent; + while((dirent = readdir(dirp)) != NULL) + { + if(isTempFile(dirent->d_name)) + { + ffStrbufAppendS(dir, dirent->d_name); + ffGetFileContent(dir->chars, &value->value); + ffStrbufSubstrBefore(dir, dirLength); + break; + } + } + + closedir(dirp); + + if(value->value.length == 0) + return false; + + ffStrbufAppendS(dir, "name"); + ffGetFileContent(dir->chars, &value->name); + ffStrbufSubstrBefore(dir, dirLength); + + ffStrbufAppendS(dir, "device/class"); + ffGetFileContent(dir->chars, &value->class); + ffStrbufSubstrBefore(dir, dirLength); + + return value->name.length > 0 || value->class.length > 0; +} + +const FFTempsResult* ffDetectTemps(const FFinstance* instance) +{ + FF_UNUSED(instance) + + static FFTempsResult result; + static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + static bool init = false; + + pthread_mutex_lock(&mutex); + if(init) + { + pthread_mutex_unlock(&mutex); + return &result; + } + init = true; + + ffListInitA(&result.values, sizeof(FFTempValue), 16); + + FFstrbuf baseDir; + ffStrbufInitA(&baseDir, 64); + ffStrbufAppendS(&baseDir, "/sys/class/hwmon/"); + + uint32_t baseDirLength = baseDir.length; + + DIR* dirp = opendir(baseDir.chars); + if(dirp == NULL) + { + pthread_mutex_unlock(&mutex); + return &result; + } + + struct dirent* entry; + while((entry = readdir(dirp)) != NULL) + { + if(entry->d_name[0] == '.') + continue; + + ffStrbufAppendS(&baseDir, entry->d_name); + ffStrbufAppendC(&baseDir, '/'); + + FFTempValue* temp = ffListAdd(&result.values); + ffStrbufInit(&temp->name); + ffStrbufInit(&temp->class); + ffStrbufInit(&temp->value); + if(!parseHwmonDir(&baseDir, temp)) + { + ffStrbufDestroy(&temp->name); + ffStrbufDestroy(&temp->class); + ffStrbufDestroy(&temp->value); + --result.values.length; + } + + ffStrbufSubstrBefore(&baseDir, baseDirLength); + } + + pthread_mutex_unlock(&mutex); + return &result; +} diff --git a/src/fastfetch.h b/src/fastfetch.h index 36be1393d..7c4f9e3fa 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -227,6 +227,18 @@ typedef struct FFDisplayServerResult FFlist resolutions; //List of FFResolutionResult } FFDisplayServerResult; +typedef struct FFTempValue +{ + FFstrbuf name; + FFstrbuf value; + FFstrbuf class; +} FFTempValue; + +typedef struct FFTempsResult +{ + FFlist values; //List of FFTempValue +} FFTempsResult; + typedef enum FFformatargtype { FF_FORMAT_ARG_TYPE_NULL = 0, @@ -427,6 +439,9 @@ const FFDisplayServerResult* ffConnectDisplayServer(const FFinstance* instance); //detection/terminalShell.c const FFTerminalShellResult* ffDetectTerminalShell(FFinstance* instance); +//detection/temps.c (currently unused) +const FFTempsResult* ffDetectTemps(const FFinstance* instance); + /********************/ /* Module functions */ /********************/