1
0
mirror of https://github.com/SuperBFG7/ympd synced 2024-12-27 11:20:26 +00:00

Merge branch 'master' into devel

This commit is contained in:
jcorporation 2018-07-16 23:39:17 +01:00
commit 76ab27f30b
5 changed files with 63 additions and 38 deletions

View File

@ -42,6 +42,7 @@ Dependencies
- libmpdclient 2: http://www.musicpd.org/libs/libmpdclient/ - libmpdclient 2: http://www.musicpd.org/libs/libmpdclient/
- cmake 2.6: http://cmake.org/ - cmake 2.6: http://cmake.org/
- OpenSSL: https://www.openssl.org/ - OpenSSL: https://www.openssl.org/
- libasan3 (for debug builds only)
Unix Build Instructions Unix Build Instructions
----------------------- -----------------------

File diff suppressed because one or more lines are too long

View File

@ -170,8 +170,7 @@ function appRoute() {
} }
else if (app.current.app == 'Queue' ) { else if (app.current.app == 'Queue' ) {
document.getElementById('QueueList').classList.add('opacity05'); document.getElementById('QueueList').classList.add('opacity05');
/* /* if (app.last.app != app.current.app) {
if (app.last.app != app.current.app) {
if (app.current.search.length < 2) { if (app.current.search.length < 2) {
setPagination(app.current.page); setPagination(app.current.page);
} }
@ -245,7 +244,6 @@ function appRoute() {
'<td colspan="5">Searching...</td></tr>'; '<td colspan="5">Searching...</td></tr>';
// else // else
// setPagination(app.current.page); // setPagination(app.current.page);
// document.getElementById('searchstr').value = app.current.search; // document.getElementById('searchstr').value = app.current.search;
} }
@ -599,6 +597,7 @@ function parseStats(obj) {
function toggleBtn(btn, state) { function toggleBtn(btn, state) {
var b = document.getElementById(btn); var b = document.getElementById(btn);
if (!b) return;
if (state == undefined) if (state == undefined)
state = b.classList.contains('active') ? 0 : 1; state = b.classList.contains('active') ? 0 : 1;
if (state == 1) { if (state == 1) {
@ -784,11 +783,7 @@ function getQueue() {
if (app.current.search.length >= 2) if (app.current.search.length >= 2)
sendAPI({"cmd": "MPD_API_SEARCH_QUEUE", "data": {"mpdtag":app.current.filter, "offset":app.current.page, "searchstr": app.current.search}}, parseQueue); sendAPI({"cmd": "MPD_API_SEARCH_QUEUE", "data": {"mpdtag":app.current.filter, "offset":app.current.page, "searchstr": app.current.search}}, parseQueue);
else { else {
var queue_version = document.getElementById('QueueList').getAttribute('data-version'); sendAPI({"cmd": "MPD_API_GET_QUEUE", "data": {"offset": app.current.page}}, parseQueue);
if (last_state && queue_version != last_state.data.queue_version)
sendAPI({"cmd": "MPD_API_GET_QUEUE", "data": {"offset": app.current.page}}, parseQueue);
else
document.getElementById('QueueList').classList.remove('opacity05');
} }
} }

View File

@ -40,7 +40,7 @@ function webSocketConnect(){socket=new WebSocket(getWsUrl());try{socket.onopen=f
break;case "update_queue":"Queue"===app.current.app&&getQueue();break;case "song_change":songChange(b);break;case "error":showNotification(b.data,"","","danger")}}},socket.onclose=function(){console.log("disconnected");modalConnectionError.show();setTimeout(function(){console.log("reconnect");webSocketConnect()},3E3)}}catch(a){alert("Error: "+a)}} break;case "update_queue":"Queue"===app.current.app&&getQueue();break;case "song_change":songChange(b);break;case "error":showNotification(b.data,"","","danger")}}},socket.onclose=function(){console.log("disconnected");modalConnectionError.show();setTimeout(function(){console.log("reconnect");webSocketConnect()},3E3)}}catch(a){alert("Error: "+a)}}
function getWsUrl(){var a=document.URL;if("https"==a.substring(0,5)){var b="wss://";a=a.substr(8)}else b="ws://","http"==a.substring(0,4)&&(a=a.substr(7));a=a.split("#");var c=/\/$/.test(a[0])?"":"/";return b+a[0]+c+"ws"} function getWsUrl(){var a=document.URL;if("https"==a.substring(0,5)){var b="wss://";a=a.substr(8)}else b="ws://","http"==a.substring(0,4)&&(a=a.substr(7));a=a.split("#");var c=/\/$/.test(a[0])?"":"/";return b+a[0]+c+"ws"}
function parseStats(a){document.getElementById("mpdstats_artists").innerText=a.data.artists;document.getElementById("mpdstats_albums").innerText=a.data.albums;document.getElementById("mpdstats_songs").innerText=a.data.songs;document.getElementById("mpdstats_dbplaytime").innerText=beautifyDuration(a.data.dbplaytime);document.getElementById("mpdstats_playtime").innerText=beautifyDuration(a.data.playtime);document.getElementById("mpdstats_uptime").innerText=beautifyDuration(a.data.uptime);var b=new Date(1E3* function parseStats(a){document.getElementById("mpdstats_artists").innerText=a.data.artists;document.getElementById("mpdstats_albums").innerText=a.data.albums;document.getElementById("mpdstats_songs").innerText=a.data.songs;document.getElementById("mpdstats_dbplaytime").innerText=beautifyDuration(a.data.dbplaytime);document.getElementById("mpdstats_playtime").innerText=beautifyDuration(a.data.playtime);document.getElementById("mpdstats_uptime").innerText=beautifyDuration(a.data.uptime);var b=new Date(1E3*
a.data.dbupdated);document.getElementById("mpdstats_dbupdated").innerText=b.toUTCString();document.getElementById("mympdVersion").innerText=a.data.mympd_version;document.getElementById("mpdVersion").innerText=a.data.mpd_version}function toggleBtn(a,b){a=document.getElementById(a);void 0==b&&(b=a.classList.contains("active")?0:1);1==b?(a.classList.add("active"),a.setAttribute("aria-pressed","true")):(a.classList.remove("active"),a.setAttribute("aria-pressed","false"))} a.data.dbupdated);document.getElementById("mpdstats_dbupdated").innerText=b.toUTCString();document.getElementById("mympdVersion").innerText=a.data.mympd_version;document.getElementById("mpdVersion").innerText=a.data.mpd_version}function toggleBtn(a,b){if(a=document.getElementById(a))void 0==b&&(b=a.classList.contains("active")?0:1),1==b?(a.classList.add("active"),a.setAttribute("aria-pressed","true")):(a.classList.remove("active"),a.setAttribute("aria-pressed","false"))}
function parseSettings(a){toggleBtn("btnRandom",a.data.random);toggleBtn("btnConsume",a.data.consume);toggleBtn("btnSingle",a.data.single);toggleBtn("btnRepeat",a.data.repeat);void 0!=a.data.crossfade?(document.getElementById("inputCrossfade").removeAttribute("disabled"),document.getElementById("inputCrossfade").value=a.data.crossfade):document.getElementById("inputCrossfade").setAttribute("disabled","disabled");void 0!=a.data.mixrampdb?(document.getElementById("inputMixrampdb").removeAttribute("disabled"), function parseSettings(a){toggleBtn("btnRandom",a.data.random);toggleBtn("btnConsume",a.data.consume);toggleBtn("btnSingle",a.data.single);toggleBtn("btnRepeat",a.data.repeat);void 0!=a.data.crossfade?(document.getElementById("inputCrossfade").removeAttribute("disabled"),document.getElementById("inputCrossfade").value=a.data.crossfade):document.getElementById("inputCrossfade").setAttribute("disabled","disabled");void 0!=a.data.mixrampdb?(document.getElementById("inputMixrampdb").removeAttribute("disabled"),
document.getElementById("inputMixrampdb").value=a.data.mixrampdb):document.getElementById("inputMixrampdb").setAttribute("disabled","disabled");void 0!=a.data.mixrampdelay?(document.getElementById("inputMixrampdelay").removeAttribute("disabled"),document.getElementById("inputMixrampdelay").value=a.data.mixrampdelay):document.getElementById("inputMixrampdelay").setAttribute("disabled","disabled");document.getElementById("selectReplaygain").value=a.data.replaygain;var b=document.getElementById("btnnotifyWeb"); document.getElementById("inputMixrampdb").value=a.data.mixrampdb):document.getElementById("inputMixrampdb").setAttribute("disabled","disabled");void 0!=a.data.mixrampdelay?(document.getElementById("inputMixrampdelay").removeAttribute("disabled"),document.getElementById("inputMixrampdelay").value=a.data.mixrampdelay):document.getElementById("inputMixrampdelay").setAttribute("disabled","disabled");document.getElementById("selectReplaygain").value=a.data.replaygain;var b=document.getElementById("btnnotifyWeb");
notificationsSupported()?a.data.notificationWeb?(toggleBtn("btnnotifyWeb",a.data.notificationWeb),Notification.requestPermission(function(b){"permission"in Notification||(Notification.permission=b);"granted"===b?toggleBtn("btnnotifyWeb",1):(toggleBtn("btnnotifyWeb",0),a.data.notificationWeb=0)})):toggleBtn("btnnotifyWeb",0):(b.setAttribute("disabled","disabled"),toggleBtn("btnnotifyWeb",0));toggleBtn("btnnotifyPage",a.data.notificationPage);settings=a.data;settings.mpdstream="http://";settings.mpdstream= notificationsSupported()?a.data.notificationWeb?(toggleBtn("btnnotifyWeb",a.data.notificationWeb),Notification.requestPermission(function(b){"permission"in Notification||(Notification.permission=b);"granted"===b?toggleBtn("btnnotifyWeb",1):(toggleBtn("btnnotifyWeb",0),a.data.notificationWeb=0)})):toggleBtn("btnnotifyWeb",0):(b.setAttribute("disabled","disabled"),toggleBtn("btnnotifyWeb",0));toggleBtn("btnnotifyPage",a.data.notificationPage);settings=a.data;settings.mpdstream="http://";settings.mpdstream=
@ -50,8 +50,7 @@ function parseState(a){if(JSON.stringify(a)!==JSON.stringify(last_state)){1==a.d
0==a.data.queue_length?domCache.btnPlay.setAttribute("disabled","disabled"):domCache.btnPlay.removeAttribute("disabled");-1==a.data.volume?(domCache.volumePrct.innerText("Volumecontrol disabled"),domCache.volumeControl.classList.add("hide")):(domCache.volumeControl.classList.remove("hide"),domCache.volumePrct.innerText=a.data.volume+" %",domCache.volumeIcon.innerText=0==a.data.volume?"volume_off":50>a.data.volume?"volume_down":"volume_up");domCache.volumeBar.value=a.data.volume;current_song.totalTime= 0==a.data.queue_length?domCache.btnPlay.setAttribute("disabled","disabled"):domCache.btnPlay.removeAttribute("disabled");-1==a.data.volume?(domCache.volumePrct.innerText("Volumecontrol disabled"),domCache.volumeControl.classList.add("hide")):(domCache.volumeControl.classList.remove("hide"),domCache.volumePrct.innerText=a.data.volume+" %",domCache.volumeIcon.innerText=0==a.data.volume?"volume_off":50>a.data.volume?"volume_down":"volume_up");domCache.volumeBar.value=a.data.volume;current_song.totalTime=
a.data.totalTime;current_song.currentSongId=a.data.currentsongid;var b=Math.floor(a.data.totalTime/60),c=a.data.totalTime-60*b,e=Math.floor(a.data.elapsedTime/60),d=a.data.elapsedTime-60*e;domCache.progressBar.value=Math.floor(100*a.data.elapsedTime/a.data.totalTime);b=e+":"+(10>d?"0":"")+d+" / "+b+":"+(10>c?"0":"")+c;domCache.counter.innerText=b;last_state&&(c=document.getElementById("queueTrackId"+last_state.data.currentsongid))&&(e=c.getElementsByTagName("td"),e[4].innerText=c.getAttribute("data-duration"), a.data.totalTime;current_song.currentSongId=a.data.currentsongid;var b=Math.floor(a.data.totalTime/60),c=a.data.totalTime-60*b,e=Math.floor(a.data.elapsedTime/60),d=a.data.elapsedTime-60*e;domCache.progressBar.value=Math.floor(100*a.data.elapsedTime/a.data.totalTime);b=e+":"+(10>d?"0":"")+d+" / "+b+":"+(10>c?"0":"")+c;domCache.counter.innerText=b;last_state&&(c=document.getElementById("queueTrackId"+last_state.data.currentsongid))&&(e=c.getElementsByTagName("td"),e[4].innerText=c.getAttribute("data-duration"),
e[0].classList.remove("material-icons"),e[0].innerText=c.getAttribute("data-songpos"),c.classList.remove("font-weight-bold"));if(c=document.getElementById("queueTrackId"+a.data.currentsongid))e=c.getElementsByTagName("td"),e[4].innerText=b,e[0].classList.add("material-icons"),e[0].innerText="play_arrow",c.classList.add("font-weight-bold");void 0!=last_state&&a.data.queue_version==last_state.data.queue_version||sendAPI({cmd:"MPD_API_GET_CURRENT_SONG"},songChange);b=a.data.outputs.length;for(c=0;c< e[0].classList.remove("material-icons"),e[0].innerText=c.getAttribute("data-songpos"),c.classList.remove("font-weight-bold"));if(c=document.getElementById("queueTrackId"+a.data.currentsongid))e=c.getElementsByTagName("td"),e[4].innerText=b,e[0].classList.add("material-icons"),e[0].innerText="play_arrow",c.classList.add("font-weight-bold");void 0!=last_state&&a.data.queue_version==last_state.data.queue_version||sendAPI({cmd:"MPD_API_GET_CURRENT_SONG"},songChange);b=a.data.outputs.length;for(c=0;c<
b;c++)toggleBtn("btnoutput"+a.data.outputs[c].id,a.data.outputs[c].state);last_state=a}} b;c++)toggleBtn("btnoutput"+a.data.outputs[c].id,a.data.outputs[c].state);last_state=a}}function getQueue(){2<=app.current.search.length?sendAPI({cmd:"MPD_API_SEARCH_QUEUE",data:{mpdtag:app.current.filter,offset:app.current.page,searchstr:app.current.search}},parseQueue):sendAPI({cmd:"MPD_API_GET_QUEUE",data:{offset:app.current.page}},parseQueue)}
function getQueue(){if(2<=app.current.search.length)sendAPI({cmd:"MPD_API_SEARCH_QUEUE",data:{mpdtag:app.current.filter,offset:app.current.page,searchstr:app.current.search}},parseQueue);else{var a=document.getElementById("QueueList").getAttribute("data-version");last_state&&a!=last_state.data.queue_version?sendAPI({cmd:"MPD_API_GET_QUEUE",data:{offset:app.current.page}},parseQueue):document.getElementById("QueueList").classList.remove("opacity05")}}
function parseQueue(a){if("Queue"===app.current.app){0<a.totalTime?document.getElementById("panel-heading-queue").innerText=a.totalEntities+" Songs \u2013 "+beautifyDuration(a.totalTime):0<a.totalEntities?document.getElementById("panel-heading-queue").innerText=a.totalEntities+" Songs":document.getElementById("panel-heading-queue").innerText="";var b=a.data.length,c=document.getElementById(app.current.app+"List");c.setAttribute("data-version",a.queue_version);c=c.getElementsByTagName("tbody")[0]; function parseQueue(a){if("Queue"===app.current.app){0<a.totalTime?document.getElementById("panel-heading-queue").innerText=a.totalEntities+" Songs \u2013 "+beautifyDuration(a.totalTime):0<a.totalEntities?document.getElementById("panel-heading-queue").innerText=a.totalEntities+" Songs":document.getElementById("panel-heading-queue").innerText="";var b=a.data.length,c=document.getElementById(app.current.app+"List");c.setAttribute("data-version",a.queue_version);c=c.getElementsByTagName("tbody")[0];
for(var e=c.getElementsByTagName("tr"),d=0;d<b;d++)if(!e[d]||e[d].getAttribute("data-trackid")!=a.data[d].id){var f=Math.floor(a.data[d].duration/60),g=a.data[d].duration-60*f;f=f+":"+(10>g?"0":"")+g;g=document.createElement("tr");g.setAttribute("data-trackid",a.data[d].id);g.setAttribute("id","queueTrackId"+a.data[d].id);g.setAttribute("data-songpos",a.data[d].pos+1);g.setAttribute("data-duration",f);g.setAttribute("data-uri",a.data[d].uri);g.innerHTML="<td>"+(a.data[d].pos+1)+"</td><td>"+a.data[d].title+ for(var e=c.getElementsByTagName("tr"),d=0;d<b;d++)if(!e[d]||e[d].getAttribute("data-trackid")!=a.data[d].id){var f=Math.floor(a.data[d].duration/60),g=a.data[d].duration-60*f;f=f+":"+(10>g?"0":"")+g;g=document.createElement("tr");g.setAttribute("data-trackid",a.data[d].id);g.setAttribute("id","queueTrackId"+a.data[d].id);g.setAttribute("data-songpos",a.data[d].pos+1);g.setAttribute("data-duration",f);g.setAttribute("data-uri",a.data[d].uri);g.innerHTML="<td>"+(a.data[d].pos+1)+"</td><td>"+a.data[d].title+
"</td><td>"+a.data[d].artist+"</td><td>"+a.data[d].album+"</td><td>"+f+'</td><td><a href="#" class="material-icons color-darkgrey">playlist_add</a></td>';d<e.length?e[d].replaceWith(g):c.append(g)}for(d=e.length-1;d>=b;d--)e[d].remove();"queuesearch"==a.type&&0==b?c.innerHTML='<tr><td><span class="material-icons">error_outline</span></td><td colspan="5">No results, please refine your search!</td></tr>':"queue"==a.type&&0==b&&(c.innerHTML='<tr><td><span class="material-icons">error_outline</span></td><td colspan="5">Empty queue</td></tr>'); "</td><td>"+a.data[d].artist+"</td><td>"+a.data[d].album+"</td><td>"+f+'</td><td><a href="#" class="material-icons color-darkgrey">playlist_add</a></td>';d<e.length?e[d].replaceWith(g):c.append(g)}for(d=e.length-1;d>=b;d--)e[d].remove();"queuesearch"==a.type&&0==b?c.innerHTML='<tr><td><span class="material-icons">error_outline</span></td><td colspan="5">No results, please refine your search!</td></tr>':"queue"==a.type&&0==b&&(c.innerHTML='<tr><td><span class="material-icons">error_outline</span></td><td colspan="5">Empty queue</td></tr>');

View File

@ -121,7 +121,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
} }
free(p_charbuf1); free(p_charbuf1);
} }
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"result\", \"data\": \"ok\"}"); n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
break; break;
case MPD_API_GET_ARTISTALBUMTITLES: case MPD_API_GET_ARTISTALBUMTITLES:
je = json_scanf(msg.p, msg.len, "{ data: { albumartist:%Q, album:%Q } }", &p_charbuf1, &p_charbuf2); je = json_scanf(msg.p, msg.len, "{ data: { albumartist:%Q, album:%Q } }", &p_charbuf1, &p_charbuf2);
@ -129,41 +129,52 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
n = mympd_put_songs_in_album(mpd.buf, p_charbuf1, p_charbuf2); n = mympd_put_songs_in_album(mpd.buf, p_charbuf1, p_charbuf2);
free(p_charbuf1); free(p_charbuf1);
free(p_charbuf2); free(p_charbuf2);
} }
break; break;
case MPD_API_WELCOME: case MPD_API_WELCOME:
n = mympd_put_welcome(mpd.buf); n = mympd_put_welcome(mpd.buf);
break; break;
case MPD_API_UPDATE_DB: case MPD_API_UPDATE_DB:
mpd_run_update(mpd.conn, NULL); mpd_run_update(mpd.conn, NULL);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
break; break;
case MPD_API_SET_PAUSE: case MPD_API_SET_PAUSE:
mpd_run_toggle_pause(mpd.conn); mpd_run_toggle_pause(mpd.conn);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
break; break;
case MPD_API_SET_PREV: case MPD_API_SET_PREV:
mpd_run_previous(mpd.conn); mpd_run_previous(mpd.conn);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
break; break;
case MPD_API_SET_NEXT: case MPD_API_SET_NEXT:
mpd_run_next(mpd.conn); mpd_run_next(mpd.conn);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
break; break;
case MPD_API_SET_PLAY: case MPD_API_SET_PLAY:
mpd_run_play(mpd.conn); mpd_run_play(mpd.conn);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
break; break;
case MPD_API_SET_STOP: case MPD_API_SET_STOP:
mpd_run_stop(mpd.conn); mpd_run_stop(mpd.conn);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
break; break;
case MPD_API_RM_ALL: case MPD_API_RM_ALL:
mpd_run_clear(mpd.conn); mpd_run_clear(mpd.conn);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
break; break;
case MPD_API_RM_TRACK: case MPD_API_RM_TRACK:
je = json_scanf(msg.p, msg.len, "{ data: { track:%u } }", &uint_buf1); je = json_scanf(msg.p, msg.len, "{ data: { track:%u } }", &uint_buf1);
if (je == 1) if (je == 1) {
mpd_run_delete_id(mpd.conn, uint_buf1); mpd_run_delete_id(mpd.conn, uint_buf1);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
}
break; break;
case MPD_API_RM_RANGE: case MPD_API_RM_RANGE:
je = json_scanf(msg.p, msg.len, "{ data: { start:%u, end:%u } }", &uint_buf1, &uint_buf2); je = json_scanf(msg.p, msg.len, "{ data: { start:%u, end:%u } }", &uint_buf1, &uint_buf2);
if (je == 2) if (je == 2) {
mpd_run_delete_range(mpd.conn, uint_buf1, uint_buf2); mpd_run_delete_range(mpd.conn, uint_buf1, uint_buf2);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
}
break; break;
case MPD_API_MOVE_TRACK: case MPD_API_MOVE_TRACK:
je = json_scanf(msg.p, msg.len, "{ data: { track:%u, pos:%u } }", &uint_buf1, &uint_buf2); je = json_scanf(msg.p, msg.len, "{ data: { track:%u, pos:%u } }", &uint_buf1, &uint_buf2);
@ -171,40 +182,48 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
uint_buf1 -= 1; uint_buf1 -= 1;
uint_buf2 -= 1; uint_buf2 -= 1;
mpd_run_move(mpd.conn, uint_buf1, uint_buf2); mpd_run_move(mpd.conn, uint_buf1, uint_buf2);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
} }
break; break;
case MPD_API_PLAY_TRACK: case MPD_API_PLAY_TRACK:
je = json_scanf(msg.p, msg.len, "{ data: { track:%u } }", &uint_buf1); je = json_scanf(msg.p, msg.len, "{ data: { track:%u } }", &uint_buf1);
if (je == 1) if (je == 1) {
mpd_run_play_id(mpd.conn, uint_buf1); mpd_run_play_id(mpd.conn, uint_buf1);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
}
break; break;
case MPD_API_GET_OUTPUTNAMES: case MPD_API_GET_OUTPUTNAMES:
n = mympd_put_outputnames(mpd.buf); n = mympd_put_outputnames(mpd.buf);
break; break;
case MPD_API_TOGGLE_OUTPUT: case MPD_API_TOGGLE_OUTPUT:
je = json_scanf(msg.p, msg.len, "{ data: { output:%u, state:%u } }", &uint_buf1, &uint_buf2); je = json_scanf(msg.p, msg.len, "{ data: { output:%u, state:%u } }", &uint_buf1, &uint_buf2);
if (je == 2) if (je == 2) {
{
if (uint_buf2) if (uint_buf2)
mpd_run_enable_output(mpd.conn, uint_buf1); mpd_run_enable_output(mpd.conn, uint_buf1);
else else
mpd_run_disable_output(mpd.conn, uint_buf1); mpd_run_disable_output(mpd.conn, uint_buf1);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
} }
break; break;
case MPD_API_SET_VOLUME: case MPD_API_SET_VOLUME:
je = json_scanf(msg.p, msg.len, "{ data: { volume:%u } }", &uint_buf1); je = json_scanf(msg.p, msg.len, "{ data: { volume:%u } }", &uint_buf1);
if (je == 1) if (je == 1) {
mpd_run_set_volume(mpd.conn, uint_buf1); mpd_run_set_volume(mpd.conn, uint_buf1);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
}
break; break;
case MPD_API_SET_SEEK: case MPD_API_SET_SEEK:
je = json_scanf(msg.p, msg.len, "{ data: { songid:%u, seek:%u } }", &uint_buf1, &uint_buf2); je = json_scanf(msg.p, msg.len, "{ data: { songid:%u, seek:%u } }", &uint_buf1, &uint_buf2);
if (je == 2) if (je == 2) {
mpd_run_seek_id(mpd.conn, uint_buf1, uint_buf2); mpd_run_seek_id(mpd.conn, uint_buf1, uint_buf2);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
}
break; break;
case MPD_API_GET_QUEUE: case MPD_API_GET_QUEUE:
je = json_scanf(msg.p, msg.len, "{ data: { offset:%u } }", &uint_buf1); je = json_scanf(msg.p, msg.len, "{ data: { offset:%u } }", &uint_buf1);
if (je == 1) if (je == 1) {
n = mympd_put_queue(mpd.buf, uint_buf1); n = mympd_put_queue(mpd.buf, uint_buf1);
}
break; break;
case MPD_API_GET_CURRENT_SONG: case MPD_API_GET_CURRENT_SONG:
n = mympd_put_current_song(mpd.buf); n = mympd_put_current_song(mpd.buf);
@ -251,6 +270,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
if (je == 2) { if (je == 2) {
mpd_run_add_id_to(mpd.conn, p_charbuf1, uint_buf1); mpd_run_add_id_to(mpd.conn, p_charbuf1, uint_buf1);
free(p_charbuf1); free(p_charbuf1);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
} }
break; break;
case MPD_API_REPLACE_TRACK: case MPD_API_REPLACE_TRACK:
@ -260,6 +280,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
mpd_run_add(mpd.conn, p_charbuf1); mpd_run_add(mpd.conn, p_charbuf1);
free(p_charbuf1); free(p_charbuf1);
mpd_run_play(mpd.conn); mpd_run_play(mpd.conn);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
} }
break; break;
case MPD_API_ADD_TRACK: case MPD_API_ADD_TRACK:
@ -267,6 +288,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
if (je == 1) { if (je == 1) {
mpd_run_add(mpd.conn, p_charbuf1); mpd_run_add(mpd.conn, p_charbuf1);
free(p_charbuf1); free(p_charbuf1);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
} }
break; break;
case MPD_API_ADD_PLAY_TRACK: case MPD_API_ADD_PLAY_TRACK:
@ -276,6 +298,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
if(int_buf != -1) if(int_buf != -1)
mpd_run_play_id(mpd.conn, int_buf); mpd_run_play_id(mpd.conn, int_buf);
free(p_charbuf1); free(p_charbuf1);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
} }
break; break;
case MPD_API_REPLACE_PLAYLIST: case MPD_API_REPLACE_PLAYLIST:
@ -285,6 +308,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
mpd_run_load(mpd.conn, p_charbuf1); mpd_run_load(mpd.conn, p_charbuf1);
free(p_charbuf1); free(p_charbuf1);
mpd_run_play(mpd.conn); mpd_run_play(mpd.conn);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
} }
break; break;
case MPD_API_ADD_PLAYLIST: case MPD_API_ADD_PLAYLIST:
@ -292,6 +316,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
if (je == 1) { if (je == 1) {
mpd_run_load(mpd.conn, p_charbuf1); mpd_run_load(mpd.conn, p_charbuf1);
free(p_charbuf1); free(p_charbuf1);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
} }
break; break;
case MPD_API_SAVE_QUEUE: case MPD_API_SAVE_QUEUE:
@ -299,6 +324,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
if (je == 1) { if (je == 1) {
mpd_run_save(mpd.conn, p_charbuf1); mpd_run_save(mpd.conn, p_charbuf1);
free(p_charbuf1); free(p_charbuf1);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
} }
break; break;
case MPD_API_SEARCH_QUEUE: case MPD_API_SEARCH_QUEUE:
@ -327,6 +353,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
break; break;
case MPD_API_SEND_SHUFFLE: case MPD_API_SEND_SHUFFLE:
mpd_run_shuffle(mpd.conn); mpd_run_shuffle(mpd.conn);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
break; break;
case MPD_API_SEND_MESSAGE: case MPD_API_SEND_MESSAGE:
je = json_scanf(msg.p, msg.len, "{ data: { channel:%Q, text:%Q } }", &p_charbuf1, &p_charbuf2); je = json_scanf(msg.p, msg.len, "{ data: { channel:%Q, text:%Q } }", &p_charbuf1, &p_charbuf2);
@ -334,6 +361,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
mpd_run_send_message(mpd.conn, p_charbuf1, p_charbuf2); mpd_run_send_message(mpd.conn, p_charbuf1, p_charbuf2);
free(p_charbuf1); free(p_charbuf1);
free(p_charbuf2); free(p_charbuf2);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
} }
break; break;
case MPD_API_RM_PLAYLIST: case MPD_API_RM_PLAYLIST:
@ -341,6 +369,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
if (je == 1) { if (je == 1) {
mpd_run_rm(mpd.conn, p_charbuf1); mpd_run_rm(mpd.conn, p_charbuf1);
free(p_charbuf1); free(p_charbuf1);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
} }
break; break;
case MPD_API_GET_SETTINGS: case MPD_API_GET_SETTINGS:
@ -355,7 +384,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
if (mpd.conn_state == MPD_CONNECTED && mpd_connection_get_error(mpd.conn) != MPD_ERROR_SUCCESS) if (mpd.conn_state == MPD_CONNECTED && mpd_connection_get_error(mpd.conn) != MPD_ERROR_SUCCESS)
{ {
#ifdef DEBUG #ifdef DEBUG
fprintf(stdout,"Error: %s\n",mpd_connection_get_error_message(mpd.conn)); fprintf(stderr,"Error: %s\n", mpd_connection_get_error_message(mpd.conn));
#endif #endif
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"error\", \"data\": \"%s\"}", n = snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"error\", \"data\": \"%s\"}",
mpd_connection_get_error_message(mpd.conn)); mpd_connection_get_error_message(mpd.conn));
@ -365,20 +394,21 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
mpd.conn_state = MPD_FAILURE; mpd.conn_state = MPD_FAILURE;
} }
if (n > 0) { if (n == 0)
if (is_websocket(nc)) { n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"error\", \"data\": \"No response for cmd %s\"}", cmd);
#ifdef DEBUG
fprintf(stdout,"Send websocket response:\n %s\n",mpd.buf); if (is_websocket(nc)) {
#endif #ifdef DEBUG
mg_send_websocket_frame(nc, WEBSOCKET_OP_TEXT, mpd.buf, n); fprintf(stdout,"Send websocket response:\n %s\n",mpd.buf);
} #endif
else { mg_send_websocket_frame(nc, WEBSOCKET_OP_TEXT, mpd.buf, n);
#ifdef DEBUG }
fprintf(stdout,"Send http response:\n %s\n",mpd.buf); else {
#endif #ifdef DEBUG
mg_send_http_chunk(nc, mpd.buf, n); fprintf(stdout,"Send http response:\n %s\n",mpd.buf);
} #endif
} mg_send_http_chunk(nc, mpd.buf, n);
}
} }
int mympd_close_handler(struct mg_connection *c) { int mympd_close_handler(struct mg_connection *c) {