From c92baa1bca29968cb7a66d607042181a05d622ca Mon Sep 17 00:00:00 2001 From: geommer Date: Mon, 28 Mar 2016 14:49:15 +0200 Subject: [PATCH] Add bandwidth internal block. --- include/yabar.h | 2 +- src/intern_blks/ya_intern.c | 70 ++++++++++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/include/yabar.h b/include/yabar.h index d72851c..9f82d97 100644 --- a/include/yabar.h +++ b/include/yabar.h @@ -97,7 +97,7 @@ enum { #define NOT_INHERIT_BLK(blk) (((blk)->attr & BLKA_INHERIT)==0) #define CMONLEN 16 -#define YA_INTERNAL_LEN 4 +#define YA_INTERNAL_LEN 5 typedef struct ya_monitor ya_monitor_t; typedef struct ya_bar ya_bar_t; diff --git a/src/intern_blks/ya_intern.c b/src/intern_blks/ya_intern.c index 26760e3..5ac28bb 100644 --- a/src/intern_blks/ya_intern.c +++ b/src/intern_blks/ya_intern.c @@ -13,12 +13,14 @@ void ya_int_uptime(ya_block_t * blk); void ya_int_memory(ya_block_t * blk); void ya_int_thermal(ya_block_t *blk); void ya_int_brightness(ya_block_t *blk); +void ya_int_bandwidth(ya_block_t *blk); struct reserved_blk ya_reserved_blks[YA_INTERNAL_LEN] = { {"YA_INT_TIME", ya_int_time}, {"YA_INT_UPTIME", ya_int_uptime}, {"YA_INT_THERMAL", ya_int_thermal}, - {"YA_INT_BRIGHTNESS", ya_int_brightness} + {"YA_INT_BRIGHTNESS", ya_int_brightness}, + {"YA_INT_BANDWIDTH", ya_int_bandwidth} //{"YA_INT_MEMORY", ya_int_memory} }; @@ -88,6 +90,10 @@ void ya_int_thermal(ya_block_t *blk) { tfile = fopen(fpath, "r"); if (tfile == NULL) { //TODO handle and exit thread + fprintf(stderr, "Error opening file %s\n", fpath); + strncpy(blk->buf, "BLOCK ERROR!", strlen("BLOCK ERROR!")); + ya_draw_pango_text(blk); + pthread_exit(NULL); } fclose(tfile); while (1) { @@ -125,6 +131,10 @@ void ya_int_brightness(ya_block_t *blk) { tfile = fopen(fpath, "r"); if (tfile == NULL) { //TODO handle and exit thread + fprintf(stderr, "Error opening file %s\n", fpath); + strncpy(blk->buf, "BLOCK ERROR!", strlen("BLOCK ERROR!")); + ya_draw_pango_text(blk); + pthread_exit(NULL); } while(1) { tfile = fopen(fpath, "r"); @@ -136,4 +146,62 @@ void ya_int_brightness(ya_block_t *blk) { fclose(tfile); } } + +void ya_int_bandwidth(ya_block_t * blk) { + unsigned long rx, tx, orx, otx; + unsigned int rxrate, txrate; + FILE *rxfile, *txfile; + char rxpath[128]; + char txpath[128]; + char rxc, txc; + snprintf(rxpath, 128, "/sys/class/net/%s/statistics/rx_bytes", blk->internal->option[0]); + snprintf(txpath, 128, "/sys/class/net/%s/statistics/tx_bytes", blk->internal->option[0]); + rxfile = fopen(rxpath, "r"); + txfile = fopen(txpath, "r"); + if (rxfile == NULL || txfile == NULL) { + //TODO handle and exit thread + fprintf(stderr, "Error opening file %s or %s\n", rxpath, txpath); + strncpy(blk->buf, "BLOCK \nERROR!", strlen("BLOCK \nERROR!")); + ya_draw_pango_text(blk); + pthread_exit(NULL); + } + else { + fscanf(rxfile, "%lu", &orx); + fscanf(txfile, "%lu", &otx); + } + fclose(rxfile); + fclose(txfile); + while(1) { + txc = rxc = 'K'; + rxfile = fopen(rxpath, "r"); + txfile = fopen(txpath, "r"); + + fscanf(rxfile, "%lu", &rx); + fscanf(txfile, "%lu", &tx); + + rxrate = (rx - orx)/((blk->sleep)*1024); + txrate = (tx - otx)/((blk->sleep)*1024); + + if(rxrate > 1024) { + rxrate/=1024; + rxc = 'M'; + } + if(txrate > 1024) { + txrate/=1024; + txc = 'M'; + } + + + orx = rx; + otx = tx; + + snprintf(blk->buf, 40, "%4u%c %4u%c", rxrate, rxc, txrate, txc); + ya_draw_pango_text(blk); + fclose(rxfile); + fclose(txfile); + memset(blk->buf, '\0', 40); + sleep(blk->sleep); + } + +} #endif //YA_INTERNAL