Xinqi Bao's Git

happy new year!
[slstatus.git] / slstatus.c
index 1f08514..155cf2e 100644 (file)
@@ -1,6 +1,5 @@
 /* See LICENSE file for copyright and license details. */
 
-#include <alsa/asoundlib.h>
 #include <err.h>
 #include <fcntl.h>
 #include <ifaddrs.h>
@@ -18,6 +17,7 @@
 #include <sys/stat.h>
 #include <sys/statvfs.h>
 #include <sys/socket.h>
+#include <sys/soundcard.h>
 #include <sys/sysinfo.h>
 #include <sys/types.h>
 #include <sys/utsname.h>
@@ -26,7 +26,6 @@
 #include <X11/Xlib.h>
 
 #undef strlcat
-#undef strlcpy
 
 #include "extern/arg.h"
 #include "extern/strlcat.h"
@@ -51,6 +50,7 @@ static char *entropy(void);
 static char *gid(void);
 static char *hostname(void);
 static char *ip(const char *iface);
+static char *kernel_release(void);
 static char *load_avg(void);
 static char *ram_free(void);
 static char *ram_perc(void);
@@ -68,9 +68,8 @@ static char *username(void);
 static char *vol_perc(const char *card);
 static char *wifi_perc(const char *iface);
 static char *wifi_essid(const char *iface);
-static char *kernel_release(void);
 static void sighandler(const int signo);
-static void usage(int);
+static void usage(const int eval);
 
 char *argv0;
 char concat[];
@@ -314,6 +313,18 @@ ip(const char *iface)
        return smprintf("%s", UNKNOWN_STR);
 }
 
+static char *
+kernel_release(void)
+{
+       struct utsname udata;
+
+       if (uname(&udata) < 0) {
+               return smprintf(UNKNOWN_STR);
+       }
+
+       return smprintf("%s", udata.release);
+}
+
 static char *
 load_avg(void)
 {
@@ -415,7 +426,7 @@ run_command(const char *cmd)
        }
        fgets(buf, sizeof(buf), fp);
        pclose(fp);
-       buf[strlen(buf)] = '\0';
+       buf[sizeof(buf)] = '\0';
 
        if ((nlptr = strstr(buf, "\n")) != NULL) {
                nlptr[0] = '\0';
@@ -630,41 +641,30 @@ uid(void)
 static char *
 vol_perc(const char *card)
 {
-       int mute;
-       long int vol, max, min;
-       snd_mixer_t *handle;
-       snd_mixer_elem_t *elem;
-       snd_mixer_selem_id_t *s_elem;
+       unsigned int i;
+       int v, afd, devmask;
+       char *vnames[] = SOUND_DEVICE_NAMES;
 
-       snd_mixer_open(&handle, 0);
-       snd_mixer_attach(handle, card);
-       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, "Master");
-       elem = snd_mixer_find_selem(handle, s_elem);
-
-       if (elem == NULL) {
-               snd_mixer_selem_id_free(s_elem);
-               snd_mixer_close(handle);
-               warn("Failed to get volume percentage for %s", card);
-               return smprintf("%s", UNKNOWN_STR);
+       afd = open(card, O_RDONLY);
+       if (afd < 0) {
+               warn("Cannot open %s", card);
+               return smprintf(UNKNOWN_STR);
        }
 
-       snd_mixer_handle_events(handle);
-       snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
-       snd_mixer_selem_get_playback_volume(elem, 0, &vol);
-       snd_mixer_selem_get_playback_switch(elem, 0, &mute);
-
-       snd_mixer_selem_id_free(s_elem);
-       snd_mixer_close(handle);
+       ioctl(afd, MIXER_READ(SOUND_MIXER_DEVMASK), &devmask);
+       for (i = 0; i < (sizeof(vnames) / sizeof((vnames[0]))); i++) {
+               if (devmask & (1 << i)) {
+                       if (!strcmp("vol", vnames[i])) {
+                               ioctl(afd, MIXER_READ(i), &v);
+                       }
+               }
+       }
 
-       if (!mute)
+       close(afd);
+       if (v == 0) {
                return smprintf("mute");
-       else if (max == 0)
-               return smprintf("0%%");
-       else
-               return smprintf("%lu%%", ((uint_fast16_t)(vol * 100) / max));
+       }
+       return smprintf("%d%%", v & 0xff);
 }
 
 static char *
@@ -737,16 +737,6 @@ wifi_essid(const char *iface)
                return smprintf("%s", (char *)wreq.u.essid.pointer);
 }
 
-static char *
-kernel_release(void)
-{
-       struct utsname udata;
-       if (uname(&udata) < 0)
-               return smprintf("%s", UNKNOWN_STR);
-
-       return smprintf("%s", udata.release);
-}
-
 static void
 sighandler(const int signo)
 {
@@ -756,7 +746,7 @@ sighandler(const int signo)
 }
 
 static void
-usage(int eval)
+usage(const int eval)
 {
        fprintf(stderr, "usage: %s [-d] [-o] [-v] [-h]\n", argv0);
        exit(eval);
@@ -779,7 +769,7 @@ main(int argc, char *argv[])
                        oflag = 1;
                        break;
                case 'v':
-                       printf("slstatus %s (C) 2016 slstatus engineers\n", VERSION);
+                       printf("slstatus (C) 2016-2017 slstatus engineers\n");
                        return 0;
                case 'h':
                        usage(0);
@@ -820,8 +810,9 @@ main(int argc, char *argv[])
                                element = smprintf("%s", UNKNOWN_STR);
                                warnx("Failed to format output");
                        }
-                       if (strlcat(status_string, element, sizeof(status_string)) >= sizeof(status_string))
+                       if (strlcat(status_string, element, sizeof(status_string)) >= sizeof(status_string)) {
                                warnx("Output too long");
+                       }
                        free(res);
                        free(element);
                }