Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* made by profil 2011-12-29.
- *
- ** Compile with:
- ** gcc -Wall -pedantic -std=c99 -lX11 status.c
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <time.h>
- #include <X11/Xlib.h>
- #include <string.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;
- }
- 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/BAT0/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/BAT0/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/BAT0/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 chargingStatus(char* stat){
- FILE *fd;
- fd = fopen("/sys/class/power_supply/BAT0/status", "r");
- if(fd == NULL){
- fprintf(stderr, "Error opening BAT0 status.\n");
- return -1;
- }
- fgets(stat, 20, fd);
- fclose(fd);
- return 0;
- }
- int main(void) {
- char *status;
- float cpu0, cpu1;
- char *datetime;
- char batstat[20];
- int bat0;
- 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");
- datetime = getdatetime();
- bat0 = getbattery();
- chargingStatus(batstat);
- snprintf(status, 200, "%0.2f, %0.2f | %d%% %s| %s", cpu0, cpu1, bat0, batstat,datetime);
- free(datetime);
- setstatus(status);
- }
- free(status);
- XCloseDisplay(dpy);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment