1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
4 * (c)2003-2006 by Warren Dukes (warren.dukes@gmail.com)
5 * This project's homepage is: http://www.musicpd.org
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * - Neither the name of the Music Player Daemon nor the names of its
19 * contributors may be used to endorse or promote products derived from
20 * this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
26 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * vim: ts=4 sw=4 noet ai cindent syntax=c
39 #include "libmpdclient.h"
43 #include <sys/types.h>
44 #include <sys/param.h>
50 # include <ws2tcpip.h>
53 # include <netinet/in.h>
54 # include <arpa/inet.h>
55 # include <sys/socket.h>
59 /* (bits + 1) / 3 (plus the sign character) */
60 #define INTLEN ((sizeof(int) * CHAR_BIT + 1) / 3 + 1)
61 #define LONGLONGLEN ((sizeof(long long) * CHAR_BIT + 1) / 3 + 1)
63 #define COMMAND_LIST 1
64 #define COMMAND_LIST_OK 2
73 # define MSG_DONTWAIT 0
77 # define SELECT_ERRNO_IGNORE (errno == WSAEINTR || errno == WSAEINPROGRESS)
78 # define SENDRECV_ERRNO_IGNORE SELECT_ERRNO_IGNORE
80 # define SELECT_ERRNO_IGNORE (errno == EINTR)
81 # define SENDRECV_ERRNO_IGNORE (errno == EINTR || errno == EAGAIN)
82 # define winsock_dll_error(c) 0
83 # define closesocket(s) close(s)
84 # define WSACleanup() do { /* nothing */ } while (0)
88 static int winsock_dll_error(mpd_Connection *connection)
92 if ((WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0
93 || LOBYTE(wsaData.wVersion) != 2
94 || HIBYTE(wsaData.wVersion) != 2) {
95 strcpy(connection->errorStr, "Could not find usable WinSock DLL.");
96 connection->error = MPD_ERROR_SYSTEM;
102 static int do_connect_fail(mpd_Connection *connection,
103 const struct sockaddr *serv_addr, int addrlen)
105 int iMode = 1; /* 0 = blocking, else non-blocking */
107 ioctlsocket(connection->sock, FIONBIO, (u_long FAR *) &iMode);
108 return (connect(connection->sock, serv_addr, addrlen) == SOCKET_ERROR
109 && WSAGetLastError() != WSAEWOULDBLOCK);
111 #else /* !WIN32 (sane operating systems) */
112 static int do_connect_fail(mpd_Connection *connection,
113 const struct sockaddr *serv_addr, int addrlen)
115 int flags = fcntl(connection->sock, F_GETFL, 0);
117 fcntl(connection->sock, F_SETFL, flags | O_NONBLOCK);
118 return (connect(connection->sock, serv_addr, addrlen) < 0
119 && errno != EINPROGRESS);
124 static int mpd_connect(mpd_Connection *connection, const char *host, int port,
128 char service[INTLEN + 1];
129 struct addrinfo hints;
130 struct addrinfo *res = NULL;
131 struct addrinfo *addrinfo = NULL;
135 * XXX: limit address family to PF_INET here.
136 * MPD does not support IPv6 yet, so if GAI returns
137 * an IPv6 address, the later connect() will fail. */
138 hints.ai_flags = AI_ADDRCONFIG;
139 hints.ai_family = PF_INET;
140 hints.ai_socktype = SOCK_STREAM;
141 hints.ai_protocol = IPPROTO_TCP;
142 hints.ai_addrlen = 0;
143 hints.ai_addr = NULL;
144 hints.ai_canonname = NULL;
145 hints.ai_next = NULL;
147 snprintf(service, sizeof(service), "%i", port);
149 error = getaddrinfo(host, service, &hints, &addrinfo);
152 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
153 "host \"%s\" not found: %s", host, gai_strerror(error));
154 connection->error = MPD_ERROR_UNKHOST;
158 for (res = addrinfo; res; res = res->ai_next) {
160 if (connection->sock > -1) {
161 closesocket(connection->sock);
163 connection->sock = socket(res->ai_family, SOCK_STREAM,
165 if (connection->sock < 0) {
166 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
167 "problems creating socket: %s", strerror(errno));
168 connection->error = MPD_ERROR_SYSTEM;
169 freeaddrinfo(addrinfo);
173 mpd_setConnectionTimeout(connection, mpd_timeout);
176 if (do_connect_fail(connection, res->ai_addr, res->ai_addrlen)) {
177 /* try the next address family */
178 closesocket(connection->sock);
179 connection->sock = -1;
184 freeaddrinfo(addrinfo);
186 if (connection->sock < 0) {
187 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
188 "problems connecting to \"%s\" on port %i: %s", host, port,
190 connection->error = MPD_ERROR_CONNPORT;
197 #else /* !MPD_HAVE_GAI */
198 static int mpd_connect(mpd_Connection *connection, const char *host, int port,
201 struct hostent he, *he_res = 0;
204 struct sockaddr *dest;
206 struct sockaddr_in sin;
208 #ifdef HAVE_GETHOSTBYNAME_R
209 if (gethostbyname_r(rhost, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
210 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
211 "%s ('%s')", hstrerror(h_errno), host);
212 connection->error = MPD_ERROR_UNKHOST;
215 #else /* HAVE_GETHOSTBYNAME_R */
216 if (!(he_res = gethostbyname(host))) {
217 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
218 "host \"%s\" not found", host);
219 connection->error = MPD_ERROR_UNKHOST;
222 #endif /* HAVE_GETHOSTBYNAME_R */
224 memset(&sin, 0, sizeof(struct sockaddr_in));
225 /* dest.sin_family = he_res->h_addrtype; */
226 sin.sin_family = AF_INET;
227 sin.sin_port = htons(port);
229 switch (he_res->h_addrtype) {
231 memcpy((char *) &sin.sin_addr.s_addr, (char *) he_res->h_addr,
233 dest = (struct sockaddr *) &sin;
234 destlen = sizeof(struct sockaddr_in);
237 strcpy(connection->errorStr, "address type is not IPv4");
238 connection->error = MPD_ERROR_SYSTEM;
243 if (connection->sock > -1) {
244 closesocket(connection->sock);
246 if ((connection->sock = socket(dest->sa_family, SOCK_STREAM, 0)) < 0) {
247 strcpy(connection->errorStr, "problems creating socket");
248 connection->error = MPD_ERROR_SYSTEM;
252 mpd_setConnectionTimeout(connection, mpd_timeout);
255 if (do_connect_fail(connection, dest, destlen)) {
256 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
257 "problems connecting to \"%s\" on port %i", host, port);
258 connection->error = MPD_ERROR_CONNPORT;
264 #endif /* !MPD_HAVE_GAI */
266 const char *mpdTagItemKeys[MPD_TAG_NUM_OF_ITEM_TYPES] = {
282 static char *mpd_sanitizeArg(const char *arg)
286 register const char *c;
289 /* instead of counting in that loop above,
290 * just use a bit more memory and halve running time */
291 ret = malloc(strlen(arg) * 2 + 1);
295 for (i = strlen(arg) + 1; i != 0; --i) {
296 if (*c == '"' || *c == '\\') {
305 static mpd_ReturnElement *mpd_newReturnElement(const char *name,
308 mpd_ReturnElement *ret = malloc(sizeof(mpd_ReturnElement));
310 ret->name = strndup(name, text_buffer_size);
311 ret->value = strndup(value, text_buffer_size);
316 static void mpd_freeReturnElement(mpd_ReturnElement *re)
323 void mpd_setConnectionTimeout(mpd_Connection *connection, float mpd_timeout)
325 connection->timeout.tv_sec = (int) timeout;
326 connection->timeout.tv_usec =
327 (int) ((mpd_timeout - connection->timeout.tv_sec) * 1e6 + 0.5);
330 static int mpd_parseWelcome(mpd_Connection *connection, const char *host,
331 int port, /* char *rt, */ char *output)
337 if (strncmp(output, MPD_WELCOME_MESSAGE, strlen(MPD_WELCOME_MESSAGE))) {
338 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
339 "mpd not running on port %i on host \"%s\"", port, host);
340 connection->error = MPD_ERROR_NOTMPD;
344 tmp = &output[strlen(MPD_WELCOME_MESSAGE)];
346 for (i = 0; i < 3; i++) {
348 connection->version[i] = strtol(tmp, &test, 10);
351 if (!tmp || (test[0] != '.' && test[0] != '\0')) {
352 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
353 "error parsing version number at \"%s\"",
354 &output[strlen(MPD_WELCOME_MESSAGE)]);
355 connection->error = MPD_ERROR_NOTMPD;
364 mpd_Connection *mpd_newConnection(const char *host, int port, float mpd_timeout)
369 mpd_Connection *connection = malloc(sizeof(mpd_Connection));
373 strcpy(connection->buffer, "");
374 connection->buflen = 0;
375 connection->bufstart = 0;
376 strcpy(connection->errorStr, "");
377 connection->error = 0;
378 connection->doneProcessing = 0;
379 connection->commandList = 0;
380 connection->listOks = 0;
381 connection->doneListOk = 0;
382 connection->sock = -1;
383 connection->returnElement = NULL;
384 connection->request = NULL;
386 if (winsock_dll_error(connection)) {
390 if (mpd_connect(connection, host, port, mpd_timeout) < 0) {
394 while (!(rt = strstr(connection->buffer, "\n"))) {
395 tv.tv_sec = connection->timeout.tv_sec;
396 tv.tv_usec = connection->timeout.tv_usec;
398 FD_SET(connection->sock, &fds);
399 if ((err = select(connection->sock + 1, &fds, NULL, NULL, &tv)) == 1) {
402 readed = recv(connection->sock,
403 &(connection->buffer[connection->buflen]),
404 MPD_BUFFER_MAX_LENGTH - connection->buflen, 0);
406 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
407 "problems getting a response from \"%s\" on port %i : %s",
408 host, port, strerror(errno));
409 connection->error = MPD_ERROR_NORESPONSE;
412 connection->buflen += readed;
413 connection->buffer[connection->buflen] = '\0';
414 } else if (err < 0) {
415 if (SELECT_ERRNO_IGNORE) {
418 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
419 "problems connecting to \"%s\" on port %i", host, port);
420 connection->error = MPD_ERROR_CONNPORT;
423 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
424 "timeout in attempting to get a response from \"%s\" on "
425 "port %i", host, port);
426 connection->error = MPD_ERROR_NORESPONSE;
432 output = strndup(connection->buffer, text_buffer_size);
433 strcpy(connection->buffer, rt + 1);
434 connection->buflen = strlen(connection->buffer);
436 if (mpd_parseWelcome(connection, host, port, /* rt, */ output) == 0) {
437 connection->doneProcessing = 1;
445 void mpd_clearError(mpd_Connection *connection)
447 connection->error = 0;
448 connection->errorStr[0] = '\0';
451 void mpd_closeConnection(mpd_Connection *connection)
453 closesocket(connection->sock);
454 if (connection->returnElement) {
455 free(connection->returnElement);
457 if (connection->request) {
458 free(connection->request);
464 static void mpd_executeCommand(mpd_Connection *connection, const char *command)
469 const char *commandPtr = command;
470 int commandLen = strlen(command);
472 if (!connection->doneProcessing && !connection->commandList) {
473 strcpy(connection->errorStr, "not done processing current command");
474 connection->error = 1;
478 mpd_clearError(connection);
481 FD_SET(connection->sock, &fds);
482 tv.tv_sec = connection->timeout.tv_sec;
483 tv.tv_usec = connection->timeout.tv_usec;
485 while ((ret = select(connection->sock + 1, NULL, &fds, NULL, &tv) == 1)
486 || (ret == -1 && SELECT_ERRNO_IGNORE)) {
487 ret = send(connection->sock, commandPtr, commandLen, MSG_DONTWAIT);
489 if (SENDRECV_ERRNO_IGNORE) {
492 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
493 "problems giving command \"%s\"", command);
494 connection->error = MPD_ERROR_SENDING;
501 if (commandLen <= 0) {
506 if (commandLen > 0) {
508 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
509 "timeout sending command \"%s\"", command);
510 connection->error = MPD_ERROR_TIMEOUT;
514 if (!connection->commandList) {
515 connection->doneProcessing = 0;
516 } else if (connection->commandList == COMMAND_LIST_OK) {
517 connection->listOks++;
521 static void mpd_getNextReturnElement(mpd_Connection *connection)
531 char *bufferCheck = NULL;
535 if (connection->returnElement) {
536 mpd_freeReturnElement(connection->returnElement);
538 connection->returnElement = NULL;
540 if (connection->doneProcessing
541 || (connection->listOks && connection->doneListOk)) {
542 strcpy(connection->errorStr,
543 "already done processing current command");
544 connection->error = 1;
548 bufferCheck = connection->buffer + connection->bufstart;
549 while (connection->bufstart >= connection->buflen
550 || !(rt = strchr(bufferCheck, '\n'))) {
551 if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
552 memmove(connection->buffer,
553 connection->buffer + connection->bufstart,
554 connection->buflen - connection->bufstart + 1);
555 connection->buflen -= connection->bufstart;
556 connection->bufstart = 0;
558 if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
559 strcpy(connection->errorStr, "buffer overrun");
560 connection->error = MPD_ERROR_BUFFEROVERRUN;
561 connection->doneProcessing = 1;
562 connection->doneListOk = 0;
565 bufferCheck = connection->buffer + connection->buflen;
566 tv.tv_sec = connection->timeout.tv_sec;
567 tv.tv_usec = connection->timeout.tv_usec;
569 FD_SET(connection->sock, &fds);
570 if ((err = select(connection->sock + 1, &fds, NULL, NULL, &tv) == 1)) {
571 readed = recv(connection->sock,
572 connection->buffer + connection->buflen,
573 MPD_BUFFER_MAX_LENGTH - connection->buflen, MSG_DONTWAIT);
574 if (readed < 0 && SENDRECV_ERRNO_IGNORE) {
578 strcpy(connection->errorStr, "connection closed");
579 connection->error = MPD_ERROR_CONNCLOSED;
580 connection->doneProcessing = 1;
581 connection->doneListOk = 0;
584 connection->buflen += readed;
585 connection->buffer[connection->buflen] = '\0';
586 } else if (err < 0 && SELECT_ERRNO_IGNORE) {
589 strcpy(connection->errorStr, "connection timeout");
590 connection->error = MPD_ERROR_TIMEOUT;
591 connection->doneProcessing = 1;
592 connection->doneListOk = 0;
598 output = connection->buffer + connection->bufstart;
599 connection->bufstart = rt - connection->buffer + 1;
601 if (strcmp(output, "OK") == 0) {
602 if (connection->listOks > 0) {
603 strcpy(connection->errorStr, "expected more list_OK's");
604 connection->error = 1;
606 connection->listOks = 0;
607 connection->doneProcessing = 1;
608 connection->doneListOk = 0;
612 if (strcmp(output, "list_OK") == 0) {
613 if (!connection->listOks) {
614 strcpy(connection->errorStr, "got an unexpected list_OK");
615 connection->error = 1;
617 connection->doneListOk = 1;
618 connection->listOks--;
623 if (strncmp(output, "ACK", strlen("ACK")) == 0) {
628 strcpy(connection->errorStr, output);
629 connection->error = MPD_ERROR_ACK;
630 connection->errorCode = MPD_ACK_ERROR_UNK;
631 connection->errorAt = MPD_ERROR_AT_UNK;
632 connection->doneProcessing = 1;
633 connection->doneListOk = 0;
635 needle = strchr(output, '[');
639 val = strtol(needle + 1, &test, 10);
643 connection->errorCode = val;
644 val = strtol(test + 1, &test, 10);
648 connection->errorAt = val;
652 tok = strchr(output, ':');
661 if (value[0] == ' ') {
662 connection->returnElement = mpd_newReturnElement(name, &(value[1]));
664 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
665 "error parsing: %s:%s", name, value);
666 connection->error = 1;
670 void mpd_finishCommand(mpd_Connection *connection)
672 while (!connection->doneProcessing) {
673 if (connection->doneListOk) {
674 connection->doneListOk = 0;
676 mpd_getNextReturnElement(connection);
680 static void mpd_finishListOkCommand(mpd_Connection *connection)
682 while (!connection->doneProcessing && connection->listOks
683 && !connection->doneListOk) {
684 mpd_getNextReturnElement(connection);
688 int mpd_nextListOkCommand(mpd_Connection *connection)
690 mpd_finishListOkCommand(connection);
691 if (!connection->doneProcessing) {
692 connection->doneListOk = 0;
694 if (connection->listOks == 0 || connection->doneProcessing) {
700 void mpd_sendStatusCommand(mpd_Connection *connection)
702 mpd_executeCommand(connection, "status\n");
705 mpd_Status *mpd_getStatus(mpd_Connection *connection)
709 /* mpd_executeCommand(connection, "status\n");
710 if (connection->error) {
714 if (connection->doneProcessing
715 || (connection->listOks && connection->doneListOk)) {
719 if (!connection->returnElement) {
720 mpd_getNextReturnElement(connection);
723 status = malloc(sizeof(mpd_Status));
727 status->playlist = -1;
728 status->playlistLength = -1;
732 status->elapsedTime = 0;
733 status->totalTime = 0;
735 status->sampleRate = 0;
737 status->channels = 0;
738 status->crossfade = -1;
739 status->error = NULL;
740 status->updatingDb = 0;
742 if (connection->error) {
746 while (connection->returnElement) {
747 mpd_ReturnElement *re = connection->returnElement;
749 if (strcmp(re->name, "volume") == 0) {
750 status->volume = atoi(re->value);
751 } else if (strcmp(re->name, "repeat") == 0) {
752 status->repeat = atoi(re->value);
753 } else if (strcmp(re->name, "random") == 0) {
754 status->random = atoi(re->value);
755 } else if (strcmp(re->name, "playlist") == 0) {
756 status->playlist = strtol(re->value, NULL, 10);
757 } else if (strcmp(re->name, "playlistlength") == 0) {
758 status->playlistLength = atoi(re->value);
759 } else if (strcmp(re->name, "bitrate") == 0) {
760 status->bitRate = atoi(re->value);
761 } else if (strcmp(re->name, "state") == 0) {
762 if (strcmp(re->value, "play") == 0) {
763 status->state = MPD_STATUS_STATE_PLAY;
764 } else if (strcmp(re->value, "stop") == 0) {
765 status->state = MPD_STATUS_STATE_STOP;
766 } else if (strcmp(re->value, "pause") == 0) {
767 status->state = MPD_STATUS_STATE_PAUSE;
769 status->state = MPD_STATUS_STATE_UNKNOWN;
771 } else if (strcmp(re->name, "song") == 0) {
772 status->song = atoi(re->value);
773 } else if (strcmp(re->name, "songid") == 0) {
774 status->songid = atoi(re->value);
775 } else if (strcmp(re->name, "time") == 0) {
776 char *tok = strchr(re->value, ':');
778 /* the second strchr below is a safety check */
779 if (tok && (strchr(tok, 0) > (tok + 1))) {
780 /* atoi stops at the first non-[0-9] char: */
781 status->elapsedTime = atoi(re->value);
782 status->totalTime = atoi(tok + 1);
784 } else if (strcmp(re->name, "error") == 0) {
785 status->error = strndup(re->value, text_buffer_size);
786 } else if (strcmp(re->name, "xfade") == 0) {
787 status->crossfade = atoi(re->value);
788 } else if (strcmp(re->name, "updating_db") == 0) {
789 status->updatingDb = atoi(re->value);
790 } else if (strcmp(re->name, "audio") == 0) {
791 char *tok = strchr(re->value, ':');
793 if (tok && (strchr(tok, 0) > (tok + 1))) {
794 status->sampleRate = atoi(re->value);
795 status->bits = atoi(++tok);
796 tok = strchr(tok, ':');
797 if (tok && (strchr(tok, 0) > (tok + 1))) {
798 status->channels = atoi(tok + 1);
803 mpd_getNextReturnElement(connection);
804 if (connection->error) {
810 if (connection->error) {
813 } else if (status->state < 0) {
814 strcpy(connection->errorStr, "state not found");
815 connection->error = 1;
823 void mpd_freeStatus(mpd_Status *status)
831 void mpd_sendStatsCommand(mpd_Connection *connection)
833 mpd_executeCommand(connection, "stats\n");
836 mpd_Stats *mpd_getStats(mpd_Connection *connection)
840 /* mpd_executeCommand(connection, "stats\n");
841 if (connection->error) {
845 if (connection->doneProcessing
846 || (connection->listOks && connection->doneListOk)) {
850 if (!connection->returnElement) {
851 mpd_getNextReturnElement(connection);
854 stats = malloc(sizeof(mpd_Stats));
855 stats->numberOfArtists = 0;
856 stats->numberOfAlbums = 0;
857 stats->numberOfSongs = 0;
859 stats->dbUpdateTime = 0;
861 stats->dbPlayTime = 0;
863 if (connection->error) {
867 while (connection->returnElement) {
868 mpd_ReturnElement *re = connection->returnElement;
870 if (strcmp(re->name, "artists") == 0) {
871 stats->numberOfArtists = atoi(re->value);
872 } else if (strcmp(re->name, "albums") == 0) {
873 stats->numberOfAlbums = atoi(re->value);
874 } else if (strcmp(re->name, "songs") == 0) {
875 stats->numberOfSongs = atoi(re->value);
876 } else if (strcmp(re->name, "uptime") == 0) {
877 stats->uptime = strtol(re->value, NULL, 10);
878 } else if (strcmp(re->name, "db_update") == 0) {
879 stats->dbUpdateTime = strtol(re->value, NULL, 10);
880 } else if (strcmp(re->name, "playtime") == 0) {
881 stats->playTime = strtol(re->value, NULL, 10);
882 } else if (strcmp(re->name, "db_playtime") == 0) {
883 stats->dbPlayTime = strtol(re->value, NULL, 10);
886 mpd_getNextReturnElement(connection);
887 if (connection->error) {
893 if (connection->error) {
901 void mpd_freeStats(mpd_Stats *stats)
906 mpd_SearchStats *mpd_getSearchStats(mpd_Connection *connection)
908 mpd_SearchStats *stats;
909 mpd_ReturnElement *re;
911 if (connection->doneProcessing
912 || (connection->listOks && connection->doneListOk)) {
916 if (!connection->returnElement) {
917 mpd_getNextReturnElement(connection);
920 if (connection->error) {
924 stats = malloc(sizeof(mpd_SearchStats));
925 stats->numberOfSongs = 0;
928 while (connection->returnElement) {
929 re = connection->returnElement;
931 if (strcmp(re->name, "songs") == 0) {
932 stats->numberOfSongs = atoi(re->value);
933 } else if (strcmp(re->name, "playtime") == 0) {
934 stats->playTime = strtol(re->value, NULL, 10);
937 mpd_getNextReturnElement(connection);
938 if (connection->error) {
944 if (connection->error) {
952 void mpd_freeSearchStats(mpd_SearchStats *stats)
957 static void mpd_initSong(mpd_Song *song)
968 song->composer = NULL;
969 song->performer = NULL;
971 song->comment = NULL;
973 song->time = MPD_SONG_NO_TIME;
974 song->pos = MPD_SONG_NO_NUM;
975 song->id = MPD_SONG_NO_ID;
978 static void mpd_finishSong(mpd_Song *song)
1004 if (song->composer) {
1005 free(song->composer);
1010 if (song->comment) {
1011 free(song->comment);
1015 mpd_Song *mpd_newSong(void)
1017 mpd_Song *ret = malloc(sizeof(mpd_Song));
1024 void mpd_freeSong(mpd_Song *song)
1026 mpd_finishSong(song);
1030 mpd_Song *mpd_songDup(mpd_Song *song)
1032 mpd_Song *ret = mpd_newSong();
1035 ret->file = strndup(song->file, text_buffer_size);
1038 ret->artist = strndup(song->artist, text_buffer_size);
1041 ret->album = strndup(song->album, text_buffer_size);
1044 ret->title = strndup(song->title, text_buffer_size);
1047 ret->track = strndup(song->track, text_buffer_size);
1050 ret->name = strndup(song->name, text_buffer_size);
1053 ret->date = strndup(song->date, text_buffer_size);
1056 ret->genre = strndup(song->genre, text_buffer_size);
1058 if (song->composer) {
1059 ret->composer = strndup(song->composer, text_buffer_size);
1062 ret->disc = strndup(song->disc, text_buffer_size);
1064 if (song->comment) {
1065 ret->comment = strndup(song->comment, text_buffer_size);
1067 ret->time = song->time;
1068 ret->pos = song->pos;
1074 static void mpd_initDirectory(mpd_Directory *directory)
1076 directory->path = NULL;
1079 static void mpd_finishDirectory(mpd_Directory *directory)
1081 if (directory->path) {
1082 free(directory->path);
1086 mpd_Directory *mpd_newDirectory(void)
1088 mpd_Directory *directory = malloc(sizeof(mpd_Directory));
1090 mpd_initDirectory(directory);
1095 void mpd_freeDirectory(mpd_Directory *directory)
1097 mpd_finishDirectory(directory);
1102 mpd_Directory *mpd_directoryDup(mpd_Directory *directory)
1104 mpd_Directory *ret = mpd_newDirectory();
1106 if (directory->path) {
1107 ret->path = strndup(directory->path, text_buffer_size);
1113 static void mpd_initPlaylistFile(mpd_PlaylistFile *playlist)
1115 playlist->path = NULL;
1118 static void mpd_finishPlaylistFile(mpd_PlaylistFile *playlist)
1120 if (playlist->path) {
1121 free(playlist->path);
1125 mpd_PlaylistFile *mpd_newPlaylistFile(void)
1127 mpd_PlaylistFile *playlist = malloc(sizeof(mpd_PlaylistFile));
1129 mpd_initPlaylistFile(playlist);
1134 void mpd_freePlaylistFile(mpd_PlaylistFile *playlist)
1136 mpd_finishPlaylistFile(playlist);
1140 mpd_PlaylistFile *mpd_playlistFileDup(mpd_PlaylistFile *playlist)
1142 mpd_PlaylistFile *ret = mpd_newPlaylistFile();
1144 if (playlist->path) {
1145 ret->path = strndup(playlist->path, text_buffer_size);
1151 static void mpd_initInfoEntity(mpd_InfoEntity *entity)
1153 entity->info.directory = NULL;
1156 static void mpd_finishInfoEntity(mpd_InfoEntity *entity)
1158 if (entity->info.directory) {
1159 if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1160 mpd_freeDirectory(entity->info.directory);
1161 } else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
1162 mpd_freeSong(entity->info.song);
1163 } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1164 mpd_freePlaylistFile(entity->info.playlistFile);
1169 mpd_InfoEntity *mpd_newInfoEntity(void)
1171 mpd_InfoEntity *entity = malloc(sizeof(mpd_InfoEntity));
1173 mpd_initInfoEntity(entity);
1178 void mpd_freeInfoEntity(mpd_InfoEntity *entity)
1180 mpd_finishInfoEntity(entity);
1184 static void mpd_sendInfoCommand(mpd_Connection *connection, char *command)
1186 mpd_executeCommand(connection, command);
1189 mpd_InfoEntity *mpd_getNextInfoEntity(mpd_Connection *connection)
1191 mpd_InfoEntity *entity = NULL;
1193 if (connection->doneProcessing
1194 || (connection->listOks && connection->doneListOk)) {
1198 if (!connection->returnElement) {
1199 mpd_getNextReturnElement(connection);
1202 if (connection->returnElement) {
1203 if (strcmp(connection->returnElement->name, "file") == 0) {
1204 entity = mpd_newInfoEntity();
1205 entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1206 entity->info.song = mpd_newSong();
1207 entity->info.song->file = strndup(connection->returnElement->value, text_buffer_size);
1208 } else if (strcmp(connection->returnElement->name, "directory") == 0) {
1209 entity = mpd_newInfoEntity();
1210 entity->type = MPD_INFO_ENTITY_TYPE_DIRECTORY;
1211 entity->info.directory = mpd_newDirectory();
1212 entity->info.directory->path =
1213 strndup(connection->returnElement->value, text_buffer_size);
1214 } else if (strcmp(connection->returnElement->name, "playlist") == 0) {
1215 entity = mpd_newInfoEntity();
1216 entity->type = MPD_INFO_ENTITY_TYPE_PLAYLISTFILE;
1217 entity->info.playlistFile = mpd_newPlaylistFile();
1218 entity->info.playlistFile->path =
1219 strndup(connection->returnElement->value, text_buffer_size);
1220 } else if (strcmp(connection->returnElement->name, "cpos") == 0) {
1221 entity = mpd_newInfoEntity();
1222 entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1223 entity->info.song = mpd_newSong();
1224 entity->info.song->pos = atoi(connection->returnElement->value);
1226 connection->error = 1;
1227 strcpy(connection->errorStr, "problem parsing song info");
1234 mpd_getNextReturnElement(connection);
1235 while (connection->returnElement) {
1236 mpd_ReturnElement *re = connection->returnElement;
1238 if (strcmp(re->name, "file") == 0) {
1240 } else if (strcmp(re->name, "directory") == 0) {
1242 } else if (strcmp(re->name, "playlist") == 0) {
1244 } else if (strcmp(re->name, "cpos") == 0) {
1248 if (entity->type == MPD_INFO_ENTITY_TYPE_SONG && strlen(re->value)) {
1249 if (!entity->info.song->artist
1250 && strcmp(re->name, "Artist") == 0) {
1251 entity->info.song->artist = strndup(re->value, text_buffer_size);
1252 } else if (!entity->info.song->album
1253 && strcmp(re->name, "Album") == 0) {
1254 entity->info.song->album = strndup(re->value, text_buffer_size);
1255 } else if (!entity->info.song->title
1256 && strcmp(re->name, "Title") == 0) {
1257 entity->info.song->title = strndup(re->value, text_buffer_size);
1258 } else if (!entity->info.song->track
1259 && strcmp(re->name, "Track") == 0) {
1260 entity->info.song->track = strndup(re->value, text_buffer_size);
1261 } else if (!entity->info.song->name
1262 && strcmp(re->name, "Name") == 0) {
1263 entity->info.song->name = strndup(re->value, text_buffer_size);
1264 } else if (entity->info.song->time == MPD_SONG_NO_TIME
1265 && strcmp(re->name, "Time") == 0) {
1266 entity->info.song->time = atoi(re->value);
1267 } else if (entity->info.song->pos == MPD_SONG_NO_NUM
1268 && strcmp(re->name, "Pos") == 0) {
1269 entity->info.song->pos = atoi(re->value);
1270 } else if (entity->info.song->id == MPD_SONG_NO_ID
1271 && strcmp(re->name, "Id") == 0) {
1272 entity->info.song->id = atoi(re->value);
1273 } else if (!entity->info.song->date
1274 && strcmp(re->name, "Date") == 0) {
1275 entity->info.song->date = strndup(re->value, text_buffer_size);
1276 } else if (!entity->info.song->genre
1277 && strcmp(re->name, "Genre") == 0) {
1278 entity->info.song->genre = strndup(re->value, text_buffer_size);
1279 } else if (!entity->info.song->composer
1280 && strcmp(re->name, "Composer") == 0) {
1281 entity->info.song->composer = strndup(re->value, text_buffer_size);
1282 } else if (!entity->info.song->performer
1283 && strcmp(re->name, "Performer") == 0) {
1284 entity->info.song->performer = strndup(re->value, text_buffer_size);
1285 } else if (!entity->info.song->disc
1286 && strcmp(re->name, "Disc") == 0) {
1287 entity->info.song->disc = strndup(re->value, text_buffer_size);
1288 } else if (!entity->info.song->comment
1289 && strcmp(re->name, "Comment") == 0) {
1290 entity->info.song->comment = strndup(re->value, text_buffer_size);
1292 } else if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1293 } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1296 mpd_getNextReturnElement(connection);
1302 static char *mpd_getNextReturnElementNamed(mpd_Connection *connection,
1305 if (connection->doneProcessing
1306 || (connection->listOks && connection->doneListOk)) {
1310 mpd_getNextReturnElement(connection);
1311 while (connection->returnElement) {
1312 mpd_ReturnElement *re = connection->returnElement;
1314 if (strcmp(re->name, name) == 0) {
1315 return strndup(re->value, text_buffer_size);
1317 mpd_getNextReturnElement(connection);
1323 char *mpd_getNextTag(mpd_Connection *connection, int type)
1325 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES
1326 || type == MPD_TAG_ITEM_ANY) {
1329 if (type == MPD_TAG_ITEM_FILENAME) {
1330 return mpd_getNextReturnElementNamed(connection, "file");
1332 return mpd_getNextReturnElementNamed(connection, mpdTagItemKeys[type]);
1335 char *mpd_getNextArtist(mpd_Connection *connection)
1337 return mpd_getNextReturnElementNamed(connection, "Artist");
1340 char *mpd_getNextAlbum(mpd_Connection *connection)
1342 return mpd_getNextReturnElementNamed(connection, "Album");
1345 void mpd_sendPlaylistInfoCommand(mpd_Connection *connection, int songPos)
1347 int len = strlen("playlistinfo") + 2 + INTLEN + 3;
1348 char *string = malloc(len);
1350 snprintf(string, len, "playlistinfo \"%i\"\n", songPos);
1351 mpd_sendInfoCommand(connection, string);
1355 void mpd_sendPlaylistIdCommand(mpd_Connection *connection, int id)
1357 int len = strlen("playlistid") + 2 + INTLEN + 3;
1358 char *string = malloc(len);
1360 snprintf(string, len, "playlistid \"%i\"\n", id);
1361 mpd_sendInfoCommand(connection, string);
1365 void mpd_sendPlChangesCommand(mpd_Connection *connection, long long playlist)
1367 int len = strlen("plchanges") + 2 + LONGLONGLEN + 3;
1368 char *string = malloc(len);
1370 snprintf(string, len, "plchanges \"%lld\"\n", playlist);
1371 mpd_sendInfoCommand(connection, string);
1375 void mpd_sendPlChangesPosIdCommand(mpd_Connection *connection,
1378 int len = strlen("plchangesposid") + 2 + LONGLONGLEN + 3;
1379 char *string = malloc(len);
1381 snprintf(string, len, "plchangesposid \"%lld\"\n", playlist);
1382 mpd_sendInfoCommand(connection, string);
1386 void mpd_sendListallCommand(mpd_Connection *connection, const char *dir)
1388 char *sDir = mpd_sanitizeArg(dir);
1389 int len = strlen("listall") + 2 + strlen(sDir) + 3;
1390 char *string = malloc(len);
1392 snprintf(string, len, "listall \"%s\"\n", sDir);
1393 mpd_sendInfoCommand(connection, string);
1398 void mpd_sendListallInfoCommand(mpd_Connection *connection, const char *dir)
1400 char *sDir = mpd_sanitizeArg(dir);
1401 int len = strlen("listallinfo") + 2 + strlen(sDir) + 3;
1402 char *string = malloc(len);
1404 snprintf(string, len, "listallinfo \"%s\"\n", sDir);
1405 mpd_sendInfoCommand(connection, string);
1410 void mpd_sendLsInfoCommand(mpd_Connection *connection, const char *dir)
1412 char *sDir = mpd_sanitizeArg(dir);
1413 int len = strlen("lsinfo") + 2 + strlen(sDir) + 3;
1414 char *string = malloc(len);
1416 snprintf(string, len, "lsinfo \"%s\"\n", sDir);
1417 mpd_sendInfoCommand(connection, string);
1422 void mpd_sendCurrentSongCommand(mpd_Connection *connection)
1424 mpd_executeCommand(connection, "currentsong\n");
1427 void mpd_sendSearchCommand(mpd_Connection *connection, int table,
1430 mpd_startSearch(connection, 0);
1431 mpd_addConstraintSearch(connection, table, str);
1432 mpd_commitSearch(connection);
1435 void mpd_sendFindCommand(mpd_Connection *connection, int table,
1438 mpd_startSearch(connection, 1);
1439 mpd_addConstraintSearch(connection, table, str);
1440 mpd_commitSearch(connection);
1443 void mpd_sendListCommand(mpd_Connection *connection, int table,
1450 if (table == MPD_TABLE_ARTIST) {
1451 strcpy(st, "artist");
1452 } else if (table == MPD_TABLE_ALBUM) {
1453 strcpy(st, "album");
1455 connection->error = 1;
1456 strcpy(connection->errorStr, "unknown table for list");
1460 char *sanitArg1 = mpd_sanitizeArg(arg1);
1462 len = strlen("list") + 1 + strlen(sanitArg1) + 2 + strlen(st) + 3;
1463 string = malloc(len);
1464 snprintf(string, len, "list %s \"%s\"\n", st, sanitArg1);
1467 len = strlen("list") + 1 + strlen(st) + 2;
1468 string = malloc(len);
1469 snprintf(string, len, "list %s\n", st);
1471 mpd_sendInfoCommand(connection, string);
1475 void mpd_sendAddCommand(mpd_Connection *connection, const char *file)
1477 char *sFile = mpd_sanitizeArg(file);
1478 int len = strlen("add") + 2 + strlen(sFile) + 3;
1479 char *string = malloc(len);
1481 snprintf(string, len, "add \"%s\"\n", sFile);
1482 mpd_executeCommand(connection, string);
1487 int mpd_sendAddIdCommand(mpd_Connection *connection, const char *file)
1490 char *sFile = mpd_sanitizeArg(file);
1491 int len = strlen("addid") + 2 + strlen(sFile) + 3;
1492 char *string = malloc(len);
1494 snprintf(string, len, "addid \"%s\"\n", sFile);
1495 mpd_sendInfoCommand(connection, string);
1499 string = mpd_getNextReturnElementNamed(connection, "Id");
1501 retval = atoi(string);
1508 void mpd_sendDeleteCommand(mpd_Connection *connection, int songPos)
1510 int len = strlen("delete") + 2 + INTLEN + 3;
1511 char *string = malloc(len);
1513 snprintf(string, len, "delete \"%i\"\n", songPos);
1514 mpd_sendInfoCommand(connection, string);
1518 void mpd_sendDeleteIdCommand(mpd_Connection *connection, int id)
1520 int len = strlen("deleteid") + 2 + INTLEN + 3;
1521 char *string = malloc(len);
1523 snprintf(string, len, "deleteid \"%i\"\n", id);
1524 mpd_sendInfoCommand(connection, string);
1528 void mpd_sendSaveCommand(mpd_Connection *connection, const char *name)
1530 char *sName = mpd_sanitizeArg(name);
1531 int len = strlen("save") + 2 + strlen(sName) + 3;
1532 char *string = malloc(len);
1534 snprintf(string, len, "save \"%s\"\n", sName);
1535 mpd_executeCommand(connection, string);
1540 void mpd_sendLoadCommand(mpd_Connection *connection, const char *name)
1542 char *sName = mpd_sanitizeArg(name);
1543 int len = strlen("load") + 2 + strlen(sName) + 3;
1544 char *string = malloc(len);
1546 snprintf(string, len, "load \"%s\"\n", sName);
1547 mpd_executeCommand(connection, string);
1552 void mpd_sendRmCommand(mpd_Connection *connection, const char *name)
1554 char *sName = mpd_sanitizeArg(name);
1555 int len = strlen("rm") + 2 + strlen(sName) + 3;
1556 char *string = malloc(len);
1558 snprintf(string, len, "rm \"%s\"\n", sName);
1559 mpd_executeCommand(connection, string);
1564 void mpd_sendRenameCommand(mpd_Connection *connection, const char *from,
1567 char *sFrom = mpd_sanitizeArg(from);
1568 char *sTo = mpd_sanitizeArg(to);
1569 int len = strlen("rename") + 2 + strlen(sFrom) + 3 + strlen(sTo) + 3;
1570 char *string = malloc(len);
1572 snprintf(string, len, "rename \"%s\" \"%s\"\n", sFrom, sTo);
1573 mpd_executeCommand(connection, string);
1579 void mpd_sendShuffleCommand(mpd_Connection *connection)
1581 mpd_executeCommand(connection, "shuffle\n");
1584 void mpd_sendClearCommand(mpd_Connection *connection)
1586 mpd_executeCommand(connection, "clear\n");
1589 void mpd_sendPlayCommand(mpd_Connection *connection, int songPos)
1591 int len = strlen("play") + 2 + INTLEN + 3;
1592 char *string = malloc(len);
1594 snprintf(string, len, "play \"%i\"\n", songPos);
1595 mpd_sendInfoCommand(connection, string);
1599 void mpd_sendPlayIdCommand(mpd_Connection *connection, int id)
1601 int len = strlen("playid") + 2 + INTLEN + 3;
1602 char *string = malloc(len);
1604 snprintf(string, len, "playid \"%i\"\n", id);
1605 mpd_sendInfoCommand(connection, string);
1609 void mpd_sendStopCommand(mpd_Connection *connection)
1611 mpd_executeCommand(connection, "stop\n");
1614 void mpd_sendPauseCommand(mpd_Connection *connection, int pauseMode)
1616 int len = strlen("pause") + 2 + INTLEN + 3;
1617 char *string = malloc(len);
1619 snprintf(string, len, "pause \"%i\"\n", pauseMode);
1620 mpd_executeCommand(connection, string);
1624 void mpd_sendNextCommand(mpd_Connection *connection)
1626 mpd_executeCommand(connection, "next\n");
1629 void mpd_sendMoveCommand(mpd_Connection *connection, int from, int to)
1631 int len = strlen("move") + 2 + INTLEN + 3 + INTLEN + 3;
1632 char *string = malloc(len);
1634 snprintf(string, len, "move \"%i\" \"%i\"\n", from, to);
1635 mpd_sendInfoCommand(connection, string);
1639 void mpd_sendMoveIdCommand(mpd_Connection *connection, int id, int to)
1641 int len = strlen("moveid") + 2 + INTLEN + 3 + INTLEN + 3;
1642 char *string = malloc(len);
1644 snprintf(string, len, "moveid \"%i\" \"%i\"\n", id, to);
1645 mpd_sendInfoCommand(connection, string);
1649 void mpd_sendSwapCommand(mpd_Connection *connection, int song1, int song2)
1651 int len = strlen("swap") + 2 + INTLEN + 3 + INTLEN + 3;
1652 char *string = malloc(len);
1654 snprintf(string, len, "swap \"%i\" \"%i\"\n", song1, song2);
1655 mpd_sendInfoCommand(connection, string);
1659 void mpd_sendSwapIdCommand(mpd_Connection *connection, int id1, int id2)
1661 int len = strlen("swapid") + 2 + INTLEN + 3 + INTLEN + 3;
1662 char *string = malloc(len);
1664 snprintf(string, len, "swapid \"%i\" \"%i\"\n", id1, id2);
1665 mpd_sendInfoCommand(connection, string);
1669 void mpd_sendSeekCommand(mpd_Connection *connection, int song, int seek_time)
1671 int len = strlen("seek") + 2 + INTLEN + 3 + INTLEN + 3;
1672 char *string = malloc(len);
1674 snprintf(string, len, "seek \"%i\" \"%i\"\n", song, seek_time);
1675 mpd_sendInfoCommand(connection, string);
1679 void mpd_sendSeekIdCommand(mpd_Connection *connection, int id, int seek_time)
1681 int len = strlen("seekid") + 2 + INTLEN + 3 + INTLEN + 3;
1682 char *string = malloc(len);
1684 snprintf(string, len, "seekid \"%i\" \"%i\"\n", id, seek_time);
1685 mpd_sendInfoCommand(connection, string);
1689 void mpd_sendUpdateCommand(mpd_Connection *connection, char *path)
1691 char *sPath = mpd_sanitizeArg(path);
1692 int len = strlen("update") + 2 + strlen(sPath) + 3;
1693 char *string = malloc(len);
1695 snprintf(string, len, "update \"%s\"\n", sPath);
1696 mpd_sendInfoCommand(connection, string);
1701 int mpd_getUpdateId(mpd_Connection *connection)
1706 jobid = mpd_getNextReturnElementNamed(connection, "updating_db");
1715 void mpd_sendPrevCommand(mpd_Connection *connection)
1717 mpd_executeCommand(connection, "previous\n");
1720 void mpd_sendRepeatCommand(mpd_Connection *connection, int repeatMode)
1722 int len = strlen("repeat") + 2 + INTLEN + 3;
1723 char *string = malloc(len);
1725 snprintf(string, len, "repeat \"%i\"\n", repeatMode);
1726 mpd_executeCommand(connection, string);
1730 void mpd_sendRandomCommand(mpd_Connection *connection, int randomMode)
1732 int len = strlen("random") + 2 + INTLEN + 3;
1733 char *string = malloc(len);
1735 snprintf(string, len, "random \"%i\"\n", randomMode);
1736 mpd_executeCommand(connection, string);
1740 void mpd_sendSetvolCommand(mpd_Connection *connection, int volumeChange)
1742 int len = strlen("setvol") + 2 + INTLEN + 3;
1743 char *string = malloc(len);
1745 snprintf(string, len, "setvol \"%i\"\n", volumeChange);
1746 mpd_executeCommand(connection, string);
1750 void mpd_sendVolumeCommand(mpd_Connection *connection, int volumeChange)
1752 int len = strlen("volume") + 2 + INTLEN + 3;
1753 char *string = malloc(len);
1755 snprintf(string, len, "volume \"%i\"\n", volumeChange);
1756 mpd_executeCommand(connection, string);
1760 void mpd_sendCrossfadeCommand(mpd_Connection *connection, int seconds)
1762 int len = strlen("crossfade") + 2 + INTLEN + 3;
1763 char *string = malloc(len);
1765 snprintf(string, len, "crossfade \"%i\"\n", seconds);
1766 mpd_executeCommand(connection, string);
1770 void mpd_sendPasswordCommand(mpd_Connection *connection, const char *pass)
1772 char *sPass = mpd_sanitizeArg(pass);
1773 int len = strlen("password") + 2 + strlen(sPass) + 3;
1774 char *string = malloc(len);
1776 snprintf(string, len, "password \"%s\"\n", sPass);
1777 mpd_executeCommand(connection, string);
1782 void mpd_sendCommandListBegin(mpd_Connection *connection)
1784 if (connection->commandList) {
1785 strcpy(connection->errorStr, "already in command list mode");
1786 connection->error = 1;
1789 connection->commandList = COMMAND_LIST;
1790 mpd_executeCommand(connection, "command_list_begin\n");
1793 void mpd_sendCommandListOkBegin(mpd_Connection *connection)
1795 if (connection->commandList) {
1796 strcpy(connection->errorStr, "already in command list mode");
1797 connection->error = 1;
1800 connection->commandList = COMMAND_LIST_OK;
1801 mpd_executeCommand(connection, "command_list_ok_begin\n");
1802 connection->listOks = 0;
1805 void mpd_sendCommandListEnd(mpd_Connection *connection)
1807 if (!connection->commandList) {
1808 strcpy(connection->errorStr, "not in command list mode");
1809 connection->error = 1;
1812 connection->commandList = 0;
1813 mpd_executeCommand(connection, "command_list_end\n");
1816 void mpd_sendOutputsCommand(mpd_Connection *connection)
1818 mpd_executeCommand(connection, "outputs\n");
1821 mpd_OutputEntity *mpd_getNextOutput(mpd_Connection *connection)
1823 mpd_OutputEntity *output = NULL;
1825 if (connection->doneProcessing
1826 || (connection->listOks && connection->doneListOk)) {
1830 if (connection->error) {
1834 output = malloc(sizeof(mpd_OutputEntity));
1836 output->name = NULL;
1837 output->enabled = 0;
1839 if (!connection->returnElement) {
1840 mpd_getNextReturnElement(connection);
1843 while (connection->returnElement) {
1844 mpd_ReturnElement *re = connection->returnElement;
1846 if (strcmp(re->name, "outputid") == 0) {
1847 if (output != NULL && output->id >= 0) {
1850 output->id = atoi(re->value);
1851 } else if (strcmp(re->name, "outputname") == 0) {
1852 output->name = strndup(re->value, text_buffer_size);
1853 } else if (strcmp(re->name, "outputenabled") == 0) {
1854 output->enabled = atoi(re->value);
1857 mpd_getNextReturnElement(connection);
1858 if (connection->error) {
1867 void mpd_sendEnableOutputCommand(mpd_Connection *connection, int outputId)
1869 int len = strlen("enableoutput") + 2 + INTLEN + 3;
1870 char *string = malloc(len);
1872 snprintf(string, len, "enableoutput \"%i\"\n", outputId);
1873 mpd_executeCommand(connection, string);
1877 void mpd_sendDisableOutputCommand(mpd_Connection *connection, int outputId)
1879 int len = strlen("disableoutput") + 2 + INTLEN + 3;
1880 char *string = malloc(len);
1882 snprintf(string, len, "disableoutput \"%i\"\n", outputId);
1883 mpd_executeCommand(connection, string);
1887 void mpd_freeOutputElement(mpd_OutputEntity *output)
1893 /** odd naming, but it gets the not allowed commands */
1894 void mpd_sendNotCommandsCommand(mpd_Connection *connection)
1896 mpd_executeCommand(connection, "notcommands\n");
1899 /** odd naming, but it gets the allowed commands */
1900 void mpd_sendCommandsCommand(mpd_Connection *connection)
1902 mpd_executeCommand(connection, "commands\n");
1905 /** Get the next returned command */
1906 char *mpd_getNextCommand(mpd_Connection *connection)
1908 return mpd_getNextReturnElementNamed(connection, "command");
1911 void mpd_sendUrlHandlersCommand(mpd_Connection *connection)
1913 mpd_executeCommand(connection, "urlhandlers\n");
1916 char *mpd_getNextHandler(mpd_Connection *connection)
1918 return mpd_getNextReturnElementNamed(connection, "handler");
1921 void mpd_sendTagTypesCommand(mpd_Connection *connection)
1923 mpd_executeCommand(connection, "tagtypes\n");
1926 char *mpd_getNextTagType(mpd_Connection *connection)
1928 return mpd_getNextReturnElementNamed(connection, "tagtype");
1931 void mpd_startSearch(mpd_Connection *connection, int exact)
1933 if (connection->request) {
1934 strcpy(connection->errorStr, "search already in progress");
1935 connection->error = 1;
1940 connection->request = strndup("find", text_buffer_size);
1942 connection->request = strndup("search", text_buffer_size);
1946 void mpd_startStatsSearch(mpd_Connection *connection)
1948 if (connection->request) {
1949 strcpy(connection->errorStr, "search already in progress");
1950 connection->error = 1;
1954 connection->request = strndup("count", text_buffer_size);
1957 void mpd_startPlaylistSearch(mpd_Connection *connection, int exact)
1959 if (connection->request) {
1960 strcpy(connection->errorStr, "search already in progress");
1961 connection->error = 1;
1966 connection->request = strndup("playlistfind", text_buffer_size);
1968 connection->request = strndup("playlistsearch", text_buffer_size);
1972 void mpd_startFieldSearch(mpd_Connection *connection, int type)
1974 const char *strtype;
1977 if (connection->request) {
1978 strcpy(connection->errorStr, "search already in progress");
1979 connection->error = 1;
1983 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
1984 strcpy(connection->errorStr, "invalid type specified");
1985 connection->error = 1;
1989 strtype = mpdTagItemKeys[type];
1991 len = 5 + strlen(strtype) + 1;
1992 connection->request = malloc(len);
1994 snprintf(connection->request, len, "list %c%s", tolower(strtype[0]),
1998 void mpd_addConstraintSearch(mpd_Connection *connection, int type,
2001 const char *strtype;
2006 if (!connection->request) {
2007 strcpy(connection->errorStr, "no search in progress");
2008 connection->error = 1;
2012 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
2013 strcpy(connection->errorStr, "invalid type specified");
2014 connection->error = 1;
2019 strcpy(connection->errorStr, "no name specified");
2020 connection->error = 1;
2024 string = strndup(connection->request, text_buffer_size);
2025 strtype = mpdTagItemKeys[type];
2026 arg = mpd_sanitizeArg(name);
2028 len = strlen(string) + 1 + strlen(strtype) + 2 + strlen(arg) + 2;
2029 connection->request = realloc(connection->request, len);
2030 snprintf(connection->request, len, "%s %c%s \"%s\"", string,
2031 tolower(strtype[0]), strtype + 1, arg);
2037 void mpd_commitSearch(mpd_Connection *connection)
2041 if (!connection->request) {
2042 strcpy(connection->errorStr, "no search in progress");
2043 connection->error = 1;
2047 len = strlen(connection->request) + 2;
2048 connection->request = realloc(connection->request, len);
2049 connection->request[len - 2] = '\n';
2050 connection->request[len - 1] = '\0';
2051 mpd_sendInfoCommand(connection, connection->request);
2053 free(connection->request);
2054 connection->request = NULL;
2058 * @param connection a MpdConnection
2059 * @param path the path to the playlist.
2061 * List the content, with full metadata, of a stored playlist. */
2062 void mpd_sendListPlaylistInfoCommand(mpd_Connection *connection, char *path)
2064 char *arg = mpd_sanitizeArg(path);
2065 int len = strlen("listplaylistinfo") + 2 + strlen(arg) + 3;
2066 char *query = malloc(len);
2068 snprintf(query, len, "listplaylistinfo \"%s\"\n", arg);
2069 mpd_sendInfoCommand(connection, query);
2075 * @param connection a MpdConnection
2076 * @param path the path to the playlist.
2078 * List the content of a stored playlist. */
2079 void mpd_sendListPlaylistCommand(mpd_Connection *connection, char *path)
2081 char *arg = mpd_sanitizeArg(path);
2082 int len = strlen("listplaylist") + 2 + strlen(arg) + 3;
2083 char *query = malloc(len);
2085 snprintf(query, len, "listplaylist \"%s\"\n", arg);
2086 mpd_sendInfoCommand(connection, query);
2091 void mpd_sendPlaylistClearCommand(mpd_Connection *connection, char *path)
2093 char *sPath = mpd_sanitizeArg(path);
2094 int len = strlen("playlistclear") + 2 + strlen(sPath) + 3;
2095 char *string = malloc(len);
2097 snprintf(string, len, "playlistclear \"%s\"\n", sPath);
2098 mpd_executeCommand(connection, string);
2103 void mpd_sendPlaylistAddCommand(mpd_Connection *connection, char *playlist,
2106 char *sPlaylist = mpd_sanitizeArg(playlist);
2107 char *sPath = mpd_sanitizeArg(path);
2108 int len = strlen("playlistadd") + 2 + strlen(sPlaylist) + 3 +
2110 char *string = malloc(len);
2112 snprintf(string, len, "playlistadd \"%s\" \"%s\"\n", sPlaylist, sPath);
2113 mpd_executeCommand(connection, string);
2119 void mpd_sendPlaylistMoveCommand(mpd_Connection *connection, char *playlist,
2122 char *sPlaylist = mpd_sanitizeArg(playlist);
2123 int len = strlen("playlistmove") + 2 + strlen(sPlaylist) + 3 + INTLEN +
2125 char *string = malloc(len);
2127 snprintf(string, len, "playlistmove \"%s\" \"%i\" \"%i\"\n", sPlaylist,
2129 mpd_executeCommand(connection, string);
2134 void mpd_sendPlaylistDeleteCommand(mpd_Connection *connection, char *playlist,
2137 char *sPlaylist = mpd_sanitizeArg(playlist);
2138 int len = strlen("playlistdelete") + 2 + strlen(sPlaylist) + 3 +
2140 char *string = malloc(len);
2142 snprintf(string, len, "playlistdelete \"%s\" \"%i\"\n", sPlaylist, pos);
2143 mpd_executeCommand(connection, string);