implement argv handler and basic mpc commands

This commit is contained in:
unK
2008-10-05 22:41:29 +02:00
parent 9fd373625d
commit 4ab4e72c97
7 changed files with 152 additions and 13 deletions

View File

@@ -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<string> &v)
{
using std::cout;
using std::endl;
bool exit = 0;
for (vector<string>::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 [+-]<num> adjusts volume by [+-]<num>\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)

View File

@@ -27,6 +27,9 @@
extern ncmpcpp_config Config;
bool ConnectToMPD();
bool ParseArgv(vector<string> &);
void LockStatusbar();
void UnlockStatusbar();

View File

@@ -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) {

View File

@@ -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 */

View File

@@ -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;

View File

@@ -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;

View File

@@ -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<string> 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();