Initial version of dwmst.

This commit is contained in:
Alexander Weidinger
2017-11-11 15:16:34 +01:00
commit 6f64315083
3 changed files with 75 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
dwmst

5
Makefile Normal file
View File

@@ -0,0 +1,5 @@
all:
gcc -o dwmst -lX11 dwmst.c
clean:
rm -f dwmst

69
dwmst.c Normal file
View File

@@ -0,0 +1,69 @@
#include <unistd.h>
#include <stdio.h>
#include <X11/Xlib.h>
#include <time.h>
#include <string.h>
#define INTERVAL 10
int main(void) {
Display *display = XOpenDisplay(NULL);
if (display == NULL) {
fprintf(stderr, "dwmst: cannot open display.\n");
}
/* time */
char tbuffer[128] = { 0 };
time_t currentTime;
struct tm *timeinfo;
/* battery */
char bbuffer[6] = { 0 };
FILE *f;
/* status */
char status[256] = { 0 };
for (;;sleep(INTERVAL)) {
/* get current time */
time(&currentTime);
timeinfo = localtime(&currentTime);
strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M", timeinfo);
/* battery status */
f = fopen("/sys/class/power_supply/BAT0/capacity", "r");
if (f == NULL) {
fprintf(stderr, "dwmst: cannot access battery.\n");
} else {
fgets(bbuffer, 4, f);
strtok(bbuffer, "\n"); /* remove trailing white space */
strcat(bbuffer, "%");
}
fclose(f);
f = fopen("/sys/class/power_supply/BAT0/status", "r");
if (f == NULL) {
fprintf(stderr, "dwmst: cannot access battery.\n");
} else {
switch(getc(f)) {
case 'D': /* Discharging */
strcat(bbuffer, "-");
break;
case 'C': /* Charging */
strcat(bbuffer, "+");
break;
case 'F': /* Full */
strcat(bbuffer, "=");
}
}
fclose(f);
/* build status */
sprintf(status, "%s | %s", tbuffer, bbuffer);
/* store root window name */
XStoreName(display, DefaultRootWindow(display), status);
XSync(display, False);
}
}