1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2 * vim: ts=4 sw=4 noet ai cindent syntax=c
5 * (c)2003-2006 by Warren Dukes (warren.dukes@gmail.com)
6 * This project's homepage is: http://www.musicpd.org
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * - Neither the name of the Music Player Daemon nor the names of its
20 * contributors may be used to endorse or promote products derived from
21 * this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include "libmpdclient.h"
42 #include <sys/types.h>
43 #include <sys/param.h>
49 # include <ws2tcpip.h>
52 # include <netinet/in.h>
53 # include <arpa/inet.h>
54 # 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);
123 static int uds_connect(mpd_Connection *connection, const char *host,
126 struct sockaddr_un addr;
128 strncpy(addr.sun_path, host, sizeof(addr.sun_path)-1);
129 addr.sun_family = AF_UNIX;
130 addr.sun_path[sizeof(addr.sun_path)-1] = 0;
131 connection->sock = socket(AF_UNIX, SOCK_STREAM, 0);
133 if (connection->sock < 0) {
134 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
135 "problems creating socket: %s", strerror(errno));
136 connection->error = MPD_ERROR_SYSTEM;
140 mpd_setConnectionTimeout(connection, timeout);
143 if (do_connect_fail(connection, (struct sockaddr *)&addr, SUN_LEN(&addr))) {
144 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
145 "problems cconnecting socket: %s", strerror(errno));
146 closesocket(connection->sock);
147 connection->sock = -1;
148 connection->error = MPD_ERROR_SYSTEM;
156 static int mpd_connect(mpd_Connection *connection, const char *host, int port,
160 char service[INTLEN + 1];
161 struct addrinfo hints;
162 struct addrinfo *res = NULL;
163 struct addrinfo *addrinfo = NULL;
166 return uds_connect(connection, host, timeout);
169 hints.ai_flags = AI_ADDRCONFIG;
170 hints.ai_family = AF_UNSPEC;
171 hints.ai_socktype = SOCK_STREAM;
172 hints.ai_protocol = IPPROTO_TCP;
173 hints.ai_addrlen = 0;
174 hints.ai_addr = NULL;
175 hints.ai_canonname = NULL;
176 hints.ai_next = NULL;
178 snprintf(service, sizeof(service), "%i", port);
180 error = getaddrinfo(host, service, &hints, &addrinfo);
183 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
184 "host \"%s\" not found: %s", host, gai_strerror(error));
185 connection->error = MPD_ERROR_UNKHOST;
189 for (res = addrinfo; res; res = res->ai_next) {
191 if (connection->sock > -1) {
192 closesocket(connection->sock);
194 connection->sock = socket(res->ai_family, SOCK_STREAM,
196 if (connection->sock < 0) {
197 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
198 "problems creating socket: %s", strerror(errno));
199 connection->error = MPD_ERROR_SYSTEM;
200 freeaddrinfo(addrinfo);
204 mpd_setConnectionTimeout(connection, timeout);
207 if (do_connect_fail(connection, res->ai_addr, res->ai_addrlen)) {
208 /* try the next address family */
209 closesocket(connection->sock);
210 connection->sock = -1;
215 freeaddrinfo(addrinfo);
217 if (connection->sock < 0) {
218 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
219 "problems connecting to \"%s\" on port %i: %s", host, port,
221 connection->error = MPD_ERROR_CONNPORT;
228 #else /* !MPD_HAVE_GAI */
229 static int mpd_connect(mpd_Connection *connection, const char *host, int port,
232 struct hostent he, *he_res = 0;
235 struct sockaddr *dest;
237 struct sockaddr_in sin;
240 return uds_connect(connection, host, timeout);
242 #ifdef HAVE_GETHOSTBYNAME_R
243 if (gethostbyname_r(rhost, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
244 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
245 "%s ('%s')", hstrerror(h_errno), host);
246 connection->error = MPD_ERROR_UNKHOST;
249 #else /* HAVE_GETHOSTBYNAME_R */
250 if (!(he_res = gethostbyname(host))) {
251 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
252 "host \"%s\" not found", host);
253 connection->error = MPD_ERROR_UNKHOST;
256 #endif /* HAVE_GETHOSTBYNAME_R */
258 memset(&sin, 0, sizeof(struct sockaddr_in));
259 /* dest.sin_family = he_res->h_addrtype; */
260 sin.sin_family = AF_INET;
261 sin.sin_port = htons(port);
263 switch (he_res->h_addrtype) {
265 memcpy((char *) &sin.sin_addr.s_addr, (char *) he_res->h_addr,
267 dest = (struct sockaddr *) &sin;
268 destlen = sizeof(struct sockaddr_in);
271 strcpy(connection->errorStr, "address type is not IPv4");
272 connection->error = MPD_ERROR_SYSTEM;
277 if (connection->sock > -1) {
278 closesocket(connection->sock);
280 if ((connection->sock = socket(dest->sa_family, SOCK_STREAM, 0)) < 0) {
281 strcpy(connection->errorStr, "problems creating socket");
282 connection->error = MPD_ERROR_SYSTEM;
286 mpd_setConnectionTimeout(connection, timeout);
289 if (do_connect_fail(connection, dest, destlen)) {
290 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
291 "problems connecting to \"%s\" on port %i", host, port);
292 connection->error = MPD_ERROR_CONNPORT;
298 #endif /* !MPD_HAVE_GAI */
300 const char *mpdTagItemKeys[MPD_TAG_NUM_OF_ITEM_TYPES] = {
316 static char *mpd_sanitizeArg(const char *arg)
320 register const char *c;
323 /* instead of counting in that loop above,
324 * just use a bit more memory and halve running time */
325 ret = malloc(strlen(arg) * 2 + 1);
329 for (i = strlen(arg) + 1; i != 0; --i) {
330 if (*c == '"' || *c == '\\') {
339 static mpd_ReturnElement *mpd_newReturnElement(const char *name,
342 mpd_ReturnElement *ret = malloc(sizeof(mpd_ReturnElement));
344 ret->name = strndup(name, text_buffer_size);
345 ret->value = strndup(value, text_buffer_size);
350 static void mpd_freeReturnElement(mpd_ReturnElement *re)
357 void mpd_setConnectionTimeout(mpd_Connection *connection, float timeout)
359 connection->timeout.tv_sec = (int) timeout;
360 connection->timeout.tv_usec =
361 (int) ((timeout - connection->timeout.tv_sec) * 1e6 + 0.5);
364 static int mpd_parseWelcome(mpd_Connection *connection, const char *host,
365 int port, /* char *rt, */ char *output)
371 if (strncmp(output, MPD_WELCOME_MESSAGE, strlen(MPD_WELCOME_MESSAGE))) {
372 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
373 "mpd not running on port %i on host \"%s\"", port, host);
374 connection->error = MPD_ERROR_NOTMPD;
378 tmp = &output[strlen(MPD_WELCOME_MESSAGE)];
380 for (i = 0; i < 3; i++) {
382 connection->version[i] = strtol(tmp, &test, 10);
385 if (!tmp || (test[0] != '.' && test[0] != '\0')) {
386 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
387 "error parsing version number at \"%s\"",
388 &output[strlen(MPD_WELCOME_MESSAGE)]);
389 connection->error = MPD_ERROR_NOTMPD;
398 mpd_Connection *mpd_newConnection(const char *host, int port, float timeout)
403 mpd_Connection *connection = malloc(sizeof(mpd_Connection));
407 strcpy(connection->buffer, "");
408 connection->buflen = 0;
409 connection->bufstart = 0;
410 strcpy(connection->errorStr, "");
411 connection->error = 0;
412 connection->doneProcessing = 0;
413 connection->commandList = 0;
414 connection->listOks = 0;
415 connection->doneListOk = 0;
416 connection->sock = -1;
417 connection->returnElement = NULL;
418 connection->request = NULL;
420 if (winsock_dll_error(connection)) {
424 if (mpd_connect(connection, host, port, timeout) < 0) {
428 while (!(rt = strstr(connection->buffer, "\n"))) {
429 tv.tv_sec = connection->timeout.tv_sec;
430 tv.tv_usec = connection->timeout.tv_usec;
432 FD_SET(connection->sock, &fds);
433 if ((err = select(connection->sock + 1, &fds, NULL, NULL, &tv)) == 1) {
436 readed = recv(connection->sock,
437 &(connection->buffer[connection->buflen]),
438 MPD_BUFFER_MAX_LENGTH - connection->buflen, 0);
440 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
441 "problems getting a response from \"%s\" on port %i : %s",
442 host, port, strerror(errno));
443 connection->error = MPD_ERROR_NORESPONSE;
446 connection->buflen += readed;
447 connection->buffer[connection->buflen] = '\0';
448 } else if (err < 0) {
449 if (SELECT_ERRNO_IGNORE) {
452 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
453 "problems connecting to \"%s\" on port %i", host, port);
454 connection->error = MPD_ERROR_CONNPORT;
457 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
458 "timeout in attempting to get a response from \"%s\" on "
459 "port %i", host, port);
460 connection->error = MPD_ERROR_NORESPONSE;
466 output = strndup(connection->buffer, text_buffer_size);
467 strcpy(connection->buffer, rt + 1);
468 connection->buflen = strlen(connection->buffer);
470 if (mpd_parseWelcome(connection, host, port, /* rt, */ output) == 0) {
471 connection->doneProcessing = 1;
479 void mpd_clearError(mpd_Connection *connection)
481 connection->error = 0;
482 connection->errorStr[0] = '\0';
485 void mpd_closeConnection(mpd_Connection *connection)
487 closesocket(connection->sock);
488 if (connection->returnElement) {
489 free(connection->returnElement);
491 if (connection->request) {
492 free(connection->request);
498 static void mpd_executeCommand(mpd_Connection *connection, const char *command)
503 const char *commandPtr = command;
504 int commandLen = strlen(command);
506 if (!connection->doneProcessing && !connection->commandList) {
507 strcpy(connection->errorStr, "not done processing current command");
508 connection->error = 1;
512 mpd_clearError(connection);
515 FD_SET(connection->sock, &fds);
516 tv.tv_sec = connection->timeout.tv_sec;
517 tv.tv_usec = connection->timeout.tv_usec;
519 while ((ret = select(connection->sock + 1, NULL, &fds, NULL, &tv) == 1)
520 || (ret == -1 && SELECT_ERRNO_IGNORE)) {
521 ret = send(connection->sock, commandPtr, commandLen, MSG_DONTWAIT);
523 if (SENDRECV_ERRNO_IGNORE) {
526 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
527 "problems giving command \"%s\"", command);
528 connection->error = MPD_ERROR_SENDING;
535 if (commandLen <= 0) {
540 if (commandLen > 0) {
542 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
543 "timeout sending command \"%s\"", command);
544 connection->error = MPD_ERROR_TIMEOUT;
548 if (!connection->commandList) {
549 connection->doneProcessing = 0;
550 } else if (connection->commandList == COMMAND_LIST_OK) {
551 connection->listOks++;
555 static void mpd_getNextReturnElement(mpd_Connection *connection)
565 char *bufferCheck = NULL;
569 if (connection->returnElement) {
570 mpd_freeReturnElement(connection->returnElement);
572 connection->returnElement = NULL;
574 if (connection->doneProcessing
575 || (connection->listOks && connection->doneListOk)) {
576 strcpy(connection->errorStr,
577 "already done processing current command");
578 connection->error = 1;
582 bufferCheck = connection->buffer + connection->bufstart;
583 while (connection->bufstart >= connection->buflen
584 || !(rt = strchr(bufferCheck, '\n'))) {
585 if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
586 memmove(connection->buffer,
587 connection->buffer + connection->bufstart,
588 connection->buflen - connection->bufstart + 1);
589 connection->buflen -= connection->bufstart;
590 connection->bufstart = 0;
592 if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
593 strcpy(connection->errorStr, "buffer overrun");
594 connection->error = MPD_ERROR_BUFFEROVERRUN;
595 connection->doneProcessing = 1;
596 connection->doneListOk = 0;
599 bufferCheck = connection->buffer + connection->buflen;
600 tv.tv_sec = connection->timeout.tv_sec;
601 tv.tv_usec = connection->timeout.tv_usec;
603 FD_SET(connection->sock, &fds);
604 if ((err = select(connection->sock + 1, &fds, NULL, NULL, &tv) == 1)) {
605 readed = recv(connection->sock,
606 connection->buffer + connection->buflen,
607 MPD_BUFFER_MAX_LENGTH - connection->buflen, MSG_DONTWAIT);
608 if (readed < 0 && SENDRECV_ERRNO_IGNORE) {
612 strcpy(connection->errorStr, "connection closed");
613 connection->error = MPD_ERROR_CONNCLOSED;
614 connection->doneProcessing = 1;
615 connection->doneListOk = 0;
618 connection->buflen += readed;
619 connection->buffer[connection->buflen] = '\0';
620 } else if (err < 0 && SELECT_ERRNO_IGNORE) {
623 strcpy(connection->errorStr, "connection timeout");
624 connection->error = MPD_ERROR_TIMEOUT;
625 connection->doneProcessing = 1;
626 connection->doneListOk = 0;
632 output = connection->buffer + connection->bufstart;
633 connection->bufstart = rt - connection->buffer + 1;
635 if (strcmp(output, "OK") == 0) {
636 if (connection->listOks > 0) {
637 strcpy(connection->errorStr, "expected more list_OK's");
638 connection->error = 1;
640 connection->listOks = 0;
641 connection->doneProcessing = 1;
642 connection->doneListOk = 0;
646 if (strcmp(output, "list_OK") == 0) {
647 if (!connection->listOks) {
648 strcpy(connection->errorStr, "got an unexpected list_OK");
649 connection->error = 1;
651 connection->doneListOk = 1;
652 connection->listOks--;
657 if (strncmp(output, "ACK", strlen("ACK")) == 0) {
662 strcpy(connection->errorStr, output);
663 connection->error = MPD_ERROR_ACK;
664 connection->errorCode = MPD_ACK_ERROR_UNK;
665 connection->errorAt = MPD_ERROR_AT_UNK;
666 connection->doneProcessing = 1;
667 connection->doneListOk = 0;
669 needle = strchr(output, '[');
673 val = strtol(needle + 1, &test, 10);
677 connection->errorCode = val;
678 val = strtol(test + 1, &test, 10);
682 connection->errorAt = val;
686 tok = strchr(output, ':');
695 if (value[0] == ' ') {
696 connection->returnElement = mpd_newReturnElement(name, &(value[1]));
698 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
699 "error parsing: %s:%s", name, value);
700 connection->error = 1;
704 void mpd_finishCommand(mpd_Connection *connection)
706 while (!connection->doneProcessing) {
707 if (connection->doneListOk) {
708 connection->doneListOk = 0;
710 mpd_getNextReturnElement(connection);
714 static void mpd_finishListOkCommand(mpd_Connection *connection)
716 while (!connection->doneProcessing && connection->listOks
717 && !connection->doneListOk) {
718 mpd_getNextReturnElement(connection);
722 int mpd_nextListOkCommand(mpd_Connection *connection)
724 mpd_finishListOkCommand(connection);
725 if (!connection->doneProcessing) {
726 connection->doneListOk = 0;
728 if (connection->listOks == 0 || connection->doneProcessing) {
734 void mpd_sendStatusCommand(mpd_Connection *connection)
736 mpd_executeCommand(connection, "status\n");
739 mpd_Status *mpd_getStatus(mpd_Connection *connection)
743 /* mpd_executeCommand(connection, "status\n");
744 if (connection->error) {
748 if (connection->doneProcessing
749 || (connection->listOks && connection->doneListOk)) {
753 if (!connection->returnElement) {
754 mpd_getNextReturnElement(connection);
757 status = malloc(sizeof(mpd_Status));
761 status->playlist = -1;
762 status->playlistLength = -1;
766 status->elapsedTime = 0;
767 status->totalTime = 0;
769 status->sampleRate = 0;
771 status->channels = 0;
772 status->crossfade = -1;
773 status->error = NULL;
774 status->updatingDb = 0;
776 if (connection->error) {
780 while (connection->returnElement) {
781 mpd_ReturnElement *re = connection->returnElement;
783 if (strcmp(re->name, "volume") == 0) {
784 status->volume = atoi(re->value);
785 } else if (strcmp(re->name, "repeat") == 0) {
786 status->repeat = atoi(re->value);
787 } else if (strcmp(re->name, "random") == 0) {
788 status->random = atoi(re->value);
789 } else if (strcmp(re->name, "playlist") == 0) {
790 status->playlist = strtol(re->value, NULL, 10);
791 } else if (strcmp(re->name, "playlistlength") == 0) {
792 status->playlistLength = atoi(re->value);
793 } else if (strcmp(re->name, "bitrate") == 0) {
794 status->bitRate = atoi(re->value);
795 } else if (strcmp(re->name, "state") == 0) {
796 if (strcmp(re->value, "play") == 0) {
797 status->state = MPD_STATUS_STATE_PLAY;
798 } else if (strcmp(re->value, "stop") == 0) {
799 status->state = MPD_STATUS_STATE_STOP;
800 } else if (strcmp(re->value, "pause") == 0) {
801 status->state = MPD_STATUS_STATE_PAUSE;
803 status->state = MPD_STATUS_STATE_UNKNOWN;
805 } else if (strcmp(re->name, "song") == 0) {
806 status->song = atoi(re->value);
807 } else if (strcmp(re->name, "songid") == 0) {
808 status->songid = atoi(re->value);
809 } else if (strcmp(re->name, "time") == 0) {
810 char *tok = strchr(re->value, ':');
812 /* the second strchr below is a safety check */
813 if (tok && (strchr(tok, 0) > (tok + 1))) {
814 /* atoi stops at the first non-[0-9] char: */
815 status->elapsedTime = atoi(re->value);
816 status->totalTime = atoi(tok + 1);
818 } else if (strcmp(re->name, "error") == 0) {
819 status->error = strndup(re->value, text_buffer_size);
820 } else if (strcmp(re->name, "xfade") == 0) {
821 status->crossfade = atoi(re->value);
822 } else if (strcmp(re->name, "updating_db") == 0) {
823 status->updatingDb = atoi(re->value);
824 } else if (strcmp(re->name, "audio") == 0) {
825 char *tok = strchr(re->value, ':');
827 if (tok && (strchr(tok, 0) > (tok + 1))) {
828 status->sampleRate = atoi(re->value);
829 status->bits = atoi(++tok);
830 tok = strchr(tok, ':');
831 if (tok && (strchr(tok, 0) > (tok + 1))) {
832 status->channels = atoi(tok + 1);
837 mpd_getNextReturnElement(connection);
838 if (connection->error) {
844 if (connection->error) {
847 } else if (status->state < 0) {
848 strcpy(connection->errorStr, "state not found");
849 connection->error = 1;
857 void mpd_freeStatus(mpd_Status *status)
865 void mpd_sendStatsCommand(mpd_Connection *connection)
867 mpd_executeCommand(connection, "stats\n");
870 mpd_Stats *mpd_getStats(mpd_Connection *connection)
874 /* mpd_executeCommand(connection, "stats\n");
875 if (connection->error) {
879 if (connection->doneProcessing
880 || (connection->listOks && connection->doneListOk)) {
884 if (!connection->returnElement) {
885 mpd_getNextReturnElement(connection);
888 stats = malloc(sizeof(mpd_Stats));
889 stats->numberOfArtists = 0;
890 stats->numberOfAlbums = 0;
891 stats->numberOfSongs = 0;
893 stats->dbUpdateTime = 0;
895 stats->dbPlayTime = 0;
897 if (connection->error) {
901 while (connection->returnElement) {
902 mpd_ReturnElement *re = connection->returnElement;
904 if (strcmp(re->name, "artists") == 0) {
905 stats->numberOfArtists = atoi(re->value);
906 } else if (strcmp(re->name, "albums") == 0) {
907 stats->numberOfAlbums = atoi(re->value);
908 } else if (strcmp(re->name, "songs") == 0) {
909 stats->numberOfSongs = atoi(re->value);
910 } else if (strcmp(re->name, "uptime") == 0) {
911 stats->uptime = strtol(re->value, NULL, 10);
912 } else if (strcmp(re->name, "db_update") == 0) {
913 stats->dbUpdateTime = strtol(re->value, NULL, 10);
914 } else if (strcmp(re->name, "playtime") == 0) {
915 stats->playTime = strtol(re->value, NULL, 10);
916 } else if (strcmp(re->name, "db_playtime") == 0) {
917 stats->dbPlayTime = strtol(re->value, NULL, 10);
920 mpd_getNextReturnElement(connection);
921 if (connection->error) {
927 if (connection->error) {
935 void mpd_freeStats(mpd_Stats *stats)
940 mpd_SearchStats *mpd_getSearchStats(mpd_Connection *connection)
942 mpd_SearchStats *stats;
943 mpd_ReturnElement *re;
945 if (connection->doneProcessing
946 || (connection->listOks && connection->doneListOk)) {
950 if (!connection->returnElement) {
951 mpd_getNextReturnElement(connection);
954 if (connection->error) {
958 stats = malloc(sizeof(mpd_SearchStats));
959 stats->numberOfSongs = 0;
962 while (connection->returnElement) {
963 re = connection->returnElement;
965 if (strcmp(re->name, "songs") == 0) {
966 stats->numberOfSongs = atoi(re->value);
967 } else if (strcmp(re->name, "playtime") == 0) {
968 stats->playTime = strtol(re->value, NULL, 10);
971 mpd_getNextReturnElement(connection);
972 if (connection->error) {
978 if (connection->error) {
986 void mpd_freeSearchStats(mpd_SearchStats *stats)
991 static void mpd_initSong(mpd_Song *song)
1000 /* added by Qball */
1002 song->composer = NULL;
1003 song->performer = NULL;
1005 song->comment = NULL;
1007 song->time = MPD_SONG_NO_TIME;
1008 song->pos = MPD_SONG_NO_NUM;
1009 song->id = MPD_SONG_NO_ID;
1012 static void mpd_finishSong(mpd_Song *song)
1038 if (song->composer) {
1039 free(song->composer);
1044 if (song->comment) {
1045 free(song->comment);
1049 mpd_Song *mpd_newSong(void)
1051 mpd_Song *ret = malloc(sizeof(mpd_Song));
1058 void mpd_freeSong(mpd_Song *song)
1060 mpd_finishSong(song);
1064 mpd_Song *mpd_songDup(mpd_Song *song)
1066 mpd_Song *ret = mpd_newSong();
1069 ret->file = strndup(song->file, text_buffer_size);
1072 ret->artist = strndup(song->artist, text_buffer_size);
1075 ret->album = strndup(song->album, text_buffer_size);
1078 ret->title = strndup(song->title, text_buffer_size);
1081 ret->track = strndup(song->track, text_buffer_size);
1084 ret->name = strndup(song->name, text_buffer_size);
1087 ret->date = strndup(song->date, text_buffer_size);
1090 ret->genre = strndup(song->genre, text_buffer_size);
1092 if (song->composer) {
1093 ret->composer = strndup(song->composer, text_buffer_size);
1096 ret->disc = strndup(song->disc, text_buffer_size);
1098 if (song->comment) {
1099 ret->comment = strndup(song->comment, text_buffer_size);
1101 ret->time = song->time;
1102 ret->pos = song->pos;
1108 static void mpd_initDirectory(mpd_Directory *directory)
1110 directory->path = NULL;
1113 static void mpd_finishDirectory(mpd_Directory *directory)
1115 if (directory->path) {
1116 free(directory->path);
1120 mpd_Directory *mpd_newDirectory(void)
1122 mpd_Directory *directory = malloc(sizeof(mpd_Directory));
1124 mpd_initDirectory(directory);
1129 void mpd_freeDirectory(mpd_Directory *directory)
1131 mpd_finishDirectory(directory);
1136 mpd_Directory *mpd_directoryDup(mpd_Directory *directory)
1138 mpd_Directory *ret = mpd_newDirectory();
1140 if (directory->path) {
1141 ret->path = strndup(directory->path, text_buffer_size);
1147 static void mpd_initPlaylistFile(mpd_PlaylistFile *playlist)
1149 playlist->path = NULL;
1152 static void mpd_finishPlaylistFile(mpd_PlaylistFile *playlist)
1154 if (playlist->path) {
1155 free(playlist->path);
1159 mpd_PlaylistFile *mpd_newPlaylistFile(void)
1161 mpd_PlaylistFile *playlist = malloc(sizeof(mpd_PlaylistFile));
1163 mpd_initPlaylistFile(playlist);
1168 void mpd_freePlaylistFile(mpd_PlaylistFile *playlist)
1170 mpd_finishPlaylistFile(playlist);
1174 mpd_PlaylistFile *mpd_playlistFileDup(mpd_PlaylistFile *playlist)
1176 mpd_PlaylistFile *ret = mpd_newPlaylistFile();
1178 if (playlist->path) {
1179 ret->path = strndup(playlist->path, text_buffer_size);
1185 static void mpd_initInfoEntity(mpd_InfoEntity *entity)
1187 entity->info.directory = NULL;
1190 static void mpd_finishInfoEntity(mpd_InfoEntity *entity)
1192 if (entity->info.directory) {
1193 if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1194 mpd_freeDirectory(entity->info.directory);
1195 } else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
1196 mpd_freeSong(entity->info.song);
1197 } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1198 mpd_freePlaylistFile(entity->info.playlistFile);
1203 mpd_InfoEntity *mpd_newInfoEntity(void)
1205 mpd_InfoEntity *entity = malloc(sizeof(mpd_InfoEntity));
1207 mpd_initInfoEntity(entity);
1212 void mpd_freeInfoEntity(mpd_InfoEntity *entity)
1214 mpd_finishInfoEntity(entity);
1218 static void mpd_sendInfoCommand(mpd_Connection *connection, char *command)
1220 mpd_executeCommand(connection, command);
1223 mpd_InfoEntity *mpd_getNextInfoEntity(mpd_Connection *connection)
1225 mpd_InfoEntity *entity = NULL;
1227 if (connection->doneProcessing
1228 || (connection->listOks && connection->doneListOk)) {
1232 if (!connection->returnElement) {
1233 mpd_getNextReturnElement(connection);
1236 if (connection->returnElement) {
1237 if (strcmp(connection->returnElement->name, "file") == 0) {
1238 entity = mpd_newInfoEntity();
1239 entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1240 entity->info.song = mpd_newSong();
1241 entity->info.song->file = strndup(connection->returnElement->value, text_buffer_size);
1242 } else if (strcmp(connection->returnElement->name, "directory") == 0) {
1243 entity = mpd_newInfoEntity();
1244 entity->type = MPD_INFO_ENTITY_TYPE_DIRECTORY;
1245 entity->info.directory = mpd_newDirectory();
1246 entity->info.directory->path =
1247 strndup(connection->returnElement->value, text_buffer_size);
1248 } else if (strcmp(connection->returnElement->name, "playlist") == 0) {
1249 entity = mpd_newInfoEntity();
1250 entity->type = MPD_INFO_ENTITY_TYPE_PLAYLISTFILE;
1251 entity->info.playlistFile = mpd_newPlaylistFile();
1252 entity->info.playlistFile->path =
1253 strndup(connection->returnElement->value, text_buffer_size);
1254 } else if (strcmp(connection->returnElement->name, "cpos") == 0) {
1255 entity = mpd_newInfoEntity();
1256 entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1257 entity->info.song = mpd_newSong();
1258 entity->info.song->pos = atoi(connection->returnElement->value);
1260 connection->error = 1;
1261 strcpy(connection->errorStr, "problem parsing song info");
1268 mpd_getNextReturnElement(connection);
1269 while (connection->returnElement) {
1270 mpd_ReturnElement *re = connection->returnElement;
1272 if (strcmp(re->name, "file") == 0) {
1274 } else if (strcmp(re->name, "directory") == 0) {
1276 } else if (strcmp(re->name, "playlist") == 0) {
1278 } else if (strcmp(re->name, "cpos") == 0) {
1282 if (entity->type == MPD_INFO_ENTITY_TYPE_SONG && strlen(re->value)) {
1283 if (!entity->info.song->artist
1284 && strcmp(re->name, "Artist") == 0) {
1285 entity->info.song->artist = strndup(re->value, text_buffer_size);
1286 } else if (!entity->info.song->album
1287 && strcmp(re->name, "Album") == 0) {
1288 entity->info.song->album = strndup(re->value, text_buffer_size);
1289 } else if (!entity->info.song->title
1290 && strcmp(re->name, "Title") == 0) {
1291 entity->info.song->title = strndup(re->value, text_buffer_size);
1292 } else if (!entity->info.song->track
1293 && strcmp(re->name, "Track") == 0) {
1294 entity->info.song->track = strndup(re->value, text_buffer_size);
1295 } else if (!entity->info.song->name
1296 && strcmp(re->name, "Name") == 0) {
1297 entity->info.song->name = strndup(re->value, text_buffer_size);
1298 } else if (entity->info.song->time == MPD_SONG_NO_TIME
1299 && strcmp(re->name, "Time") == 0) {
1300 entity->info.song->time = atoi(re->value);
1301 } else if (entity->info.song->pos == MPD_SONG_NO_NUM
1302 && strcmp(re->name, "Pos") == 0) {
1303 entity->info.song->pos = atoi(re->value);
1304 } else if (entity->info.song->id == MPD_SONG_NO_ID
1305 && strcmp(re->name, "Id") == 0) {
1306 entity->info.song->id = atoi(re->value);
1307 } else if (!entity->info.song->date
1308 && strcmp(re->name, "Date") == 0) {
1309 entity->info.song->date = strndup(re->value, text_buffer_size);
1310 } else if (!entity->info.song->genre
1311 && strcmp(re->name, "Genre") == 0) {
1312 entity->info.song->genre = strndup(re->value, text_buffer_size);
1313 } else if (!entity->info.song->composer
1314 && strcmp(re->name, "Composer") == 0) {
1315 entity->info.song->composer = strndup(re->value, text_buffer_size);
1316 } else if (!entity->info.song->performer
1317 && strcmp(re->name, "Performer") == 0) {
1318 entity->info.song->performer = strndup(re->value, text_buffer_size);
1319 } else if (!entity->info.song->disc
1320 && strcmp(re->name, "Disc") == 0) {
1321 entity->info.song->disc = strndup(re->value, text_buffer_size);
1322 } else if (!entity->info.song->comment
1323 && strcmp(re->name, "Comment") == 0) {
1324 entity->info.song->comment = strndup(re->value, text_buffer_size);
1326 } else if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1327 } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1330 mpd_getNextReturnElement(connection);
1336 static char *mpd_getNextReturnElementNamed(mpd_Connection *connection,
1339 if (connection->doneProcessing
1340 || (connection->listOks && connection->doneListOk)) {
1344 mpd_getNextReturnElement(connection);
1345 while (connection->returnElement) {
1346 mpd_ReturnElement *re = connection->returnElement;
1348 if (strcmp(re->name, name) == 0) {
1349 return strndup(re->value, text_buffer_size);
1351 mpd_getNextReturnElement(connection);
1357 char *mpd_getNextTag(mpd_Connection *connection, int type)
1359 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES
1360 || type == MPD_TAG_ITEM_ANY) {
1363 if (type == MPD_TAG_ITEM_FILENAME) {
1364 return mpd_getNextReturnElementNamed(connection, "file");
1366 return mpd_getNextReturnElementNamed(connection, mpdTagItemKeys[type]);
1369 char *mpd_getNextArtist(mpd_Connection *connection)
1371 return mpd_getNextReturnElementNamed(connection, "Artist");
1374 char *mpd_getNextAlbum(mpd_Connection *connection)
1376 return mpd_getNextReturnElementNamed(connection, "Album");
1379 void mpd_sendPlaylistInfoCommand(mpd_Connection *connection, int songPos)
1381 int len = strlen("playlistinfo") + 2 + INTLEN + 3;
1382 char *string = malloc(len);
1384 snprintf(string, len, "playlistinfo \"%i\"\n", songPos);
1385 mpd_sendInfoCommand(connection, string);
1389 void mpd_sendPlaylistIdCommand(mpd_Connection *connection, int id)
1391 int len = strlen("playlistid") + 2 + INTLEN + 3;
1392 char *string = malloc(len);
1394 snprintf(string, len, "playlistid \"%i\"\n", id);
1395 mpd_sendInfoCommand(connection, string);
1399 void mpd_sendPlChangesCommand(mpd_Connection *connection, long long playlist)
1401 int len = strlen("plchanges") + 2 + LONGLONGLEN + 3;
1402 char *string = malloc(len);
1404 snprintf(string, len, "plchanges \"%lld\"\n", playlist);
1405 mpd_sendInfoCommand(connection, string);
1409 void mpd_sendPlChangesPosIdCommand(mpd_Connection *connection,
1412 int len = strlen("plchangesposid") + 2 + LONGLONGLEN + 3;
1413 char *string = malloc(len);
1415 snprintf(string, len, "plchangesposid \"%lld\"\n", playlist);
1416 mpd_sendInfoCommand(connection, string);
1420 void mpd_sendListallCommand(mpd_Connection *connection, const char *dir)
1422 char *sDir = mpd_sanitizeArg(dir);
1423 int len = strlen("listall") + 2 + strlen(sDir) + 3;
1424 char *string = malloc(len);
1426 snprintf(string, len, "listall \"%s\"\n", sDir);
1427 mpd_sendInfoCommand(connection, string);
1432 void mpd_sendListallInfoCommand(mpd_Connection *connection, const char *dir)
1434 char *sDir = mpd_sanitizeArg(dir);
1435 int len = strlen("listallinfo") + 2 + strlen(sDir) + 3;
1436 char *string = malloc(len);
1438 snprintf(string, len, "listallinfo \"%s\"\n", sDir);
1439 mpd_sendInfoCommand(connection, string);
1444 void mpd_sendLsInfoCommand(mpd_Connection *connection, const char *dir)
1446 char *sDir = mpd_sanitizeArg(dir);
1447 int len = strlen("lsinfo") + 2 + strlen(sDir) + 3;
1448 char *string = malloc(len);
1450 snprintf(string, len, "lsinfo \"%s\"\n", sDir);
1451 mpd_sendInfoCommand(connection, string);
1456 void mpd_sendCurrentSongCommand(mpd_Connection *connection)
1458 mpd_executeCommand(connection, "currentsong\n");
1461 void mpd_sendSearchCommand(mpd_Connection *connection, int table,
1464 mpd_startSearch(connection, 0);
1465 mpd_addConstraintSearch(connection, table, str);
1466 mpd_commitSearch(connection);
1469 void mpd_sendFindCommand(mpd_Connection *connection, int table,
1472 mpd_startSearch(connection, 1);
1473 mpd_addConstraintSearch(connection, table, str);
1474 mpd_commitSearch(connection);
1477 void mpd_sendListCommand(mpd_Connection *connection, int table,
1484 if (table == MPD_TABLE_ARTIST) {
1485 strcpy(st, "artist");
1486 } else if (table == MPD_TABLE_ALBUM) {
1487 strcpy(st, "album");
1489 connection->error = 1;
1490 strcpy(connection->errorStr, "unknown table for list");
1494 char *sanitArg1 = mpd_sanitizeArg(arg1);
1496 len = strlen("list") + 1 + strlen(sanitArg1) + 2 + strlen(st) + 3;
1497 string = malloc(len);
1498 snprintf(string, len, "list %s \"%s\"\n", st, sanitArg1);
1501 len = strlen("list") + 1 + strlen(st) + 2;
1502 string = malloc(len);
1503 snprintf(string, len, "list %s\n", st);
1505 mpd_sendInfoCommand(connection, string);
1509 void mpd_sendAddCommand(mpd_Connection *connection, const char *file)
1511 char *sFile = mpd_sanitizeArg(file);
1512 int len = strlen("add") + 2 + strlen(sFile) + 3;
1513 char *string = malloc(len);
1515 snprintf(string, len, "add \"%s\"\n", sFile);
1516 mpd_executeCommand(connection, string);
1521 int mpd_sendAddIdCommand(mpd_Connection *connection, const char *file)
1524 char *sFile = mpd_sanitizeArg(file);
1525 int len = strlen("addid") + 2 + strlen(sFile) + 3;
1526 char *string = malloc(len);
1528 snprintf(string, len, "addid \"%s\"\n", sFile);
1529 mpd_sendInfoCommand(connection, string);
1533 string = mpd_getNextReturnElementNamed(connection, "Id");
1535 retval = atoi(string);
1542 void mpd_sendDeleteCommand(mpd_Connection *connection, int songPos)
1544 int len = strlen("delete") + 2 + INTLEN + 3;
1545 char *string = malloc(len);
1547 snprintf(string, len, "delete \"%i\"\n", songPos);
1548 mpd_sendInfoCommand(connection, string);
1552 void mpd_sendDeleteIdCommand(mpd_Connection *connection, int id)
1554 int len = strlen("deleteid") + 2 + INTLEN + 3;
1555 char *string = malloc(len);
1557 snprintf(string, len, "deleteid \"%i\"\n", id);
1558 mpd_sendInfoCommand(connection, string);
1562 void mpd_sendSaveCommand(mpd_Connection *connection, const char *name)
1564 char *sName = mpd_sanitizeArg(name);
1565 int len = strlen("save") + 2 + strlen(sName) + 3;
1566 char *string = malloc(len);
1568 snprintf(string, len, "save \"%s\"\n", sName);
1569 mpd_executeCommand(connection, string);
1574 void mpd_sendLoadCommand(mpd_Connection *connection, const char *name)
1576 char *sName = mpd_sanitizeArg(name);
1577 int len = strlen("load") + 2 + strlen(sName) + 3;
1578 char *string = malloc(len);
1580 snprintf(string, len, "load \"%s\"\n", sName);
1581 mpd_executeCommand(connection, string);
1586 void mpd_sendRmCommand(mpd_Connection *connection, const char *name)
1588 char *sName = mpd_sanitizeArg(name);
1589 int len = strlen("rm") + 2 + strlen(sName) + 3;
1590 char *string = malloc(len);
1592 snprintf(string, len, "rm \"%s\"\n", sName);
1593 mpd_executeCommand(connection, string);
1598 void mpd_sendRenameCommand(mpd_Connection *connection, const char *from,
1601 char *sFrom = mpd_sanitizeArg(from);
1602 char *sTo = mpd_sanitizeArg(to);
1603 int len = strlen("rename") + 2 + strlen(sFrom) + 3 + strlen(sTo) + 3;
1604 char *string = malloc(len);
1606 snprintf(string, len, "rename \"%s\" \"%s\"\n", sFrom, sTo);
1607 mpd_executeCommand(connection, string);
1613 void mpd_sendShuffleCommand(mpd_Connection *connection)
1615 mpd_executeCommand(connection, "shuffle\n");
1618 void mpd_sendClearCommand(mpd_Connection *connection)
1620 mpd_executeCommand(connection, "clear\n");
1623 void mpd_sendPlayCommand(mpd_Connection *connection, int songPos)
1625 int len = strlen("play") + 2 + INTLEN + 3;
1626 char *string = malloc(len);
1628 snprintf(string, len, "play \"%i\"\n", songPos);
1629 mpd_sendInfoCommand(connection, string);
1633 void mpd_sendPlayIdCommand(mpd_Connection *connection, int id)
1635 int len = strlen("playid") + 2 + INTLEN + 3;
1636 char *string = malloc(len);
1638 snprintf(string, len, "playid \"%i\"\n", id);
1639 mpd_sendInfoCommand(connection, string);
1643 void mpd_sendStopCommand(mpd_Connection *connection)
1645 mpd_executeCommand(connection, "stop\n");
1648 void mpd_sendPauseCommand(mpd_Connection *connection, int pauseMode)
1650 int len = strlen("pause") + 2 + INTLEN + 3;
1651 char *string = malloc(len);
1653 snprintf(string, len, "pause \"%i\"\n", pauseMode);
1654 mpd_executeCommand(connection, string);
1658 void mpd_sendNextCommand(mpd_Connection *connection)
1660 mpd_executeCommand(connection, "next\n");
1663 void mpd_sendMoveCommand(mpd_Connection *connection, int from, int to)
1665 int len = strlen("move") + 2 + INTLEN + 3 + INTLEN + 3;
1666 char *string = malloc(len);
1668 snprintf(string, len, "move \"%i\" \"%i\"\n", from, to);
1669 mpd_sendInfoCommand(connection, string);
1673 void mpd_sendMoveIdCommand(mpd_Connection *connection, int id, int to)
1675 int len = strlen("moveid") + 2 + INTLEN + 3 + INTLEN + 3;
1676 char *string = malloc(len);
1678 snprintf(string, len, "moveid \"%i\" \"%i\"\n", id, to);
1679 mpd_sendInfoCommand(connection, string);
1683 void mpd_sendSwapCommand(mpd_Connection *connection, int song1, int song2)
1685 int len = strlen("swap") + 2 + INTLEN + 3 + INTLEN + 3;
1686 char *string = malloc(len);
1688 snprintf(string, len, "swap \"%i\" \"%i\"\n", song1, song2);
1689 mpd_sendInfoCommand(connection, string);
1693 void mpd_sendSwapIdCommand(mpd_Connection *connection, int id1, int id2)
1695 int len = strlen("swapid") + 2 + INTLEN + 3 + INTLEN + 3;
1696 char *string = malloc(len);
1698 snprintf(string, len, "swapid \"%i\" \"%i\"\n", id1, id2);
1699 mpd_sendInfoCommand(connection, string);
1703 void mpd_sendSeekCommand(mpd_Connection *connection, int song, int seek_time)
1705 int len = strlen("seek") + 2 + INTLEN + 3 + INTLEN + 3;
1706 char *string = malloc(len);
1708 snprintf(string, len, "seek \"%i\" \"%i\"\n", song, seek_time);
1709 mpd_sendInfoCommand(connection, string);
1713 void mpd_sendSeekIdCommand(mpd_Connection *connection, int id, int seek_time)
1715 int len = strlen("seekid") + 2 + INTLEN + 3 + INTLEN + 3;
1716 char *string = malloc(len);
1718 snprintf(string, len, "seekid \"%i\" \"%i\"\n", id, seek_time);
1719 mpd_sendInfoCommand(connection, string);
1723 void mpd_sendUpdateCommand(mpd_Connection *connection, char *path)
1725 char *sPath = mpd_sanitizeArg(path);
1726 int len = strlen("update") + 2 + strlen(sPath) + 3;
1727 char *string = malloc(len);
1729 snprintf(string, len, "update \"%s\"\n", sPath);
1730 mpd_sendInfoCommand(connection, string);
1735 int mpd_getUpdateId(mpd_Connection *connection)
1740 jobid = mpd_getNextReturnElementNamed(connection, "updating_db");
1749 void mpd_sendPrevCommand(mpd_Connection *connection)
1751 mpd_executeCommand(connection, "previous\n");
1754 void mpd_sendRepeatCommand(mpd_Connection *connection, int repeatMode)
1756 int len = strlen("repeat") + 2 + INTLEN + 3;
1757 char *string = malloc(len);
1759 snprintf(string, len, "repeat \"%i\"\n", repeatMode);
1760 mpd_executeCommand(connection, string);
1764 void mpd_sendRandomCommand(mpd_Connection *connection, int randomMode)
1766 int len = strlen("random") + 2 + INTLEN + 3;
1767 char *string = malloc(len);
1769 snprintf(string, len, "random \"%i\"\n", randomMode);
1770 mpd_executeCommand(connection, string);
1774 void mpd_sendSetvolCommand(mpd_Connection *connection, int volumeChange)
1776 int len = strlen("setvol") + 2 + INTLEN + 3;
1777 char *string = malloc(len);
1779 snprintf(string, len, "setvol \"%i\"\n", volumeChange);
1780 mpd_executeCommand(connection, string);
1784 void mpd_sendVolumeCommand(mpd_Connection *connection, int volumeChange)
1786 int len = strlen("volume") + 2 + INTLEN + 3;
1787 char *string = malloc(len);
1789 snprintf(string, len, "volume \"%i\"\n", volumeChange);
1790 mpd_executeCommand(connection, string);
1794 void mpd_sendCrossfadeCommand(mpd_Connection *connection, int seconds)
1796 int len = strlen("crossfade") + 2 + INTLEN + 3;
1797 char *string = malloc(len);
1799 snprintf(string, len, "crossfade \"%i\"\n", seconds);
1800 mpd_executeCommand(connection, string);
1804 void mpd_sendPasswordCommand(mpd_Connection *connection, const char *pass)
1806 char *sPass = mpd_sanitizeArg(pass);
1807 int len = strlen("password") + 2 + strlen(sPass) + 3;
1808 char *string = malloc(len);
1810 snprintf(string, len, "password \"%s\"\n", sPass);
1811 mpd_executeCommand(connection, string);
1816 void mpd_sendCommandListBegin(mpd_Connection *connection)
1818 if (connection->commandList) {
1819 strcpy(connection->errorStr, "already in command list mode");
1820 connection->error = 1;
1823 connection->commandList = COMMAND_LIST;
1824 mpd_executeCommand(connection, "command_list_begin\n");
1827 void mpd_sendCommandListOkBegin(mpd_Connection *connection)
1829 if (connection->commandList) {
1830 strcpy(connection->errorStr, "already in command list mode");
1831 connection->error = 1;
1834 connection->commandList = COMMAND_LIST_OK;
1835 mpd_executeCommand(connection, "command_list_ok_begin\n");
1836 connection->listOks = 0;
1839 void mpd_sendCommandListEnd(mpd_Connection *connection)
1841 if (!connection->commandList) {
1842 strcpy(connection->errorStr, "not in command list mode");
1843 connection->error = 1;
1846 connection->commandList = 0;
1847 mpd_executeCommand(connection, "command_list_end\n");
1850 void mpd_sendOutputsCommand(mpd_Connection *connection)
1852 mpd_executeCommand(connection, "outputs\n");
1855 mpd_OutputEntity *mpd_getNextOutput(mpd_Connection *connection)
1857 mpd_OutputEntity *output = NULL;
1859 if (connection->doneProcessing
1860 || (connection->listOks && connection->doneListOk)) {
1864 if (connection->error) {
1868 output = malloc(sizeof(mpd_OutputEntity));
1870 output->name = NULL;
1871 output->enabled = 0;
1873 if (!connection->returnElement) {
1874 mpd_getNextReturnElement(connection);
1877 while (connection->returnElement) {
1878 mpd_ReturnElement *re = connection->returnElement;
1880 if (strcmp(re->name, "outputid") == 0) {
1881 if (output != NULL && output->id >= 0) {
1884 output->id = atoi(re->value);
1885 } else if (strcmp(re->name, "outputname") == 0) {
1886 output->name = strndup(re->value, text_buffer_size);
1887 } else if (strcmp(re->name, "outputenabled") == 0) {
1888 output->enabled = atoi(re->value);
1891 mpd_getNextReturnElement(connection);
1892 if (connection->error) {
1901 void mpd_sendEnableOutputCommand(mpd_Connection *connection, int outputId)
1903 int len = strlen("enableoutput") + 2 + INTLEN + 3;
1904 char *string = malloc(len);
1906 snprintf(string, len, "enableoutput \"%i\"\n", outputId);
1907 mpd_executeCommand(connection, string);
1911 void mpd_sendDisableOutputCommand(mpd_Connection *connection, int outputId)
1913 int len = strlen("disableoutput") + 2 + INTLEN + 3;
1914 char *string = malloc(len);
1916 snprintf(string, len, "disableoutput \"%i\"\n", outputId);
1917 mpd_executeCommand(connection, string);
1921 void mpd_freeOutputElement(mpd_OutputEntity *output)
1927 /** odd naming, but it gets the not allowed commands */
1928 void mpd_sendNotCommandsCommand(mpd_Connection *connection)
1930 mpd_executeCommand(connection, "notcommands\n");
1933 /** odd naming, but it gets the allowed commands */
1934 void mpd_sendCommandsCommand(mpd_Connection *connection)
1936 mpd_executeCommand(connection, "commands\n");
1939 /** Get the next returned command */
1940 char *mpd_getNextCommand(mpd_Connection *connection)
1942 return mpd_getNextReturnElementNamed(connection, "command");
1945 void mpd_sendUrlHandlersCommand(mpd_Connection *connection)
1947 mpd_executeCommand(connection, "urlhandlers\n");
1950 char *mpd_getNextHandler(mpd_Connection *connection)
1952 return mpd_getNextReturnElementNamed(connection, "handler");
1955 void mpd_sendTagTypesCommand(mpd_Connection *connection)
1957 mpd_executeCommand(connection, "tagtypes\n");
1960 char *mpd_getNextTagType(mpd_Connection *connection)
1962 return mpd_getNextReturnElementNamed(connection, "tagtype");
1965 void mpd_startSearch(mpd_Connection *connection, int exact)
1967 if (connection->request) {
1968 strcpy(connection->errorStr, "search already in progress");
1969 connection->error = 1;
1974 connection->request = strndup("find", text_buffer_size);
1976 connection->request = strndup("search", text_buffer_size);
1980 void mpd_startStatsSearch(mpd_Connection *connection)
1982 if (connection->request) {
1983 strcpy(connection->errorStr, "search already in progress");
1984 connection->error = 1;
1988 connection->request = strndup("count", text_buffer_size);
1991 void mpd_startPlaylistSearch(mpd_Connection *connection, int exact)
1993 if (connection->request) {
1994 strcpy(connection->errorStr, "search already in progress");
1995 connection->error = 1;
2000 connection->request = strndup("playlistfind", text_buffer_size);
2002 connection->request = strndup("playlistsearch", text_buffer_size);
2006 void mpd_startFieldSearch(mpd_Connection *connection, int type)
2008 const char *strtype;
2011 if (connection->request) {
2012 strcpy(connection->errorStr, "search already in progress");
2013 connection->error = 1;
2017 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
2018 strcpy(connection->errorStr, "invalid type specified");
2019 connection->error = 1;
2023 strtype = mpdTagItemKeys[type];
2025 len = 5 + strlen(strtype) + 1;
2026 connection->request = malloc(len);
2028 snprintf(connection->request, len, "list %c%s", tolower(strtype[0]),
2032 void mpd_addConstraintSearch(mpd_Connection *connection, int type,
2035 const char *strtype;
2040 if (!connection->request) {
2041 strcpy(connection->errorStr, "no search in progress");
2042 connection->error = 1;
2046 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
2047 strcpy(connection->errorStr, "invalid type specified");
2048 connection->error = 1;
2053 strcpy(connection->errorStr, "no name specified");
2054 connection->error = 1;
2058 string = strndup(connection->request, text_buffer_size);
2059 strtype = mpdTagItemKeys[type];
2060 arg = mpd_sanitizeArg(name);
2062 len = strlen(string) + 1 + strlen(strtype) + 2 + strlen(arg) + 2;
2063 connection->request = realloc(connection->request, len);
2064 snprintf(connection->request, len, "%s %c%s \"%s\"", string,
2065 tolower(strtype[0]), strtype + 1, arg);
2071 void mpd_commitSearch(mpd_Connection *connection)
2075 if (!connection->request) {
2076 strcpy(connection->errorStr, "no search in progress");
2077 connection->error = 1;
2081 len = strlen(connection->request) + 2;
2082 connection->request = realloc(connection->request, len);
2083 connection->request[len - 2] = '\n';
2084 connection->request[len - 1] = '\0';
2085 mpd_sendInfoCommand(connection, connection->request);
2087 free(connection->request);
2088 connection->request = NULL;
2092 * @param connection a MpdConnection
2093 * @param path the path to the playlist.
2095 * List the content, with full metadata, of a stored playlist. */
2096 void mpd_sendListPlaylistInfoCommand(mpd_Connection *connection, char *path)
2098 char *arg = mpd_sanitizeArg(path);
2099 int len = strlen("listplaylistinfo") + 2 + strlen(arg) + 3;
2100 char *query = malloc(len);
2102 snprintf(query, len, "listplaylistinfo \"%s\"\n", arg);
2103 mpd_sendInfoCommand(connection, query);
2109 * @param connection a MpdConnection
2110 * @param path the path to the playlist.
2112 * List the content of a stored playlist. */
2113 void mpd_sendListPlaylistCommand(mpd_Connection *connection, char *path)
2115 char *arg = mpd_sanitizeArg(path);
2116 int len = strlen("listplaylist") + 2 + strlen(arg) + 3;
2117 char *query = malloc(len);
2119 snprintf(query, len, "listplaylist \"%s\"\n", arg);
2120 mpd_sendInfoCommand(connection, query);
2125 void mpd_sendPlaylistClearCommand(mpd_Connection *connection, char *path)
2127 char *sPath = mpd_sanitizeArg(path);
2128 int len = strlen("playlistclear") + 2 + strlen(sPath) + 3;
2129 char *string = malloc(len);
2131 snprintf(string, len, "playlistclear \"%s\"\n", sPath);
2132 mpd_executeCommand(connection, string);
2137 void mpd_sendPlaylistAddCommand(mpd_Connection *connection, char *playlist,
2140 char *sPlaylist = mpd_sanitizeArg(playlist);
2141 char *sPath = mpd_sanitizeArg(path);
2142 int len = strlen("playlistadd") + 2 + strlen(sPlaylist) + 3 +
2144 char *string = malloc(len);
2146 snprintf(string, len, "playlistadd \"%s\" \"%s\"\n", sPlaylist, sPath);
2147 mpd_executeCommand(connection, string);
2153 void mpd_sendPlaylistMoveCommand(mpd_Connection *connection, char *playlist,
2156 char *sPlaylist = mpd_sanitizeArg(playlist);
2157 int len = strlen("playlistmove") + 2 + strlen(sPlaylist) + 3 + INTLEN +
2159 char *string = malloc(len);
2161 snprintf(string, len, "playlistmove \"%s\" \"%i\" \"%i\"\n", sPlaylist,
2163 mpd_executeCommand(connection, string);
2168 void mpd_sendPlaylistDeleteCommand(mpd_Connection *connection, char *playlist,
2171 char *sPlaylist = mpd_sanitizeArg(playlist);
2172 int len = strlen("playlistdelete") + 2 + strlen(sPlaylist) + 3 +
2174 char *string = malloc(len);
2176 snprintf(string, len, "playlistdelete \"%s\" \"%i\"\n", sPlaylist, pos);
2177 mpd_executeCommand(connection, string);