From b3ece5a5554a242522799864ef4714513d4bcf97 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Sun, 3 Mar 2019 16:40:09 +1300 Subject: Re-add debug macro, short circuit icon logic --- bat_tray.c | 23 +++++++++++++++-------- common.c | 2 +- common.h | 14 +++++++++++++- tray.c | 16 +++++++++++++--- 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/bat_tray.c b/bat_tray.c index e4f7c39..493900a 100644 --- a/bat_tray.c +++ b/bat_tray.c @@ -9,7 +9,8 @@ static GtkStatusIcon* tray; static char tooltip_text[1024]; -static int old_rounded; +static int old_rounded = -1; +static int old_battery_state = -1; static int bat_num; // Shortcoming: we only detect one battery static char CHARGE_VALUE_PATH[1024]; static char CHARGE_STATE_PATH[1024]; @@ -161,26 +162,35 @@ static gboolean update() { char icon_file[1024]; int rounded = 0; + int battery_state = 0; update_tooltip_cache(); - /* if rounded hasn't changed, don't spend time changing icon */ + /* if charge and status hasn't changed, don't spend time changing icon */ + battery_state = get_battery_state(); rounded = get_bat_percent_rounded(); - if (rounded == old_rounded) { + if (rounded == old_rounded && battery_state == old_battery_state) { + debug("Short-circuiting\n"); return true; } - switch (get_battery_state()) + old_rounded = rounded; + old_battery_state = battery_state; + + switch (battery_state) { case STATE_DISCHARGING: + debug("discharging\n"); snprintf(icon_file, sizeof(icon_file), "%s/bat-%d.png", DEFAULT_THEME, rounded); break; case STATE_CHARGING: + debug("charging\n"); snprintf(icon_file, sizeof(icon_file), "%s/bat-%d-charging.png", DEFAULT_THEME, rounded); break; default: + debug("unknown/charged\n"); snprintf(icon_file, sizeof(icon_file), "%s/bat-charged.png", DEFAULT_THEME); break; } @@ -209,10 +219,7 @@ void bat_tray_init() gtk_status_icon_set_from_file(tray, icon_file); gtk_status_icon_set_has_tooltip (tray, TRUE); g_signal_connect(G_OBJECT(tray), "query-tooltip", G_CALLBACK(show_tooltip), NULL); - g_timeout_add(10000, update, NULL); - - /* trigger icon refresh in update() below */ - old_rounded = get_bat_percent_rounded() - 1; + g_timeout_add(BAT_TRAY_UPDATE_INTERVAL, update, NULL); /* pre-load cached tooltip text */ update(); diff --git a/common.c b/common.c index bb8681a..0e76a85 100644 --- a/common.c +++ b/common.c @@ -26,7 +26,7 @@ int vget_int_value_from_filef(const char* format, va_list args) { char filename[1024]; if (vsnprintf(filename, sizeof(filename), format, args) == sizeof(filename)) - fprintf(stderr, "WARN: filename buffer too small"); + info("Error: filename buffer too small"); return get_int_value_from_file(filename); } diff --git a/common.h b/common.h index ff8c8a8..6b2e92d 100644 --- a/common.h +++ b/common.h @@ -12,5 +12,17 @@ int get_int(const char* string); #define STR_LINE STRING(__LINE__) /* */ -#define info(...) printf("INFO: "__FILE__":"STR_LINE" --- "__VA_ARGS__) +#define info(format, ...) printf("INFO : "__FILE__"@%s():"STR_LINE": "format, __func__ __VA_OPT__(,) __VA_ARGS__) + +#ifdef DEBUG +#define debug(format, ...) fprintf(stderr, "DEBUG: "__FILE__"@%s():"STR_LINE": "format, __func__ __VA_OPT__(,) __VA_ARGS__) +#else +#define debug(...) +#endif + +#define debug_entry() debug("entry\n") +#define debug_exit() debug("exit\n") + #define FILE_PATH_SIZE 2048 +#define CPU_TRAY_UPDATE_INTERVAL 2000 +#define BAT_TRAY_UPDATE_INTERVAL 10000 diff --git a/tray.c b/tray.c index 3a5ea32..8f1ce17 100644 --- a/tray.c +++ b/tray.c @@ -1,4 +1,5 @@ #include "tray.h" +#include "common.h" #include "getcore.h" #include "getfreq.h" #include "getgov.h" @@ -17,6 +18,7 @@ static char tooltip_text[1024]; static GtkWidget* menu; static GSList* menu_items; static GtkWidget* checked_menu_item; +static old_adjusted_percent = -1; /*********************************************************************** @@ -213,7 +215,15 @@ static void update_icon() } } + if (adjusted_percent == old_adjusted_percent) { + debug("Unchanged, short circuiting\n"); + return; + } + + old_adjusted_percent = adjusted_percent; + snprintf(file, sizeof(file), "%s/cpu-%d.png", DEFAULT_THEME,adjusted_percent); + debug("Set icon to %s\n", file); gtk_status_icon_set_from_file(tray, file); } @@ -240,7 +250,7 @@ static gboolean update() switch ( now_bat_state ) { case STATE_DISCHARGING: - fprintf(stderr, "discharging\n"); + debug("discharging\n"); if (DEFAULT_BAT_GOV) { for (i = 0; i < gc_number(); i++) @@ -250,7 +260,7 @@ static gboolean update() case STATE_CHARGING: case STATE_FULL: - fprintf(stderr, "ac power\n"); + debug("ac power\n"); if (DEFAULT_AC_GOV) { for (i = 0; i < gc_number(); i++) @@ -306,7 +316,7 @@ void tray_init() g_signal_connect(G_OBJECT(tray), "query-tooltip", G_CALLBACK(show_tooltip), NULL); g_signal_connect(G_OBJECT(tray), "popup-menu", G_CALLBACK(popup_menu), NULL); - g_timeout_add(1000, update, NULL); + g_timeout_add(CPU_TRAY_UPDATE_INTERVAL, update, NULL); /* Force meaningful tooltip cached text and force meaningful icon */ update(); -- cgit v1.1