X-Git-Url: https://git.xinqibao.xyz/slstatus.git/blobdiff_plain/015f832c3a52a7c49c0181d85ab39eec7b8489a0..abbaa7af4401bbece8c9a9cb27408a0c84421762:/slstatus.c diff --git a/slstatus.c b/slstatus.c index 46e13c9..cb4798b 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)(); @@ -40,7 +35,9 @@ struct arg { static char *smprintf(const char *fmt, ...); static char *battery_perc(const char *bat); +static char *battery_power(const char *bat); static char *battery_state(const char *bat); +static char *cpu_freq(void); static char *cpu_perc(void); static char *datetime(const char *fmt); static char *disk_free(const char *mnt); @@ -52,6 +49,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,13 +68,12 @@ 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; +static unsigned short int dflag, oflag, nflag; static Display *dpy; #include "config.h" @@ -108,30 +105,51 @@ 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); fclose(fp); - return smprintf("%d%%", perc); + return smprintf("%d", perc); +} + +static char * +battery_power(const char *bat) +{ + char path[PATH_MAX]; + FILE *fp; + int watts; + + snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/power_now"); + fp = fopen(path, "r"); + if (fp == NULL) { + warn("Failed to open file %s", path); + return smprintf("%s", UNKNOWN_STR); + } + fscanf(fp, "%i", &watts); + fclose(fp); + + return smprintf("%d", (watts + 500000) / 1000000); } 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,11 +161,30 @@ 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("?"); } } +static char * +cpu_freq(void) +{ + int freq; + FILE *fp; + + fp = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq", "r"); + if (fp == NULL) { + warn("Failed to open file /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"); + return smprintf("%s", UNKNOWN_STR); + } + fscanf(fp, "%i", &freq); + fclose(fp); + + return smprintf("%d", (freq + 500) / 1000); +} + static char * cpu_perc(void) { @@ -175,7 +212,7 @@ cpu_perc(void) 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); + return smprintf("%d", perc); } static char * @@ -218,7 +255,7 @@ disk_perc(const char *mnt) perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks)); - return smprintf("%d%%", perc); + return smprintf("%d", perc); } static char * @@ -326,6 +363,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) { @@ -373,7 +433,7 @@ ram_perc(void) fscanf(fp, "Cached: %ld kB\n", &cached); fclose(fp); - return smprintf("%d%%", 100 * ((total - free) - (buffers + cached)) / total); + return smprintf("%d", 100 * ((total - free) - (buffers + cached)) / total); } static char * @@ -427,9 +487,9 @@ run_command(const char *cmd) } fgets(buf, sizeof(buf), fp); pclose(fp); - buf[strlen(buf)] = '\0'; + buf[sizeof(buf) - 1] = '\0'; - if ((nlptr = strstr(buf, "\n")) != NULL) { + if ((nlptr = strrchr(buf, '\n')) != NULL) { nlptr[0] = '\0'; } @@ -451,7 +511,7 @@ swap_free(void) return smprintf("%s", UNKNOWN_STR); } - if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) { + if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) { warn("swap_free: read error"); fclose(fp); return smprintf("%s", UNKNOWN_STR); @@ -488,7 +548,7 @@ swap_perc(void) return smprintf("%s", UNKNOWN_STR); } - if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) { + if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) { warn("swap_perc: read error"); fclose(fp); return smprintf("%s", UNKNOWN_STR); @@ -512,7 +572,7 @@ swap_perc(void) } sscanf(match, "SwapFree: %ld kB\n", &free); - return smprintf("%d%%", 100 * (total - free - cached) / total); + return smprintf("%d", 100 * (total - free - cached) / total); } static char * @@ -529,7 +589,7 @@ swap_total(void) warn("Failed to open file /proc/meminfo"); return smprintf("%s", UNKNOWN_STR); } - if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) { + if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) { warn("swap_total: read error"); fclose(fp); return smprintf("%s", UNKNOWN_STR); @@ -560,7 +620,7 @@ swap_used(void) warn("Failed to open file /proc/meminfo"); return smprintf("%s", UNKNOWN_STR); } - if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) { + if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) { warn("swap_used: read error"); fclose(fp); return smprintf("%s", UNKNOWN_STR); @@ -601,7 +661,7 @@ temp(const char *file) fscanf(fp, "%d", &temp); fclose(fp); - return smprintf("%d°C", temp / 1000); + return smprintf("%d", temp / 1000); } static char * @@ -621,8 +681,7 @@ uptime(void) static char * username(void) { - uid_t uid = geteuid(); - struct passwd *pw = getpwuid(uid); + struct passwd *pw = getpwuid(geteuid()); if (pw == NULL) { warn("Failed to get username"); @@ -642,41 +701,34 @@ 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); + afd = open(card, O_RDONLY | O_NONBLOCK); + if (afd == -1) { + warn("Cannot open %s", card); + return smprintf(UNKNOWN_STR); + } - if (elem == NULL) { - snd_mixer_selem_id_free(s_elem); - snd_mixer_close(handle); - warn("Failed to get volume percentage for %s", card); + 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) && !strcmp("vol", vnames[i])) { + if (ioctl(afd, MIXER_READ(i), &v) == -1) { + warn("vol_perc: ioctl"); + close(afd); + return smprintf("%s", 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); + 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 +737,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,19 +758,19 @@ 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)); sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &perc); - return smprintf("%d%%", perc); + return smprintf("%d", perc); } static char * @@ -758,9 +811,9 @@ sighandler(const int signo) } static void -usage(int eval) +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); } @@ -780,8 +833,11 @@ main(int argc, char *argv[]) case 'o': oflag = 1; break; + case 'n': + nflag = 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); @@ -789,7 +845,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) { @@ -822,18 +878,19 @@ 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); } - 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) {