From b8a57ae04c2214926e8ef1038b265c8f1146aa83 Mon Sep 17 00:00:00 2001 From: Faheem Pervez Date: Thu, 28 Jan 2010 11:03:03 +0100 Subject: [PATCH] Minor cleanups (memory, D-Bus, signals) * Memory allocated is freed when headphoned is exited * Instead of creating a new DBusConnection using dbus_bus_get (), the one provided by the osso_context_t is used * g_new0 automatically aborts the program if it cannot allocate the memory --- src/headphoned.c | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/src/headphoned.c b/src/headphoned.c index 27d2d09..ba45861 100644 --- a/src/headphoned.c +++ b/src/headphoned.c @@ -29,6 +29,7 @@ **/ #include +#include #include #include #include @@ -71,6 +72,17 @@ typedef struct { gboolean initial; } Headphoned; +static +GMainLoop* loop = NULL; + +static void +sig_handler (int sig G_GNUC_UNUSED) +{ + if (loop && g_main_loop_is_running (loop)) { + g_main_loop_quit (loop); + } +} + void on_volume_changed(GConfClient* client, guint gnxn_id, GConfEntry* entry, gpointer data) @@ -84,7 +96,6 @@ Headphoned* headphoned_new() { Headphoned* this = g_new0(Headphoned, 1); - assert(this != NULL); this->osso = osso_initialize("headphoned", "1.0", FALSE, NULL); assert(this->osso != NULL); @@ -97,7 +108,7 @@ headphoned_new() on_volume_changed, this, NULL, NULL); #endif - this->session_bus = dbus_bus_get(DBUS_BUS_SESSION, NULL); + this->session_bus = (DBusConnection*)osso_get_dbus_connection(this->osso); this->initial = TRUE; return this; @@ -173,13 +184,34 @@ on_file_changed(GIOChannel* source, GIOCondition condition, gpointer data) int main(int argc, char* argv[]) { + Headphoned *headphoned; g_type_init(); - GMainLoop* loop = g_main_loop_new(NULL, FALSE); + + signal(SIGINT, sig_handler); + signal(SIGQUIT, sig_handler); + signal(SIGTERM, sig_handler); + + loop = g_main_loop_new(NULL, FALSE); + headphoned = headphoned_new(); GIOChannel* state = g_io_channel_new_file(STATE_FILE, "r", NULL); - g_io_add_watch(state, G_IO_PRI, on_file_changed, headphoned_new()); + g_io_add_watch(state, G_IO_PRI, on_file_changed, headphoned); g_main_loop_run(loop); + +#ifdef ENABLE_VOLUME_CONTROL + if (headphoned->client) { + gconf_client_remove_dir(headphoned->client, GCONF_VOLUME_CONTROL, NULL); + g_object_unref(headphoned->client); + } +#endif + + if (state) { + g_io_channel_unref(state); + } + + osso_deinitialize(headphoned->osso); + g_free(headphoned); return 0; } -- 1.7.9.5