diff options
| author | David Phillips <dbphillipsnz@gmail.com> | 2015-08-28 23:54:00 +1200 | 
|---|---|---|
| committer | David Phillips <dbphillipsnz@gmail.com> | 2015-08-29 00:54:30 +1200 | 
| commit | b71fdc1baa7aa1f6a8099e6c90929718e282b899 (patch) | |
| tree | 9cef0630db9c8af5ee1bb776c51abbe2a5e234e5 | |
| parent | b3e88dca9e4574b8aec8028a8edeca1df1d19843 (diff) | |
| download | paramano-b71fdc1baa7aa1f6a8099e6c90929718e282b899.tar.xz | |
Made tooltip texts cached, fixes GitHub issue #4
| -rw-r--r-- | bat_tray.c | 36 | ||||
| -rw-r--r-- | tray.c | 53 | ||||
| -rw-r--r-- | tray.h | 2 | 
3 files changed, 59 insertions, 32 deletions
@@ -20,6 +20,7 @@  #include "paramano.h"  static GtkStatusIcon* tray; +static char tooltip_text[1024];  int bat_num; // Shortcoming: we only detect one battery  char *CHARGE_VALUE_PATH, *CHARGE_STATE_PATH; @@ -103,17 +104,23 @@ long get_bat_seconds_left()  /*********************************************************************** - * Updates the battery tray tooltip text + * Shows/updates the battery tray tooltip   **********************************************************************/ -static gboolean update_tooltip(GtkStatusIcon* status_icon,gint x,gint y,gboolean keyboard_mode,GtkTooltip* tooltip,gpointer data) +static gboolean show_tooltip(GtkStatusIcon* status_icon, gint x, gint y, gboolean keyboard_mode, GtkTooltip* tooltip, gpointer data) +{ +	gtk_tooltip_set_text(tooltip, tooltip_text); +	return true; +} + +/*********************************************************************** + * Updates the battery tray tooltip's cached text + **********************************************************************/ +static void update_tooltip_cache()  {  	char* msg;  	int seconds_left = get_bat_seconds_left(); -  	char* time_left; -	fprintf(stderr, "%d seconds left on bat\n", seconds_left); -  	if (seconds_left == -1)  	{  		asprintf(&time_left, _("Unknown time left")); @@ -143,20 +150,19 @@ static gboolean update_tooltip(GtkStatusIcon* status_icon,gint x,gint y,gboolean  			asprintf(&msg, _("Unknown status") );  			break;  	} -	debug("Setting tooltip text to '%s'\n",msg); -	gtk_tooltip_set_text(tooltip, msg); +	debug("Setting cached tooltip text to '%s'\n",msg); +	strncpy(tooltip_text, msg, sizeof(tooltip_text));  	free(time_left);  	free(msg); - -	return true;  }  /***********************************************************************   * Updates the battery tray icon based upon battery percent + * Also updates the cached tooltip text   **********************************************************************/ -static gboolean update_icon(gpointer user_data) +static gboolean update()  {  	char *icon_file;  	unsigned int rounded; @@ -180,6 +186,8 @@ static gboolean update_icon(gpointer user_data)  			break;  	} + +	update_tooltip_cache();  	debug("Setting tray icon to '%s'\n",icon_file);  	gtk_status_icon_set_from_file(tray, icon_file); @@ -209,8 +217,12 @@ void bat_tray_init()  	gtk_status_icon_set_from_file(tray, icon_file);  	free(icon_file);  	gtk_status_icon_set_has_tooltip (tray, TRUE); -	g_signal_connect(G_OBJECT(tray), "query-tooltip", GTK_SIGNAL_FUNC(update_tooltip), NULL); -	g_timeout_add(5000, update_icon, NULL); +	g_signal_connect(G_OBJECT(tray), "query-tooltip", GTK_SIGNAL_FUNC(show_tooltip), NULL); +	g_timeout_add(5000, update, NULL); + +	/* Force something useful to be in the cached tooltip text, +	 * force meaningful icon */ +	update();  } @@ -19,11 +19,11 @@  #include "paramano.h" -GtkStatusIcon* tray; - -GtkWidget* menu; -GSList* menu_items; -GtkWidget* checked_menu_item; +static GtkStatusIcon* tray; +static char tooltip_text[1024]; +static GtkWidget* menu; +static GSList* menu_items; +static GtkWidget* checked_menu_item;  /*********************************************************************** @@ -166,9 +166,19 @@ static void tray_generate_menu()  }  /*********************************************************************** - * Refresh the tooltip message + * Tell gtk what the tooltip message should be   **********************************************************************/ -static gboolean update_tooltip(GtkStatusIcon* status_icon,gint x,gint y,gboolean keyboard_mode,GtkTooltip* tooltip,gpointer data) +static gboolean show_tooltip(GtkStatusIcon* status_icon, gint x, gint y, gboolean keyboard_mode,GtkTooltip* tooltip, gpointer data) +{ +	gtk_tooltip_set_text(tooltip, tooltip_text); +	return true; +} + + +/*********************************************************************** + * Work out what the tooltip message would be now, store it away + **********************************************************************/ +static void update_tooltip_cache()  {  	char *msg, *label;  	char current_governor[20]; // TO DO @@ -188,12 +198,10 @@ static gboolean update_tooltip(GtkStatusIcon* status_icon,gint x,gint y,gboolean  		free(label);  	} -	debug("Setting tooltip text\n"); -	gtk_tooltip_set_text(tooltip, msg); +	strncpy(tooltip_text, msg, sizeof(tooltip_text)); +	tooltip_text[sizeof(tooltip_text)] = '\0';  	free(msg); - -	return TRUE;  } @@ -211,7 +219,7 @@ static void popup_menu(GtkStatusIcon* statuc_icon,guint button,guint activate_ti  /**********************************************************************   * Set icon based on current freq/governor   **********************************************************************/ -static void tray_update_icon_percent() +static void update_icon()  {  	char* file;  	int max_frequency = gf_freqi(0, 0); @@ -224,7 +232,7 @@ static void tray_update_icon_percent()  		adjusted_percent = 0;  	} else {  		// Percentages need to be {25,50,75,100}. Round to one of these numbers. -		// TO DO: round instead of lots of ifs +		// TO DO: round/truncate instead of lots of ifs  		percent = (gf_current(0) * 100)/max_frequency;  		if(percent == 100) {  			adjusted_percent = 100; @@ -250,8 +258,9 @@ static void tray_update_icon_percent()  /***********************************************************************   * Update the freq/gov tray icon + * Also updates the cached tooltip text   **********************************************************************/ -static gboolean update_icon(gpointer user_data) +static gboolean update()  {  	unsigned int i;  	switch ( get_battery_state() ) @@ -278,8 +287,9 @@ static gboolean update_icon(gpointer user_data)  	}  	debug("Updating icon\n"); -	tray_update_icon_percent(); -	return TRUE; +	update_tooltip_cache(); +	update_icon(); +	return true;  } @@ -318,16 +328,23 @@ void tray_init()  	tray = gtk_status_icon_new();  	char* icon_file = g_strconcat(DEFAULT_THEME, "/cpu-0.png", NULL); +	/* Force something useful to be in the cached tooltip text */ +	update_tooltip_cache(); +  	debug("Setting icon to '%s'\n",icon_file);  	gtk_status_icon_set_from_file(tray, icon_file);  	gtk_status_icon_set_has_tooltip(tray, TRUE);  	debug("Setting up callbacks\n"); -	g_signal_connect(G_OBJECT(tray), "query-tooltip", GTK_SIGNAL_FUNC(update_tooltip), NULL); +	g_signal_connect(G_OBJECT(tray), "query-tooltip", GTK_SIGNAL_FUNC(show_tooltip), NULL);  	g_signal_connect(G_OBJECT(tray), "popup-menu", GTK_SIGNAL_FUNC(popup_menu), NULL);  	debug("Adding timeout\n"); -	g_timeout_add(1000, update_icon, NULL); +	g_timeout_add(1000, update, NULL); + +	/* Force meaningful tooltip cached text and force meaningful icon */ +	update(); +  	tray_init_menu();  } @@ -21,8 +21,6 @@  void tray_set_defaults();  void tray_init(); -void tray_set_tooltip(const char* msg); -//void tray_update_icon_percent();  void tray_show();  void tray_hide();  bool tray_visible();  | 
