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;
133 hints.ai_flags = AI_ADDRCONFIG;
134 hints.ai_family = AF_UNSPEC;
135 hints.ai_socktype = SOCK_STREAM;
136 hints.ai_protocol = IPPROTO_TCP;
137 hints.ai_addrlen = 0;
138 hints.ai_addr = NULL;
139 hints.ai_canonname = NULL;
140 hints.ai_next = NULL;
142 snprintf(service, sizeof(service), "%i", port);
144 error = getaddrinfo(host, service, &hints, &addrinfo);
147 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
148 "host \"%s\" not found: %s", host, gai_strerror(error));
149 connection->error = MPD_ERROR_UNKHOST;
153 for (res = addrinfo; res; res = res->ai_next) {
155 if (connection->sock > -1) {
156 closesocket(connection->sock);
158 connection->sock = socket(res->ai_family, SOCK_STREAM,
160 if (connection->sock < 0) {
161 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
162 "problems creating socket: %s", strerror(errno));
163 connection->error = MPD_ERROR_SYSTEM;
164 freeaddrinfo(addrinfo);
168 mpd_setConnectionTimeout(connection, timeout);
171 if (do_connect_fail(connection, res->ai_addr, res->ai_addrlen)) {
172 /* try the next address family */
173 closesocket(connection->sock);
174 connection->sock = -1;
179 freeaddrinfo(addrinfo);
181 if (connection->sock < 0) {
182 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
183 "problems connecting to \"%s\" on port %i: %s", host, port,
185 connection->error = MPD_ERROR_CONNPORT;
192 #else /* !MPD_HAVE_GAI */
193 static int mpd_connect(mpd_Connection *connection, const char *host, int port,
196 struct hostent he, *he_res = 0;
199 struct sockaddr *dest;
201 struct sockaddr_in sin;
203 #ifdef HAVE_GETHOSTBYNAME_R
204 if (gethostbyname_r(rhost, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
205 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
206 "%s ('%s')", hstrerror(h_errno), host);
207 connection->error = MPD_ERROR_UNKHOST;
210 #else /* HAVE_GETHOSTBYNAME_R */
211 if (!(he_res = gethostbyname(host))) {
212 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
213 "host \"%s\" not found", host);
214 connection->error = MPD_ERROR_UNKHOST;
217 #endif /* HAVE_GETHOSTBYNAME_R */
219 memset(&sin, 0, sizeof(struct sockaddr_in));
220 /* dest.sin_family = he_res->h_addrtype; */
221 sin.sin_family = AF_INET;
222 sin.sin_port = htons(port);
224 switch (he_res->h_addrtype) {
226 memcpy((char *) &sin.sin_addr.s_addr, (char *) he_res->h_addr,
228 dest = (struct sockaddr *) &sin;
229 destlen = sizeof(struct sockaddr_in);
232 strcpy(connection->errorStr, "address type is not IPv4");
233 connection->error = MPD_ERROR_SYSTEM;
238 if (connection->sock > -1) {
239 closesocket(connection->sock);
241 if ((connection->sock = socket(dest->sa_family, SOCK_STREAM, 0)) < 0) {
242 strcpy(connection->errorStr, "problems creating socket");
243 connection->error = MPD_ERROR_SYSTEM;
247 mpd_setConnectionTimeout(connection, timeout);
250 if (do_connect_fail(connection, dest, destlen)) {
251 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
252 "problems connecting to \"%s\" on port %i", host, port);
253 connection->error = MPD_ERROR_CONNPORT;
259 #endif /* !MPD_HAVE_GAI */
261 const char *mpdTagItemKeys[MPD_TAG_NUM_OF_ITEM_TYPES] = {
277 static char *mpd_sanitizeArg(const char *arg)
281 register const char *c;
284 /* instead of counting in that loop above,
285 * just use a bit more memory and halve running time */
286 ret = malloc(strlen(arg) * 2 + 1);
290 for (i = strlen(arg) + 1; i != 0; --i) {
291 if (*c == '"' || *c == '\\') {
300 static mpd_ReturnElement *mpd_newReturnElement(const char *name,
303 mpd_ReturnElement *ret = malloc(sizeof(mpd_ReturnElement));
305 ret->name = strndup(name, text_buffer_size);
306 ret->value = strndup(value, text_buffer_size);
311 static void mpd_freeReturnElement(mpd_ReturnElement *re)
318 void mpd_setConnectionTimeout(mpd_Connection *connection, float timeout)
320 connection->timeout.tv_sec = (int) timeout;
321 connection->timeout.tv_usec =
322 (int) ((timeout - connection->timeout.tv_sec) * 1e6 + 0.5);
325 static int mpd_parseWelcome(mpd_Connection *connection, const char *host,
326 int port, /* char *rt, */ char *output)
332 if (strncmp(output, MPD_WELCOME_MESSAGE, strlen(MPD_WELCOME_MESSAGE))) {
333 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
334 "mpd not running on port %i on host \"%s\"", port, host);
335 connection->error = MPD_ERROR_NOTMPD;
339 tmp = &output[strlen(MPD_WELCOME_MESSAGE)];
341 for (i = 0; i < 3; i++) {
343 connection->version[i] = strtol(tmp, &test, 10);
346 if (!tmp || (test[0] != '.' && test[0] != '\0')) {
347 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
348 "error parsing version number at \"%s\"",
349 &output[strlen(MPD_WELCOME_MESSAGE)]);
350 connection->error = MPD_ERROR_NOTMPD;
359 mpd_Connection *mpd_newConnection(const char *host, int port, float timeout)
364 mpd_Connection *connection = malloc(sizeof(mpd_Connection));
368 strcpy(connection->buffer, "");
369 connection->buflen = 0;
370 connection->bufstart = 0;
371 strcpy(connection->errorStr, "");
372 connection->error = 0;
373 connection->doneProcessing = 0;
374 connection->commandList = 0;
375 connection->listOks = 0;
376 connection->doneListOk = 0;
377 connection->sock = -1;
378 connection->returnElement = NULL;
379 connection->request = NULL;
381 if (winsock_dll_error(connection)) {
385 if (mpd_connect(connection, host, port, timeout) < 0) {
389 while (!(rt = strstr(connection->buffer, "\n"))) {
390 tv.tv_sec = connection->timeout.tv_sec;
391 tv.tv_usec = connection->timeout.tv_usec;
393 FD_SET(connection->sock, &fds);
394 if ((err = select(connection->sock + 1, &fds, NULL, NULL, &tv)) == 1) {
397 readed = recv(connection->sock,
398 &(connection->buffer[connection->buflen]),
399 MPD_BUFFER_MAX_LENGTH - connection->buflen, 0);
401 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
402 "problems getting a response from \"%s\" on port %i : %s",
403 host, port, strerror(errno));
404 connection->error = MPD_ERROR_NORESPONSE;
407 connection->buflen += readed;
408 connection->buffer[connection->buflen] = '\0';
409 } else if (err < 0) {
410 if (SELECT_ERRNO_IGNORE) {
413 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
414 "problems connecting to \"%s\" on port %i", host, port);
415 connection->error = MPD_ERROR_CONNPORT;
418 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
419 "timeout in attempting to get a response from \"%s\" on "
420 "port %i", host, port);
421 connection->error = MPD_ERROR_NORESPONSE;
427 output = strndup(connection->buffer, text_buffer_size);
428 strcpy(connection->buffer, rt + 1);
429 connection->buflen = strlen(connection->buffer);
431 if (mpd_parseWelcome(connection, host, port, /* rt, */ output) == 0) {
432 connection->doneProcessing = 1;
440 void mpd_clearError(mpd_Connection *connection)
442 connection->error = 0;
443 connection->errorStr[0] = '\0';
446 void mpd_closeConnection(mpd_Connection *connection)
448 closesocket(connection->sock);
449 if (connection->returnElement) {
450 free(connection->returnElement);
452 if (connection->request) {
453 free(connection->request);
459 static void mpd_executeCommand(mpd_Connection *connection, const char *command)
464 const char *commandPtr = command;
465 int commandLen = strlen(command);
467 if (!connection->doneProcessing && !connection->commandList) {
468 strcpy(connection->errorStr, "not done processing current command");
469 connection->error = 1;
473 mpd_clearError(connection);
476 FD_SET(connection->sock, &fds);
477 tv.tv_sec = connection->timeout.tv_sec;
478 tv.tv_usec = connection->timeout.tv_usec;
480 while ((ret = select(connection->sock + 1, NULL, &fds, NULL, &tv) == 1)
481 || (ret == -1 && SELECT_ERRNO_IGNORE)) {
482 ret = send(connection->sock, commandPtr, commandLen, MSG_DONTWAIT);
484 if (SENDRECV_ERRNO_IGNORE) {
487 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
488 "problems giving command \"%s\"", command);
489 connection->error = MPD_ERROR_SENDING;
496 if (commandLen <= 0) {
501 if (commandLen > 0) {
503 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
504 "timeout sending command \"%s\"", command);
505 connection->error = MPD_ERROR_TIMEOUT;
509 if (!connection->commandList) {
510 connection->doneProcessing = 0;
511 } else if (connection->commandList == COMMAND_LIST_OK) {
512 connection->listOks++;
516 static void mpd_getNextReturnElement(mpd_Connection *connection)
526 char *bufferCheck = NULL;
530 if (connection->returnElement) {
531 mpd_freeReturnElement(connection->returnElement);
533 connection->returnElement = NULL;
535 if (connection->doneProcessing
536 || (connection->listOks && connection->doneListOk)) {
537 strcpy(connection->errorStr,
538 "already done processing current command");
539 connection->error = 1;
543 bufferCheck = connection->buffer + connection->bufstart;
544 while (connection->bufstart >= connection->buflen
545 || !(rt = strchr(bufferCheck, '\n'))) {
546 if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
547 memmove(connection->buffer,
548 connection->buffer + connection->bufstart,
549 connection->buflen - connection->bufstart + 1);
550 connection->buflen -= connection->bufstart;
551 connection->bufstart = 0;
553 if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
554 strcpy(connection->errorStr, "buffer overrun");
555 connection->error = MPD_ERROR_BUFFEROVERRUN;
556 connection->doneProcessing = 1;
557 connection->doneListOk = 0;
560 bufferCheck = connection->buffer + connection->buflen;
561 tv.tv_sec = connection->timeout.tv_sec;
562 tv.tv_usec = connection->timeout.tv_usec;
564 FD_SET(connection->sock, &fds);
565 if ((err = select(connection->sock + 1, &fds, NULL, NULL, &tv) == 1)) {
566 readed = recv(connection->sock,
567 connection->buffer + connection->buflen,
568 MPD_BUFFER_MAX_LENGTH - connection->buflen, MSG_DONTWAIT);
569 if (readed < 0 && SENDRECV_ERRNO_IGNORE) {
573 strcpy(connection->errorStr, "connection closed");
574 connection->error = MPD_ERROR_CONNCLOSED;
575 connection->doneProcessing = 1;
576 connection->doneListOk = 0;
579 connection->buflen += readed;
580 connection->buffer[connection->buflen] = '\0';
581 } else if (err < 0 && SELECT_ERRNO_IGNORE) {
584 strcpy(connection->errorStr, "connection timeout");
585 connection->error = MPD_ERROR_TIMEOUT;
586 connection->doneProcessing = 1;
587 connection->doneListOk = 0;
593 output = connection->buffer + connection->bufstart;
594 connection->bufstart = rt - connection->buffer + 1;
596 if (strcmp(output, "OK") == 0) {
597 if (connection->listOks > 0) {
598 strcpy(connection->errorStr, "expected more list_OK's");
599 connection->error = 1;
601 connection->listOks = 0;
602 connection->doneProcessing = 1;
603 connection->doneListOk = 0;
607 if (strcmp(output, "list_OK") == 0) {
608 if (!connection->listOks) {
609 strcpy(connection->errorStr, "got an unexpected list_OK");
610 connection->error = 1;
612 connection->doneListOk = 1;
613 connection->listOks--;
618 if (strncmp(output, "ACK", strlen("ACK")) == 0) {
623 strcpy(connection->errorStr, output);
624 connection->error = MPD_ERROR_ACK;
625 connection->errorCode = MPD_ACK_ERROR_UNK;
626 connection->errorAt = MPD_ERROR_AT_UNK;
627 connection->doneProcessing = 1;
628 connection->doneListOk = 0;
630 needle = strchr(output, '[');
634 val = strtol(needle + 1, &test, 10);
638 connection->errorCode = val;
639 val = strtol(test + 1, &test, 10);
643 connection->errorAt = val;
647 tok = strchr(output, ':');
656 if (value[0] == ' ') {
657 connection->returnElement = mpd_newReturnElement(name, &(value[1]));
659 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
660 "error parsing: %s:%s", name, value);
661 connection->error = 1;
665 void mpd_finishCommand(mpd_Connection *connection)
667 while (!connection->doneProcessing) {
668 if (connection->doneListOk) {
669 connection->doneListOk = 0;
671 mpd_getNextReturnElement(connection);
675 static void mpd_finishListOkCommand(mpd_Connection *connection)
677 while (!connection->doneProcessing && connection->listOks
678 && !connection->doneListOk) {
679 mpd_getNextReturnElement(connection);
683 int mpd_nextListOkCommand(mpd_Connection *connection)
685 mpd_finishListOkCommand(connection);
686 if (!connection->doneProcessing) {
687 connection->doneListOk = 0;
689 if (connection->listOks == 0 || connection->doneProcessing) {
695 void mpd_sendStatusCommand(mpd_Connection *connection)
697 mpd_executeCommand(connection, "status\n");
700 mpd_Status *mpd_getStatus(mpd_Connection *connection)
704 /* mpd_executeCommand(connection, "status\n");
705 if (connection->error) {
709 if (connection->doneProcessing
710 || (connection->listOks && connection->doneListOk)) {
714 if (!connection->returnElement) {
715 mpd_getNextReturnElement(connection);
718 status = malloc(sizeof(mpd_Status));
722 status->playlist = -1;
723 status->playlistLength = -1;
727 status->elapsedTime = 0;
728 status->totalTime = 0;
730 status->sampleRate = 0;
732 status->channels = 0;
733 status->crossfade = -1;
734 status->error = NULL;
735 status->updatingDb = 0;
737 if (connection->error) {
741 while (connection->returnElement) {
742 mpd_ReturnElement *re = connection->returnElement;
744 if (strcmp(re->name, "volume") == 0) {
745 status->volume = atoi(re->value);
746 } else if (strcmp(re->name, "repeat") == 0) {
747 status->repeat = atoi(re->value);
748 } else if (strcmp(re->name, "random") == 0) {
749 status->random = atoi(re->value);
750 } else if (strcmp(re->name, "playlist") == 0) {
751 status->playlist = strtol(re->value, NULL, 10);
752 } else if (strcmp(re->name, "playlistlength") == 0) {
753 status->playlistLength = atoi(re->value);
754 } else if (strcmp(re->name, "bitrate") == 0) {
755 status->bitRate = atoi(re->value);
756 } else if (strcmp(re->name, "state") == 0) {
757 if (strcmp(re->value, "play") == 0) {
758 status->state = MPD_STATUS_STATE_PLAY;
759 } else if (strcmp(re->value, "stop") == 0) {
760 status->state = MPD_STATUS_STATE_STOP;
761 } else if (strcmp(re->value, "pause") == 0) {
762 status->state = MPD_STATUS_STATE_PAUSE;
764 status->state = MPD_STATUS_STATE_UNKNOWN;
766 } else if (strcmp(re->name, "song") == 0) {
767 status->song = atoi(re->value);
768 } else if (strcmp(re->name, "songid") == 0) {
769 status->songid = atoi(re->value);
770 } else if (strcmp(re->name, "time") == 0) {
771 char *tok = strchr(re->value, ':');
773 /* the second strchr below is a safety check */
774 if (tok && (strchr(tok, 0) > (tok + 1))) {
775 /* atoi stops at the first non-[0-9] char: */
776 status->elapsedTime = atoi(re->value);
777 status->totalTime = atoi(tok + 1);
779 } else if (strcmp(re->name, "error") == 0) {
780 status->error = strndup(re->value, text_buffer_size);
781 } else if (strcmp(re->name, "xfade") == 0) {
782 status->crossfade = atoi(re->value);
783 } else if (strcmp(re->name, "updating_db") == 0) {
784 status->updatingDb = atoi(re->value);
785 } else if (strcmp(re->name, "audio") == 0) {
786 char *tok = strchr(re->value, ':');
788 if (tok && (strchr(tok, 0) > (tok + 1))) {
789 status->sampleRate = atoi(re->value);
790 status->bits = atoi(++tok);
791 tok = strchr(tok, ':');
792 if (tok && (strchr(tok, 0) > (tok + 1))) {
793 status->channels = atoi(tok + 1);
798 mpd_getNextReturnElement(connection);
799 if (connection->error) {
805 if (connection->error) {
808 } else if (status->state < 0) {
809 strcpy(connection->errorStr, "state not found");
810 connection->error = 1;
818 void mpd_freeStatus(mpd_Status *status)
826 void mpd_sendStatsCommand(mpd_Connection *connection)
828 mpd_executeCommand(connection, "stats\n");
831 mpd_Stats *mpd_getStats(mpd_Connection *connection)
835 /* mpd_executeCommand(connection, "stats\n");
836 if (connection->error) {
840 if (connection->doneProcessing
841 || (connection->listOks && connection->doneListOk)) {
845 if (!connection->returnElement) {
846 mpd_getNextReturnElement(connection);
849 stats = malloc(sizeof(mpd_Stats));
850 stats->numberOfArtists = 0;
851 stats->numberOfAlbums = 0;
852 stats->numberOfSongs = 0;
854 stats->dbUpdateTime = 0;
856 stats->dbPlayTime = 0;
858 if (connection->error) {
862 while (connection->returnElement) {
863 mpd_ReturnElement *re = connection->returnElement;
865 if (strcmp(re->name, "artists") == 0) {
866 stats->numberOfArtists = atoi(re->value);
867 } else if (strcmp(re->name, "albums") == 0) {
868 stats->numberOfAlbums = atoi(re->value);
869 } else if (strcmp(re->name, "songs") == 0) {
870 stats->numberOfSongs = atoi(re->value);
871 } else if (strcmp(re->name, "uptime") == 0) {
872 stats->uptime = strtol(re->value, NULL, 10);
873 } else if (strcmp(re->name, "db_update") == 0) {
874 stats->dbUpdateTime = strtol(re->value, NULL, 10);
875 } else if (strcmp(re->name, "playtime") == 0) {
876 stats->playTime = strtol(re->value, NULL, 10);
877 } else if (strcmp(re->name, "db_playtime") == 0) {
878 stats->dbPlayTime = strtol(re->value, NULL, 10);
881 mpd_getNextReturnElement(connection);
882 if (connection->error) {
888 if (connection->error) {
896 void mpd_freeStats(mpd_Stats *stats)
901 mpd_SearchStats *mpd_getSearchStats(mpd_Connection *connection)
903 mpd_SearchStats *stats;
904 mpd_ReturnElement *re;
906 if (connection->doneProcessing
907 || (connection->listOks && connection->doneListOk)) {
911 if (!connection->returnElement) {
912 mpd_getNextReturnElement(connection);
915 if (connection->error) {
919 stats = malloc(sizeof(mpd_SearchStats));
920 stats->numberOfSongs = 0;
923 while (connection->returnElement) {
924 re = connection->returnElement;
926 if (strcmp(re->name, "songs") == 0) {
927 stats->numberOfSongs = atoi(re->value);
928 } else if (strcmp(re->name, "playtime") == 0) {
929 stats->playTime = strtol(re->value, NULL, 10);
932 mpd_getNextReturnElement(connection);
933 if (connection->error) {
939 if (connection->error) {
947 void mpd_freeSearchStats(mpd_SearchStats *stats)
952 static void mpd_initSong(mpd_Song *song)
963 song->composer = NULL;
964 song->performer = NULL;
966 song->comment = NULL;
968 song->time = MPD_SONG_NO_TIME;
969 song->pos = MPD_SONG_NO_NUM;
970 song->id = MPD_SONG_NO_ID;
973 static void mpd_finishSong(mpd_Song *song)
999 if (song->composer) {
1000 free(song->composer);
1005 if (song->comment) {
1006 free(song->comment);
1010 mpd_Song *mpd_newSong(void)
1012 mpd_Song *ret = malloc(sizeof(mpd_Song));
1019 void mpd_freeSong(mpd_Song *song)
1021 mpd_finishSong(song);
1025 mpd_Song *mpd_songDup(mpd_Song *song)
1027 mpd_Song *ret = mpd_newSong();
1030 ret->file = strndup(song->file, text_buffer_size);
1033 ret->artist = strndup(song->artist, text_buffer_size);
1036 ret->album = strndup(song->album, text_buffer_size);
1039 ret->title = strndup(song->title, text_buffer_size);
1042 ret->track = strndup(song->track, text_buffer_size);
1045 ret->name = strndup(song->name, text_buffer_size);
1048 ret->date = strndup(song->date, text_buffer_size);
1051 ret->genre = strndup(song->genre, text_buffer_size);
1053 if (song->composer) {
1054 ret->composer = strndup(song->composer, text_buffer_size);
1057 ret->disc = strndup(song->disc, text_buffer_size);
1059 if (song->comment) {
1060 ret->comment = strndup(song->comment, text_buffer_size);
1062 ret->time = song->time;
1063 ret->pos = song->pos;
1069 static void mpd_initDirectory(mpd_Directory *directory)
1071 directory->path = NULL;
1074 static void mpd_finishDirectory(mpd_Directory *directory)
1076 if (directory->path) {
1077 free(directory->path);
1081 mpd_Directory *mpd_newDirectory(void)
1083 mpd_Directory *directory = malloc(sizeof(mpd_Directory));
1085 mpd_initDirectory(directory);
1090 void mpd_freeDirectory(mpd_Directory *directory)
1092 mpd_finishDirectory(directory);
1097 mpd_Directory *mpd_directoryDup(mpd_Directory *directory)
1099 mpd_Directory *ret = mpd_newDirectory();
1101 if (directory->path) {
1102 ret->path = strndup(directory->path, text_buffer_size);
1108 static void mpd_initPlaylistFile(mpd_PlaylistFile *playlist)
1110 playlist->path = NULL;
1113 static void mpd_finishPlaylistFile(mpd_PlaylistFile *playlist)
1115 if (playlist->path) {
1116 free(playlist->path);
1120 mpd_PlaylistFile *mpd_newPlaylistFile(void)
1122 mpd_PlaylistFile *playlist = malloc(sizeof(mpd_PlaylistFile));
1124 mpd_initPlaylistFile(playlist);
1129 void mpd_freePlaylistFile(mpd_PlaylistFile *playlist)
1131 mpd_finishPlaylistFile(playlist);
1135 mpd_PlaylistFile *mpd_playlistFileDup(mpd_PlaylistFile *playlist)
1137 mpd_PlaylistFile *ret = mpd_newPlaylistFile();
1139 if (playlist->path) {
1140 ret->path = strndup(playlist->path, text_buffer_size);
1146 static void mpd_initInfoEntity(mpd_InfoEntity *entity)
1148 entity->info.directory = NULL;
1151 static void mpd_finishInfoEntity(mpd_InfoEntity *entity)
1153 if (entity->info.directory) {
1154 if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1155 mpd_freeDirectory(entity->info.directory);
1156 } else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
1157 mpd_freeSong(entity->info.song);
1158 } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1159 mpd_freePlaylistFile(entity->info.playlistFile);
1164 mpd_InfoEntity *mpd_newInfoEntity(void)
1166 mpd_InfoEntity *entity = malloc(sizeof(mpd_InfoEntity));
1168 mpd_initInfoEntity(entity);
1173 void mpd_freeInfoEntity(mpd_InfoEntity *entity)
1175 mpd_finishInfoEntity(entity);
1179 static void mpd_sendInfoCommand(mpd_Connection *connection, char *command)
1181 mpd_executeCommand(connection, command);
1184 mpd_InfoEntity *mpd_getNextInfoEntity(mpd_Connection *connection)
1186 mpd_InfoEntity *entity = NULL;
1188 if (connection->doneProcessing
1189 || (connection->listOks && connection->doneListOk)) {
1193 if (!connection->returnElement) {
1194 mpd_getNextReturnElement(connection);
1197 if (connection->returnElement) {
1198 if (strcmp(connection->returnElement->name, "file") == 0) {
1199 entity = mpd_newInfoEntity();
1200 entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1201 entity->info.song = mpd_newSong();
1202 entity->info.song->file = strndup(connection->returnElement->value, text_buffer_size);
1203 } else if (strcmp(connection->returnElement->name, "directory") == 0) {
1204 entity = mpd_newInfoEntity();
1205 entity->type = MPD_INFO_ENTITY_TYPE_DIRECTORY;
1206 entity->info.directory = mpd_newDirectory();
1207 entity->info.directory->path =
1208 strndup(connection->returnElement->value, text_buffer_size);
1209 } else if (strcmp(connection->returnElement->name, "playlist") == 0) {
1210 entity = mpd_newInfoEntity();
1211 entity->type = MPD_INFO_ENTITY_TYPE_PLAYLISTFILE;
1212 entity->info.playlistFile = mpd_newPlaylistFile();
1213 entity->info.playlistFile->path =
1214 strndup(connection->returnElement->value, text_buffer_size);
1215 } else if (strcmp(connection->returnElement->name, "cpos") == 0) {
1216 entity = mpd_newInfoEntity();
1217 entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1218 entity->info.song = mpd_newSong();
1219 entity->info.song->pos = atoi(connection->returnElement->value);
1221 connection->error = 1;
1222 strcpy(connection->errorStr, "problem parsing song info");
1229 mpd_getNextReturnElement(connection);
1230 while (connection->returnElement) {
1231 mpd_ReturnElement *re = connection->returnElement;
1233 if (strcmp(re->name, "file") == 0) {
1235 } else if (strcmp(re->name, "directory") == 0) {
1237 } else if (strcmp(re->name, "playlist") == 0) {
1239 } else if (strcmp(re->name, "cpos") == 0) {
1243 if (entity->type == MPD_INFO_ENTITY_TYPE_SONG && strlen(re->value)) {
1244 if (!entity->info.song->artist
1245 && strcmp(re->name, "Artist") == 0) {
1246 entity->info.song->artist = strndup(re->value, text_buffer_size);
1247 } else if (!entity->info.song->album
1248 && strcmp(re->name, "Album") == 0) {
1249 entity->info.song->album = strndup(re->value, text_buffer_size);
1250 } else if (!entity->info.song->title
1251 && strcmp(re->name, "Title") == 0) {
1252 entity->info.song->title = strndup(re->value, text_buffer_size);
1253 } else if (!entity->info.song->track
1254 && strcmp(re->name, "Track") == 0) {
1255 entity->info.song->track = strndup(re->value, text_buffer_size);
1256 } else if (!entity->info.song->name
1257 && strcmp(re->name, "Name") == 0) {
1258 entity->info.song->name = strndup(re->value, text_buffer_size);
1259 } else if (entity->info.song->time == MPD_SONG_NO_TIME
1260 && strcmp(re->name, "Time") == 0) {
1261 entity->info.song->time = atoi(re->value);
1262 } else if (entity->info.song->pos == MPD_SONG_NO_NUM
1263 && strcmp(re->name, "Pos") == 0) {
1264 entity->info.song->pos = atoi(re->value);
1265 } else if (entity->info.song->id == MPD_SONG_NO_ID
1266 && strcmp(re->name, "Id") == 0) {
1267 entity->info.song->id = atoi(re->value);
1268 } else if (!entity->info.song->date
1269 && strcmp(re->name, "Date") == 0) {
1270 entity->info.song->date = strndup(re->value, text_buffer_size);
1271 } else if (!entity->info.song->genre
1272 && strcmp(re->name, "Genre") == 0) {
1273 entity->info.song->genre = strndup(re->value, text_buffer_size);
1274 } else if (!entity->info.song->composer
1275 && strcmp(re->name, "Composer") == 0) {
1276 entity->info.song->composer = strndup(re->value, text_buffer_size);
1277 } else if (!entity->info.song->performer
1278 && strcmp(re->name, "Performer") == 0) {
1279 entity->info.song->performer = strndup(re->value, text_buffer_size);
1280 } else if (!entity->info.song->disc
1281 && strcmp(re->name, "Disc") == 0) {
1282 entity->info.song->disc = strndup(re->value, text_buffer_size);
1283 } else if (!entity->info.song->comment
1284 && strcmp(re->name, "Comment") == 0) {
1285 entity->info.song->comment = strndup(re->value, text_buffer_size);
1287 } else if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1288 } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1291 mpd_getNextReturnElement(connection);
1297 static char *mpd_getNextReturnElementNamed(mpd_Connection *connection,
1300 if (connection->doneProcessing
1301 || (connection->listOks && connection->doneListOk)) {
1305 mpd_getNextReturnElement(connection);
1306 while (connection->returnElement) {
1307 mpd_ReturnElement *re = connection->returnElement;
1309 if (strcmp(re->name, name) == 0) {
1310 return strndup(re->value, text_buffer_size);
1312 mpd_getNextReturnElement(connection);
1318 char *mpd_getNextTag(mpd_Connection *connection, int type)
1320 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES
1321 || type == MPD_TAG_ITEM_ANY) {
1324 if (type == MPD_TAG_ITEM_FILENAME) {
1325 return mpd_getNextReturnElementNamed(connection, "file");
1327 return mpd_getNextReturnElementNamed(connection, mpdTagItemKeys[type]);
1330 char *mpd_getNextArtist(mpd_Connection *connection)
1332 return mpd_getNextReturnElementNamed(connection, "Artist");
1335 char *mpd_getNextAlbum(mpd_Connection *connection)
1337 return mpd_getNextReturnElementNamed(connection, "Album");
1340 void mpd_sendPlaylistInfoCommand(mpd_Connection *connection, int songPos)
1342 int len = strlen("playlistinfo") + 2 + INTLEN + 3;
1343 char *string = malloc(len);
1345 snprintf(string, len, "playlistinfo \"%i\"\n", songPos);
1346 mpd_sendInfoCommand(connection, string);
1350 void mpd_sendPlaylistIdCommand(mpd_Connection *connection, int id)
1352 int len = strlen("playlistid") + 2 + INTLEN + 3;
1353 char *string = malloc(len);
1355 snprintf(string, len, "playlistid \"%i\"\n", id);
1356 mpd_sendInfoCommand(connection, string);
1360 void mpd_sendPlChangesCommand(mpd_Connection *connection, long long playlist)
1362 int len = strlen("plchanges") + 2 + LONGLONGLEN + 3;
1363 char *string = malloc(len);
1365 snprintf(string, len, "plchanges \"%lld\"\n", playlist);
1366 mpd_sendInfoCommand(connection, string);
1370 void mpd_sendPlChangesPosIdCommand(mpd_Connection *connection,
1373 int len = strlen("plchangesposid") + 2 + LONGLONGLEN + 3;
1374 char *string = malloc(len);
1376 snprintf(string, len, "plchangesposid \"%lld\"\n", playlist);
1377 mpd_sendInfoCommand(connection, string);
1381 void mpd_sendListallCommand(mpd_Connection *connection, const char *dir)
1383 char *sDir = mpd_sanitizeArg(dir);
1384 int len = strlen("listall") + 2 + strlen(sDir) + 3;
1385 char *string = malloc(len);
1387 snprintf(string, len, "listall \"%s\"\n", sDir);
1388 mpd_sendInfoCommand(connection, string);
1393 void mpd_sendListallInfoCommand(mpd_Connection *connection, const char *dir)
1395 char *sDir = mpd_sanitizeArg(dir);
1396 int len = strlen("listallinfo") + 2 + strlen(sDir) + 3;
1397 char *string = malloc(len);
1399 snprintf(string, len, "listallinfo \"%s\"\n", sDir);
1400 mpd_sendInfoCommand(connection, string);
1405 void mpd_sendLsInfoCommand(mpd_Connection *connection, const char *dir)
1407 char *sDir = mpd_sanitizeArg(dir);
1408 int len = strlen("lsinfo") + 2 + strlen(sDir) + 3;
1409 char *string = malloc(len);
1411 snprintf(string, len, "lsinfo \"%s\"\n", sDir);
1412 mpd_sendInfoCommand(connection, string);
1417 void mpd_sendCurrentSongCommand(mpd_Connection *connection)
1419 mpd_executeCommand(connection, "currentsong\n");
1422 void mpd_sendSearchCommand(mpd_Connection *connection, int table,
1425 mpd_startSearch(connection, 0);
1426 mpd_addConstraintSearch(connection, table, str);
1427 mpd_commitSearch(connection);
1430 void mpd_sendFindCommand(mpd_Connection *connection, int table,
1433 mpd_startSearch(connection, 1);
1434 mpd_addConstraintSearch(connection, table, str);
1435 mpd_commitSearch(connection);
1438 void mpd_sendListCommand(mpd_Connection *connection, int table,
1445 if (table == MPD_TABLE_ARTIST) {
1446 strcpy(st, "artist");
1447 } else if (table == MPD_TABLE_ALBUM) {
1448 strcpy(st, "album");
1450 connection->error = 1;
1451 strcpy(connection->errorStr, "unknown table for list");
1455 char *sanitArg1 = mpd_sanitizeArg(arg1);
1457 len = strlen("list") + 1 + strlen(sanitArg1) + 2 + strlen(st) + 3;
1458 string = malloc(len);
1459 snprintf(string, len, "list %s \"%s\"\n", st, sanitArg1);
1462 len = strlen("list") + 1 + strlen(st) + 2;
1463 string = malloc(len);
1464 snprintf(string, len, "list %s\n", st);
1466 mpd_sendInfoCommand(connection, string);
1470 void mpd_sendAddCommand(mpd_Connection *connection, const char *file)
1472 char *sFile = mpd_sanitizeArg(file);
1473 int len = strlen("add") + 2 + strlen(sFile) + 3;
1474 char *string = malloc(len);
1476 snprintf(string, len, "add \"%s\"\n", sFile);
1477 mpd_executeCommand(connection, string);
1482 int mpd_sendAddIdCommand(mpd_Connection *connection, const char *file)
1485 char *sFile = mpd_sanitizeArg(file);
1486 int len = strlen("addid") + 2 + strlen(sFile) + 3;
1487 char *string = malloc(len);
1489 snprintf(string, len, "addid \"%s\"\n", sFile);
1490 mpd_sendInfoCommand(connection, string);
1494 string = mpd_getNextReturnElementNamed(connection, "Id");
1496 retval = atoi(string);
1503 void mpd_sendDeleteCommand(mpd_Connection *connection, int songPos)
1505 int len = strlen("delete") + 2 + INTLEN + 3;
1506 char *string = malloc(len);
1508 snprintf(string, len, "delete \"%i\"\n", songPos);
1509 mpd_sendInfoCommand(connection, string);
1513 void mpd_sendDeleteIdCommand(mpd_Connection *connection, int id)
1515 int len = strlen("deleteid") + 2 + INTLEN + 3;
1516 char *string = malloc(len);
1518 snprintf(string, len, "deleteid \"%i\"\n", id);
1519 mpd_sendInfoCommand(connection, string);
1523 void mpd_sendSaveCommand(mpd_Connection *connection, const char *name)
1525 char *sName = mpd_sanitizeArg(name);
1526 int len = strlen("save") + 2 + strlen(sName) + 3;
1527 char *string = malloc(len);
1529 snprintf(string, len, "save \"%s\"\n", sName);
1530 mpd_executeCommand(connection, string);
1535 void mpd_sendLoadCommand(mpd_Connection *connection, const char *name)
1537 char *sName = mpd_sanitizeArg(name);
1538 int len = strlen("load") + 2 + strlen(sName) + 3;
1539 char *string = malloc(len);
1541 snprintf(string, len, "load \"%s\"\n", sName);
1542 mpd_executeCommand(connection, string);
1547 void mpd_sendRmCommand(mpd_Connection *connection, const char *name)
1549 char *sName = mpd_sanitizeArg(name);
1550 int len = strlen("rm") + 2 + strlen(sName) + 3;
1551 char *string = malloc(len);
1553 snprintf(string, len, "rm \"%s\"\n", sName);
1554 mpd_executeCommand(connection, string);
1559 void mpd_sendRenameCommand(mpd_Connection *connection, const char *from,
1562 char *sFrom = mpd_sanitizeArg(from);
1563 char *sTo = mpd_sanitizeArg(to);
1564 int len = strlen("rename") + 2 + strlen(sFrom) + 3 + strlen(sTo) + 3;
1565 char *string = malloc(len);
1567 snprintf(string, len, "rename \"%s\" \"%s\"\n", sFrom, sTo);
1568 mpd_executeCommand(connection, string);
1574 void mpd_sendShuffleCommand(mpd_Connection *connection)
1576 mpd_executeCommand(connection, "shuffle\n");
1579 void mpd_sendClearCommand(mpd_Connection *connection)
1581 mpd_executeCommand(connection, "clear\n");
1584 void mpd_sendPlayCommand(mpd_Connection *connection, int songPos)
1586 int len = strlen("play") + 2 + INTLEN + 3;
1587 char *string = malloc(len);
1589 snprintf(string, len, "play \"%i\"\n", songPos);
1590 mpd_sendInfoCommand(connection, string);
1594 void mpd_sendPlayIdCommand(mpd_Connection *connection, int id)
1596 int len = strlen("playid") + 2 + INTLEN + 3;
1597 char *string = malloc(len);
1599 snprintf(string, len, "playid \"%i\"\n", id);
1600 mpd_sendInfoCommand(connection, string);
1604 void mpd_sendStopCommand(mpd_Connection *connection)
1606 mpd_executeCommand(connection, "stop\n");
1609 void mpd_sendPauseCommand(mpd_Connection *connection, int pauseMode)
1611 int len = strlen("pause") + 2 + INTLEN + 3;
1612 char *string = malloc(len);
1614 snprintf(string, len, "pause \"%i\"\n", pauseMode);
1615 mpd_executeCommand(connection, string);
1619 void mpd_sendNextCommand(mpd_Connection *connection)
1621 mpd_executeCommand(connection, "next\n");
1624 void mpd_sendMoveCommand(mpd_Connection *connection, int from, int to)
1626 int len = strlen("move") + 2 + INTLEN + 3 + INTLEN + 3;
1627 char *string = malloc(len);
1629 snprintf(string, len, "move \"%i\" \"%i\"\n", from, to);
1630 mpd_sendInfoCommand(connection, string);
1634 void mpd_sendMoveIdCommand(mpd_Connection *connection, int id, int to)
1636 int len = strlen("moveid") + 2 + INTLEN + 3 + INTLEN + 3;
1637 char *string = malloc(len);
1639 snprintf(string, len, "moveid \"%i\" \"%i\"\n", id, to);
1640 mpd_sendInfoCommand(connection, string);
1644 void mpd_sendSwapCommand(mpd_Connection *connection, int song1, int song2)
1646 int len = strlen("swap") + 2 + INTLEN + 3 + INTLEN + 3;
1647 char *string = malloc(len);
1649 snprintf(string, len, "swap \"%i\" \"%i\"\n", song1, song2);
1650 mpd_sendInfoCommand(connection, string);
1654 void mpd_sendSwapIdCommand(mpd_Connection *connection, int id1, int id2)
1656 int len = strlen("swapid") + 2 + INTLEN + 3 + INTLEN + 3;
1657 char *string = malloc(len);
1659 snprintf(string, len, "swapid \"%i\" \"%i\"\n", id1, id2);
1660 mpd_sendInfoCommand(connection, string);
1664 void mpd_sendSeekCommand(mpd_Connection *connection, int song, int seek_time)
1666 int len = strlen("seek") + 2 + INTLEN + 3 + INTLEN + 3;
1667 char *string = malloc(len);
1669 snprintf(string, len, "seek \"%i\" \"%i\"\n", song, seek_time);
1670 mpd_sendInfoCommand(connection, string);
1674 void mpd_sendSeekIdCommand(mpd_Connection *connection, int id, int seek_time)
1676 int len = strlen("seekid") + 2 + INTLEN + 3 + INTLEN + 3;
1677 char *string = malloc(len);
1679 snprintf(string, len, "seekid \"%i\" \"%i\"\n", id, seek_time);
1680 mpd_sendInfoCommand(connection, string);
1684 void mpd_sendUpdateCommand(mpd_Connection *connection, char *path)
1686 char *sPath = mpd_sanitizeArg(path);
1687 int len = strlen("update") + 2 + strlen(sPath) + 3;
1688 char *string = malloc(len);
1690 snprintf(string, len, "update \"%s\"\n", sPath);
1691 mpd_sendInfoCommand(connection, string);
1696 int mpd_getUpdateId(mpd_Connection *connection)
1701 jobid = mpd_getNextReturnElementNamed(connection, "updating_db");
1710 void mpd_sendPrevCommand(mpd_Connection *connection)
1712 mpd_executeCommand(connection, "previous\n");
1715 void mpd_sendRepeatCommand(mpd_Connection *connection, int repeatMode)
1717 int len = strlen("repeat") + 2 + INTLEN + 3;
1718 char *string = malloc(len);
1720 snprintf(string, len, "repeat \"%i\"\n", repeatMode);
1721 mpd_executeCommand(connection, string);
1725 void mpd_sendRandomCommand(mpd_Connection *connection, int randomMode)
1727 int len = strlen("random") + 2 + INTLEN + 3;
1728 char *string = malloc(len);
1730 snprintf(string, len, "random \"%i\"\n", randomMode);
1731 mpd_executeCommand(connection, string);
1735 void mpd_sendSetvolCommand(mpd_Connection *connection, int volumeChange)
1737 int len = strlen("setvol") + 2 + INTLEN + 3;
1738 char *string = malloc(len);
1740 snprintf(string, len, "setvol \"%i\"\n", volumeChange);
1741 mpd_executeCommand(connection, string);
1745 void mpd_sendVolumeCommand(mpd_Connection *connection, int volumeChange)
1747 int len = strlen("volume") + 2 + INTLEN + 3;
1748 char *string = malloc(len);
1750 snprintf(string, len, "volume \"%i\"\n", volumeChange);
1751 mpd_executeCommand(connection, string);
1755 void mpd_sendCrossfadeCommand(mpd_Connection *connection, int seconds)
1757 int len = strlen("crossfade") + 2 + INTLEN + 3;
1758 char *string = malloc(len);
1760 snprintf(string, len, "crossfade \"%i\"\n", seconds);
1761 mpd_executeCommand(connection, string);
1765 void mpd_sendPasswordCommand(mpd_Connection *connection, const char *pass)
1767 char *sPass = mpd_sanitizeArg(pass);
1768 int len = strlen("password") + 2 + strlen(sPass) + 3;
1769 char *string = malloc(len);
1771 snprintf(string, len, "password \"%s\"\n", sPass);
1772 mpd_executeCommand(connection, string);
1777 void mpd_sendCommandListBegin(mpd_Connection *connection)
1779 if (connection->commandList) {
1780 strcpy(connection->errorStr, "already in command list mode");
1781 connection->error = 1;
1784 connection->commandList = COMMAND_LIST;
1785 mpd_executeCommand(connection, "command_list_begin\n");
1788 void mpd_sendCommandListOkBegin(mpd_Connection *connection)
1790 if (connection->commandList) {
1791 strcpy(connection->errorStr, "already in command list mode");
1792 connection->error = 1;
1795 connection->commandList = COMMAND_LIST_OK;
1796 mpd_executeCommand(connection, "command_list_ok_begin\n");
1797 connection->listOks = 0;
1800 void mpd_sendCommandListEnd(mpd_Connection *connection)
1802 if (!connection->commandList) {
1803 strcpy(connection->errorStr, "not in command list mode");
1804 connection->error = 1;
1807 connection->commandList = 0;
1808 mpd_executeCommand(connection, "command_list_end\n");
1811 void mpd_sendOutputsCommand(mpd_Connection *connection)
1813 mpd_executeCommand(connection, "outputs\n");
1816 mpd_OutputEntity *mpd_getNextOutput(mpd_Connection *connection)
1818 mpd_OutputEntity *output = NULL;
1820 if (connection->doneProcessing
1821 || (connection->listOks && connection->doneListOk)) {
1825 if (connection->error) {
1829 output = malloc(sizeof(mpd_OutputEntity));
1831 output->name = NULL;
1832 output->enabled = 0;
1834 if (!connection->returnElement) {
1835 mpd_getNextReturnElement(connection);
1838 while (connection->returnElement) {
1839 mpd_ReturnElement *re = connection->returnElement;
1841 if (strcmp(re->name, "outputid") == 0) {
1842 if (output != NULL && output->id >= 0) {
1845 output->id = atoi(re->value);
1846 } else if (strcmp(re->name, "outputname") == 0) {
1847 output->name = strndup(re->value, text_buffer_size);
1848 } else if (strcmp(re->name, "outputenabled") == 0) {
1849 output->enabled = atoi(re->value);
1852 mpd_getNextReturnElement(connection);
1853 if (connection->error) {
1862 void mpd_sendEnableOutputCommand(mpd_Connection *connection, int outputId)
1864 int len = strlen("enableoutput") + 2 + INTLEN + 3;
1865 char *string = malloc(len);
1867 snprintf(string, len, "enableoutput \"%i\"\n", outputId);
1868 mpd_executeCommand(connection, string);
1872 void mpd_sendDisableOutputCommand(mpd_Connection *connection, int outputId)
1874 int len = strlen("disableoutput") + 2 + INTLEN + 3;
1875 char *string = malloc(len);
1877 snprintf(string, len, "disableoutput \"%i\"\n", outputId);
1878 mpd_executeCommand(connection, string);
1882 void mpd_freeOutputElement(mpd_OutputEntity *output)
1888 /** odd naming, but it gets the not allowed commands */
1889 void mpd_sendNotCommandsCommand(mpd_Connection *connection)
1891 mpd_executeCommand(connection, "notcommands\n");
1894 /** odd naming, but it gets the allowed commands */
1895 void mpd_sendCommandsCommand(mpd_Connection *connection)
1897 mpd_executeCommand(connection, "commands\n");
1900 /** Get the next returned command */
1901 char *mpd_getNextCommand(mpd_Connection *connection)
1903 return mpd_getNextReturnElementNamed(connection, "command");
1906 void mpd_sendUrlHandlersCommand(mpd_Connection *connection)
1908 mpd_executeCommand(connection, "urlhandlers\n");
1911 char *mpd_getNextHandler(mpd_Connection *connection)
1913 return mpd_getNextReturnElementNamed(connection, "handler");
1916 void mpd_sendTagTypesCommand(mpd_Connection *connection)
1918 mpd_executeCommand(connection, "tagtypes\n");
1921 char *mpd_getNextTagType(mpd_Connection *connection)
1923 return mpd_getNextReturnElementNamed(connection, "tagtype");
1926 void mpd_startSearch(mpd_Connection *connection, int exact)
1928 if (connection->request) {
1929 strcpy(connection->errorStr, "search already in progress");
1930 connection->error = 1;
1935 connection->request = strndup("find", text_buffer_size);
1937 connection->request = strndup("search", text_buffer_size);
1941 void mpd_startStatsSearch(mpd_Connection *connection)
1943 if (connection->request) {
1944 strcpy(connection->errorStr, "search already in progress");
1945 connection->error = 1;
1949 connection->request = strndup("count", text_buffer_size);
1952 void mpd_startPlaylistSearch(mpd_Connection *connection, int exact)
1954 if (connection->request) {
1955 strcpy(connection->errorStr, "search already in progress");
1956 connection->error = 1;
1961 connection->request = strndup("playlistfind", text_buffer_size);
1963 connection->request = strndup("playlistsearch", text_buffer_size);
1967 void mpd_startFieldSearch(mpd_Connection *connection, int type)
1969 const char *strtype;
1972 if (connection->request) {
1973 strcpy(connection->errorStr, "search already in progress");
1974 connection->error = 1;
1978 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
1979 strcpy(connection->errorStr, "invalid type specified");
1980 connection->error = 1;
1984 strtype = mpdTagItemKeys[type];
1986 len = 5 + strlen(strtype) + 1;
1987 connection->request = malloc(len);
1989 snprintf(connection->request, len, "list %c%s", tolower(strtype[0]),
1993 void mpd_addConstraintSearch(mpd_Connection *connection, int type,
1996 const char *strtype;
2001 if (!connection->request) {
2002 strcpy(connection->errorStr, "no search in progress");
2003 connection->error = 1;
2007 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
2008 strcpy(connection->errorStr, "invalid type specified");
2009 connection->error = 1;
2014 strcpy(connection->errorStr, "no name specified");
2015 connection->error = 1;
2019 string = strndup(connection->request, text_buffer_size);
2020 strtype = mpdTagItemKeys[type];
2021 arg = mpd_sanitizeArg(name);
2023 len = strlen(string) + 1 + strlen(strtype) + 2 + strlen(arg) + 2;
2024 connection->request = realloc(connection->request, len);
2025 snprintf(connection->request, len, "%s %c%s \"%s\"", string,
2026 tolower(strtype[0]), strtype + 1, arg);
2032 void mpd_commitSearch(mpd_Connection *connection)
2036 if (!connection->request) {
2037 strcpy(connection->errorStr, "no search in progress");
2038 connection->error = 1;
2042 len = strlen(connection->request) + 2;
2043 connection->request = realloc(connection->request, len);
2044 connection->request[len - 2] = '\n';
2045 connection->request[len - 1] = '\0';
2046 mpd_sendInfoCommand(connection, connection->request);
2048 free(connection->request);
2049 connection->request = NULL;
2053 * @param connection a MpdConnection
2054 * @param path the path to the playlist.
2056 * List the content, with full metadata, of a stored playlist. */
2057 void mpd_sendListPlaylistInfoCommand(mpd_Connection *connection, char *path)
2059 char *arg = mpd_sanitizeArg(path);
2060 int len = strlen("listplaylistinfo") + 2 + strlen(arg) + 3;
2061 char *query = malloc(len);
2063 snprintf(query, len, "listplaylistinfo \"%s\"\n", arg);
2064 mpd_sendInfoCommand(connection, query);
2070 * @param connection a MpdConnection
2071 * @param path the path to the playlist.
2073 * List the content of a stored playlist. */
2074 void mpd_sendListPlaylistCommand(mpd_Connection *connection, char *path)
2076 char *arg = mpd_sanitizeArg(path);
2077 int len = strlen("listplaylist") + 2 + strlen(arg) + 3;
2078 char *query = malloc(len);
2080 snprintf(query, len, "listplaylist \"%s\"\n", arg);
2081 mpd_sendInfoCommand(connection, query);
2086 void mpd_sendPlaylistClearCommand(mpd_Connection *connection, char *path)
2088 char *sPath = mpd_sanitizeArg(path);
2089 int len = strlen("playlistclear") + 2 + strlen(sPath) + 3;
2090 char *string = malloc(len);
2092 snprintf(string, len, "playlistclear \"%s\"\n", sPath);
2093 mpd_executeCommand(connection, string);
2098 void mpd_sendPlaylistAddCommand(mpd_Connection *connection, char *playlist,
2101 char *sPlaylist = mpd_sanitizeArg(playlist);
2102 char *sPath = mpd_sanitizeArg(path);
2103 int len = strlen("playlistadd") + 2 + strlen(sPlaylist) + 3 +
2105 char *string = malloc(len);
2107 snprintf(string, len, "playlistadd \"%s\" \"%s\"\n", sPlaylist, sPath);
2108 mpd_executeCommand(connection, string);
2114 void mpd_sendPlaylistMoveCommand(mpd_Connection *connection, char *playlist,
2117 char *sPlaylist = mpd_sanitizeArg(playlist);
2118 int len = strlen("playlistmove") + 2 + strlen(sPlaylist) + 3 + INTLEN +
2120 char *string = malloc(len);
2122 snprintf(string, len, "playlistmove \"%s\" \"%i\" \"%i\"\n", sPlaylist,
2124 mpd_executeCommand(connection, string);
2129 void mpd_sendPlaylistDeleteCommand(mpd_Connection *connection, char *playlist,
2132 char *sPlaylist = mpd_sanitizeArg(playlist);
2133 int len = strlen("playlistdelete") + 2 + strlen(sPlaylist) + 3 +
2135 char *string = malloc(len);
2137 snprintf(string, len, "playlistdelete \"%s\" \"%i\"\n", sPlaylist, pos);
2138 mpd_executeCommand(connection, string);