#define _BSD_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #define CPUTEMP "/sys/class/hwmon/hwmon0/temp1_input" #define KERCOL "\03%s\x01" #define TEMPCOL "\02%02.0f°C\x01" #define TIMECOL "\x03%^a %d %^b\x04 / \x03%H:%M\x01 " char *tzpar = "Europe/Paris"; 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; memset(buf, 0, 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* readfile(const char *file) { char line[513]; FILE *fd; memset(line, 0, sizeof(line)); fd = fopen(file, "r"); if (fd == NULL) return NULL; if (fgets(line, sizeof(line)-1, fd) == NULL) return NULL; fclose(fd); return smprintf("%s", line); } char* kernel(void) { struct utsname unake; uname(&unake); return smprintf(KERCOL, unake.release); } int getnvctrltempcelsius(void) { int temp; if (!XNVCTRLQueryAttribute(dpy, 0, 0, NV_CTRL_GPU_CORE_TEMPERATURE, &temp)) { return -1; } return temp; } char* gettemperature() { char *co; co = readfile(CPUTEMP); if (co == NULL) return NULL; return smprintf(TEMPCOL, atof(co) / 1000); } int main(void) { char *status; char *kerv; char *temp; char *tmdat; if (!(dpy = XOpenDisplay(NULL))) { fprintf(stderr, "dwmstatusbar: cannot open display.\n"); return 1; } for (;;sleep(30)) { kerv = kernel(); int nv_temp = getnvctrltempcelsius(); temp = gettemperature(); tmdat = mktimes(TIMECOL, tzpar); status = smprintf("K:%s G:%d TCPU:%s %s", kerv, nv_temp, temp, tmdat); setstatus(status); free(kerv); free(temp); free(tmdat); free(status); } XCloseDisplay(dpy); return 0; }