Code

main: call g_io_channel_unref() early
[ncmpc.git] / src / main.c
index 177f1b529be07e0506fe8d245bf82b5d78c94ceb..c20dd6d2a127134908764096ff42028f5cdcf725 100644 (file)
@@ -90,18 +90,6 @@ update_xterm_title(void)
 }
 #endif
 
-static void
-exit_and_cleanup(void)
-{
-       screen_exit();
-#ifndef NCMPC_MINI
-       set_xterm_title("");
-#endif
-       printf("\n");
-
-       mpdclient_free(mpd);
-}
-
 #ifndef WIN32
 static void
 catch_sigint(gcc_unused int sig)
@@ -252,7 +240,7 @@ default_settings_name(void)
 static gboolean
 timer_reconnect(gcc_unused gpointer data)
 {
-       assert(!mpdclient_is_connected(mpd));
+       assert(mpdclient_is_dead(mpd));
 
        reconnect_source_id = 0;
 
@@ -263,14 +251,26 @@ timer_reconnect(gcc_unused gpointer data)
        doupdate();
 
        mpdclient_disconnect(mpd);
-       if (!mpdclient_connect(mpd, options.host, options.port,
-                              options.timeout_ms,
-                              options.password)) {
-               /* try again in 5 seconds */
-               reconnect_source_id = g_timeout_add(5000,
-                                                   timer_reconnect, NULL);
-               return FALSE;
-       }
+       mpdclient_connect(mpd, options.host, options.port,
+                         options.timeout_ms,
+                         options.password);
+
+       return FALSE;
+}
+
+static void
+check_reconnect(void)
+{
+       if (mpdclient_is_dead(mpd) && reconnect_source_id == 0)
+               /* reconnect when the connection is lost */
+               reconnect_source_id = g_timeout_add(1000, timer_reconnect,
+                                                   NULL);
+}
+
+void
+mpdclient_connected_callback(void)
+{
+       assert(reconnect_source_id == 0);
 
 #ifndef NCMPC_MINI
        /* quit if mpd is pre 0.14 - song id not supported by mpd */
@@ -287,7 +287,7 @@ timer_reconnect(gcc_unused gpointer data)
                /* try again after 30 seconds */
                reconnect_source_id = g_timeout_add(30000,
                                                    timer_reconnect, NULL);
-               return FALSE;
+               return;
        }
 #endif
 
@@ -300,17 +300,16 @@ timer_reconnect(gcc_unused gpointer data)
        do_mpd_update();
 
        auto_update_timer();
-
-       return FALSE;
 }
 
-static void
-check_reconnect(void)
+void
+mpdclient_failed_callback(void)
 {
-       if (!mpdclient_is_connected(mpd) && reconnect_source_id == 0)
-               /* reconnect when the connection is lost */
-               reconnect_source_id = g_timeout_add(1000, timer_reconnect,
-                                                   NULL);
+       assert(reconnect_source_id == 0);
+
+       /* try again in 5 seconds */
+       reconnect_source_id = g_timeout_add(5000,
+                                           timer_reconnect, NULL);
 }
 
 void
@@ -552,23 +551,24 @@ main(int argc, const char *argv[])
        /* watch out for keyboard input */
        GIOChannel *keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
        g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
+       g_io_channel_unref(keyboard_channel);
 
 #ifdef ENABLE_LIRC
        /* watch out for lirc input */
        int lirc_socket = ncmpc_lirc_open();
-       GIOChannel *lirc_channel = NULL;
        if (lirc_socket >= 0) {
-               lirc_channel = g_io_channel_unix_new(lirc_socket);
+               GIOChannel *lirc_channel = g_io_channel_unix_new(lirc_socket);
                g_io_add_watch(lirc_channel, G_IO_IN, lirc_event, NULL);
+               g_io_channel_unref(lirc_channel);
        }
 #endif
 
 #ifndef WIN32
-       GIOChannel *sigwinch_channel = NULL;
        if (!pipe(sigwinch_pipes) &&
                !fcntl(sigwinch_pipes[1], F_SETFL, O_NONBLOCK)) {
-               sigwinch_channel = g_io_channel_unix_new(sigwinch_pipes[0]);
+               GIOChannel *sigwinch_channel = g_io_channel_unix_new(sigwinch_pipes[0]);
                g_io_add_watch(sigwinch_channel, G_IO_IN, sigwinch_event, NULL);
+               g_io_channel_unref(sigwinch_channel);
        }
        else {
                perror("sigwinch pipe creation failed");
@@ -588,6 +588,7 @@ main(int argc, const char *argv[])
        screen_paint(mpd);
 
        g_main_loop_run(main_loop);
+       g_main_loop_unref(main_loop);
 
        /* cleanup */
 
@@ -603,19 +604,20 @@ main(int argc, const char *argv[])
                g_source_remove(check_key_bindings_source_id);
 #endif
 
-       g_main_loop_unref(main_loop);
-       g_io_channel_unref(keyboard_channel);
-       g_io_channel_unref(sigwinch_channel);
        close(sigwinch_pipes[0]);
        close(sigwinch_pipes[1]);
 
 #ifdef ENABLE_LIRC
-       if (lirc_socket >= 0)
-               g_io_channel_unref(lirc_channel);
        ncmpc_lirc_close();
 #endif
 
-       exit_and_cleanup();
+       screen_exit();
+#ifndef NCMPC_MINI
+       set_xterm_title("");
+#endif
+       printf("\n");
+
+       mpdclient_free(mpd);
 
 #ifdef ENABLE_LYRICS_SCREEN
        lyrics_deinit();