mirror of
https://github.com/SuperBFG7/ympd
synced 2024-12-25 18:40:26 +00:00
Feat: option to select jukebox playlist #37
This commit is contained in:
parent
881567a2bd
commit
fb9668b39d
@ -650,6 +650,7 @@
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<form class="needs-validation" id="settingsFrm" novalidate>
|
||||
<h4>MPD</h4>
|
||||
<div class="row">
|
||||
<div class="form-group col-md-6">
|
||||
<button data-href='{"cmd":"toggleBtn", "options":["btnRandom"]}' id="btnRandom" type="button" class="btn btn-secondary btn-block" title="Random">
|
||||
@ -709,14 +710,21 @@
|
||||
<div class="invalid-feedback">Must be a number.</div>
|
||||
</div>
|
||||
</div>
|
||||
<hr/>
|
||||
<h4>Jukebox</h4>
|
||||
<div class="row">
|
||||
<div class="form-group col-md-6" data-toggle="buttons">
|
||||
<button data-href='{"cmd": "toggleBtn", "options": ["btnJukebox"]}' id="btnJukebox" type="button" class="btn btn-secondary btn-block" title="Jukebox">
|
||||
Jukebox
|
||||
</button>
|
||||
</div>
|
||||
<div class="form-group input-group col-md-6 border-secondary">
|
||||
<select id="jukeboxPlaylist" class="form-control custom-select border-secondary">
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<hr/>
|
||||
<h4>Notifications</h4>
|
||||
<div class="row">
|
||||
<div class="form-group col-md-6" data-toggle="buttons">
|
||||
<button data-href='{"cmd": "toggleBtn", "options": ["btnnotifyPage"]}' type="button" class="btn btn-secondary btn-block" id="btnnotifyPage">
|
||||
|
@ -31,8 +31,9 @@ var playstate = '';
|
||||
var settings = {};
|
||||
var alertTimeout;
|
||||
var progressTimer;
|
||||
let deferredPrompt;
|
||||
var deferredPrompt;
|
||||
var dragEl;
|
||||
var playlistEl;
|
||||
|
||||
var app = {};
|
||||
app.apps = { "Playback": { "state": "0/-/", "scrollPos": 0 },
|
||||
@ -850,6 +851,10 @@ function parseSettings(obj) {
|
||||
}
|
||||
|
||||
settings = obj.data;
|
||||
|
||||
playlistEl = 'jukeboxPlaylist';
|
||||
sendAPI({"cmd": "MPD_API_PLAYLIST_LIST", "data": {"offset": 0, "filter": "-"}}, getAllPlaylists);
|
||||
|
||||
settings.mpdstream = 'http://';
|
||||
if (settings.mpdhost == '127.0.0.1' || settings.mpdhost == 'localhost')
|
||||
settings.mpdstream += window.location.hostname;
|
||||
@ -1504,14 +1509,26 @@ function playlistClear() {
|
||||
|
||||
function getAllPlaylists(obj) {
|
||||
var nrItems = obj.data.length;
|
||||
var playlists = '<option></option><option>New Playlist</option>';
|
||||
for (var i = 0; i < nrItems; i++) {
|
||||
playlists += '<option>' + obj.data[i].uri + '</option>';
|
||||
var playlists = '';
|
||||
if (obj.offset == 0) {
|
||||
if (playlistEl == 'addToPlaylistPlaylist')
|
||||
playlists = '<option></option><option>New Playlist</option>';
|
||||
else if (playlistEl == 'jukeboxPlaylist')
|
||||
playlists = '<option>Database</option>';
|
||||
}
|
||||
document.getElementById('addToPlaylistPlaylist').innerHTML += playlists;
|
||||
for (var i = 0; i < nrItems; i++) {
|
||||
playlists += '<option';
|
||||
if (playlistEl == 'jukeboxPlaylist' && obj.data[i].uri == settings.jukeboxPlaylist)
|
||||
playlists += ' selected';
|
||||
playlists += '>' + obj.data[i].uri + '</option>';
|
||||
}
|
||||
if (obj.offset == 0)
|
||||
document.getElementById(playlistEl).innerHTML = playlists;
|
||||
else
|
||||
document.getElementById(playlistEl).innerHTML += playlists;
|
||||
if (obj.totalEntities > obj.returnedEntities) {
|
||||
obj.offset += settings.maxElementsPerPage;
|
||||
sendAPI({"cmd": "MPD_API_PLAYLIST_LIST","data": {"offset": obj.offset, "filter": "-"}}, getAllPlaylists);
|
||||
sendAPI({"cmd": "MPD_API_PLAYLIST_LIST", "data": {"offset": obj.offset, "filter": "-"}}, getAllPlaylists);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1524,7 +1541,7 @@ function voteSong(vote) {
|
||||
vote = 1;
|
||||
else if (vote == 0 && domCache.btnVoteDown.classList.contains('active-fg-red'))
|
||||
vote = 1;
|
||||
sendAPI({"cmd": "MPD_API_LIKE","data": {"uri": uri, "like": vote}});
|
||||
sendAPI({"cmd": "MPD_API_LIKE", "data": {"uri": uri, "like": vote}});
|
||||
setVoteSongBtns(vote, uri);
|
||||
}
|
||||
|
||||
@ -1591,6 +1608,7 @@ function showAddToPlaylist(uri) {
|
||||
document.getElementById('addToPlaylistLabel').innerText = 'Add Stream';
|
||||
}
|
||||
modalAddToPlaylist.show();
|
||||
playlistEl = 'addToPlaylistPlaylist';
|
||||
sendAPI({"cmd": "MPD_API_PLAYLIST_LIST","data": {"offset": 0, "filter": "-"}}, getAllPlaylists);
|
||||
}
|
||||
|
||||
@ -1926,6 +1944,7 @@ function confirmSettings() {
|
||||
|
||||
if (formOK == true) {
|
||||
var selectReplaygain = document.getElementById('selectReplaygain');
|
||||
var selectJukeboxPlaylist = document.getElementById('jukeboxPlaylist');
|
||||
sendAPI({"cmd": "MPD_API_SETTINGS_SET", "data": {
|
||||
"consume": (document.getElementById('btnConsume').classList.contains('active') ? 1 : 0),
|
||||
"random": (document.getElementById('btnRandom').classList.contains('active') ? 1 : 0),
|
||||
@ -1937,7 +1956,8 @@ function confirmSettings() {
|
||||
"mixrampdelay": (settings.mixramp == true ? document.getElementById('inputMixrampdelay').value : settings.mixrampdelay),
|
||||
"notificationWeb": (document.getElementById('btnnotifyWeb').classList.contains('active') ? true : false),
|
||||
"notificationPage": (document.getElementById('btnnotifyPage').classList.contains('active') ? true : false),
|
||||
"jukeboxMode": (document.getElementById('btnJukebox').classList.contains('active') ? true : false)
|
||||
"jukeboxMode": (document.getElementById('btnJukebox').classList.contains('active') ? true : false),
|
||||
"jukeboxPlaylist": selectJukeboxPlaylist.options[selectJukeboxPlaylist.selectedIndex].value
|
||||
}}, getSettings);
|
||||
modalSettings.hide();
|
||||
} else
|
||||
|
@ -112,18 +112,22 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg) {
|
||||
n = mympd_put_state(mpd.buf, &mpd.song_id, &mpd.next_song_id, &mpd.last_song_id, &mpd.queue_version, &mpd.queue_length);
|
||||
break;
|
||||
case MPD_API_SETTINGS_SET:
|
||||
je = json_scanf(msg.p, msg.len, "{data: {notificationWeb: %B, notificationPage: %B, jukeboxMode: %B}}",
|
||||
je = json_scanf(msg.p, msg.len, "{data: {notificationWeb: %B, notificationPage: %B, jukeboxMode: %B, jukeboxPlaylist: %Q}}",
|
||||
&mympd_state.notificationWeb,
|
||||
&mympd_state.notificationPage,
|
||||
&mympd_state.jukeboxMode);
|
||||
if (je == 3) {
|
||||
&mympd_state.jukeboxMode,
|
||||
&mympd_state.jukeboxPlaylist);
|
||||
if (je == 4) {
|
||||
char tmp_file[200];
|
||||
snprintf(tmp_file, 200, "%s.tmp", config.statefile);
|
||||
json_fprintf(tmp_file, "{notificationWeb: %B, notificationPage: %B, jukeboxMode: %B}",
|
||||
json_fprintf(tmp_file, "{notificationWeb: %B, notificationPage: %B, jukeboxMode: %B, jukeboxPlaylist: %Q}",
|
||||
mympd_state.notificationWeb,
|
||||
mympd_state.notificationPage,
|
||||
mympd_state.jukeboxMode);
|
||||
mympd_state.jukeboxMode,
|
||||
mympd_state.jukeboxPlaylist);
|
||||
rename(tmp_file, config.statefile);
|
||||
if (mympd_state.jukeboxMode == true)
|
||||
mympd_jukebox();
|
||||
}
|
||||
|
||||
je = json_scanf(msg.p, msg.len, "{data: {random: %u}}", &uint_buf1);
|
||||
@ -697,7 +701,7 @@ void mympd_idle(struct mg_mgr *s, int timeout) {
|
||||
switch (mpd.conn_state) {
|
||||
case MPD_DISCONNECTED:
|
||||
/* Try to connect */
|
||||
printf("MPD Connecting to %s: %ld\n", config.mpdhost, config.mpdport);
|
||||
printf("MPD Connecting to %s:%ld\n", config.mpdhost, config.mpdport);
|
||||
mpd.conn = mpd_connection_new(config.mpdhost, config.mpdport, mpd.timeout);
|
||||
if (mpd.conn == NULL) {
|
||||
printf("MPD connection failed.");
|
||||
@ -934,31 +938,62 @@ void mympd_jukebox() {
|
||||
if (queue_length > 0)
|
||||
return;
|
||||
|
||||
struct mpd_stats *stats = mpd_run_stats(mpd.conn);
|
||||
num_songs = mpd_stats_get_number_of_songs(stats);
|
||||
mpd_stats_free(stats);
|
||||
num_songs--;
|
||||
if (num_songs > 0) {
|
||||
srand((unsigned int)time(NULL));
|
||||
rand_song = rand() % num_songs;
|
||||
mpd_send_list_all(mpd.conn, "/");
|
||||
i = 0;
|
||||
while ((entity = mpd_recv_entity(mpd.conn)) != NULL) {
|
||||
if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
|
||||
if (i == rand_song)
|
||||
srand((unsigned int)time(NULL));
|
||||
|
||||
if (strcmp(mympd_state.jukeboxPlaylist, "Database") == 0) {
|
||||
struct mpd_stats *stats = mpd_run_stats(mpd.conn);
|
||||
num_songs = mpd_stats_get_number_of_songs(stats);
|
||||
mpd_stats_free(stats);
|
||||
num_songs--;
|
||||
if (num_songs > 0) {
|
||||
rand_song = rand() % num_songs;
|
||||
mpd_send_list_all(mpd.conn, "/");
|
||||
i = 0;
|
||||
while ((entity = mpd_recv_entity(mpd.conn)) != NULL) {
|
||||
if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
|
||||
if (i == rand_song)
|
||||
break;
|
||||
i++;
|
||||
i++;
|
||||
}
|
||||
mpd_entity_free(entity);
|
||||
}
|
||||
mpd_response_finish(mpd.conn);
|
||||
song = mpd_entity_get_song(entity);
|
||||
if (song != NULL) {
|
||||
printf("Jukebox enabled, playing random song from database: %d/%d\n", rand_song, num_songs);
|
||||
mpd_run_add(mpd.conn, mpd_song_get_uri(song));
|
||||
mpd_run_play(mpd.conn);
|
||||
}
|
||||
mpd_entity_free(entity);
|
||||
}
|
||||
mpd_response_finish(mpd.conn);
|
||||
song = mpd_entity_get_song(entity);
|
||||
if (song != NULL) {
|
||||
printf("Jukebox enabled, playing random song %d/%d\n", rand_song, num_songs);
|
||||
mpd_run_add(mpd.conn, mpd_song_get_uri(song));
|
||||
mpd_run_play(mpd.conn);
|
||||
}
|
||||
else {
|
||||
mpd_send_list_playlist(mpd.conn, mympd_state.jukeboxPlaylist);
|
||||
num_songs = 0;
|
||||
while ((entity = mpd_recv_entity(mpd.conn)) != NULL) {
|
||||
num_songs++;
|
||||
mpd_entity_free(entity);
|
||||
}
|
||||
num_songs--;
|
||||
if (num_songs > 0) {
|
||||
rand_song = rand() % num_songs;
|
||||
mpd_send_list_playlist(mpd.conn, mympd_state.jukeboxPlaylist);
|
||||
i = 0;
|
||||
while ((entity = mpd_recv_entity(mpd.conn)) != NULL) {
|
||||
if (i == rand_song)
|
||||
break;
|
||||
i++;
|
||||
mpd_entity_free(entity);
|
||||
}
|
||||
mpd_response_finish(mpd.conn);
|
||||
song = mpd_entity_get_song(entity);
|
||||
if (song != NULL) {
|
||||
printf("Jukebox enabled, playing random song from %s: %d/%d\n", mympd_state.jukeboxPlaylist, rand_song, num_songs);
|
||||
mpd_run_add(mpd.conn, mpd_song_get_uri(song));
|
||||
mpd_run_play(mpd.conn);
|
||||
}
|
||||
mpd_entity_free(entity);
|
||||
}
|
||||
mpd_entity_free(entity);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1051,7 +1086,7 @@ int mympd_put_settings(char *buffer) {
|
||||
"repeat: %d, single: %d, crossfade: %d, consume: %d, random: %d, "
|
||||
"mixrampdb: %f, mixrampdelay: %f, mpdhost: %Q, mpdport: %d, passwort_set: %B, "
|
||||
"streamport: %d, coverimage: %Q, stickers: %B, mixramp: %B, maxElementsPerPage: %d, "
|
||||
"replaygain: %Q, notificationWeb: %B, notificationPage: %B, jukeboxMode: %B, "
|
||||
"replaygain: %Q, notificationWeb: %B, notificationPage: %B, jukeboxMode: %B, jukeboxPlaylist: %Q, "
|
||||
"tags: { Artist: %B, Album: %B, AlbumArtist: %B, Title: %B, Track: %B, Genre: %B, Date: %B,"
|
||||
"Composer: %B, Performer: %B }"
|
||||
"}}",
|
||||
@ -1074,6 +1109,7 @@ int mympd_put_settings(char *buffer) {
|
||||
mympd_state.notificationWeb,
|
||||
mympd_state.notificationPage,
|
||||
mympd_state.jukeboxMode,
|
||||
mympd_state.jukeboxPlaylist,
|
||||
mpd.tag_artist,
|
||||
mpd.tag_album,
|
||||
mpd.tag_album_artist,
|
||||
|
@ -175,6 +175,7 @@ typedef struct {
|
||||
bool notificationWeb;
|
||||
bool notificationPage;
|
||||
bool jukeboxMode;
|
||||
const char* jukeboxPlaylist;
|
||||
} t_mympd_state;
|
||||
|
||||
t_mympd_state mympd_state;
|
||||
|
@ -236,19 +236,22 @@ int main(int argc, char **argv) {
|
||||
|
||||
if (access( config.statefile, F_OK ) != -1 ) {
|
||||
char *content = json_fread(config.statefile);
|
||||
int je = json_scanf(content, strlen(content), "{notificationWeb: %B, notificationPage: %B, jukeboxMode: %B}",
|
||||
int je = json_scanf(content, strlen(content), "{notificationWeb: %B, notificationPage: %B, jukeboxMode: %B, jukeboxPlaylist: %Q}",
|
||||
&mympd_state.notificationWeb,
|
||||
&mympd_state.notificationPage,
|
||||
&mympd_state.jukeboxMode);
|
||||
if (je != 3) {
|
||||
&mympd_state.jukeboxMode,
|
||||
&mympd_state.jukeboxPlaylist);
|
||||
if (je != 4) {
|
||||
mympd_state.notificationWeb = false;
|
||||
mympd_state.notificationPage = true;
|
||||
mympd_state.jukeboxMode = false;
|
||||
mympd_state.jukeboxPlaylist = "Database";
|
||||
}
|
||||
} else {
|
||||
mympd_state.notificationWeb = false;
|
||||
mympd_state.notificationPage = true;
|
||||
mympd_state.jukeboxMode = false;
|
||||
mympd_state.jukeboxPlaylist = "Database";
|
||||
}
|
||||
|
||||
signal(SIGTERM, signal_handler);
|
||||
|
Loading…
Reference in New Issue
Block a user