added cpu usage to statline

makefile now has configurable options
This commit is contained in:
shockrahwow 2019-09-03 11:39:59 -07:00
parent 8f5152206b
commit c3917e34b1
2 changed files with 46 additions and 6 deletions

View File

@ -1,11 +1,19 @@
# Here are the configuraable options
# LAPTOP : enable batter level
# CPU_USAGE : CPU usage in percentage
# DEBUG : for fixing issues
# NOTE: the -D is needed for gcc
# MFLAGS= -D MY_OPTION -D ANOTHER_OPT ...
MFLAGS=-D LAPTOP -D CPU_USAGE
flags=-O2 -s -lX11 flags=-O2 -s -lX11
output=statline output=statline
stat:
gcc -o $(output) status.c $(flags)
laptop:
# compiling with LAPTOP=1 stat:
gcc -D LAPTOP=1 status.c -o $(output) $(flags) gcc $(MFLAGS) -o $(output) status.c $(flags)
run: run:
./statline ./statline

View File

@ -20,6 +20,7 @@
// Time in seconds // Time in seconds
#define TIME_DELAY 5 #define TIME_DELAY 5
#define MAXSTR 1024 #define MAXSTR 1024
#define SMALL_BUF 32
#define MEM_BREAD_SIZE 32 #define MEM_BREAD_SIZE 32
#define STD_IO #define STD_IO
@ -91,11 +92,39 @@ battery_percentage(void)
#endif #endif
#ifdef CPU_USAGE
// stat(2) ->
#define CPU_PROC_BASE
#define TOTAL "head /proc/stat -n 1 | cut -c 6- | sed 's/ /\\+/g' | bc"
#define IDLE "head /proc/stat -n 1 | cut -c 6- | awk '{print $4}'"
static char* static char*
cpu_usage(void) cpu_usage(void)
{ {
return "CPU Usage Placeholder"; // NOTE: not accounting for the query so this may be somewhat off by some metric
// Grabbing the total usage
FILE* query_p;
static char buf[SMALL_BUF];
unsigned total, idle;
// get the total time
query_p = popen(TOTAL, "r");
fgets(buf, sizeof(buf)-1, query_p);
total = atoi(buf);
pclose(query_p);
// get the idle time
query_p = popen(IDLE, "r");
fgets(buf, sizeof(buf)-1, query_p);
idle = atoi(buf);
pclose(query_p);
double usage = 1.00 - ((double)idle / (double)total);
#ifdef DEBUG
printf("IDLE/TOTAL = %.02f\n", usage);
#endif
sprintf(buf, "CPU: %.02f % ", usage);
return buf;
} }
#endif
static void static void
XSetRoot(char* name) XSetRoot(char* name)
@ -122,6 +151,9 @@ main(void)
date_time, date_time,
#ifdef LAPTOP #ifdef LAPTOP
battery_percentage, battery_percentage,
#endif
#ifdef CPU_USAGE
cpu_usage,
#endif #endif
}; };