mirror of
https://github.com/SuperBFG7/ympd
synced 2025-10-29 12:57:50 +00:00
Merge pull request #69 from kurt-vd/master
add support for output enable/disable
This commit is contained in:
@@ -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_outputs(mpd.buf, 1);
|
||||
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);
|
||||
@@ -351,6 +367,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_outputs(mpd.buf, 1);
|
||||
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:
|
||||
@@ -371,6 +394,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, 0);
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -424,6 +453,39 @@ int mpd_put_state(char *buffer, int *current_song_id, unsigned *queue_version)
|
||||
return len;
|
||||
}
|
||||
|
||||
int mpd_put_outputs(char *buffer, int names)
|
||||
{
|
||||
struct mpd_output *out;
|
||||
int nout;
|
||||
char *str, *strend;
|
||||
|
||||
str = buffer;
|
||||
strend = buffer+MAX_SIZE;
|
||||
str += snprintf(str, strend-str, "{\"type\":\"%s\", \"data\":{",
|
||||
names ? "outputnames" : "outputs");
|
||||
|
||||
mpd_send_outputs(mpd.conn);
|
||||
nout = 0;
|
||||
while ((out = mpd_recv_output(mpd.conn)) != NULL) {
|
||||
if (nout++)
|
||||
*str++ = ',';
|
||||
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);
|
||||
}
|
||||
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 0;
|
||||
}
|
||||
str += snprintf(str, strend-str, " }}");
|
||||
return str-buffer;
|
||||
}
|
||||
|
||||
int mpd_put_current_song(char *buffer)
|
||||
{
|
||||
char *cur = buffer;
|
||||
|
||||
@@ -58,6 +58,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) \
|
||||
@@ -101,6 +103,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 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);
|
||||
|
||||
Reference in New Issue
Block a user