diff --git a/main.c b/main.c index f754f46..34da80d 100644 --- a/main.c +++ b/main.c @@ -36,10 +36,10 @@ { struct mpd_connection *conn; unsigned int tries = 0; - conn = mpd_connection_new(NULL, 0, 0); + conn = mpd_connection_new("/var/run/mpd/socket", 0, 0); while ((mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) && (tries < 60)) { mpd_connection_free(conn); - conn = mpd_connection_new(NULL, 0, 0); + conn = mpd_connection_new("/var/run/mpd/socket", 0, 0); tries++; sleep(1); } @@ -48,6 +48,7 @@ return EXIT_FAILURE; } + mpd_connection_set_keepalive(conn, true); wiringPiSetupGpio(); pullUpDnControl(FASTFORWARD_PIN, PUD_UP); pullUpDnControl(PLAYPAUSE_PIN, PUD_UP); @@ -72,33 +73,61 @@ last_pin_pressed = pin_pressed; pin_pressed = 0; pthread_mutex_unlock(&isr_mtx); - switch(last_pin_pressed) { - case PLAYPAUSE_PIN: - mpd_run_toggle_pause(conn); - break; - case ONOFF_PIN: - if (playing) { - mpd_run_stop(conn); - playing = false; - } else { - mpd_run_play(conn); - playing = true; + bool success = false; + while(!success) { + switch(last_pin_pressed) { + case PLAYPAUSE_PIN: + success = mpd_run_toggle_pause(conn); + break; + case ONOFF_PIN: + if (playing) { + success = mpd_run_stop(conn); + playing = false; + } else { + success = mpd_run_play(conn); + playing = true; + } + if (success) { + playing = !playing; + } + break; + case FASTFORWARD_PIN: + success = mpd_run_next(conn); + break; + case REWIND_PIN: + success = mpd_run_previous(conn); + break; + case VOLUMEDOWN_PIN: + success = mpd_run_change_volume(conn, -10); + break; + case VOLUMEUP_PIN: + success = mpd_run_change_volume(conn, 10); + break; + default: + success = true; + break; } - break; - case FASTFORWARD_PIN: - mpd_run_next(conn); - break; - case REWIND_PIN: - mpd_run_previous(conn); - break; - case VOLUMEDOWN_PIN: - mpd_run_change_volume(conn, -10); - break; - case VOLUMEUP_PIN: - mpd_run_change_volume(conn, 10); - break; - default: - break; + } + if (!success) { + switch(mpd_connection_get_error(conn)) { + case MPD_ERROR_SERVER: + case MPD_ERROR_ARGUMENT: + case MPD_ERROR_STATE: + case MPD_ERROR_MALFORMED: + mpd_connection_clear_error(conn); + break; + case MPD_ERROR_TIMEOUT: + case MPD_ERROR_CLOSED: + mpd_connection_free(conn); + conn = mpd_connection_new("/var/run/mpd/socket", 0, 0); + if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) { + mpd_connection_free(conn); + return EXIT_FAILURE; + } + break; + default: + return EXIT_FAILURE; + } } }