Commits

Thomas Dahms committed a4ebd2b

initial commit

Comments (0)

Files changed (3)

+# 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 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
+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
+LIBS = -L/usr/lib -lc -L${X11LIB} -lX11
+
+# flags
+CPPFLAGS = -DVERSION=\"${VERSION}\"
+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
+
+#define _BSD_SOURCE
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+//#include <strings.h>
+//#include <sys/time.h>
+#include <time.h>
+//#include <sys/types.h>
+//#include <sys/wait.h>
+
+#include <X11/Xlib.h>
+
+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;
+}
+
+char *
+mktimes(char *fmt)
+{
+	char buf[129];
+	time_t tim;
+	struct tm *timtm;
+
+	bzero(buf, sizeof(buf));
+	tim = time(NULL);
+	timtm = localtime(&tim);
+	if (timtm == NULL) {
+		perror("localtime");
+		exit(1);
+	}
+
+	if (!strftime(buf, sizeof(buf)-1, fmt, timtm)) {
+		fprintf(stderr, "strftime == 0\n");
+		exit(1);
+	}
+
+	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) {
+		perror("getloadavg");
+		exit(1);
+	}
+
+	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");
+	if (fd == NULL)
+		return NULL;
+	free(path);
+
+	if (fgets(line, sizeof(line)-1, fd) == NULL)
+		return NULL;
+	fclose(fd);
+
+	return smprintf("%s", line);
+}
+
+/*
+ * Linux seems to change the filenames after suspend/hibernate
+ * according to a random scheme. So just check for both possibilities.
+ */
+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");
+	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);
+
+	if (remcap < 0 || descap < 0)
+		return smprintf("invalid");
+
+	return smprintf("%.0f", ((float)remcap / (float)descap) * 100);
+}
+
+int
+main(void)
+{
+	char *status;
+	char *avgs;
+	char *tmbln;
+	char *bttry;
+
+	if (!(dpy = XOpenDisplay(NULL))) {
+		fprintf(stderr, "dwmstatus: cannot open display.\n");
+		return 1;
+	}
+
+	for (;;sleep(1)) {
+		avgs = loadavg();
+		tmbln = mktimes("%a %d %b %T");
+		bttry = getbattery("/sys/class/power_supply/BAT0/");
+		status = smprintf("battery %s%%, load %s, %s", bttry, avgs, tmbln);
+		setstatus(status);
+		free(avgs);
+		free(tmbln);
+		free(status);
+	}
+
+	XCloseDisplay(dpy);
+
+	return 0;
+}
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.