diff options
Diffstat (limited to 'status/cpu.c')
-rw-r--r-- | status/cpu.c | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/status/cpu.c b/status/cpu.c new file mode 100644 index 0000000..5f203ee --- /dev/null +++ b/status/cpu.c @@ -0,0 +1,77 @@ +#include "kconfig.h" +#include "dwmstatus.h" + +static int prev_total = 0; +static int prev_idle = 0; + +int print_cpu( bbuf *buf ) +{ + ASSERT( buf != NULL ); + + + int str_size=0; + int curr_user = 0, curr_nice = 0, curr_system = 0, curr_idle = 0, curr_total; + int diff_idle, diff_total, diff_usage; + + if ( buf->size < 1 ) + { + buf->size = 1024; + buf->str = malloc( buf->size ); + } + + FILE *f = fopen( "/proc/stat", "r" ); + ASSERT(f != NULL ); + + fscanf( f, "cpu %d %d %d %d", &curr_user, &curr_nice, &curr_system, &curr_idle ); + fclose( f ); + + ASSERT( curr_user > 0 ); + ASSERT( curr_nice > 0 ); + ASSERT( curr_system > 0 ); + ASSERT( curr_idle > 0 ); + + //printf("%d %d %d %d\n", curr_user, curr_nice, curr_system, curr_idle ); + + curr_total = curr_user + curr_nice + curr_system + curr_idle; + diff_idle = curr_idle - prev_idle; + diff_total = curr_total - prev_total; + diff_usage = (diff_total ? (1000 * (diff_total - diff_idle)/diff_total + 5)/10 : 0); + prev_total = curr_total; + prev_idle = curr_idle; + +#ifdef CONFIG_STATUS_UTF8 + //chip f64f, //ef ac 99 + buf->str[0] = 0xef; + buf->str[1] = 0xac; + buf->str[2] = 0x99; + str_size = 3; + printf("asd1 [%s]\n", buf->str); +#endif + +#ifdef CONFIG_STATUS_FMT_PANGO + //seperate by colors + // COLD - BLUE - < 20% + // WARM - GREEN 20-80% + // HOT - RED >80% + if ( diff_usage < 20 ) + { + snprintf( buf->str, buf->size-1, "CPU:<span color=\"#3366ff\">%02d%%</span> ", diff_usage ); + } else if( diff_usage > 80 ) + { + snprintf( buf->str, buf->size-1, "CPU:<span color=\"#ff6633\">%02d%%</span> ", diff_usage ); + } else + { + snprintf( buf->str, buf->size-1, "CPU:<span color=\"#33ff66\">%02d%%</span> ", diff_usage ); + } +#else + #ifdef CONFIG_STATUS_UTF8 + snprintf( &buf->str[str_size], buf->size-1-str_size, "%02d%% ", diff_usage ); + #else + snprintf( &buf->str[str_size], buf->size-1-str_size, "CPU:%02d%% ", diff_usage ); + #endif + +#endif + + return 0; +} + |