From 31d3dc42ef25b1ba43a50ace638498b60dedf475 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 6 Mar 2015 14:26:38 -0600 Subject: move from g_bus_own_name_on_connection() to g_bus_own_name() to simplify the code --- src/main.c | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/src/main.c b/src/main.c index ad8b3d4..4633b4b 100644 --- a/src/main.c +++ b/src/main.c @@ -32,7 +32,7 @@ sigterm_handler (gpointer data) static void name_lost (GDBusConnection * connection, const gchar * name, gpointer user_data) { - g_debug("Name lost"); + g_warning("Name lost or unable to acquire bus: %s", name); g_main_loop_quit((GMainLoop *)user_data); } @@ -40,21 +40,11 @@ int main (int argc, char ** argv) { GMainLoop * loop = NULL; IndicatorSoundService* service = NULL; - GDBusConnection * bus = NULL; bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); setlocale (LC_ALL, ""); bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR); - /* Grab DBus */ - GError * error = NULL; - bus = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error); - if (error != NULL) { - g_error("Unable to get session bus: %s", error->message); - g_error_free(error); - return -1; - } - /* Build Mainloop */ loop = g_main_loop_new(NULL, FALSE); @@ -63,6 +53,15 @@ main (int argc, char ** argv) { /* Initialize libnotify */ notify_init ("indicator-sound"); + g_bus_own_name(G_BUS_TYPE_SESSION, + "com.canonical.indicator.sound", + G_BUS_NAME_OWNER_FLAGS_NONE, + NULL, /* bus acquired */ + NULL, /* name acquired */ + name_lost, + loop, + NULL); + MediaPlayerList * playerlist = NULL; AccountsServiceUser * accounts = NULL; @@ -77,20 +76,11 @@ main (int argc, char ** argv) { service = indicator_sound_service_new (playerlist, volume, accounts); - g_bus_own_name_on_connection(bus, - "com.canonical.indicator.sound", - G_BUS_NAME_OWNER_FLAGS_NONE, - NULL, /* acquired */ - name_lost, - loop, - NULL); - g_main_loop_run(loop); g_clear_object(&playerlist); g_clear_object(&accounts); g_clear_object(&service); - g_clear_object(&bus); return 0; } -- cgit v1.2.3 From cbc49b71a9fa14f291057d191ad33ac8e2eb3b00 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 10 Mar 2015 14:15:04 -0500 Subject: in main.c's g_bus_own_name() calls, move service creation/export to bus_acquired_handler to ensure that everything is exported onto the bus before name_acquired_handler is reached --- src/main.c | 56 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/src/main.c b/src/main.c index 4633b4b..33a92f9 100644 --- a/src/main.c +++ b/src/main.c @@ -21,6 +21,8 @@ #include "indicator-sound-service.h" #include "config.h" +static IndicatorSoundService * service = NULL; + static gboolean sigterm_handler (gpointer data) { @@ -30,16 +32,44 @@ sigterm_handler (gpointer data) } static void -name_lost (GDBusConnection * connection, const gchar * name, gpointer user_data) +on_name_lost(GDBusConnection * connection, + const gchar * name, + gpointer user_data) { g_warning("Name lost or unable to acquire bus: %s", name); g_main_loop_quit((GMainLoop *)user_data); } +static void +on_bus_acquired(GDBusConnection *connection, + const gchar *name, + gpointer user_data) +{ + MediaPlayerList * playerlist = NULL; + VolumeControlPulse * volume = NULL; + AccountsServiceUser * accounts = NULL; + + + if (g_strcmp0("lightdm", g_get_user_name()) == 0) { + playerlist = MEDIA_PLAYER_LIST(media_player_list_greeter_new()); + } else { + playerlist = MEDIA_PLAYER_LIST(media_player_list_mpris_new()); + accounts = accounts_service_user_new(); + } + + volume = volume_control_pulse_new(); + + service = indicator_sound_service_new (playerlist, volume, accounts); + + g_clear_object(&playerlist); + g_clear_object(&volume); + g_clear_object(&accounts); +} + int -main (int argc, char ** argv) { +main (int argc, char ** argv) +{ GMainLoop * loop = NULL; - IndicatorSoundService* service = NULL; bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); setlocale (LC_ALL, ""); @@ -56,30 +86,14 @@ main (int argc, char ** argv) { g_bus_own_name(G_BUS_TYPE_SESSION, "com.canonical.indicator.sound", G_BUS_NAME_OWNER_FLAGS_NONE, - NULL, /* bus acquired */ + on_bus_acquired, NULL, /* name acquired */ - name_lost, + on_name_lost, loop, NULL); - MediaPlayerList * playerlist = NULL; - AccountsServiceUser * accounts = NULL; - - if (g_strcmp0("lightdm", g_get_user_name()) == 0) { - playerlist = MEDIA_PLAYER_LIST(media_player_list_greeter_new()); - } else { - playerlist = MEDIA_PLAYER_LIST(media_player_list_mpris_new()); - accounts = accounts_service_user_new(); - } - - VolumeControlPulse * volume = volume_control_pulse_new(); - - service = indicator_sound_service_new (playerlist, volume, accounts); - g_main_loop_run(loop); - g_clear_object(&playerlist); - g_clear_object(&accounts); g_clear_object(&service); return 0; -- cgit v1.2.3 From 4ac72ab6ca244060b9d51244a83d140e945fd36c Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 10 Mar 2015 14:16:26 -0500 Subject: in main(), balance the notify_init() call with a notify_uninit() right before exiting --- src/main.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main.c b/src/main.c index 33a92f9..c368577 100644 --- a/src/main.c +++ b/src/main.c @@ -96,6 +96,8 @@ main (int argc, char ** argv) g_clear_object(&service); + notify_uninit(); + return 0; } -- cgit v1.2.3