X-Git-Url: https://git.xinqibao.xyz/slstatus.git/blobdiff_plain/e3c9a6f92313b9e2ac1b409895b70fd09f6c774d..cceeec0efada64579525b90b30e5597cd24dfc6c:/slstatus.c diff --git a/slstatus.c b/slstatus.c index 46e13c9..90b4808 100644 --- a/slstatus.c +++ b/slstatus.c @@ -1,6 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include #include #include #include @@ -18,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -25,12 +25,7 @@ #include #include -#undef strlcat -#undef strlcpy - -#include "extern/arg.h" -#include "extern/strlcat.h" -#include "extern/concat.h" +#include "arg.h" struct arg { char *(*func)(); @@ -52,6 +47,7 @@ static char *gid(void); static char *hostname(void); static char *ip(const char *iface); static char *kernel_release(void); +static char *keyboard_indicators(void); static char *load_avg(void); static char *ram_free(void); static char *ram_perc(void); @@ -70,10 +66,9 @@ static char *vol_perc(const char *card); static char *wifi_perc(const char *iface); static char *wifi_essid(const char *iface); static void sighandler(const int signo); -static void usage(int); +static void usage(const int eval); char *argv0; -char concat[]; static unsigned short int delay = 0; static unsigned short int done; static unsigned short int dflag, oflag; @@ -108,12 +103,13 @@ static char * battery_perc(const char *bat) { int perc; + char path[PATH_MAX]; FILE *fp; - ccat(3, "/sys/class/power_supply/", bat, "/capacity"); - fp = fopen(concat, "r"); + snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/capacity"); + fp = fopen(path, "r"); if (fp == NULL) { - warn("Failed to open file %s", concat); + warn("Failed to open file %s", path); return smprintf("%s", UNKNOWN_STR); } fscanf(fp, "%i", &perc); @@ -125,13 +121,14 @@ battery_perc(const char *bat) static char * battery_state(const char *bat) { + char path[PATH_MAX]; char state[12]; FILE *fp; - ccat(3, "/sys/class/power_supply/", bat, "/status"); - fp = fopen(concat, "r"); + snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/status"); + fp = fopen(path, "r"); if (fp == NULL) { - warn("Failed to open file %s", concat); + warn("Failed to open file %s", path); return smprintf("%s", UNKNOWN_STR); } fscanf(fp, "%12s", state); @@ -143,6 +140,8 @@ battery_state(const char *bat) return smprintf("-"); } else if (strcmp(state, "Full") == 0) { return smprintf("="); + } else if (strcmp(state, "Unknown") == 0) { + return smprintf("/"); } else { return smprintf("?"); } @@ -326,6 +325,29 @@ kernel_release(void) return smprintf("%s", udata.release); } +static char * +keyboard_indicators(void) +{ + Display *dpy = XOpenDisplay(NULL); + XKeyboardState state; + XGetKeyboardControl(dpy, &state); + XCloseDisplay(dpy); + + switch (state.led_mask) { + case 1: + return smprintf("c"); + break; + case 2: + return smprintf("n"); + break; + case 3: + return smprintf("cn"); + break; + default: + return smprintf(""); + } +} + static char * load_avg(void) { @@ -427,7 +449,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'; @@ -642,41 +664,28 @@ 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; - - 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); + unsigned int i; + int v, afd, devmask; + char *vnames[] = SOUND_DEVICE_NAMES; - 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); + 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); + } + } + } - snd_mixer_selem_id_free(s_elem); - snd_mixer_close(handle); + close(afd); - if (!mute) - 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 * @@ -685,13 +694,14 @@ wifi_perc(const char *iface) int perc; char buf[255]; char *datastart; + char path[PATH_MAX]; char status[5]; FILE *fp; - ccat(3, "/sys/class/net/", iface, "/operstate"); - fp = fopen(concat, "r"); + snprintf(path, sizeof(path), "%s%s%s", "/sys/class/net/", iface, "/operstate"); + fp = fopen(path, "r"); if (fp == NULL) { - warn("Failed to open file %s", concat); + warn("Failed to open file %s", path); return smprintf("%s", UNKNOWN_STR); } fgets(status, 5, fp); @@ -705,13 +715,13 @@ wifi_perc(const char *iface) warn("Failed to open file /proc/net/wireless"); return smprintf("%s", UNKNOWN_STR); } - ccat(2, iface, ":"); + fgets(buf, sizeof(buf), fp); fgets(buf, sizeof(buf), fp); fgets(buf, sizeof(buf), fp); fclose(fp); - if ((datastart = strstr(buf, concat)) == NULL) { + if ((datastart = strstr(buf, iface)) == NULL) { return smprintf("%s", UNKNOWN_STR); } datastart = (datastart+(strlen(iface)+1)); @@ -758,7 +768,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); @@ -781,7 +791,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); @@ -822,9 +832,7 @@ 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)) { - warnx("Output too long"); - } + strncat(status_string, element, sizeof(status_string) - strlen(status_string) - 1); free(res); free(element); }