#include #include #include #include #include #include #include #include "window.h" #include "log.h" #include "draw.h" #include "connect.h" #include "state.h" #define EVEN(n) ((int)(n) - ((int)(n) % 2 != 0)) static void log_handler(const char *log_domain, GLogLevelFlags level, const char *message, gpointer log_level) { GLogLevelFlags message_level = level & G_LOG_LEVEL_MASK; if ((GLogLevelFlags)log_level < message_level) return; if (message_level <= G_LOG_LEVEL_WARNING) g_printerr("%s\n", message); else g_print("%s\n", message); } static gboolean mainloop_quit(gpointer data) { g_main_loop_quit(data); return G_SOURCE_CONTINUE; } static gboolean disk_update(gpointer data) { Button *btn = data; struct statvfs buffer; g_assert(statvfs("/", &buffer) == 0); const double used = 1.0 - ((double)buffer.f_bavail / (double)buffer.f_blocks); size_t len1 = g_utf8_strlen(btn->text, -1); g_free(btn->text); btn->text = g_strdup_printf(" %d%%", (int)round(used * 100.0)); size_t len2 = g_utf8_strlen(btn->text, -1); log_debug("Updated disk percentage"); state_redraw(btn->action_data, len1 != len2); 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) { static long double a[7]; long double b[7], sum; 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])); 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) { size_t len1 = g_utf8_strlen(btn->text, -1); g_free(btn->text); btn->text = perc; size_t len2 = g_utf8_strlen(btn->text, -1); state_redraw(btn->action_data, len1 != len2); } log_debug("Updated cpu percentage"); return G_SOURCE_CONTINUE; } 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, "MemTotal: %ju kB\n" "MemFree: %ju kB\n" "MemAvailable: %ju kB\n" "Buffers: %ju kB\n" "Cached: %ju kB\n", &total, &unused, &buffers, &buffers, &cached)); fclose(meminfo); size_t len1 = g_utf8_strlen(btn->text, -1); g_free(btn->text); btn->text = g_strdup_printf(" %d%%", (int)(100 * (total - unused - buffers - cached) / total)); size_t len2 = g_utf8_strlen(btn->text, -1); log_debug("Updated ram percentage"); state_redraw(btn->action_data, len1 != len2); return G_SOURCE_CONTINUE; } static gboolean date_update(gpointer data) { Button *btn = data; size_t len1 = g_utf8_strlen(btn->text, -1); g_free(btn->text); GDateTime *time = g_date_time_new_now_local(); char *date = g_date_time_format(time, "%A %d %H:%M"); g_assert(date != NULL); btn->text = date; size_t len2 = g_utf8_strlen(btn->text, -1); struct { State *state; timer_t timer; } *date_ctx = btn->action_data; log_debug("Updated date and time"); state_redraw(date_ctx->state, len1 != len2); struct timespec current; clock_gettime(CLOCK_REALTIME, ¤t); struct itimerspec its = { 0 }; its.it_value.tv_sec = 60 - (current.tv_sec % 60); timer_settime(date_ctx->timer, 0, &its, NULL); return G_SOURCE_CONTINUE; } static void show_action(Button *btn) { log_info("Called action: %s", btn->text); } static void quit_action(Button *btn) { log_info("Quit button pressed"); g_main_loop_quit(btn->action_data); } int main(int argc, char **argv) { setlocale(LC_CTYPE, ""); g_log_set_default_handler(log_handler, (gpointer)G_LOG_LEVEL_DEBUG); GMainLoop *mainloop = g_main_loop_new(NULL, FALSE); Connection *con = connect_create(); Window *win = window_create(con); int screen_width = con->screen_size->width; int screen_height = con->screen_size->height; int height = EVEN(round(screen_height * 0.021)); int x_padding = EVEN(round(screen_width * 0.005)); int y_padding = EVEN(round(screen_height * 0.004)); log_debug("Calculated dimensions [height=%d, x_pad=%d, y_pad=%d]", height, x_padding, y_padding); Color background_all = { 0.3, 0.3, 0.3, 1 }; Drawable *draw = draw_create("Hack 13 Bold", height, x_padding, x_padding, y_padding, 0); draw_set_background(draw, background_all); State *state = state_create(win, draw); Color background = { 0.4, 0.4, 0.4, 1 }; Color foreground = { 0.8, 0.8, 0.8, 1 }; Color stroke = { 0.8, 0.8, 0.8, 1 }; for (int i = 0; i < 9; ++i) { char text[] = { '1' + i, '\0' }; Button *btn = button_create(text, PANGO_ALIGN_LEFT); button_set_colors(btn, background, foreground, stroke); button_set_action(btn, show_action, NULL); state_add_button(state, 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); 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(10 * 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; } date_ctx = { state, 0 }; Button *date_btn = button_create("date", PANGO_ALIGN_CENTER); button_set_colors(date_btn, background, foreground, stroke); button_set_action(date_btn, show_action, &date_ctx); state_add_button(state, date_btn); struct sigevent sev = { 0 }; sev.sigev_notify = SIGEV_SIGNAL; sev.sigev_signo = SIGUSR1; g_assert(timer_create(CLOCK_REALTIME, &sev, &date_ctx.timer) == 0); date_update(date_btn); // purple Color background2 = { 0.502, 0.168, 0.886, 1 }; 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); connect_attach_state(con, state); connect_attach_source(con); guint source_alrm = g_unix_signal_add(SIGUSR1, date_update, date_btn); guint source_term = g_unix_signal_add(SIGTERM, mainloop_quit, mainloop); guint source_int = g_unix_signal_add(SIGINT, mainloop_quit, mainloop); state_redraw(state, true); log_debug("Starting main loop"); g_main_loop_run(mainloop); log_debug("Cleaning main loop"); g_clear_pointer(&mainloop, g_main_loop_unref); g_source_remove(source_alrm); g_source_remove(source_term); g_source_remove(source_int); timer_delete(date_ctx.timer); state_destroy(state); draw_destroy(draw); window_destroy(win); return 0; } // vim: ts=4 sw=4 et