battery_state() function added

This commit is contained in:
Aaron Marcher 2016-09-13 22:03:36 +02:00 committed by Aaron Marcher (drkhsh)
parent 7aad78bd95
commit f13104156f
3 changed files with 34 additions and 1 deletions

View File

@ -8,7 +8,7 @@ Looking at the LOC (lines of code) of the [Conky project](https://github.com/brn
The following information is included: The following information is included:
- Battery percentage - Battery percentage/state
- CPU usage (in percent) - CPU usage (in percent)
- Custom shell commands - Custom shell commands
- Date and time - Date and time

View File

@ -8,6 +8,7 @@
/* statusbar /* statusbar
- battery_perc (battery percentage) [argument: battery name] - battery_perc (battery percentage) [argument: battery name]
- battery_state (battery charging state) [argument: battery name]
- cpu_perc (cpu usage in percent) [argument: NULL] - cpu_perc (cpu usage in percent) [argument: NULL]
- datetime (date and time) [argument: format] - datetime (date and time) [argument: format]
- disk_free (disk usage in percent) [argument: mountpoint] - disk_free (disk usage in percent) [argument: mountpoint]

View File

@ -40,6 +40,7 @@ struct arg {
static char *smprintf(const char *, ...); static char *smprintf(const char *, ...);
static char *battery_perc(const char *); static char *battery_perc(const char *);
static char *battery_state(const char *);
static char *cpu_perc(void); static char *cpu_perc(void);
static char *datetime(const char *); static char *datetime(const char *);
static char *disk_free(const char *); static char *disk_free(const char *);
@ -113,6 +114,37 @@ battery_perc(const char *battery)
return smprintf("%d%%", perc); return smprintf("%d%%", perc);
} }
static char *
battery_state(const char *battery)
{
char *state = malloc(sizeof(char)*12);
FILE *fp;
if (!state) {
warn("Failed to get battery state.");
return smprintf(UNKNOWN_STR);
}
ccat(3, "/sys/class/power_supply/", battery, "/status");
fp = fopen(concat, "r");
if (fp == NULL) {
warn("Error opening battery file: %s", concat);
return smprintf(UNKNOWN_STR);
}
fscanf(fp, "%s", state);
fclose(fp);
if (strcmp(state, "Charging") == 0)
return smprintf("+");
else if (strcmp(state, "Discharging") == 0)
return smprintf("-");
else if (strcmp(state, "Full") == 0)
return smprintf("=");
else
return smprintf("?");
}
static char * static char *
cpu_perc(void) cpu_perc(void)
{ {