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);
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"
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 *
}
}
+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)
{
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 *
perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks));
- return smprintf("%d%%", perc);
+ return smprintf("%d", perc);
}
static char *
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 *
}
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 *
fscanf(fp, "%d", &temp);
fclose(fp);
- return smprintf("%d°C", temp / 1000);
+ return smprintf("%d", temp / 1000);
}
static char *
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);
}
}
}
close(afd);
- return smprintf("%d%%", v & 0xff);
+ return smprintf("%d", v & 0xff);
}
static char *
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 *
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);
}
case 'o':
oflag = 1;
break;
+ case 'n':
+ nflag = 1;
+ break;
case 'v':
printf("slstatus (C) 2016-2017 slstatus engineers\n");
return 0;
usage(1);
} ARGEND
- if (dflag && oflag) {
+ if ((dflag && oflag) || (dflag && nflag) || (oflag && nflag)) {
usage(1);
}
if (dflag && daemon(1, 1) < 0) {
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) {