added playlist updates

This commit is contained in:
Andrew Karpow 2013-11-06 00:15:24 +01:00
parent be6d0ddfb3
commit bc39214dc4
4 changed files with 24 additions and 12 deletions

View file

@ -98,7 +98,6 @@
<thead> <thead>
<tr> <tr>
<th>#</th> <th>#</th>
<th>Track</th>
<th>Title</th> <th>Title</th>
<th>Duration</th> <th>Duration</th>
</tr> </tr>

View file

@ -30,14 +30,14 @@ function webSocketConnect() {
var obj = JSON.parse(msg.data); var obj = JSON.parse(msg.data);
switch (obj.type) { switch (obj.type) {
case "playlist": case "playlist":
$('#salamisandwich').find("tr:gt(0)").remove();
for (var song in obj.data) { for (var song in obj.data) {
var minutes = Math.floor(obj.data[song].duration / 60); var minutes = Math.floor(obj.data[song].duration / 60);
var seconds = obj.data[song].duration - minutes * 60; var seconds = obj.data[song].duration - minutes * 60;
$('#salamisandwich tr:last').after( $('#salamisandwich tr:last').after(
"<tr id=\"playlist_" + obj.data[song].id + "\"><td>" + obj.data[song].id + "</td>" + "<tr id=\"playlist_" + obj.data[song].id + "\"><td>" + obj.data[song].pos + "</td>" +
"<td>"+ obj.data[song].uri +"</td>" + "<td>"+ obj.data[song].title +"</td>" +
"<td>"+ obj.data[song].title.replace(/%07/g, '"') +"</td>" +
"<td>"+ minutes + ":" + (seconds < 10 ? '0' : '') + seconds +"</td></tr>"); "<td>"+ minutes + ":" + (seconds < 10 ? '0' : '') + seconds +"</td></tr>");
} }
break; break;

View file

@ -18,6 +18,7 @@
struct mpd_connection *conn = NULL; struct mpd_connection *conn = NULL;
enum mpd_conn_states mpd_conn_state = MPD_DISCONNECTED; enum mpd_conn_states mpd_conn_state = MPD_DISCONNECTED;
enum mpd_state mpd_play_state = MPD_STATE_UNKNOWN; enum mpd_state mpd_play_state = MPD_STATE_UNKNOWN;
unsigned queue_version;
int callback_ympd(struct libwebsocket_context *context, int callback_ympd(struct libwebsocket_context *context,
struct libwebsocket *wsi, struct libwebsocket *wsi,
@ -46,6 +47,10 @@ int callback_ympd(struct libwebsocket_context *context,
if(mpd_conn_state != MPD_CONNECTED) { if(mpd_conn_state != MPD_CONNECTED) {
n = snprintf(p, MAX_SIZE, "{\"type\":\"disconnected\"}"); 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) { else if(pss->do_send & DO_SEND_STATE) {
n = mpd_put_state(p); n = mpd_put_state(p);
pss->do_send &= ~DO_SEND_STATE; 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) if(str == NULL)
return NULL; return NULL;
ptr = str;
while(*ptr++ != '\0') while(*ptr++ != '\0')
if(*ptr=='"') if(*ptr=='"')
*ptr=' '; *ptr=' ';
return str; return str;
} }
@ -189,6 +201,7 @@ int mpd_put_state(char* buffer)
mpd_status_get_total_time(status), mpd_status_get_total_time(status),
mpd_status_get_song_id(status)); mpd_status_get_song_id(status));
queue_version = mpd_status_get_queue_version(status);
printf("buffer: %s\n", buffer); printf("buffer: %s\n", buffer);
mpd_status_free(status); mpd_status_free(status);
return len; return len;
@ -202,11 +215,10 @@ int mpd_put_current_song(char* buffer)
song = mpd_run_current_song(conn); song = mpd_run_current_song(conn);
if (song != NULL) { if (song != NULL) {
len = snprintf(buffer, MAX_SIZE, "{\"type\": \"current_song\", \"data\": {" 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_id(song),
mpd_song_get_uri(song),
mpd_song_get_duration(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); mpd_song_free(song);
} }
@ -231,11 +243,11 @@ int mpd_put_playlist(char* buffer)
if(mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) { if(mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
song = mpd_entity_get_song(entity); song = mpd_entity_get_song(entity);
cur += snprintf(cur, end - cur, 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_id(song),
mpd_song_get_uri(song), mpd_song_get_pos(song),
mpd_song_get_duration(song), mpd_song_get_duration(song),
encode_string(mpd_song_get_tag(song, MPD_TAG_TITLE, 0)) mpd_get_title(song)
); );
} }

View file

@ -8,6 +8,7 @@ struct libwebsocket_protocols *protocol_array;
struct per_session_data__ympd { struct per_session_data__ympd {
int do_send; int do_send;
unsigned queue_version;
}; };
enum mpd_conn_states { enum mpd_conn_states {