diff --git a/src/main.c b/src/main.c
index fe21c6bcf6c947605c8af89a6c581bd5b2759457..c20dd6d2a127134908764096ff42028f5cdcf725 100644 (file)
--- a/src/main.c
+++ b/src/main.c
static void
update_xterm_title(void)
{
- struct mpd_status *status = NULL;
- const struct mpd_song *song = NULL;
- if (mpd) {
- status = mpd->status;
- song = mpd->song;
- }
+ const struct mpd_song *song = mpd->song;
char tmp[BUFSIZE];
- if (options.xterm_title_format && status && song &&
- mpd_status_get_state(status) == MPD_STATE_PLAY)
+ if (options.xterm_title_format && mpd->playing && song)
strfsong(tmp, BUFSIZE, options.xterm_title_format, song);
else
g_strlcpy(tmp, PACKAGE " version " VERSION, BUFSIZE);
}
#endif
-static void
-exit_and_cleanup(void)
-{
- screen_exit();
-#ifndef NCMPC_MINI
- set_xterm_title("");
-#endif
- printf("\n");
-
- if (mpd) {
- mpdclient_free(mpd);
- }
-}
-
#ifndef WIN32
static void
catch_sigint(gcc_unused int sig)
static bool
should_enable_update_timer(void)
{
- return (mpdclient_is_connected(mpd) &&
- mpd->status != NULL &&
- mpd_status_get_state(mpd->status) == MPD_STATE_PLAY)
+ return mpd->playing
#ifndef NCMPC_MINI
|| options.display_time
#endif
do_mpd_update(void)
{
if (mpdclient_is_connected(mpd) &&
- (mpd->events != 0 ||
- (mpd->status != NULL &&
- mpd_status_get_state(mpd->status) == MPD_STATE_PLAY)))
+ (mpd->events != 0 || mpd->playing))
mpdclient_update(mpd);
#ifndef NCMPC_MINI
static gboolean
timer_reconnect(gcc_unused gpointer data)
{
- assert(!mpdclient_is_connected(mpd));
+ assert(mpdclient_is_dead(mpd));
reconnect_source_id = 0;
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 */
/* try again after 30 seconds */
reconnect_source_id = g_timeout_add(30000,
timer_reconnect, NULL);
- return FALSE;
+ return;
}
#endif
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
/* 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");
screen_paint(mpd);
g_main_loop_run(main_loop);
+ g_main_loop_unref(main_loop);
/* cleanup */
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();