Merge pull request #51 from NBonaparte/master

Added internal battery block.
This commit is contained in:
George Badawi 2016-04-15 03:55:36 +03:00
commit a187dee742
3 changed files with 72 additions and 3 deletions

View file

@ -5,7 +5,7 @@ bar-list = ["bar1"];
bar1:{
font: "Droid Sans, FontAwesome Bold 10";
block-list: ["ya_ws", "ya_title", "ya_date", "ya_uptime", "ya_cpu", "ya_thermal", "ya_brightness", "ya_bw", "ya_mem", "ya_disk"];
block-list: ["ya_ws", "ya_title", "ya_date", "ya_uptime", "ya_cpu", "ya_thermal", "ya_brightness", "ya_bw", "ya_mem", "ya_disk", "ya_bat"];
#block-list: ["ya_date", "ya_uptime"];
position: "top";
gap-horizontal: 20; #width in pixels
@ -108,6 +108,16 @@ bar1:{
underline-color-rgb:0xE97F02;
#internal-spacing: true;
}
ya_bat: {
exec: "YABAR_BATTERY";
align: "right";
fixed-size: 70;
interval: 1;
internal-suffix: "%";
internal-option1: "BAT0";
internal-option2: "    ";
#internal-spacing: true;
}
ya_disk: {
exec: "YABAR_DISKIO";
align: "right";

View file

@ -101,9 +101,9 @@ enum {
};
#ifdef YA_INTERNAL_EWMH
#define YA_INTERNAL_LEN 11
#define YA_INTERNAL_LEN 12
#else
#define YA_INTERNAL_LEN 9
#define YA_INTERNAL_LEN 10
#endif
enum {
YA_INT_DATE = 0,
@ -115,6 +115,7 @@ enum {
YA_INT_CPU,
YA_INT_DISKIO,
YA_INT_NETWORK,
YA_INT_BATTERY,
YA_INT_TITLE,
YA_INT_WORKSPACE
};

View file

@ -17,6 +17,7 @@ void ya_int_bandwidth(ya_block_t *blk);
void ya_int_cpu(ya_block_t *blk);
void ya_int_diskio(ya_block_t *blk);
void ya_int_network(ya_block_t *blk);
void ya_int_battery(ya_block_t *blk);
struct reserved_blk ya_reserved_blks[YA_INTERNAL_LEN] = {
{"YABAR_DATE", ya_int_date},
@ -28,6 +29,7 @@ struct reserved_blk ya_reserved_blks[YA_INTERNAL_LEN] = {
{"YABAR_CPU", ya_int_cpu},
{"YABAR_DISKIO", ya_int_diskio},
{"YABAR_NETWORK", ya_int_network},
{"YABAR_BATTERY", ya_int_battery},
#ifdef YA_INTERNAL_EWMH
{"YABAR_TITLE", NULL},
{"YABAR_WORKSPACE", NULL}
@ -428,6 +430,62 @@ void ya_int_diskio(ya_block_t *blk) {
}
void ya_int_battery(ya_block_t *blk) {
int bat, space;
char stat;
char bat_25str[20], bat_50str[20], bat_75str[20], bat_100str[20], bat_chargestr[20];
char *startstr = blk->buf;
size_t prflen=0,suflen=0;
ya_setup_prefix_suffix(blk, &prflen, &suflen, &startstr);
FILE *cfile, *sfile;
if(blk->internal->spacing)
space = 3;
else
space = 0;
char cpath[128], spath[128];
snprintf(cpath, 128, "/sys/class/power_supply/%s/capacity", blk->internal->option[0]);
snprintf(spath, 128, "/sys/class/power_supply/%s/status", blk->internal->option[0]);
if(blk->internal->option[1]) {
sscanf(blk->internal->option[1], "%s %s %s %s %s", bat_25str, bat_50str, bat_75str, bat_100str, bat_chargestr);
}
cfile = fopen(cpath, "r");
sfile = fopen(spath, "r");
if (cfile == NULL || sfile == NULL) {
fprintf(stderr, "Error opening file %s or %s\n", cpath, spath);
strncpy(blk->buf, "BLOCK ERROR!", strlen("BLOCK ERROR!"));
ya_draw_pango_text(blk);
pthread_detach(blk->thread);
pthread_exit(NULL);
}
fclose(cfile);
fclose(sfile);
while(1) {
cfile = fopen(cpath, "r");
sfile = fopen(spath, "r");
if(fscanf(cfile, "%d", &bat) != 1)
fprintf(stderr, "Error getting values from file (%s)\n", cpath);
if(fscanf(sfile, "%c", &stat) != 1)
fprintf(stderr, "Error getting values from file (%s)\n", spath);
if(bat <= 25)
strcpy(startstr, bat_25str);
else if(bat <= 50)
strcpy(startstr, bat_50str);
else if(bat <= 75)
strcpy(startstr, bat_75str);
else
strcpy(startstr, bat_100str);
if(stat == 'C')
strcat(strcat(startstr, " "), bat_chargestr);
sprintf(startstr+strlen(startstr), "%*d", space, bat);
if(suflen)
strcat(blk->buf, blk->internal->suffix);
ya_draw_pango_text(blk);
fclose(cfile);
fclose(sfile);
sleep(blk->sleep);
}
}
#define _GNU_SOURCE
#include <sys/socket.h>
#include <ifaddrs.h>