X-Git-Url: https://git.xinqibao.xyz/slstatus.git/blobdiff_plain/c288663ebdeb78ef50a6378651bb14380ed4b20a..870d68d44e33f9fb7a5402becbc0b5c363066bcc:/slstatus.c diff --git a/slstatus.c b/slstatus.c index f06c373..8241451 100644 --- a/slstatus.c +++ b/slstatus.c @@ -71,7 +71,7 @@ static void usage(const int eval); char *argv0; static unsigned short int delay = 0; static unsigned short int done; -static unsigned short int dflag, oflag; +static unsigned short int dflag, oflag, nflag; static Display *dpy; #include "config.h" @@ -667,17 +667,23 @@ vol_perc(const char *card) int v, afd, devmask; char *vnames[] = SOUND_DEVICE_NAMES; - afd = open(card, O_RDONLY); - if (afd < 0) { + afd = open(card, O_RDONLY | O_NONBLOCK); + if (afd == -1) { warn("Cannot open %s", card); return smprintf(UNKNOWN_STR); } - ioctl(afd, MIXER_READ(SOUND_MIXER_DEVMASK), &devmask); + if (ioctl(afd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1) { + warn("Cannot get volume for %s", card); + close(afd); + return smprintf("%s", UNKNOWN_STR); + } 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 (devmask & (1 << i) && !strcmp("vol", vnames[i])) { + if (ioctl(afd, MIXER_READ(i), &v) == -1) { + warn("vol_perc: ioctl"); + close(afd); + return smprintf("%s", UNKNOWN_STR); } } } @@ -769,7 +775,7 @@ sighandler(const int signo) static void usage(const int eval) { - fprintf(stderr, "usage: %s [-d] [-o] [-v] [-h]\n", argv0); + fprintf(stderr, "usage: %s [-d] [-o] [-n] [-v] [-h]\n", argv0); exit(eval); } @@ -789,6 +795,9 @@ main(int argc, char *argv[]) case 'o': oflag = 1; break; + case 'n': + nflag = 1; + break; case 'v': printf("slstatus (C) 2016-2017 slstatus engineers\n"); return 0; @@ -798,7 +807,7 @@ main(int argc, char *argv[]) usage(1); } ARGEND - if (dflag && oflag) { + if ((dflag && oflag) || (dflag && nflag) || (oflag && nflag)) { usage(1); } if (dflag && daemon(1, 1) < 0) { @@ -836,11 +845,14 @@ main(int argc, char *argv[]) free(element); } - if (!oflag) { + if (oflag) { + printf("%s\n", status_string); + } else if (nflag) { + printf("%s\n", status_string); + done = 1; + } else { XStoreName(dpy, DefaultRootWindow(dpy), status_string); XSync(dpy, False); - } else { - printf("%s\n", status_string); } if ((UPDATE_INTERVAL - delay) <= 0) {