Minor cleanups (memory, D-Bus, signals)
authorFaheem Pervez <trippin1@gmail.com>
Thu, 28 Jan 2010 10:03:03 +0000 (11:03 +0100)
committerThomas Perl <thp@thpinfo.com>
Thu, 28 Jan 2010 10:03:03 +0000 (11:03 +0100)
* 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

index 27d2d09..ba45861 100644 (file)
@@ -29,6 +29,7 @@
  **/
 
 #include <stdio.h>
+#include <unistd.h>
 #include <assert.h>
 #include <glib.h>
 #include <gconf/gconf-client.h>
@@ -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;
 }