aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFederico Angelilli <code@fedang.net>2023-11-25 12:36:10 +0100
committerFederico Angelilli <code@fedang.net>2023-11-25 12:36:10 +0100
commit14b4c8084c14ba7a6e34cafb6c9a22a330f57644 (patch)
tree2da424c8ee3cebff28ddc3282b4eccf2bc920548 /src
parentc5a5da12c6823114b1e3cf817b73189172ff033b (diff)
Add cpu percentage button
Diffstat (limited to 'src')
-rw-r--r--src/comet.c62
1 files changed, 56 insertions, 6 deletions
diff --git a/src/comet.c b/src/comet.c
index a94bd54..fd393bc 100644
--- a/src/comet.c
+++ b/src/comet.c
@@ -54,6 +54,47 @@ static gboolean disk_update(gpointer data)
return G_SOURCE_CONTINUE;
}
+// Taken from slstatus
+char *cpu_percentage(void)
+{
+ static long double a[7];
+ long double b[7], sum;
+ memcpy(b, a, sizeof(b));
+
+ FILE *stat = fopen("/proc/stat", "rb");
+ /* cpu user nice system idle iowait irq softirq */
+ g_assert(7 == fscanf(stat, "%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf",
+ &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6]));
+
+ fclose(stat);
+ if (b[0] == 0) return NULL;
+
+ sum = (b[0] + b[1] + b[2] + b[3] + b[4] + b[5] + b[6]) -
+ (a[0] + a[1] + a[2] + a[3] + a[4] + a[5] + a[6]);
+
+ if (sum == 0) return NULL;
+
+ return g_strdup_printf(" %d%%", (int)(100 *
+ ((b[0] + b[1] + b[2] + b[5] + b[6]) -
+ (a[0] + a[1] + a[2] + a[5] + a[6])) / sum));
+}
+
+static gboolean cpu_update(gpointer data)
+{
+ Button *btn = data;
+
+ char *perc = cpu_percentage();
+ // Don't update on error
+ if (perc != NULL) {
+ g_free(btn->text);
+ btn->text = perc;
+ state_redraw(btn->action_data);
+ }
+
+ log_debug("Updated cpu percentage");
+ return G_SOURCE_CONTINUE;
+}
+
static gboolean ram_update(gpointer data)
{
Button *btn = data;
@@ -69,6 +110,7 @@ static gboolean ram_update(gpointer data)
"Cached: %ju kB\n",
&total, &unused, &buffers, &buffers, &cached));
+ fclose(meminfo);
g_free(btn->text);
btn->text = g_strdup_printf(" %d%%", (int)(100 * (total - unused - buffers - cached) / total));
@@ -154,13 +196,13 @@ int main(int argc, char **argv)
state_add_button(state, btn);
}
- Button *disk_btn = button_create("disk", PANGO_ALIGN_RIGHT);
- button_set_colors(disk_btn, background, foreground, stroke);
- button_set_action(disk_btn, show_action, state);
- state_add_button(state, disk_btn);
+ Button *cpu_btn = button_create("cpu", PANGO_ALIGN_RIGHT);
+ button_set_colors(cpu_btn, background, foreground, stroke);
+ button_set_action(cpu_btn, show_action, state);
+ state_add_button(state, cpu_btn);
- disk_update(disk_btn);
- g_timeout_add(60 * 1000, disk_update, disk_btn);
+ cpu_update(cpu_btn);
+ g_timeout_add(1000, cpu_update, cpu_btn);
Button *ram_btn = button_create("ram", PANGO_ALIGN_RIGHT);
button_set_colors(ram_btn, background, foreground, stroke);
@@ -170,6 +212,14 @@ int main(int argc, char **argv)
ram_update(ram_btn);
g_timeout_add(60 * 1000, ram_update, ram_btn);
+ Button *disk_btn = button_create("disk", PANGO_ALIGN_RIGHT);
+ button_set_colors(disk_btn, background, foreground, stroke);
+ button_set_action(disk_btn, show_action, state);
+ state_add_button(state, disk_btn);
+
+ disk_update(disk_btn);
+ g_timeout_add(60 * 1000, disk_update, disk_btn);
+
struct {
State *state;
timer_t timer;