Files
fastfetch/src/detection/temps.c
T

124 lines
3.0 KiB
C
Raw Normal View History

2022-01-16 16:33:15 +01:00
#include "fastfetch.h"
#include "detection/temps.h"
2022-07-25 11:48:48 +02:00
#include "common/io.h"
2022-01-16 16:33:15 +01:00
2022-06-18 14:25:31 +02:00
#include <string.h>
2022-01-16 16:33:15 +01:00
#include <pthread.h>
#include <dirent.h>
static bool isTempFile(const char* name)
{
return
2022-06-15 23:15:49 +02:00
strncmp(name, "temp", 4) == 0 &&
2022-01-16 16:33:15 +01:00
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;
2022-06-17 23:38:33 +02:00
value->value = 0.0 / 0.0; //use NaN as error value
FFstrbuf valueString;
ffStrbufInit(&valueString);
2022-01-16 16:33:15 +01:00
struct dirent* dirent;
while((dirent = readdir(dirp)) != NULL)
{
2022-06-17 23:38:33 +02:00
if(!isTempFile(dirent->d_name))
continue;
ffStrbufAppendS(dir, dirent->d_name);
2022-06-20 16:35:15 +02:00
ffReadFileBuffer(dir->chars, &valueString);
2022-06-17 23:38:33 +02:00
ffStrbufSubstrBefore(dir, dirLength);
//ffStrbufToDouble() returns NaN if the string couldn't be parsed
value->value = ffStrbufToDouble(&valueString);
if(value->value != value->value)
continue;
value->value /= 1000.0; //millidegrees to degrees
break;
2022-01-16 16:33:15 +01:00
}
closedir(dirp);
2022-06-17 23:38:33 +02:00
ffStrbufDestroy(&valueString);
2022-01-16 16:33:15 +01:00
2022-06-17 23:38:33 +02:00
if(value->value != value->value)
2022-01-16 16:33:15 +01:00
return false;
ffStrbufAppendS(dir, "name");
2022-06-20 16:35:15 +02:00
ffReadFileBuffer(dir->chars, &value->name);
2022-01-16 16:33:15 +01:00
ffStrbufSubstrBefore(dir, dirLength);
ffStrbufAppendS(dir, "device/class");
2022-06-20 16:35:15 +02:00
ffReadFileBuffer(dir->chars, &value->deviceClass);
2022-01-16 16:33:15 +01:00
ffStrbufSubstrBefore(dir, dirLength);
return value->name.length > 0 || value->deviceClass.length > 0;
2022-01-16 16:33:15 +01:00
}
2022-09-01 17:14:50 +02:00
const FFTempsResult* ffDetectTemps()
2022-01-16 16:33:15 +01:00
{
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)
{
2022-09-18 16:30:26 +02:00
ffStrbufDestroy(&baseDir);
2022-01-16 16:33:15 +01:00
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->deviceClass);
2022-01-16 16:33:15 +01:00
if(!parseHwmonDir(&baseDir, temp))
{
ffStrbufDestroy(&temp->name);
ffStrbufDestroy(&temp->deviceClass);
2022-01-16 16:33:15 +01:00
--result.values.length;
}
ffStrbufSubstrBefore(&baseDir, baseDirLength);
}
2022-09-18 16:30:26 +02:00
closedir(dirp);
ffStrbufDestroy(&baseDir);
2022-01-16 16:33:15 +01:00
pthread_mutex_unlock(&mutex);
return &result;
}