#include "battery.h" #include #include #include #include #include #include #include #define VALUE_SIZE 16 #define BATTERY_PATH "/sys/class/power_supply/BAT1/" struct battery { int status; int capacity; time_t last_updated; char value[VALUE_SIZE]; }; static int open_battery_file(const char * name) { char path[128]; snprintf(path, 128, "%s%s", BATTERY_PATH, name); return open(path, O_RDONLY); } static int battery_value(int file) { const size_t buf_size = 4; int val = 0; char buf[buf_size]; memset(&buf, '\0', buf_size); lseek(file, 0, SEEK_SET); read(file, &buf, sizeof(char) * buf_size); sscanf(buf, "%d", &val); return val; } static bool battery_status_discharging(int file) { const size_t buf_size = 16; char buf[buf_size]; memset(&buf, '\0', buf_size); lseek(file, 0, SEEK_SET); read(file, &buf, sizeof(char) * buf_size); return strncmp(buf, "Discharging\n", buf_size) == 0; } struct battery * battery_new() { struct battery * b = malloc(sizeof(struct battery)); b->last_updated = 0; b->capacity = open_battery_file("capacity"); b->status = open_battery_file("status"); return b; } void battery_free(struct battery * b) { close(b->status); close(b->capacity); free(b); } void battery_update(struct battery * b) { int cap = battery_value(b->capacity); bool discharge = battery_status_discharging(b->status); snprintf( b->value, VALUE_SIZE, "%s% 4d%%", discharge ? "\uf241" : "\uf0e7", cap); } const char * battery_get_val(struct battery * b) { return b->value; } bool battery_should_update(struct battery * b, time_t now, unsigned short limit) { double time_diff = difftime(now, b->last_updated); if(time_diff >= limit) { b->last_updated = now; return true; } return false; }