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

API Improvements

This commit is contained in:
jcorporation 2018-06-19 23:43:36 +01:00
parent ef7b7b9017
commit 8555803683
4 changed files with 179 additions and 205 deletions

View File

@ -33,6 +33,7 @@ var playstate = '';
var progressBar; var progressBar;
var volumeBar; var volumeBar;
var coverImageFile = ''; var coverImageFile = '';
var settings = { "notifyPage":1, "notifyWeb": 0 };
var app = {}; var app = {};
@ -120,10 +121,7 @@ app.route=function() {
app.prepare(); app.prepare();
if (app.current.app == 'Playback') { if (app.current.app == 'Playback') {
$.ajax({url: "/api", contentType:"application/json", method: "POST", sendAPI({"cmd":"MPD_API_GET_CURRENT_SONG"},songChange);
data: JSON.stringify({"cmd":"MPD_API_GET_CURRENT_SONG"})}).done(function( obj ) {
songChange(obj.data.title, obj.data.artist, obj.data.album, obj.data.uri, obj.data.currentsongid);
});
} }
else if (app.current.app == 'Queue' ) { else if (app.current.app == 'Queue' ) {
if (app.last.app != app.current.app) { if (app.last.app != app.current.app) {
@ -141,39 +139,23 @@ app.route=function() {
getQueue(); getQueue();
} }
else if (app.current.app == 'Browse' && app.current.tab == 'Playlists') { else if (app.current.app == 'Browse' && app.current.tab == 'Playlists') {
$.ajax({url: "/api", contentType:"application/json", method: "POST", sendAPI({"cmd":"MPD_API_GET_PLAYLISTS","data": {"offset": app.current.page, "filter": app.current.filter}},parsePlaylists);
data: JSON.stringify({"cmd":"MPD_API_GET_PLAYLISTS","data": { "offset": app.current.page, "filter": app.current.filter}})}).done(function( obj ) {
parsePlaylists(obj);
doSetFilterLetter('#browsePlaylistsFilter');
});
} }
else if (app.current.app == 'Browse' && app.current.tab == 'Database' && app.current.view == 'Artist') { else if (app.current.app == 'Browse' && app.current.tab == 'Database' && app.current.view == 'Artist') {
$.ajax({url: "/api", contentType:"application/json", method: "POST", sendAPI({"cmd":"MPD_API_GET_ARTISTS","data": {"offset": app.current.page, "filter": app.current.filter}},parseListDBtags);
data: JSON.stringify({"cmd":"MPD_API_GET_ARTISTS","data": { "offset": app.current.page, "filter": app.current.filter}})}).done(function( obj ) {
parseListDBtags(obj);
doSetFilterLetter('#BrowseDatabaseFilter');
});
} }
else if (app.current.app == 'Browse' && app.current.tab == 'Database' && app.current.view == 'Album') { else if (app.current.app == 'Browse' && app.current.tab == 'Database' && app.current.view == 'Album') {
$.ajax({url: "/api", contentType:"application/json", method: "POST", sendAPI({"cmd":"MPD_API_GET_ARTISTALBUMS","data": {"offset": app.current.page, "filter": app.current.filter, "albumartist": app.current.search}},parseListDBtags);
data: JSON.stringify({"cmd":"MPD_API_GET_ARTISTALBUMS","data": { "offset": app.current.page, "filter": app.current.filter, "albumartist": app.current.search}})}).done(function( obj ) {
parseListDBtags(obj);
doSetFilterLetter('#BrowseDatabaseFilter');
});
} }
else if (app.current.app == 'Browse' && app.current.tab == 'Filesystem') { else if (app.current.app == 'Browse' && app.current.tab == 'Filesystem') {
$('#BrowseBreadcrumb').empty().append('<li class="breadcrumb-item"><a uri="">root</a></li>'); $('#BrowseBreadcrumb').empty().append('<li class="breadcrumb-item"><a uri="">root</a></li>');
$.ajax({url: "/api", contentType:"application/json", method: "POST", sendAPI({"cmd":"MPD_API_GET_FILESYSTEM","data": {"offset": app.current.page,"path":(app.current.search ? app.current.search : "/"),"filter": app.current.filter}},parseFilesystem);
data: JSON.stringify({"cmd":"MPD_API_GET_FILESYSTEM","data": { "offset": app.current.page,"path":(app.current.search ? app.current.search : "/"),"filter": app.current.filter}})}).done(function( obj ) {
parseFilesystem(obj);
doSetFilterLetter('#BrowseFilesystemFilter');
});
// Don't add all songs from root // Don't add all songs from root
var add_all_songs = $('#browseFilesystemAddAllSongs'); var add_all_songs = $('#browseFilesystemAddAllSongs');
if (app.current.search) { if (app.current.search) {
add_all_songs.off(); // remove previous binds add_all_songs.off(); // remove previous binds
add_all_songs.on('click', function() { add_all_songs.on('click', function() {
sendAPI(JSON.stringify({"cmd":"MPD_API_ADD_TRACK", "data": { "uri": app.current.search}})); sendAPI({"cmd":"MPD_API_ADD_TRACK", "data": { "uri": app.current.search}});
}); });
add_all_songs.removeAttr('disabled').removeClass('disabled'); add_all_songs.removeAttr('disabled').removeClass('disabled');
} else { } else {
@ -187,7 +169,6 @@ app.route=function() {
$('#BrowseBreadcrumb').append("<li class=\"breadcrumb-item active\">"+ chunk + "</li>"); $('#BrowseBreadcrumb').append("<li class=\"breadcrumb-item active\">"+ chunk + "</li>");
return; return;
} }
full_path = full_path + chunk; full_path = full_path + chunk;
$('#BrowseBreadcrumb').append("<li class=\"breadcrumb-item\"><a uri=\"" + full_path + "\">"+chunk+"</a></li>"); $('#BrowseBreadcrumb').append("<li class=\"breadcrumb-item\"><a uri=\"" + full_path + "\">"+chunk+"</a></li>");
full_path += "/"; full_path += "/";
@ -214,10 +195,7 @@ app.route=function() {
} }
}); });
if (app.current.search.length >= 2) { if (app.current.search.length >= 2) {
$.ajax({url: "/api", contentType:"application/json", method: "POST", sendAPI({"cmd":"MPD_API_SEARCH", "data": { "mpdtag": app.current.filter,"offset":app.current.page,"searchstr":app.current.search}},parseSearch);
data: JSON.stringify({"cmd":"MPD_API_SEARCH", "data": { "mpdtag": app.current.filter,"offset":app.current.page,"searchstr":app.current.search}})}).done(function( obj ) {
parseSearch(obj);
});
} else { } else {
$('#SearchList > tbody').empty(); $('#SearchList > tbody').empty();
$('#searchAddAllSongs').attr('disabled','disabled').addClass('disabled'); $('#searchAddAllSongs').attr('disabled','disabled').addClass('disabled');
@ -233,22 +211,16 @@ app.route=function() {
}; };
$(document).ready(function(){ $(document).ready(function(){
$.ajax({url: "/api", contentType:"application/json", method: "POST", sendAPI({"cmd":"MPD_API_GET_MPD_SETTINGS"},parseSettings);
data: JSON.stringify({"cmd":"MPD_API_GET_SETTINGS"})}).done(function( obj ) { sendAPI({"cmd":"MPD_API_GET_MYMPD_SETTINGS"},parseMympdSettings);
parseSettings(obj); sendAPI({"cmd":"MPD_API_GET_OUTPUTNAMES"},parseOutputnames);
});
$.ajax({url: "/api", contentType:"application/json", method: "POST",
data: JSON.stringify({"cmd":"MPD_API_GET_OUTPUTNAMES"})}).done(function( obj ) {
parseOutputnames(obj);
});
webSocketConnect(); webSocketConnect();
volumeBar=$('#volumebar').slider(); volumeBar=$('#volumebar').slider();
volumeBar.slider('setValue',0); volumeBar.slider('setValue',0);
volumeBar.slider('on','slideStop', function(value){ volumeBar.slider('on','slideStop', function(value){
sendAPI(JSON.stringify({"cmd":"MPD_API_SET_VOLUME","data": {"volume":value}})); sendAPI({"cmd":"MPD_API_SET_VOLUME","data": {"volume":value}});
}); });
progressBar=$('#progressbar').slider(); progressBar=$('#progressbar').slider();
@ -257,22 +229,16 @@ $(document).ready(function(){
progressBar.slider('on','slideStop', function(value){ progressBar.slider('on','slideStop', function(value){
if(current_song && current_song.currentSongId >= 0) { if(current_song && current_song.currentSongId >= 0) {
var seekVal = Math.ceil(current_song.totalTime*(value/100)); var seekVal = Math.ceil(current_song.totalTime*(value/100));
sendAPI(JSON.stringify({"cmd":"MPD_API_SET_SEEK", "data": {"songid":current_song.currentSongId,"seek":seekVal}})); sendAPI({"cmd":"MPD_API_SET_SEEK", "data": {"songid":current_song.currentSongId,"seek":seekVal}});
} }
}); });
$('#about').on('shown.bs.modal', function () { $('#about').on('shown.bs.modal', function () {
$.ajax({url: "/api", contentType:"application/json", method: "POST", sendAPI({"cmd":"MPD_API_GET_STATS"},parseStats);
data: JSON.stringify({"cmd":"MPD_API_GET_STATS"})}).done(function( obj ) {
parseStats(obj);
});
}) })
$('#settings').on('shown.bs.modal', function () { $('#settings').on('shown.bs.modal', function () {
$.ajax({url: "/api", contentType:"application/json", method: "POST", sendAPI({"cmd":"MPD_API_GET_SETTINGS"},parseSettings);
data: JSON.stringify({"cmd":"MPD_API_GET_SETTINGS"})}).done(function( obj ) {
parseSettings(obj);
});
}) })
$('#addstream').on('shown.bs.modal', function () { $('#addstream').on('shown.bs.modal', function () {
@ -340,7 +306,7 @@ function webSocketConnect() {
getQueue(); getQueue();
break; break;
case "song_change": case "song_change":
songChange(obj.data.title, obj.data.artist, obj.data.album, obj.data.uri, obj.data.currentsongid); songChange(obj);
break; break;
case 'settings': case 'settings':
parseSettings(obj); parseSettings(obj);
@ -404,6 +370,12 @@ function parseStats(obj) {
$('#mpdVersion').text(obj.data.mpd_version); $('#mpdVersion').text(obj.data.mpd_version);
} }
function parseMympdSettings(obj) {
settings.notifyPage=obj.data.notifyPage;
settings.notifyWeb=obj.data.notifyWeb;
//try to set web notification
}
function parseSettings(obj) { function parseSettings(obj) {
if (!isNaN(obj.data.max_elements_per_page)) if (!isNaN(obj.data.max_elements_per_page))
MAX_ELEMENTS_PER_PAGE=obj.data.max_elements_per_page; MAX_ELEMENTS_PER_PAGE=obj.data.max_elements_per_page;
@ -501,10 +473,7 @@ function parseState(obj) {
//Get current song on queue change for http streams //Get current song on queue change for http streams
if (last_state == undefined || obj.data.queue_version != last_state.data.queue_version) { if (last_state == undefined || obj.data.queue_version != last_state.data.queue_version) {
$.ajax({url: "/api", contentType:"application/json", method: "POST", sendAPI({"cmd":"MPD_API_GET_CURRENT_SONG"},songChange);
data: JSON.stringify({"cmd":"MPD_API_GET_CURRENT_SONG"})}).done(function( obj ) {
songChange(obj.data.title, obj.data.artist, obj.data.album, obj.data.uri, obj.data.currentsongid);
});
} }
last_state = obj; last_state = obj;
@ -519,18 +488,10 @@ function parseState(obj) {
} }
function getQueue() { function getQueue() {
if (app.current.search.length >= 2) { if (app.current.search.length >= 2)
$.ajax({url: "/api", contentType:"application/json", method: "POST", sendAPI({"cmd":"MPD_API_SEARCH_QUEUE", "data": {"mpdtag":app.current.filter, "offset":app.current.page,"searchstr": app.current.search}},parseQueue);
data: JSON.stringify({"cmd":"MPD_API_SEARCH_QUEUE", "data": {"mpdtag":app.current.filter, "offset":app.current.page,"searchstr": app.current.search}})}).done(function( obj ) { else
parseQueue(obj); sendAPI({"cmd":"MPD_API_GET_QUEUE", "data": {"offset": app.current.page}},parseQueue);
});
}
else {
$.ajax({url: "/api", contentType:"application/json", method: "POST",
data: JSON.stringify({"cmd":"MPD_API_GET_QUEUE", "data": {"offset": app.current.page}})}).done(function( obj ) {
parseQueue(obj);
});
}
} }
function parseQueue(obj) { function parseQueue(obj) {
@ -609,7 +570,7 @@ function parseQueue(obj) {
$('#QueueList > tbody > tr').on({ $('#QueueList > tbody > tr').on({
click: function() { click: function() {
$('#queueList > tbody > tr').removeClass('active'); $('#queueList > tbody > tr').removeClass('active');
sendAPI(JSON.stringify({"cmd":"MPD_API_PLAY_TRACK","data": {"track":$(this).attr('trackid')}})); sendAPI({"cmd":"MPD_API_PLAY_TRACK","data": {"track":$(this).attr('trackid')}});
$(this).addClass('active'); $(this).addClass('active');
}, },
}); });
@ -686,7 +647,7 @@ function parseFilesystem(obj) {
'<span class="material-icons">' + glyphicon + '</span></a>') '<span class="material-icons">' + glyphicon + '</span></a>')
.find('a').click(function(e) { .find('a').click(function(e) {
e.stopPropagation(); e.stopPropagation();
sendAPI(JSON.stringify({"cmd":onClickAction,"data":{ "uri":decodeURI($(this).parents("tr").attr("uri"))}})); sendAPI({"cmd":onClickAction,"data":{ "uri":decodeURI($(this).parents("tr").attr("uri"))}});
showNotification('"' + $('td:nth-last-child(3)', $(this).parents('tr')).text() + '" added','','','success'); showNotification('"' + $('td:nth-last-child(3)', $(this).parents('tr')).text() + '" added','','','success');
}); });
} }
@ -717,11 +678,11 @@ function parseFilesystem(obj) {
app.goto('Browse','Filesystem',undefined,app.current.page+'/'+app.current.filter+'/'+app.current.search); app.goto('Browse','Filesystem',undefined,app.current.page+'/'+app.current.filter+'/'+app.current.search);
break; break;
case 'song': case 'song':
sendAPI(JSON.stringify({"cmd":"MPD_API_ADD_TRACK", "data": {"uri": decodeURI($(this).attr("uri"))}})); sendAPI({"cmd":"MPD_API_ADD_TRACK", "data": {"uri": decodeURI($(this).attr("uri"))}});
showNotification('"' + $('td:nth-last-child(5)', this).text() + '" added','','','success'); showNotification('"' + $('td:nth-last-child(5)', this).text() + '" added','','','success');
break; break;
case 'plist': case 'plist':
sendAPI(JSON.stringify({"cmd":"MPD_API_ADD_PLAYLIST", "data": {"plist": decodeURI($(this).attr("uri"))}})); sendAPI({"cmd":"MPD_API_ADD_PLAYLIST", "data": {"plist": decodeURI($(this).attr("uri"))}});
showNotification('"' + $('td:nth-last-child(3)', this).text() + '" added','','','success'); showNotification('"' + $('td:nth-last-child(3)', this).text() + '" added','','','success');
break; break;
} }
@ -736,6 +697,7 @@ function parseFilesystem(obj) {
app.goto('Browse','Filesystem',undefined,app.current.page+'/'+app.current.filter+'/'+app.current.search); app.goto('Browse','Filesystem',undefined,app.current.page+'/'+app.current.filter+'/'+app.current.search);
} }
}); });
doSetFilterLetter('#BrowseFilesystemFilter');
} }
function parsePlaylists(obj) { function parsePlaylists(obj) {
@ -779,7 +741,7 @@ function parsePlaylists(obj) {
}; };
$('#'+app.current.app+app.current.tab+'List > tbody > tr').on({ $('#'+app.current.app+app.current.tab+'List > tbody > tr').on({
click: function() { click: function() {
sendAPI(JSON.stringify({"cmd":"MPD_API_ADD_PLAYLIST", "data": { "plist": decodeURI($(this).attr('uri'))}})); sendAPI({"cmd":"MPD_API_ADD_PLAYLIST", "data": { "plist": decodeURI($(this).attr('uri'))}});
showNotification('"' + $('td:nth-last-child(3)', this).text() + '" added','','','success'); showNotification('"' + $('td:nth-last-child(3)', this).text() + '" added','','','success');
} }
}); });
@ -790,7 +752,7 @@ function parsePlaylists(obj) {
'<td></td><td></td></tr>' '<td></td><td></td></tr>'
); );
} }
doSetFilterLetter('#browsePlaylistsFilter');
} }
function parseListDBtags(obj) { function parseListDBtags(obj) {
@ -843,13 +805,11 @@ function parseListDBtags(obj) {
' </div>'+ ' </div>'+
'</div></div>'; '</div></div>';
$('#BrowseDatabaseAlbumCards').append(card); $('#BrowseDatabaseAlbumCards').append(card);
$.ajax({url: "/api", contentType:"application/json", method: "POST", sendAPI({"cmd":"MPD_API_GET_ARTISTALBUMTITLES", "data": { "albumartist": obj.searchstr, "album": obj.data[item].value}},parseListTitles);
data: JSON.stringify({"cmd":"MPD_API_GET_ARTISTALBUMTITLES", "data": { "albumartist": obj.searchstr, "album": obj.data[item].value}})}).done(function( obj ) {
parseListTitles(obj);
});
} }
setPagination(obj.totalEntities); setPagination(obj.totalEntities);
} }
doSetFilterLetter('#BrowseDatabaseFilter');
} }
function parseListTitles(obj) { function parseListTitles(obj) {
@ -867,14 +827,14 @@ function parseListTitles(obj) {
$('#'+genId(obj.album)+' > img').on({ $('#'+genId(obj.album)+' > img').on({
click: function() { click: function() {
sendAPI(JSON.stringify({"cmd":"MPD_API_ADD_TRACK", "data": { "track": decodeURI($(this).attr('uri'))}})); sendAPI({"cmd":"MPD_API_ADD_TRACK", "data": { "track": decodeURI($(this).attr('uri'))}});
showNotification('"'+decodeURI($(this).attr('data-album'))+'" added','','','success'); showNotification('"'+decodeURI($(this).attr('data-album'))+'" added','','','success');
} }
}); });
$('#tbl'+genId(obj.album)+' > tbody > tr').on({ $('#tbl'+genId(obj.album)+' > tbody > tr').on({
click: function() { click: function() {
sendAPI(JSON.stringify({"cmd":"MPD_API_ADD_TRACK", "data": { "track": decodeURI($(this).attr('uri'))}})); sendAPI({"cmd":"MPD_API_ADD_TRACK", "data": { "track": decodeURI($(this).attr('uri'))}});
showNotification('"' + $('td:nth-last-child(1)', this).text() + '" added','','','success'); showNotification('"' + $('td:nth-last-child(1)', this).text() + '" added','','','success');
} }
}); });
@ -967,33 +927,37 @@ function updatePlayIcon(obj) {
} }
} }
function sendAPI(cmd) { function sendAPI(request, callback) {
$.ajax({url: "/api", contentType:"application/json", method: "POST", data: cmd}); $.ajax({url: "/api", contentType:"application/json", method: "POST", data: JSON.stringify(request)}).done(function(obj,callback) {
if (callback != undefined && typeof callback == 'function') {
callback(obj);
}
});
} }
function updateDB(event) { function updateDB(event) {
sendAPI(JSON.stringify({"cmd":"MPD_API_UPDATE_DB"})); sendAPI({"cmd":"MPD_API_UPDATE_DB"});
showNotification('Updating MPD Database...','','','success'); showNotification('Updating MPD Database...','','','success');
event.preventDefault(); event.preventDefault();
} }
function clickPlay() { function clickPlay() {
if( playstate != 'play') if( playstate != 'play')
sendAPI(JSON.stringify({"cmd":"MPD_API_SET_PLAY"})); sendAPI({"cmd":"MPD_API_SET_PLAY"});
else else
sendAPI(JSON.stringify({"cmd":"MPD_API_SET_PAUSE"})); sendAPI({"cmd":"MPD_API_SET_PAUSE"});
} }
function clickStop() { function clickStop() {
sendAPI(JSON.stringify({"cmd":"MPD_API_SET_STOP"})); sendAPI({"cmd":"MPD_API_SET_STOP"});
} }
function clickPrev() { function clickPrev() {
sendAPI(JSON.stringify({"cmd":"MPD_API_SET_PREV"})); sendAPI({"cmd":"MPD_API_SET_PREV"});
} }
function clickNext() { function clickNext() {
sendAPI(JSON.stringify({"cmd":"MPD_API_SET_NEXT"})); sendAPI({"cmd":"MPD_API_SET_NEXT"});
} }
function setLocalStream(mpdhost,streamport) { function setLocalStream(mpdhost,streamport) {
@ -1009,16 +973,16 @@ function setLocalStream(mpdhost,streamport) {
function delQueueSong(tr,event) { function delQueueSong(tr,event) {
event.stopPropagation(); event.stopPropagation();
if ( $('#btntrashmodeup').hasClass('btn-success') ) { if ( $('#btntrashmodeup').hasClass('btn-success') ) {
sendAPI(JSON.stringify({"cmd":"MPD_API_RM_RANGE", "data": {"start":0, "end": (tr.index() + 1)}})); sendAPI({"cmd":"MPD_API_RM_RANGE", "data": {"start":0, "end": (tr.index() + 1)}});
} else if ( $('#btntrashmodesingle').hasClass('btn-success') ) { } else if ( $('#btntrashmodesingle').hasClass('btn-success') ) {
sendAPI(JSON.stringify({"cmd":"MPD_API_RM_TRACK", "data": { "track": tr.attr('trackid')}})); sendAPI({"cmd":"MPD_API_RM_TRACK", "data": { "track": tr.attr('trackid')}});
} else if ( $('#btntrashmodedown').hasClass('btn-success') ) { } else if ( $('#btntrashmodedown').hasClass('btn-success') ) {
sendAPI(JSON.stringify({"cmd":"MPD_API_RM_RANGE", "data": {"start": tr.index(), "end":-1}})); sendAPI({"cmd":"MPD_API_RM_RANGE", "data": {"start": tr.index(), "end":-1}});
}; };
} }
function delPlaylist(tr) { function delPlaylist(tr) {
sendAPI(JSON.stringify({"cmd":"MPD_API_RM_PLAYLIST","data": {"plist": decodeURI(tr.attr("uri"))}})); sendAPI({"cmd":"MPD_API_RM_PLAYLIST","data": {"plist": decodeURI(tr.attr("uri"))}});
tr.remove(); tr.remove();
} }
@ -1131,23 +1095,26 @@ function confirmSettings() {
} }
} }
if (formOK == true) { if (formOK == true) {
sendAPI(JSON.stringify({"cmd":"MPD_API_TOGGLE_CONSUME","data": {"state": ($('#btnconsume').hasClass('btn-success') ? 1 : 0)}})); sendAPI({"cmd":"MPD_API_SET_MPD_SETTINGS", "data": {
sendAPI(JSON.stringify({"cmd":"MPD_API_TOGGLE_RANDOM","data":{"state": ($('#btnrandom').hasClass('btn-success') ? 1 : 0)}})); "consume": ($('#btnconsume').hasClass('btn-success') ? 1 : 0),
sendAPI(JSON.stringify({"cmd":"MPD_API_TOGGLE_SINGLE","data":{"state": ($('#btnsingle').hasClass('btn-success') ? 1 : 0)}})); "random": ($('#btnrandom').hasClass('btn-success') ? 1 : 0),
sendAPI(JSON.stringify({"cmd":"MPD_API_TOGGLE_REPEAT","data":{"state": ($('#btnrepeat').hasClass('btn-success') ? 1 : 0)}})); "single": ($('#btnsingle').hasClass('btn-success') ? 1 : 0),
sendAPI(JSON.stringify({"cmd":"MPD_API_SET_REPLAYGAIN","data":{"mode": $('#selectReplaygain').val()}})); "repeat": ($('#btnrepeat').hasClass('btn-success') ? 1 : 0),
if (!$('#inputCrossfade').is(':disabled')) "replaygain": $('#selectReplaygain').val(),
sendAPI(JSON.stringify({"cmd":"MPD_API_SET_CROSSFADE","data":{"state": $('#inputCrossfade').val()}})); "crossfade": $('#inputCrossfade').val(),
if (!$('#inputMixrampdb').is(':disabled')) "mixrampdb": $('#inputMixrampdb').val(),
sendAPI(JSON.stringify({"cmd":"MPD_API_SET_MIXRAMPDB","data":{"state": $('#inputMixrampdb').val()}})); "mixrampdelay": $('#inputMixrampdelay').val()
if (!$('#inputMixrampdelay').is(':disabled')) }});
sendAPI(JSON.stringify({"cmd":"MPD_API_SET_MIXRAMPDELAY","data":{"state": $('#inputMixrampdelay').val()}})); sendAPI({"cmd":"MPD_API_SET_MYMPD_SETTINGS","data": {
"notificationsWeb": ($('#btnnotifyWeb').hasClass('btn-success') ? 1 : 0),
"notificationsPage": ($('#btnnotifyPage').hasClass('btn-success') ? 1 : 0)
}});
$('#settings').modal('hide'); $('#settings').modal('hide');
} }
} }
function toggleoutput(button, id) { function toggleoutput(button, id) {
sendAPI(JSON.stringify({"cmd":"MPD_API_TOGGLE_OUTPUT", "data": {"output": id, "state": ($(button).hasClass('btn-success') ? 0 : 1)}})); sendAPI({"cmd":"MPD_API_TOGGLE_OUTPUT", "data": {"output": id, "state": ($(button).hasClass('btn-success') ? 0 : 1)}});
} }
$('#trashmodebtns > button').on('click', function(e) { $('#trashmodebtns > button').on('click', function(e) {
@ -1200,7 +1167,7 @@ $('#search2').submit(function () {
function addAllFromSearch() { function addAllFromSearch() {
if (app.current.search.length >= 2) { if (app.current.search.length >= 2) {
sendAPI(JSON.stringify({"cmd":"MPD_API_SEARCH_ADD","data":{"filter": app.current.filter,"searchstr": + app.current.search}})); sendAPI({"cmd":"MPD_API_SEARCH_ADD","data":{"filter": app.current.filter,"searchstr": + app.current.search}});
var rowCount = $('#SearchList >tbody >tr').length; var rowCount = $('#SearchList >tbody >tr').length;
showNotification('Added '+rowCount+' songs from search','','','success'); showNotification('Added '+rowCount+' songs from search','','','success');
} }
@ -1265,7 +1232,7 @@ function gotoPage(x,element,event) {
function addStream() { function addStream() {
if($('#streamurl').val().length > 0) { if($('#streamurl').val().length > 0) {
sendAPI(JSON.stringify({"cmd":"MPD_API_ADD_TRACK","data":{"uri":$('#streamurl').val()}})); sendAPI({"cmd":"MPD_API_ADD_TRACK","data":{"uri":$('#streamurl').val()}});
} }
$('#streamurl').val(""); $('#streamurl').val("");
$('#addstream').modal('hide'); $('#addstream').modal('hide');
@ -1273,7 +1240,7 @@ function addStream() {
function saveQueue() { function saveQueue() {
if($('#playlistname').val().length > 0) { if($('#playlistname').val().length > 0) {
sendAPI(JSON.stringify({"cmd":"MPD_API_SAVE_QUEUE","data":{"plist":$('#playlistname').val()}})); sendAPI({"cmd":"MPD_API_SAVE_QUEUE","data":{"plist":$('#playlistname').val()}});
} }
$('#savequeue').modal('hide'); $('#savequeue').modal('hide');
} }
@ -1303,49 +1270,49 @@ function notificationsSupported() {
} }
function songChange(title, artist, album, uri, songId) { function songChange(title, artist, album, uri, songId) {
if (last_song == title+artist+album+uri+songId) if (last_song == obj.data.title+obj.data.artist+obj.data.album+obj.data.uri+obj.data.currentsongid)
return; return;
var textNotification = ''; var textNotification = '';
var htmlNotification = ''; var htmlNotification = '';
var pageTitle = 'myMPD: '; var pageTitle = 'myMPD: ';
if (typeof uri != 'undefined' && uri.length > 0) { if (typeof obj.data.uri != 'undefined' && obj.data.uri.length > 0) {
var coverImg=''; var coverImg='';
if (uri.indexOf('http://') == 0 || uri.indexOf('https://') == 0 ) { if (obj.data.uri.indexOf('http://') == 0 || obj.data.uri.indexOf('https://') == 0 ) {
coverImg='/assets/coverimage-httpstream.png'; coverImg='/assets/coverimage-httpstream.png';
} else if (coverImageFile != '') { } else if (coverImageFile != '') {
coverImg='/library/'+uri.replace(/\/[^\/]+$/,'\/'+coverImageFile); coverImg='/library/'+obj.data.uri.replace(/\/[^\/]+$/,'\/'+coverImageFile);
} else { } else {
coverImg='/assets/coverimage-notavailable.png'; coverImg='/assets/coverimage-notavailable.png';
} }
$('#album-cover').css('backgroundImage','url("'+coverImg+'"),url("/assets/coverimage-notavailable.png")'); $('#album-cover').css('backgroundImage','url("'+coverImg+'"),url("/assets/coverimage-notavailable.png")');
} }
if(typeof artist != 'undefined' && artist.length > 0 && artist != '-') { if(typeof obj.data.artist != 'undefined' && obj.data.artist.length > 0 && obj.data.artist != '-') {
textNotification += artist; textNotification += obj.data.artist;
htmlNotification += '<br/>' + artist; htmlNotification += '<br/>' + obj.data.artist;
pageTitle += artist + ' - '; pageTitle += obj.data.artist + ' - ';
$('#artist').text(artist); $('#artist').text(obj.data.artist);
} else { } else {
$('#artist').text(''); $('#artist').text('');
} }
if(typeof album != 'undefined' && album.length > 0 && album != '-') { if(typeof obj.data.album != 'undefined' && obj.data.album.length > 0 && obj.data.album != '-') {
textNotification += ' - ' + album; textNotification += ' - ' + obj.data.album;
htmlNotification += '<br/>' + album; htmlNotification += '<br/>' + obj.data.album;
$('#album').text(album); $('#album').text(obj.data.album);
} }
else { else {
$('#album').text(''); $('#album').text('');
} }
if(typeof title != 'undefined' && title.length > 0) { if(typeof obj.data.title != 'undefined' && obj.data.title.length > 0) {
pageTitle += title; pageTitle += obj.data.title;
$('#currenttrack').text(title); $('#currenttrack').text(obj.data.title);
} else { } else {
$('#currenttrack').text(''); $('#currenttrack').text('');
} }
document.title = pageTitle; document.title = pageTitle;
showNotification(title,textNotification,htmlNotification,'success'); showNotification(obj.data.title,textNotification,htmlNotification,'success');
last_song = title+artist+album+uri+songId; last_song = obj.data.title+obj.data.artist+obj.data.album+obj.data.uri+obj.data.currentsongid;
} }
@ -1355,10 +1322,10 @@ $(document).keydown(function(e){
} }
switch (e.which) { switch (e.which) {
case 37: //left case 37: //left
sendAPI(JSON.stringify({"cmd":"MPD_API_SET_PREV"})); sendAPI({"cmd":"MPD_API_SET_PREV"});
break; break;
case 39: //right case 39: //right
sendAPI(JSON.stringify({"cmd":"MPD_API_SET_NEXT"})); sendAPI({"cmd":"MPD_API_SET_NEXT"});
break; break;
case 32: //space case 32: //space
clickPlay(); clickPlay();
@ -1410,7 +1377,7 @@ function chVolume (increment) {
if (newValue<0) { newValue=0; } if (newValue<0) { newValue=0; }
else if (newValue > 100) { newValue=100; } else if (newValue > 100) { newValue=100; }
volumeBar.slider('setValue',newValue); volumeBar.slider('setValue',newValue);
sendAPI(JSON.stringify({"cmd":"MPD_API_SET_VOLUME", "data": {"volume":newValue}})); sendAPI({"cmd":"MPD_API_SET_VOLUME", "data": {"volume":newValue}});
} }
function beautifyDuration(x) { function beautifyDuration(x) {

View File

@ -59,6 +59,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
int je, int_buf; int je, int_buf;
float float_buf; float float_buf;
char *p_charbuf1, *p_charbuf2; char *p_charbuf1, *p_charbuf2;
FILE *fp;
#ifdef DEBUG #ifdef DEBUG
fprintf(stdout,"Got request: %s\n",msg.p); fprintf(stdout,"Got request: %s\n",msg.p);
@ -71,6 +72,63 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
return; return;
switch(cmd_id) { switch(cmd_id) {
case MPD_API_SET_MYMPD_SETTINGS:
fp = fopen(mpd.statefile,"w");
if(fp != NULL) {
fprintf(fp,"%.*s",msg.len,msg.p);
fclose(fp);
} else {
fprintf(stderr,"Cant write state file\n");
}
break;
case MPD_API_GET_MYMPD_SETTINGS:
fp = fopen(mpd.statefile,"r");
if(fp != NULL) {
fgets(mpd.buf, MAX_SIZE, fp);
fclose(fp);
n=strlen(mpd.buf);
}
break;
case MPD_API_SET_MPD_SETTINGS:
je = json_scanf(msg.p, msg.len, "{ data: { random:%u } }", &uint_buf1);
if (je == 1)
mpd_run_random(mpd.conn, uint_buf1);
je = json_scanf(msg.p, msg.len, "{ data: { repeat:%u } }", &uint_buf1);
if (je == 1)
mpd_run_repeat(mpd.conn, uint_buf1);
je = json_scanf(msg.p, msg.len, "{ data: { consume:%u } }", &uint_buf1);
if (je == 1)
mpd_run_consume(mpd.conn, uint_buf1);
je = json_scanf(msg.p, msg.len, "{ data: { single:%u } }", &uint_buf1);
if (je == 1)
mpd_run_single(mpd.conn, uint_buf1);
je = json_scanf(msg.p, msg.len, "{ data: { crossfade:%u } }", &uint_buf1);
if (je == 1)
mpd_run_crossfade(mpd.conn, uint_buf1);
je = json_scanf(msg.p, msg.len, "{ data: { mixrampdb:%f } }", &float_buf);
if (je == 1)
mpd_run_mixrampdb(mpd.conn, float_buf);
je = json_scanf(msg.p, msg.len, "{ data: { mixrampdelay:%f } }", &float_buf);
if (je == 1)
mpd_run_mixrampdelay(mpd.conn, float_buf);
je = json_scanf(msg.p, msg.len, "{ data: { replaygain:%Q } }", &p_charbuf1);
if (je == 1)
{
mpd_send_command(mpd.conn, "replay_gain_mode", p_charbuf1, NULL);
struct mpd_pair *pair;
while ((pair = mpd_recv_pair(mpd.conn)) != NULL) {
mpd_return_pair(mpd.conn, pair);
}
}
free(p_charbuf1);
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);
if (je == 2) if (je == 2)
@ -80,6 +138,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
break; break;
case MPD_API_WELCOME: case MPD_API_WELCOME:
n = mympd_put_welcome(mpd.buf); n = mympd_put_welcome(mpd.buf);
break;
case MPD_API_UPDATE_DB: case MPD_API_UPDATE_DB:
mpd_run_update(mpd.conn, NULL); mpd_run_update(mpd.conn, NULL);
break; break;
@ -125,41 +184,6 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
if (je == 1) if (je == 1)
mpd_run_play_id(mpd.conn, uint_buf1); mpd_run_play_id(mpd.conn, uint_buf1);
break; break;
case MPD_API_TOGGLE_RANDOM:
je = json_scanf(msg.p, msg.len, "{ data: { state:%u } }", &uint_buf1);
if (je == 1)
mpd_run_random(mpd.conn, uint_buf1);
break;
case MPD_API_TOGGLE_REPEAT:
je = json_scanf(msg.p, msg.len, "{ data: { state:%u } }", &uint_buf1);
if (je == 1)
mpd_run_repeat(mpd.conn, uint_buf1);
break;
case MPD_API_TOGGLE_CONSUME:
je = json_scanf(msg.p, msg.len, "{ data: { state:%u } }", &uint_buf1);
if (je == 1)
mpd_run_consume(mpd.conn, uint_buf1);
break;
case MPD_API_TOGGLE_SINGLE:
je = json_scanf(msg.p, msg.len, "{ data: { state:%u } }", &uint_buf1);
if (je == 1)
mpd_run_single(mpd.conn, uint_buf1);
break;
case MPD_API_SET_CROSSFADE:
je = json_scanf(msg.p, msg.len, "{ data: { state:%u } }", &uint_buf1);
if (je == 1)
mpd_run_crossfade(mpd.conn, uint_buf1);
break;
case MPD_API_SET_MIXRAMPDB:
je = json_scanf(msg.p, msg.len, "{ data: { state:%f } }", &float_buf);
if (je == 1)
mpd_run_mixrampdb(mpd.conn, float_buf);
break;
case MPD_API_SET_MIXRAMPDELAY:
je = json_scanf(msg.p, msg.len, "{ data: { state:%f } }", &float_buf);
if (je == 1)
mpd_run_mixrampdelay(mpd.conn, float_buf);
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;
@ -281,24 +305,12 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
mpd_run_rm(mpd.conn, p_charbuf1); mpd_run_rm(mpd.conn, p_charbuf1);
free(p_charbuf1); free(p_charbuf1);
break; break;
case MPD_API_GET_SETTINGS: case MPD_API_GET_MPD_SETTINGS:
n = mympd_put_settings(mpd.buf); n = mympd_put_settings(mpd.buf);
break; break;
case MPD_API_GET_STATS: case MPD_API_GET_STATS:
n = mympd_get_stats(mpd.buf); n = mympd_get_stats(mpd.buf);
break; break;
case MPD_API_SET_REPLAYGAIN:
je = json_scanf(msg.p, msg.len, "{ data: { mode:%Q } }", &p_charbuf1);
if (je == 1)
{
mpd_send_command(mpd.conn, "replay_gain_mode", p_charbuf1, NULL);
struct mpd_pair *pair;
while ((pair = mpd_recv_pair(mpd.conn)) != NULL) {
mpd_return_pair(mpd.conn, pair);
}
}
free(p_charbuf1);
break;
} }
free(cmd); free(cmd);

View File

@ -69,24 +69,19 @@
X(MPD_API_UPDATE_DB) \ X(MPD_API_UPDATE_DB) \
X(MPD_API_GET_OUTPUTNAMES) \ X(MPD_API_GET_OUTPUTNAMES) \
X(MPD_API_TOGGLE_OUTPUT) \ X(MPD_API_TOGGLE_OUTPUT) \
X(MPD_API_TOGGLE_RANDOM) \
X(MPD_API_TOGGLE_CONSUME) \
X(MPD_API_TOGGLE_SINGLE) \
X(MPD_API_SET_CROSSFADE) \
X(MPD_API_TOGGLE_REPEAT) \
X(MPD_API_GET_SETTINGS) \
X(MPD_API_SEND_SHUFFLE) \ X(MPD_API_SEND_SHUFFLE) \
X(MPD_API_GET_STATS) \ X(MPD_API_GET_STATS) \
X(MPD_API_SET_MIXRAMPDB) \
X(MPD_API_SET_MIXRAMPDELAY) \
X(MPD_API_GET_PLAYLISTS) \ X(MPD_API_GET_PLAYLISTS) \
X(MPD_API_RM_PLAYLIST) \ X(MPD_API_RM_PLAYLIST) \
X(MPD_API_SET_REPLAYGAIN) \
X(MPD_API_GET_ARTISTALBUMS) \ X(MPD_API_GET_ARTISTALBUMS) \
X(MPD_API_GET_ARTISTALBUMTITLES) \ X(MPD_API_GET_ARTISTALBUMTITLES) \
X(MPD_API_GET_ARTISTS) \ X(MPD_API_GET_ARTISTS) \
X(MPD_API_GET_CURRENT_SONG) \ X(MPD_API_GET_CURRENT_SONG) \
X(MPD_API_WELCOME) X(MPD_API_WELCOME) \
X(MPD_API_GET_MPD_SETTINGS) \
X(MPD_API_SET_MPD_SETTINGS) \
X(MPD_API_SET_MYMPD_SETTINGS) \
X(MPD_API_GET_MYMPD_SETTINGS)
enum mpd_cmd_ids { enum mpd_cmd_ids {
MPD_CMDS(GEN_ENUM) MPD_CMDS(GEN_ENUM)
@ -102,10 +97,9 @@ enum mpd_conn_states {
struct t_mpd { struct t_mpd {
int port; int port;
int local_port;
char host[128]; char host[128];
char *password; char *password;
char *gpass; char *statefile;
struct mpd_connection *conn; struct mpd_connection *conn;
enum mpd_conn_states conn_state; enum mpd_conn_states conn_state;

View File

@ -105,11 +105,10 @@ int main(int argc, char **argv)
char *run_as_user = NULL; char *run_as_user = NULL;
char *webport = "8080"; char *webport = "8080";
mpd.port = 6600; mpd.port = 6600;
mpd.local_port = 0;
mpd.gpass = NULL;
strcpy(mpd.host, "127.0.0.1"); strcpy(mpd.host, "127.0.0.1");
streamport = 8000; streamport = 8000;
strcpy(coverimage, "folder.jpg"); strcpy(coverimage, "folder.jpg");
mpd.statefile="/var/lib/mympd/mympd.state";
static struct option long_options[] = { static struct option long_options[] = {
{"host", required_argument, 0, 'h'}, {"host", required_argument, 0, 'h'},
@ -121,14 +120,15 @@ int main(int argc, char **argv)
{"mpdpass", required_argument, 0, 'm'}, {"mpdpass", required_argument, 0, 'm'},
{"streamport", required_argument, 0, 's'}, {"streamport", required_argument, 0, 's'},
{"coverimage", required_argument, 0, 'i'}, {"coverimage", required_argument, 0, 'i'},
{"statefile", required_argument, 0, 't'},
{0, 0, 0, 0 } {0, 0, 0, 0 }
}; };
while((n = getopt_long(argc, argv, "D:h:p:w:u:vm:s:i:c:", while((n = getopt_long(argc, argv, "D:h:p:w:u:vm:s:i:c:t:",
long_options, &option_index)) != -1) { long_options, &option_index)) != -1) {
switch (n) { switch (n) {
case 'D': case 't':
mpd.gpass = strdup(optarg); mpd.statefile = strdup(optarg);
break; break;
case 'h': case 'h':
strncpy(mpd.host, optarg, sizeof(mpd.host)); strncpy(mpd.host, optarg, sizeof(mpd.host));
@ -169,6 +169,7 @@ int main(int argc, char **argv)
" -m, --mpdpass <password>\tspecifies the password to use when connecting to mpd\n" " -m, --mpdpass <password>\tspecifies the password to use when connecting to mpd\n"
" -s, --streamport <port>\tconnect to mpd http stream at port [8000]\n" " -s, --streamport <port>\tconnect to mpd http stream at port [8000]\n"
" -i, --coverimage <filename>\tfilename for coverimage [folder.jpg]\n" " -i, --coverimage <filename>\tfilename for coverimage [folder.jpg]\n"
" -t, --statefile <filename>\tstatefile [/var/lib/mympd/mympd.state]\n"
" --help\t\t\t\tthis help\n" " --help\t\t\t\tthis help\n"
, argv[0]); , argv[0]);
return EXIT_FAILURE; return EXIT_FAILURE;