From 507ea5746c277fea83984c551ffd9e8c445effb5 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 17 Apr 2016 23:57:05 +0100 Subject: [PATCH 1/4] Grouped tray icon initialisation code --- src/profanity.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/profanity.c b/src/profanity.c index 4ca649cd..79d13c08 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -103,14 +103,6 @@ static gboolean gtk_ready = FALSE; void prof_run(char *log_level, char *account_name) { -#ifdef HAVE_GTK - gtk_ready = gtk_init_check(0, NULL); - log_debug("Env is GTK-ready: %s", gtk_ready ? "true" : "false"); - if (gtk_ready) { - gtk_init(0, NULL); - gtk_main_iteration_do(FALSE); - } -#endif _init(log_level); plugins_on_start(); _connect_default(account_name); @@ -373,9 +365,15 @@ _init(char *log_level) atexit(_shutdown); plugins_init(); #ifdef HAVE_GTK - if (gtk_ready && prefs_get_boolean(PREF_TRAY)) { - log_debug("Building GTK icon"); - create_tray(); + gtk_ready = gtk_init_check(0, NULL); + log_debug("Env is GTK-ready: %s", gtk_ready ? "true" : "false"); + if (gtk_ready) { + gtk_init(0, NULL); + gtk_main_iteration_do(FALSE); + if (prefs_get_boolean(PREF_TRAY)) { + log_debug("Building GTK icon"); + create_tray(); + } } #endif inp_nonblocking(TRUE); From de65f505a80ab2cee79fbcc01d24c9eed01691e1 Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 18 Apr 2016 00:07:15 +0100 Subject: [PATCH 2/4] Moved gtk specific code to tray.c --- src/profanity.c | 23 +++-------------------- src/tray.c | 35 +++++++++++++++++++++++++++++++++++ src/tray.h | 4 ++++ 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/src/profanity.c b/src/profanity.c index 79d13c08..3c508dcd 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -38,7 +38,6 @@ #endif #ifdef HAVE_GTK -#include #include "tray.h" #endif #include @@ -96,9 +95,6 @@ char *saved_status; static gboolean cont = TRUE; static gboolean force_quit = FALSE; -#ifdef HAVE_GTK -static gboolean gtk_ready = FALSE; -#endif void prof_run(char *log_level, char *account_name) @@ -138,9 +134,7 @@ prof_run(char *log_level, char *account_name) iq_autoping_check(); ui_update(); #ifdef HAVE_GTK - if (gtk_ready) { - gtk_main_iteration_do(FALSE); - } + tray_update(); #endif } } @@ -365,16 +359,7 @@ _init(char *log_level) atexit(_shutdown); plugins_init(); #ifdef HAVE_GTK - gtk_ready = gtk_init_check(0, NULL); - log_debug("Env is GTK-ready: %s", gtk_ready ? "true" : "false"); - if (gtk_ready) { - gtk_init(0, NULL); - gtk_main_iteration_do(FALSE); - if (prefs_get_boolean(PREF_TRAY)) { - log_debug("Building GTK icon"); - create_tray(); - } - } + tray_init(); #endif inp_nonblocking(TRUE); } @@ -395,9 +380,7 @@ _shutdown(void) cl_ev_disconnect(); } #ifdef HAVE_GTK - if (gtk_ready && prefs_get_boolean(PREF_TRAY)) { - destroy_tray(); - } + tray_close(); #endif jabber_shutdown(); plugins_on_shutdown(); diff --git a/src/tray.c b/src/tray.c index 98039977..7c731313 100644 --- a/src/tray.c +++ b/src/tray.c @@ -42,7 +42,9 @@ #include "tray.h" #include "window_list.h" #include "log.h" +#include "config/preferences.h" +static gboolean gtk_ready = FALSE; static GtkStatusIcon *prof_tray = NULL; static GString *icon_filename = NULL; static GString *icon_msg_filename = NULL; @@ -140,6 +142,39 @@ _tray_change_icon(gpointer data) /* }}} */ /* {{{ Public */ +void +tray_init(void) +{ + gtk_ready = gtk_init_check(0, NULL); + log_debug("Env is GTK-ready: %s", gtk_ready ? "true" : "false"); + if (!gtk_ready) { + return; + } + + gtk_init(0, NULL); + gtk_main_iteration_do(FALSE); + if (prefs_get_boolean(PREF_TRAY)) { + log_debug("Building GTK icon"); + create_tray(); + } +} + +void +tray_update(void) +{ + if (gtk_ready) { + gtk_main_iteration_do(FALSE); + } +} + +void +tray_close(void) +{ + if (gtk_ready && prefs_get_boolean(PREF_TRAY)) { + destroy_tray(); + } +} + void create_tray(void) { diff --git a/src/tray.h b/src/tray.h index 416ca7d2..98cac490 100644 --- a/src/tray.h +++ b/src/tray.h @@ -35,6 +35,10 @@ #ifndef PROFANITY_TRAY_H #define PROFANITY_TRAY_H +void tray_init(void); +void tray_update(void); +void tray_close(void); + /* * Create tray icon * From 12727744ab31b28eaec2f148f2ed22cdc7338a99 Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 18 Apr 2016 00:26:17 +0100 Subject: [PATCH 3/4] Load/destroy tray icons on init/shutdown --- src/command/commands.c | 4 ++-- src/profanity.c | 2 +- src/tray.c | 16 ++++++++-------- src/tray.h | 6 +++--- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/command/commands.c b/src/command/commands.c index e1eba540..50877227 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -5546,9 +5546,9 @@ cmd_tray(ProfWin *window, const char *const command, gchar **args) gboolean new = prefs_get_boolean(PREF_TRAY); if (old != new) { if (new) { - create_tray(); + tray_enable(); } else { - destroy_tray(); + tray_disable(); } } return TRUE; diff --git a/src/profanity.c b/src/profanity.c index 3c508dcd..1140594f 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -380,7 +380,7 @@ _shutdown(void) cl_ev_disconnect(); } #ifdef HAVE_GTK - tray_close(); + tray_shutdown(); #endif jabber_shutdown(); plugins_on_shutdown(); diff --git a/src/tray.c b/src/tray.c index 7c731313..3e2c7a68 100644 --- a/src/tray.c +++ b/src/tray.c @@ -145,6 +145,7 @@ _tray_change_icon(gpointer data) void tray_init(void) { + _get_icons(); gtk_ready = gtk_init_check(0, NULL); log_debug("Env is GTK-ready: %s", gtk_ready ? "true" : "false"); if (!gtk_ready) { @@ -155,7 +156,7 @@ tray_init(void) gtk_main_iteration_do(FALSE); if (prefs_get_boolean(PREF_TRAY)) { log_debug("Building GTK icon"); - create_tray(); + tray_enable(); } } @@ -168,24 +169,25 @@ tray_update(void) } void -tray_close(void) +tray_shutdown(void) { if (gtk_ready && prefs_get_boolean(PREF_TRAY)) { - destroy_tray(); + tray_disable(); } + g_string_free(icon_filename, TRUE); + g_string_free(icon_msg_filename, TRUE); } void -create_tray(void) +tray_enable(void) { - _get_icons(); prof_tray = gtk_status_icon_new_from_file(icon_filename->str); shutting_down = FALSE; timer = g_timeout_add(5000, _tray_change_icon, NULL); } void -destroy_tray(void) +tray_disable(void) { shutting_down = TRUE; g_source_remove(timer); @@ -193,8 +195,6 @@ destroy_tray(void) g_clear_object(&prof_tray); prof_tray = NULL; } - g_string_free(icon_filename, TRUE); - g_string_free(icon_msg_filename, TRUE); } /* }}} */ diff --git a/src/tray.h b/src/tray.h index 98cac490..81ddd20f 100644 --- a/src/tray.h +++ b/src/tray.h @@ -37,7 +37,7 @@ void tray_init(void); void tray_update(void); -void tray_close(void); +void tray_shutdown(void); /* * Create tray icon @@ -45,10 +45,10 @@ void tray_close(void); * This will initialize the timer that will be called in order to change the icons * and will search the icons in the defaults paths */ -void create_tray(void); +void tray_enable(void); /* * Destroy tray icon */ -void destroy_tray(void); +void tray_disable(void); #endif From 20a0313d17db9c088f317651d1a61cc204f8ce3d Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 18 Apr 2016 00:35:03 +0100 Subject: [PATCH 4/4] Show correct tray icon immediately on enable --- src/tray.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/tray.c b/src/tray.c index 3e2c7a68..ff9593c3 100644 --- a/src/tray.c +++ b/src/tray.c @@ -153,11 +153,12 @@ tray_init(void) } gtk_init(0, NULL); - gtk_main_iteration_do(FALSE); if (prefs_get_boolean(PREF_TRAY)) { log_debug("Building GTK icon"); tray_enable(); } + + gtk_main_iteration_do(FALSE); } void @@ -183,6 +184,7 @@ tray_enable(void) { prof_tray = gtk_status_icon_new_from_file(icon_filename->str); shutting_down = FALSE; + _tray_change_icon(NULL); timer = g_timeout_add(5000, _tray_change_icon, NULL); }