~andreafeletto/cstatus

969aaa1122786d647b37013137da4f5e874e6b37 — Andrea Feletto a month ago be19393
refactor battery config
2 files changed, 14 insertions(+), 9 deletions(-)

M battery.c
M battery.h
M battery.c => battery.c +7 -7
@@ 13,27 13,27 @@ battery(char *buf) {
	char status[16], *icon;
	int capacity;

	if ((fp = fopen(BATCAP, "r")) == NULL) {
		fprintf(stderr, "cannot open file '%s'\n", BATCAP);
	if ((fp = fopen(BAT_VALUE, "r")) == NULL) {
		fprintf(stderr, "cannot open file '%s'\n", BAT_VALUE);
	}
	if (fscanf(fp, "%d", &capacity) != 1) {
		fputs("cannot read battery capacity\n", stderr);
	}
	fclose(fp);

	if ((fp = fopen(BATSTAT, "r")) == NULL) {
		fprintf(stderr, "cannot open file '%s'\n", BATSTAT);
	if ((fp = fopen(BAT_STATUS, "r")) == NULL) {
		fprintf(stderr, "cannot open file '%s'\n", BAT_STATUS);
	}
	if (fscanf(fp, "%s", &status[0]) != 1) {
		fputs("cannot read battery status\n", stderr);
	}
	fclose(fp);

	if (strcmp(status, "Discharging") == 0) {
	if (strcmp(status, BAT_STATUS_DISCHARGING) == 0) {
		icon = "\U0001f50b";
	} else if (strcmp(status, "Charging") == 0) {
	} else if (strcmp(status, BAT_STATUS_CHARGING) == 0) {
		icon = "\U0001f50c";
	} else if (strcmp(status, "Full") == 0) {
	} else if (strcmp(status, BAT_STATUS_FULL) == 0) {
		icon = "\u26a1";
	} else {
		icon = "\u2753";

M battery.h => battery.h +7 -2
@@ 3,10 3,15 @@
#define CSTATUS_BATTERY_H

/* path of file containing battery charge percentage */
#define BATCAP   "/sys/class/power_supply/BAT1/capacity"
#define BAT_VALUE   "/sys/class/power_supply/BAT1/capacity"

/* path of file containing battery status */
#define BATSTAT  "/sys/class/power_supply/BAT1/status"
#define BAT_STATUS  "/sys/class/power_supply/BAT1/status"

/* possible contents of file BAT_STATUS */
#define BAT_STATUS_DISCHARGING "Discharging"
#define BAT_STATUS_CHARGING "Charging"
#define BAT_STATUS_FULL "Full"

int battery(char *buf);