/* wait for user input */ static void BarMainHandleUserInput (BarApp_t *app) { char buf[2]; if (BarReadline (buf, sizeof (buf), NULL, &app->input, BAR_RL_FULLRETURN | BAR_RL_NOECHO, 2) > 0) { BarUiDispatch (app, buf[0], app->curStation, app->playlist, true, BAR_DC_GLOBAL); } }
/* Yes/No? * @param defaul (user presses enter) */ int BarReadlineYesNo (char def, FILE *fd) { char buf[2]; BarReadline (buf, sizeof (buf), "yYnN", 1, 0, fd); if (*buf == 'y' || *buf == 'Y' || (def == 1 && *buf == '\0')) { return 1; } else { return 0; } }
/* Read int from stdin * @param write result into this variable * @return number of bytes read from stdin */ size_t BarReadlineInt (int *ret, FILE *fd) { int rlRet = 0; char buf[16]; rlRet = BarReadline (buf, sizeof (buf), "0123456789", 0, 0, fd); *ret = atoi ((char *) buf); return rlRet; }
/* search music: query, search request, return music id * @param app handle * @param seed suggestion station * @param seed suggestion musicid * @param prompt string * @return musicId or NULL on abort/error */ char *BarUiSelectMusicId (BarApp_t *app, PianoStation_t *station, const char *msg) { char *musicId = NULL; char lineBuf[100], selectBuf[2]; PianoSearchResult_t searchResult; PianoArtist_t *tmpArtist; PianoSong_t *tmpSong; BarUiMsg (&app->settings, MSG_QUESTION, msg); if (BarReadlineStr (lineBuf, sizeof (lineBuf), &app->input, BAR_RL_DEFAULT) > 0) { PianoReturn_t pRet; WaitressReturn_t wRet; PianoRequestDataSearch_t reqData; reqData.searchStr = lineBuf; BarUiMsg (&app->settings, MSG_INFO, "Searching... "); if (!BarUiPianoCall (app, PIANO_REQUEST_SEARCH, &reqData, &pRet, &wRet)) { return NULL; } memcpy (&searchResult, &reqData.searchResult, sizeof (searchResult)); BarUiMsg (&app->settings, MSG_NONE, "\r"); if (searchResult.songs != NULL && searchResult.artists != NULL) { /* songs and artists found */ BarUiMsg (&app->settings, MSG_QUESTION, "Is this an [a]rtist or [t]rack name? "); BarReadline (selectBuf, sizeof (selectBuf), "at", &app->input, BAR_RL_FULLRETURN, -1); if (*selectBuf == 'a') { tmpArtist = BarUiSelectArtist (app, searchResult.artists); if (tmpArtist != NULL) { musicId = strdup (tmpArtist->musicId); } } else if (*selectBuf == 't') { tmpSong = BarUiSelectSong (&app->settings, searchResult.songs, &app->input); if (tmpSong != NULL) { musicId = strdup (tmpSong->musicId); } } } else if (searchResult.songs != NULL) { /* songs found */ tmpSong = BarUiSelectSong (&app->settings, searchResult.songs, &app->input); if (tmpSong != NULL) { musicId = strdup (tmpSong->musicId); } } else if (searchResult.artists != NULL) { /* artists found */ tmpArtist = BarUiSelectArtist (app, searchResult.artists); if (tmpArtist != NULL) { musicId = strdup (tmpArtist->musicId); } } else { BarUiMsg (&app->settings, MSG_INFO, "Nothing found...\n"); } PianoDestroySearchResult (&searchResult); } return musicId; }
/* Read string from stdin * @param buffer * @param buffer size * @return number of bytes read from stdin */ size_t BarReadlineStr (char *buf, size_t bufSize, char noEcho, FILE *fd) { return BarReadline (buf, bufSize, NULL, 0, noEcho, fd); }
/* search music: query, search request, return music id * @param piano handle * @param read data from fd * @param allow seed suggestions if != NULL * @return musicId or NULL on abort/error */ char *BarUiSelectMusicId (PianoHandle_t *ph, WaitressHandle_t *waith, FILE *curFd, char *similarToId) { char *musicId = NULL; char lineBuf[100], selectBuf[2]; PianoSearchResult_t searchResult; PianoArtist_t *tmpArtist; PianoSong_t *tmpSong; BarUiMsg (MSG_QUESTION, "Search for artist/title: "); if (BarReadlineStr (lineBuf, sizeof (lineBuf), 0, curFd) > 0) { if (strcmp ("?", lineBuf) == 0 && similarToId != NULL) { PianoReturn_t pRet; WaitressReturn_t wRet; PianoRequestDataGetSeedSuggestions_t reqData; reqData.musicId = similarToId; reqData.max = 20; BarUiMsg (MSG_INFO, "Receiving suggestions... "); if (!BarUiPianoCall (ph, PIANO_REQUEST_GET_SEED_SUGGESTIONS, waith, &reqData, &pRet, &wRet)) { return NULL; } memcpy (&searchResult, &reqData.searchResult, sizeof (searchResult)); } else { PianoReturn_t pRet; WaitressReturn_t wRet; PianoRequestDataSearch_t reqData; reqData.searchStr = lineBuf; BarUiMsg (MSG_INFO, "Searching... "); if (!BarUiPianoCall (ph, PIANO_REQUEST_SEARCH, waith, &reqData, &pRet, &wRet)) { return NULL; } memcpy (&searchResult, &reqData.searchResult, sizeof (searchResult)); } BarUiMsg (MSG_NONE, "\r"); if (searchResult.songs != NULL && searchResult.artists != NULL) { /* songs and artists found */ BarUiMsg (MSG_QUESTION, "Is this an [a]rtist or [t]rack name? "); BarReadline (selectBuf, sizeof (selectBuf), "at", 1, 0, curFd); if (*selectBuf == 'a') { tmpArtist = BarUiSelectArtist (searchResult.artists, curFd); if (tmpArtist != NULL) { musicId = strdup (tmpArtist->musicId); } } else if (*selectBuf == 't') { tmpSong = BarUiSelectSong (searchResult.songs, curFd); if (tmpSong != NULL) { musicId = strdup (tmpSong->musicId); } } } else if (searchResult.songs != NULL) { /* songs found */ tmpSong = BarUiSelectSong (searchResult.songs, curFd); if (tmpSong != NULL) { musicId = strdup (tmpSong->musicId); } } else if (searchResult.artists != NULL) { /* artists found */ tmpArtist = BarUiSelectArtist (searchResult.artists, curFd); if (tmpArtist != NULL) { musicId = strdup (tmpArtist->musicId); } } else { BarUiMsg (MSG_INFO, "Nothing found...\n"); } PianoDestroySearchResult (&searchResult); } return musicId; }