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>
43 #include <sys/param.h>
51 # include <ws2tcpip.h>
54 # include <netinet/in.h>
55 # include <arpa/inet.h>
56 # include <sys/socket.h>
60 /* (bits + 1) / 3 (plus the sign character) */
61 #define INTLEN ((sizeof(int) * CHAR_BIT + 1) / 3 + 1)
62 #define LONGLONGLEN ((sizeof(long long) * CHAR_BIT + 1) / 3 + 1)
64 #define COMMAND_LIST 1
65 #define COMMAND_LIST_OK 2
74 # define MSG_DONTWAIT 0
78 # define SELECT_ERRNO_IGNORE (errno == WSAEINTR || errno == WSAEINPROGRESS)
79 # define SENDRECV_ERRNO_IGNORE SELECT_ERRNO_IGNORE
81 # define SELECT_ERRNO_IGNORE (errno == EINTR)
82 # define SENDRECV_ERRNO_IGNORE (errno == EINTR || errno == EAGAIN)
83 # define winsock_dll_error(c) 0
84 # define closesocket(s) close(s)
85 # define WSACleanup() do { /* nothing */ } while (0)
89 static int winsock_dll_error(mpd_Connection *connection)
93 if ((WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0
94 || LOBYTE(wsaData.wVersion) != 2
95 || HIBYTE(wsaData.wVersion) != 2) {
96 strcpy(connection->errorStr, "Could not find usable WinSock DLL.");
97 connection->error = MPD_ERROR_SYSTEM;
103 static int do_connect_fail(mpd_Connection *connection,
104 const struct sockaddr *serv_addr, int addrlen)
106 int iMode = 1; /* 0 = blocking, else non-blocking */
108 ioctlsocket(connection->sock, FIONBIO, (u_long FAR *) &iMode);
109 return (connect(connection->sock, serv_addr, addrlen) == SOCKET_ERROR
110 && WSAGetLastError() != WSAEWOULDBLOCK);
112 #else /* !WIN32 (sane operating systems) */
113 static int do_connect_fail(mpd_Connection *connection,
114 const struct sockaddr *serv_addr, int addrlen)
116 int flags = fcntl(connection->sock, F_GETFL, 0);
118 fcntl(connection->sock, F_SETFL, flags | O_NONBLOCK);
119 return (connect(connection->sock, serv_addr, addrlen) < 0
120 && errno != EINPROGRESS);
125 static int mpd_connect(mpd_Connection *connection, const char *host, int port,
129 char service[INTLEN + 1];
130 struct addrinfo hints;
131 struct addrinfo *res = NULL;
132 struct addrinfo *addrinfo = NULL;
135 hints.ai_flags = AI_ADDRCONFIG;
136 hints.ai_family = PF_UNSPEC;
137 hints.ai_socktype = SOCK_STREAM;
138 hints.ai_protocol = IPPROTO_TCP;
139 hints.ai_addrlen = 0;
140 hints.ai_addr = NULL;
141 hints.ai_canonname = NULL;
142 hints.ai_next = NULL;
144 snprintf(service, sizeof(service), "%i", port);
146 error = getaddrinfo(host, service, &hints, &addrinfo);
149 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
150 "host \"%s\" not found: %s", host, gai_strerror(error));
151 connection->error = MPD_ERROR_UNKHOST;
155 for (res = addrinfo; res; res = res->ai_next) {
157 if (connection->sock > -1) {
158 closesocket(connection->sock);
160 connection->sock = socket(res->ai_family, SOCK_STREAM,
162 if (connection->sock < 0) {
163 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
164 "problems creating socket: %s", strerror(errno));
165 connection->error = MPD_ERROR_SYSTEM;
166 freeaddrinfo(addrinfo);
170 mpd_setConnectionTimeout(connection, timeout);
173 if (do_connect_fail(connection, res->ai_addr, res->ai_addrlen)) {
174 /* try the next address family */
175 closesocket(connection->sock);
176 connection->sock = -1;
181 freeaddrinfo(addrinfo);
183 if (connection->sock < 0) {
184 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
185 "problems connecting to \"%s\" on port %i: %s", host, port,
187 connection->error = MPD_ERROR_CONNPORT;
194 #else /* !MPD_HAVE_GAI */
195 static int mpd_connect(mpd_Connection *connection, const char *host, int port,
198 struct hostent he, *he_res = 0;
201 struct sockaddr *dest;
203 struct sockaddr_in sin;
205 #ifdef HAVE_GETHOSTBYNAME_R
206 if (gethostbyname_r(rhost, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
207 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
208 "%s ('%s')", hstrerror(h_errno), host);
209 connection->error = MPD_ERROR_UNKHOST;
212 #else /* HAVE_GETHOSTBYNAME_R */
213 if (!(he_res = gethostbyname(host))) {
214 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
215 "host \"%s\" not found", host);
216 connection->error = MPD_ERROR_UNKHOST;
219 #endif /* HAVE_GETHOSTBYNAME_R */
221 memset(&sin, 0, sizeof(struct sockaddr_in));
222 /* dest.sin_family = he_res->h_addrtype; */
223 sin.sin_family = AF_INET;
224 sin.sin_port = htons(port);
226 switch (he_res->h_addrtype) {
228 memcpy((char *) &sin.sin_addr.s_addr, (char *) he_res->h_addr,
230 dest = (struct sockaddr *) &sin;
231 destlen = sizeof(struct sockaddr_in);
234 strcpy(connection->errorStr, "address type is not IPv4");
235 connection->error = MPD_ERROR_SYSTEM;
240 if (connection->sock > -1) {
241 closesocket(connection->sock);
243 if ((connection->sock = socket(dest->sa_family, SOCK_STREAM, 0)) < 0) {
244 strcpy(connection->errorStr, "problems creating socket");
245 connection->error = MPD_ERROR_SYSTEM;
249 mpd_setConnectionTimeout(connection, timeout);
252 if (do_connect_fail(connection, dest, destlen)) {
253 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
254 "problems connecting to \"%s\" on port %i", host, port);
255 connection->error = MPD_ERROR_CONNPORT;
261 #endif /* !MPD_HAVE_GAI */
263 const char *mpdTagItemKeys[MPD_TAG_NUM_OF_ITEM_TYPES] = {
279 static char *mpd_sanitizeArg(const char *arg)
283 register const char *c;
286 /* instead of counting in that loop above,
287 * just use a bit more memory and halve running time */
288 ret = malloc(strlen(arg) * 2 + 1);
292 for (i = strlen(arg) + 1; i != 0; --i) {
293 if (*c == '"' || *c == '\\') {
302 static mpd_ReturnElement *mpd_newReturnElement(const char *name,
305 mpd_ReturnElement *ret = malloc(sizeof(mpd_ReturnElement));
307 ret->name = strndup(name, text_buffer_size);
308 ret->value = strndup(value, text_buffer_size);
313 static void mpd_freeReturnElement(mpd_ReturnElement *re)
320 void mpd_setConnectionTimeout(mpd_Connection *connection, float timeout)
322 connection->timeout.tv_sec = (int) timeout;
323 connection->timeout.tv_usec =
324 (int) ((timeout - connection->timeout.tv_sec) * 1e6 + 0.5);
327 static int mpd_parseWelcome(mpd_Connection *connection, const char *host,
328 int port, /* char *rt, */ char *output)
334 if (strncmp(output, MPD_WELCOME_MESSAGE, strlen(MPD_WELCOME_MESSAGE))) {
335 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
336 "mpd not running on port %i on host \"%s\"", port, host);
337 connection->error = MPD_ERROR_NOTMPD;
341 tmp = &output[strlen(MPD_WELCOME_MESSAGE)];
343 for (i = 0; i < 3; i++) {
345 connection->version[i] = strtol(tmp, &test, 10);
348 if (!tmp || (test[0] != '.' && test[0] != '\0')) {
349 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
350 "error parsing version number at \"%s\"",
351 &output[strlen(MPD_WELCOME_MESSAGE)]);
352 connection->error = MPD_ERROR_NOTMPD;
361 mpd_Connection *mpd_newConnection(const char *host, int port, float timeout)
366 mpd_Connection *connection = malloc(sizeof(mpd_Connection));
370 strcpy(connection->buffer, "");
371 connection->buflen = 0;
372 connection->bufstart = 0;
373 strcpy(connection->errorStr, "");
374 connection->error = 0;
375 connection->doneProcessing = 0;
376 connection->commandList = 0;
377 connection->listOks = 0;
378 connection->doneListOk = 0;
379 connection->sock = -1;
380 connection->returnElement = NULL;
381 connection->request = NULL;
383 if (winsock_dll_error(connection)) {
387 if (mpd_connect(connection, host, port, timeout) < 0) {
391 while (!(rt = strstr(connection->buffer, "\n"))) {
392 tv.tv_sec = connection->timeout.tv_sec;
393 tv.tv_usec = connection->timeout.tv_usec;
395 FD_SET(connection->sock, &fds);
396 if ((err = select(connection->sock + 1, &fds, NULL, NULL, &tv)) == 1) {
399 readed = recv(connection->sock,
400 &(connection->buffer[connection->buflen]),
401 MPD_BUFFER_MAX_LENGTH - connection->buflen, 0);
403 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
404 "problems getting a response from \"%s\" on port %i : %s",
405 host, port, strerror(errno));
406 connection->error = MPD_ERROR_NORESPONSE;
409 connection->buflen += readed;
410 connection->buffer[connection->buflen] = '\0';
411 } else if (err < 0) {
412 if (SELECT_ERRNO_IGNORE) {
415 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
416 "problems connecting to \"%s\" on port %i", host, port);
417 connection->error = MPD_ERROR_CONNPORT;
420 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
421 "timeout in attempting to get a response from \"%s\" on "
422 "port %i", host, port);
423 connection->error = MPD_ERROR_NORESPONSE;
429 output = strndup(connection->buffer, text_buffer_size);
430 strcpy(connection->buffer, rt + 1);
431 connection->buflen = strlen(connection->buffer);
433 if (mpd_parseWelcome(connection, host, port, /* rt, */ output) == 0) {
434 connection->doneProcessing = 1;
442 void mpd_clearError(mpd_Connection *connection)
444 connection->error = 0;
445 connection->errorStr[0] = '\0';
448 void mpd_closeConnection(mpd_Connection *connection)
450 closesocket(connection->sock);
451 if (connection->returnElement) {
452 free(connection->returnElement);
454 if (connection->request) {
455 free(connection->request);
461 static void mpd_executeCommand(mpd_Connection *connection, const char *command)
466 const char *commandPtr = command;
467 int commandLen = strlen(command);
469 if (!connection->doneProcessing && !connection->commandList) {
470 strcpy(connection->errorStr, "not done processing current command");
471 connection->error = 1;
475 mpd_clearError(connection);
478 FD_SET(connection->sock, &fds);
479 tv.tv_sec = connection->timeout.tv_sec;
480 tv.tv_usec = connection->timeout.tv_usec;
482 while ((ret = select(connection->sock + 1, NULL, &fds, NULL, &tv) == 1)
483 || (ret == -1 && SELECT_ERRNO_IGNORE)) {
484 ret = send(connection->sock, commandPtr, commandLen, MSG_DONTWAIT);
486 if (SENDRECV_ERRNO_IGNORE) {
489 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
490 "problems giving command \"%s\"", command);
491 connection->error = MPD_ERROR_SENDING;
498 if (commandLen <= 0) {
503 if (commandLen > 0) {
505 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
506 "timeout sending command \"%s\"", command);
507 connection->error = MPD_ERROR_TIMEOUT;
511 if (!connection->commandList) {
512 connection->doneProcessing = 0;
513 } else if (connection->commandList == COMMAND_LIST_OK) {
514 connection->listOks++;
518 static void mpd_getNextReturnElement(mpd_Connection *connection)
528 char *bufferCheck = NULL;
532 if (connection->returnElement) {
533 mpd_freeReturnElement(connection->returnElement);
535 connection->returnElement = NULL;
537 if (connection->doneProcessing
538 || (connection->listOks && connection->doneListOk)) {
539 strcpy(connection->errorStr,
540 "already done processing current command");
541 connection->error = 1;
545 bufferCheck = connection->buffer + connection->bufstart;
546 while (connection->bufstart >= connection->buflen
547 || !(rt = strchr(bufferCheck, '\n'))) {
548 if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
549 memmove(connection->buffer,
550 connection->buffer + connection->bufstart,
551 connection->buflen - connection->bufstart + 1);
552 connection->buflen -= connection->bufstart;
553 connection->bufstart = 0;
555 if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
556 strcpy(connection->errorStr, "buffer overrun");
557 connection->error = MPD_ERROR_BUFFEROVERRUN;
558 connection->doneProcessing = 1;
559 connection->doneListOk = 0;
562 bufferCheck = connection->buffer + connection->buflen;
563 tv.tv_sec = connection->timeout.tv_sec;
564 tv.tv_usec = connection->timeout.tv_usec;
566 FD_SET(connection->sock, &fds);
567 if ((err = select(connection->sock + 1, &fds, NULL, NULL, &tv) == 1)) {
568 readed = recv(connection->sock,
569 connection->buffer + connection->buflen,
570 MPD_BUFFER_MAX_LENGTH - connection->buflen, MSG_DONTWAIT);
571 if (readed < 0 && SENDRECV_ERRNO_IGNORE) {
575 strcpy(connection->errorStr, "connection closed");
576 connection->error = MPD_ERROR_CONNCLOSED;
577 connection->doneProcessing = 1;
578 connection->doneListOk = 0;
581 connection->buflen += readed;
582 connection->buffer[connection->buflen] = '\0';
583 } else if (err < 0 && SELECT_ERRNO_IGNORE) {
586 strcpy(connection->errorStr, "connection timeout");
587 connection->error = MPD_ERROR_TIMEOUT;
588 connection->doneProcessing = 1;
589 connection->doneListOk = 0;
595 output = connection->buffer + connection->bufstart;
596 connection->bufstart = rt - connection->buffer + 1;
598 if (strcmp(output, "OK") == 0) {
599 if (connection->listOks > 0) {
600 strcpy(connection->errorStr, "expected more list_OK's");
601 connection->error = 1;
603 connection->listOks = 0;
604 connection->doneProcessing = 1;
605 connection->doneListOk = 0;
609 if (strcmp(output, "list_OK") == 0) {
610 if (!connection->listOks) {
611 strcpy(connection->errorStr, "got an unexpected list_OK");
612 connection->error = 1;
614 connection->doneListOk = 1;
615 connection->listOks--;
620 if (strncmp(output, "ACK", strlen("ACK")) == 0) {
625 strcpy(connection->errorStr, output);
626 connection->error = MPD_ERROR_ACK;
627 connection->errorCode = MPD_ACK_ERROR_UNK;
628 connection->errorAt = MPD_ERROR_AT_UNK;
629 connection->doneProcessing = 1;
630 connection->doneListOk = 0;
632 needle = strchr(output, '[');
636 val = strtol(needle + 1, &test, 10);
640 connection->errorCode = val;
641 val = strtol(test + 1, &test, 10);
645 connection->errorAt = val;
649 tok = strchr(output, ':');
658 if (value[0] == ' ') {
659 connection->returnElement = mpd_newReturnElement(name, &(value[1]));
661 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
662 "error parsing: %s:%s", name, value);
663 connection->error = 1;
667 void mpd_finishCommand(mpd_Connection *connection)
669 while (!connection->doneProcessing) {
670 if (connection->doneListOk) {
671 connection->doneListOk = 0;
673 mpd_getNextReturnElement(connection);
677 static void mpd_finishListOkCommand(mpd_Connection *connection)
679 while (!connection->doneProcessing && connection->listOks
680 && !connection->doneListOk) {
681 mpd_getNextReturnElement(connection);
685 int mpd_nextListOkCommand(mpd_Connection *connection)
687 mpd_finishListOkCommand(connection);
688 if (!connection->doneProcessing) {
689 connection->doneListOk = 0;
691 if (connection->listOks == 0 || connection->doneProcessing) {
697 void mpd_sendStatusCommand(mpd_Connection *connection)
699 mpd_executeCommand(connection, "status\n");
702 mpd_Status *mpd_getStatus(mpd_Connection *connection)
706 /* mpd_executeCommand(connection, "status\n");
707 if (connection->error) {
711 if (connection->doneProcessing
712 || (connection->listOks && connection->doneListOk)) {
716 if (!connection->returnElement) {
717 mpd_getNextReturnElement(connection);
720 status = malloc(sizeof(mpd_Status));
724 status->playlist = -1;
725 status->playlistLength = -1;
729 status->elapsedTime = 0;
730 status->totalTime = 0;
732 status->sampleRate = 0;
734 status->channels = 0;
735 status->crossfade = -1;
736 status->error = NULL;
737 status->updatingDb = 0;
739 if (connection->error) {
743 while (connection->returnElement) {
744 mpd_ReturnElement *re = connection->returnElement;
746 if (strcmp(re->name, "volume") == 0) {
747 status->volume = atoi(re->value);
748 } else if (strcmp(re->name, "repeat") == 0) {
749 status->repeat = atoi(re->value);
750 } else if (strcmp(re->name, "random") == 0) {
751 status->random = atoi(re->value);
752 } else if (strcmp(re->name, "playlist") == 0) {
753 status->playlist = strtol(re->value, NULL, 10);
754 } else if (strcmp(re->name, "playlistlength") == 0) {
755 status->playlistLength = atoi(re->value);
756 } else if (strcmp(re->name, "bitrate") == 0) {
757 status->bitRate = atoi(re->value);
758 } else if (strcmp(re->name, "state") == 0) {
759 if (strcmp(re->value, "play") == 0) {
760 status->state = MPD_STATUS_STATE_PLAY;
761 } else if (strcmp(re->value, "stop") == 0) {
762 status->state = MPD_STATUS_STATE_STOP;
763 } else if (strcmp(re->value, "pause") == 0) {
764 status->state = MPD_STATUS_STATE_PAUSE;
766 status->state = MPD_STATUS_STATE_UNKNOWN;
768 } else if (strcmp(re->name, "song") == 0) {
769 status->song = atoi(re->value);
770 } else if (strcmp(re->name, "songid") == 0) {
771 status->songid = atoi(re->value);
772 } else if (strcmp(re->name, "time") == 0) {
773 char *tok = strchr(re->value, ':');
775 /* the second strchr below is a safety check */
776 if (tok && (strchr(tok, 0) > (tok + 1))) {
777 /* atoi stops at the first non-[0-9] char: */
778 status->elapsedTime = atoi(re->value);
779 status->totalTime = atoi(tok + 1);
781 } else if (strcmp(re->name, "error") == 0) {
782 status->error = strndup(re->value, text_buffer_size);
783 } else if (strcmp(re->name, "xfade") == 0) {
784 status->crossfade = atoi(re->value);
785 } else if (strcmp(re->name, "updating_db") == 0) {
786 status->updatingDb = atoi(re->value);
787 } else if (strcmp(re->name, "audio") == 0) {
788 char *tok = strchr(re->value, ':');
790 if (tok && (strchr(tok, 0) > (tok + 1))) {
791 status->sampleRate = atoi(re->value);
792 status->bits = atoi(++tok);
793 tok = strchr(tok, ':');
794 if (tok && (strchr(tok, 0) > (tok + 1))) {
795 status->channels = atoi(tok + 1);
800 mpd_getNextReturnElement(connection);
801 if (connection->error) {
807 if (connection->error) {
810 } else if (status->state < 0) {
811 strcpy(connection->errorStr, "state not found");
812 connection->error = 1;
820 void mpd_freeStatus(mpd_Status *status)
828 void mpd_sendStatsCommand(mpd_Connection *connection)
830 mpd_executeCommand(connection, "stats\n");
833 mpd_Stats *mpd_getStats(mpd_Connection *connection)
837 /* mpd_executeCommand(connection, "stats\n");
838 if (connection->error) {
842 if (connection->doneProcessing
843 || (connection->listOks && connection->doneListOk)) {
847 if (!connection->returnElement) {
848 mpd_getNextReturnElement(connection);
851 stats = malloc(sizeof(mpd_Stats));
852 stats->numberOfArtists = 0;
853 stats->numberOfAlbums = 0;
854 stats->numberOfSongs = 0;
856 stats->dbUpdateTime = 0;
858 stats->dbPlayTime = 0;
860 if (connection->error) {
864 while (connection->returnElement) {
865 mpd_ReturnElement *re = connection->returnElement;
867 if (strcmp(re->name, "artists") == 0) {
868 stats->numberOfArtists = atoi(re->value);
869 } else if (strcmp(re->name, "albums") == 0) {
870 stats->numberOfAlbums = atoi(re->value);
871 } else if (strcmp(re->name, "songs") == 0) {
872 stats->numberOfSongs = atoi(re->value);
873 } else if (strcmp(re->name, "uptime") == 0) {
874 stats->uptime = strtol(re->value, NULL, 10);
875 } else if (strcmp(re->name, "db_update") == 0) {
876 stats->dbUpdateTime = strtol(re->value, NULL, 10);
877 } else if (strcmp(re->name, "playtime") == 0) {
878 stats->playTime = strtol(re->value, NULL, 10);
879 } else if (strcmp(re->name, "db_playtime") == 0) {
880 stats->dbPlayTime = strtol(re->value, NULL, 10);
883 mpd_getNextReturnElement(connection);
884 if (connection->error) {
890 if (connection->error) {
898 void mpd_freeStats(mpd_Stats *stats)
903 mpd_SearchStats *mpd_getSearchStats(mpd_Connection *connection)
905 mpd_SearchStats *stats;
906 mpd_ReturnElement *re;
908 if (connection->doneProcessing
909 || (connection->listOks && connection->doneListOk)) {
913 if (!connection->returnElement) {
914 mpd_getNextReturnElement(connection);
917 if (connection->error) {
921 stats = malloc(sizeof(mpd_SearchStats));
922 stats->numberOfSongs = 0;
925 while (connection->returnElement) {
926 re = connection->returnElement;
928 if (strcmp(re->name, "songs") == 0) {
929 stats->numberOfSongs = atoi(re->value);
930 } else if (strcmp(re->name, "playtime") == 0) {
931 stats->playTime = strtol(re->value, NULL, 10);
934 mpd_getNextReturnElement(connection);
935 if (connection->error) {
941 if (connection->error) {
949 void mpd_freeSearchStats(mpd_SearchStats *stats)
954 static void mpd_initSong(mpd_Song *song)
965 song->composer = NULL;
966 song->performer = NULL;
968 song->comment = NULL;
970 song->time = MPD_SONG_NO_TIME;
971 song->pos = MPD_SONG_NO_NUM;
972 song->id = MPD_SONG_NO_ID;
975 static void mpd_finishSong(mpd_Song *song)
1001 if (song->composer) {
1002 free(song->composer);
1007 if (song->comment) {
1008 free(song->comment);
1012 mpd_Song *mpd_newSong(void)
1014 mpd_Song *ret = malloc(sizeof(mpd_Song));
1021 void mpd_freeSong(mpd_Song *song)
1023 mpd_finishSong(song);
1027 mpd_Song *mpd_songDup(mpd_Song *song)
1029 mpd_Song *ret = mpd_newSong();
1032 ret->file = strndup(song->file, text_buffer_size);
1035 ret->artist = strndup(song->artist, text_buffer_size);
1038 ret->album = strndup(song->album, text_buffer_size);
1041 ret->title = strndup(song->title, text_buffer_size);
1044 ret->track = strndup(song->track, text_buffer_size);
1047 ret->name = strndup(song->name, text_buffer_size);
1050 ret->date = strndup(song->date, text_buffer_size);
1053 ret->genre = strndup(song->genre, text_buffer_size);
1055 if (song->composer) {
1056 ret->composer = strndup(song->composer, text_buffer_size);
1059 ret->disc = strndup(song->disc, text_buffer_size);
1061 if (song->comment) {
1062 ret->comment = strndup(song->comment, text_buffer_size);
1064 ret->time = song->time;
1065 ret->pos = song->pos;
1071 static void mpd_initDirectory(mpd_Directory *directory)
1073 directory->path = NULL;
1076 static void mpd_finishDirectory(mpd_Directory *directory)
1078 if (directory->path) {
1079 free(directory->path);
1083 mpd_Directory *mpd_newDirectory(void)
1085 mpd_Directory *directory = malloc(sizeof(mpd_Directory));
1087 mpd_initDirectory(directory);
1092 void mpd_freeDirectory(mpd_Directory *directory)
1094 mpd_finishDirectory(directory);
1099 mpd_Directory *mpd_directoryDup(mpd_Directory *directory)
1101 mpd_Directory *ret = mpd_newDirectory();
1103 if (directory->path) {
1104 ret->path = strndup(directory->path, text_buffer_size);
1110 static void mpd_initPlaylistFile(mpd_PlaylistFile *playlist)
1112 playlist->path = NULL;
1115 static void mpd_finishPlaylistFile(mpd_PlaylistFile *playlist)
1117 if (playlist->path) {
1118 free(playlist->path);
1122 mpd_PlaylistFile *mpd_newPlaylistFile(void)
1124 mpd_PlaylistFile *playlist = malloc(sizeof(mpd_PlaylistFile));
1126 mpd_initPlaylistFile(playlist);
1131 void mpd_freePlaylistFile(mpd_PlaylistFile *playlist)
1133 mpd_finishPlaylistFile(playlist);
1137 mpd_PlaylistFile *mpd_playlistFileDup(mpd_PlaylistFile *playlist)
1139 mpd_PlaylistFile *ret = mpd_newPlaylistFile();
1141 if (playlist->path) {
1142 ret->path = strndup(playlist->path, text_buffer_size);
1148 static void mpd_initInfoEntity(mpd_InfoEntity *entity)
1150 entity->info.directory = NULL;
1153 static void mpd_finishInfoEntity(mpd_InfoEntity *entity)
1155 if (entity->info.directory) {
1156 if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1157 mpd_freeDirectory(entity->info.directory);
1158 } else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
1159 mpd_freeSong(entity->info.song);
1160 } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1161 mpd_freePlaylistFile(entity->info.playlistFile);
1166 mpd_InfoEntity *mpd_newInfoEntity(void)
1168 mpd_InfoEntity *entity = malloc(sizeof(mpd_InfoEntity));
1170 mpd_initInfoEntity(entity);
1175 void mpd_freeInfoEntity(mpd_InfoEntity *entity)
1177 mpd_finishInfoEntity(entity);
1181 static void mpd_sendInfoCommand(mpd_Connection *connection, char *command)
1183 mpd_executeCommand(connection, command);
1186 mpd_InfoEntity *mpd_getNextInfoEntity(mpd_Connection *connection)
1188 mpd_InfoEntity *entity = NULL;
1190 if (connection->doneProcessing
1191 || (connection->listOks && connection->doneListOk)) {
1195 if (!connection->returnElement) {
1196 mpd_getNextReturnElement(connection);
1199 if (connection->returnElement) {
1200 if (strcmp(connection->returnElement->name, "file") == 0) {
1201 entity = mpd_newInfoEntity();
1202 entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1203 entity->info.song = mpd_newSong();
1204 entity->info.song->file = strndup(connection->returnElement->value, text_buffer_size);
1205 } else if (strcmp(connection->returnElement->name, "directory") == 0) {
1206 entity = mpd_newInfoEntity();
1207 entity->type = MPD_INFO_ENTITY_TYPE_DIRECTORY;
1208 entity->info.directory = mpd_newDirectory();
1209 entity->info.directory->path =
1210 strndup(connection->returnElement->value, text_buffer_size);
1211 } else if (strcmp(connection->returnElement->name, "playlist") == 0) {
1212 entity = mpd_newInfoEntity();
1213 entity->type = MPD_INFO_ENTITY_TYPE_PLAYLISTFILE;
1214 entity->info.playlistFile = mpd_newPlaylistFile();
1215 entity->info.playlistFile->path =
1216 strndup(connection->returnElement->value, text_buffer_size);
1217 } else if (strcmp(connection->returnElement->name, "cpos") == 0) {
1218 entity = mpd_newInfoEntity();
1219 entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1220 entity->info.song = mpd_newSong();
1221 entity->info.song->pos = atoi(connection->returnElement->value);
1223 connection->error = 1;
1224 strcpy(connection->errorStr, "problem parsing song info");
1231 mpd_getNextReturnElement(connection);
1232 while (connection->returnElement) {
1233 mpd_ReturnElement *re = connection->returnElement;
1235 if (strcmp(re->name, "file") == 0) {
1237 } else if (strcmp(re->name, "directory") == 0) {
1239 } else if (strcmp(re->name, "playlist") == 0) {
1241 } else if (strcmp(re->name, "cpos") == 0) {
1245 if (entity->type == MPD_INFO_ENTITY_TYPE_SONG && strlen(re->value)) {
1246 if (!entity->info.song->artist
1247 && strcmp(re->name, "Artist") == 0) {
1248 entity->info.song->artist = strndup(re->value, text_buffer_size);
1249 } else if (!entity->info.song->album
1250 && strcmp(re->name, "Album") == 0) {
1251 entity->info.song->album = strndup(re->value, text_buffer_size);
1252 } else if (!entity->info.song->title
1253 && strcmp(re->name, "Title") == 0) {
1254 entity->info.song->title = strndup(re->value, text_buffer_size);
1255 } else if (!entity->info.song->track
1256 && strcmp(re->name, "Track") == 0) {
1257 entity->info.song->track = strndup(re->value, text_buffer_size);
1258 } else if (!entity->info.song->name
1259 && strcmp(re->name, "Name") == 0) {
1260 entity->info.song->name = strndup(re->value, text_buffer_size);
1261 } else if (entity->info.song->time == MPD_SONG_NO_TIME
1262 && strcmp(re->name, "Time") == 0) {
1263 entity->info.song->time = atoi(re->value);
1264 } else if (entity->info.song->pos == MPD_SONG_NO_NUM
1265 && strcmp(re->name, "Pos") == 0) {
1266 entity->info.song->pos = atoi(re->value);
1267 } else if (entity->info.song->id == MPD_SONG_NO_ID
1268 && strcmp(re->name, "Id") == 0) {
1269 entity->info.song->id = atoi(re->value);
1270 } else if (!entity->info.song->date
1271 && strcmp(re->name, "Date") == 0) {
1272 entity->info.song->date = strndup(re->value, text_buffer_size);
1273 } else if (!entity->info.song->genre
1274 && strcmp(re->name, "Genre") == 0) {
1275 entity->info.song->genre = strndup(re->value, text_buffer_size);
1276 } else if (!entity->info.song->composer
1277 && strcmp(re->name, "Composer") == 0) {
1278 entity->info.song->composer = strndup(re->value, text_buffer_size);
1279 } else if (!entity->info.song->performer
1280 && strcmp(re->name, "Performer") == 0) {
1281 entity->info.song->performer = strndup(re->value, text_buffer_size);
1282 } else if (!entity->info.song->disc
1283 && strcmp(re->name, "Disc") == 0) {
1284 entity->info.song->disc = strndup(re->value, text_buffer_size);
1285 } else if (!entity->info.song->comment
1286 && strcmp(re->name, "Comment") == 0) {
1287 entity->info.song->comment = strndup(re->value, text_buffer_size);
1289 } else if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1290 } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1293 mpd_getNextReturnElement(connection);
1299 static char *mpd_getNextReturnElementNamed(mpd_Connection *connection,
1302 if (connection->doneProcessing
1303 || (connection->listOks && connection->doneListOk)) {
1307 mpd_getNextReturnElement(connection);
1308 while (connection->returnElement) {
1309 mpd_ReturnElement *re = connection->returnElement;
1311 if (strcmp(re->name, name) == 0) {
1312 return strndup(re->value, text_buffer_size);
1314 mpd_getNextReturnElement(connection);
1320 char *mpd_getNextTag(mpd_Connection *connection, int type)
1322 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES
1323 || type == MPD_TAG_ITEM_ANY) {
1326 if (type == MPD_TAG_ITEM_FILENAME) {
1327 return mpd_getNextReturnElementNamed(connection, "file");
1329 return mpd_getNextReturnElementNamed(connection, mpdTagItemKeys[type]);
1332 char *mpd_getNextArtist(mpd_Connection *connection)
1334 return mpd_getNextReturnElementNamed(connection, "Artist");
1337 char *mpd_getNextAlbum(mpd_Connection *connection)
1339 return mpd_getNextReturnElementNamed(connection, "Album");
1342 void mpd_sendPlaylistInfoCommand(mpd_Connection *connection, int songPos)
1344 int len = strlen("playlistinfo") + 2 + INTLEN + 3;
1345 char *string = malloc(len);
1347 snprintf(string, len, "playlistinfo \"%i\"\n", songPos);
1348 mpd_sendInfoCommand(connection, string);
1352 void mpd_sendPlaylistIdCommand(mpd_Connection *connection, int id)
1354 int len = strlen("playlistid") + 2 + INTLEN + 3;
1355 char *string = malloc(len);
1357 snprintf(string, len, "playlistid \"%i\"\n", id);
1358 mpd_sendInfoCommand(connection, string);
1362 void mpd_sendPlChangesCommand(mpd_Connection *connection, long long playlist)
1364 int len = strlen("plchanges") + 2 + LONGLONGLEN + 3;
1365 char *string = malloc(len);
1367 snprintf(string, len, "plchanges \"%lld\"\n", playlist);
1368 mpd_sendInfoCommand(connection, string);
1372 void mpd_sendPlChangesPosIdCommand(mpd_Connection *connection,
1375 int len = strlen("plchangesposid") + 2 + LONGLONGLEN + 3;
1376 char *string = malloc(len);
1378 snprintf(string, len, "plchangesposid \"%lld\"\n", playlist);
1379 mpd_sendInfoCommand(connection, string);
1383 void mpd_sendListallCommand(mpd_Connection *connection, const char *dir)
1385 char *sDir = mpd_sanitizeArg(dir);
1386 int len = strlen("listall") + 2 + strlen(sDir) + 3;
1387 char *string = malloc(len);
1389 snprintf(string, len, "listall \"%s\"\n", sDir);
1390 mpd_sendInfoCommand(connection, string);
1395 void mpd_sendListallInfoCommand(mpd_Connection *connection, const char *dir)
1397 char *sDir = mpd_sanitizeArg(dir);
1398 int len = strlen("listallinfo") + 2 + strlen(sDir) + 3;
1399 char *string = malloc(len);
1401 snprintf(string, len, "listallinfo \"%s\"\n", sDir);
1402 mpd_sendInfoCommand(connection, string);
1407 void mpd_sendLsInfoCommand(mpd_Connection *connection, const char *dir)
1409 char *sDir = mpd_sanitizeArg(dir);
1410 int len = strlen("lsinfo") + 2 + strlen(sDir) + 3;
1411 char *string = malloc(len);
1413 snprintf(string, len, "lsinfo \"%s\"\n", sDir);
1414 mpd_sendInfoCommand(connection, string);
1419 void mpd_sendCurrentSongCommand(mpd_Connection *connection)
1421 mpd_executeCommand(connection, "currentsong\n");
1424 void mpd_sendSearchCommand(mpd_Connection *connection, int table,
1427 mpd_startSearch(connection, 0);
1428 mpd_addConstraintSearch(connection, table, str);
1429 mpd_commitSearch(connection);
1432 void mpd_sendFindCommand(mpd_Connection *connection, int table,
1435 mpd_startSearch(connection, 1);
1436 mpd_addConstraintSearch(connection, table, str);
1437 mpd_commitSearch(connection);
1440 void mpd_sendListCommand(mpd_Connection *connection, int table,
1447 if (table == MPD_TABLE_ARTIST) {
1448 strcpy(st, "artist");
1449 } else if (table == MPD_TABLE_ALBUM) {
1450 strcpy(st, "album");
1452 connection->error = 1;
1453 strcpy(connection->errorStr, "unknown table for list");
1457 char *sanitArg1 = mpd_sanitizeArg(arg1);
1459 len = strlen("list") + 1 + strlen(sanitArg1) + 2 + strlen(st) + 3;
1460 string = malloc(len);
1461 snprintf(string, len, "list %s \"%s\"\n", st, sanitArg1);
1464 len = strlen("list") + 1 + strlen(st) + 2;
1465 string = malloc(len);
1466 snprintf(string, len, "list %s\n", st);
1468 mpd_sendInfoCommand(connection, string);
1472 void mpd_sendAddCommand(mpd_Connection *connection, const char *file)
1474 char *sFile = mpd_sanitizeArg(file);
1475 int len = strlen("add") + 2 + strlen(sFile) + 3;
1476 char *string = malloc(len);
1478 snprintf(string, len, "add \"%s\"\n", sFile);
1479 mpd_executeCommand(connection, string);
1484 int mpd_sendAddIdCommand(mpd_Connection *connection, const char *file)
1487 char *sFile = mpd_sanitizeArg(file);
1488 int len = strlen("addid") + 2 + strlen(sFile) + 3;
1489 char *string = malloc(len);
1491 snprintf(string, len, "addid \"%s\"\n", sFile);
1492 mpd_sendInfoCommand(connection, string);
1496 string = mpd_getNextReturnElementNamed(connection, "Id");
1498 retval = atoi(string);
1505 void mpd_sendDeleteCommand(mpd_Connection *connection, int songPos)
1507 int len = strlen("delete") + 2 + INTLEN + 3;
1508 char *string = malloc(len);
1510 snprintf(string, len, "delete \"%i\"\n", songPos);
1511 mpd_sendInfoCommand(connection, string);
1515 void mpd_sendDeleteIdCommand(mpd_Connection *connection, int id)
1517 int len = strlen("deleteid") + 2 + INTLEN + 3;
1518 char *string = malloc(len);
1520 snprintf(string, len, "deleteid \"%i\"\n", id);
1521 mpd_sendInfoCommand(connection, string);
1525 void mpd_sendSaveCommand(mpd_Connection *connection, const char *name)
1527 char *sName = mpd_sanitizeArg(name);
1528 int len = strlen("save") + 2 + strlen(sName) + 3;
1529 char *string = malloc(len);
1531 snprintf(string, len, "save \"%s\"\n", sName);
1532 mpd_executeCommand(connection, string);
1537 void mpd_sendLoadCommand(mpd_Connection *connection, const char *name)
1539 char *sName = mpd_sanitizeArg(name);
1540 int len = strlen("load") + 2 + strlen(sName) + 3;
1541 char *string = malloc(len);
1543 snprintf(string, len, "load \"%s\"\n", sName);
1544 mpd_executeCommand(connection, string);
1549 void mpd_sendRmCommand(mpd_Connection *connection, const char *name)
1551 char *sName = mpd_sanitizeArg(name);
1552 int len = strlen("rm") + 2 + strlen(sName) + 3;
1553 char *string = malloc(len);
1555 snprintf(string, len, "rm \"%s\"\n", sName);
1556 mpd_executeCommand(connection, string);
1561 void mpd_sendRenameCommand(mpd_Connection *connection, const char *from,
1564 char *sFrom = mpd_sanitizeArg(from);
1565 char *sTo = mpd_sanitizeArg(to);
1566 int len = strlen("rename") + 2 + strlen(sFrom) + 3 + strlen(sTo) + 3;
1567 char *string = malloc(len);
1569 snprintf(string, len, "rename \"%s\" \"%s\"\n", sFrom, sTo);
1570 mpd_executeCommand(connection, string);
1576 void mpd_sendShuffleCommand(mpd_Connection *connection)
1578 mpd_executeCommand(connection, "shuffle\n");
1581 void mpd_sendClearCommand(mpd_Connection *connection)
1583 mpd_executeCommand(connection, "clear\n");
1586 void mpd_sendPlayCommand(mpd_Connection *connection, int songPos)
1588 int len = strlen("play") + 2 + INTLEN + 3;
1589 char *string = malloc(len);
1591 snprintf(string, len, "play \"%i\"\n", songPos);
1592 mpd_sendInfoCommand(connection, string);
1596 void mpd_sendPlayIdCommand(mpd_Connection *connection, int id)
1598 int len = strlen("playid") + 2 + INTLEN + 3;
1599 char *string = malloc(len);
1601 snprintf(string, len, "playid \"%i\"\n", id);
1602 mpd_sendInfoCommand(connection, string);
1606 void mpd_sendStopCommand(mpd_Connection *connection)
1608 mpd_executeCommand(connection, "stop\n");
1611 void mpd_sendPauseCommand(mpd_Connection *connection, int pauseMode)
1613 int len = strlen("pause") + 2 + INTLEN + 3;
1614 char *string = malloc(len);
1616 snprintf(string, len, "pause \"%i\"\n", pauseMode);
1617 mpd_executeCommand(connection, string);
1621 void mpd_sendNextCommand(mpd_Connection *connection)
1623 mpd_executeCommand(connection, "next\n");
1626 void mpd_sendMoveCommand(mpd_Connection *connection, int from, int to)
1628 int len = strlen("move") + 2 + INTLEN + 3 + INTLEN + 3;
1629 char *string = malloc(len);
1631 snprintf(string, len, "move \"%i\" \"%i\"\n", from, to);
1632 mpd_sendInfoCommand(connection, string);
1636 void mpd_sendMoveIdCommand(mpd_Connection *connection, int id, int to)
1638 int len = strlen("moveid") + 2 + INTLEN + 3 + INTLEN + 3;
1639 char *string = malloc(len);
1641 snprintf(string, len, "moveid \"%i\" \"%i\"\n", id, to);
1642 mpd_sendInfoCommand(connection, string);
1646 void mpd_sendSwapCommand(mpd_Connection *connection, int song1, int song2)
1648 int len = strlen("swap") + 2 + INTLEN + 3 + INTLEN + 3;
1649 char *string = malloc(len);
1651 snprintf(string, len, "swap \"%i\" \"%i\"\n", song1, song2);
1652 mpd_sendInfoCommand(connection, string);
1656 void mpd_sendSwapIdCommand(mpd_Connection *connection, int id1, int id2)
1658 int len = strlen("swapid") + 2 + INTLEN + 3 + INTLEN + 3;
1659 char *string = malloc(len);
1661 snprintf(string, len, "swapid \"%i\" \"%i\"\n", id1, id2);
1662 mpd_sendInfoCommand(connection, string);
1666 void mpd_sendSeekCommand(mpd_Connection *connection, int song, int seek_time)
1668 int len = strlen("seek") + 2 + INTLEN + 3 + INTLEN + 3;
1669 char *string = malloc(len);
1671 snprintf(string, len, "seek \"%i\" \"%i\"\n", song, seek_time);
1672 mpd_sendInfoCommand(connection, string);
1676 void mpd_sendSeekIdCommand(mpd_Connection *connection, int id, int seek_time)
1678 int len = strlen("seekid") + 2 + INTLEN + 3 + INTLEN + 3;
1679 char *string = malloc(len);
1681 snprintf(string, len, "seekid \"%i\" \"%i\"\n", id, seek_time);
1682 mpd_sendInfoCommand(connection, string);
1686 void mpd_sendUpdateCommand(mpd_Connection *connection, char *path)
1688 char *sPath = mpd_sanitizeArg(path);
1689 int len = strlen("update") + 2 + strlen(sPath) + 3;
1690 char *string = malloc(len);
1692 snprintf(string, len, "update \"%s\"\n", sPath);
1693 mpd_sendInfoCommand(connection, string);
1698 int mpd_getUpdateId(mpd_Connection *connection)
1703 jobid = mpd_getNextReturnElementNamed(connection, "updating_db");
1712 void mpd_sendPrevCommand(mpd_Connection *connection)
1714 mpd_executeCommand(connection, "previous\n");
1717 void mpd_sendRepeatCommand(mpd_Connection *connection, int repeatMode)
1719 int len = strlen("repeat") + 2 + INTLEN + 3;
1720 char *string = malloc(len);
1722 snprintf(string, len, "repeat \"%i\"\n", repeatMode);
1723 mpd_executeCommand(connection, string);
1727 void mpd_sendRandomCommand(mpd_Connection *connection, int randomMode)
1729 int len = strlen("random") + 2 + INTLEN + 3;
1730 char *string = malloc(len);
1732 snprintf(string, len, "random \"%i\"\n", randomMode);
1733 mpd_executeCommand(connection, string);
1737 void mpd_sendSetvolCommand(mpd_Connection *connection, int volumeChange)
1739 int len = strlen("setvol") + 2 + INTLEN + 3;
1740 char *string = malloc(len);
1742 snprintf(string, len, "setvol \"%i\"\n", volumeChange);
1743 mpd_executeCommand(connection, string);
1747 void mpd_sendVolumeCommand(mpd_Connection *connection, int volumeChange)
1749 int len = strlen("volume") + 2 + INTLEN + 3;
1750 char *string = malloc(len);
1752 snprintf(string, len, "volume \"%i\"\n", volumeChange);
1753 mpd_executeCommand(connection, string);
1757 void mpd_sendCrossfadeCommand(mpd_Connection *connection, int seconds)
1759 int len = strlen("crossfade") + 2 + INTLEN + 3;
1760 char *string = malloc(len);
1762 snprintf(string, len, "crossfade \"%i\"\n", seconds);
1763 mpd_executeCommand(connection, string);
1767 void mpd_sendPasswordCommand(mpd_Connection *connection, const char *pass)
1769 char *sPass = mpd_sanitizeArg(pass);
1770 int len = strlen("password") + 2 + strlen(sPass) + 3;
1771 char *string = malloc(len);
1773 snprintf(string, len, "password \"%s\"\n", sPass);
1774 mpd_executeCommand(connection, string);
1779 void mpd_sendCommandListBegin(mpd_Connection *connection)
1781 if (connection->commandList) {
1782 strcpy(connection->errorStr, "already in command list mode");
1783 connection->error = 1;
1786 connection->commandList = COMMAND_LIST;
1787 mpd_executeCommand(connection, "command_list_begin\n");
1790 void mpd_sendCommandListOkBegin(mpd_Connection *connection)
1792 if (connection->commandList) {
1793 strcpy(connection->errorStr, "already in command list mode");
1794 connection->error = 1;
1797 connection->commandList = COMMAND_LIST_OK;
1798 mpd_executeCommand(connection, "command_list_ok_begin\n");
1799 connection->listOks = 0;
1802 void mpd_sendCommandListEnd(mpd_Connection *connection)
1804 if (!connection->commandList) {
1805 strcpy(connection->errorStr, "not in command list mode");
1806 connection->error = 1;
1809 connection->commandList = 0;
1810 mpd_executeCommand(connection, "command_list_end\n");
1813 void mpd_sendOutputsCommand(mpd_Connection *connection)
1815 mpd_executeCommand(connection, "outputs\n");
1818 mpd_OutputEntity *mpd_getNextOutput(mpd_Connection *connection)
1820 mpd_OutputEntity *output = NULL;
1822 if (connection->doneProcessing
1823 || (connection->listOks && connection->doneListOk)) {
1827 if (connection->error) {
1831 output = malloc(sizeof(mpd_OutputEntity));
1833 output->name = NULL;
1834 output->enabled = 0;
1836 if (!connection->returnElement) {
1837 mpd_getNextReturnElement(connection);
1840 while (connection->returnElement) {
1841 mpd_ReturnElement *re = connection->returnElement;
1843 if (strcmp(re->name, "outputid") == 0) {
1844 if (output != NULL && output->id >= 0) {
1847 output->id = atoi(re->value);
1848 } else if (strcmp(re->name, "outputname") == 0) {
1849 output->name = strndup(re->value, text_buffer_size);
1850 } else if (strcmp(re->name, "outputenabled") == 0) {
1851 output->enabled = atoi(re->value);
1854 mpd_getNextReturnElement(connection);
1855 if (connection->error) {
1864 void mpd_sendEnableOutputCommand(mpd_Connection *connection, int outputId)
1866 int len = strlen("enableoutput") + 2 + INTLEN + 3;
1867 char *string = malloc(len);
1869 snprintf(string, len, "enableoutput \"%i\"\n", outputId);
1870 mpd_executeCommand(connection, string);
1874 void mpd_sendDisableOutputCommand(mpd_Connection *connection, int outputId)
1876 int len = strlen("disableoutput") + 2 + INTLEN + 3;
1877 char *string = malloc(len);
1879 snprintf(string, len, "disableoutput \"%i\"\n", outputId);
1880 mpd_executeCommand(connection, string);
1884 void mpd_freeOutputElement(mpd_OutputEntity *output)
1890 /** odd naming, but it gets the not allowed commands */
1891 void mpd_sendNotCommandsCommand(mpd_Connection *connection)
1893 mpd_executeCommand(connection, "notcommands\n");
1896 /** odd naming, but it gets the allowed commands */
1897 void mpd_sendCommandsCommand(mpd_Connection *connection)
1899 mpd_executeCommand(connection, "commands\n");
1902 /** Get the next returned command */
1903 char *mpd_getNextCommand(mpd_Connection *connection)
1905 return mpd_getNextReturnElementNamed(connection, "command");
1908 void mpd_sendUrlHandlersCommand(mpd_Connection *connection)
1910 mpd_executeCommand(connection, "urlhandlers\n");
1913 char *mpd_getNextHandler(mpd_Connection *connection)
1915 return mpd_getNextReturnElementNamed(connection, "handler");
1918 void mpd_sendTagTypesCommand(mpd_Connection *connection)
1920 mpd_executeCommand(connection, "tagtypes\n");
1923 char *mpd_getNextTagType(mpd_Connection *connection)
1925 return mpd_getNextReturnElementNamed(connection, "tagtype");
1928 void mpd_startSearch(mpd_Connection *connection, int exact)
1930 if (connection->request) {
1931 strcpy(connection->errorStr, "search already in progress");
1932 connection->error = 1;
1937 connection->request = strndup("find", text_buffer_size);
1939 connection->request = strndup("search", text_buffer_size);
1943 void mpd_startStatsSearch(mpd_Connection *connection)
1945 if (connection->request) {
1946 strcpy(connection->errorStr, "search already in progress");
1947 connection->error = 1;
1951 connection->request = strndup("count", text_buffer_size);
1954 void mpd_startPlaylistSearch(mpd_Connection *connection, int exact)
1956 if (connection->request) {
1957 strcpy(connection->errorStr, "search already in progress");
1958 connection->error = 1;
1963 connection->request = strndup("playlistfind", text_buffer_size);
1965 connection->request = strndup("playlistsearch", text_buffer_size);
1969 void mpd_startFieldSearch(mpd_Connection *connection, int type)
1971 const char *strtype;
1974 if (connection->request) {
1975 strcpy(connection->errorStr, "search already in progress");
1976 connection->error = 1;
1980 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
1981 strcpy(connection->errorStr, "invalid type specified");
1982 connection->error = 1;
1986 strtype = mpdTagItemKeys[type];
1988 len = 5 + strlen(strtype) + 1;
1989 connection->request = malloc(len);
1991 snprintf(connection->request, len, "list %c%s", tolower(strtype[0]),
1995 void mpd_addConstraintSearch(mpd_Connection *connection, int type,
1998 const char *strtype;
2003 if (!connection->request) {
2004 strcpy(connection->errorStr, "no search in progress");
2005 connection->error = 1;
2009 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
2010 strcpy(connection->errorStr, "invalid type specified");
2011 connection->error = 1;
2016 strcpy(connection->errorStr, "no name specified");
2017 connection->error = 1;
2021 string = strndup(connection->request, text_buffer_size);
2022 strtype = mpdTagItemKeys[type];
2023 arg = mpd_sanitizeArg(name);
2025 len = strlen(string) + 1 + strlen(strtype) + 2 + strlen(arg) + 2;
2026 connection->request = realloc(connection->request, len);
2027 snprintf(connection->request, len, "%s %c%s \"%s\"", string,
2028 tolower(strtype[0]), strtype + 1, arg);
2034 void mpd_commitSearch(mpd_Connection *connection)
2038 if (!connection->request) {
2039 strcpy(connection->errorStr, "no search in progress");
2040 connection->error = 1;
2044 len = strlen(connection->request) + 2;
2045 connection->request = realloc(connection->request, len);
2046 connection->request[len - 2] = '\n';
2047 connection->request[len - 1] = '\0';
2048 mpd_sendInfoCommand(connection, connection->request);
2050 free(connection->request);
2051 connection->request = NULL;
2055 * @param connection a MpdConnection
2056 * @param path the path to the playlist.
2058 * List the content, with full metadata, of a stored playlist. */
2059 void mpd_sendListPlaylistInfoCommand(mpd_Connection *connection, char *path)
2061 char *arg = mpd_sanitizeArg(path);
2062 int len = strlen("listplaylistinfo") + 2 + strlen(arg) + 3;
2063 char *query = malloc(len);
2065 snprintf(query, len, "listplaylistinfo \"%s\"\n", arg);
2066 mpd_sendInfoCommand(connection, query);
2072 * @param connection a MpdConnection
2073 * @param path the path to the playlist.
2075 * List the content of a stored playlist. */
2076 void mpd_sendListPlaylistCommand(mpd_Connection *connection, char *path)
2078 char *arg = mpd_sanitizeArg(path);
2079 int len = strlen("listplaylist") + 2 + strlen(arg) + 3;
2080 char *query = malloc(len);
2082 snprintf(query, len, "listplaylist \"%s\"\n", arg);
2083 mpd_sendInfoCommand(connection, query);
2088 void mpd_sendPlaylistClearCommand(mpd_Connection *connection, char *path)
2090 char *sPath = mpd_sanitizeArg(path);
2091 int len = strlen("playlistclear") + 2 + strlen(sPath) + 3;
2092 char *string = malloc(len);
2094 snprintf(string, len, "playlistclear \"%s\"\n", sPath);
2095 mpd_executeCommand(connection, string);
2100 void mpd_sendPlaylistAddCommand(mpd_Connection *connection, char *playlist,
2103 char *sPlaylist = mpd_sanitizeArg(playlist);
2104 char *sPath = mpd_sanitizeArg(path);
2105 int len = strlen("playlistadd") + 2 + strlen(sPlaylist) + 3 +
2107 char *string = malloc(len);
2109 snprintf(string, len, "playlistadd \"%s\" \"%s\"\n", sPlaylist, sPath);
2110 mpd_executeCommand(connection, string);
2116 void mpd_sendPlaylistMoveCommand(mpd_Connection *connection, char *playlist,
2119 char *sPlaylist = mpd_sanitizeArg(playlist);
2120 int len = strlen("playlistmove") + 2 + strlen(sPlaylist) + 3 + INTLEN +
2122 char *string = malloc(len);
2124 snprintf(string, len, "playlistmove \"%s\" \"%i\" \"%i\"\n", sPlaylist,
2126 mpd_executeCommand(connection, string);
2131 void mpd_sendPlaylistDeleteCommand(mpd_Connection *connection, char *playlist,
2134 char *sPlaylist = mpd_sanitizeArg(playlist);
2135 int len = strlen("playlistdelete") + 2 + strlen(sPlaylist) + 3 +
2137 char *string = malloc(len);
2139 snprintf(string, len, "playlistdelete \"%s\" \"%i\"\n", sPlaylist, pos);
2140 mpd_executeCommand(connection, string);