From d344ec05a9ddb5c36818c7f8b657e14a2066e755 Mon Sep 17 00:00:00 2001 From: Kurt Van Dijck Date: Tue, 28 Apr 2015 11:08:21 +0200 Subject: [PATCH 1/6] add support for output enable/disable --- htdocs/index.html | 2 ++ htdocs/js/mpd.js | 27 ++++++++++++++ src/mpd_client.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++ src/mpd_client.h | 4 +++ 4 files changed, 123 insertions(+) diff --git a/htdocs/index.html b/htdocs/index.html index 33f0bc9..3b6d97c 100644 --- a/htdocs/index.html +++ b/htdocs/index.html @@ -142,6 +142,8 @@ Repeat +
+
'); + btn.appendTo($('#btn-outputs-block')); + }); + /* remove cache, since the buttons have been recreated */ + last_outputs = ''; + break; + case "outputs": + if(JSON.stringify(obj) === JSON.stringify(last_outputs)) + break; + $.each(obj.data, function(id, enabled){ + if (enabled) + $('#btnoutput'+id).addClass("active"); + else + $('#btnoutput'+id).removeClass("active"); + }); + last_outputs = obj; + break; case "disconnected": if($('.top-right').has('div').length == 0) $('.top-right').notify({ @@ -500,6 +523,10 @@ $('#btnrepeat').on('click', function (e) { socket.send("MPD_API_TOGGLE_REPEAT," + ($(this).hasClass('active') ? 0 : 1)); }); +function toggleoutput(button, id) { + socket.send("MPD_API_TOGGLE_OUTPUT,"+id+"," + ($(button).hasClass('active') ? 0 : 1)); +} + $('#btnnotify').on('click', function (e) { if($.cookie("notification") === "true") { $.cookie("notification", false); diff --git a/src/mpd_client.c b/src/mpd_client.c index ee49c84..c276bee 100644 --- a/src/mpd_client.c +++ b/src/mpd_client.c @@ -27,6 +27,9 @@ #include "config.h" #include "json_encode.h" +/* forward declaration */ +static int mpd_notify_callback(struct mg_connection *c, enum mg_event ev); + const char * mpd_cmd_strs[] = { MPD_CMDS(GEN_STR) }; @@ -106,6 +109,19 @@ int callback_mpd(struct mg_connection *c) if(sscanf(c->content, "MPD_API_TOGGLE_CROSSFADE,%u", &uint_buf)) mpd_run_crossfade(mpd.conn, uint_buf); break; + case MPD_API_GET_OUTPUTS: + mpd.buf_size = mpd_put_outputnames(mpd.buf); + c->callback_param = NULL; + mpd_notify_callback(c, MG_POLL); + break; + case MPD_API_TOGGLE_OUTPUT: + if (sscanf(c->content, "MPD_API_TOGGLE_OUTPUT,%u,%u", &uint_buf, &uint_buf_2)) { + if (uint_buf_2) + mpd_run_enable_output(mpd.conn, uint_buf); + else + mpd_run_disable_output(mpd.conn, uint_buf); + } + break; case MPD_API_SET_VOLUME: if(sscanf(c->content, "MPD_API_SET_VOLUME,%ud", &uint_buf) && uint_buf <= 100) mpd_run_set_volume(mpd.conn, uint_buf); @@ -339,6 +355,13 @@ void mpd_poll(struct mg_server *s) fprintf(stderr, "MPD connected.\n"); mpd_connection_set_timeout(mpd.conn, 10000); mpd.conn_state = MPD_CONNECTED; + /* write outputs */ + mpd.buf_size = mpd_put_outputnames(mpd.buf); + for (struct mg_connection *c = mg_next(s, NULL); c != NULL; c = mg_next(s, c)) + { + c->callback_param = NULL; + mpd_notify_callback(c, MG_POLL); + } break; case MPD_FAILURE: @@ -359,6 +382,12 @@ void mpd_poll(struct mg_server *s) c->callback_param = NULL; mpd_notify_callback(c, MG_POLL); } + mpd.buf_size = mpd_put_outputs(mpd.buf); + for (struct mg_connection *c = mg_next(s, NULL); c != NULL; c = mg_next(s, c)) + { + c->callback_param = NULL; + mpd_notify_callback(c, MG_POLL); + } break; } } @@ -412,6 +441,67 @@ int mpd_put_state(char *buffer, int *current_song_id, unsigned *queue_version) return len; } +int mpd_put_outputs(char *buffer) +{ + struct mpd_output *out; + static int *outputs; + static int soutputs; + int idx, maxidx; + char *str, *strend; + + maxidx = 0; + mpd_send_outputs(mpd.conn); + while ((out = mpd_recv_output(mpd.conn)) != NULL) { + idx = mpd_output_get_id(out); + if (idx >= soutputs) { + /* realloc some more */ + soutputs = (idx + 15) & ~15; /* round up to 16 */ + outputs = realloc(outputs, sizeof(*outputs)*soutputs); + if (!outputs) + exit(1); + } + if (idx > maxidx) + maxidx = idx; + outputs[idx] = mpd_output_get_enabled(out); + mpd_output_free(out); + } + mpd_response_finish(mpd.conn); + + str = buffer; + strend = buffer+MAX_SIZE; + str += snprintf(str, strend-str, "{\"type\":\"outputs\", \"data\":{"); + for (idx = 0; idx <= maxidx; ++idx) + str += snprintf(str, strend-str, "%c \"%d\":%d", + idx ? ',' : ' ', idx, outputs[idx]); + str += snprintf(str, strend-str, " }}"); + return str-buffer; +} + +int mpd_put_outputnames(char *buffer) +{ + struct mpd_output *out; + int nout; + char *str, *strend; + + str = buffer; + strend = buffer+MAX_SIZE; + str += snprintf(str, strend-str, "{\"type\":\"outputnames\", \"data\":{"); + + mpd_send_outputs(mpd.conn); + nout = 0; + while ((out = mpd_recv_output(mpd.conn)) != NULL) { + if (nout++) + *str++ = ','; + str += snprintf(str, strend - str, " \"%d\":\"%s\"", + mpd_output_get_id(out), + mpd_output_get_name(out)); + mpd_output_free(out); + } + mpd_response_finish(mpd.conn); + str += snprintf(str, strend-str, " }}"); + return str-buffer; +} + int mpd_put_current_song(char *buffer) { char *cur = buffer; diff --git a/src/mpd_client.h b/src/mpd_client.h index 165bb5b..a36dbaf 100644 --- a/src/mpd_client.h +++ b/src/mpd_client.h @@ -57,6 +57,8 @@ X(MPD_API_SET_MPDHOST) \ X(MPD_API_SET_MPDPASS) \ X(MPD_API_UPDATE_DB) \ + X(MPD_API_GET_OUTPUTS) \ + X(MPD_API_TOGGLE_OUTPUT) \ X(MPD_API_TOGGLE_RANDOM) \ X(MPD_API_TOGGLE_CONSUME) \ X(MPD_API_TOGGLE_SINGLE) \ @@ -100,6 +102,8 @@ void mpd_poll(struct mg_server *s); int callback_mpd(struct mg_connection *c); int mpd_close_handler(struct mg_connection *c); int mpd_put_state(char *buffer, int *current_song_id, unsigned *queue_version); +int mpd_put_outputs(char *buffer); +int mpd_put_outputnames(char *buffer); int mpd_put_current_song(char *buffer); int mpd_put_queue(char *buffer, unsigned int offset); int mpd_put_browse(char *buffer, char *path, unsigned int offset); From 66ed2518a2b58601100e0590f449b878a3db15b4 Mon Sep 17 00:00:00 2001 From: Kurt Van Dijck Date: Fri, 1 May 2015 23:33:07 +0200 Subject: [PATCH 2/6] mpd_client: fix allocation of outputs --- src/mpd_client.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mpd_client.c b/src/mpd_client.c index c276bee..39e1f5d 100644 --- a/src/mpd_client.c +++ b/src/mpd_client.c @@ -455,7 +455,7 @@ int mpd_put_outputs(char *buffer) idx = mpd_output_get_id(out); if (idx >= soutputs) { /* realloc some more */ - soutputs = (idx + 15) & ~15; /* round up to 16 */ + soutputs = (idx + 1 + 15) & ~15; /* round up to 16 */ outputs = realloc(outputs, sizeof(*outputs)*soutputs); if (!outputs) exit(1); From 0c3ded1cf3864bae6b60ab4551c06a37e627fd0d Mon Sep 17 00:00:00 2001 From: Kurt Van Dijck Date: Fri, 1 May 2015 23:51:44 +0200 Subject: [PATCH 3/6] outputs: reduce duplicate code --- src/mpd_client.c | 56 +++++++++++------------------------------------- src/mpd_client.h | 3 +-- 2 files changed, 13 insertions(+), 46 deletions(-) diff --git a/src/mpd_client.c b/src/mpd_client.c index 39e1f5d..654dced 100644 --- a/src/mpd_client.c +++ b/src/mpd_client.c @@ -110,7 +110,7 @@ int callback_mpd(struct mg_connection *c) mpd_run_crossfade(mpd.conn, uint_buf); break; case MPD_API_GET_OUTPUTS: - mpd.buf_size = mpd_put_outputnames(mpd.buf); + mpd.buf_size = mpd_put_outputs(mpd.buf, 1); c->callback_param = NULL; mpd_notify_callback(c, MG_POLL); break; @@ -356,7 +356,7 @@ void mpd_poll(struct mg_server *s) mpd_connection_set_timeout(mpd.conn, 10000); mpd.conn_state = MPD_CONNECTED; /* write outputs */ - mpd.buf_size = mpd_put_outputnames(mpd.buf); + mpd.buf_size = mpd_put_outputs(mpd.buf, 1); for (struct mg_connection *c = mg_next(s, NULL); c != NULL; c = mg_next(s, c)) { c->callback_param = NULL; @@ -382,7 +382,7 @@ void mpd_poll(struct mg_server *s) c->callback_param = NULL; mpd_notify_callback(c, MG_POLL); } - mpd.buf_size = mpd_put_outputs(mpd.buf); + mpd.buf_size = mpd_put_outputs(mpd.buf, 0); for (struct mg_connection *c = mg_next(s, NULL); c != NULL; c = mg_next(s, c)) { c->callback_param = NULL; @@ -441,43 +441,7 @@ int mpd_put_state(char *buffer, int *current_song_id, unsigned *queue_version) return len; } -int mpd_put_outputs(char *buffer) -{ - struct mpd_output *out; - static int *outputs; - static int soutputs; - int idx, maxidx; - char *str, *strend; - - maxidx = 0; - mpd_send_outputs(mpd.conn); - while ((out = mpd_recv_output(mpd.conn)) != NULL) { - idx = mpd_output_get_id(out); - if (idx >= soutputs) { - /* realloc some more */ - soutputs = (idx + 1 + 15) & ~15; /* round up to 16 */ - outputs = realloc(outputs, sizeof(*outputs)*soutputs); - if (!outputs) - exit(1); - } - if (idx > maxidx) - maxidx = idx; - outputs[idx] = mpd_output_get_enabled(out); - mpd_output_free(out); - } - mpd_response_finish(mpd.conn); - - str = buffer; - strend = buffer+MAX_SIZE; - str += snprintf(str, strend-str, "{\"type\":\"outputs\", \"data\":{"); - for (idx = 0; idx <= maxidx; ++idx) - str += snprintf(str, strend-str, "%c \"%d\":%d", - idx ? ',' : ' ', idx, outputs[idx]); - str += snprintf(str, strend-str, " }}"); - return str-buffer; -} - -int mpd_put_outputnames(char *buffer) +int mpd_put_outputs(char *buffer, int names) { struct mpd_output *out; int nout; @@ -485,16 +449,20 @@ int mpd_put_outputnames(char *buffer) str = buffer; strend = buffer+MAX_SIZE; - str += snprintf(str, strend-str, "{\"type\":\"outputnames\", \"data\":{"); + str += snprintf(str, strend-str, "{\"type\":\"%s\", \"data\":{", + names ? "outputnames" : "output"); mpd_send_outputs(mpd.conn); nout = 0; while ((out = mpd_recv_output(mpd.conn)) != NULL) { if (nout++) *str++ = ','; - str += snprintf(str, strend - str, " \"%d\":\"%s\"", - mpd_output_get_id(out), - mpd_output_get_name(out)); + if (names) + str += snprintf(str, strend - str, " \"%d\":\"%s\"", + mpd_output_get_id(out), mpd_output_get_name(out)); + else + str += snprintf(str, strend-str, " \"%d\":%d", + mpd_output_get_id(out), mpd_output_get_enabled(out)); mpd_output_free(out); } mpd_response_finish(mpd.conn); diff --git a/src/mpd_client.h b/src/mpd_client.h index a36dbaf..6f060d2 100644 --- a/src/mpd_client.h +++ b/src/mpd_client.h @@ -102,8 +102,7 @@ void mpd_poll(struct mg_server *s); int callback_mpd(struct mg_connection *c); int mpd_close_handler(struct mg_connection *c); int mpd_put_state(char *buffer, int *current_song_id, unsigned *queue_version); -int mpd_put_outputs(char *buffer); -int mpd_put_outputnames(char *buffer); +int mpd_put_outputs(char *buffer, int putnames); int mpd_put_current_song(char *buffer); int mpd_put_queue(char *buffer, unsigned int offset); int mpd_put_browse(char *buffer, char *path, unsigned int offset); From c3ddc8318b6f9452efde2d81e697051164d3ede5 Mon Sep 17 00:00:00 2001 From: Kurt Van Dijck Date: Sat, 2 May 2015 00:07:37 +0200 Subject: [PATCH 4/6] outputs: clear possible error --- src/mpd_client.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/mpd_client.c b/src/mpd_client.c index 654dced..d61e82f 100644 --- a/src/mpd_client.c +++ b/src/mpd_client.c @@ -465,7 +465,10 @@ int mpd_put_outputs(char *buffer, int names) mpd_output_get_id(out), mpd_output_get_enabled(out)); mpd_output_free(out); } - mpd_response_finish(mpd.conn); + if (!mpd_response_finish(mpd.conn)) { + mpd_connection_clear_error(mpd.conn); + return NULL; + } str += snprintf(str, strend-str, " }}"); return str-buffer; } From d125f0a1d36080171ce118fa84df816b7ccaf847 Mon Sep 17 00:00:00 2001 From: Kurt Van Dijck Date: Sat, 2 May 2015 14:36:02 +0200 Subject: [PATCH 5/6] outputs: show MPD error message on stderr --- src/mpd_client.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mpd_client.c b/src/mpd_client.c index d61e82f..48772bc 100644 --- a/src/mpd_client.c +++ b/src/mpd_client.c @@ -466,8 +466,9 @@ int mpd_put_outputs(char *buffer, int names) mpd_output_free(out); } if (!mpd_response_finish(mpd.conn)) { + fprintf(stderr, "MPD outputs: %s\n", mpd_connection_get_error_message(mpd.conn)); mpd_connection_clear_error(mpd.conn); - return NULL; + return 0; } str += snprintf(str, strend-str, " }}"); return str-buffer; From 3b72651d7e9aac9977db04ce21499f3354673784 Mon Sep 17 00:00:00 2001 From: Kurt Van Dijck Date: Mon, 25 May 2015 18:11:22 +0200 Subject: [PATCH 6/6] outputs: fix typo --- src/mpd_client.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mpd_client.c b/src/mpd_client.c index 48772bc..f2e61e1 100644 --- a/src/mpd_client.c +++ b/src/mpd_client.c @@ -450,7 +450,7 @@ int mpd_put_outputs(char *buffer, int names) str = buffer; strend = buffer+MAX_SIZE; str += snprintf(str, strend-str, "{\"type\":\"%s\", \"data\":{", - names ? "outputnames" : "output"); + names ? "outputnames" : "outputs"); mpd_send_outputs(mpd.conn); nout = 0;