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.
37 #include "libmpdclient.h"
41 #include <sys/types.h>
42 #include <sys/param.h>
48 # include <ws2tcpip.h>
51 # include <netinet/in.h>
52 # include <arpa/inet.h>
53 # include <sys/socket.h>
57 /* (bits + 1) / 3 (plus the sign character) */
58 #define INTLEN ((sizeof(int) * CHAR_BIT + 1) / 3 + 1)
59 #define LONGLONGLEN ((sizeof(long long) * CHAR_BIT + 1) / 3 + 1)
61 #define COMMAND_LIST 1
62 #define COMMAND_LIST_OK 2
71 # define MSG_DONTWAIT 0
75 # define SELECT_ERRNO_IGNORE (errno == WSAEINTR || errno == WSAEINPROGRESS)
76 # define SENDRECV_ERRNO_IGNORE SELECT_ERRNO_IGNORE
78 # define SELECT_ERRNO_IGNORE (errno == EINTR)
79 # define SENDRECV_ERRNO_IGNORE (errno == EINTR || errno == EAGAIN)
80 # define winsock_dll_error(c) 0
81 # define closesocket(s) close(s)
82 # define WSACleanup() do { /* nothing */ } while (0)
86 static int winsock_dll_error(mpd_Connection *connection)
90 if ((WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0
91 || LOBYTE(wsaData.wVersion) != 2
92 || HIBYTE(wsaData.wVersion) != 2) {
93 strcpy(connection->errorStr, "Could not find usable WinSock DLL.");
94 connection->error = MPD_ERROR_SYSTEM;
100 static int do_connect_fail(mpd_Connection *connection,
101 const struct sockaddr *serv_addr, int addrlen)
103 int iMode = 1; /* 0 = blocking, else non-blocking */
105 ioctlsocket(connection->sock, FIONBIO, (u_long FAR *) &iMode);
106 return (connect(connection->sock, serv_addr, addrlen) == SOCKET_ERROR
107 && WSAGetLastError() != WSAEWOULDBLOCK);
109 #else /* !WIN32 (sane operating systems) */
110 static int do_connect_fail(mpd_Connection *connection,
111 const struct sockaddr *serv_addr, int addrlen)
113 int flags = fcntl(connection->sock, F_GETFL, 0);
115 fcntl(connection->sock, F_SETFL, flags | O_NONBLOCK);
116 return (connect(connection->sock, serv_addr, addrlen) < 0
117 && errno != EINPROGRESS);
122 static int mpd_connect(mpd_Connection *connection, const char *host, int port,
126 char service[INTLEN + 1];
127 struct addrinfo hints;
128 struct addrinfo *res = NULL;
129 struct addrinfo *addrinfo = NULL;
132 hints.ai_flags = AI_ADDRCONFIG;
133 hints.ai_family = PF_UNSPEC;
134 hints.ai_socktype = SOCK_STREAM;
135 hints.ai_protocol = IPPROTO_TCP;
136 hints.ai_addrlen = 0;
137 hints.ai_addr = NULL;
138 hints.ai_canonname = NULL;
139 hints.ai_next = NULL;
141 snprintf(service, sizeof(service), "%i", port);
143 error = getaddrinfo(host, service, &hints, &addrinfo);
146 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
147 "host \"%s\" not found: %s", host, gai_strerror(error));
148 connection->error = MPD_ERROR_UNKHOST;
152 for (res = addrinfo; res; res = res->ai_next) {
154 if (connection->sock > -1) {
155 closesocket(connection->sock);
157 connection->sock = socket(res->ai_family, SOCK_STREAM,
159 if (connection->sock < 0) {
160 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
161 "problems creating socket: %s", strerror(errno));
162 connection->error = MPD_ERROR_SYSTEM;
163 freeaddrinfo(addrinfo);
167 mpd_setConnectionTimeout(connection, timeout);
170 if (do_connect_fail(connection, res->ai_addr, res->ai_addrlen)) {
171 /* try the next address family */
172 closesocket(connection->sock);
173 connection->sock = -1;
178 freeaddrinfo(addrinfo);
180 if (connection->sock < 0) {
181 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
182 "problems connecting to \"%s\" on port %i: %s", host, port,
184 connection->error = MPD_ERROR_CONNPORT;
191 #else /* !MPD_HAVE_GAI */
192 static int mpd_connect(mpd_Connection *connection, const char *host, int port,
195 struct hostent he, *he_res = 0;
198 struct sockaddr *dest;
200 struct sockaddr_in sin;
202 #ifdef HAVE_GETHOSTBYNAME_R
203 if (gethostbyname_r(rhost, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
204 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
205 "%s ('%s')", hstrerror(h_errno), host);
206 connection->error = MPD_ERROR_UNKHOST;
209 #else /* HAVE_GETHOSTBYNAME_R */
210 if (!(he_res = gethostbyname(host))) {
211 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
212 "host \"%s\" not found", host);
213 connection->error = MPD_ERROR_UNKHOST;
216 #endif /* HAVE_GETHOSTBYNAME_R */
218 memset(&sin, 0, sizeof(struct sockaddr_in));
219 /* dest.sin_family = he_res->h_addrtype; */
220 sin.sin_family = AF_INET;
221 sin.sin_port = htons(port);
223 switch (he_res->h_addrtype) {
225 memcpy((char *) &sin.sin_addr.s_addr, (char *) he_res->h_addr,
227 dest = (struct sockaddr *) &sin;
228 destlen = sizeof(struct sockaddr_in);
231 strcpy(connection->errorStr, "address type is not IPv4");
232 connection->error = MPD_ERROR_SYSTEM;
237 if (connection->sock > -1) {
238 closesocket(connection->sock);
240 if ((connection->sock = socket(dest->sa_family, SOCK_STREAM, 0)) < 0) {
241 strcpy(connection->errorStr, "problems creating socket");
242 connection->error = MPD_ERROR_SYSTEM;
246 mpd_setConnectionTimeout(connection, timeout);
249 if (do_connect_fail(connection, dest, destlen)) {
250 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
251 "problems connecting to \"%s\" on port %i", host, port);
252 connection->error = MPD_ERROR_CONNPORT;
258 #endif /* !MPD_HAVE_GAI */
260 const char *mpdTagItemKeys[MPD_TAG_NUM_OF_ITEM_TYPES] = {
276 static char *mpd_sanitizeArg(const char *arg)
280 register const char *c;
283 /* instead of counting in that loop above,
284 * just use a bit more memory and halve running time */
285 ret = malloc(strlen(arg) * 2 + 1);
289 for (i = strlen(arg) + 1; i != 0; --i) {
290 if (*c == '"' || *c == '\\') {
299 static mpd_ReturnElement *mpd_newReturnElement(const char *name,
302 mpd_ReturnElement *ret = malloc(sizeof(mpd_ReturnElement));
304 ret->name = strndup(name, text_buffer_size);
305 ret->value = strndup(value, text_buffer_size);
310 static void mpd_freeReturnElement(mpd_ReturnElement *re)
317 void mpd_setConnectionTimeout(mpd_Connection *connection, float timeout)
319 connection->timeout.tv_sec = (int) timeout;
320 connection->timeout.tv_usec =
321 (int) ((timeout - connection->timeout.tv_sec) * 1e6 + 0.5);
324 static int mpd_parseWelcome(mpd_Connection *connection, const char *host,
325 int port, /* char *rt, */ char *output)
331 if (strncmp(output, MPD_WELCOME_MESSAGE, strlen(MPD_WELCOME_MESSAGE))) {
332 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
333 "mpd not running on port %i on host \"%s\"", port, host);
334 connection->error = MPD_ERROR_NOTMPD;
338 tmp = &output[strlen(MPD_WELCOME_MESSAGE)];
340 for (i = 0; i < 3; i++) {
342 connection->version[i] = strtol(tmp, &test, 10);
345 if (!tmp || (test[0] != '.' && test[0] != '\0')) {
346 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
347 "error parsing version number at \"%s\"",
348 &output[strlen(MPD_WELCOME_MESSAGE)]);
349 connection->error = MPD_ERROR_NOTMPD;
358 mpd_Connection *mpd_newConnection(const char *host, int port, float timeout)
363 mpd_Connection *connection = malloc(sizeof(mpd_Connection));
367 strcpy(connection->buffer, "");
368 connection->buflen = 0;
369 connection->bufstart = 0;
370 strcpy(connection->errorStr, "");
371 connection->error = 0;
372 connection->doneProcessing = 0;
373 connection->commandList = 0;
374 connection->listOks = 0;
375 connection->doneListOk = 0;
376 connection->sock = -1;
377 connection->returnElement = NULL;
378 connection->request = NULL;
380 if (winsock_dll_error(connection)) {
384 if (mpd_connect(connection, host, port, timeout) < 0) {
388 while (!(rt = strstr(connection->buffer, "\n"))) {
389 tv.tv_sec = connection->timeout.tv_sec;
390 tv.tv_usec = connection->timeout.tv_usec;
392 FD_SET(connection->sock, &fds);
393 if ((err = select(connection->sock + 1, &fds, NULL, NULL, &tv)) == 1) {
396 readed = recv(connection->sock,
397 &(connection->buffer[connection->buflen]),
398 MPD_BUFFER_MAX_LENGTH - connection->buflen, 0);
400 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
401 "problems getting a response from \"%s\" on port %i : %s",
402 host, port, strerror(errno));
403 connection->error = MPD_ERROR_NORESPONSE;
406 connection->buflen += readed;
407 connection->buffer[connection->buflen] = '\0';
408 } else if (err < 0) {
409 if (SELECT_ERRNO_IGNORE) {
412 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
413 "problems connecting to \"%s\" on port %i", host, port);
414 connection->error = MPD_ERROR_CONNPORT;
417 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
418 "timeout in attempting to get a response from \"%s\" on "
419 "port %i", host, port);
420 connection->error = MPD_ERROR_NORESPONSE;
426 output = strndup(connection->buffer, text_buffer_size);
427 strcpy(connection->buffer, rt + 1);
428 connection->buflen = strlen(connection->buffer);
430 if (mpd_parseWelcome(connection, host, port, /* rt, */ output) == 0) {
431 connection->doneProcessing = 1;
439 void mpd_clearError(mpd_Connection *connection)
441 connection->error = 0;
442 connection->errorStr[0] = '\0';
445 void mpd_closeConnection(mpd_Connection *connection)
447 closesocket(connection->sock);
448 if (connection->returnElement) {
449 free(connection->returnElement);
451 if (connection->request) {
452 free(connection->request);
458 static void mpd_executeCommand(mpd_Connection *connection, const char *command)
463 const char *commandPtr = command;
464 int commandLen = strlen(command);
466 if (!connection->doneProcessing && !connection->commandList) {
467 strcpy(connection->errorStr, "not done processing current command");
468 connection->error = 1;
472 mpd_clearError(connection);
475 FD_SET(connection->sock, &fds);
476 tv.tv_sec = connection->timeout.tv_sec;
477 tv.tv_usec = connection->timeout.tv_usec;
479 while ((ret = select(connection->sock + 1, NULL, &fds, NULL, &tv) == 1)
480 || (ret == -1 && SELECT_ERRNO_IGNORE)) {
481 ret = send(connection->sock, commandPtr, commandLen, MSG_DONTWAIT);
483 if (SENDRECV_ERRNO_IGNORE) {
486 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
487 "problems giving command \"%s\"", command);
488 connection->error = MPD_ERROR_SENDING;
495 if (commandLen <= 0) {
500 if (commandLen > 0) {
502 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
503 "timeout sending command \"%s\"", command);
504 connection->error = MPD_ERROR_TIMEOUT;
508 if (!connection->commandList) {
509 connection->doneProcessing = 0;
510 } else if (connection->commandList == COMMAND_LIST_OK) {
511 connection->listOks++;
515 static void mpd_getNextReturnElement(mpd_Connection *connection)
525 char *bufferCheck = NULL;
529 if (connection->returnElement) {
530 mpd_freeReturnElement(connection->returnElement);
532 connection->returnElement = NULL;
534 if (connection->doneProcessing
535 || (connection->listOks && connection->doneListOk)) {
536 strcpy(connection->errorStr,
537 "already done processing current command");
538 connection->error = 1;
542 bufferCheck = connection->buffer + connection->bufstart;
543 while (connection->bufstart >= connection->buflen
544 || !(rt = strchr(bufferCheck, '\n'))) {
545 if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
546 memmove(connection->buffer,
547 connection->buffer + connection->bufstart,
548 connection->buflen - connection->bufstart + 1);
549 connection->buflen -= connection->bufstart;
550 connection->bufstart = 0;
552 if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
553 strcpy(connection->errorStr, "buffer overrun");
554 connection->error = MPD_ERROR_BUFFEROVERRUN;
555 connection->doneProcessing = 1;
556 connection->doneListOk = 0;
559 bufferCheck = connection->buffer + connection->buflen;
560 tv.tv_sec = connection->timeout.tv_sec;
561 tv.tv_usec = connection->timeout.tv_usec;
563 FD_SET(connection->sock, &fds);
564 if ((err = select(connection->sock + 1, &fds, NULL, NULL, &tv) == 1)) {
565 readed = recv(connection->sock,
566 connection->buffer + connection->buflen,
567 MPD_BUFFER_MAX_LENGTH - connection->buflen, MSG_DONTWAIT);
568 if (readed < 0 && SENDRECV_ERRNO_IGNORE) {
572 strcpy(connection->errorStr, "connection closed");
573 connection->error = MPD_ERROR_CONNCLOSED;
574 connection->doneProcessing = 1;
575 connection->doneListOk = 0;
578 connection->buflen += readed;
579 connection->buffer[connection->buflen] = '\0';
580 } else if (err < 0 && SELECT_ERRNO_IGNORE) {
583 strcpy(connection->errorStr, "connection timeout");
584 connection->error = MPD_ERROR_TIMEOUT;
585 connection->doneProcessing = 1;
586 connection->doneListOk = 0;
592 output = connection->buffer + connection->bufstart;
593 connection->bufstart = rt - connection->buffer + 1;
595 if (strcmp(output, "OK") == 0) {
596 if (connection->listOks > 0) {
597 strcpy(connection->errorStr, "expected more list_OK's");
598 connection->error = 1;
600 connection->listOks = 0;
601 connection->doneProcessing = 1;
602 connection->doneListOk = 0;
606 if (strcmp(output, "list_OK") == 0) {
607 if (!connection->listOks) {
608 strcpy(connection->errorStr, "got an unexpected list_OK");
609 connection->error = 1;
611 connection->doneListOk = 1;
612 connection->listOks--;
617 if (strncmp(output, "ACK", strlen("ACK")) == 0) {
622 strcpy(connection->errorStr, output);
623 connection->error = MPD_ERROR_ACK;
624 connection->errorCode = MPD_ACK_ERROR_UNK;
625 connection->errorAt = MPD_ERROR_AT_UNK;
626 connection->doneProcessing = 1;
627 connection->doneListOk = 0;
629 needle = strchr(output, '[');
633 val = strtol(needle + 1, &test, 10);
637 connection->errorCode = val;
638 val = strtol(test + 1, &test, 10);
642 connection->errorAt = val;
646 tok = strchr(output, ':');
655 if (value[0] == ' ') {
656 connection->returnElement = mpd_newReturnElement(name, &(value[1]));
658 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
659 "error parsing: %s:%s", name, value);
660 connection->error = 1;
664 void mpd_finishCommand(mpd_Connection *connection)
666 while (!connection->doneProcessing) {
667 if (connection->doneListOk) {
668 connection->doneListOk = 0;
670 mpd_getNextReturnElement(connection);
674 static void mpd_finishListOkCommand(mpd_Connection *connection)
676 while (!connection->doneProcessing && connection->listOks
677 && !connection->doneListOk) {
678 mpd_getNextReturnElement(connection);
682 int mpd_nextListOkCommand(mpd_Connection *connection)
684 mpd_finishListOkCommand(connection);
685 if (!connection->doneProcessing) {
686 connection->doneListOk = 0;
688 if (connection->listOks == 0 || connection->doneProcessing) {
694 void mpd_sendStatusCommand(mpd_Connection *connection)
696 mpd_executeCommand(connection, "status\n");
699 mpd_Status *mpd_getStatus(mpd_Connection *connection)
703 /* mpd_executeCommand(connection, "status\n");
704 if (connection->error) {
708 if (connection->doneProcessing
709 || (connection->listOks && connection->doneListOk)) {
713 if (!connection->returnElement) {
714 mpd_getNextReturnElement(connection);
717 status = malloc(sizeof(mpd_Status));
721 status->playlist = -1;
722 status->playlistLength = -1;
726 status->elapsedTime = 0;
727 status->totalTime = 0;
729 status->sampleRate = 0;
731 status->channels = 0;
732 status->crossfade = -1;
733 status->error = NULL;
734 status->updatingDb = 0;
736 if (connection->error) {
740 while (connection->returnElement) {
741 mpd_ReturnElement *re = connection->returnElement;
743 if (strcmp(re->name, "volume") == 0) {
744 status->volume = atoi(re->value);
745 } else if (strcmp(re->name, "repeat") == 0) {
746 status->repeat = atoi(re->value);
747 } else if (strcmp(re->name, "random") == 0) {
748 status->random = atoi(re->value);
749 } else if (strcmp(re->name, "playlist") == 0) {
750 status->playlist = strtol(re->value, NULL, 10);
751 } else if (strcmp(re->name, "playlistlength") == 0) {
752 status->playlistLength = atoi(re->value);
753 } else if (strcmp(re->name, "bitrate") == 0) {
754 status->bitRate = atoi(re->value);
755 } else if (strcmp(re->name, "state") == 0) {
756 if (strcmp(re->value, "play") == 0) {
757 status->state = MPD_STATUS_STATE_PLAY;
758 } else if (strcmp(re->value, "stop") == 0) {
759 status->state = MPD_STATUS_STATE_STOP;
760 } else if (strcmp(re->value, "pause") == 0) {
761 status->state = MPD_STATUS_STATE_PAUSE;
763 status->state = MPD_STATUS_STATE_UNKNOWN;
765 } else if (strcmp(re->name, "song") == 0) {
766 status->song = atoi(re->value);
767 } else if (strcmp(re->name, "songid") == 0) {
768 status->songid = atoi(re->value);
769 } else if (strcmp(re->name, "time") == 0) {
770 char *tok = strchr(re->value, ':');
772 /* the second strchr below is a safety check */
773 if (tok && (strchr(tok, 0) > (tok + 1))) {
774 /* atoi stops at the first non-[0-9] char: */
775 status->elapsedTime = atoi(re->value);
776 status->totalTime = atoi(tok + 1);
778 } else if (strcmp(re->name, "error") == 0) {
779 status->error = strndup(re->value, text_buffer_size);
780 } else if (strcmp(re->name, "xfade") == 0) {
781 status->crossfade = atoi(re->value);
782 } else if (strcmp(re->name, "updating_db") == 0) {
783 status->updatingDb = atoi(re->value);
784 } else if (strcmp(re->name, "audio") == 0) {
785 char *tok = strchr(re->value, ':');
787 if (tok && (strchr(tok, 0) > (tok + 1))) {
788 status->sampleRate = atoi(re->value);
789 status->bits = atoi(++tok);
790 tok = strchr(tok, ':');
791 if (tok && (strchr(tok, 0) > (tok + 1))) {
792 status->channels = atoi(tok + 1);
797 mpd_getNextReturnElement(connection);
798 if (connection->error) {
804 if (connection->error) {
807 } else if (status->state < 0) {
808 strcpy(connection->errorStr, "state not found");
809 connection->error = 1;
817 void mpd_freeStatus(mpd_Status *status)
825 void mpd_sendStatsCommand(mpd_Connection *connection)
827 mpd_executeCommand(connection, "stats\n");
830 mpd_Stats *mpd_getStats(mpd_Connection *connection)
834 /* mpd_executeCommand(connection, "stats\n");
835 if (connection->error) {
839 if (connection->doneProcessing
840 || (connection->listOks && connection->doneListOk)) {
844 if (!connection->returnElement) {
845 mpd_getNextReturnElement(connection);
848 stats = malloc(sizeof(mpd_Stats));
849 stats->numberOfArtists = 0;
850 stats->numberOfAlbums = 0;
851 stats->numberOfSongs = 0;
853 stats->dbUpdateTime = 0;
855 stats->dbPlayTime = 0;
857 if (connection->error) {
861 while (connection->returnElement) {
862 mpd_ReturnElement *re = connection->returnElement;
864 if (strcmp(re->name, "artists") == 0) {
865 stats->numberOfArtists = atoi(re->value);
866 } else if (strcmp(re->name, "albums") == 0) {
867 stats->numberOfAlbums = atoi(re->value);
868 } else if (strcmp(re->name, "songs") == 0) {
869 stats->numberOfSongs = atoi(re->value);
870 } else if (strcmp(re->name, "uptime") == 0) {
871 stats->uptime = strtol(re->value, NULL, 10);
872 } else if (strcmp(re->name, "db_update") == 0) {
873 stats->dbUpdateTime = strtol(re->value, NULL, 10);
874 } else if (strcmp(re->name, "playtime") == 0) {
875 stats->playTime = strtol(re->value, NULL, 10);
876 } else if (strcmp(re->name, "db_playtime") == 0) {
877 stats->dbPlayTime = strtol(re->value, NULL, 10);
880 mpd_getNextReturnElement(connection);
881 if (connection->error) {
887 if (connection->error) {
895 void mpd_freeStats(mpd_Stats *stats)
900 mpd_SearchStats *mpd_getSearchStats(mpd_Connection *connection)
902 mpd_SearchStats *stats;
903 mpd_ReturnElement *re;
905 if (connection->doneProcessing
906 || (connection->listOks && connection->doneListOk)) {
910 if (!connection->returnElement) {
911 mpd_getNextReturnElement(connection);
914 if (connection->error) {
918 stats = malloc(sizeof(mpd_SearchStats));
919 stats->numberOfSongs = 0;
922 while (connection->returnElement) {
923 re = connection->returnElement;
925 if (strcmp(re->name, "songs") == 0) {
926 stats->numberOfSongs = atoi(re->value);
927 } else if (strcmp(re->name, "playtime") == 0) {
928 stats->playTime = strtol(re->value, NULL, 10);
931 mpd_getNextReturnElement(connection);
932 if (connection->error) {
938 if (connection->error) {
946 void mpd_freeSearchStats(mpd_SearchStats *stats)
951 static void mpd_initSong(mpd_Song *song)
962 song->composer = NULL;
963 song->performer = NULL;
965 song->comment = NULL;
967 song->time = MPD_SONG_NO_TIME;
968 song->pos = MPD_SONG_NO_NUM;
969 song->id = MPD_SONG_NO_ID;
972 static void mpd_finishSong(mpd_Song *song)
998 if (song->composer) {
999 free(song->composer);
1004 if (song->comment) {
1005 free(song->comment);
1009 mpd_Song *mpd_newSong(void)
1011 mpd_Song *ret = malloc(sizeof(mpd_Song));
1018 void mpd_freeSong(mpd_Song *song)
1020 mpd_finishSong(song);
1024 mpd_Song *mpd_songDup(mpd_Song *song)
1026 mpd_Song *ret = mpd_newSong();
1029 ret->file = strndup(song->file, text_buffer_size);
1032 ret->artist = strndup(song->artist, text_buffer_size);
1035 ret->album = strndup(song->album, text_buffer_size);
1038 ret->title = strndup(song->title, text_buffer_size);
1041 ret->track = strndup(song->track, text_buffer_size);
1044 ret->name = strndup(song->name, text_buffer_size);
1047 ret->date = strndup(song->date, text_buffer_size);
1050 ret->genre = strndup(song->genre, text_buffer_size);
1052 if (song->composer) {
1053 ret->composer = strndup(song->composer, text_buffer_size);
1056 ret->disc = strndup(song->disc, text_buffer_size);
1058 if (song->comment) {
1059 ret->comment = strndup(song->comment, text_buffer_size);
1061 ret->time = song->time;
1062 ret->pos = song->pos;
1068 static void mpd_initDirectory(mpd_Directory *directory)
1070 directory->path = NULL;
1073 static void mpd_finishDirectory(mpd_Directory *directory)
1075 if (directory->path) {
1076 free(directory->path);
1080 mpd_Directory *mpd_newDirectory(void)
1082 mpd_Directory *directory = malloc(sizeof(mpd_Directory));
1084 mpd_initDirectory(directory);
1089 void mpd_freeDirectory(mpd_Directory *directory)
1091 mpd_finishDirectory(directory);
1096 mpd_Directory *mpd_directoryDup(mpd_Directory *directory)
1098 mpd_Directory *ret = mpd_newDirectory();
1100 if (directory->path) {
1101 ret->path = strndup(directory->path, text_buffer_size);
1107 static void mpd_initPlaylistFile(mpd_PlaylistFile *playlist)
1109 playlist->path = NULL;
1112 static void mpd_finishPlaylistFile(mpd_PlaylistFile *playlist)
1114 if (playlist->path) {
1115 free(playlist->path);
1119 mpd_PlaylistFile *mpd_newPlaylistFile(void)
1121 mpd_PlaylistFile *playlist = malloc(sizeof(mpd_PlaylistFile));
1123 mpd_initPlaylistFile(playlist);
1128 void mpd_freePlaylistFile(mpd_PlaylistFile *playlist)
1130 mpd_finishPlaylistFile(playlist);
1134 mpd_PlaylistFile *mpd_playlistFileDup(mpd_PlaylistFile *playlist)
1136 mpd_PlaylistFile *ret = mpd_newPlaylistFile();
1138 if (playlist->path) {
1139 ret->path = strndup(playlist->path, text_buffer_size);
1145 static void mpd_initInfoEntity(mpd_InfoEntity *entity)
1147 entity->info.directory = NULL;
1150 static void mpd_finishInfoEntity(mpd_InfoEntity *entity)
1152 if (entity->info.directory) {
1153 if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1154 mpd_freeDirectory(entity->info.directory);
1155 } else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
1156 mpd_freeSong(entity->info.song);
1157 } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1158 mpd_freePlaylistFile(entity->info.playlistFile);
1163 mpd_InfoEntity *mpd_newInfoEntity(void)
1165 mpd_InfoEntity *entity = malloc(sizeof(mpd_InfoEntity));
1167 mpd_initInfoEntity(entity);
1172 void mpd_freeInfoEntity(mpd_InfoEntity *entity)
1174 mpd_finishInfoEntity(entity);
1178 static void mpd_sendInfoCommand(mpd_Connection *connection, char *command)
1180 mpd_executeCommand(connection, command);
1183 mpd_InfoEntity *mpd_getNextInfoEntity(mpd_Connection *connection)
1185 mpd_InfoEntity *entity = NULL;
1187 if (connection->doneProcessing
1188 || (connection->listOks && connection->doneListOk)) {
1192 if (!connection->returnElement) {
1193 mpd_getNextReturnElement(connection);
1196 if (connection->returnElement) {
1197 if (strcmp(connection->returnElement->name, "file") == 0) {
1198 entity = mpd_newInfoEntity();
1199 entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1200 entity->info.song = mpd_newSong();
1201 entity->info.song->file = strndup(connection->returnElement->value, text_buffer_size);
1202 } else if (strcmp(connection->returnElement->name, "directory") == 0) {
1203 entity = mpd_newInfoEntity();
1204 entity->type = MPD_INFO_ENTITY_TYPE_DIRECTORY;
1205 entity->info.directory = mpd_newDirectory();
1206 entity->info.directory->path =
1207 strndup(connection->returnElement->value, text_buffer_size);
1208 } else if (strcmp(connection->returnElement->name, "playlist") == 0) {
1209 entity = mpd_newInfoEntity();
1210 entity->type = MPD_INFO_ENTITY_TYPE_PLAYLISTFILE;
1211 entity->info.playlistFile = mpd_newPlaylistFile();
1212 entity->info.playlistFile->path =
1213 strndup(connection->returnElement->value, text_buffer_size);
1214 } else if (strcmp(connection->returnElement->name, "cpos") == 0) {
1215 entity = mpd_newInfoEntity();
1216 entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1217 entity->info.song = mpd_newSong();
1218 entity->info.song->pos = atoi(connection->returnElement->value);
1220 connection->error = 1;
1221 strcpy(connection->errorStr, "problem parsing song info");
1228 mpd_getNextReturnElement(connection);
1229 while (connection->returnElement) {
1230 mpd_ReturnElement *re = connection->returnElement;
1232 if (strcmp(re->name, "file") == 0) {
1234 } else if (strcmp(re->name, "directory") == 0) {
1236 } else if (strcmp(re->name, "playlist") == 0) {
1238 } else if (strcmp(re->name, "cpos") == 0) {
1242 if (entity->type == MPD_INFO_ENTITY_TYPE_SONG && strlen(re->value)) {
1243 if (!entity->info.song->artist
1244 && strcmp(re->name, "Artist") == 0) {
1245 entity->info.song->artist = strndup(re->value, text_buffer_size);
1246 } else if (!entity->info.song->album
1247 && strcmp(re->name, "Album") == 0) {
1248 entity->info.song->album = strndup(re->value, text_buffer_size);
1249 } else if (!entity->info.song->title
1250 && strcmp(re->name, "Title") == 0) {
1251 entity->info.song->title = strndup(re->value, text_buffer_size);
1252 } else if (!entity->info.song->track
1253 && strcmp(re->name, "Track") == 0) {
1254 entity->info.song->track = strndup(re->value, text_buffer_size);
1255 } else if (!entity->info.song->name
1256 && strcmp(re->name, "Name") == 0) {
1257 entity->info.song->name = strndup(re->value, text_buffer_size);
1258 } else if (entity->info.song->time == MPD_SONG_NO_TIME
1259 && strcmp(re->name, "Time") == 0) {
1260 entity->info.song->time = atoi(re->value);
1261 } else if (entity->info.song->pos == MPD_SONG_NO_NUM
1262 && strcmp(re->name, "Pos") == 0) {
1263 entity->info.song->pos = atoi(re->value);
1264 } else if (entity->info.song->id == MPD_SONG_NO_ID
1265 && strcmp(re->name, "Id") == 0) {
1266 entity->info.song->id = atoi(re->value);
1267 } else if (!entity->info.song->date
1268 && strcmp(re->name, "Date") == 0) {
1269 entity->info.song->date = strndup(re->value, text_buffer_size);
1270 } else if (!entity->info.song->genre
1271 && strcmp(re->name, "Genre") == 0) {
1272 entity->info.song->genre = strndup(re->value, text_buffer_size);
1273 } else if (!entity->info.song->composer
1274 && strcmp(re->name, "Composer") == 0) {
1275 entity->info.song->composer = strndup(re->value, text_buffer_size);
1276 } else if (!entity->info.song->performer
1277 && strcmp(re->name, "Performer") == 0) {
1278 entity->info.song->performer = strndup(re->value, text_buffer_size);
1279 } else if (!entity->info.song->disc
1280 && strcmp(re->name, "Disc") == 0) {
1281 entity->info.song->disc = strndup(re->value, text_buffer_size);
1282 } else if (!entity->info.song->comment
1283 && strcmp(re->name, "Comment") == 0) {
1284 entity->info.song->comment = strndup(re->value, text_buffer_size);
1286 } else if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1287 } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1290 mpd_getNextReturnElement(connection);
1296 static char *mpd_getNextReturnElementNamed(mpd_Connection *connection,
1299 if (connection->doneProcessing
1300 || (connection->listOks && connection->doneListOk)) {
1304 mpd_getNextReturnElement(connection);
1305 while (connection->returnElement) {
1306 mpd_ReturnElement *re = connection->returnElement;
1308 if (strcmp(re->name, name) == 0) {
1309 return strndup(re->value, text_buffer_size);
1311 mpd_getNextReturnElement(connection);
1317 char *mpd_getNextTag(mpd_Connection *connection, int type)
1319 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES
1320 || type == MPD_TAG_ITEM_ANY) {
1323 if (type == MPD_TAG_ITEM_FILENAME) {
1324 return mpd_getNextReturnElementNamed(connection, "file");
1326 return mpd_getNextReturnElementNamed(connection, mpdTagItemKeys[type]);
1329 char *mpd_getNextArtist(mpd_Connection *connection)
1331 return mpd_getNextReturnElementNamed(connection, "Artist");
1334 char *mpd_getNextAlbum(mpd_Connection *connection)
1336 return mpd_getNextReturnElementNamed(connection, "Album");
1339 void mpd_sendPlaylistInfoCommand(mpd_Connection *connection, int songPos)
1341 int len = strlen("playlistinfo") + 2 + INTLEN + 3;
1342 char *string = malloc(len);
1344 snprintf(string, len, "playlistinfo \"%i\"\n", songPos);
1345 mpd_sendInfoCommand(connection, string);
1349 void mpd_sendPlaylistIdCommand(mpd_Connection *connection, int id)
1351 int len = strlen("playlistid") + 2 + INTLEN + 3;
1352 char *string = malloc(len);
1354 snprintf(string, len, "playlistid \"%i\"\n", id);
1355 mpd_sendInfoCommand(connection, string);
1359 void mpd_sendPlChangesCommand(mpd_Connection *connection, long long playlist)
1361 int len = strlen("plchanges") + 2 + LONGLONGLEN + 3;
1362 char *string = malloc(len);
1364 snprintf(string, len, "plchanges \"%lld\"\n", playlist);
1365 mpd_sendInfoCommand(connection, string);
1369 void mpd_sendPlChangesPosIdCommand(mpd_Connection *connection,
1372 int len = strlen("plchangesposid") + 2 + LONGLONGLEN + 3;
1373 char *string = malloc(len);
1375 snprintf(string, len, "plchangesposid \"%lld\"\n", playlist);
1376 mpd_sendInfoCommand(connection, string);
1380 void mpd_sendListallCommand(mpd_Connection *connection, const char *dir)
1382 char *sDir = mpd_sanitizeArg(dir);
1383 int len = strlen("listall") + 2 + strlen(sDir) + 3;
1384 char *string = malloc(len);
1386 snprintf(string, len, "listall \"%s\"\n", sDir);
1387 mpd_sendInfoCommand(connection, string);
1392 void mpd_sendListallInfoCommand(mpd_Connection *connection, const char *dir)
1394 char *sDir = mpd_sanitizeArg(dir);
1395 int len = strlen("listallinfo") + 2 + strlen(sDir) + 3;
1396 char *string = malloc(len);
1398 snprintf(string, len, "listallinfo \"%s\"\n", sDir);
1399 mpd_sendInfoCommand(connection, string);
1404 void mpd_sendLsInfoCommand(mpd_Connection *connection, const char *dir)
1406 char *sDir = mpd_sanitizeArg(dir);
1407 int len = strlen("lsinfo") + 2 + strlen(sDir) + 3;
1408 char *string = malloc(len);
1410 snprintf(string, len, "lsinfo \"%s\"\n", sDir);
1411 mpd_sendInfoCommand(connection, string);
1416 void mpd_sendCurrentSongCommand(mpd_Connection *connection)
1418 mpd_executeCommand(connection, "currentsong\n");
1421 void mpd_sendSearchCommand(mpd_Connection *connection, int table,
1424 mpd_startSearch(connection, 0);
1425 mpd_addConstraintSearch(connection, table, str);
1426 mpd_commitSearch(connection);
1429 void mpd_sendFindCommand(mpd_Connection *connection, int table,
1432 mpd_startSearch(connection, 1);
1433 mpd_addConstraintSearch(connection, table, str);
1434 mpd_commitSearch(connection);
1437 void mpd_sendListCommand(mpd_Connection *connection, int table,
1444 if (table == MPD_TABLE_ARTIST) {
1445 strcpy(st, "artist");
1446 } else if (table == MPD_TABLE_ALBUM) {
1447 strcpy(st, "album");
1449 connection->error = 1;
1450 strcpy(connection->errorStr, "unknown table for list");
1454 char *sanitArg1 = mpd_sanitizeArg(arg1);
1456 len = strlen("list") + 1 + strlen(sanitArg1) + 2 + strlen(st) + 3;
1457 string = malloc(len);
1458 snprintf(string, len, "list %s \"%s\"\n", st, sanitArg1);
1461 len = strlen("list") + 1 + strlen(st) + 2;
1462 string = malloc(len);
1463 snprintf(string, len, "list %s\n", st);
1465 mpd_sendInfoCommand(connection, string);
1469 void mpd_sendAddCommand(mpd_Connection *connection, const char *file)
1471 char *sFile = mpd_sanitizeArg(file);
1472 int len = strlen("add") + 2 + strlen(sFile) + 3;
1473 char *string = malloc(len);
1475 snprintf(string, len, "add \"%s\"\n", sFile);
1476 mpd_executeCommand(connection, string);
1481 int mpd_sendAddIdCommand(mpd_Connection *connection, const char *file)
1484 char *sFile = mpd_sanitizeArg(file);
1485 int len = strlen("addid") + 2 + strlen(sFile) + 3;
1486 char *string = malloc(len);
1488 snprintf(string, len, "addid \"%s\"\n", sFile);
1489 mpd_sendInfoCommand(connection, string);
1493 string = mpd_getNextReturnElementNamed(connection, "Id");
1495 retval = atoi(string);
1502 void mpd_sendDeleteCommand(mpd_Connection *connection, int songPos)
1504 int len = strlen("delete") + 2 + INTLEN + 3;
1505 char *string = malloc(len);
1507 snprintf(string, len, "delete \"%i\"\n", songPos);
1508 mpd_sendInfoCommand(connection, string);
1512 void mpd_sendDeleteIdCommand(mpd_Connection *connection, int id)
1514 int len = strlen("deleteid") + 2 + INTLEN + 3;
1515 char *string = malloc(len);
1517 snprintf(string, len, "deleteid \"%i\"\n", id);
1518 mpd_sendInfoCommand(connection, string);
1522 void mpd_sendSaveCommand(mpd_Connection *connection, const char *name)
1524 char *sName = mpd_sanitizeArg(name);
1525 int len = strlen("save") + 2 + strlen(sName) + 3;
1526 char *string = malloc(len);
1528 snprintf(string, len, "save \"%s\"\n", sName);
1529 mpd_executeCommand(connection, string);
1534 void mpd_sendLoadCommand(mpd_Connection *connection, const char *name)
1536 char *sName = mpd_sanitizeArg(name);
1537 int len = strlen("load") + 2 + strlen(sName) + 3;
1538 char *string = malloc(len);
1540 snprintf(string, len, "load \"%s\"\n", sName);
1541 mpd_executeCommand(connection, string);
1546 void mpd_sendRmCommand(mpd_Connection *connection, const char *name)
1548 char *sName = mpd_sanitizeArg(name);
1549 int len = strlen("rm") + 2 + strlen(sName) + 3;
1550 char *string = malloc(len);
1552 snprintf(string, len, "rm \"%s\"\n", sName);
1553 mpd_executeCommand(connection, string);
1558 void mpd_sendRenameCommand(mpd_Connection *connection, const char *from,
1561 char *sFrom = mpd_sanitizeArg(from);
1562 char *sTo = mpd_sanitizeArg(to);
1563 int len = strlen("rename") + 2 + strlen(sFrom) + 3 + strlen(sTo) + 3;
1564 char *string = malloc(len);
1566 snprintf(string, len, "rename \"%s\" \"%s\"\n", sFrom, sTo);
1567 mpd_executeCommand(connection, string);
1573 void mpd_sendShuffleCommand(mpd_Connection *connection)
1575 mpd_executeCommand(connection, "shuffle\n");
1578 void mpd_sendClearCommand(mpd_Connection *connection)
1580 mpd_executeCommand(connection, "clear\n");
1583 void mpd_sendPlayCommand(mpd_Connection *connection, int songPos)
1585 int len = strlen("play") + 2 + INTLEN + 3;
1586 char *string = malloc(len);
1588 snprintf(string, len, "play \"%i\"\n", songPos);
1589 mpd_sendInfoCommand(connection, string);
1593 void mpd_sendPlayIdCommand(mpd_Connection *connection, int id)
1595 int len = strlen("playid") + 2 + INTLEN + 3;
1596 char *string = malloc(len);
1598 snprintf(string, len, "playid \"%i\"\n", id);
1599 mpd_sendInfoCommand(connection, string);
1603 void mpd_sendStopCommand(mpd_Connection *connection)
1605 mpd_executeCommand(connection, "stop\n");
1608 void mpd_sendPauseCommand(mpd_Connection *connection, int pauseMode)
1610 int len = strlen("pause") + 2 + INTLEN + 3;
1611 char *string = malloc(len);
1613 snprintf(string, len, "pause \"%i\"\n", pauseMode);
1614 mpd_executeCommand(connection, string);
1618 void mpd_sendNextCommand(mpd_Connection *connection)
1620 mpd_executeCommand(connection, "next\n");
1623 void mpd_sendMoveCommand(mpd_Connection *connection, int from, int to)
1625 int len = strlen("move") + 2 + INTLEN + 3 + INTLEN + 3;
1626 char *string = malloc(len);
1628 snprintf(string, len, "move \"%i\" \"%i\"\n", from, to);
1629 mpd_sendInfoCommand(connection, string);
1633 void mpd_sendMoveIdCommand(mpd_Connection *connection, int id, int to)
1635 int len = strlen("moveid") + 2 + INTLEN + 3 + INTLEN + 3;
1636 char *string = malloc(len);
1638 snprintf(string, len, "moveid \"%i\" \"%i\"\n", id, to);
1639 mpd_sendInfoCommand(connection, string);
1643 void mpd_sendSwapCommand(mpd_Connection *connection, int song1, int song2)
1645 int len = strlen("swap") + 2 + INTLEN + 3 + INTLEN + 3;
1646 char *string = malloc(len);
1648 snprintf(string, len, "swap \"%i\" \"%i\"\n", song1, song2);
1649 mpd_sendInfoCommand(connection, string);
1653 void mpd_sendSwapIdCommand(mpd_Connection *connection, int id1, int id2)
1655 int len = strlen("swapid") + 2 + INTLEN + 3 + INTLEN + 3;
1656 char *string = malloc(len);
1658 snprintf(string, len, "swapid \"%i\" \"%i\"\n", id1, id2);
1659 mpd_sendInfoCommand(connection, string);
1663 void mpd_sendSeekCommand(mpd_Connection *connection, int song, int seek_time)
1665 int len = strlen("seek") + 2 + INTLEN + 3 + INTLEN + 3;
1666 char *string = malloc(len);
1668 snprintf(string, len, "seek \"%i\" \"%i\"\n", song, seek_time);
1669 mpd_sendInfoCommand(connection, string);
1673 void mpd_sendSeekIdCommand(mpd_Connection *connection, int id, int seek_time)
1675 int len = strlen("seekid") + 2 + INTLEN + 3 + INTLEN + 3;
1676 char *string = malloc(len);
1678 snprintf(string, len, "seekid \"%i\" \"%i\"\n", id, seek_time);
1679 mpd_sendInfoCommand(connection, string);
1683 void mpd_sendUpdateCommand(mpd_Connection *connection, char *path)
1685 char *sPath = mpd_sanitizeArg(path);
1686 int len = strlen("update") + 2 + strlen(sPath) + 3;
1687 char *string = malloc(len);
1689 snprintf(string, len, "update \"%s\"\n", sPath);
1690 mpd_sendInfoCommand(connection, string);
1695 int mpd_getUpdateId(mpd_Connection *connection)
1700 jobid = mpd_getNextReturnElementNamed(connection, "updating_db");
1709 void mpd_sendPrevCommand(mpd_Connection *connection)
1711 mpd_executeCommand(connection, "previous\n");
1714 void mpd_sendRepeatCommand(mpd_Connection *connection, int repeatMode)
1716 int len = strlen("repeat") + 2 + INTLEN + 3;
1717 char *string = malloc(len);
1719 snprintf(string, len, "repeat \"%i\"\n", repeatMode);
1720 mpd_executeCommand(connection, string);
1724 void mpd_sendRandomCommand(mpd_Connection *connection, int randomMode)
1726 int len = strlen("random") + 2 + INTLEN + 3;
1727 char *string = malloc(len);
1729 snprintf(string, len, "random \"%i\"\n", randomMode);
1730 mpd_executeCommand(connection, string);
1734 void mpd_sendSetvolCommand(mpd_Connection *connection, int volumeChange)
1736 int len = strlen("setvol") + 2 + INTLEN + 3;
1737 char *string = malloc(len);
1739 snprintf(string, len, "setvol \"%i\"\n", volumeChange);
1740 mpd_executeCommand(connection, string);
1744 void mpd_sendVolumeCommand(mpd_Connection *connection, int volumeChange)
1746 int len = strlen("volume") + 2 + INTLEN + 3;
1747 char *string = malloc(len);
1749 snprintf(string, len, "volume \"%i\"\n", volumeChange);
1750 mpd_executeCommand(connection, string);
1754 void mpd_sendCrossfadeCommand(mpd_Connection *connection, int seconds)
1756 int len = strlen("crossfade") + 2 + INTLEN + 3;
1757 char *string = malloc(len);
1759 snprintf(string, len, "crossfade \"%i\"\n", seconds);
1760 mpd_executeCommand(connection, string);
1764 void mpd_sendPasswordCommand(mpd_Connection *connection, const char *pass)
1766 char *sPass = mpd_sanitizeArg(pass);
1767 int len = strlen("password") + 2 + strlen(sPass) + 3;
1768 char *string = malloc(len);
1770 snprintf(string, len, "password \"%s\"\n", sPass);
1771 mpd_executeCommand(connection, string);
1776 void mpd_sendCommandListBegin(mpd_Connection *connection)
1778 if (connection->commandList) {
1779 strcpy(connection->errorStr, "already in command list mode");
1780 connection->error = 1;
1783 connection->commandList = COMMAND_LIST;
1784 mpd_executeCommand(connection, "command_list_begin\n");
1787 void mpd_sendCommandListOkBegin(mpd_Connection *connection)
1789 if (connection->commandList) {
1790 strcpy(connection->errorStr, "already in command list mode");
1791 connection->error = 1;
1794 connection->commandList = COMMAND_LIST_OK;
1795 mpd_executeCommand(connection, "command_list_ok_begin\n");
1796 connection->listOks = 0;
1799 void mpd_sendCommandListEnd(mpd_Connection *connection)
1801 if (!connection->commandList) {
1802 strcpy(connection->errorStr, "not in command list mode");
1803 connection->error = 1;
1806 connection->commandList = 0;
1807 mpd_executeCommand(connection, "command_list_end\n");
1810 void mpd_sendOutputsCommand(mpd_Connection *connection)
1812 mpd_executeCommand(connection, "outputs\n");
1815 mpd_OutputEntity *mpd_getNextOutput(mpd_Connection *connection)
1817 mpd_OutputEntity *output = NULL;
1819 if (connection->doneProcessing
1820 || (connection->listOks && connection->doneListOk)) {
1824 if (connection->error) {
1828 output = malloc(sizeof(mpd_OutputEntity));
1830 output->name = NULL;
1831 output->enabled = 0;
1833 if (!connection->returnElement) {
1834 mpd_getNextReturnElement(connection);
1837 while (connection->returnElement) {
1838 mpd_ReturnElement *re = connection->returnElement;
1840 if (strcmp(re->name, "outputid") == 0) {
1841 if (output != NULL && output->id >= 0) {
1844 output->id = atoi(re->value);
1845 } else if (strcmp(re->name, "outputname") == 0) {
1846 output->name = strndup(re->value, text_buffer_size);
1847 } else if (strcmp(re->name, "outputenabled") == 0) {
1848 output->enabled = atoi(re->value);
1851 mpd_getNextReturnElement(connection);
1852 if (connection->error) {
1861 void mpd_sendEnableOutputCommand(mpd_Connection *connection, int outputId)
1863 int len = strlen("enableoutput") + 2 + INTLEN + 3;
1864 char *string = malloc(len);
1866 snprintf(string, len, "enableoutput \"%i\"\n", outputId);
1867 mpd_executeCommand(connection, string);
1871 void mpd_sendDisableOutputCommand(mpd_Connection *connection, int outputId)
1873 int len = strlen("disableoutput") + 2 + INTLEN + 3;
1874 char *string = malloc(len);
1876 snprintf(string, len, "disableoutput \"%i\"\n", outputId);
1877 mpd_executeCommand(connection, string);
1881 void mpd_freeOutputElement(mpd_OutputEntity *output)
1887 /** odd naming, but it gets the not allowed commands */
1888 void mpd_sendNotCommandsCommand(mpd_Connection *connection)
1890 mpd_executeCommand(connection, "notcommands\n");
1893 /** odd naming, but it gets the allowed commands */
1894 void mpd_sendCommandsCommand(mpd_Connection *connection)
1896 mpd_executeCommand(connection, "commands\n");
1899 /** Get the next returned command */
1900 char *mpd_getNextCommand(mpd_Connection *connection)
1902 return mpd_getNextReturnElementNamed(connection, "command");
1905 void mpd_sendUrlHandlersCommand(mpd_Connection *connection)
1907 mpd_executeCommand(connection, "urlhandlers\n");
1910 char *mpd_getNextHandler(mpd_Connection *connection)
1912 return mpd_getNextReturnElementNamed(connection, "handler");
1915 void mpd_sendTagTypesCommand(mpd_Connection *connection)
1917 mpd_executeCommand(connection, "tagtypes\n");
1920 char *mpd_getNextTagType(mpd_Connection *connection)
1922 return mpd_getNextReturnElementNamed(connection, "tagtype");
1925 void mpd_startSearch(mpd_Connection *connection, int exact)
1927 if (connection->request) {
1928 strcpy(connection->errorStr, "search already in progress");
1929 connection->error = 1;
1934 connection->request = strndup("find", text_buffer_size);
1936 connection->request = strndup("search", text_buffer_size);
1940 void mpd_startStatsSearch(mpd_Connection *connection)
1942 if (connection->request) {
1943 strcpy(connection->errorStr, "search already in progress");
1944 connection->error = 1;
1948 connection->request = strndup("count", text_buffer_size);
1951 void mpd_startPlaylistSearch(mpd_Connection *connection, int exact)
1953 if (connection->request) {
1954 strcpy(connection->errorStr, "search already in progress");
1955 connection->error = 1;
1960 connection->request = strndup("playlistfind", text_buffer_size);
1962 connection->request = strndup("playlistsearch", text_buffer_size);
1966 void mpd_startFieldSearch(mpd_Connection *connection, int type)
1968 const char *strtype;
1971 if (connection->request) {
1972 strcpy(connection->errorStr, "search already in progress");
1973 connection->error = 1;
1977 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
1978 strcpy(connection->errorStr, "invalid type specified");
1979 connection->error = 1;
1983 strtype = mpdTagItemKeys[type];
1985 len = 5 + strlen(strtype) + 1;
1986 connection->request = malloc(len);
1988 snprintf(connection->request, len, "list %c%s", tolower(strtype[0]),
1992 void mpd_addConstraintSearch(mpd_Connection *connection, int type,
1995 const char *strtype;
2000 if (!connection->request) {
2001 strcpy(connection->errorStr, "no search in progress");
2002 connection->error = 1;
2006 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
2007 strcpy(connection->errorStr, "invalid type specified");
2008 connection->error = 1;
2013 strcpy(connection->errorStr, "no name specified");
2014 connection->error = 1;
2018 string = strndup(connection->request, text_buffer_size);
2019 strtype = mpdTagItemKeys[type];
2020 arg = mpd_sanitizeArg(name);
2022 len = strlen(string) + 1 + strlen(strtype) + 2 + strlen(arg) + 2;
2023 connection->request = realloc(connection->request, len);
2024 snprintf(connection->request, len, "%s %c%s \"%s\"", string,
2025 tolower(strtype[0]), strtype + 1, arg);
2031 void mpd_commitSearch(mpd_Connection *connection)
2035 if (!connection->request) {
2036 strcpy(connection->errorStr, "no search in progress");
2037 connection->error = 1;
2041 len = strlen(connection->request) + 2;
2042 connection->request = realloc(connection->request, len);
2043 connection->request[len - 2] = '\n';
2044 connection->request[len - 1] = '\0';
2045 mpd_sendInfoCommand(connection, connection->request);
2047 free(connection->request);
2048 connection->request = NULL;
2052 * @param connection a MpdConnection
2053 * @param path the path to the playlist.
2055 * List the content, with full metadata, of a stored playlist. */
2056 void mpd_sendListPlaylistInfoCommand(mpd_Connection *connection, char *path)
2058 char *arg = mpd_sanitizeArg(path);
2059 int len = strlen("listplaylistinfo") + 2 + strlen(arg) + 3;
2060 char *query = malloc(len);
2062 snprintf(query, len, "listplaylistinfo \"%s\"\n", arg);
2063 mpd_sendInfoCommand(connection, query);
2069 * @param connection a MpdConnection
2070 * @param path the path to the playlist.
2072 * List the content of a stored playlist. */
2073 void mpd_sendListPlaylistCommand(mpd_Connection *connection, char *path)
2075 char *arg = mpd_sanitizeArg(path);
2076 int len = strlen("listplaylist") + 2 + strlen(arg) + 3;
2077 char *query = malloc(len);
2079 snprintf(query, len, "listplaylist \"%s\"\n", arg);
2080 mpd_sendInfoCommand(connection, query);
2085 void mpd_sendPlaylistClearCommand(mpd_Connection *connection, char *path)
2087 char *sPath = mpd_sanitizeArg(path);
2088 int len = strlen("playlistclear") + 2 + strlen(sPath) + 3;
2089 char *string = malloc(len);
2091 snprintf(string, len, "playlistclear \"%s\"\n", sPath);
2092 mpd_executeCommand(connection, string);
2097 void mpd_sendPlaylistAddCommand(mpd_Connection *connection, char *playlist,
2100 char *sPlaylist = mpd_sanitizeArg(playlist);
2101 char *sPath = mpd_sanitizeArg(path);
2102 int len = strlen("playlistadd") + 2 + strlen(sPlaylist) + 3 +
2104 char *string = malloc(len);
2106 snprintf(string, len, "playlistadd \"%s\" \"%s\"\n", sPlaylist, sPath);
2107 mpd_executeCommand(connection, string);
2113 void mpd_sendPlaylistMoveCommand(mpd_Connection *connection, char *playlist,
2116 char *sPlaylist = mpd_sanitizeArg(playlist);
2117 int len = strlen("playlistmove") + 2 + strlen(sPlaylist) + 3 + INTLEN +
2119 char *string = malloc(len);
2121 snprintf(string, len, "playlistmove \"%s\" \"%i\" \"%i\"\n", sPlaylist,
2123 mpd_executeCommand(connection, string);
2128 void mpd_sendPlaylistDeleteCommand(mpd_Connection *connection, char *playlist,
2131 char *sPlaylist = mpd_sanitizeArg(playlist);
2132 int len = strlen("playlistdelete") + 2 + strlen(sPlaylist) + 3 +
2134 char *string = malloc(len);
2136 snprintf(string, len, "playlistdelete \"%s\" \"%i\"\n", sPlaylist, pos);
2137 mpd_executeCommand(connection, string);