implement argv handler and basic mpc commands
This commit is contained in:
105
src/helpers.cpp
105
src/helpers.cpp
@@ -53,6 +53,111 @@ extern string UNKNOWN_ARTIST;
|
|||||||
extern string UNKNOWN_TITLE;
|
extern string UNKNOWN_TITLE;
|
||||||
extern string UNKNOWN_ALBUM;
|
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()
|
void LockStatusbar()
|
||||||
{
|
{
|
||||||
if (Config.statusbar_visibility)
|
if (Config.statusbar_visibility)
|
||||||
|
|||||||
@@ -27,6 +27,9 @@
|
|||||||
|
|
||||||
extern ncmpcpp_config Config;
|
extern ncmpcpp_config Config;
|
||||||
|
|
||||||
|
bool ConnectToMPD();
|
||||||
|
bool ParseArgv(vector<string> &);
|
||||||
|
|
||||||
void LockStatusbar();
|
void LockStatusbar();
|
||||||
void UnlockStatusbar();
|
void UnlockStatusbar();
|
||||||
|
|
||||||
|
|||||||
@@ -439,11 +439,11 @@ void mpd_closeConnection(mpd_Connection * connection) {
|
|||||||
WSACleanup();
|
WSACleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void mpd_executeCommand(mpd_Connection * connection, char * command) {
|
void mpd_executeCommand(mpd_Connection * connection, const char * command) {
|
||||||
int ret;
|
int ret;
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
fd_set fds;
|
fd_set fds;
|
||||||
char * commandPtr = command;
|
const char * commandPtr = command;
|
||||||
int commandLen = strlen(command);
|
int commandLen = strlen(command);
|
||||||
|
|
||||||
if(!connection->doneProcessing && !connection->commandList) {
|
if(!connection->doneProcessing && !connection->commandList) {
|
||||||
|
|||||||
@@ -149,6 +149,9 @@ void mpd_closeConnection(mpd_Connection * connection);
|
|||||||
*/
|
*/
|
||||||
void mpd_clearError(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 */
|
/* STATUS STUFF */
|
||||||
|
|
||||||
/* use these with status.state to determine what state the player is in */
|
/* use these with status.state to determine what state the player is in */
|
||||||
|
|||||||
@@ -119,15 +119,20 @@ void MPDConnection::SetErrorHandler(ErrorHandler handler, void *data)
|
|||||||
itsErrorHandlerUserdata = data;
|
itsErrorHandlerUserdata = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MPDConnection::UpdateStatus()
|
void MPDConnection::GetStatus()
|
||||||
{
|
{
|
||||||
CheckForErrors();
|
|
||||||
|
|
||||||
if (itsOldStatus)
|
if (itsOldStatus)
|
||||||
mpd_freeStatus(itsOldStatus);
|
mpd_freeStatus(itsOldStatus);
|
||||||
itsOldStatus = itsCurrentStatus;
|
itsOldStatus = itsCurrentStatus;
|
||||||
mpd_sendStatusCommand(itsConnection);
|
mpd_sendStatusCommand(itsConnection);
|
||||||
itsCurrentStatus = mpd_getStatus(itsConnection);
|
itsCurrentStatus = mpd_getStatus(itsConnection);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MPDConnection::UpdateStatus()
|
||||||
|
{
|
||||||
|
CheckForErrors();
|
||||||
|
|
||||||
|
GetStatus();
|
||||||
|
|
||||||
if (!itsMaxPlaylistLength)
|
if (!itsMaxPlaylistLength)
|
||||||
itsMaxPlaylistLength = GetPlaylistLength();
|
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
|
void MPDConnection::Play() const
|
||||||
{
|
{
|
||||||
if (isConnected)
|
if (isConnected)
|
||||||
@@ -842,7 +856,7 @@ int MPDConnection::CheckForErrors()
|
|||||||
itsErrorHandler(this, itsConnection->error, itsConnection->errorStr, itsErrorHandlerUserdata);
|
itsErrorHandler(this, itsConnection->error, itsConnection->errorStr, itsErrorHandlerUserdata);
|
||||||
itsErrorCode = itsConnection->error;
|
itsErrorCode = itsConnection->error;
|
||||||
}
|
}
|
||||||
itsLastErrorMessage = itsConnection->errorStr;
|
itsErrorMessage = itsConnection->errorStr;
|
||||||
mpd_clearError(itsConnection);
|
mpd_clearError(itsConnection);
|
||||||
}
|
}
|
||||||
return itsErrorCode;
|
return itsErrorCode;
|
||||||
|
|||||||
10
src/mpdpp.h
10
src/mpdpp.h
@@ -90,9 +90,12 @@ class MPDConnection
|
|||||||
|
|
||||||
void SetStatusUpdater(StatusUpdater, void *);
|
void SetStatusUpdater(StatusUpdater, void *);
|
||||||
void SetErrorHandler(ErrorHandler, void *);
|
void SetErrorHandler(ErrorHandler, void *);
|
||||||
|
void GetStatus();
|
||||||
void UpdateStatus();
|
void UpdateStatus();
|
||||||
void UpdateDirectory(const string &) const;
|
void UpdateDirectory(const string &) const;
|
||||||
|
|
||||||
|
void Execute(const string &) const;
|
||||||
|
|
||||||
void Play() const;
|
void Play() const;
|
||||||
void Play(int) const;
|
void Play(int) const;
|
||||||
void PlayID(int) const;
|
void PlayID(int) const;
|
||||||
@@ -119,7 +122,7 @@ class MPDConnection
|
|||||||
int GetPlaylistLength() const { return isConnected && itsCurrentStatus ? itsCurrentStatus->playlistLength : 0; }
|
int GetPlaylistLength() const { return isConnected && itsCurrentStatus ? itsCurrentStatus->playlistLength : 0; }
|
||||||
void GetPlaylistChanges(long long, SongList &) const;
|
void GetPlaylistChanges(long long, SongList &) const;
|
||||||
|
|
||||||
string GetLastErrorMessage() const { return itsLastErrorMessage; }
|
string GetErrorMessage() const { return itsErrorMessage; }
|
||||||
int GetErrorCode() const { return itsErrorCode; }
|
int GetErrorCode() const { return itsErrorCode; }
|
||||||
|
|
||||||
Song GetCurrentSong() const;
|
Song GetCurrentSong() const;
|
||||||
@@ -168,14 +171,15 @@ class MPDConnection
|
|||||||
void GetSongs(const string &, SongList &) const;
|
void GetSongs(const string &, SongList &) const;
|
||||||
void GetDirectories(const string &, TagList &) const;
|
void GetDirectories(const string &, TagList &) const;
|
||||||
|
|
||||||
private:
|
|
||||||
int CheckForErrors();
|
int CheckForErrors();
|
||||||
|
|
||||||
|
private:
|
||||||
void ClearQueue();
|
void ClearQueue();
|
||||||
|
|
||||||
mpd_Connection *itsConnection;
|
mpd_Connection *itsConnection;
|
||||||
bool isConnected;
|
bool isConnected;
|
||||||
|
|
||||||
string itsLastErrorMessage;
|
string itsErrorMessage;
|
||||||
int itsErrorCode;
|
int itsErrorCode;
|
||||||
unsigned int itsMaxPlaylistLength;
|
unsigned int itsMaxPlaylistLength;
|
||||||
|
|
||||||
|
|||||||
@@ -160,12 +160,22 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
Mpd->SetTimeout(Config.mpd_connection_timeout);
|
Mpd->SetTimeout(Config.mpd_connection_timeout);
|
||||||
|
|
||||||
if (!Mpd->Connect())
|
if (argc > 1)
|
||||||
{
|
{
|
||||||
printf("Cannot connect to mpd: %s\n", Mpd->GetLastErrorMessage().c_str());
|
vector<string> args;
|
||||||
return -1;
|
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,"");
|
setlocale(LC_ALL,"");
|
||||||
initscr();
|
initscr();
|
||||||
noecho();
|
noecho();
|
||||||
|
|||||||
Reference in New Issue
Block a user