spoon

set dwm status
git clone git://git.2f30.org/spoon.git
Log | Files | Refs | LICENSE

commit e12c3d3c4d8ba3ddd7734b1cfacecbd609b33b40
parent d3b7e88e03c32c823676e2f9895289af2d399ca4
Author: lostd <lostd@2f30.org>
Date:   Thu Oct 13 00:24:56 +0100

Separate all system-dependent code

Diffstat:
Makefile | 5+++--
cpu.c | 27+++++++++++++++++++++++++++
mix.c | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
spoon.c | 119-------------------------------------------------------------------------------
temp.c | 32++++++++++++++++++++++++++++++++
5 files changed, 135 insertions(+), 121 deletions(-)
diff --git a/Makefile b/Makefile @@ -1,8 +1,9 @@ VERSION = 0.2 PREFIX = /usr/local -DISTFILES = spoon.c batt.c wifi.c strlcpy.c strlcat.c util.h config.def.h\ +DISTFILES = spoon.c batt.c wifi.c cpu.c temp.c mix.c\ + strlcpy.c strlcat.c util.h config.def.h\ Makefile LICENSE configure -OBJ = spoon.o batt.o wifi.o strlcpy.o strlcat.o +OBJ = spoon.o batt.o wifi.o cpu.o temp.o mix.o strlcpy.o strlcat.o BIN = spoon include config.mk diff --git a/cpu.c b/cpu.c @@ -0,0 +1,27 @@ +#include <stddef.h> +#include <stdio.h> + +#ifdef __OpenBSD__ +#include <sys/sysctl.h> + +int +cpuread(char *buf, size_t len) +{ + int mib[2], cpuspeed; + size_t sz; + + mib[0] = CTL_HW; + mib[1] = HW_CPUSPEED; + sz = sizeof(cpuspeed); + if (sysctl(mib, 2, &cpuspeed, &sz, NULL, 0) < 0) + return -1; + snprintf(buf, len, "%4dMHz", cpuspeed); + return 0; +} +#else +int +cpuread(char *buf, size_t len) +{ + return -1; +} +#endif diff --git a/mix.c b/mix.c @@ -0,0 +1,73 @@ +#include <err.h> +#include <stddef.h> +#include <stdio.h> + +#ifdef __OpenBSD__ +#include <sys/ioctl.h> +#include <sys/audioio.h> + +#include <fcntl.h> +#include <string.h> + +int +mixread(char *buf, size_t len) +{ + mixer_devinfo_t dinfo; + mixer_ctrl_t mctl; + int fd, master, ret = 0, i = -1; + + fd = open("/dev/mixer", O_RDONLY); + if (fd == -1) { + warn("open %s", "/dev/mixer"); + return -1; + } + dinfo.index = 0; + /* outputs */ + for (; ; dinfo.index++) { + ret = ioctl(fd, AUDIO_MIXER_DEVINFO, &dinfo); + if (ret == -1) { + warn("AUDIO_MIXER_DEVINFO %s", "/dev/mixer"); + goto out; + } + if (dinfo.type == AUDIO_MIXER_CLASS && + strcmp(dinfo.label.name, AudioCoutputs) == 0) { + i = dinfo.index; + break; + } + } + if (i == -1) { + warnx("no outputs mixer class: %s", "/dev/mixer"); + goto out; + } + /* outputs.master */ + for (; ; dinfo.index++) { + ret = ioctl(fd, AUDIO_MIXER_DEVINFO, &dinfo); + if (ret == -1) { + warn("AUDIO_MIXER_DEVINFO %s", "/dev/mixer"); + goto out; + } + if (dinfo.type == AUDIO_MIXER_VALUE && + dinfo.prev == AUDIO_MIXER_LAST && + dinfo.mixer_class == i && + strcmp(dinfo.label.name, AudioNmaster) == 0) + break; + } + mctl.dev = dinfo.index; + ret = ioctl(fd, AUDIO_MIXER_READ, &mctl); + if (ret == -1) { + warn("AUDIO_MIXER_READ %s", "/dev/mixer"); + goto out; + } + master = mctl.un.value.level[0] * 100 / 255; + snprintf(buf, len, "%d%%", master); +out: + close(fd); + return ret; +} +#else +int +mixread(char *buf, size_t len) +{ + return -1; +} +#endif diff --git a/spoon.c b/spoon.c @@ -88,125 +88,6 @@ out: return -1; } -#ifdef __OpenBSD__ -#include <sys/select.h> -#include <sys/sysctl.h> -#include <sys/sensors.h> -#include <sys/ioctl.h> -#include <sys/audioio.h> - -#include <fcntl.h> -#include <limits.h> - -int -cpuread(char *buf, size_t len) -{ - int mib[2], cpuspeed; - size_t sz; - - mib[0] = CTL_HW; - mib[1] = HW_CPUSPEED; - sz = sizeof(cpuspeed); - if (sysctl(mib, 2, &cpuspeed, &sz, NULL, 0) < 0) - return -1; - snprintf(buf, len, "%4dMHz", cpuspeed); - return 0; -} - -int -tempread(char *buf, size_t len) -{ - int mib[5]; - struct sensor temp; - size_t sz; - - mib[0] = CTL_HW; - mib[1] = HW_SENSORS; - mib[2] = 0; /* cpu0 */ - mib[3] = SENSOR_TEMP; - mib[4] = 0; /* temp0 */ - sz = sizeof(temp); - if (sysctl(mib, 5, &temp, &sz, NULL, 0) == -1) - return -1; - snprintf(buf, len, "%ddegC", (temp.value - 273150000) / 1000000); - return 0; -} - -int -mixread(char *buf, size_t len) -{ - mixer_devinfo_t dinfo; - mixer_ctrl_t mctl; - int fd, master, ret = 0, i = -1; - - fd = open("/dev/mixer", O_RDONLY); - if (fd == -1) { - warn("open %s", "/dev/mixer"); - return -1; - } - dinfo.index = 0; - /* outputs */ - for (; ; dinfo.index++) { - ret = ioctl(fd, AUDIO_MIXER_DEVINFO, &dinfo); - if (ret == -1) { - warn("AUDIO_MIXER_DEVINFO %s", "/dev/mixer"); - goto out; - } - if (dinfo.type == AUDIO_MIXER_CLASS && - strcmp(dinfo.label.name, AudioCoutputs) == 0) { - i = dinfo.index; - break; - } - } - if (i == -1) { - warnx("no outputs mixer class: %s", "/dev/mixer"); - goto out; - } - /* outputs.master */ - for (; ; dinfo.index++) { - ret = ioctl(fd, AUDIO_MIXER_DEVINFO, &dinfo); - if (ret == -1) { - warn("AUDIO_MIXER_DEVINFO %s", "/dev/mixer"); - goto out; - } - if (dinfo.type == AUDIO_MIXER_VALUE && - dinfo.prev == AUDIO_MIXER_LAST && - dinfo.mixer_class == i && - strcmp(dinfo.label.name, AudioNmaster) == 0) - break; - } - mctl.dev = dinfo.index; - ret = ioctl(fd, AUDIO_MIXER_READ, &mctl); - if (ret == -1) { - warn("AUDIO_MIXER_READ %s", "/dev/mixer"); - goto out; - } - master = mctl.un.value.level[0] * 100 / 255; - snprintf(buf, len, "%d%%", master); -out: - close(fd); - return ret; -} -#else -int -cpuread(char *buf, size_t len) -{ - return -1; -} - -int -tempread(char *buf, size_t len) -{ - return -1; -} - -int -mixread(char *buf, size_t len) -{ - return -1; -} -#endif - int dateread(char *buf, size_t len) { diff --git a/temp.c b/temp.c @@ -0,0 +1,32 @@ +#include <stddef.h> +#include <stdio.h> + +#ifdef __OpenBSD__ +#include <sys/sysctl.h> +#include <sys/sensors.h> + +int +tempread(char *buf, size_t len) +{ + int mib[5]; + struct sensor temp; + size_t sz; + + mib[0] = CTL_HW; + mib[1] = HW_SENSORS; + mib[2] = 0; /* cpu0 */ + mib[3] = SENSOR_TEMP; + mib[4] = 0; /* temp0 */ + sz = sizeof(temp); + if (sysctl(mib, 5, &temp, &sz, NULL, 0) == -1) + return -1; + snprintf(buf, len, "%ddegC", (temp.value - 273150000) / 1000000); + return 0; +} +#else +int +tempread(char *buf, size_t len) +{ + return -1; +} +#endif