Files
fastfetch/src/battery.c
T

62 lines
1.8 KiB
C
Raw Normal View History

2021-02-18 22:25:36 +01:00
#include "fastfetch.h"
void ffPrintBattery(FFstate* state)
{
2021-02-18 23:04:31 +01:00
int scanned;
2021-02-18 22:25:36 +01:00
FILE* fullFile = fopen("/sys/class/power_supply/BAT0/charge_full", "r");
if(fullFile == NULL)
2021-02-19 16:23:41 +01:00
{
if(state->showErrors)
ffPrintError(state, "Battery", "fopen(\"/sys/class/power_supply/BAT0/charge_full\", \"r\") == NULL");
2021-02-18 22:25:36 +01:00
return;
2021-02-19 16:23:41 +01:00
}
2021-02-18 22:25:36 +01:00
uint32_t full;
2021-02-19 10:07:00 +01:00
scanned = fscanf(fullFile, "%u", &full);
2021-02-18 23:04:31 +01:00
if(scanned != 1)
2021-02-19 16:23:41 +01:00
{
if(state->showErrors)
ffPrintError(state, "Battery", "fscanf(fullFile, \"%u\", &full) != 1");
2021-02-18 23:04:31 +01:00
return;
2021-02-19 16:23:41 +01:00
}
2021-02-18 22:25:36 +01:00
fclose(fullFile);
2021-02-19 16:23:41 +01:00
FILE* nowFile = fopen("/sys/class/power_supply/BAT0/charge_now", "r");
2021-02-18 22:25:36 +01:00
if(nowFile == NULL)
2021-02-19 16:23:41 +01:00
{
if(state->showErrors)
ffPrintError(state, "Battery", "fopen(\"/sys/class/power_supply/BAT0/charge_now\", \"r\") == NULL");
2021-02-18 22:25:36 +01:00
return;
2021-02-19 16:23:41 +01:00
}
2021-02-18 22:25:36 +01:00
uint32_t now;
2021-02-19 10:07:00 +01:00
scanned = fscanf(nowFile, "%u", &now);
2021-02-18 23:04:31 +01:00
if(scanned != 1)
2021-02-19 16:23:41 +01:00
{
if(state->showErrors)
ffPrintError(state, "Battery", "fscanf(nowFile, \"%u\", &now) != 1");
2021-02-18 23:04:31 +01:00
return;
2021-02-19 16:23:41 +01:00
}
2021-02-18 22:25:36 +01:00
fclose(nowFile);
FILE* statusFile = fopen("/sys/class/power_supply/BAT0/status", "r");
if(statusFile == NULL)
2021-02-19 16:23:41 +01:00
{
if(state->showErrors)
ffPrintError(state, "Battery", "fopen(\"/sys/class/power_supply/BAT0/status\", \"r\") == NULL");
2021-02-18 22:25:36 +01:00
return;
2021-02-19 16:23:41 +01:00
}
2021-02-18 22:25:36 +01:00
char status[256];
2021-02-18 23:04:31 +01:00
scanned = fscanf(statusFile, "%s", status);
if(scanned != 1)
2021-02-19 16:23:41 +01:00
{
if(state->showErrors)
ffPrintError(state, "Battery", "fscanf(statusFile, \"%u\", &status) != 1");
2021-02-18 23:04:31 +01:00
return;
2021-02-19 16:23:41 +01:00
}
2021-02-18 22:25:36 +01:00
fclose(statusFile);
uint32_t percentage = (now / (double) full) * 100;
ffPrintLogoAndKey(state, "Battery");
2021-02-19 10:07:00 +01:00
printf("%u%% [%s]\n", percentage, status);
2021-02-18 22:25:36 +01:00
}