aboutsummaryrefslogtreecommitdiff
path: root/cpu.c
diff options
context:
space:
mode:
Diffstat (limited to 'cpu.c')
-rw-r--r--cpu.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/cpu.c b/cpu.c
new file mode 100644
index 0000000..0d53461
--- /dev/null
+++ b/cpu.c
@@ -0,0 +1,38 @@
+#include <stdio.h>
+#include "block.h"
+#define ICON_CPU " "
+FILE *proc_stat;
+int stat[8], cpu_delay = 6;
+double prev_idle, prev_total, total, idle, usage_p;
+char usage_str[11];
+void cpu(void) {
+ if (cpu_delay++ < 6) goto show_stat;
+ else cpu_delay = 0;
+ prev_idle = idle;
+ prev_total = total;
+ if (!proc_stat) proc_stat = fopen("/proc/stat","r");
+ if (proc_stat) {
+ int c, i;
+ fseek(proc_stat,5,0);
+ for (i = 0;i < 8;i++) stat[i] = 0;
+ for (i = 0; i < 8;) {
+ c = fgetc(proc_stat);
+ if (c != ' ')
+ stat[i] = 10 * stat[i] + (c - '0');
+ else i++;
+ }
+ total = stat[0]+stat[1]+stat[2];
+ idle = stat[3]+stat[4];
+ total += stat[5]+stat[6]+stat[7];
+ }
+ usage_p = (idle + total) - (prev_idle + prev_total);
+ usage_p -= (idle - prev_idle);
+ usage_p *= 100;
+ usage_p /= (idle + total) - (prev_idle + prev_total);
+ snprintf(usage_str,11,ICON_CPU"%02.2f%%",usage_p);
+show_stat:
+ if (usage_p < 60)
+ block(usage_str,"#222222","#acae3299","#acae3299");
+ else
+ block(usage_str,"#222222","#ea544399","#ea544399");
+}