Xinqi Bao's Git

battery_state() function added
[slstatus.git] / slstatus.c
index 53de9cb..1d46674 100644 (file)
@@ -1,12 +1,14 @@
 /* See LICENSE file for copyright and license details. */
 
 #include <alsa/asoundlib.h>
+#include <err.h>
 #include <fcntl.h>
 #include <ifaddrs.h>
 #include <limits.h>
 #include <linux/wireless.h>
 #include <netdb.h>
 #include <pwd.h>
+#include <signal.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -38,6 +40,7 @@ struct arg {
 
 static char *smprintf(const char *, ...);
 static char *battery_perc(const char *);
+static char *battery_state(const char *);
 static char *cpu_perc(void);
 static char *datetime(const char *);
 static char *disk_free(const char *);
@@ -61,8 +64,11 @@ static char *username(void);
 static char *vol_perc(const char *);
 static char *wifi_perc(const char *);
 static char *wifi_essid(const char *);
+static void sighandler(const int);
 
+static unsigned short int delay;
 static Display *dpy;
+static int done = 0;
 
 #include "config.h"
 
@@ -93,38 +99,50 @@ smprintf(const char *fmt, ...)
 static char *
 battery_perc(const char *battery)
 {
-       int now, full, perc;
+       int perc;
        FILE *fp;
 
-       ccat(4, BATTERY_PATH, battery, "/", BATTERY_NOW);
-
+       ccat(3, "/sys/class/power_supply/", battery, "/capacity");
        fp = fopen(concat, "r");
        if (fp == NULL) {
-               fprintf(stderr, "Error opening battery file: %s: %s\n",
-                                               concat,
-                                               strerror(errno));
+               warn("Error opening battery file: %s", concat);
                return smprintf(UNKNOWN_STR);
        }
-
-       fscanf(fp, "%i", &now);
+       fscanf(fp, "%i", &perc);
        fclose(fp);
 
-       ccat(4, BATTERY_PATH, battery, "/", BATTERY_FULL);
+       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) {
-               fprintf(stderr, "Error opening battery file: %s: %s\n",
-                                               concat,
-                                               strerror(errno));
+               warn("Error opening battery file: %s", concat);
                return smprintf(UNKNOWN_STR);
        }
-
-       fscanf(fp, "%i", &full);
+       fscanf(fp, "%s", state);
        fclose(fp);
 
-       perc = now / (full / 100);
-
-       return smprintf("%d%%", perc);
+       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 *
@@ -135,26 +153,26 @@ cpu_perc(void)
        FILE *fp = fopen("/proc/stat","r");
 
        if (fp == NULL) {
-               fprintf(stderr, "Error opening stat file: %s\n",
-                                               strerror(errno));
+               warn("Error opening stat file");
                return smprintf(UNKNOWN_STR);
        }
 
        fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
        fclose(fp);
 
-       sleep(1);
+       delay = (UPDATE_INTERVAL - (UPDATE_INTERVAL - 1));
+       sleep(delay);
 
        fp = fopen("/proc/stat","r");
        if (fp == NULL) {
-               fprintf(stderr, "Error opening stat file: %s\n",
-                                               strerror(errno));
+               warn("Error opening stat file");
                return smprintf(UNKNOWN_STR);
        }
 
        fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]);
        fclose(fp);
        perc = 100 * ((b[0]+b[1]+b[2]) - (a[0]+a[1]+a[2])) / ((b[0]+b[1]+b[2]+b[3]) - (a[0]+a[1]+a[2]+a[3]));
+
        return smprintf("%d%%", perc);
 }
 
@@ -177,10 +195,10 @@ disk_free(const char *mountpoint)
        struct statvfs fs;
 
        if (statvfs(mountpoint, &fs) < 0) {
-               fprintf(stderr, "Could not get filesystem info: %s\n",
-                                               strerror(errno));
+               warn("Could not get filesystem info");
                return smprintf(UNKNOWN_STR);
        }
+
        return smprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024);
 }
 
@@ -191,12 +209,12 @@ disk_perc(const char *mountpoint)
        struct statvfs fs;
 
        if (statvfs(mountpoint, &fs) < 0) {
-               fprintf(stderr, "Could not get filesystem info: %s\n",
-                                               strerror(errno));
+               warn("Could not get filesystem info");
                return smprintf(UNKNOWN_STR);
        }
 
        perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks));
+
        return smprintf("%d%%", perc);
 }
 
@@ -206,8 +224,7 @@ disk_total(const char *mountpoint)
        struct statvfs fs;
 
        if (statvfs(mountpoint, &fs) < 0) {
-               fprintf(stderr, "Could not get filesystem info: %s\n",
-                                               strerror(errno));
+               warn("Could not get filesystem info");
                return smprintf(UNKNOWN_STR);
        }
 
@@ -220,8 +237,7 @@ disk_used(const char *mountpoint)
        struct statvfs fs;
 
        if (statvfs(mountpoint, &fs) < 0) {
-               fprintf(stderr, "Could not get filesystem info: %s\n",
-                                               strerror(errno));
+               warn("Could not get filesystem info");
                return smprintf(UNKNOWN_STR);
        }
 
@@ -235,13 +251,13 @@ entropy(void)
        FILE *fp = fopen("/proc/sys/kernel/random/entropy_avail", "r");
 
        if (fp == NULL) {
-               fprintf(stderr, "Could not open entropy file: %s\n",
-                                               strerror(errno));
+               warn("Could not open entropy file");
                return smprintf(UNKNOWN_STR);
        }
 
        fscanf(fp, "%d", &entropy);
        fclose(fp);
+
        return smprintf("%d", entropy);
 }
 
@@ -258,8 +274,7 @@ hostname(void)
        FILE *fp = fopen("/proc/sys/kernel/hostname", "r");
 
        if (fp == NULL) {
-               fprintf(stderr, "Could not open hostname file: %s\n",
-                                               strerror(errno));
+               warn("Could not open hostname file");
                return smprintf(UNKNOWN_STR);
        }
 
@@ -268,6 +283,7 @@ hostname(void)
        memset(&hostname[strlen(hostname)-1], '\0',
                sizeof(hostname) - strlen(hostname));
        fclose(fp);
+
        return smprintf("%s", hostname);
 }
 
@@ -279,12 +295,10 @@ ip(const char *interface)
        char host[NI_MAXHOST];
 
        if (getifaddrs(&ifaddr) == -1) {
-               fprintf(stderr, "Error getting IP address: %s\n",
-                                               strerror(errno));
+               warn("Error getting IP address");
                return smprintf(UNKNOWN_STR);
        }
 
-       /* get the ip address */
        for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
                if (ifa->ifa_addr == NULL)
                        continue;
@@ -294,14 +308,13 @@ ip(const char *interface)
 
                if ((strcmp(ifa->ifa_name, interface) == 0) && (ifa->ifa_addr->sa_family == AF_INET)) {
                        if (s != 0) {
-                               fprintf(stderr, "Error getting IP address.\n");
+                               warnx("Error getting IP address.");
                                return smprintf(UNKNOWN_STR);
                        }
                        return smprintf("%s", host);
                }
        }
 
-       /* free the address */
        freeifaddrs(ifaddr);
 
        return smprintf(UNKNOWN_STR);
@@ -313,7 +326,7 @@ load_avg(void)
        double avgs[3];
 
        if (getloadavg(avgs, 3) < 0) {
-               fprintf(stderr, "Error getting load avg.\n");
+               warnx("Error getting load avg.");
                return smprintf(UNKNOWN_STR);
        }
 
@@ -327,13 +340,13 @@ ram_free(void)
        FILE *fp = fopen("/proc/meminfo", "r");
 
        if (fp == NULL) {
-               fprintf(stderr, "Error opening meminfo file: %s\n",
-                                               strerror(errno));
+               warn("Error opening meminfo file");
                return smprintf(UNKNOWN_STR);
        }
 
        fscanf(fp, "MemFree: %ld kB\n", &free);
        fclose(fp);
+
        return smprintf("%f", (float)free / 1024 / 1024);
 }
 
@@ -345,8 +358,7 @@ ram_perc(void)
        FILE *fp = fopen("/proc/meminfo", "r");
 
        if (fp == NULL) {
-               fprintf(stderr, "Error opening meminfo file: %s\n",
-                                               strerror(errno));
+               warn("Error opening meminfo file");
                return smprintf(UNKNOWN_STR);
        }
 
@@ -357,6 +369,7 @@ ram_perc(void)
 
        fclose(fp);
        perc = 100 * ((total - free) - (buffers + cached)) / total;
+
        return smprintf("%d%%", perc);
 }
 
@@ -367,13 +380,13 @@ ram_total(void)
        FILE *fp = fopen("/proc/meminfo", "r");
 
        if (fp == NULL) {
-               fprintf(stderr, "Error opening meminfo file: %s\n",
-                                               strerror(errno));
+               warn("Error opening meminfo file");
                return smprintf(UNKNOWN_STR);
        }
 
        fscanf(fp, "MemTotal: %ld kB\n", &total);
        fclose(fp);
+
        return smprintf("%f", (float)total / 1024 / 1024);
 }
 
@@ -384,8 +397,7 @@ ram_used(void)
        FILE *fp = fopen("/proc/meminfo", "r");
 
        if (fp == NULL) {
-               fprintf(stderr, "Error opening meminfo file: %s\n",
-                                               strerror(errno));
+               warn("Error opening meminfo file");
                return smprintf(UNKNOWN_STR);
        }
 
@@ -396,6 +408,7 @@ ram_used(void)
 
        fclose(fp);
        used = total - free - buffers - cached;
+
        return smprintf("%f", (float)used / 1024 / 1024);
 }
 
@@ -404,11 +417,10 @@ run_command(const char* command)
 {
        int good;
        FILE *fp = popen(command, "r");
-       char buffer[64];
+       char buffer[64] = "";
 
        if (fp == NULL) {
-               fprintf(stderr, "Could not get command output for: %s: %s\n",
-                                               command, strerror(errno));
+               warn("Could not get command output for: %s", command);
                return smprintf(UNKNOWN_STR);
        }
 
@@ -422,6 +434,7 @@ run_command(const char* command)
        }
        if (good)
                buffer[strlen(buffer)-1] = '\0';
+
        return smprintf("%s", buffer);
 }
 
@@ -432,13 +445,13 @@ temp(const char *file)
        FILE *fp = fopen(file, "r");
 
        if (fp == NULL) {
-               fprintf(stderr, "Could not open temperature file: %s\n",
-                                                       strerror(errno));
+               warn("Could not open temperature file");
                return smprintf(UNKNOWN_STR);
        }
 
        fscanf(fp, "%d", &temperature);
        fclose(fp);
+
        return smprintf("%d°C", temperature / 1000);
 }
 
@@ -462,12 +475,12 @@ username(void)
        uid_t uid = geteuid();
        struct passwd *pw = getpwuid(uid);
 
-       if (pw == NULL)
-               return smprintf("%s", pw->pw_name);
+       if (pw == NULL) {
+               warn("Could not get username");
+               return smprintf(UNKNOWN_STR);
+       }
 
-       fprintf(stderr, "Could not get username: %s\n",
-                                       strerror(errno));
-       return smprintf(UNKNOWN_STR);
+       return smprintf("%s", pw->pw_name);
 }
 
 static char *
@@ -477,26 +490,26 @@ uid(void)
 }
 
 
-static char * 
-vol_perc(const char *snd_card)
-{ /* thanks to botika for this function */
+static char *
+vol_perc(const char *soundcard)
+{
        long int vol, max, min;
        snd_mixer_t *handle;
        snd_mixer_elem_t *elem;
        snd_mixer_selem_id_t *s_elem;
 
        snd_mixer_open(&handle, 0);
-       snd_mixer_attach(handle, "default");
+       snd_mixer_attach(handle, soundcard);
        snd_mixer_selem_register(handle, NULL, NULL);
        snd_mixer_load(handle);
        snd_mixer_selem_id_malloc(&s_elem);
-       snd_mixer_selem_id_set_name(s_elem, snd_card);
+       snd_mixer_selem_id_set_name(s_elem, "Master");
        elem = snd_mixer_find_selem(handle, s_elem);
 
        if (elem == NULL) {
                snd_mixer_selem_id_free(s_elem);
                snd_mixer_close(handle);
-               perror("alsa error");
+               warn("Failed to get volume percentage for: %s.", soundcard);
                return smprintf(UNKNOWN_STR);
        }
 
@@ -507,7 +520,7 @@ vol_perc(const char *snd_card)
        snd_mixer_selem_id_free(s_elem);
        snd_mixer_close(handle);
 
-       return smprintf("%d", (vol * 100) / max);
+       return smprintf("%d%%", ((uint_fast16_t)(vol * 100) / max));
 }
 
 static char *
@@ -519,13 +532,12 @@ wifi_perc(const char *wificard)
        char status[5];
        FILE *fp;
 
-       ccat(3, "/sys/class/net", wificard, "/operstate");
+       ccat(3, "/sys/class/net/", wificard, "/operstate");
 
        fp = fopen(concat, "r");
 
-       if(fp == NULL) {
-               fprintf(stderr, "Error opening wifi operstate file: %s\n",
-                                                       strerror(errno));
+       if (fp == NULL) {
+               warn("Error opening wifi operstate file");
                return smprintf(UNKNOWN_STR);
        }
 
@@ -536,8 +548,7 @@ wifi_perc(const char *wificard)
 
        fp = fopen("/proc/net/wireless", "r");
        if (fp == NULL) {
-               fprintf(stderr, "Error opening wireless file: %s\n",
-                                               strerror(errno));
+               warn("Error opening wireless file");
                return smprintf(UNKNOWN_STR);
        }
 
@@ -553,6 +564,7 @@ wifi_perc(const char *wificard)
        }
 
        fclose(fp);
+
        return smprintf("%d%%", strength);
 }
 
@@ -566,36 +578,52 @@ wifi_essid(const char *wificard)
        memset(&wreq, 0, sizeof(struct iwreq));
        wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
        sprintf(wreq.ifr_name, wificard);
-       if(sockfd == -1) {
-               fprintf(stderr, "Cannot open socket for interface: %s: %s\n",
-                                               wificard, strerror(errno));
+       if (sockfd == -1) {
+               warn("Cannot open socket for interface: %s", wificard);
                return smprintf(UNKNOWN_STR);
        }
        wreq.u.essid.pointer = id;
        if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
-               fprintf(stderr, "Get ESSID ioctl failed for interface %s: %s\n",
-                                               wificard, strerror(errno));
+               warn("Get ESSID ioctl failed for interface %s", wificard);
                return smprintf(UNKNOWN_STR);
        }
 
+       close(sockfd);
+
        if (strcmp((char *)wreq.u.essid.pointer, "") == 0)
                return smprintf(UNKNOWN_STR);
        else
                return smprintf("%s", (char *)wreq.u.essid.pointer);
 }
 
+static void
+sighandler(const int signo)
+{
+       if (signo == SIGTERM || signo == SIGINT) {
+               done = 1;
+       }
+}
+
 int
 main(void)
 {
        size_t i;
        char status_string[4096];
-       char *res, *element;
+       char *res, *element, *status_old;
        struct arg argument;
+       struct sigaction act;
+
+       memset(&act, 0, sizeof(act));
+       act.sa_handler = sighandler;
+       sigaction(SIGINT,  &act, 0);
+       sigaction(SIGTERM, &act, 0);
 
        dpy = XOpenDisplay(NULL);
 
-       for (;;) {
-               memset(status_string, 0, sizeof(status_string));
+       XFetchName(dpy, DefaultRootWindow(dpy), &status_old);
+
+       while (!done) {
+               status_string[0] = '\0';
                for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) {
                        argument = args[i];
                        if (argument.args == NULL)
@@ -605,7 +633,7 @@ main(void)
                        element = smprintf(argument.format, res);
                        if (element == NULL) {
                                element = smprintf(UNKNOWN_STR);
-                               fprintf(stderr, "Failed to format output.\n");
+                               warnx("Failed to format output.");
                        }
                        strlcat(status_string, element, sizeof(status_string));
                        free(res);
@@ -613,7 +641,17 @@ main(void)
                }
                XStoreName(dpy, DefaultRootWindow(dpy), status_string);
                XSync(dpy, False);
+               /*
+                * subtract delay time spend in function
+                * calls from the actual global delay time
+                */
+               sleep(UPDATE_INTERVAL - delay);
+               delay = 0;
        }
+
+       XStoreName(dpy, DefaultRootWindow(dpy), status_old);
+       XSync(dpy, False);
+
        XCloseDisplay(dpy);
 
        return 0;