Created
June 13, 2014 14:03
-
-
Save raymontag/6f9bd9d00f2d397bddb3 to your computer and use it in GitHub Desktop.
Revisions
-
raymontag created this gist
Jun 13, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,140 @@ /* made by grayfox, inspired by profil-dwmstatus ** ** Compile with: ** gcc -Wall -pedantic -std=c99 -lX11 status.c */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <time.h> #include <X11/Xlib.h> static Display *dpy; void setstatus(char *str) { XStoreName(dpy, DefaultRootWindow(dpy), str); XSync(dpy, False); } //float getfreq(char *file) { // FILE *fd; // char *freq; // float ret; // // freq = malloc(10); // fd = fopen(file, "r"); // if(fd == NULL) { // fprintf(stderr, "Cannot open '%s' for reading.\n", file); // exit(1); // } // // fgets(freq, 10, fd); // fclose(fd); // // ret = atof(freq)/1000000; // free(freq); // return ret; //} void getnetstatus(char buf[18]) { FILE *fd; char tmp[5]; fd = fopen("/sys/class/net/wlp2s0/operstate", "r"); if(fd == NULL) { fprintf(stderr, "Cannot open /sys/class/net/wlp2s0/operstate.\n"); exit(1); } fscanf(fd, "%4s", tmp); strcpy(buf, "W: "); strncat(buf, tmp, 4); fclose(fd); fopen("/sys/class/net/enp3s0/operstate", "r"); if(fd == NULL) { fprintf(stderr, "Cannot open /sys/class/net/enp3s0/operstate.\n"); exit(1); } fscanf(fd, "%4s", tmp); strcat(buf, " | E: "); strncat(buf, tmp, 4); fclose(fd); } char *getdatetime() { char *buf; time_t result; struct tm *resulttm; if((buf = malloc(sizeof(char)*65)) == NULL) { fprintf(stderr, "Cannot allocate memory for buf.\n"); exit(1); } result = time(NULL); resulttm = localtime(&result); if(resulttm == NULL) { fprintf(stderr, "Error getting localtime.\n"); exit(1); } if(!strftime(buf, sizeof(char)*65-1, "%a %b %d %H:%M:%S", resulttm)) { fprintf(stderr, "strftime is 0.\n"); exit(1); } return buf; } int getbattery() { FILE *fd; int energy_now, energy_full, voltage_now; fd = fopen("/sys/class/power_supply/BAT1/charge_now", "r"); if(fd == NULL) { fprintf(stderr, "Error opening charge_now.\n"); return -1; } fscanf(fd, "%d", &energy_now); fclose(fd); fd = fopen("/sys/class/power_supply/BAT1/charge_full", "r"); if(fd == NULL) { fprintf(stderr, "Error opening charge_full.\n"); return -1; } fscanf(fd, "%d", &energy_full); fclose(fd); fd = fopen("/sys/class/power_supply/BAT1/voltage_now", "r"); if(fd == NULL) { fprintf(stderr, "Error opening voltage_now.\n"); return -1; } fscanf(fd, "%d", &voltage_now); fclose(fd); return ((float)energy_now * 1000 / (float)voltage_now) * 100 / ((float)energy_full * 1000 / (float)voltage_now); } int main(void) { char *status; //float cpu0, cpu1, cpu2, cpu3; char *datetime; char net[18]; int bat1; if (!(dpy = XOpenDisplay(NULL))) { fprintf(stderr, "Cannot open display.\n"); return 1; } if((status = malloc(200)) == NULL) exit(1); for (;;sleep(1)) { //cpu0 = getfreq("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"); //cpu1 = getfreq("/sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq"); //cpu2 = getfreq("/sys/devices/system/cpu/cpu2/cpufreq/scaling_cur_freq"); //cpu3 = getfreq("/sys/devices/system/cpu/cpu3/cpufreq/scaling_cur_freq"); datetime = getdatetime(); bat1 = getbattery(); getnetstatus(net); //snprintf(status, 200, "%0.2f, %0.2f, %0.2f, %0.2f | %d%% | %s", cpu0, cpu1, cpu2, cpu3, bat1, datetime); snprintf(status, 200, "%s | %d%% | %s", net, bat1, datetime); free(datetime); setstatus(status); } free(status); XCloseDisplay(dpy); return 0; }