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;
130 hints.ai_flags = AI_ADDRCONFIG;
131 hints.ai_family = PF_UNSPEC;
132 hints.ai_socktype = SOCK_STREAM;
133 hints.ai_protocol = IPPROTO_TCP;
134 hints.ai_addrlen = 0;
135 hints.ai_addr = NULL;
136 hints.ai_canonname = NULL;
137 hints.ai_next = NULL;
139 snprintf(service, sizeof(service), "%i", port);
141 error = getaddrinfo(host, service, &hints, &addrinfo);
144 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
145 "host \"%s\" not found: %s", host, gai_strerror(error));
146 connection->error = MPD_ERROR_UNKHOST;
150 for (res = addrinfo; res; res = res->ai_next) {
152 if (connection->sock > -1) {
153 closesocket(connection->sock);
155 connection->sock = socket(res->ai_family, SOCK_STREAM,
157 if (connection->sock < 0) {
158 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
159 "problems creating socket: %s", strerror(errno));
160 connection->error = MPD_ERROR_SYSTEM;
161 freeaddrinfo(addrinfo);
165 mpd_setConnectionTimeout(connection, timeout);
168 if (do_connect_fail(connection, res->ai_addr, res->ai_addrlen)) {
169 /* try the next address family */
170 closesocket(connection->sock);
171 connection->sock = -1;
176 freeaddrinfo(addrinfo);
178 if (connection->sock < 0) {
179 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
180 "problems connecting to \"%s\" on port %i: %s", host, port,
182 connection->error = MPD_ERROR_CONNPORT;
189 #else /* !MPD_HAVE_GAI */
190 static int mpd_connect(mpd_Connection *connection, const char *host, int port,
193 struct hostent he, *he_res = 0;
196 struct sockaddr *dest;
198 struct sockaddr_in sin;
200 #ifdef HAVE_GETHOSTBYNAME_R
201 if (gethostbyname_r(rhost, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
202 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
203 "%s ('%s')", hstrerror(h_errno), host);
204 connection->error = MPD_ERROR_UNKHOST;
207 #else /* HAVE_GETHOSTBYNAME_R */
208 if (!(he_res = gethostbyname(host))) {
209 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
210 "host \"%s\" not found", host);
211 connection->error = MPD_ERROR_UNKHOST;
214 #endif /* HAVE_GETHOSTBYNAME_R */
216 memset(&sin, 0, sizeof(struct sockaddr_in));
217 /* dest.sin_family = he_res->h_addrtype; */
218 sin.sin_family = AF_INET;
219 sin.sin_port = htons(port);
221 switch (he_res->h_addrtype) {
223 memcpy((char *) &sin.sin_addr.s_addr, (char *) he_res->h_addr,
225 dest = (struct sockaddr *) &sin;
226 destlen = sizeof(struct sockaddr_in);
229 strcpy(connection->errorStr, "address type is not IPv4");
230 connection->error = MPD_ERROR_SYSTEM;
235 if (connection->sock > -1) {
236 closesocket(connection->sock);
238 if ((connection->sock = socket(dest->sa_family, SOCK_STREAM, 0)) < 0) {
239 strcpy(connection->errorStr, "problems creating socket");
240 connection->error = MPD_ERROR_SYSTEM;
244 mpd_setConnectionTimeout(connection, timeout);
247 if (do_connect_fail(connection, dest, destlen)) {
248 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
249 "problems connecting to \"%s\" on port %i", host, port);
250 connection->error = MPD_ERROR_CONNPORT;
256 #endif /* !MPD_HAVE_GAI */
258 const char *mpdTagItemKeys[MPD_TAG_NUM_OF_ITEM_TYPES] = {
274 static char *mpd_sanitizeArg(const char *arg)
278 register const char *c;
281 /* instead of counting in that loop above,
282 * just use a bit more memory and halve running time */
283 ret = malloc(strlen(arg) * 2 + 1);
287 for (i = strlen(arg) + 1; i != 0; --i) {
288 if (*c == '"' || *c == '\\') {
297 static mpd_ReturnElement *mpd_newReturnElement(const char *name,
300 mpd_ReturnElement *ret = malloc(sizeof(mpd_ReturnElement));
302 ret->name = strndup(name, text_buffer_size);
303 ret->value = strndup(value, text_buffer_size);
308 static void mpd_freeReturnElement(mpd_ReturnElement *re)
315 void mpd_setConnectionTimeout(mpd_Connection *connection, float timeout)
317 connection->timeout.tv_sec = (int) timeout;
318 connection->timeout.tv_usec =
319 (int) ((timeout - connection->timeout.tv_sec) * 1e6 + 0.5);
322 static int mpd_parseWelcome(mpd_Connection *connection, const char *host,
323 int port, /* char *rt, */ char *output)
329 if (strncmp(output, MPD_WELCOME_MESSAGE, strlen(MPD_WELCOME_MESSAGE))) {
330 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
331 "mpd not running on port %i on host \"%s\"", port, host);
332 connection->error = MPD_ERROR_NOTMPD;
336 tmp = &output[strlen(MPD_WELCOME_MESSAGE)];
338 for (i = 0; i < 3; i++) {
340 connection->version[i] = strtol(tmp, &test, 10);
343 if (!tmp || (test[0] != '.' && test[0] != '\0')) {
344 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
345 "error parsing version number at \"%s\"",
346 &output[strlen(MPD_WELCOME_MESSAGE)]);
347 connection->error = MPD_ERROR_NOTMPD;
356 mpd_Connection *mpd_newConnection(const char *host, int port, float timeout)
361 mpd_Connection *connection = malloc(sizeof(mpd_Connection));
365 strcpy(connection->buffer, "");
366 connection->buflen = 0;
367 connection->bufstart = 0;
368 strcpy(connection->errorStr, "");
369 connection->error = 0;
370 connection->doneProcessing = 0;
371 connection->commandList = 0;
372 connection->listOks = 0;
373 connection->doneListOk = 0;
374 connection->sock = -1;
375 connection->returnElement = NULL;
376 connection->request = NULL;
378 if (winsock_dll_error(connection)) {
382 if (mpd_connect(connection, host, port, timeout) < 0) {
386 while (!(rt = strstr(connection->buffer, "\n"))) {
387 tv.tv_sec = connection->timeout.tv_sec;
388 tv.tv_usec = connection->timeout.tv_usec;
390 FD_SET(connection->sock, &fds);
391 if ((err = select(connection->sock + 1, &fds, NULL, NULL, &tv)) == 1) {
394 readed = recv(connection->sock,
395 &(connection->buffer[connection->buflen]),
396 MPD_BUFFER_MAX_LENGTH - connection->buflen, 0);
398 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
399 "problems getting a response from \"%s\" on port %i : %s",
400 host, port, strerror(errno));
401 connection->error = MPD_ERROR_NORESPONSE;
404 connection->buflen += readed;
405 connection->buffer[connection->buflen] = '\0';
406 } else if (err < 0) {
407 if (SELECT_ERRNO_IGNORE) {
410 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
411 "problems connecting to \"%s\" on port %i", host, port);
412 connection->error = MPD_ERROR_CONNPORT;
415 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
416 "timeout in attempting to get a response from \"%s\" on "
417 "port %i", host, port);
418 connection->error = MPD_ERROR_NORESPONSE;
424 output = strndup(connection->buffer, text_buffer_size);
425 strcpy(connection->buffer, rt + 1);
426 connection->buflen = strlen(connection->buffer);
428 if (mpd_parseWelcome(connection, host, port, /* rt, */ output) == 0) {
429 connection->doneProcessing = 1;
437 void mpd_clearError(mpd_Connection *connection)
439 connection->error = 0;
440 connection->errorStr[0] = '\0';
443 void mpd_closeConnection(mpd_Connection *connection)
445 closesocket(connection->sock);
446 if (connection->returnElement) {
447 free(connection->returnElement);
449 if (connection->request) {
450 free(connection->request);
456 static void mpd_executeCommand(mpd_Connection *connection, const char *command)
461 const char *commandPtr = command;
462 int commandLen = strlen(command);
464 if (!connection->doneProcessing && !connection->commandList) {
465 strcpy(connection->errorStr, "not done processing current command");
466 connection->error = 1;
470 mpd_clearError(connection);
473 FD_SET(connection->sock, &fds);
474 tv.tv_sec = connection->timeout.tv_sec;
475 tv.tv_usec = connection->timeout.tv_usec;
477 while ((ret = select(connection->sock + 1, NULL, &fds, NULL, &tv) == 1)
478 || (ret == -1 && SELECT_ERRNO_IGNORE)) {
479 ret = send(connection->sock, commandPtr, commandLen, MSG_DONTWAIT);
481 if (SENDRECV_ERRNO_IGNORE) {
484 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
485 "problems giving command \"%s\"", command);
486 connection->error = MPD_ERROR_SENDING;
493 if (commandLen <= 0) {
498 if (commandLen > 0) {
500 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
501 "timeout sending command \"%s\"", command);
502 connection->error = MPD_ERROR_TIMEOUT;
506 if (!connection->commandList) {
507 connection->doneProcessing = 0;
508 } else if (connection->commandList == COMMAND_LIST_OK) {
509 connection->listOks++;
513 static void mpd_getNextReturnElement(mpd_Connection *connection)
523 char *bufferCheck = NULL;
527 if (connection->returnElement) {
528 mpd_freeReturnElement(connection->returnElement);
530 connection->returnElement = NULL;
532 if (connection->doneProcessing
533 || (connection->listOks && connection->doneListOk)) {
534 strcpy(connection->errorStr,
535 "already done processing current command");
536 connection->error = 1;
540 bufferCheck = connection->buffer + connection->bufstart;
541 while (connection->bufstart >= connection->buflen
542 || !(rt = strchr(bufferCheck, '\n'))) {
543 if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
544 memmove(connection->buffer,
545 connection->buffer + connection->bufstart,
546 connection->buflen - connection->bufstart + 1);
547 connection->buflen -= connection->bufstart;
548 connection->bufstart = 0;
550 if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
551 strcpy(connection->errorStr, "buffer overrun");
552 connection->error = MPD_ERROR_BUFFEROVERRUN;
553 connection->doneProcessing = 1;
554 connection->doneListOk = 0;
557 bufferCheck = connection->buffer + connection->buflen;
558 tv.tv_sec = connection->timeout.tv_sec;
559 tv.tv_usec = connection->timeout.tv_usec;
561 FD_SET(connection->sock, &fds);
562 if ((err = select(connection->sock + 1, &fds, NULL, NULL, &tv) == 1)) {
563 readed = recv(connection->sock,
564 connection->buffer + connection->buflen,
565 MPD_BUFFER_MAX_LENGTH - connection->buflen, MSG_DONTWAIT);
566 if (readed < 0 && SENDRECV_ERRNO_IGNORE) {
570 strcpy(connection->errorStr, "connection closed");
571 connection->error = MPD_ERROR_CONNCLOSED;
572 connection->doneProcessing = 1;
573 connection->doneListOk = 0;
576 connection->buflen += readed;
577 connection->buffer[connection->buflen] = '\0';
578 } else if (err < 0 && SELECT_ERRNO_IGNORE) {
581 strcpy(connection->errorStr, "connection timeout");
582 connection->error = MPD_ERROR_TIMEOUT;
583 connection->doneProcessing = 1;
584 connection->doneListOk = 0;
590 output = connection->buffer + connection->bufstart;
591 connection->bufstart = rt - connection->buffer + 1;
593 if (strcmp(output, "OK") == 0) {
594 if (connection->listOks > 0) {
595 strcpy(connection->errorStr, "expected more list_OK's");
596 connection->error = 1;
598 connection->listOks = 0;
599 connection->doneProcessing = 1;
600 connection->doneListOk = 0;
604 if (strcmp(output, "list_OK") == 0) {
605 if (!connection->listOks) {
606 strcpy(connection->errorStr, "got an unexpected list_OK");
607 connection->error = 1;
609 connection->doneListOk = 1;
610 connection->listOks--;
615 if (strncmp(output, "ACK", strlen("ACK")) == 0) {
620 strcpy(connection->errorStr, output);
621 connection->error = MPD_ERROR_ACK;
622 connection->errorCode = MPD_ACK_ERROR_UNK;
623 connection->errorAt = MPD_ERROR_AT_UNK;
624 connection->doneProcessing = 1;
625 connection->doneListOk = 0;
627 needle = strchr(output, '[');
631 val = strtol(needle + 1, &test, 10);
635 connection->errorCode = val;
636 val = strtol(test + 1, &test, 10);
640 connection->errorAt = val;
644 tok = strchr(output, ':');
653 if (value[0] == ' ') {
654 connection->returnElement = mpd_newReturnElement(name, &(value[1]));
656 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
657 "error parsing: %s:%s", name, value);
658 connection->error = 1;
662 void mpd_finishCommand(mpd_Connection *connection)
664 while (!connection->doneProcessing) {
665 if (connection->doneListOk) {
666 connection->doneListOk = 0;
668 mpd_getNextReturnElement(connection);
672 static void mpd_finishListOkCommand(mpd_Connection *connection)
674 while (!connection->doneProcessing && connection->listOks
675 && !connection->doneListOk) {
676 mpd_getNextReturnElement(connection);
680 int mpd_nextListOkCommand(mpd_Connection *connection)
682 mpd_finishListOkCommand(connection);
683 if (!connection->doneProcessing) {
684 connection->doneListOk = 0;
686 if (connection->listOks == 0 || connection->doneProcessing) {
692 void mpd_sendStatusCommand(mpd_Connection *connection)
694 mpd_executeCommand(connection, "status\n");
697 mpd_Status *mpd_getStatus(mpd_Connection *connection)
701 /* mpd_executeCommand(connection, "status\n");
702 if (connection->error) {
706 if (connection->doneProcessing
707 || (connection->listOks && connection->doneListOk)) {
711 if (!connection->returnElement) {
712 mpd_getNextReturnElement(connection);
715 status = malloc(sizeof(mpd_Status));
719 status->playlist = -1;
720 status->playlistLength = -1;
724 status->elapsedTime = 0;
725 status->totalTime = 0;
727 status->sampleRate = 0;
729 status->channels = 0;
730 status->crossfade = -1;
731 status->error = NULL;
732 status->updatingDb = 0;
734 if (connection->error) {
738 while (connection->returnElement) {
739 mpd_ReturnElement *re = connection->returnElement;
741 if (strcmp(re->name, "volume") == 0) {
742 status->volume = atoi(re->value);
743 } else if (strcmp(re->name, "repeat") == 0) {
744 status->repeat = atoi(re->value);
745 } else if (strcmp(re->name, "random") == 0) {
746 status->random = atoi(re->value);
747 } else if (strcmp(re->name, "playlist") == 0) {
748 status->playlist = strtol(re->value, NULL, 10);
749 } else if (strcmp(re->name, "playlistlength") == 0) {
750 status->playlistLength = atoi(re->value);
751 } else if (strcmp(re->name, "bitrate") == 0) {
752 status->bitRate = atoi(re->value);
753 } else if (strcmp(re->name, "state") == 0) {
754 if (strcmp(re->value, "play") == 0) {
755 status->state = MPD_STATUS_STATE_PLAY;
756 } else if (strcmp(re->value, "stop") == 0) {
757 status->state = MPD_STATUS_STATE_STOP;
758 } else if (strcmp(re->value, "pause") == 0) {
759 status->state = MPD_STATUS_STATE_PAUSE;
761 status->state = MPD_STATUS_STATE_UNKNOWN;
763 } else if (strcmp(re->name, "song") == 0) {
764 status->song = atoi(re->value);
765 } else if (strcmp(re->name, "songid") == 0) {
766 status->songid = atoi(re->value);
767 } else if (strcmp(re->name, "time") == 0) {
768 char *tok = strchr(re->value, ':');
770 /* the second strchr below is a safety check */
771 if (tok && (strchr(tok, 0) > (tok + 1))) {
772 /* atoi stops at the first non-[0-9] char: */
773 status->elapsedTime = atoi(re->value);
774 status->totalTime = atoi(tok + 1);
776 } else if (strcmp(re->name, "error") == 0) {
777 status->error = strndup(re->value, text_buffer_size);
778 } else if (strcmp(re->name, "xfade") == 0) {
779 status->crossfade = atoi(re->value);
780 } else if (strcmp(re->name, "updating_db") == 0) {
781 status->updatingDb = atoi(re->value);
782 } else if (strcmp(re->name, "audio") == 0) {
783 char *tok = strchr(re->value, ':');
785 if (tok && (strchr(tok, 0) > (tok + 1))) {
786 status->sampleRate = atoi(re->value);
787 status->bits = atoi(++tok);
788 tok = strchr(tok, ':');
789 if (tok && (strchr(tok, 0) > (tok + 1))) {
790 status->channels = atoi(tok + 1);
795 mpd_getNextReturnElement(connection);
796 if (connection->error) {
802 if (connection->error) {
805 } else if (status->state < 0) {
806 strcpy(connection->errorStr, "state not found");
807 connection->error = 1;
815 void mpd_freeStatus(mpd_Status *status)
823 void mpd_sendStatsCommand(mpd_Connection *connection)
825 mpd_executeCommand(connection, "stats\n");
828 mpd_Stats *mpd_getStats(mpd_Connection *connection)
832 /* mpd_executeCommand(connection, "stats\n");
833 if (connection->error) {
837 if (connection->doneProcessing
838 || (connection->listOks && connection->doneListOk)) {
842 if (!connection->returnElement) {
843 mpd_getNextReturnElement(connection);
846 stats = malloc(sizeof(mpd_Stats));
847 stats->numberOfArtists = 0;
848 stats->numberOfAlbums = 0;
849 stats->numberOfSongs = 0;
851 stats->dbUpdateTime = 0;
853 stats->dbPlayTime = 0;
855 if (connection->error) {
859 while (connection->returnElement) {
860 mpd_ReturnElement *re = connection->returnElement;
862 if (strcmp(re->name, "artists") == 0) {
863 stats->numberOfArtists = atoi(re->value);
864 } else if (strcmp(re->name, "albums") == 0) {
865 stats->numberOfAlbums = atoi(re->value);
866 } else if (strcmp(re->name, "songs") == 0) {
867 stats->numberOfSongs = atoi(re->value);
868 } else if (strcmp(re->name, "uptime") == 0) {
869 stats->uptime = strtol(re->value, NULL, 10);
870 } else if (strcmp(re->name, "db_update") == 0) {
871 stats->dbUpdateTime = strtol(re->value, NULL, 10);
872 } else if (strcmp(re->name, "playtime") == 0) {
873 stats->playTime = strtol(re->value, NULL, 10);
874 } else if (strcmp(re->name, "db_playtime") == 0) {
875 stats->dbPlayTime = strtol(re->value, NULL, 10);
878 mpd_getNextReturnElement(connection);
879 if (connection->error) {
885 if (connection->error) {
893 void mpd_freeStats(mpd_Stats *stats)
898 mpd_SearchStats *mpd_getSearchStats(mpd_Connection *connection)
900 mpd_SearchStats *stats;
901 mpd_ReturnElement *re;
903 if (connection->doneProcessing
904 || (connection->listOks && connection->doneListOk)) {
908 if (!connection->returnElement) {
909 mpd_getNextReturnElement(connection);
912 if (connection->error) {
916 stats = malloc(sizeof(mpd_SearchStats));
917 stats->numberOfSongs = 0;
920 while (connection->returnElement) {
921 re = connection->returnElement;
923 if (strcmp(re->name, "songs") == 0) {
924 stats->numberOfSongs = atoi(re->value);
925 } else if (strcmp(re->name, "playtime") == 0) {
926 stats->playTime = strtol(re->value, NULL, 10);
929 mpd_getNextReturnElement(connection);
930 if (connection->error) {
936 if (connection->error) {
944 void mpd_freeSearchStats(mpd_SearchStats *stats)
949 static void mpd_initSong(mpd_Song *song)
960 song->composer = NULL;
961 song->performer = NULL;
963 song->comment = NULL;
965 song->time = MPD_SONG_NO_TIME;
966 song->pos = MPD_SONG_NO_NUM;
967 song->id = MPD_SONG_NO_ID;
970 static void mpd_finishSong(mpd_Song *song)
996 if (song->composer) {
997 free(song->composer);
1002 if (song->comment) {
1003 free(song->comment);
1007 mpd_Song *mpd_newSong(void)
1009 mpd_Song *ret = malloc(sizeof(mpd_Song));
1016 void mpd_freeSong(mpd_Song *song)
1018 mpd_finishSong(song);
1022 mpd_Song *mpd_songDup(mpd_Song *song)
1024 mpd_Song *ret = mpd_newSong();
1027 ret->file = strndup(song->file, text_buffer_size);
1030 ret->artist = strndup(song->artist, text_buffer_size);
1033 ret->album = strndup(song->album, text_buffer_size);
1036 ret->title = strndup(song->title, text_buffer_size);
1039 ret->track = strndup(song->track, text_buffer_size);
1042 ret->name = strndup(song->name, text_buffer_size);
1045 ret->date = strndup(song->date, text_buffer_size);
1048 ret->genre = strndup(song->genre, text_buffer_size);
1050 if (song->composer) {
1051 ret->composer = strndup(song->composer, text_buffer_size);
1054 ret->disc = strndup(song->disc, text_buffer_size);
1056 if (song->comment) {
1057 ret->comment = strndup(song->comment, text_buffer_size);
1059 ret->time = song->time;
1060 ret->pos = song->pos;
1066 static void mpd_initDirectory(mpd_Directory *directory)
1068 directory->path = NULL;
1071 static void mpd_finishDirectory(mpd_Directory *directory)
1073 if (directory->path) {
1074 free(directory->path);
1078 mpd_Directory *mpd_newDirectory(void)
1080 mpd_Directory *directory = malloc(sizeof(mpd_Directory));
1082 mpd_initDirectory(directory);
1087 void mpd_freeDirectory(mpd_Directory *directory)
1089 mpd_finishDirectory(directory);
1094 mpd_Directory *mpd_directoryDup(mpd_Directory *directory)
1096 mpd_Directory *ret = mpd_newDirectory();
1098 if (directory->path) {
1099 ret->path = strndup(directory->path, text_buffer_size);
1105 static void mpd_initPlaylistFile(mpd_PlaylistFile *playlist)
1107 playlist->path = NULL;
1110 static void mpd_finishPlaylistFile(mpd_PlaylistFile *playlist)
1112 if (playlist->path) {
1113 free(playlist->path);
1117 mpd_PlaylistFile *mpd_newPlaylistFile(void)
1119 mpd_PlaylistFile *playlist = malloc(sizeof(mpd_PlaylistFile));
1121 mpd_initPlaylistFile(playlist);
1126 void mpd_freePlaylistFile(mpd_PlaylistFile *playlist)
1128 mpd_finishPlaylistFile(playlist);
1132 mpd_PlaylistFile *mpd_playlistFileDup(mpd_PlaylistFile *playlist)
1134 mpd_PlaylistFile *ret = mpd_newPlaylistFile();
1136 if (playlist->path) {
1137 ret->path = strndup(playlist->path, text_buffer_size);
1143 static void mpd_initInfoEntity(mpd_InfoEntity *entity)
1145 entity->info.directory = NULL;
1148 static void mpd_finishInfoEntity(mpd_InfoEntity *entity)
1150 if (entity->info.directory) {
1151 if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1152 mpd_freeDirectory(entity->info.directory);
1153 } else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
1154 mpd_freeSong(entity->info.song);
1155 } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1156 mpd_freePlaylistFile(entity->info.playlistFile);
1161 mpd_InfoEntity *mpd_newInfoEntity(void)
1163 mpd_InfoEntity *entity = malloc(sizeof(mpd_InfoEntity));
1165 mpd_initInfoEntity(entity);
1170 void mpd_freeInfoEntity(mpd_InfoEntity *entity)
1172 mpd_finishInfoEntity(entity);
1176 static void mpd_sendInfoCommand(mpd_Connection *connection, char *command)
1178 mpd_executeCommand(connection, command);
1181 mpd_InfoEntity *mpd_getNextInfoEntity(mpd_Connection *connection)
1183 mpd_InfoEntity *entity = NULL;
1185 if (connection->doneProcessing
1186 || (connection->listOks && connection->doneListOk)) {
1190 if (!connection->returnElement) {
1191 mpd_getNextReturnElement(connection);
1194 if (connection->returnElement) {
1195 if (strcmp(connection->returnElement->name, "file") == 0) {
1196 entity = mpd_newInfoEntity();
1197 entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1198 entity->info.song = mpd_newSong();
1199 entity->info.song->file = strndup(connection->returnElement->value, text_buffer_size);
1200 } else if (strcmp(connection->returnElement->name, "directory") == 0) {
1201 entity = mpd_newInfoEntity();
1202 entity->type = MPD_INFO_ENTITY_TYPE_DIRECTORY;
1203 entity->info.directory = mpd_newDirectory();
1204 entity->info.directory->path =
1205 strndup(connection->returnElement->value, text_buffer_size);
1206 } else if (strcmp(connection->returnElement->name, "playlist") == 0) {
1207 entity = mpd_newInfoEntity();
1208 entity->type = MPD_INFO_ENTITY_TYPE_PLAYLISTFILE;
1209 entity->info.playlistFile = mpd_newPlaylistFile();
1210 entity->info.playlistFile->path =
1211 strndup(connection->returnElement->value, text_buffer_size);
1212 } else if (strcmp(connection->returnElement->name, "cpos") == 0) {
1213 entity = mpd_newInfoEntity();
1214 entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1215 entity->info.song = mpd_newSong();
1216 entity->info.song->pos = atoi(connection->returnElement->value);
1218 connection->error = 1;
1219 strcpy(connection->errorStr, "problem parsing song info");
1226 mpd_getNextReturnElement(connection);
1227 while (connection->returnElement) {
1228 mpd_ReturnElement *re = connection->returnElement;
1230 if (strcmp(re->name, "file") == 0) {
1232 } else if (strcmp(re->name, "directory") == 0) {
1234 } else if (strcmp(re->name, "playlist") == 0) {
1236 } else if (strcmp(re->name, "cpos") == 0) {
1240 if (entity->type == MPD_INFO_ENTITY_TYPE_SONG && strlen(re->value)) {
1241 if (!entity->info.song->artist
1242 && strcmp(re->name, "Artist") == 0) {
1243 entity->info.song->artist = strndup(re->value, text_buffer_size);
1244 } else if (!entity->info.song->album
1245 && strcmp(re->name, "Album") == 0) {
1246 entity->info.song->album = strndup(re->value, text_buffer_size);
1247 } else if (!entity->info.song->title
1248 && strcmp(re->name, "Title") == 0) {
1249 entity->info.song->title = strndup(re->value, text_buffer_size);
1250 } else if (!entity->info.song->track
1251 && strcmp(re->name, "Track") == 0) {
1252 entity->info.song->track = strndup(re->value, text_buffer_size);
1253 } else if (!entity->info.song->name
1254 && strcmp(re->name, "Name") == 0) {
1255 entity->info.song->name = strndup(re->value, text_buffer_size);
1256 } else if (entity->info.song->time == MPD_SONG_NO_TIME
1257 && strcmp(re->name, "Time") == 0) {
1258 entity->info.song->time = atoi(re->value);
1259 } else if (entity->info.song->pos == MPD_SONG_NO_NUM
1260 && strcmp(re->name, "Pos") == 0) {
1261 entity->info.song->pos = atoi(re->value);
1262 } else if (entity->info.song->id == MPD_SONG_NO_ID
1263 && strcmp(re->name, "Id") == 0) {
1264 entity->info.song->id = atoi(re->value);
1265 } else if (!entity->info.song->date
1266 && strcmp(re->name, "Date") == 0) {
1267 entity->info.song->date = strndup(re->value, text_buffer_size);
1268 } else if (!entity->info.song->genre
1269 && strcmp(re->name, "Genre") == 0) {
1270 entity->info.song->genre = strndup(re->value, text_buffer_size);
1271 } else if (!entity->info.song->composer
1272 && strcmp(re->name, "Composer") == 0) {
1273 entity->info.song->composer = strndup(re->value, text_buffer_size);
1274 } else if (!entity->info.song->performer
1275 && strcmp(re->name, "Performer") == 0) {
1276 entity->info.song->performer = strndup(re->value, text_buffer_size);
1277 } else if (!entity->info.song->disc
1278 && strcmp(re->name, "Disc") == 0) {
1279 entity->info.song->disc = strndup(re->value, text_buffer_size);
1280 } else if (!entity->info.song->comment
1281 && strcmp(re->name, "Comment") == 0) {
1282 entity->info.song->comment = strndup(re->value, text_buffer_size);
1284 } else if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1285 } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1288 mpd_getNextReturnElement(connection);
1294 static char *mpd_getNextReturnElementNamed(mpd_Connection *connection,
1297 if (connection->doneProcessing
1298 || (connection->listOks && connection->doneListOk)) {
1302 mpd_getNextReturnElement(connection);
1303 while (connection->returnElement) {
1304 mpd_ReturnElement *re = connection->returnElement;
1306 if (strcmp(re->name, name) == 0) {
1307 return strndup(re->value, text_buffer_size);
1309 mpd_getNextReturnElement(connection);
1315 char *mpd_getNextTag(mpd_Connection *connection, int type)
1317 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES
1318 || type == MPD_TAG_ITEM_ANY) {
1321 if (type == MPD_TAG_ITEM_FILENAME) {
1322 return mpd_getNextReturnElementNamed(connection, "file");
1324 return mpd_getNextReturnElementNamed(connection, mpdTagItemKeys[type]);
1327 char *mpd_getNextArtist(mpd_Connection *connection)
1329 return mpd_getNextReturnElementNamed(connection, "Artist");
1332 char *mpd_getNextAlbum(mpd_Connection *connection)
1334 return mpd_getNextReturnElementNamed(connection, "Album");
1337 void mpd_sendPlaylistInfoCommand(mpd_Connection *connection, int songPos)
1339 int len = strlen("playlistinfo") + 2 + INTLEN + 3;
1340 char *string = malloc(len);
1342 snprintf(string, len, "playlistinfo \"%i\"\n", songPos);
1343 mpd_sendInfoCommand(connection, string);
1347 void mpd_sendPlaylistIdCommand(mpd_Connection *connection, int id)
1349 int len = strlen("playlistid") + 2 + INTLEN + 3;
1350 char *string = malloc(len);
1352 snprintf(string, len, "playlistid \"%i\"\n", id);
1353 mpd_sendInfoCommand(connection, string);
1357 void mpd_sendPlChangesCommand(mpd_Connection *connection, long long playlist)
1359 int len = strlen("plchanges") + 2 + LONGLONGLEN + 3;
1360 char *string = malloc(len);
1362 snprintf(string, len, "plchanges \"%lld\"\n", playlist);
1363 mpd_sendInfoCommand(connection, string);
1367 void mpd_sendPlChangesPosIdCommand(mpd_Connection *connection,
1370 int len = strlen("plchangesposid") + 2 + LONGLONGLEN + 3;
1371 char *string = malloc(len);
1373 snprintf(string, len, "plchangesposid \"%lld\"\n", playlist);
1374 mpd_sendInfoCommand(connection, string);
1378 void mpd_sendListallCommand(mpd_Connection *connection, const char *dir)
1380 char *sDir = mpd_sanitizeArg(dir);
1381 int len = strlen("listall") + 2 + strlen(sDir) + 3;
1382 char *string = malloc(len);
1384 snprintf(string, len, "listall \"%s\"\n", sDir);
1385 mpd_sendInfoCommand(connection, string);
1390 void mpd_sendListallInfoCommand(mpd_Connection *connection, const char *dir)
1392 char *sDir = mpd_sanitizeArg(dir);
1393 int len = strlen("listallinfo") + 2 + strlen(sDir) + 3;
1394 char *string = malloc(len);
1396 snprintf(string, len, "listallinfo \"%s\"\n", sDir);
1397 mpd_sendInfoCommand(connection, string);
1402 void mpd_sendLsInfoCommand(mpd_Connection *connection, const char *dir)
1404 char *sDir = mpd_sanitizeArg(dir);
1405 int len = strlen("lsinfo") + 2 + strlen(sDir) + 3;
1406 char *string = malloc(len);
1408 snprintf(string, len, "lsinfo \"%s\"\n", sDir);
1409 mpd_sendInfoCommand(connection, string);
1414 void mpd_sendCurrentSongCommand(mpd_Connection *connection)
1416 mpd_executeCommand(connection, "currentsong\n");
1419 void mpd_sendSearchCommand(mpd_Connection *connection, int table,
1422 mpd_startSearch(connection, 0);
1423 mpd_addConstraintSearch(connection, table, str);
1424 mpd_commitSearch(connection);
1427 void mpd_sendFindCommand(mpd_Connection *connection, int table,
1430 mpd_startSearch(connection, 1);
1431 mpd_addConstraintSearch(connection, table, str);
1432 mpd_commitSearch(connection);
1435 void mpd_sendListCommand(mpd_Connection *connection, int table,
1442 if (table == MPD_TABLE_ARTIST) {
1443 strcpy(st, "artist");
1444 } else if (table == MPD_TABLE_ALBUM) {
1445 strcpy(st, "album");
1447 connection->error = 1;
1448 strcpy(connection->errorStr, "unknown table for list");
1452 char *sanitArg1 = mpd_sanitizeArg(arg1);
1454 len = strlen("list") + 1 + strlen(sanitArg1) + 2 + strlen(st) + 3;
1455 string = malloc(len);
1456 snprintf(string, len, "list %s \"%s\"\n", st, sanitArg1);
1459 len = strlen("list") + 1 + strlen(st) + 2;
1460 string = malloc(len);
1461 snprintf(string, len, "list %s\n", st);
1463 mpd_sendInfoCommand(connection, string);
1467 void mpd_sendAddCommand(mpd_Connection *connection, const char *file)
1469 char *sFile = mpd_sanitizeArg(file);
1470 int len = strlen("add") + 2 + strlen(sFile) + 3;
1471 char *string = malloc(len);
1473 snprintf(string, len, "add \"%s\"\n", sFile);
1474 mpd_executeCommand(connection, string);
1479 int mpd_sendAddIdCommand(mpd_Connection *connection, const char *file)
1482 char *sFile = mpd_sanitizeArg(file);
1483 int len = strlen("addid") + 2 + strlen(sFile) + 3;
1484 char *string = malloc(len);
1486 snprintf(string, len, "addid \"%s\"\n", sFile);
1487 mpd_sendInfoCommand(connection, string);
1491 string = mpd_getNextReturnElementNamed(connection, "Id");
1493 retval = atoi(string);
1500 void mpd_sendDeleteCommand(mpd_Connection *connection, int songPos)
1502 int len = strlen("delete") + 2 + INTLEN + 3;
1503 char *string = malloc(len);
1505 snprintf(string, len, "delete \"%i\"\n", songPos);
1506 mpd_sendInfoCommand(connection, string);
1510 void mpd_sendDeleteIdCommand(mpd_Connection *connection, int id)
1512 int len = strlen("deleteid") + 2 + INTLEN + 3;
1513 char *string = malloc(len);
1515 snprintf(string, len, "deleteid \"%i\"\n", id);
1516 mpd_sendInfoCommand(connection, string);
1520 void mpd_sendSaveCommand(mpd_Connection *connection, const char *name)
1522 char *sName = mpd_sanitizeArg(name);
1523 int len = strlen("save") + 2 + strlen(sName) + 3;
1524 char *string = malloc(len);
1526 snprintf(string, len, "save \"%s\"\n", sName);
1527 mpd_executeCommand(connection, string);
1532 void mpd_sendLoadCommand(mpd_Connection *connection, const char *name)
1534 char *sName = mpd_sanitizeArg(name);
1535 int len = strlen("load") + 2 + strlen(sName) + 3;
1536 char *string = malloc(len);
1538 snprintf(string, len, "load \"%s\"\n", sName);
1539 mpd_executeCommand(connection, string);
1544 void mpd_sendRmCommand(mpd_Connection *connection, const char *name)
1546 char *sName = mpd_sanitizeArg(name);
1547 int len = strlen("rm") + 2 + strlen(sName) + 3;
1548 char *string = malloc(len);
1550 snprintf(string, len, "rm \"%s\"\n", sName);
1551 mpd_executeCommand(connection, string);
1556 void mpd_sendRenameCommand(mpd_Connection *connection, const char *from,
1559 char *sFrom = mpd_sanitizeArg(from);
1560 char *sTo = mpd_sanitizeArg(to);
1561 int len = strlen("rename") + 2 + strlen(sFrom) + 3 + strlen(sTo) + 3;
1562 char *string = malloc(len);
1564 snprintf(string, len, "rename \"%s\" \"%s\"\n", sFrom, sTo);
1565 mpd_executeCommand(connection, string);
1571 void mpd_sendShuffleCommand(mpd_Connection *connection)
1573 mpd_executeCommand(connection, "shuffle\n");
1576 void mpd_sendClearCommand(mpd_Connection *connection)
1578 mpd_executeCommand(connection, "clear\n");
1581 void mpd_sendPlayCommand(mpd_Connection *connection, int songPos)
1583 int len = strlen("play") + 2 + INTLEN + 3;
1584 char *string = malloc(len);
1586 snprintf(string, len, "play \"%i\"\n", songPos);
1587 mpd_sendInfoCommand(connection, string);
1591 void mpd_sendPlayIdCommand(mpd_Connection *connection, int id)
1593 int len = strlen("playid") + 2 + INTLEN + 3;
1594 char *string = malloc(len);
1596 snprintf(string, len, "playid \"%i\"\n", id);
1597 mpd_sendInfoCommand(connection, string);
1601 void mpd_sendStopCommand(mpd_Connection *connection)
1603 mpd_executeCommand(connection, "stop\n");
1606 void mpd_sendPauseCommand(mpd_Connection *connection, int pauseMode)
1608 int len = strlen("pause") + 2 + INTLEN + 3;
1609 char *string = malloc(len);
1611 snprintf(string, len, "pause \"%i\"\n", pauseMode);
1612 mpd_executeCommand(connection, string);
1616 void mpd_sendNextCommand(mpd_Connection *connection)
1618 mpd_executeCommand(connection, "next\n");
1621 void mpd_sendMoveCommand(mpd_Connection *connection, int from, int to)
1623 int len = strlen("move") + 2 + INTLEN + 3 + INTLEN + 3;
1624 char *string = malloc(len);
1626 snprintf(string, len, "move \"%i\" \"%i\"\n", from, to);
1627 mpd_sendInfoCommand(connection, string);
1631 void mpd_sendMoveIdCommand(mpd_Connection *connection, int id, int to)
1633 int len = strlen("moveid") + 2 + INTLEN + 3 + INTLEN + 3;
1634 char *string = malloc(len);
1636 snprintf(string, len, "moveid \"%i\" \"%i\"\n", id, to);
1637 mpd_sendInfoCommand(connection, string);
1641 void mpd_sendSwapCommand(mpd_Connection *connection, int song1, int song2)
1643 int len = strlen("swap") + 2 + INTLEN + 3 + INTLEN + 3;
1644 char *string = malloc(len);
1646 snprintf(string, len, "swap \"%i\" \"%i\"\n", song1, song2);
1647 mpd_sendInfoCommand(connection, string);
1651 void mpd_sendSwapIdCommand(mpd_Connection *connection, int id1, int id2)
1653 int len = strlen("swapid") + 2 + INTLEN + 3 + INTLEN + 3;
1654 char *string = malloc(len);
1656 snprintf(string, len, "swapid \"%i\" \"%i\"\n", id1, id2);
1657 mpd_sendInfoCommand(connection, string);
1661 void mpd_sendSeekCommand(mpd_Connection *connection, int song, int seek_time)
1663 int len = strlen("seek") + 2 + INTLEN + 3 + INTLEN + 3;
1664 char *string = malloc(len);
1666 snprintf(string, len, "seek \"%i\" \"%i\"\n", song, seek_time);
1667 mpd_sendInfoCommand(connection, string);
1671 void mpd_sendSeekIdCommand(mpd_Connection *connection, int id, int seek_time)
1673 int len = strlen("seekid") + 2 + INTLEN + 3 + INTLEN + 3;
1674 char *string = malloc(len);
1676 snprintf(string, len, "seekid \"%i\" \"%i\"\n", id, seek_time);
1677 mpd_sendInfoCommand(connection, string);
1681 void mpd_sendUpdateCommand(mpd_Connection *connection, char *path)
1683 char *sPath = mpd_sanitizeArg(path);
1684 int len = strlen("update") + 2 + strlen(sPath) + 3;
1685 char *string = malloc(len);
1687 snprintf(string, len, "update \"%s\"\n", sPath);
1688 mpd_sendInfoCommand(connection, string);
1693 int mpd_getUpdateId(mpd_Connection *connection)
1698 jobid = mpd_getNextReturnElementNamed(connection, "updating_db");
1707 void mpd_sendPrevCommand(mpd_Connection *connection)
1709 mpd_executeCommand(connection, "previous\n");
1712 void mpd_sendRepeatCommand(mpd_Connection *connection, int repeatMode)
1714 int len = strlen("repeat") + 2 + INTLEN + 3;
1715 char *string = malloc(len);
1717 snprintf(string, len, "repeat \"%i\"\n", repeatMode);
1718 mpd_executeCommand(connection, string);
1722 void mpd_sendRandomCommand(mpd_Connection *connection, int randomMode)
1724 int len = strlen("random") + 2 + INTLEN + 3;
1725 char *string = malloc(len);
1727 snprintf(string, len, "random \"%i\"\n", randomMode);
1728 mpd_executeCommand(connection, string);
1732 void mpd_sendSetvolCommand(mpd_Connection *connection, int volumeChange)
1734 int len = strlen("setvol") + 2 + INTLEN + 3;
1735 char *string = malloc(len);
1737 snprintf(string, len, "setvol \"%i\"\n", volumeChange);
1738 mpd_executeCommand(connection, string);
1742 void mpd_sendVolumeCommand(mpd_Connection *connection, int volumeChange)
1744 int len = strlen("volume") + 2 + INTLEN + 3;
1745 char *string = malloc(len);
1747 snprintf(string, len, "volume \"%i\"\n", volumeChange);
1748 mpd_executeCommand(connection, string);
1752 void mpd_sendCrossfadeCommand(mpd_Connection *connection, int seconds)
1754 int len = strlen("crossfade") + 2 + INTLEN + 3;
1755 char *string = malloc(len);
1757 snprintf(string, len, "crossfade \"%i\"\n", seconds);
1758 mpd_executeCommand(connection, string);
1762 void mpd_sendPasswordCommand(mpd_Connection *connection, const char *pass)
1764 char *sPass = mpd_sanitizeArg(pass);
1765 int len = strlen("password") + 2 + strlen(sPass) + 3;
1766 char *string = malloc(len);
1768 snprintf(string, len, "password \"%s\"\n", sPass);
1769 mpd_executeCommand(connection, string);
1774 void mpd_sendCommandListBegin(mpd_Connection *connection)
1776 if (connection->commandList) {
1777 strcpy(connection->errorStr, "already in command list mode");
1778 connection->error = 1;
1781 connection->commandList = COMMAND_LIST;
1782 mpd_executeCommand(connection, "command_list_begin\n");
1785 void mpd_sendCommandListOkBegin(mpd_Connection *connection)
1787 if (connection->commandList) {
1788 strcpy(connection->errorStr, "already in command list mode");
1789 connection->error = 1;
1792 connection->commandList = COMMAND_LIST_OK;
1793 mpd_executeCommand(connection, "command_list_ok_begin\n");
1794 connection->listOks = 0;
1797 void mpd_sendCommandListEnd(mpd_Connection *connection)
1799 if (!connection->commandList) {
1800 strcpy(connection->errorStr, "not in command list mode");
1801 connection->error = 1;
1804 connection->commandList = 0;
1805 mpd_executeCommand(connection, "command_list_end\n");
1808 void mpd_sendOutputsCommand(mpd_Connection *connection)
1810 mpd_executeCommand(connection, "outputs\n");
1813 mpd_OutputEntity *mpd_getNextOutput(mpd_Connection *connection)
1815 mpd_OutputEntity *output = NULL;
1817 if (connection->doneProcessing
1818 || (connection->listOks && connection->doneListOk)) {
1822 if (connection->error) {
1826 output = malloc(sizeof(mpd_OutputEntity));
1828 output->name = NULL;
1829 output->enabled = 0;
1831 if (!connection->returnElement) {
1832 mpd_getNextReturnElement(connection);
1835 while (connection->returnElement) {
1836 mpd_ReturnElement *re = connection->returnElement;
1838 if (strcmp(re->name, "outputid") == 0) {
1839 if (output != NULL && output->id >= 0) {
1842 output->id = atoi(re->value);
1843 } else if (strcmp(re->name, "outputname") == 0) {
1844 output->name = strndup(re->value, text_buffer_size);
1845 } else if (strcmp(re->name, "outputenabled") == 0) {
1846 output->enabled = atoi(re->value);
1849 mpd_getNextReturnElement(connection);
1850 if (connection->error) {
1859 void mpd_sendEnableOutputCommand(mpd_Connection *connection, int outputId)
1861 int len = strlen("enableoutput") + 2 + INTLEN + 3;
1862 char *string = malloc(len);
1864 snprintf(string, len, "enableoutput \"%i\"\n", outputId);
1865 mpd_executeCommand(connection, string);
1869 void mpd_sendDisableOutputCommand(mpd_Connection *connection, int outputId)
1871 int len = strlen("disableoutput") + 2 + INTLEN + 3;
1872 char *string = malloc(len);
1874 snprintf(string, len, "disableoutput \"%i\"\n", outputId);
1875 mpd_executeCommand(connection, string);
1879 void mpd_freeOutputElement(mpd_OutputEntity *output)
1885 /** odd naming, but it gets the not allowed commands */
1886 void mpd_sendNotCommandsCommand(mpd_Connection *connection)
1888 mpd_executeCommand(connection, "notcommands\n");
1891 /** odd naming, but it gets the allowed commands */
1892 void mpd_sendCommandsCommand(mpd_Connection *connection)
1894 mpd_executeCommand(connection, "commands\n");
1897 /** Get the next returned command */
1898 char *mpd_getNextCommand(mpd_Connection *connection)
1900 return mpd_getNextReturnElementNamed(connection, "command");
1903 void mpd_sendUrlHandlersCommand(mpd_Connection *connection)
1905 mpd_executeCommand(connection, "urlhandlers\n");
1908 char *mpd_getNextHandler(mpd_Connection *connection)
1910 return mpd_getNextReturnElementNamed(connection, "handler");
1913 void mpd_sendTagTypesCommand(mpd_Connection *connection)
1915 mpd_executeCommand(connection, "tagtypes\n");
1918 char *mpd_getNextTagType(mpd_Connection *connection)
1920 return mpd_getNextReturnElementNamed(connection, "tagtype");
1923 void mpd_startSearch(mpd_Connection *connection, int exact)
1925 if (connection->request) {
1926 strcpy(connection->errorStr, "search already in progress");
1927 connection->error = 1;
1932 connection->request = strndup("find", text_buffer_size);
1934 connection->request = strndup("search", text_buffer_size);
1938 void mpd_startStatsSearch(mpd_Connection *connection)
1940 if (connection->request) {
1941 strcpy(connection->errorStr, "search already in progress");
1942 connection->error = 1;
1946 connection->request = strndup("count", text_buffer_size);
1949 void mpd_startPlaylistSearch(mpd_Connection *connection, int exact)
1951 if (connection->request) {
1952 strcpy(connection->errorStr, "search already in progress");
1953 connection->error = 1;
1958 connection->request = strndup("playlistfind", text_buffer_size);
1960 connection->request = strndup("playlistsearch", text_buffer_size);
1964 void mpd_startFieldSearch(mpd_Connection *connection, int type)
1966 const char *strtype;
1969 if (connection->request) {
1970 strcpy(connection->errorStr, "search already in progress");
1971 connection->error = 1;
1975 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
1976 strcpy(connection->errorStr, "invalid type specified");
1977 connection->error = 1;
1981 strtype = mpdTagItemKeys[type];
1983 len = 5 + strlen(strtype) + 1;
1984 connection->request = malloc(len);
1986 snprintf(connection->request, len, "list %c%s", tolower(strtype[0]),
1990 void mpd_addConstraintSearch(mpd_Connection *connection, int type,
1993 const char *strtype;
1998 if (!connection->request) {
1999 strcpy(connection->errorStr, "no search in progress");
2000 connection->error = 1;
2004 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
2005 strcpy(connection->errorStr, "invalid type specified");
2006 connection->error = 1;
2011 strcpy(connection->errorStr, "no name specified");
2012 connection->error = 1;
2016 string = strndup(connection->request, text_buffer_size);
2017 strtype = mpdTagItemKeys[type];
2018 arg = mpd_sanitizeArg(name);
2020 len = strlen(string) + 1 + strlen(strtype) + 2 + strlen(arg) + 2;
2021 connection->request = realloc(connection->request, len);
2022 snprintf(connection->request, len, "%s %c%s \"%s\"", string,
2023 tolower(strtype[0]), strtype + 1, arg);
2029 void mpd_commitSearch(mpd_Connection *connection)
2033 if (!connection->request) {
2034 strcpy(connection->errorStr, "no search in progress");
2035 connection->error = 1;
2039 len = strlen(connection->request) + 2;
2040 connection->request = realloc(connection->request, len);
2041 connection->request[len - 2] = '\n';
2042 connection->request[len - 1] = '\0';
2043 mpd_sendInfoCommand(connection, connection->request);
2045 free(connection->request);
2046 connection->request = NULL;
2050 * @param connection a MpdConnection
2051 * @param path the path to the playlist.
2053 * List the content, with full metadata, of a stored playlist. */
2054 void mpd_sendListPlaylistInfoCommand(mpd_Connection *connection, char *path)
2056 char *arg = mpd_sanitizeArg(path);
2057 int len = strlen("listplaylistinfo") + 2 + strlen(arg) + 3;
2058 char *query = malloc(len);
2060 snprintf(query, len, "listplaylistinfo \"%s\"\n", arg);
2061 mpd_sendInfoCommand(connection, query);
2067 * @param connection a MpdConnection
2068 * @param path the path to the playlist.
2070 * List the content of a stored playlist. */
2071 void mpd_sendListPlaylistCommand(mpd_Connection *connection, char *path)
2073 char *arg = mpd_sanitizeArg(path);
2074 int len = strlen("listplaylist") + 2 + strlen(arg) + 3;
2075 char *query = malloc(len);
2077 snprintf(query, len, "listplaylist \"%s\"\n", arg);
2078 mpd_sendInfoCommand(connection, query);
2083 void mpd_sendPlaylistClearCommand(mpd_Connection *connection, char *path)
2085 char *sPath = mpd_sanitizeArg(path);
2086 int len = strlen("playlistclear") + 2 + strlen(sPath) + 3;
2087 char *string = malloc(len);
2089 snprintf(string, len, "playlistclear \"%s\"\n", sPath);
2090 mpd_executeCommand(connection, string);
2095 void mpd_sendPlaylistAddCommand(mpd_Connection *connection, char *playlist,
2098 char *sPlaylist = mpd_sanitizeArg(playlist);
2099 char *sPath = mpd_sanitizeArg(path);
2100 int len = strlen("playlistadd") + 2 + strlen(sPlaylist) + 3 +
2102 char *string = malloc(len);
2104 snprintf(string, len, "playlistadd \"%s\" \"%s\"\n", sPlaylist, sPath);
2105 mpd_executeCommand(connection, string);
2111 void mpd_sendPlaylistMoveCommand(mpd_Connection *connection, char *playlist,
2114 char *sPlaylist = mpd_sanitizeArg(playlist);
2115 int len = strlen("playlistmove") + 2 + strlen(sPlaylist) + 3 + INTLEN +
2117 char *string = malloc(len);
2119 snprintf(string, len, "playlistmove \"%s\" \"%i\" \"%i\"\n", sPlaylist,
2121 mpd_executeCommand(connection, string);
2126 void mpd_sendPlaylistDeleteCommand(mpd_Connection *connection, char *playlist,
2129 char *sPlaylist = mpd_sanitizeArg(playlist);
2130 int len = strlen("playlistdelete") + 2 + strlen(sPlaylist) + 3 +
2132 char *string = malloc(len);
2134 snprintf(string, len, "playlistdelete \"%s\" \"%i\"\n", sPlaylist, pos);
2135 mpd_executeCommand(connection, string);