4 Commits

Author SHA1 Message Date
Sravan Balaji
4dc68f322f Full Battery Fix
- Add mapping for battery full
- Output remaining time if discharging, plug icon otherwise
2020-08-07 23:42:05 -04:00
Sravan Balaji
59be5d4cd5 Icon Changes & AC Power
- Remove dividers from inside battery_all_in_one function
- When there is no battery, show plug with AC
- Change charging status symbol to plug
- Move charging status in front of battery icon
- Change divider back to "|"
2020-08-07 23:05:55 -04:00
Sravan Balaji
764813c0c1 No Battery Fix
- Check if battery information directory exists before displaying info
- Fix divider spacing in battery-all-in-one
2020-08-07 00:05:13 -04:00
Sravan Balaji
1d22eea138 Battery All-In-One Function
- Condense separate battery functions into one that handles icons & dividers
- Only shows battery information if it exists
2020-08-06 23:44:03 -04:00
3 changed files with 81 additions and 4 deletions

View File

@@ -1,6 +1,9 @@
/* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include "../util.h"
@@ -52,6 +55,7 @@
} map[] = {
{ "Charging", "+" },
{ "Discharging", "-" },
{ "Full", "=" },
};
size_t i;
char path[PATH_MAX], state[12];
@@ -115,6 +119,79 @@
return "";
}
const char *
battery_all_in_one(const char *bat)
{
char path[PATH_MAX];
if (esnprintf(path, sizeof(path),
"/sys/class/power_supply/%s", bat) < 0) {
return " AC";
}
DIR* dir = opendir(path);
if (dir) {
// Battery exists
closedir(dir);
}
else if (ENOENT == errno) {
// Battery doesn't exist
return " AC";
}
int percentage = atoi(battery_perc(bat));
char *bat_icon = "";
char *bat_status = "";
char *bat_rem = "";
// Add battery percentage icon
if (percentage < 20) {
bat_icon = "";
}
else if (percentage < 30) {
bat_icon = "";
}
else if (percentage < 40) {
bat_icon = "";
}
else if (percentage < 50) {
bat_icon = "";
}
else if (percentage < 60) {
bat_icon = "";
}
else if (percentage < 70) {
bat_icon = "";
}
else if (percentage < 80) {
bat_icon = "";
}
else if (percentage < 90) {
bat_icon = "";
}
else if (percentage < 100) {
bat_icon = "";
}
else if (percentage == 100) {
bat_icon = "";
}
if (!strcmp(battery_state(bat), "-")) {
bat_status = "";
const char *temp = battery_remaining(bat);
bat_rem = (char*) malloc(sizeof(temp) + 2);
sprintf(bat_rem, " (%s)", temp);
}
else {
bat_status = "";
bat_rem = "";
}
return bprintf(" %s%s %i%%%s ", bat_status, bat_icon, percentage, bat_rem);
}
#elif defined(__OpenBSD__)
#include <fcntl.h>
#include <machine/apmvar.h>

View File

@@ -10,7 +10,7 @@ static const char unknown_str[] = "?";
#define MAXLEN 2048
static const char battery_name[] = "BAT0";
static const char divider_symbol[] = "";
static const char divider_symbol[] = "|";
/*
* function description argument (example)
@@ -21,6 +21,7 @@ static const char divider_symbol[] = "";
* NULL on OpenBSD/FreeBSD
* battery_remaining battery remaining HH:MM battery name (BAT0)
* NULL on OpenBSD/FreeBSD
* battery_all_in_one all-in-one battery status battery name (BAT0)
* check_updates num available updates NULL
* cpu_perc cpu usage in percent NULL
* cpu_freq cpu frequency in MHz NULL
@@ -77,9 +78,7 @@ static const struct arg args[] = {
{ divider, " %s ", divider_symbol },
{ disk_perc, " %s%%", "/home" },
{ divider, " %s ", divider_symbol },
{ battery_state, "%s", battery_name }, // TODO: Make the battery icon change with the level and charging status
{ battery_perc, "%s%%", battery_name },
{ battery_remaining, " (%s)", battery_name },
{ battery_all_in_one, "%s", battery_name, },
{ divider, " %s ", divider_symbol },
{ datetime, " %s", "%a %x" },
{ divider, " %s ", divider_symbol },

View File

@@ -4,6 +4,7 @@
const char *battery_perc(const char *);
const char *battery_state(const char *);
const char *battery_remaining(const char *);
const char *battery_all_in_one(const char *bat);
/* check updates */
const char *check_updates(void);