Zexanima

status

Jan 3rd, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.86 KB | None | 0 0
  1. /* made by profil 2011-12-29.
  2. *
  3. ** Compile with:
  4. ** gcc -Wall -pedantic -std=c99 -lX11 status.c
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <time.h>
  10. #include <X11/Xlib.h>
  11. #include <string.h>
  12. static Display *dpy;
  13.  
  14. void setstatus(char *str) {
  15.     XStoreName(dpy, DefaultRootWindow(dpy), str);
  16.     XSync(dpy, False);
  17. }
  18.  
  19. float getfreq(char *file) {
  20.     FILE *fd;
  21.     char *freq;
  22.     float ret;
  23.  
  24.     freq = malloc(10);
  25.     fd = fopen(file, "r");
  26.     if(fd == NULL) {
  27.         fprintf(stderr, "Cannot open '%s' for reading.\n", file);
  28.         exit(1);
  29.     }
  30.  
  31.     fgets(freq, 10, fd);
  32.     fclose(fd);
  33.  
  34.     ret = atof(freq)/1000000;
  35.     free(freq);
  36.     return ret;
  37. }
  38.  
  39. char *getdatetime() {
  40.     char *buf;
  41.     time_t result;
  42.     struct tm *resulttm;
  43.  
  44.     if((buf = malloc(sizeof(char)*65)) == NULL) {
  45.         fprintf(stderr, "Cannot allocate memory for buf.\n");
  46.         exit(1);
  47.     }
  48.     result = time(NULL);
  49.     resulttm = localtime(&result);
  50.     if(resulttm == NULL) {
  51.         fprintf(stderr, "Error getting localtime.\n");
  52.         exit(1);
  53.     }
  54.     if(!strftime(buf, sizeof(char)*65-1, "%a %b %d %H:%M:%S", resulttm)) {
  55.         fprintf(stderr, "strftime is 0.\n");
  56.         exit(1);
  57.     }
  58.    
  59.     return buf;
  60. }
  61.  
  62. int getbattery() {
  63.     FILE *fd;
  64.     int energy_now, energy_full, voltage_now;
  65.  
  66.     fd = fopen("/sys/class/power_supply/BAT0/charge_now", "r");
  67.     if(fd == NULL) {
  68.         fprintf(stderr, "Error opening charge_now.\n");
  69.         return -1;
  70.     }
  71.     fscanf(fd, "%d", &energy_now);
  72.     fclose(fd);
  73.  
  74.  
  75.     fd = fopen("/sys/class/power_supply/BAT0/charge_full", "r");
  76.     if(fd == NULL) {
  77.         fprintf(stderr, "Error opening charge_full.\n");
  78.         return -1;
  79.     }
  80.     fscanf(fd, "%d", &energy_full);
  81.     fclose(fd);
  82.  
  83.  
  84.     fd = fopen("/sys/class/power_supply/BAT0/voltage_now", "r");
  85.     if(fd == NULL) {
  86.         fprintf(stderr, "Error opening voltage_now.\n");
  87.         return -1;
  88.     }
  89.     fscanf(fd, "%d", &voltage_now);
  90.     fclose(fd);
  91.  
  92.  
  93.     return ((float)energy_now * 1000 / (float)voltage_now) * 100 / ((float)energy_full * 1000 / (float)voltage_now);
  94. }
  95.  
  96. int chargingStatus(char* stat){
  97.     FILE *fd;
  98.     fd = fopen("/sys/class/power_supply/BAT0/status", "r");
  99.     if(fd == NULL){
  100.         fprintf(stderr, "Error opening BAT0 status.\n");
  101.         return -1;
  102.     }
  103.     fgets(stat, 20, fd);
  104.     fclose(fd);
  105.     return 0;
  106. }
  107. int main(void) {
  108.     char *status;
  109.     float cpu0, cpu1;
  110.     char *datetime;
  111.     char batstat[20];
  112.     int bat0;
  113.    
  114.     if (!(dpy = XOpenDisplay(NULL))) {
  115.         fprintf(stderr, "Cannot open display.\n");
  116.         return 1;
  117.     }
  118.  
  119.     if((status = malloc(200)) == NULL)
  120.         exit(1);
  121.    
  122.  
  123.     for (;;sleep(1)) {
  124.         cpu0 = getfreq("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");
  125.         cpu1 = getfreq("/sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq");
  126.         datetime = getdatetime();
  127.         bat0 = getbattery();
  128.         chargingStatus(batstat);
  129.         snprintf(status, 200, "%0.2f, %0.2f | %d%% %s| %s", cpu0, cpu1, bat0, batstat,datetime);
  130.  
  131.         free(datetime);
  132.         setstatus(status);
  133.     }
  134.     free(status);
  135.     XCloseDisplay(dpy);
  136.  
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment