From f0b577378e84be0549a9557717120a8d8c4dbcaa Mon Sep 17 00:00:00 2001 From: jcorporation Date: Wed, 15 Aug 2018 15:06:45 +0200 Subject: [PATCH] Feat: finished mpd idle usage --- htdocs/js/mpd.js | 1 + src/mpd_client.c | 43 +++++++++++++++---------------------------- src/mpd_client.h | 29 ++++++++++++++--------------- src/mympd.c | 5 ++--- 4 files changed, 32 insertions(+), 46 deletions(-) diff --git a/htdocs/js/mpd.js b/htdocs/js/mpd.js index 9ef75e2..39e2c44 100644 --- a/htdocs/js/mpd.js +++ b/htdocs/js/mpd.js @@ -646,6 +646,7 @@ function webSocketConnect() { showNotification('Connected to myMPD', '', '', 'success'); modalConnectionError.hide(); appRoute(); + sendAPI({"cmd":"MPD_API_GET_STATE"}, parseState); } socket.onmessage = function got_packet(msg) { diff --git a/src/mpd_client.c b/src/mpd_client.c index fca955f..7a21dcc 100644 --- a/src/mpd_client.c +++ b/src/mpd_client.c @@ -37,9 +37,6 @@ #include "config.h" #include "../dist/src/frozen/frozen.h" -/* forward declaration */ -void mympd_notify(struct mg_mgr *s); - const char * mpd_cmd_strs[] = { MPD_CMDS(GEN_STR) }; @@ -462,7 +459,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg) { break; case MPD_API_GET_STATS: n = mympd_put_stats(mpd.buf); - break; + break; } if (mpd.conn_state == MPD_CONNECTED && mpd_connection_get_error(mpd.conn) != MPD_ERROR_SUCCESS) { @@ -497,11 +494,15 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg) { free(cmd); } -int mympd_close_handler(struct mg_connection *c) { - /* Cleanup session data */ - if (c->user_data) - free(c->user_data); - return 0; +void mympd_notify(struct mg_mgr *s) { + for (struct mg_connection *c = mg_next(s, NULL); c != NULL; c = mg_next(s, c)) { + if (!is_websocket(c)) + continue; + mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, strlen(mpd.buf)); + } + #ifdef DEBUG + fprintf(stderr,"NOTIFY: %s\n", mpd.buf); + #endif } void mympd_poll(struct mg_mgr *s, int timeout) { @@ -547,7 +548,6 @@ void mympd_poll(struct mg_mgr *s, int timeout) { fprintf(stderr, "MPD connection failed.\n"); snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"disconnected\"}"); mympd_notify(s); - case MPD_DISCONNECT: case MPD_RECONNECT: if (mpd.conn != NULL) @@ -569,21 +569,8 @@ void mympd_poll(struct mg_mgr *s, int timeout) { } } -void mympd_notify(struct mg_mgr *s) { - for (struct mg_connection *c = mg_next(s, NULL); c != NULL; c = mg_next(s, c)) { - if (!is_websocket(c)) - continue; - mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, strlen(mpd.buf)); - } - #ifdef DEBUG - fprintf(stderr,"NOTIFY: %s\n", mpd.buf); - #endif -} - void mympd_parse_idle(struct mg_mgr *s) { -// mpd_connection_set_timeout(mpd.conn, 60); enum mpd_idle idle_bitmask = mpd_recv_idle(mpd.conn, false); -// mpd_connection_set_timeout(mpd.conn, mpd.timeout); int len; for (unsigned j = 0;; ++j) { @@ -659,13 +646,13 @@ int mympd_put_state(char *buffer, int *current_song_id, int *next_song_id, unsig mpd.conn_state = MPD_FAILURE; return 0; } - if (status) { + else { audioformat = mpd_status_get_audio_format(status); } - len = json_printf(&out,"{type: update_state, data:{" + len = json_printf(&out,"{type: update_state, data: {" "state:%d, volume:%d, songPos: %d, elapsedTime: %d, " - "totalTime:%d, currentSongId: %d, kbitrate: %d, " + "totalTime: %d, currentSongId: %d, kbitrate: %d, " "audioFormat: { sampleRate: %d, bits: %d, channels: %d}, " "queueLength: %d, nextSongPos: %d, nextSongId: %d, " "queueVersion: %d", @@ -742,8 +729,8 @@ int mympd_put_settings(char *buffer) { } len = json_printf(&out, - "{type:settings, data:{" - "repeat:%d, single:%d, crossfade:%d, consume:%d, random:%d, " + "{type: settings, data: {" + "repeat: %d, single: %d, crossfade: %d, consume: %d, random: %d, " "mixrampdb: %f, mixrampdelay: %f, mpdhost : %Q, mpdport: %d, passwort_set: %B, " "streamport: %d, coverimage: %Q, max_elements_per_page: %d, replaygain: %Q," "notificationWeb: %d, notificationPage: %d" diff --git a/src/mpd_client.h b/src/mpd_client.h index 1d0f381..e6cc12f 100644 --- a/src/mpd_client.h +++ b/src/mpd_client.h @@ -122,30 +122,30 @@ struct t_mpd { } mpd; typedef struct { - int mpdport; - const char* mpdhost; - const char* mpdpass; - const char* webport; - bool ssl; - const char* sslport; - const char* sslcert; - const char* sslkey; - const char* user; - int streamport; - const char* coverimage; - const char* statefile; + int mpdport; + const char* mpdhost; + const char* mpdpass; + const char* webport; + bool ssl; + const char* sslport; + const char* sslcert; + const char* sslkey; + const char* user; + int streamport; + const char* coverimage; + const char* statefile; } configuration; configuration config; static int is_websocket(const struct mg_connection *nc) { - return nc->flags & MG_F_IS_WEBSOCKET; + return nc->flags & MG_F_IS_WEBSOCKET; } void mympd_poll(struct mg_mgr *sm, int timeout); void mympd_parse_idle(struct mg_mgr *s); void callback_mympd(struct mg_connection *nc, const struct mg_str msg); -int mympd_close_handler(struct mg_connection *c); +void mympd_notify(struct mg_mgr *s); int mympd_put_state(char *buffer, int *current_song_id, int *next_song_id, unsigned *queue_version); int mympd_put_outputs(char *buffer); int mympd_put_current_song(char *buffer); @@ -166,4 +166,3 @@ int mympd_put_songdetails(char *buffer, char *uri); int mympd_queue_crop(char *buffer); void mympd_disconnect(); #endif - diff --git a/src/mympd.c b/src/mympd.c index 032d8a9..a238ee6 100644 --- a/src/mympd.c +++ b/src/mympd.c @@ -84,7 +84,7 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { #ifdef DEBUG printf("Websocket connection closed\n"); #endif - mympd_close_handler(nc); +// mympd_close_handler(nc); } else { #ifdef DEBUG @@ -255,10 +255,9 @@ int main(int argc, char **argv) { if (config.ssl == true) printf("myMPD started on ssl port %s\n", config.sslport); - //Main loop for http handling while (s_signal_received == 0) { mg_mgr_poll(&mgr, 100); - mympd_poll(&mgr, 1); + mympd_poll(&mgr, 0); } mg_mgr_free(&mgr); mympd_disconnect();