From 0ec2df77a72edde7b65a4ac8e7ffff4470120a51 Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Thu, 14 Apr 2016 16:21:34 +0200 Subject: Fix songfinish event when changing stations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The event did not contain any song data (title, artist, …), because the playlist was destroyed by the ui command. Now the actual station switch is deferred by introducing nextStation and letting the main loop handle it. Fixes issue #584. --- src/main.c | 30 +++++++++++++++--------------- src/main.h | 4 +++- src/ui.c | 3 ++- src/ui_act.c | 18 ++++++++++-------- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/main.c b/src/main.c index c0388e2..a85949a 100644 --- a/src/main.c +++ b/src/main.c @@ -177,21 +177,18 @@ static bool BarMainGetStations (BarApp_t *app) { static void BarMainGetInitialStation (BarApp_t *app) { /* try to get autostart station */ if (app->settings.autostartStation != NULL) { - app->curStation = PianoFindStationById (app->ph.stations, + app->nextStation = PianoFindStationById (app->ph.stations, app->settings.autostartStation); - if (app->curStation == NULL) { + if (app->nextStation == NULL) { BarUiMsg (&app->settings, MSG_ERR, "Error: Autostart station not found.\n"); } } /* no autostart? ask the user */ - if (app->curStation == NULL) { - app->curStation = BarUiSelectStation (app, app->ph.stations, + if (app->nextStation == NULL) { + app->nextStation = BarUiSelectStation (app, app->ph.stations, "Select station: ", NULL, app->settings.autoselect); } - if (app->curStation != NULL) { - BarUiPrintStation (&app->settings, app->curStation); - } } /* wait for user input @@ -211,20 +208,21 @@ static void BarMainGetPlaylist (BarApp_t *app) { PianoReturn_t pRet; CURLcode wRet; PianoRequestDataGetPlaylist_t reqData; - reqData.station = app->curStation; + reqData.station = app->nextStation; reqData.quality = app->settings.audioQuality; BarUiMsg (&app->settings, MSG_INFO, "Receiving new playlist... "); if (!BarUiPianoCall (app, PIANO_REQUEST_GET_PLAYLIST, &reqData, &pRet, &wRet)) { - app->curStation = NULL; + app->nextStation = NULL; } else { app->playlist = reqData.retPlaylist; if (app->playlist == NULL) { BarUiMsg (&app->settings, MSG_INFO, "No tracks left.\n"); - app->curStation = NULL; + app->nextStation = NULL; } } + app->curStation = app->nextStation; BarUiStartEventCmd (&app->settings, "stationfetchplaylist", app->curStation, app->playlist, &app->player, app->ph.stations, pRet, wRet); @@ -297,10 +295,10 @@ static void BarMainPlayerCleanup (BarApp_t *app, pthread_t *playerThread) { ++app->playerErrors; if (app->playerErrors >= app->settings.maxPlayerErrors) { /* don't continue playback if thread reports too many error */ - app->curStation = NULL; + app->nextStation = NULL; } } else { - app->curStation = NULL; + app->nextStation = NULL; } memset (&app->player, 0, sizeof (app->player)); @@ -363,8 +361,7 @@ static void BarMainLoop (BarApp_t *app) { /* check whether player finished playing and start playing new * song */ - if (app->player.mode == PLAYER_DEAD && app->curStation != NULL && - !app->doQuit) { + if (app->player.mode == PLAYER_DEAD) { /* what's next? */ if (app->playlist != NULL) { PianoSong_t *histsong = app->playlist; @@ -372,7 +369,10 @@ static void BarMainLoop (BarApp_t *app) { histsong->head.next = NULL; BarUiHistoryPrepend (app, histsong); } - if (app->playlist == NULL) { + if (app->playlist == NULL && app->nextStation != NULL && !app->doQuit) { + if (app->nextStation != app->curStation) { + BarUiPrintStation (&app->settings, app->nextStation); + } BarMainGetPlaylist (app); } /* song ready to play */ diff --git a/src/main.h b/src/main.h index 0dd2647..7dc0ac8 100644 --- a/src/main.h +++ b/src/main.h @@ -39,7 +39,9 @@ typedef struct { /* first item is current song */ PianoSong_t *playlist; PianoSong_t *songHistory; - PianoStation_t *curStation; + /* station of current song and station used to fetch songs from if playlist + * is empty */ + PianoStation_t *curStation, *nextStation; sig_atomic_t doQuit; BarReadlineFds_t input; unsigned int playerErrors; diff --git a/src/ui.c b/src/ui.c index c2a55df..5350dea 100644 --- a/src/ui.c +++ b/src/ui.c @@ -802,7 +802,8 @@ void BarUiStartEventCmd (const BarSettings_t *settings, const char *type, pipeWriteFd = fdopen (pipeFd[1], "w"); - if (curSong != NULL && stations != NULL && curStation->isQuickMix) { + if (curSong != NULL && stations != NULL && curStation != NULL && + curStation->isQuickMix) { songStation = PianoFindStationById (stations, curSong->stationId); } diff --git a/src/ui_act.c b/src/ui_act.c index e8452e6..e1dbfc0 100644 --- a/src/ui_act.c +++ b/src/ui_act.c @@ -236,10 +236,14 @@ BarUiActCallback(BarUiActDeleteStation) { if (BarUiActDefaultPianoCall (PIANO_REQUEST_DELETE_STATION, selStation) && selStation == app->curStation) { BarUiDoSkipSong (&app->player); - PianoDestroyPlaylist (PianoListNextP (app->playlist)); - app->playlist->head.next = NULL; - BarUiHistoryPrepend (app, app->playlist); - app->playlist = NULL; + if (app->playlist != NULL) { + /* drain playlist */ + PianoDestroyPlaylist (PianoListNextP (app->playlist)); + app->playlist->head.next = NULL; + } + app->nextStation = NULL; + /* XXX: usually we shoudn’t touch cur*, but DELETE_STATION destroys + * station struct */ app->curStation = NULL; } BarUiActDefaultEventcmd ("stationdelete"); @@ -468,14 +472,12 @@ BarUiActCallback(BarUiActSelectStation) { PianoStation_t *newStation = BarUiSelectStation (app, app->ph.stations, "Select station: ", NULL, app->settings.autoselect); if (newStation != NULL) { - app->curStation = newStation; - BarUiPrintStation (&app->settings, app->curStation); + app->nextStation = newStation; BarUiDoSkipSong (&app->player); if (app->playlist != NULL) { + /* drain playlist */ PianoDestroyPlaylist (PianoListNextP (app->playlist)); app->playlist->head.next = NULL; - BarUiHistoryPrepend (app, app->playlist); - app->playlist = NULL; } } } -- cgit v1.2.3