diff options
| author | Federico Angelilli <code@fedang.net> | 2023-11-26 16:05:28 +0100 |
|---|---|---|
| committer | Federico Angelilli <code@fedang.net> | 2023-11-26 16:05:28 +0100 |
| commit | 5a8c1c34a0a739a01853406b737cc7497884ed7a (patch) | |
| tree | 0495b28a4a326beb16e1a224ced73a4c90060afe /src/comet.c | |
| parent | e9bbe8178ab45cb9880c9b6b0324e9b4467f26d5 (diff) | |
Add cpu temperature button
Diffstat (limited to 'src/comet.c')
| -rw-r--r-- | src/comet.c | 39 |
1 files changed, 37 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); |
