123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- /*
- * Copy me if you can.
- * by 20h
- */
- #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>
- char *tzchile = "America/Santiago";
- 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 *
- 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);
- }
- int
- main(void)
- {
- char *status;
- char *bat;
- char *tmcl;
- if (!(dpy = XOpenDisplay(NULL))) {
- fprintf(stderr, "dwmstatus: cannot open display.\n");
- return 1;
- }
- for (;;sleep(60)) {
- bat = getbattery("/sys/class/power_supply/BAT0");
- tmcl = mktimes("%H:%M"/*" | %a %d %b %Y"*/, tzchile);
- status = smprintf("B:%s | %s | pomelo",
- bat, tmcl);
- setstatus(status);
- free(bat);
- free(tmcl);
- free(status);
- }
- XCloseDisplay(dpy);
- return 0;
- }
|