Xinqi Bao's Git

added a tool for resetting the status bar && worked around some issues && removed...
[slstatus.git] / slstatus.c
index a9aeecb..dae5e8a 100644 (file)
@@ -1,12 +1,10 @@
 /* See LICENSE file for copyright and license details. */
 
 #include <alsa/asoundlib.h>
-#include <arpa/inet.h>
 #include <fcntl.h>
 #include <ifaddrs.h>
 #include <limits.h>
 #include <linux/wireless.h>
-#include <locale.h>
 #include <netdb.h>
 #include <pwd.h>
 #include <stdarg.h>
 #include "strlcat.h"
 #include "strlcpy.h"
 
-typedef char *(*op_fun)();
 struct arg {
-       op_fun func;
+       char *(*func)();
        const char *format;
        const char *args;
 };
 
-static void setstatus(const char *);
 static char *smprintf(const char *, ...);
 static char *battery_perc(const char *);
 static char *cpu_perc(void);
@@ -67,14 +63,6 @@ static Display *dpy;
 
 #include "config.h"
 
-static void
-setstatus(const char *str)
-{
-       /* set WM_NAME via X11 */
-       XStoreName(dpy, DefaultRootWindow(dpy), str);
-       XSync(dpy, False);
-}
-
 static char *
 smprintf(const char *fmt, ...)
 {
@@ -159,7 +147,6 @@ cpu_perc(void)
        fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
        fclose(fp);
 
-       /* wait a second (for avg values) */
        sleep(1);
 
        fp = fopen("/proc/stat","r");
@@ -265,8 +252,7 @@ entropy(void)
 static char *
 gid(void)
 {
-       gid_t gid = getgid();
-       return smprintf("%d", gid);
+       return smprintf("%d", getgid());
 }
 
 static char *
@@ -477,45 +463,31 @@ uptime(void)
 static char *
 username(void)
 {
-       register struct passwd *pw;
-       register uid_t uid;
-
-       uid = geteuid();
-       pw = getpwuid(uid);
+       uid_t uid = geteuid();
+       struct passwd *pw = getpwuid(uid);
 
-       if (pw)
+       if (pw == NULL)
                return smprintf("%s", pw->pw_name);
-       else {
-               fprintf(stderr, "Could not get username: %s\n",
-                                       strerror(errno));
-               return smprintf(UNKNOWN_STR);
-       }
 
+       fprintf(stderr, "Could not get username: %s\n",
+                                       strerror(errno));
        return smprintf(UNKNOWN_STR);
 }
 
 static char *
 uid(void)
 {
-       /* FIXME: WHY USE register modifier? */
-       register uid_t uid;
-
-       uid = geteuid();
-
-       if (uid)
-               return smprintf("%d", uid);
-       else {
-               fprintf(stderr, "Could not get uid.\n");
-               return smprintf(UNKNOWN_STR);
-       }
-
-       return smprintf(UNKNOWN_STR);
+       return smprintf("%d", geteuid());
 }
 
 
 static char *
 vol_perc(const char *soundcard)
 {
+       /*
+        * TODO: FIXME: 
+        * https://github.com/drkh5h/slstatus/issues/12
+        */
        int mute = 0;
        long vol = 0, max = 0, min = 0;
        snd_mixer_t *handle;
@@ -642,34 +614,29 @@ main(void)
        char *res, *element;
        struct arg argument;
 
-       dpy = XOpenDisplay(0x0);
-       if (!dpy) {
-               fprintf(stderr, "Cannot open display!\n");
-               exit(1);
-       }
-
-       for (;;) {
-               memset(status_string, 0, sizeof(status_string));
-               for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) {
-                       argument = args[i];
-                       if (argument.args == NULL)
-                               res = argument.func();
-                       else
-                               res = argument.func(argument.args);
-                       element = smprintf(argument.format, res);
-                       if (element == NULL) {
-                               element = smprintf(UNKNOWN_STR);
-                               fprintf(stderr, "Failed to format output.\n");
-                       }
-                       strlcat(status_string, element, sizeof(status_string));
-                       free(res);
-                       free(element);
+       stderr = stderr;
+       dpy = XOpenDisplay(NULL);
+
+       memset(status_string, 0, sizeof(status_string));
+       for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) {
+               argument = args[i];
+               if (argument.args == NULL)
+                       res = argument.func();
+               else
+                       res = argument.func(argument.args);
+               element = smprintf(argument.format, res);
+               if (element == NULL) {
+                       element = smprintf(UNKNOWN_STR);
+                       fprintf(stderr, "Failed to format output.\n");
                }
-
-               setstatus(status_string);
-               sleep(UPDATE_INTERVAL -1);
+               strlcat(status_string, element, sizeof(status_string));
+               free(res);
+               free(element);
        }
 
+       XStoreName(dpy, DefaultRootWindow(dpy), status_string);
+       XSync(dpy, False);
        XCloseDisplay(dpy);
+
        return 0;
 }