From bc39214dc416a73a6e60ee5af81e75fd613ac5f1 Mon Sep 17 00:00:00 2001 From: Andrew Karpow Date: Wed, 6 Nov 2013 00:15:24 +0100 Subject: [PATCH] added playlist updates --- htdocs/index.html | 1 - htdocs/js/mpd.js | 6 +++--- src/mpd_client.c | 28 ++++++++++++++++++++-------- src/mpd_client.h | 1 + 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/htdocs/index.html b/htdocs/index.html index 331eb14..b8ccf21 100644 --- a/htdocs/index.html +++ b/htdocs/index.html @@ -98,7 +98,6 @@ # - Track Title Duration diff --git a/htdocs/js/mpd.js b/htdocs/js/mpd.js index 4e35a64..ab48494 100644 --- a/htdocs/js/mpd.js +++ b/htdocs/js/mpd.js @@ -30,14 +30,14 @@ function webSocketConnect() { var obj = JSON.parse(msg.data); switch (obj.type) { case "playlist": + $('#salamisandwich').find("tr:gt(0)").remove(); for (var song in obj.data) { var minutes = Math.floor(obj.data[song].duration / 60); var seconds = obj.data[song].duration - minutes * 60; $('#salamisandwich tr:last').after( - "" + obj.data[song].id + "" + - ""+ obj.data[song].uri +"" + - ""+ obj.data[song].title.replace(/%07/g, '"') +"" + + "" + obj.data[song].pos + "" + + ""+ obj.data[song].title +"" + ""+ minutes + ":" + (seconds < 10 ? '0' : '') + seconds +""); } break; diff --git a/src/mpd_client.c b/src/mpd_client.c index e7c13f7..c2a97a2 100644 --- a/src/mpd_client.c +++ b/src/mpd_client.c @@ -18,6 +18,7 @@ struct mpd_connection *conn = NULL; enum mpd_conn_states mpd_conn_state = MPD_DISCONNECTED; enum mpd_state mpd_play_state = MPD_STATE_UNKNOWN; +unsigned queue_version; int callback_ympd(struct libwebsocket_context *context, struct libwebsocket *wsi, @@ -46,6 +47,10 @@ int callback_ympd(struct libwebsocket_context *context, if(mpd_conn_state != MPD_CONNECTED) { n = snprintf(p, MAX_SIZE, "{\"type\":\"disconnected\"}"); } + else if(pss->queue_version != queue_version) { + n = mpd_put_playlist(p); + pss->queue_version = queue_version; + } else if(pss->do_send & DO_SEND_STATE) { n = mpd_put_state(p); pss->do_send &= ~DO_SEND_STATE; @@ -147,15 +152,22 @@ void mpd_loop() } } -const char* encode_string(const char *str) +char* mpd_get_title(struct mpd_song const *song) { - char *ptr = (char *)str; + char *str, *ptr; + + str = (char *)mpd_song_get_tag(song, MPD_TAG_TITLE, 0); + if(str == NULL) + str = (char *)mpd_song_get_uri(song); + if(str == NULL) return NULL; + ptr = str; while(*ptr++ != '\0') if(*ptr=='"') *ptr=' '; + return str; } @@ -189,6 +201,7 @@ int mpd_put_state(char* buffer) mpd_status_get_total_time(status), mpd_status_get_song_id(status)); + queue_version = mpd_status_get_queue_version(status); printf("buffer: %s\n", buffer); mpd_status_free(status); return len; @@ -202,11 +215,10 @@ int mpd_put_current_song(char* buffer) song = mpd_run_current_song(conn); if (song != NULL) { len = snprintf(buffer, MAX_SIZE, "{\"type\": \"current_song\", \"data\": {" - "{\"id\":%d, \"uri\":\"%s\", \"duration\":%d, \"title\":\"%s\"},", + "{\"id\":%d, \"duration\":%d, \"title\":\"%s\"},", mpd_song_get_id(song), - mpd_song_get_uri(song), mpd_song_get_duration(song), - encode_string(mpd_song_get_tag(song, MPD_TAG_TITLE, 0)) + mpd_get_title(song) ); mpd_song_free(song); } @@ -231,11 +243,11 @@ int mpd_put_playlist(char* buffer) if(mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) { song = mpd_entity_get_song(entity); cur += snprintf(cur, end - cur, - "{\"id\":%d, \"uri\":\"%s\", \"duration\":%d, \"title\":\"%s\"},", + "{\"id\":%d, \"pos\":%d, \"duration\":%d, \"title\":\"%s\"},", mpd_song_get_id(song), - mpd_song_get_uri(song), + mpd_song_get_pos(song), mpd_song_get_duration(song), - encode_string(mpd_song_get_tag(song, MPD_TAG_TITLE, 0)) + mpd_get_title(song) ); } diff --git a/src/mpd_client.h b/src/mpd_client.h index 5af5128..b411231 100644 --- a/src/mpd_client.h +++ b/src/mpd_client.h @@ -8,6 +8,7 @@ struct libwebsocket_protocols *protocol_array; struct per_session_data__ympd { int do_send; + unsigned queue_version; }; enum mpd_conn_states { -- 2.20.1