Skip to content

Commit f8eabdd

Browse files
committed
Battry (Linux): try detecting cycle count and temperature
1 parent 5ab2c34 commit f8eabdd

File tree

5 files changed

+36
-32
lines changed

5 files changed

+36
-32
lines changed

src/detection/battery/battery_linux.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
#include "fastfetch.h"
2-
#include "common/io/io.h"
31
#include "battery.h"
2+
#include "common/io/io.h"
3+
#include "detection/temps/temps_linux.h"
44
#include "util/stringUtils.h"
55

66
#include <dirent.h>
77

8-
static void parseBattery(FFstrbuf* dir, FFlist* results)
8+
static void parseBattery(FFstrbuf* dir, const char* id, FFBatteryOptions* options, FFlist* results)
99
{
1010
uint32_t dirLength = dir->length;
1111

@@ -65,7 +65,26 @@ static void parseBattery(FFstrbuf* dir, FFlist* results)
6565
ffReadFileBuffer(dir->chars, &result->status);
6666
ffStrbufSubstrBefore(dir, dirLength);
6767

68+
ffStrbufAppendS(dir, "/cycle_count");
69+
ffReadFileBuffer(dir->chars, &testBatteryBuffer);
70+
ffStrbufSubstrBefore(dir, dirLength);
71+
if(dir->length)
72+
result->cycleCount = (int32_t) ffStrbufToUInt(&testBatteryBuffer, 0);
73+
6874
result->temperature = FF_BATTERY_TEMP_UNSET;
75+
if (options->temp)
76+
{
77+
const FFlist* tempsResult = ffDetectTemps();
78+
79+
FF_LIST_FOR_EACH(FFTempValue, value, *tempsResult)
80+
{
81+
if (ffStrbufEqualS(&value->name, id))
82+
{
83+
result->temperature = value->value;
84+
break;
85+
}
86+
}
87+
}
6988
}
7089

7190
const char* ffDetectBattery(FFBatteryOptions* options, FFlist* results)
@@ -84,7 +103,7 @@ const char* ffDetectBattery(FFBatteryOptions* options, FFlist* results)
84103

85104
uint32_t baseDirLength = baseDir.length;
86105

87-
DIR* dirp = opendir(baseDir.chars);
106+
FF_AUTO_CLOSE_DIR DIR* dirp = opendir(baseDir.chars);
88107
if(dirp == NULL)
89108
return "opendir(batteryDir) == NULL";
90109

@@ -95,12 +114,10 @@ const char* ffDetectBattery(FFBatteryOptions* options, FFlist* results)
95114
continue;
96115

97116
ffStrbufAppendS(&baseDir, entry->d_name);
98-
parseBattery(&baseDir, results);
117+
parseBattery(&baseDir, entry->d_name, options, results);
99118
ffStrbufSubstrBefore(&baseDir, baseDirLength);
100119
}
101120

102-
closedir(dirp);
103-
104121
if(results->length == 0)
105122
return "batteryDir doesn't contain any battery folder";
106123

src/detection/cpu/cpu_linux.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,10 @@ static double getFrequency(const char* info, const char* scaling)
8383

8484
static double detectCPUTemp(void)
8585
{
86-
const FFTempsResult* temps = ffDetectTemps();
86+
const FFlist* tempsResult = ffDetectTemps();
8787

88-
for(uint32_t i = 0; i < temps->values.length; i++)
88+
FF_LIST_FOR_EACH(FFTempValue, value, *tempsResult)
8989
{
90-
FFTempValue* value = ffListGet(&temps->values, i);
91-
9290
if(
9391
ffStrbufFirstIndexS(&value->name, "cpu") < value->name.length ||
9492
ffStrbufCompS(&value->name, "k10temp") == 0 ||

src/detection/gpu/gpu_linux.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,10 @@ static void pciDetectDriverName(FFGPUResult* gpu, PCIData* pci, struct pci_dev*
138138

139139
FF_MAYBE_UNUSED static void pciDetectTemp(FFGPUResult* gpu, struct pci_dev* device)
140140
{
141-
const FFTempsResult* tempsResult = ffDetectTemps();
141+
const FFlist* tempsResult = ffDetectTemps();
142142

143-
for(uint32_t i = 0; i < tempsResult->values.length; i++)
143+
FF_LIST_FOR_EACH(FFTempValue, tempValue, *tempsResult)
144144
{
145-
FFTempValue* tempValue = ffListGet(&tempsResult->values, i);
146-
147145
//The kernel exposes the device class multiplied by 256 for some reason
148146
if(tempValue->deviceClass == device->device_class * 256)
149147
{

src/detection/temps/temps_linux.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,21 @@ static bool parseHwmonDir(FFstrbuf* dir, FFTempValue* value)
4141
return value->name.length > 0 || value->deviceClass > 0;
4242
}
4343

44-
const FFTempsResult* ffDetectTemps(void)
44+
const FFlist* ffDetectTemps(void)
4545
{
46-
static FFTempsResult result;
47-
static bool init = false;
46+
static FFlist result;
4847

49-
if(init)
48+
if(result.elementSize > 0)
5049
return &result;
51-
init = true;
5250

53-
ffListInitA(&result.values, sizeof(FFTempValue), 16);
51+
ffListInitA(&result, sizeof(FFTempValue), 16);
5452

5553
FF_STRBUF_AUTO_DESTROY baseDir = ffStrbufCreateA(64);
5654
ffStrbufAppendS(&baseDir, "/sys/class/hwmon/");
5755

5856
uint32_t baseDirLength = baseDir.length;
5957

60-
DIR* dirp = opendir(baseDir.chars);
58+
FF_AUTO_CLOSE_DIR DIR* dirp = opendir(baseDir.chars);
6159
if(dirp == NULL)
6260
return &result;
6361

@@ -70,19 +68,17 @@ const FFTempsResult* ffDetectTemps(void)
7068
ffStrbufAppendS(&baseDir, entry->d_name);
7169
ffStrbufAppendC(&baseDir, '/');
7270

73-
FFTempValue* temp = ffListAdd(&result.values);
71+
FFTempValue* temp = ffListAdd(&result);
7472
ffStrbufInit(&temp->name);
7573
temp->deviceClass = 0;
7674
if(!parseHwmonDir(&baseDir, temp))
7775
{
7876
ffStrbufDestroy(&temp->name);
79-
--result.values.length;
77+
--result.length;
8078
}
8179

8280
ffStrbufSubstrBefore(&baseDir, baseDirLength);
8381
}
8482

85-
closedir(dirp);
86-
8783
return &result;
8884
}

src/detection/temps/temps_linux.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ typedef struct FFTempValue
1212
double value;
1313
} FFTempValue;
1414

15-
typedef struct FFTempsResult
16-
{
17-
FFlist values; //List of FFTempValue
18-
} FFTempsResult;
19-
20-
const FFTempsResult* ffDetectTemps();
15+
const FFlist* /* List of FFTempValue */ ffDetectTemps();
2116

2217
#endif

0 commit comments

Comments
 (0)