From 417d5f893c8d07557bbce28140e087ed402882ab Mon Sep 17 00:00:00 2001 From: Michał Cichoń Date: Thu, 10 Dec 2015 14:15:06 +0100 Subject: Add missing new line after password prompt. --- src/main.c | 637 +++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 342 insertions(+), 295 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index f0172da..eca4705 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,6 @@ /* Copyright (c) 2008-2013 - Lars-Dominik Braun + Lars-Dominik Braun Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -34,313 +34,356 @@ THE SOFTWARE. /* authenticate user */ -static bool BarMainLoginUser (BarApp_t *app) { - PianoReturn_t pRet; - PianoRequestDataLogin_t reqData; - bool ret; - - reqData.user = app->settings.username; - reqData.password = app->settings.password; - reqData.step = 0; - - BarUiMsg (&app->settings, MSG_INFO, "Login... "); - ret = BarUiPianoCall (app, PIANO_REQUEST_LOGIN, &reqData, &pRet); - BarUiStartEventCmd (&app->settings, "userlogin", NULL, NULL, &app->player, - NULL, pRet); - - return ret; +static bool BarMainLoginUser(BarApp_t *app) +{ + PianoReturn_t pRet; + PianoRequestDataLogin_t reqData; + bool ret; + + reqData.user = app->settings.username; + reqData.password = app->settings.password; + reqData.step = 0; + + BarUiMsg(&app->settings, MSG_INFO, "Login... "); + ret = BarUiPianoCall(app, PIANO_REQUEST_LOGIN, &reqData, &pRet); + BarUiStartEventCmd(&app->settings, "userlogin", NULL, NULL, &app->player, + NULL, pRet); + + return ret; } /* ask for username/password if none were provided in settings */ -static bool BarMainGetLoginCredentials (BarSettings_t *settings, - BarReadline_t rl) { - bool usernameFromConfig = true; - - if (settings->username == NULL) { - char nameBuf[100]; - - BarUiMsg (settings, MSG_QUESTION, "Email: "); - BarReadlineStr (nameBuf, sizeof (nameBuf), rl, BAR_RL_DEFAULT); - settings->username = strdup (nameBuf); - usernameFromConfig = false; - } - - if (settings->password == NULL) { - char passBuf[100]; - - if (usernameFromConfig) { - BarUiMsg (settings, MSG_QUESTION, "Email: %s\n", settings->username); - } - - if (settings->passwordCmd == NULL) { - BarUiMsg (settings, MSG_QUESTION, "Password: "); - BarReadlineStr (passBuf, sizeof (passBuf), rl, BAR_RL_NOECHO); - /* write missing newline */ - BarConsolePuts(""); - settings->password = strdup (passBuf); - } else { - //pid_t chld; - //int pipeFd[2]; - - //BarUiMsg (settings, MSG_INFO, "Requesting password from external helper... "); - - //if (pipe (pipeFd) == -1) { - // BarUiMsg (settings, MSG_NONE, "Error: %s\n", strerror (errno)); - // return false; - //} - - //chld = fork (); - //if (chld == 0) { - // /* child */ - // close (pipeFd[0]); - // dup2 (pipeFd[1], fileno (stdout)); - // execl ("/bin/sh", "/bin/sh", "-c", settings->passwordCmd, (char *) NULL); - // BarUiMsg (settings, MSG_NONE, "Error: %s\n", strerror (errno)); - // close (pipeFd[1]); - // exit (1); - //} else if (chld == -1) { - // BarUiMsg (settings, MSG_NONE, "Error: %s\n", strerror (errno)); - // return false; - //} else { - // /* parent */ - // int status; - - // close (pipeFd[1]); - // memset (passBuf, 0, sizeof (passBuf)); - // read (pipeFd[0], passBuf, sizeof (passBuf)-1); - // close (pipeFd[0]); - - // /* drop trailing newlines */ - // ssize_t len = strlen (passBuf)-1; - // while (len >= 0 && passBuf[len] == '\n') { - // passBuf[len] = '\0'; - // --len; - // } - - // waitpid (chld, &status, 0); - // if (WEXITSTATUS (status) == 0) { - // settings->password = strdup (passBuf); - // BarUiMsg (settings, MSG_NONE, "Ok.\n"); - // } else { - // BarUiMsg (settings, MSG_NONE, "Error: Exit status %i.\n", WEXITSTATUS (status)); - // return false; - // } - //} - return false; - } /* end else passwordCmd */ - } - - return true; +static bool BarMainGetLoginCredentials(BarSettings_t *settings, + BarReadline_t rl) +{ + bool usernameFromConfig = true; + + if (settings->username == NULL) + { + char nameBuf[100]; + + BarUiMsg(settings, MSG_QUESTION, "Email: "); + BarReadlineStr(nameBuf, sizeof(nameBuf), rl, BAR_RL_DEFAULT); + settings->username = strdup(nameBuf); + usernameFromConfig = false; + } + + if (settings->password == NULL) + { + char passBuf[100]; + + if (usernameFromConfig) + { + BarUiMsg(settings, MSG_QUESTION, "Email: %s\n", settings->username); + } + + if (settings->passwordCmd == NULL) + { + BarUiMsg(settings, MSG_QUESTION, "Password: "); + BarReadlineStr(passBuf, sizeof(passBuf), rl, BAR_RL_NOECHO); + /* write missing newline */ + BarConsolePutc('\n'); + settings->password = strdup(passBuf); + } + else + { + //pid_t chld; + //int pipeFd[2]; + + //BarUiMsg (settings, MSG_INFO, "Requesting password from external helper... "); + + //if (pipe (pipeFd) == -1) { + // BarUiMsg (settings, MSG_NONE, "Error: %s\n", strerror (errno)); + // return false; + //} + + //chld = fork (); + //if (chld == 0) { + // /* child */ + // close (pipeFd[0]); + // dup2 (pipeFd[1], fileno (stdout)); + // execl ("/bin/sh", "/bin/sh", "-c", settings->passwordCmd, (char *) NULL); + // BarUiMsg (settings, MSG_NONE, "Error: %s\n", strerror (errno)); + // close (pipeFd[1]); + // exit (1); + //} else if (chld == -1) { + // BarUiMsg (settings, MSG_NONE, "Error: %s\n", strerror (errno)); + // return false; + //} else { + // /* parent */ + // int status; + + // close (pipeFd[1]); + // memset (passBuf, 0, sizeof (passBuf)); + // read (pipeFd[0], passBuf, sizeof (passBuf)-1); + // close (pipeFd[0]); + + // /* drop trailing newlines */ + // ssize_t len = strlen (passBuf)-1; + // while (len >= 0 && passBuf[len] == '\n') { + // passBuf[len] = '\0'; + // --len; + // } + + // waitpid (chld, &status, 0); + // if (WEXITSTATUS (status) == 0) { + // settings->password = strdup (passBuf); + // BarUiMsg (settings, MSG_NONE, "Ok.\n"); + // } else { + // BarUiMsg (settings, MSG_NONE, "Error: Exit status %i.\n", WEXITSTATUS (status)); + // return false; + // } + //} + return false; + } /* end else passwordCmd */ + } + + return true; } /* get station list */ -static bool BarMainGetStations (BarApp_t *app) { - PianoReturn_t pRet; - bool ret; - - BarUiMsg (&app->settings, MSG_INFO, "Get stations... "); - ret = BarUiPianoCall (app, PIANO_REQUEST_GET_STATIONS, NULL, &pRet); - BarUiStartEventCmd (&app->settings, "usergetstations", NULL, NULL, &app->player, - app->ph.stations, pRet); - return ret; +static bool BarMainGetStations(BarApp_t *app) +{ + PianoReturn_t pRet; + bool ret; + + BarUiMsg(&app->settings, MSG_INFO, "Get stations... "); + ret = BarUiPianoCall(app, PIANO_REQUEST_GET_STATIONS, NULL, &pRet); + BarUiStartEventCmd(&app->settings, "usergetstations", NULL, NULL, &app->player, + app->ph.stations, pRet); + return ret; } /* get initial station from autostart setting or user rl */ -static void BarMainGetInitialStation (BarApp_t *app) { - /* try to get autostart station */ - if (app->settings.autostartStation != NULL) { - app->curStation = PianoFindStationById (app->ph.stations, - app->settings.autostartStation); - if (app->curStation == 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, - "Select station: ", NULL, app->settings.autoselect); - } - if (app->curStation != NULL) { - BarUiPrintStation (&app->settings, app->curStation); - } +static void BarMainGetInitialStation(BarApp_t *app) +{ + /* try to get autostart station */ + if (app->settings.autostartStation != NULL) + { + app->curStation = PianoFindStationById(app->ph.stations, + app->settings.autostartStation); + if (app->curStation == 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, + "Select station: ", NULL, app->settings.autoselect); + } + if (app->curStation != NULL) + { + BarUiPrintStation(&app->settings, app->curStation); + } } /* wait for user rl */ -static void BarMainHandleUserInput (BarApp_t *app) { - char buf[2]; - if (BarReadline (buf, sizeof (buf), NULL, app->rl, - BAR_RL_FULLRETURN | BAR_RL_NOECHO, 1) > 0) { - BarUiDispatch (app, buf[0], app->curStation, app->playlist, true, - BAR_DC_GLOBAL); - } +static void BarMainHandleUserInput(BarApp_t *app) +{ + char buf[2]; + if (BarReadline(buf, sizeof(buf), NULL, app->rl, + BAR_RL_FULLRETURN | BAR_RL_NOECHO, 1) > 0) + { + BarUiDispatch(app, buf[0], app->curStation, app->playlist, true, + BAR_DC_GLOBAL); + } } /* fetch new playlist */ -static void BarMainGetPlaylist (BarApp_t *app) { - PianoReturn_t pRet; - PianoRequestDataGetPlaylist_t reqData; - reqData.station = app->curStation; - reqData.quality = app->settings.audioQuality; - - BarUiMsg (&app->settings, MSG_INFO, "Receiving new playlist... "); - if (!BarUiPianoCall (app, PIANO_REQUEST_GET_PLAYLIST, - &reqData, &pRet)) { - app->curStation = NULL; - } else { - app->playlist = reqData.retPlaylist; - if (app->playlist == NULL) { - BarUiMsg (&app->settings, MSG_INFO, "No tracks left.\n"); - app->curStation = NULL; - } - } - BarUiStartEventCmd (&app->settings, "stationfetchplaylist", - app->curStation, app->playlist, &app->player, app->ph.stations, - pRet); +static void BarMainGetPlaylist(BarApp_t *app) +{ + PianoReturn_t pRet; + PianoRequestDataGetPlaylist_t reqData; + reqData.station = app->curStation; + reqData.quality = app->settings.audioQuality; + + BarUiMsg(&app->settings, MSG_INFO, "Receiving new playlist... "); + if (!BarUiPianoCall(app, PIANO_REQUEST_GET_PLAYLIST, + &reqData, &pRet)) + { + app->curStation = NULL; + } + else + { + app->playlist = reqData.retPlaylist; + if (app->playlist == NULL) + { + BarUiMsg(&app->settings, MSG_INFO, "No tracks left.\n"); + app->curStation = NULL; + } + } + BarUiStartEventCmd(&app->settings, "stationfetchplaylist", + app->curStation, app->playlist, &app->player, app->ph.stations, + pRet); } /* start new player thread */ -static void BarMainStartPlayback (BarApp_t *app) { - assert (app != NULL); - - const PianoSong_t * const curSong = app->playlist; - assert (curSong != NULL); - - BarUiPrintSong (&app->settings, curSong, app->curStation->isQuickMix ? - PianoFindStationById (app->ph.stations, - curSong->stationId) : NULL); - - static const char httpPrefix[] = "http://"; - /* avoid playing local files */ - if (curSong->audioUrl == NULL || - strncmp (curSong->audioUrl, httpPrefix, strlen (httpPrefix)) != 0) { - BarUiMsg (&app->settings, MSG_ERR, "Invalid song url.\n"); - } else { - BarPlayer2SetGain(app->player, curSong->fileGain); - BarPlayer2Open(app->player, curSong->audioUrl); - - /* throw event */ - BarUiStartEventCmd (&app->settings, "songstart", - app->curStation, curSong, &app->player, app->ph.stations, - PIANO_RET_OK); - - if (!BarPlayer2Play(app->player)) - ++app->playerErrors; - else - app->playerErrors = 0; - } +static void BarMainStartPlayback(BarApp_t *app) +{ + assert(app != NULL); + + const PianoSong_t * const curSong = app->playlist; + assert(curSong != NULL); + + BarUiPrintSong(&app->settings, curSong, app->curStation->isQuickMix ? + PianoFindStationById(app->ph.stations, + curSong->stationId) : NULL); + + static const char httpPrefix[] = "http://"; + /* avoid playing local files */ + if (curSong->audioUrl == NULL || + strncmp(curSong->audioUrl, httpPrefix, strlen(httpPrefix)) != 0) + { + BarUiMsg(&app->settings, MSG_ERR, "Invalid song url.\n"); + } + else + { + BarPlayer2SetGain(app->player, curSong->fileGain); + BarPlayer2Open(app->player, curSong->audioUrl); + + /* throw event */ + BarUiStartEventCmd(&app->settings, "songstart", + app->curStation, curSong, &app->player, app->ph.stations, + PIANO_RET_OK); + + if (!BarPlayer2Play(app->player)) + ++app->playerErrors; + else + app->playerErrors = 0; + } } /* player is done, clean up */ -static void BarMainPlayerCleanup (BarApp_t *app) { - BarUiStartEventCmd (&app->settings, "songfinish", app->curStation, - app->playlist, &app->player, app->ph.stations, PIANO_RET_OK); +static void BarMainPlayerCleanup(BarApp_t *app) +{ + BarUiStartEventCmd(&app->settings, "songfinish", app->curStation, + app->playlist, &app->player, app->ph.stations, PIANO_RET_OK); - BarPlayer2Finish(app->player); + BarPlayer2Finish(app->player); - BarConsoleSetTitle (TITLE); + BarConsoleSetTitle(TITLE); - if (app->playerErrors >= app->settings.maxPlayerErrors) { - /* don't continue playback if thread reports too many error */ - app->curStation = NULL; - app->playerErrors = 0; - } + if (app->playerErrors >= app->settings.maxPlayerErrors) + { + /* don't continue playback if thread reports too many error */ + app->curStation = NULL; + app->playerErrors = 0; + } } /* print song duration */ -static void BarMainPrintTime (BarApp_t *app) { - double songPlayed, songDuration, songRemaining; - char sign; - - songDuration = BarPlayer2GetDuration(app->player); - songPlayed = BarPlayer2GetTime(app->player); - - if (songPlayed <= songDuration) { - songRemaining = songDuration - songPlayed; - sign = '-'; - } else { - /* longer than expected */ - songRemaining = songPlayed - songDuration; - sign = '+'; - } - BarUiMsg (&app->settings, MSG_TIME, "%c%02u:%02u/%02u:%02u\r", - sign, (int)songRemaining / 60, (int)songRemaining % 60, - (int)songDuration / 60, (int)songDuration % 60); +static void BarMainPrintTime(BarApp_t *app) +{ + double songPlayed, songDuration, songRemaining; + char sign; + + songDuration = BarPlayer2GetDuration(app->player); + songPlayed = BarPlayer2GetTime(app->player); + + if (songPlayed <= songDuration) + { + songRemaining = songDuration - songPlayed; + sign = '-'; + } + else + { + /* longer than expected */ + songRemaining = songPlayed - songDuration; + sign = '+'; + } + BarUiMsg(&app->settings, MSG_TIME, "%c%02u:%02u/%02u:%02u\r", + sign, (int)songRemaining / 60, (int)songRemaining % 60, + (int)songDuration / 60, (int)songDuration % 60); } /* main loop */ -static void BarMainLoop (BarApp_t *app) { - if (!BarMainGetLoginCredentials (&app->settings, app->rl)) { - return; - } - - if (!BarMainLoginUser (app)) { - return; - } - - if (!BarMainGetStations (app)) { - return; - } - - BarMainGetInitialStation (app); - - while (!app->doQuit) { - /* song finished playing, clean up things/scrobble song */ - if (BarPlayer2IsStopped(app->player)) { - BarMainPlayerCleanup (app); - } - - /* check whether player finished playing and start playing new - * song */ - if (BarPlayer2IsFinished(app->player) && app->curStation != NULL) { - /* what's next? */ - if (app->playlist != NULL) { - PianoSong_t *histsong = app->playlist; - app->playlist = PianoListNextP (app->playlist); - histsong->head.next = NULL; - BarUiHistoryPrepend (app, histsong); - } - if (app->playlist == NULL) { - BarMainGetPlaylist (app); - } - /* song ready to play */ - if (app->playlist != NULL) { - BarMainStartPlayback (app); - } - } - - BarMainHandleUserInput (app); - - /* show time */ - if (BarPlayer2IsPlaying(app->player) || BarPlayer2IsPaused(app->player)) { - BarMainPrintTime (app); - } - } +static void BarMainLoop(BarApp_t *app) +{ + if (!BarMainGetLoginCredentials(&app->settings, app->rl)) + { + return; + } + + if (!BarMainLoginUser(app)) + { + return; + } + + if (!BarMainGetStations(app)) + { + return; + } + + BarMainGetInitialStation(app); + + while (!app->doQuit) + { + /* song finished playing, clean up things/scrobble song */ + if (BarPlayer2IsStopped(app->player)) + { + BarMainPlayerCleanup(app); + } + + /* check whether player finished playing and start playing new + * song */ + if (BarPlayer2IsFinished(app->player) && app->curStation != NULL) + { + /* what's next? */ + if (app->playlist != NULL) + { + PianoSong_t *histsong = app->playlist; + app->playlist = PianoListNextP(app->playlist); + histsong->head.next = NULL; + BarUiHistoryPrepend(app, histsong); + } + if (app->playlist == NULL) + { + BarMainGetPlaylist(app); + } + /* song ready to play */ + if (app->playlist != NULL) + { + BarMainStartPlayback(app); + } + } + + BarMainHandleUserInput(app); + + /* show time */ + if (BarPlayer2IsPlaying(app->player) || BarPlayer2IsPaused(app->player)) + { + BarMainPrintTime(app); + } + } } -int main (int argc, char **argv) { - static BarApp_t app; +int main(int argc, char **argv) +{ + static BarApp_t app; - memset (&app, 0, sizeof (app)); + memset(&app, 0, sizeof(app)); - BarConsoleInit (); + BarConsoleInit(); - BarConsoleSetTitle (TITLE); + BarConsoleSetTitle(TITLE); - /* init some things */ - BarSettingsInit (&app.settings); - BarSettingsRead (&app.settings); + /* init some things */ + BarSettingsInit(&app.settings); + BarSettingsRead(&app.settings); - if (!BarPlayer2Init (&app.player, app.settings.player)) + if (!BarPlayer2Init(&app.player, app.settings.player)) { if (app.settings.player) BarUiMsg(&app.settings, MSG_ERR, "Player \"%s\" initialization failed.", app.settings.player); @@ -349,47 +392,51 @@ int main (int argc, char **argv) { return 0; } - PianoReturn_t pret; - if ((pret = PianoInit (&app.ph, app.settings.partnerUser, - app.settings.partnerPassword, app.settings.device, - app.settings.inkey, app.settings.outkey)) != PIANO_RET_OK) { - BarUiMsg (&app.settings, MSG_ERR, "Initialization failed:" - " %s\n", PianoErrorToStr (pret)); - return 0; - } + PianoReturn_t pret; + if ((pret = PianoInit(&app.ph, app.settings.partnerUser, + app.settings.partnerPassword, app.settings.device, + app.settings.inkey, app.settings.outkey)) != PIANO_RET_OK) + { + BarUiMsg(&app.settings, MSG_ERR, "Initialization failed:" + " %s\n", PianoErrorToStr(pret)); + return 0; + } - BarUiMsg (&app.settings, MSG_NONE, - "Welcome to " PACKAGE " (" VERSION ")!\n"); - if (app.settings.keys[BAR_KS_HELP] == BAR_KS_DISABLED) { - BarUiMsg (&app.settings, MSG_NONE, "\n"); - } else { - BarUiMsg (&app.settings, MSG_NONE, - "Press %c for a list of commands.\n", - app.settings.keys[BAR_KS_HELP]); - } + BarUiMsg(&app.settings, MSG_NONE, + "Welcome to " PACKAGE " (" VERSION ")!\n"); + if (app.settings.keys[BAR_KS_HELP] == BAR_KS_DISABLED) + { + BarUiMsg(&app.settings, MSG_NONE, "\n"); + } + else + { + BarUiMsg(&app.settings, MSG_NONE, + "Press %c for a list of commands.\n", + app.settings.keys[BAR_KS_HELP]); + } - HttpInit(&app.http2, app.settings.rpcHost, app.settings.rpcTlsPort); - if (app.settings.controlProxy) - HttpSetProxy(app.http2, app.settings.controlProxy); + HttpInit(&app.http2, app.settings.rpcHost, app.settings.rpcTlsPort); + if (app.settings.controlProxy) + HttpSetProxy(app.http2, app.settings.controlProxy); - BarReadlineInit (&app.rl); + BarReadlineInit(&app.rl); - BarMainLoop (&app); + BarMainLoop(&app); - BarReadlineDestroy (app.rl); + BarReadlineDestroy(app.rl); - /* write statefile */ - BarSettingsWrite (app.curStation, &app.settings); + /* write statefile */ + BarSettingsWrite(app.curStation, &app.settings); - PianoDestroy (&app.ph); - PianoDestroyPlaylist (app.songHistory); - PianoDestroyPlaylist (app.playlist); - HttpDestroy (app.http2); - BarPlayer2Destroy (app.player); - BarSettingsDestroy (&app.settings); - BarConsoleDestroy (); + PianoDestroy(&app.ph); + PianoDestroyPlaylist(app.songHistory); + PianoDestroyPlaylist(app.playlist); + HttpDestroy(app.http2); + BarPlayer2Destroy(app.player); + BarSettingsDestroy(&app.settings); + BarConsoleDestroy(); - return 0; + return 0; } -- cgit v1.2.3 From 817ff6a87020a55d7628d600e2ee09b947ee2f4e Mon Sep 17 00:00:00 2001 From: Michał Cichoń Date: Wed, 17 May 2017 04:07:07 +0200 Subject: Show status code text if no other source of error is available. --- src/http/http.c | 21 +++++++++++- src/main.c | 100 ++++++++++++++++++++++++++------------------------------ src/main.h | 4 ++- src/ui.c | 3 +- src/ui_act.c | 18 +++++----- 5 files changed, 81 insertions(+), 65 deletions(-) (limited to 'src/main.c') diff --git a/src/http/http.c b/src/http/http.c index dd64ad1..609317f 100644 --- a/src/http/http.c +++ b/src/http/http.c @@ -44,6 +44,7 @@ static wchar_t* HttpToWideString(const char* string, int size); static bool HttpCreateConnection (http_t http); static void HttpCloseConnection (http_t http); static void HttpSetLastError (http_t http, const char* message); +static void HttpSetLastErrorW (http_t http, const wchar_t* message); static void HttpSetLastErrorFromWinHttp (http_t http); static char* HttpFormatWinApiError (DWORD errorCode, HINSTANCE module); static char* HttpFormatWinHttpError (DWORD errorCode); @@ -118,11 +119,19 @@ static void HttpCloseConnection (http_t http) { static void HttpSetLastError (http_t http, const char* message) { free(http->error); http->error = NULL; - + if (message) http->error = strdup(message); } +static void HttpSetLastErrorW (http_t http, const wchar_t* message) { + free(http->error); + http->error = NULL; + + if (message) + http->error = HttpToString(message, wcslen(message)); +} + static void HttpSetLastErrorFromWinHttp (http_t http) { free(http->error); http->error = NULL; @@ -396,6 +405,13 @@ bool HttpRequest(http_t http, PianoRequest_t * const request) { } if (succeeded && statusCode == 407) { + wchar_t statusText[256] = { 0 }; + DWORD statusTextSize = sizeof(statusText) - 1; + WinHttpQueryHeaders(handle, + WINHTTP_QUERY_STATUS_TEXT, + WINHTTP_HEADER_NAME_BY_INDEX, + statusText, &statusTextSize, WINHTTP_NO_HEADER_INDEX); + HttpSetLastErrorW (http, statusText); requestSent = false; retry = true; } @@ -467,6 +483,9 @@ bool HttpRequest(http_t http, PianoRequest_t * const request) { HttpSetLastError (http, "Maximum retries count exceeded"); } + if (retryLimit == 0) + goto done; + complete = true; HttpSetLastError (http, NULL); diff --git a/src/main.c b/src/main.c index eca4705..6e99ada 100644 --- a/src/main.c +++ b/src/main.c @@ -158,29 +158,21 @@ static bool BarMainGetStations(BarApp_t *app) /* get initial station from autostart setting or user rl */ -static void BarMainGetInitialStation(BarApp_t *app) -{ - /* try to get autostart station */ - if (app->settings.autostartStation != NULL) - { - app->curStation = PianoFindStationById(app->ph.stations, - app->settings.autostartStation); - if (app->curStation == 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, - "Select station: ", NULL, app->settings.autoselect); - } - if (app->curStation != NULL) - { - BarUiPrintStation(&app->settings, app->curStation); - } +static void BarMainGetInitialStation (BarApp_t *app) { + /* try to get autostart station */ + if (app->settings.autostartStation != NULL) { + app->nextStation = PianoFindStationById (app->ph.stations, + app->settings.autostartStation); + if (app->nextStation == NULL) { + BarUiMsg (&app->settings, MSG_ERR, + "Error: Autostart station not found.\n"); + } + } + /* no autostart? ask the user */ + if (app->nextStation == NULL) { + app->nextStation = BarUiSelectStation (app, app->ph.stations, + "Select station: ", NULL, app->settings.autoselect); + } } /* wait for user rl @@ -198,31 +190,27 @@ static void BarMainHandleUserInput(BarApp_t *app) /* fetch new playlist */ -static void BarMainGetPlaylist(BarApp_t *app) -{ - PianoReturn_t pRet; - PianoRequestDataGetPlaylist_t reqData; - reqData.station = app->curStation; - reqData.quality = app->settings.audioQuality; - - BarUiMsg(&app->settings, MSG_INFO, "Receiving new playlist... "); - if (!BarUiPianoCall(app, PIANO_REQUEST_GET_PLAYLIST, - &reqData, &pRet)) - { - app->curStation = NULL; - } - else - { - app->playlist = reqData.retPlaylist; - if (app->playlist == NULL) - { - BarUiMsg(&app->settings, MSG_INFO, "No tracks left.\n"); - app->curStation = NULL; - } - } - BarUiStartEventCmd(&app->settings, "stationfetchplaylist", - app->curStation, app->playlist, &app->player, app->ph.stations, - pRet); +static void BarMainGetPlaylist (BarApp_t *app) { + PianoReturn_t pRet; + PianoRequestDataGetPlaylist_t reqData; + 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)) { + app->nextStation = NULL; + } else { + app->playlist = reqData.retPlaylist; + if (app->playlist == NULL) { + BarUiMsg (&app->settings, MSG_INFO, "No tracks left.\n"); + app->nextStation = NULL; + } + } + app->curStation = app->nextStation; + BarUiStartEventCmd (&app->settings, "stationfetchplaylist", + app->curStation, app->playlist, &app->player, app->ph.stations, + pRet); } /* start new player thread @@ -276,7 +264,7 @@ static void BarMainPlayerCleanup(BarApp_t *app) if (app->playerErrors >= app->settings.maxPlayerErrors) { /* don't continue playback if thread reports too many error */ - app->curStation = NULL; + app->nextStation = NULL; app->playerErrors = 0; } } @@ -338,7 +326,7 @@ static void BarMainLoop(BarApp_t *app) /* check whether player finished playing and start playing new * song */ - if (BarPlayer2IsFinished(app->player) && app->curStation != NULL) + if (BarPlayer2IsFinished(app->player) && app->nextStation != NULL) { /* what's next? */ if (app->playlist != NULL) @@ -348,10 +336,14 @@ static void BarMainLoop(BarApp_t *app) histsong->head.next = NULL; BarUiHistoryPrepend(app, histsong); } - if (app->playlist == NULL) - { - BarMainGetPlaylist(app); - } + 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 */ if (app->playlist != NULL) { diff --git a/src/main.h b/src/main.h index 6c34d71..e5e988d 100644 --- a/src/main.h +++ b/src/main.h @@ -42,7 +42,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; char doQuit; BarReadline_t rl; unsigned int playerErrors; diff --git a/src/ui.c b/src/ui.c index 8efc7aa..88ad4d7 100644 --- a/src/ui.c +++ b/src/ui.c @@ -715,7 +715,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 4206a33..cb1d7a8 100644 --- a/src/ui_act.c +++ b/src/ui_act.c @@ -224,10 +224,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"); @@ -446,14 +450,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 From 8576aa5186a2cdd4f4bf44ec34c80e90d04de969 Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Fri, 2 Dec 2016 15:03:10 +0100 Subject: Exit when no email/password was entered or ^C was pressed Fixes #600. # Conflicts: # src/main.c --- src/main.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 6e99ada..3e6e85f 100644 --- a/src/main.c +++ b/src/main.c @@ -64,7 +64,8 @@ static bool BarMainGetLoginCredentials(BarSettings_t *settings, char nameBuf[100]; BarUiMsg(settings, MSG_QUESTION, "Email: "); - BarReadlineStr(nameBuf, sizeof(nameBuf), rl, BAR_RL_DEFAULT); + if (BarReadlineStr(nameBuf, sizeof(nameBuf), rl, BAR_RL_DEFAULT) == 0) + return false; settings->username = strdup(nameBuf); usernameFromConfig = false; } @@ -78,18 +79,20 @@ static bool BarMainGetLoginCredentials(BarSettings_t *settings, BarUiMsg(settings, MSG_QUESTION, "Email: %s\n", settings->username); } - if (settings->passwordCmd == NULL) - { - BarUiMsg(settings, MSG_QUESTION, "Password: "); - BarReadlineStr(passBuf, sizeof(passBuf), rl, BAR_RL_NOECHO); + if (settings->passwordCmd == NULL) { + BarUiMsg (settings, MSG_QUESTION, "Password: "); + if (BarReadlineStr (passBuf, sizeof (passBuf), rl, BAR_RL_NOECHO) == 0) { + BarConsolePutc('\n'); + return false; + } /* write missing newline */ BarConsolePutc('\n'); - settings->password = strdup(passBuf); - } + settings->password = strdup (passBuf); + } else { - //pid_t chld; - //int pipeFd[2]; + //pid_t chld; + //int pipeFd[2]; //BarUiMsg (settings, MSG_INFO, "Requesting password from external helper... "); -- cgit v1.2.3 From c934c373e16acc7b7db6a374b1047649a0875dc3 Mon Sep 17 00:00:00 2001 From: Sean Greenslade Date: Tue, 14 Mar 2017 19:36:24 -0700 Subject: Added gain_mul setting to soften effect of replaygain. --- contrib/pianobar.1 | 6 ++++++ src/main.c | 3 ++- src/settings.c | 3 +++ src/settings.h | 1 + 4 files changed, 12 insertions(+), 1 deletion(-) (limited to 'src/main.c') diff --git a/contrib/pianobar.1 b/contrib/pianobar.1 index 910af9a..0619735 100644 --- a/contrib/pianobar.1 +++ b/contrib/pianobar.1 @@ -366,6 +366,12 @@ Your pandora.com username. .B volume = 0 Initial volume correction in dB. Usually between -30 and +5. +.TP +.B gain_mul = 1.0 +Pandora sends a ReplayGain value with every song. This sets a multiplier so that the gain adjustment can be +reduced. 0.0 means no gain adjustment, 1.0 means full gain adjustment, values inbetween reduce the magnitude +of gain adjustment. + .SH REMOTE CONTROL .B pianobar can be controlled through a fifo. You have to create it yourself by executing diff --git a/src/main.c b/src/main.c index 3e6e85f..4f4214a 100644 --- a/src/main.c +++ b/src/main.c @@ -31,6 +31,7 @@ THE SOFTWARE. #include "ui.h" #include "ui_dispatch.h" #include "ui_readline.h" +#include "settings.h" /* authenticate user */ @@ -238,7 +239,7 @@ static void BarMainStartPlayback(BarApp_t *app) } else { - BarPlayer2SetGain(app->player, curSong->fileGain); + BarPlayer2SetGain(app->player, curSong->fileGain * app->settings.gainMul); BarPlayer2Open(app->player, curSong->audioUrl); /* throw event */ diff --git a/src/settings.c b/src/settings.c index b15053d..4f0ef8c 100644 --- a/src/settings.c +++ b/src/settings.c @@ -172,6 +172,7 @@ void BarSettingsRead (BarSettings_t *settings) { settings->autoselect = true; settings->history = 5; settings->volume = 0; + settings->gainMul = 1.0; settings->maxPlayerErrors = 5; settings->sortOrder = BAR_SORT_NAME_AZ; settings->loveIcon = strdup (" <3"); @@ -378,6 +379,8 @@ void BarSettingsRead (BarSettings_t *settings) { settings->atIcon = strdup (val); } else if (streq ("volume", key)) { settings->volume = atoi (val); + } else if (streq ("gain_mul", key)) { + settings->gainMul = (float)atof (val); } else if (streq ("format_nowplaying_song", key)) { free (settings->npSongFormat); settings->npSongFormat = strdup (val); diff --git a/src/settings.h b/src/settings.h index e6067f3..f8225ce 100644 --- a/src/settings.h +++ b/src/settings.h @@ -87,6 +87,7 @@ typedef struct { bool autoselect; unsigned int history, maxPlayerErrors; int volume; + float gainMul; BarStationSorting_t sortOrder; PianoAudioQuality_t audioQuality; char *username; -- cgit v1.2.3