2 * (c)2003-2006 by Warren Dukes (warren.dukes@gmail.com)
3 * This project's homepage is: http://www.musicpd.org
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * - Neither the name of the Music Player Daemon nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include "libmpdclient.h"
39 #include <sys/types.h>
40 #include <sys/param.h>
46 # include <ws2tcpip.h>
49 # include <netinet/in.h>
50 # include <arpa/inet.h>
51 # include <sys/socket.h>
55 /* (bits + 1) / 3 (plus the sign character) */
56 #define INTLEN ((sizeof(int) * CHAR_BIT + 1) / 3 + 1)
57 #define LONGLONGLEN ((sizeof(long long) * CHAR_BIT + 1) / 3 + 1)
59 #define COMMAND_LIST 1
60 #define COMMAND_LIST_OK 2
69 # define MSG_DONTWAIT 0
73 # define SELECT_ERRNO_IGNORE (errno == WSAEINTR || errno == WSAEINPROGRESS)
74 # define SENDRECV_ERRNO_IGNORE SELECT_ERRNO_IGNORE
76 # define SELECT_ERRNO_IGNORE (errno == EINTR)
77 # define SENDRECV_ERRNO_IGNORE (errno == EINTR || errno == EAGAIN)
78 # define winsock_dll_error(c) 0
79 # define closesocket(s) close(s)
80 # define WSACleanup() do { /* nothing */ } while (0)
84 static int winsock_dll_error(mpd_Connection *connection)
88 if ((WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0
89 || LOBYTE(wsaData.wVersion) != 2
90 || HIBYTE(wsaData.wVersion) != 2) {
91 strcpy(connection->errorStr, "Could not find usable WinSock DLL.");
92 connection->error = MPD_ERROR_SYSTEM;
98 static int do_connect_fail(mpd_Connection *connection,
99 const struct sockaddr *serv_addr, int addrlen)
101 int iMode = 1; /* 0 = blocking, else non-blocking */
103 ioctlsocket(connection->sock, FIONBIO, (u_long FAR *) &iMode);
104 return (connect(connection->sock, serv_addr, addrlen) == SOCKET_ERROR
105 && WSAGetLastError() != WSAEWOULDBLOCK);
107 #else /* !WIN32 (sane operating systems) */
108 static int do_connect_fail(mpd_Connection *connection,
109 const struct sockaddr *serv_addr, int addrlen)
111 int flags = fcntl(connection->sock, F_GETFL, 0);
113 fcntl(connection->sock, F_SETFL, flags | O_NONBLOCK);
114 return (connect(connection->sock, serv_addr, addrlen) < 0
115 && errno != EINPROGRESS);
120 static int mpd_connect(mpd_Connection *connection, const char *host, int port,
124 char service[INTLEN + 1];
125 struct addrinfo hints;
126 struct addrinfo *res = NULL;
127 struct addrinfo *addrinfo = NULL;
131 * XXX: limit address family to PF_INET here.
132 * MPD does not support IPv6 yet, so if GAI returns
133 * an IPv6 address, the later connect() will fail. */
134 hints.ai_flags = AI_ADDRCONFIG;
135 hints.ai_family = PF_INET;
136 hints.ai_socktype = SOCK_STREAM;
137 hints.ai_protocol = IPPROTO_TCP;
138 hints.ai_addrlen = 0;
139 hints.ai_addr = NULL;
140 hints.ai_canonname = NULL;
141 hints.ai_next = NULL;
143 snprintf(service, sizeof(service), "%i", port);
145 error = getaddrinfo(host, service, &hints, &addrinfo);
148 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
149 "host \"%s\" not found: %s", host, gai_strerror(error));
150 connection->error = MPD_ERROR_UNKHOST;
154 for (res = addrinfo; res; res = res->ai_next) {
156 if (connection->sock > -1) {
157 closesocket(connection->sock);
159 connection->sock = socket(res->ai_family, SOCK_STREAM,
161 if (connection->sock < 0) {
162 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
163 "problems creating socket: %s", strerror(errno));
164 connection->error = MPD_ERROR_SYSTEM;
165 freeaddrinfo(addrinfo);
169 mpd_setConnectionTimeout(connection, timeout);
172 if (do_connect_fail(connection, res->ai_addr, res->ai_addrlen)) {
173 /* try the next address family */
174 closesocket(connection->sock);
175 connection->sock = -1;
180 freeaddrinfo(addrinfo);
182 if (connection->sock < 0) {
183 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
184 "problems connecting to \"%s\" on port %i: %s", host, port,
186 connection->error = MPD_ERROR_CONNPORT;
193 #else /* !MPD_HAVE_GAI */
194 static int mpd_connect(mpd_Connection *connection, const char *host, int port,
197 struct hostent he, *he_res = 0;
200 struct sockaddr *dest;
202 struct sockaddr_in sin;
204 #ifdef HAVE_GETHOSTBYNAME_R
205 if (gethostbyname_r(rhost, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
206 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
207 "%s ('%s')", hstrerror(h_errno), host);
208 connection->error = MPD_ERROR_UNKHOST;
211 #else /* HAVE_GETHOSTBYNAME_R */
212 if (!(he_res = gethostbyname(host))) {
213 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
214 "host \"%s\" not found", host);
215 connection->error = MPD_ERROR_UNKHOST;
218 #endif /* HAVE_GETHOSTBYNAME_R */
220 memset(&sin, 0, sizeof(struct sockaddr_in));
221 /* dest.sin_family = he_res->h_addrtype; */
222 sin.sin_family = AF_INET;
223 sin.sin_port = htons(port);
225 switch (he_res->h_addrtype) {
227 memcpy((char *) &sin.sin_addr.s_addr, (char *) he_res->h_addr,
229 dest = (struct sockaddr *) &sin;
230 destlen = sizeof(struct sockaddr_in);
233 strcpy(connection->errorStr, "address type is not IPv4");
234 connection->error = MPD_ERROR_SYSTEM;
239 if (connection->sock > -1) {
240 closesocket(connection->sock);
242 if ((connection->sock = socket(dest->sa_family, SOCK_STREAM, 0)) < 0) {
243 strcpy(connection->errorStr, "problems creating socket");
244 connection->error = MPD_ERROR_SYSTEM;
248 mpd_setConnectionTimeout(connection, timeout);
251 if (do_connect_fail(connection, dest, destlen)) {
252 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
253 "problems connecting to \"%s\" on port %i", host, port);
254 connection->error = MPD_ERROR_CONNPORT;
260 #endif /* !MPD_HAVE_GAI */
262 const char *mpdTagItemKeys[MPD_TAG_NUM_OF_ITEM_TYPES] = {
278 static char *mpd_sanitizeArg(const char *arg)
282 register const char *c;
285 /* instead of counting in that loop above,
286 * just use a bit more memory and halve running time */
287 ret = malloc(strlen(arg) * 2 + 1);
291 for (i = strlen(arg) + 1; i != 0; --i) {
292 if (*c == '"' || *c == '\\') {
301 static mpd_ReturnElement *mpd_newReturnElement(const char *name,
304 mpd_ReturnElement *ret = malloc(sizeof(mpd_ReturnElement));
306 ret->name = strndup(name, text_buffer_size);
307 ret->value = strndup(value, text_buffer_size);
312 static void mpd_freeReturnElement(mpd_ReturnElement *re)
319 void mpd_setConnectionTimeout(mpd_Connection *connection, float timeout)
321 connection->timeout.tv_sec = (int) timeout;
322 connection->timeout.tv_usec =
323 (int) ((timeout - connection->timeout.tv_sec) * 1e6 + 0.5);
326 static int mpd_parseWelcome(mpd_Connection *connection, const char *host,
327 int port, /* char *rt, */ char *output)
333 if (strncmp(output, MPD_WELCOME_MESSAGE, strlen(MPD_WELCOME_MESSAGE))) {
334 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
335 "mpd not running on port %i on host \"%s\"", port, host);
336 connection->error = MPD_ERROR_NOTMPD;
340 tmp = &output[strlen(MPD_WELCOME_MESSAGE)];
342 for (i = 0; i < 3; i++) {
344 connection->version[i] = strtol(tmp, &test, 10);
347 if (!tmp || (test[0] != '.' && test[0] != '\0')) {
348 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
349 "error parsing version number at \"%s\"",
350 &output[strlen(MPD_WELCOME_MESSAGE)]);
351 connection->error = MPD_ERROR_NOTMPD;
360 mpd_Connection *mpd_newConnection(const char *host, int port, float timeout)
365 mpd_Connection *connection = malloc(sizeof(mpd_Connection));
369 strcpy(connection->buffer, "");
370 connection->buflen = 0;
371 connection->bufstart = 0;
372 strcpy(connection->errorStr, "");
373 connection->error = 0;
374 connection->doneProcessing = 0;
375 connection->commandList = 0;
376 connection->listOks = 0;
377 connection->doneListOk = 0;
378 connection->sock = -1;
379 connection->returnElement = NULL;
380 connection->request = NULL;
382 if (winsock_dll_error(connection)) {
386 if (mpd_connect(connection, host, port, timeout) < 0) {
390 while (!(rt = strstr(connection->buffer, "\n"))) {
391 tv.tv_sec = connection->timeout.tv_sec;
392 tv.tv_usec = connection->timeout.tv_usec;
394 FD_SET(connection->sock, &fds);
395 if ((err = select(connection->sock + 1, &fds, NULL, NULL, &tv)) == 1) {
398 readed = recv(connection->sock,
399 &(connection->buffer[connection->buflen]),
400 MPD_BUFFER_MAX_LENGTH - connection->buflen, 0);
402 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
403 "problems getting a response from \"%s\" on port %i : %s",
404 host, port, strerror(errno));
405 connection->error = MPD_ERROR_NORESPONSE;
408 connection->buflen += readed;
409 connection->buffer[connection->buflen] = '\0';
410 } else if (err < 0) {
411 if (SELECT_ERRNO_IGNORE) {
414 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
415 "problems connecting to \"%s\" on port %i", host, port);
416 connection->error = MPD_ERROR_CONNPORT;
419 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
420 "timeout in attempting to get a response from \"%s\" on "
421 "port %i", host, port);
422 connection->error = MPD_ERROR_NORESPONSE;
428 output = strndup(connection->buffer, text_buffer_size);
429 strcpy(connection->buffer, rt + 1);
430 connection->buflen = strlen(connection->buffer);
432 if (mpd_parseWelcome(connection, host, port, /* rt, */ output) == 0) {
433 connection->doneProcessing = 1;
441 void mpd_clearError(mpd_Connection *connection)
443 connection->error = 0;
444 connection->errorStr[0] = '\0';
447 void mpd_closeConnection(mpd_Connection *connection)
449 closesocket(connection->sock);
450 if (connection->returnElement) {
451 free(connection->returnElement);
453 if (connection->request) {
454 free(connection->request);
460 static void mpd_executeCommand(mpd_Connection *connection, const char *command)
465 const char *commandPtr = command;
466 int commandLen = strlen(command);
468 if (!connection->doneProcessing && !connection->commandList) {
469 strcpy(connection->errorStr, "not done processing current command");
470 connection->error = 1;
474 mpd_clearError(connection);
477 FD_SET(connection->sock, &fds);
478 tv.tv_sec = connection->timeout.tv_sec;
479 tv.tv_usec = connection->timeout.tv_usec;
481 while ((ret = select(connection->sock + 1, NULL, &fds, NULL, &tv) == 1)
482 || (ret == -1 && SELECT_ERRNO_IGNORE)) {
483 ret = send(connection->sock, commandPtr, commandLen, MSG_DONTWAIT);
485 if (SENDRECV_ERRNO_IGNORE) {
488 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
489 "problems giving command \"%s\"", command);
490 connection->error = MPD_ERROR_SENDING;
497 if (commandLen <= 0) {
502 if (commandLen > 0) {
504 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
505 "timeout sending command \"%s\"", command);
506 connection->error = MPD_ERROR_TIMEOUT;
510 if (!connection->commandList) {
511 connection->doneProcessing = 0;
512 } else if (connection->commandList == COMMAND_LIST_OK) {
513 connection->listOks++;
517 static void mpd_getNextReturnElement(mpd_Connection *connection)
527 char *bufferCheck = NULL;
531 if (connection->returnElement) {
532 mpd_freeReturnElement(connection->returnElement);
534 connection->returnElement = NULL;
536 if (connection->doneProcessing
537 || (connection->listOks && connection->doneListOk)) {
538 strcpy(connection->errorStr,
539 "already done processing current command");
540 connection->error = 1;
544 bufferCheck = connection->buffer + connection->bufstart;
545 while (connection->bufstart >= connection->buflen
546 || !(rt = strchr(bufferCheck, '\n'))) {
547 if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
548 memmove(connection->buffer,
549 connection->buffer + connection->bufstart,
550 connection->buflen - connection->bufstart + 1);
551 connection->buflen -= connection->bufstart;
552 connection->bufstart = 0;
554 if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
555 strcpy(connection->errorStr, "buffer overrun");
556 connection->error = MPD_ERROR_BUFFEROVERRUN;
557 connection->doneProcessing = 1;
558 connection->doneListOk = 0;
561 bufferCheck = connection->buffer + connection->buflen;
562 tv.tv_sec = connection->timeout.tv_sec;
563 tv.tv_usec = connection->timeout.tv_usec;
565 FD_SET(connection->sock, &fds);
566 if ((err = select(connection->sock + 1, &fds, NULL, NULL, &tv) == 1)) {
567 readed = recv(connection->sock,
568 connection->buffer + connection->buflen,
569 MPD_BUFFER_MAX_LENGTH - connection->buflen, MSG_DONTWAIT);
570 if (readed < 0 && SENDRECV_ERRNO_IGNORE) {
574 strcpy(connection->errorStr, "connection closed");
575 connection->error = MPD_ERROR_CONNCLOSED;
576 connection->doneProcessing = 1;
577 connection->doneListOk = 0;
580 connection->buflen += readed;
581 connection->buffer[connection->buflen] = '\0';
582 } else if (err < 0 && SELECT_ERRNO_IGNORE) {
585 strcpy(connection->errorStr, "connection timeout");
586 connection->error = MPD_ERROR_TIMEOUT;
587 connection->doneProcessing = 1;
588 connection->doneListOk = 0;
594 output = connection->buffer + connection->bufstart;
595 connection->bufstart = rt - connection->buffer + 1;
597 if (strcmp(output, "OK") == 0) {
598 if (connection->listOks > 0) {
599 strcpy(connection->errorStr, "expected more list_OK's");
600 connection->error = 1;
602 connection->listOks = 0;
603 connection->doneProcessing = 1;
604 connection->doneListOk = 0;
608 if (strcmp(output, "list_OK") == 0) {
609 if (!connection->listOks) {
610 strcpy(connection->errorStr, "got an unexpected list_OK");
611 connection->error = 1;
613 connection->doneListOk = 1;
614 connection->listOks--;
619 if (strncmp(output, "ACK", strlen("ACK")) == 0) {
624 strcpy(connection->errorStr, output);
625 connection->error = MPD_ERROR_ACK;
626 connection->errorCode = MPD_ACK_ERROR_UNK;
627 connection->errorAt = MPD_ERROR_AT_UNK;
628 connection->doneProcessing = 1;
629 connection->doneListOk = 0;
631 needle = strchr(output, '[');
635 val = strtol(needle + 1, &test, 10);
639 connection->errorCode = val;
640 val = strtol(test + 1, &test, 10);
644 connection->errorAt = val;
648 tok = strchr(output, ':');
657 if (value[0] == ' ') {
658 connection->returnElement = mpd_newReturnElement(name, &(value[1]));
660 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
661 "error parsing: %s:%s", name, value);
662 connection->error = 1;
666 void mpd_finishCommand(mpd_Connection *connection)
668 while (!connection->doneProcessing) {
669 if (connection->doneListOk) {
670 connection->doneListOk = 0;
672 mpd_getNextReturnElement(connection);
676 static void mpd_finishListOkCommand(mpd_Connection *connection)
678 while (!connection->doneProcessing && connection->listOks
679 && !connection->doneListOk) {
680 mpd_getNextReturnElement(connection);
684 int mpd_nextListOkCommand(mpd_Connection *connection)
686 mpd_finishListOkCommand(connection);
687 if (!connection->doneProcessing) {
688 connection->doneListOk = 0;
690 if (connection->listOks == 0 || connection->doneProcessing) {
696 void mpd_sendStatusCommand(mpd_Connection *connection)
698 mpd_executeCommand(connection, "status\n");
701 mpd_Status *mpd_getStatus(mpd_Connection *connection)
705 /* mpd_executeCommand(connection, "status\n");
706 if (connection->error) {
710 if (connection->doneProcessing
711 || (connection->listOks && connection->doneListOk)) {
715 if (!connection->returnElement) {
716 mpd_getNextReturnElement(connection);
719 status = malloc(sizeof(mpd_Status));
723 status->playlist = -1;
724 status->playlistLength = -1;
728 status->elapsedTime = 0;
729 status->totalTime = 0;
731 status->sampleRate = 0;
733 status->channels = 0;
734 status->crossfade = -1;
735 status->error = NULL;
736 status->updatingDb = 0;
738 if (connection->error) {
742 while (connection->returnElement) {
743 mpd_ReturnElement *re = connection->returnElement;
745 if (strcmp(re->name, "volume") == 0) {
746 status->volume = atoi(re->value);
747 } else if (strcmp(re->name, "repeat") == 0) {
748 status->repeat = atoi(re->value);
749 } else if (strcmp(re->name, "random") == 0) {
750 status->random = atoi(re->value);
751 } else if (strcmp(re->name, "playlist") == 0) {
752 status->playlist = strtol(re->value, NULL, 10);
753 } else if (strcmp(re->name, "playlistlength") == 0) {
754 status->playlistLength = atoi(re->value);
755 } else if (strcmp(re->name, "bitrate") == 0) {
756 status->bitRate = atoi(re->value);
757 } else if (strcmp(re->name, "state") == 0) {
758 if (strcmp(re->value, "play") == 0) {
759 status->state = MPD_STATUS_STATE_PLAY;
760 } else if (strcmp(re->value, "stop") == 0) {
761 status->state = MPD_STATUS_STATE_STOP;
762 } else if (strcmp(re->value, "pause") == 0) {
763 status->state = MPD_STATUS_STATE_PAUSE;
765 status->state = MPD_STATUS_STATE_UNKNOWN;
767 } else if (strcmp(re->name, "song") == 0) {
768 status->song = atoi(re->value);
769 } else if (strcmp(re->name, "songid") == 0) {
770 status->songid = atoi(re->value);
771 } else if (strcmp(re->name, "time") == 0) {
772 char *tok = strchr(re->value, ':');
774 /* the second strchr below is a safety check */
775 if (tok && (strchr(tok, 0) > (tok + 1))) {
776 /* atoi stops at the first non-[0-9] char: */
777 status->elapsedTime = atoi(re->value);
778 status->totalTime = atoi(tok + 1);
780 } else if (strcmp(re->name, "error") == 0) {
781 status->error = strndup(re->value, text_buffer_size);
782 } else if (strcmp(re->name, "xfade") == 0) {
783 status->crossfade = atoi(re->value);
784 } else if (strcmp(re->name, "updating_db") == 0) {
785 status->updatingDb = atoi(re->value);
786 } else if (strcmp(re->name, "audio") == 0) {
787 char *tok = strchr(re->value, ':');
789 if (tok && (strchr(tok, 0) > (tok + 1))) {
790 status->sampleRate = atoi(re->value);
791 status->bits = atoi(++tok);
792 tok = strchr(tok, ':');
793 if (tok && (strchr(tok, 0) > (tok + 1))) {
794 status->channels = atoi(tok + 1);
799 mpd_getNextReturnElement(connection);
800 if (connection->error) {
806 if (connection->error) {
809 } else if (status->state < 0) {
810 strcpy(connection->errorStr, "state not found");
811 connection->error = 1;
819 void mpd_freeStatus(mpd_Status *status)
827 void mpd_sendStatsCommand(mpd_Connection *connection)
829 mpd_executeCommand(connection, "stats\n");
832 mpd_Stats *mpd_getStats(mpd_Connection *connection)
836 /* mpd_executeCommand(connection, "stats\n");
837 if (connection->error) {
841 if (connection->doneProcessing
842 || (connection->listOks && connection->doneListOk)) {
846 if (!connection->returnElement) {
847 mpd_getNextReturnElement(connection);
850 stats = malloc(sizeof(mpd_Stats));
851 stats->numberOfArtists = 0;
852 stats->numberOfAlbums = 0;
853 stats->numberOfSongs = 0;
855 stats->dbUpdateTime = 0;
857 stats->dbPlayTime = 0;
859 if (connection->error) {
863 while (connection->returnElement) {
864 mpd_ReturnElement *re = connection->returnElement;
866 if (strcmp(re->name, "artists") == 0) {
867 stats->numberOfArtists = atoi(re->value);
868 } else if (strcmp(re->name, "albums") == 0) {
869 stats->numberOfAlbums = atoi(re->value);
870 } else if (strcmp(re->name, "songs") == 0) {
871 stats->numberOfSongs = atoi(re->value);
872 } else if (strcmp(re->name, "uptime") == 0) {
873 stats->uptime = strtol(re->value, NULL, 10);
874 } else if (strcmp(re->name, "db_update") == 0) {
875 stats->dbUpdateTime = strtol(re->value, NULL, 10);
876 } else if (strcmp(re->name, "playtime") == 0) {
877 stats->playTime = strtol(re->value, NULL, 10);
878 } else if (strcmp(re->name, "db_playtime") == 0) {
879 stats->dbPlayTime = strtol(re->value, NULL, 10);
882 mpd_getNextReturnElement(connection);
883 if (connection->error) {
889 if (connection->error) {
897 void mpd_freeStats(mpd_Stats *stats)
902 mpd_SearchStats *mpd_getSearchStats(mpd_Connection *connection)
904 mpd_SearchStats *stats;
905 mpd_ReturnElement *re;
907 if (connection->doneProcessing
908 || (connection->listOks && connection->doneListOk)) {
912 if (!connection->returnElement) {
913 mpd_getNextReturnElement(connection);
916 if (connection->error) {
920 stats = malloc(sizeof(mpd_SearchStats));
921 stats->numberOfSongs = 0;
924 while (connection->returnElement) {
925 re = connection->returnElement;
927 if (strcmp(re->name, "songs") == 0) {
928 stats->numberOfSongs = atoi(re->value);
929 } else if (strcmp(re->name, "playtime") == 0) {
930 stats->playTime = strtol(re->value, NULL, 10);
933 mpd_getNextReturnElement(connection);
934 if (connection->error) {
940 if (connection->error) {
948 void mpd_freeSearchStats(mpd_SearchStats *stats)
953 static void mpd_initSong(mpd_Song *song)
964 song->composer = NULL;
965 song->performer = NULL;
967 song->comment = NULL;
969 song->time = MPD_SONG_NO_TIME;
970 song->pos = MPD_SONG_NO_NUM;
971 song->id = MPD_SONG_NO_ID;
974 static void mpd_finishSong(mpd_Song *song)
1000 if (song->composer) {
1001 free(song->composer);
1006 if (song->comment) {
1007 free(song->comment);
1011 mpd_Song *mpd_newSong(void)
1013 mpd_Song *ret = malloc(sizeof(mpd_Song));
1020 void mpd_freeSong(mpd_Song *song)
1022 mpd_finishSong(song);
1026 mpd_Song *mpd_songDup(mpd_Song *song)
1028 mpd_Song *ret = mpd_newSong();
1031 ret->file = strndup(song->file, text_buffer_size);
1034 ret->artist = strndup(song->artist, text_buffer_size);
1037 ret->album = strndup(song->album, text_buffer_size);
1040 ret->title = strndup(song->title, text_buffer_size);
1043 ret->track = strndup(song->track, text_buffer_size);
1046 ret->name = strndup(song->name, text_buffer_size);
1049 ret->date = strndup(song->date, text_buffer_size);
1052 ret->genre = strndup(song->genre, text_buffer_size);
1054 if (song->composer) {
1055 ret->composer = strndup(song->composer, text_buffer_size);
1058 ret->disc = strndup(song->disc, text_buffer_size);
1060 if (song->comment) {
1061 ret->comment = strndup(song->comment, text_buffer_size);
1063 ret->time = song->time;
1064 ret->pos = song->pos;
1070 static void mpd_initDirectory(mpd_Directory *directory)
1072 directory->path = NULL;
1075 static void mpd_finishDirectory(mpd_Directory *directory)
1077 if (directory->path) {
1078 free(directory->path);
1082 mpd_Directory *mpd_newDirectory(void)
1084 mpd_Directory *directory = malloc(sizeof(mpd_Directory));
1086 mpd_initDirectory(directory);
1091 void mpd_freeDirectory(mpd_Directory *directory)
1093 mpd_finishDirectory(directory);
1098 mpd_Directory *mpd_directoryDup(mpd_Directory *directory)
1100 mpd_Directory *ret = mpd_newDirectory();
1102 if (directory->path) {
1103 ret->path = strndup(directory->path, text_buffer_size);
1109 static void mpd_initPlaylistFile(mpd_PlaylistFile *playlist)
1111 playlist->path = NULL;
1114 static void mpd_finishPlaylistFile(mpd_PlaylistFile *playlist)
1116 if (playlist->path) {
1117 free(playlist->path);
1121 mpd_PlaylistFile *mpd_newPlaylistFile(void)
1123 mpd_PlaylistFile *playlist = malloc(sizeof(mpd_PlaylistFile));
1125 mpd_initPlaylistFile(playlist);
1130 void mpd_freePlaylistFile(mpd_PlaylistFile *playlist)
1132 mpd_finishPlaylistFile(playlist);
1136 mpd_PlaylistFile *mpd_playlistFileDup(mpd_PlaylistFile *playlist)
1138 mpd_PlaylistFile *ret = mpd_newPlaylistFile();
1140 if (playlist->path) {
1141 ret->path = strndup(playlist->path, text_buffer_size);
1147 static void mpd_initInfoEntity(mpd_InfoEntity *entity)
1149 entity->info.directory = NULL;
1152 static void mpd_finishInfoEntity(mpd_InfoEntity *entity)
1154 if (entity->info.directory) {
1155 if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1156 mpd_freeDirectory(entity->info.directory);
1157 } else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
1158 mpd_freeSong(entity->info.song);
1159 } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1160 mpd_freePlaylistFile(entity->info.playlistFile);
1165 mpd_InfoEntity *mpd_newInfoEntity(void)
1167 mpd_InfoEntity *entity = malloc(sizeof(mpd_InfoEntity));
1169 mpd_initInfoEntity(entity);
1174 void mpd_freeInfoEntity(mpd_InfoEntity *entity)
1176 mpd_finishInfoEntity(entity);
1180 static void mpd_sendInfoCommand(mpd_Connection *connection, char *command)
1182 mpd_executeCommand(connection, command);
1185 mpd_InfoEntity *mpd_getNextInfoEntity(mpd_Connection *connection)
1187 mpd_InfoEntity *entity = NULL;
1189 if (connection->doneProcessing
1190 || (connection->listOks && connection->doneListOk)) {
1194 if (!connection->returnElement) {
1195 mpd_getNextReturnElement(connection);
1198 if (connection->returnElement) {
1199 if (strcmp(connection->returnElement->name, "file") == 0) {
1200 entity = mpd_newInfoEntity();
1201 entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1202 entity->info.song = mpd_newSong();
1203 entity->info.song->file = strndup(connection->returnElement->value, text_buffer_size);
1204 } else if (strcmp(connection->returnElement->name, "directory") == 0) {
1205 entity = mpd_newInfoEntity();
1206 entity->type = MPD_INFO_ENTITY_TYPE_DIRECTORY;
1207 entity->info.directory = mpd_newDirectory();
1208 entity->info.directory->path =
1209 strndup(connection->returnElement->value, text_buffer_size);
1210 } else if (strcmp(connection->returnElement->name, "playlist") == 0) {
1211 entity = mpd_newInfoEntity();
1212 entity->type = MPD_INFO_ENTITY_TYPE_PLAYLISTFILE;
1213 entity->info.playlistFile = mpd_newPlaylistFile();
1214 entity->info.playlistFile->path =
1215 strndup(connection->returnElement->value, text_buffer_size);
1216 } else if (strcmp(connection->returnElement->name, "cpos") == 0) {
1217 entity = mpd_newInfoEntity();
1218 entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1219 entity->info.song = mpd_newSong();
1220 entity->info.song->pos = atoi(connection->returnElement->value);
1222 connection->error = 1;
1223 strcpy(connection->errorStr, "problem parsing song info");
1230 mpd_getNextReturnElement(connection);
1231 while (connection->returnElement) {
1232 mpd_ReturnElement *re = connection->returnElement;
1234 if (strcmp(re->name, "file") == 0) {
1236 } else if (strcmp(re->name, "directory") == 0) {
1238 } else if (strcmp(re->name, "playlist") == 0) {
1240 } else if (strcmp(re->name, "cpos") == 0) {
1244 if (entity->type == MPD_INFO_ENTITY_TYPE_SONG && strlen(re->value)) {
1245 if (!entity->info.song->artist
1246 && strcmp(re->name, "Artist") == 0) {
1247 entity->info.song->artist = strndup(re->value, text_buffer_size);
1248 } else if (!entity->info.song->album
1249 && strcmp(re->name, "Album") == 0) {
1250 entity->info.song->album = strndup(re->value, text_buffer_size);
1251 } else if (!entity->info.song->title
1252 && strcmp(re->name, "Title") == 0) {
1253 entity->info.song->title = strndup(re->value, text_buffer_size);
1254 } else if (!entity->info.song->track
1255 && strcmp(re->name, "Track") == 0) {
1256 entity->info.song->track = strndup(re->value, text_buffer_size);
1257 } else if (!entity->info.song->name
1258 && strcmp(re->name, "Name") == 0) {
1259 entity->info.song->name = strndup(re->value, text_buffer_size);
1260 } else if (entity->info.song->time == MPD_SONG_NO_TIME
1261 && strcmp(re->name, "Time") == 0) {
1262 entity->info.song->time = atoi(re->value);
1263 } else if (entity->info.song->pos == MPD_SONG_NO_NUM
1264 && strcmp(re->name, "Pos") == 0) {
1265 entity->info.song->pos = atoi(re->value);
1266 } else if (entity->info.song->id == MPD_SONG_NO_ID
1267 && strcmp(re->name, "Id") == 0) {
1268 entity->info.song->id = atoi(re->value);
1269 } else if (!entity->info.song->date
1270 && strcmp(re->name, "Date") == 0) {
1271 entity->info.song->date = strndup(re->value, text_buffer_size);
1272 } else if (!entity->info.song->genre
1273 && strcmp(re->name, "Genre") == 0) {
1274 entity->info.song->genre = strndup(re->value, text_buffer_size);
1275 } else if (!entity->info.song->composer
1276 && strcmp(re->name, "Composer") == 0) {
1277 entity->info.song->composer = strndup(re->value, text_buffer_size);
1278 } else if (!entity->info.song->performer
1279 && strcmp(re->name, "Performer") == 0) {
1280 entity->info.song->performer = strndup(re->value, text_buffer_size);
1281 } else if (!entity->info.song->disc
1282 && strcmp(re->name, "Disc") == 0) {
1283 entity->info.song->disc = strndup(re->value, text_buffer_size);
1284 } else if (!entity->info.song->comment
1285 && strcmp(re->name, "Comment") == 0) {
1286 entity->info.song->comment = strndup(re->value, text_buffer_size);
1288 } else if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1289 } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1292 mpd_getNextReturnElement(connection);
1298 static char *mpd_getNextReturnElementNamed(mpd_Connection *connection,
1301 if (connection->doneProcessing
1302 || (connection->listOks && connection->doneListOk)) {
1306 mpd_getNextReturnElement(connection);
1307 while (connection->returnElement) {
1308 mpd_ReturnElement *re = connection->returnElement;
1310 if (strcmp(re->name, name) == 0) {
1311 return strndup(re->value, text_buffer_size);
1313 mpd_getNextReturnElement(connection);
1319 char *mpd_getNextTag(mpd_Connection *connection, int type)
1321 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES
1322 || type == MPD_TAG_ITEM_ANY) {
1325 if (type == MPD_TAG_ITEM_FILENAME) {
1326 return mpd_getNextReturnElementNamed(connection, "file");
1328 return mpd_getNextReturnElementNamed(connection, mpdTagItemKeys[type]);
1331 char *mpd_getNextArtist(mpd_Connection *connection)
1333 return mpd_getNextReturnElementNamed(connection, "Artist");
1336 char *mpd_getNextAlbum(mpd_Connection *connection)
1338 return mpd_getNextReturnElementNamed(connection, "Album");
1341 void mpd_sendPlaylistInfoCommand(mpd_Connection *connection, int songPos)
1343 int len = strlen("playlistinfo") + 2 + INTLEN + 3;
1344 char *string = malloc(len);
1346 snprintf(string, len, "playlistinfo \"%i\"\n", songPos);
1347 mpd_sendInfoCommand(connection, string);
1351 void mpd_sendPlaylistIdCommand(mpd_Connection *connection, int id)
1353 int len = strlen("playlistid") + 2 + INTLEN + 3;
1354 char *string = malloc(len);
1356 snprintf(string, len, "playlistid \"%i\"\n", id);
1357 mpd_sendInfoCommand(connection, string);
1361 void mpd_sendPlChangesCommand(mpd_Connection *connection, long long playlist)
1363 int len = strlen("plchanges") + 2 + LONGLONGLEN + 3;
1364 char *string = malloc(len);
1366 snprintf(string, len, "plchanges \"%lld\"\n", playlist);
1367 mpd_sendInfoCommand(connection, string);
1371 void mpd_sendPlChangesPosIdCommand(mpd_Connection *connection,
1374 int len = strlen("plchangesposid") + 2 + LONGLONGLEN + 3;
1375 char *string = malloc(len);
1377 snprintf(string, len, "plchangesposid \"%lld\"\n", playlist);
1378 mpd_sendInfoCommand(connection, string);
1382 void mpd_sendListallCommand(mpd_Connection *connection, const char *dir)
1384 char *sDir = mpd_sanitizeArg(dir);
1385 int len = strlen("listall") + 2 + strlen(sDir) + 3;
1386 char *string = malloc(len);
1388 snprintf(string, len, "listall \"%s\"\n", sDir);
1389 mpd_sendInfoCommand(connection, string);
1394 void mpd_sendListallInfoCommand(mpd_Connection *connection, const char *dir)
1396 char *sDir = mpd_sanitizeArg(dir);
1397 int len = strlen("listallinfo") + 2 + strlen(sDir) + 3;
1398 char *string = malloc(len);
1400 snprintf(string, len, "listallinfo \"%s\"\n", sDir);
1401 mpd_sendInfoCommand(connection, string);
1406 void mpd_sendLsInfoCommand(mpd_Connection *connection, const char *dir)
1408 char *sDir = mpd_sanitizeArg(dir);
1409 int len = strlen("lsinfo") + 2 + strlen(sDir) + 3;
1410 char *string = malloc(len);
1412 snprintf(string, len, "lsinfo \"%s\"\n", sDir);
1413 mpd_sendInfoCommand(connection, string);
1418 void mpd_sendCurrentSongCommand(mpd_Connection *connection)
1420 mpd_executeCommand(connection, "currentsong\n");
1423 void mpd_sendSearchCommand(mpd_Connection *connection, int table,
1426 mpd_startSearch(connection, 0);
1427 mpd_addConstraintSearch(connection, table, str);
1428 mpd_commitSearch(connection);
1431 void mpd_sendFindCommand(mpd_Connection *connection, int table,
1434 mpd_startSearch(connection, 1);
1435 mpd_addConstraintSearch(connection, table, str);
1436 mpd_commitSearch(connection);
1439 void mpd_sendListCommand(mpd_Connection *connection, int table,
1446 if (table == MPD_TABLE_ARTIST) {
1447 strcpy(st, "artist");
1448 } else if (table == MPD_TABLE_ALBUM) {
1449 strcpy(st, "album");
1451 connection->error = 1;
1452 strcpy(connection->errorStr, "unknown table for list");
1456 char *sanitArg1 = mpd_sanitizeArg(arg1);
1458 len = strlen("list") + 1 + strlen(sanitArg1) + 2 + strlen(st) + 3;
1459 string = malloc(len);
1460 snprintf(string, len, "list %s \"%s\"\n", st, sanitArg1);
1463 len = strlen("list") + 1 + strlen(st) + 2;
1464 string = malloc(len);
1465 snprintf(string, len, "list %s\n", st);
1467 mpd_sendInfoCommand(connection, string);
1471 void mpd_sendAddCommand(mpd_Connection *connection, const char *file)
1473 char *sFile = mpd_sanitizeArg(file);
1474 int len = strlen("add") + 2 + strlen(sFile) + 3;
1475 char *string = malloc(len);
1477 snprintf(string, len, "add \"%s\"\n", sFile);
1478 mpd_executeCommand(connection, string);
1483 int mpd_sendAddIdCommand(mpd_Connection *connection, const char *file)
1486 char *sFile = mpd_sanitizeArg(file);
1487 int len = strlen("addid") + 2 + strlen(sFile) + 3;
1488 char *string = malloc(len);
1490 snprintf(string, len, "addid \"%s\"\n", sFile);
1491 mpd_sendInfoCommand(connection, string);
1495 string = mpd_getNextReturnElementNamed(connection, "Id");
1497 retval = atoi(string);
1504 void mpd_sendDeleteCommand(mpd_Connection *connection, int songPos)
1506 int len = strlen("delete") + 2 + INTLEN + 3;
1507 char *string = malloc(len);
1509 snprintf(string, len, "delete \"%i\"\n", songPos);
1510 mpd_sendInfoCommand(connection, string);
1514 void mpd_sendDeleteIdCommand(mpd_Connection *connection, int id)
1516 int len = strlen("deleteid") + 2 + INTLEN + 3;
1517 char *string = malloc(len);
1519 snprintf(string, len, "deleteid \"%i\"\n", id);
1520 mpd_sendInfoCommand(connection, string);
1524 void mpd_sendSaveCommand(mpd_Connection *connection, const char *name)
1526 char *sName = mpd_sanitizeArg(name);
1527 int len = strlen("save") + 2 + strlen(sName) + 3;
1528 char *string = malloc(len);
1530 snprintf(string, len, "save \"%s\"\n", sName);
1531 mpd_executeCommand(connection, string);
1536 void mpd_sendLoadCommand(mpd_Connection *connection, const char *name)
1538 char *sName = mpd_sanitizeArg(name);
1539 int len = strlen("load") + 2 + strlen(sName) + 3;
1540 char *string = malloc(len);
1542 snprintf(string, len, "load \"%s\"\n", sName);
1543 mpd_executeCommand(connection, string);
1548 void mpd_sendRmCommand(mpd_Connection *connection, const char *name)
1550 char *sName = mpd_sanitizeArg(name);
1551 int len = strlen("rm") + 2 + strlen(sName) + 3;
1552 char *string = malloc(len);
1554 snprintf(string, len, "rm \"%s\"\n", sName);
1555 mpd_executeCommand(connection, string);
1560 void mpd_sendRenameCommand(mpd_Connection *connection, const char *from,
1563 char *sFrom = mpd_sanitizeArg(from);
1564 char *sTo = mpd_sanitizeArg(to);
1565 int len = strlen("rename") + 2 + strlen(sFrom) + 3 + strlen(sTo) + 3;
1566 char *string = malloc(len);
1568 snprintf(string, len, "rename \"%s\" \"%s\"\n", sFrom, sTo);
1569 mpd_executeCommand(connection, string);
1575 void mpd_sendShuffleCommand(mpd_Connection *connection)
1577 mpd_executeCommand(connection, "shuffle\n");
1580 void mpd_sendClearCommand(mpd_Connection *connection)
1582 mpd_executeCommand(connection, "clear\n");
1585 void mpd_sendPlayCommand(mpd_Connection *connection, int songPos)
1587 int len = strlen("play") + 2 + INTLEN + 3;
1588 char *string = malloc(len);
1590 snprintf(string, len, "play \"%i\"\n", songPos);
1591 mpd_sendInfoCommand(connection, string);
1595 void mpd_sendPlayIdCommand(mpd_Connection *connection, int id)
1597 int len = strlen("playid") + 2 + INTLEN + 3;
1598 char *string = malloc(len);
1600 snprintf(string, len, "playid \"%i\"\n", id);
1601 mpd_sendInfoCommand(connection, string);
1605 void mpd_sendStopCommand(mpd_Connection *connection)
1607 mpd_executeCommand(connection, "stop\n");
1610 void mpd_sendPauseCommand(mpd_Connection *connection, int pauseMode)
1612 int len = strlen("pause") + 2 + INTLEN + 3;
1613 char *string = malloc(len);
1615 snprintf(string, len, "pause \"%i\"\n", pauseMode);
1616 mpd_executeCommand(connection, string);
1620 void mpd_sendNextCommand(mpd_Connection *connection)
1622 mpd_executeCommand(connection, "next\n");
1625 void mpd_sendMoveCommand(mpd_Connection *connection, int from, int to)
1627 int len = strlen("move") + 2 + INTLEN + 3 + INTLEN + 3;
1628 char *string = malloc(len);
1630 snprintf(string, len, "move \"%i\" \"%i\"\n", from, to);
1631 mpd_sendInfoCommand(connection, string);
1635 void mpd_sendMoveIdCommand(mpd_Connection *connection, int id, int to)
1637 int len = strlen("moveid") + 2 + INTLEN + 3 + INTLEN + 3;
1638 char *string = malloc(len);
1640 snprintf(string, len, "moveid \"%i\" \"%i\"\n", id, to);
1641 mpd_sendInfoCommand(connection, string);
1645 void mpd_sendSwapCommand(mpd_Connection *connection, int song1, int song2)
1647 int len = strlen("swap") + 2 + INTLEN + 3 + INTLEN + 3;
1648 char *string = malloc(len);
1650 snprintf(string, len, "swap \"%i\" \"%i\"\n", song1, song2);
1651 mpd_sendInfoCommand(connection, string);
1655 void mpd_sendSwapIdCommand(mpd_Connection *connection, int id1, int id2)
1657 int len = strlen("swapid") + 2 + INTLEN + 3 + INTLEN + 3;
1658 char *string = malloc(len);
1660 snprintf(string, len, "swapid \"%i\" \"%i\"\n", id1, id2);
1661 mpd_sendInfoCommand(connection, string);
1665 void mpd_sendSeekCommand(mpd_Connection *connection, int song, int seek_time)
1667 int len = strlen("seek") + 2 + INTLEN + 3 + INTLEN + 3;
1668 char *string = malloc(len);
1670 snprintf(string, len, "seek \"%i\" \"%i\"\n", song, seek_time);
1671 mpd_sendInfoCommand(connection, string);
1675 void mpd_sendSeekIdCommand(mpd_Connection *connection, int id, int seek_time)
1677 int len = strlen("seekid") + 2 + INTLEN + 3 + INTLEN + 3;
1678 char *string = malloc(len);
1680 snprintf(string, len, "seekid \"%i\" \"%i\"\n", id, seek_time);
1681 mpd_sendInfoCommand(connection, string);
1685 void mpd_sendUpdateCommand(mpd_Connection *connection, char *path)
1687 char *sPath = mpd_sanitizeArg(path);
1688 int len = strlen("update") + 2 + strlen(sPath) + 3;
1689 char *string = malloc(len);
1691 snprintf(string, len, "update \"%s\"\n", sPath);
1692 mpd_sendInfoCommand(connection, string);
1697 int mpd_getUpdateId(mpd_Connection *connection)
1702 jobid = mpd_getNextReturnElementNamed(connection, "updating_db");
1711 void mpd_sendPrevCommand(mpd_Connection *connection)
1713 mpd_executeCommand(connection, "previous\n");
1716 void mpd_sendRepeatCommand(mpd_Connection *connection, int repeatMode)
1718 int len = strlen("repeat") + 2 + INTLEN + 3;
1719 char *string = malloc(len);
1721 snprintf(string, len, "repeat \"%i\"\n", repeatMode);
1722 mpd_executeCommand(connection, string);
1726 void mpd_sendRandomCommand(mpd_Connection *connection, int randomMode)
1728 int len = strlen("random") + 2 + INTLEN + 3;
1729 char *string = malloc(len);
1731 snprintf(string, len, "random \"%i\"\n", randomMode);
1732 mpd_executeCommand(connection, string);
1736 void mpd_sendSetvolCommand(mpd_Connection *connection, int volumeChange)
1738 int len = strlen("setvol") + 2 + INTLEN + 3;
1739 char *string = malloc(len);
1741 snprintf(string, len, "setvol \"%i\"\n", volumeChange);
1742 mpd_executeCommand(connection, string);
1746 void mpd_sendVolumeCommand(mpd_Connection *connection, int volumeChange)
1748 int len = strlen("volume") + 2 + INTLEN + 3;
1749 char *string = malloc(len);
1751 snprintf(string, len, "volume \"%i\"\n", volumeChange);
1752 mpd_executeCommand(connection, string);
1756 void mpd_sendCrossfadeCommand(mpd_Connection *connection, int seconds)
1758 int len = strlen("crossfade") + 2 + INTLEN + 3;
1759 char *string = malloc(len);
1761 snprintf(string, len, "crossfade \"%i\"\n", seconds);
1762 mpd_executeCommand(connection, string);
1766 void mpd_sendPasswordCommand(mpd_Connection *connection, const char *pass)
1768 char *sPass = mpd_sanitizeArg(pass);
1769 int len = strlen("password") + 2 + strlen(sPass) + 3;
1770 char *string = malloc(len);
1772 snprintf(string, len, "password \"%s\"\n", sPass);
1773 mpd_executeCommand(connection, string);
1778 void mpd_sendCommandListBegin(mpd_Connection *connection)
1780 if (connection->commandList) {
1781 strcpy(connection->errorStr, "already in command list mode");
1782 connection->error = 1;
1785 connection->commandList = COMMAND_LIST;
1786 mpd_executeCommand(connection, "command_list_begin\n");
1789 void mpd_sendCommandListOkBegin(mpd_Connection *connection)
1791 if (connection->commandList) {
1792 strcpy(connection->errorStr, "already in command list mode");
1793 connection->error = 1;
1796 connection->commandList = COMMAND_LIST_OK;
1797 mpd_executeCommand(connection, "command_list_ok_begin\n");
1798 connection->listOks = 0;
1801 void mpd_sendCommandListEnd(mpd_Connection *connection)
1803 if (!connection->commandList) {
1804 strcpy(connection->errorStr, "not in command list mode");
1805 connection->error = 1;
1808 connection->commandList = 0;
1809 mpd_executeCommand(connection, "command_list_end\n");
1812 void mpd_sendOutputsCommand(mpd_Connection *connection)
1814 mpd_executeCommand(connection, "outputs\n");
1817 mpd_OutputEntity *mpd_getNextOutput(mpd_Connection *connection)
1819 mpd_OutputEntity *output = NULL;
1821 if (connection->doneProcessing
1822 || (connection->listOks && connection->doneListOk)) {
1826 if (connection->error) {
1830 output = malloc(sizeof(mpd_OutputEntity));
1832 output->name = NULL;
1833 output->enabled = 0;
1835 if (!connection->returnElement) {
1836 mpd_getNextReturnElement(connection);
1839 while (connection->returnElement) {
1840 mpd_ReturnElement *re = connection->returnElement;
1842 if (strcmp(re->name, "outputid") == 0) {
1843 if (output != NULL && output->id >= 0) {
1846 output->id = atoi(re->value);
1847 } else if (strcmp(re->name, "outputname") == 0) {
1848 output->name = strndup(re->value, text_buffer_size);
1849 } else if (strcmp(re->name, "outputenabled") == 0) {
1850 output->enabled = atoi(re->value);
1853 mpd_getNextReturnElement(connection);
1854 if (connection->error) {
1863 void mpd_sendEnableOutputCommand(mpd_Connection *connection, int outputId)
1865 int len = strlen("enableoutput") + 2 + INTLEN + 3;
1866 char *string = malloc(len);
1868 snprintf(string, len, "enableoutput \"%i\"\n", outputId);
1869 mpd_executeCommand(connection, string);
1873 void mpd_sendDisableOutputCommand(mpd_Connection *connection, int outputId)
1875 int len = strlen("disableoutput") + 2 + INTLEN + 3;
1876 char *string = malloc(len);
1878 snprintf(string, len, "disableoutput \"%i\"\n", outputId);
1879 mpd_executeCommand(connection, string);
1883 void mpd_freeOutputElement(mpd_OutputEntity *output)
1889 /** odd naming, but it gets the not allowed commands */
1890 void mpd_sendNotCommandsCommand(mpd_Connection *connection)
1892 mpd_executeCommand(connection, "notcommands\n");
1895 /** odd naming, but it gets the allowed commands */
1896 void mpd_sendCommandsCommand(mpd_Connection *connection)
1898 mpd_executeCommand(connection, "commands\n");
1901 /** Get the next returned command */
1902 char *mpd_getNextCommand(mpd_Connection *connection)
1904 return mpd_getNextReturnElementNamed(connection, "command");
1907 void mpd_sendUrlHandlersCommand(mpd_Connection *connection)
1909 mpd_executeCommand(connection, "urlhandlers\n");
1912 char *mpd_getNextHandler(mpd_Connection *connection)
1914 return mpd_getNextReturnElementNamed(connection, "handler");
1917 void mpd_sendTagTypesCommand(mpd_Connection *connection)
1919 mpd_executeCommand(connection, "tagtypes\n");
1922 char *mpd_getNextTagType(mpd_Connection *connection)
1924 return mpd_getNextReturnElementNamed(connection, "tagtype");
1927 void mpd_startSearch(mpd_Connection *connection, int exact)
1929 if (connection->request) {
1930 strcpy(connection->errorStr, "search already in progress");
1931 connection->error = 1;
1936 connection->request = strndup("find", text_buffer_size);
1938 connection->request = strndup("search", text_buffer_size);
1942 void mpd_startStatsSearch(mpd_Connection *connection)
1944 if (connection->request) {
1945 strcpy(connection->errorStr, "search already in progress");
1946 connection->error = 1;
1950 connection->request = strndup("count", text_buffer_size);
1953 void mpd_startPlaylistSearch(mpd_Connection *connection, int exact)
1955 if (connection->request) {
1956 strcpy(connection->errorStr, "search already in progress");
1957 connection->error = 1;
1962 connection->request = strndup("playlistfind", text_buffer_size);
1964 connection->request = strndup("playlistsearch", text_buffer_size);
1968 void mpd_startFieldSearch(mpd_Connection *connection, int type)
1970 const char *strtype;
1973 if (connection->request) {
1974 strcpy(connection->errorStr, "search already in progress");
1975 connection->error = 1;
1979 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
1980 strcpy(connection->errorStr, "invalid type specified");
1981 connection->error = 1;
1985 strtype = mpdTagItemKeys[type];
1987 len = 5 + strlen(strtype) + 1;
1988 connection->request = malloc(len);
1990 snprintf(connection->request, len, "list %c%s", tolower(strtype[0]),
1994 void mpd_addConstraintSearch(mpd_Connection *connection, int type,
1997 const char *strtype;
2002 if (!connection->request) {
2003 strcpy(connection->errorStr, "no search in progress");
2004 connection->error = 1;
2008 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
2009 strcpy(connection->errorStr, "invalid type specified");
2010 connection->error = 1;
2015 strcpy(connection->errorStr, "no name specified");
2016 connection->error = 1;
2020 string = strndup(connection->request, text_buffer_size);
2021 strtype = mpdTagItemKeys[type];
2022 arg = mpd_sanitizeArg(name);
2024 len = strlen(string) + 1 + strlen(strtype) + 2 + strlen(arg) + 2;
2025 connection->request = realloc(connection->request, len);
2026 snprintf(connection->request, len, "%s %c%s \"%s\"", string,
2027 tolower(strtype[0]), strtype + 1, arg);
2033 void mpd_commitSearch(mpd_Connection *connection)
2037 if (!connection->request) {
2038 strcpy(connection->errorStr, "no search in progress");
2039 connection->error = 1;
2043 len = strlen(connection->request) + 2;
2044 connection->request = realloc(connection->request, len);
2045 connection->request[len - 2] = '\n';
2046 connection->request[len - 1] = '\0';
2047 mpd_sendInfoCommand(connection, connection->request);
2049 free(connection->request);
2050 connection->request = NULL;
2054 * @param connection a MpdConnection
2055 * @param path the path to the playlist.
2057 * List the content, with full metadata, of a stored playlist. */
2058 void mpd_sendListPlaylistInfoCommand(mpd_Connection *connection, char *path)
2060 char *arg = mpd_sanitizeArg(path);
2061 int len = strlen("listplaylistinfo") + 2 + strlen(arg) + 3;
2062 char *query = malloc(len);
2064 snprintf(query, len, "listplaylistinfo \"%s\"\n", arg);
2065 mpd_sendInfoCommand(connection, query);
2071 * @param connection a MpdConnection
2072 * @param path the path to the playlist.
2074 * List the content of a stored playlist. */
2075 void mpd_sendListPlaylistCommand(mpd_Connection *connection, char *path)
2077 char *arg = mpd_sanitizeArg(path);
2078 int len = strlen("listplaylist") + 2 + strlen(arg) + 3;
2079 char *query = malloc(len);
2081 snprintf(query, len, "listplaylist \"%s\"\n", arg);
2082 mpd_sendInfoCommand(connection, query);
2087 void mpd_sendPlaylistClearCommand(mpd_Connection *connection, char *path)
2089 char *sPath = mpd_sanitizeArg(path);
2090 int len = strlen("playlistclear") + 2 + strlen(sPath) + 3;
2091 char *string = malloc(len);
2093 snprintf(string, len, "playlistclear \"%s\"\n", sPath);
2094 mpd_executeCommand(connection, string);
2099 void mpd_sendPlaylistAddCommand(mpd_Connection *connection, char *playlist,
2102 char *sPlaylist = mpd_sanitizeArg(playlist);
2103 char *sPath = mpd_sanitizeArg(path);
2104 int len = strlen("playlistadd") + 2 + strlen(sPlaylist) + 3 +
2106 char *string = malloc(len);
2108 snprintf(string, len, "playlistadd \"%s\" \"%s\"\n", sPlaylist, sPath);
2109 mpd_executeCommand(connection, string);
2115 void mpd_sendPlaylistMoveCommand(mpd_Connection *connection, char *playlist,
2118 char *sPlaylist = mpd_sanitizeArg(playlist);
2119 int len = strlen("playlistmove") + 2 + strlen(sPlaylist) + 3 + INTLEN +
2121 char *string = malloc(len);
2123 snprintf(string, len, "playlistmove \"%s\" \"%i\" \"%i\"\n", sPlaylist,
2125 mpd_executeCommand(connection, string);
2130 void mpd_sendPlaylistDeleteCommand(mpd_Connection *connection, char *playlist,
2133 char *sPlaylist = mpd_sanitizeArg(playlist);
2134 int len = strlen("playlistdelete") + 2 + strlen(sPlaylist) + 3 +
2136 char *string = malloc(len);
2138 snprintf(string, len, "playlistdelete \"%s\" \"%i\"\n", sPlaylist, pos);
2139 mpd_executeCommand(connection, string);