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>
58 /* (bits + 1) / 3 (plus the sign character) */
59 #define INTLEN ((sizeof(int) * CHAR_BIT + 1) / 3 + 1)
60 #define LONGLONGLEN ((sizeof(long long) * CHAR_BIT + 1) / 3 + 1)
62 #define COMMAND_LIST 1
63 #define COMMAND_LIST_OK 2
72 # define MSG_DONTWAIT 0
76 # define SELECT_ERRNO_IGNORE (errno == WSAEINTR || errno == WSAEINPROGRESS)
77 # define SENDRECV_ERRNO_IGNORE SELECT_ERRNO_IGNORE
79 # define SELECT_ERRNO_IGNORE (errno == EINTR)
80 # define SENDRECV_ERRNO_IGNORE (errno == EINTR || errno == EAGAIN)
81 # define winsock_dll_error(c) 0
82 # define closesocket(s) close(s)
83 # define WSACleanup() do { /* nothing */ } while (0)
87 static int winsock_dll_error(mpd_Connection *connection)
91 if ((WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0
92 || LOBYTE(wsaData.wVersion) != 2
93 || HIBYTE(wsaData.wVersion) != 2) {
94 strcpy(connection->errorStr, "Could not find usable WinSock DLL.");
95 connection->error = MPD_ERROR_SYSTEM;
101 static int do_connect_fail(mpd_Connection *connection,
102 const struct sockaddr *serv_addr, int addrlen)
104 int iMode = 1; /* 0 = blocking, else non-blocking */
106 ioctlsocket(connection->sock, FIONBIO, (u_long FAR *) &iMode);
107 return (connect(connection->sock, serv_addr, addrlen) == SOCKET_ERROR
108 && WSAGetLastError() != WSAEWOULDBLOCK);
110 #else /* !WIN32 (sane operating systems) */
111 static int do_connect_fail(mpd_Connection *connection,
112 const struct sockaddr *serv_addr, int addrlen)
114 int flags = fcntl(connection->sock, F_GETFL, 0);
116 fcntl(connection->sock, F_SETFL, flags | O_NONBLOCK);
117 return (connect(connection->sock, serv_addr, addrlen) < 0
118 && errno != EINPROGRESS);
123 static int mpd_connect(mpd_Connection *connection, const char *host, int port,
127 char service[INTLEN + 1];
128 struct addrinfo hints;
129 struct addrinfo *res = NULL;
130 struct addrinfo *addrinfo = NULL;
134 * XXX: limit address family to PF_INET here.
135 * MPD does not support IPv6 yet, so if GAI returns
136 * an IPv6 address, the later connect() will fail. */
137 hints.ai_flags = AI_ADDRCONFIG;
138 hints.ai_family = PF_INET;
139 hints.ai_socktype = SOCK_STREAM;
140 hints.ai_protocol = IPPROTO_TCP;
141 hints.ai_addrlen = 0;
142 hints.ai_addr = NULL;
143 hints.ai_canonname = NULL;
144 hints.ai_next = NULL;
146 snprintf(service, sizeof(service), "%i", port);
148 error = getaddrinfo(host, service, &hints, &addrinfo);
151 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
152 "host \"%s\" not found: %s", host, gai_strerror(error));
153 connection->error = MPD_ERROR_UNKHOST;
157 for (res = addrinfo; res; res = res->ai_next) {
159 if (connection->sock > -1) {
160 closesocket(connection->sock);
162 connection->sock = socket(res->ai_family, SOCK_STREAM,
164 if (connection->sock < 0) {
165 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
166 "problems creating socket: %s", strerror(errno));
167 connection->error = MPD_ERROR_SYSTEM;
168 freeaddrinfo(addrinfo);
172 mpd_setConnectionTimeout(connection, timeout);
175 if (do_connect_fail(connection, res->ai_addr, res->ai_addrlen)) {
176 /* try the next address family */
177 closesocket(connection->sock);
178 connection->sock = -1;
183 freeaddrinfo(addrinfo);
185 if (connection->sock < 0) {
186 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
187 "problems connecting to \"%s\" on port %i: %s", host, port,
189 connection->error = MPD_ERROR_CONNPORT;
196 #else /* !MPD_HAVE_GAI */
197 static int mpd_connect(mpd_Connection *connection, const char *host, int port,
200 struct hostent he, *he_res = 0;
203 struct sockaddr *dest;
205 struct sockaddr_in sin;
207 #ifdef HAVE_GETHOSTBYNAME_R
208 if (gethostbyname_r(rhost, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
209 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
210 "%s ('%s')", hstrerror(h_errno), host);
211 connection->error = MPD_ERROR_UNKHOST;
214 #else /* HAVE_GETHOSTBYNAME_R */
215 if (!(he_res = gethostbyname(host))) {
216 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
217 "host \"%s\" not found", host);
218 connection->error = MPD_ERROR_UNKHOST;
221 #endif /* HAVE_GETHOSTBYNAME_R */
223 memset(&sin, 0, sizeof(struct sockaddr_in));
224 /* dest.sin_family = he_res->h_addrtype; */
225 sin.sin_family = AF_INET;
226 sin.sin_port = htons(port);
228 switch (he_res->h_addrtype) {
230 memcpy((char *) &sin.sin_addr.s_addr, (char *) he_res->h_addr,
232 dest = (struct sockaddr *) &sin;
233 destlen = sizeof(struct sockaddr_in);
236 strcpy(connection->errorStr, "address type is not IPv4");
237 connection->error = MPD_ERROR_SYSTEM;
242 if (connection->sock > -1) {
243 closesocket(connection->sock);
245 if ((connection->sock = socket(dest->sa_family, SOCK_STREAM, 0)) < 0) {
246 strcpy(connection->errorStr, "problems creating socket");
247 connection->error = MPD_ERROR_SYSTEM;
251 mpd_setConnectionTimeout(connection, timeout);
254 if (do_connect_fail(connection, dest, destlen)) {
255 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
256 "problems connecting to \"%s\" on port %i", host, port);
257 connection->error = MPD_ERROR_CONNPORT;
263 #endif /* !MPD_HAVE_GAI */
265 const char *mpdTagItemKeys[MPD_TAG_NUM_OF_ITEM_TYPES] = {
281 static char *mpd_sanitizeArg(const char *arg)
285 register const char *c;
288 /* instead of counting in that loop above,
289 * just use a bit more memory and halve running time */
290 ret = malloc(strlen(arg) * 2 + 1);
294 for (i = strlen(arg) + 1; i != 0; --i) {
295 if (*c == '"' || *c == '\\') {
304 static mpd_ReturnElement *mpd_newReturnElement(const char *name,
307 mpd_ReturnElement *ret = malloc(sizeof(mpd_ReturnElement));
309 ret->name = strndup(name, text_buffer_size);
310 ret->value = strndup(value, text_buffer_size);
315 static void mpd_freeReturnElement(mpd_ReturnElement *re)
322 void mpd_setConnectionTimeout(mpd_Connection *connection, float timeout)
324 connection->timeout.tv_sec = (int) timeout;
325 connection->timeout.tv_usec =
326 (int) ((timeout - connection->timeout.tv_sec) * 1e6 + 0.5);
329 static int mpd_parseWelcome(mpd_Connection *connection, const char *host,
330 int port, /* char *rt, */ char *output)
336 if (strncmp(output, MPD_WELCOME_MESSAGE, strlen(MPD_WELCOME_MESSAGE))) {
337 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
338 "mpd not running on port %i on host \"%s\"", port, host);
339 connection->error = MPD_ERROR_NOTMPD;
343 tmp = &output[strlen(MPD_WELCOME_MESSAGE)];
345 for (i = 0; i < 3; i++) {
347 connection->version[i] = strtol(tmp, &test, 10);
350 if (!tmp || (test[0] != '.' && test[0] != '\0')) {
351 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
352 "error parsing version number at \"%s\"",
353 &output[strlen(MPD_WELCOME_MESSAGE)]);
354 connection->error = MPD_ERROR_NOTMPD;
363 mpd_Connection *mpd_newConnection(const char *host, int port, float timeout)
368 mpd_Connection *connection = malloc(sizeof(mpd_Connection));
372 strcpy(connection->buffer, "");
373 connection->buflen = 0;
374 connection->bufstart = 0;
375 strcpy(connection->errorStr, "");
376 connection->error = 0;
377 connection->doneProcessing = 0;
378 connection->commandList = 0;
379 connection->listOks = 0;
380 connection->doneListOk = 0;
381 connection->sock = -1;
382 connection->returnElement = NULL;
383 connection->request = NULL;
385 if (winsock_dll_error(connection)) {
389 if (mpd_connect(connection, host, port, timeout) < 0) {
393 while (!(rt = strstr(connection->buffer, "\n"))) {
394 tv.tv_sec = connection->timeout.tv_sec;
395 tv.tv_usec = connection->timeout.tv_usec;
397 FD_SET(connection->sock, &fds);
398 if ((err = select(connection->sock + 1, &fds, NULL, NULL, &tv)) == 1) {
401 readed = recv(connection->sock,
402 &(connection->buffer[connection->buflen]),
403 MPD_BUFFER_MAX_LENGTH - connection->buflen, 0);
405 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
406 "problems getting a response from \"%s\" on port %i : %s",
407 host, port, strerror(errno));
408 connection->error = MPD_ERROR_NORESPONSE;
411 connection->buflen += readed;
412 connection->buffer[connection->buflen] = '\0';
413 } else if (err < 0) {
414 if (SELECT_ERRNO_IGNORE) {
417 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
418 "problems connecting to \"%s\" on port %i", host, port);
419 connection->error = MPD_ERROR_CONNPORT;
422 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
423 "timeout in attempting to get a response from \"%s\" on "
424 "port %i", host, port);
425 connection->error = MPD_ERROR_NORESPONSE;
431 output = strndup(connection->buffer, text_buffer_size);
432 strcpy(connection->buffer, rt + 1);
433 connection->buflen = strlen(connection->buffer);
435 if (mpd_parseWelcome(connection, host, port, /* rt, */ output) == 0) {
436 connection->doneProcessing = 1;
444 void mpd_clearError(mpd_Connection *connection)
446 connection->error = 0;
447 connection->errorStr[0] = '\0';
450 void mpd_closeConnection(mpd_Connection *connection)
452 closesocket(connection->sock);
453 if (connection->returnElement) {
454 free(connection->returnElement);
456 if (connection->request) {
457 free(connection->request);
463 static void mpd_executeCommand(mpd_Connection *connection, const char *command)
468 const char *commandPtr = command;
469 int commandLen = strlen(command);
471 if (!connection->doneProcessing && !connection->commandList) {
472 strcpy(connection->errorStr, "not done processing current command");
473 connection->error = 1;
477 mpd_clearError(connection);
480 FD_SET(connection->sock, &fds);
481 tv.tv_sec = connection->timeout.tv_sec;
482 tv.tv_usec = connection->timeout.tv_usec;
484 while ((ret = select(connection->sock + 1, NULL, &fds, NULL, &tv) == 1)
485 || (ret == -1 && SELECT_ERRNO_IGNORE)) {
486 ret = send(connection->sock, commandPtr, commandLen, MSG_DONTWAIT);
488 if (SENDRECV_ERRNO_IGNORE) {
491 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
492 "problems giving command \"%s\"", command);
493 connection->error = MPD_ERROR_SENDING;
500 if (commandLen <= 0) {
505 if (commandLen > 0) {
507 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
508 "timeout sending command \"%s\"", command);
509 connection->error = MPD_ERROR_TIMEOUT;
513 if (!connection->commandList) {
514 connection->doneProcessing = 0;
515 } else if (connection->commandList == COMMAND_LIST_OK) {
516 connection->listOks++;
520 static void mpd_getNextReturnElement(mpd_Connection *connection)
530 char *bufferCheck = NULL;
534 if (connection->returnElement) {
535 mpd_freeReturnElement(connection->returnElement);
537 connection->returnElement = NULL;
539 if (connection->doneProcessing
540 || (connection->listOks && connection->doneListOk)) {
541 strcpy(connection->errorStr,
542 "already done processing current command");
543 connection->error = 1;
547 bufferCheck = connection->buffer + connection->bufstart;
548 while (connection->bufstart >= connection->buflen
549 || !(rt = strchr(bufferCheck, '\n'))) {
550 if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
551 memmove(connection->buffer,
552 connection->buffer + connection->bufstart,
553 connection->buflen - connection->bufstart + 1);
554 connection->buflen -= connection->bufstart;
555 connection->bufstart = 0;
557 if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
558 strcpy(connection->errorStr, "buffer overrun");
559 connection->error = MPD_ERROR_BUFFEROVERRUN;
560 connection->doneProcessing = 1;
561 connection->doneListOk = 0;
564 bufferCheck = connection->buffer + connection->buflen;
565 tv.tv_sec = connection->timeout.tv_sec;
566 tv.tv_usec = connection->timeout.tv_usec;
568 FD_SET(connection->sock, &fds);
569 if ((err = select(connection->sock + 1, &fds, NULL, NULL, &tv) == 1)) {
570 readed = recv(connection->sock,
571 connection->buffer + connection->buflen,
572 MPD_BUFFER_MAX_LENGTH - connection->buflen, MSG_DONTWAIT);
573 if (readed < 0 && SENDRECV_ERRNO_IGNORE) {
577 strcpy(connection->errorStr, "connection closed");
578 connection->error = MPD_ERROR_CONNCLOSED;
579 connection->doneProcessing = 1;
580 connection->doneListOk = 0;
583 connection->buflen += readed;
584 connection->buffer[connection->buflen] = '\0';
585 } else if (err < 0 && SELECT_ERRNO_IGNORE) {
588 strcpy(connection->errorStr, "connection timeout");
589 connection->error = MPD_ERROR_TIMEOUT;
590 connection->doneProcessing = 1;
591 connection->doneListOk = 0;
597 output = connection->buffer + connection->bufstart;
598 connection->bufstart = rt - connection->buffer + 1;
600 if (strcmp(output, "OK") == 0) {
601 if (connection->listOks > 0) {
602 strcpy(connection->errorStr, "expected more list_OK's");
603 connection->error = 1;
605 connection->listOks = 0;
606 connection->doneProcessing = 1;
607 connection->doneListOk = 0;
611 if (strcmp(output, "list_OK") == 0) {
612 if (!connection->listOks) {
613 strcpy(connection->errorStr, "got an unexpected list_OK");
614 connection->error = 1;
616 connection->doneListOk = 1;
617 connection->listOks--;
622 if (strncmp(output, "ACK", strlen("ACK")) == 0) {
627 strcpy(connection->errorStr, output);
628 connection->error = MPD_ERROR_ACK;
629 connection->errorCode = MPD_ACK_ERROR_UNK;
630 connection->errorAt = MPD_ERROR_AT_UNK;
631 connection->doneProcessing = 1;
632 connection->doneListOk = 0;
634 needle = strchr(output, '[');
638 val = strtol(needle + 1, &test, 10);
642 connection->errorCode = val;
643 val = strtol(test + 1, &test, 10);
647 connection->errorAt = val;
651 tok = strchr(output, ':');
660 if (value[0] == ' ') {
661 connection->returnElement = mpd_newReturnElement(name, &(value[1]));
663 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
664 "error parsing: %s:%s", name, value);
665 connection->error = 1;
669 void mpd_finishCommand(mpd_Connection *connection)
671 while (!connection->doneProcessing) {
672 if (connection->doneListOk) {
673 connection->doneListOk = 0;
675 mpd_getNextReturnElement(connection);
679 static void mpd_finishListOkCommand(mpd_Connection *connection)
681 while (!connection->doneProcessing && connection->listOks
682 && !connection->doneListOk) {
683 mpd_getNextReturnElement(connection);
687 int mpd_nextListOkCommand(mpd_Connection *connection)
689 mpd_finishListOkCommand(connection);
690 if (!connection->doneProcessing) {
691 connection->doneListOk = 0;
693 if (connection->listOks == 0 || connection->doneProcessing) {
699 void mpd_sendStatusCommand(mpd_Connection *connection)
701 mpd_executeCommand(connection, "status\n");
704 mpd_Status *mpd_getStatus(mpd_Connection *connection)
708 /* mpd_executeCommand(connection, "status\n");
709 if (connection->error) {
713 if (connection->doneProcessing
714 || (connection->listOks && connection->doneListOk)) {
718 if (!connection->returnElement) {
719 mpd_getNextReturnElement(connection);
722 status = malloc(sizeof(mpd_Status));
726 status->playlist = -1;
727 status->playlistLength = -1;
731 status->elapsedTime = 0;
732 status->totalTime = 0;
734 status->sampleRate = 0;
736 status->channels = 0;
737 status->crossfade = -1;
738 status->error = NULL;
739 status->updatingDb = 0;
741 if (connection->error) {
745 while (connection->returnElement) {
746 mpd_ReturnElement *re = connection->returnElement;
748 if (strcmp(re->name, "volume") == 0) {
749 status->volume = atoi(re->value);
750 } else if (strcmp(re->name, "repeat") == 0) {
751 status->repeat = atoi(re->value);
752 } else if (strcmp(re->name, "random") == 0) {
753 status->random = atoi(re->value);
754 } else if (strcmp(re->name, "playlist") == 0) {
755 status->playlist = strtol(re->value, NULL, 10);
756 } else if (strcmp(re->name, "playlistlength") == 0) {
757 status->playlistLength = atoi(re->value);
758 } else if (strcmp(re->name, "bitrate") == 0) {
759 status->bitRate = atoi(re->value);
760 } else if (strcmp(re->name, "state") == 0) {
761 if (strcmp(re->value, "play") == 0) {
762 status->state = MPD_STATUS_STATE_PLAY;
763 } else if (strcmp(re->value, "stop") == 0) {
764 status->state = MPD_STATUS_STATE_STOP;
765 } else if (strcmp(re->value, "pause") == 0) {
766 status->state = MPD_STATUS_STATE_PAUSE;
768 status->state = MPD_STATUS_STATE_UNKNOWN;
770 } else if (strcmp(re->name, "song") == 0) {
771 status->song = atoi(re->value);
772 } else if (strcmp(re->name, "songid") == 0) {
773 status->songid = atoi(re->value);
774 } else if (strcmp(re->name, "time") == 0) {
775 char *tok = strchr(re->value, ':');
777 /* the second strchr below is a safety check */
778 if (tok && (strchr(tok, 0) > (tok + 1))) {
779 /* atoi stops at the first non-[0-9] char: */
780 status->elapsedTime = atoi(re->value);
781 status->totalTime = atoi(tok + 1);
783 } else if (strcmp(re->name, "error") == 0) {
784 status->error = strndup(re->value, text_buffer_size);
785 } else if (strcmp(re->name, "xfade") == 0) {
786 status->crossfade = atoi(re->value);
787 } else if (strcmp(re->name, "updating_db") == 0) {
788 status->updatingDb = atoi(re->value);
789 } else if (strcmp(re->name, "audio") == 0) {
790 char *tok = strchr(re->value, ':');
792 if (tok && (strchr(tok, 0) > (tok + 1))) {
793 status->sampleRate = atoi(re->value);
794 status->bits = atoi(++tok);
795 tok = strchr(tok, ':');
796 if (tok && (strchr(tok, 0) > (tok + 1))) {
797 status->channels = atoi(tok + 1);
802 mpd_getNextReturnElement(connection);
803 if (connection->error) {
809 if (connection->error) {
812 } else if (status->state < 0) {
813 strcpy(connection->errorStr, "state not found");
814 connection->error = 1;
822 void mpd_freeStatus(mpd_Status *status)
830 void mpd_sendStatsCommand(mpd_Connection *connection)
832 mpd_executeCommand(connection, "stats\n");
835 mpd_Stats *mpd_getStats(mpd_Connection *connection)
839 /* mpd_executeCommand(connection, "stats\n");
840 if (connection->error) {
844 if (connection->doneProcessing
845 || (connection->listOks && connection->doneListOk)) {
849 if (!connection->returnElement) {
850 mpd_getNextReturnElement(connection);
853 stats = malloc(sizeof(mpd_Stats));
854 stats->numberOfArtists = 0;
855 stats->numberOfAlbums = 0;
856 stats->numberOfSongs = 0;
858 stats->dbUpdateTime = 0;
860 stats->dbPlayTime = 0;
862 if (connection->error) {
866 while (connection->returnElement) {
867 mpd_ReturnElement *re = connection->returnElement;
869 if (strcmp(re->name, "artists") == 0) {
870 stats->numberOfArtists = atoi(re->value);
871 } else if (strcmp(re->name, "albums") == 0) {
872 stats->numberOfAlbums = atoi(re->value);
873 } else if (strcmp(re->name, "songs") == 0) {
874 stats->numberOfSongs = atoi(re->value);
875 } else if (strcmp(re->name, "uptime") == 0) {
876 stats->uptime = strtol(re->value, NULL, 10);
877 } else if (strcmp(re->name, "db_update") == 0) {
878 stats->dbUpdateTime = strtol(re->value, NULL, 10);
879 } else if (strcmp(re->name, "playtime") == 0) {
880 stats->playTime = strtol(re->value, NULL, 10);
881 } else if (strcmp(re->name, "db_playtime") == 0) {
882 stats->dbPlayTime = strtol(re->value, NULL, 10);
885 mpd_getNextReturnElement(connection);
886 if (connection->error) {
892 if (connection->error) {
900 void mpd_freeStats(mpd_Stats *stats)
905 mpd_SearchStats *mpd_getSearchStats(mpd_Connection *connection)
907 mpd_SearchStats *stats;
908 mpd_ReturnElement *re;
910 if (connection->doneProcessing
911 || (connection->listOks && connection->doneListOk)) {
915 if (!connection->returnElement) {
916 mpd_getNextReturnElement(connection);
919 if (connection->error) {
923 stats = malloc(sizeof(mpd_SearchStats));
924 stats->numberOfSongs = 0;
927 while (connection->returnElement) {
928 re = connection->returnElement;
930 if (strcmp(re->name, "songs") == 0) {
931 stats->numberOfSongs = atoi(re->value);
932 } else if (strcmp(re->name, "playtime") == 0) {
933 stats->playTime = strtol(re->value, NULL, 10);
936 mpd_getNextReturnElement(connection);
937 if (connection->error) {
943 if (connection->error) {
951 void mpd_freeSearchStats(mpd_SearchStats *stats)
956 static void mpd_initSong(mpd_Song *song)
967 song->composer = NULL;
968 song->performer = NULL;
970 song->comment = NULL;
972 song->time = MPD_SONG_NO_TIME;
973 song->pos = MPD_SONG_NO_NUM;
974 song->id = MPD_SONG_NO_ID;
977 static void mpd_finishSong(mpd_Song *song)
1003 if (song->composer) {
1004 free(song->composer);
1009 if (song->comment) {
1010 free(song->comment);
1014 mpd_Song *mpd_newSong(void)
1016 mpd_Song *ret = malloc(sizeof(mpd_Song));
1023 void mpd_freeSong(mpd_Song *song)
1025 mpd_finishSong(song);
1029 mpd_Song *mpd_songDup(mpd_Song *song)
1031 mpd_Song *ret = mpd_newSong();
1034 ret->file = strndup(song->file, text_buffer_size);
1037 ret->artist = strndup(song->artist, text_buffer_size);
1040 ret->album = strndup(song->album, text_buffer_size);
1043 ret->title = strndup(song->title, text_buffer_size);
1046 ret->track = strndup(song->track, text_buffer_size);
1049 ret->name = strndup(song->name, text_buffer_size);
1052 ret->date = strndup(song->date, text_buffer_size);
1055 ret->genre = strndup(song->genre, text_buffer_size);
1057 if (song->composer) {
1058 ret->composer = strndup(song->composer, text_buffer_size);
1061 ret->disc = strndup(song->disc, text_buffer_size);
1063 if (song->comment) {
1064 ret->comment = strndup(song->comment, text_buffer_size);
1066 ret->time = song->time;
1067 ret->pos = song->pos;
1073 static void mpd_initDirectory(mpd_Directory *directory)
1075 directory->path = NULL;
1078 static void mpd_finishDirectory(mpd_Directory *directory)
1080 if (directory->path) {
1081 free(directory->path);
1085 mpd_Directory *mpd_newDirectory(void)
1087 mpd_Directory *directory = malloc(sizeof(mpd_Directory));
1089 mpd_initDirectory(directory);
1094 void mpd_freeDirectory(mpd_Directory *directory)
1096 mpd_finishDirectory(directory);
1101 mpd_Directory *mpd_directoryDup(mpd_Directory *directory)
1103 mpd_Directory *ret = mpd_newDirectory();
1105 if (directory->path) {
1106 ret->path = strndup(directory->path, text_buffer_size);
1112 static void mpd_initPlaylistFile(mpd_PlaylistFile *playlist)
1114 playlist->path = NULL;
1117 static void mpd_finishPlaylistFile(mpd_PlaylistFile *playlist)
1119 if (playlist->path) {
1120 free(playlist->path);
1124 mpd_PlaylistFile *mpd_newPlaylistFile(void)
1126 mpd_PlaylistFile *playlist = malloc(sizeof(mpd_PlaylistFile));
1128 mpd_initPlaylistFile(playlist);
1133 void mpd_freePlaylistFile(mpd_PlaylistFile *playlist)
1135 mpd_finishPlaylistFile(playlist);
1139 mpd_PlaylistFile *mpd_playlistFileDup(mpd_PlaylistFile *playlist)
1141 mpd_PlaylistFile *ret = mpd_newPlaylistFile();
1143 if (playlist->path) {
1144 ret->path = strndup(playlist->path, text_buffer_size);
1150 static void mpd_initInfoEntity(mpd_InfoEntity *entity)
1152 entity->info.directory = NULL;
1155 static void mpd_finishInfoEntity(mpd_InfoEntity *entity)
1157 if (entity->info.directory) {
1158 if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1159 mpd_freeDirectory(entity->info.directory);
1160 } else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
1161 mpd_freeSong(entity->info.song);
1162 } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1163 mpd_freePlaylistFile(entity->info.playlistFile);
1168 mpd_InfoEntity *mpd_newInfoEntity(void)
1170 mpd_InfoEntity *entity = malloc(sizeof(mpd_InfoEntity));
1172 mpd_initInfoEntity(entity);
1177 void mpd_freeInfoEntity(mpd_InfoEntity *entity)
1179 mpd_finishInfoEntity(entity);
1183 static void mpd_sendInfoCommand(mpd_Connection *connection, char *command)
1185 mpd_executeCommand(connection, command);
1188 mpd_InfoEntity *mpd_getNextInfoEntity(mpd_Connection *connection)
1190 mpd_InfoEntity *entity = NULL;
1192 if (connection->doneProcessing
1193 || (connection->listOks && connection->doneListOk)) {
1197 if (!connection->returnElement) {
1198 mpd_getNextReturnElement(connection);
1201 if (connection->returnElement) {
1202 if (strcmp(connection->returnElement->name, "file") == 0) {
1203 entity = mpd_newInfoEntity();
1204 entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1205 entity->info.song = mpd_newSong();
1206 entity->info.song->file = strndup(connection->returnElement->value, text_buffer_size);
1207 } else if (strcmp(connection->returnElement->name, "directory") == 0) {
1208 entity = mpd_newInfoEntity();
1209 entity->type = MPD_INFO_ENTITY_TYPE_DIRECTORY;
1210 entity->info.directory = mpd_newDirectory();
1211 entity->info.directory->path =
1212 strndup(connection->returnElement->value, text_buffer_size);
1213 } else if (strcmp(connection->returnElement->name, "playlist") == 0) {
1214 entity = mpd_newInfoEntity();
1215 entity->type = MPD_INFO_ENTITY_TYPE_PLAYLISTFILE;
1216 entity->info.playlistFile = mpd_newPlaylistFile();
1217 entity->info.playlistFile->path =
1218 strndup(connection->returnElement->value, text_buffer_size);
1219 } else if (strcmp(connection->returnElement->name, "cpos") == 0) {
1220 entity = mpd_newInfoEntity();
1221 entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1222 entity->info.song = mpd_newSong();
1223 entity->info.song->pos = atoi(connection->returnElement->value);
1225 connection->error = 1;
1226 strcpy(connection->errorStr, "problem parsing song info");
1233 mpd_getNextReturnElement(connection);
1234 while (connection->returnElement) {
1235 mpd_ReturnElement *re = connection->returnElement;
1237 if (strcmp(re->name, "file") == 0) {
1239 } else if (strcmp(re->name, "directory") == 0) {
1241 } else if (strcmp(re->name, "playlist") == 0) {
1243 } else if (strcmp(re->name, "cpos") == 0) {
1247 if (entity->type == MPD_INFO_ENTITY_TYPE_SONG && strlen(re->value)) {
1248 if (!entity->info.song->artist
1249 && strcmp(re->name, "Artist") == 0) {
1250 entity->info.song->artist = strndup(re->value, text_buffer_size);
1251 } else if (!entity->info.song->album
1252 && strcmp(re->name, "Album") == 0) {
1253 entity->info.song->album = strndup(re->value, text_buffer_size);
1254 } else if (!entity->info.song->title
1255 && strcmp(re->name, "Title") == 0) {
1256 entity->info.song->title = strndup(re->value, text_buffer_size);
1257 } else if (!entity->info.song->track
1258 && strcmp(re->name, "Track") == 0) {
1259 entity->info.song->track = strndup(re->value, text_buffer_size);
1260 } else if (!entity->info.song->name
1261 && strcmp(re->name, "Name") == 0) {
1262 entity->info.song->name = strndup(re->value, text_buffer_size);
1263 } else if (entity->info.song->time == MPD_SONG_NO_TIME
1264 && strcmp(re->name, "Time") == 0) {
1265 entity->info.song->time = atoi(re->value);
1266 } else if (entity->info.song->pos == MPD_SONG_NO_NUM
1267 && strcmp(re->name, "Pos") == 0) {
1268 entity->info.song->pos = atoi(re->value);
1269 } else if (entity->info.song->id == MPD_SONG_NO_ID
1270 && strcmp(re->name, "Id") == 0) {
1271 entity->info.song->id = atoi(re->value);
1272 } else if (!entity->info.song->date
1273 && strcmp(re->name, "Date") == 0) {
1274 entity->info.song->date = strndup(re->value, text_buffer_size);
1275 } else if (!entity->info.song->genre
1276 && strcmp(re->name, "Genre") == 0) {
1277 entity->info.song->genre = strndup(re->value, text_buffer_size);
1278 } else if (!entity->info.song->composer
1279 && strcmp(re->name, "Composer") == 0) {
1280 entity->info.song->composer = strndup(re->value, text_buffer_size);
1281 } else if (!entity->info.song->performer
1282 && strcmp(re->name, "Performer") == 0) {
1283 entity->info.song->performer = strndup(re->value, text_buffer_size);
1284 } else if (!entity->info.song->disc
1285 && strcmp(re->name, "Disc") == 0) {
1286 entity->info.song->disc = strndup(re->value, text_buffer_size);
1287 } else if (!entity->info.song->comment
1288 && strcmp(re->name, "Comment") == 0) {
1289 entity->info.song->comment = strndup(re->value, text_buffer_size);
1291 } else if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1292 } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1295 mpd_getNextReturnElement(connection);
1301 static char *mpd_getNextReturnElementNamed(mpd_Connection *connection,
1304 if (connection->doneProcessing
1305 || (connection->listOks && connection->doneListOk)) {
1309 mpd_getNextReturnElement(connection);
1310 while (connection->returnElement) {
1311 mpd_ReturnElement *re = connection->returnElement;
1313 if (strcmp(re->name, name) == 0) {
1314 return strndup(re->value, text_buffer_size);
1316 mpd_getNextReturnElement(connection);
1322 char *mpd_getNextTag(mpd_Connection *connection, int type)
1324 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES
1325 || type == MPD_TAG_ITEM_ANY) {
1328 if (type == MPD_TAG_ITEM_FILENAME) {
1329 return mpd_getNextReturnElementNamed(connection, "file");
1331 return mpd_getNextReturnElementNamed(connection, mpdTagItemKeys[type]);
1334 char *mpd_getNextArtist(mpd_Connection *connection)
1336 return mpd_getNextReturnElementNamed(connection, "Artist");
1339 char *mpd_getNextAlbum(mpd_Connection *connection)
1341 return mpd_getNextReturnElementNamed(connection, "Album");
1344 void mpd_sendPlaylistInfoCommand(mpd_Connection *connection, int songPos)
1346 int len = strlen("playlistinfo") + 2 + INTLEN + 3;
1347 char *string = malloc(len);
1349 snprintf(string, len, "playlistinfo \"%i\"\n", songPos);
1350 mpd_sendInfoCommand(connection, string);
1354 void mpd_sendPlaylistIdCommand(mpd_Connection *connection, int id)
1356 int len = strlen("playlistid") + 2 + INTLEN + 3;
1357 char *string = malloc(len);
1359 snprintf(string, len, "playlistid \"%i\"\n", id);
1360 mpd_sendInfoCommand(connection, string);
1364 void mpd_sendPlChangesCommand(mpd_Connection *connection, long long playlist)
1366 int len = strlen("plchanges") + 2 + LONGLONGLEN + 3;
1367 char *string = malloc(len);
1369 snprintf(string, len, "plchanges \"%lld\"\n", playlist);
1370 mpd_sendInfoCommand(connection, string);
1374 void mpd_sendPlChangesPosIdCommand(mpd_Connection *connection,
1377 int len = strlen("plchangesposid") + 2 + LONGLONGLEN + 3;
1378 char *string = malloc(len);
1380 snprintf(string, len, "plchangesposid \"%lld\"\n", playlist);
1381 mpd_sendInfoCommand(connection, string);
1385 void mpd_sendListallCommand(mpd_Connection *connection, const char *dir)
1387 char *sDir = mpd_sanitizeArg(dir);
1388 int len = strlen("listall") + 2 + strlen(sDir) + 3;
1389 char *string = malloc(len);
1391 snprintf(string, len, "listall \"%s\"\n", sDir);
1392 mpd_sendInfoCommand(connection, string);
1397 void mpd_sendListallInfoCommand(mpd_Connection *connection, const char *dir)
1399 char *sDir = mpd_sanitizeArg(dir);
1400 int len = strlen("listallinfo") + 2 + strlen(sDir) + 3;
1401 char *string = malloc(len);
1403 snprintf(string, len, "listallinfo \"%s\"\n", sDir);
1404 mpd_sendInfoCommand(connection, string);
1409 void mpd_sendLsInfoCommand(mpd_Connection *connection, const char *dir)
1411 char *sDir = mpd_sanitizeArg(dir);
1412 int len = strlen("lsinfo") + 2 + strlen(sDir) + 3;
1413 char *string = malloc(len);
1415 snprintf(string, len, "lsinfo \"%s\"\n", sDir);
1416 mpd_sendInfoCommand(connection, string);
1421 void mpd_sendCurrentSongCommand(mpd_Connection *connection)
1423 mpd_executeCommand(connection, "currentsong\n");
1426 void mpd_sendSearchCommand(mpd_Connection *connection, int table,
1429 mpd_startSearch(connection, 0);
1430 mpd_addConstraintSearch(connection, table, str);
1431 mpd_commitSearch(connection);
1434 void mpd_sendFindCommand(mpd_Connection *connection, int table,
1437 mpd_startSearch(connection, 1);
1438 mpd_addConstraintSearch(connection, table, str);
1439 mpd_commitSearch(connection);
1442 void mpd_sendListCommand(mpd_Connection *connection, int table,
1449 if (table == MPD_TABLE_ARTIST) {
1450 strcpy(st, "artist");
1451 } else if (table == MPD_TABLE_ALBUM) {
1452 strcpy(st, "album");
1454 connection->error = 1;
1455 strcpy(connection->errorStr, "unknown table for list");
1459 char *sanitArg1 = mpd_sanitizeArg(arg1);
1461 len = strlen("list") + 1 + strlen(sanitArg1) + 2 + strlen(st) + 3;
1462 string = malloc(len);
1463 snprintf(string, len, "list %s \"%s\"\n", st, sanitArg1);
1466 len = strlen("list") + 1 + strlen(st) + 2;
1467 string = malloc(len);
1468 snprintf(string, len, "list %s\n", st);
1470 mpd_sendInfoCommand(connection, string);
1474 void mpd_sendAddCommand(mpd_Connection *connection, const char *file)
1476 char *sFile = mpd_sanitizeArg(file);
1477 int len = strlen("add") + 2 + strlen(sFile) + 3;
1478 char *string = malloc(len);
1480 snprintf(string, len, "add \"%s\"\n", sFile);
1481 mpd_executeCommand(connection, string);
1486 int mpd_sendAddIdCommand(mpd_Connection *connection, const char *file)
1489 char *sFile = mpd_sanitizeArg(file);
1490 int len = strlen("addid") + 2 + strlen(sFile) + 3;
1491 char *string = malloc(len);
1493 snprintf(string, len, "addid \"%s\"\n", sFile);
1494 mpd_sendInfoCommand(connection, string);
1498 string = mpd_getNextReturnElementNamed(connection, "Id");
1500 retval = atoi(string);
1507 void mpd_sendDeleteCommand(mpd_Connection *connection, int songPos)
1509 int len = strlen("delete") + 2 + INTLEN + 3;
1510 char *string = malloc(len);
1512 snprintf(string, len, "delete \"%i\"\n", songPos);
1513 mpd_sendInfoCommand(connection, string);
1517 void mpd_sendDeleteIdCommand(mpd_Connection *connection, int id)
1519 int len = strlen("deleteid") + 2 + INTLEN + 3;
1520 char *string = malloc(len);
1522 snprintf(string, len, "deleteid \"%i\"\n", id);
1523 mpd_sendInfoCommand(connection, string);
1527 void mpd_sendSaveCommand(mpd_Connection *connection, const char *name)
1529 char *sName = mpd_sanitizeArg(name);
1530 int len = strlen("save") + 2 + strlen(sName) + 3;
1531 char *string = malloc(len);
1533 snprintf(string, len, "save \"%s\"\n", sName);
1534 mpd_executeCommand(connection, string);
1539 void mpd_sendLoadCommand(mpd_Connection *connection, const char *name)
1541 char *sName = mpd_sanitizeArg(name);
1542 int len = strlen("load") + 2 + strlen(sName) + 3;
1543 char *string = malloc(len);
1545 snprintf(string, len, "load \"%s\"\n", sName);
1546 mpd_executeCommand(connection, string);
1551 void mpd_sendRmCommand(mpd_Connection *connection, const char *name)
1553 char *sName = mpd_sanitizeArg(name);
1554 int len = strlen("rm") + 2 + strlen(sName) + 3;
1555 char *string = malloc(len);
1557 snprintf(string, len, "rm \"%s\"\n", sName);
1558 mpd_executeCommand(connection, string);
1563 void mpd_sendRenameCommand(mpd_Connection *connection, const char *from,
1566 char *sFrom = mpd_sanitizeArg(from);
1567 char *sTo = mpd_sanitizeArg(to);
1568 int len = strlen("rename") + 2 + strlen(sFrom) + 3 + strlen(sTo) + 3;
1569 char *string = malloc(len);
1571 snprintf(string, len, "rename \"%s\" \"%s\"\n", sFrom, sTo);
1572 mpd_executeCommand(connection, string);
1578 void mpd_sendShuffleCommand(mpd_Connection *connection)
1580 mpd_executeCommand(connection, "shuffle\n");
1583 void mpd_sendClearCommand(mpd_Connection *connection)
1585 mpd_executeCommand(connection, "clear\n");
1588 void mpd_sendPlayCommand(mpd_Connection *connection, int songPos)
1590 int len = strlen("play") + 2 + INTLEN + 3;
1591 char *string = malloc(len);
1593 snprintf(string, len, "play \"%i\"\n", songPos);
1594 mpd_sendInfoCommand(connection, string);
1598 void mpd_sendPlayIdCommand(mpd_Connection *connection, int id)
1600 int len = strlen("playid") + 2 + INTLEN + 3;
1601 char *string = malloc(len);
1603 snprintf(string, len, "playid \"%i\"\n", id);
1604 mpd_sendInfoCommand(connection, string);
1608 void mpd_sendStopCommand(mpd_Connection *connection)
1610 mpd_executeCommand(connection, "stop\n");
1613 void mpd_sendPauseCommand(mpd_Connection *connection, int pauseMode)
1615 int len = strlen("pause") + 2 + INTLEN + 3;
1616 char *string = malloc(len);
1618 snprintf(string, len, "pause \"%i\"\n", pauseMode);
1619 mpd_executeCommand(connection, string);
1623 void mpd_sendNextCommand(mpd_Connection *connection)
1625 mpd_executeCommand(connection, "next\n");
1628 void mpd_sendMoveCommand(mpd_Connection *connection, int from, int to)
1630 int len = strlen("move") + 2 + INTLEN + 3 + INTLEN + 3;
1631 char *string = malloc(len);
1633 snprintf(string, len, "move \"%i\" \"%i\"\n", from, to);
1634 mpd_sendInfoCommand(connection, string);
1638 void mpd_sendMoveIdCommand(mpd_Connection *connection, int id, int to)
1640 int len = strlen("moveid") + 2 + INTLEN + 3 + INTLEN + 3;
1641 char *string = malloc(len);
1643 snprintf(string, len, "moveid \"%i\" \"%i\"\n", id, to);
1644 mpd_sendInfoCommand(connection, string);
1648 void mpd_sendSwapCommand(mpd_Connection *connection, int song1, int song2)
1650 int len = strlen("swap") + 2 + INTLEN + 3 + INTLEN + 3;
1651 char *string = malloc(len);
1653 snprintf(string, len, "swap \"%i\" \"%i\"\n", song1, song2);
1654 mpd_sendInfoCommand(connection, string);
1658 void mpd_sendSwapIdCommand(mpd_Connection *connection, int id1, int id2)
1660 int len = strlen("swapid") + 2 + INTLEN + 3 + INTLEN + 3;
1661 char *string = malloc(len);
1663 snprintf(string, len, "swapid \"%i\" \"%i\"\n", id1, id2);
1664 mpd_sendInfoCommand(connection, string);
1668 void mpd_sendSeekCommand(mpd_Connection *connection, int song, int seek_time)
1670 int len = strlen("seek") + 2 + INTLEN + 3 + INTLEN + 3;
1671 char *string = malloc(len);
1673 snprintf(string, len, "seek \"%i\" \"%i\"\n", song, seek_time);
1674 mpd_sendInfoCommand(connection, string);
1678 void mpd_sendSeekIdCommand(mpd_Connection *connection, int id, int seek_time)
1680 int len = strlen("seekid") + 2 + INTLEN + 3 + INTLEN + 3;
1681 char *string = malloc(len);
1683 snprintf(string, len, "seekid \"%i\" \"%i\"\n", id, seek_time);
1684 mpd_sendInfoCommand(connection, string);
1688 void mpd_sendUpdateCommand(mpd_Connection *connection, char *path)
1690 char *sPath = mpd_sanitizeArg(path);
1691 int len = strlen("update") + 2 + strlen(sPath) + 3;
1692 char *string = malloc(len);
1694 snprintf(string, len, "update \"%s\"\n", sPath);
1695 mpd_sendInfoCommand(connection, string);
1700 int mpd_getUpdateId(mpd_Connection *connection)
1705 jobid = mpd_getNextReturnElementNamed(connection, "updating_db");
1714 void mpd_sendPrevCommand(mpd_Connection *connection)
1716 mpd_executeCommand(connection, "previous\n");
1719 void mpd_sendRepeatCommand(mpd_Connection *connection, int repeatMode)
1721 int len = strlen("repeat") + 2 + INTLEN + 3;
1722 char *string = malloc(len);
1724 snprintf(string, len, "repeat \"%i\"\n", repeatMode);
1725 mpd_executeCommand(connection, string);
1729 void mpd_sendRandomCommand(mpd_Connection *connection, int randomMode)
1731 int len = strlen("random") + 2 + INTLEN + 3;
1732 char *string = malloc(len);
1734 snprintf(string, len, "random \"%i\"\n", randomMode);
1735 mpd_executeCommand(connection, string);
1739 void mpd_sendSetvolCommand(mpd_Connection *connection, int volumeChange)
1741 int len = strlen("setvol") + 2 + INTLEN + 3;
1742 char *string = malloc(len);
1744 snprintf(string, len, "setvol \"%i\"\n", volumeChange);
1745 mpd_executeCommand(connection, string);
1749 void mpd_sendVolumeCommand(mpd_Connection *connection, int volumeChange)
1751 int len = strlen("volume") + 2 + INTLEN + 3;
1752 char *string = malloc(len);
1754 snprintf(string, len, "volume \"%i\"\n", volumeChange);
1755 mpd_executeCommand(connection, string);
1759 void mpd_sendCrossfadeCommand(mpd_Connection *connection, int seconds)
1761 int len = strlen("crossfade") + 2 + INTLEN + 3;
1762 char *string = malloc(len);
1764 snprintf(string, len, "crossfade \"%i\"\n", seconds);
1765 mpd_executeCommand(connection, string);
1769 void mpd_sendPasswordCommand(mpd_Connection *connection, const char *pass)
1771 char *sPass = mpd_sanitizeArg(pass);
1772 int len = strlen("password") + 2 + strlen(sPass) + 3;
1773 char *string = malloc(len);
1775 snprintf(string, len, "password \"%s\"\n", sPass);
1776 mpd_executeCommand(connection, string);
1781 void mpd_sendCommandListBegin(mpd_Connection *connection)
1783 if (connection->commandList) {
1784 strcpy(connection->errorStr, "already in command list mode");
1785 connection->error = 1;
1788 connection->commandList = COMMAND_LIST;
1789 mpd_executeCommand(connection, "command_list_begin\n");
1792 void mpd_sendCommandListOkBegin(mpd_Connection *connection)
1794 if (connection->commandList) {
1795 strcpy(connection->errorStr, "already in command list mode");
1796 connection->error = 1;
1799 connection->commandList = COMMAND_LIST_OK;
1800 mpd_executeCommand(connection, "command_list_ok_begin\n");
1801 connection->listOks = 0;
1804 void mpd_sendCommandListEnd(mpd_Connection *connection)
1806 if (!connection->commandList) {
1807 strcpy(connection->errorStr, "not in command list mode");
1808 connection->error = 1;
1811 connection->commandList = 0;
1812 mpd_executeCommand(connection, "command_list_end\n");
1815 void mpd_sendOutputsCommand(mpd_Connection *connection)
1817 mpd_executeCommand(connection, "outputs\n");
1820 mpd_OutputEntity *mpd_getNextOutput(mpd_Connection *connection)
1822 mpd_OutputEntity *output = NULL;
1824 if (connection->doneProcessing
1825 || (connection->listOks && connection->doneListOk)) {
1829 if (connection->error) {
1833 output = malloc(sizeof(mpd_OutputEntity));
1835 output->name = NULL;
1836 output->enabled = 0;
1838 if (!connection->returnElement) {
1839 mpd_getNextReturnElement(connection);
1842 while (connection->returnElement) {
1843 mpd_ReturnElement *re = connection->returnElement;
1845 if (strcmp(re->name, "outputid") == 0) {
1846 if (output != NULL && output->id >= 0) {
1849 output->id = atoi(re->value);
1850 } else if (strcmp(re->name, "outputname") == 0) {
1851 output->name = strndup(re->value, text_buffer_size);
1852 } else if (strcmp(re->name, "outputenabled") == 0) {
1853 output->enabled = atoi(re->value);
1856 mpd_getNextReturnElement(connection);
1857 if (connection->error) {
1866 void mpd_sendEnableOutputCommand(mpd_Connection *connection, int outputId)
1868 int len = strlen("enableoutput") + 2 + INTLEN + 3;
1869 char *string = malloc(len);
1871 snprintf(string, len, "enableoutput \"%i\"\n", outputId);
1872 mpd_executeCommand(connection, string);
1876 void mpd_sendDisableOutputCommand(mpd_Connection *connection, int outputId)
1878 int len = strlen("disableoutput") + 2 + INTLEN + 3;
1879 char *string = malloc(len);
1881 snprintf(string, len, "disableoutput \"%i\"\n", outputId);
1882 mpd_executeCommand(connection, string);
1886 void mpd_freeOutputElement(mpd_OutputEntity *output)
1892 /** odd naming, but it gets the not allowed commands */
1893 void mpd_sendNotCommandsCommand(mpd_Connection *connection)
1895 mpd_executeCommand(connection, "notcommands\n");
1898 /** odd naming, but it gets the allowed commands */
1899 void mpd_sendCommandsCommand(mpd_Connection *connection)
1901 mpd_executeCommand(connection, "commands\n");
1904 /** Get the next returned command */
1905 char *mpd_getNextCommand(mpd_Connection *connection)
1907 return mpd_getNextReturnElementNamed(connection, "command");
1910 void mpd_sendUrlHandlersCommand(mpd_Connection *connection)
1912 mpd_executeCommand(connection, "urlhandlers\n");
1915 char *mpd_getNextHandler(mpd_Connection *connection)
1917 return mpd_getNextReturnElementNamed(connection, "handler");
1920 void mpd_sendTagTypesCommand(mpd_Connection *connection)
1922 mpd_executeCommand(connection, "tagtypes\n");
1925 char *mpd_getNextTagType(mpd_Connection *connection)
1927 return mpd_getNextReturnElementNamed(connection, "tagtype");
1930 void mpd_startSearch(mpd_Connection *connection, int exact)
1932 if (connection->request) {
1933 strcpy(connection->errorStr, "search already in progress");
1934 connection->error = 1;
1939 connection->request = strndup("find", text_buffer_size);
1941 connection->request = strndup("search", text_buffer_size);
1945 void mpd_startStatsSearch(mpd_Connection *connection)
1947 if (connection->request) {
1948 strcpy(connection->errorStr, "search already in progress");
1949 connection->error = 1;
1953 connection->request = strndup("count", text_buffer_size);
1956 void mpd_startPlaylistSearch(mpd_Connection *connection, int exact)
1958 if (connection->request) {
1959 strcpy(connection->errorStr, "search already in progress");
1960 connection->error = 1;
1965 connection->request = strndup("playlistfind", text_buffer_size);
1967 connection->request = strndup("playlistsearch", text_buffer_size);
1971 void mpd_startFieldSearch(mpd_Connection *connection, int type)
1973 const char *strtype;
1976 if (connection->request) {
1977 strcpy(connection->errorStr, "search already in progress");
1978 connection->error = 1;
1982 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
1983 strcpy(connection->errorStr, "invalid type specified");
1984 connection->error = 1;
1988 strtype = mpdTagItemKeys[type];
1990 len = 5 + strlen(strtype) + 1;
1991 connection->request = malloc(len);
1993 snprintf(connection->request, len, "list %c%s", tolower(strtype[0]),
1997 void mpd_addConstraintSearch(mpd_Connection *connection, int type,
2000 const char *strtype;
2005 if (!connection->request) {
2006 strcpy(connection->errorStr, "no search in progress");
2007 connection->error = 1;
2011 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
2012 strcpy(connection->errorStr, "invalid type specified");
2013 connection->error = 1;
2018 strcpy(connection->errorStr, "no name specified");
2019 connection->error = 1;
2023 string = strndup(connection->request, text_buffer_size);
2024 strtype = mpdTagItemKeys[type];
2025 arg = mpd_sanitizeArg(name);
2027 len = strlen(string) + 1 + strlen(strtype) + 2 + strlen(arg) + 2;
2028 connection->request = realloc(connection->request, len);
2029 snprintf(connection->request, len, "%s %c%s \"%s\"", string,
2030 tolower(strtype[0]), strtype + 1, arg);
2036 void mpd_commitSearch(mpd_Connection *connection)
2040 if (!connection->request) {
2041 strcpy(connection->errorStr, "no search in progress");
2042 connection->error = 1;
2046 len = strlen(connection->request) + 2;
2047 connection->request = realloc(connection->request, len);
2048 connection->request[len - 2] = '\n';
2049 connection->request[len - 1] = '\0';
2050 mpd_sendInfoCommand(connection, connection->request);
2052 free(connection->request);
2053 connection->request = NULL;
2057 * @param connection a MpdConnection
2058 * @param path the path to the playlist.
2060 * List the content, with full metadata, of a stored playlist. */
2061 void mpd_sendListPlaylistInfoCommand(mpd_Connection *connection, char *path)
2063 char *arg = mpd_sanitizeArg(path);
2064 int len = strlen("listplaylistinfo") + 2 + strlen(arg) + 3;
2065 char *query = malloc(len);
2067 snprintf(query, len, "listplaylistinfo \"%s\"\n", arg);
2068 mpd_sendInfoCommand(connection, query);
2074 * @param connection a MpdConnection
2075 * @param path the path to the playlist.
2077 * List the content of a stored playlist. */
2078 void mpd_sendListPlaylistCommand(mpd_Connection *connection, char *path)
2080 char *arg = mpd_sanitizeArg(path);
2081 int len = strlen("listplaylist") + 2 + strlen(arg) + 3;
2082 char *query = malloc(len);
2084 snprintf(query, len, "listplaylist \"%s\"\n", arg);
2085 mpd_sendInfoCommand(connection, query);
2090 void mpd_sendPlaylistClearCommand(mpd_Connection *connection, char *path)
2092 char *sPath = mpd_sanitizeArg(path);
2093 int len = strlen("playlistclear") + 2 + strlen(sPath) + 3;
2094 char *string = malloc(len);
2096 snprintf(string, len, "playlistclear \"%s\"\n", sPath);
2097 mpd_executeCommand(connection, string);
2102 void mpd_sendPlaylistAddCommand(mpd_Connection *connection, char *playlist,
2105 char *sPlaylist = mpd_sanitizeArg(playlist);
2106 char *sPath = mpd_sanitizeArg(path);
2107 int len = strlen("playlistadd") + 2 + strlen(sPlaylist) + 3 +
2109 char *string = malloc(len);
2111 snprintf(string, len, "playlistadd \"%s\" \"%s\"\n", sPlaylist, sPath);
2112 mpd_executeCommand(connection, string);
2118 void mpd_sendPlaylistMoveCommand(mpd_Connection *connection, char *playlist,
2121 char *sPlaylist = mpd_sanitizeArg(playlist);
2122 int len = strlen("playlistmove") + 2 + strlen(sPlaylist) + 3 + INTLEN +
2124 char *string = malloc(len);
2126 snprintf(string, len, "playlistmove \"%s\" \"%i\" \"%i\"\n", sPlaylist,
2128 mpd_executeCommand(connection, string);
2133 void mpd_sendPlaylistDeleteCommand(mpd_Connection *connection, char *playlist,
2136 char *sPlaylist = mpd_sanitizeArg(playlist);
2137 int len = strlen("playlistdelete") + 2 + strlen(sPlaylist) + 3 +
2139 char *string = malloc(len);
2141 snprintf(string, len, "playlistdelete \"%s\" \"%i\"\n", sPlaylist, pos);
2142 mpd_executeCommand(connection, string);