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.
32 * vim: ts=4 sw=4 noet ai cindent syntax=c
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;
133 * XXX: limit address family to PF_INET here.
134 * MPD does not support IPv6 yet, so if GAI returns
135 * an IPv6 address, the later connect() will fail. */
136 hints.ai_flags = AI_ADDRCONFIG;
137 hints.ai_family = PF_INET;
138 hints.ai_socktype = SOCK_STREAM;
139 hints.ai_protocol = IPPROTO_TCP;
140 hints.ai_addrlen = 0;
141 hints.ai_addr = NULL;
142 hints.ai_canonname = NULL;
143 hints.ai_next = NULL;
145 snprintf(service, sizeof(service), "%i", port);
147 error = getaddrinfo(host, service, &hints, &addrinfo);
150 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
151 "host \"%s\" not found: %s", host, gai_strerror(error));
152 connection->error = MPD_ERROR_UNKHOST;
156 for (res = addrinfo; res; res = res->ai_next) {
158 if (connection->sock > -1) {
159 closesocket(connection->sock);
161 connection->sock = socket(res->ai_family, SOCK_STREAM,
163 if (connection->sock < 0) {
164 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
165 "problems creating socket: %s", strerror(errno));
166 connection->error = MPD_ERROR_SYSTEM;
167 freeaddrinfo(addrinfo);
171 mpd_setConnectionTimeout(connection, timeout);
174 if (do_connect_fail(connection, res->ai_addr, res->ai_addrlen)) {
175 /* try the next address family */
176 closesocket(connection->sock);
177 connection->sock = -1;
182 freeaddrinfo(addrinfo);
184 if (connection->sock < 0) {
185 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
186 "problems connecting to \"%s\" on port %i: %s", host, port,
188 connection->error = MPD_ERROR_CONNPORT;
195 #else /* !MPD_HAVE_GAI */
196 static int mpd_connect(mpd_Connection *connection, const char *host, int port,
199 struct hostent he, *he_res = 0;
202 struct sockaddr *dest;
204 struct sockaddr_in sin;
206 #ifdef HAVE_GETHOSTBYNAME_R
207 if (gethostbyname_r(rhost, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
208 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
209 "%s ('%s')", hstrerror(h_errno), host);
210 connection->error = MPD_ERROR_UNKHOST;
213 #else /* HAVE_GETHOSTBYNAME_R */
214 if (!(he_res = gethostbyname(host))) {
215 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
216 "host \"%s\" not found", host);
217 connection->error = MPD_ERROR_UNKHOST;
220 #endif /* HAVE_GETHOSTBYNAME_R */
222 memset(&sin, 0, sizeof(struct sockaddr_in));
223 /* dest.sin_family = he_res->h_addrtype; */
224 sin.sin_family = AF_INET;
225 sin.sin_port = htons(port);
227 switch (he_res->h_addrtype) {
229 memcpy((char *) &sin.sin_addr.s_addr, (char *) he_res->h_addr,
231 dest = (struct sockaddr *) &sin;
232 destlen = sizeof(struct sockaddr_in);
235 strcpy(connection->errorStr, "address type is not IPv4");
236 connection->error = MPD_ERROR_SYSTEM;
241 if (connection->sock > -1) {
242 closesocket(connection->sock);
244 if ((connection->sock = socket(dest->sa_family, SOCK_STREAM, 0)) < 0) {
245 strcpy(connection->errorStr, "problems creating socket");
246 connection->error = MPD_ERROR_SYSTEM;
250 mpd_setConnectionTimeout(connection, timeout);
253 if (do_connect_fail(connection, dest, destlen)) {
254 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
255 "problems connecting to \"%s\" on port %i", host, port);
256 connection->error = MPD_ERROR_CONNPORT;
262 #endif /* !MPD_HAVE_GAI */
264 const char *mpdTagItemKeys[MPD_TAG_NUM_OF_ITEM_TYPES] = {
280 static char *mpd_sanitizeArg(const char *arg)
284 register const char *c;
287 /* instead of counting in that loop above,
288 * just use a bit more memory and halve running time */
289 ret = malloc(strlen(arg) * 2 + 1);
293 for (i = strlen(arg) + 1; i != 0; --i) {
294 if (*c == '"' || *c == '\\') {
303 static mpd_ReturnElement *mpd_newReturnElement(const char *name,
306 mpd_ReturnElement *ret = malloc(sizeof(mpd_ReturnElement));
308 ret->name = strndup(name, text_buffer_size);
309 ret->value = strndup(value, text_buffer_size);
314 static void mpd_freeReturnElement(mpd_ReturnElement *re)
321 void mpd_setConnectionTimeout(mpd_Connection *connection, float timeout)
323 connection->timeout.tv_sec = (int) timeout;
324 connection->timeout.tv_usec =
325 (int) ((timeout - connection->timeout.tv_sec) * 1e6 + 0.5);
328 static int mpd_parseWelcome(mpd_Connection *connection, const char *host,
329 int port, /* char *rt, */ char *output)
335 if (strncmp(output, MPD_WELCOME_MESSAGE, strlen(MPD_WELCOME_MESSAGE))) {
336 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
337 "mpd not running on port %i on host \"%s\"", port, host);
338 connection->error = MPD_ERROR_NOTMPD;
342 tmp = &output[strlen(MPD_WELCOME_MESSAGE)];
344 for (i = 0; i < 3; i++) {
346 connection->version[i] = strtol(tmp, &test, 10);
349 if (!tmp || (test[0] != '.' && test[0] != '\0')) {
350 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
351 "error parsing version number at \"%s\"",
352 &output[strlen(MPD_WELCOME_MESSAGE)]);
353 connection->error = MPD_ERROR_NOTMPD;
362 mpd_Connection *mpd_newConnection(const char *host, int port, float timeout)
367 mpd_Connection *connection = malloc(sizeof(mpd_Connection));
371 strcpy(connection->buffer, "");
372 connection->buflen = 0;
373 connection->bufstart = 0;
374 strcpy(connection->errorStr, "");
375 connection->error = 0;
376 connection->doneProcessing = 0;
377 connection->commandList = 0;
378 connection->listOks = 0;
379 connection->doneListOk = 0;
380 connection->sock = -1;
381 connection->returnElement = NULL;
382 connection->request = NULL;
384 if (winsock_dll_error(connection)) {
388 if (mpd_connect(connection, host, port, timeout) < 0) {
392 while (!(rt = strstr(connection->buffer, "\n"))) {
393 tv.tv_sec = connection->timeout.tv_sec;
394 tv.tv_usec = connection->timeout.tv_usec;
396 FD_SET(connection->sock, &fds);
397 if ((err = select(connection->sock + 1, &fds, NULL, NULL, &tv)) == 1) {
400 readed = recv(connection->sock,
401 &(connection->buffer[connection->buflen]),
402 MPD_BUFFER_MAX_LENGTH - connection->buflen, 0);
404 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
405 "problems getting a response from \"%s\" on port %i : %s",
406 host, port, strerror(errno));
407 connection->error = MPD_ERROR_NORESPONSE;
410 connection->buflen += readed;
411 connection->buffer[connection->buflen] = '\0';
412 } else if (err < 0) {
413 if (SELECT_ERRNO_IGNORE) {
416 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
417 "problems connecting to \"%s\" on port %i", host, port);
418 connection->error = MPD_ERROR_CONNPORT;
421 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
422 "timeout in attempting to get a response from \"%s\" on "
423 "port %i", host, port);
424 connection->error = MPD_ERROR_NORESPONSE;
430 output = strndup(connection->buffer, text_buffer_size);
431 strcpy(connection->buffer, rt + 1);
432 connection->buflen = strlen(connection->buffer);
434 if (mpd_parseWelcome(connection, host, port, /* rt, */ output) == 0) {
435 connection->doneProcessing = 1;
443 void mpd_clearError(mpd_Connection *connection)
445 connection->error = 0;
446 connection->errorStr[0] = '\0';
449 void mpd_closeConnection(mpd_Connection *connection)
451 closesocket(connection->sock);
452 if (connection->returnElement) {
453 free(connection->returnElement);
455 if (connection->request) {
456 free(connection->request);
462 static void mpd_executeCommand(mpd_Connection *connection, const char *command)
467 const char *commandPtr = command;
468 int commandLen = strlen(command);
470 if (!connection->doneProcessing && !connection->commandList) {
471 strcpy(connection->errorStr, "not done processing current command");
472 connection->error = 1;
476 mpd_clearError(connection);
479 FD_SET(connection->sock, &fds);
480 tv.tv_sec = connection->timeout.tv_sec;
481 tv.tv_usec = connection->timeout.tv_usec;
483 while ((ret = select(connection->sock + 1, NULL, &fds, NULL, &tv) == 1)
484 || (ret == -1 && SELECT_ERRNO_IGNORE)) {
485 ret = send(connection->sock, commandPtr, commandLen, MSG_DONTWAIT);
487 if (SENDRECV_ERRNO_IGNORE) {
490 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
491 "problems giving command \"%s\"", command);
492 connection->error = MPD_ERROR_SENDING;
499 if (commandLen <= 0) {
504 if (commandLen > 0) {
506 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
507 "timeout sending command \"%s\"", command);
508 connection->error = MPD_ERROR_TIMEOUT;
512 if (!connection->commandList) {
513 connection->doneProcessing = 0;
514 } else if (connection->commandList == COMMAND_LIST_OK) {
515 connection->listOks++;
519 static void mpd_getNextReturnElement(mpd_Connection *connection)
529 char *bufferCheck = NULL;
533 if (connection->returnElement) {
534 mpd_freeReturnElement(connection->returnElement);
536 connection->returnElement = NULL;
538 if (connection->doneProcessing
539 || (connection->listOks && connection->doneListOk)) {
540 strcpy(connection->errorStr,
541 "already done processing current command");
542 connection->error = 1;
546 bufferCheck = connection->buffer + connection->bufstart;
547 while (connection->bufstart >= connection->buflen
548 || !(rt = strchr(bufferCheck, '\n'))) {
549 if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
550 memmove(connection->buffer,
551 connection->buffer + connection->bufstart,
552 connection->buflen - connection->bufstart + 1);
553 connection->buflen -= connection->bufstart;
554 connection->bufstart = 0;
556 if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
557 strcpy(connection->errorStr, "buffer overrun");
558 connection->error = MPD_ERROR_BUFFEROVERRUN;
559 connection->doneProcessing = 1;
560 connection->doneListOk = 0;
563 bufferCheck = connection->buffer + connection->buflen;
564 tv.tv_sec = connection->timeout.tv_sec;
565 tv.tv_usec = connection->timeout.tv_usec;
567 FD_SET(connection->sock, &fds);
568 if ((err = select(connection->sock + 1, &fds, NULL, NULL, &tv) == 1)) {
569 readed = recv(connection->sock,
570 connection->buffer + connection->buflen,
571 MPD_BUFFER_MAX_LENGTH - connection->buflen, MSG_DONTWAIT);
572 if (readed < 0 && SENDRECV_ERRNO_IGNORE) {
576 strcpy(connection->errorStr, "connection closed");
577 connection->error = MPD_ERROR_CONNCLOSED;
578 connection->doneProcessing = 1;
579 connection->doneListOk = 0;
582 connection->buflen += readed;
583 connection->buffer[connection->buflen] = '\0';
584 } else if (err < 0 && SELECT_ERRNO_IGNORE) {
587 strcpy(connection->errorStr, "connection timeout");
588 connection->error = MPD_ERROR_TIMEOUT;
589 connection->doneProcessing = 1;
590 connection->doneListOk = 0;
596 output = connection->buffer + connection->bufstart;
597 connection->bufstart = rt - connection->buffer + 1;
599 if (strcmp(output, "OK") == 0) {
600 if (connection->listOks > 0) {
601 strcpy(connection->errorStr, "expected more list_OK's");
602 connection->error = 1;
604 connection->listOks = 0;
605 connection->doneProcessing = 1;
606 connection->doneListOk = 0;
610 if (strcmp(output, "list_OK") == 0) {
611 if (!connection->listOks) {
612 strcpy(connection->errorStr, "got an unexpected list_OK");
613 connection->error = 1;
615 connection->doneListOk = 1;
616 connection->listOks--;
621 if (strncmp(output, "ACK", strlen("ACK")) == 0) {
626 strcpy(connection->errorStr, output);
627 connection->error = MPD_ERROR_ACK;
628 connection->errorCode = MPD_ACK_ERROR_UNK;
629 connection->errorAt = MPD_ERROR_AT_UNK;
630 connection->doneProcessing = 1;
631 connection->doneListOk = 0;
633 needle = strchr(output, '[');
637 val = strtol(needle + 1, &test, 10);
641 connection->errorCode = val;
642 val = strtol(test + 1, &test, 10);
646 connection->errorAt = val;
650 tok = strchr(output, ':');
659 if (value[0] == ' ') {
660 connection->returnElement = mpd_newReturnElement(name, &(value[1]));
662 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
663 "error parsing: %s:%s", name, value);
664 connection->error = 1;
668 void mpd_finishCommand(mpd_Connection *connection)
670 while (!connection->doneProcessing) {
671 if (connection->doneListOk) {
672 connection->doneListOk = 0;
674 mpd_getNextReturnElement(connection);
678 static void mpd_finishListOkCommand(mpd_Connection *connection)
680 while (!connection->doneProcessing && connection->listOks
681 && !connection->doneListOk) {
682 mpd_getNextReturnElement(connection);
686 int mpd_nextListOkCommand(mpd_Connection *connection)
688 mpd_finishListOkCommand(connection);
689 if (!connection->doneProcessing) {
690 connection->doneListOk = 0;
692 if (connection->listOks == 0 || connection->doneProcessing) {
698 void mpd_sendStatusCommand(mpd_Connection *connection)
700 mpd_executeCommand(connection, "status\n");
703 mpd_Status *mpd_getStatus(mpd_Connection *connection)
707 /* mpd_executeCommand(connection, "status\n");
708 if (connection->error) {
712 if (connection->doneProcessing
713 || (connection->listOks && connection->doneListOk)) {
717 if (!connection->returnElement) {
718 mpd_getNextReturnElement(connection);
721 status = malloc(sizeof(mpd_Status));
725 status->playlist = -1;
726 status->playlistLength = -1;
730 status->elapsedTime = 0;
731 status->totalTime = 0;
733 status->sampleRate = 0;
735 status->channels = 0;
736 status->crossfade = -1;
737 status->error = NULL;
738 status->updatingDb = 0;
740 if (connection->error) {
744 while (connection->returnElement) {
745 mpd_ReturnElement *re = connection->returnElement;
747 if (strcmp(re->name, "volume") == 0) {
748 status->volume = atoi(re->value);
749 } else if (strcmp(re->name, "repeat") == 0) {
750 status->repeat = atoi(re->value);
751 } else if (strcmp(re->name, "random") == 0) {
752 status->random = atoi(re->value);
753 } else if (strcmp(re->name, "playlist") == 0) {
754 status->playlist = strtol(re->value, NULL, 10);
755 } else if (strcmp(re->name, "playlistlength") == 0) {
756 status->playlistLength = atoi(re->value);
757 } else if (strcmp(re->name, "bitrate") == 0) {
758 status->bitRate = atoi(re->value);
759 } else if (strcmp(re->name, "state") == 0) {
760 if (strcmp(re->value, "play") == 0) {
761 status->state = MPD_STATUS_STATE_PLAY;
762 } else if (strcmp(re->value, "stop") == 0) {
763 status->state = MPD_STATUS_STATE_STOP;
764 } else if (strcmp(re->value, "pause") == 0) {
765 status->state = MPD_STATUS_STATE_PAUSE;
767 status->state = MPD_STATUS_STATE_UNKNOWN;
769 } else if (strcmp(re->name, "song") == 0) {
770 status->song = atoi(re->value);
771 } else if (strcmp(re->name, "songid") == 0) {
772 status->songid = atoi(re->value);
773 } else if (strcmp(re->name, "time") == 0) {
774 char *tok = strchr(re->value, ':');
776 /* the second strchr below is a safety check */
777 if (tok && (strchr(tok, 0) > (tok + 1))) {
778 /* atoi stops at the first non-[0-9] char: */
779 status->elapsedTime = atoi(re->value);
780 status->totalTime = atoi(tok + 1);
782 } else if (strcmp(re->name, "error") == 0) {
783 status->error = strndup(re->value, text_buffer_size);
784 } else if (strcmp(re->name, "xfade") == 0) {
785 status->crossfade = atoi(re->value);
786 } else if (strcmp(re->name, "updating_db") == 0) {
787 status->updatingDb = atoi(re->value);
788 } else if (strcmp(re->name, "audio") == 0) {
789 char *tok = strchr(re->value, ':');
791 if (tok && (strchr(tok, 0) > (tok + 1))) {
792 status->sampleRate = atoi(re->value);
793 status->bits = atoi(++tok);
794 tok = strchr(tok, ':');
795 if (tok && (strchr(tok, 0) > (tok + 1))) {
796 status->channels = atoi(tok + 1);
801 mpd_getNextReturnElement(connection);
802 if (connection->error) {
808 if (connection->error) {
811 } else if (status->state < 0) {
812 strcpy(connection->errorStr, "state not found");
813 connection->error = 1;
821 void mpd_freeStatus(mpd_Status *status)
829 void mpd_sendStatsCommand(mpd_Connection *connection)
831 mpd_executeCommand(connection, "stats\n");
834 mpd_Stats *mpd_getStats(mpd_Connection *connection)
838 /* mpd_executeCommand(connection, "stats\n");
839 if (connection->error) {
843 if (connection->doneProcessing
844 || (connection->listOks && connection->doneListOk)) {
848 if (!connection->returnElement) {
849 mpd_getNextReturnElement(connection);
852 stats = malloc(sizeof(mpd_Stats));
853 stats->numberOfArtists = 0;
854 stats->numberOfAlbums = 0;
855 stats->numberOfSongs = 0;
857 stats->dbUpdateTime = 0;
859 stats->dbPlayTime = 0;
861 if (connection->error) {
865 while (connection->returnElement) {
866 mpd_ReturnElement *re = connection->returnElement;
868 if (strcmp(re->name, "artists") == 0) {
869 stats->numberOfArtists = atoi(re->value);
870 } else if (strcmp(re->name, "albums") == 0) {
871 stats->numberOfAlbums = atoi(re->value);
872 } else if (strcmp(re->name, "songs") == 0) {
873 stats->numberOfSongs = atoi(re->value);
874 } else if (strcmp(re->name, "uptime") == 0) {
875 stats->uptime = strtol(re->value, NULL, 10);
876 } else if (strcmp(re->name, "db_update") == 0) {
877 stats->dbUpdateTime = strtol(re->value, NULL, 10);
878 } else if (strcmp(re->name, "playtime") == 0) {
879 stats->playTime = strtol(re->value, NULL, 10);
880 } else if (strcmp(re->name, "db_playtime") == 0) {
881 stats->dbPlayTime = strtol(re->value, NULL, 10);
884 mpd_getNextReturnElement(connection);
885 if (connection->error) {
891 if (connection->error) {
899 void mpd_freeStats(mpd_Stats *stats)
904 mpd_SearchStats *mpd_getSearchStats(mpd_Connection *connection)
906 mpd_SearchStats *stats;
907 mpd_ReturnElement *re;
909 if (connection->doneProcessing
910 || (connection->listOks && connection->doneListOk)) {
914 if (!connection->returnElement) {
915 mpd_getNextReturnElement(connection);
918 if (connection->error) {
922 stats = malloc(sizeof(mpd_SearchStats));
923 stats->numberOfSongs = 0;
926 while (connection->returnElement) {
927 re = connection->returnElement;
929 if (strcmp(re->name, "songs") == 0) {
930 stats->numberOfSongs = atoi(re->value);
931 } else if (strcmp(re->name, "playtime") == 0) {
932 stats->playTime = strtol(re->value, NULL, 10);
935 mpd_getNextReturnElement(connection);
936 if (connection->error) {
942 if (connection->error) {
950 void mpd_freeSearchStats(mpd_SearchStats *stats)
955 static void mpd_initSong(mpd_Song *song)
966 song->composer = NULL;
967 song->performer = NULL;
969 song->comment = NULL;
971 song->time = MPD_SONG_NO_TIME;
972 song->pos = MPD_SONG_NO_NUM;
973 song->id = MPD_SONG_NO_ID;
976 static void mpd_finishSong(mpd_Song *song)
1002 if (song->composer) {
1003 free(song->composer);
1008 if (song->comment) {
1009 free(song->comment);
1013 mpd_Song *mpd_newSong(void)
1015 mpd_Song *ret = malloc(sizeof(mpd_Song));
1022 void mpd_freeSong(mpd_Song *song)
1024 mpd_finishSong(song);
1028 mpd_Song *mpd_songDup(mpd_Song *song)
1030 mpd_Song *ret = mpd_newSong();
1033 ret->file = strndup(song->file, text_buffer_size);
1036 ret->artist = strndup(song->artist, text_buffer_size);
1039 ret->album = strndup(song->album, text_buffer_size);
1042 ret->title = strndup(song->title, text_buffer_size);
1045 ret->track = strndup(song->track, text_buffer_size);
1048 ret->name = strndup(song->name, text_buffer_size);
1051 ret->date = strndup(song->date, text_buffer_size);
1054 ret->genre = strndup(song->genre, text_buffer_size);
1056 if (song->composer) {
1057 ret->composer = strndup(song->composer, text_buffer_size);
1060 ret->disc = strndup(song->disc, text_buffer_size);
1062 if (song->comment) {
1063 ret->comment = strndup(song->comment, text_buffer_size);
1065 ret->time = song->time;
1066 ret->pos = song->pos;
1072 static void mpd_initDirectory(mpd_Directory *directory)
1074 directory->path = NULL;
1077 static void mpd_finishDirectory(mpd_Directory *directory)
1079 if (directory->path) {
1080 free(directory->path);
1084 mpd_Directory *mpd_newDirectory(void)
1086 mpd_Directory *directory = malloc(sizeof(mpd_Directory));
1088 mpd_initDirectory(directory);
1093 void mpd_freeDirectory(mpd_Directory *directory)
1095 mpd_finishDirectory(directory);
1100 mpd_Directory *mpd_directoryDup(mpd_Directory *directory)
1102 mpd_Directory *ret = mpd_newDirectory();
1104 if (directory->path) {
1105 ret->path = strndup(directory->path, text_buffer_size);
1111 static void mpd_initPlaylistFile(mpd_PlaylistFile *playlist)
1113 playlist->path = NULL;
1116 static void mpd_finishPlaylistFile(mpd_PlaylistFile *playlist)
1118 if (playlist->path) {
1119 free(playlist->path);
1123 mpd_PlaylistFile *mpd_newPlaylistFile(void)
1125 mpd_PlaylistFile *playlist = malloc(sizeof(mpd_PlaylistFile));
1127 mpd_initPlaylistFile(playlist);
1132 void mpd_freePlaylistFile(mpd_PlaylistFile *playlist)
1134 mpd_finishPlaylistFile(playlist);
1138 mpd_PlaylistFile *mpd_playlistFileDup(mpd_PlaylistFile *playlist)
1140 mpd_PlaylistFile *ret = mpd_newPlaylistFile();
1142 if (playlist->path) {
1143 ret->path = strndup(playlist->path, text_buffer_size);
1149 static void mpd_initInfoEntity(mpd_InfoEntity *entity)
1151 entity->info.directory = NULL;
1154 static void mpd_finishInfoEntity(mpd_InfoEntity *entity)
1156 if (entity->info.directory) {
1157 if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1158 mpd_freeDirectory(entity->info.directory);
1159 } else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
1160 mpd_freeSong(entity->info.song);
1161 } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1162 mpd_freePlaylistFile(entity->info.playlistFile);
1167 mpd_InfoEntity *mpd_newInfoEntity(void)
1169 mpd_InfoEntity *entity = malloc(sizeof(mpd_InfoEntity));
1171 mpd_initInfoEntity(entity);
1176 void mpd_freeInfoEntity(mpd_InfoEntity *entity)
1178 mpd_finishInfoEntity(entity);
1182 static void mpd_sendInfoCommand(mpd_Connection *connection, char *command)
1184 mpd_executeCommand(connection, command);
1187 mpd_InfoEntity *mpd_getNextInfoEntity(mpd_Connection *connection)
1189 mpd_InfoEntity *entity = NULL;
1191 if (connection->doneProcessing
1192 || (connection->listOks && connection->doneListOk)) {
1196 if (!connection->returnElement) {
1197 mpd_getNextReturnElement(connection);
1200 if (connection->returnElement) {
1201 if (strcmp(connection->returnElement->name, "file") == 0) {
1202 entity = mpd_newInfoEntity();
1203 entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1204 entity->info.song = mpd_newSong();
1205 entity->info.song->file = strndup(connection->returnElement->value, text_buffer_size);
1206 } else if (strcmp(connection->returnElement->name, "directory") == 0) {
1207 entity = mpd_newInfoEntity();
1208 entity->type = MPD_INFO_ENTITY_TYPE_DIRECTORY;
1209 entity->info.directory = mpd_newDirectory();
1210 entity->info.directory->path =
1211 strndup(connection->returnElement->value, text_buffer_size);
1212 } else if (strcmp(connection->returnElement->name, "playlist") == 0) {
1213 entity = mpd_newInfoEntity();
1214 entity->type = MPD_INFO_ENTITY_TYPE_PLAYLISTFILE;
1215 entity->info.playlistFile = mpd_newPlaylistFile();
1216 entity->info.playlistFile->path =
1217 strndup(connection->returnElement->value, text_buffer_size);
1218 } else if (strcmp(connection->returnElement->name, "cpos") == 0) {
1219 entity = mpd_newInfoEntity();
1220 entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1221 entity->info.song = mpd_newSong();
1222 entity->info.song->pos = atoi(connection->returnElement->value);
1224 connection->error = 1;
1225 strcpy(connection->errorStr, "problem parsing song info");
1232 mpd_getNextReturnElement(connection);
1233 while (connection->returnElement) {
1234 mpd_ReturnElement *re = connection->returnElement;
1236 if (strcmp(re->name, "file") == 0) {
1238 } else if (strcmp(re->name, "directory") == 0) {
1240 } else if (strcmp(re->name, "playlist") == 0) {
1242 } else if (strcmp(re->name, "cpos") == 0) {
1246 if (entity->type == MPD_INFO_ENTITY_TYPE_SONG && strlen(re->value)) {
1247 if (!entity->info.song->artist
1248 && strcmp(re->name, "Artist") == 0) {
1249 entity->info.song->artist = strndup(re->value, text_buffer_size);
1250 } else if (!entity->info.song->album
1251 && strcmp(re->name, "Album") == 0) {
1252 entity->info.song->album = strndup(re->value, text_buffer_size);
1253 } else if (!entity->info.song->title
1254 && strcmp(re->name, "Title") == 0) {
1255 entity->info.song->title = strndup(re->value, text_buffer_size);
1256 } else if (!entity->info.song->track
1257 && strcmp(re->name, "Track") == 0) {
1258 entity->info.song->track = strndup(re->value, text_buffer_size);
1259 } else if (!entity->info.song->name
1260 && strcmp(re->name, "Name") == 0) {
1261 entity->info.song->name = strndup(re->value, text_buffer_size);
1262 } else if (entity->info.song->time == MPD_SONG_NO_TIME
1263 && strcmp(re->name, "Time") == 0) {
1264 entity->info.song->time = atoi(re->value);
1265 } else if (entity->info.song->pos == MPD_SONG_NO_NUM
1266 && strcmp(re->name, "Pos") == 0) {
1267 entity->info.song->pos = atoi(re->value);
1268 } else if (entity->info.song->id == MPD_SONG_NO_ID
1269 && strcmp(re->name, "Id") == 0) {
1270 entity->info.song->id = atoi(re->value);
1271 } else if (!entity->info.song->date
1272 && strcmp(re->name, "Date") == 0) {
1273 entity->info.song->date = strndup(re->value, text_buffer_size);
1274 } else if (!entity->info.song->genre
1275 && strcmp(re->name, "Genre") == 0) {
1276 entity->info.song->genre = strndup(re->value, text_buffer_size);
1277 } else if (!entity->info.song->composer
1278 && strcmp(re->name, "Composer") == 0) {
1279 entity->info.song->composer = strndup(re->value, text_buffer_size);
1280 } else if (!entity->info.song->performer
1281 && strcmp(re->name, "Performer") == 0) {
1282 entity->info.song->performer = strndup(re->value, text_buffer_size);
1283 } else if (!entity->info.song->disc
1284 && strcmp(re->name, "Disc") == 0) {
1285 entity->info.song->disc = strndup(re->value, text_buffer_size);
1286 } else if (!entity->info.song->comment
1287 && strcmp(re->name, "Comment") == 0) {
1288 entity->info.song->comment = strndup(re->value, text_buffer_size);
1290 } else if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1291 } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1294 mpd_getNextReturnElement(connection);
1300 static char *mpd_getNextReturnElementNamed(mpd_Connection *connection,
1303 if (connection->doneProcessing
1304 || (connection->listOks && connection->doneListOk)) {
1308 mpd_getNextReturnElement(connection);
1309 while (connection->returnElement) {
1310 mpd_ReturnElement *re = connection->returnElement;
1312 if (strcmp(re->name, name) == 0) {
1313 return strndup(re->value, text_buffer_size);
1315 mpd_getNextReturnElement(connection);
1321 char *mpd_getNextTag(mpd_Connection *connection, int type)
1323 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES
1324 || type == MPD_TAG_ITEM_ANY) {
1327 if (type == MPD_TAG_ITEM_FILENAME) {
1328 return mpd_getNextReturnElementNamed(connection, "file");
1330 return mpd_getNextReturnElementNamed(connection, mpdTagItemKeys[type]);
1333 char *mpd_getNextArtist(mpd_Connection *connection)
1335 return mpd_getNextReturnElementNamed(connection, "Artist");
1338 char *mpd_getNextAlbum(mpd_Connection *connection)
1340 return mpd_getNextReturnElementNamed(connection, "Album");
1343 void mpd_sendPlaylistInfoCommand(mpd_Connection *connection, int songPos)
1345 int len = strlen("playlistinfo") + 2 + INTLEN + 3;
1346 char *string = malloc(len);
1348 snprintf(string, len, "playlistinfo \"%i\"\n", songPos);
1349 mpd_sendInfoCommand(connection, string);
1353 void mpd_sendPlaylistIdCommand(mpd_Connection *connection, int id)
1355 int len = strlen("playlistid") + 2 + INTLEN + 3;
1356 char *string = malloc(len);
1358 snprintf(string, len, "playlistid \"%i\"\n", id);
1359 mpd_sendInfoCommand(connection, string);
1363 void mpd_sendPlChangesCommand(mpd_Connection *connection, long long playlist)
1365 int len = strlen("plchanges") + 2 + LONGLONGLEN + 3;
1366 char *string = malloc(len);
1368 snprintf(string, len, "plchanges \"%lld\"\n", playlist);
1369 mpd_sendInfoCommand(connection, string);
1373 void mpd_sendPlChangesPosIdCommand(mpd_Connection *connection,
1376 int len = strlen("plchangesposid") + 2 + LONGLONGLEN + 3;
1377 char *string = malloc(len);
1379 snprintf(string, len, "plchangesposid \"%lld\"\n", playlist);
1380 mpd_sendInfoCommand(connection, string);
1384 void mpd_sendListallCommand(mpd_Connection *connection, const char *dir)
1386 char *sDir = mpd_sanitizeArg(dir);
1387 int len = strlen("listall") + 2 + strlen(sDir) + 3;
1388 char *string = malloc(len);
1390 snprintf(string, len, "listall \"%s\"\n", sDir);
1391 mpd_sendInfoCommand(connection, string);
1396 void mpd_sendListallInfoCommand(mpd_Connection *connection, const char *dir)
1398 char *sDir = mpd_sanitizeArg(dir);
1399 int len = strlen("listallinfo") + 2 + strlen(sDir) + 3;
1400 char *string = malloc(len);
1402 snprintf(string, len, "listallinfo \"%s\"\n", sDir);
1403 mpd_sendInfoCommand(connection, string);
1408 void mpd_sendLsInfoCommand(mpd_Connection *connection, const char *dir)
1410 char *sDir = mpd_sanitizeArg(dir);
1411 int len = strlen("lsinfo") + 2 + strlen(sDir) + 3;
1412 char *string = malloc(len);
1414 snprintf(string, len, "lsinfo \"%s\"\n", sDir);
1415 mpd_sendInfoCommand(connection, string);
1420 void mpd_sendCurrentSongCommand(mpd_Connection *connection)
1422 mpd_executeCommand(connection, "currentsong\n");
1425 void mpd_sendSearchCommand(mpd_Connection *connection, int table,
1428 mpd_startSearch(connection, 0);
1429 mpd_addConstraintSearch(connection, table, str);
1430 mpd_commitSearch(connection);
1433 void mpd_sendFindCommand(mpd_Connection *connection, int table,
1436 mpd_startSearch(connection, 1);
1437 mpd_addConstraintSearch(connection, table, str);
1438 mpd_commitSearch(connection);
1441 void mpd_sendListCommand(mpd_Connection *connection, int table,
1448 if (table == MPD_TABLE_ARTIST) {
1449 strcpy(st, "artist");
1450 } else if (table == MPD_TABLE_ALBUM) {
1451 strcpy(st, "album");
1453 connection->error = 1;
1454 strcpy(connection->errorStr, "unknown table for list");
1458 char *sanitArg1 = mpd_sanitizeArg(arg1);
1460 len = strlen("list") + 1 + strlen(sanitArg1) + 2 + strlen(st) + 3;
1461 string = malloc(len);
1462 snprintf(string, len, "list %s \"%s\"\n", st, sanitArg1);
1465 len = strlen("list") + 1 + strlen(st) + 2;
1466 string = malloc(len);
1467 snprintf(string, len, "list %s\n", st);
1469 mpd_sendInfoCommand(connection, string);
1473 void mpd_sendAddCommand(mpd_Connection *connection, const char *file)
1475 char *sFile = mpd_sanitizeArg(file);
1476 int len = strlen("add") + 2 + strlen(sFile) + 3;
1477 char *string = malloc(len);
1479 snprintf(string, len, "add \"%s\"\n", sFile);
1480 mpd_executeCommand(connection, string);
1485 int mpd_sendAddIdCommand(mpd_Connection *connection, const char *file)
1488 char *sFile = mpd_sanitizeArg(file);
1489 int len = strlen("addid") + 2 + strlen(sFile) + 3;
1490 char *string = malloc(len);
1492 snprintf(string, len, "addid \"%s\"\n", sFile);
1493 mpd_sendInfoCommand(connection, string);
1497 string = mpd_getNextReturnElementNamed(connection, "Id");
1499 retval = atoi(string);
1506 void mpd_sendDeleteCommand(mpd_Connection *connection, int songPos)
1508 int len = strlen("delete") + 2 + INTLEN + 3;
1509 char *string = malloc(len);
1511 snprintf(string, len, "delete \"%i\"\n", songPos);
1512 mpd_sendInfoCommand(connection, string);
1516 void mpd_sendDeleteIdCommand(mpd_Connection *connection, int id)
1518 int len = strlen("deleteid") + 2 + INTLEN + 3;
1519 char *string = malloc(len);
1521 snprintf(string, len, "deleteid \"%i\"\n", id);
1522 mpd_sendInfoCommand(connection, string);
1526 void mpd_sendSaveCommand(mpd_Connection *connection, const char *name)
1528 char *sName = mpd_sanitizeArg(name);
1529 int len = strlen("save") + 2 + strlen(sName) + 3;
1530 char *string = malloc(len);
1532 snprintf(string, len, "save \"%s\"\n", sName);
1533 mpd_executeCommand(connection, string);
1538 void mpd_sendLoadCommand(mpd_Connection *connection, const char *name)
1540 char *sName = mpd_sanitizeArg(name);
1541 int len = strlen("load") + 2 + strlen(sName) + 3;
1542 char *string = malloc(len);
1544 snprintf(string, len, "load \"%s\"\n", sName);
1545 mpd_executeCommand(connection, string);
1550 void mpd_sendRmCommand(mpd_Connection *connection, const char *name)
1552 char *sName = mpd_sanitizeArg(name);
1553 int len = strlen("rm") + 2 + strlen(sName) + 3;
1554 char *string = malloc(len);
1556 snprintf(string, len, "rm \"%s\"\n", sName);
1557 mpd_executeCommand(connection, string);
1562 void mpd_sendRenameCommand(mpd_Connection *connection, const char *from,
1565 char *sFrom = mpd_sanitizeArg(from);
1566 char *sTo = mpd_sanitizeArg(to);
1567 int len = strlen("rename") + 2 + strlen(sFrom) + 3 + strlen(sTo) + 3;
1568 char *string = malloc(len);
1570 snprintf(string, len, "rename \"%s\" \"%s\"\n", sFrom, sTo);
1571 mpd_executeCommand(connection, string);
1577 void mpd_sendShuffleCommand(mpd_Connection *connection)
1579 mpd_executeCommand(connection, "shuffle\n");
1582 void mpd_sendClearCommand(mpd_Connection *connection)
1584 mpd_executeCommand(connection, "clear\n");
1587 void mpd_sendPlayCommand(mpd_Connection *connection, int songPos)
1589 int len = strlen("play") + 2 + INTLEN + 3;
1590 char *string = malloc(len);
1592 snprintf(string, len, "play \"%i\"\n", songPos);
1593 mpd_sendInfoCommand(connection, string);
1597 void mpd_sendPlayIdCommand(mpd_Connection *connection, int id)
1599 int len = strlen("playid") + 2 + INTLEN + 3;
1600 char *string = malloc(len);
1602 snprintf(string, len, "playid \"%i\"\n", id);
1603 mpd_sendInfoCommand(connection, string);
1607 void mpd_sendStopCommand(mpd_Connection *connection)
1609 mpd_executeCommand(connection, "stop\n");
1612 void mpd_sendPauseCommand(mpd_Connection *connection, int pauseMode)
1614 int len = strlen("pause") + 2 + INTLEN + 3;
1615 char *string = malloc(len);
1617 snprintf(string, len, "pause \"%i\"\n", pauseMode);
1618 mpd_executeCommand(connection, string);
1622 void mpd_sendNextCommand(mpd_Connection *connection)
1624 mpd_executeCommand(connection, "next\n");
1627 void mpd_sendMoveCommand(mpd_Connection *connection, int from, int to)
1629 int len = strlen("move") + 2 + INTLEN + 3 + INTLEN + 3;
1630 char *string = malloc(len);
1632 snprintf(string, len, "move \"%i\" \"%i\"\n", from, to);
1633 mpd_sendInfoCommand(connection, string);
1637 void mpd_sendMoveIdCommand(mpd_Connection *connection, int id, int to)
1639 int len = strlen("moveid") + 2 + INTLEN + 3 + INTLEN + 3;
1640 char *string = malloc(len);
1642 snprintf(string, len, "moveid \"%i\" \"%i\"\n", id, to);
1643 mpd_sendInfoCommand(connection, string);
1647 void mpd_sendSwapCommand(mpd_Connection *connection, int song1, int song2)
1649 int len = strlen("swap") + 2 + INTLEN + 3 + INTLEN + 3;
1650 char *string = malloc(len);
1652 snprintf(string, len, "swap \"%i\" \"%i\"\n", song1, song2);
1653 mpd_sendInfoCommand(connection, string);
1657 void mpd_sendSwapIdCommand(mpd_Connection *connection, int id1, int id2)
1659 int len = strlen("swapid") + 2 + INTLEN + 3 + INTLEN + 3;
1660 char *string = malloc(len);
1662 snprintf(string, len, "swapid \"%i\" \"%i\"\n", id1, id2);
1663 mpd_sendInfoCommand(connection, string);
1667 void mpd_sendSeekCommand(mpd_Connection *connection, int song, int seek_time)
1669 int len = strlen("seek") + 2 + INTLEN + 3 + INTLEN + 3;
1670 char *string = malloc(len);
1672 snprintf(string, len, "seek \"%i\" \"%i\"\n", song, seek_time);
1673 mpd_sendInfoCommand(connection, string);
1677 void mpd_sendSeekIdCommand(mpd_Connection *connection, int id, int seek_time)
1679 int len = strlen("seekid") + 2 + INTLEN + 3 + INTLEN + 3;
1680 char *string = malloc(len);
1682 snprintf(string, len, "seekid \"%i\" \"%i\"\n", id, seek_time);
1683 mpd_sendInfoCommand(connection, string);
1687 void mpd_sendUpdateCommand(mpd_Connection *connection, char *path)
1689 char *sPath = mpd_sanitizeArg(path);
1690 int len = strlen("update") + 2 + strlen(sPath) + 3;
1691 char *string = malloc(len);
1693 snprintf(string, len, "update \"%s\"\n", sPath);
1694 mpd_sendInfoCommand(connection, string);
1699 int mpd_getUpdateId(mpd_Connection *connection)
1704 jobid = mpd_getNextReturnElementNamed(connection, "updating_db");
1713 void mpd_sendPrevCommand(mpd_Connection *connection)
1715 mpd_executeCommand(connection, "previous\n");
1718 void mpd_sendRepeatCommand(mpd_Connection *connection, int repeatMode)
1720 int len = strlen("repeat") + 2 + INTLEN + 3;
1721 char *string = malloc(len);
1723 snprintf(string, len, "repeat \"%i\"\n", repeatMode);
1724 mpd_executeCommand(connection, string);
1728 void mpd_sendRandomCommand(mpd_Connection *connection, int randomMode)
1730 int len = strlen("random") + 2 + INTLEN + 3;
1731 char *string = malloc(len);
1733 snprintf(string, len, "random \"%i\"\n", randomMode);
1734 mpd_executeCommand(connection, string);
1738 void mpd_sendSetvolCommand(mpd_Connection *connection, int volumeChange)
1740 int len = strlen("setvol") + 2 + INTLEN + 3;
1741 char *string = malloc(len);
1743 snprintf(string, len, "setvol \"%i\"\n", volumeChange);
1744 mpd_executeCommand(connection, string);
1748 void mpd_sendVolumeCommand(mpd_Connection *connection, int volumeChange)
1750 int len = strlen("volume") + 2 + INTLEN + 3;
1751 char *string = malloc(len);
1753 snprintf(string, len, "volume \"%i\"\n", volumeChange);
1754 mpd_executeCommand(connection, string);
1758 void mpd_sendCrossfadeCommand(mpd_Connection *connection, int seconds)
1760 int len = strlen("crossfade") + 2 + INTLEN + 3;
1761 char *string = malloc(len);
1763 snprintf(string, len, "crossfade \"%i\"\n", seconds);
1764 mpd_executeCommand(connection, string);
1768 void mpd_sendPasswordCommand(mpd_Connection *connection, const char *pass)
1770 char *sPass = mpd_sanitizeArg(pass);
1771 int len = strlen("password") + 2 + strlen(sPass) + 3;
1772 char *string = malloc(len);
1774 snprintf(string, len, "password \"%s\"\n", sPass);
1775 mpd_executeCommand(connection, string);
1780 void mpd_sendCommandListBegin(mpd_Connection *connection)
1782 if (connection->commandList) {
1783 strcpy(connection->errorStr, "already in command list mode");
1784 connection->error = 1;
1787 connection->commandList = COMMAND_LIST;
1788 mpd_executeCommand(connection, "command_list_begin\n");
1791 void mpd_sendCommandListOkBegin(mpd_Connection *connection)
1793 if (connection->commandList) {
1794 strcpy(connection->errorStr, "already in command list mode");
1795 connection->error = 1;
1798 connection->commandList = COMMAND_LIST_OK;
1799 mpd_executeCommand(connection, "command_list_ok_begin\n");
1800 connection->listOks = 0;
1803 void mpd_sendCommandListEnd(mpd_Connection *connection)
1805 if (!connection->commandList) {
1806 strcpy(connection->errorStr, "not in command list mode");
1807 connection->error = 1;
1810 connection->commandList = 0;
1811 mpd_executeCommand(connection, "command_list_end\n");
1814 void mpd_sendOutputsCommand(mpd_Connection *connection)
1816 mpd_executeCommand(connection, "outputs\n");
1819 mpd_OutputEntity *mpd_getNextOutput(mpd_Connection *connection)
1821 mpd_OutputEntity *output = NULL;
1823 if (connection->doneProcessing
1824 || (connection->listOks && connection->doneListOk)) {
1828 if (connection->error) {
1832 output = malloc(sizeof(mpd_OutputEntity));
1834 output->name = NULL;
1835 output->enabled = 0;
1837 if (!connection->returnElement) {
1838 mpd_getNextReturnElement(connection);
1841 while (connection->returnElement) {
1842 mpd_ReturnElement *re = connection->returnElement;
1844 if (strcmp(re->name, "outputid") == 0) {
1845 if (output != NULL && output->id >= 0) {
1848 output->id = atoi(re->value);
1849 } else if (strcmp(re->name, "outputname") == 0) {
1850 output->name = strndup(re->value, text_buffer_size);
1851 } else if (strcmp(re->name, "outputenabled") == 0) {
1852 output->enabled = atoi(re->value);
1855 mpd_getNextReturnElement(connection);
1856 if (connection->error) {
1865 void mpd_sendEnableOutputCommand(mpd_Connection *connection, int outputId)
1867 int len = strlen("enableoutput") + 2 + INTLEN + 3;
1868 char *string = malloc(len);
1870 snprintf(string, len, "enableoutput \"%i\"\n", outputId);
1871 mpd_executeCommand(connection, string);
1875 void mpd_sendDisableOutputCommand(mpd_Connection *connection, int outputId)
1877 int len = strlen("disableoutput") + 2 + INTLEN + 3;
1878 char *string = malloc(len);
1880 snprintf(string, len, "disableoutput \"%i\"\n", outputId);
1881 mpd_executeCommand(connection, string);
1885 void mpd_freeOutputElement(mpd_OutputEntity *output)
1891 /** odd naming, but it gets the not allowed commands */
1892 void mpd_sendNotCommandsCommand(mpd_Connection *connection)
1894 mpd_executeCommand(connection, "notcommands\n");
1897 /** odd naming, but it gets the allowed commands */
1898 void mpd_sendCommandsCommand(mpd_Connection *connection)
1900 mpd_executeCommand(connection, "commands\n");
1903 /** Get the next returned command */
1904 char *mpd_getNextCommand(mpd_Connection *connection)
1906 return mpd_getNextReturnElementNamed(connection, "command");
1909 void mpd_sendUrlHandlersCommand(mpd_Connection *connection)
1911 mpd_executeCommand(connection, "urlhandlers\n");
1914 char *mpd_getNextHandler(mpd_Connection *connection)
1916 return mpd_getNextReturnElementNamed(connection, "handler");
1919 void mpd_sendTagTypesCommand(mpd_Connection *connection)
1921 mpd_executeCommand(connection, "tagtypes\n");
1924 char *mpd_getNextTagType(mpd_Connection *connection)
1926 return mpd_getNextReturnElementNamed(connection, "tagtype");
1929 void mpd_startSearch(mpd_Connection *connection, int exact)
1931 if (connection->request) {
1932 strcpy(connection->errorStr, "search already in progress");
1933 connection->error = 1;
1938 connection->request = strndup("find", text_buffer_size);
1940 connection->request = strndup("search", text_buffer_size);
1944 void mpd_startStatsSearch(mpd_Connection *connection)
1946 if (connection->request) {
1947 strcpy(connection->errorStr, "search already in progress");
1948 connection->error = 1;
1952 connection->request = strndup("count", text_buffer_size);
1955 void mpd_startPlaylistSearch(mpd_Connection *connection, int exact)
1957 if (connection->request) {
1958 strcpy(connection->errorStr, "search already in progress");
1959 connection->error = 1;
1964 connection->request = strndup("playlistfind", text_buffer_size);
1966 connection->request = strndup("playlistsearch", text_buffer_size);
1970 void mpd_startFieldSearch(mpd_Connection *connection, int type)
1972 const char *strtype;
1975 if (connection->request) {
1976 strcpy(connection->errorStr, "search already in progress");
1977 connection->error = 1;
1981 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
1982 strcpy(connection->errorStr, "invalid type specified");
1983 connection->error = 1;
1987 strtype = mpdTagItemKeys[type];
1989 len = 5 + strlen(strtype) + 1;
1990 connection->request = malloc(len);
1992 snprintf(connection->request, len, "list %c%s", tolower(strtype[0]),
1996 void mpd_addConstraintSearch(mpd_Connection *connection, int type,
1999 const char *strtype;
2004 if (!connection->request) {
2005 strcpy(connection->errorStr, "no search in progress");
2006 connection->error = 1;
2010 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
2011 strcpy(connection->errorStr, "invalid type specified");
2012 connection->error = 1;
2017 strcpy(connection->errorStr, "no name specified");
2018 connection->error = 1;
2022 string = strndup(connection->request, text_buffer_size);
2023 strtype = mpdTagItemKeys[type];
2024 arg = mpd_sanitizeArg(name);
2026 len = strlen(string) + 1 + strlen(strtype) + 2 + strlen(arg) + 2;
2027 connection->request = realloc(connection->request, len);
2028 snprintf(connection->request, len, "%s %c%s \"%s\"", string,
2029 tolower(strtype[0]), strtype + 1, arg);
2035 void mpd_commitSearch(mpd_Connection *connection)
2039 if (!connection->request) {
2040 strcpy(connection->errorStr, "no search in progress");
2041 connection->error = 1;
2045 len = strlen(connection->request) + 2;
2046 connection->request = realloc(connection->request, len);
2047 connection->request[len - 2] = '\n';
2048 connection->request[len - 1] = '\0';
2049 mpd_sendInfoCommand(connection, connection->request);
2051 free(connection->request);
2052 connection->request = NULL;
2056 * @param connection a MpdConnection
2057 * @param path the path to the playlist.
2059 * List the content, with full metadata, of a stored playlist. */
2060 void mpd_sendListPlaylistInfoCommand(mpd_Connection *connection, char *path)
2062 char *arg = mpd_sanitizeArg(path);
2063 int len = strlen("listplaylistinfo") + 2 + strlen(arg) + 3;
2064 char *query = malloc(len);
2066 snprintf(query, len, "listplaylistinfo \"%s\"\n", arg);
2067 mpd_sendInfoCommand(connection, query);
2073 * @param connection a MpdConnection
2074 * @param path the path to the playlist.
2076 * List the content of a stored playlist. */
2077 void mpd_sendListPlaylistCommand(mpd_Connection *connection, char *path)
2079 char *arg = mpd_sanitizeArg(path);
2080 int len = strlen("listplaylist") + 2 + strlen(arg) + 3;
2081 char *query = malloc(len);
2083 snprintf(query, len, "listplaylist \"%s\"\n", arg);
2084 mpd_sendInfoCommand(connection, query);
2089 void mpd_sendPlaylistClearCommand(mpd_Connection *connection, char *path)
2091 char *sPath = mpd_sanitizeArg(path);
2092 int len = strlen("playlistclear") + 2 + strlen(sPath) + 3;
2093 char *string = malloc(len);
2095 snprintf(string, len, "playlistclear \"%s\"\n", sPath);
2096 mpd_executeCommand(connection, string);
2101 void mpd_sendPlaylistAddCommand(mpd_Connection *connection, char *playlist,
2104 char *sPlaylist = mpd_sanitizeArg(playlist);
2105 char *sPath = mpd_sanitizeArg(path);
2106 int len = strlen("playlistadd") + 2 + strlen(sPlaylist) + 3 +
2108 char *string = malloc(len);
2110 snprintf(string, len, "playlistadd \"%s\" \"%s\"\n", sPlaylist, sPath);
2111 mpd_executeCommand(connection, string);
2117 void mpd_sendPlaylistMoveCommand(mpd_Connection *connection, char *playlist,
2120 char *sPlaylist = mpd_sanitizeArg(playlist);
2121 int len = strlen("playlistmove") + 2 + strlen(sPlaylist) + 3 + INTLEN +
2123 char *string = malloc(len);
2125 snprintf(string, len, "playlistmove \"%s\" \"%i\" \"%i\"\n", sPlaylist,
2127 mpd_executeCommand(connection, string);
2132 void mpd_sendPlaylistDeleteCommand(mpd_Connection *connection, char *playlist,
2135 char *sPlaylist = mpd_sanitizeArg(playlist);
2136 int len = strlen("playlistdelete") + 2 + strlen(sPlaylist) + 3 +
2138 char *string = malloc(len);
2140 snprintf(string, len, "playlistdelete \"%s\" \"%i\"\n", sPlaylist, pos);
2141 mpd_executeCommand(connection, string);