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 #include "libmpdclient.h"
36 #include <sys/types.h>
38 #include <sys/param.h>
46 # include <ws2tcpip.h>
49 # include <netinet/in.h>
50 # include <arpa/inet.h>
51 # include <sys/socket.h>
55 /* (bits + 1) / 3 (plus the sign character) */
56 #define INTLEN ((sizeof(int) * CHAR_BIT + 1) / 3 + 1)
57 #define LONGLONGLEN ((sizeof(long long) * CHAR_BIT + 1) / 3 + 1)
59 #define COMMAND_LIST 1
60 #define COMMAND_LIST_OK 2
69 # define MSG_DONTWAIT 0
73 # define SELECT_ERRNO_IGNORE (errno == WSAEINTR || errno == WSAEINPROGRESS)
74 # define SENDRECV_ERRNO_IGNORE SELECT_ERRNO_IGNORE
76 # define SELECT_ERRNO_IGNORE (errno == EINTR)
77 # define SENDRECV_ERRNO_IGNORE (errno == EINTR || errno == EAGAIN)
78 # define winsock_dll_error(c) 0
79 # define closesocket(s) close(s)
80 # define WSACleanup() do { /* nothing */ } while (0)
84 static int winsock_dll_error(mpd_Connection *connection)
88 if ((WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0
89 || LOBYTE(wsaData.wVersion) != 2
90 || HIBYTE(wsaData.wVersion) != 2) {
91 strcpy(connection->errorStr, "Could not find usable WinSock DLL.");
92 connection->error = MPD_ERROR_SYSTEM;
98 static int do_connect_fail(mpd_Connection *connection,
99 const struct sockaddr *serv_addr, int addrlen)
101 int iMode = 1; /* 0 = blocking, else non-blocking */
103 ioctlsocket(connection->sock, FIONBIO, (u_long FAR *) &iMode);
104 return (connect(connection->sock, serv_addr, addrlen) == SOCKET_ERROR
105 && WSAGetLastError() != WSAEWOULDBLOCK);
107 #else /* !WIN32 (sane operating systems) */
108 static int do_connect_fail(mpd_Connection *connection,
109 const struct sockaddr *serv_addr, int addrlen)
111 int flags = fcntl(connection->sock, F_GETFL, 0);
113 fcntl(connection->sock, F_SETFL, flags | O_NONBLOCK);
114 return (connect(connection->sock, serv_addr, addrlen) < 0
115 && errno != EINPROGRESS);
120 static int mpd_connect(mpd_Connection *connection, const char *host, int port,
124 char service[INTLEN + 1];
125 struct addrinfo hints;
126 struct addrinfo *res = NULL;
127 struct addrinfo *addrinfo = NULL;
130 hints.ai_flags = AI_ADDRCONFIG;
131 hints.ai_family = PF_UNSPEC;
132 hints.ai_socktype = SOCK_STREAM;
133 hints.ai_protocol = IPPROTO_TCP;
134 hints.ai_addrlen = 0;
135 hints.ai_addr = NULL;
136 hints.ai_canonname = NULL;
137 hints.ai_next = NULL;
139 snprintf(service, sizeof(service), "%i", port);
141 error = getaddrinfo(host, service, &hints, &addrinfo);
144 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
145 "host \"%s\" not found: %s", host, gai_strerror(error));
146 connection->error = MPD_ERROR_UNKHOST;
150 for (res = addrinfo; res; res = res->ai_next) {
152 connection->sock = socket(res->ai_family, SOCK_STREAM,
154 if (connection->sock < 0) {
155 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
156 "problems creating socket: %s", strerror(errno));
157 connection->error = MPD_ERROR_SYSTEM;
158 freeaddrinfo(addrinfo);
162 mpd_setConnectionTimeout(connection, timeout);
165 if (do_connect_fail(connection, res->ai_addr, res->ai_addrlen)) {
166 /* try the next address family */
167 closesocket(connection->sock);
168 connection->sock = -1;
173 freeaddrinfo(addrinfo);
175 if (connection->sock < 0) {
176 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
177 "problems connecting to \"%s\" on port %i: %s", host, port,
179 connection->error = MPD_ERROR_CONNPORT;
186 #else /* !MPD_HAVE_GAI */
187 static int mpd_connect(mpd_Connection *connection, const char *host, int port,
191 struct sockaddr *dest;
193 struct sockaddr_in sin;
195 if (!(he = gethostbyname(host))) {
196 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
197 "host \"%s\" not found", host);
198 connection->error = MPD_ERROR_UNKHOST;
202 memset(&sin, 0, sizeof(struct sockaddr_in));
203 /* dest.sin_family = he->h_addrtype; */
204 sin.sin_family = AF_INET;
205 sin.sin_port = htons(port);
207 switch (he->h_addrtype) {
209 memcpy((char *) &sin.sin_addr.s_addr, (char *) he->h_addr,
211 dest = (struct sockaddr *) &sin;
212 destlen = sizeof(struct sockaddr_in);
215 strcpy(connection->errorStr, "address type is not IPv4");
216 connection->error = MPD_ERROR_SYSTEM;
221 if ((connection->sock = socket(dest->sa_family, SOCK_STREAM, 0)) < 0) {
222 strcpy(connection->errorStr, "problems creating socket");
223 connection->error = MPD_ERROR_SYSTEM;
227 mpd_setConnectionTimeout(connection, timeout);
230 if (do_connect_fail(connection, dest, destlen)) {
231 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
232 "problems connecting to \"%s\" on port %i", host, port);
233 connection->error = MPD_ERROR_CONNPORT;
239 #endif /* !MPD_HAVE_GAI */
241 char *mpdTagItemKeys[MPD_TAG_NUM_OF_ITEM_TYPES] = {
257 static char *mpd_sanitizeArg(const char *arg)
261 register const char *c;
264 /* instead of counting in that loop above,
265 * just use a bit more memory and halve running time */
266 ret = malloc(strlen(arg) * 2 + 1);
270 for (i = strlen(arg) + 1; i != 0; --i) {
271 if (*c == '"' || *c == '\\') {
280 static mpd_ReturnElement *mpd_newReturnElement(const char *name,
283 mpd_ReturnElement *ret = malloc(sizeof(mpd_ReturnElement));
285 ret->name = strdup(name);
286 ret->value = strdup(value);
291 static void mpd_freeReturnElement(mpd_ReturnElement *re)
298 void mpd_setConnectionTimeout(mpd_Connection *connection, float timeout)
300 connection->timeout.tv_sec = (int) timeout;
301 connection->timeout.tv_usec =
302 (int) ((timeout - connection->timeout.tv_sec) * 1e6 + 0.5);
305 static int mpd_parseWelcome(mpd_Connection *connection, const char *host,
306 int port, /* char *rt, */ char *output)
312 if (strncmp(output, MPD_WELCOME_MESSAGE, strlen(MPD_WELCOME_MESSAGE))) {
313 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
314 "mpd not running on port %i on host \"%s\"", port, host);
315 connection->error = MPD_ERROR_NOTMPD;
319 tmp = &output[strlen(MPD_WELCOME_MESSAGE)];
321 for (i = 0; i < 3; i++) {
323 connection->version[i] = strtol(tmp, &test, 10);
326 if (!tmp || (test[0] != '.' && test[0] != '\0')) {
327 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
328 "error parsing version number at \"%s\"",
329 &output[strlen(MPD_WELCOME_MESSAGE)]);
330 connection->error = MPD_ERROR_NOTMPD;
339 mpd_Connection *mpd_newConnection(const char *host, int port, float timeout)
344 mpd_Connection *connection = malloc(sizeof(mpd_Connection));
348 strcpy(connection->buffer, "");
349 connection->buflen = 0;
350 connection->bufstart = 0;
351 strcpy(connection->errorStr, "");
352 connection->error = 0;
353 connection->doneProcessing = 0;
354 connection->commandList = 0;
355 connection->listOks = 0;
356 connection->doneListOk = 0;
357 connection->returnElement = NULL;
358 connection->request = NULL;
360 if (winsock_dll_error(connection)) {
364 if (mpd_connect(connection, host, port, timeout) < 0) {
368 while (!(rt = strstr(connection->buffer, "\n"))) {
369 tv.tv_sec = connection->timeout.tv_sec;
370 tv.tv_usec = connection->timeout.tv_usec;
372 FD_SET(connection->sock, &fds);
373 if ((err = select(connection->sock + 1, &fds, NULL, NULL, &tv)) == 1) {
376 readed = recv(connection->sock,
377 &(connection->buffer[connection->buflen]),
378 MPD_BUFFER_MAX_LENGTH - connection->buflen, 0);
380 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
381 "problems getting a response from \"%s\" on port %i : %s",
382 host, port, strerror(errno));
383 connection->error = MPD_ERROR_NORESPONSE;
386 connection->buflen += readed;
387 connection->buffer[connection->buflen] = '\0';
388 } else if (err < 0) {
389 if (SELECT_ERRNO_IGNORE) {
392 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
393 "problems connecting to \"%s\" on port %i", host, port);
394 connection->error = MPD_ERROR_CONNPORT;
397 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
398 "timeout in attempting to get a response from \"%s\" on "
399 "port %i", host, port);
400 connection->error = MPD_ERROR_NORESPONSE;
406 output = strdup(connection->buffer);
407 strcpy(connection->buffer, rt + 1);
408 connection->buflen = strlen(connection->buffer);
410 if (mpd_parseWelcome(connection, host, port, /* rt, */ output) == 0) {
411 connection->doneProcessing = 1;
419 void mpd_clearError(mpd_Connection *connection)
421 connection->error = 0;
422 connection->errorStr[0] = '\0';
425 void mpd_closeConnection(mpd_Connection *connection)
427 closesocket(connection->sock);
428 if (connection->returnElement) {
429 free(connection->returnElement);
431 if (connection->request) {
432 free(connection->request);
438 static void mpd_executeCommand(mpd_Connection *connection, char *command)
443 char *commandPtr = command;
444 int commandLen = strlen(command);
446 if (!connection->doneProcessing && !connection->commandList) {
447 strcpy(connection->errorStr, "not done processing current command");
448 connection->error = 1;
452 mpd_clearError(connection);
455 FD_SET(connection->sock, &fds);
456 tv.tv_sec = connection->timeout.tv_sec;
457 tv.tv_usec = connection->timeout.tv_usec;
459 while ((ret = select(connection->sock + 1, NULL, &fds, NULL, &tv) == 1)
460 || (ret == -1 && SELECT_ERRNO_IGNORE)) {
461 ret = send(connection->sock, commandPtr, commandLen, MSG_DONTWAIT);
463 if (SENDRECV_ERRNO_IGNORE) {
466 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
467 "problems giving command \"%s\"", command);
468 connection->error = MPD_ERROR_SENDING;
475 if (commandLen <= 0) {
480 if (commandLen > 0) {
482 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
483 "timeout sending command \"%s\"", command);
484 connection->error = MPD_ERROR_TIMEOUT;
488 if (!connection->commandList) {
489 connection->doneProcessing = 0;
490 } else if (connection->commandList == COMMAND_LIST_OK) {
491 connection->listOks++;
495 static void mpd_getNextReturnElement(mpd_Connection *connection)
505 char *bufferCheck = NULL;
509 if (connection->returnElement) {
510 mpd_freeReturnElement(connection->returnElement);
512 connection->returnElement = NULL;
514 if (connection->doneProcessing
515 || (connection->listOks && connection->doneListOk)) {
516 strcpy(connection->errorStr,
517 "already done processing current command");
518 connection->error = 1;
522 bufferCheck = connection->buffer + connection->bufstart;
523 while (connection->bufstart >= connection->buflen
524 || !(rt = strchr(bufferCheck, '\n'))) {
525 if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
526 memmove(connection->buffer,
527 connection->buffer + connection->bufstart,
528 connection->buflen - connection->bufstart + 1);
529 connection->buflen -= connection->bufstart;
530 connection->bufstart = 0;
532 if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
533 strcpy(connection->errorStr, "buffer overrun");
534 connection->error = MPD_ERROR_BUFFEROVERRUN;
535 connection->doneProcessing = 1;
536 connection->doneListOk = 0;
539 bufferCheck = connection->buffer + connection->buflen;
540 tv.tv_sec = connection->timeout.tv_sec;
541 tv.tv_usec = connection->timeout.tv_usec;
543 FD_SET(connection->sock, &fds);
544 if ((err = select(connection->sock + 1, &fds, NULL, NULL, &tv) == 1)) {
545 readed = recv(connection->sock,
546 connection->buffer + connection->buflen,
547 MPD_BUFFER_MAX_LENGTH - connection->buflen, MSG_DONTWAIT);
548 if (readed < 0 && SENDRECV_ERRNO_IGNORE) {
552 strcpy(connection->errorStr, "connection closed");
553 connection->error = MPD_ERROR_CONNCLOSED;
554 connection->doneProcessing = 1;
555 connection->doneListOk = 0;
558 connection->buflen += readed;
559 connection->buffer[connection->buflen] = '\0';
560 } else if (err < 0 && SELECT_ERRNO_IGNORE) {
563 strcpy(connection->errorStr, "connection timeout");
564 connection->error = MPD_ERROR_TIMEOUT;
565 connection->doneProcessing = 1;
566 connection->doneListOk = 0;
572 output = connection->buffer + connection->bufstart;
573 connection->bufstart = rt - connection->buffer + 1;
575 if (strcmp(output, "OK") == 0) {
576 if (connection->listOks > 0) {
577 strcpy(connection->errorStr, "expected more list_OK's");
578 connection->error = 1;
580 connection->listOks = 0;
581 connection->doneProcessing = 1;
582 connection->doneListOk = 0;
586 if (strcmp(output, "list_OK") == 0) {
587 if (!connection->listOks) {
588 strcpy(connection->errorStr, "got an unexpected list_OK");
589 connection->error = 1;
591 connection->doneListOk = 1;
592 connection->listOks--;
597 if (strncmp(output, "ACK", strlen("ACK")) == 0) {
602 strcpy(connection->errorStr, output);
603 connection->error = MPD_ERROR_ACK;
604 connection->errorCode = MPD_ACK_ERROR_UNK;
605 connection->errorAt = MPD_ERROR_AT_UNK;
606 connection->doneProcessing = 1;
607 connection->doneListOk = 0;
609 needle = strchr(output, '[');
613 val = strtol(needle + 1, &test, 10);
617 connection->errorCode = val;
618 val = strtol(test + 1, &test, 10);
622 connection->errorAt = val;
626 tok = strchr(output, ':');
635 if (value[0] == ' ') {
636 connection->returnElement = mpd_newReturnElement(name, &(value[1]));
638 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
639 "error parsing: %s:%s", name, value);
640 connection->error = 1;
644 void mpd_finishCommand(mpd_Connection *connection)
646 while (!connection->doneProcessing) {
647 if (connection->doneListOk) {
648 connection->doneListOk = 0;
650 mpd_getNextReturnElement(connection);
654 static void mpd_finishListOkCommand(mpd_Connection *connection)
656 while (!connection->doneProcessing && connection->listOks
657 && !connection->doneListOk) {
658 mpd_getNextReturnElement(connection);
662 int mpd_nextListOkCommand(mpd_Connection *connection)
664 mpd_finishListOkCommand(connection);
665 if (!connection->doneProcessing) {
666 connection->doneListOk = 0;
668 if (connection->listOks == 0 || connection->doneProcessing) {
674 void mpd_sendStatusCommand(mpd_Connection *connection)
676 mpd_executeCommand(connection, "status\n");
679 mpd_Status *mpd_getStatus(mpd_Connection *connection)
683 /* mpd_executeCommand(connection, "status\n");
684 if (connection->error) {
688 if (connection->doneProcessing
689 || (connection->listOks && connection->doneListOk)) {
693 if (!connection->returnElement) {
694 mpd_getNextReturnElement(connection);
697 status = malloc(sizeof(mpd_Status));
701 status->playlist = -1;
702 status->playlistLength = -1;
706 status->elapsedTime = 0;
707 status->totalTime = 0;
709 status->sampleRate = 0;
711 status->channels = 0;
712 status->crossfade = -1;
713 status->error = NULL;
714 status->updatingDb = 0;
716 if (connection->error) {
720 while (connection->returnElement) {
721 mpd_ReturnElement *re = connection->returnElement;
723 if (strcmp(re->name, "volume") == 0) {
724 status->volume = atoi(re->value);
725 } else if (strcmp(re->name, "repeat") == 0) {
726 status->repeat = atoi(re->value);
727 } else if (strcmp(re->name, "random") == 0) {
728 status->random = atoi(re->value);
729 } else if (strcmp(re->name, "playlist") == 0) {
730 status->playlist = strtol(re->value, NULL, 10);
731 } else if (strcmp(re->name, "playlistlength") == 0) {
732 status->playlistLength = atoi(re->value);
733 } else if (strcmp(re->name, "bitrate") == 0) {
734 status->bitRate = atoi(re->value);
735 } else if (strcmp(re->name, "state") == 0) {
736 if (strcmp(re->value, "play") == 0) {
737 status->state = MPD_STATUS_STATE_PLAY;
738 } else if (strcmp(re->value, "stop") == 0) {
739 status->state = MPD_STATUS_STATE_STOP;
740 } else if (strcmp(re->value, "pause") == 0) {
741 status->state = MPD_STATUS_STATE_PAUSE;
743 status->state = MPD_STATUS_STATE_UNKNOWN;
745 } else if (strcmp(re->name, "song") == 0) {
746 status->song = atoi(re->value);
747 } else if (strcmp(re->name, "songid") == 0) {
748 status->songid = atoi(re->value);
749 } else if (strcmp(re->name, "time") == 0) {
750 char *tok = strchr(re->value, ':');
752 /* the second strchr below is a safety check */
753 if (tok && (strchr(tok, 0) > (tok + 1))) {
754 /* atoi stops at the first non-[0-9] char: */
755 status->elapsedTime = atoi(re->value);
756 status->totalTime = atoi(tok + 1);
758 } else if (strcmp(re->name, "error") == 0) {
759 status->error = strdup(re->value);
760 } else if (strcmp(re->name, "xfade") == 0) {
761 status->crossfade = atoi(re->value);
762 } else if (strcmp(re->name, "updating_db") == 0) {
763 status->updatingDb = atoi(re->value);
764 } else if (strcmp(re->name, "audio") == 0) {
765 char *tok = strchr(re->value, ':');
767 if (tok && (strchr(tok, 0) > (tok + 1))) {
768 status->sampleRate = atoi(re->value);
769 status->bits = atoi(++tok);
770 tok = strchr(tok, ':');
771 if (tok && (strchr(tok, 0) > (tok + 1))) {
772 status->channels = atoi(tok + 1);
777 mpd_getNextReturnElement(connection);
778 if (connection->error) {
784 if (connection->error) {
787 } else if (status->state < 0) {
788 strcpy(connection->errorStr, "state not found");
789 connection->error = 1;
797 void mpd_freeStatus(mpd_Status *status)
805 void mpd_sendStatsCommand(mpd_Connection *connection)
807 mpd_executeCommand(connection, "stats\n");
810 mpd_Stats *mpd_getStats(mpd_Connection *connection)
814 /* mpd_executeCommand(connection, "stats\n");
815 if (connection->error) {
819 if (connection->doneProcessing
820 || (connection->listOks && connection->doneListOk)) {
824 if (!connection->returnElement) {
825 mpd_getNextReturnElement(connection);
828 stats = malloc(sizeof(mpd_Stats));
829 stats->numberOfArtists = 0;
830 stats->numberOfAlbums = 0;
831 stats->numberOfSongs = 0;
833 stats->dbUpdateTime = 0;
835 stats->dbPlayTime = 0;
837 if (connection->error) {
841 while (connection->returnElement) {
842 mpd_ReturnElement *re = connection->returnElement;
844 if (strcmp(re->name, "artists") == 0) {
845 stats->numberOfArtists = atoi(re->value);
846 } else if (strcmp(re->name, "albums") == 0) {
847 stats->numberOfAlbums = atoi(re->value);
848 } else if (strcmp(re->name, "songs") == 0) {
849 stats->numberOfSongs = atoi(re->value);
850 } else if (strcmp(re->name, "uptime") == 0) {
851 stats->uptime = strtol(re->value, NULL, 10);
852 } else if (strcmp(re->name, "db_update") == 0) {
853 stats->dbUpdateTime = strtol(re->value, NULL, 10);
854 } else if (strcmp(re->name, "playtime") == 0) {
855 stats->playTime = strtol(re->value, NULL, 10);
856 } else if (strcmp(re->name, "db_playtime") == 0) {
857 stats->dbPlayTime = strtol(re->value, NULL, 10);
860 mpd_getNextReturnElement(connection);
861 if (connection->error) {
867 if (connection->error) {
875 void mpd_freeStats(mpd_Stats *stats)
880 mpd_SearchStats *mpd_getSearchStats(mpd_Connection *connection)
882 mpd_SearchStats *stats;
883 mpd_ReturnElement *re;
885 if (connection->doneProcessing
886 || (connection->listOks && connection->doneListOk)) {
890 if (!connection->returnElement) {
891 mpd_getNextReturnElement(connection);
894 if (connection->error) {
898 stats = malloc(sizeof(mpd_SearchStats));
899 stats->numberOfSongs = 0;
902 while (connection->returnElement) {
903 re = connection->returnElement;
905 if (strcmp(re->name, "songs") == 0) {
906 stats->numberOfSongs = atoi(re->value);
907 } else if (strcmp(re->name, "playtime") == 0) {
908 stats->playTime = strtol(re->value, NULL, 10);
911 mpd_getNextReturnElement(connection);
912 if (connection->error) {
918 if (connection->error) {
926 void mpd_freeSearchStats(mpd_SearchStats *stats)
931 static void mpd_initSong(mpd_Song *song)
942 song->composer = NULL;
943 song->performer = NULL;
945 song->comment = NULL;
947 song->time = MPD_SONG_NO_TIME;
948 song->pos = MPD_SONG_NO_NUM;
949 song->id = MPD_SONG_NO_ID;
952 static void mpd_finishSong(mpd_Song *song)
978 if (song->composer) {
979 free(song->composer);
989 mpd_Song *mpd_newSong(void)
991 mpd_Song *ret = malloc(sizeof(mpd_Song));
998 void mpd_freeSong(mpd_Song *song)
1000 mpd_finishSong(song);
1004 mpd_Song *mpd_songDup(mpd_Song *song)
1006 mpd_Song *ret = mpd_newSong();
1009 ret->file = strdup(song->file);
1012 ret->artist = strdup(song->artist);
1015 ret->album = strdup(song->album);
1018 ret->title = strdup(song->title);
1021 ret->track = strdup(song->track);
1024 ret->name = strdup(song->name);
1027 ret->date = strdup(song->date);
1030 ret->genre = strdup(song->genre);
1032 if (song->composer) {
1033 ret->composer = strdup(song->composer);
1036 ret->disc = strdup(song->disc);
1038 if (song->comment) {
1039 ret->comment = strdup(song->comment);
1041 ret->time = song->time;
1042 ret->pos = song->pos;
1048 static void mpd_initDirectory(mpd_Directory *directory)
1050 directory->path = NULL;
1053 static void mpd_finishDirectory(mpd_Directory *directory)
1055 if (directory->path) {
1056 free(directory->path);
1060 mpd_Directory *mpd_newDirectory(void)
1062 mpd_Directory *directory = malloc(sizeof(mpd_Directory));
1064 mpd_initDirectory(directory);
1069 void mpd_freeDirectory(mpd_Directory *directory)
1071 mpd_finishDirectory(directory);
1076 mpd_Directory *mpd_directoryDup(mpd_Directory *directory)
1078 mpd_Directory *ret = mpd_newDirectory();
1080 if (directory->path) {
1081 ret->path = strdup(directory->path);
1087 static void mpd_initPlaylistFile(mpd_PlaylistFile *playlist)
1089 playlist->path = NULL;
1092 static void mpd_finishPlaylistFile(mpd_PlaylistFile *playlist)
1094 if (playlist->path) {
1095 free(playlist->path);
1099 mpd_PlaylistFile *mpd_newPlaylistFile(void)
1101 mpd_PlaylistFile *playlist = malloc(sizeof(mpd_PlaylistFile));
1103 mpd_initPlaylistFile(playlist);
1108 void mpd_freePlaylistFile(mpd_PlaylistFile *playlist)
1110 mpd_finishPlaylistFile(playlist);
1114 mpd_PlaylistFile *mpd_playlistFileDup(mpd_PlaylistFile *playlist)
1116 mpd_PlaylistFile *ret = mpd_newPlaylistFile();
1118 if (playlist->path) {
1119 ret->path = strdup(playlist->path);
1125 static void mpd_initInfoEntity(mpd_InfoEntity *entity)
1127 entity->info.directory = NULL;
1130 static void mpd_finishInfoEntity(mpd_InfoEntity *entity)
1132 if (entity->info.directory) {
1133 if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1134 mpd_freeDirectory(entity->info.directory);
1135 } else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
1136 mpd_freeSong(entity->info.song);
1137 } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1138 mpd_freePlaylistFile(entity->info.playlistFile);
1143 mpd_InfoEntity *mpd_newInfoEntity(void)
1145 mpd_InfoEntity *entity = malloc(sizeof(mpd_InfoEntity));
1147 mpd_initInfoEntity(entity);
1152 void mpd_freeInfoEntity(mpd_InfoEntity *entity)
1154 mpd_finishInfoEntity(entity);
1158 static void mpd_sendInfoCommand(mpd_Connection *connection, char *command)
1160 mpd_executeCommand(connection, command);
1163 mpd_InfoEntity *mpd_getNextInfoEntity(mpd_Connection *connection)
1165 mpd_InfoEntity *entity = NULL;
1167 if (connection->doneProcessing
1168 || (connection->listOks && connection->doneListOk)) {
1172 if (!connection->returnElement) {
1173 mpd_getNextReturnElement(connection);
1176 if (connection->returnElement) {
1177 if (strcmp(connection->returnElement->name, "file") == 0) {
1178 entity = mpd_newInfoEntity();
1179 entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1180 entity->info.song = mpd_newSong();
1181 entity->info.song->file = strdup(connection->returnElement->value);
1182 } else if (strcmp(connection->returnElement->name, "directory") == 0) {
1183 entity = mpd_newInfoEntity();
1184 entity->type = MPD_INFO_ENTITY_TYPE_DIRECTORY;
1185 entity->info.directory = mpd_newDirectory();
1186 entity->info.directory->path =
1187 strdup(connection->returnElement->value);
1188 } else if (strcmp(connection->returnElement->name, "playlist") == 0) {
1189 entity = mpd_newInfoEntity();
1190 entity->type = MPD_INFO_ENTITY_TYPE_PLAYLISTFILE;
1191 entity->info.playlistFile = mpd_newPlaylistFile();
1192 entity->info.playlistFile->path =
1193 strdup(connection->returnElement->value);
1194 } else if (strcmp(connection->returnElement->name, "cpos") == 0) {
1195 entity = mpd_newInfoEntity();
1196 entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1197 entity->info.song = mpd_newSong();
1198 entity->info.song->pos = atoi(connection->returnElement->value);
1200 connection->error = 1;
1201 strcpy(connection->errorStr, "problem parsing song info");
1208 mpd_getNextReturnElement(connection);
1209 while (connection->returnElement) {
1210 mpd_ReturnElement *re = connection->returnElement;
1212 if (strcmp(re->name, "file") == 0) {
1214 } else if (strcmp(re->name, "directory") == 0) {
1216 } else if (strcmp(re->name, "playlist") == 0) {
1218 } else if (strcmp(re->name, "cpos") == 0) {
1222 if (entity->type == MPD_INFO_ENTITY_TYPE_SONG && strlen(re->value)) {
1223 if (!entity->info.song->artist
1224 && strcmp(re->name, "Artist") == 0) {
1225 entity->info.song->artist = strdup(re->value);
1226 } else if (!entity->info.song->album
1227 && strcmp(re->name, "Album") == 0) {
1228 entity->info.song->album = strdup(re->value);
1229 } else if (!entity->info.song->title
1230 && strcmp(re->name, "Title") == 0) {
1231 entity->info.song->title = strdup(re->value);
1232 } else if (!entity->info.song->track
1233 && strcmp(re->name, "Track") == 0) {
1234 entity->info.song->track = strdup(re->value);
1235 } else if (!entity->info.song->name
1236 && strcmp(re->name, "Name") == 0) {
1237 entity->info.song->name = strdup(re->value);
1238 } else if (entity->info.song->time == MPD_SONG_NO_TIME
1239 && strcmp(re->name, "Time") == 0) {
1240 entity->info.song->time = atoi(re->value);
1241 } else if (entity->info.song->pos == MPD_SONG_NO_NUM
1242 && strcmp(re->name, "Pos") == 0) {
1243 entity->info.song->pos = atoi(re->value);
1244 } else if (entity->info.song->id == MPD_SONG_NO_ID
1245 && strcmp(re->name, "Id") == 0) {
1246 entity->info.song->id = atoi(re->value);
1247 } else if (!entity->info.song->date
1248 && strcmp(re->name, "Date") == 0) {
1249 entity->info.song->date = strdup(re->value);
1250 } else if (!entity->info.song->genre
1251 && strcmp(re->name, "Genre") == 0) {
1252 entity->info.song->genre = strdup(re->value);
1253 } else if (!entity->info.song->composer
1254 && strcmp(re->name, "Composer") == 0) {
1255 entity->info.song->composer = strdup(re->value);
1256 } else if (!entity->info.song->performer
1257 && strcmp(re->name, "Performer") == 0) {
1258 entity->info.song->performer = strdup(re->value);
1259 } else if (!entity->info.song->disc
1260 && strcmp(re->name, "Disc") == 0) {
1261 entity->info.song->disc = strdup(re->value);
1262 } else if (!entity->info.song->comment
1263 && strcmp(re->name, "Comment") == 0) {
1264 entity->info.song->comment = strdup(re->value);
1266 } else if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1267 } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1270 mpd_getNextReturnElement(connection);
1276 static char *mpd_getNextReturnElementNamed(mpd_Connection *connection,
1279 if (connection->doneProcessing
1280 || (connection->listOks && connection->doneListOk)) {
1284 mpd_getNextReturnElement(connection);
1285 while (connection->returnElement) {
1286 mpd_ReturnElement *re = connection->returnElement;
1288 if (strcmp(re->name, name) == 0) {
1289 return strdup(re->value);
1291 mpd_getNextReturnElement(connection);
1297 char *mpd_getNextTag(mpd_Connection *connection, int type)
1299 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES
1300 || type == MPD_TAG_ITEM_ANY) {
1303 if (type == MPD_TAG_ITEM_FILENAME) {
1304 return mpd_getNextReturnElementNamed(connection, "file");
1306 return mpd_getNextReturnElementNamed(connection, mpdTagItemKeys[type]);
1309 char *mpd_getNextArtist(mpd_Connection *connection)
1311 return mpd_getNextReturnElementNamed(connection, "Artist");
1314 char *mpd_getNextAlbum(mpd_Connection *connection)
1316 return mpd_getNextReturnElementNamed(connection, "Album");
1319 void mpd_sendPlaylistInfoCommand(mpd_Connection *connection, int songPos)
1321 int len = strlen("playlistinfo") + 2 + INTLEN + 3;
1322 char *string = malloc(len);
1324 snprintf(string, len, "playlistinfo \"%i\"\n", songPos);
1325 mpd_sendInfoCommand(connection, string);
1329 void mpd_sendPlaylistIdCommand(mpd_Connection *connection, int id)
1331 int len = strlen("playlistid") + 2 + INTLEN + 3;
1332 char *string = malloc(len);
1334 snprintf(string, len, "playlistid \"%i\"\n", id);
1335 mpd_sendInfoCommand(connection, string);
1339 void mpd_sendPlChangesCommand(mpd_Connection *connection, long long playlist)
1341 int len = strlen("plchanges") + 2 + LONGLONGLEN + 3;
1342 char *string = malloc(len);
1344 snprintf(string, len, "plchanges \"%lld\"\n", playlist);
1345 mpd_sendInfoCommand(connection, string);
1349 void mpd_sendPlChangesPosIdCommand(mpd_Connection *connection,
1352 int len = strlen("plchangesposid") + 2 + LONGLONGLEN + 3;
1353 char *string = malloc(len);
1355 snprintf(string, len, "plchangesposid \"%lld\"\n", playlist);
1356 mpd_sendInfoCommand(connection, string);
1360 void mpd_sendListallCommand(mpd_Connection *connection, const char *dir)
1362 char *sDir = mpd_sanitizeArg(dir);
1363 int len = strlen("listall") + 2 + strlen(sDir) + 3;
1364 char *string = malloc(len);
1366 snprintf(string, len, "listall \"%s\"\n", sDir);
1367 mpd_sendInfoCommand(connection, string);
1372 void mpd_sendListallInfoCommand(mpd_Connection *connection, const char *dir)
1374 char *sDir = mpd_sanitizeArg(dir);
1375 int len = strlen("listallinfo") + 2 + strlen(sDir) + 3;
1376 char *string = malloc(len);
1378 snprintf(string, len, "listallinfo \"%s\"\n", sDir);
1379 mpd_sendInfoCommand(connection, string);
1384 void mpd_sendLsInfoCommand(mpd_Connection *connection, const char *dir)
1386 char *sDir = mpd_sanitizeArg(dir);
1387 int len = strlen("lsinfo") + 2 + strlen(sDir) + 3;
1388 char *string = malloc(len);
1390 snprintf(string, len, "lsinfo \"%s\"\n", sDir);
1391 mpd_sendInfoCommand(connection, string);
1396 void mpd_sendCurrentSongCommand(mpd_Connection *connection)
1398 mpd_executeCommand(connection, "currentsong\n");
1401 void mpd_sendSearchCommand(mpd_Connection *connection, int table,
1404 mpd_startSearch(connection, 0);
1405 mpd_addConstraintSearch(connection, table, str);
1406 mpd_commitSearch(connection);
1409 void mpd_sendFindCommand(mpd_Connection *connection, int table,
1412 mpd_startSearch(connection, 1);
1413 mpd_addConstraintSearch(connection, table, str);
1414 mpd_commitSearch(connection);
1417 void mpd_sendListCommand(mpd_Connection *connection, int table,
1424 if (table == MPD_TABLE_ARTIST) {
1425 strcpy(st, "artist");
1426 } else if (table == MPD_TABLE_ALBUM) {
1427 strcpy(st, "album");
1429 connection->error = 1;
1430 strcpy(connection->errorStr, "unknown table for list");
1434 char *sanitArg1 = mpd_sanitizeArg(arg1);
1436 len = strlen("list") + 1 + strlen(sanitArg1) + 2 + strlen(st) + 3;
1437 string = malloc(len);
1438 snprintf(string, len, "list %s \"%s\"\n", st, sanitArg1);
1441 len = strlen("list") + 1 + strlen(st) + 2;
1442 string = malloc(len);
1443 snprintf(string, len, "list %s\n", st);
1445 mpd_sendInfoCommand(connection, string);
1449 void mpd_sendAddCommand(mpd_Connection *connection, const char *file)
1451 char *sFile = mpd_sanitizeArg(file);
1452 int len = strlen("add") + 2 + strlen(sFile) + 3;
1453 char *string = malloc(len);
1455 snprintf(string, len, "add \"%s\"\n", sFile);
1456 mpd_executeCommand(connection, string);
1461 int mpd_sendAddIdCommand(mpd_Connection *connection, const char *file)
1464 char *sFile = mpd_sanitizeArg(file);
1465 int len = strlen("addid") + 2 + strlen(sFile) + 3;
1466 char *string = malloc(len);
1468 snprintf(string, len, "addid \"%s\"\n", sFile);
1469 mpd_sendInfoCommand(connection, string);
1473 string = mpd_getNextReturnElementNamed(connection, "Id");
1475 retval = atoi(string);
1482 void mpd_sendDeleteCommand(mpd_Connection *connection, int songPos)
1484 int len = strlen("delete") + 2 + INTLEN + 3;
1485 char *string = malloc(len);
1487 snprintf(string, len, "delete \"%i\"\n", songPos);
1488 mpd_sendInfoCommand(connection, string);
1492 void mpd_sendDeleteIdCommand(mpd_Connection *connection, int id)
1494 int len = strlen("deleteid") + 2 + INTLEN + 3;
1495 char *string = malloc(len);
1497 snprintf(string, len, "deleteid \"%i\"\n", id);
1498 mpd_sendInfoCommand(connection, string);
1502 void mpd_sendSaveCommand(mpd_Connection *connection, const char *name)
1504 char *sName = mpd_sanitizeArg(name);
1505 int len = strlen("save") + 2 + strlen(sName) + 3;
1506 char *string = malloc(len);
1508 snprintf(string, len, "save \"%s\"\n", sName);
1509 mpd_executeCommand(connection, string);
1514 void mpd_sendLoadCommand(mpd_Connection *connection, const char *name)
1516 char *sName = mpd_sanitizeArg(name);
1517 int len = strlen("load") + 2 + strlen(sName) + 3;
1518 char *string = malloc(len);
1520 snprintf(string, len, "load \"%s\"\n", sName);
1521 mpd_executeCommand(connection, string);
1526 void mpd_sendRmCommand(mpd_Connection *connection, const char *name)
1528 char *sName = mpd_sanitizeArg(name);
1529 int len = strlen("rm") + 2 + strlen(sName) + 3;
1530 char *string = malloc(len);
1532 snprintf(string, len, "rm \"%s\"\n", sName);
1533 mpd_executeCommand(connection, string);
1538 void mpd_sendRenameCommand(mpd_Connection *connection, const char *from,
1541 char *sFrom = mpd_sanitizeArg(from);
1542 char *sTo = mpd_sanitizeArg(to);
1543 int len = strlen("rename") + 2 + strlen(sFrom) + 3 + strlen(sTo) + 3;
1544 char *string = malloc(len);
1546 snprintf(string, len, "rename \"%s\" \"%s\"\n", sFrom, sTo);
1547 mpd_executeCommand(connection, string);
1553 void mpd_sendShuffleCommand(mpd_Connection *connection)
1555 mpd_executeCommand(connection, "shuffle\n");
1558 void mpd_sendClearCommand(mpd_Connection *connection)
1560 mpd_executeCommand(connection, "clear\n");
1563 void mpd_sendPlayCommand(mpd_Connection *connection, int songPos)
1565 int len = strlen("play") + 2 + INTLEN + 3;
1566 char *string = malloc(len);
1568 snprintf(string, len, "play \"%i\"\n", songPos);
1569 mpd_sendInfoCommand(connection, string);
1573 void mpd_sendPlayIdCommand(mpd_Connection *connection, int id)
1575 int len = strlen("playid") + 2 + INTLEN + 3;
1576 char *string = malloc(len);
1578 snprintf(string, len, "playid \"%i\"\n", id);
1579 mpd_sendInfoCommand(connection, string);
1583 void mpd_sendStopCommand(mpd_Connection *connection)
1585 mpd_executeCommand(connection, "stop\n");
1588 void mpd_sendPauseCommand(mpd_Connection *connection, int pauseMode)
1590 int len = strlen("pause") + 2 + INTLEN + 3;
1591 char *string = malloc(len);
1593 snprintf(string, len, "pause \"%i\"\n", pauseMode);
1594 mpd_executeCommand(connection, string);
1598 void mpd_sendNextCommand(mpd_Connection *connection)
1600 mpd_executeCommand(connection, "next\n");
1603 void mpd_sendMoveCommand(mpd_Connection *connection, int from, int to)
1605 int len = strlen("move") + 2 + INTLEN + 3 + INTLEN + 3;
1606 char *string = malloc(len);
1608 snprintf(string, len, "move \"%i\" \"%i\"\n", from, to);
1609 mpd_sendInfoCommand(connection, string);
1613 void mpd_sendMoveIdCommand(mpd_Connection *connection, int id, int to)
1615 int len = strlen("moveid") + 2 + INTLEN + 3 + INTLEN + 3;
1616 char *string = malloc(len);
1618 snprintf(string, len, "moveid \"%i\" \"%i\"\n", id, to);
1619 mpd_sendInfoCommand(connection, string);
1623 void mpd_sendSwapCommand(mpd_Connection *connection, int song1, int song2)
1625 int len = strlen("swap") + 2 + INTLEN + 3 + INTLEN + 3;
1626 char *string = malloc(len);
1628 snprintf(string, len, "swap \"%i\" \"%i\"\n", song1, song2);
1629 mpd_sendInfoCommand(connection, string);
1633 void mpd_sendSwapIdCommand(mpd_Connection *connection, int id1, int id2)
1635 int len = strlen("swapid") + 2 + INTLEN + 3 + INTLEN + 3;
1636 char *string = malloc(len);
1638 snprintf(string, len, "swapid \"%i\" \"%i\"\n", id1, id2);
1639 mpd_sendInfoCommand(connection, string);
1643 void mpd_sendSeekCommand(mpd_Connection *connection, int song, int time)
1645 int len = strlen("seek") + 2 + INTLEN + 3 + INTLEN + 3;
1646 char *string = malloc(len);
1648 snprintf(string, len, "seek \"%i\" \"%i\"\n", song, time);
1649 mpd_sendInfoCommand(connection, string);
1653 void mpd_sendSeekIdCommand(mpd_Connection *connection, int id, int time)
1655 int len = strlen("seekid") + 2 + INTLEN + 3 + INTLEN + 3;
1656 char *string = malloc(len);
1658 snprintf(string, len, "seekid \"%i\" \"%i\"\n", id, time);
1659 mpd_sendInfoCommand(connection, string);
1663 void mpd_sendUpdateCommand(mpd_Connection *connection, char *path)
1665 char *sPath = mpd_sanitizeArg(path);
1666 int len = strlen("update") + 2 + strlen(sPath) + 3;
1667 char *string = malloc(len);
1669 snprintf(string, len, "update \"%s\"\n", sPath);
1670 mpd_sendInfoCommand(connection, string);
1675 int mpd_getUpdateId(mpd_Connection *connection)
1680 jobid = mpd_getNextReturnElementNamed(connection, "updating_db");
1689 void mpd_sendPrevCommand(mpd_Connection *connection)
1691 mpd_executeCommand(connection, "previous\n");
1694 void mpd_sendRepeatCommand(mpd_Connection *connection, int repeatMode)
1696 int len = strlen("repeat") + 2 + INTLEN + 3;
1697 char *string = malloc(len);
1699 snprintf(string, len, "repeat \"%i\"\n", repeatMode);
1700 mpd_executeCommand(connection, string);
1704 void mpd_sendRandomCommand(mpd_Connection *connection, int randomMode)
1706 int len = strlen("random") + 2 + INTLEN + 3;
1707 char *string = malloc(len);
1709 snprintf(string, len, "random \"%i\"\n", randomMode);
1710 mpd_executeCommand(connection, string);
1714 void mpd_sendSetvolCommand(mpd_Connection *connection, int volumeChange)
1716 int len = strlen("setvol") + 2 + INTLEN + 3;
1717 char *string = malloc(len);
1719 snprintf(string, len, "setvol \"%i\"\n", volumeChange);
1720 mpd_executeCommand(connection, string);
1724 void mpd_sendVolumeCommand(mpd_Connection *connection, int volumeChange)
1726 int len = strlen("volume") + 2 + INTLEN + 3;
1727 char *string = malloc(len);
1729 snprintf(string, len, "volume \"%i\"\n", volumeChange);
1730 mpd_executeCommand(connection, string);
1734 void mpd_sendCrossfadeCommand(mpd_Connection *connection, int seconds)
1736 int len = strlen("crossfade") + 2 + INTLEN + 3;
1737 char *string = malloc(len);
1739 snprintf(string, len, "crossfade \"%i\"\n", seconds);
1740 mpd_executeCommand(connection, string);
1744 void mpd_sendPasswordCommand(mpd_Connection *connection, const char *pass)
1746 char *sPass = mpd_sanitizeArg(pass);
1747 int len = strlen("password") + 2 + strlen(sPass) + 3;
1748 char *string = malloc(len);
1750 snprintf(string, len, "password \"%s\"\n", sPass);
1751 mpd_executeCommand(connection, string);
1756 void mpd_sendCommandListBegin(mpd_Connection *connection)
1758 if (connection->commandList) {
1759 strcpy(connection->errorStr, "already in command list mode");
1760 connection->error = 1;
1763 connection->commandList = COMMAND_LIST;
1764 mpd_executeCommand(connection, "command_list_begin\n");
1767 void mpd_sendCommandListOkBegin(mpd_Connection *connection)
1769 if (connection->commandList) {
1770 strcpy(connection->errorStr, "already in command list mode");
1771 connection->error = 1;
1774 connection->commandList = COMMAND_LIST_OK;
1775 mpd_executeCommand(connection, "command_list_ok_begin\n");
1776 connection->listOks = 0;
1779 void mpd_sendCommandListEnd(mpd_Connection *connection)
1781 if (!connection->commandList) {
1782 strcpy(connection->errorStr, "not in command list mode");
1783 connection->error = 1;
1786 connection->commandList = 0;
1787 mpd_executeCommand(connection, "command_list_end\n");
1790 void mpd_sendOutputsCommand(mpd_Connection *connection)
1792 mpd_executeCommand(connection, "outputs\n");
1795 mpd_OutputEntity *mpd_getNextOutput(mpd_Connection *connection)
1797 mpd_OutputEntity *output = NULL;
1799 if (connection->doneProcessing
1800 || (connection->listOks && connection->doneListOk)) {
1804 if (connection->error) {
1808 output = malloc(sizeof(mpd_OutputEntity));
1810 output->name = NULL;
1811 output->enabled = 0;
1813 if (!connection->returnElement) {
1814 mpd_getNextReturnElement(connection);
1817 while (connection->returnElement) {
1818 mpd_ReturnElement *re = connection->returnElement;
1820 if (strcmp(re->name, "outputid") == 0) {
1821 if (output != NULL && output->id >= 0) {
1824 output->id = atoi(re->value);
1825 } else if (strcmp(re->name, "outputname") == 0) {
1826 output->name = strdup(re->value);
1827 } else if (strcmp(re->name, "outputenabled") == 0) {
1828 output->enabled = atoi(re->value);
1831 mpd_getNextReturnElement(connection);
1832 if (connection->error) {
1841 void mpd_sendEnableOutputCommand(mpd_Connection *connection, int outputId)
1843 int len = strlen("enableoutput") + 2 + INTLEN + 3;
1844 char *string = malloc(len);
1846 snprintf(string, len, "enableoutput \"%i\"\n", outputId);
1847 mpd_executeCommand(connection, string);
1851 void mpd_sendDisableOutputCommand(mpd_Connection *connection, int outputId)
1853 int len = strlen("disableoutput") + 2 + INTLEN + 3;
1854 char *string = malloc(len);
1856 snprintf(string, len, "disableoutput \"%i\"\n", outputId);
1857 mpd_executeCommand(connection, string);
1861 void mpd_freeOutputElement(mpd_OutputEntity *output)
1867 /** odd naming, but it gets the not allowed commands */
1868 void mpd_sendNotCommandsCommand(mpd_Connection *connection)
1870 mpd_executeCommand(connection, "notcommands\n");
1873 /** odd naming, but it gets the allowed commands */
1874 void mpd_sendCommandsCommand(mpd_Connection *connection)
1876 mpd_executeCommand(connection, "commands\n");
1879 /** Get the next returned command */
1880 char *mpd_getNextCommand(mpd_Connection *connection)
1882 return mpd_getNextReturnElementNamed(connection, "command");
1885 void mpd_sendUrlHandlersCommand(mpd_Connection *connection)
1887 mpd_executeCommand(connection, "urlhandlers\n");
1890 char *mpd_getNextHandler(mpd_Connection *connection)
1892 return mpd_getNextReturnElementNamed(connection, "handler");
1895 void mpd_sendTagTypesCommand(mpd_Connection *connection)
1897 mpd_executeCommand(connection, "tagtypes\n");
1900 char *mpd_getNextTagType(mpd_Connection *connection)
1902 return mpd_getNextReturnElementNamed(connection, "tagtype");
1905 void mpd_startSearch(mpd_Connection *connection, int exact)
1907 if (connection->request) {
1908 strcpy(connection->errorStr, "search already in progress");
1909 connection->error = 1;
1914 connection->request = strdup("find");
1916 connection->request = strdup("search");
1920 void mpd_startStatsSearch(mpd_Connection *connection)
1922 if (connection->request) {
1923 strcpy(connection->errorStr, "search already in progress");
1924 connection->error = 1;
1928 connection->request = strdup("count");
1931 void mpd_startPlaylistSearch(mpd_Connection *connection, int exact)
1933 if (connection->request) {
1934 strcpy(connection->errorStr, "search already in progress");
1935 connection->error = 1;
1940 connection->request = strdup("playlistfind");
1942 connection->request = strdup("playlistsearch");
1946 void mpd_startFieldSearch(mpd_Connection *connection, int type)
1951 if (connection->request) {
1952 strcpy(connection->errorStr, "search already in progress");
1953 connection->error = 1;
1957 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
1958 strcpy(connection->errorStr, "invalid type specified");
1959 connection->error = 1;
1963 strtype = mpdTagItemKeys[type];
1965 len = 5 + strlen(strtype) + 1;
1966 connection->request = malloc(len);
1968 snprintf(connection->request, len, "list %c%s", tolower(strtype[0]),
1972 void mpd_addConstraintSearch(mpd_Connection *connection, int type,
1980 if (!connection->request) {
1981 strcpy(connection->errorStr, "no search in progress");
1982 connection->error = 1;
1986 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
1987 strcpy(connection->errorStr, "invalid type specified");
1988 connection->error = 1;
1993 strcpy(connection->errorStr, "no name specified");
1994 connection->error = 1;
1998 string = strdup(connection->request);
1999 strtype = mpdTagItemKeys[type];
2000 arg = mpd_sanitizeArg(name);
2002 len = strlen(string) + 1 + strlen(strtype) + 2 + strlen(arg) + 2;
2003 connection->request = realloc(connection->request, len);
2004 snprintf(connection->request, len, "%s %c%s \"%s\"", string,
2005 tolower(strtype[0]), strtype + 1, arg);
2011 void mpd_commitSearch(mpd_Connection *connection)
2015 if (!connection->request) {
2016 strcpy(connection->errorStr, "no search in progress");
2017 connection->error = 1;
2021 len = strlen(connection->request) + 2;
2022 connection->request = realloc(connection->request, len);
2023 connection->request[len - 2] = '\n';
2024 connection->request[len - 1] = '\0';
2025 mpd_sendInfoCommand(connection, connection->request);
2027 free(connection->request);
2028 connection->request = NULL;
2032 * @param connection a MpdConnection
2033 * @param path the path to the playlist.
2035 * List the content, with full metadata, of a stored playlist. */
2036 void mpd_sendListPlaylistInfoCommand(mpd_Connection *connection, char *path)
2038 char *arg = mpd_sanitizeArg(path);
2039 int len = strlen("listplaylistinfo") + 2 + strlen(arg) + 3;
2040 char *query = malloc(len);
2042 snprintf(query, len, "listplaylistinfo \"%s\"\n", arg);
2043 mpd_sendInfoCommand(connection, query);
2049 * @param connection a MpdConnection
2050 * @param path the path to the playlist.
2052 * List the content of a stored playlist. */
2053 void mpd_sendListPlaylistCommand(mpd_Connection *connection, char *path)
2055 char *arg = mpd_sanitizeArg(path);
2056 int len = strlen("listplaylist") + 2 + strlen(arg) + 3;
2057 char *query = malloc(len);
2059 snprintf(query, len, "listplaylist \"%s\"\n", arg);
2060 mpd_sendInfoCommand(connection, query);
2065 void mpd_sendPlaylistClearCommand(mpd_Connection *connection, char *path)
2067 char *sPath = mpd_sanitizeArg(path);
2068 int len = strlen("playlistclear") + 2 + strlen(sPath) + 3;
2069 char *string = malloc(len);
2071 snprintf(string, len, "playlistclear \"%s\"\n", sPath);
2072 mpd_executeCommand(connection, string);
2077 void mpd_sendPlaylistAddCommand(mpd_Connection *connection, char *playlist,
2080 char *sPlaylist = mpd_sanitizeArg(playlist);
2081 char *sPath = mpd_sanitizeArg(path);
2082 int len = strlen("playlistadd") + 2 + strlen(sPlaylist) + 3 +
2084 char *string = malloc(len);
2086 snprintf(string, len, "playlistadd \"%s\" \"%s\"\n", sPlaylist, sPath);
2087 mpd_executeCommand(connection, string);
2093 void mpd_sendPlaylistMoveCommand(mpd_Connection *connection, char *playlist,
2096 char *sPlaylist = mpd_sanitizeArg(playlist);
2097 int len = strlen("playlistmove") + 2 + strlen(sPlaylist) + 3 + INTLEN +
2099 char *string = malloc(len);
2101 snprintf(string, len, "playlistmove \"%s\" \"%i\" \"%i\"\n", sPlaylist,
2103 mpd_executeCommand(connection, string);
2108 void mpd_sendPlaylistDeleteCommand(mpd_Connection *connection, char *playlist,
2111 char *sPlaylist = mpd_sanitizeArg(playlist);
2112 int len = strlen("playlistdelete") + 2 + strlen(sPlaylist) + 3 +
2114 char *string = malloc(len);
2116 snprintf(string, len, "playlistdelete \"%s\" \"%i\"\n", sPlaylist, pos);
2117 mpd_executeCommand(connection, string);