2 * This file is part of DisOrder.
3 * Copyright (C) 2006 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 /** @file lib/eclient.c
21 * @brief Client code for event-driven programs
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
43 #include "configuration.h"
50 #include "inputline.h"
57 #include "client-common.h"
59 /* TODO: more commands */
61 /* Types *********************************************************************/
63 /** @brief Client state */
65 state_disconnected, /**< @brief not connected */
66 state_connecting, /**< @brief waiting for connect() */
67 state_connected, /**< @brief connected but not authenticated */
68 state_idle, /**< @brief not doing anything */
69 state_cmdresponse, /**< @brief waiting for command resonse */
70 state_body, /**< @brief accumulating body */
71 state_log, /**< @brief monitoring log */
74 /** @brief Names for @ref client_state */
75 static const char *const states[] = {
85 struct operation; /* forward decl */
87 /** @brief Type of an operation callback */
88 typedef void operation_callback(disorder_eclient *c, struct operation *op);
90 /** @brief A pending operation.
92 * This can be either a command or part of the authentication protocol. In the
93 * former case new commands are appended to the list, in the latter case they
94 * are inserted at the front. */
96 struct operation *next; /**< @brief next operation */
97 char *cmd; /**< @brief command to send or 0 */
98 operation_callback *opcallback; /**< @brief internal completion callback */
99 void (*completed)(); /**< @brief user completion callback or 0 */
100 void *v; /**< @brief data for COMPLETED */
101 disorder_eclient *client; /**< @brief owning client */
102 int sent; /**< @brief true if sent to server */
105 /** @brief Client structure */
106 struct disorder_eclient {
108 int fd; /**< @brief connection to server */
109 enum client_state state; /**< @brief current state */
110 int authenticated; /**< @brief true when authenicated */
111 struct dynstr output; /**< @brief output buffer */
112 struct dynstr input; /**< @brief input buffer */
113 int eof; /**< @brief input buffer is at EOF */
114 const disorder_eclient_callbacks *callbacks; /**< @brief error callbacks */
115 void *u; /**< @brief user data */
116 struct operation *ops; /**< @brief queue of operations */
117 struct operation **opstail; /**< @brief queue tail */
118 /* accumulated response */
119 int rc; /**< @brief response code */
120 char *line; /**< @brief complete line */
121 struct vector vec; /**< @brief body */
122 const disorder_eclient_log_callbacks *log_callbacks; /**< @brief log callbacks */
123 void *log_v; /**< @brief user data */
124 unsigned long statebits; /**< @brief current state */
127 /* Forward declarations ******************************************************/
129 static int start_connect(void *cc,
130 const struct sockaddr *sa,
133 static void process_line(disorder_eclient *c, char *line);
134 static int start_connect(void *cc,
135 const struct sockaddr *sa,
138 static void maybe_connected(disorder_eclient *c);
139 static void authbanner_opcallback(disorder_eclient *c,
140 struct operation *op);
141 static void authuser_opcallback(disorder_eclient *c,
142 struct operation *op);
143 static void complete(disorder_eclient *c);
144 static void send_output(disorder_eclient *c);
145 static void put(disorder_eclient *c, const char *s, size_t n);
146 static void read_input(disorder_eclient *c);
147 static void stash_command(disorder_eclient *c,
149 operation_callback *opcallback,
154 static void log_opcallback(disorder_eclient *c, struct operation *op);
155 static void logline(disorder_eclient *c, const char *line);
156 static void logentry_completed(disorder_eclient *c, int nvec, char **vec);
157 static void logentry_failed(disorder_eclient *c, int nvec, char **vec);
158 static void logentry_moved(disorder_eclient *c, int nvec, char **vec);
159 static void logentry_playing(disorder_eclient *c, int nvec, char **vec);
160 static void logentry_queue(disorder_eclient *c, int nvec, char **vec);
161 static void logentry_recent_added(disorder_eclient *c, int nvec, char **vec);
162 static void logentry_recent_removed(disorder_eclient *c, int nvec, char **vec);
163 static void logentry_removed(disorder_eclient *c, int nvec, char **vec);
164 static void logentry_scratched(disorder_eclient *c, int nvec, char **vec);
165 static void logentry_state(disorder_eclient *c, int nvec, char **vec);
166 static void logentry_volume(disorder_eclient *c, int nvec, char **vec);
168 /* Tables ********************************************************************/
170 /** @brief One possible log entry */
171 struct logentry_handler {
172 const char *name; /**< @brief Entry name */
173 int min; /**< @brief Minimum arguments */
174 int max; /**< @brief Maximum arguments */
175 void (*handler)(disorder_eclient *c,
177 char **vec); /**< @brief Handler function */
180 /** @brief Table for parsing log entries */
181 static const struct logentry_handler logentry_handlers[] = {
182 #define LE(X, MIN, MAX) { #X, MIN, MAX, logentry_##X }
187 LE(queue, 2, INT_MAX),
188 LE(recent_added, 2, INT_MAX),
189 LE(recent_removed, 1, 1),
196 /* Setup and teardown ********************************************************/
198 /** @brief Create a new client
200 * Does NOT connect the client - connections are made (and re-made) on demand.
202 disorder_eclient *disorder_eclient_new(const disorder_eclient_callbacks *cb,
204 disorder_eclient *c = xmalloc(sizeof *c);
205 D(("disorder_eclient_new"));
209 c->opstail = &c->ops;
210 vector_init(&c->vec);
211 dynstr_init(&c->input);
212 dynstr_init(&c->output);
213 if(!config->password) {
214 error(0, "no password set");
220 /** @brief Disconnect a client */
221 void disorder_eclient_close(disorder_eclient *c) {
222 struct operation *op;
224 D(("disorder_eclient_close"));
226 D(("disorder_eclient_close closing fd %d", c->fd));
227 c->callbacks->poll(c->u, c, c->fd, 0);
230 c->state = state_disconnected;
235 c->authenticated = 0;
236 /* We'll need to resend all operations */
237 for(op = c->ops; op; op = op->next)
241 /* Error reporting ***********************************************************/
243 /** @brief called when a connection error occurs */
244 static int comms_error(disorder_eclient *c, const char *fmt, ...) {
250 byte_xvasprintf(&s, fmt, ap);
252 disorder_eclient_close(c);
253 c->callbacks->comms_error(c->u, s);
257 /** @brief called when the server reports an error */
258 static int protocol_error(disorder_eclient *c, struct operation *op,
259 int code, const char *fmt, ...) {
263 D(("protocol_error"));
265 byte_xvasprintf(&s, fmt, ap);
267 c->callbacks->protocol_error(c->u, op->v, code, s);
271 /* State machine *************************************************************/
273 /** @brief Called when there's something to do
275 * @param mode bitmap of @ref DISORDER_POLL_READ and/or @ref DISORDER_POLL_WRITE.
277 * This should be called from by your code when the file descriptor is readable
278 * or writable (as requested by the @c poll callback, see @ref
279 * disorder_eclient_callbacks) and in any case from time to time (with @p mode
280 * = 0) to allow for retries to work.
282 void disorder_eclient_polled(disorder_eclient *c, unsigned mode) {
283 struct operation *op;
285 D(("disorder_eclient_polled fd=%d state=%s mode=[%s %s]",
286 c->fd, states[c->state],
287 mode & DISORDER_POLL_READ ? "READ" : "",
288 mode & DISORDER_POLL_WRITE ? "WRITE" : ""));
289 /* The pattern here is to check each possible state in turn and try to
290 * advance (though on error we might go back). If we advance we leave open
291 * the possibility of falling through to the next state, but we set the mode
292 * bits to 0, to avoid false positives (which matter more in some cases than
295 if(c->state == state_disconnected) {
296 D(("state_disconnected"));
297 with_sockaddr(c, start_connect);
298 /* might now be state_disconnected (on error), state_connecting (slow
299 * connect) or state_connected (fast connect). If state_disconnected then
300 * we just rely on a periodic callback from the event loop sometime. */
304 if(c->state == state_connecting && mode) {
305 D(("state_connecting"));
307 /* Might be state_disconnected (on error) or state_connected (on success).
308 * In the former case we rely on the event loop for a periodic callback to
313 if(c->state == state_connected) {
314 D(("state_connected"));
315 /* We just connected. Initiate the authentication protocol. */
316 stash_command(c, 1/*queuejump*/, authbanner_opcallback,
317 0/*completed*/, 0/*v*/, 0/*cmd*/);
318 /* We never stay is state_connected very long. We could in principle jump
319 * straight to state_cmdresponse since there's actually no command to
320 * send, but that would arguably be cheating. */
321 c->state = state_idle;
324 if(c->state == state_idle) {
326 /* We are connected, and have finished any command we set off, look for
330 if(c->authenticated) {
331 /* Transmit all unsent operations */
332 for(op = c->ops; op; op = op->next) {
334 put(c, op->cmd, strlen(op->cmd));
339 /* Just send the head operation */
340 if(c->ops->cmd && !c->ops->sent) {
341 put(c, c->ops->cmd, strlen(c->ops->cmd));
345 /* Awaiting response for the operation at the head of the list */
346 c->state = state_cmdresponse;
349 c->callbacks->report(c->u, 0);
352 if(c->state == state_cmdresponse
353 || c->state == state_body
354 || c->state == state_log) {
355 D(("state_%s", states[c->state]));
356 /* We are awaiting a response */
357 if(mode & DISORDER_POLL_WRITE) send_output(c);
358 if(mode & DISORDER_POLL_READ) read_input(c);
359 /* There are a couple of reasons we might want to re-enter the state
360 * machine from the top. state_idle is obvious: there may be further
361 * commands to process. Re-entering on state_disconnected means that we
362 * immediately retry connection if a comms error occurs during a command.
363 * This is different to the case where a connection fails, where we await a
364 * spontaneous call to initiate the retry. */
366 case state_disconnected: /* lost connection */
367 case state_idle: /* completed a command */
369 disorder_eclient_polled(c, 0);
376 /* Figure out what to set the mode to */
378 case state_disconnected:
379 D(("state_disconnected (2)"));
380 /* Probably an error occurred. Await a retry. */
383 case state_connecting:
384 D(("state_connecting (2)"));
385 /* Waiting for connect to complete */
386 mode = DISORDER_POLL_READ|DISORDER_POLL_WRITE;
388 case state_connected:
389 D(("state_connected (2)"));
390 assert(!"should never be in state_connected here");
393 D(("state_idle (2)"));
394 /* Connected but nothing to do. */
397 case state_cmdresponse:
400 D(("state_%s (2)", states[c->state]));
401 /* Gathering a response. Wait for input. */
402 mode = DISORDER_POLL_READ;
403 /* Flush any pending output. */
404 if(c->output.nvec) mode |= DISORDER_POLL_WRITE;
407 D(("fd=%d new mode [%s %s]",
409 mode & DISORDER_POLL_READ ? "READ" : "",
410 mode & DISORDER_POLL_WRITE ? "WRITE" : ""));
411 if(c->fd != -1) c->callbacks->poll(c->u, c, c->fd, mode);
414 /** @brief Called to start connecting */
415 static int start_connect(void *cc,
416 const struct sockaddr *sa,
419 disorder_eclient *c = cc;
421 D(("start_connect"));
422 c->ident = xstrdup(ident);
427 if((c->fd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0)
428 return comms_error(c, "socket: %s", strerror(errno));
431 if(connect(c->fd, sa, len) < 0) {
435 c->state = state_connecting;
436 /* We are called from _polled so the state machine will get to do its
440 /* Signal the error to the caller. */
441 return comms_error(c, "connecting to %s: %s", ident, strerror(errno));
444 c->state = state_connected;
448 /** @brief Called when maybe connected */
449 static void maybe_connected(disorder_eclient *c) {
450 /* We either connected, or got an error. */
452 socklen_t len = sizeof err;
454 D(("maybe_connected"));
455 /* Work around over-enthusiastic error slippage */
456 if(getsockopt(c->fd, SOL_SOCKET, SO_ERROR, &err, &len) < 0)
459 /* The connection failed */
460 comms_error(c, "connecting to %s: %s", c->ident, strerror(err));
461 /* sets state_disconnected */
463 /* The connection succeeded */
464 c->state = state_connected;
468 /* Authentication ************************************************************/
470 static void authbanner_opcallback(disorder_eclient *c,
471 struct operation *op) {
473 const unsigned char *nonce;
476 D(("authbanner_opcallback"));
477 if(c->rc / 100 != 2) {
478 /* Banner told us to go away. We cannot proceed. */
479 protocol_error(c, op, c->rc, "%s: %s", c->ident, c->line);
480 disorder_eclient_close(c);
483 nonce = unhex(c->line + 4, &nonce_len);
484 res = authhash(nonce, nonce_len, config->password);
485 stash_command(c, 1/*queuejump*/, authuser_opcallback, 0/*completed*/, 0/*v*/,
486 "user", quoteutf8(config->username), quoteutf8(res),
490 static void authuser_opcallback(disorder_eclient *c,
491 struct operation *op) {
492 D(("authuser_opcallback"));
493 if(c->rc / 100 != 2) {
494 /* Wrong password or something. We cannot proceed. */
495 protocol_error(c, op, c->rc, "%s: %s", c->ident, c->line);
496 disorder_eclient_close(c);
499 /* OK, we're authenticated now. */
500 c->authenticated = 1;
501 if(c->log_callbacks && !(c->ops && c->ops->opcallback == log_opcallback))
502 /* We are a log client, switch to logging mode */
503 stash_command(c, 0/*queuejump*/, log_opcallback, 0/*completed*/, c->log_v,
507 /* Output ********************************************************************/
509 /* Chop N bytes off the front of a dynstr */
510 static void consume(struct dynstr *d, int n) {
511 D(("consume %d", n));
512 assert(d->nvec >= n);
513 memmove(d->vec, d->vec + n, d->nvec - n);
517 /* Write some bytes */
518 static void put(disorder_eclient *c, const char *s, size_t n) {
519 D(("put %d %.*s", c->fd, (int)n, s));
520 dynstr_append_bytes(&c->output, s, n);
523 /* Called when we can write to our FD, or at any other time */
524 static void send_output(disorder_eclient *c) {
527 D(("send_output %d bytes pending", c->output.nvec));
528 if(c->state > state_connecting && c->output.nvec) {
529 n = write(c->fd, c->output.vec, c->output.nvec);
536 comms_error(c, "writing to %s: %s", c->ident, strerror(errno));
540 consume(&c->output, n);
544 /* Input *********************************************************************/
546 /* Called when c->fd might be readable, or at any other time */
547 static void read_input(disorder_eclient *c) {
552 D(("read_input in state %s", states[c->state]));
553 if(c->state <= state_connected) return; /* ignore bogus calls */
554 /* read some more input */
555 n = read(c->fd, buffer, sizeof buffer);
562 comms_error(c, "reading from %s: %s", c->ident, strerror(errno));
565 return; /* no new input to process */
567 D(("read %d bytes: [%.*s]", n, n, buffer));
568 dynstr_append_bytes(&c->input, buffer, n);
571 /* might have more than one line to process */
572 while(c->state > state_connecting
573 && (nl = memchr(c->input.vec, '\n', c->input.nvec))) {
574 process_line(c, xstrndup(c->input.vec, nl - c->input.vec));
575 /* we might have disconnected along the way, which zogs the input buffer */
576 if(c->state > state_connecting)
577 consume(&c->input, (nl - c->input.vec) + 1);
580 comms_error(c, "reading from %s: server disconnected", c->ident);
581 c->authenticated = 0;
585 /* called with a line that has just been read */
586 static void process_line(disorder_eclient *c, char *line) {
587 D(("process_line %d [%s]", c->fd, line));
589 case state_cmdresponse:
590 /* This is the first line of a response */
591 if(!(line[0] >= '0' && line[0] <= '9'
592 && line[1] >= '0' && line[1] <= '9'
593 && line[2] >= '0' && line[2] <= '9'
595 fatal(0, "invalid response from server: %s", line);
596 c->rc = (line[0] * 10 + line[1]) * 10 + line[2] - 111 * '0';
600 /* We need to collect the body. */
601 c->state = state_body;
605 assert(c->log_callbacks != 0);
606 if(c->log_callbacks->connected)
607 c->log_callbacks->connected(c->log_v);
608 c->state = state_log;
611 /* We've got the whole response. Go into the idle state so the state
612 * machine knows we're done and then call the operation callback. */
618 if(strcmp(line, ".")) {
619 /* A line from the body */
620 vector_append(&c->vec, line + (line[0] == '.'));
622 /* End of the body. */
623 vector_terminate(&c->vec);
628 if(strcmp(line, ".")) {
629 logline(c, line + (line[0] == '.'));
634 assert(!"wrong state for location");
639 /* Called when an operation completes */
640 static void complete(disorder_eclient *c) {
641 struct operation *op;
644 /* Pop the operation off the queue */
647 if(c->opstail == &op->next)
648 c->opstail = &c->ops;
649 /* If we've pipelined a command ahead then we go straight to cmdresponser.
650 * Otherwise we go to idle, which will arrange further sends. */
651 c->state = c->ops && c->ops->sent ? state_cmdresponse : state_idle;
652 op->opcallback(c, op);
653 /* Note that we always call the opcallback even on error, though command
654 * opcallbacks generally always do the same error handling, i.e. just call
655 * protocol_error(). It's the auth* opcallbacks that have different
659 /* Operation setup ***********************************************************/
661 static void stash_command_vector(disorder_eclient *c,
663 operation_callback *opcallback,
668 struct operation *op = xmalloc(sizeof *op);
674 for(n = 0; n < ncmd; ++n) {
676 dynstr_append(&d, ' ');
677 dynstr_append_string(&d, quoteutf8(cmd[n]));
679 dynstr_append(&d, '\n');
680 dynstr_terminate(&d);
683 op->cmd = 0; /* usually, awaiting challenge */
684 op->opcallback = opcallback;
685 op->completed = completed;
689 assert(op->sent == 0);
691 /* Authentication operations jump the queue of useful commands */
694 if(c->opstail == &c->ops)
695 c->opstail = &op->next;
696 for(op = c->ops; op; op = op->next)
700 c->opstail = &op->next;
704 static void vstash_command(disorder_eclient *c,
706 operation_callback *opcallback,
709 const char *cmd, va_list ap) {
713 D(("vstash_command %s", cmd ? cmd : "NULL"));
716 vector_append(&vec, (char *)cmd);
717 while((arg = va_arg(ap, char *)))
718 vector_append(&vec, arg);
719 stash_command_vector(c, queuejump, opcallback, completed, v,
722 stash_command_vector(c, queuejump, opcallback, completed, v, 0, 0);
725 static void stash_command(disorder_eclient *c,
727 operation_callback *opcallback,
735 vstash_command(c, queuejump, opcallback, completed, v, cmd, ap);
739 /* Command support ***********************************************************/
741 /* for commands with a simple string response */
742 static void string_response_opcallback(disorder_eclient *c,
743 struct operation *op) {
744 D(("string_response_callback"));
745 if(c->rc / 100 == 2) {
747 ((disorder_eclient_string_response *)op->completed)(op->v, c->line + 4);
749 protocol_error(c, op, c->rc, "%s: %s", c->ident, c->line);
752 /* for commands with a simple integer response */
753 static void integer_response_opcallback(disorder_eclient *c,
754 struct operation *op) {
755 D(("string_response_callback"));
756 if(c->rc / 100 == 2) {
758 ((disorder_eclient_integer_response *)op->completed)
759 (op->v, strtol(c->line + 4, 0, 10));
761 protocol_error(c, op, c->rc, "%s: %s", c->ident, c->line);
764 /* for commands with no response */
765 static void no_response_opcallback(disorder_eclient *c,
766 struct operation *op) {
767 D(("no_response_callback"));
768 if(c->rc / 100 == 2) {
770 ((disorder_eclient_no_response *)op->completed)(op->v);
772 protocol_error(c, op, c->rc, "%s: %s", c->ident, c->line);
775 /* error callback for queue_unmarshall */
776 static void eclient_queue_error(const char *msg,
778 struct operation *op = u;
780 protocol_error(op->client, op, -1, "error parsing queue entry: %s", msg);
783 /* for commands that expect a queue dump */
784 static void queue_response_opcallback(disorder_eclient *c,
785 struct operation *op) {
787 struct queue_entry *q, *qh = 0, **qtail = &qh, *qlast = 0;
789 D(("queue_response_callback"));
790 if(c->rc / 100 == 2) {
791 /* parse the queue */
792 for(n = 0; n < c->vec.nvec; ++n) {
793 q = xmalloc(sizeof *q);
794 D(("queue_unmarshall %s", c->vec.vec[n]));
795 if(!queue_unmarshall(q, c->vec.vec[n], eclient_queue_error, op)) {
803 ((disorder_eclient_queue_response *)op->completed)(op->v, qh);
805 protocol_error(c, op, c->rc, "%s: %s", c->ident, c->line);
809 static void playing_response_opcallback(disorder_eclient *c,
810 struct operation *op) {
811 struct queue_entry *q;
813 D(("playing_response_callback"));
814 if(c->rc / 100 == 2) {
817 if(queue_unmarshall(q = xmalloc(sizeof *q), c->line + 4,
818 eclient_queue_error, c))
825 protocol_error(c, op, c->rc, "%s: %s", c->ident, c->line);
829 ((disorder_eclient_queue_response *)op->completed)(op->v, q);
831 protocol_error(c, op, c->rc, "%s: %s", c->ident, c->line);
834 /* for commands that expect a list of some sort */
835 static void list_response_opcallback(disorder_eclient *c,
836 struct operation *op) {
837 D(("list_response_callback"));
838 if(c->rc / 100 == 2) {
840 ((disorder_eclient_list_response *)op->completed)(op->v,
844 protocol_error(c, op, c->rc, "%s: %s", c->ident, c->line);
848 static void volume_response_opcallback(disorder_eclient *c,
849 struct operation *op) {
852 D(("volume_response_callback"));
853 if(c->rc / 100 == 2) {
855 if(sscanf(c->line + 4, "%d %d", &l, &r) != 2 || l < 0 || r < 0)
856 protocol_error(c, op, -1, "%s: invalid volume response: %s",
859 ((disorder_eclient_volume_response *)op->completed)(op->v, l, r);
862 protocol_error(c, op, c->rc, "%s: %s", c->ident, c->line);
865 static int simple(disorder_eclient *c,
866 operation_callback *opcallback,
869 const char *cmd, ...) {
873 vstash_command(c, 0/*queuejump*/, opcallback, completed, v, cmd, ap);
875 /* Give the state machine a kick, since we might be in state_idle */
876 disorder_eclient_polled(c, 0);
880 /* Commands ******************************************************************/
882 int disorder_eclient_version(disorder_eclient *c,
883 disorder_eclient_string_response *completed,
885 return simple(c, string_response_opcallback, (void (*)())completed, v,
886 "version", (char *)0);
889 int disorder_eclient_namepart(disorder_eclient *c,
890 disorder_eclient_string_response *completed,
895 return simple(c, string_response_opcallback, (void (*)())completed, v,
896 "part", track, context, part, (char *)0);
899 int disorder_eclient_play(disorder_eclient *c,
901 disorder_eclient_no_response *completed,
903 return simple(c, no_response_opcallback, (void (*)())completed, v,
904 "play", track, (char *)0);
907 int disorder_eclient_pause(disorder_eclient *c,
908 disorder_eclient_no_response *completed,
910 return simple(c, no_response_opcallback, (void (*)())completed, v,
914 int disorder_eclient_resume(disorder_eclient *c,
915 disorder_eclient_no_response *completed,
917 return simple(c, no_response_opcallback, (void (*)())completed, v,
918 "resume", (char *)0);
921 int disorder_eclient_scratch(disorder_eclient *c,
923 disorder_eclient_no_response *completed,
925 return simple(c, no_response_opcallback, (void (*)())completed, v,
926 "scratch", id, (char *)0);
929 int disorder_eclient_scratch_playing(disorder_eclient *c,
930 disorder_eclient_no_response *completed,
932 return disorder_eclient_scratch(c, 0, completed, v);
935 int disorder_eclient_remove(disorder_eclient *c,
937 disorder_eclient_no_response *completed,
939 return simple(c, no_response_opcallback, (void (*)())completed, v,
940 "remove", id, (char *)0);
943 int disorder_eclient_moveafter(disorder_eclient *c,
947 disorder_eclient_no_response *completed,
953 vector_append(&vec, (char *)"moveafter");
954 vector_append(&vec, (char *)target);
955 for(n = 0; n < nids; ++n)
956 vector_append(&vec, (char *)ids[n]);
957 stash_command_vector(c, 0/*queuejump*/, no_response_opcallback, completed, v,
959 disorder_eclient_polled(c, 0);
963 int disorder_eclient_recent(disorder_eclient *c,
964 disorder_eclient_queue_response *completed,
966 return simple(c, queue_response_opcallback, (void (*)())completed, v,
967 "recent", (char *)0);
970 int disorder_eclient_queue(disorder_eclient *c,
971 disorder_eclient_queue_response *completed,
973 return simple(c, queue_response_opcallback, (void (*)())completed, v,
977 int disorder_eclient_files(disorder_eclient *c,
978 disorder_eclient_list_response *completed,
982 return simple(c, list_response_opcallback, (void (*)())completed, v,
983 "files", dir, re, (char *)0);
986 int disorder_eclient_dirs(disorder_eclient *c,
987 disorder_eclient_list_response *completed,
991 return simple(c, list_response_opcallback, (void (*)())completed, v,
992 "dirs", dir, re, (char *)0);
995 int disorder_eclient_playing(disorder_eclient *c,
996 disorder_eclient_queue_response *completed,
998 return simple(c, playing_response_opcallback, (void (*)())completed, v,
999 "playing", (char *)0);
1002 int disorder_eclient_length(disorder_eclient *c,
1003 disorder_eclient_integer_response *completed,
1006 return simple(c, integer_response_opcallback, (void (*)())completed, v,
1007 "length", track, (char *)0);
1010 int disorder_eclient_volume(disorder_eclient *c,
1011 disorder_eclient_volume_response *completed,
1014 char sl[64], sr[64];
1016 if(l < 0 && r < 0) {
1017 return simple(c, volume_response_opcallback, (void (*)())completed, v,
1018 "volume", (char *)0);
1019 } else if(l >= 0 && r >= 0) {
1022 byte_snprintf(sl, sizeof sl, "%d", l);
1023 byte_snprintf(sr, sizeof sr, "%d", r);
1024 return simple(c, volume_response_opcallback, (void (*)())completed, v,
1025 "volume", sl, sr, (char *)0);
1027 assert(!"invalid arguments to disorder_eclient_volume");
1028 return -1; /* gcc is being dim */
1032 int disorder_eclient_enable(disorder_eclient *c,
1033 disorder_eclient_no_response *completed,
1035 return simple(c, no_response_opcallback, (void (*)())completed, v,
1036 "enable", (char *)0);
1039 int disorder_eclient_disable(disorder_eclient *c,
1040 disorder_eclient_no_response *completed,
1042 return simple(c, no_response_opcallback, (void (*)())completed, v,
1043 "disable", (char *)0);
1046 int disorder_eclient_random_enable(disorder_eclient *c,
1047 disorder_eclient_no_response *completed,
1049 return simple(c, no_response_opcallback, (void (*)())completed, v,
1050 "random-enable", (char *)0);
1053 int disorder_eclient_random_disable(disorder_eclient *c,
1054 disorder_eclient_no_response *completed,
1056 return simple(c, no_response_opcallback, (void (*)())completed, v,
1057 "random-disable", (char *)0);
1060 int disorder_eclient_get(disorder_eclient *c,
1061 disorder_eclient_string_response *completed,
1062 const char *track, const char *pref,
1064 return simple(c, string_response_opcallback, (void (*)())completed, v,
1065 "get", track, pref, (char *)0);
1068 int disorder_eclient_set(disorder_eclient *c,
1069 disorder_eclient_no_response *completed,
1070 const char *track, const char *pref,
1073 return simple(c, no_response_opcallback, (void (*)())completed, v,
1074 "set", track, pref, value, (char *)0);
1077 int disorder_eclient_unset(disorder_eclient *c,
1078 disorder_eclient_no_response *completed,
1079 const char *track, const char *pref,
1081 return simple(c, no_response_opcallback, (void (*)())completed, v,
1082 "unset", track, pref, (char *)0);
1085 int disorder_eclient_resolve(disorder_eclient *c,
1086 disorder_eclient_string_response *completed,
1089 return simple(c, string_response_opcallback, (void (*)())completed, v,
1090 "resolve", track, (char *)0);
1093 int disorder_eclient_search(disorder_eclient *c,
1094 disorder_eclient_list_response *completed,
1097 if(!split(terms, 0, SPLIT_QUOTES, 0, 0)) return -1;
1098 return simple(c, list_response_opcallback, (void (*)())completed, v,
1099 "search", terms, (char *)0);
1102 /* Log clients ***************************************************************/
1104 int disorder_eclient_log(disorder_eclient *c,
1105 const disorder_eclient_log_callbacks *callbacks,
1107 if(c->log_callbacks) return -1;
1108 c->log_callbacks = callbacks;
1110 stash_command(c, 0/*queuejump*/, log_opcallback, 0/*completed*/, v,
1115 /* If we get here we've stopped being a log client */
1116 static void log_opcallback(disorder_eclient *c,
1117 struct operation attribute((unused)) *op) {
1118 D(("log_opcallback"));
1119 c->log_callbacks = 0;
1123 /* error callback for log line parsing */
1124 static void logline_error(const char *msg, void *u) {
1125 disorder_eclient *c = u;
1126 protocol_error(c, c->ops, -1, "error parsing log line: %s", msg);
1129 /* process a single log line */
1130 static void logline(disorder_eclient *c, const char *line) {
1135 D(("log_opcallback [%s]", line));
1136 vec = split(line, &nvec, SPLIT_QUOTES, logline_error, c);
1137 if(nvec < 2) return; /* probably an error, already
1139 if(sscanf(vec[0], "%"SCNxMAX, &when) != 1) {
1140 /* probably the wrong side of a format change */
1141 protocol_error(c, c->ops, -1, "invalid log timestamp '%s'", vec[0]);
1144 /* TODO: do something with the time */
1145 n = TABLE_FIND(logentry_handlers, struct logentry_handler, name, vec[1]);
1146 if(n < 0) return; /* probably a future command */
1149 if(nvec < logentry_handlers[n].min || nvec > logentry_handlers[n].max)
1151 logentry_handlers[n].handler(c, nvec, vec);
1154 static void logentry_completed(disorder_eclient *c,
1155 int attribute((unused)) nvec, char **vec) {
1156 if(!c->log_callbacks->completed) return;
1157 c->log_callbacks->completed(c->log_v, vec[0]);
1160 static void logentry_failed(disorder_eclient *c,
1161 int attribute((unused)) nvec, char **vec) {
1162 if(!c->log_callbacks->failed)return;
1163 c->log_callbacks->failed(c->log_v, vec[0], vec[1]);
1166 static void logentry_moved(disorder_eclient *c,
1167 int attribute((unused)) nvec, char **vec) {
1168 if(!c->log_callbacks->moved) return;
1169 c->log_callbacks->moved(c->log_v, vec[0]);
1172 static void logentry_playing(disorder_eclient *c,
1173 int attribute((unused)) nvec, char **vec) {
1174 if(!c->log_callbacks->playing) return;
1175 c->log_callbacks->playing(c->log_v, vec[0], vec[1]);
1178 static void logentry_queue(disorder_eclient *c,
1179 int attribute((unused)) nvec, char **vec) {
1180 struct queue_entry *q;
1182 if(!c->log_callbacks->completed) return;
1183 q = xmalloc(sizeof *q);
1184 if(queue_unmarshall_vec(q, nvec, vec, eclient_queue_error, c))
1186 c->log_callbacks->queue(c->log_v, q);
1189 static void logentry_recent_added(disorder_eclient *c,
1190 int attribute((unused)) nvec, char **vec) {
1191 struct queue_entry *q;
1193 if(!c->log_callbacks->recent_added) return;
1194 q = xmalloc(sizeof *q);
1195 if(queue_unmarshall_vec(q, nvec, vec, eclient_queue_error, c))
1197 c->log_callbacks->recent_added(c->log_v, q);
1200 static void logentry_recent_removed(disorder_eclient *c,
1201 int attribute((unused)) nvec, char **vec) {
1202 if(!c->log_callbacks->recent_removed) return;
1203 c->log_callbacks->recent_removed(c->log_v, vec[0]);
1206 static void logentry_removed(disorder_eclient *c,
1207 int attribute((unused)) nvec, char **vec) {
1208 if(!c->log_callbacks->removed) return;
1209 c->log_callbacks->removed(c->log_v, vec[0], vec[1]);
1212 static void logentry_scratched(disorder_eclient *c,
1213 int attribute((unused)) nvec, char **vec) {
1214 if(!c->log_callbacks->scratched) return;
1215 c->log_callbacks->scratched(c->log_v, vec[0], vec[1]);
1218 static const struct {
1221 const char *disable;
1222 } statestrings[] = {
1223 { DISORDER_PLAYING_ENABLED, "enable_play", "disable_play" },
1224 { DISORDER_RANDOM_ENABLED, "enable_random", "disable_random" },
1225 { DISORDER_TRACK_PAUSED, "pause", "resume" },
1227 #define NSTATES (int)(sizeof states / sizeof *states)
1229 static void logentry_state(disorder_eclient *c,
1230 int attribute((unused)) nvec, char **vec) {
1233 for(n = 0; n < NSTATES; ++n)
1234 if(!strcmp(vec[0], statestrings[n].enable)) {
1235 c->statebits |= statestrings[n].bit;
1237 } else if(!strcmp(vec[0], statestrings[n].disable)) {
1238 c->statebits &= ~statestrings[n].bit;
1241 if(!c->log_callbacks->state) return;
1242 c->log_callbacks->state(c->log_v, c->statebits);
1245 static void logentry_volume(disorder_eclient *c,
1246 int attribute((unused)) nvec, char **vec) {
1249 if(!c->log_callbacks->volume) return;
1250 if(xstrtol(&l, vec[0], 0, 10)
1251 || xstrtol(&r, vec[1], 0, 10)
1252 || l < 0 || l > INT_MAX
1253 || r < 0 || r > INT_MAX)
1255 c->log_callbacks->volume(c->log_v, (int)l, (int)r);
1263 indent-tabs-mode:nil