From a1751622782eb9d3d9bfae56b75b857d8cbd0c6c Mon Sep 17 00:00:00 2001 From: Christoph Lohmann <20h@r-36.net> Date: Sun, 10 Sep 2017 19:28:54 +0200 Subject: Initial commit. --- LICENSE | 21 +++++ Makefile | 49 +++++++++++ config.mk | 30 +++++++ dwmstatus-temperature.c | 15 ++++ dwmstatus.c | 229 ++++++++++++++++++++++++++++++++++++++++++++++++ dwmstatus.o | Bin 0 -> 22488 bytes new-acpi-battery.c | 55 ++++++++++++ 7 files changed, 399 insertions(+) create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 config.mk create mode 100644 dwmstatus-temperature.c create mode 100644 dwmstatus.c create mode 100644 dwmstatus.o create mode 100644 new-acpi-battery.c diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..288e644 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT/X Consortium License + +© 2011 Christoph Lohmann <20h@r-36.net> + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..62240a5 --- /dev/null +++ b/Makefile @@ -0,0 +1,49 @@ +# See LICENSE file for copyright and license details. + +include config.mk + +SRC = ${NAME}.c +OBJ = ${SRC:.c=.o} + +all: options ${NAME} + +options: + @echo ${NAME} build options: + @echo "CFLAGS = ${CFLAGS}" + @echo "LDFLAGS = ${LDFLAGS}" + @echo "CC = ${CC}" + +.c.o: + @echo CC $< + @${CC} -c ${CFLAGS} $< + +${OBJ}: config.mk + +${NAME}: ${OBJ} + @echo CC -o $@ + @${CC} -o $@ ${OBJ} ${LDFLAGS} + +clean: + @echo cleaning + @rm -f ${NAME} ${OBJ} ${NAME}-${VERSION}.tar.gz + +dist: clean + @echo creating dist tarball + @mkdir -p ${NAME}-${VERSION} + @cp -R Makefile LICENSE config.mk \ + ${SRC} ${NAME}-${VERSION} + @tar -cf ${NAME}-${VERSION}.tar ${NAME}-${VERSION} + @gzip ${NAME}-${VERSION}.tar + @rm -rf ${NAME}-${VERSION} + +install: all + @echo installing executable file to ${DESTDIR}${PREFIX}/bin + @mkdir -p ${DESTDIR}${PREFIX}/bin + @cp -f ${NAME} ${DESTDIR}${PREFIX}/bin + @chmod 755 ${DESTDIR}${PREFIX}/bin/${NAME} + +uninstall: + @echo removing executable file from ${DESTDIR}${PREFIX}/bin + @rm -f ${DESTDIR}${PREFIX}/bin/${NAME} + +.PHONY: all options clean dist install uninstall diff --git a/config.mk b/config.mk new file mode 100644 index 0000000..0ec9e47 --- /dev/null +++ b/config.mk @@ -0,0 +1,30 @@ +NAME = dwmstatus +VERSION = 1.0 + +# Customize below to fit your system + +# paths +PREFIX = /usr +MANPREFIX = ${PREFIX}/share/man + +X11INC = /usr/X11R6/include +X11LIB = /usr/X11R6/lib + +# includes and libs +INCS = -I. -I/usr/include -I${X11INC} +LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 + +# flags +CPPFLAGS = -DVERSION=\"${VERSION}\" -D_DEFAULT_SOURCE +CFLAGS = -g -std=c99 -pedantic -Wall -O0 ${INCS} ${CPPFLAGS} +#CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS} +LDFLAGS = -g ${LIBS} +#LDFLAGS = -s ${LIBS} + +# Solaris +#CFLAGS = -fast ${INCS} -DVERSION=\"${VERSION}\" +#LDFLAGS = ${LIBS} + +# compiler and linker +CC = cc + diff --git a/dwmstatus-temperature.c b/dwmstatus-temperature.c new file mode 100644 index 0000000..13f27f6 --- /dev/null +++ b/dwmstatus-temperature.c @@ -0,0 +1,15 @@ +/* + * gettemperature("/sys/class/hwmon/hwmon0/device", "temp1_input"); + */ + +char * +gettemperature(char *base, char *sensor) +{ + char *co; + + co = readfile(base, sensor); + if (co == NULL) + return smprintf(""); + return smprintf("%02.0f°C", atof(co) / 1000); +} + diff --git a/dwmstatus.c b/dwmstatus.c new file mode 100644 index 0000000..9790212 --- /dev/null +++ b/dwmstatus.c @@ -0,0 +1,229 @@ +/* + * Copy me if you can. + * by 20h + */ + +#define _BSD_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +char *tzargentina = "America/Buenos_Aires"; +char *tzutc = "UTC"; +char *tzberlin = "Europe/Berlin"; + +static Display *dpy; + +char * +smprintf(char *fmt, ...) +{ + va_list fmtargs; + char *ret; + int len; + + va_start(fmtargs, fmt); + len = vsnprintf(NULL, 0, fmt, fmtargs); + va_end(fmtargs); + + ret = malloc(++len); + if (ret == NULL) { + perror("malloc"); + exit(1); + } + + va_start(fmtargs, fmt); + vsnprintf(ret, len, fmt, fmtargs); + va_end(fmtargs); + + return ret; +} + +void +settz(char *tzname) +{ + setenv("TZ", tzname, 1); +} + +char * +mktimes(char *fmt, char *tzname) +{ + char buf[129]; + time_t tim; + struct tm *timtm; + + settz(tzname); + tim = time(NULL); + timtm = localtime(&tim); + if (timtm == NULL) + return smprintf(""); + + if (!strftime(buf, sizeof(buf)-1, fmt, timtm)) { + fprintf(stderr, "strftime == 0\n"); + return smprintf(""); + } + + return smprintf("%s", buf); +} + +void +setstatus(char *str) +{ + XStoreName(dpy, DefaultRootWindow(dpy), str); + XSync(dpy, False); +} + +char * +loadavg(void) +{ + double avgs[3]; + + if (getloadavg(avgs, 3) < 0) + return smprintf(""); + + return smprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]); +} + +char * +readfile(char *base, char *file) +{ + char *path, line[513]; + FILE *fd; + + memset(line, 0, sizeof(line)); + + path = smprintf("%s/%s", base, file); + fd = fopen(path, "r"); + free(path); + if (fd == NULL) + return NULL; + + if (fgets(line, sizeof(line)-1, fd) == NULL) + return NULL; + fclose(fd); + + return smprintf("%s", line); +} + +char * +getbattery(char *base) +{ + char *co, status; + int descap, remcap; + + descap = -1; + remcap = -1; + + co = readfile(base, "present"); + if (co == NULL) + return smprintf(""); + if (co[0] != '1') { + free(co); + return smprintf("not present"); + } + free(co); + + co = readfile(base, "charge_full_design"); + if (co == NULL) { + co = readfile(base, "energy_full_design"); + if (co == NULL) + return smprintf(""); + } + sscanf(co, "%d", &descap); + free(co); + + co = readfile(base, "charge_now"); + if (co == NULL) { + co = readfile(base, "energy_now"); + if (co == NULL) + return smprintf(""); + } + sscanf(co, "%d", &remcap); + free(co); + + co = readfile(base, "status"); + if (!strncmp(co, "Discharging", 11)) { + status = '-'; + } else if(!strncmp(co, "Charging", 8)) { + status = '+'; + } else { + status = '?'; + } + + if (remcap < 0 || descap < 0) + return smprintf("invalid"); + + return smprintf("%.0f%%%c", ((float)remcap / (float)descap) * 100, status); +} + +char * +gettemperature(char *base, char *sensor) +{ + char *co; + + co = readfile(base, sensor); + if (co == NULL) + return smprintf(""); + return smprintf("%02.0f°C", atof(co) / 1000); +} + +int +main(void) +{ + char *status; + char *avgs; + char *bat; + char *bat1; + char *tmar; + char *tmutc; + char *tmbln; + char *t0, *t1, *t2, *t3; + + if (!(dpy = XOpenDisplay(NULL))) { + fprintf(stderr, "dwmstatus: cannot open display.\n"); + return 1; + } + + for (;;sleep(60)) { + avgs = loadavg(); + bat = getbattery("/sys/class/power_supply/BAT0"); + bat1 = getbattery("/sys/class/power_supply/BAT1"); + tmar = mktimes("%H:%M", tzargentina); + tmutc = mktimes("%H:%M", tzutc); + tmbln = mktimes("KW %W %a %d %b %H:%M %Z %Y", tzberlin); + t0 = gettemperature("/sys/devices/virtual/hwmon/hwmon1", "temp1_input"); + t1 = gettemperature("/sys/devices/platform/coretemp.0/hwmon/hwmon3", "temp1_input"); + t2 = gettemperature("/sys/devices/platform/coretemp.0/hwmon/hwmon3", "temp2_input"); + t3 = gettemperature("/sys/devices/platform/coretemp.0/hwmon/hwmon3", "temp3_input"); + + status = smprintf("T:%s|%s|%s|%s L:%s B:%s|%s A:%s U:%s %s", + t0, t1, t2, t3, avgs, bat, bat1, tmar, tmutc, + tmbln); + setstatus(status); + + free(t0); + free(t1); + free(t2); + free(t3); + free(avgs); + free(bat); + free(bat1); + free(tmar); + free(tmutc); + free(tmbln); + free(status); + } + + XCloseDisplay(dpy); + + return 0; +} + diff --git a/dwmstatus.o b/dwmstatus.o new file mode 100644 index 0000000..05e63c4 Binary files /dev/null and b/dwmstatus.o differ diff --git a/new-acpi-battery.c b/new-acpi-battery.c new file mode 100644 index 0000000..cda0084 --- /dev/null +++ b/new-acpi-battery.c @@ -0,0 +1,55 @@ +char * +readfile(char *base, char *file) +{ + char *path, line[513]; + FILE *fd; + + memset(line, 0, sizeof(line)); + + path = smprintf("%s/%s", base, file); + fd = fopen(path, "r"); + if (fd == NULL) { + perror("fopen"); + exit(1); + } + free(path); + + if (fgets(line, sizeof(line)-1, fd) == NULL) { + perror("fgets"); + exit(1); + } + fclose(fd); + + return smprintf("%s", line); +} + +char * +getbattery(char *base) +{ + char *co; + int descap, remcap; + + descap = -1; + remcap = -1; + + co = readfile(base, "present"); + if (co[0] != '1') { + free(co); + return smprintf("not present"); + } + free(co); + + co = readfile(base, "charge_full_design"); + sscanf(co, "%d", &descap); + free(co); + + co = readfile(base, "charge_now"); + sscanf(co, "%d", &remcap); + free(co); + + if (remcap < 0 || descap < 0) + return smprintf("invalid"); + + return smprintf("%.0f", ((float)remcap / (float)descap) * 100); +} + -- cgit v1.2.3