diff --git a/mpd_control.c b/mpd_control.c index c089a19..b200752 100644 --- a/mpd_control.c +++ b/mpd_control.c @@ -41,21 +41,13 @@ scroll_text(const char *text_to_scroll, const size_t text_len, char *buffer, } static int -handle_error(struct mpd_connection *conn, bool revive) +handle_error(struct mpd_connection *c) { - if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) { - fprintf(stderr, "%s\n", mpd_connection_get_error_message(conn)); + assert(mpd_connection_get_error(c) != MPD_ERROR_SUCCESS); - if (revive) { - if (!mpd_connection_clear_error(conn)) { - mpd_connection_free(conn); - conn = mpd_connection_new(NULL, 0, 0); - } - } else { - return EXIT_FAILURE; - } - } - return 0; + fprintf(stderr, "%s\n", mpd_connection_get_error_message(c)); + mpd_connection_free(c); + return EXIT_FAILURE; } static void @@ -75,7 +67,7 @@ print_status(struct mpd_connection *conn, struct worker_meta *meta) struct mpd_status *status = mpd_run_status(conn); if (status == NULL) - return handle_error(conn, false); + return handle_error(conn); const enum mpd_state state = mpd_status_get_state(status); @@ -113,10 +105,8 @@ print_status(struct mpd_connection *conn, struct worker_meta *meta) const unsigned remaining_secs = remaining % 60; mpd_status_free(status); - - if (handle_error(conn, false)) { - return 1; - } + if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) + return handle_error(conn); char artist[256]; char album[256]; @@ -128,9 +118,8 @@ print_status(struct mpd_connection *conn, struct worker_meta *meta) get_tag(song, title, MPD_TAG_TITLE); mpd_song_free(song); - if (handle_error(conn, false)) { - return 1; - } + if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) + return handle_error(conn); // the full song label. would be cool if this was customizable at runtime char full_label[strlen(artist) + strlen(title) + 4]; @@ -181,8 +170,10 @@ status_loop(void* worker_meta) struct worker_meta *meta = (struct worker_meta*)worker_meta; struct mpd_connection *conn = mpd_connection_new(NULL, 0, 0); - while (!meta->stop) { - handle_error(conn, true); + for (;;) { + if (meta->stop) { + break; + } long long start = current_timestamp(); @@ -202,9 +193,8 @@ enum click_command { }; void -mpd_run_command(struct worker_meta *meta, struct mpd_connection *conn, enum click_command command) { - handle_error(conn, true); - +mpd_run_command(struct worker_meta *meta, enum click_command command) { + struct mpd_connection *conn = mpd_connection_new(NULL, 0, 0); switch (command) { case TOGGLE_PAUSE: mpd_run_toggle_pause(conn); @@ -224,6 +214,7 @@ mpd_run_command(struct worker_meta *meta, struct mpd_connection *conn, enum clic } print_status(conn, meta); + mpd_connection_free(conn); } @@ -236,28 +227,26 @@ int main(void) { return 1; } - struct mpd_connection *conn = mpd_connection_new(NULL, 0, 0); - while (!meta.stop) { + for (;;) { char *line = NULL; size_t size; if (getline(&line, &size, stdin) != -1) { if (strcmp(line, "1\n") == 0) { - mpd_run_command(&meta, conn, PREVIOUS); + mpd_run_command(&meta, PREVIOUS); } else if (strcmp(line, "2\n") == 0) { - mpd_run_command(&meta, conn, TOGGLE_PAUSE); + mpd_run_command(&meta, TOGGLE_PAUSE); } else if (strcmp(line, "3\n") == 0) { - mpd_run_command(&meta, conn, NEXT); + mpd_run_command(&meta, NEXT); } else if (strcmp(line, "4\n") == 0) { - mpd_run_command(&meta, conn, SEEK_FWD); + mpd_run_command(&meta, SEEK_FWD); } else if (strcmp(line, "5\n") == 0) { - mpd_run_command(&meta, conn, SEEK_BCKWD); + mpd_run_command(&meta, SEEK_BCKWD); } free(line); } } - mpd_connection_free(conn); if(pthread_join(update_thread, NULL)) { fprintf(stderr, "Error joining thread\n");