this patch cleans up lcd4linux deps. (#3521)
[openwrt/svn-archive/archive.git] / utils / lcd4linux / patches / 150-addlibmpdclient.patch
1 diff -urN lcd4linux-r870.orig/libmpdclient.c lcd4linux-r870/libmpdclient.c
2 --- lcd4linux-r870.orig/libmpdclient.c 1970-01-01 01:00:00.000000000 +0100
3 +++ lcd4linux-r870/libmpdclient.c 2008-03-26 04:43:31.000000000 +0100
4 @@ -0,0 +1,1957 @@
5 +/* libmpdclient
6 + (c)2003-2006 by Warren Dukes (warren.dukes@gmail.com)
7 + This project's homepage is: http://www.musicpd.org
8 +
9 + Redistribution and use in source and binary forms, with or without
10 + modification, are permitted provided that the following conditions
11 + are met:
12 +
13 + - Redistributions of source code must retain the above copyright
14 + notice, this list of conditions and the following disclaimer.
15 +
16 + - Redistributions in binary form must reproduce the above copyright
17 + notice, this list of conditions and the following disclaimer in the
18 + documentation and/or other materials provided with the distribution.
19 +
20 + - Neither the name of the Music Player Daemon nor the names of its
21 + contributors may be used to endorse or promote products derived from
22 + this software without specific prior written permission.
23 +
24 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
28 + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 +*/
36 +
37 +#include "libmpdclient.h"
38 +
39 +#include <errno.h>
40 +#include <ctype.h>
41 +#include <sys/types.h>
42 +#include <stdio.h>
43 +#include <sys/param.h>
44 +#include <string.h>
45 +#include <unistd.h>
46 +#include <stdlib.h>
47 +#include <fcntl.h>
48 +#include <limits.h>
49 +
50 +#ifdef WIN32
51 +# include <ws2tcpip.h>
52 +# include <winsock.h>
53 +#else
54 +# include <netinet/in.h>
55 +# include <arpa/inet.h>
56 +# include <sys/socket.h>
57 +# include <netdb.h>
58 +#endif
59 +
60 +/* (bits+1)/3 (plus the sign character) */
61 +#define INTLEN ((sizeof(int) * CHAR_BIT + 1) / 3 + 1)
62 +#define LONGLONGLEN ((sizeof(long long) * CHAR_BIT + 1) / 3 + 1)
63 +
64 +#define COMMAND_LIST 1
65 +#define COMMAND_LIST_OK 2
66 +
67 +#ifndef MPD_NO_GAI
68 +# ifdef AI_ADDRCONFIG
69 +# define MPD_HAVE_GAI
70 +# endif
71 +#endif
72 +
73 +#ifndef MSG_DONTWAIT
74 +# define MSG_DONTWAIT 0
75 +#endif
76 +
77 +#ifdef WIN32
78 +# define SELECT_ERRNO_IGNORE (errno == WSAEINTR || errno == WSAEINPROGRESS)
79 +# define SENDRECV_ERRNO_IGNORE SELECT_ERRNO_IGNORE
80 +#else
81 +# define SELECT_ERRNO_IGNORE (errno == EINTR)
82 +# define SENDRECV_ERRNO_IGNORE (errno == EINTR || errno == EAGAIN)
83 +# define winsock_dll_error(c) 0
84 +# define closesocket(s) close(s)
85 +# define WSACleanup() do { /* nothing */ } while (0)
86 +#endif
87 +
88 +#ifdef WIN32
89 +static int winsock_dll_error(mpd_Connection * connection)
90 +{
91 + WSADATA wsaData;
92 + if ((WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0 || LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
93 + strcpy(connection->errorStr, "Could not find usable WinSock DLL.");
94 + connection->error = MPD_ERROR_SYSTEM;
95 + return 1;
96 + }
97 + return 0;
98 +}
99 +
100 +static int do_connect_fail(mpd_Connection * connection, const struct sockaddr *serv_addr, int addrlen)
101 +{
102 + int iMode = 1; /* 0 = blocking, else non-blocking */
103 + ioctlsocket(connection->sock, FIONBIO, (u_long FAR *) & iMode);
104 + return (connect(connection->sock, serv_addr, addrlen) == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK);
105 +}
106 +#else /* !WIN32 (sane operating systems) */
107 +static int do_connect_fail(mpd_Connection * connection, const struct sockaddr *serv_addr, int addrlen)
108 +{
109 + int flags = fcntl(connection->sock, F_GETFL, 0);
110 + fcntl(connection->sock, F_SETFL, flags | O_NONBLOCK);
111 + return (connect(connection->sock, serv_addr, addrlen) < 0 && errno != EINPROGRESS);
112 +}
113 +#endif /* !WIN32 */
114 +
115 +#ifdef MPD_HAVE_GAI
116 +static int mpd_connect(mpd_Connection * connection, const char *host, int port, float timeout)
117 +{
118 + int error;
119 + char service[INTLEN + 1];
120 + struct addrinfo hints;
121 + struct addrinfo *res = NULL;
122 + struct addrinfo *addrinfo = NULL;
123 +
124 + /**
125 + * Setup hints
126 + */
127 + hints.ai_flags = AI_ADDRCONFIG;
128 + hints.ai_family = PF_UNSPEC;
129 + hints.ai_socktype = SOCK_STREAM;
130 + hints.ai_protocol = IPPROTO_TCP;
131 + hints.ai_addrlen = 0;
132 + hints.ai_addr = NULL;
133 + hints.ai_canonname = NULL;
134 + hints.ai_next = NULL;
135 +
136 + snprintf(service, sizeof(service), "%i", port);
137 +
138 + error = getaddrinfo(host, service, &hints, &addrinfo);
139 +
140 + if (error) {
141 + snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH, "host \"%s\" not found: %s", host, gai_strerror(error));
142 + connection->error = MPD_ERROR_UNKHOST;
143 + return -1;
144 + }
145 +
146 + for (res = addrinfo; res; res = res->ai_next) {
147 + /* create socket */
148 + connection->sock = socket(res->ai_family, SOCK_STREAM, res->ai_protocol);
149 + if (connection->sock < 0) {
150 + snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH, "problems creating socket: %s", strerror(errno));
151 + connection->error = MPD_ERROR_SYSTEM;
152 + freeaddrinfo(addrinfo);
153 + return -1;
154 + }
155 +
156 + mpd_setConnectionTimeout(connection, timeout);
157 +
158 + /* connect stuff */
159 + if (do_connect_fail(connection, res->ai_addr, res->ai_addrlen)) {
160 + /* try the next address family */
161 + closesocket(connection->sock);
162 + connection->sock = -1;
163 + continue;
164 + }
165 + }
166 +
167 + freeaddrinfo(addrinfo);
168 +
169 + if (connection->sock < 0) {
170 + snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
171 + "problems connecting to \"%s\" on port %i: %s", host, port, strerror(errno));
172 + connection->error = MPD_ERROR_CONNPORT;
173 +
174 + return -1;
175 + }
176 +
177 + return 0;
178 +}
179 +#else /* !MPD_HAVE_GAI */
180 +static int mpd_connect(mpd_Connection * connection, const char *host, int port, float timeout)
181 +{
182 + struct hostent *he;
183 + struct sockaddr *dest;
184 + int destlen;
185 + struct sockaddr_in sin;
186 +
187 + if (!(he = gethostbyname(host))) {
188 + snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH, "host \"%s\" not found", host);
189 + connection->error = MPD_ERROR_UNKHOST;
190 + return -1;
191 + }
192 +
193 + memset(&sin, 0, sizeof(struct sockaddr_in));
194 + /*dest.sin_family = he->h_addrtype; */
195 + sin.sin_family = AF_INET;
196 + sin.sin_port = htons(port);
197 +
198 + switch (he->h_addrtype) {
199 + case AF_INET:
200 + memcpy((char *) &sin.sin_addr.s_addr, (char *) he->h_addr, he->h_length);
201 + dest = (struct sockaddr *) &sin;
202 + destlen = sizeof(struct sockaddr_in);
203 + break;
204 + default:
205 + strcpy(connection->errorStr, "address type is not IPv4");
206 + connection->error = MPD_ERROR_SYSTEM;
207 + return -1;
208 + break;
209 + }
210 +
211 + if ((connection->sock = socket(dest->sa_family, SOCK_STREAM, 0)) < 0) {
212 + strcpy(connection->errorStr, "problems creating socket");
213 + connection->error = MPD_ERROR_SYSTEM;
214 + return -1;
215 + }
216 +
217 + mpd_setConnectionTimeout(connection, timeout);
218 +
219 + /* connect stuff */
220 + if (do_connect_fail(connection, dest, destlen)) {
221 + snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
222 + "problems connecting to \"%s\" on port" " %i", host, port);
223 + connection->error = MPD_ERROR_CONNPORT;
224 + return -1;
225 + }
226 +
227 + return 0;
228 +}
229 +#endif /* !MPD_HAVE_GAI */
230 +
231 +char *mpdTagItemKeys[MPD_TAG_NUM_OF_ITEM_TYPES] = {
232 + "Artist",
233 + "Album",
234 + "Title",
235 + "Track",
236 + "Name",
237 + "Genre",
238 + "Date",
239 + "Composer",
240 + "Performer",
241 + "Comment",
242 + "Disc",
243 + "Filename",
244 + "Any"
245 +};
246 +
247 +static char *mpd_sanitizeArg(const char *arg)
248 +{
249 + size_t i;
250 + char *ret;
251 + register const char *c;
252 + register char *rc;
253 +
254 + /* instead of counting in that loop above, just
255 + * use a bit more memory and half running time
256 + */
257 + ret = malloc(strlen(arg) * 2 + 1);
258 +
259 + c = arg;
260 + rc = ret;
261 + for (i = strlen(arg) + 1; i != 0; --i) {
262 + if (*c == '"' || *c == '\\')
263 + *rc++ = '\\';
264 + *(rc++) = *(c++);
265 + }
266 +
267 + return ret;
268 +}
269 +
270 +static mpd_ReturnElement *mpd_newReturnElement(const char *name, const char *value)
271 +{
272 + mpd_ReturnElement *ret = malloc(sizeof(mpd_ReturnElement));
273 +
274 + ret->name = strdup(name);
275 + ret->value = strdup(value);
276 +
277 + return ret;
278 +}
279 +
280 +static void mpd_freeReturnElement(mpd_ReturnElement * re)
281 +{
282 + free(re->name);
283 + free(re->value);
284 + free(re);
285 +}
286 +
287 +void mpd_setConnectionTimeout(mpd_Connection * connection, float timeout)
288 +{
289 + connection->timeout.tv_sec = (int) timeout;
290 + connection->timeout.tv_usec = (int) (timeout * 1e6 - connection->timeout.tv_sec * 1000000 + 0.5);
291 +}
292 +
293 +static int mpd_parseWelcome(mpd_Connection * connection, const char *host, int port, char *rt, char *output)
294 +{
295 + char *tmp;
296 + char *test;
297 + int i;
298 +
299 + if (strncmp(output, MPD_WELCOME_MESSAGE, strlen(MPD_WELCOME_MESSAGE))) {
300 + snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
301 + "mpd not running on port %i on host \"%s\"", port, host);
302 + connection->error = MPD_ERROR_NOTMPD;
303 + return 1;
304 + }
305 +
306 + tmp = &output[strlen(MPD_WELCOME_MESSAGE)];
307 +
308 + for (i = 0; i < 3; i++) {
309 + if (tmp)
310 + connection->version[i] = strtol(tmp, &test, 10);
311 +
312 + if (!tmp || (test[0] != '.' && test[0] != '\0')) {
313 + snprintf(connection->errorStr,
314 + MPD_ERRORSTR_MAX_LENGTH,
315 + "error parsing version number at " "\"%s\"", &output[strlen(MPD_WELCOME_MESSAGE)]);
316 + connection->error = MPD_ERROR_NOTMPD;
317 + return 1;
318 + }
319 + tmp = ++test;
320 + }
321 +
322 + return 0;
323 +}
324 +
325 +mpd_Connection *mpd_newConnection(const char *host, int port, float timeout)
326 +{
327 + int err;
328 + char *rt;
329 + char *output = NULL;
330 + mpd_Connection *connection = malloc(sizeof(mpd_Connection));
331 + struct timeval tv;
332 + fd_set fds;
333 + strcpy(connection->buffer, "");
334 + connection->buflen = 0;
335 + connection->bufstart = 0;
336 + strcpy(connection->errorStr, "");
337 + connection->error = 0;
338 + connection->doneProcessing = 0;
339 + connection->commandList = 0;
340 + connection->listOks = 0;
341 + connection->doneListOk = 0;
342 + connection->returnElement = NULL;
343 + connection->request = NULL;
344 +
345 + if (winsock_dll_error(connection))
346 + return connection;
347 +
348 + if (mpd_connect(connection, host, port, timeout) < 0)
349 + return connection;
350 +
351 + while (!(rt = strstr(connection->buffer, "\n"))) {
352 + tv.tv_sec = connection->timeout.tv_sec;
353 + tv.tv_usec = connection->timeout.tv_usec;
354 + FD_ZERO(&fds);
355 + FD_SET(connection->sock, &fds);
356 + if ((err = select(connection->sock + 1, &fds, NULL, NULL, &tv)) == 1) {
357 + int readed;
358 + readed = recv(connection->sock,
359 + &(connection->buffer[connection->buflen]), MPD_BUFFER_MAX_LENGTH - connection->buflen, 0);
360 + if (readed <= 0) {
361 + snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
362 + "problems getting a response from" " \"%s\" on port %i : %s", host, port, strerror(errno));
363 + connection->error = MPD_ERROR_NORESPONSE;
364 + return connection;
365 + }
366 + connection->buflen += readed;
367 + connection->buffer[connection->buflen] = '\0';
368 + } else if (err < 0) {
369 + if (SELECT_ERRNO_IGNORE)
370 + continue;
371 + snprintf(connection->errorStr,
372 + MPD_ERRORSTR_MAX_LENGTH, "problems connecting to \"%s\" on port" " %i", host, port);
373 + connection->error = MPD_ERROR_CONNPORT;
374 + return connection;
375 + } else {
376 + snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
377 + "timeout in attempting to get a response from" " \"%s\" on port %i", host, port);
378 + connection->error = MPD_ERROR_NORESPONSE;
379 + return connection;
380 + }
381 + }
382 +
383 + *rt = '\0';
384 + output = strdup(connection->buffer);
385 + strcpy(connection->buffer, rt + 1);
386 + connection->buflen = strlen(connection->buffer);
387 +
388 + if (mpd_parseWelcome(connection, host, port, rt, output) == 0)
389 + connection->doneProcessing = 1;
390 +
391 + free(output);
392 +
393 + return connection;
394 +}
395 +
396 +void mpd_clearError(mpd_Connection * connection)
397 +{
398 + connection->error = 0;
399 + connection->errorStr[0] = '\0';
400 +}
401 +
402 +void mpd_closeConnection(mpd_Connection * connection)
403 +{
404 + closesocket(connection->sock);
405 + if (connection->returnElement)
406 + free(connection->returnElement);
407 + if (connection->request)
408 + free(connection->request);
409 + free(connection);
410 + WSACleanup();
411 +}
412 +
413 +static void mpd_executeCommand(mpd_Connection * connection, char *command)
414 +{
415 + int ret;
416 + struct timeval tv;
417 + fd_set fds;
418 + char *commandPtr = command;
419 + int commandLen = strlen(command);
420 +
421 + if (!connection->doneProcessing && !connection->commandList) {
422 + strcpy(connection->errorStr, "not done processing current command");
423 + connection->error = 1;
424 + return;
425 + }
426 +
427 + mpd_clearError(connection);
428 +
429 + FD_ZERO(&fds);
430 + FD_SET(connection->sock, &fds);
431 + tv.tv_sec = connection->timeout.tv_sec;
432 + tv.tv_usec = connection->timeout.tv_usec;
433 +
434 + while ((ret = select(connection->sock + 1, NULL, &fds, NULL, &tv) == 1) || (ret == -1 && SELECT_ERRNO_IGNORE)) {
435 + ret = send(connection->sock, commandPtr, commandLen, MSG_DONTWAIT);
436 + if (ret <= 0) {
437 + if (SENDRECV_ERRNO_IGNORE)
438 + continue;
439 + snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH, "problems giving command \"%s\"", command);
440 + connection->error = MPD_ERROR_SENDING;
441 + return;
442 + } else {
443 + commandPtr += ret;
444 + commandLen -= ret;
445 + }
446 +
447 + if (commandLen <= 0)
448 + break;
449 + }
450 +
451 + if (commandLen > 0) {
452 + perror("");
453 + snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH, "timeout sending command \"%s\"", command);
454 + connection->error = MPD_ERROR_TIMEOUT;
455 + return;
456 + }
457 +
458 + if (!connection->commandList)
459 + connection->doneProcessing = 0;
460 + else if (connection->commandList == COMMAND_LIST_OK) {
461 + connection->listOks++;
462 + }
463 +}
464 +
465 +static void mpd_getNextReturnElement(mpd_Connection * connection)
466 +{
467 + char *output = NULL;
468 + char *rt = NULL;
469 + char *name = NULL;
470 + char *value = NULL;
471 + fd_set fds;
472 + struct timeval tv;
473 + char *tok = NULL;
474 + int readed;
475 + char *bufferCheck = NULL;
476 + int err;
477 + int pos;
478 +
479 + if (connection->returnElement)
480 + mpd_freeReturnElement(connection->returnElement);
481 + connection->returnElement = NULL;
482 +
483 + if (connection->doneProcessing || (connection->listOks && connection->doneListOk)) {
484 + strcpy(connection->errorStr, "already done processing current command");
485 + connection->error = 1;
486 + return;
487 + }
488 +
489 + bufferCheck = connection->buffer + connection->bufstart;
490 + while (connection->bufstart >= connection->buflen || !(rt = strchr(bufferCheck, '\n'))) {
491 + if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
492 + memmove(connection->buffer,
493 + connection->buffer + connection->bufstart, connection->buflen - connection->bufstart + 1);
494 + connection->buflen -= connection->bufstart;
495 + connection->bufstart = 0;
496 + }
497 + if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
498 + strcpy(connection->errorStr, "buffer overrun");
499 + connection->error = MPD_ERROR_BUFFEROVERRUN;
500 + connection->doneProcessing = 1;
501 + connection->doneListOk = 0;
502 + return;
503 + }
504 + bufferCheck = connection->buffer + connection->buflen;
505 + tv.tv_sec = connection->timeout.tv_sec;
506 + tv.tv_usec = connection->timeout.tv_usec;
507 + FD_ZERO(&fds);
508 + FD_SET(connection->sock, &fds);
509 + if ((err = select(connection->sock + 1, &fds, NULL, NULL, &tv) == 1)) {
510 + readed = recv(connection->sock,
511 + connection->buffer + connection->buflen,
512 + MPD_BUFFER_MAX_LENGTH - connection->buflen, MSG_DONTWAIT);
513 + if (readed < 0 && SENDRECV_ERRNO_IGNORE) {
514 + continue;
515 + }
516 + if (readed <= 0) {
517 + strcpy(connection->errorStr, "connection" " closed");
518 + connection->error = MPD_ERROR_CONNCLOSED;
519 + connection->doneProcessing = 1;
520 + connection->doneListOk = 0;
521 + return;
522 + }
523 + connection->buflen += readed;
524 + connection->buffer[connection->buflen] = '\0';
525 + } else if (err < 0 && SELECT_ERRNO_IGNORE)
526 + continue;
527 + else {
528 + strcpy(connection->errorStr, "connection timeout");
529 + connection->error = MPD_ERROR_TIMEOUT;
530 + connection->doneProcessing = 1;
531 + connection->doneListOk = 0;
532 + return;
533 + }
534 + }
535 +
536 + *rt = '\0';
537 + output = connection->buffer + connection->bufstart;
538 + connection->bufstart = rt - connection->buffer + 1;
539 +
540 + if (strcmp(output, "OK") == 0) {
541 + if (connection->listOks > 0) {
542 + strcpy(connection->errorStr, "expected more list_OK's");
543 + connection->error = 1;
544 + }
545 + connection->listOks = 0;
546 + connection->doneProcessing = 1;
547 + connection->doneListOk = 0;
548 + return;
549 + }
550 +
551 + if (strcmp(output, "list_OK") == 0) {
552 + if (!connection->listOks) {
553 + strcpy(connection->errorStr, "got an unexpected list_OK");
554 + connection->error = 1;
555 + } else {
556 + connection->doneListOk = 1;
557 + connection->listOks--;
558 + }
559 + return;
560 + }
561 +
562 + if (strncmp(output, "ACK", strlen("ACK")) == 0) {
563 + char *test;
564 + char *needle;
565 + int val;
566 +
567 + strcpy(connection->errorStr, output);
568 + connection->error = MPD_ERROR_ACK;
569 + connection->errorCode = MPD_ACK_ERROR_UNK;
570 + connection->errorAt = MPD_ERROR_AT_UNK;
571 + connection->doneProcessing = 1;
572 + connection->doneListOk = 0;
573 +
574 + needle = strchr(output, '[');
575 + if (!needle)
576 + return;
577 + val = strtol(needle + 1, &test, 10);
578 + if (*test != '@')
579 + return;
580 + connection->errorCode = val;
581 + val = strtol(test + 1, &test, 10);
582 + if (*test != ']')
583 + return;
584 + connection->errorAt = val;
585 + return;
586 + }
587 +
588 + tok = strchr(output, ':');
589 + if (!tok)
590 + return;
591 + pos = tok - output;
592 + value = ++tok;
593 + name = output;
594 + name[pos] = '\0';
595 +
596 + if (value[0] == ' ') {
597 + connection->returnElement = mpd_newReturnElement(name, &(value[1]));
598 + } else {
599 + snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH, "error parsing: %s:%s", name, value);
600 + connection->error = 1;
601 + }
602 +}
603 +
604 +void mpd_finishCommand(mpd_Connection * connection)
605 +{
606 + while (!connection->doneProcessing) {
607 + if (connection->doneListOk)
608 + connection->doneListOk = 0;
609 + mpd_getNextReturnElement(connection);
610 + }
611 +}
612 +
613 +static void mpd_finishListOkCommand(mpd_Connection * connection)
614 +{
615 + while (!connection->doneProcessing && connection->listOks && !connection->doneListOk) {
616 + mpd_getNextReturnElement(connection);
617 + }
618 +}
619 +
620 +int mpd_nextListOkCommand(mpd_Connection * connection)
621 +{
622 + mpd_finishListOkCommand(connection);
623 + if (!connection->doneProcessing)
624 + connection->doneListOk = 0;
625 + if (connection->listOks == 0 || connection->doneProcessing)
626 + return -1;
627 + return 0;
628 +}
629 +
630 +void mpd_sendStatusCommand(mpd_Connection * connection)
631 +{
632 + mpd_executeCommand(connection, "status\n");
633 +}
634 +
635 +mpd_Status *mpd_getStatus(mpd_Connection * connection)
636 +{
637 + mpd_Status *status;
638 +
639 + /*mpd_executeCommand(connection,"status\n");
640 +
641 + if(connection->error) return NULL; */
642 +
643 + if (connection->doneProcessing || (connection->listOks && connection->doneListOk)) {
644 + return NULL;
645 + }
646 +
647 + if (!connection->returnElement)
648 + mpd_getNextReturnElement(connection);
649 +
650 + status = malloc(sizeof(mpd_Status));
651 + status->volume = -1;
652 + status->repeat = 0;
653 + status->random = 0;
654 + status->playlist = -1;
655 + status->playlistLength = -1;
656 + status->state = -1;
657 + status->song = 0;
658 + status->songid = 0;
659 + status->elapsedTime = 0;
660 + status->totalTime = 0;
661 + status->bitRate = 0;
662 + status->sampleRate = 0;
663 + status->bits = 0;
664 + status->channels = 0;
665 + status->crossfade = -1;
666 + status->error = NULL;
667 + status->updatingDb = 0;
668 +
669 + if (connection->error) {
670 + free(status);
671 + return NULL;
672 + }
673 + while (connection->returnElement) {
674 + mpd_ReturnElement *re = connection->returnElement;
675 + if (strcmp(re->name, "volume") == 0) {
676 + status->volume = atoi(re->value);
677 + } else if (strcmp(re->name, "repeat") == 0) {
678 + status->repeat = atoi(re->value);
679 + } else if (strcmp(re->name, "random") == 0) {
680 + status->random = atoi(re->value);
681 + } else if (strcmp(re->name, "playlist") == 0) {
682 + status->playlist = strtol(re->value, NULL, 10);
683 + } else if (strcmp(re->name, "playlistlength") == 0) {
684 + status->playlistLength = atoi(re->value);
685 + } else if (strcmp(re->name, "bitrate") == 0) {
686 + status->bitRate = atoi(re->value);
687 + } else if (strcmp(re->name, "state") == 0) {
688 + if (strcmp(re->value, "play") == 0) {
689 + status->state = MPD_STATUS_STATE_PLAY;
690 + } else if (strcmp(re->value, "stop") == 0) {
691 + status->state = MPD_STATUS_STATE_STOP;
692 + } else if (strcmp(re->value, "pause") == 0) {
693 + status->state = MPD_STATUS_STATE_PAUSE;
694 + } else {
695 + status->state = MPD_STATUS_STATE_UNKNOWN;
696 + }
697 + } else if (strcmp(re->name, "song") == 0) {
698 + status->song = atoi(re->value);
699 + } else if (strcmp(re->name, "songid") == 0) {
700 + status->songid = atoi(re->value);
701 + } else if (strcmp(re->name, "time") == 0) {
702 + char *tok = strchr(re->value, ':');
703 + /* the second strchr below is a safety check */
704 + if (tok && (strchr(tok, 0) > (tok + 1))) {
705 + /* atoi stops at the first non-[0-9] char: */
706 + status->elapsedTime = atoi(re->value);
707 + status->totalTime = atoi(tok + 1);
708 + }
709 + } else if (strcmp(re->name, "error") == 0) {
710 + status->error = strdup(re->value);
711 + } else if (strcmp(re->name, "xfade") == 0) {
712 + status->crossfade = atoi(re->value);
713 + } else if (strcmp(re->name, "updating_db") == 0) {
714 + status->updatingDb = atoi(re->value);
715 + } else if (strcmp(re->name, "audio") == 0) {
716 + char *tok = strchr(re->value, ':');
717 + if (tok && (strchr(tok, 0) > (tok + 1))) {
718 + status->sampleRate = atoi(re->value);
719 + status->bits = atoi(++tok);
720 + tok = strchr(tok, ':');
721 + if (tok && (strchr(tok, 0) > (tok + 1)))
722 + status->channels = atoi(tok + 1);
723 + }
724 + }
725 +
726 + mpd_getNextReturnElement(connection);
727 + if (connection->error) {
728 + free(status);
729 + return NULL;
730 + }
731 + }
732 +
733 + if (connection->error) {
734 + free(status);
735 + return NULL;
736 + } else if (status->state < 0) {
737 + strcpy(connection->errorStr, "state not found");
738 + connection->error = 1;
739 + free(status);
740 + return NULL;
741 + }
742 +
743 + return status;
744 +}
745 +
746 +void mpd_freeStatus(mpd_Status * status)
747 +{
748 + if (status->error)
749 + free(status->error);
750 + free(status);
751 +}
752 +
753 +void mpd_sendStatsCommand(mpd_Connection * connection)
754 +{
755 + mpd_executeCommand(connection, "stats\n");
756 +}
757 +
758 +mpd_Stats *mpd_getStats(mpd_Connection * connection)
759 +{
760 + mpd_Stats *stats;
761 +
762 + /*mpd_executeCommand(connection,"stats\n");
763 +
764 + if(connection->error) return NULL; */
765 +
766 + if (connection->doneProcessing || (connection->listOks && connection->doneListOk)) {
767 + return NULL;
768 + }
769 +
770 + if (!connection->returnElement)
771 + mpd_getNextReturnElement(connection);
772 +
773 + stats = malloc(sizeof(mpd_Stats));
774 + stats->numberOfArtists = 0;
775 + stats->numberOfAlbums = 0;
776 + stats->numberOfSongs = 0;
777 + stats->uptime = 0;
778 + stats->dbUpdateTime = 0;
779 + stats->playTime = 0;
780 + stats->dbPlayTime = 0;
781 +
782 + if (connection->error) {
783 + free(stats);
784 + return NULL;
785 + }
786 + while (connection->returnElement) {
787 + mpd_ReturnElement *re = connection->returnElement;
788 + if (strcmp(re->name, "artists") == 0) {
789 + stats->numberOfArtists = atoi(re->value);
790 + } else if (strcmp(re->name, "albums") == 0) {
791 + stats->numberOfAlbums = atoi(re->value);
792 + } else if (strcmp(re->name, "songs") == 0) {
793 + stats->numberOfSongs = atoi(re->value);
794 + } else if (strcmp(re->name, "uptime") == 0) {
795 + stats->uptime = strtol(re->value, NULL, 10);
796 + } else if (strcmp(re->name, "db_update") == 0) {
797 + stats->dbUpdateTime = strtol(re->value, NULL, 10);
798 + } else if (strcmp(re->name, "playtime") == 0) {
799 + stats->playTime = strtol(re->value, NULL, 10);
800 + } else if (strcmp(re->name, "db_playtime") == 0) {
801 + stats->dbPlayTime = strtol(re->value, NULL, 10);
802 + }
803 +
804 + mpd_getNextReturnElement(connection);
805 + if (connection->error) {
806 + free(stats);
807 + return NULL;
808 + }
809 + }
810 +
811 + if (connection->error) {
812 + free(stats);
813 + return NULL;
814 + }
815 +
816 + return stats;
817 +}
818 +
819 +void mpd_freeStats(mpd_Stats * stats)
820 +{
821 + free(stats);
822 +}
823 +
824 +mpd_SearchStats *mpd_getSearchStats(mpd_Connection * connection)
825 +{
826 + mpd_SearchStats *stats;
827 + mpd_ReturnElement *re;
828 +
829 + if (connection->doneProcessing || (connection->listOks && connection->doneListOk)) {
830 + return NULL;
831 + }
832 +
833 + if (!connection->returnElement)
834 + mpd_getNextReturnElement(connection);
835 +
836 + if (connection->error)
837 + return NULL;
838 +
839 + stats = malloc(sizeof(mpd_SearchStats));
840 + stats->numberOfSongs = 0;
841 + stats->playTime = 0;
842 +
843 + while (connection->returnElement) {
844 + re = connection->returnElement;
845 +
846 + if (strcmp(re->name, "songs") == 0) {
847 + stats->numberOfSongs = atoi(re->value);
848 + } else if (strcmp(re->name, "playtime") == 0) {
849 + stats->playTime = strtol(re->value, NULL, 10);
850 + }
851 +
852 + mpd_getNextReturnElement(connection);
853 + if (connection->error) {
854 + free(stats);
855 + return NULL;
856 + }
857 + }
858 +
859 + if (connection->error) {
860 + free(stats);
861 + return NULL;
862 + }
863 +
864 + return stats;
865 +}
866 +
867 +void mpd_freeSearchStats(mpd_SearchStats * stats)
868 +{
869 + free(stats);
870 +}
871 +
872 +static void mpd_initSong(mpd_Song * song)
873 +{
874 + song->file = NULL;
875 + song->artist = NULL;
876 + song->album = NULL;
877 + song->track = NULL;
878 + song->title = NULL;
879 + song->name = NULL;
880 + song->date = NULL;
881 + /* added by Qball */
882 + song->genre = NULL;
883 + song->composer = NULL;
884 + song->performer = NULL;
885 + song->disc = NULL;
886 + song->comment = NULL;
887 +
888 + song->time = MPD_SONG_NO_TIME;
889 + song->pos = MPD_SONG_NO_NUM;
890 + song->id = MPD_SONG_NO_ID;
891 +}
892 +
893 +static void mpd_finishSong(mpd_Song * song)
894 +{
895 + if (song->file)
896 + free(song->file);
897 + if (song->artist)
898 + free(song->artist);
899 + if (song->album)
900 + free(song->album);
901 + if (song->title)
902 + free(song->title);
903 + if (song->track)
904 + free(song->track);
905 + if (song->name)
906 + free(song->name);
907 + if (song->date)
908 + free(song->date);
909 + if (song->genre)
910 + free(song->genre);
911 + if (song->composer)
912 + free(song->composer);
913 + if (song->disc)
914 + free(song->disc);
915 + if (song->comment)
916 + free(song->comment);
917 +}
918 +
919 +mpd_Song *mpd_newSong(void)
920 +{
921 + mpd_Song *ret = malloc(sizeof(mpd_Song));
922 +
923 + mpd_initSong(ret);
924 +
925 + return ret;
926 +}
927 +
928 +void mpd_freeSong(mpd_Song * song)
929 +{
930 + mpd_finishSong(song);
931 + free(song);
932 +}
933 +
934 +mpd_Song *mpd_songDup(mpd_Song * song)
935 +{
936 + mpd_Song *ret = mpd_newSong();
937 +
938 + if (song->file)
939 + ret->file = strdup(song->file);
940 + if (song->artist)
941 + ret->artist = strdup(song->artist);
942 + if (song->album)
943 + ret->album = strdup(song->album);
944 + if (song->title)
945 + ret->title = strdup(song->title);
946 + if (song->track)
947 + ret->track = strdup(song->track);
948 + if (song->name)
949 + ret->name = strdup(song->name);
950 + if (song->date)
951 + ret->date = strdup(song->date);
952 + if (song->genre)
953 + ret->genre = strdup(song->genre);
954 + if (song->composer)
955 + ret->composer = strdup(song->composer);
956 + if (song->disc)
957 + ret->disc = strdup(song->disc);
958 + if (song->comment)
959 + ret->comment = strdup(song->comment);
960 + ret->time = song->time;
961 + ret->pos = song->pos;
962 + ret->id = song->id;
963 +
964 + return ret;
965 +}
966 +
967 +static void mpd_initDirectory(mpd_Directory * directory)
968 +{
969 + directory->path = NULL;
970 +}
971 +
972 +static void mpd_finishDirectory(mpd_Directory * directory)
973 +{
974 + if (directory->path)
975 + free(directory->path);
976 +}
977 +
978 +mpd_Directory *mpd_newDirectory(void)
979 +{
980 + mpd_Directory *directory = malloc(sizeof(mpd_Directory));;
981 +
982 + mpd_initDirectory(directory);
983 +
984 + return directory;
985 +}
986 +
987 +void mpd_freeDirectory(mpd_Directory * directory)
988 +{
989 + mpd_finishDirectory(directory);
990 +
991 + free(directory);
992 +}
993 +
994 +mpd_Directory *mpd_directoryDup(mpd_Directory * directory)
995 +{
996 + mpd_Directory *ret = mpd_newDirectory();
997 +
998 + if (directory->path)
999 + ret->path = strdup(directory->path);
1000 +
1001 + return ret;
1002 +}
1003 +
1004 +static void mpd_initPlaylistFile(mpd_PlaylistFile * playlist)
1005 +{
1006 + playlist->path = NULL;
1007 +}
1008 +
1009 +static void mpd_finishPlaylistFile(mpd_PlaylistFile * playlist)
1010 +{
1011 + if (playlist->path)
1012 + free(playlist->path);
1013 +}
1014 +
1015 +mpd_PlaylistFile *mpd_newPlaylistFile(void)
1016 +{
1017 + mpd_PlaylistFile *playlist = malloc(sizeof(mpd_PlaylistFile));
1018 +
1019 + mpd_initPlaylistFile(playlist);
1020 +
1021 + return playlist;
1022 +}
1023 +
1024 +void mpd_freePlaylistFile(mpd_PlaylistFile * playlist)
1025 +{
1026 + mpd_finishPlaylistFile(playlist);
1027 + free(playlist);
1028 +}
1029 +
1030 +mpd_PlaylistFile *mpd_playlistFileDup(mpd_PlaylistFile * playlist)
1031 +{
1032 + mpd_PlaylistFile *ret = mpd_newPlaylistFile();
1033 +
1034 + if (playlist->path)
1035 + ret->path = strdup(playlist->path);
1036 +
1037 + return ret;
1038 +}
1039 +
1040 +static void mpd_initInfoEntity(mpd_InfoEntity * entity)
1041 +{
1042 + entity->info.directory = NULL;
1043 +}
1044 +
1045 +static void mpd_finishInfoEntity(mpd_InfoEntity * entity)
1046 +{
1047 + if (entity->info.directory) {
1048 + if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1049 + mpd_freeDirectory(entity->info.directory);
1050 + } else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
1051 + mpd_freeSong(entity->info.song);
1052 + } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1053 + mpd_freePlaylistFile(entity->info.playlistFile);
1054 + }
1055 + }
1056 +}
1057 +
1058 +mpd_InfoEntity *mpd_newInfoEntity(void)
1059 +{
1060 + mpd_InfoEntity *entity = malloc(sizeof(mpd_InfoEntity));
1061 +
1062 + mpd_initInfoEntity(entity);
1063 +
1064 + return entity;
1065 +}
1066 +
1067 +void mpd_freeInfoEntity(mpd_InfoEntity * entity)
1068 +{
1069 + mpd_finishInfoEntity(entity);
1070 + free(entity);
1071 +}
1072 +
1073 +static void mpd_sendInfoCommand(mpd_Connection * connection, char *command)
1074 +{
1075 + mpd_executeCommand(connection, command);
1076 +}
1077 +
1078 +mpd_InfoEntity *mpd_getNextInfoEntity(mpd_Connection * connection)
1079 +{
1080 + mpd_InfoEntity *entity = NULL;
1081 +
1082 + if (connection->doneProcessing || (connection->listOks && connection->doneListOk)) {
1083 + return NULL;
1084 + }
1085 +
1086 + if (!connection->returnElement)
1087 + mpd_getNextReturnElement(connection);
1088 +
1089 + if (connection->returnElement) {
1090 + if (strcmp(connection->returnElement->name, "file") == 0) {
1091 + entity = mpd_newInfoEntity();
1092 + entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1093 + entity->info.song = mpd_newSong();
1094 + entity->info.song->file = strdup(connection->returnElement->value);
1095 + } else if (strcmp(connection->returnElement->name, "directory") == 0) {
1096 + entity = mpd_newInfoEntity();
1097 + entity->type = MPD_INFO_ENTITY_TYPE_DIRECTORY;
1098 + entity->info.directory = mpd_newDirectory();
1099 + entity->info.directory->path = strdup(connection->returnElement->value);
1100 + } else if (strcmp(connection->returnElement->name, "playlist") == 0) {
1101 + entity = mpd_newInfoEntity();
1102 + entity->type = MPD_INFO_ENTITY_TYPE_PLAYLISTFILE;
1103 + entity->info.playlistFile = mpd_newPlaylistFile();
1104 + entity->info.playlistFile->path = strdup(connection->returnElement->value);
1105 + } else if (strcmp(connection->returnElement->name, "cpos") == 0) {
1106 + entity = mpd_newInfoEntity();
1107 + entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1108 + entity->info.song = mpd_newSong();
1109 + entity->info.song->pos = atoi(connection->returnElement->value);
1110 + } else {
1111 + connection->error = 1;
1112 + strcpy(connection->errorStr, "problem parsing song info");
1113 + return NULL;
1114 + }
1115 + } else
1116 + return NULL;
1117 +
1118 + mpd_getNextReturnElement(connection);
1119 + while (connection->returnElement) {
1120 + mpd_ReturnElement *re = connection->returnElement;
1121 +
1122 + if (strcmp(re->name, "file") == 0)
1123 + return entity;
1124 + else if (strcmp(re->name, "directory") == 0)
1125 + return entity;
1126 + else if (strcmp(re->name, "playlist") == 0)
1127 + return entity;
1128 + else if (strcmp(re->name, "cpos") == 0)
1129 + return entity;
1130 +
1131 + if (entity->type == MPD_INFO_ENTITY_TYPE_SONG && strlen(re->value)) {
1132 + if (!entity->info.song->artist && strcmp(re->name, "Artist") == 0) {
1133 + entity->info.song->artist = strdup(re->value);
1134 + } else if (!entity->info.song->album && strcmp(re->name, "Album") == 0) {
1135 + entity->info.song->album = strdup(re->value);
1136 + } else if (!entity->info.song->title && strcmp(re->name, "Title") == 0) {
1137 + entity->info.song->title = strdup(re->value);
1138 + } else if (!entity->info.song->track && strcmp(re->name, "Track") == 0) {
1139 + entity->info.song->track = strdup(re->value);
1140 + } else if (!entity->info.song->name && strcmp(re->name, "Name") == 0) {
1141 + entity->info.song->name = strdup(re->value);
1142 + } else if (entity->info.song->time == MPD_SONG_NO_TIME && strcmp(re->name, "Time") == 0) {
1143 + entity->info.song->time = atoi(re->value);
1144 + } else if (entity->info.song->pos == MPD_SONG_NO_NUM && strcmp(re->name, "Pos") == 0) {
1145 + entity->info.song->pos = atoi(re->value);
1146 + } else if (entity->info.song->id == MPD_SONG_NO_ID && strcmp(re->name, "Id") == 0) {
1147 + entity->info.song->id = atoi(re->value);
1148 + } else if (!entity->info.song->date && strcmp(re->name, "Date") == 0) {
1149 + entity->info.song->date = strdup(re->value);
1150 + } else if (!entity->info.song->genre && strcmp(re->name, "Genre") == 0) {
1151 + entity->info.song->genre = strdup(re->value);
1152 + } else if (!entity->info.song->composer && strcmp(re->name, "Composer") == 0) {
1153 + entity->info.song->composer = strdup(re->value);
1154 + } else if (!entity->info.song->performer && strcmp(re->name, "Performer") == 0) {
1155 + entity->info.song->performer = strdup(re->value);
1156 + } else if (!entity->info.song->disc && strcmp(re->name, "Disc") == 0) {
1157 + entity->info.song->disc = strdup(re->value);
1158 + } else if (!entity->info.song->comment && strcmp(re->name, "Comment") == 0) {
1159 + entity->info.song->comment = strdup(re->value);
1160 + }
1161 + } else if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1162 + } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1163 + }
1164 +
1165 + mpd_getNextReturnElement(connection);
1166 + }
1167 +
1168 + return entity;
1169 +}
1170 +
1171 +static char *mpd_getNextReturnElementNamed(mpd_Connection * connection, const char *name)
1172 +{
1173 + if (connection->doneProcessing || (connection->listOks && connection->doneListOk)) {
1174 + return NULL;
1175 + }
1176 +
1177 + mpd_getNextReturnElement(connection);
1178 + while (connection->returnElement) {
1179 + mpd_ReturnElement *re = connection->returnElement;
1180 +
1181 + if (strcmp(re->name, name) == 0)
1182 + return strdup(re->value);
1183 + mpd_getNextReturnElement(connection);
1184 + }
1185 +
1186 + return NULL;
1187 +}
1188 +
1189 +char *mpd_getNextTag(mpd_Connection * connection, int type)
1190 +{
1191 + if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES || type == MPD_TAG_ITEM_ANY)
1192 + return NULL;
1193 + if (type == MPD_TAG_ITEM_FILENAME)
1194 + return mpd_getNextReturnElementNamed(connection, "file");
1195 + return mpd_getNextReturnElementNamed(connection, mpdTagItemKeys[type]);
1196 +}
1197 +
1198 +char *mpd_getNextArtist(mpd_Connection * connection)
1199 +{
1200 + return mpd_getNextReturnElementNamed(connection, "Artist");
1201 +}
1202 +
1203 +char *mpd_getNextAlbum(mpd_Connection * connection)
1204 +{
1205 + return mpd_getNextReturnElementNamed(connection, "Album");
1206 +}
1207 +
1208 +void mpd_sendPlaylistInfoCommand(mpd_Connection * connection, int songPos)
1209 +{
1210 + int len = strlen("playlistinfo") + 2 + INTLEN + 3;
1211 + char *string = malloc(len);
1212 + snprintf(string, len, "playlistinfo \"%i\"\n", songPos);
1213 + mpd_sendInfoCommand(connection, string);
1214 + free(string);
1215 +}
1216 +
1217 +void mpd_sendPlaylistIdCommand(mpd_Connection * connection, int id)
1218 +{
1219 + int len = strlen("playlistid") + 2 + INTLEN + 3;
1220 + char *string = malloc(len);
1221 + snprintf(string, len, "playlistid \"%i\"\n", id);
1222 + mpd_sendInfoCommand(connection, string);
1223 + free(string);
1224 +}
1225 +
1226 +void mpd_sendPlChangesCommand(mpd_Connection * connection, long long playlist)
1227 +{
1228 + int len = strlen("plchanges") + 2 + LONGLONGLEN + 3;
1229 + char *string = malloc(len);
1230 + snprintf(string, len, "plchanges \"%lld\"\n", playlist);
1231 + mpd_sendInfoCommand(connection, string);
1232 + free(string);
1233 +}
1234 +
1235 +void mpd_sendPlChangesPosIdCommand(mpd_Connection * connection, long long playlist)
1236 +{
1237 + int len = strlen("plchangesposid") + 2 + LONGLONGLEN + 3;
1238 + char *string = malloc(len);
1239 + snprintf(string, len, "plchangesposid \"%lld\"\n", playlist);
1240 + mpd_sendInfoCommand(connection, string);
1241 + free(string);
1242 +}
1243 +
1244 +void mpd_sendListallCommand(mpd_Connection * connection, const char *dir)
1245 +{
1246 + char *sDir = mpd_sanitizeArg(dir);
1247 + int len = strlen("listall") + 2 + strlen(sDir) + 3;
1248 + char *string = malloc(len);
1249 + snprintf(string, len, "listall \"%s\"\n", sDir);
1250 + mpd_sendInfoCommand(connection, string);
1251 + free(string);
1252 + free(sDir);
1253 +}
1254 +
1255 +void mpd_sendListallInfoCommand(mpd_Connection * connection, const char *dir)
1256 +{
1257 + char *sDir = mpd_sanitizeArg(dir);
1258 + int len = strlen("listallinfo") + 2 + strlen(sDir) + 3;
1259 + char *string = malloc(len);
1260 + snprintf(string, len, "listallinfo \"%s\"\n", sDir);
1261 + mpd_sendInfoCommand(connection, string);
1262 + free(string);
1263 + free(sDir);
1264 +}
1265 +
1266 +void mpd_sendLsInfoCommand(mpd_Connection * connection, const char *dir)
1267 +{
1268 + char *sDir = mpd_sanitizeArg(dir);
1269 + int len = strlen("lsinfo") + 2 + strlen(sDir) + 3;
1270 + char *string = malloc(len);
1271 + snprintf(string, len, "lsinfo \"%s\"\n", sDir);
1272 + mpd_sendInfoCommand(connection, string);
1273 + free(string);
1274 + free(sDir);
1275 +}
1276 +
1277 +void mpd_sendCurrentSongCommand(mpd_Connection * connection)
1278 +{
1279 + mpd_executeCommand(connection, "currentsong\n");
1280 +}
1281 +
1282 +void mpd_sendSearchCommand(mpd_Connection * connection, int table, const char *str)
1283 +{
1284 + mpd_startSearch(connection, 0);
1285 + mpd_addConstraintSearch(connection, table, str);
1286 + mpd_commitSearch(connection);
1287 +}
1288 +
1289 +void mpd_sendFindCommand(mpd_Connection * connection, int table, const char *str)
1290 +{
1291 + mpd_startSearch(connection, 1);
1292 + mpd_addConstraintSearch(connection, table, str);
1293 + mpd_commitSearch(connection);
1294 +}
1295 +
1296 +void mpd_sendListCommand(mpd_Connection * connection, int table, const char *arg1)
1297 +{
1298 + char st[10];
1299 + int len;
1300 + char *string;
1301 + if (table == MPD_TABLE_ARTIST)
1302 + strcpy(st, "artist");
1303 + else if (table == MPD_TABLE_ALBUM)
1304 + strcpy(st, "album");
1305 + else {
1306 + connection->error = 1;
1307 + strcpy(connection->errorStr, "unknown table for list");
1308 + return;
1309 + }
1310 + if (arg1) {
1311 + char *sanitArg1 = mpd_sanitizeArg(arg1);
1312 + len = strlen("list") + 1 + strlen(sanitArg1) + 2 + strlen(st) + 3;
1313 + string = malloc(len);
1314 + snprintf(string, len, "list %s \"%s\"\n", st, sanitArg1);
1315 + free(sanitArg1);
1316 + } else {
1317 + len = strlen("list") + 1 + strlen(st) + 2;
1318 + string = malloc(len);
1319 + snprintf(string, len, "list %s\n", st);
1320 + }
1321 + mpd_sendInfoCommand(connection, string);
1322 + free(string);
1323 +}
1324 +
1325 +void mpd_sendAddCommand(mpd_Connection * connection, const char *file)
1326 +{
1327 + char *sFile = mpd_sanitizeArg(file);
1328 + int len = strlen("add") + 2 + strlen(sFile) + 3;
1329 + char *string = malloc(len);
1330 + snprintf(string, len, "add \"%s\"\n", sFile);
1331 + mpd_executeCommand(connection, string);
1332 + free(string);
1333 + free(sFile);
1334 +}
1335 +
1336 +int mpd_sendAddIdCommand(mpd_Connection * connection, const char *file)
1337 +{
1338 + int retval = -1;
1339 + char *sFile = mpd_sanitizeArg(file);
1340 + int len = strlen("addid") + 2 + strlen(sFile) + 3;
1341 + char *string = malloc(len);
1342 +
1343 + snprintf(string, len, "addid \"%s\"\n", sFile);
1344 + mpd_sendInfoCommand(connection, string);
1345 + free(string);
1346 + free(sFile);
1347 +
1348 + string = mpd_getNextReturnElementNamed(connection, "Id");
1349 + if (string) {
1350 + retval = atoi(string);
1351 + free(string);
1352 + }
1353 +
1354 + return retval;
1355 +}
1356 +
1357 +void mpd_sendDeleteCommand(mpd_Connection * connection, int songPos)
1358 +{
1359 + int len = strlen("delete") + 2 + INTLEN + 3;
1360 + char *string = malloc(len);
1361 + snprintf(string, len, "delete \"%i\"\n", songPos);
1362 + mpd_sendInfoCommand(connection, string);
1363 + free(string);
1364 +}
1365 +
1366 +void mpd_sendDeleteIdCommand(mpd_Connection * connection, int id)
1367 +{
1368 + int len = strlen("deleteid") + 2 + INTLEN + 3;
1369 + char *string = malloc(len);
1370 + snprintf(string, len, "deleteid \"%i\"\n", id);
1371 + mpd_sendInfoCommand(connection, string);
1372 + free(string);
1373 +}
1374 +
1375 +void mpd_sendSaveCommand(mpd_Connection * connection, const char *name)
1376 +{
1377 + char *sName = mpd_sanitizeArg(name);
1378 + int len = strlen("save") + 2 + strlen(sName) + 3;
1379 + char *string = malloc(len);
1380 + snprintf(string, len, "save \"%s\"\n", sName);
1381 + mpd_executeCommand(connection, string);
1382 + free(string);
1383 + free(sName);
1384 +}
1385 +
1386 +void mpd_sendLoadCommand(mpd_Connection * connection, const char *name)
1387 +{
1388 + char *sName = mpd_sanitizeArg(name);
1389 + int len = strlen("load") + 2 + strlen(sName) + 3;
1390 + char *string = malloc(len);
1391 + snprintf(string, len, "load \"%s\"\n", sName);
1392 + mpd_executeCommand(connection, string);
1393 + free(string);
1394 + free(sName);
1395 +}
1396 +
1397 +void mpd_sendRmCommand(mpd_Connection * connection, const char *name)
1398 +{
1399 + char *sName = mpd_sanitizeArg(name);
1400 + int len = strlen("rm") + 2 + strlen(sName) + 3;
1401 + char *string = malloc(len);
1402 + snprintf(string, len, "rm \"%s\"\n", sName);
1403 + mpd_executeCommand(connection, string);
1404 + free(string);
1405 + free(sName);
1406 +}
1407 +
1408 +void mpd_sendRenameCommand(mpd_Connection * connection, const char *from, const char *to)
1409 +{
1410 + char *sFrom = mpd_sanitizeArg(from);
1411 + char *sTo = mpd_sanitizeArg(to);
1412 + int len = strlen("rename") + 2 + strlen(sFrom) + 3 + strlen(sTo) + 3;
1413 + char *string = malloc(len);
1414 + snprintf(string, len, "rename \"%s\" \"%s\"\n", sFrom, sTo);
1415 + mpd_executeCommand(connection, string);
1416 + free(string);
1417 + free(sFrom);
1418 + free(sTo);
1419 +}
1420 +
1421 +void mpd_sendShuffleCommand(mpd_Connection * connection)
1422 +{
1423 + mpd_executeCommand(connection, "shuffle\n");
1424 +}
1425 +
1426 +void mpd_sendClearCommand(mpd_Connection * connection)
1427 +{
1428 + mpd_executeCommand(connection, "clear\n");
1429 +}
1430 +
1431 +void mpd_sendPlayCommand(mpd_Connection * connection, int songPos)
1432 +{
1433 + int len = strlen("play") + 2 + INTLEN + 3;
1434 + char *string = malloc(len);
1435 + snprintf(string, len, "play \"%i\"\n", songPos);
1436 + mpd_sendInfoCommand(connection, string);
1437 + free(string);
1438 +}
1439 +
1440 +void mpd_sendPlayIdCommand(mpd_Connection * connection, int id)
1441 +{
1442 + int len = strlen("playid") + 2 + INTLEN + 3;
1443 + char *string = malloc(len);
1444 + snprintf(string, len, "playid \"%i\"\n", id);
1445 + mpd_sendInfoCommand(connection, string);
1446 + free(string);
1447 +}
1448 +
1449 +void mpd_sendStopCommand(mpd_Connection * connection)
1450 +{
1451 + mpd_executeCommand(connection, "stop\n");
1452 +}
1453 +
1454 +void mpd_sendPauseCommand(mpd_Connection * connection, int pauseMode)
1455 +{
1456 + int len = strlen("pause") + 2 + INTLEN + 3;
1457 + char *string = malloc(len);
1458 + snprintf(string, len, "pause \"%i\"\n", pauseMode);
1459 + mpd_executeCommand(connection, string);
1460 + free(string);
1461 +}
1462 +
1463 +void mpd_sendNextCommand(mpd_Connection * connection)
1464 +{
1465 + mpd_executeCommand(connection, "next\n");
1466 +}
1467 +
1468 +void mpd_sendMoveCommand(mpd_Connection * connection, int from, int to)
1469 +{
1470 + int len = strlen("move") + 2 + INTLEN + 3 + INTLEN + 3;
1471 + char *string = malloc(len);
1472 + snprintf(string, len, "move \"%i\" \"%i\"\n", from, to);
1473 + mpd_sendInfoCommand(connection, string);
1474 + free(string);
1475 +}
1476 +
1477 +void mpd_sendMoveIdCommand(mpd_Connection * connection, int id, int to)
1478 +{
1479 + int len = strlen("moveid") + 2 + INTLEN + 3 + INTLEN + 3;
1480 + char *string = malloc(len);
1481 + snprintf(string, len, "moveid \"%i\" \"%i\"\n", id, to);
1482 + mpd_sendInfoCommand(connection, string);
1483 + free(string);
1484 +}
1485 +
1486 +void mpd_sendSwapCommand(mpd_Connection * connection, int song1, int song2)
1487 +{
1488 + int len = strlen("swap") + 2 + INTLEN + 3 + INTLEN + 3;
1489 + char *string = malloc(len);
1490 + snprintf(string, len, "swap \"%i\" \"%i\"\n", song1, song2);
1491 + mpd_sendInfoCommand(connection, string);
1492 + free(string);
1493 +}
1494 +
1495 +void mpd_sendSwapIdCommand(mpd_Connection * connection, int id1, int id2)
1496 +{
1497 + int len = strlen("swapid") + 2 + INTLEN + 3 + INTLEN + 3;
1498 + char *string = malloc(len);
1499 + snprintf(string, len, "swapid \"%i\" \"%i\"\n", id1, id2);
1500 + mpd_sendInfoCommand(connection, string);
1501 + free(string);
1502 +}
1503 +
1504 +void mpd_sendSeekCommand(mpd_Connection * connection, int song, int time)
1505 +{
1506 + int len = strlen("seek") + 2 + INTLEN + 3 + INTLEN + 3;
1507 + char *string = malloc(len);
1508 + snprintf(string, len, "seek \"%i\" \"%i\"\n", song, time);
1509 + mpd_sendInfoCommand(connection, string);
1510 + free(string);
1511 +}
1512 +
1513 +void mpd_sendSeekIdCommand(mpd_Connection * connection, int id, int time)
1514 +{
1515 + int len = strlen("seekid") + 2 + INTLEN + 3 + INTLEN + 3;
1516 + char *string = malloc(len);
1517 + snprintf(string, len, "seekid \"%i\" \"%i\"\n", id, time);
1518 + mpd_sendInfoCommand(connection, string);
1519 + free(string);
1520 +}
1521 +
1522 +void mpd_sendUpdateCommand(mpd_Connection * connection, char *path)
1523 +{
1524 + char *sPath = mpd_sanitizeArg(path);
1525 + int len = strlen("update") + 2 + strlen(sPath) + 3;
1526 + char *string = malloc(len);
1527 + snprintf(string, len, "update \"%s\"\n", sPath);
1528 + mpd_sendInfoCommand(connection, string);
1529 + free(string);
1530 + free(sPath);
1531 +}
1532 +
1533 +int mpd_getUpdateId(mpd_Connection * connection)
1534 +{
1535 + char *jobid;
1536 + int ret = 0;
1537 +
1538 + jobid = mpd_getNextReturnElementNamed(connection, "updating_db");
1539 + if (jobid) {
1540 + ret = atoi(jobid);
1541 + free(jobid);
1542 + }
1543 +
1544 + return ret;
1545 +}
1546 +
1547 +void mpd_sendPrevCommand(mpd_Connection * connection)
1548 +{
1549 + mpd_executeCommand(connection, "previous\n");
1550 +}
1551 +
1552 +void mpd_sendRepeatCommand(mpd_Connection * connection, int repeatMode)
1553 +{
1554 + int len = strlen("repeat") + 2 + INTLEN + 3;
1555 + char *string = malloc(len);
1556 + snprintf(string, len, "repeat \"%i\"\n", repeatMode);
1557 + mpd_executeCommand(connection, string);
1558 + free(string);
1559 +}
1560 +
1561 +void mpd_sendRandomCommand(mpd_Connection * connection, int randomMode)
1562 +{
1563 + int len = strlen("random") + 2 + INTLEN + 3;
1564 + char *string = malloc(len);
1565 + snprintf(string, len, "random \"%i\"\n", randomMode);
1566 + mpd_executeCommand(connection, string);
1567 + free(string);
1568 +}
1569 +
1570 +void mpd_sendSetvolCommand(mpd_Connection * connection, int volumeChange)
1571 +{
1572 + int len = strlen("setvol") + 2 + INTLEN + 3;
1573 + char *string = malloc(len);
1574 + snprintf(string, len, "setvol \"%i\"\n", volumeChange);
1575 + mpd_executeCommand(connection, string);
1576 + free(string);
1577 +}
1578 +
1579 +void mpd_sendVolumeCommand(mpd_Connection * connection, int volumeChange)
1580 +{
1581 + int len = strlen("volume") + 2 + INTLEN + 3;
1582 + char *string = malloc(len);
1583 + snprintf(string, len, "volume \"%i\"\n", volumeChange);
1584 + mpd_executeCommand(connection, string);
1585 + free(string);
1586 +}
1587 +
1588 +void mpd_sendCrossfadeCommand(mpd_Connection * connection, int seconds)
1589 +{
1590 + int len = strlen("crossfade") + 2 + INTLEN + 3;
1591 + char *string = malloc(len);
1592 + snprintf(string, len, "crossfade \"%i\"\n", seconds);
1593 + mpd_executeCommand(connection, string);
1594 + free(string);
1595 +}
1596 +
1597 +void mpd_sendPasswordCommand(mpd_Connection * connection, const char *pass)
1598 +{
1599 + char *sPass = mpd_sanitizeArg(pass);
1600 + int len = strlen("password") + 2 + strlen(sPass) + 3;
1601 + char *string = malloc(len);
1602 + snprintf(string, len, "password \"%s\"\n", sPass);
1603 + mpd_executeCommand(connection, string);
1604 + free(string);
1605 + free(sPass);
1606 +}
1607 +
1608 +void mpd_sendCommandListBegin(mpd_Connection * connection)
1609 +{
1610 + if (connection->commandList) {
1611 + strcpy(connection->errorStr, "already in command list mode");
1612 + connection->error = 1;
1613 + return;
1614 + }
1615 + connection->commandList = COMMAND_LIST;
1616 + mpd_executeCommand(connection, "command_list_begin\n");
1617 +}
1618 +
1619 +void mpd_sendCommandListOkBegin(mpd_Connection * connection)
1620 +{
1621 + if (connection->commandList) {
1622 + strcpy(connection->errorStr, "already in command list mode");
1623 + connection->error = 1;
1624 + return;
1625 + }
1626 + connection->commandList = COMMAND_LIST_OK;
1627 + mpd_executeCommand(connection, "command_list_ok_begin\n");
1628 + connection->listOks = 0;
1629 +}
1630 +
1631 +void mpd_sendCommandListEnd(mpd_Connection * connection)
1632 +{
1633 + if (!connection->commandList) {
1634 + strcpy(connection->errorStr, "not in command list mode");
1635 + connection->error = 1;
1636 + return;
1637 + }
1638 + connection->commandList = 0;
1639 + mpd_executeCommand(connection, "command_list_end\n");
1640 +}
1641 +
1642 +void mpd_sendOutputsCommand(mpd_Connection * connection)
1643 +{
1644 + mpd_executeCommand(connection, "outputs\n");
1645 +}
1646 +
1647 +mpd_OutputEntity *mpd_getNextOutput(mpd_Connection * connection)
1648 +{
1649 + mpd_OutputEntity *output = NULL;
1650 +
1651 + if (connection->doneProcessing || (connection->listOks && connection->doneListOk)) {
1652 + return NULL;
1653 + }
1654 +
1655 + if (connection->error)
1656 + return NULL;
1657 +
1658 + output = malloc(sizeof(mpd_OutputEntity));
1659 + output->id = -10;
1660 + output->name = NULL;
1661 + output->enabled = 0;
1662 +
1663 + if (!connection->returnElement)
1664 + mpd_getNextReturnElement(connection);
1665 +
1666 + while (connection->returnElement) {
1667 + mpd_ReturnElement *re = connection->returnElement;
1668 + if (strcmp(re->name, "outputid") == 0) {
1669 + if (output != NULL && output->id >= 0)
1670 + return output;
1671 + output->id = atoi(re->value);
1672 + } else if (strcmp(re->name, "outputname") == 0) {
1673 + output->name = strdup(re->value);
1674 + } else if (strcmp(re->name, "outputenabled") == 0) {
1675 + output->enabled = atoi(re->value);
1676 + }
1677 +
1678 + mpd_getNextReturnElement(connection);
1679 + if (connection->error) {
1680 + free(output);
1681 + return NULL;
1682 + }
1683 +
1684 + }
1685 +
1686 + return output;
1687 +}
1688 +
1689 +void mpd_sendEnableOutputCommand(mpd_Connection * connection, int outputId)
1690 +{
1691 + int len = strlen("enableoutput") + 2 + INTLEN + 3;
1692 + char *string = malloc(len);
1693 + snprintf(string, len, "enableoutput \"%i\"\n", outputId);
1694 + mpd_executeCommand(connection, string);
1695 + free(string);
1696 +}
1697 +
1698 +void mpd_sendDisableOutputCommand(mpd_Connection * connection, int outputId)
1699 +{
1700 + int len = strlen("disableoutput") + 2 + INTLEN + 3;
1701 + char *string = malloc(len);
1702 + snprintf(string, len, "disableoutput \"%i\"\n", outputId);
1703 + mpd_executeCommand(connection, string);
1704 + free(string);
1705 +}
1706 +
1707 +void mpd_freeOutputElement(mpd_OutputEntity * output)
1708 +{
1709 + free(output->name);
1710 + free(output);
1711 +}
1712 +
1713 +/**
1714 + * mpd_sendNotCommandsCommand
1715 + * odd naming, but it gets the not allowed commands
1716 + */
1717 +
1718 +void mpd_sendNotCommandsCommand(mpd_Connection * connection)
1719 +{
1720 + mpd_executeCommand(connection, "notcommands\n");
1721 +}
1722 +
1723 +/**
1724 + * mpd_sendCommandsCommand
1725 + * odd naming, but it gets the allowed commands
1726 + */
1727 +void mpd_sendCommandsCommand(mpd_Connection * connection)
1728 +{
1729 + mpd_executeCommand(connection, "commands\n");
1730 +}
1731 +
1732 +/**
1733 + * Get the next returned command
1734 + */
1735 +char *mpd_getNextCommand(mpd_Connection * connection)
1736 +{
1737 + return mpd_getNextReturnElementNamed(connection, "command");
1738 +}
1739 +
1740 +void mpd_sendUrlHandlersCommand(mpd_Connection * connection)
1741 +{
1742 + mpd_executeCommand(connection, "urlhandlers\n");
1743 +}
1744 +
1745 +char *mpd_getNextHandler(mpd_Connection * connection)
1746 +{
1747 + return mpd_getNextReturnElementNamed(connection, "handler");
1748 +}
1749 +
1750 +void mpd_sendTagTypesCommand(mpd_Connection * connection)
1751 +{
1752 + mpd_executeCommand(connection, "tagtypes\n");
1753 +}
1754 +
1755 +char *mpd_getNextTagType(mpd_Connection * connection)
1756 +{
1757 + return mpd_getNextReturnElementNamed(connection, "tagtype");
1758 +}
1759 +
1760 +void mpd_startSearch(mpd_Connection * connection, int exact)
1761 +{
1762 + if (connection->request) {
1763 + strcpy(connection->errorStr, "search already in progress");
1764 + connection->error = 1;
1765 + return;
1766 + }
1767 +
1768 + if (exact)
1769 + connection->request = strdup("find");
1770 + else
1771 + connection->request = strdup("search");
1772 +}
1773 +
1774 +void mpd_startStatsSearch(mpd_Connection * connection)
1775 +{
1776 + if (connection->request) {
1777 + strcpy(connection->errorStr, "search already in progress");
1778 + connection->error = 1;
1779 + return;
1780 + }
1781 +
1782 + connection->request = strdup("count");
1783 +}
1784 +
1785 +void mpd_startPlaylistSearch(mpd_Connection * connection, int exact)
1786 +{
1787 + if (connection->request) {
1788 + strcpy(connection->errorStr, "search already in progress");
1789 + connection->error = 1;
1790 + return;
1791 + }
1792 +
1793 + if (exact)
1794 + connection->request = strdup("playlistfind");
1795 + else
1796 + connection->request = strdup("playlistsearch");
1797 +}
1798 +
1799 +void mpd_startFieldSearch(mpd_Connection * connection, int type)
1800 +{
1801 + char *strtype;
1802 + int len;
1803 +
1804 + if (connection->request) {
1805 + strcpy(connection->errorStr, "search already in progress");
1806 + connection->error = 1;
1807 + return;
1808 + }
1809 +
1810 + if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
1811 + strcpy(connection->errorStr, "invalid type specified");
1812 + connection->error = 1;
1813 + return;
1814 + }
1815 +
1816 + strtype = mpdTagItemKeys[type];
1817 +
1818 + len = 5 + strlen(strtype) + 1;
1819 + connection->request = malloc(len);
1820 +
1821 + snprintf(connection->request, len, "list %c%s", tolower(strtype[0]), strtype + 1);
1822 +}
1823 +
1824 +void mpd_addConstraintSearch(mpd_Connection * connection, int type, const char *name)
1825 +{
1826 + char *strtype;
1827 + char *arg;
1828 + int len;
1829 + char *string;
1830 +
1831 + if (!connection->request) {
1832 + strcpy(connection->errorStr, "no search in progress");
1833 + connection->error = 1;
1834 + return;
1835 + }
1836 +
1837 + if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
1838 + strcpy(connection->errorStr, "invalid type specified");
1839 + connection->error = 1;
1840 + return;
1841 + }
1842 +
1843 + if (name == NULL) {
1844 + strcpy(connection->errorStr, "no name specified");
1845 + connection->error = 1;
1846 + return;
1847 + }
1848 +
1849 + string = strdup(connection->request);
1850 + strtype = mpdTagItemKeys[type];
1851 + arg = mpd_sanitizeArg(name);
1852 +
1853 + len = strlen(string) + 1 + strlen(strtype) + 2 + strlen(arg) + 2;
1854 + connection->request = realloc(connection->request, len);
1855 + snprintf(connection->request, len, "%s %c%s \"%s\"", string, tolower(strtype[0]), strtype + 1, arg);
1856 +
1857 + free(string);
1858 + free(arg);
1859 +}
1860 +
1861 +void mpd_commitSearch(mpd_Connection * connection)
1862 +{
1863 + int len;
1864 +
1865 + if (!connection->request) {
1866 + strcpy(connection->errorStr, "no search in progress");
1867 + connection->error = 1;
1868 + return;
1869 + }
1870 +
1871 + len = strlen(connection->request) + 2;
1872 + connection->request = realloc(connection->request, len);
1873 + connection->request[len - 2] = '\n';
1874 + connection->request[len - 1] = '\0';
1875 + mpd_sendInfoCommand(connection, connection->request);
1876 +
1877 + free(connection->request);
1878 + connection->request = NULL;
1879 +}
1880 +
1881 +/**
1882 + * @param connection a MpdConnection
1883 + * @param path the path to the playlist.
1884 + *
1885 + * List the content, with full metadata, of a stored playlist.
1886 + *
1887 + */
1888 +void mpd_sendListPlaylistInfoCommand(mpd_Connection * connection, char *path)
1889 +{
1890 + char *arg = mpd_sanitizeArg(path);
1891 + int len = strlen("listplaylistinfo") + 2 + strlen(arg) + 3;
1892 + char *query = malloc(len);
1893 + snprintf(query, len, "listplaylistinfo \"%s\"\n", arg);
1894 + mpd_sendInfoCommand(connection, query);
1895 + free(arg);
1896 + free(query);
1897 +}
1898 +
1899 +/**
1900 + * @param connection a MpdConnection
1901 + * @param path the path to the playlist.
1902 + *
1903 + * List the content of a stored playlist.
1904 + *
1905 + */
1906 +void mpd_sendListPlaylistCommand(mpd_Connection * connection, char *path)
1907 +{
1908 + char *arg = mpd_sanitizeArg(path);
1909 + int len = strlen("listplaylist") + 2 + strlen(arg) + 3;
1910 + char *query = malloc(len);
1911 + snprintf(query, len, "listplaylist \"%s\"\n", arg);
1912 + mpd_sendInfoCommand(connection, query);
1913 + free(arg);
1914 + free(query);
1915 +}
1916 +
1917 +void mpd_sendPlaylistClearCommand(mpd_Connection * connection, char *path)
1918 +{
1919 + char *sPath = mpd_sanitizeArg(path);
1920 + int len = strlen("playlistclear") + 2 + strlen(sPath) + 3;
1921 + char *string = malloc(len);
1922 + snprintf(string, len, "playlistclear \"%s\"\n", sPath);
1923 + mpd_executeCommand(connection, string);
1924 + free(sPath);
1925 + free(string);
1926 +}
1927 +
1928 +void mpd_sendPlaylistAddCommand(mpd_Connection * connection, char *playlist, char *path)
1929 +{
1930 + char *sPlaylist = mpd_sanitizeArg(playlist);
1931 + char *sPath = mpd_sanitizeArg(path);
1932 + int len = strlen("playlistadd") + 2 + strlen(sPlaylist) + 3 + strlen(sPath) + 3;
1933 + char *string = malloc(len);
1934 + snprintf(string, len, "playlistadd \"%s\" \"%s\"\n", sPlaylist, sPath);
1935 + mpd_executeCommand(connection, string);
1936 + free(sPlaylist);
1937 + free(sPath);
1938 + free(string);
1939 +}
1940 +
1941 +void mpd_sendPlaylistMoveCommand(mpd_Connection * connection, char *playlist, int from, int to)
1942 +{
1943 + char *sPlaylist = mpd_sanitizeArg(playlist);
1944 + int len = strlen("playlistmove") + 2 + strlen(sPlaylist) + 3 + INTLEN + 3 + INTLEN + 3;
1945 + char *string = malloc(len);
1946 + snprintf(string, len, "playlistmove \"%s\" \"%i\" \"%i\"\n", sPlaylist, from, to);
1947 + mpd_executeCommand(connection, string);
1948 + free(sPlaylist);
1949 + free(string);
1950 +}
1951 +
1952 +void mpd_sendPlaylistDeleteCommand(mpd_Connection * connection, char *playlist, int pos)
1953 +{
1954 + char *sPlaylist = mpd_sanitizeArg(playlist);
1955 + int len = strlen("playlistdelete") + 2 + strlen(sPlaylist) + 3 + INTLEN + 3;
1956 + char *string = malloc(len);
1957 + snprintf(string, len, "playlistdelete \"%s\" \"%i\"\n", sPlaylist, pos);
1958 + mpd_executeCommand(connection, string);
1959 + free(sPlaylist);
1960 + free(string);
1961 +}
1962 diff -urN lcd4linux-r870.orig/libmpdclient.h lcd4linux-r870/libmpdclient.h
1963 --- lcd4linux-r870.orig/libmpdclient.h 1970-01-01 01:00:00.000000000 +0100
1964 +++ lcd4linux-r870/libmpdclient.h 2008-03-26 04:43:32.000000000 +0100
1965 @@ -0,0 +1,661 @@
1966 +/* libmpdclient
1967 + (c)2003-2006 by Warren Dukes (warren.dukes@gmail.com)
1968 + This project's homepage is: http://www.musicpd.org
1969 +
1970 + Redistribution and use in source and binary forms, with or without
1971 + modification, are permitted provided that the following conditions
1972 + are met:
1973 +
1974 + - Redistributions of source code must retain the above copyright
1975 + notice, this list of conditions and the following disclaimer.
1976 +
1977 + - Redistributions in binary form must reproduce the above copyright
1978 + notice, this list of conditions and the following disclaimer in the
1979 + documentation and/or other materials provided with the distribution.
1980 +
1981 + - Neither the name of the Music Player Daemon nor the names of its
1982 + contributors may be used to endorse or promote products derived from
1983 + this software without specific prior written permission.
1984 +
1985 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1986 + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1987 + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1988 + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
1989 + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1990 + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1991 + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1992 + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1993 + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1994 + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1995 + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1996 +*/
1997 +
1998 +#ifndef LIBMPDCLIENT_H
1999 +#define LIBMPDCLIENT_H
2000 +
2001 +#ifdef WIN32
2002 +# define __W32API_USE_DLLIMPORT__ 1
2003 +#endif
2004 +
2005 +#include <sys/time.h>
2006 +#include <stdarg.h>
2007 +#define MPD_BUFFER_MAX_LENGTH 50000
2008 +#define MPD_ERRORSTR_MAX_LENGTH 1000
2009 +#define MPD_WELCOME_MESSAGE "OK MPD "
2010 +
2011 +#define MPD_ERROR_TIMEOUT 10 /* timeout trying to talk to mpd */
2012 +#define MPD_ERROR_SYSTEM 11 /* system error */
2013 +#define MPD_ERROR_UNKHOST 12 /* unknown host */
2014 +#define MPD_ERROR_CONNPORT 13 /* problems connecting to port on host */
2015 +#define MPD_ERROR_NOTMPD 14 /* mpd not running on port at host */
2016 +#define MPD_ERROR_NORESPONSE 15 /* no response on attempting to connect */
2017 +#define MPD_ERROR_SENDING 16 /* error sending command */
2018 +#define MPD_ERROR_CONNCLOSED 17 /* connection closed by mpd */
2019 +#define MPD_ERROR_ACK 18 /* ACK returned! */
2020 +#define MPD_ERROR_BUFFEROVERRUN 19 /* Buffer was overrun! */
2021 +
2022 +#define MPD_ACK_ERROR_UNK -1
2023 +#define MPD_ERROR_AT_UNK -1
2024 +
2025 +#define MPD_ACK_ERROR_NOT_LIST 1
2026 +#define MPD_ACK_ERROR_ARG 2
2027 +#define MPD_ACK_ERROR_PASSWORD 3
2028 +#define MPD_ACK_ERROR_PERMISSION 4
2029 +#define MPD_ACK_ERROR_UNKNOWN_CMD 5
2030 +
2031 +#define MPD_ACK_ERROR_NO_EXIST 50
2032 +#define MPD_ACK_ERROR_PLAYLIST_MAX 51
2033 +#define MPD_ACK_ERROR_SYSTEM 52
2034 +#define MPD_ACK_ERROR_PLAYLIST_LOAD 53
2035 +#define MPD_ACK_ERROR_UPDATE_ALREADY 54
2036 +#define MPD_ACK_ERROR_PLAYER_SYNC 55
2037 +#define MPD_ACK_ERROR_EXIST 56
2038 +
2039 +#ifdef __cplusplus
2040 +extern "C" {
2041 +#endif
2042 +
2043 + typedef enum mpd_TagItems {
2044 + MPD_TAG_ITEM_ARTIST,
2045 + MPD_TAG_ITEM_ALBUM,
2046 + MPD_TAG_ITEM_TITLE,
2047 + MPD_TAG_ITEM_TRACK,
2048 + MPD_TAG_ITEM_NAME,
2049 + MPD_TAG_ITEM_GENRE,
2050 + MPD_TAG_ITEM_DATE,
2051 + MPD_TAG_ITEM_COMPOSER,
2052 + MPD_TAG_ITEM_PERFORMER,
2053 + MPD_TAG_ITEM_COMMENT,
2054 + MPD_TAG_ITEM_DISC,
2055 + MPD_TAG_ITEM_FILENAME,
2056 + MPD_TAG_ITEM_ANY,
2057 + MPD_TAG_NUM_OF_ITEM_TYPES
2058 + } mpd_TagItems;
2059 +
2060 + extern char *mpdTagItemKeys[MPD_TAG_NUM_OF_ITEM_TYPES];
2061 +
2062 +/* internal stuff don't touch this struct */
2063 + typedef struct _mpd_ReturnElement {
2064 + char *name;
2065 + char *value;
2066 + } mpd_ReturnElement;
2067 +
2068 +/* mpd_Connection
2069 + * holds info about connection to mpd
2070 + * use error, and errorStr to detect errors
2071 + */
2072 + typedef struct _mpd_Connection {
2073 + /* use this to check the version of mpd */
2074 + int version[3];
2075 + /* IMPORTANT, you want to get the error messages from here */
2076 + char errorStr[MPD_ERRORSTR_MAX_LENGTH + 1];
2077 + int errorCode;
2078 + int errorAt;
2079 + /* this will be set to MPD_ERROR_* if there is an error, 0 if not */
2080 + int error;
2081 + /* DON'T TOUCH any of the rest of this stuff */
2082 + int sock;
2083 + char buffer[MPD_BUFFER_MAX_LENGTH + 1];
2084 + int buflen;
2085 + int bufstart;
2086 + int doneProcessing;
2087 + int listOks;
2088 + int doneListOk;
2089 + int commandList;
2090 + mpd_ReturnElement *returnElement;
2091 + struct timeval timeout;
2092 + char *request;
2093 + } mpd_Connection;
2094 +
2095 +/* mpd_newConnection
2096 + * use this to open a new connection
2097 + * you should use mpd_closeConnection, when your done with the connection,
2098 + * even if an error has occurred
2099 + * _timeout_ is the connection timeout period in seconds
2100 + */
2101 + mpd_Connection *mpd_newConnection(const char *host, int port, float timeout);
2102 +
2103 + void mpd_setConnectionTimeout(mpd_Connection * connection, float timeout);
2104 +
2105 +/* mpd_closeConnection
2106 + * use this to close a connection and free'ing subsequent memory
2107 + */
2108 + void mpd_closeConnection(mpd_Connection * connection);
2109 +
2110 +/* mpd_clearError
2111 + * clears error
2112 + */
2113 + void mpd_clearError(mpd_Connection * connection);
2114 +
2115 +/* STATUS STUFF */
2116 +
2117 +/* use these with status.state to determine what state the player is in */
2118 +#define MPD_STATUS_STATE_UNKNOWN 0
2119 +#define MPD_STATUS_STATE_STOP 1
2120 +#define MPD_STATUS_STATE_PLAY 2
2121 +#define MPD_STATUS_STATE_PAUSE 3
2122 +
2123 +/* us this with status.volume to determine if mpd has volume support */
2124 +#define MPD_STATUS_NO_VOLUME -1
2125 +
2126 +/* mpd_Status
2127 + * holds info return from status command
2128 + */
2129 + typedef struct mpd_Status {
2130 + /* 0-100, or MPD_STATUS_NO_VOLUME when there is no volume support */
2131 + int volume;
2132 + /* 1 if repeat is on, 0 otherwise */
2133 + int repeat;
2134 + /* 1 if random is on, 0 otherwise */
2135 + int random;
2136 + /* playlist length */
2137 + int playlistLength;
2138 + /* playlist, use this to determine when the playlist has changed */
2139 + long long playlist;
2140 + /* use with MPD_STATUS_STATE_* to determine state of player */
2141 + int state;
2142 + /* crossfade setting in seconds */
2143 + int crossfade;
2144 + /* if a song is currently selected (always the case when state is
2145 + * PLAY or PAUSE), this is the position of the currently
2146 + * playing song in the playlist, beginning with 0
2147 + */
2148 + int song;
2149 + /* Song ID of the currently selected song */
2150 + int songid;
2151 + /* time in seconds that have elapsed in the currently playing/paused
2152 + * song
2153 + */
2154 + int elapsedTime;
2155 + /* length in seconds of the currently playing/paused song */
2156 + int totalTime;
2157 + /* current bit rate in kbs */
2158 + int bitRate;
2159 + /* audio sample rate */
2160 + unsigned int sampleRate;
2161 + /* audio bits */
2162 + int bits;
2163 + /* audio channels */
2164 + int channels;
2165 + /* 1 if mpd is updating, 0 otherwise */
2166 + int updatingDb;
2167 + /* error */
2168 + char *error;
2169 + } mpd_Status;
2170 +
2171 + void mpd_sendStatusCommand(mpd_Connection * connection);
2172 +
2173 +/* mpd_getStatus
2174 + * returns status info, be sure to free it with mpd_freeStatus()
2175 + * call this after mpd_sendStatusCommand()
2176 + */
2177 + mpd_Status *mpd_getStatus(mpd_Connection * connection);
2178 +
2179 +/* mpd_freeStatus
2180 + * free's status info malloc'd and returned by mpd_getStatus
2181 + */
2182 + void mpd_freeStatus(mpd_Status * status);
2183 +
2184 + typedef struct _mpd_Stats {
2185 + int numberOfArtists;
2186 + int numberOfAlbums;
2187 + int numberOfSongs;
2188 + unsigned long uptime;
2189 + unsigned long dbUpdateTime;
2190 + unsigned long playTime;
2191 + unsigned long dbPlayTime;
2192 + } mpd_Stats;
2193 +
2194 + typedef struct _mpd_SearchStats {
2195 + int numberOfSongs;
2196 + unsigned long playTime;
2197 + } mpd_SearchStats;
2198 +
2199 + void mpd_sendStatsCommand(mpd_Connection * connection);
2200 +
2201 + mpd_Stats *mpd_getStats(mpd_Connection * connection);
2202 +
2203 + void mpd_freeStats(mpd_Stats * stats);
2204 +
2205 + mpd_SearchStats *mpd_getSearchStats(mpd_Connection * connection);
2206 +
2207 + void mpd_freeSearchStats(mpd_SearchStats * stats);
2208 +
2209 +/* SONG STUFF */
2210 +
2211 +#define MPD_SONG_NO_TIME -1
2212 +#define MPD_SONG_NO_NUM -1
2213 +#define MPD_SONG_NO_ID -1
2214 +
2215 +/* mpd_Song
2216 + * for storing song info returned by mpd
2217 + */
2218 + typedef struct _mpd_Song {
2219 + /* filename of song */
2220 + char *file;
2221 + /* artist, maybe NULL if there is no tag */
2222 + char *artist;
2223 + /* title, maybe NULL if there is no tag */
2224 + char *title;
2225 + /* album, maybe NULL if there is no tag */
2226 + char *album;
2227 + /* track, maybe NULL if there is no tag */
2228 + char *track;
2229 + /* name, maybe NULL if there is no tag; it's the name of the current
2230 + * song, f.e. the icyName of the stream */
2231 + char *name;
2232 + /* date */
2233 + char *date;
2234 +
2235 + /* added by qball */
2236 + /* Genre */
2237 + char *genre;
2238 + /* Composer */
2239 + char *composer;
2240 + /* Performer */
2241 + char *performer;
2242 + /* Disc */
2243 + char *disc;
2244 + /* Comment */
2245 + char *comment;
2246 +
2247 + /* length of song in seconds, check that it is not MPD_SONG_NO_TIME */
2248 + int time;
2249 + /* if plchanges/playlistinfo/playlistid used, is the position of the
2250 + * song in the playlist */
2251 + int pos;
2252 + /* song id for a song in the playlist */
2253 + int id;
2254 + } mpd_Song;
2255 +
2256 +/* mpd_newSong
2257 + * use to allocate memory for a new mpd_Song
2258 + * file, artist, etc all initialized to NULL
2259 + * if your going to assign values to file, artist, etc
2260 + * be sure to malloc or strdup the memory
2261 + * use mpd_freeSong to free the memory for the mpd_Song, it will also
2262 + * free memory for file, artist, etc, so don't do it yourself
2263 + */
2264 + mpd_Song *mpd_newSong(void);
2265 +
2266 +/* mpd_freeSong
2267 + * use to free memory allocated by mpd_newSong
2268 + * also it will free memory pointed to by file, artist, etc, so be careful
2269 + */
2270 + void mpd_freeSong(mpd_Song * song);
2271 +
2272 +/* mpd_songDup
2273 + * works like strDup, but for a mpd_Song
2274 + */
2275 + mpd_Song *mpd_songDup(mpd_Song * song);
2276 +
2277 +/* DIRECTORY STUFF */
2278 +
2279 +/* mpd_Directory
2280 + * used to store info fro directory (right now that just the path)
2281 + */
2282 + typedef struct _mpd_Directory {
2283 + char *path;
2284 + } mpd_Directory;
2285 +
2286 +/* mpd_newDirectory
2287 + * allocates memory for a new directory
2288 + * use mpd_freeDirectory to free this memory
2289 + */
2290 + mpd_Directory *mpd_newDirectory(void);
2291 +
2292 +/* mpd_freeDirectory
2293 + * used to free memory allocated with mpd_newDirectory, and it frees
2294 + * path of mpd_Directory, so be careful
2295 + */
2296 + void mpd_freeDirectory(mpd_Directory * directory);
2297 +
2298 +/* mpd_directoryDup
2299 + * works like strdup, but for mpd_Directory
2300 + */
2301 + mpd_Directory *mpd_directoryDup(mpd_Directory * directory);
2302 +
2303 +/* PLAYLISTFILE STUFF */
2304 +
2305 +/* mpd_PlaylistFile
2306 + * stores info about playlist file returned by lsinfo
2307 + */
2308 + typedef struct _mpd_PlaylistFile {
2309 + char *path;
2310 + } mpd_PlaylistFile;
2311 +
2312 +/* mpd_newPlaylistFile
2313 + * allocates memory for new mpd_PlaylistFile, path is set to NULL
2314 + * free this memory with mpd_freePlaylistFile
2315 + */
2316 + mpd_PlaylistFile *mpd_newPlaylistFile(void);
2317 +
2318 +/* mpd_freePlaylist
2319 + * free memory allocated for freePlaylistFile, will also free
2320 + * path, so be careful
2321 + */
2322 + void mpd_freePlaylistFile(mpd_PlaylistFile * playlist);
2323 +
2324 +/* mpd_playlistFileDup
2325 + * works like strdup, but for mpd_PlaylistFile
2326 + */
2327 + mpd_PlaylistFile *mpd_playlistFileDup(mpd_PlaylistFile * playlist);
2328 +
2329 +/* INFO ENTITY STUFF */
2330 +
2331 +/* the type of entity returned from one of the commands that generates info
2332 + * use in conjunction with mpd_InfoEntity.type
2333 + */
2334 +#define MPD_INFO_ENTITY_TYPE_DIRECTORY 0
2335 +#define MPD_INFO_ENTITY_TYPE_SONG 1
2336 +#define MPD_INFO_ENTITY_TYPE_PLAYLISTFILE 2
2337 +
2338 +/* mpd_InfoEntity
2339 + * stores info on stuff returned info commands
2340 + */
2341 + typedef struct mpd_InfoEntity {
2342 + /* the type of entity, use with MPD_INFO_ENTITY_TYPE_* to determine
2343 + * what this entity is (song, directory, etc...)
2344 + */
2345 + int type;
2346 + /* the actual data you want, mpd_Song, mpd_Directory, etc */
2347 + union {
2348 + mpd_Directory *directory;
2349 + mpd_Song *song;
2350 + mpd_PlaylistFile *playlistFile;
2351 + } info;
2352 + } mpd_InfoEntity;
2353 +
2354 + mpd_InfoEntity *mpd_newInfoEntity(void);
2355 +
2356 + void mpd_freeInfoEntity(mpd_InfoEntity * entity);
2357 +
2358 +/* INFO COMMANDS AND STUFF */
2359 +
2360 +/* use this function to loop over after calling Info/Listall functions */
2361 + mpd_InfoEntity *mpd_getNextInfoEntity(mpd_Connection * connection);
2362 +
2363 +/* fetches the currently seeletect song (the song referenced by status->song
2364 + * and status->songid*/
2365 + void mpd_sendCurrentSongCommand(mpd_Connection * connection);
2366 +
2367 +/* songNum of -1, means to display the whole list */
2368 + void mpd_sendPlaylistInfoCommand(mpd_Connection * connection, int songNum);
2369 +
2370 +/* songId of -1, means to display the whole list */
2371 + void mpd_sendPlaylistIdCommand(mpd_Connection * connection, int songId);
2372 +
2373 +/* use this to get the changes in the playlist since version _playlist_ */
2374 + void mpd_sendPlChangesCommand(mpd_Connection * connection, long long playlist);
2375 +
2376 +/**
2377 + * @param connection: A valid and connected mpd_Connection.
2378 + * @param playlist: The playlist version you want the diff with.
2379 + * A more bandwidth efficient version of the mpd_sendPlChangesCommand.
2380 + * It only returns the pos+id of the changes song.
2381 + */
2382 + void mpd_sendPlChangesPosIdCommand(mpd_Connection * connection, long long playlist);
2383 +
2384 +/* recursivel fetches all songs/dir/playlists in "dir* (no metadata is
2385 + * returned) */
2386 + void mpd_sendListallCommand(mpd_Connection * connection, const char *dir);
2387 +
2388 +/* same as sendListallCommand, but also metadata is returned */
2389 + void mpd_sendListallInfoCommand(mpd_Connection * connection, const char *dir);
2390 +
2391 +/* non-recursive version of ListallInfo */
2392 + void mpd_sendLsInfoCommand(mpd_Connection * connection, const char *dir);
2393 +
2394 +#define MPD_TABLE_ARTIST MPD_TAG_ITEM_ARTIST
2395 +#define MPD_TABLE_ALBUM MPD_TAG_ITEM_ALBUM
2396 +#define MPD_TABLE_TITLE MPD_TAG_ITEM_TITLE
2397 +#define MPD_TABLE_FILENAME MPD_TAG_ITEM_FILENAME
2398 +
2399 + void mpd_sendSearchCommand(mpd_Connection * connection, int table, const char *str);
2400 +
2401 + void mpd_sendFindCommand(mpd_Connection * connection, int table, const char *str);
2402 +
2403 +/* LIST TAG COMMANDS */
2404 +
2405 +/* use this function fetch next artist entry, be sure to free the returned
2406 + * string. NULL means there are no more. Best used with sendListArtists
2407 + */
2408 + char *mpd_getNextArtist(mpd_Connection * connection);
2409 +
2410 + char *mpd_getNextAlbum(mpd_Connection * connection);
2411 +
2412 + char *mpd_getNextTag(mpd_Connection * connection, int type);
2413 +
2414 +/* list artist or albums by artist, arg1 should be set to the artist if
2415 + * listing albums by a artist, otherwise NULL for listing all artists or albums
2416 + */
2417 + void mpd_sendListCommand(mpd_Connection * connection, int table, const char *arg1);
2418 +
2419 +/* SIMPLE COMMANDS */
2420 +
2421 + void mpd_sendAddCommand(mpd_Connection * connection, const char *file);
2422 +
2423 + int mpd_sendAddIdCommand(mpd_Connection * connection, const char *file);
2424 +
2425 + void mpd_sendDeleteCommand(mpd_Connection * connection, int songNum);
2426 +
2427 + void mpd_sendDeleteIdCommand(mpd_Connection * connection, int songNum);
2428 +
2429 + void mpd_sendSaveCommand(mpd_Connection * connection, const char *name);
2430 +
2431 + void mpd_sendLoadCommand(mpd_Connection * connection, const char *name);
2432 +
2433 + void mpd_sendRmCommand(mpd_Connection * connection, const char *name);
2434 +
2435 + void mpd_sendRenameCommand(mpd_Connection * connection, const char *from, const char *to);
2436 +
2437 + void mpd_sendShuffleCommand(mpd_Connection * connection);
2438 +
2439 + void mpd_sendClearCommand(mpd_Connection * connection);
2440 +
2441 +/* use this to start playing at the beginning, useful when in random mode */
2442 +#define MPD_PLAY_AT_BEGINNING -1
2443 +
2444 + void mpd_sendPlayCommand(mpd_Connection * connection, int songNum);
2445 +
2446 + void mpd_sendPlayIdCommand(mpd_Connection * connection, int songNum);
2447 +
2448 + void mpd_sendStopCommand(mpd_Connection * connection);
2449 +
2450 + void mpd_sendPauseCommand(mpd_Connection * connection, int pauseMode);
2451 +
2452 + void mpd_sendNextCommand(mpd_Connection * connection);
2453 +
2454 + void mpd_sendPrevCommand(mpd_Connection * connection);
2455 +
2456 + void mpd_sendMoveCommand(mpd_Connection * connection, int from, int to);
2457 +
2458 + void mpd_sendMoveIdCommand(mpd_Connection * connection, int from, int to);
2459 +
2460 + void mpd_sendSwapCommand(mpd_Connection * connection, int song1, int song2);
2461 +
2462 + void mpd_sendSwapIdCommand(mpd_Connection * connection, int song1, int song2);
2463 +
2464 + void mpd_sendSeekCommand(mpd_Connection * connection, int song, int time);
2465 +
2466 + void mpd_sendSeekIdCommand(mpd_Connection * connection, int song, int time);
2467 +
2468 + void mpd_sendRepeatCommand(mpd_Connection * connection, int repeatMode);
2469 +
2470 + void mpd_sendRandomCommand(mpd_Connection * connection, int randomMode);
2471 +
2472 + void mpd_sendSetvolCommand(mpd_Connection * connection, int volumeChange);
2473 +
2474 +/* WARNING: don't use volume command, its depreacted */
2475 + void mpd_sendVolumeCommand(mpd_Connection * connection, int volumeChange);
2476 +
2477 + void mpd_sendCrossfadeCommand(mpd_Connection * connection, int seconds);
2478 +
2479 + void mpd_sendUpdateCommand(mpd_Connection * connection, char *path);
2480 +
2481 +/* returns the update job id, call this after a update command*/
2482 + int mpd_getUpdateId(mpd_Connection * connection);
2483 +
2484 + void mpd_sendPasswordCommand(mpd_Connection * connection, const char *pass);
2485 +
2486 +/* after executing a command, when your done with it to get its status
2487 + * (you want to check connection->error for an error)
2488 + */
2489 + void mpd_finishCommand(mpd_Connection * connection);
2490 +
2491 +/* command list stuff, use this to do things like add files very quickly */
2492 + void mpd_sendCommandListBegin(mpd_Connection * connection);
2493 +
2494 + void mpd_sendCommandListOkBegin(mpd_Connection * connection);
2495 +
2496 + void mpd_sendCommandListEnd(mpd_Connection * connection);
2497 +
2498 +/* advance to the next listOk
2499 + * returns 0 if advanced to the next list_OK,
2500 + * returns -1 if it advanced to an OK or ACK */
2501 + int mpd_nextListOkCommand(mpd_Connection * connection);
2502 +
2503 + typedef struct _mpd_OutputEntity {
2504 + int id;
2505 + char *name;
2506 + int enabled;
2507 + } mpd_OutputEntity;
2508 +
2509 + void mpd_sendOutputsCommand(mpd_Connection * connection);
2510 +
2511 + mpd_OutputEntity *mpd_getNextOutput(mpd_Connection * connection);
2512 +
2513 + void mpd_sendEnableOutputCommand(mpd_Connection * connection, int outputId);
2514 +
2515 + void mpd_sendDisableOutputCommand(mpd_Connection * connection, int outputId);
2516 +
2517 + void mpd_freeOutputElement(mpd_OutputEntity * output);
2518 +
2519 +/**
2520 + * @param connection a #mpd_Connection
2521 + *
2522 + * Queries mpd for the allowed commands
2523 + */
2524 + void mpd_sendCommandsCommand(mpd_Connection * connection);
2525 +
2526 +/**
2527 + * @param connection a #mpd_Connection
2528 + *
2529 + * Queries mpd for the not allowed commands
2530 + */
2531 + void mpd_sendNotCommandsCommand(mpd_Connection * connection);
2532 +
2533 +/**
2534 + * @param connection a #mpd_Connection
2535 + *
2536 + * returns the next supported command.
2537 + *
2538 + * @returns a string, needs to be free'ed
2539 + */
2540 + char *mpd_getNextCommand(mpd_Connection * connection);
2541 +
2542 + void mpd_sendUrlHandlersCommand(mpd_Connection * connection);
2543 +
2544 + char *mpd_getNextHandler(mpd_Connection * connection);
2545 +
2546 + void mpd_sendTagTypesCommand(mpd_Connection * connection);
2547 +
2548 + char *mpd_getNextTagType(mpd_Connection * connection);
2549 +
2550 +/**
2551 + * @param connection a MpdConnection
2552 + * @param path the path to the playlist.
2553 + *
2554 + * List the content, with full metadata, of a stored playlist.
2555 + *
2556 + */
2557 + void mpd_sendListPlaylistInfoCommand(mpd_Connection * connection, char *path);
2558 +
2559 +/**
2560 + * @param connection a MpdConnection
2561 + * @param path the path to the playlist.
2562 + *
2563 + * List the content of a stored playlist.
2564 + *
2565 + */
2566 + void mpd_sendListPlaylistCommand(mpd_Connection * connection, char *path);
2567 +
2568 +/**
2569 + * @param connection a #mpd_Connection
2570 + * @param exact if to match exact
2571 + *
2572 + * starts a search, use mpd_addConstraintSearch to add
2573 + * a constraint to the search, and mpd_commitSearch to do the actual search
2574 + */
2575 + void mpd_startSearch(mpd_Connection * connection, int exact);
2576 +
2577 +/**
2578 + * @param connection a #mpd_Connection
2579 + * @param type
2580 + * @param name
2581 + */
2582 + void mpd_addConstraintSearch(mpd_Connection * connection, int type, const char *name);
2583 +
2584 +/**
2585 + * @param connection a #mpd_Connection
2586 + */
2587 + void mpd_commitSearch(mpd_Connection * connection);
2588 +
2589 +/**
2590 + * @param connection a #mpd_Connection
2591 + * @param type The type to search for
2592 + *
2593 + * starts a search for fields... f.e. get a list of artists would be:
2594 + * @code
2595 + * mpd_startFieldSearch(connection, MPD_TAG_ITEM_ARTIST);
2596 + * mpd_commitSearch(connection);
2597 + * @endcode
2598 + *
2599 + * or get a list of artist in genre "jazz" would be:
2600 + * @code
2601 + * mpd_startFieldSearch(connection, MPD_TAG_ITEM_ARTIST);
2602 + * mpd_addConstraintSearch(connection, MPD_TAG_ITEM_GENRE, "jazz")
2603 + * mpd_commitSearch(connection);
2604 + * @endcode
2605 + *
2606 + * mpd_startSearch will return a list of songs (and you need mpd_getNextInfoEntity)
2607 + * this one will return a list of only one field (the one specified with type) and you need
2608 + * mpd_getNextTag to get the results
2609 + */
2610 + void mpd_startFieldSearch(mpd_Connection * connection, int type);
2611 +
2612 + void mpd_startPlaylistSearch(mpd_Connection * connection, int exact);
2613 +
2614 + void mpd_startStatsSearch(mpd_Connection * connection);
2615 +
2616 + void mpd_sendPlaylistClearCommand(mpd_Connection * connection, char *path);
2617 +
2618 + void mpd_sendPlaylistAddCommand(mpd_Connection * connection, char *playlist, char *path);
2619 +
2620 + void mpd_sendPlaylistMoveCommand(mpd_Connection * connection, char *playlist, int from, int to);
2621 +
2622 + void mpd_sendPlaylistDeleteCommand(mpd_Connection * connection, char *playlist, int pos);
2623 +#ifdef __cplusplus
2624 +}
2625 +#endif
2626 +#endif