diff --git a/src/helpers.cpp b/src/helpers.cpp index d56610ee..57d156cc 100644 --- a/src/helpers.cpp +++ b/src/helpers.cpp @@ -53,6 +53,111 @@ extern string UNKNOWN_ARTIST; extern string UNKNOWN_TITLE; extern string UNKNOWN_ALBUM; +bool ConnectToMPD() +{ + if (!Mpd->Connect()) + { + printf("Cannot connect to mpd: %s\n", Mpd->GetErrorMessage().c_str()); + return false; + } + return true; +} + +bool ParseArgv(vector &v) +{ + using std::cout; + using std::endl; + + bool exit = 0; + for (vector::iterator it = v.begin(); it != v.end() && !exit; it++) + { + if (*it == "-v" || *it == "--version") + { + cout << "ncmpcpp version: " << VERSION << endl + << "build with support for:" +# ifdef HAVE_CURL_CURL_H + << " curl" +# endif +# ifdef HAVE_TAGLIB_H + << " taglib" +# endif +# ifdef UTF8_ENABLED + << " unicode" +# endif + << endl; + return 1; + } + else if (*it == "-?" || *it == "--help") + { + cout + << "Usage: ncmpcpp [OPTION]...\n" + << " -?, --help show this help message\n" + << " -v, --version display version information\n\n" + << " play start playing\n" + << " pause pause the currently playing song\n" + << " toggle toggle play/pause mode\n" + << " next play the next song\n" + << " prev play the previous song\n" + << " volume [+-] adjusts volume by [+-]\n" + ; + return 1; + } + + if (!ConnectToMPD()) + return 1; + + if (*it == "play") + { + Mpd->Play(); + exit = 1; + } + else if (*it == "pause") + { + Mpd->Execute("pause \"1\"\n"); + exit = 1; + } + else if (*it == "toggle") + { + Mpd->Execute("pause\n"); + exit = 1; + } + else if (*it == "next") + { + Mpd->Next(); + exit = 1; + } + else if (*it == "prev") + { + Mpd->Prev(); + exit = 1; + } + else if (*it == "volume") + { + it++; + Mpd->GetStatus(); + if (Mpd->CheckForErrors()) + { + cout << "Error: " << Mpd->GetErrorMessage() << endl; + return 1; + } + if (it != v.end()) + Mpd->SetVolume(Mpd->GetVolume()+StrToInt(*it)); + exit = 1; + } + else + { + cout << "ncmpcpp: invalid option " << *it << endl; + return 1; + } + if (Mpd->CheckForErrors()) + { + cout << "Error: " << Mpd->GetErrorMessage() << endl; + return 1; + } + } + return exit; +} + void LockStatusbar() { if (Config.statusbar_visibility) diff --git a/src/helpers.h b/src/helpers.h index 65d7d9f0..c5b51693 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -27,6 +27,9 @@ extern ncmpcpp_config Config; +bool ConnectToMPD(); +bool ParseArgv(vector &); + void LockStatusbar(); void UnlockStatusbar(); diff --git a/src/libmpdclient.c b/src/libmpdclient.c index 1aa03799..95552588 100644 --- a/src/libmpdclient.c +++ b/src/libmpdclient.c @@ -439,11 +439,11 @@ void mpd_closeConnection(mpd_Connection * connection) { WSACleanup(); } -static void mpd_executeCommand(mpd_Connection * connection, char * command) { +void mpd_executeCommand(mpd_Connection * connection, const char * command) { int ret; struct timeval tv; fd_set fds; - char * commandPtr = command; + const char * commandPtr = command; int commandLen = strlen(command); if(!connection->doneProcessing && !connection->commandList) { diff --git a/src/libmpdclient.h b/src/libmpdclient.h index 729b92a4..9834f659 100644 --- a/src/libmpdclient.h +++ b/src/libmpdclient.h @@ -149,6 +149,9 @@ void mpd_closeConnection(mpd_Connection * connection); */ void mpd_clearError(mpd_Connection * connection); +/* added by unK, make this extern as I want to use it */ +void mpd_executeCommand(mpd_Connection * connection, const char * command); + /* STATUS STUFF */ /* use these with status.state to determine what state the player is in */ diff --git a/src/mpdpp.cpp b/src/mpdpp.cpp index ec40e6e5..dc384f05 100644 --- a/src/mpdpp.cpp +++ b/src/mpdpp.cpp @@ -119,15 +119,20 @@ void MPDConnection::SetErrorHandler(ErrorHandler handler, void *data) itsErrorHandlerUserdata = data; } -void MPDConnection::UpdateStatus() +void MPDConnection::GetStatus() { - CheckForErrors(); - if (itsOldStatus) mpd_freeStatus(itsOldStatus); itsOldStatus = itsCurrentStatus; mpd_sendStatusCommand(itsConnection); itsCurrentStatus = mpd_getStatus(itsConnection); +} + +void MPDConnection::UpdateStatus() +{ + CheckForErrors(); + + GetStatus(); if (!itsMaxPlaylistLength) itsMaxPlaylistLength = GetPlaylistLength(); @@ -182,6 +187,15 @@ void MPDConnection::UpdateDirectory(const string &path) const } } +void MPDConnection::Execute(const string &command) const +{ + if (isConnected) + { + mpd_executeCommand(itsConnection, command.c_str()); + mpd_finishCommand(itsConnection); + } +} + void MPDConnection::Play() const { if (isConnected) @@ -842,7 +856,7 @@ int MPDConnection::CheckForErrors() itsErrorHandler(this, itsConnection->error, itsConnection->errorStr, itsErrorHandlerUserdata); itsErrorCode = itsConnection->error; } - itsLastErrorMessage = itsConnection->errorStr; + itsErrorMessage = itsConnection->errorStr; mpd_clearError(itsConnection); } return itsErrorCode; diff --git a/src/mpdpp.h b/src/mpdpp.h index b80c93c2..fe9ed657 100644 --- a/src/mpdpp.h +++ b/src/mpdpp.h @@ -90,9 +90,12 @@ class MPDConnection void SetStatusUpdater(StatusUpdater, void *); void SetErrorHandler(ErrorHandler, void *); + void GetStatus(); void UpdateStatus(); void UpdateDirectory(const string &) const; + void Execute(const string &) const; + void Play() const; void Play(int) const; void PlayID(int) const; @@ -119,7 +122,7 @@ class MPDConnection int GetPlaylistLength() const { return isConnected && itsCurrentStatus ? itsCurrentStatus->playlistLength : 0; } void GetPlaylistChanges(long long, SongList &) const; - string GetLastErrorMessage() const { return itsLastErrorMessage; } + string GetErrorMessage() const { return itsErrorMessage; } int GetErrorCode() const { return itsErrorCode; } Song GetCurrentSong() const; @@ -167,15 +170,16 @@ class MPDConnection void GetDirectoryRecursive(const string &, SongList &) const; void GetSongs(const string &, SongList &) const; void GetDirectories(const string &, TagList &) const; - - private: + int CheckForErrors(); + + private: void ClearQueue(); mpd_Connection *itsConnection; bool isConnected; - string itsLastErrorMessage; + string itsErrorMessage; int itsErrorCode; unsigned int itsMaxPlaylistLength; diff --git a/src/ncmpcpp.cpp b/src/ncmpcpp.cpp index 227ff2f3..28b58dd7 100644 --- a/src/ncmpcpp.cpp +++ b/src/ncmpcpp.cpp @@ -160,12 +160,22 @@ int main(int argc, char *argv[]) Mpd->SetTimeout(Config.mpd_connection_timeout); - if (!Mpd->Connect()) + if (argc > 1) { - printf("Cannot connect to mpd: %s\n", Mpd->GetLastErrorMessage().c_str()); - return -1; + vector args; + args.reserve(argc-1); + for (int i = 1; i < argc; i++) + args.push_back(argv[i]); + if (ParseArgv(args)) + { + Mpd->Disconnect(); + return 0; + } } + if (!ConnectToMPD()) + return -1; + setlocale(LC_ALL,""); initscr(); noecho();