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,
199 struct sockaddr *dest;
201 struct sockaddr_in sin;
203 if (!(he = gethostbyname(host))) {
204 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
205 "host \"%s\" not found", host);
206 connection->error = MPD_ERROR_UNKHOST;
210 memset(&sin, 0, sizeof(struct sockaddr_in));
211 /* dest.sin_family = he->h_addrtype; */
212 sin.sin_family = AF_INET;
213 sin.sin_port = htons(port);
215 switch (he->h_addrtype) {
217 memcpy((char *) &sin.sin_addr.s_addr, (char *) he->h_addr,
219 dest = (struct sockaddr *) &sin;
220 destlen = sizeof(struct sockaddr_in);
223 strcpy(connection->errorStr, "address type is not IPv4");
224 connection->error = MPD_ERROR_SYSTEM;
229 if (connection->sock > -1) {
230 closesocket(connection->sock);
232 if ((connection->sock = socket(dest->sa_family, SOCK_STREAM, 0)) < 0) {
233 strcpy(connection->errorStr, "problems creating socket");
234 connection->error = MPD_ERROR_SYSTEM;
238 mpd_setConnectionTimeout(connection, timeout);
241 if (do_connect_fail(connection, dest, destlen)) {
242 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
243 "problems connecting to \"%s\" on port %i", host, port);
244 connection->error = MPD_ERROR_CONNPORT;
250 #endif /* !MPD_HAVE_GAI */
252 const char *mpdTagItemKeys[MPD_TAG_NUM_OF_ITEM_TYPES] = {
268 static char *mpd_sanitizeArg(const char *arg)
272 register const char *c;
275 /* instead of counting in that loop above,
276 * just use a bit more memory and halve running time */
277 ret = malloc(strlen(arg) * 2 + 1);
281 for (i = strlen(arg) + 1; i != 0; --i) {
282 if (*c == '"' || *c == '\\') {
291 static mpd_ReturnElement *mpd_newReturnElement(const char *name,
294 mpd_ReturnElement *ret = malloc(sizeof(mpd_ReturnElement));
296 ret->name = strdup(name);
297 ret->value = strdup(value);
302 static void mpd_freeReturnElement(mpd_ReturnElement *re)
309 void mpd_setConnectionTimeout(mpd_Connection *connection, float timeout)
311 connection->timeout.tv_sec = (int) timeout;
312 connection->timeout.tv_usec =
313 (int) ((timeout - connection->timeout.tv_sec) * 1e6 + 0.5);
316 static int mpd_parseWelcome(mpd_Connection *connection, const char *host,
317 int port, /* char *rt, */ char *output)
323 if (strncmp(output, MPD_WELCOME_MESSAGE, strlen(MPD_WELCOME_MESSAGE))) {
324 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
325 "mpd not running on port %i on host \"%s\"", port, host);
326 connection->error = MPD_ERROR_NOTMPD;
330 tmp = &output[strlen(MPD_WELCOME_MESSAGE)];
332 for (i = 0; i < 3; i++) {
334 connection->version[i] = strtol(tmp, &test, 10);
337 if (!tmp || (test[0] != '.' && test[0] != '\0')) {
338 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
339 "error parsing version number at \"%s\"",
340 &output[strlen(MPD_WELCOME_MESSAGE)]);
341 connection->error = MPD_ERROR_NOTMPD;
350 mpd_Connection *mpd_newConnection(const char *host, int port, float timeout)
355 mpd_Connection *connection = malloc(sizeof(mpd_Connection));
359 strcpy(connection->buffer, "");
360 connection->buflen = 0;
361 connection->bufstart = 0;
362 strcpy(connection->errorStr, "");
363 connection->error = 0;
364 connection->doneProcessing = 0;
365 connection->commandList = 0;
366 connection->listOks = 0;
367 connection->doneListOk = 0;
368 connection->sock = -1;
369 connection->returnElement = NULL;
370 connection->request = NULL;
372 if (winsock_dll_error(connection)) {
376 if (mpd_connect(connection, host, port, timeout) < 0) {
380 while (!(rt = strstr(connection->buffer, "\n"))) {
381 tv.tv_sec = connection->timeout.tv_sec;
382 tv.tv_usec = connection->timeout.tv_usec;
384 FD_SET(connection->sock, &fds);
385 if ((err = select(connection->sock + 1, &fds, NULL, NULL, &tv)) == 1) {
388 readed = recv(connection->sock,
389 &(connection->buffer[connection->buflen]),
390 MPD_BUFFER_MAX_LENGTH - connection->buflen, 0);
392 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
393 "problems getting a response from \"%s\" on port %i : %s",
394 host, port, strerror(errno));
395 connection->error = MPD_ERROR_NORESPONSE;
398 connection->buflen += readed;
399 connection->buffer[connection->buflen] = '\0';
400 } else if (err < 0) {
401 if (SELECT_ERRNO_IGNORE) {
404 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
405 "problems connecting to \"%s\" on port %i", host, port);
406 connection->error = MPD_ERROR_CONNPORT;
409 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
410 "timeout in attempting to get a response from \"%s\" on "
411 "port %i", host, port);
412 connection->error = MPD_ERROR_NORESPONSE;
418 output = strdup(connection->buffer);
419 strcpy(connection->buffer, rt + 1);
420 connection->buflen = strlen(connection->buffer);
422 if (mpd_parseWelcome(connection, host, port, /* rt, */ output) == 0) {
423 connection->doneProcessing = 1;
431 void mpd_clearError(mpd_Connection *connection)
433 connection->error = 0;
434 connection->errorStr[0] = '\0';
437 void mpd_closeConnection(mpd_Connection *connection)
439 closesocket(connection->sock);
440 if (connection->returnElement) {
441 free(connection->returnElement);
443 if (connection->request) {
444 free(connection->request);
450 static void mpd_executeCommand(mpd_Connection *connection, const char *command)
455 const char *commandPtr = command;
456 int commandLen = strlen(command);
458 if (!connection->doneProcessing && !connection->commandList) {
459 strcpy(connection->errorStr, "not done processing current command");
460 connection->error = 1;
464 mpd_clearError(connection);
467 FD_SET(connection->sock, &fds);
468 tv.tv_sec = connection->timeout.tv_sec;
469 tv.tv_usec = connection->timeout.tv_usec;
471 while ((ret = select(connection->sock + 1, NULL, &fds, NULL, &tv) == 1)
472 || (ret == -1 && SELECT_ERRNO_IGNORE)) {
473 ret = send(connection->sock, commandPtr, commandLen, MSG_DONTWAIT);
475 if (SENDRECV_ERRNO_IGNORE) {
478 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
479 "problems giving command \"%s\"", command);
480 connection->error = MPD_ERROR_SENDING;
487 if (commandLen <= 0) {
492 if (commandLen > 0) {
494 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
495 "timeout sending command \"%s\"", command);
496 connection->error = MPD_ERROR_TIMEOUT;
500 if (!connection->commandList) {
501 connection->doneProcessing = 0;
502 } else if (connection->commandList == COMMAND_LIST_OK) {
503 connection->listOks++;
507 static void mpd_getNextReturnElement(mpd_Connection *connection)
517 char *bufferCheck = NULL;
521 if (connection->returnElement) {
522 mpd_freeReturnElement(connection->returnElement);
524 connection->returnElement = NULL;
526 if (connection->doneProcessing
527 || (connection->listOks && connection->doneListOk)) {
528 strcpy(connection->errorStr,
529 "already done processing current command");
530 connection->error = 1;
534 bufferCheck = connection->buffer + connection->bufstart;
535 while (connection->bufstart >= connection->buflen
536 || !(rt = strchr(bufferCheck, '\n'))) {
537 if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
538 memmove(connection->buffer,
539 connection->buffer + connection->bufstart,
540 connection->buflen - connection->bufstart + 1);
541 connection->buflen -= connection->bufstart;
542 connection->bufstart = 0;
544 if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
545 strcpy(connection->errorStr, "buffer overrun");
546 connection->error = MPD_ERROR_BUFFEROVERRUN;
547 connection->doneProcessing = 1;
548 connection->doneListOk = 0;
551 bufferCheck = connection->buffer + connection->buflen;
552 tv.tv_sec = connection->timeout.tv_sec;
553 tv.tv_usec = connection->timeout.tv_usec;
555 FD_SET(connection->sock, &fds);
556 if ((err = select(connection->sock + 1, &fds, NULL, NULL, &tv) == 1)) {
557 readed = recv(connection->sock,
558 connection->buffer + connection->buflen,
559 MPD_BUFFER_MAX_LENGTH - connection->buflen, MSG_DONTWAIT);
560 if (readed < 0 && SENDRECV_ERRNO_IGNORE) {
564 strcpy(connection->errorStr, "connection closed");
565 connection->error = MPD_ERROR_CONNCLOSED;
566 connection->doneProcessing = 1;
567 connection->doneListOk = 0;
570 connection->buflen += readed;
571 connection->buffer[connection->buflen] = '\0';
572 } else if (err < 0 && SELECT_ERRNO_IGNORE) {
575 strcpy(connection->errorStr, "connection timeout");
576 connection->error = MPD_ERROR_TIMEOUT;
577 connection->doneProcessing = 1;
578 connection->doneListOk = 0;
584 output = connection->buffer + connection->bufstart;
585 connection->bufstart = rt - connection->buffer + 1;
587 if (strcmp(output, "OK") == 0) {
588 if (connection->listOks > 0) {
589 strcpy(connection->errorStr, "expected more list_OK's");
590 connection->error = 1;
592 connection->listOks = 0;
593 connection->doneProcessing = 1;
594 connection->doneListOk = 0;
598 if (strcmp(output, "list_OK") == 0) {
599 if (!connection->listOks) {
600 strcpy(connection->errorStr, "got an unexpected list_OK");
601 connection->error = 1;
603 connection->doneListOk = 1;
604 connection->listOks--;
609 if (strncmp(output, "ACK", strlen("ACK")) == 0) {
614 strcpy(connection->errorStr, output);
615 connection->error = MPD_ERROR_ACK;
616 connection->errorCode = MPD_ACK_ERROR_UNK;
617 connection->errorAt = MPD_ERROR_AT_UNK;
618 connection->doneProcessing = 1;
619 connection->doneListOk = 0;
621 needle = strchr(output, '[');
625 val = strtol(needle + 1, &test, 10);
629 connection->errorCode = val;
630 val = strtol(test + 1, &test, 10);
634 connection->errorAt = val;
638 tok = strchr(output, ':');
647 if (value[0] == ' ') {
648 connection->returnElement = mpd_newReturnElement(name, &(value[1]));
650 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
651 "error parsing: %s:%s", name, value);
652 connection->error = 1;
656 void mpd_finishCommand(mpd_Connection *connection)
658 while (!connection->doneProcessing) {
659 if (connection->doneListOk) {
660 connection->doneListOk = 0;
662 mpd_getNextReturnElement(connection);
666 static void mpd_finishListOkCommand(mpd_Connection *connection)
668 while (!connection->doneProcessing && connection->listOks
669 && !connection->doneListOk) {
670 mpd_getNextReturnElement(connection);
674 int mpd_nextListOkCommand(mpd_Connection *connection)
676 mpd_finishListOkCommand(connection);
677 if (!connection->doneProcessing) {
678 connection->doneListOk = 0;
680 if (connection->listOks == 0 || connection->doneProcessing) {
686 void mpd_sendStatusCommand(mpd_Connection *connection)
688 mpd_executeCommand(connection, "status\n");
691 mpd_Status *mpd_getStatus(mpd_Connection *connection)
695 /* mpd_executeCommand(connection, "status\n");
696 if (connection->error) {
700 if (connection->doneProcessing
701 || (connection->listOks && connection->doneListOk)) {
705 if (!connection->returnElement) {
706 mpd_getNextReturnElement(connection);
709 status = malloc(sizeof(mpd_Status));
713 status->playlist = -1;
714 status->playlistLength = -1;
718 status->elapsedTime = 0;
719 status->totalTime = 0;
721 status->sampleRate = 0;
723 status->channels = 0;
724 status->crossfade = -1;
725 status->error = NULL;
726 status->updatingDb = 0;
728 if (connection->error) {
732 while (connection->returnElement) {
733 mpd_ReturnElement *re = connection->returnElement;
735 if (strcmp(re->name, "volume") == 0) {
736 status->volume = atoi(re->value);
737 } else if (strcmp(re->name, "repeat") == 0) {
738 status->repeat = atoi(re->value);
739 } else if (strcmp(re->name, "random") == 0) {
740 status->random = atoi(re->value);
741 } else if (strcmp(re->name, "playlist") == 0) {
742 status->playlist = strtol(re->value, NULL, 10);
743 } else if (strcmp(re->name, "playlistlength") == 0) {
744 status->playlistLength = atoi(re->value);
745 } else if (strcmp(re->name, "bitrate") == 0) {
746 status->bitRate = atoi(re->value);
747 } else if (strcmp(re->name, "state") == 0) {
748 if (strcmp(re->value, "play") == 0) {
749 status->state = MPD_STATUS_STATE_PLAY;
750 } else if (strcmp(re->value, "stop") == 0) {
751 status->state = MPD_STATUS_STATE_STOP;
752 } else if (strcmp(re->value, "pause") == 0) {
753 status->state = MPD_STATUS_STATE_PAUSE;
755 status->state = MPD_STATUS_STATE_UNKNOWN;
757 } else if (strcmp(re->name, "song") == 0) {
758 status->song = atoi(re->value);
759 } else if (strcmp(re->name, "songid") == 0) {
760 status->songid = atoi(re->value);
761 } else if (strcmp(re->name, "time") == 0) {
762 char *tok = strchr(re->value, ':');
764 /* the second strchr below is a safety check */
765 if (tok && (strchr(tok, 0) > (tok + 1))) {
766 /* atoi stops at the first non-[0-9] char: */
767 status->elapsedTime = atoi(re->value);
768 status->totalTime = atoi(tok + 1);
770 } else if (strcmp(re->name, "error") == 0) {
771 status->error = strdup(re->value);
772 } else if (strcmp(re->name, "xfade") == 0) {
773 status->crossfade = atoi(re->value);
774 } else if (strcmp(re->name, "updating_db") == 0) {
775 status->updatingDb = atoi(re->value);
776 } else if (strcmp(re->name, "audio") == 0) {
777 char *tok = strchr(re->value, ':');
779 if (tok && (strchr(tok, 0) > (tok + 1))) {
780 status->sampleRate = atoi(re->value);
781 status->bits = atoi(++tok);
782 tok = strchr(tok, ':');
783 if (tok && (strchr(tok, 0) > (tok + 1))) {
784 status->channels = atoi(tok + 1);
789 mpd_getNextReturnElement(connection);
790 if (connection->error) {
796 if (connection->error) {
799 } else if (status->state < 0) {
800 strcpy(connection->errorStr, "state not found");
801 connection->error = 1;
809 void mpd_freeStatus(mpd_Status *status)
817 void mpd_sendStatsCommand(mpd_Connection *connection)
819 mpd_executeCommand(connection, "stats\n");
822 mpd_Stats *mpd_getStats(mpd_Connection *connection)
826 /* mpd_executeCommand(connection, "stats\n");
827 if (connection->error) {
831 if (connection->doneProcessing
832 || (connection->listOks && connection->doneListOk)) {
836 if (!connection->returnElement) {
837 mpd_getNextReturnElement(connection);
840 stats = malloc(sizeof(mpd_Stats));
841 stats->numberOfArtists = 0;
842 stats->numberOfAlbums = 0;
843 stats->numberOfSongs = 0;
845 stats->dbUpdateTime = 0;
847 stats->dbPlayTime = 0;
849 if (connection->error) {
853 while (connection->returnElement) {
854 mpd_ReturnElement *re = connection->returnElement;
856 if (strcmp(re->name, "artists") == 0) {
857 stats->numberOfArtists = atoi(re->value);
858 } else if (strcmp(re->name, "albums") == 0) {
859 stats->numberOfAlbums = atoi(re->value);
860 } else if (strcmp(re->name, "songs") == 0) {
861 stats->numberOfSongs = atoi(re->value);
862 } else if (strcmp(re->name, "uptime") == 0) {
863 stats->uptime = strtol(re->value, NULL, 10);
864 } else if (strcmp(re->name, "db_update") == 0) {
865 stats->dbUpdateTime = strtol(re->value, NULL, 10);
866 } else if (strcmp(re->name, "playtime") == 0) {
867 stats->playTime = strtol(re->value, NULL, 10);
868 } else if (strcmp(re->name, "db_playtime") == 0) {
869 stats->dbPlayTime = strtol(re->value, NULL, 10);
872 mpd_getNextReturnElement(connection);
873 if (connection->error) {
879 if (connection->error) {
887 void mpd_freeStats(mpd_Stats *stats)
892 mpd_SearchStats *mpd_getSearchStats(mpd_Connection *connection)
894 mpd_SearchStats *stats;
895 mpd_ReturnElement *re;
897 if (connection->doneProcessing
898 || (connection->listOks && connection->doneListOk)) {
902 if (!connection->returnElement) {
903 mpd_getNextReturnElement(connection);
906 if (connection->error) {
910 stats = malloc(sizeof(mpd_SearchStats));
911 stats->numberOfSongs = 0;
914 while (connection->returnElement) {
915 re = connection->returnElement;
917 if (strcmp(re->name, "songs") == 0) {
918 stats->numberOfSongs = atoi(re->value);
919 } else if (strcmp(re->name, "playtime") == 0) {
920 stats->playTime = strtol(re->value, NULL, 10);
923 mpd_getNextReturnElement(connection);
924 if (connection->error) {
930 if (connection->error) {
938 void mpd_freeSearchStats(mpd_SearchStats *stats)
943 static void mpd_initSong(mpd_Song *song)
954 song->composer = NULL;
955 song->performer = NULL;
957 song->comment = NULL;
959 song->time = MPD_SONG_NO_TIME;
960 song->pos = MPD_SONG_NO_NUM;
961 song->id = MPD_SONG_NO_ID;
964 static void mpd_finishSong(mpd_Song *song)
990 if (song->composer) {
991 free(song->composer);
1001 mpd_Song *mpd_newSong(void)
1003 mpd_Song *ret = malloc(sizeof(mpd_Song));
1010 void mpd_freeSong(mpd_Song *song)
1012 mpd_finishSong(song);
1016 mpd_Song *mpd_songDup(mpd_Song *song)
1018 mpd_Song *ret = mpd_newSong();
1021 ret->file = strdup(song->file);
1024 ret->artist = strdup(song->artist);
1027 ret->album = strdup(song->album);
1030 ret->title = strdup(song->title);
1033 ret->track = strdup(song->track);
1036 ret->name = strdup(song->name);
1039 ret->date = strdup(song->date);
1042 ret->genre = strdup(song->genre);
1044 if (song->composer) {
1045 ret->composer = strdup(song->composer);
1048 ret->disc = strdup(song->disc);
1050 if (song->comment) {
1051 ret->comment = strdup(song->comment);
1053 ret->time = song->time;
1054 ret->pos = song->pos;
1060 static void mpd_initDirectory(mpd_Directory *directory)
1062 directory->path = NULL;
1065 static void mpd_finishDirectory(mpd_Directory *directory)
1067 if (directory->path) {
1068 free(directory->path);
1072 mpd_Directory *mpd_newDirectory(void)
1074 mpd_Directory *directory = malloc(sizeof(mpd_Directory));
1076 mpd_initDirectory(directory);
1081 void mpd_freeDirectory(mpd_Directory *directory)
1083 mpd_finishDirectory(directory);
1088 mpd_Directory *mpd_directoryDup(mpd_Directory *directory)
1090 mpd_Directory *ret = mpd_newDirectory();
1092 if (directory->path) {
1093 ret->path = strdup(directory->path);
1099 static void mpd_initPlaylistFile(mpd_PlaylistFile *playlist)
1101 playlist->path = NULL;
1104 static void mpd_finishPlaylistFile(mpd_PlaylistFile *playlist)
1106 if (playlist->path) {
1107 free(playlist->path);
1111 mpd_PlaylistFile *mpd_newPlaylistFile(void)
1113 mpd_PlaylistFile *playlist = malloc(sizeof(mpd_PlaylistFile));
1115 mpd_initPlaylistFile(playlist);
1120 void mpd_freePlaylistFile(mpd_PlaylistFile *playlist)
1122 mpd_finishPlaylistFile(playlist);
1126 mpd_PlaylistFile *mpd_playlistFileDup(mpd_PlaylistFile *playlist)
1128 mpd_PlaylistFile *ret = mpd_newPlaylistFile();
1130 if (playlist->path) {
1131 ret->path = strdup(playlist->path);
1137 static void mpd_initInfoEntity(mpd_InfoEntity *entity)
1139 entity->info.directory = NULL;
1142 static void mpd_finishInfoEntity(mpd_InfoEntity *entity)
1144 if (entity->info.directory) {
1145 if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1146 mpd_freeDirectory(entity->info.directory);
1147 } else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
1148 mpd_freeSong(entity->info.song);
1149 } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1150 mpd_freePlaylistFile(entity->info.playlistFile);
1155 mpd_InfoEntity *mpd_newInfoEntity(void)
1157 mpd_InfoEntity *entity = malloc(sizeof(mpd_InfoEntity));
1159 mpd_initInfoEntity(entity);
1164 void mpd_freeInfoEntity(mpd_InfoEntity *entity)
1166 mpd_finishInfoEntity(entity);
1170 static void mpd_sendInfoCommand(mpd_Connection *connection, char *command)
1172 mpd_executeCommand(connection, command);
1175 mpd_InfoEntity *mpd_getNextInfoEntity(mpd_Connection *connection)
1177 mpd_InfoEntity *entity = NULL;
1179 if (connection->doneProcessing
1180 || (connection->listOks && connection->doneListOk)) {
1184 if (!connection->returnElement) {
1185 mpd_getNextReturnElement(connection);
1188 if (connection->returnElement) {
1189 if (strcmp(connection->returnElement->name, "file") == 0) {
1190 entity = mpd_newInfoEntity();
1191 entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1192 entity->info.song = mpd_newSong();
1193 entity->info.song->file = strdup(connection->returnElement->value);
1194 } else if (strcmp(connection->returnElement->name, "directory") == 0) {
1195 entity = mpd_newInfoEntity();
1196 entity->type = MPD_INFO_ENTITY_TYPE_DIRECTORY;
1197 entity->info.directory = mpd_newDirectory();
1198 entity->info.directory->path =
1199 strdup(connection->returnElement->value);
1200 } else if (strcmp(connection->returnElement->name, "playlist") == 0) {
1201 entity = mpd_newInfoEntity();
1202 entity->type = MPD_INFO_ENTITY_TYPE_PLAYLISTFILE;
1203 entity->info.playlistFile = mpd_newPlaylistFile();
1204 entity->info.playlistFile->path =
1205 strdup(connection->returnElement->value);
1206 } else if (strcmp(connection->returnElement->name, "cpos") == 0) {
1207 entity = mpd_newInfoEntity();
1208 entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1209 entity->info.song = mpd_newSong();
1210 entity->info.song->pos = atoi(connection->returnElement->value);
1212 connection->error = 1;
1213 strcpy(connection->errorStr, "problem parsing song info");
1220 mpd_getNextReturnElement(connection);
1221 while (connection->returnElement) {
1222 mpd_ReturnElement *re = connection->returnElement;
1224 if (strcmp(re->name, "file") == 0) {
1226 } else if (strcmp(re->name, "directory") == 0) {
1228 } else if (strcmp(re->name, "playlist") == 0) {
1230 } else if (strcmp(re->name, "cpos") == 0) {
1234 if (entity->type == MPD_INFO_ENTITY_TYPE_SONG && strlen(re->value)) {
1235 if (!entity->info.song->artist
1236 && strcmp(re->name, "Artist") == 0) {
1237 entity->info.song->artist = strdup(re->value);
1238 } else if (!entity->info.song->album
1239 && strcmp(re->name, "Album") == 0) {
1240 entity->info.song->album = strdup(re->value);
1241 } else if (!entity->info.song->title
1242 && strcmp(re->name, "Title") == 0) {
1243 entity->info.song->title = strdup(re->value);
1244 } else if (!entity->info.song->track
1245 && strcmp(re->name, "Track") == 0) {
1246 entity->info.song->track = strdup(re->value);
1247 } else if (!entity->info.song->name
1248 && strcmp(re->name, "Name") == 0) {
1249 entity->info.song->name = strdup(re->value);
1250 } else if (entity->info.song->time == MPD_SONG_NO_TIME
1251 && strcmp(re->name, "Time") == 0) {
1252 entity->info.song->time = atoi(re->value);
1253 } else if (entity->info.song->pos == MPD_SONG_NO_NUM
1254 && strcmp(re->name, "Pos") == 0) {
1255 entity->info.song->pos = atoi(re->value);
1256 } else if (entity->info.song->id == MPD_SONG_NO_ID
1257 && strcmp(re->name, "Id") == 0) {
1258 entity->info.song->id = atoi(re->value);
1259 } else if (!entity->info.song->date
1260 && strcmp(re->name, "Date") == 0) {
1261 entity->info.song->date = strdup(re->value);
1262 } else if (!entity->info.song->genre
1263 && strcmp(re->name, "Genre") == 0) {
1264 entity->info.song->genre = strdup(re->value);
1265 } else if (!entity->info.song->composer
1266 && strcmp(re->name, "Composer") == 0) {
1267 entity->info.song->composer = strdup(re->value);
1268 } else if (!entity->info.song->performer
1269 && strcmp(re->name, "Performer") == 0) {
1270 entity->info.song->performer = strdup(re->value);
1271 } else if (!entity->info.song->disc
1272 && strcmp(re->name, "Disc") == 0) {
1273 entity->info.song->disc = strdup(re->value);
1274 } else if (!entity->info.song->comment
1275 && strcmp(re->name, "Comment") == 0) {
1276 entity->info.song->comment = strdup(re->value);
1278 } else if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1279 } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1282 mpd_getNextReturnElement(connection);
1288 static char *mpd_getNextReturnElementNamed(mpd_Connection *connection,
1291 if (connection->doneProcessing
1292 || (connection->listOks && connection->doneListOk)) {
1296 mpd_getNextReturnElement(connection);
1297 while (connection->returnElement) {
1298 mpd_ReturnElement *re = connection->returnElement;
1300 if (strcmp(re->name, name) == 0) {
1301 return strdup(re->value);
1303 mpd_getNextReturnElement(connection);
1309 char *mpd_getNextTag(mpd_Connection *connection, int type)
1311 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES
1312 || type == MPD_TAG_ITEM_ANY) {
1315 if (type == MPD_TAG_ITEM_FILENAME) {
1316 return mpd_getNextReturnElementNamed(connection, "file");
1318 return mpd_getNextReturnElementNamed(connection, mpdTagItemKeys[type]);
1321 char *mpd_getNextArtist(mpd_Connection *connection)
1323 return mpd_getNextReturnElementNamed(connection, "Artist");
1326 char *mpd_getNextAlbum(mpd_Connection *connection)
1328 return mpd_getNextReturnElementNamed(connection, "Album");
1331 void mpd_sendPlaylistInfoCommand(mpd_Connection *connection, int songPos)
1333 int len = strlen("playlistinfo") + 2 + INTLEN + 3;
1334 char *string = malloc(len);
1336 snprintf(string, len, "playlistinfo \"%i\"\n", songPos);
1337 mpd_sendInfoCommand(connection, string);
1341 void mpd_sendPlaylistIdCommand(mpd_Connection *connection, int id)
1343 int len = strlen("playlistid") + 2 + INTLEN + 3;
1344 char *string = malloc(len);
1346 snprintf(string, len, "playlistid \"%i\"\n", id);
1347 mpd_sendInfoCommand(connection, string);
1351 void mpd_sendPlChangesCommand(mpd_Connection *connection, long long playlist)
1353 int len = strlen("plchanges") + 2 + LONGLONGLEN + 3;
1354 char *string = malloc(len);
1356 snprintf(string, len, "plchanges \"%lld\"\n", playlist);
1357 mpd_sendInfoCommand(connection, string);
1361 void mpd_sendPlChangesPosIdCommand(mpd_Connection *connection,
1364 int len = strlen("plchangesposid") + 2 + LONGLONGLEN + 3;
1365 char *string = malloc(len);
1367 snprintf(string, len, "plchangesposid \"%lld\"\n", playlist);
1368 mpd_sendInfoCommand(connection, string);
1372 void mpd_sendListallCommand(mpd_Connection *connection, const char *dir)
1374 char *sDir = mpd_sanitizeArg(dir);
1375 int len = strlen("listall") + 2 + strlen(sDir) + 3;
1376 char *string = malloc(len);
1378 snprintf(string, len, "listall \"%s\"\n", sDir);
1379 mpd_sendInfoCommand(connection, string);
1384 void mpd_sendListallInfoCommand(mpd_Connection *connection, const char *dir)
1386 char *sDir = mpd_sanitizeArg(dir);
1387 int len = strlen("listallinfo") + 2 + strlen(sDir) + 3;
1388 char *string = malloc(len);
1390 snprintf(string, len, "listallinfo \"%s\"\n", sDir);
1391 mpd_sendInfoCommand(connection, string);
1396 void mpd_sendLsInfoCommand(mpd_Connection *connection, const char *dir)
1398 char *sDir = mpd_sanitizeArg(dir);
1399 int len = strlen("lsinfo") + 2 + strlen(sDir) + 3;
1400 char *string = malloc(len);
1402 snprintf(string, len, "lsinfo \"%s\"\n", sDir);
1403 mpd_sendInfoCommand(connection, string);
1408 void mpd_sendCurrentSongCommand(mpd_Connection *connection)
1410 mpd_executeCommand(connection, "currentsong\n");
1413 void mpd_sendSearchCommand(mpd_Connection *connection, int table,
1416 mpd_startSearch(connection, 0);
1417 mpd_addConstraintSearch(connection, table, str);
1418 mpd_commitSearch(connection);
1421 void mpd_sendFindCommand(mpd_Connection *connection, int table,
1424 mpd_startSearch(connection, 1);
1425 mpd_addConstraintSearch(connection, table, str);
1426 mpd_commitSearch(connection);
1429 void mpd_sendListCommand(mpd_Connection *connection, int table,
1436 if (table == MPD_TABLE_ARTIST) {
1437 strcpy(st, "artist");
1438 } else if (table == MPD_TABLE_ALBUM) {
1439 strcpy(st, "album");
1441 connection->error = 1;
1442 strcpy(connection->errorStr, "unknown table for list");
1446 char *sanitArg1 = mpd_sanitizeArg(arg1);
1448 len = strlen("list") + 1 + strlen(sanitArg1) + 2 + strlen(st) + 3;
1449 string = malloc(len);
1450 snprintf(string, len, "list %s \"%s\"\n", st, sanitArg1);
1453 len = strlen("list") + 1 + strlen(st) + 2;
1454 string = malloc(len);
1455 snprintf(string, len, "list %s\n", st);
1457 mpd_sendInfoCommand(connection, string);
1461 void mpd_sendAddCommand(mpd_Connection *connection, const char *file)
1463 char *sFile = mpd_sanitizeArg(file);
1464 int len = strlen("add") + 2 + strlen(sFile) + 3;
1465 char *string = malloc(len);
1467 snprintf(string, len, "add \"%s\"\n", sFile);
1468 mpd_executeCommand(connection, string);
1473 int mpd_sendAddIdCommand(mpd_Connection *connection, const char *file)
1476 char *sFile = mpd_sanitizeArg(file);
1477 int len = strlen("addid") + 2 + strlen(sFile) + 3;
1478 char *string = malloc(len);
1480 snprintf(string, len, "addid \"%s\"\n", sFile);
1481 mpd_sendInfoCommand(connection, string);
1485 string = mpd_getNextReturnElementNamed(connection, "Id");
1487 retval = atoi(string);
1494 void mpd_sendDeleteCommand(mpd_Connection *connection, int songPos)
1496 int len = strlen("delete") + 2 + INTLEN + 3;
1497 char *string = malloc(len);
1499 snprintf(string, len, "delete \"%i\"\n", songPos);
1500 mpd_sendInfoCommand(connection, string);
1504 void mpd_sendDeleteIdCommand(mpd_Connection *connection, int id)
1506 int len = strlen("deleteid") + 2 + INTLEN + 3;
1507 char *string = malloc(len);
1509 snprintf(string, len, "deleteid \"%i\"\n", id);
1510 mpd_sendInfoCommand(connection, string);
1514 void mpd_sendSaveCommand(mpd_Connection *connection, const char *name)
1516 char *sName = mpd_sanitizeArg(name);
1517 int len = strlen("save") + 2 + strlen(sName) + 3;
1518 char *string = malloc(len);
1520 snprintf(string, len, "save \"%s\"\n", sName);
1521 mpd_executeCommand(connection, string);
1526 void mpd_sendLoadCommand(mpd_Connection *connection, const char *name)
1528 char *sName = mpd_sanitizeArg(name);
1529 int len = strlen("load") + 2 + strlen(sName) + 3;
1530 char *string = malloc(len);
1532 snprintf(string, len, "load \"%s\"\n", sName);
1533 mpd_executeCommand(connection, string);
1538 void mpd_sendRmCommand(mpd_Connection *connection, const char *name)
1540 char *sName = mpd_sanitizeArg(name);
1541 int len = strlen("rm") + 2 + strlen(sName) + 3;
1542 char *string = malloc(len);
1544 snprintf(string, len, "rm \"%s\"\n", sName);
1545 mpd_executeCommand(connection, string);
1550 void mpd_sendRenameCommand(mpd_Connection *connection, const char *from,
1553 char *sFrom = mpd_sanitizeArg(from);
1554 char *sTo = mpd_sanitizeArg(to);
1555 int len = strlen("rename") + 2 + strlen(sFrom) + 3 + strlen(sTo) + 3;
1556 char *string = malloc(len);
1558 snprintf(string, len, "rename \"%s\" \"%s\"\n", sFrom, sTo);
1559 mpd_executeCommand(connection, string);
1565 void mpd_sendShuffleCommand(mpd_Connection *connection)
1567 mpd_executeCommand(connection, "shuffle\n");
1570 void mpd_sendClearCommand(mpd_Connection *connection)
1572 mpd_executeCommand(connection, "clear\n");
1575 void mpd_sendPlayCommand(mpd_Connection *connection, int songPos)
1577 int len = strlen("play") + 2 + INTLEN + 3;
1578 char *string = malloc(len);
1580 snprintf(string, len, "play \"%i\"\n", songPos);
1581 mpd_sendInfoCommand(connection, string);
1585 void mpd_sendPlayIdCommand(mpd_Connection *connection, int id)
1587 int len = strlen("playid") + 2 + INTLEN + 3;
1588 char *string = malloc(len);
1590 snprintf(string, len, "playid \"%i\"\n", id);
1591 mpd_sendInfoCommand(connection, string);
1595 void mpd_sendStopCommand(mpd_Connection *connection)
1597 mpd_executeCommand(connection, "stop\n");
1600 void mpd_sendPauseCommand(mpd_Connection *connection, int pauseMode)
1602 int len = strlen("pause") + 2 + INTLEN + 3;
1603 char *string = malloc(len);
1605 snprintf(string, len, "pause \"%i\"\n", pauseMode);
1606 mpd_executeCommand(connection, string);
1610 void mpd_sendNextCommand(mpd_Connection *connection)
1612 mpd_executeCommand(connection, "next\n");
1615 void mpd_sendMoveCommand(mpd_Connection *connection, int from, int to)
1617 int len = strlen("move") + 2 + INTLEN + 3 + INTLEN + 3;
1618 char *string = malloc(len);
1620 snprintf(string, len, "move \"%i\" \"%i\"\n", from, to);
1621 mpd_sendInfoCommand(connection, string);
1625 void mpd_sendMoveIdCommand(mpd_Connection *connection, int id, int to)
1627 int len = strlen("moveid") + 2 + INTLEN + 3 + INTLEN + 3;
1628 char *string = malloc(len);
1630 snprintf(string, len, "moveid \"%i\" \"%i\"\n", id, to);
1631 mpd_sendInfoCommand(connection, string);
1635 void mpd_sendSwapCommand(mpd_Connection *connection, int song1, int song2)
1637 int len = strlen("swap") + 2 + INTLEN + 3 + INTLEN + 3;
1638 char *string = malloc(len);
1640 snprintf(string, len, "swap \"%i\" \"%i\"\n", song1, song2);
1641 mpd_sendInfoCommand(connection, string);
1645 void mpd_sendSwapIdCommand(mpd_Connection *connection, int id1, int id2)
1647 int len = strlen("swapid") + 2 + INTLEN + 3 + INTLEN + 3;
1648 char *string = malloc(len);
1650 snprintf(string, len, "swapid \"%i\" \"%i\"\n", id1, id2);
1651 mpd_sendInfoCommand(connection, string);
1655 void mpd_sendSeekCommand(mpd_Connection *connection, int song, int seek_time)
1657 int len = strlen("seek") + 2 + INTLEN + 3 + INTLEN + 3;
1658 char *string = malloc(len);
1660 snprintf(string, len, "seek \"%i\" \"%i\"\n", song, seek_time);
1661 mpd_sendInfoCommand(connection, string);
1665 void mpd_sendSeekIdCommand(mpd_Connection *connection, int id, int seek_time)
1667 int len = strlen("seekid") + 2 + INTLEN + 3 + INTLEN + 3;
1668 char *string = malloc(len);
1670 snprintf(string, len, "seekid \"%i\" \"%i\"\n", id, seek_time);
1671 mpd_sendInfoCommand(connection, string);
1675 void mpd_sendUpdateCommand(mpd_Connection *connection, char *path)
1677 char *sPath = mpd_sanitizeArg(path);
1678 int len = strlen("update") + 2 + strlen(sPath) + 3;
1679 char *string = malloc(len);
1681 snprintf(string, len, "update \"%s\"\n", sPath);
1682 mpd_sendInfoCommand(connection, string);
1687 int mpd_getUpdateId(mpd_Connection *connection)
1692 jobid = mpd_getNextReturnElementNamed(connection, "updating_db");
1701 void mpd_sendPrevCommand(mpd_Connection *connection)
1703 mpd_executeCommand(connection, "previous\n");
1706 void mpd_sendRepeatCommand(mpd_Connection *connection, int repeatMode)
1708 int len = strlen("repeat") + 2 + INTLEN + 3;
1709 char *string = malloc(len);
1711 snprintf(string, len, "repeat \"%i\"\n", repeatMode);
1712 mpd_executeCommand(connection, string);
1716 void mpd_sendRandomCommand(mpd_Connection *connection, int randomMode)
1718 int len = strlen("random") + 2 + INTLEN + 3;
1719 char *string = malloc(len);
1721 snprintf(string, len, "random \"%i\"\n", randomMode);
1722 mpd_executeCommand(connection, string);
1726 void mpd_sendSetvolCommand(mpd_Connection *connection, int volumeChange)
1728 int len = strlen("setvol") + 2 + INTLEN + 3;
1729 char *string = malloc(len);
1731 snprintf(string, len, "setvol \"%i\"\n", volumeChange);
1732 mpd_executeCommand(connection, string);
1736 void mpd_sendVolumeCommand(mpd_Connection *connection, int volumeChange)
1738 int len = strlen("volume") + 2 + INTLEN + 3;
1739 char *string = malloc(len);
1741 snprintf(string, len, "volume \"%i\"\n", volumeChange);
1742 mpd_executeCommand(connection, string);
1746 void mpd_sendCrossfadeCommand(mpd_Connection *connection, int seconds)
1748 int len = strlen("crossfade") + 2 + INTLEN + 3;
1749 char *string = malloc(len);
1751 snprintf(string, len, "crossfade \"%i\"\n", seconds);
1752 mpd_executeCommand(connection, string);
1756 void mpd_sendPasswordCommand(mpd_Connection *connection, const char *pass)
1758 char *sPass = mpd_sanitizeArg(pass);
1759 int len = strlen("password") + 2 + strlen(sPass) + 3;
1760 char *string = malloc(len);
1762 snprintf(string, len, "password \"%s\"\n", sPass);
1763 mpd_executeCommand(connection, string);
1768 void mpd_sendCommandListBegin(mpd_Connection *connection)
1770 if (connection->commandList) {
1771 strcpy(connection->errorStr, "already in command list mode");
1772 connection->error = 1;
1775 connection->commandList = COMMAND_LIST;
1776 mpd_executeCommand(connection, "command_list_begin\n");
1779 void mpd_sendCommandListOkBegin(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_OK;
1787 mpd_executeCommand(connection, "command_list_ok_begin\n");
1788 connection->listOks = 0;
1791 void mpd_sendCommandListEnd(mpd_Connection *connection)
1793 if (!connection->commandList) {
1794 strcpy(connection->errorStr, "not in command list mode");
1795 connection->error = 1;
1798 connection->commandList = 0;
1799 mpd_executeCommand(connection, "command_list_end\n");
1802 void mpd_sendOutputsCommand(mpd_Connection *connection)
1804 mpd_executeCommand(connection, "outputs\n");
1807 mpd_OutputEntity *mpd_getNextOutput(mpd_Connection *connection)
1809 mpd_OutputEntity *output = NULL;
1811 if (connection->doneProcessing
1812 || (connection->listOks && connection->doneListOk)) {
1816 if (connection->error) {
1820 output = malloc(sizeof(mpd_OutputEntity));
1822 output->name = NULL;
1823 output->enabled = 0;
1825 if (!connection->returnElement) {
1826 mpd_getNextReturnElement(connection);
1829 while (connection->returnElement) {
1830 mpd_ReturnElement *re = connection->returnElement;
1832 if (strcmp(re->name, "outputid") == 0) {
1833 if (output != NULL && output->id >= 0) {
1836 output->id = atoi(re->value);
1837 } else if (strcmp(re->name, "outputname") == 0) {
1838 output->name = strdup(re->value);
1839 } else if (strcmp(re->name, "outputenabled") == 0) {
1840 output->enabled = atoi(re->value);
1843 mpd_getNextReturnElement(connection);
1844 if (connection->error) {
1853 void mpd_sendEnableOutputCommand(mpd_Connection *connection, int outputId)
1855 int len = strlen("enableoutput") + 2 + INTLEN + 3;
1856 char *string = malloc(len);
1858 snprintf(string, len, "enableoutput \"%i\"\n", outputId);
1859 mpd_executeCommand(connection, string);
1863 void mpd_sendDisableOutputCommand(mpd_Connection *connection, int outputId)
1865 int len = strlen("disableoutput") + 2 + INTLEN + 3;
1866 char *string = malloc(len);
1868 snprintf(string, len, "disableoutput \"%i\"\n", outputId);
1869 mpd_executeCommand(connection, string);
1873 void mpd_freeOutputElement(mpd_OutputEntity *output)
1879 /** odd naming, but it gets the not allowed commands */
1880 void mpd_sendNotCommandsCommand(mpd_Connection *connection)
1882 mpd_executeCommand(connection, "notcommands\n");
1885 /** odd naming, but it gets the allowed commands */
1886 void mpd_sendCommandsCommand(mpd_Connection *connection)
1888 mpd_executeCommand(connection, "commands\n");
1891 /** Get the next returned command */
1892 char *mpd_getNextCommand(mpd_Connection *connection)
1894 return mpd_getNextReturnElementNamed(connection, "command");
1897 void mpd_sendUrlHandlersCommand(mpd_Connection *connection)
1899 mpd_executeCommand(connection, "urlhandlers\n");
1902 char *mpd_getNextHandler(mpd_Connection *connection)
1904 return mpd_getNextReturnElementNamed(connection, "handler");
1907 void mpd_sendTagTypesCommand(mpd_Connection *connection)
1909 mpd_executeCommand(connection, "tagtypes\n");
1912 char *mpd_getNextTagType(mpd_Connection *connection)
1914 return mpd_getNextReturnElementNamed(connection, "tagtype");
1917 void mpd_startSearch(mpd_Connection *connection, int exact)
1919 if (connection->request) {
1920 strcpy(connection->errorStr, "search already in progress");
1921 connection->error = 1;
1926 connection->request = strdup("find");
1928 connection->request = strdup("search");
1932 void mpd_startStatsSearch(mpd_Connection *connection)
1934 if (connection->request) {
1935 strcpy(connection->errorStr, "search already in progress");
1936 connection->error = 1;
1940 connection->request = strdup("count");
1943 void mpd_startPlaylistSearch(mpd_Connection *connection, int exact)
1945 if (connection->request) {
1946 strcpy(connection->errorStr, "search already in progress");
1947 connection->error = 1;
1952 connection->request = strdup("playlistfind");
1954 connection->request = strdup("playlistsearch");
1958 void mpd_startFieldSearch(mpd_Connection *connection, int type)
1960 const char *strtype;
1963 if (connection->request) {
1964 strcpy(connection->errorStr, "search already in progress");
1965 connection->error = 1;
1969 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
1970 strcpy(connection->errorStr, "invalid type specified");
1971 connection->error = 1;
1975 strtype = mpdTagItemKeys[type];
1977 len = 5 + strlen(strtype) + 1;
1978 connection->request = malloc(len);
1980 snprintf(connection->request, len, "list %c%s", tolower(strtype[0]),
1984 void mpd_addConstraintSearch(mpd_Connection *connection, int type,
1987 const char *strtype;
1992 if (!connection->request) {
1993 strcpy(connection->errorStr, "no search in progress");
1994 connection->error = 1;
1998 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
1999 strcpy(connection->errorStr, "invalid type specified");
2000 connection->error = 1;
2005 strcpy(connection->errorStr, "no name specified");
2006 connection->error = 1;
2010 string = strdup(connection->request);
2011 strtype = mpdTagItemKeys[type];
2012 arg = mpd_sanitizeArg(name);
2014 len = strlen(string) + 1 + strlen(strtype) + 2 + strlen(arg) + 2;
2015 connection->request = realloc(connection->request, len);
2016 snprintf(connection->request, len, "%s %c%s \"%s\"", string,
2017 tolower(strtype[0]), strtype + 1, arg);
2023 void mpd_commitSearch(mpd_Connection *connection)
2027 if (!connection->request) {
2028 strcpy(connection->errorStr, "no search in progress");
2029 connection->error = 1;
2033 len = strlen(connection->request) + 2;
2034 connection->request = realloc(connection->request, len);
2035 connection->request[len - 2] = '\n';
2036 connection->request[len - 1] = '\0';
2037 mpd_sendInfoCommand(connection, connection->request);
2039 free(connection->request);
2040 connection->request = NULL;
2044 * @param connection a MpdConnection
2045 * @param path the path to the playlist.
2047 * List the content, with full metadata, of a stored playlist. */
2048 void mpd_sendListPlaylistInfoCommand(mpd_Connection *connection, char *path)
2050 char *arg = mpd_sanitizeArg(path);
2051 int len = strlen("listplaylistinfo") + 2 + strlen(arg) + 3;
2052 char *query = malloc(len);
2054 snprintf(query, len, "listplaylistinfo \"%s\"\n", arg);
2055 mpd_sendInfoCommand(connection, query);
2061 * @param connection a MpdConnection
2062 * @param path the path to the playlist.
2064 * List the content of a stored playlist. */
2065 void mpd_sendListPlaylistCommand(mpd_Connection *connection, char *path)
2067 char *arg = mpd_sanitizeArg(path);
2068 int len = strlen("listplaylist") + 2 + strlen(arg) + 3;
2069 char *query = malloc(len);
2071 snprintf(query, len, "listplaylist \"%s\"\n", arg);
2072 mpd_sendInfoCommand(connection, query);
2077 void mpd_sendPlaylistClearCommand(mpd_Connection *connection, char *path)
2079 char *sPath = mpd_sanitizeArg(path);
2080 int len = strlen("playlistclear") + 2 + strlen(sPath) + 3;
2081 char *string = malloc(len);
2083 snprintf(string, len, "playlistclear \"%s\"\n", sPath);
2084 mpd_executeCommand(connection, string);
2089 void mpd_sendPlaylistAddCommand(mpd_Connection *connection, char *playlist,
2092 char *sPlaylist = mpd_sanitizeArg(playlist);
2093 char *sPath = mpd_sanitizeArg(path);
2094 int len = strlen("playlistadd") + 2 + strlen(sPlaylist) + 3 +
2096 char *string = malloc(len);
2098 snprintf(string, len, "playlistadd \"%s\" \"%s\"\n", sPlaylist, sPath);
2099 mpd_executeCommand(connection, string);
2105 void mpd_sendPlaylistMoveCommand(mpd_Connection *connection, char *playlist,
2108 char *sPlaylist = mpd_sanitizeArg(playlist);
2109 int len = strlen("playlistmove") + 2 + strlen(sPlaylist) + 3 + INTLEN +
2111 char *string = malloc(len);
2113 snprintf(string, len, "playlistmove \"%s\" \"%i\" \"%i\"\n", sPlaylist,
2115 mpd_executeCommand(connection, string);
2120 void mpd_sendPlaylistDeleteCommand(mpd_Connection *connection, char *playlist,
2123 char *sPlaylist = mpd_sanitizeArg(playlist);
2124 int len = strlen("playlistdelete") + 2 + strlen(sPlaylist) + 3 +
2126 char *string = malloc(len);
2128 snprintf(string, len, "playlistdelete \"%s\" \"%i\"\n", sPlaylist, pos);
2129 mpd_executeCommand(connection, string);