Temp detection

This commit is contained in:
Linus Dierheimer
2022-01-16 16:33:15 +01:00
parent 20427a534e
commit a0f5432903
2 changed files with 128 additions and 0 deletions
+113
View File
@@ -0,0 +1,113 @@
#include "fastfetch.h"
#include <pthread.h>
#include <dirent.h>
#include <string.h>
#include <inttypes.h>
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;
}
+15
View File
@@ -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 */
/********************/