Revive connection upon error.
Keep connection on input loop alive.
This commit is contained in:
parent
d848dc0560
commit
0c401294b7
@ -41,13 +41,21 @@ scroll_text(const char *text_to_scroll, const size_t text_len, char *buffer,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
handle_error(struct mpd_connection *c)
|
handle_error(struct mpd_connection *conn, bool revive)
|
||||||
{
|
{
|
||||||
assert(mpd_connection_get_error(c) != MPD_ERROR_SUCCESS);
|
if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) {
|
||||||
|
fprintf(stderr, "%s\n", mpd_connection_get_error_message(conn));
|
||||||
|
|
||||||
fprintf(stderr, "%s\n", mpd_connection_get_error_message(c));
|
if (revive) {
|
||||||
mpd_connection_free(c);
|
if (!mpd_connection_clear_error(conn)) {
|
||||||
return EXIT_FAILURE;
|
mpd_connection_free(conn);
|
||||||
|
conn = mpd_connection_new(NULL, 0, 0);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -67,7 +75,7 @@ print_status(struct mpd_connection *conn, struct worker_meta *meta)
|
|||||||
struct mpd_status *status = mpd_run_status(conn);
|
struct mpd_status *status = mpd_run_status(conn);
|
||||||
|
|
||||||
if (status == NULL)
|
if (status == NULL)
|
||||||
return handle_error(conn);
|
return handle_error(conn, false);
|
||||||
|
|
||||||
const enum mpd_state state = mpd_status_get_state(status);
|
const enum mpd_state state = mpd_status_get_state(status);
|
||||||
|
|
||||||
@ -104,8 +112,10 @@ print_status(struct mpd_connection *conn, struct worker_meta *meta)
|
|||||||
const unsigned remaining_secs = remaining % 60;
|
const unsigned remaining_secs = remaining % 60;
|
||||||
|
|
||||||
mpd_status_free(status);
|
mpd_status_free(status);
|
||||||
if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS)
|
|
||||||
return handle_error(conn);
|
if (handle_error(conn, false)) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
char artist[256];
|
char artist[256];
|
||||||
char album[256];
|
char album[256];
|
||||||
@ -117,8 +127,9 @@ print_status(struct mpd_connection *conn, struct worker_meta *meta)
|
|||||||
get_tag(song, title, MPD_TAG_TITLE);
|
get_tag(song, title, MPD_TAG_TITLE);
|
||||||
|
|
||||||
mpd_song_free(song);
|
mpd_song_free(song);
|
||||||
if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS)
|
if (handle_error(conn, false)) {
|
||||||
return handle_error(conn);
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
// the full song label. would be cool if this was customizable at runtime
|
// the full song label. would be cool if this was customizable at runtime
|
||||||
char full_label[strlen(artist) + strlen(title) + 4];
|
char full_label[strlen(artist) + strlen(title) + 4];
|
||||||
@ -170,10 +181,8 @@ status_loop(void* worker_meta)
|
|||||||
struct worker_meta *meta = (struct worker_meta*)worker_meta;
|
struct worker_meta *meta = (struct worker_meta*)worker_meta;
|
||||||
struct mpd_connection *conn = mpd_connection_new(NULL, 0, 0);
|
struct mpd_connection *conn = mpd_connection_new(NULL, 0, 0);
|
||||||
|
|
||||||
for (;;) {
|
while (!meta->stop) {
|
||||||
if (meta->stop) {
|
handle_error(conn, true);
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
long long start = current_timestamp();
|
long long start = current_timestamp();
|
||||||
print_status(conn, meta);
|
print_status(conn, meta);
|
||||||
@ -192,8 +201,9 @@ enum click_command {
|
|||||||
};
|
};
|
||||||
|
|
||||||
void
|
void
|
||||||
mpd_run_command(struct worker_meta *meta, enum click_command command) {
|
mpd_run_command(struct worker_meta *meta, struct mpd_connection *conn, enum click_command command) {
|
||||||
struct mpd_connection *conn = mpd_connection_new(NULL, 0, 0);
|
handle_error(conn, true);
|
||||||
|
|
||||||
switch (command) {
|
switch (command) {
|
||||||
case TOGGLE_PAUSE:
|
case TOGGLE_PAUSE:
|
||||||
mpd_run_toggle_pause(conn);
|
mpd_run_toggle_pause(conn);
|
||||||
@ -213,7 +223,6 @@ mpd_run_command(struct worker_meta *meta, enum click_command command) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
print_status(conn, meta);
|
print_status(conn, meta);
|
||||||
mpd_connection_free(conn);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -226,26 +235,28 @@ int main(void) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (;;) {
|
struct mpd_connection *conn = mpd_connection_new(NULL, 0, 0);
|
||||||
|
while (!meta.stop) {
|
||||||
char *line = NULL;
|
char *line = NULL;
|
||||||
size_t size;
|
size_t size;
|
||||||
|
|
||||||
if (getline(&line, &size, stdin) != -1) {
|
if (getline(&line, &size, stdin) != -1) {
|
||||||
if (strcmp(line, "1\n") == 0) {
|
if (strcmp(line, "1\n") == 0) {
|
||||||
mpd_run_command(&meta, PREVIOUS);
|
mpd_run_command(&meta, conn, PREVIOUS);
|
||||||
} else if (strcmp(line, "2\n") == 0) {
|
} else if (strcmp(line, "2\n") == 0) {
|
||||||
mpd_run_command(&meta, TOGGLE_PAUSE);
|
mpd_run_command(&meta, conn, TOGGLE_PAUSE);
|
||||||
} else if (strcmp(line, "3\n") == 0) {
|
} else if (strcmp(line, "3\n") == 0) {
|
||||||
mpd_run_command(&meta, NEXT);
|
mpd_run_command(&meta, conn, NEXT);
|
||||||
} else if (strcmp(line, "4\n") == 0) {
|
} else if (strcmp(line, "4\n") == 0) {
|
||||||
mpd_run_command(&meta, SEEK_FWD);
|
mpd_run_command(&meta, conn, SEEK_FWD);
|
||||||
} else if (strcmp(line, "5\n") == 0) {
|
} else if (strcmp(line, "5\n") == 0) {
|
||||||
mpd_run_command(&meta, SEEK_BCKWD);
|
mpd_run_command(&meta, conn, SEEK_BCKWD);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(line);
|
free(line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
mpd_connection_free(conn);
|
||||||
|
|
||||||
if(pthread_join(update_thread, NULL)) {
|
if(pthread_join(update_thread, NULL)) {
|
||||||
fprintf(stderr, "Error joining thread\n");
|
fprintf(stderr, "Error joining thread\n");
|
||||||
|
Loading…
Reference in New Issue
Block a user