From 5a8c1c34a0a739a01853406b737cc7497884ed7a Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Sun, 26 Nov 2023 16:05:28 +0100 Subject: Add cpu temperature button --- src/comet.c | 39 +++++++++++++++++++++++++++++++++++++-- src/connect.c | 3 +++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/comet.c b/src/comet.c index 45de1f6..4b28a71 100644 --- a/src/comet.c +++ b/src/comet.c @@ -57,6 +57,30 @@ static gboolean disk_update(gpointer data) return G_SOURCE_CONTINUE; } +static gboolean temp_update(gpointer data) +{ + Button *btn = data; + + FILE *temp = fopen("/sys/class/thermal/thermal_zone2/temp", "rb"); + g_assert_nonnull(temp); + + uintmax_t value; + g_assert(1 == fscanf(temp, "%ju\n", &value)); + + fclose(temp); + + size_t len1 = g_utf8_strlen(btn->text, -1); + + g_free(btn->text); + btn->text = g_strdup_printf(" %d °C", (int)(value / 1000)); + + size_t len2 = g_utf8_strlen(btn->text, -1); + + log_debug("Updated temperature"); + state_redraw(btn->action_data, len1 != len2); + return G_SOURCE_CONTINUE; +} + // Taken from slstatus char *cpu_percentage(void) { @@ -65,6 +89,8 @@ char *cpu_percentage(void) memcpy(b, a, sizeof(b)); FILE *stat = fopen("/proc/stat", "rb"); + g_assert_nonnull(stat); + /* 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])); @@ -107,6 +133,7 @@ static gboolean ram_update(gpointer data) Button *btn = data; FILE *meminfo = fopen("/proc/meminfo", "rb"); + g_assert_nonnull(meminfo); uintmax_t total, unused, buffers, cached; g_assert(5 == fscanf(meminfo, @@ -219,13 +246,21 @@ int main(int argc, char **argv) cpu_update(cpu_btn); g_timeout_add(1000, cpu_update, cpu_btn); + Button *temp_btn = button_create("temp", PANGO_ALIGN_RIGHT); + button_set_colors(temp_btn, background, foreground, stroke); + button_set_action(temp_btn, show_action, state); + state_add_button(state, temp_btn); + + temp_update(temp_btn); + g_timeout_add(20 * 1000, temp_update, temp_btn); + Button *ram_btn = button_create("ram", PANGO_ALIGN_RIGHT); button_set_colors(ram_btn, background, foreground, stroke); button_set_action(ram_btn, show_action, state); state_add_button(state, ram_btn); ram_update(ram_btn); - g_timeout_add(60 * 1000, ram_update, ram_btn); + g_timeout_add(10 * 1000, ram_update, ram_btn); Button *disk_btn = button_create("disk", PANGO_ALIGN_RIGHT); button_set_colors(disk_btn, background, foreground, stroke); @@ -254,7 +289,7 @@ int main(int argc, char **argv) // purple Color background2 = { 0.502, 0.168, 0.886, 1 }; - Button *q_btn = button_create("Q", PANGO_ALIGN_RIGHT); + Button *q_btn = button_create("", PANGO_ALIGN_RIGHT); button_set_colors(q_btn, background2, foreground, stroke); button_set_action(q_btn, quit_action, mainloop); state_add_button(state, q_btn); diff --git a/src/connect.c b/src/connect.c index fa19ffb..b3e4e92 100644 --- a/src/connect.c +++ b/src/connect.c @@ -48,11 +48,13 @@ static void update_scale(Connection *con) } } +// Check if point (px, py) is inside a rectangle in (x, y), (x+w, y), (x, y+h) and (w+h, y+h) static inline bool in_rect(int px, int py, int x, int y, int w, int h) { return px >= x && px <= x + w && py >= y && py <= y + h; } +// Check if point (px, py) is inside a circle of radius r and center (x, y) static inline bool in_circle(int px, int py, int x, int y, int r) { int dx = x - px; @@ -60,6 +62,7 @@ static inline bool in_circle(int px, int py, int x, int y, int r) return (dx * dx + dy * dy) <= r * r; } +// Check if point (px, py) is inside a capsule in (x, y), (x+w, y), (x, y+h) and (w+h, y+h) static inline bool in_capsule(int px, int py, int x, int y, int w, int h) { g_assert(w >= h); -- cgit v1.2.3