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: [PATCH] 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 0000000000000000000000000000000000000000..05e63c4c2a088ffb14605b51686bb1d77baf6bcc GIT binary patch literal 22488 zcmb_k3w)HtwV!V{kB{Y%KzM|QaYe#I&5M_Sq5+I7ih=?f(YkDQzfIQc%iV92faOsj z)-*;*E0$|fY1PVYs~5Gk2!g??pjAtM)?WNw^>VEg6sjm*e9Jv&=FFGLkhQnJc7E)B z=lti)nKS2{nQy+?FRZSaf2qqbm@Ednh;?%kWvuw>Zn~P2)od6W%$E4t4*5DB9CQ4G zQ>T3E`i)`8cncZxwe2cC1z6i}YWZd3GJff3fXn>0)+E1lw7_M3m+dW>P_w3B!WA0| zCM?-fFyTAf3MN=x1ruWX3npX^7ED-qJmhQ7DIE{}`r2B3(59+uZ)+u7O@Q{cP6nzH zD?4fmL0}rNzK+&~cxh{`=G;b<_O-VPoBrr)dv-UD;u-upf?sW|V?bgZ|2EqB#ka}V zwwG6en(^3v-7dVa%urv)OsC~D=r1nz57|V01dsbVI)rf>xpD2HE7>!#?HxGmkZ)2y z?gl%TclhG(r2o4*ScMJVVNQ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KurWUs^*lKfB&|1vq8g?WdD5{{ubFI z&;6ty4inD*2M}m0@vF$r7d1UoDBnKR@CPYB3+N$V>XGN;@;puAKC-`x%(=Xrecu|vIKzVpT z!|x;fu!e7;@t)N1I?9uC=}-o%@w}o{`i*euANhXAd`({3Q?KFjcm0fplR0dihRf%Q zA8PnJWY1>8r9JXJktZ~HY0n-FKTdi3mWE3`hc*53y^?PF6VofhRb<+P{ZYX;Rmr$C+(E^=A}4GT;>}qPcMf*1gL|nof6juNx`3h z1j=pv(~)xi)^&MZI?c*c3BmAMB+G#+=Wh~0B{m6Jb(wnjTZeE08ge>NSC_JydQ=Be zfp|UqrH9&tzY#ofTK>my;f*%eB#32@xmC%_EImNv+F*=Ch&)yynI1+2<~**E1{yd{z5UCWSWx8e7lJ!oHU<^F*#{ou)7->1P<#11fz%_%FZm~d=*@pEj`cPR_uqxX;7=id77!i5RS Ke62%`W&Z~lEcjgj literal 0 HcmV?d00001 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); +} +