From 2b461aafa2d64488f862fb9e422b416320c952b4 Mon Sep 17 00:00:00 2001 From: SuperBFG7 Date: Thu, 17 Jun 2021 20:50:15 +0200 Subject: [PATCH] Only show love button when either mpdas or mpdscribble channel is present and use the correct channel to send the message of love --- htdocs/js/mpd.js | 26 +++++++++++++++++++++----- src/mpd_client.c | 31 +++++++++++++++++++++++++++++++ src/mpd_client.h | 2 ++ 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/htdocs/js/mpd.js b/htdocs/js/mpd.js index 1f62534..88d817c 100644 --- a/htdocs/js/mpd.js +++ b/htdocs/js/mpd.js @@ -29,6 +29,7 @@ var current_song = new Object(); var MAX_ELEMENTS_PER_PAGE = 512; var isTouch = Modernizr.touch ? 1 : 0; var filter = ''; +var scrobbler = ''; var app = $.sammy(function () { function runBrowse() { @@ -264,6 +265,7 @@ function webSocketConnect() { app.run(); /* emit initial request for output names */ socket.send('MPD_API_GET_OUTPUTS'); + socket.send('MPD_API_GET_CHANNELS'); }; socket.onmessage = function got_packet(msg) { @@ -815,6 +817,22 @@ function webSocketConnect() { }); last_outputs = obj; break; + case 'channels': + scrobbler = ''; + $('#love').addClass('hide'); + if (Object.keys(obj.data).length) { + $.each(obj.data, function (id, name) { + switch (name) { + case 'mpdas': + case 'mpdscribble': + scrobbler = name; + $('#love').removeClass('hide'); + default: + break; + } + }); + } + break; case 'disconnected': if ($('.top-right').has('div').length == 0) $('.top-right') @@ -1082,11 +1100,9 @@ function basename(path) { function clickLove() { socket.send( - 'MPD_API_SEND_MESSAGE,mpdas,' + - ($('#btnlove').hasClass('active') ? 'unlove' : 'love') - ); - socket.send( - 'MPD_API_SEND_MESSAGE,mpdscribble,' + + 'MPD_API_SEND_MESSAGE,' + + scrobbler + + ',' + ($('#btnlove').hasClass('active') ? 'unlove' : 'love') ); if ($('#btnlove').hasClass('active')) $('#btnlove').removeClass('active'); diff --git a/src/mpd_client.c b/src/mpd_client.c index 2894d5e..4aa6565 100644 --- a/src/mpd_client.c +++ b/src/mpd_client.c @@ -259,6 +259,11 @@ int callback_mpd(struct mg_connection *c) { out_send_message: free(p_charbuf); break; + case MPD_API_GET_CHANNELS: + mpd.buf_size = mpd_put_channels(mpd.buf); + c->callback_param = NULL; + mpd_notify_callback(c, MG_POLL); + break; #ifdef WITH_MPD_HOST_CHANGE /* Commands allowed when disconnected from MPD server */ case MPD_API_SET_MPDHOST: @@ -550,6 +555,32 @@ int mpd_put_outputs(char *buffer, int names) { return str - buffer; } +int mpd_put_channels(char *buffer) { + struct mpd_pair *channel; + int nchan; + char *str, *strend; + + str = buffer; + strend = buffer + MAX_SIZE; + str += snprintf(str, strend - str, "{\"type\":\"%s\", \"data\":{", "channels"); + + mpd_send_channels(mpd.conn); + nchan = 0; + while ((channel = mpd_recv_channel_pair(mpd.conn)) != NULL) { + if (nchan++) + *str++ = ','; + str += snprintf(str, strend - str, " \"%d\":\"%s\"", nchan, channel->value); + mpd_return_pair(mpd.conn, channel); + } + 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; const char *end = buffer + MAX_SIZE; diff --git a/src/mpd_client.h b/src/mpd_client.h index 11ab7a0..fe6e9f7 100644 --- a/src/mpd_client.h +++ b/src/mpd_client.h @@ -50,6 +50,7 @@ X(MPD_API_RM_ALL) \ X(MPD_API_MOVE_TRACK) \ X(MPD_API_SEARCH) \ + X(MPD_API_GET_CHANNELS) \ X(MPD_API_SEND_MESSAGE) \ X(MPD_API_SET_VOLUME) \ X(MPD_API_SET_PAUSE) \ @@ -109,6 +110,7 @@ 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_channels(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); -- 2.20.1