|
1 | 1 | #include "fastfetch.h" |
2 | 2 | #include "battery.h" |
3 | 3 | #include "common/io/io.h" |
4 | | -#include "util/stringUtils.h" |
5 | 4 |
|
6 | | -void parseBattery(FFstrbuf* content, FFlist* results) |
| 5 | +#include <private/device/power_managment.h> |
| 6 | +#include <sys/ioctl.h> |
| 7 | +#include <fcntl.h> |
| 8 | + |
| 9 | +const char* parseBattery(int dfd, const char* battId, FFlist* results) |
7 | 10 | { |
8 | | - char* line = NULL; |
9 | | - size_t n = 0; |
10 | | - while (ffStrbufGetline(&line, &n, content)) |
11 | | - { |
| 11 | + FF_AUTO_CLOSE_FD int fd = openat(dfd, battId, O_RDWR); |
| 12 | + if (fd < 0) return "openat() failed"; |
12 | 13 |
|
13 | | - } |
| 14 | + acpi_battery_info basic = {}; |
| 15 | + if (ioctl(fd, GET_BATTERY_INFO, &basic, sizeof(basic)) != 0) |
| 16 | + return "ioctl(GET_BATTERY_INFO) failed"; |
| 17 | + acpi_extended_battery_info extended = {}; |
| 18 | + if (ioctl(fd, GET_EXTENDED_BATTERY_INFO, &extended, sizeof(extended)) != 0) |
| 19 | + return "ioctl(GET_EXTENDED_BATTERY_INFO) failed"; |
| 20 | + |
| 21 | + FFBatteryResult* battery = (FFBatteryResult*)ffListAdd(results); |
| 22 | + ffStrbufInitS(&battery->modelName, extended.model_number); |
| 23 | + ffStrbufInitS(&battery->manufacturer, extended.oem_info); |
| 24 | + ffStrbufInit(&battery->manufactureDate); |
| 25 | + ffStrbufInitS(&battery->technology, extended.type); // extended.technology? |
| 26 | + ffStrbufInit(&battery->status); |
| 27 | + ffStrbufInitS(&battery->serial, extended.serial_number); |
| 28 | + battery->temperature = FF_BATTERY_TEMP_UNSET; |
| 29 | + battery->cycleCount = extended.cycles; |
| 30 | + battery->timeRemaining = -1; |
| 31 | + battery->capacity = (double) basic.capacity / (double) extended.last_full_charge; |
| 32 | + |
| 33 | + if (basic.state & BATTERY_DISCHARGING) |
| 34 | + ffStrbufAppendS(&battery->status, "Discharging, "); |
| 35 | + if (basic.state & BATTERY_CHARGING) |
| 36 | + ffStrbufAppendS(&battery->status, "Charging, "); |
| 37 | + if (basic.state & BATTERY_CRITICAL_STATE) |
| 38 | + ffStrbufAppendS(&battery->status, "Critical, "); |
| 39 | + if (basic.state & BATTERY_NOT_CHARGING) |
| 40 | + ffStrbufAppendS(&battery->status, "AC Connected, "); |
| 41 | + ffStrbufTrimRight(&battery->status, ' '); |
| 42 | + ffStrbufTrimRight(&battery->status, ','); |
| 43 | + |
| 44 | + return NULL; |
14 | 45 | } |
15 | 46 |
|
16 | 47 | const char* ffDetectBattery(FFBatteryOptions* options, FFlist* results) |
17 | 48 | { |
18 | 49 | FF_AUTO_CLOSE_DIR DIR* dir = opendir("/dev/power/acpi_battery/"); |
19 | 50 | if (!dir) return "opendir(/dev/power/acpi_battery) failed"; |
20 | 51 |
|
21 | | - FF_STRBUF_AUTO_DESTROY content = ffStrbufCreate(); |
22 | 52 | struct dirent* entry; |
23 | 53 | while ((entry = readdir(dir))) |
24 | 54 | { |
25 | 55 | if (entry->d_name[0] == '.') continue; |
26 | | - if (!ffReadFileBufferRelative(dirfd(dir), entry->d_name, &content)) continue; |
27 | | - parseBattery(&content, results); |
| 56 | + parseBattery(dirfd(dir), entry->d_name, results); |
28 | 57 | } |
29 | 58 |
|
30 | | - return "To be supported"; |
| 59 | + return NULL; |
31 | 60 | } |
0 commit comments