* dwmstatus for OpenBSD
       
   URI git clone git://git.codevoid.de/dwmstatus-sdk
   DIR Log
   DIR Files
   DIR Refs
   DIR LICENSE
       ---
   DIR commit 9cd3f8860642d0feb9210335c46262947e48cee7
   DIR parent 3c50b451da564723f8a919954082293f741d3ea5
   URI Author: Stefan Hagen <sh+git[at]codevoid[dot]de>
       Date:   Mon,  4 Nov 2019 18:26:37 +0100
       
       limited linux support
       
       Diffstat:
         M Makefile                            |       2 +-
         M dwmstatus.c                         |     137 +++++++++++++++++--------------
       
       2 files changed, 77 insertions(+), 62 deletions(-)
       ---
   DIR diff --git a/Makefile b/Makefile
       @@ -4,7 +4,7 @@ include config.mk
        
        SRC = ${NAME}.c
        OBJ = ${SRC:.c=.o}
       -CC  = clang
       +CC  = cc
        
        all: options ${NAME}
        
   DIR diff --git a/dwmstatus.c b/dwmstatus.c
       @@ -9,17 +9,23 @@
        #include <string.h>
        #include <sys/time.h>
        #include <sys/sysctl.h>
       -#include <sys/sensors.h>
        #include <sys/ioctl.h>
       -#include <sys/audioio.h>
        #include <sys/types.h>
        #include <sys/socket.h>
        #include <ifaddrs.h>
       -#include <machine/apmvar.h>
        #include <fcntl.h>
        #include <X11/Xlib.h>
        #include <sys/param.h>
        #include <sys/mount.h>
       +#ifdef __linux__
       +#include <sys/statfs.h>
       +#include <time.h>
       +#else
       +#include <sys/sensors.h>
       +#include <sys/audioio.h>
       +#include <machine/apmvar.h>
       +
       +#endif
        
        // configuration
        #define UPDATE_INTERVAL 10
       @@ -29,10 +35,10 @@
        #define BTOMB(x) ((x)*1048576
        #define BTOGB(x) ((x)*1073741824
        
       -static Display *dpy;
       -char *tzlocal = "Europe/Berlin";
       +    static Display *dpy;
       +    char *tzlocal = "Europe/Berlin";
        
       -char * smprintf(char *fmt, ...) {
       +    char * smprintf(char *fmt, ...) {
            va_list fmtargs;
            char *ret;
            int len;
       @@ -43,8 +49,8 @@ char * smprintf(char *fmt, ...) {
        
            ret = malloc(++len);
            if (ret == NULL) {
       -        perror("malloc");
       -        exit(1);
       +    perror("malloc");
       +    exit(1);
            }
        
            va_start(fmtargs, fmt);
       @@ -52,7 +58,7 @@ char * smprintf(char *fmt, ...) {
            va_end(fmtargs);
        
            return ret;
       -}
       +    }
        
        char * getfreespace(const char *path) {
            struct statfs vfs;
       @@ -60,6 +66,7 @@ char * getfreespace(const char *path) {
            return smprintf("%ld", (vfs.f_bavail * vfs.f_bsize) / 1024 / 1024);
        }
        
       +#ifdef __OpenBSD__
        char * getcputemp() {
            struct sensor sensor;
            size_t slen = sizeof(sensor);
       @@ -68,7 +75,9 @@ char * getcputemp() {
                return smprintf("%.0f", (sensor.value  - 273150000) / 1000000.0);
            return smprintf("err");
        }
       +#endif
        
       +#ifdef __OpenBSD__
        char * getfan() {
            struct sensor sensor;
            size_t slen = sizeof(sensor);
       @@ -77,47 +86,49 @@ char * getfan() {
                return smprintf("%i", sensor.value);
            return smprintf("err");
        }
       +#endif
        
       +#ifdef __OpenBSD__
        char * getvolume() {
       +    static int                        cls = -1, fd, v;
       +    static struct mixer_devinfo        mdi;
       +    static struct mixer_ctrl        mc;
       +
       +    if ((fd = open("/dev/mixer", O_RDONLY)) == -1)
       +        return smprintf("NOMIXER");
       +
       +    for (mdi.index = 0; cls == -1; ++mdi.index) {
       +        if (ioctl(fd, AUDIO_MIXER_DEVINFO, &mdi) == -1)
       +            goto fail;
       +        if (mdi.type == AUDIO_MIXER_CLASS &&
       +                strcmp(mdi.label.name, AudioCoutputs) == 0)
       +            cls = mdi.index;
       +    }
       +    for (mdi.index = 0, v = -1; v == -1; ++mdi.index) {
       +        if (ioctl(fd, AUDIO_MIXER_DEVINFO, &mdi) == -1)
       +            goto fail;
       +        if (mdi.type == AUDIO_MIXER_VALUE &&
       +                mdi.prev == AUDIO_MIXER_LAST &&
       +                mdi.mixer_class == cls &&
       +                strcmp(mdi.label.name, AudioNmaster) == 0) {
       +            mc.dev = mdi.index;
       +            if (ioctl(fd, AUDIO_MIXER_READ, &mc) == -1)
       +                goto fail;
       +            v = MAX(mc.un.value.level[AUDIO_MIXER_LEVEL_MONO],
       +                    mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT]);
       +        }
       +    }
       +
       +    if (v == -1 || close(fd) == -1)
       +        return smprintf("NOCHAN");
        
       -        static int                        cls = -1, fd, v;
       -        static struct mixer_devinfo        mdi;
       -        static struct mixer_ctrl        mc;
       -
       -        if ((fd = open("/dev/mixer", O_RDONLY)) == -1)
       -            return smprintf("NOMIXER");
       -
       -        for (mdi.index = 0; cls == -1; ++mdi.index) {
       -                if (ioctl(fd, AUDIO_MIXER_DEVINFO, &mdi) == -1)
       -                        goto fail;
       -                if (mdi.type == AUDIO_MIXER_CLASS &&
       -                    strcmp(mdi.label.name, AudioCoutputs) == 0)
       -                        cls = mdi.index;
       -        }
       -        for (mdi.index = 0, v = -1; v == -1; ++mdi.index) {
       -                if (ioctl(fd, AUDIO_MIXER_DEVINFO, &mdi) == -1)
       -                        goto fail;
       -                if (mdi.type == AUDIO_MIXER_VALUE &&
       -                    mdi.prev == AUDIO_MIXER_LAST &&
       -                    mdi.mixer_class == cls &&
       -                    strcmp(mdi.label.name, AudioNmaster) == 0) {
       -                        mc.dev = mdi.index;
       -                        if (ioctl(fd, AUDIO_MIXER_READ, &mc) == -1)
       -                                goto fail;
       -                        v = MAX(mc.un.value.level[AUDIO_MIXER_LEVEL_MONO],
       -                                mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT]);
       -                }
       -        }
       -
       -        if (v == -1 || close(fd) == -1)
       -            return smprintf("NOCHAN");
       -
       -        return smprintf("%i", v * 100 / 255);
       +    return smprintf("%i", v * 100 / 255);
        
        fail:
       -        (void)close(fd);
       -        return smprintf("FAILED");
       +    (void)close(fd);
       +    return smprintf("FAILED");
        }
       +#endif 
        
        void settz(char *tzname) {
            setenv("TZ", tzname, 1);
       @@ -176,22 +187,24 @@ char * readfile(char *base, char *file) {
            return smprintf("%s", line);
        }
        
       +#ifdef __OpenBSD__
        char * getbattery()
        {
       -        int        fd;
       -        struct apm_power_info pi;
       +    int        fd;
       +    struct apm_power_info pi;
        
       -        if ((fd = open("/dev/apm", O_RDONLY)) == -1 ||
       -            ioctl(fd, APM_IOC_GETPOWER, &pi) == -1 ||
       -            close(fd) == -1)
       -            return smprintf("battery");
       +    if ((fd = open("/dev/apm", O_RDONLY)) == -1 ||
       +            ioctl(fd, APM_IOC_GETPOWER, &pi) == -1 ||
       +            close(fd) == -1)
       +        return smprintf("battery");
        
       -        if (pi.battery_state == APM_BATT_UNKNOWN ||
       -            pi.battery_state == APM_BATTERY_ABSENT)
       -            return smprintf("no battery");
       +    if (pi.battery_state == APM_BATT_UNKNOWN ||
       +            pi.battery_state == APM_BATTERY_ABSENT)
       +        return smprintf("no battery");
        
       -        return smprintf("%i", pi.battery_life);
       +    return smprintf("%i", pi.battery_life);
        }
       +#endif
        
        char * getifaddr(char * intf)
        {
       @@ -217,31 +230,33 @@ void update() {
        
            // date and time (local)
            char *time = mktimes("%H:%M", tzlocal);
       -    //char *date = mktimes("%Y-%m-%d", tzlocal);
       +    char *date = mktimes("%a %Y-%m-%d", tzlocal);
        
            // free disk space
            char *freehomespace = getfreespace("/home");
       -    char *freedataspace = getfreespace("/data");
       -    char *freerootspace = getfreespace("/");
        
            // display status
       +#ifdef __linux__
       +    setstatus(smprintf(
       +                "SPACE: %.2fG | LOAD: %s | %s %s",
       +                (float)atoi(freehomespace) / 1024, loadavg(0), date, time));
       +#else
            setstatus(smprintf(
                        "H:%.2fG D:%.2fG šŸ”Œ %s%% šŸ”Š %s%% šŸ”„ %sĀ°C šŸ”ƒ %sRPM šŸ“ˆ %s @ %s",
                        (float)atoi(freehomespace) / 1024, (float)atoi(freedataspace) / 1024,
                        getbattery(), getvolume(), getcputemp(), getfan(), loadavg(0), time));
       +#endif
        
            // free everything up
            free(time);
       -    //free(date);
       +    free(date);
            free(freehomespace);
       -    free(freerootspace);
       -    free(freedataspace);
        }
        
        void sig_handler(int signo) {
            if (signo == SIGUSR1)
                update();
       -        usleep(50000);
       +    usleep(50000);
        }
        
        int main(void) {