diff --git a/ChangeLog b/ChangeLog index dd8d4eda..fa534803 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ # $Id$ +2006-12-23 + * Updated to libmpdclient 0.12.0 + 2006-12-22 * client/server infrastructure and prototype for linux only (so far): - requires libdexter (http://sourceforge.net/projects/libdexter) diff --git a/src/libmpdclient.c b/src/libmpdclient.c index cc8868d3..27bd9ad5 100644 --- a/src/libmpdclient.c +++ b/src/libmpdclient.c @@ -1,22 +1,22 @@ /* libmpdclient - (c)2003-2004 by Warren Dukes (shank@mercury.chem.pitt.edu) + (c)2003-2006 by Warren Dukes (warren.dukes@gmail.com) This project's homepage is: http://www.musicpd.org - + Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - + - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - + - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - + - Neither the name of the Music Player Daemon nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR @@ -28,79 +28,252 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ #include "libmpdclient.h" #include #include -#include -#include #include #include #include -#include -#include #include #include #include -#include -#include -#ifndef MPD_NO_IPV6 -#ifdef AF_INET6 -#define MPD_HAVE_IPV6 -#endif +#ifdef WIN32 +# include +# include +#else +# include +# include +# include +# include #endif -static char sigpipe = 0; +#ifndef MSG_DONTWAIT +# define MSG_DONTWAIT 0 +#endif -#define COMMAND_LIST 1 -#define COMMAND_LIST_OK 2 +#ifndef MPD_NO_GAI +# ifdef AI_ADDRCONFIG +# define MPD_HAVE_GAI +# endif +#endif -#ifdef MPD_HAVE_IPV6 -int mpd_ipv6Supported() +#define COMMAND_LIST 1 +#define COMMAND_LIST_OK 2 + +#ifdef WIN32 +# define SELECT_ERRNO_IGNORE (errno == WSAEINTR || errno == WSAEINPROGRESS) +# define SENDRECV_ERRNO_IGNORE SELECT_ERRNO_IGNORE +#else +# define SELECT_ERRNO_IGNORE (errno == EINTR) +# define SENDRECV_ERRNO_IGNORE (errno == EINTR || errno == EAGAIN) +# define winsock_dll_error(c) 0 +# define closesocket(s) close(s) +# define WSACleanup() do { /* nothing */ } while (0) +#endif + +#ifdef WIN32 +static int winsock_dll_error(mpd_Connection *connection) { - int s; - s = socket(AF_INET6, SOCK_STREAM, 0); - if (s == -1) - return 0; - close(s); - return 1; + WSADATA wsaData; + if ((WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0 || + LOBYTE(wsaData.wVersion) != 2 || + HIBYTE(wsaData.wVersion) != 2 ) { + snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, + "Could not find usable WinSock DLL."); + connection->error = MPD_ERROR_SYSTEM; + return 1; + } + return 0; } -#endif - -char *mpd_sanitizeArg(const char *arg) +static int do_connect_fail(mpd_Connection *connection, + const struct sockaddr *serv_addr, int addrlen) { - size_t i; - int count = 0; - char *ret; + int iMode = 1; /* 0 = blocking, else non-blocking */ + ioctlsocket(connection->sock, FIONBIO, (u_long FAR*) &iMode); + return (connect(connection->sock,serv_addr,addrlen) == SOCKET_ERROR + && WSAGetLastError() != WSAEWOULDBLOCK); +} +#else /* !WIN32 (sane operating systems) */ +static int do_connect_fail(mpd_Connection *connection, + const struct sockaddr *serv_addr, int addrlen) +{ + int flags = fcntl(connection->sock, F_GETFL, 0); + fcntl(connection->sock, F_SETFL, flags | O_NONBLOCK); + return (connect(connection->sock,serv_addr,addrlen)<0 && + errno!=EINPROGRESS); +} +#endif /* !WIN32 */ - for (i = 0; i < strlen(arg); i++) { - if (arg[i] == '"' || arg[i] == '\\') - count++; +#ifdef MPD_HAVE_GAI +static int mpd_connect(mpd_Connection * connection, const char * host, int port, + float timeout) +{ + int error; + char service[20]; + struct addrinfo hints; + struct addrinfo *res = NULL; + struct addrinfo *addrinfo = NULL; + + /** + * Setup hints + */ + hints.ai_flags = AI_ADDRCONFIG; + hints.ai_family = PF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + hints.ai_addrlen = 0; + hints.ai_addr = NULL; + hints.ai_canonname = NULL; + hints.ai_next = NULL; + + snprintf(service, sizeof(service), "%d", port); + + error = getaddrinfo(host, service, &hints, &addrinfo); + + if (error) { + snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, + "host \"%s\" not found: %s",host, gai_strerror(error)); + connection->error = MPD_ERROR_UNKHOST; + return -1; } - ret = malloc(strlen(arg) + count + 1); - - count = 0; - for (i = 0; i < strlen(arg) + 1; i++) { - if (arg[i] == '"' || arg[i] == '\\') { - ret[i + count] = '\\'; - count++; + for (res = addrinfo; res; res = res->ai_next) { + /* create socket */ + connection->sock = socket(res->ai_family, SOCK_STREAM, res->ai_protocol); + if (connection->sock < 0) { + snprintf(connection->errorStr, MPD_BUFFER_MAX_LENGTH, + "problems creating socket: %s", + strerror(errno)); + connection->error = MPD_ERROR_SYSTEM; + freeaddrinfo(addrinfo); + return -1; } - ret[i + count] = arg[i]; + + mpd_setConnectionTimeout(connection,timeout); + + /* connect stuff */ + if (do_connect_fail(connection, res->ai_addr, res->ai_addrlen)) { + /* try the next address family */ + closesocket(connection->sock); + connection->sock = -1; + continue; + } + } + freeaddrinfo(addrinfo); + + if (connection->sock < 0) { + snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, + "problems connecting to \"%s\" on port" + " %i: %s",host,port, strerror(errno)); + connection->error = MPD_ERROR_CONNPORT; + + return -1; + } + + return 0; +} +#else /* !MPD_HAVE_GAI */ +static int mpd_connect(mpd_Connection * connection, const char * host, int port, + float timeout) +{ + struct hostent * he; + struct sockaddr * dest; + int destlen; + struct sockaddr_in sin; + + if(!(he=gethostbyname(host))) { + snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, + "host \"%s\" not found",host); + connection->error = MPD_ERROR_UNKHOST; + return -1; + } + + memset(&sin,0,sizeof(struct sockaddr_in)); + /*dest.sin_family = he->h_addrtype;*/ + sin.sin_family = AF_INET; + sin.sin_port = htons(port); + + switch(he->h_addrtype) { + case AF_INET: + memcpy((char *)&sin.sin_addr.s_addr,(char *)he->h_addr, + he->h_length); + dest = (struct sockaddr *)&sin; + destlen = sizeof(struct sockaddr_in); + break; + default: + strcpy(connection->errorStr,"address type is not IPv4\n"); + connection->error = MPD_ERROR_SYSTEM; + return -1; + break; + } + + if((connection->sock = socket(dest->sa_family,SOCK_STREAM,0))<0) { + strcpy(connection->errorStr,"problems creating socket"); + connection->error = MPD_ERROR_SYSTEM; + return -1; + } + + mpd_setConnectionTimeout(connection,timeout); + + /* connect stuff */ + if (do_connect_fail(connection, dest, destlen)) { + snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, + "problems connecting to \"%s\" on port" + " %i",host,port); + connection->error = MPD_ERROR_CONNPORT; + return -1; + } + + return 0; +} +#endif /* !MPD_HAVE_GAI */ + +char * mpdTagItemKeys[MPD_TAG_NUM_OF_ITEM_TYPES] = +{ + "Artist", + "Album", + "Title", + "Track", + "Name", + "Genre", + "Date", + "Composer", + "Performer", + "Comment", + "Disc", + "filename" +}; + +static char * mpd_sanitizeArg(const char * arg) { + size_t i; + char * ret; + register const char *c; + register char *rc; + + /* instead of counting in that loop above, just + * use a bit more memory and half running time + */ + ret = malloc(strlen(arg) * 2 + 1); + + c = arg; + rc = ret; + for(i = strlen(arg)+1; i != 0; --i) { + if(*c=='"' || *c=='\\') + *rc++ = '\\'; + *(rc++) = *(c++); } return ret; } -mpd_ReturnElement *mpd_newReturnElement(const char *name, - const char *value) +static mpd_ReturnElement * mpd_newReturnElement(const char * name, const char * value) { - mpd_ReturnElement *ret = malloc(sizeof(mpd_ReturnElement)); + mpd_ReturnElement * ret = malloc(sizeof(mpd_ReturnElement)); ret->name = strdup(name); ret->value = strdup(value); @@ -108,175 +281,113 @@ mpd_ReturnElement *mpd_newReturnElement(const char *name, return ret; } -void mpd_freeReturnElement(mpd_ReturnElement * re) -{ +static void mpd_freeReturnElement(mpd_ReturnElement * re) { free(re->name); free(re->value); free(re); } -void mpd_setConnectionTimeout(mpd_Connection * connection, float timeout) -{ - connection->timeout.tv_sec = (int) timeout; - connection->timeout.tv_usec = (int) (timeout * 1e6 - - connection->timeout.tv_sec * - 1000000 + 0.5); +void mpd_setConnectionTimeout(mpd_Connection * connection, float timeout) { + connection->timeout.tv_sec = (int)timeout; + connection->timeout.tv_usec = (int)(timeout*1e6 - + connection->timeout.tv_sec*1000000 + + 0.5); } -mpd_Connection *mpd_newConnection(const char *host, int port, - float timeout) -{ +static int mpd_parseWelcome(mpd_Connection * connection, const char * host, int port, + char * rt, char * output) { + char * tmp; + char * test; + int i; + + if(strncmp(output,MPD_WELCOME_MESSAGE,strlen(MPD_WELCOME_MESSAGE))) { + snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, + "mpd not running on port %i on host \"%s\"", + port,host); + connection->error = MPD_ERROR_NOTMPD; + return 1; + } + + tmp = &output[strlen(MPD_WELCOME_MESSAGE)]; + + for(i=0;i<3;i++) { + if(tmp) connection->version[i] = strtol(tmp,&test,10); + + if (!tmp || (test[0] != '.' && test[0] != '\0')) { + snprintf(connection->errorStr, + MPD_BUFFER_MAX_LENGTH, + "error parsing version number at " + "\"%s\"", + &output[strlen(MPD_WELCOME_MESSAGE)]); + connection->error = MPD_ERROR_NOTMPD; + return 1; + } + tmp = ++test; + } + + return 0; +} + +mpd_Connection * mpd_newConnection(const char * host, int port, float timeout) { int err; - struct hostent *he; - struct sockaddr *dest; -#ifdef HAVE_SOCKLEN_T - socklen_t destlen; -#else - int destlen; -#endif - struct sockaddr_in sin; - char *rt; - char *output; - mpd_Connection *connection = malloc(sizeof(mpd_Connection)); + char * rt; + char * output = NULL; + mpd_Connection * connection = malloc(sizeof(mpd_Connection)); struct timeval tv; fd_set fds; - struct sigaction sapipe; /* definition of signal action */ - -#ifdef MPD_HAVE_IPV6 - struct sockaddr_in6 sin6; -#endif - strcpy(connection->buffer, ""); + strcpy(connection->buffer,""); connection->buflen = 0; connection->bufstart = 0; - strcpy(connection->errorStr, ""); + strcpy(connection->errorStr,""); connection->error = 0; connection->doneProcessing = 0; connection->commandList = 0; connection->listOks = 0; connection->doneListOk = 0; connection->returnElement = NULL; - sigpipe = 0; + connection->request = NULL; - if (!(he = gethostbyname(host))) { - snprintf(connection->errorStr, MPD_BUFFER_MAX_LENGTH, - "host \"%s\" not found", host); - connection->error = MPD_ERROR_UNKHOST; + if (winsock_dll_error(connection)) return connection; - } - memset(&sin, 0, sizeof(struct sockaddr_in)); - /*dest.sin_family = he->h_addrtype; */ - sin.sin_family = AF_INET; - sin.sin_port = htons(port); -#ifdef MPD_HAVE_IPV6 - memset(&sin6, 0, sizeof(struct sockaddr_in6)); - sin6.sin6_family = AF_INET6; - sin6.sin6_port = htons(port); -#endif - switch (he->h_addrtype) { - case AF_INET: - memcpy((char *) &sin.sin_addr.s_addr, (char *) he->h_addr, - he->h_length); - dest = (struct sockaddr *) &sin; - destlen = sizeof(struct sockaddr_in); - break; -#ifdef MPD_HAVE_IPV6 - case AF_INET6: - if (!mpd_ipv6Supported()) { - strcpy(connection->errorStr, - "no IPv6 suuport but a " - "IPv6 address found\n"); - connection->error = MPD_ERROR_SYSTEM; - return connection; - } - memcpy((char *) &sin6.sin6_addr.s6_addr, - (char *) he->h_addr, he->h_length); - dest = (struct sockaddr *) &sin6; - destlen = sizeof(struct sockaddr_in6); - break; -#endif - default: - strcpy(connection->errorStr, - "address type is not IPv4 or " "IPv6\n"); - connection->error = MPD_ERROR_SYSTEM; + if (mpd_connect(connection, host, port, timeout) < 0) return connection; - break; - } - if ((connection->sock = - socket(dest->sa_family, SOCK_STREAM, 0)) < 0) { - strcpy(connection->errorStr, "problems creating socket"); - connection->error = MPD_ERROR_SYSTEM; - return connection; - } - - mpd_setConnectionTimeout(connection, timeout); - - /* install the signal handler */ - sapipe.sa_handler = mpd_signalHandler; -// sapipe.sa_mask = 0; - sapipe.sa_flags = 0; - sigaction(SIGPIPE,&sapipe,NULL); - - /* connect stuff */ - { - int flags = fcntl(connection->sock, F_GETFL, 0); - fcntl(connection->sock, F_SETFL, flags | O_NONBLOCK); - - if (connect(connection->sock, dest, destlen) < 0 - && errno != EINPROGRESS) { - snprintf(connection->errorStr, - MPD_BUFFER_MAX_LENGTH, - "problems connecting to \"%s\" on port" - " %i", host, port); - connection->error = MPD_ERROR_CONNPORT; - return connection; - } - } - - while (!(rt = strstr(connection->buffer, "\n"))) { + while(!(rt = strstr(connection->buffer,"\n"))) { tv.tv_sec = connection->timeout.tv_sec; tv.tv_usec = connection->timeout.tv_usec; FD_ZERO(&fds); - FD_SET(connection->sock, &fds); - if ((err = - select(connection->sock + 1, &fds, NULL, NULL, - &tv)) == 1) { + FD_SET(connection->sock,&fds); + if((err = select(connection->sock+1,&fds,NULL,NULL,&tv)) == 1) { int readed; readed = recv(connection->sock, - &(connection-> - buffer[connection->buflen]), - MPD_BUFFER_MAX_LENGTH - - connection->buflen, 0); - if (readed <= 0) { - snprintf(connection->errorStr, - MPD_BUFFER_MAX_LENGTH, - "problems getting a response from" - " \"%s\" on port %i", host, port); + &(connection->buffer[connection->buflen]), + MPD_BUFFER_MAX_LENGTH-connection->buflen,0); + if(readed<=0) { + snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, + "problems getting a response from" + " \"%s\" on port %i : %s",host, + port, strerror(errno)); connection->error = MPD_ERROR_NORESPONSE; return connection; } - connection->buflen += readed; + connection->buflen+=readed; connection->buffer[connection->buflen] = '\0'; - tv.tv_sec = connection->timeout.tv_sec; - tv.tv_usec = connection->timeout.tv_usec; - } else if (err < 0) { - switch (errno) { - case EINTR: + } + else if(err<0) { + if (SELECT_ERRNO_IGNORE) continue; - default: - snprintf(connection->errorStr, - MPD_BUFFER_MAX_LENGTH, - "problems connecting to \"%s\" on port" - " %i", host, port); - connection->error = MPD_ERROR_CONNPORT; - return connection; - } - } else { snprintf(connection->errorStr, - MPD_BUFFER_MAX_LENGTH, - "timeout in attempting to get a response from" - " \"%s\" on port %i", host, port); + MPD_BUFFER_MAX_LENGTH, + "problems connecting to \"%s\" on port" + " %i",host,port); + connection->error = MPD_ERROR_CONNPORT; + return connection; + } + else { + snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, + "timeout in attempting to get a response from" + " \"%s\" on port %i",host,port); connection->error = MPD_ERROR_NORESPONSE; return connection; } @@ -284,91 +395,38 @@ mpd_Connection *mpd_newConnection(const char *host, int port, *rt = '\0'; output = strdup(connection->buffer); - strcpy(connection->buffer, rt + 1); + strcpy(connection->buffer,rt+1); connection->buflen = strlen(connection->buffer); - if (strncmp - (output, MPD_WELCOME_MESSAGE, strlen(MPD_WELCOME_MESSAGE))) { - free(output); - snprintf(connection->errorStr, MPD_BUFFER_MAX_LENGTH, - "mpd not running on port %i on host \"%s\"", port, - host); - connection->error = MPD_ERROR_NOTMPD; - return connection; - } - - { - char *test; - char *version[3]; - char *tmp = &output[strlen(MPD_WELCOME_MESSAGE)]; - char *search = "."; - int i; - - for (i = 0; i < 3; i++) { - char *tok; - if (i == 3) - search = " "; - version[i] = strtok_r(tmp, search, &tok); - if (!version[i]) { - free(output); - snprintf(connection->errorStr, - MPD_BUFFER_MAX_LENGTH, - "error parsing version number at " - "\"%s\"", - &output[strlen - (MPD_WELCOME_MESSAGE)]); - connection->error = MPD_ERROR_NOTMPD; - return connection; - } - connection->version[i] = - strtol(version[i], &test, 10); - if (version[i] == test || *test != '\0') { - free(output); - snprintf(connection->errorStr, - MPD_BUFFER_MAX_LENGTH, - "error parsing version number at " - "\"%s\"", - &output[strlen - (MPD_WELCOME_MESSAGE)]); - connection->error = MPD_ERROR_NOTMPD; - return connection; - } - tmp = NULL; - } - } + if(mpd_parseWelcome(connection,host,port,rt,output) == 0) connection->doneProcessing = 1; free(output); - connection->doneProcessing = 1; - return connection; } -void mpd_clearError(mpd_Connection * connection) -{ +void mpd_clearError(mpd_Connection * connection) { connection->error = 0; connection->errorStr[0] = '\0'; } -void mpd_closeConnection(mpd_Connection * connection) -{ - close(connection->sock); - if (connection->returnElement) - free(connection->returnElement); +void mpd_closeConnection(mpd_Connection * connection) { + closesocket(connection->sock); + if(connection->returnElement) free(connection->returnElement); + if(connection->request) free(connection->request); free(connection); + WSACleanup(); } -void mpd_executeCommand(mpd_Connection * connection, char *command) -{ +static void mpd_executeCommand(mpd_Connection * connection, char * command) { int ret; struct timeval tv; fd_set fds; - char *commandPtr = command; + char * commandPtr = command; int commandLen = strlen(command); - if (!connection->doneProcessing && !connection->commandList) { - strcpy(connection->errorStr, - "not done processing current command"); + if(!connection->doneProcessing && !connection->commandList) { + strcpy(connection->errorStr,"not done processing current command"); connection->error = 1; return; } @@ -376,130 +434,113 @@ void mpd_executeCommand(mpd_Connection * connection, char *command) mpd_clearError(connection); FD_ZERO(&fds); - FD_SET(connection->sock, &fds); + FD_SET(connection->sock,&fds); tv.tv_sec = connection->timeout.tv_sec; tv.tv_usec = connection->timeout.tv_usec; - if (sigpipe) { - perror(""); - snprintf(connection->errorStr, MPD_BUFFER_MAX_LENGTH, "got SIGINT"); - connection->error = MPD_ERROR_SENDING; - return; - } - while ((ret = select(connection->sock + 1, NULL, &fds, NULL, &tv) == 1) || (ret == -1 && errno == EINTR)) { - if (sigpipe) { - perror(""); - snprintf(connection->errorStr, MPD_BUFFER_MAX_LENGTH, "got SIGINT"); + while((ret = select(connection->sock+1,NULL,&fds,NULL,&tv)==1) || + (ret==-1 && SELECT_ERRNO_IGNORE)) { + ret = send(connection->sock,commandPtr,commandLen,MSG_DONTWAIT); + if(ret<=0) + { + if (SENDRECV_ERRNO_IGNORE) continue; + snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, + "problems giving command \"%s\"",command); connection->error = MPD_ERROR_SENDING; return; - } else { - ret = send(connection->sock, commandPtr, commandLen, MSG_DONTWAIT); - if (ret <= 0) { - if (ret == EAGAIN || ret == EINTR) - continue; - snprintf(connection->errorStr, MPD_BUFFER_MAX_LENGTH, "problems giving command \"%s\"", command); - connection->error = MPD_ERROR_SENDING; - return; - } else { - commandPtr += ret; - commandLen -= ret; - } - if (commandLen <= 0) - break; } + else { + commandPtr+=ret; + commandLen-=ret; + } + + if(commandLen<=0) break; } - if (commandLen > 0) { + if(commandLen>0) { perror(""); - snprintf(connection->errorStr, MPD_BUFFER_MAX_LENGTH, - "timeout sending command \"%s\"", command); + snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, + "timeout sending command \"%s\"",command); connection->error = MPD_ERROR_TIMEOUT; return; } - if (!connection->commandList) - connection->doneProcessing = 0; - else if (connection->commandList == COMMAND_LIST_OK) { + if(!connection->commandList) connection->doneProcessing = 0; + else if(connection->commandList == COMMAND_LIST_OK) { connection->listOks++; } } -void mpd_getNextReturnElement(mpd_Connection * connection) -{ - char *output = NULL; - char *rt = NULL; - char *name = NULL; - char *value = NULL; +static void mpd_getNextReturnElement(mpd_Connection * connection) { + char * output = NULL; + char * rt = NULL; + char * name = NULL; + char * value = NULL; fd_set fds; struct timeval tv; - char *tok = NULL; + char * tok = NULL; int readed; - char *bufferCheck = NULL; + char * bufferCheck = NULL; int err; + int pos; - if (connection->returnElement) - mpd_freeReturnElement(connection->returnElement); + if(connection->returnElement) mpd_freeReturnElement(connection->returnElement); connection->returnElement = NULL; - if (connection->doneProcessing || (connection->listOks && - connection->doneListOk)) { - strcpy(connection->errorStr, - "already done processing current command"); + if(connection->doneProcessing || (connection->listOks && + connection->doneListOk)) + { + strcpy(connection->errorStr,"already done processing current command"); connection->error = 1; return; } - bufferCheck = connection->buffer + connection->bufstart; - while (connection->bufstart >= connection->buflen || - !(rt = strstr(bufferCheck, "\n"))) { - if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) { + bufferCheck = connection->buffer+connection->bufstart; + while(connection->bufstart>=connection->buflen || + !(rt = strchr(bufferCheck,'\n'))) { + if(connection->buflen>=MPD_BUFFER_MAX_LENGTH) { memmove(connection->buffer, - connection->buffer + - connection->bufstart, - connection->buflen - connection->bufstart + - 1); - bufferCheck -= connection->bufstart; - connection->buflen -= connection->bufstart; + connection->buffer+ + connection->bufstart, + connection->buflen- + connection->bufstart+1); + connection->buflen-=connection->bufstart; connection->bufstart = 0; } - if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) { - strcpy(connection->errorStr, "buffer overrun"); + if(connection->buflen>=MPD_BUFFER_MAX_LENGTH) { + strcpy(connection->errorStr,"buffer overrun"); connection->error = MPD_ERROR_BUFFEROVERRUN; connection->doneProcessing = 1; connection->doneListOk = 0; return; } - bufferCheck += connection->buflen - connection->bufstart; + bufferCheck = connection->buffer+connection->buflen; tv.tv_sec = connection->timeout.tv_sec; tv.tv_usec = connection->timeout.tv_usec; FD_ZERO(&fds); - FD_SET(connection->sock, &fds); - if ((err = - select(connection->sock + 1, &fds, NULL, NULL, - &tv) == 1)) { - readed = - recv(connection->sock, - connection->buffer + connection->buflen, - MPD_BUFFER_MAX_LENGTH - - connection->buflen, MSG_DONTWAIT); - if (readed < 0 - && (errno == EAGAIN || errno == EINTR)) { + FD_SET(connection->sock,&fds); + if((err = select(connection->sock+1,&fds,NULL,NULL,&tv) == 1)) { + readed = recv(connection->sock, + connection->buffer+connection->buflen, + MPD_BUFFER_MAX_LENGTH-connection->buflen, + MSG_DONTWAIT); + if(readed<0 && SENDRECV_ERRNO_IGNORE) { continue; } - if (readed <= 0) { - strcpy(connection->errorStr, - "connection" " closed"); + if(readed<=0) { + strcpy(connection->errorStr,"connection" + " closed"); connection->error = MPD_ERROR_CONNCLOSED; connection->doneProcessing = 1; connection->doneListOk = 0; return; } - connection->buflen += readed; + connection->buflen+=readed; connection->buffer[connection->buflen] = '\0'; - } else if (err < 0 && errno == EINTR) - continue; + } + else if(err<0 && SELECT_ERRNO_IGNORE) continue; else { - strcpy(connection->errorStr, "connection timeout"); + strcpy(connection->errorStr,"connection timeout"); connection->error = MPD_ERROR_TIMEOUT; connection->doneProcessing = 1; connection->doneListOk = 0; @@ -508,13 +549,12 @@ void mpd_getNextReturnElement(mpd_Connection * connection) } *rt = '\0'; - output = connection->buffer + connection->bufstart; + output = connection->buffer+connection->bufstart; connection->bufstart = rt - connection->buffer + 1; - if (strcmp(output, "OK") == 0) { - if (connection->listOks > 0) { - strcpy(connection->errorStr, - "expected more list_OK's"); + if(strcmp(output,"OK")==0) { + if(connection->listOks > 0) { + strcpy(connection->errorStr, "expected more list_OK's"); connection->error = 1; } connection->listOks = 0; @@ -523,21 +563,22 @@ void mpd_getNextReturnElement(mpd_Connection * connection) return; } - if (strcmp(output, "list_OK") == 0) { - if (!connection->listOks) { + if(strcmp(output, "list_OK") == 0) { + if(!connection->listOks) { strcpy(connection->errorStr, - "got an unexpected list_OK"); + "got an unexpected list_OK"); connection->error = 1; - } else { + } + else { connection->doneListOk = 1; connection->listOks--; } return; } - if (strncmp(output, "ACK", strlen("ACK")) == 0) { - char *test; - char *needle; + if(strncmp(output,"ACK",strlen("ACK"))==0) { + char * test; + char * needle; int val; strcpy(connection->errorStr, output); @@ -548,85 +589,74 @@ void mpd_getNextReturnElement(mpd_Connection * connection) connection->doneListOk = 0; needle = strchr(output, '['); - if (!needle) - return; - val = strtol(needle + 1, &test, 10); - if (*test != '@') - return; + if(!needle) return; + val = strtol(needle+1, &test, 10); + if(*test != '@') return; connection->errorCode = val; - val = strtol(test + 1, &test, 10); - if (*test != ']') - return; + val = strtol(test+1, &test, 10); + if(*test != ']') return; connection->errorAt = val; return; } - name = strtok_r(output, ":", &tok); - if (name && (value = strtok_r(NULL, "", &tok)) && value[0] == ' ') { - connection->returnElement = - mpd_newReturnElement(name, &(value[1])); - } else { - if (!name || !value) { - snprintf(connection->errorStr, - MPD_BUFFER_MAX_LENGTH, - "error parsing: %s", output); - } else { - snprintf(connection->errorStr, - MPD_BUFFER_MAX_LENGTH, - "error parsing: %s:%s", name, value); - } + tok = strchr(output, ':'); + if (!tok) return; + pos = tok - output; + value = ++tok; + name = output; + name[pos] = '\0'; + + if(value[0]==' ') { + connection->returnElement = mpd_newReturnElement(name,&(value[1])); + } + else { + snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, + "error parsing: %s:%s",name,value); connection->errorStr[MPD_BUFFER_MAX_LENGTH] = '\0'; connection->error = 1; } } -void mpd_finishCommand(mpd_Connection * connection) -{ - while (!connection->doneProcessing) { - if (connection->doneListOk) - connection->doneListOk = 0; +void mpd_finishCommand(mpd_Connection * connection) { + while(!connection->doneProcessing) { + if(connection->doneListOk) connection->doneListOk = 0; mpd_getNextReturnElement(connection); } } -void mpd_finishListOkCommand(mpd_Connection * connection) -{ - while (!connection->doneProcessing && connection->listOks && - !connection->doneListOk) { +static void mpd_finishListOkCommand(mpd_Connection * connection) { + while(!connection->doneProcessing && connection->listOks && + !connection->doneListOk) + { mpd_getNextReturnElement(connection); } } -int mpd_nextListOkCommand(mpd_Connection * connection) -{ +int mpd_nextListOkCommand(mpd_Connection * connection) { mpd_finishListOkCommand(connection); - if (!connection->doneProcessing) - connection->doneListOk = 0; - if (connection->listOks == 0 || connection->doneProcessing) - return -1; + if(!connection->doneProcessing) connection->doneListOk = 0; + if(connection->listOks == 0 || connection->doneProcessing) return -1; return 0; } -void mpd_sendStatusCommand(mpd_Connection * connection) -{ - mpd_executeCommand(connection, "status\n"); +void mpd_sendStatusCommand(mpd_Connection * connection) { + mpd_executeCommand(connection,"status\n"); } -mpd_Status *mpd_getStatus(mpd_Connection * connection) -{ - mpd_Status *status; +mpd_Status * mpd_getStatus(mpd_Connection * connection) { + mpd_Status * status; /*mpd_executeCommand(connection,"status\n"); - if(connection->error) return NULL; */ + if(connection->error) return NULL;*/ - if (connection->doneProcessing || (connection->listOks && - connection->doneListOk)) { + if(connection->doneProcessing || (connection->listOks && + connection->doneListOk)) + { return NULL; } - if (!connection->returnElement) - mpd_getNextReturnElement(connection); + if(!connection->returnElement) mpd_getNextReturnElement(connection); status = malloc(sizeof(mpd_Status)); status->volume = -1; @@ -636,6 +666,7 @@ mpd_Status *mpd_getStatus(mpd_Connection * connection) status->playlistLength = -1; status->state = -1; status->song = 0; + status->songid = 0; status->elapsedTime = 0; status->totalTime = 0; status->bitRate = 0; @@ -646,89 +677,92 @@ mpd_Status *mpd_getStatus(mpd_Connection * connection) status->error = NULL; status->updatingDb = 0; - if (connection->error) { + if(connection->error) { free(status); return NULL; } - while (connection->returnElement) { - mpd_ReturnElement *re = connection->returnElement; - if (strcmp(re->name, "volume") == 0) { + while(connection->returnElement) { + mpd_ReturnElement * re = connection->returnElement; + if(strcmp(re->name,"volume")==0) { status->volume = atoi(re->value); - } else if (strcmp(re->name, "repeat") == 0) { + } + else if(strcmp(re->name,"repeat")==0) { status->repeat = atoi(re->value); - } else if (strcmp(re->name, "random") == 0) { + } + else if(strcmp(re->name,"random")==0) { status->random = atoi(re->value); - } else if (strcmp(re->name, "playlist") == 0) { - status->playlist = strtol(re->value, NULL, 10); - } else if (strcmp(re->name, "playlistlength") == 0) { + } + else if(strcmp(re->name,"playlist")==0) { + status->playlist = strtol(re->value,NULL,10); + } + else if(strcmp(re->name,"playlistlength")==0) { status->playlistLength = atoi(re->value); - } else if (strcmp(re->name, "bitrate") == 0) { + } + else if(strcmp(re->name,"bitrate")==0) { status->bitRate = atoi(re->value); - } else if (strcmp(re->name, "state") == 0) { - if (strcmp(re->value, "play") == 0) { + } + else if(strcmp(re->name,"state")==0) { + if(strcmp(re->value,"play")==0) { status->state = MPD_STATUS_STATE_PLAY; - } else if (strcmp(re->value, "stop") == 0) { + } + else if(strcmp(re->value,"stop")==0) { status->state = MPD_STATUS_STATE_STOP; - } else if (strcmp(re->value, "pause") == 0) { + } + else if(strcmp(re->value,"pause")==0) { status->state = MPD_STATUS_STATE_PAUSE; - } else { + } + else { status->state = MPD_STATUS_STATE_UNKNOWN; } - } else if (strcmp(re->name, "song") == 0) { + } + else if(strcmp(re->name,"song")==0) { status->song = atoi(re->value); - } else if (strcmp(re->name, "songid") == 0) { + } + else if(strcmp(re->name,"songid")==0) { status->songid = atoi(re->value); - } else if (strcmp(re->name, "time") == 0) { - char *tok; - char *copy; - char *temp; - copy = strdup(re->value); - temp = strtok_r(copy, ":", &tok); - if (temp) { - status->elapsedTime = atoi(temp); - temp = strtok_r(NULL, "", &tok); - if (temp) - status->totalTime = atoi(temp); + } + else if(strcmp(re->name,"time")==0) { + char * tok = strchr(re->value,':'); + /* the second strchr below is a safety check */ + if (tok && (strchr(tok,0) > (tok+1))) { + /* atoi stops at the first non-[0-9] char: */ + status->elapsedTime = atoi(re->value); + status->totalTime = atoi(tok+1); } - free(copy); - } else if (strcmp(re->name, "error") == 0) { + } + else if(strcmp(re->name,"error")==0) { status->error = strdup(re->value); - } else if (strcmp(re->name, "xfade") == 0) { + } + else if(strcmp(re->name,"xfade")==0) { status->crossfade = atoi(re->value); - } else if (strcmp(re->name, "updating_db") == 0) { + } + else if(strcmp(re->name,"updating_db")==0) { status->updatingDb = atoi(re->value); - } else if (strcmp(re->name, "audio") == 0) { - char *tok; - char *copy; - char *temp; - copy = strdup(re->value); - temp = strtok_r(copy, ":", &tok); - if (temp) { - status->sampleRate = atoi(temp); - temp = strtok_r(NULL, ":", &tok); - if (temp) { - status->bits = atoi(temp); - temp = strtok_r(NULL, "", &tok); - if (temp) - status->channels = - atoi(temp); - } + } + else if(strcmp(re->name,"audio")==0) { + char * tok = strchr(re->value,':'); + if (tok && (strchr(tok,0) > (tok+1))) { + status->sampleRate = atoi(re->value); + status->bits = atoi(++tok); + tok = strchr(tok,':'); + if (tok && (strchr(tok,0) > (tok+1))) + status->channels = atoi(tok+1); } - free(copy); } mpd_getNextReturnElement(connection); - if (connection->error) { + if(connection->error) { free(status); return NULL; } } - if (connection->error) { + if(connection->error) { free(status); return NULL; - } else if (status->state < 0) { - strcpy(connection->errorStr, "state not found"); + } + else if(status->state<0) { + strcpy(connection->errorStr,"state not found"); connection->error = 1; free(status); return NULL; @@ -737,33 +771,29 @@ mpd_Status *mpd_getStatus(mpd_Connection * connection) return status; } -void mpd_freeStatus(mpd_Status * status) -{ - if (status->error) - free(status->error); +void mpd_freeStatus(mpd_Status * status) { + if(status->error) free(status->error); free(status); } -void mpd_sendStatsCommand(mpd_Connection * connection) -{ - mpd_executeCommand(connection, "stats\n"); +void mpd_sendStatsCommand(mpd_Connection * connection) { + mpd_executeCommand(connection,"stats\n"); } -mpd_Stats *mpd_getStats(mpd_Connection * connection) -{ - mpd_Stats *stats; +mpd_Stats * mpd_getStats(mpd_Connection * connection) { + mpd_Stats * stats; /*mpd_executeCommand(connection,"stats\n"); - if(connection->error) return NULL; */ + if(connection->error) return NULL;*/ - if (connection->doneProcessing || (connection->listOks && - connection->doneListOk)) { + if(connection->doneProcessing || (connection->listOks && + connection->doneListOk)) + { return NULL; } - if (!connection->returnElement) - mpd_getNextReturnElement(connection); + if(!connection->returnElement) mpd_getNextReturnElement(connection); stats = malloc(sizeof(mpd_Stats)); stats->numberOfArtists = 0; @@ -774,36 +804,42 @@ mpd_Stats *mpd_getStats(mpd_Connection * connection) stats->playTime = 0; stats->dbPlayTime = 0; - if (connection->error) { + if(connection->error) { free(stats); return NULL; } - while (connection->returnElement) { - mpd_ReturnElement *re = connection->returnElement; - if (strcmp(re->name, "artists") == 0) { + while(connection->returnElement) { + mpd_ReturnElement * re = connection->returnElement; + if(strcmp(re->name,"artists")==0) { stats->numberOfArtists = atoi(re->value); - } else if (strcmp(re->name, "albums") == 0) { + } + else if(strcmp(re->name,"albums")==0) { stats->numberOfAlbums = atoi(re->value); - } else if (strcmp(re->name, "songs") == 0) { + } + else if(strcmp(re->name,"songs")==0) { stats->numberOfSongs = atoi(re->value); - } else if (strcmp(re->name, "uptime") == 0) { - stats->uptime = strtol(re->value, NULL, 10); - } else if (strcmp(re->name, "db_update") == 0) { - stats->dbUpdateTime = strtol(re->value, NULL, 10); - } else if (strcmp(re->name, "playtime") == 0) { - stats->playTime = strtol(re->value, NULL, 10); - } else if (strcmp(re->name, "db_playtime") == 0) { - stats->dbPlayTime = strtol(re->value, NULL, 10); + } + else if(strcmp(re->name,"uptime")==0) { + stats->uptime = strtol(re->value,NULL,10); + } + else if(strcmp(re->name,"db_update")==0) { + stats->dbUpdateTime = strtol(re->value,NULL,10); + } + else if(strcmp(re->name,"playtime")==0) { + stats->playTime = strtol(re->value,NULL,10); + } + else if(strcmp(re->name,"db_playtime")==0) { + stats->dbPlayTime = strtol(re->value,NULL,10); } mpd_getNextReturnElement(connection); - if (connection->error) { + if(connection->error) { free(stats); return NULL; } } - if (connection->error) { + if(connection->error) { free(stats); return NULL; } @@ -811,71 +847,70 @@ mpd_Stats *mpd_getStats(mpd_Connection * connection) return stats; } -void mpd_freeStats(mpd_Stats * stats) -{ +void mpd_freeStats(mpd_Stats * stats) { free(stats); } -void mpd_initSong(mpd_Song * song) -{ +static void mpd_initSong(mpd_Song * song) { song->file = NULL; song->artist = NULL; song->album = NULL; song->track = NULL; song->title = NULL; song->name = NULL; + song->date = NULL; + /* added by Qball */ + song->genre = NULL; + song->composer = NULL; + song->disc = NULL; + song->comment = NULL; + song->time = MPD_SONG_NO_TIME; song->pos = MPD_SONG_NO_NUM; song->id = MPD_SONG_NO_ID; } -void mpd_finishSong(mpd_Song * song) -{ - if (song->file) - free(song->file); - if (song->artist) - free(song->artist); - if (song->album) - free(song->album); - if (song->title) - free(song->title); - if (song->track) - free(song->track); - if (song->name) - free(song->name); +static void mpd_finishSong(mpd_Song * song) { + if(song->file) free(song->file); + if(song->artist) free(song->artist); + if(song->album) free(song->album); + if(song->title) free(song->title); + if(song->track) free(song->track); + if(song->name) free(song->name); + if(song->date) free(song->date); + if(song->genre) free(song->genre); + if(song->composer) free(song->composer); + if(song->disc) free(song->disc); + if(song->comment) free(song->comment); } -mpd_Song *mpd_newSong() -{ - mpd_Song *ret = malloc(sizeof(mpd_Song)); +mpd_Song * mpd_newSong(void) { + mpd_Song * ret = malloc(sizeof(mpd_Song)); mpd_initSong(ret); return ret; } -void mpd_freeSong(mpd_Song * song) -{ +void mpd_freeSong(mpd_Song * song) { mpd_finishSong(song); free(song); } -mpd_Song *mpd_songDup(mpd_Song * song) -{ - mpd_Song *ret = mpd_newSong(); +mpd_Song * mpd_songDup(mpd_Song * song) { + mpd_Song * ret = mpd_newSong(); - if (song->file) - ret->file = strdup(song->file); - if (song->artist) - ret->artist = strdup(song->artist); - if (song->album) - ret->album = strdup(song->album); - if (song->title) - ret->title = strdup(song->title); - if (song->track) - ret->track = strdup(song->track); - if (song->name) - ret->name = strdup(song->name); + if(song->file) ret->file = strdup(song->file); + if(song->artist) ret->artist = strdup(song->artist); + if(song->album) ret->album = strdup(song->album); + if(song->title) ret->title = strdup(song->title); + if(song->track) ret->track = strdup(song->track); + if(song->name) ret->name = strdup(song->name); + if(song->date) ret->date = strdup(song->date); + if(song->genre) ret->genre= strdup(song->genre); + if(song->composer) ret->composer= strdup(song->composer); + if(song->disc) ret->disc = strdup(song->disc); + if(song->comment) ret->comment = strdup(song->comment); ret->time = song->time; ret->pos = song->pos; ret->id = song->id; @@ -883,211 +918,215 @@ mpd_Song *mpd_songDup(mpd_Song * song) return ret; } -void mpd_initDirectory(mpd_Directory * directory) -{ +static void mpd_initDirectory(mpd_Directory * directory) { directory->path = NULL; } -void mpd_finishDirectory(mpd_Directory * directory) -{ - if (directory->path) - free(directory->path); +static void mpd_finishDirectory(mpd_Directory * directory) { + if(directory->path) free(directory->path); } -mpd_Directory *mpd_newDirectory() -{ - mpd_Directory *directory = malloc(sizeof(mpd_Directory));; +mpd_Directory * mpd_newDirectory(void) { + mpd_Directory * directory = malloc(sizeof(mpd_Directory));; mpd_initDirectory(directory); return directory; } -void mpd_freeDirectory(mpd_Directory * directory) -{ +void mpd_freeDirectory(mpd_Directory * directory) { mpd_finishDirectory(directory); free(directory); } -mpd_Directory *mpd_directoryDup(mpd_Directory * directory) -{ - mpd_Directory *ret = mpd_newDirectory(); +mpd_Directory * mpd_directoryDup(mpd_Directory * directory) { + mpd_Directory * ret = mpd_newDirectory(); - if (directory->path) - ret->path = strdup(directory->path); + if(directory->path) ret->path = strdup(directory->path); return ret; } -void mpd_initPlaylistFile(mpd_PlaylistFile * playlist) -{ +static void mpd_initPlaylistFile(mpd_PlaylistFile * playlist) { playlist->path = NULL; } -void mpd_finishPlaylistFile(mpd_PlaylistFile * playlist) -{ - if (playlist->path) - free(playlist->path); +static void mpd_finishPlaylistFile(mpd_PlaylistFile * playlist) { + if(playlist->path) free(playlist->path); } -mpd_PlaylistFile *mpd_newPlaylistFile() -{ - mpd_PlaylistFile *playlist = malloc(sizeof(mpd_PlaylistFile)); +mpd_PlaylistFile * mpd_newPlaylistFile(void) { + mpd_PlaylistFile * playlist = malloc(sizeof(mpd_PlaylistFile)); mpd_initPlaylistFile(playlist); return playlist; } -void mpd_freePlaylistFile(mpd_PlaylistFile * playlist) -{ +void mpd_freePlaylistFile(mpd_PlaylistFile * playlist) { mpd_finishPlaylistFile(playlist); free(playlist); } -mpd_PlaylistFile *mpd_playlistFileDup(mpd_PlaylistFile * playlist) -{ - mpd_PlaylistFile *ret = mpd_newPlaylistFile(); +mpd_PlaylistFile * mpd_playlistFileDup(mpd_PlaylistFile * playlist) { + mpd_PlaylistFile * ret = mpd_newPlaylistFile(); - if (playlist->path) - ret->path = strdup(playlist->path); + if(playlist->path) ret->path = strdup(playlist->path); return ret; } -void mpd_initInfoEntity(mpd_InfoEntity * entity) -{ +static void mpd_initInfoEntity(mpd_InfoEntity * entity) { entity->info.directory = NULL; } -void mpd_finishInfoEntity(mpd_InfoEntity * entity) -{ - if (entity->info.directory) { - if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) { +static void mpd_finishInfoEntity(mpd_InfoEntity * entity) { + if(entity->info.directory) { + if(entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) { mpd_freeDirectory(entity->info.directory); - } else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG) { + } + else if(entity->type == MPD_INFO_ENTITY_TYPE_SONG) { mpd_freeSong(entity->info.song); - } else if (entity->type == - MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) { + } + else if(entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) { mpd_freePlaylistFile(entity->info.playlistFile); } } } -mpd_InfoEntity *mpd_newInfoEntity() -{ - mpd_InfoEntity *entity = malloc(sizeof(mpd_InfoEntity)); +mpd_InfoEntity * mpd_newInfoEntity(void) { + mpd_InfoEntity * entity = malloc(sizeof(mpd_InfoEntity)); mpd_initInfoEntity(entity); return entity; } -void mpd_freeInfoEntity(mpd_InfoEntity * entity) -{ +void mpd_freeInfoEntity(mpd_InfoEntity * entity) { mpd_finishInfoEntity(entity); free(entity); } -void mpd_sendInfoCommand(mpd_Connection * connection, char *command) -{ - mpd_executeCommand(connection, command); +static void mpd_sendInfoCommand(mpd_Connection * connection, char * command) { + mpd_executeCommand(connection,command); } -mpd_InfoEntity *mpd_getNextInfoEntity(mpd_Connection * connection) -{ - mpd_InfoEntity *entity = NULL; +mpd_InfoEntity * mpd_getNextInfoEntity(mpd_Connection * connection) { + mpd_InfoEntity * entity = NULL; - if (connection->doneProcessing || (connection->listOks && - connection->doneListOk)) { + if(connection->doneProcessing || (connection->listOks && + connection->doneListOk)) + { return NULL; } - if (!connection->returnElement) - mpd_getNextReturnElement(connection); + if(!connection->returnElement) mpd_getNextReturnElement(connection); - if (connection->returnElement) { - if (strcmp(connection->returnElement->name, "file") == 0) { + if(connection->returnElement) { + if(strcmp(connection->returnElement->name,"file")==0) { entity = mpd_newInfoEntity(); entity->type = MPD_INFO_ENTITY_TYPE_SONG; entity->info.song = mpd_newSong(); entity->info.song->file = - strdup(connection->returnElement->value); - } else - if (strcmp - (connection->returnElement->name, - "directory") == 0) { + strdup(connection->returnElement->value); + } + else if(strcmp(connection->returnElement->name, + "directory")==0) { entity = mpd_newInfoEntity(); entity->type = MPD_INFO_ENTITY_TYPE_DIRECTORY; entity->info.directory = mpd_newDirectory(); entity->info.directory->path = - strdup(connection->returnElement->value); - } else - if (strcmp(connection->returnElement->name, "playlist") - == 0) { + strdup(connection->returnElement->value); + } + else if(strcmp(connection->returnElement->name,"playlist")==0) { entity = mpd_newInfoEntity(); entity->type = MPD_INFO_ENTITY_TYPE_PLAYLISTFILE; entity->info.playlistFile = mpd_newPlaylistFile(); entity->info.playlistFile->path = - strdup(connection->returnElement->value); - } else { + strdup(connection->returnElement->value); + } + else if(strcmp(connection->returnElement->name, "cpos") == 0){ + entity = mpd_newInfoEntity(); + entity->type = MPD_INFO_ENTITY_TYPE_SONG; + entity->info.song = mpd_newSong(); + entity->info.song->pos = atoi(connection->returnElement->value); + } + else { connection->error = 1; - strcpy(connection->errorStr, - "problem parsing song info"); + strcpy(connection->errorStr,"problem parsing song info"); return NULL; } - } else - return NULL; + } + else return NULL; mpd_getNextReturnElement(connection); - while (connection->returnElement) { - mpd_ReturnElement *re = connection->returnElement; + while(connection->returnElement) { + mpd_ReturnElement * re = connection->returnElement; - if (strcmp(re->name, "file") == 0) - return entity; - else if (strcmp(re->name, "directory") == 0) - return entity; - else if (strcmp(re->name, "playlist") == 0) - return entity; + if(strcmp(re->name,"file")==0) return entity; + else if(strcmp(re->name,"directory")==0) return entity; + else if(strcmp(re->name,"playlist")==0) return entity; + else if(strcmp(re->name,"cpos")==0) return entity; - if (entity->type == MPD_INFO_ENTITY_TYPE_SONG - && strlen(re->value)) { - if (!entity->info.song->artist - && strcmp(re->name, "Artist") == 0) { - entity->info.song->artist = - strdup(re->value); - } else if (!entity->info.song->album - && strcmp(re->name, "Album") == 0) { - entity->info.song->album = - strdup(re->value); - } else if (!entity->info.song->title - && strcmp(re->name, "Title") == 0) { - entity->info.song->title = - strdup(re->value); - } else if (!entity->info.song->track - && strcmp(re->name, "Track") == 0) { - entity->info.song->track = - strdup(re->value); - } else if (!entity->info.song->name - && strcmp(re->name, "Name") == 0) { - entity->info.song->name = - strdup(re->value); - } else if (entity->info.song->time == - MPD_SONG_NO_TIME - && strcmp(re->name, "Time") == 0) { + if(entity->type == MPD_INFO_ENTITY_TYPE_SONG && + strlen(re->value)) { + if(!entity->info.song->artist && + strcmp(re->name,"Artist")==0) { + entity->info.song->artist = strdup(re->value); + } + else if(!entity->info.song->album && + strcmp(re->name,"Album")==0) { + entity->info.song->album = strdup(re->value); + } + else if(!entity->info.song->title && + strcmp(re->name,"Title")==0) { + entity->info.song->title = strdup(re->value); + } + else if(!entity->info.song->track && + strcmp(re->name,"Track")==0) { + entity->info.song->track = strdup(re->value); + } + else if(!entity->info.song->name && + strcmp(re->name,"Name")==0) { + entity->info.song->name = strdup(re->value); + } + else if(entity->info.song->time==MPD_SONG_NO_TIME && + strcmp(re->name,"Time")==0) { entity->info.song->time = atoi(re->value); - } else if (entity->info.song->pos == - MPD_SONG_NO_NUM - && strcmp(re->name, "Pos") == 0) { + } + else if(entity->info.song->pos==MPD_SONG_NO_NUM && + strcmp(re->name,"Pos")==0) { entity->info.song->pos = atoi(re->value); - } else if (entity->info.song->id == MPD_SONG_NO_ID - && strcmp(re->name, "Id") == 0) { + } + else if(entity->info.song->id==MPD_SONG_NO_ID && + strcmp(re->name,"Id")==0) { entity->info.song->id = atoi(re->value); } - } else if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) { - } else if (entity->type == - MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) { + else if(!entity->info.song->date && + strcmp(re->name, "Date") == 0) { + entity->info.song->date = strdup(re->value); + } + else if(!entity->info.song->genre && + strcmp(re->name, "Genre") == 0) { + entity->info.song->genre = strdup(re->value); + } + else if(!entity->info.song->composer && + strcmp(re->name, "Composer") == 0) { + entity->info.song->composer = strdup(re->value); + } + else if(!entity->info.song->disc && + strcmp(re->name, "Disc") == 0) { + entity->info.song->disc = strdup(re->value); + } + else if(!entity->info.song->comment && + strcmp(re->name, "Comment") == 0) { + entity->info.song->comment = strdup(re->value); + } + } + else if(entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) { + } + else if(entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) { } mpd_getNextReturnElement(connection); @@ -1096,346 +1135,314 @@ mpd_InfoEntity *mpd_getNextInfoEntity(mpd_Connection * connection) return entity; } -char *mpd_getNextReturnElementNamed(mpd_Connection * connection, - const char *name) +static char * mpd_getNextReturnElementNamed(mpd_Connection * connection, + const char * name) { - if (connection->doneProcessing || (connection->listOks && - connection->doneListOk)) { + if(connection->doneProcessing || (connection->listOks && + connection->doneListOk)) + { return NULL; } mpd_getNextReturnElement(connection); - while (connection->returnElement) { - mpd_ReturnElement *re = connection->returnElement; + while(connection->returnElement) { + mpd_ReturnElement * re = connection->returnElement; - if (strcmp(re->name, name) == 0) - return strdup(re->value); + if(strcmp(re->name,name)==0) return strdup(re->value); mpd_getNextReturnElement(connection); } return NULL; } -char *mpd_getNextArtist(mpd_Connection * connection) -{ - return mpd_getNextReturnElementNamed(connection, "Artist"); +char * mpd_getNextTag(mpd_Connection * connection,int table) { + if(table >= 0 && table < MPD_TAG_NUM_OF_ITEM_TYPES) + { + return mpd_getNextReturnElementNamed(connection,mpdTagItemKeys[table]); + } + return NULL; } -char *mpd_getNextAlbum(mpd_Connection * connection) -{ - return mpd_getNextReturnElementNamed(connection, "Album"); +char * mpd_getNextArtist(mpd_Connection * connection) { + return mpd_getNextReturnElementNamed(connection,"Artist"); } -void mpd_sendPlaylistInfoCommand(mpd_Connection * connection, int songPos) -{ - char *string = malloc(strlen("playlistinfo") + 25); - sprintf(string, "playlistinfo \"%i\"\n", songPos); - mpd_sendInfoCommand(connection, string); +char * mpd_getNextAlbum(mpd_Connection * connection) { + return mpd_getNextReturnElementNamed(connection,"Album"); +} + +void mpd_sendPlaylistInfoCommand(mpd_Connection * connection, int songPos) { + char * string = malloc(strlen("playlistinfo")+25); + sprintf(string,"playlistinfo \"%i\"\n",songPos); + mpd_sendInfoCommand(connection,string); free(string); } -void mpd_sendPlaylistIdCommand(mpd_Connection * connection, int id) -{ - char *string = malloc(strlen("playlistid") + 25); +void mpd_sendPlaylistIdCommand(mpd_Connection * connection, int id) { + char * string = malloc(strlen("playlistid")+25); sprintf(string, "playlistid \"%i\"\n", id); mpd_sendInfoCommand(connection, string); free(string); } -void -mpd_sendPlChangesCommand(mpd_Connection * connection, long long playlist) -{ - char *string = malloc(strlen("plchanges") + 25); - sprintf(string, "plchanges \"%lld\"\n", playlist); - mpd_sendInfoCommand(connection, string); +void mpd_sendPlChangesCommand(mpd_Connection * connection, long long playlist) { + char * string = malloc(strlen("plchanges")+25); + sprintf(string,"plchanges \"%lld\"\n",playlist); + mpd_sendInfoCommand(connection,string); free(string); } -void mpd_sendListallCommand(mpd_Connection * connection, const char *dir) -{ - char *sDir = mpd_sanitizeArg(dir); - char *string = malloc(strlen("listall") + strlen(sDir) + 5); - sprintf(string, "listall \"%s\"\n", sDir); - mpd_sendInfoCommand(connection, string); +void mpd_sendPlChangesPosIdCommand(mpd_Connection * connection, long long playlist) { + char * string = malloc(strlen("plchangesposid")+25); + sprintf(string,"plchangesposid \"%lld\"\n",playlist); + mpd_sendInfoCommand(connection,string); + free(string); +} + +void mpd_sendListallCommand(mpd_Connection * connection, const char * dir) { + char * sDir = mpd_sanitizeArg(dir); + char * string = malloc(strlen("listall")+strlen(sDir)+5); + sprintf(string,"listall \"%s\"\n",sDir); + mpd_sendInfoCommand(connection,string); free(string); free(sDir); } -void -mpd_sendListallInfoCommand(mpd_Connection * connection, const char *dir) -{ - char *sDir = mpd_sanitizeArg(dir); - char *string = malloc(strlen("listallinfo") + strlen(sDir) + 5); - sprintf(string, "listallinfo \"%s\"\n", sDir); - mpd_sendInfoCommand(connection, string); +void mpd_sendListallInfoCommand(mpd_Connection * connection, const char * dir) { + char * sDir = mpd_sanitizeArg(dir); + char * string = malloc(strlen("listallinfo")+strlen(sDir)+5); + sprintf(string,"listallinfo \"%s\"\n",sDir); + mpd_sendInfoCommand(connection,string); free(string); free(sDir); } -void mpd_sendLsInfoCommand(mpd_Connection * connection, const char *dir) -{ - char *sDir = mpd_sanitizeArg(dir); - char *string = malloc(strlen("lsinfo") + strlen(sDir) + 5); - sprintf(string, "lsinfo \"%s\"\n", sDir); - mpd_sendInfoCommand(connection, string); +void mpd_sendLsInfoCommand(mpd_Connection * connection, const char * dir) { + char * sDir = mpd_sanitizeArg(dir); + char * string = malloc(strlen("lsinfo")+strlen(sDir)+5); + sprintf(string,"lsinfo \"%s\"\n",sDir); + mpd_sendInfoCommand(connection,string); free(string); free(sDir); } -void mpd_sendCurrentSongCommand(mpd_Connection * connection) -{ - mpd_executeCommand(connection, "currentsong\n"); +void mpd_sendCurrentSongCommand(mpd_Connection * connection) { + mpd_executeCommand(connection,"currentsong\n"); } -void -mpd_sendSearchCommand(mpd_Connection * connection, int table, - const char *str) +void mpd_sendSearchCommand(mpd_Connection * connection, int table, + const char * str) { char st[10]; - char *string; - char *sanitStr = mpd_sanitizeArg(str); - if (table == MPD_TABLE_ARTIST) - strcpy(st, "artist"); - else if (table == MPD_TABLE_ALBUM) - strcpy(st, "album"); - else if (table == MPD_TABLE_TITLE) - strcpy(st, "title"); - else if (table == MPD_TABLE_FILENAME) - strcpy(st, "filename"); + char * string; + char * sanitStr = mpd_sanitizeArg(str); + if(table == MPD_TABLE_ARTIST) strcpy(st,"artist"); + else if(table == MPD_TABLE_ALBUM) strcpy(st,"album"); + else if(table == MPD_TABLE_TITLE) strcpy(st,"title"); + else if(table == MPD_TABLE_FILENAME) strcpy(st,"filename"); else { connection->error = 1; - strcpy(connection->errorStr, "unknown table for search"); + strcpy(connection->errorStr,"unknown table for search"); return; } - string = - malloc(strlen("search") + strlen(sanitStr) + strlen(st) + 6); - sprintf(string, "search %s \"%s\"\n", st, sanitStr); - mpd_sendInfoCommand(connection, string); + string = malloc(strlen("search")+strlen(sanitStr)+strlen(st)+6); + sprintf(string,"search %s \"%s\"\n",st,sanitStr); + mpd_sendInfoCommand(connection,string); free(string); free(sanitStr); } -void -mpd_sendFindCommand(mpd_Connection * connection, int table, - const char *str) +void mpd_sendFindCommand(mpd_Connection * connection, int table, + const char * str) { char st[10]; - char *string; - char *sanitStr = mpd_sanitizeArg(str); - if (table == MPD_TABLE_ARTIST) - strcpy(st, "artist"); - else if (table == MPD_TABLE_ALBUM) - strcpy(st, "album"); - else if (table == MPD_TABLE_TITLE) - strcpy(st, "title"); + char * string; + char * sanitStr = mpd_sanitizeArg(str); + if(table == MPD_TABLE_ARTIST) strcpy(st,"artist"); + else if(table == MPD_TABLE_ALBUM) strcpy(st,"album"); + else if(table == MPD_TABLE_TITLE) strcpy(st,"title"); else { connection->error = 1; - strcpy(connection->errorStr, "unknown table for find"); + strcpy(connection->errorStr,"unknown table for find"); return; } - string = - malloc(strlen("find") + strlen(sanitStr) + strlen(st) + 6); - sprintf(string, "find %s \"%s\"\n", st, sanitStr); - mpd_sendInfoCommand(connection, string); + string = malloc(strlen("find")+strlen(sanitStr)+strlen(st)+6); + sprintf(string,"find %s \"%s\"\n",st,sanitStr); + mpd_sendInfoCommand(connection,string); free(string); free(sanitStr); } -void -mpd_sendListCommand(mpd_Connection * connection, int table, - const char *arg1) +void mpd_sendListCommand(mpd_Connection * connection, int table, + const char * arg1) { char st[10]; - char *string; - if (table == MPD_TABLE_ARTIST) - strcpy(st, "artist"); - else if (table == MPD_TABLE_ALBUM) - strcpy(st, "album"); + char * string; + if(table == MPD_TABLE_ARTIST) strcpy(st,"artist"); + else if(table == MPD_TABLE_ALBUM) strcpy(st,"album"); else { connection->error = 1; - strcpy(connection->errorStr, "unknown table for list"); + strcpy(connection->errorStr,"unknown table for list"); return; } - if (arg1) { - char *sanitArg1 = mpd_sanitizeArg(arg1); - string = - malloc(strlen("list") + strlen(sanitArg1) + - strlen(st) + 6); - sprintf(string, "list %s \"%s\"\n", st, sanitArg1); + if(arg1) { + char * sanitArg1 = mpd_sanitizeArg(arg1); + string = malloc(strlen("list")+strlen(sanitArg1)+strlen(st)+6); + sprintf(string,"list %s \"%s\"\n",st,sanitArg1); free(sanitArg1); - } else { - string = malloc(strlen("list") + strlen(st) + 3); - sprintf(string, "list %s\n", st); } - mpd_sendInfoCommand(connection, string); + else { + string = malloc(strlen("list")+strlen(st)+3); + sprintf(string,"list %s\n",st); + } + mpd_sendInfoCommand(connection,string); free(string); } -void mpd_sendAddCommand(mpd_Connection * connection, const char *file) -{ - char *sFile = mpd_sanitizeArg(file); - char *string = malloc(strlen("add") + strlen(sFile) + 5); - sprintf(string, "add \"%s\"\n", sFile); - mpd_executeCommand(connection, string); +void mpd_sendAddCommand(mpd_Connection * connection, const char * file) { + char * sFile = mpd_sanitizeArg(file); + char * string = malloc(strlen("add")+strlen(sFile)+5); + sprintf(string,"add \"%s\"\n",sFile); + mpd_executeCommand(connection,string); free(string); free(sFile); } -void mpd_sendDeleteCommand(mpd_Connection * connection, int songPos) -{ - char *string = malloc(strlen("delete") + 25); - sprintf(string, "delete \"%i\"\n", songPos); - mpd_sendInfoCommand(connection, string); +void mpd_sendDeleteCommand(mpd_Connection * connection, int songPos) { + char * string = malloc(strlen("delete")+25); + sprintf(string,"delete \"%i\"\n",songPos); + mpd_sendInfoCommand(connection,string); free(string); } -void mpd_sendDeleteIdCommand(mpd_Connection * connection, int id) -{ - char *string = malloc(strlen("deleteid") + 25); +void mpd_sendDeleteIdCommand(mpd_Connection * connection, int id) { + char * string = malloc(strlen("deleteid")+25); sprintf(string, "deleteid \"%i\"\n", id); - mpd_sendInfoCommand(connection, string); + mpd_sendInfoCommand(connection,string); free(string); } -void mpd_sendSaveCommand(mpd_Connection * connection, const char *name) -{ - char *sName = mpd_sanitizeArg(name); - char *string = malloc(strlen("save") + strlen(sName) + 5); - sprintf(string, "save \"%s\"\n", sName); - mpd_executeCommand(connection, string); +void mpd_sendSaveCommand(mpd_Connection * connection, const char * name) { + char * sName = mpd_sanitizeArg(name); + char * string = malloc(strlen("save")+strlen(sName)+5); + sprintf(string,"save \"%s\"\n",sName); + mpd_executeCommand(connection,string); free(string); free(sName); } -void mpd_sendLoadCommand(mpd_Connection * connection, const char *name) -{ - char *sName = mpd_sanitizeArg(name); - char *string = malloc(strlen("load") + strlen(sName) + 5); - sprintf(string, "load \"%s\"\n", sName); - mpd_executeCommand(connection, string); +void mpd_sendLoadCommand(mpd_Connection * connection, const char * name) { + char * sName = mpd_sanitizeArg(name); + char * string = malloc(strlen("load")+strlen(sName)+5); + sprintf(string,"load \"%s\"\n",sName); + mpd_executeCommand(connection,string); free(string); free(sName); } -void mpd_sendRmCommand(mpd_Connection * connection, const char *name) -{ - char *sName = mpd_sanitizeArg(name); - char *string = malloc(strlen("rm") + strlen(sName) + 5); - sprintf(string, "rm \"%s\"\n", sName); - mpd_executeCommand(connection, string); +void mpd_sendRmCommand(mpd_Connection * connection, const char * name) { + char * sName = mpd_sanitizeArg(name); + char * string = malloc(strlen("rm")+strlen(sName)+5); + sprintf(string,"rm \"%s\"\n",sName); + mpd_executeCommand(connection,string); free(string); free(sName); } -void mpd_sendShuffleCommand(mpd_Connection * connection) -{ - mpd_executeCommand(connection, "shuffle\n"); +void mpd_sendShuffleCommand(mpd_Connection * connection) { + mpd_executeCommand(connection,"shuffle\n"); } -void mpd_sendClearCommand(mpd_Connection * connection) -{ - mpd_executeCommand(connection, "clear\n"); +void mpd_sendClearCommand(mpd_Connection * connection) { + mpd_executeCommand(connection,"clear\n"); } -void mpd_sendPlayCommand(mpd_Connection * connection, int songPos) -{ - char *string = malloc(strlen("play") + 25); - sprintf(string, "play \"%i\"\n", songPos); - mpd_sendInfoCommand(connection, string); +void mpd_sendPlayCommand(mpd_Connection * connection, int songPos) { + char * string = malloc(strlen("play")+25); + sprintf(string,"play \"%i\"\n",songPos); + mpd_sendInfoCommand(connection,string); free(string); } -void mpd_sendPlayIdCommand(mpd_Connection * connection, int id) -{ - char *string = malloc(strlen("playid") + 25); - sprintf(string, "playid \"%i\"\n", id); - mpd_sendInfoCommand(connection, string); +void mpd_sendPlayIdCommand(mpd_Connection * connection, int id) { + char * string = malloc(strlen("playid")+25); + sprintf(string,"playid \"%i\"\n",id); + mpd_sendInfoCommand(connection,string); free(string); } -void mpd_sendStopCommand(mpd_Connection * connection) -{ - mpd_executeCommand(connection, "stop\n"); +void mpd_sendStopCommand(mpd_Connection * connection) { + mpd_executeCommand(connection,"stop\n"); } -void mpd_sendPauseCommand(mpd_Connection * connection, int pauseMode) -{ - char *string = malloc(strlen("pause") + 25); - sprintf(string, "pause \"%i\"\n", pauseMode); - mpd_executeCommand(connection, string); +void mpd_sendPauseCommand(mpd_Connection * connection, int pauseMode) { + char * string = malloc(strlen("pause")+25); + sprintf(string,"pause \"%i\"\n",pauseMode); + mpd_executeCommand(connection,string); free(string); } -void mpd_sendNextCommand(mpd_Connection * connection) -{ - mpd_executeCommand(connection, "next\n"); +void mpd_sendNextCommand(mpd_Connection * connection) { + mpd_executeCommand(connection,"next\n"); } -void mpd_sendMoveCommand(mpd_Connection * connection, int from, int to) -{ - char *string = malloc(strlen("move") + 25); - sprintf(string, "move \"%i\" \"%i\"\n", from, to); - mpd_sendInfoCommand(connection, string); +void mpd_sendMoveCommand(mpd_Connection * connection, int from, int to) { + char * string = malloc(strlen("move")+25); + sprintf(string,"move \"%i\" \"%i\"\n",from,to); + mpd_sendInfoCommand(connection,string); free(string); } -void mpd_sendMoveIdCommand(mpd_Connection * connection, int id, int to) -{ - char *string = malloc(strlen("moveid") + 25); +void mpd_sendMoveIdCommand(mpd_Connection * connection, int id, int to) { + char * string = malloc(strlen("moveid")+25); sprintf(string, "moveid \"%i\" \"%i\"\n", id, to); - mpd_sendInfoCommand(connection, string); + mpd_sendInfoCommand(connection,string); free(string); } -void mpd_sendSwapCommand(mpd_Connection * connection, int song1, int song2) -{ - char *string = malloc(strlen("swap") + 25); - sprintf(string, "swap \"%i\" \"%i\"\n", song1, song2); - mpd_sendInfoCommand(connection, string); +void mpd_sendSwapCommand(mpd_Connection * connection, int song1, int song2) { + char * string = malloc(strlen("swap")+25); + sprintf(string,"swap \"%i\" \"%i\"\n",song1,song2); + mpd_sendInfoCommand(connection,string); free(string); } -void mpd_sendSwapIdCommand(mpd_Connection * connection, int id1, int id2) -{ - char *string = malloc(strlen("swapid") + 25); +void mpd_sendSwapIdCommand(mpd_Connection * connection, int id1, int id2) { + char * string = malloc(strlen("swapid")+25); sprintf(string, "swapid \"%i\" \"%i\"\n", id1, id2); - mpd_sendInfoCommand(connection, string); + mpd_sendInfoCommand(connection,string); free(string); } -void mpd_sendSeekCommand(mpd_Connection * connection, int song, int time) -{ - char *string = malloc(strlen("seek") + 25); - sprintf(string, "seek \"%i\" \"%i\"\n", song, time); - mpd_sendInfoCommand(connection, string); +void mpd_sendSeekCommand(mpd_Connection * connection, int song, int time) { + char * string = malloc(strlen("seek")+25); + sprintf(string,"seek \"%i\" \"%i\"\n",song,time); + mpd_sendInfoCommand(connection,string); free(string); } -void mpd_sendSeekIdCommand(mpd_Connection * connection, int id, int time) -{ - char *string = malloc(strlen("seekid") + 25); - sprintf(string, "seekid \"%i\" \"%i\"\n", id, time); - mpd_sendInfoCommand(connection, string); +void mpd_sendSeekIdCommand(mpd_Connection * connection, int id, int time) { + char * string = malloc(strlen("seekid")+25); + sprintf(string,"seekid \"%i\" \"%i\"\n",id,time); + mpd_sendInfoCommand(connection,string); free(string); } -void mpd_sendUpdateCommand(mpd_Connection * connection, char *path) -{ - char *sPath = mpd_sanitizeArg(path); - char *string = malloc(strlen("update") + strlen(sPath) + 5); - sprintf(string, "update \"%s\"\n", sPath); - mpd_sendInfoCommand(connection, string); +void mpd_sendUpdateCommand(mpd_Connection * connection, char * path) { + char * sPath = mpd_sanitizeArg(path); + char * string = malloc(strlen("update")+strlen(sPath)+5); + sprintf(string,"update \"%s\"\n",sPath); + mpd_sendInfoCommand(connection,string); free(string); free(sPath); } -int mpd_getUpdateId(mpd_Connection * connection) -{ - char *jobid; +int mpd_getUpdateId(mpd_Connection * connection) { + char * jobid; int ret = 0; - jobid = mpd_getNextReturnElementNamed(connection, "updating_db"); - if (jobid) { + jobid = mpd_getNextReturnElementNamed(connection,"updating_db"); + if(jobid) { ret = atoi(jobid); free(jobid); } @@ -1443,102 +1450,301 @@ int mpd_getUpdateId(mpd_Connection * connection) return ret; } -void mpd_sendPrevCommand(mpd_Connection * connection) -{ - mpd_executeCommand(connection, "previous\n"); +void mpd_sendPrevCommand(mpd_Connection * connection) { + mpd_executeCommand(connection,"previous\n"); } -void mpd_sendRepeatCommand(mpd_Connection * connection, int repeatMode) -{ - char *string = malloc(strlen("repeat") + 25); - sprintf(string, "repeat \"%i\"\n", repeatMode); - mpd_executeCommand(connection, string); +void mpd_sendRepeatCommand(mpd_Connection * connection, int repeatMode) { + char * string = malloc(strlen("repeat")+25); + sprintf(string,"repeat \"%i\"\n",repeatMode); + mpd_executeCommand(connection,string); free(string); } -void mpd_sendRandomCommand(mpd_Connection * connection, int randomMode) -{ - char *string = malloc(strlen("random") + 25); - sprintf(string, "random \"%i\"\n", randomMode); - mpd_executeCommand(connection, string); +void mpd_sendRandomCommand(mpd_Connection * connection, int randomMode) { + char * string = malloc(strlen("random")+25); + sprintf(string,"random \"%i\"\n",randomMode); + mpd_executeCommand(connection,string); free(string); } -void mpd_sendSetvolCommand(mpd_Connection * connection, int volumeChange) -{ - char *string = malloc(strlen("setvol") + 25); - sprintf(string, "setvol \"%i\"\n", volumeChange); - mpd_executeCommand(connection, string); +void mpd_sendSetvolCommand(mpd_Connection * connection, int volumeChange) { + char * string = malloc(strlen("setvol")+25); + sprintf(string,"setvol \"%i\"\n",volumeChange); + mpd_executeCommand(connection,string); free(string); } -void mpd_sendVolumeCommand(mpd_Connection * connection, int volumeChange) -{ - char *string = malloc(strlen("volume") + 25); - sprintf(string, "volume \"%i\"\n", volumeChange); - mpd_executeCommand(connection, string); +void mpd_sendVolumeCommand(mpd_Connection * connection, int volumeChange) { + char * string = malloc(strlen("volume")+25); + sprintf(string,"volume \"%i\"\n",volumeChange); + mpd_executeCommand(connection,string); free(string); } -void mpd_sendCrossfadeCommand(mpd_Connection * connection, int seconds) -{ - char *string = malloc(strlen("crossfade") + 25); - sprintf(string, "crossfade \"%i\"\n", seconds); - mpd_executeCommand(connection, string); +void mpd_sendCrossfadeCommand(mpd_Connection * connection, int seconds) { + char * string = malloc(strlen("crossfade")+25); + sprintf(string,"crossfade \"%i\"\n",seconds); + mpd_executeCommand(connection,string); free(string); } -void mpd_sendPasswordCommand(mpd_Connection * connection, const char *pass) -{ - char *sPass = mpd_sanitizeArg(pass); - char *string = malloc(strlen("password") + strlen(sPass) + 5); - sprintf(string, "password \"%s\"\n", sPass); - mpd_executeCommand(connection, string); +void mpd_sendPasswordCommand(mpd_Connection * connection, const char * pass) { + char * sPass = mpd_sanitizeArg(pass); + char * string = malloc(strlen("password")+strlen(sPass)+5); + sprintf(string,"password \"%s\"\n",sPass); + mpd_executeCommand(connection,string); free(string); free(sPass); } -void mpd_sendCommandListBegin(mpd_Connection * connection) -{ - if (connection->commandList) { - strcpy(connection->errorStr, - "already in command list mode"); +void mpd_sendCommandListBegin(mpd_Connection * connection) { + if(connection->commandList) { + strcpy(connection->errorStr,"already in command list mode"); connection->error = 1; return; } connection->commandList = COMMAND_LIST; - mpd_executeCommand(connection, "command_list_begin\n"); + mpd_executeCommand(connection,"command_list_begin\n"); } -void mpd_sendCommandListOkBegin(mpd_Connection * connection) -{ - if (connection->commandList) { - strcpy(connection->errorStr, - "already in command list mode"); +void mpd_sendCommandListOkBegin(mpd_Connection * connection) { + if(connection->commandList) { + strcpy(connection->errorStr,"already in command list mode"); connection->error = 1; return; } connection->commandList = COMMAND_LIST_OK; - mpd_executeCommand(connection, "command_list_ok_begin\n"); + mpd_executeCommand(connection,"command_list_ok_begin\n"); connection->listOks = 0; } -void mpd_sendCommandListEnd(mpd_Connection * connection) -{ - if (!connection->commandList) { - strcpy(connection->errorStr, "not in command list mode"); +void mpd_sendCommandListEnd(mpd_Connection * connection) { + if(!connection->commandList) { + strcpy(connection->errorStr,"not in command list mode"); connection->error = 1; return; } connection->commandList = 0; - mpd_executeCommand(connection, "command_list_end\n"); + mpd_executeCommand(connection,"command_list_end\n"); } -void mpd_signalHandler(int signal) -{ - if (signal == SIGPIPE) { - fprintf(stderr, "Conky: recieved SIGPIPE from MPD connection\n"); - sigpipe = 1; +void mpd_sendOutputsCommand(mpd_Connection * connection) { + mpd_executeCommand(connection,"outputs\n"); +} + +mpd_OutputEntity * mpd_getNextOutput(mpd_Connection * connection) { + mpd_OutputEntity * output = NULL; + + if(connection->doneProcessing || (connection->listOks && + connection->doneListOk)) + { + return NULL; + } + + if(connection->error) return NULL; + + output = malloc(sizeof(mpd_OutputEntity)); + output->id = -10; + output->name = NULL; + output->enabled = 0; + + if(!connection->returnElement) mpd_getNextReturnElement(connection); + + while(connection->returnElement) { + mpd_ReturnElement * re = connection->returnElement; + if(strcmp(re->name,"outputid")==0) { + if(output!=NULL && output->id>=0) return output; + output->id = atoi(re->value); + } + else if(strcmp(re->name,"outputname")==0) { + output->name = strdup(re->value); + } + else if(strcmp(re->name,"outputenabled")==0) { + output->enabled = atoi(re->value); + } + + mpd_getNextReturnElement(connection); + if(connection->error) { + free(output); + return NULL; + } + + } + + return output; +} + +void mpd_sendEnableOutputCommand(mpd_Connection * connection, int outputId) { + char * string = malloc(strlen("enableoutput")+25); + sprintf(string,"enableoutput \"%i\"\n",outputId); + mpd_executeCommand(connection,string); + free(string); +} + +void mpd_sendDisableOutputCommand(mpd_Connection * connection, int outputId) { + char * string = malloc(strlen("disableoutput")+25); + sprintf(string,"disableoutput \"%i\"\n",outputId); + mpd_executeCommand(connection,string); + free(string); +} + +void mpd_freeOutputElement(mpd_OutputEntity * output) { + free(output->name); + free(output); +} + +/** + * mpd_sendNotCommandsCommand + * odd naming, but it gets the not allowed commands + */ + +void mpd_sendNotCommandsCommand(mpd_Connection * connection) { + mpd_executeCommand(connection,"notcommands\n"); +} + +/** + * mpd_sendCommandsCommand + * odd naming, but it gets the allowed commands + */ + +void mpd_sendCommandsCommand(mpd_Connection * connection) { + mpd_executeCommand(connection,"commands\n"); +} +/** + * Get the next returned command + */ +char * mpd_getNextCommand(mpd_Connection * connection) { + return mpd_getNextReturnElementNamed(connection,"command"); +} + +void mpd_startSearch(mpd_Connection * connection,int exact) { + if(connection->request) { + /* search/find allready in progress */ + /* TODO: set error here? */ + return; + } + if(exact){ + connection->request = strdup("find"); + } + else{ + connection->request = strdup("search"); } } + +void mpd_startFieldSearch(mpd_Connection * connection,int field) { + if(connection->request) { + /* search/find allready in progress */ + /* TODO: set error here? */ + return; + } + if(field < 0 || field >= MPD_TAG_NUM_OF_ITEM_TYPES) { + /* set error here */ + return; + } + + connection->request = malloc(sizeof(char)*( + /* length of the field name */ + strlen(mpdTagItemKeys[field])+ + /* "list"+space+\0 */ + 6 + )); + sprintf(connection->request, "list %s", mpdTagItemKeys[field]); +} + + + +void mpd_addConstraintSearch(mpd_Connection *connection, + int field, + char *name) +{ + char *arg = NULL; + if(!connection->request){ + return; + } + if(name == NULL) { + return; + } + if(field < 0 || field >= MPD_TAG_NUM_OF_ITEM_TYPES) { + return; + } + /* clean up the query */ + arg = mpd_sanitizeArg(name); + /* create space for the query */ + connection->request = realloc(connection->request, ( + /* length of the old string */ + strlen(connection->request)+ + /* space between */ + 1+ + /* length of the field name */ + strlen(mpdTagItemKeys[field])+ + /* space plus starting " */ + 2+ + /* length of search term */ + strlen(arg)+ + /* closign " +\0 that is added sprintf */ + 2 + )*sizeof(char)); + /* and form the query */ + sprintf(connection->request, "%s %s \"%s\"", + connection->request, + mpdTagItemKeys[field], + arg); + free(arg); +} + + +void mpd_commitSearch(mpd_Connection *connection) +{ + if(connection->request) + { + int length = strlen(connection->request); + /* fixing up the string for mpd to like */ + connection->request = realloc(connection->request, + (length+ /* old length */ + 2 /* closing \n and \0 */ + )*sizeof(char)); + connection->request[length] = '\n'; + connection->request[length+1] = '\0'; + /* and off we go */ + mpd_sendInfoCommand(connection, connection->request); + /* clean up a bit */ + free(connection->request); + connection->request = NULL; + } +} + +/** + * @param connection a MpdConnection + * @param path the path to the playlist. + * + * List the content, with full metadata, of a stored playlist. + * + */ +void mpd_sendListPlaylistInfoCommand(mpd_Connection *connection, char *path) +{ + char *arg = mpd_sanitizeArg(path); + char *query = malloc(strlen("listplaylistinfo")+strlen(arg)+5); + sprintf(query, "listplaylistinfo \"%s\"\n",arg); + mpd_sendInfoCommand(connection, query); + free(arg); + free(query); +} + +/** + * @param connection a MpdConnection + * @param path the path to the playlist. + * + * List the content of a stored playlist. + * + */ +void mpd_sendListPlaylistCommand(mpd_Connection *connection, char *path) +{ + char *arg = mpd_sanitizeArg(path); + char *query = malloc(strlen("listplaylist")+strlen(arg)+5); + sprintf(query, "listplaylist \"%s\"\n",arg); + mpd_sendInfoCommand(connection, query); + free(arg); + free(query); +} diff --git a/src/libmpdclient.h b/src/libmpdclient.h index c7132828..437951b3 100644 --- a/src/libmpdclient.h +++ b/src/libmpdclient.h @@ -1,22 +1,22 @@ /* libmpdclient - (c)2003-2004 by Warren Dukes (shank@mercury.chem.pitt.edu) + (c)2003-2006 by Warren Dukes (warren.dukes@gmail.com) This project's homepage is: http://www.musicpd.org - + Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - + - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - + - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - + - Neither the name of the Music Player Daemon nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR @@ -28,29 +28,30 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - $Id$ - */ #ifndef LIBMPDCLIENT_H #define LIBMPDCLIENT_H -#include +#ifdef WIN32 +# define __W32API_USE_DLLIMPORT__ 1 +#endif +#include +#include #define MPD_BUFFER_MAX_LENGTH 50000 #define MPD_WELCOME_MESSAGE "OK MPD " -#define MPD_ERROR_TIMEOUT 10 /* timeout trying to talk to mpd */ -#define MPD_ERROR_SYSTEM 11 /* system error */ -#define MPD_ERROR_UNKHOST 12 /* unknown host */ -#define MPD_ERROR_CONNPORT 13 /* problems connecting to port on host */ -#define MPD_ERROR_NOTMPD 14 /* mpd not running on port at host */ -#define MPD_ERROR_NORESPONSE 15 /* no response on attempting to connect */ -#define MPD_ERROR_SENDING 16 /* error sending command */ -#define MPD_ERROR_CONNCLOSED 17 /* connection closed by mpd */ -#define MPD_ERROR_ACK 18 /* ACK returned! */ -#define MPD_ERROR_BUFFEROVERRUN 19 /* Buffer was overrun! */ +#define MPD_ERROR_TIMEOUT 10 /* timeout trying to talk to mpd */ +#define MPD_ERROR_SYSTEM 11 /* system error */ +#define MPD_ERROR_UNKHOST 12 /* unknown host */ +#define MPD_ERROR_CONNPORT 13 /* problems connecting to port on host */ +#define MPD_ERROR_NOTMPD 14 /* mpd not running on port at host */ +#define MPD_ERROR_NORESPONSE 15 /* no response on attempting to connect */ +#define MPD_ERROR_SENDING 16 /* error sending command */ +#define MPD_ERROR_CONNCLOSED 17 /* connection closed by mpd */ +#define MPD_ERROR_ACK 18 /* ACK returned! */ +#define MPD_ERROR_BUFFEROVERRUN 19 /* Buffer was overrun! */ #define MPD_ACK_ERROR_UNK -1 #define MPD_ERROR_AT_UNK -1 @@ -73,37 +74,57 @@ extern "C" { #endif +typedef enum mpd_TagItems +{ + MPD_TAG_ITEM_ARTIST, + MPD_TAG_ITEM_ALBUM, + MPD_TAG_ITEM_TITLE, + MPD_TAG_ITEM_TRACK, + MPD_TAG_ITEM_NAME, + MPD_TAG_ITEM_GENRE, + MPD_TAG_ITEM_DATE, + MPD_TAG_ITEM_COMPOSER, + MPD_TAG_ITEM_PERFORMER, + MPD_TAG_ITEM_COMMENT, + MPD_TAG_ITEM_DISC, + MPD_TAG_ITEM_FILENAME, + MPD_TAG_NUM_OF_ITEM_TYPES +}mpd_TagItems; + +extern char * mpdTagItemKeys[MPD_TAG_NUM_OF_ITEM_TYPES]; + /* internal stuff don't touch this struct */ - typedef struct _mpd_ReturnElement { - char *name; - char *value; - } mpd_ReturnElement; +typedef struct _mpd_ReturnElement { + char * name; + char * value; +} mpd_ReturnElement; /* mpd_Connection * holds info about connection to mpd * use error, and errorStr to detect errors */ - typedef struct _mpd_Connection { - /* use this to check the version of mpd */ - int version[3]; - /* IMPORTANT, you want to get the error messages from here */ - char errorStr[MPD_BUFFER_MAX_LENGTH + 1]; - int errorCode; - int errorAt; - /* this will be set to MPD_ERROR_* if there is an error, 0 if not */ - int error; - /* DON'T TOUCH any of the rest of this stuff */ - int sock; - char buffer[MPD_BUFFER_MAX_LENGTH + 1]; - int buflen; - int bufstart; - int doneProcessing; - int listOks; - int doneListOk; - int commandList; - mpd_ReturnElement *returnElement; - struct timeval timeout; - } mpd_Connection; +typedef struct _mpd_Connection { + /* use this to check the version of mpd */ + int version[3]; + /* IMPORTANT, you want to get the error messages from here */ + char errorStr[MPD_BUFFER_MAX_LENGTH+1]; + int errorCode; + int errorAt; + /* this will be set to MPD_ERROR_* if there is an error, 0 if not */ + int error; + /* DON'T TOUCH any of the rest of this stuff */ + int sock; + char buffer[MPD_BUFFER_MAX_LENGTH+1]; + int buflen; + int bufstart; + int doneProcessing; + int listOks; + int doneListOk; + int commandList; + mpd_ReturnElement * returnElement; + struct timeval timeout; + char *request; +} mpd_Connection; /* mpd_newConnection * use this to open a new connection @@ -111,21 +132,19 @@ extern "C" { * even if an error has occurred * _timeout_ is the connection timeout period in seconds */ - mpd_Connection *mpd_newConnection(const char *host, int port, - float timeout); +mpd_Connection * mpd_newConnection(const char * host, int port, float timeout); - void mpd_setConnectionTimeout(mpd_Connection * connection, - float timeout); +void mpd_setConnectionTimeout(mpd_Connection * connection, float timeout); /* mpd_closeConnection * use this to close a connection and free'ing subsequent memory */ - void mpd_closeConnection(mpd_Connection * connection); +void mpd_closeConnection(mpd_Connection * connection); /* mpd_clearError * clears error */ - void mpd_clearError(mpd_Connection * connection); +void mpd_clearError(mpd_Connection * connection); /* STATUS STUFF */ @@ -141,76 +160,76 @@ extern "C" { /* mpd_Status * holds info return from status command */ - typedef struct mpd_Status { - /* 0-100, or MPD_STATUS_NO_VOLUME when there is no volume support */ - int volume; - /* 1 if repeat is on, 0 otherwise */ - int repeat; - /* 1 if random is on, 0 otherwise */ - int random; - /* playlist length */ - int playlistLength; - /* playlist, use this to determine when the playlist has changed */ - long long playlist; - /* use with MPD_STATUS_STATE_* to determine state of player */ - int state; - /* crossfade setting in seconds */ - int crossfade; - /* if a song is currently selected (always the case when state is - * PLAY or PAUSE), this is the position of the currently - * playing song in the playlist, beginning with 0 - */ - int song; - /* Song ID of the currently selected song */ - int songid; - /* time in seconds that have elapsed in the currently playing/paused - * song - */ - int elapsedTime; - /* length in seconds of the currently playing/paused song */ - int totalTime; - /* current bit rate in kbs */ - int bitRate; - /* audio sample rate */ - unsigned int sampleRate; - /* audio bits */ - int bits; - /* audio channels */ - int channels; - /* 1 if mpd is updating, 0 otherwise */ - int updatingDb; - /* error */ - char *error; - } mpd_Status; +typedef struct mpd_Status { + /* 0-100, or MPD_STATUS_NO_VOLUME when there is no volume support */ + int volume; + /* 1 if repeat is on, 0 otherwise */ + int repeat; + /* 1 if random is on, 0 otherwise */ + int random; + /* playlist length */ + int playlistLength; + /* playlist, use this to determine when the playlist has changed */ + long long playlist; + /* use with MPD_STATUS_STATE_* to determine state of player */ + int state; + /* crossfade setting in seconds */ + int crossfade; + /* if a song is currently selected (always the case when state is + * PLAY or PAUSE), this is the position of the currently + * playing song in the playlist, beginning with 0 + */ + int song; + /* Song ID of the currently selected song */ + int songid; + /* time in seconds that have elapsed in the currently playing/paused + * song + */ + int elapsedTime; + /* length in seconds of the currently playing/paused song */ + int totalTime; + /* current bit rate in kbs */ + int bitRate; + /* audio sample rate */ + unsigned int sampleRate; + /* audio bits */ + int bits; + /* audio channels */ + int channels; + /* 1 if mpd is updating, 0 otherwise */ + int updatingDb; + /* error */ + char * error; +} mpd_Status; - void mpd_sendStatusCommand(mpd_Connection * connection); +void mpd_sendStatusCommand(mpd_Connection * connection); /* mpd_getStatus * returns status info, be sure to free it with mpd_freeStatus() * call this after mpd_sendStatusCommand() */ - mpd_Status *mpd_getStatus(mpd_Connection * connection); +mpd_Status * mpd_getStatus(mpd_Connection * connection); /* mpd_freeStatus * free's status info malloc'd and returned by mpd_getStatus */ - void mpd_freeStatus(mpd_Status * status); +void mpd_freeStatus(mpd_Status * status); - typedef struct _mpd_Stats { - int numberOfArtists; - int numberOfAlbums; - int numberOfSongs; - unsigned long uptime; - unsigned long dbUpdateTime; - unsigned long playTime; - unsigned long dbPlayTime; - } mpd_Stats; +typedef struct _mpd_Stats { + int numberOfArtists; + int numberOfAlbums; + int numberOfSongs; + unsigned long uptime; + unsigned long dbUpdateTime; + unsigned long playTime; + unsigned long dbPlayTime; +} mpd_Stats; - void mpd_sendStatsCommand(mpd_Connection * connection); +void mpd_sendStatsCommand(mpd_Connection * connection); - mpd_Stats *mpd_getStats(mpd_Connection * connection); +mpd_Stats * mpd_getStats(mpd_Connection * connection); - void mpd_freeStats(mpd_Stats * stats); +void mpd_freeStats(mpd_Stats * stats); /* SONG STUFF */ @@ -221,28 +240,41 @@ extern "C" { /* mpd_Song * for storing song info returned by mpd */ - typedef struct _mpd_Song { - /* filename of song */ - char *file; - /* artist, maybe NULL if there is no tag */ - char *artist; - /* title, maybe NULL if there is no tag */ - char *title; - /* album, maybe NULL if there is no tag */ - char *album; - /* track, maybe NULL if there is no tag */ - char *track; - /* name, maybe NULL if there is no tag; it's the name of the current - * song, f.e. the icyName of the stream */ - char *name; - /* length of song in seconds, check that it is not MPD_SONG_NO_TIME */ - int time; - /* if plchanges/playlistinfo/playlistid used, is the position of the - * song in the playlist */ - int pos; - /* song id for a song in the playlist */ - int id; - } mpd_Song; +typedef struct _mpd_Song { + /* filename of song */ + char * file; + /* artist, maybe NULL if there is no tag */ + char * artist; + /* title, maybe NULL if there is no tag */ + char * title; + /* album, maybe NULL if there is no tag */ + char * album; + /* track, maybe NULL if there is no tag */ + char * track; + /* name, maybe NULL if there is no tag; it's the name of the current + * song, f.e. the icyName of the stream */ + char * name; + /* date */ + char *date; + + /* added by qball */ + /* Genre */ + char *genre; + /* Composer */ + char *composer; + /* Disc */ + char *disc; + /* Comment */ + char *comment; + + /* length of song in seconds, check that it is not MPD_SONG_NO_TIME */ + int time; + /* if plchanges/playlistinfo/playlistid used, is the position of the + * song in the playlist */ + int pos; + /* song id for a song in the playlist */ + int id; +} mpd_Song; /* mpd_newSong * use to allocate memory for a new mpd_Song @@ -252,70 +284,70 @@ extern "C" { * use mpd_freeSong to free the memory for the mpd_Song, it will also * free memory for file, artist, etc, so don't do it yourself */ - mpd_Song *mpd_newSong(); +mpd_Song * mpd_newSong(void); /* mpd_freeSong * use to free memory allocated by mpd_newSong * also it will free memory pointed to by file, artist, etc, so be careful */ - void mpd_freeSong(mpd_Song * song); +void mpd_freeSong(mpd_Song * song); /* mpd_songDup * works like strDup, but for a mpd_Song */ - mpd_Song *mpd_songDup(mpd_Song * song); +mpd_Song * mpd_songDup(mpd_Song * song); /* DIRECTORY STUFF */ /* mpd_Directory * used to store info fro directory (right now that just the path) */ - typedef struct _mpd_Directory { - char *path; - } mpd_Directory; +typedef struct _mpd_Directory { + char * path; +} mpd_Directory; /* mpd_newDirectory * allocates memory for a new directory * use mpd_freeDirectory to free this memory */ - mpd_Directory *mpd_newDirectory(); +mpd_Directory * mpd_newDirectory(void); /* mpd_freeDirectory * used to free memory allocated with mpd_newDirectory, and it frees * path of mpd_Directory, so be careful */ - void mpd_freeDirectory(mpd_Directory * directory); +void mpd_freeDirectory(mpd_Directory * directory); /* mpd_directoryDup * works like strdup, but for mpd_Directory */ - mpd_Directory *mpd_directoryDup(mpd_Directory * directory); +mpd_Directory * mpd_directoryDup(mpd_Directory * directory); /* PLAYLISTFILE STUFF */ /* mpd_PlaylistFile * stores info about playlist file returned by lsinfo */ - typedef struct _mpd_PlaylistFile { - char *path; - } mpd_PlaylistFile; +typedef struct _mpd_PlaylistFile { + char * path; +} mpd_PlaylistFile; /* mpd_newPlaylistFile * allocates memory for new mpd_PlaylistFile, path is set to NULL * free this memory with mpd_freePlaylistFile */ - mpd_PlaylistFile *mpd_newPlaylistFile(); +mpd_PlaylistFile * mpd_newPlaylistFile(void); /* mpd_freePlaylist * free memory allocated for freePlaylistFile, will also free * path, so be careful */ - void mpd_freePlaylistFile(mpd_PlaylistFile * playlist); +void mpd_freePlaylistFile(mpd_PlaylistFile * playlist); /* mpd_playlistFileDup * works like strdup, but for mpd_PlaylistFile */ - mpd_PlaylistFile *mpd_playlistFileDup(mpd_PlaylistFile * playlist); +mpd_PlaylistFile * mpd_playlistFileDup(mpd_PlaylistFile * playlist); /* INFO ENTITY STUFF */ @@ -329,184 +361,265 @@ extern "C" { /* mpd_InfoEntity * stores info on stuff returned info commands */ - typedef struct mpd_InfoEntity { - /* the type of entity, use with MPD_INFO_ENTITY_TYPE_* to determine - * what this entity is (song, directory, etc...) - */ - int type; - /* the actual data you want, mpd_Song, mpd_Directory, etc */ - union { - mpd_Directory *directory; - mpd_Song *song; - mpd_PlaylistFile *playlistFile; - } info; - } mpd_InfoEntity; +typedef struct mpd_InfoEntity { + /* the type of entity, use with MPD_INFO_ENTITY_TYPE_* to determine + * what this entity is (song, directory, etc...) + */ + int type; + /* the actual data you want, mpd_Song, mpd_Directory, etc */ + union { + mpd_Directory * directory; + mpd_Song * song; + mpd_PlaylistFile * playlistFile; + } info; +} mpd_InfoEntity; - mpd_InfoEntity *mpd_newInfoEntity(); +mpd_InfoEntity * mpd_newInfoEntity(void); - void mpd_freeInfoEntity(mpd_InfoEntity * entity); +void mpd_freeInfoEntity(mpd_InfoEntity * entity); /* INFO COMMANDS AND STUFF */ /* use this function to loop over after calling Info/Listall functions */ - mpd_InfoEntity *mpd_getNextInfoEntity(mpd_Connection * connection); +mpd_InfoEntity * mpd_getNextInfoEntity(mpd_Connection * connection); /* fetches the currently seeletect song (the song referenced by status->song * and status->songid*/ - void mpd_sendCurrentSongCommand(mpd_Connection * connection); +void mpd_sendCurrentSongCommand(mpd_Connection * connection); /* songNum of -1, means to display the whole list */ - void mpd_sendPlaylistInfoCommand(mpd_Connection * connection, - int songNum); +void mpd_sendPlaylistInfoCommand(mpd_Connection * connection, int songNum); + +/* songId of -1, means to display the whole list */ +void mpd_sendPlaylistIdCommand(mpd_Connection * connection, int songId); /* use this to get the changes in the playlist since version _playlist_ */ - void mpd_sendPlChangesCommand(mpd_Connection * connection, - long long playlist); +void mpd_sendPlChangesCommand(mpd_Connection * connection, long long playlist); -/* recursivel fetches all songs/dir/playlists in "dir* (no metadata is +/** + * @param connection: A valid and connected mpd_Connection. + * @param playlist: The playlist version you want the diff with. + * A more bandwidth efficient version of the mpd_sendPlChangesCommand. + * It only returns the pos+id of the changes song. + */ +void mpd_sendPlChangesPosIdCommand(mpd_Connection * connection, long long playlist); + +/* recursivel fetches all songs/dir/playlists in "dir* (no metadata is * returned) */ - void mpd_sendListallCommand(mpd_Connection * connection, - const char *dir); +void mpd_sendListallCommand(mpd_Connection * connection, const char * dir); /* same as sendListallCommand, but also metadata is returned */ - void mpd_sendListallInfoCommand(mpd_Connection * connection, - const char *dir); +void mpd_sendListallInfoCommand(mpd_Connection * connection, const char * dir); /* non-recursive version of ListallInfo */ - void mpd_sendLsInfoCommand(mpd_Connection * connection, - const char *dir); +void mpd_sendLsInfoCommand(mpd_Connection * connection, const char * dir); #define MPD_TABLE_ARTIST 0 #define MPD_TABLE_ALBUM 1 #define MPD_TABLE_TITLE 2 #define MPD_TABLE_FILENAME 3 - void mpd_sendSearchCommand(mpd_Connection * connection, int table, - const char *str); +void mpd_sendSearchCommand(mpd_Connection * connection, int table, + const char * str); - void mpd_sendFindCommand(mpd_Connection * connection, int table, - const char *str); +void mpd_sendFindCommand(mpd_Connection * connection, int table, + const char * str); /* LIST TAG COMMANDS */ -/* use this function fetch next artist entry, be sure to free the returned +/* use this function fetch next artist entry, be sure to free the returned * string. NULL means there are no more. Best used with sendListArtists */ - char *mpd_getNextArtist(mpd_Connection * connection); +char * mpd_getNextArtist(mpd_Connection * connection); - char *mpd_getNextAlbum(mpd_Connection * connection); +char * mpd_getNextAlbum(mpd_Connection * connection); + +char * mpd_getNextTag(mpd_Connection *connection, int table); /* list artist or albums by artist, arg1 should be set to the artist if * listing albums by a artist, otherwise NULL for listing all artists or albums */ - void mpd_sendListCommand(mpd_Connection * connection, int table, - const char *arg1); +void mpd_sendListCommand(mpd_Connection * connection, int table, + const char * arg1); /* SIMPLE COMMANDS */ - void mpd_sendAddCommand(mpd_Connection * connection, - const char *file); +void mpd_sendAddCommand(mpd_Connection * connection, const char * file); - void mpd_sendDeleteCommand(mpd_Connection * connection, - int songNum); +void mpd_sendDeleteCommand(mpd_Connection * connection, int songNum); - void mpd_sendDeleteIdCommand(mpd_Connection * connection, - int songNum); +void mpd_sendDeleteIdCommand(mpd_Connection * connection, int songNum); - void mpd_sendSaveCommand(mpd_Connection * connection, - const char *name); +void mpd_sendSaveCommand(mpd_Connection * connection, const char * name); - void mpd_sendLoadCommand(mpd_Connection * connection, - const char *name); +void mpd_sendLoadCommand(mpd_Connection * connection, const char * name); - void mpd_sendRmCommand(mpd_Connection * connection, - const char *name); +void mpd_sendRmCommand(mpd_Connection * connection, const char * name); - void mpd_sendShuffleCommand(mpd_Connection * connection); +void mpd_sendShuffleCommand(mpd_Connection * connection); - void mpd_sendClearCommand(mpd_Connection * connection); +void mpd_sendClearCommand(mpd_Connection * connection); /* use this to start playing at the beginning, useful when in random mode */ #define MPD_PLAY_AT_BEGINNING -1 - void mpd_sendPlayCommand(mpd_Connection * connection, int songNum); +void mpd_sendPlayCommand(mpd_Connection * connection, int songNum); - void mpd_sendPlayIdCommand(mpd_Connection * connection, - int songNum); +void mpd_sendPlayIdCommand(mpd_Connection * connection, int songNum); - void mpd_sendStopCommand(mpd_Connection * connection); +void mpd_sendStopCommand(mpd_Connection * connection); - void mpd_sendPauseCommand(mpd_Connection * connection, - int pauseMode); +void mpd_sendPauseCommand(mpd_Connection * connection, int pauseMode); - void mpd_sendNextCommand(mpd_Connection * connection); +void mpd_sendNextCommand(mpd_Connection * connection); - void mpd_sendPrevCommand(mpd_Connection * connection); +void mpd_sendPrevCommand(mpd_Connection * connection); - void mpd_sendMoveCommand(mpd_Connection * connection, int from, - int to); +void mpd_sendMoveCommand(mpd_Connection * connection, int from, int to); - void mpd_sendMoveIdCommand(mpd_Connection * connection, int from, - int to); +void mpd_sendMoveIdCommand(mpd_Connection * connection, int from, int to); - void mpd_sendSwapCommand(mpd_Connection * connection, int song1, - int song2); +void mpd_sendSwapCommand(mpd_Connection * connection, int song1, int song2); - void mpd_sendSwapIdCommand(mpd_Connection * connection, int song1, - int song2); +void mpd_sendSwapIdCommand(mpd_Connection * connection, int song1, int song2); - void mpd_sendSeekCommand(mpd_Connection * connection, int song, - int time); +void mpd_sendSeekCommand(mpd_Connection * connection, int song, int time); - void mpd_sendSeekIdCommand(mpd_Connection * connection, int song, - int time); +void mpd_sendSeekIdCommand(mpd_Connection * connection, int song, int time); - void mpd_sendRepeatCommand(mpd_Connection * connection, - int repeatMode); +void mpd_sendRepeatCommand(mpd_Connection * connection, int repeatMode); - void mpd_sendRandomCommand(mpd_Connection * connection, - int randomMode); +void mpd_sendRandomCommand(mpd_Connection * connection, int randomMode); - void mpd_sendSetvolCommand(mpd_Connection * connection, - int volumeChange); +void mpd_sendSetvolCommand(mpd_Connection * connection, int volumeChange); /* WARNING: don't use volume command, its depreacted */ - void mpd_sendVolumeCommand(mpd_Connection * connection, - int volumeChange); +void mpd_sendVolumeCommand(mpd_Connection * connection, int volumeChange); - void mpd_sendCrossfadeCommand(mpd_Connection * connection, - int seconds); +void mpd_sendCrossfadeCommand(mpd_Connection * connection, int seconds); - void mpd_sendUpdateCommand(mpd_Connection * connection, - char *path); +void mpd_sendUpdateCommand(mpd_Connection * connection, char * path); /* returns the update job id, call this after a update command*/ - int mpd_getUpdateId(mpd_Connection * connection); +int mpd_getUpdateId(mpd_Connection * connection); - void mpd_sendPasswordCommand(mpd_Connection * connection, - const char *pass); +void mpd_sendPasswordCommand(mpd_Connection * connection, const char * pass); /* after executing a command, when your done with it to get its status * (you want to check connection->error for an error) */ - void mpd_finishCommand(mpd_Connection * connection); +void mpd_finishCommand(mpd_Connection * connection); /* command list stuff, use this to do things like add files very quickly */ - void mpd_sendCommandListBegin(mpd_Connection * connection); +void mpd_sendCommandListBegin(mpd_Connection * connection); - void mpd_sendCommandListOkBegin(mpd_Connection * connection); +void mpd_sendCommandListOkBegin(mpd_Connection * connection); - void mpd_sendCommandListEnd(mpd_Connection * connection); +void mpd_sendCommandListEnd(mpd_Connection * connection); -/* advance to the next listOk +/* advance to the next listOk * returns 0 if advanced to the next list_OK, * returns -1 if it advanced to an OK or ACK */ - int mpd_nextListOkCommand(mpd_Connection * connection); +int mpd_nextListOkCommand(mpd_Connection * connection); -/* handles SIGPIPE from full close on the server side */ - void mpd_signalHandler(int); +typedef struct _mpd_OutputEntity { + int id; + char * name; + int enabled; +} mpd_OutputEntity; +void mpd_sendOutputsCommand(mpd_Connection * connection); + +mpd_OutputEntity * mpd_getNextOutput(mpd_Connection * connection); + +void mpd_sendEnableOutputCommand(mpd_Connection * connection, int outputId); + +void mpd_sendDisableOutputCommand(mpd_Connection * connection, int outputId); + +void mpd_freeOutputElement(mpd_OutputEntity * output); + +/** + * @param connection a #mpd_Connection + * + * Queries mpd for the allowed commands + */ +void mpd_sendCommandsCommand(mpd_Connection * connection); +/** + * @param connection a #mpd_Connection + * + * Queries mpd for the not allowed commands + */ +void mpd_sendNotCommandsCommand(mpd_Connection * connection); + +/** + * @param connection a #mpd_Connection + * + * returns the next supported command. + * + * @returns a string, needs to be free'ed + */ +char *mpd_getNextCommand(mpd_Connection *connection); + +/** + * @param connection a MpdConnection + * @param path the path to the playlist. + * + * List the content, with full metadata, of a stored playlist. + * + */ +void mpd_sendListPlaylistInfoCommand(mpd_Connection *connection, char *path); +/** + * @param connection a MpdConnection + * @param path the path to the playlist. + * + * List the content of a stored playlist. + * + */ +void mpd_sendListPlaylistCommand(mpd_Connection *connection, char *path); + +/** + * @param connection a #mpd_Connection + * @param exact if to match exact + * + * starts a search, use mpd_addConstraintSearch to add + * a constraint to the search, and mpd_commitSearch to do the actual search + */ +void mpd_startSearch(mpd_Connection * connection,int exact); +/** + * @param connection a #mpd_Connection + * @param field + * @param name + * + */ +void mpd_addConstraintSearch(mpd_Connection *connection, int field, char *name); +/** + * @param connection a #mpd_Connection + * + */ +void mpd_commitSearch(mpd_Connection *connection); + +/** + * @param connection a #mpd_Connection + * @param field The field to search + * + * starts a search for fields... f.e. get a list of artists would be: + * mpd_startFieldSearch(connection, MPD_TAG_ITEM_ARTIST); + * mpd_commitSearch(connection); + * + * or get a list of artist in genre "jazz" would be: + * @code + * mpd_startFieldSearch(connection, MPD_TAG_ITEM_ARTIST); + * mpd_addConstraintSearch(connection, MPD_TAG_ITEM_GENRE, "jazz") + * mpd_commitSearch(connection); + * @endcode + * + * mpd_startSearch will return a list of songs (and you need mpd_getNextInfoEntity) + * this one will return a list of only one field (the field specified with field) and you need + * mpd_getNextTag to get the results + */ +void mpd_startFieldSearch(mpd_Connection * connection,int field); #ifdef __cplusplus } #endif + #endif