|
| 1 | +#include "fastfetch.h" |
| 2 | +#include "common/io.h" |
| 3 | +#include "battery.h" |
| 4 | + |
| 5 | +#include <dirent.h> |
| 6 | + |
| 7 | +static void parseBattery(FFstrbuf* dir, FFlist* results) |
| 8 | +{ |
| 9 | + uint32_t dirLength = dir->length; |
| 10 | + |
| 11 | + FFstrbuf testBatteryBuffer; |
| 12 | + ffStrbufInit(&testBatteryBuffer); |
| 13 | + |
| 14 | + //type must exist and be "Battery" |
| 15 | + ffStrbufAppendS(dir, "/type"); |
| 16 | + ffReadFileBuffer(dir->chars, &testBatteryBuffer); |
| 17 | + ffStrbufSubstrBefore(dir, dirLength); |
| 18 | + |
| 19 | + if(ffStrbufIgnCaseCompS(&testBatteryBuffer, "Battery") != 0) |
| 20 | + { |
| 21 | + ffStrbufDestroy(&testBatteryBuffer); |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + //scope may not exist or must not be "Device" |
| 26 | + ffStrbufAppendS(dir, "/scope"); |
| 27 | + ffReadFileBuffer(dir->chars, &testBatteryBuffer); |
| 28 | + ffStrbufSubstrBefore(dir, dirLength); |
| 29 | + |
| 30 | + if(ffStrbufIgnCaseCompS(&testBatteryBuffer, "Device") == 0) |
| 31 | + { |
| 32 | + ffStrbufDestroy(&testBatteryBuffer); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + ffStrbufDestroy(&testBatteryBuffer); |
| 37 | + BatteryResult* result = ffListAdd(results); |
| 38 | + |
| 39 | + //capacity must exist and be not empty |
| 40 | + ffStrbufInit(&result->capacity); |
| 41 | + ffStrbufAppendS(dir, "/capacity"); |
| 42 | + ffReadFileBuffer(dir->chars, &result->capacity); |
| 43 | + ffStrbufSubstrBefore(dir, dirLength); |
| 44 | + |
| 45 | + if(result->capacity.length == 0) |
| 46 | + { |
| 47 | + ffStrbufDestroy(&result->capacity); |
| 48 | + --results->length; |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + //At this point, we have a battery. Try to get as much values as possible. |
| 53 | + |
| 54 | + ffStrbufInit(&result->manufacturer); |
| 55 | + ffStrbufAppendS(dir, "/manufacturer"); |
| 56 | + ffReadFileBuffer(dir->chars, &result->manufacturer); |
| 57 | + ffStrbufSubstrBefore(dir, dirLength); |
| 58 | + |
| 59 | + ffStrbufInit(&result->modelName); |
| 60 | + ffStrbufAppendS(dir, "/model_name"); |
| 61 | + ffReadFileBuffer(dir->chars, &result->modelName); |
| 62 | + ffStrbufSubstrBefore(dir, dirLength); |
| 63 | + |
| 64 | + ffStrbufInit(&result->technology); |
| 65 | + ffStrbufAppendS(dir, "/technology"); |
| 66 | + ffReadFileBuffer(dir->chars, &result->technology); |
| 67 | + ffStrbufSubstrBefore(dir, dirLength); |
| 68 | + |
| 69 | + ffStrbufInit(&result->status); |
| 70 | + ffStrbufAppendS(dir, "/status"); |
| 71 | + ffReadFileBuffer(dir->chars, &result->status); |
| 72 | + ffStrbufSubstrBefore(dir, dirLength); |
| 73 | +} |
| 74 | + |
| 75 | +const char* ffDetectBatteryImpl(FFinstance* instance, FFlist* results) { |
| 76 | + FFstrbuf baseDir; |
| 77 | + ffStrbufInitA(&baseDir, 64); |
| 78 | + if(instance->config.batteryDir.length > 0) |
| 79 | + { |
| 80 | + ffStrbufAppend(&baseDir, &instance->config.batteryDir); |
| 81 | + if (!ffStrbufEndsWithC(&baseDir, '/')) |
| 82 | + { |
| 83 | + ffStrbufAppendC(&baseDir, '/'); |
| 84 | + } |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + ffStrbufAppendS(&baseDir, "/sys/class/power_supply/"); |
| 89 | + } |
| 90 | + |
| 91 | + uint32_t baseDirLength = baseDir.length; |
| 92 | + |
| 93 | + DIR* dirp = opendir(baseDir.chars); |
| 94 | + if(dirp == NULL) |
| 95 | + { |
| 96 | + ffStrbufDestroy(&baseDir); |
| 97 | + return "opendir(batteryDir) == NULL"; |
| 98 | + } |
| 99 | + |
| 100 | + struct dirent* entry; |
| 101 | + while((entry = readdir(dirp)) != NULL) |
| 102 | + { |
| 103 | + ffStrbufAppendS(&baseDir, entry->d_name); |
| 104 | + parseBattery(&baseDir, results); |
| 105 | + ffStrbufSubstrBefore(&baseDir, baseDirLength); |
| 106 | + } |
| 107 | + |
| 108 | + closedir(dirp); |
| 109 | + |
| 110 | + if(results->length == 0) { |
| 111 | + ffStrbufDestroy(&baseDir); |
| 112 | + return "batteryDir doesn't contain any battery folder"; |
| 113 | + } |
| 114 | + |
| 115 | + ffStrbufDestroy(&baseDir); |
| 116 | + return NULL; |
| 117 | +} |
0 commit comments