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 connection->sock = socket(res->ai_family, SOCK_STREAM,
159 if (connection->sock < 0) {
160 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
161 "problems creating socket: %s", strerror(errno));
162 connection->error = MPD_ERROR_SYSTEM;
163 freeaddrinfo(addrinfo);
167 mpd_setConnectionTimeout(connection, timeout);
170 if (do_connect_fail(connection, res->ai_addr, res->ai_addrlen)) {
171 /* try the next address family */
172 closesocket(connection->sock);
173 connection->sock = -1;
178 freeaddrinfo(addrinfo);
180 if (connection->sock < 0) {
181 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
182 "problems connecting to \"%s\" on port %i: %s", host, port,
184 connection->error = MPD_ERROR_CONNPORT;
191 #else /* !MPD_HAVE_GAI */
192 static int mpd_connect(mpd_Connection *connection, const char *host, int port,
196 struct sockaddr *dest;
198 struct sockaddr_in sin;
200 if (!(he = gethostbyname(host))) {
201 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
202 "host \"%s\" not found", host);
203 connection->error = MPD_ERROR_UNKHOST;
207 memset(&sin, 0, sizeof(struct sockaddr_in));
208 /* dest.sin_family = he->h_addrtype; */
209 sin.sin_family = AF_INET;
210 sin.sin_port = htons(port);
212 switch (he->h_addrtype) {
214 memcpy((char *) &sin.sin_addr.s_addr, (char *) he->h_addr,
216 dest = (struct sockaddr *) &sin;
217 destlen = sizeof(struct sockaddr_in);
220 strcpy(connection->errorStr, "address type is not IPv4");
221 connection->error = MPD_ERROR_SYSTEM;
226 if ((connection->sock = socket(dest->sa_family, SOCK_STREAM, 0)) < 0) {
227 strcpy(connection->errorStr, "problems creating socket");
228 connection->error = MPD_ERROR_SYSTEM;
232 mpd_setConnectionTimeout(connection, timeout);
235 if (do_connect_fail(connection, dest, destlen)) {
236 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
237 "problems connecting to \"%s\" on port %i", host, port);
238 connection->error = MPD_ERROR_CONNPORT;
244 #endif /* !MPD_HAVE_GAI */
246 const char *mpdTagItemKeys[MPD_TAG_NUM_OF_ITEM_TYPES] = {
262 static char *mpd_sanitizeArg(const char *arg)
266 register const char *c;
269 /* instead of counting in that loop above,
270 * just use a bit more memory and halve running time */
271 ret = malloc(strlen(arg) * 2 + 1);
275 for (i = strlen(arg) + 1; i != 0; --i) {
276 if (*c == '"' || *c == '\\') {
285 static mpd_ReturnElement *mpd_newReturnElement(const char *name,
288 mpd_ReturnElement *ret = malloc(sizeof(mpd_ReturnElement));
290 ret->name = strdup(name);
291 ret->value = strdup(value);
296 static void mpd_freeReturnElement(mpd_ReturnElement *re)
303 void mpd_setConnectionTimeout(mpd_Connection *connection, float timeout)
305 connection->timeout.tv_sec = (int) timeout;
306 connection->timeout.tv_usec =
307 (int) ((timeout - connection->timeout.tv_sec) * 1e6 + 0.5);
310 static int mpd_parseWelcome(mpd_Connection *connection, const char *host,
311 int port, /* char *rt, */ char *output)
317 if (strncmp(output, MPD_WELCOME_MESSAGE, strlen(MPD_WELCOME_MESSAGE))) {
318 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
319 "mpd not running on port %i on host \"%s\"", port, host);
320 connection->error = MPD_ERROR_NOTMPD;
324 tmp = &output[strlen(MPD_WELCOME_MESSAGE)];
326 for (i = 0; i < 3; i++) {
328 connection->version[i] = strtol(tmp, &test, 10);
331 if (!tmp || (test[0] != '.' && test[0] != '\0')) {
332 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
333 "error parsing version number at \"%s\"",
334 &output[strlen(MPD_WELCOME_MESSAGE)]);
335 connection->error = MPD_ERROR_NOTMPD;
344 mpd_Connection *mpd_newConnection(const char *host, int port, float timeout)
349 mpd_Connection *connection = malloc(sizeof(mpd_Connection));
353 strcpy(connection->buffer, "");
354 connection->buflen = 0;
355 connection->bufstart = 0;
356 strcpy(connection->errorStr, "");
357 connection->error = 0;
358 connection->doneProcessing = 0;
359 connection->commandList = 0;
360 connection->listOks = 0;
361 connection->doneListOk = 0;
362 connection->returnElement = NULL;
363 connection->request = NULL;
365 if (winsock_dll_error(connection)) {
369 if (mpd_connect(connection, host, port, timeout) < 0) {
373 while (!(rt = strstr(connection->buffer, "\n"))) {
374 tv.tv_sec = connection->timeout.tv_sec;
375 tv.tv_usec = connection->timeout.tv_usec;
377 FD_SET(connection->sock, &fds);
378 if ((err = select(connection->sock + 1, &fds, NULL, NULL, &tv)) == 1) {
381 readed = recv(connection->sock,
382 &(connection->buffer[connection->buflen]),
383 MPD_BUFFER_MAX_LENGTH - connection->buflen, 0);
385 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
386 "problems getting a response from \"%s\" on port %i : %s",
387 host, port, strerror(errno));
388 connection->error = MPD_ERROR_NORESPONSE;
391 connection->buflen += readed;
392 connection->buffer[connection->buflen] = '\0';
393 } else if (err < 0) {
394 if (SELECT_ERRNO_IGNORE) {
397 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
398 "problems connecting to \"%s\" on port %i", host, port);
399 connection->error = MPD_ERROR_CONNPORT;
402 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
403 "timeout in attempting to get a response from \"%s\" on "
404 "port %i", host, port);
405 connection->error = MPD_ERROR_NORESPONSE;
411 output = strdup(connection->buffer);
412 strcpy(connection->buffer, rt + 1);
413 connection->buflen = strlen(connection->buffer);
415 if (mpd_parseWelcome(connection, host, port, /* rt, */ output) == 0) {
416 connection->doneProcessing = 1;
424 void mpd_clearError(mpd_Connection *connection)
426 connection->error = 0;
427 connection->errorStr[0] = '\0';
430 void mpd_closeConnection(mpd_Connection *connection)
432 closesocket(connection->sock);
433 if (connection->returnElement) {
434 free(connection->returnElement);
436 if (connection->request) {
437 free(connection->request);
443 static void mpd_executeCommand(mpd_Connection *connection, const char *command)
448 const char *commandPtr = command;
449 int commandLen = strlen(command);
451 if (!connection->doneProcessing && !connection->commandList) {
452 strcpy(connection->errorStr, "not done processing current command");
453 connection->error = 1;
457 mpd_clearError(connection);
460 FD_SET(connection->sock, &fds);
461 tv.tv_sec = connection->timeout.tv_sec;
462 tv.tv_usec = connection->timeout.tv_usec;
464 while ((ret = select(connection->sock + 1, NULL, &fds, NULL, &tv) == 1)
465 || (ret == -1 && SELECT_ERRNO_IGNORE)) {
466 ret = send(connection->sock, commandPtr, commandLen, MSG_DONTWAIT);
468 if (SENDRECV_ERRNO_IGNORE) {
471 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
472 "problems giving command \"%s\"", command);
473 connection->error = MPD_ERROR_SENDING;
480 if (commandLen <= 0) {
485 if (commandLen > 0) {
487 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
488 "timeout sending command \"%s\"", command);
489 connection->error = MPD_ERROR_TIMEOUT;
493 if (!connection->commandList) {
494 connection->doneProcessing = 0;
495 } else if (connection->commandList == COMMAND_LIST_OK) {
496 connection->listOks++;
500 static void mpd_getNextReturnElement(mpd_Connection *connection)
510 char *bufferCheck = NULL;
514 if (connection->returnElement) {
515 mpd_freeReturnElement(connection->returnElement);
517 connection->returnElement = NULL;
519 if (connection->doneProcessing
520 || (connection->listOks && connection->doneListOk)) {
521 strcpy(connection->errorStr,
522 "already done processing current command");
523 connection->error = 1;
527 bufferCheck = connection->buffer + connection->bufstart;
528 while (connection->bufstart >= connection->buflen
529 || !(rt = strchr(bufferCheck, '\n'))) {
530 if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
531 memmove(connection->buffer,
532 connection->buffer + connection->bufstart,
533 connection->buflen - connection->bufstart + 1);
534 connection->buflen -= connection->bufstart;
535 connection->bufstart = 0;
537 if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
538 strcpy(connection->errorStr, "buffer overrun");
539 connection->error = MPD_ERROR_BUFFEROVERRUN;
540 connection->doneProcessing = 1;
541 connection->doneListOk = 0;
544 bufferCheck = connection->buffer + connection->buflen;
545 tv.tv_sec = connection->timeout.tv_sec;
546 tv.tv_usec = connection->timeout.tv_usec;
548 FD_SET(connection->sock, &fds);
549 if ((err = select(connection->sock + 1, &fds, NULL, NULL, &tv) == 1)) {
550 readed = recv(connection->sock,
551 connection->buffer + connection->buflen,
552 MPD_BUFFER_MAX_LENGTH - connection->buflen, MSG_DONTWAIT);
553 if (readed < 0 && SENDRECV_ERRNO_IGNORE) {
557 strcpy(connection->errorStr, "connection closed");
558 connection->error = MPD_ERROR_CONNCLOSED;
559 connection->doneProcessing = 1;
560 connection->doneListOk = 0;
563 connection->buflen += readed;
564 connection->buffer[connection->buflen] = '\0';
565 } else if (err < 0 && SELECT_ERRNO_IGNORE) {
568 strcpy(connection->errorStr, "connection timeout");
569 connection->error = MPD_ERROR_TIMEOUT;
570 connection->doneProcessing = 1;
571 connection->doneListOk = 0;
577 output = connection->buffer + connection->bufstart;
578 connection->bufstart = rt - connection->buffer + 1;
580 if (strcmp(output, "OK") == 0) {
581 if (connection->listOks > 0) {
582 strcpy(connection->errorStr, "expected more list_OK's");
583 connection->error = 1;
585 connection->listOks = 0;
586 connection->doneProcessing = 1;
587 connection->doneListOk = 0;
591 if (strcmp(output, "list_OK") == 0) {
592 if (!connection->listOks) {
593 strcpy(connection->errorStr, "got an unexpected list_OK");
594 connection->error = 1;
596 connection->doneListOk = 1;
597 connection->listOks--;
602 if (strncmp(output, "ACK", strlen("ACK")) == 0) {
607 strcpy(connection->errorStr, output);
608 connection->error = MPD_ERROR_ACK;
609 connection->errorCode = MPD_ACK_ERROR_UNK;
610 connection->errorAt = MPD_ERROR_AT_UNK;
611 connection->doneProcessing = 1;
612 connection->doneListOk = 0;
614 needle = strchr(output, '[');
618 val = strtol(needle + 1, &test, 10);
622 connection->errorCode = val;
623 val = strtol(test + 1, &test, 10);
627 connection->errorAt = val;
631 tok = strchr(output, ':');
640 if (value[0] == ' ') {
641 connection->returnElement = mpd_newReturnElement(name, &(value[1]));
643 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
644 "error parsing: %s:%s", name, value);
645 connection->error = 1;
649 void mpd_finishCommand(mpd_Connection *connection)
651 while (!connection->doneProcessing) {
652 if (connection->doneListOk) {
653 connection->doneListOk = 0;
655 mpd_getNextReturnElement(connection);
659 static void mpd_finishListOkCommand(mpd_Connection *connection)
661 while (!connection->doneProcessing && connection->listOks
662 && !connection->doneListOk) {
663 mpd_getNextReturnElement(connection);
667 int mpd_nextListOkCommand(mpd_Connection *connection)
669 mpd_finishListOkCommand(connection);
670 if (!connection->doneProcessing) {
671 connection->doneListOk = 0;
673 if (connection->listOks == 0 || connection->doneProcessing) {
679 void mpd_sendStatusCommand(mpd_Connection *connection)
681 mpd_executeCommand(connection, "status\n");
684 mpd_Status *mpd_getStatus(mpd_Connection *connection)
688 /* mpd_executeCommand(connection, "status\n");
689 if (connection->error) {
693 if (connection->doneProcessing
694 || (connection->listOks && connection->doneListOk)) {
698 if (!connection->returnElement) {
699 mpd_getNextReturnElement(connection);
702 status = malloc(sizeof(mpd_Status));
706 status->playlist = -1;
707 status->playlistLength = -1;
711 status->elapsedTime = 0;
712 status->totalTime = 0;
714 status->sampleRate = 0;
716 status->channels = 0;
717 status->crossfade = -1;
718 status->error = NULL;
719 status->updatingDb = 0;
721 if (connection->error) {
725 while (connection->returnElement) {
726 mpd_ReturnElement *re = connection->returnElement;
728 if (strcmp(re->name, "volume") == 0) {
729 status->volume = atoi(re->value);
730 } else if (strcmp(re->name, "repeat") == 0) {
731 status->repeat = atoi(re->value);
732 } else if (strcmp(re->name, "random") == 0) {
733 status->random = atoi(re->value);
734 } else if (strcmp(re->name, "playlist") == 0) {
735 status->playlist = strtol(re->value, NULL, 10);
736 } else if (strcmp(re->name, "playlistlength") == 0) {
737 status->playlistLength = atoi(re->value);
738 } else if (strcmp(re->name, "bitrate") == 0) {
739 status->bitRate = atoi(re->value);
740 } else if (strcmp(re->name, "state") == 0) {
741 if (strcmp(re->value, "play") == 0) {
742 status->state = MPD_STATUS_STATE_PLAY;
743 } else if (strcmp(re->value, "stop") == 0) {
744 status->state = MPD_STATUS_STATE_STOP;
745 } else if (strcmp(re->value, "pause") == 0) {
746 status->state = MPD_STATUS_STATE_PAUSE;
748 status->state = MPD_STATUS_STATE_UNKNOWN;
750 } else if (strcmp(re->name, "song") == 0) {
751 status->song = atoi(re->value);
752 } else if (strcmp(re->name, "songid") == 0) {
753 status->songid = atoi(re->value);
754 } else if (strcmp(re->name, "time") == 0) {
755 char *tok = strchr(re->value, ':');
757 /* the second strchr below is a safety check */
758 if (tok && (strchr(tok, 0) > (tok + 1))) {
759 /* atoi stops at the first non-[0-9] char: */
760 status->elapsedTime = atoi(re->value);
761 status->totalTime = atoi(tok + 1);
763 } else if (strcmp(re->name, "error") == 0) {
764 status->error = strdup(re->value);
765 } else if (strcmp(re->name, "xfade") == 0) {
766 status->crossfade = atoi(re->value);
767 } else if (strcmp(re->name, "updating_db") == 0) {
768 status->updatingDb = atoi(re->value);
769 } else if (strcmp(re->name, "audio") == 0) {
770 char *tok = strchr(re->value, ':');
772 if (tok && (strchr(tok, 0) > (tok + 1))) {
773 status->sampleRate = atoi(re->value);
774 status->bits = atoi(++tok);
775 tok = strchr(tok, ':');
776 if (tok && (strchr(tok, 0) > (tok + 1))) {
777 status->channels = atoi(tok + 1);
782 mpd_getNextReturnElement(connection);
783 if (connection->error) {
789 if (connection->error) {
792 } else if (status->state < 0) {
793 strcpy(connection->errorStr, "state not found");
794 connection->error = 1;
802 void mpd_freeStatus(mpd_Status *status)
810 void mpd_sendStatsCommand(mpd_Connection *connection)
812 mpd_executeCommand(connection, "stats\n");
815 mpd_Stats *mpd_getStats(mpd_Connection *connection)
819 /* mpd_executeCommand(connection, "stats\n");
820 if (connection->error) {
824 if (connection->doneProcessing
825 || (connection->listOks && connection->doneListOk)) {
829 if (!connection->returnElement) {
830 mpd_getNextReturnElement(connection);
833 stats = malloc(sizeof(mpd_Stats));
834 stats->numberOfArtists = 0;
835 stats->numberOfAlbums = 0;
836 stats->numberOfSongs = 0;
838 stats->dbUpdateTime = 0;
840 stats->dbPlayTime = 0;
842 if (connection->error) {
846 while (connection->returnElement) {
847 mpd_ReturnElement *re = connection->returnElement;
849 if (strcmp(re->name, "artists") == 0) {
850 stats->numberOfArtists = atoi(re->value);
851 } else if (strcmp(re->name, "albums") == 0) {
852 stats->numberOfAlbums = atoi(re->value);
853 } else if (strcmp(re->name, "songs") == 0) {
854 stats->numberOfSongs = atoi(re->value);
855 } else if (strcmp(re->name, "uptime") == 0) {
856 stats->uptime = strtol(re->value, NULL, 10);
857 } else if (strcmp(re->name, "db_update") == 0) {
858 stats->dbUpdateTime = strtol(re->value, NULL, 10);
859 } else if (strcmp(re->name, "playtime") == 0) {
860 stats->playTime = strtol(re->value, NULL, 10);
861 } else if (strcmp(re->name, "db_playtime") == 0) {
862 stats->dbPlayTime = strtol(re->value, NULL, 10);
865 mpd_getNextReturnElement(connection);
866 if (connection->error) {
872 if (connection->error) {
880 void mpd_freeStats(mpd_Stats *stats)
885 mpd_SearchStats *mpd_getSearchStats(mpd_Connection *connection)
887 mpd_SearchStats *stats;
888 mpd_ReturnElement *re;
890 if (connection->doneProcessing
891 || (connection->listOks && connection->doneListOk)) {
895 if (!connection->returnElement) {
896 mpd_getNextReturnElement(connection);
899 if (connection->error) {
903 stats = malloc(sizeof(mpd_SearchStats));
904 stats->numberOfSongs = 0;
907 while (connection->returnElement) {
908 re = connection->returnElement;
910 if (strcmp(re->name, "songs") == 0) {
911 stats->numberOfSongs = atoi(re->value);
912 } else if (strcmp(re->name, "playtime") == 0) {
913 stats->playTime = strtol(re->value, NULL, 10);
916 mpd_getNextReturnElement(connection);
917 if (connection->error) {
923 if (connection->error) {
931 void mpd_freeSearchStats(mpd_SearchStats *stats)
936 static void mpd_initSong(mpd_Song *song)
947 song->composer = NULL;
948 song->performer = NULL;
950 song->comment = NULL;
952 song->time = MPD_SONG_NO_TIME;
953 song->pos = MPD_SONG_NO_NUM;
954 song->id = MPD_SONG_NO_ID;
957 static void mpd_finishSong(mpd_Song *song)
983 if (song->composer) {
984 free(song->composer);
994 mpd_Song *mpd_newSong(void)
996 mpd_Song *ret = malloc(sizeof(mpd_Song));
1003 void mpd_freeSong(mpd_Song *song)
1005 mpd_finishSong(song);
1009 mpd_Song *mpd_songDup(mpd_Song *song)
1011 mpd_Song *ret = mpd_newSong();
1014 ret->file = strdup(song->file);
1017 ret->artist = strdup(song->artist);
1020 ret->album = strdup(song->album);
1023 ret->title = strdup(song->title);
1026 ret->track = strdup(song->track);
1029 ret->name = strdup(song->name);
1032 ret->date = strdup(song->date);
1035 ret->genre = strdup(song->genre);
1037 if (song->composer) {
1038 ret->composer = strdup(song->composer);
1041 ret->disc = strdup(song->disc);
1043 if (song->comment) {
1044 ret->comment = strdup(song->comment);
1046 ret->time = song->time;
1047 ret->pos = song->pos;
1053 static void mpd_initDirectory(mpd_Directory *directory)
1055 directory->path = NULL;
1058 static void mpd_finishDirectory(mpd_Directory *directory)
1060 if (directory->path) {
1061 free(directory->path);
1065 mpd_Directory *mpd_newDirectory(void)
1067 mpd_Directory *directory = malloc(sizeof(mpd_Directory));
1069 mpd_initDirectory(directory);
1074 void mpd_freeDirectory(mpd_Directory *directory)
1076 mpd_finishDirectory(directory);
1081 mpd_Directory *mpd_directoryDup(mpd_Directory *directory)
1083 mpd_Directory *ret = mpd_newDirectory();
1085 if (directory->path) {
1086 ret->path = strdup(directory->path);
1092 static void mpd_initPlaylistFile(mpd_PlaylistFile *playlist)
1094 playlist->path = NULL;
1097 static void mpd_finishPlaylistFile(mpd_PlaylistFile *playlist)
1099 if (playlist->path) {
1100 free(playlist->path);
1104 mpd_PlaylistFile *mpd_newPlaylistFile(void)
1106 mpd_PlaylistFile *playlist = malloc(sizeof(mpd_PlaylistFile));
1108 mpd_initPlaylistFile(playlist);
1113 void mpd_freePlaylistFile(mpd_PlaylistFile *playlist)
1115 mpd_finishPlaylistFile(playlist);
1119 mpd_PlaylistFile *mpd_playlistFileDup(mpd_PlaylistFile *playlist)
1121 mpd_PlaylistFile *ret = mpd_newPlaylistFile();
1123 if (playlist->path) {
1124 ret->path = strdup(playlist->path);
1130 static void mpd_initInfoEntity(mpd_InfoEntity *entity)
1132 entity->info.directory = NULL;
1135 static void mpd_finishInfoEntity(mpd_InfoEntity *entity)
1137 if (entity->info.directory) {
1138 if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1139 mpd_freeDirectory(entity->info.directory);
1140 } else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
1141 mpd_freeSong(entity->info.song);
1142 } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1143 mpd_freePlaylistFile(entity->info.playlistFile);
1148 mpd_InfoEntity *mpd_newInfoEntity(void)
1150 mpd_InfoEntity *entity = malloc(sizeof(mpd_InfoEntity));
1152 mpd_initInfoEntity(entity);
1157 void mpd_freeInfoEntity(mpd_InfoEntity *entity)
1159 mpd_finishInfoEntity(entity);
1163 static void mpd_sendInfoCommand(mpd_Connection *connection, char *command)
1165 mpd_executeCommand(connection, command);
1168 mpd_InfoEntity *mpd_getNextInfoEntity(mpd_Connection *connection)
1170 mpd_InfoEntity *entity = NULL;
1172 if (connection->doneProcessing
1173 || (connection->listOks && connection->doneListOk)) {
1177 if (!connection->returnElement) {
1178 mpd_getNextReturnElement(connection);
1181 if (connection->returnElement) {
1182 if (strcmp(connection->returnElement->name, "file") == 0) {
1183 entity = mpd_newInfoEntity();
1184 entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1185 entity->info.song = mpd_newSong();
1186 entity->info.song->file = strdup(connection->returnElement->value);
1187 } else if (strcmp(connection->returnElement->name, "directory") == 0) {
1188 entity = mpd_newInfoEntity();
1189 entity->type = MPD_INFO_ENTITY_TYPE_DIRECTORY;
1190 entity->info.directory = mpd_newDirectory();
1191 entity->info.directory->path =
1192 strdup(connection->returnElement->value);
1193 } else if (strcmp(connection->returnElement->name, "playlist") == 0) {
1194 entity = mpd_newInfoEntity();
1195 entity->type = MPD_INFO_ENTITY_TYPE_PLAYLISTFILE;
1196 entity->info.playlistFile = mpd_newPlaylistFile();
1197 entity->info.playlistFile->path =
1198 strdup(connection->returnElement->value);
1199 } else if (strcmp(connection->returnElement->name, "cpos") == 0) {
1200 entity = mpd_newInfoEntity();
1201 entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1202 entity->info.song = mpd_newSong();
1203 entity->info.song->pos = atoi(connection->returnElement->value);
1205 connection->error = 1;
1206 strcpy(connection->errorStr, "problem parsing song info");
1213 mpd_getNextReturnElement(connection);
1214 while (connection->returnElement) {
1215 mpd_ReturnElement *re = connection->returnElement;
1217 if (strcmp(re->name, "file") == 0) {
1219 } else if (strcmp(re->name, "directory") == 0) {
1221 } else if (strcmp(re->name, "playlist") == 0) {
1223 } else if (strcmp(re->name, "cpos") == 0) {
1227 if (entity->type == MPD_INFO_ENTITY_TYPE_SONG && strlen(re->value)) {
1228 if (!entity->info.song->artist
1229 && strcmp(re->name, "Artist") == 0) {
1230 entity->info.song->artist = strdup(re->value);
1231 } else if (!entity->info.song->album
1232 && strcmp(re->name, "Album") == 0) {
1233 entity->info.song->album = strdup(re->value);
1234 } else if (!entity->info.song->title
1235 && strcmp(re->name, "Title") == 0) {
1236 entity->info.song->title = strdup(re->value);
1237 } else if (!entity->info.song->track
1238 && strcmp(re->name, "Track") == 0) {
1239 entity->info.song->track = strdup(re->value);
1240 } else if (!entity->info.song->name
1241 && strcmp(re->name, "Name") == 0) {
1242 entity->info.song->name = strdup(re->value);
1243 } else if (entity->info.song->time == MPD_SONG_NO_TIME
1244 && strcmp(re->name, "Time") == 0) {
1245 entity->info.song->time = atoi(re->value);
1246 } else if (entity->info.song->pos == MPD_SONG_NO_NUM
1247 && strcmp(re->name, "Pos") == 0) {
1248 entity->info.song->pos = atoi(re->value);
1249 } else if (entity->info.song->id == MPD_SONG_NO_ID
1250 && strcmp(re->name, "Id") == 0) {
1251 entity->info.song->id = atoi(re->value);
1252 } else if (!entity->info.song->date
1253 && strcmp(re->name, "Date") == 0) {
1254 entity->info.song->date = strdup(re->value);
1255 } else if (!entity->info.song->genre
1256 && strcmp(re->name, "Genre") == 0) {
1257 entity->info.song->genre = strdup(re->value);
1258 } else if (!entity->info.song->composer
1259 && strcmp(re->name, "Composer") == 0) {
1260 entity->info.song->composer = strdup(re->value);
1261 } else if (!entity->info.song->performer
1262 && strcmp(re->name, "Performer") == 0) {
1263 entity->info.song->performer = strdup(re->value);
1264 } else if (!entity->info.song->disc
1265 && strcmp(re->name, "Disc") == 0) {
1266 entity->info.song->disc = strdup(re->value);
1267 } else if (!entity->info.song->comment
1268 && strcmp(re->name, "Comment") == 0) {
1269 entity->info.song->comment = strdup(re->value);
1271 } else if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1272 } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1275 mpd_getNextReturnElement(connection);
1281 static char *mpd_getNextReturnElementNamed(mpd_Connection *connection,
1284 if (connection->doneProcessing
1285 || (connection->listOks && connection->doneListOk)) {
1289 mpd_getNextReturnElement(connection);
1290 while (connection->returnElement) {
1291 mpd_ReturnElement *re = connection->returnElement;
1293 if (strcmp(re->name, name) == 0) {
1294 return strdup(re->value);
1296 mpd_getNextReturnElement(connection);
1302 char *mpd_getNextTag(mpd_Connection *connection, int type)
1304 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES
1305 || type == MPD_TAG_ITEM_ANY) {
1308 if (type == MPD_TAG_ITEM_FILENAME) {
1309 return mpd_getNextReturnElementNamed(connection, "file");
1311 return mpd_getNextReturnElementNamed(connection, mpdTagItemKeys[type]);
1314 char *mpd_getNextArtist(mpd_Connection *connection)
1316 return mpd_getNextReturnElementNamed(connection, "Artist");
1319 char *mpd_getNextAlbum(mpd_Connection *connection)
1321 return mpd_getNextReturnElementNamed(connection, "Album");
1324 void mpd_sendPlaylistInfoCommand(mpd_Connection *connection, int songPos)
1326 int len = strlen("playlistinfo") + 2 + INTLEN + 3;
1327 char *string = malloc(len);
1329 snprintf(string, len, "playlistinfo \"%i\"\n", songPos);
1330 mpd_sendInfoCommand(connection, string);
1334 void mpd_sendPlaylistIdCommand(mpd_Connection *connection, int id)
1336 int len = strlen("playlistid") + 2 + INTLEN + 3;
1337 char *string = malloc(len);
1339 snprintf(string, len, "playlistid \"%i\"\n", id);
1340 mpd_sendInfoCommand(connection, string);
1344 void mpd_sendPlChangesCommand(mpd_Connection *connection, long long playlist)
1346 int len = strlen("plchanges") + 2 + LONGLONGLEN + 3;
1347 char *string = malloc(len);
1349 snprintf(string, len, "plchanges \"%lld\"\n", playlist);
1350 mpd_sendInfoCommand(connection, string);
1354 void mpd_sendPlChangesPosIdCommand(mpd_Connection *connection,
1357 int len = strlen("plchangesposid") + 2 + LONGLONGLEN + 3;
1358 char *string = malloc(len);
1360 snprintf(string, len, "plchangesposid \"%lld\"\n", playlist);
1361 mpd_sendInfoCommand(connection, string);
1365 void mpd_sendListallCommand(mpd_Connection *connection, const char *dir)
1367 char *sDir = mpd_sanitizeArg(dir);
1368 int len = strlen("listall") + 2 + strlen(sDir) + 3;
1369 char *string = malloc(len);
1371 snprintf(string, len, "listall \"%s\"\n", sDir);
1372 mpd_sendInfoCommand(connection, string);
1377 void mpd_sendListallInfoCommand(mpd_Connection *connection, const char *dir)
1379 char *sDir = mpd_sanitizeArg(dir);
1380 int len = strlen("listallinfo") + 2 + strlen(sDir) + 3;
1381 char *string = malloc(len);
1383 snprintf(string, len, "listallinfo \"%s\"\n", sDir);
1384 mpd_sendInfoCommand(connection, string);
1389 void mpd_sendLsInfoCommand(mpd_Connection *connection, const char *dir)
1391 char *sDir = mpd_sanitizeArg(dir);
1392 int len = strlen("lsinfo") + 2 + strlen(sDir) + 3;
1393 char *string = malloc(len);
1395 snprintf(string, len, "lsinfo \"%s\"\n", sDir);
1396 mpd_sendInfoCommand(connection, string);
1401 void mpd_sendCurrentSongCommand(mpd_Connection *connection)
1403 mpd_executeCommand(connection, "currentsong\n");
1406 void mpd_sendSearchCommand(mpd_Connection *connection, int table,
1409 mpd_startSearch(connection, 0);
1410 mpd_addConstraintSearch(connection, table, str);
1411 mpd_commitSearch(connection);
1414 void mpd_sendFindCommand(mpd_Connection *connection, int table,
1417 mpd_startSearch(connection, 1);
1418 mpd_addConstraintSearch(connection, table, str);
1419 mpd_commitSearch(connection);
1422 void mpd_sendListCommand(mpd_Connection *connection, int table,
1429 if (table == MPD_TABLE_ARTIST) {
1430 strcpy(st, "artist");
1431 } else if (table == MPD_TABLE_ALBUM) {
1432 strcpy(st, "album");
1434 connection->error = 1;
1435 strcpy(connection->errorStr, "unknown table for list");
1439 char *sanitArg1 = mpd_sanitizeArg(arg1);
1441 len = strlen("list") + 1 + strlen(sanitArg1) + 2 + strlen(st) + 3;
1442 string = malloc(len);
1443 snprintf(string, len, "list %s \"%s\"\n", st, sanitArg1);
1446 len = strlen("list") + 1 + strlen(st) + 2;
1447 string = malloc(len);
1448 snprintf(string, len, "list %s\n", st);
1450 mpd_sendInfoCommand(connection, string);
1454 void mpd_sendAddCommand(mpd_Connection *connection, const char *file)
1456 char *sFile = mpd_sanitizeArg(file);
1457 int len = strlen("add") + 2 + strlen(sFile) + 3;
1458 char *string = malloc(len);
1460 snprintf(string, len, "add \"%s\"\n", sFile);
1461 mpd_executeCommand(connection, string);
1466 int mpd_sendAddIdCommand(mpd_Connection *connection, const char *file)
1469 char *sFile = mpd_sanitizeArg(file);
1470 int len = strlen("addid") + 2 + strlen(sFile) + 3;
1471 char *string = malloc(len);
1473 snprintf(string, len, "addid \"%s\"\n", sFile);
1474 mpd_sendInfoCommand(connection, string);
1478 string = mpd_getNextReturnElementNamed(connection, "Id");
1480 retval = atoi(string);
1487 void mpd_sendDeleteCommand(mpd_Connection *connection, int songPos)
1489 int len = strlen("delete") + 2 + INTLEN + 3;
1490 char *string = malloc(len);
1492 snprintf(string, len, "delete \"%i\"\n", songPos);
1493 mpd_sendInfoCommand(connection, string);
1497 void mpd_sendDeleteIdCommand(mpd_Connection *connection, int id)
1499 int len = strlen("deleteid") + 2 + INTLEN + 3;
1500 char *string = malloc(len);
1502 snprintf(string, len, "deleteid \"%i\"\n", id);
1503 mpd_sendInfoCommand(connection, string);
1507 void mpd_sendSaveCommand(mpd_Connection *connection, const char *name)
1509 char *sName = mpd_sanitizeArg(name);
1510 int len = strlen("save") + 2 + strlen(sName) + 3;
1511 char *string = malloc(len);
1513 snprintf(string, len, "save \"%s\"\n", sName);
1514 mpd_executeCommand(connection, string);
1519 void mpd_sendLoadCommand(mpd_Connection *connection, const char *name)
1521 char *sName = mpd_sanitizeArg(name);
1522 int len = strlen("load") + 2 + strlen(sName) + 3;
1523 char *string = malloc(len);
1525 snprintf(string, len, "load \"%s\"\n", sName);
1526 mpd_executeCommand(connection, string);
1531 void mpd_sendRmCommand(mpd_Connection *connection, const char *name)
1533 char *sName = mpd_sanitizeArg(name);
1534 int len = strlen("rm") + 2 + strlen(sName) + 3;
1535 char *string = malloc(len);
1537 snprintf(string, len, "rm \"%s\"\n", sName);
1538 mpd_executeCommand(connection, string);
1543 void mpd_sendRenameCommand(mpd_Connection *connection, const char *from,
1546 char *sFrom = mpd_sanitizeArg(from);
1547 char *sTo = mpd_sanitizeArg(to);
1548 int len = strlen("rename") + 2 + strlen(sFrom) + 3 + strlen(sTo) + 3;
1549 char *string = malloc(len);
1551 snprintf(string, len, "rename \"%s\" \"%s\"\n", sFrom, sTo);
1552 mpd_executeCommand(connection, string);
1558 void mpd_sendShuffleCommand(mpd_Connection *connection)
1560 mpd_executeCommand(connection, "shuffle\n");
1563 void mpd_sendClearCommand(mpd_Connection *connection)
1565 mpd_executeCommand(connection, "clear\n");
1568 void mpd_sendPlayCommand(mpd_Connection *connection, int songPos)
1570 int len = strlen("play") + 2 + INTLEN + 3;
1571 char *string = malloc(len);
1573 snprintf(string, len, "play \"%i\"\n", songPos);
1574 mpd_sendInfoCommand(connection, string);
1578 void mpd_sendPlayIdCommand(mpd_Connection *connection, int id)
1580 int len = strlen("playid") + 2 + INTLEN + 3;
1581 char *string = malloc(len);
1583 snprintf(string, len, "playid \"%i\"\n", id);
1584 mpd_sendInfoCommand(connection, string);
1588 void mpd_sendStopCommand(mpd_Connection *connection)
1590 mpd_executeCommand(connection, "stop\n");
1593 void mpd_sendPauseCommand(mpd_Connection *connection, int pauseMode)
1595 int len = strlen("pause") + 2 + INTLEN + 3;
1596 char *string = malloc(len);
1598 snprintf(string, len, "pause \"%i\"\n", pauseMode);
1599 mpd_executeCommand(connection, string);
1603 void mpd_sendNextCommand(mpd_Connection *connection)
1605 mpd_executeCommand(connection, "next\n");
1608 void mpd_sendMoveCommand(mpd_Connection *connection, int from, int to)
1610 int len = strlen("move") + 2 + INTLEN + 3 + INTLEN + 3;
1611 char *string = malloc(len);
1613 snprintf(string, len, "move \"%i\" \"%i\"\n", from, to);
1614 mpd_sendInfoCommand(connection, string);
1618 void mpd_sendMoveIdCommand(mpd_Connection *connection, int id, int to)
1620 int len = strlen("moveid") + 2 + INTLEN + 3 + INTLEN + 3;
1621 char *string = malloc(len);
1623 snprintf(string, len, "moveid \"%i\" \"%i\"\n", id, to);
1624 mpd_sendInfoCommand(connection, string);
1628 void mpd_sendSwapCommand(mpd_Connection *connection, int song1, int song2)
1630 int len = strlen("swap") + 2 + INTLEN + 3 + INTLEN + 3;
1631 char *string = malloc(len);
1633 snprintf(string, len, "swap \"%i\" \"%i\"\n", song1, song2);
1634 mpd_sendInfoCommand(connection, string);
1638 void mpd_sendSwapIdCommand(mpd_Connection *connection, int id1, int id2)
1640 int len = strlen("swapid") + 2 + INTLEN + 3 + INTLEN + 3;
1641 char *string = malloc(len);
1643 snprintf(string, len, "swapid \"%i\" \"%i\"\n", id1, id2);
1644 mpd_sendInfoCommand(connection, string);
1648 void mpd_sendSeekCommand(mpd_Connection *connection, int song, int time)
1650 int len = strlen("seek") + 2 + INTLEN + 3 + INTLEN + 3;
1651 char *string = malloc(len);
1653 snprintf(string, len, "seek \"%i\" \"%i\"\n", song, time);
1654 mpd_sendInfoCommand(connection, string);
1658 void mpd_sendSeekIdCommand(mpd_Connection *connection, int id, int time)
1660 int len = strlen("seekid") + 2 + INTLEN + 3 + INTLEN + 3;
1661 char *string = malloc(len);
1663 snprintf(string, len, "seekid \"%i\" \"%i\"\n", id, time);
1664 mpd_sendInfoCommand(connection, string);
1668 void mpd_sendUpdateCommand(mpd_Connection *connection, char *path)
1670 char *sPath = mpd_sanitizeArg(path);
1671 int len = strlen("update") + 2 + strlen(sPath) + 3;
1672 char *string = malloc(len);
1674 snprintf(string, len, "update \"%s\"\n", sPath);
1675 mpd_sendInfoCommand(connection, string);
1680 int mpd_getUpdateId(mpd_Connection *connection)
1685 jobid = mpd_getNextReturnElementNamed(connection, "updating_db");
1694 void mpd_sendPrevCommand(mpd_Connection *connection)
1696 mpd_executeCommand(connection, "previous\n");
1699 void mpd_sendRepeatCommand(mpd_Connection *connection, int repeatMode)
1701 int len = strlen("repeat") + 2 + INTLEN + 3;
1702 char *string = malloc(len);
1704 snprintf(string, len, "repeat \"%i\"\n", repeatMode);
1705 mpd_executeCommand(connection, string);
1709 void mpd_sendRandomCommand(mpd_Connection *connection, int randomMode)
1711 int len = strlen("random") + 2 + INTLEN + 3;
1712 char *string = malloc(len);
1714 snprintf(string, len, "random \"%i\"\n", randomMode);
1715 mpd_executeCommand(connection, string);
1719 void mpd_sendSetvolCommand(mpd_Connection *connection, int volumeChange)
1721 int len = strlen("setvol") + 2 + INTLEN + 3;
1722 char *string = malloc(len);
1724 snprintf(string, len, "setvol \"%i\"\n", volumeChange);
1725 mpd_executeCommand(connection, string);
1729 void mpd_sendVolumeCommand(mpd_Connection *connection, int volumeChange)
1731 int len = strlen("volume") + 2 + INTLEN + 3;
1732 char *string = malloc(len);
1734 snprintf(string, len, "volume \"%i\"\n", volumeChange);
1735 mpd_executeCommand(connection, string);
1739 void mpd_sendCrossfadeCommand(mpd_Connection *connection, int seconds)
1741 int len = strlen("crossfade") + 2 + INTLEN + 3;
1742 char *string = malloc(len);
1744 snprintf(string, len, "crossfade \"%i\"\n", seconds);
1745 mpd_executeCommand(connection, string);
1749 void mpd_sendPasswordCommand(mpd_Connection *connection, const char *pass)
1751 char *sPass = mpd_sanitizeArg(pass);
1752 int len = strlen("password") + 2 + strlen(sPass) + 3;
1753 char *string = malloc(len);
1755 snprintf(string, len, "password \"%s\"\n", sPass);
1756 mpd_executeCommand(connection, string);
1761 void mpd_sendCommandListBegin(mpd_Connection *connection)
1763 if (connection->commandList) {
1764 strcpy(connection->errorStr, "already in command list mode");
1765 connection->error = 1;
1768 connection->commandList = COMMAND_LIST;
1769 mpd_executeCommand(connection, "command_list_begin\n");
1772 void mpd_sendCommandListOkBegin(mpd_Connection *connection)
1774 if (connection->commandList) {
1775 strcpy(connection->errorStr, "already in command list mode");
1776 connection->error = 1;
1779 connection->commandList = COMMAND_LIST_OK;
1780 mpd_executeCommand(connection, "command_list_ok_begin\n");
1781 connection->listOks = 0;
1784 void mpd_sendCommandListEnd(mpd_Connection *connection)
1786 if (!connection->commandList) {
1787 strcpy(connection->errorStr, "not in command list mode");
1788 connection->error = 1;
1791 connection->commandList = 0;
1792 mpd_executeCommand(connection, "command_list_end\n");
1795 void mpd_sendOutputsCommand(mpd_Connection *connection)
1797 mpd_executeCommand(connection, "outputs\n");
1800 mpd_OutputEntity *mpd_getNextOutput(mpd_Connection *connection)
1802 mpd_OutputEntity *output = NULL;
1804 if (connection->doneProcessing
1805 || (connection->listOks && connection->doneListOk)) {
1809 if (connection->error) {
1813 output = malloc(sizeof(mpd_OutputEntity));
1815 output->name = NULL;
1816 output->enabled = 0;
1818 if (!connection->returnElement) {
1819 mpd_getNextReturnElement(connection);
1822 while (connection->returnElement) {
1823 mpd_ReturnElement *re = connection->returnElement;
1825 if (strcmp(re->name, "outputid") == 0) {
1826 if (output != NULL && output->id >= 0) {
1829 output->id = atoi(re->value);
1830 } else if (strcmp(re->name, "outputname") == 0) {
1831 output->name = strdup(re->value);
1832 } else if (strcmp(re->name, "outputenabled") == 0) {
1833 output->enabled = atoi(re->value);
1836 mpd_getNextReturnElement(connection);
1837 if (connection->error) {
1846 void mpd_sendEnableOutputCommand(mpd_Connection *connection, int outputId)
1848 int len = strlen("enableoutput") + 2 + INTLEN + 3;
1849 char *string = malloc(len);
1851 snprintf(string, len, "enableoutput \"%i\"\n", outputId);
1852 mpd_executeCommand(connection, string);
1856 void mpd_sendDisableOutputCommand(mpd_Connection *connection, int outputId)
1858 int len = strlen("disableoutput") + 2 + INTLEN + 3;
1859 char *string = malloc(len);
1861 snprintf(string, len, "disableoutput \"%i\"\n", outputId);
1862 mpd_executeCommand(connection, string);
1866 void mpd_freeOutputElement(mpd_OutputEntity *output)
1872 /** odd naming, but it gets the not allowed commands */
1873 void mpd_sendNotCommandsCommand(mpd_Connection *connection)
1875 mpd_executeCommand(connection, "notcommands\n");
1878 /** odd naming, but it gets the allowed commands */
1879 void mpd_sendCommandsCommand(mpd_Connection *connection)
1881 mpd_executeCommand(connection, "commands\n");
1884 /** Get the next returned command */
1885 char *mpd_getNextCommand(mpd_Connection *connection)
1887 return mpd_getNextReturnElementNamed(connection, "command");
1890 void mpd_sendUrlHandlersCommand(mpd_Connection *connection)
1892 mpd_executeCommand(connection, "urlhandlers\n");
1895 char *mpd_getNextHandler(mpd_Connection *connection)
1897 return mpd_getNextReturnElementNamed(connection, "handler");
1900 void mpd_sendTagTypesCommand(mpd_Connection *connection)
1902 mpd_executeCommand(connection, "tagtypes\n");
1905 char *mpd_getNextTagType(mpd_Connection *connection)
1907 return mpd_getNextReturnElementNamed(connection, "tagtype");
1910 void mpd_startSearch(mpd_Connection *connection, int exact)
1912 if (connection->request) {
1913 strcpy(connection->errorStr, "search already in progress");
1914 connection->error = 1;
1919 connection->request = strdup("find");
1921 connection->request = strdup("search");
1925 void mpd_startStatsSearch(mpd_Connection *connection)
1927 if (connection->request) {
1928 strcpy(connection->errorStr, "search already in progress");
1929 connection->error = 1;
1933 connection->request = strdup("count");
1936 void mpd_startPlaylistSearch(mpd_Connection *connection, int exact)
1938 if (connection->request) {
1939 strcpy(connection->errorStr, "search already in progress");
1940 connection->error = 1;
1945 connection->request = strdup("playlistfind");
1947 connection->request = strdup("playlistsearch");
1951 void mpd_startFieldSearch(mpd_Connection *connection, int type)
1953 const char *strtype;
1956 if (connection->request) {
1957 strcpy(connection->errorStr, "search already in progress");
1958 connection->error = 1;
1962 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
1963 strcpy(connection->errorStr, "invalid type specified");
1964 connection->error = 1;
1968 strtype = mpdTagItemKeys[type];
1970 len = 5 + strlen(strtype) + 1;
1971 connection->request = malloc(len);
1973 snprintf(connection->request, len, "list %c%s", tolower(strtype[0]),
1977 void mpd_addConstraintSearch(mpd_Connection *connection, int type,
1980 const char *strtype;
1985 if (!connection->request) {
1986 strcpy(connection->errorStr, "no search in progress");
1987 connection->error = 1;
1991 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
1992 strcpy(connection->errorStr, "invalid type specified");
1993 connection->error = 1;
1998 strcpy(connection->errorStr, "no name specified");
1999 connection->error = 1;
2003 string = strdup(connection->request);
2004 strtype = mpdTagItemKeys[type];
2005 arg = mpd_sanitizeArg(name);
2007 len = strlen(string) + 1 + strlen(strtype) + 2 + strlen(arg) + 2;
2008 connection->request = realloc(connection->request, len);
2009 snprintf(connection->request, len, "%s %c%s \"%s\"", string,
2010 tolower(strtype[0]), strtype + 1, arg);
2016 void mpd_commitSearch(mpd_Connection *connection)
2020 if (!connection->request) {
2021 strcpy(connection->errorStr, "no search in progress");
2022 connection->error = 1;
2026 len = strlen(connection->request) + 2;
2027 connection->request = realloc(connection->request, len);
2028 connection->request[len - 2] = '\n';
2029 connection->request[len - 1] = '\0';
2030 mpd_sendInfoCommand(connection, connection->request);
2032 free(connection->request);
2033 connection->request = NULL;
2037 * @param connection a MpdConnection
2038 * @param path the path to the playlist.
2040 * List the content, with full metadata, of a stored playlist. */
2041 void mpd_sendListPlaylistInfoCommand(mpd_Connection *connection, char *path)
2043 char *arg = mpd_sanitizeArg(path);
2044 int len = strlen("listplaylistinfo") + 2 + strlen(arg) + 3;
2045 char *query = malloc(len);
2047 snprintf(query, len, "listplaylistinfo \"%s\"\n", arg);
2048 mpd_sendInfoCommand(connection, query);
2054 * @param connection a MpdConnection
2055 * @param path the path to the playlist.
2057 * List the content of a stored playlist. */
2058 void mpd_sendListPlaylistCommand(mpd_Connection *connection, char *path)
2060 char *arg = mpd_sanitizeArg(path);
2061 int len = strlen("listplaylist") + 2 + strlen(arg) + 3;
2062 char *query = malloc(len);
2064 snprintf(query, len, "listplaylist \"%s\"\n", arg);
2065 mpd_sendInfoCommand(connection, query);
2070 void mpd_sendPlaylistClearCommand(mpd_Connection *connection, char *path)
2072 char *sPath = mpd_sanitizeArg(path);
2073 int len = strlen("playlistclear") + 2 + strlen(sPath) + 3;
2074 char *string = malloc(len);
2076 snprintf(string, len, "playlistclear \"%s\"\n", sPath);
2077 mpd_executeCommand(connection, string);
2082 void mpd_sendPlaylistAddCommand(mpd_Connection *connection, char *playlist,
2085 char *sPlaylist = mpd_sanitizeArg(playlist);
2086 char *sPath = mpd_sanitizeArg(path);
2087 int len = strlen("playlistadd") + 2 + strlen(sPlaylist) + 3 +
2089 char *string = malloc(len);
2091 snprintf(string, len, "playlistadd \"%s\" \"%s\"\n", sPlaylist, sPath);
2092 mpd_executeCommand(connection, string);
2098 void mpd_sendPlaylistMoveCommand(mpd_Connection *connection, char *playlist,
2101 char *sPlaylist = mpd_sanitizeArg(playlist);
2102 int len = strlen("playlistmove") + 2 + strlen(sPlaylist) + 3 + INTLEN +
2104 char *string = malloc(len);
2106 snprintf(string, len, "playlistmove \"%s\" \"%i\" \"%i\"\n", sPlaylist,
2108 mpd_executeCommand(connection, string);
2113 void mpd_sendPlaylistDeleteCommand(mpd_Connection *connection, char *playlist,
2116 char *sPlaylist = mpd_sanitizeArg(playlist);
2117 int len = strlen("playlistdelete") + 2 + strlen(sPlaylist) + 3 +
2119 char *string = malloc(len);
2121 snprintf(string, len, "playlistdelete \"%s\" \"%i\"\n", sPlaylist, pos);
2122 mpd_executeCommand(connection, string);