sites

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit f6b1522b348c9df8dd6f0d177466de1936bbce77
parent ed4bfa97565bb4df14ac251fefa84c6609d12d91
Author: profil <profil@libs.se>
Date:   Thu, 29 Dec 2011 02:20:41 +0100

added another dwmstatus
Diffstat:
Mdwm.suckless.org/dwmstatus/index.md | 8++++++++
Adwm.suckless.org/dwmstatus/profil-dwmstatus-1.0.c | 128+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 136 insertions(+), 0 deletions(-)

diff --git a/dwm.suckless.org/dwmstatus/index.md b/dwm.suckless.org/dwmstatus/index.md @@ -14,6 +14,14 @@ The Barebone This is a barebone project you can use for changing it to your needs. +User submitted versions +----------------------- + +Please add your own version of dwmstatus here. + +* [profil-dwmstatus-1.0.c](profil-dwmstatus-1.0.c) - cpufreq, battery percent and date/time + + Helper functions ---------------- diff --git a/dwm.suckless.org/dwmstatus/profil-dwmstatus-1.0.c b/dwm.suckless.org/dwmstatus/profil-dwmstatus-1.0.c @@ -0,0 +1,128 @@ +/* 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> + +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/energy_now", "r"); + if(fd == NULL) { + fprintf(stderr, "Error opening energy_now.\n"); + return -1; + } + fscanf(fd, "%d", &energy_now); + fclose(fd); + + + fd = fopen("/sys/class/power_supply/BAT0/energy_full", "r"); + if(fd == NULL) { + fprintf(stderr, "Error opening energy_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 main(void) { + char *status; + float cpu0, cpu1; + char *datetime; + 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(); + snprintf(status, 200, "%0.2f, %0.2f | %d%% | %s", cpu0, cpu1, bat0, datetime); + + free(datetime); + setstatus(status); + } + + free(status); + XCloseDisplay(dpy); + + return 0; +} +