2018-05-24 17:50:05 +00:00
|
|
|
/* myMPD
|
|
|
|
(c) 2018 Juergen Mang <mail@jcgames.de>
|
2018-06-21 21:29:49 +00:00
|
|
|
This project's homepage is: https://github.com/jcorporation/mympd
|
2018-05-24 17:50:05 +00:00
|
|
|
|
|
|
|
myMPD ist fork of:
|
|
|
|
|
|
|
|
ympd
|
2014-02-22 00:57:26 +00:00
|
|
|
(c) 2013-2014 Andrew Karpow <andy@ndyk.de>
|
2014-01-17 18:41:41 +00:00
|
|
|
This project's homepage is: http://www.ympd.org
|
2014-02-22 00:57:26 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; version 2 of the License.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2014-01-17 18:41:41 +00:00
|
|
|
*/
|
|
|
|
|
2014-01-08 01:23:02 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <libgen.h>
|
2018-06-03 08:52:38 +00:00
|
|
|
#include <ctype.h>
|
2018-06-22 00:01:15 +00:00
|
|
|
#include <libgen.h>
|
2018-08-09 21:15:00 +00:00
|
|
|
#include <poll.h>
|
2013-11-04 17:18:38 +00:00
|
|
|
#include <mpd/client.h>
|
|
|
|
|
|
|
|
#include "mpd_client.h"
|
2014-02-04 16:58:10 +00:00
|
|
|
#include "config.h"
|
2018-07-30 22:05:37 +00:00
|
|
|
#include "../dist/src/frozen/frozen.h"
|
2013-11-04 17:18:38 +00:00
|
|
|
|
2014-02-16 18:46:53 +00:00
|
|
|
const char * mpd_cmd_strs[] = {
|
|
|
|
MPD_CMDS(GEN_STR)
|
|
|
|
};
|
2013-11-04 17:18:38 +00:00
|
|
|
|
2018-08-06 21:29:20 +00:00
|
|
|
static inline enum mpd_cmd_ids get_cmd_id(const char *cmd) {
|
2018-10-18 21:55:45 +00:00
|
|
|
for (unsigned i = 0; i < sizeof(mpd_cmd_strs) / sizeof(mpd_cmd_strs[0]); i++)
|
2018-07-24 22:58:29 +00:00
|
|
|
if (!strncmp(cmd, mpd_cmd_strs[i], strlen(mpd_cmd_strs[i])))
|
2014-02-16 18:46:53 +00:00
|
|
|
return i;
|
2013-11-09 01:07:03 +00:00
|
|
|
|
2014-02-16 18:46:53 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2013-11-09 01:07:03 +00:00
|
|
|
|
2018-08-22 16:29:47 +00:00
|
|
|
enum mpd_idle idle_bitmask_save = 0;
|
|
|
|
|
2018-08-06 21:29:20 +00:00
|
|
|
void callback_mympd(struct mg_connection *nc, const struct mg_str msg) {
|
2018-06-14 22:57:57 +00:00
|
|
|
size_t n = 0;
|
|
|
|
char *cmd;
|
2018-07-18 22:38:46 +00:00
|
|
|
unsigned int uint_buf1, uint_buf2, uint_rc;
|
2018-10-08 22:46:08 +00:00
|
|
|
int je, int_buf1, int_rc;
|
2018-06-14 22:57:57 +00:00
|
|
|
float float_buf;
|
2018-09-30 22:18:55 +00:00
|
|
|
char *p_charbuf1, *p_charbuf2, *p_charbuf3, *p_charbuf4;
|
2018-10-01 19:13:38 +00:00
|
|
|
char p_char[4];
|
2018-07-02 19:40:56 +00:00
|
|
|
enum mpd_cmd_ids cmd_id;
|
2018-08-22 16:29:47 +00:00
|
|
|
struct pollfd fds[1];
|
|
|
|
int pollrc;
|
2018-06-21 16:57:40 +00:00
|
|
|
|
2018-06-14 22:57:57 +00:00
|
|
|
#ifdef DEBUG
|
2018-08-23 09:27:12 +00:00
|
|
|
fprintf(stderr, "Got request: %s\n", msg.p);
|
2018-06-14 22:57:57 +00:00
|
|
|
#endif
|
|
|
|
|
2018-08-22 00:13:30 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{cmd: %Q}", &cmd);
|
2018-07-02 19:40:56 +00:00
|
|
|
if (je == 1)
|
2018-07-19 17:58:23 +00:00
|
|
|
cmd_id = get_cmd_id(cmd);
|
2018-07-02 19:40:56 +00:00
|
|
|
else
|
2018-07-19 17:58:23 +00:00
|
|
|
cmd_id = get_cmd_id("MPD_API_UNKNOWN");
|
2018-06-14 22:57:57 +00:00
|
|
|
|
2018-07-24 22:58:29 +00:00
|
|
|
if (cmd_id == -1)
|
2018-07-19 17:58:23 +00:00
|
|
|
cmd_id = get_cmd_id("MPD_API_UNKNOWN");
|
2018-08-09 21:15:00 +00:00
|
|
|
|
2018-08-15 15:23:47 +00:00
|
|
|
mpd_send_noidle(mpd.conn);
|
2018-08-22 16:29:47 +00:00
|
|
|
//save idle events (processing later)
|
|
|
|
fds[0].fd = mpd_connection_get_fd(mpd.conn);
|
|
|
|
fds[0].events = POLLIN;
|
|
|
|
pollrc = poll(fds, 1, 100);
|
|
|
|
if (pollrc > 0) {
|
|
|
|
idle_bitmask_save = mpd_recv_idle(mpd.conn, false);
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (idle_bitmask_save > 0)
|
|
|
|
fprintf(stderr, "IDLE EVENT BEFORE REQUEST: %d\n", idle_bitmask_save);
|
|
|
|
#endif
|
|
|
|
}
|
2018-08-09 21:15:00 +00:00
|
|
|
mpd_response_finish(mpd.conn);
|
2018-08-22 16:29:47 +00:00
|
|
|
//handle request
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "HANDLE REQUEST: %s\n", cmd);
|
|
|
|
#endif
|
2018-06-14 22:57:57 +00:00
|
|
|
switch(cmd_id) {
|
2018-07-19 17:58:23 +00:00
|
|
|
case MPD_API_UNKNOWN:
|
2018-07-22 19:00:26 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"error\", \"data\": \"Unknown request\"}");
|
2018-07-19 17:58:23 +00:00
|
|
|
break;
|
2018-08-15 15:23:47 +00:00
|
|
|
case MPD_API_LIKE:
|
2018-08-21 19:28:13 +00:00
|
|
|
if (config.stickers) {
|
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {uri: %Q, like: %d}}", &p_charbuf1, &uint_buf1);
|
|
|
|
if (je == 2) {
|
|
|
|
mympd_like_song_uri(p_charbuf1, uint_buf1);
|
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
|
|
|
free(p_charbuf1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"error\", \"data\": \"MPD stickers are disabled\"}");
|
2018-08-15 15:23:47 +00:00
|
|
|
}
|
|
|
|
break;
|
2018-10-22 20:03:45 +00:00
|
|
|
case MPD_API_COLS_SAVE:
|
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {table: %Q}}", &p_charbuf1);
|
|
|
|
if (je == 1) {
|
|
|
|
char column_list[800];
|
|
|
|
snprintf(column_list, 800, "%.*s", msg.len, msg.p);
|
|
|
|
char *cols = strchr(column_list, '[');
|
|
|
|
int len = strlen(cols);
|
|
|
|
if (len > 1)
|
|
|
|
cols[len - 2] = '\0';
|
2018-11-14 23:14:47 +00:00
|
|
|
if (strcmp(p_charbuf1,"colsQueueCurrent")==0) {
|
|
|
|
free(mympd_state.colsQueueCurrent);
|
|
|
|
mympd_state.colsQueueCurrent = strdup(cols);
|
2018-10-22 20:03:45 +00:00
|
|
|
}
|
|
|
|
else if (strcmp(p_charbuf1,"colsSearch")==0) {
|
|
|
|
free(mympd_state.colsSearch);
|
|
|
|
mympd_state.colsSearch = strdup(cols);
|
|
|
|
}
|
|
|
|
else if (strcmp(p_charbuf1,"colsBrowseDatabase")==0) {
|
|
|
|
free(mympd_state.colsBrowseDatabase);
|
|
|
|
mympd_state.colsBrowseDatabase = strdup(cols);
|
|
|
|
}
|
2018-10-22 22:20:57 +00:00
|
|
|
else if (strcmp(p_charbuf1,"colsBrowsePlaylistsDetail")==0) {
|
|
|
|
free(mympd_state.colsBrowsePlaylistsDetail);
|
|
|
|
mympd_state.colsBrowsePlaylistsDetail = strdup(cols);
|
2018-10-22 20:03:45 +00:00
|
|
|
}
|
|
|
|
else if (strcmp(p_charbuf1,"colsBrowseFilesystem")==0) {
|
|
|
|
free(mympd_state.colsBrowseFilesystem);
|
|
|
|
mympd_state.colsBrowseFilesystem = strdup(cols);
|
|
|
|
}
|
2018-11-05 19:57:06 +00:00
|
|
|
else if (strcmp(p_charbuf1,"colsPlayback")==0) {
|
|
|
|
free(mympd_state.colsPlayback);
|
|
|
|
mympd_state.colsPlayback = strdup(cols);
|
|
|
|
}
|
2018-11-14 23:14:47 +00:00
|
|
|
else if (strcmp(p_charbuf1,"colsQueueLastPlayed")==0) {
|
|
|
|
free(mympd_state.colsQueueLastPlayed);
|
|
|
|
mympd_state.colsQueueLastPlayed = strdup(cols);
|
2018-11-12 23:48:29 +00:00
|
|
|
}
|
2018-10-22 20:03:45 +00:00
|
|
|
mympd_state_set(p_charbuf1, cols);
|
|
|
|
free(p_charbuf1);
|
|
|
|
}
|
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
|
|
|
break;
|
2018-10-15 22:34:25 +00:00
|
|
|
case MPD_API_SYSCMD:
|
2018-10-29 18:17:04 +00:00
|
|
|
if (config.syscmds == true) {
|
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {cmd: %Q}}", &p_charbuf1);
|
|
|
|
if (je == 1) {
|
|
|
|
int_buf1 = list_get_value(&syscmds, p_charbuf1);
|
|
|
|
if (int_buf1 > -1)
|
|
|
|
n = mympd_syscmd(mpd.buf, p_charbuf1, int_buf1);
|
|
|
|
free(p_charbuf1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"error\", \"data\": \"System commands are disabled.\"}");
|
2018-10-15 22:34:25 +00:00
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_PLAYER_STATE:
|
2018-08-23 15:24:37 +00:00
|
|
|
n = mympd_put_state(mpd.buf, &mpd.song_id, &mpd.next_song_id, &mpd.last_song_id, &mpd.queue_version, &mpd.queue_length);
|
2018-08-09 22:52:58 +00:00
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_SETTINGS_SET:
|
2018-10-01 19:13:38 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {notificationWeb: %B}}", &mympd_state.notificationWeb);
|
|
|
|
if (je == 1)
|
|
|
|
mympd_state_set("notificationWeb", (mympd_state.notificationWeb == true ? "true" : "false"));
|
|
|
|
|
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {notificationPage: %B}}", &mympd_state.notificationPage);
|
|
|
|
if (je == 1)
|
|
|
|
mympd_state_set("notificationPage", (mympd_state.notificationPage == true ? "true" : "false"));
|
2018-06-21 16:57:40 +00:00
|
|
|
|
2018-10-01 19:13:38 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {jukeboxMode: %d}}", &mympd_state.jukeboxMode);
|
|
|
|
if (je == 1) {
|
|
|
|
snprintf(p_char, 4, "%d", mympd_state.jukeboxMode);
|
|
|
|
mympd_state_set("jukeboxMode", p_char);
|
|
|
|
}
|
|
|
|
|
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {jukeboxPlaylist: %Q}}", &mympd_state.jukeboxPlaylist);
|
|
|
|
if (je == 1)
|
|
|
|
mympd_state_set("jukeboxPlaylist", mympd_state.jukeboxPlaylist);
|
|
|
|
|
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {jukeboxQueueLength: %d}}", &mympd_state.jukeboxQueueLength);
|
|
|
|
if (je == 1) {
|
|
|
|
snprintf(p_char, 4, "%d", mympd_state.jukeboxQueueLength);
|
|
|
|
mympd_state_set("jukeboxQueueLength", p_char);
|
|
|
|
}
|
|
|
|
|
2018-08-17 09:52:22 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {random: %u}}", &uint_buf1);
|
2018-06-19 22:43:36 +00:00
|
|
|
if (je == 1)
|
|
|
|
mpd_run_random(mpd.conn, uint_buf1);
|
|
|
|
|
2018-08-17 09:52:22 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {repeat: %u}}", &uint_buf1);
|
2018-06-19 22:43:36 +00:00
|
|
|
if (je == 1)
|
|
|
|
mpd_run_repeat(mpd.conn, uint_buf1);
|
|
|
|
|
2018-08-17 09:52:22 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {consume: %u}}", &uint_buf1);
|
2018-06-19 22:43:36 +00:00
|
|
|
if (je == 1)
|
|
|
|
mpd_run_consume(mpd.conn, uint_buf1);
|
|
|
|
|
2018-08-17 09:52:22 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {single: %u}}", &uint_buf1);
|
2018-06-19 22:43:36 +00:00
|
|
|
if (je == 1)
|
|
|
|
mpd_run_single(mpd.conn, uint_buf1);
|
|
|
|
|
2018-08-17 09:52:22 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {crossfade: %u}}", &uint_buf1);
|
2018-06-19 22:43:36 +00:00
|
|
|
if (je == 1)
|
|
|
|
mpd_run_crossfade(mpd.conn, uint_buf1);
|
|
|
|
|
2018-08-21 19:28:13 +00:00
|
|
|
if (config.mixramp) {
|
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {mixrampdb: %f}}", &float_buf);
|
|
|
|
if (je == 1)
|
|
|
|
mpd_run_mixrampdb(mpd.conn, float_buf);
|
2018-06-19 22:43:36 +00:00
|
|
|
|
2018-08-21 19:28:13 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {mixrampdelay: %f}}", &float_buf);
|
|
|
|
if (je == 1)
|
|
|
|
mpd_run_mixrampdelay(mpd.conn, float_buf);
|
|
|
|
}
|
2018-06-19 22:43:36 +00:00
|
|
|
|
2018-09-30 22:18:55 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {replaygain: %Q}}", &p_charbuf1);
|
2018-07-10 23:06:31 +00:00
|
|
|
if (je == 1) {
|
2018-06-19 22:43:36 +00:00
|
|
|
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);
|
2018-06-26 21:25:52 +00:00
|
|
|
}
|
|
|
|
free(p_charbuf1);
|
2018-06-19 22:43:36 +00:00
|
|
|
}
|
2018-10-01 19:13:38 +00:00
|
|
|
if (mympd_state.jukeboxMode > 0)
|
|
|
|
mympd_jukebox();
|
2018-07-16 22:33:55 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
2018-06-19 22:43:36 +00:00
|
|
|
break;
|
2018-06-14 20:19:36 +00:00
|
|
|
case MPD_API_WELCOME:
|
2018-06-19 22:43:36 +00:00
|
|
|
n = mympd_put_welcome(mpd.buf);
|
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_DATABASE_UPDATE:
|
2018-07-18 22:38:46 +00:00
|
|
|
uint_rc = mpd_run_update(mpd.conn, NULL);
|
|
|
|
if (uint_rc > 0)
|
2018-07-22 19:00:26 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
2013-11-09 01:07:03 +00:00
|
|
|
break;
|
2018-09-23 21:48:12 +00:00
|
|
|
case MPD_API_DATABASE_RESCAN:
|
|
|
|
uint_rc = mpd_run_rescan(mpd.conn, NULL);
|
|
|
|
if (uint_rc > 0)
|
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
2018-09-24 20:55:53 +00:00
|
|
|
break;
|
2018-09-29 23:03:00 +00:00
|
|
|
case MPD_API_SMARTPLS_UPDATE_ALL:
|
2018-09-24 20:55:53 +00:00
|
|
|
uint_rc = mympd_smartpls_update_all();
|
|
|
|
if (uint_rc == 0)
|
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"Smart Playlists updated\"}");
|
|
|
|
else
|
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"error\", \"data\": \"Smart Playlists update failed\"}");
|
|
|
|
break;
|
2018-09-26 22:10:51 +00:00
|
|
|
case MPD_API_SMARTPLS_SAVE:
|
2018-10-01 17:07:38 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {type: %Q}}", &p_charbuf1);
|
2018-09-30 22:18:55 +00:00
|
|
|
if (je == 1) {
|
|
|
|
if (strcmp(p_charbuf1, "sticker") == 0) {
|
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {playlist: %Q, sticker: %Q, maxentries: %d}}", &p_charbuf2, &p_charbuf3, &int_buf1);
|
|
|
|
n = mympd_smartpls_save(p_charbuf1, p_charbuf2, p_charbuf3, NULL, int_buf1, 0);
|
|
|
|
free(p_charbuf2);
|
|
|
|
free(p_charbuf3);
|
|
|
|
}
|
|
|
|
else if (strcmp(p_charbuf1, "newest") == 0) {
|
2018-10-08 20:44:22 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {playlist: %Q, timerange: %d}}", &p_charbuf2, &int_buf1);
|
|
|
|
n = mympd_smartpls_save(p_charbuf1, p_charbuf2, NULL, NULL, 0, int_buf1);
|
2018-09-30 22:18:55 +00:00
|
|
|
free(p_charbuf2);
|
|
|
|
}
|
|
|
|
else if (strcmp(p_charbuf1, "search") == 0) {
|
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {playlist: %Q, tag: %Q, searchstr: %Q}}", &p_charbuf2, &p_charbuf3, &p_charbuf4);
|
|
|
|
n = mympd_smartpls_save(p_charbuf1, p_charbuf2, p_charbuf3, p_charbuf4, 0, 0);
|
|
|
|
free(p_charbuf2);
|
|
|
|
free(p_charbuf3);
|
|
|
|
free(p_charbuf4);
|
|
|
|
}
|
2018-09-26 22:10:51 +00:00
|
|
|
free(p_charbuf1);
|
2018-09-30 22:18:55 +00:00
|
|
|
}
|
|
|
|
if (n == 0)
|
2018-09-26 22:10:51 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
2018-09-30 22:18:55 +00:00
|
|
|
else
|
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"error\", \"data\": \"Error saving playlist\"}");
|
|
|
|
break;
|
|
|
|
case MPD_API_SMARTPLS_GET:
|
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {playlist: %Q}}", &p_charbuf1);
|
|
|
|
if (je == 1) {
|
|
|
|
n = mympd_smartpls_put(mpd.buf, p_charbuf1);
|
|
|
|
free(p_charbuf1);
|
2018-09-26 22:10:51 +00:00
|
|
|
}
|
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_PLAYER_PAUSE:
|
2014-02-16 18:46:53 +00:00
|
|
|
mpd_run_toggle_pause(mpd.conn);
|
2018-07-16 22:33:55 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
2014-02-16 18:46:53 +00:00
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_PLAYER_PREV:
|
2014-02-16 18:46:53 +00:00
|
|
|
mpd_run_previous(mpd.conn);
|
2018-07-16 22:33:55 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
2014-02-16 18:46:53 +00:00
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_PLAYER_NEXT:
|
2018-08-21 19:28:13 +00:00
|
|
|
if (config.stickers)
|
|
|
|
mympd_count_song_id(mpd.song_id, "skipCount", 1);
|
2014-02-16 18:46:53 +00:00
|
|
|
mpd_run_next(mpd.conn);
|
2018-07-16 22:33:55 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
2014-02-16 18:46:53 +00:00
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_PLAYER_PLAY:
|
2014-02-16 18:46:53 +00:00
|
|
|
mpd_run_play(mpd.conn);
|
2018-07-16 22:33:55 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
2014-02-16 18:46:53 +00:00
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_PLAYER_STOP:
|
2014-02-16 18:46:53 +00:00
|
|
|
mpd_run_stop(mpd.conn);
|
2018-07-16 22:33:55 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
2014-02-16 18:46:53 +00:00
|
|
|
break;
|
2018-08-05 22:02:29 +00:00
|
|
|
case MPD_API_QUEUE_CLEAR:
|
2014-02-16 18:46:53 +00:00
|
|
|
mpd_run_clear(mpd.conn);
|
2018-07-16 22:33:55 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
2014-02-16 18:46:53 +00:00
|
|
|
break;
|
2018-08-05 22:02:29 +00:00
|
|
|
case MPD_API_QUEUE_CROP:
|
|
|
|
n = mympd_queue_crop(mpd.buf);
|
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_QUEUE_RM_TRACK:
|
2018-08-06 21:29:20 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {track:%u}}", &uint_buf1);
|
2018-07-16 22:33:55 +00:00
|
|
|
if (je == 1) {
|
2018-06-18 19:55:54 +00:00
|
|
|
mpd_run_delete_id(mpd.conn, uint_buf1);
|
2018-07-16 22:33:55 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
|
|
|
}
|
2014-02-16 18:46:53 +00:00
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_QUEUE_RM_RANGE:
|
2018-08-17 09:52:22 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {start: %u, end: %u}}", &uint_buf1, &uint_buf2);
|
2018-07-16 22:33:55 +00:00
|
|
|
if (je == 2) {
|
2018-06-18 19:55:54 +00:00
|
|
|
mpd_run_delete_range(mpd.conn, uint_buf1, uint_buf2);
|
2018-07-16 22:33:55 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
|
|
|
}
|
2017-04-08 09:25:40 +00:00
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_QUEUE_MOVE_TRACK:
|
2018-08-17 09:52:22 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {from: %u, to: %u}}", &uint_buf1, &uint_buf2);
|
2018-07-10 23:06:31 +00:00
|
|
|
if (je == 2) {
|
2018-10-18 21:55:45 +00:00
|
|
|
uint_buf1--;
|
|
|
|
uint_buf2--;
|
2018-07-26 21:48:03 +00:00
|
|
|
if (uint_buf1 < uint_buf2)
|
2018-10-18 21:55:45 +00:00
|
|
|
uint_buf2--;
|
2018-06-18 19:55:54 +00:00
|
|
|
mpd_run_move(mpd.conn, uint_buf1, uint_buf2);
|
2018-07-16 22:33:55 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
2015-09-04 14:43:16 +00:00
|
|
|
}
|
|
|
|
break;
|
2018-07-26 21:48:03 +00:00
|
|
|
case MPD_API_PLAYLIST_MOVE_TRACK:
|
2018-08-17 09:52:22 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {plist: %Q, from: %u, to: %u }}", &p_charbuf1, &uint_buf1, &uint_buf2);
|
2018-07-26 21:48:03 +00:00
|
|
|
if (je == 3) {
|
2018-10-18 21:55:45 +00:00
|
|
|
uint_buf1--;
|
|
|
|
uint_buf2--;
|
2018-07-26 21:48:03 +00:00
|
|
|
if (uint_buf1 < uint_buf2)
|
2018-10-18 21:55:45 +00:00
|
|
|
uint_buf2--;
|
2018-07-26 21:48:03 +00:00
|
|
|
mpd_send_playlist_move(mpd.conn, p_charbuf1, uint_buf1, uint_buf2);
|
|
|
|
mpd_response_finish(mpd.conn);
|
|
|
|
free(p_charbuf1);
|
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
|
|
|
}
|
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_PLAYER_PLAY_TRACK:
|
2018-08-06 21:29:20 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: { track:%u}}", &uint_buf1);
|
2018-07-16 22:33:55 +00:00
|
|
|
if (je == 1) {
|
2018-06-18 19:55:54 +00:00
|
|
|
mpd_run_play_id(mpd.conn, uint_buf1);
|
2018-07-16 22:33:55 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
|
|
|
}
|
2014-02-16 18:46:53 +00:00
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_PLAYER_OUTPUT_LIST:
|
2018-08-06 18:57:42 +00:00
|
|
|
n = mympd_put_outputs(mpd.buf);
|
2015-04-28 09:08:21 +00:00
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_PLAYER_TOGGLE_OUTPUT:
|
2018-08-17 09:52:22 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {output: %u, state: %u}}", &uint_buf1, &uint_buf2);
|
2018-07-16 22:33:55 +00:00
|
|
|
if (je == 2) {
|
2018-06-18 19:55:54 +00:00
|
|
|
if (uint_buf2)
|
|
|
|
mpd_run_enable_output(mpd.conn, uint_buf1);
|
2015-04-28 09:08:21 +00:00
|
|
|
else
|
2018-06-18 19:55:54 +00:00
|
|
|
mpd_run_disable_output(mpd.conn, uint_buf1);
|
2018-07-16 22:33:55 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
2015-04-28 09:08:21 +00:00
|
|
|
}
|
|
|
|
break;
|
2018-10-11 19:50:26 +00:00
|
|
|
case MPD_API_PLAYER_VOLUME_SET:
|
2018-08-06 21:29:20 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {volume:%u}}", &uint_buf1);
|
2018-07-16 22:33:55 +00:00
|
|
|
if (je == 1) {
|
2018-06-18 19:55:54 +00:00
|
|
|
mpd_run_set_volume(mpd.conn, uint_buf1);
|
2018-07-16 22:33:55 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
|
|
|
}
|
2014-02-16 18:46:53 +00:00
|
|
|
break;
|
2018-10-11 19:50:26 +00:00
|
|
|
case MPD_API_PLAYER_VOLUME_GET:
|
|
|
|
n = mympd_put_volume(mpd.buf);
|
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_PLAYER_SEEK:
|
2018-08-17 09:52:22 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {songid: %u, seek: %u}}", &uint_buf1, &uint_buf2);
|
2018-07-16 22:33:55 +00:00
|
|
|
if (je == 2) {
|
2018-06-18 19:55:54 +00:00
|
|
|
mpd_run_seek_id(mpd.conn, uint_buf1, uint_buf2);
|
2018-07-16 22:33:55 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
|
|
|
}
|
2014-02-16 18:46:53 +00:00
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_QUEUE_LIST:
|
2018-10-18 21:55:45 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {offset: %u}}", &uint_buf1);
|
2018-07-16 22:33:55 +00:00
|
|
|
if (je == 1) {
|
2018-08-27 19:34:21 +00:00
|
|
|
n = mympd_put_queue(mpd.buf, uint_buf1, &mpd.queue_version, &mpd.queue_length);
|
2018-07-16 22:33:55 +00:00
|
|
|
}
|
2014-02-22 01:11:45 +00:00
|
|
|
break;
|
2018-11-12 23:48:29 +00:00
|
|
|
case MPD_API_QUEUE_LAST_PLAYED:
|
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {offset: %u}}", &uint_buf1);
|
|
|
|
if (je == 1) {
|
|
|
|
n = mympd_put_last_played_songs(mpd.buf, uint_buf1);
|
|
|
|
}
|
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_PLAYER_CURRENT_SONG:
|
2018-06-14 22:00:54 +00:00
|
|
|
n = mympd_put_current_song(mpd.buf);
|
2018-07-09 17:28:28 +00:00
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_DATABASE_SONGDETAILS:
|
2018-10-18 21:55:45 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: { uri: %Q}}", &p_charbuf1);
|
2018-07-09 17:28:28 +00:00
|
|
|
if (je == 1) {
|
|
|
|
n = mympd_put_songdetails(mpd.buf, p_charbuf1);
|
|
|
|
free(p_charbuf1);
|
|
|
|
}
|
2018-06-11 17:33:11 +00:00
|
|
|
break;
|
2018-09-10 20:32:15 +00:00
|
|
|
case MPD_API_DATABASE_TAG_LIST:
|
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {offset: %u, filter: %Q, tag: %Q}}", &uint_buf1, &p_charbuf1, &p_charbuf2);
|
|
|
|
if (je == 3) {
|
|
|
|
n = mympd_put_db_tag(mpd.buf, uint_buf1, p_charbuf2, "", "", p_charbuf1);
|
2018-06-26 21:25:52 +00:00
|
|
|
free(p_charbuf1);
|
2018-09-10 20:32:15 +00:00
|
|
|
free(p_charbuf2);
|
2018-06-26 21:25:52 +00:00
|
|
|
}
|
2018-06-03 16:36:06 +00:00
|
|
|
break;
|
2018-09-10 20:32:15 +00:00
|
|
|
case MPD_API_DATABASE_TAG_ALBUM_LIST:
|
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {offset: %u, filter: %Q, search: %Q, tag: %Q}}", &uint_buf1, &p_charbuf1, &p_charbuf2, &p_charbuf3);
|
|
|
|
if (je == 4) {
|
|
|
|
n = mympd_put_db_tag(mpd.buf, uint_buf1, "Album", p_charbuf3, p_charbuf2, p_charbuf1);
|
2018-06-26 21:25:52 +00:00
|
|
|
free(p_charbuf1);
|
|
|
|
free(p_charbuf2);
|
2018-09-10 20:32:15 +00:00
|
|
|
free(p_charbuf3);
|
2018-06-26 21:25:52 +00:00
|
|
|
}
|
2018-06-03 16:36:06 +00:00
|
|
|
break;
|
2018-09-10 20:32:15 +00:00
|
|
|
case MPD_API_DATABASE_TAG_ALBUM_TITLE_LIST:
|
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {album: %Q, search: %Q, tag: %Q}}", &p_charbuf1, &p_charbuf2, &p_charbuf3);
|
|
|
|
if (je == 3) {
|
|
|
|
n = mympd_put_songs_in_album(mpd.buf, p_charbuf1, p_charbuf2, p_charbuf3);
|
|
|
|
free(p_charbuf1);
|
|
|
|
free(p_charbuf2);
|
|
|
|
free(p_charbuf3);
|
|
|
|
}
|
|
|
|
break;
|
2018-07-22 19:00:26 +00:00
|
|
|
case MPD_API_PLAYLIST_RENAME:
|
2018-08-17 09:52:22 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {from: %Q, to: %Q}}", &p_charbuf1, &p_charbuf2);
|
2018-07-22 19:00:26 +00:00
|
|
|
if (je == 2) {
|
2018-09-29 23:03:00 +00:00
|
|
|
//rename smart playlist
|
|
|
|
char old_pl_file[400];
|
|
|
|
char new_pl_file[400];
|
|
|
|
snprintf(old_pl_file, 400, "%s/smartpls/%s", config.varlibdir, p_charbuf1);
|
|
|
|
snprintf(new_pl_file, 400, "%s/smartpls/%s", config.varlibdir, p_charbuf2);
|
|
|
|
if (access(old_pl_file, F_OK ) != -1) {
|
|
|
|
if (access(new_pl_file, F_OK ) == -1) {
|
|
|
|
rename(old_pl_file, new_pl_file);
|
|
|
|
//rename mpd playlist
|
|
|
|
mpd_run_rename(mpd.conn, p_charbuf1, p_charbuf2);
|
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"Renamed playlist %s to %s\"}", p_charbuf1, p_charbuf2);
|
|
|
|
} else
|
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"error\", \"data\": \"Renamed playlist %s already exists\"}", p_charbuf2);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mpd_run_rename(mpd.conn, p_charbuf1, p_charbuf2);
|
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"Renamed playlist %s to %s\"}", p_charbuf1, p_charbuf2);
|
|
|
|
}
|
2018-07-22 19:00:26 +00:00
|
|
|
free(p_charbuf1);
|
|
|
|
free(p_charbuf2);
|
|
|
|
}
|
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_PLAYLIST_LIST:
|
2018-10-18 21:55:45 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {offset: %u, filter: %Q}}", &uint_buf1, &p_charbuf1);
|
2018-06-26 21:25:52 +00:00
|
|
|
if (je == 2) {
|
2018-06-18 19:55:54 +00:00
|
|
|
n = mympd_put_playlists(mpd.buf, uint_buf1, p_charbuf1);
|
2018-06-26 21:25:52 +00:00
|
|
|
free(p_charbuf1);
|
|
|
|
}
|
2018-05-28 22:05:56 +00:00
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_PLAYLIST_CONTENT_LIST:
|
2018-08-06 21:29:20 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {uri: %Q, offset:%u, filter:%Q}}", &p_charbuf1, &uint_buf1, &p_charbuf2);
|
2018-07-22 19:00:26 +00:00
|
|
|
if (je == 3) {
|
|
|
|
n = mympd_put_playlist_list(mpd.buf, p_charbuf1, uint_buf1, p_charbuf2);
|
|
|
|
free(p_charbuf1);
|
|
|
|
free(p_charbuf2);
|
|
|
|
}
|
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_PLAYLIST_ADD_TRACK:
|
2018-08-06 21:29:20 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {plist:%Q, uri:%Q}}", &p_charbuf1, &p_charbuf2);
|
2018-07-22 19:00:26 +00:00
|
|
|
if (je == 2) {
|
|
|
|
mpd_run_playlist_add(mpd.conn, p_charbuf1, p_charbuf2);
|
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"Added %s to playlist %s\"}", p_charbuf2, p_charbuf1);
|
|
|
|
free(p_charbuf1);
|
|
|
|
free(p_charbuf2);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MPD_API_PLAYLIST_CLEAR:
|
2018-08-06 21:29:20 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {uri:%Q}}", &p_charbuf1);
|
2018-07-22 19:00:26 +00:00
|
|
|
if (je == 1) {
|
|
|
|
mpd_run_playlist_clear(mpd.conn, p_charbuf1);
|
|
|
|
free(p_charbuf1);
|
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
|
|
|
}
|
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_PLAYLIST_RM_TRACK:
|
2018-08-06 21:29:20 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {uri:%Q, track:%u}}", &p_charbuf1, &uint_buf1);
|
2018-07-22 19:00:26 +00:00
|
|
|
if (je == 2) {
|
|
|
|
mpd_run_playlist_delete(mpd.conn, p_charbuf1, uint_buf1);
|
|
|
|
free(p_charbuf1);
|
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
|
|
|
}
|
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_DATABASE_FILESYSTEM_LIST:
|
2018-08-06 21:29:20 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {offset:%u, filter:%Q, path:%Q}}", &uint_buf1, &p_charbuf1, &p_charbuf2);
|
2018-06-26 21:25:52 +00:00
|
|
|
if (je == 3) {
|
2018-06-18 19:55:54 +00:00
|
|
|
n = mympd_put_browse(mpd.buf, p_charbuf2, uint_buf1, p_charbuf1);
|
2018-06-26 21:25:52 +00:00
|
|
|
free(p_charbuf1);
|
|
|
|
free(p_charbuf2);
|
|
|
|
}
|
2014-02-16 18:46:53 +00:00
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_QUEUE_ADD_TRACK_AFTER:
|
2018-09-30 22:18:55 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {uri:%Q, to:%d}}", &p_charbuf1, &int_buf1);
|
2018-07-05 19:34:16 +00:00
|
|
|
if (je == 2) {
|
2018-09-30 22:18:55 +00:00
|
|
|
int_rc = mpd_run_add_id_to(mpd.conn, p_charbuf1, int_buf1);
|
2018-07-18 22:38:46 +00:00
|
|
|
if (int_rc > -1 )
|
2018-07-19 16:33:53 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
2018-07-05 19:34:16 +00:00
|
|
|
free(p_charbuf1);
|
|
|
|
}
|
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_QUEUE_REPLACE_TRACK:
|
2018-08-06 21:29:20 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {uri:%Q }}", &p_charbuf1);
|
2018-07-05 19:34:16 +00:00
|
|
|
if (je == 1) {
|
|
|
|
mpd_run_clear(mpd.conn);
|
|
|
|
mpd_run_add(mpd.conn, p_charbuf1);
|
|
|
|
free(p_charbuf1);
|
|
|
|
mpd_run_play(mpd.conn);
|
2018-07-16 22:33:55 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
2018-07-05 19:34:16 +00:00
|
|
|
}
|
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_QUEUE_ADD_TRACK:
|
2018-08-06 21:29:20 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {uri:%Q}}", &p_charbuf1);
|
2018-06-26 21:25:52 +00:00
|
|
|
if (je == 1) {
|
2018-06-18 19:55:54 +00:00
|
|
|
mpd_run_add(mpd.conn, p_charbuf1);
|
2018-06-26 21:25:52 +00:00
|
|
|
free(p_charbuf1);
|
2018-07-16 22:33:55 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
2018-06-26 21:25:52 +00:00
|
|
|
}
|
2014-02-16 18:46:53 +00:00
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_QUEUE_ADD_PLAY_TRACK:
|
2018-08-06 21:29:20 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {uri:%Q}}", &p_charbuf1);
|
2018-06-18 19:55:54 +00:00
|
|
|
if (je == 1) {
|
2018-09-30 22:18:55 +00:00
|
|
|
int_buf1 = mpd_run_add_id(mpd.conn, p_charbuf1);
|
|
|
|
if (int_buf1 != -1)
|
|
|
|
mpd_run_play_id(mpd.conn, int_buf1);
|
2018-06-26 21:25:52 +00:00
|
|
|
free(p_charbuf1);
|
2018-07-16 22:33:55 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
2018-06-18 19:55:54 +00:00
|
|
|
}
|
2014-02-22 00:57:26 +00:00
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_QUEUE_REPLACE_PLAYLIST:
|
2018-08-06 21:29:20 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {plist:%Q}}", &p_charbuf1);
|
2018-07-05 19:34:16 +00:00
|
|
|
if (je == 1) {
|
|
|
|
mpd_run_clear(mpd.conn);
|
|
|
|
mpd_run_load(mpd.conn, p_charbuf1);
|
|
|
|
free(p_charbuf1);
|
|
|
|
mpd_run_play(mpd.conn);
|
2018-07-16 22:33:55 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
2018-07-05 19:34:16 +00:00
|
|
|
}
|
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_QUEUE_ADD_PLAYLIST:
|
2018-08-06 21:29:20 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {plist:%Q}}", &p_charbuf1);
|
2018-06-26 21:25:52 +00:00
|
|
|
if (je == 1) {
|
2018-06-18 19:55:54 +00:00
|
|
|
mpd_run_load(mpd.conn, p_charbuf1);
|
2018-06-26 21:25:52 +00:00
|
|
|
free(p_charbuf1);
|
2018-07-16 22:33:55 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
2018-06-26 21:25:52 +00:00
|
|
|
}
|
2014-02-22 00:57:26 +00:00
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_QUEUE_SAVE:
|
2018-08-06 21:29:20 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{ data: {plist:%Q}}", &p_charbuf1);
|
2018-06-26 21:25:52 +00:00
|
|
|
if (je == 1) {
|
2018-06-18 19:55:54 +00:00
|
|
|
mpd_run_save(mpd.conn, p_charbuf1);
|
2018-06-26 21:25:52 +00:00
|
|
|
free(p_charbuf1);
|
2018-07-16 22:33:55 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
2018-06-26 21:25:52 +00:00
|
|
|
}
|
2015-09-02 17:24:52 +00:00
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_QUEUE_SEARCH:
|
2018-09-03 18:02:12 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {offset:%u, filter:%Q, searchstr:%Q}}", &uint_buf1, &p_charbuf1, &p_charbuf2);
|
2018-06-26 21:25:52 +00:00
|
|
|
if (je == 3) {
|
2018-06-18 19:55:54 +00:00
|
|
|
n = mympd_search_queue(mpd.buf, p_charbuf1, uint_buf1, p_charbuf2);
|
2018-06-26 21:25:52 +00:00
|
|
|
free(p_charbuf1);
|
|
|
|
free(p_charbuf2);
|
|
|
|
}
|
2018-05-24 22:21:19 +00:00
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_DATABASE_SEARCH:
|
2018-09-03 18:02:12 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {searchstr:%Q, filter:%Q, plist:%Q, offset:%u}}", &p_charbuf1, &p_charbuf2, &p_charbuf3, &uint_buf1);
|
|
|
|
if (je == 4) {
|
|
|
|
n = mympd_search(mpd.buf, p_charbuf1, p_charbuf2, p_charbuf3, uint_buf1);
|
2018-06-26 21:25:52 +00:00
|
|
|
free(p_charbuf1);
|
|
|
|
free(p_charbuf2);
|
|
|
|
}
|
2014-02-22 00:57:26 +00:00
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_QUEUE_SHUFFLE:
|
2018-05-24 19:36:40 +00:00
|
|
|
mpd_run_shuffle(mpd.conn);
|
2018-07-16 22:33:55 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
2018-05-24 19:36:40 +00:00
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_MESSAGE_SEND:
|
2018-08-06 21:29:20 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: { channel:%Q, text:%Q}}", &p_charbuf1, &p_charbuf2);
|
2018-06-26 21:25:52 +00:00
|
|
|
if (je == 2) {
|
2018-06-18 19:55:54 +00:00
|
|
|
mpd_run_send_message(mpd.conn, p_charbuf1, p_charbuf2);
|
2018-06-26 21:25:52 +00:00
|
|
|
free(p_charbuf1);
|
|
|
|
free(p_charbuf2);
|
2018-07-16 22:33:55 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
2018-06-26 21:25:52 +00:00
|
|
|
}
|
2017-03-18 12:31:26 +00:00
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_PLAYLIST_RM:
|
2018-08-22 00:13:30 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{data: {uri:%Q}}", &p_charbuf1);
|
2018-06-26 21:25:52 +00:00
|
|
|
if (je == 1) {
|
2018-09-29 23:03:00 +00:00
|
|
|
//remove smart playlist
|
|
|
|
char pl_file[400];
|
|
|
|
snprintf(pl_file, 400, "%s/smartpls/%s", config.varlibdir, p_charbuf1);
|
|
|
|
if (access(pl_file, F_OK ) != -1 )
|
|
|
|
unlink(pl_file);
|
|
|
|
//remove mpd playlist
|
2018-06-18 19:55:54 +00:00
|
|
|
mpd_run_rm(mpd.conn, p_charbuf1);
|
2018-06-26 21:25:52 +00:00
|
|
|
free(p_charbuf1);
|
2018-07-16 22:33:55 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
|
2018-06-26 21:25:52 +00:00
|
|
|
}
|
2018-05-29 20:12:11 +00:00
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_SETTINGS_GET:
|
2018-05-29 21:05:34 +00:00
|
|
|
n = mympd_put_settings(mpd.buf);
|
2014-02-16 18:46:53 +00:00
|
|
|
break;
|
2018-08-27 18:47:00 +00:00
|
|
|
case MPD_API_DATABASE_STATS:
|
2018-08-06 21:29:20 +00:00
|
|
|
n = mympd_put_stats(mpd.buf);
|
2018-08-15 13:06:45 +00:00
|
|
|
break;
|
2014-02-16 18:46:53 +00:00
|
|
|
}
|
2018-07-18 21:47:07 +00:00
|
|
|
|
|
|
|
if (mpd.conn_state == MPD_CONNECTED && mpd_connection_get_error(mpd.conn) != MPD_ERROR_SUCCESS) {
|
2018-06-14 15:28:10 +00:00
|
|
|
#ifdef DEBUG
|
2018-08-06 21:29:20 +00:00
|
|
|
fprintf(stderr, "Error: %s\n", mpd_connection_get_error_message(mpd.conn));
|
2018-06-14 15:28:10 +00:00
|
|
|
#endif
|
2014-02-16 18:46:53 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"error\", \"data\": \"%s\"}",
|
|
|
|
mpd_connection_get_error_message(mpd.conn));
|
2013-11-09 01:07:03 +00:00
|
|
|
|
2014-02-16 18:46:53 +00:00
|
|
|
/* Try to recover error */
|
|
|
|
if (!mpd_connection_clear_error(mpd.conn))
|
|
|
|
mpd.conn_state = MPD_FAILURE;
|
2013-11-09 01:07:03 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 21:15:00 +00:00
|
|
|
mpd_send_idle(mpd.conn);
|
|
|
|
|
2018-07-16 22:36:01 +00:00
|
|
|
if (n == 0)
|
2018-07-16 22:33:55 +00:00
|
|
|
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"error\", \"data\": \"No response for cmd %s\"}", cmd);
|
|
|
|
|
|
|
|
if (is_websocket(nc)) {
|
|
|
|
#ifdef DEBUG
|
2018-08-22 16:39:46 +00:00
|
|
|
fprintf(stderr, "Send websocket response:\n %s\n", mpd.buf);
|
2018-07-16 22:33:55 +00:00
|
|
|
#endif
|
|
|
|
mg_send_websocket_frame(nc, WEBSOCKET_OP_TEXT, mpd.buf, n);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
#ifdef DEBUG
|
2018-08-22 16:39:46 +00:00
|
|
|
fprintf(stderr, "Send http response (first 800 chars):\n%*.*s\n", 0, 800, mpd.buf);
|
2018-07-16 22:33:55 +00:00
|
|
|
#endif
|
|
|
|
mg_send_http_chunk(nc, mpd.buf, n);
|
|
|
|
}
|
2018-07-18 21:47:07 +00:00
|
|
|
free(cmd);
|
2014-02-16 18:46:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 13:06:45 +00:00
|
|
|
void mympd_notify(struct mg_mgr *s) {
|
|
|
|
for (struct mg_connection *c = mg_next(s, NULL); c != NULL; c = mg_next(s, c)) {
|
|
|
|
if (!is_websocket(c))
|
|
|
|
continue;
|
|
|
|
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, strlen(mpd.buf));
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
2018-08-23 09:27:12 +00:00
|
|
|
fprintf(stderr, "NOTIFY: %s\n", mpd.buf);
|
2018-08-15 13:06:45 +00:00
|
|
|
#endif
|
2013-11-04 17:18:38 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 15:24:37 +00:00
|
|
|
void mympd_parse_idle(struct mg_mgr *s, int idle_bitmask) {
|
2018-08-22 16:29:47 +00:00
|
|
|
int len = 0;
|
2018-10-18 21:55:45 +00:00
|
|
|
for (unsigned j = 0;; j++) {
|
2018-08-22 16:29:47 +00:00
|
|
|
enum mpd_idle idle_event = 1 << j;
|
|
|
|
const char *idle_name = mpd_idle_name(idle_event);
|
|
|
|
if (idle_name == NULL)
|
|
|
|
break;
|
|
|
|
if (idle_bitmask & idle_event) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "IDLE: %s\n", idle_name);
|
|
|
|
#endif
|
|
|
|
switch(idle_event) {
|
|
|
|
case MPD_IDLE_DATABASE:
|
|
|
|
len = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"update_database\"}");
|
2018-09-24 20:55:53 +00:00
|
|
|
mympd_smartpls_update_all();
|
2018-08-22 16:29:47 +00:00
|
|
|
break;
|
|
|
|
case MPD_IDLE_STORED_PLAYLIST:
|
|
|
|
len = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"update_stored_playlist\"}");
|
|
|
|
break;
|
|
|
|
case MPD_IDLE_QUEUE:
|
|
|
|
len = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"update_queue\"}");
|
2018-09-30 01:03:33 +00:00
|
|
|
if (mympd_state.jukeboxMode > 0)
|
2018-09-17 22:25:05 +00:00
|
|
|
mympd_jukebox();
|
2018-08-22 16:29:47 +00:00
|
|
|
break;
|
|
|
|
case MPD_IDLE_PLAYER:
|
2018-08-23 15:24:37 +00:00
|
|
|
len = mympd_put_state(mpd.buf, &mpd.song_id, &mpd.next_song_id, &mpd.last_song_id, &mpd.queue_version, &mpd.queue_length);
|
2018-11-12 23:48:29 +00:00
|
|
|
if (mpd.song_id != mpd.last_song_id) {
|
|
|
|
if (mpd.last_last_played_id != mpd.song_id)
|
|
|
|
mympd_last_played_list(mpd.song_id);
|
|
|
|
if (config.stickers && mpd.last_update_sticker_song_id != mpd.song_id) {
|
|
|
|
mympd_count_song_id(mpd.song_id, "playCount", 1);
|
|
|
|
mympd_last_played_song_id(mpd.song_id);
|
|
|
|
mpd.last_update_sticker_song_id = mpd.song_id;
|
|
|
|
}
|
2018-08-22 16:29:47 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MPD_IDLE_MIXER:
|
2018-10-11 19:50:26 +00:00
|
|
|
len = mympd_put_volume(mpd.buf);
|
2018-08-22 16:29:47 +00:00
|
|
|
break;
|
|
|
|
case MPD_IDLE_OUTPUT:
|
|
|
|
len = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"update_outputs\"}");
|
|
|
|
break;
|
|
|
|
case MPD_IDLE_OPTIONS:
|
|
|
|
len = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"update_options\"}");
|
|
|
|
break;
|
|
|
|
case MPD_IDLE_UPDATE:
|
2018-08-24 10:11:27 +00:00
|
|
|
len = mympd_get_updatedb_state(mpd.buf);
|
2018-08-22 16:29:47 +00:00
|
|
|
break;
|
|
|
|
case MPD_IDLE_STICKER:
|
|
|
|
len = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"update_sticker\"}");
|
|
|
|
break;
|
|
|
|
case MPD_IDLE_SUBSCRIPTION:
|
|
|
|
len = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"update_subscription\"}");
|
|
|
|
break;
|
|
|
|
case MPD_IDLE_MESSAGE:
|
|
|
|
len = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"update_message\"}");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
if (len > 0)
|
|
|
|
mympd_notify(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-03 21:49:55 +00:00
|
|
|
void mympd_mpd_features() {
|
|
|
|
struct mpd_pair *pair;
|
2018-09-17 17:06:06 +00:00
|
|
|
char s[2] = ",";
|
2018-10-29 18:17:04 +00:00
|
|
|
char *taglist = strdup(config.taglist);
|
|
|
|
char *searchtaglist = strdup(config.searchtaglist);
|
|
|
|
char *browsetaglist = strdup(config.browsetaglist);
|
2018-09-17 17:06:06 +00:00
|
|
|
char *token;
|
|
|
|
|
2018-09-03 22:20:28 +00:00
|
|
|
mpd.protocol = mpd_connection_get_server_version(mpd.conn);
|
|
|
|
|
|
|
|
// Defaults
|
2018-09-03 21:49:55 +00:00
|
|
|
mpd.feat_sticker = false;
|
2018-10-25 20:03:33 +00:00
|
|
|
mpd.feat_playlists = false;
|
|
|
|
mpd.feat_tags = false;
|
2018-09-03 21:49:55 +00:00
|
|
|
|
|
|
|
mpd_send_allowed_commands(mpd.conn);
|
|
|
|
while ((pair = mpd_recv_command_pair(mpd.conn)) != NULL) {
|
|
|
|
if (strcmp(pair->value, "sticker") == 0)
|
|
|
|
mpd.feat_sticker = true;
|
2018-10-25 20:03:33 +00:00
|
|
|
if (strcmp(pair->value, "listplaylists") == 0)
|
|
|
|
mpd.feat_playlists = true;
|
2018-09-03 21:49:55 +00:00
|
|
|
mpd_return_pair(mpd.conn, pair);
|
|
|
|
}
|
|
|
|
mpd_response_finish(mpd.conn);
|
2018-09-03 22:20:28 +00:00
|
|
|
printf("MPD protocoll version: %i.%i.%i\n", mpd.protocol[0], mpd.protocol[1], mpd.protocol[2]);
|
2018-09-03 21:49:55 +00:00
|
|
|
if (mpd.feat_sticker == false && config.stickers == true) {
|
|
|
|
printf("MPD don't support stickers, disabling myMPD feature\n");
|
|
|
|
config.stickers = false;
|
|
|
|
}
|
2018-09-24 22:27:24 +00:00
|
|
|
if (config.stickers == false && config.smartpls == true) {
|
2018-09-24 20:55:53 +00:00
|
|
|
printf("Stickers are disabled, disabling smartplaylists\n");
|
2018-09-24 22:27:24 +00:00
|
|
|
config.smartpls = false;
|
2018-09-24 20:55:53 +00:00
|
|
|
}
|
2018-10-25 20:03:33 +00:00
|
|
|
if (mpd.feat_playlists == false && config.smartpls == true) {
|
|
|
|
printf("Playlists are disabled, disabling smartplaylists\n");
|
|
|
|
config.smartpls = false;
|
|
|
|
}
|
|
|
|
|
2018-09-03 22:20:28 +00:00
|
|
|
printf("MPD supported tags: ");
|
2018-10-09 21:51:47 +00:00
|
|
|
list_free(&mpd_tags);
|
2018-09-03 22:20:28 +00:00
|
|
|
mpd_send_list_tag_types(mpd.conn);
|
|
|
|
while ((pair = mpd_recv_tag_type_pair(mpd.conn)) != NULL) {
|
|
|
|
printf("%s ", pair->value);
|
2018-10-09 21:51:47 +00:00
|
|
|
list_push(&mpd_tags, pair->value, 1);
|
2018-09-03 22:20:28 +00:00
|
|
|
mpd_return_pair(mpd.conn, pair);
|
|
|
|
}
|
|
|
|
mpd_response_finish(mpd.conn);
|
2018-10-09 21:51:47 +00:00
|
|
|
list_free(&mympd_tags);
|
2018-10-25 20:03:33 +00:00
|
|
|
if (mpd_tags.length == 0) {
|
|
|
|
printf("none\nTags are disabled\n");
|
|
|
|
mpd.feat_tags = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mpd.feat_tags = true;
|
|
|
|
printf("\nmyMPD enabled tags: ");
|
2018-10-29 18:17:04 +00:00
|
|
|
token = strtok(taglist, s);
|
2018-10-25 20:03:33 +00:00
|
|
|
while (token != NULL) {
|
|
|
|
if (list_get_value(&mpd_tags, token) == 1) {
|
|
|
|
list_push(&mympd_tags, token, 1);
|
|
|
|
printf("%s ", token);
|
|
|
|
}
|
|
|
|
token = strtok(NULL, s);
|
2018-09-17 17:06:06 +00:00
|
|
|
}
|
2018-10-29 18:17:04 +00:00
|
|
|
printf("\nmyMPD enabled searchtags: ");
|
|
|
|
token = strtok(searchtaglist, s);
|
|
|
|
while (token != NULL) {
|
2018-11-04 23:26:59 +00:00
|
|
|
if (list_get_value(&mympd_tags, token) == 1) {
|
2018-10-29 18:17:04 +00:00
|
|
|
list_push(&mympd_searchtags, token, 1);
|
|
|
|
printf("%s ", token);
|
|
|
|
}
|
|
|
|
token = strtok(NULL, s);
|
|
|
|
}
|
|
|
|
printf("\nmyMPD enabled browsetags: ");
|
|
|
|
token = strtok(browsetaglist, s);
|
|
|
|
while (token != NULL) {
|
2018-11-04 23:26:59 +00:00
|
|
|
if (list_get_value(&mympd_tags, token) == 1) {
|
2018-10-29 18:17:04 +00:00
|
|
|
list_push(&mympd_browsetags, token, 1);
|
|
|
|
printf("%s ", token);
|
|
|
|
}
|
|
|
|
token = strtok(NULL, s);
|
|
|
|
}
|
|
|
|
printf("\n");
|
2018-10-25 20:03:33 +00:00
|
|
|
}
|
2018-10-29 18:17:04 +00:00
|
|
|
free(taglist);
|
|
|
|
free(searchtaglist);
|
|
|
|
free(browsetaglist);
|
2018-09-03 21:49:55 +00:00
|
|
|
}
|
2018-08-22 16:29:47 +00:00
|
|
|
|
2018-08-22 00:13:30 +00:00
|
|
|
void mympd_idle(struct mg_mgr *s, int timeout) {
|
2018-08-09 21:15:00 +00:00
|
|
|
struct pollfd fds[1];
|
|
|
|
int pollrc;
|
|
|
|
|
2014-02-16 18:46:53 +00:00
|
|
|
switch (mpd.conn_state) {
|
2013-11-09 01:07:03 +00:00
|
|
|
case MPD_DISCONNECTED:
|
|
|
|
/* Try to connect */
|
2018-09-20 16:59:29 +00:00
|
|
|
printf("MPD Connecting to %s:%ld\n", config.mpdhost, config.mpdport);
|
2018-08-09 21:15:00 +00:00
|
|
|
mpd.conn = mpd_connection_new(config.mpdhost, config.mpdport, mpd.timeout);
|
2014-02-16 18:46:53 +00:00
|
|
|
if (mpd.conn == NULL) {
|
2018-08-23 09:27:12 +00:00
|
|
|
printf("MPD connection failed.");
|
2018-08-09 22:52:58 +00:00
|
|
|
snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"disconnected\"}");
|
|
|
|
mympd_notify(s);
|
2014-02-16 18:46:53 +00:00
|
|
|
mpd.conn_state = MPD_FAILURE;
|
2013-11-09 01:07:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-16 18:46:53 +00:00
|
|
|
if (mpd_connection_get_error(mpd.conn) != MPD_ERROR_SUCCESS) {
|
2018-08-23 09:27:12 +00:00
|
|
|
printf("MPD connection: %s\n", mpd_connection_get_error_message(mpd.conn));
|
2018-08-09 22:52:58 +00:00
|
|
|
snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"error\", \"data\": \"%s\"}", mpd_connection_get_error_message(mpd.conn));
|
|
|
|
mympd_notify(s);
|
2014-02-16 18:46:53 +00:00
|
|
|
mpd.conn_state = MPD_FAILURE;
|
2013-11-09 01:07:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-06 20:51:48 +00:00
|
|
|
if (config.mpdpass && !mpd_run_password(mpd.conn, config.mpdpass)) {
|
2018-08-23 09:27:12 +00:00
|
|
|
printf("MPD connection: %s\n", mpd_connection_get_error_message(mpd.conn));
|
2018-08-09 22:52:58 +00:00
|
|
|
snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"error\", \"data\": \"%s\"}", mpd_connection_get_error_message(mpd.conn));
|
|
|
|
mympd_notify(s);
|
2014-02-16 18:46:53 +00:00
|
|
|
mpd.conn_state = MPD_FAILURE;
|
2013-11-09 01:07:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-23 09:27:12 +00:00
|
|
|
printf("MPD connected.\n");
|
2018-08-09 21:15:00 +00:00
|
|
|
mpd_connection_set_timeout(mpd.conn, mpd.timeout);
|
2014-02-16 18:46:53 +00:00
|
|
|
mpd.conn_state = MPD_CONNECTED;
|
2018-09-03 21:49:55 +00:00
|
|
|
mympd_mpd_features();
|
2018-09-24 20:55:53 +00:00
|
|
|
mympd_smartpls_update_all();
|
2018-09-30 01:03:33 +00:00
|
|
|
if (mympd_state.jukeboxMode > 0)
|
|
|
|
mympd_jukebox();
|
2018-08-09 21:15:00 +00:00
|
|
|
mpd_send_idle(mpd.conn);
|
2013-11-09 01:07:03 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MPD_FAILURE:
|
2018-08-23 09:27:12 +00:00
|
|
|
printf("MPD connection failed.\n");
|
2018-08-09 22:52:58 +00:00
|
|
|
snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"disconnected\"}");
|
|
|
|
mympd_notify(s);
|
2018-08-15 16:06:12 +00:00
|
|
|
|
2014-02-16 18:46:53 +00:00
|
|
|
case MPD_DISCONNECT:
|
2014-02-04 16:58:10 +00:00
|
|
|
case MPD_RECONNECT:
|
2018-07-24 22:58:29 +00:00
|
|
|
if (mpd.conn != NULL)
|
2014-02-16 18:46:53 +00:00
|
|
|
mpd_connection_free(mpd.conn);
|
|
|
|
mpd.conn = NULL;
|
|
|
|
mpd.conn_state = MPD_DISCONNECTED;
|
2013-11-09 01:07:03 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MPD_CONNECTED:
|
2018-08-09 21:15:00 +00:00
|
|
|
fds[0].fd = mpd_connection_get_fd(mpd.conn);
|
|
|
|
fds[0].events = POLLIN;
|
|
|
|
pollrc = poll(fds, 1, timeout);
|
2018-08-22 16:29:47 +00:00
|
|
|
if (pollrc > 0 || idle_bitmask_save > 0) {
|
|
|
|
//Handle idle event
|
2018-08-09 21:15:00 +00:00
|
|
|
mpd_send_noidle(mpd.conn);
|
2018-08-22 16:29:47 +00:00
|
|
|
if (pollrc > 0) {
|
|
|
|
enum mpd_idle idle_bitmask = mpd_recv_idle(mpd.conn, false);
|
|
|
|
mympd_parse_idle(s, idle_bitmask);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mpd_response_finish(mpd.conn);
|
|
|
|
}
|
|
|
|
if (idle_bitmask_save > 0) {
|
|
|
|
//Handle idle event saved in mympd_callback
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "HANDLE SAVED IDLE EVENT\n");
|
|
|
|
#endif
|
|
|
|
mympd_parse_idle(s, idle_bitmask_save);
|
|
|
|
idle_bitmask_save = 0;
|
|
|
|
}
|
2018-08-09 21:15:00 +00:00
|
|
|
mpd_send_idle(mpd.conn);
|
2015-04-21 03:11:03 +00:00
|
|
|
}
|
2013-11-09 01:07:03 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-11-04 17:18:38 +00:00
|
|
|
}
|
|
|
|
|
2018-08-24 10:11:27 +00:00
|
|
|
int mympd_get_updatedb_state(char *buffer) {
|
|
|
|
struct mpd_status *status;
|
2018-08-27 16:57:17 +00:00
|
|
|
int len, update_id;
|
2018-08-24 10:11:27 +00:00
|
|
|
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
|
|
|
|
|
|
|
|
status = mpd_run_status(mpd.conn);
|
2018-10-14 22:03:56 +00:00
|
|
|
if (!status)
|
|
|
|
RETURN_ERROR_AND_RECOVER("replay_gain_status");
|
2018-08-27 16:57:17 +00:00
|
|
|
update_id = mpd_status_get_update_id(status);
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "Update database ID: %d\n", update_id);
|
|
|
|
#endif
|
|
|
|
if ( update_id > 0)
|
|
|
|
len = json_printf(&out, "{type: update_started, data: {jobid: %d}}", update_id);
|
2018-08-24 10:11:27 +00:00
|
|
|
else
|
2018-08-27 16:57:17 +00:00
|
|
|
len = json_printf(&out, "{type: update_finished}");
|
2018-08-24 10:11:27 +00:00
|
|
|
|
|
|
|
mpd_status_free(status);
|
|
|
|
|
2018-10-14 22:03:56 +00:00
|
|
|
CHECK_RETURN_LEN();
|
2018-08-24 10:11:27 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 09:52:22 +00:00
|
|
|
void mympd_get_sticker(const char *uri, t_sticker *sticker) {
|
|
|
|
struct mpd_pair *pair;
|
2018-08-22 12:06:22 +00:00
|
|
|
char *crap;
|
2018-08-17 09:52:22 +00:00
|
|
|
sticker->playCount = 0;
|
|
|
|
sticker->skipCount = 0;
|
|
|
|
sticker->lastPlayed = 0;
|
|
|
|
sticker->like = 1;
|
2018-08-22 18:42:23 +00:00
|
|
|
if (uri == NULL || strncasecmp("http:", uri, 5) == 0 || strncasecmp("https:", uri, 6) == 0)
|
|
|
|
return;
|
|
|
|
|
2018-08-17 09:52:22 +00:00
|
|
|
if (mpd_send_sticker_list(mpd.conn, "song", uri)) {
|
|
|
|
while ((pair = mpd_recv_sticker(mpd.conn)) != NULL) {
|
|
|
|
if (strcmp(pair->name, "playCount") == 0)
|
2018-08-22 12:06:22 +00:00
|
|
|
sticker->playCount = strtol(pair->value, &crap, 10);
|
2018-08-17 09:52:22 +00:00
|
|
|
else if (strcmp(pair->name, "skipCount") == 0)
|
2018-08-22 12:06:22 +00:00
|
|
|
sticker->skipCount = strtol(pair->value, &crap, 10);
|
2018-08-17 09:52:22 +00:00
|
|
|
else if (strcmp(pair->name, "lastPlayed") == 0)
|
2018-08-22 12:06:22 +00:00
|
|
|
sticker->lastPlayed = strtol(pair->value, &crap, 10);
|
2018-08-17 09:52:22 +00:00
|
|
|
else if (strcmp(pair->name, "like") == 0)
|
2018-08-22 12:06:22 +00:00
|
|
|
sticker->like = strtol(pair->value, &crap, 10);
|
2018-08-17 09:52:22 +00:00
|
|
|
mpd_return_sticker(mpd.conn, pair);
|
|
|
|
}
|
|
|
|
}
|
2018-08-22 00:13:30 +00:00
|
|
|
else
|
|
|
|
LOG_ERROR_AND_RECOVER("mpd_send_sticker_list");
|
2018-08-17 09:52:22 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 22:23:58 +00:00
|
|
|
void mympd_count_song_id(int song_id, char *name, int value) {
|
2018-08-15 15:23:47 +00:00
|
|
|
struct mpd_song *song;
|
|
|
|
if (song_id > -1) {
|
|
|
|
song = mpd_run_get_queue_song_id(mpd.conn, song_id);
|
|
|
|
if (song) {
|
2018-10-18 22:23:58 +00:00
|
|
|
mympd_count_song_uri(mpd_song_get_uri(song), name, value);
|
2018-08-15 15:23:47 +00:00
|
|
|
mpd_song_free(song);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mympd_count_song_uri(const char *uri, char *name, int value) {
|
2018-08-22 18:42:23 +00:00
|
|
|
if (uri == NULL || strncasecmp("http:", uri, 5) == 0 || strncasecmp("https:", uri, 6) == 0)
|
2018-08-22 12:06:22 +00:00
|
|
|
return;
|
2018-08-15 15:23:47 +00:00
|
|
|
struct mpd_pair *pair;
|
2018-08-22 12:06:22 +00:00
|
|
|
char *crap;
|
2018-08-15 15:23:47 +00:00
|
|
|
int old_value = 0;
|
2018-08-22 00:13:30 +00:00
|
|
|
char v[4];
|
2018-08-15 15:23:47 +00:00
|
|
|
if (mpd_send_sticker_list(mpd.conn, "song", uri)) {
|
|
|
|
while ((pair = mpd_recv_sticker(mpd.conn)) != NULL) {
|
|
|
|
if (strcmp(pair->name, name) == 0)
|
2018-08-22 12:06:22 +00:00
|
|
|
old_value = strtol(pair->value, &crap, 10);
|
2018-08-15 15:23:47 +00:00
|
|
|
mpd_return_sticker(mpd.conn, pair);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
old_value += value;
|
2018-08-22 00:13:30 +00:00
|
|
|
if (old_value > 999)
|
|
|
|
old_value = 999;
|
2018-08-15 15:23:47 +00:00
|
|
|
else if (old_value < 0)
|
|
|
|
old_value = 0;
|
2018-08-24 10:11:27 +00:00
|
|
|
snprintf(v, 4, "%d", old_value);
|
2018-08-22 12:06:22 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "STICKER_COUNT_SONG: \"%s\" -> %s: %s\n", uri, name, v);
|
|
|
|
#endif
|
2018-08-22 00:13:30 +00:00
|
|
|
if (!mpd_run_sticker_set(mpd.conn, "song", uri, name, v))
|
|
|
|
LOG_ERROR_AND_RECOVER("mpd_send_sticker_set");
|
2018-08-15 15:23:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void mympd_like_song_uri(const char *uri, int value) {
|
2018-08-22 18:42:23 +00:00
|
|
|
if (uri == NULL || strncasecmp("http:", uri, 5) == 0 || strncasecmp("https:", uri, 6) == 0)
|
|
|
|
return;
|
2018-08-24 10:11:27 +00:00
|
|
|
char v[2];
|
2018-08-17 09:52:22 +00:00
|
|
|
if (value > 2)
|
|
|
|
value = 2;
|
|
|
|
else if (value < 0)
|
|
|
|
value = 0;
|
2018-08-15 15:23:47 +00:00
|
|
|
snprintf(v, 2, "%d", value);
|
2018-08-22 00:13:30 +00:00
|
|
|
if (!mpd_run_sticker_set(mpd.conn, "song", uri, "like", v))
|
|
|
|
LOG_ERROR_AND_RECOVER("mpd_send_sticker_set");
|
2018-08-15 15:23:47 +00:00
|
|
|
}
|
|
|
|
|
2018-11-12 23:48:29 +00:00
|
|
|
void mympd_last_played_list(int song_id) {
|
|
|
|
struct mpd_song *song;
|
2018-11-13 23:50:30 +00:00
|
|
|
char tmpfile[400];
|
|
|
|
char cfgfile[400];
|
|
|
|
snprintf(cfgfile, 400, "%s/state/last_played", config.varlibdir);
|
|
|
|
snprintf(tmpfile, 400, "%s/tmp/last_played", config.varlibdir);
|
|
|
|
|
2018-11-12 23:48:29 +00:00
|
|
|
if (song_id > -1) {
|
|
|
|
song = mpd_run_get_queue_song_id(mpd.conn, song_id);
|
|
|
|
if (song) {
|
2018-11-13 23:50:30 +00:00
|
|
|
list_insert(&last_played, mpd_song_get_uri(song), time(NULL));
|
2018-11-12 23:48:29 +00:00
|
|
|
mpd.last_last_played_id = song_id;
|
|
|
|
mpd_song_free(song);
|
|
|
|
if (last_played.length > config.last_played_count) {
|
2018-11-13 23:50:30 +00:00
|
|
|
list_shift(&last_played, last_played.length -1);
|
|
|
|
}
|
|
|
|
FILE *fp = fopen(tmpfile, "w");
|
|
|
|
if (fp == NULL) {
|
|
|
|
printf("Error opening %s\n", tmpfile);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
struct node *current = last_played.list;
|
|
|
|
while (current != NULL) {
|
|
|
|
fprintf(fp, "%ld::%s\n", current->value, current->data);
|
|
|
|
current = current->next;
|
2018-11-12 23:48:29 +00:00
|
|
|
}
|
2018-11-13 23:50:30 +00:00
|
|
|
fclose(fp);
|
|
|
|
rename(tmpfile, cfgfile);
|
2018-11-12 23:48:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-15 16:06:12 +00:00
|
|
|
void mympd_last_played_song_id(int song_id) {
|
2018-10-18 22:23:58 +00:00
|
|
|
struct mpd_song *song;
|
2018-11-13 23:50:30 +00:00
|
|
|
|
2018-10-18 22:23:58 +00:00
|
|
|
if (song_id > -1) {
|
|
|
|
song = mpd_run_get_queue_song_id(mpd.conn, song_id);
|
|
|
|
if (song) {
|
|
|
|
mympd_last_played_song_uri(mpd_song_get_uri(song));
|
|
|
|
mpd_song_free(song);
|
|
|
|
}
|
|
|
|
}
|
2018-08-15 16:06:12 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 15:23:47 +00:00
|
|
|
void mympd_last_played_song_uri(const char *uri) {
|
2018-08-22 18:42:23 +00:00
|
|
|
if (uri == NULL || strncasecmp("http:", uri, 5) == 0 || strncasecmp("https:", uri, 6) == 0)
|
2018-08-22 12:06:22 +00:00
|
|
|
return;
|
2018-08-15 15:23:47 +00:00
|
|
|
char v[20];
|
2018-08-24 10:11:27 +00:00
|
|
|
snprintf(v, 20, "%lu", time(NULL));
|
2018-08-22 00:13:30 +00:00
|
|
|
if (!mpd_run_sticker_set(mpd.conn, "song", uri, "lastPlayed", v))
|
|
|
|
LOG_ERROR_AND_RECOVER("mpd_send_sticker_set");
|
2018-08-15 15:23:47 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 21:15:00 +00:00
|
|
|
|
2018-07-10 23:06:31 +00:00
|
|
|
char* mympd_get_tag(struct mpd_song const *song, enum mpd_tag_type tag) {
|
2014-02-22 01:11:45 +00:00
|
|
|
char *str;
|
2018-07-09 17:49:55 +00:00
|
|
|
str = (char *)mpd_song_get_tag(song, tag, 0);
|
2018-07-10 23:06:31 +00:00
|
|
|
if (str == NULL) {
|
2018-07-09 17:49:55 +00:00
|
|
|
if (tag == MPD_TAG_TITLE)
|
|
|
|
str = basename((char *)mpd_song_get_uri(song));
|
2018-09-11 18:59:22 +00:00
|
|
|
else if (tag == MPD_TAG_ALBUM_ARTIST)
|
|
|
|
str = (char *)mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
|
2018-07-09 17:49:55 +00:00
|
|
|
else
|
|
|
|
str = "-";
|
2014-11-20 19:17:16 +00:00
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2018-09-17 22:25:05 +00:00
|
|
|
void mympd_jukebox() {
|
|
|
|
struct mpd_status *status;
|
|
|
|
status = mpd_run_status(mpd.conn);
|
2018-10-03 22:24:14 +00:00
|
|
|
int queue_length, addSongs, i;
|
2018-09-17 22:25:05 +00:00
|
|
|
struct mpd_entity *entity;
|
|
|
|
const struct mpd_song *song;
|
2018-09-30 01:03:33 +00:00
|
|
|
struct mpd_pair *pair;
|
2018-10-03 22:24:14 +00:00
|
|
|
int lineno = 1;
|
|
|
|
int nkeep = 0;
|
2018-10-01 19:51:16 +00:00
|
|
|
|
2018-09-17 22:25:05 +00:00
|
|
|
if (!status) {
|
2018-10-01 19:51:16 +00:00
|
|
|
LOG_ERROR_AND_RECOVER("mpd_run_status");
|
2018-09-17 22:25:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
queue_length = mpd_status_get_queue_length(status);
|
|
|
|
mpd_status_free(status);
|
2018-09-30 01:03:33 +00:00
|
|
|
if (queue_length > mympd_state.jukeboxQueueLength)
|
2018-09-17 22:25:05 +00:00
|
|
|
return;
|
2018-09-30 01:03:33 +00:00
|
|
|
|
2018-10-03 22:24:14 +00:00
|
|
|
if (mympd_state.jukeboxMode == 1)
|
|
|
|
addSongs = mympd_state.jukeboxQueueLength - queue_length;
|
|
|
|
else
|
|
|
|
addSongs = 1;
|
|
|
|
|
|
|
|
if (addSongs < 1)
|
|
|
|
return;
|
2018-10-25 20:03:33 +00:00
|
|
|
|
|
|
|
if (mpd.feat_playlists == false && strcmp(mympd_state.jukeboxPlaylist, "Database") != 0) {
|
|
|
|
printf("Jukebox: Playlists are disabled\n");
|
|
|
|
return;
|
|
|
|
}
|
2018-10-03 22:24:14 +00:00
|
|
|
|
|
|
|
srand((unsigned int)time(NULL));
|
2018-10-07 21:21:00 +00:00
|
|
|
|
|
|
|
struct list add_list;
|
|
|
|
list_init(&add_list);
|
2018-10-03 22:24:14 +00:00
|
|
|
|
|
|
|
if (mympd_state.jukeboxMode == 1) {
|
|
|
|
//add songs
|
|
|
|
if (strcmp(mympd_state.jukeboxPlaylist, "Database") == 0) {
|
|
|
|
if (!mpd_send_list_all(mpd.conn, "/")) {
|
2018-10-25 20:03:33 +00:00
|
|
|
LOG_ERROR_AND_RECOVER("mpd_send_list_all");
|
2018-10-07 21:21:00 +00:00
|
|
|
list_free(&add_list);
|
2018-10-03 22:24:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!mpd_send_list_playlist(mpd.conn, mympd_state.jukeboxPlaylist)) {
|
|
|
|
LOG_ERROR_AND_RECOVER("mpd_send_list_playlist");
|
2018-10-07 21:21:00 +00:00
|
|
|
list_free(&add_list);
|
2018-10-03 22:24:14 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-10-01 19:51:16 +00:00
|
|
|
}
|
2018-09-30 22:18:55 +00:00
|
|
|
while ((entity = mpd_recv_entity(mpd.conn)) != NULL) {
|
2018-10-07 21:21:00 +00:00
|
|
|
if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
|
|
|
|
if (randrange(lineno) < addSongs) {
|
|
|
|
if (nkeep < addSongs) {
|
|
|
|
song = mpd_entity_get_song(entity);
|
|
|
|
list_push(&add_list, mpd_song_get_uri(song), lineno);
|
|
|
|
nkeep++;
|
2018-10-03 22:24:14 +00:00
|
|
|
}
|
|
|
|
else {
|
2018-10-07 21:21:00 +00:00
|
|
|
i = 0;
|
|
|
|
if (addSongs > 1)
|
|
|
|
i = randrange(addSongs);
|
|
|
|
if (addSongs == 1) {
|
|
|
|
song = mpd_entity_get_song(entity);
|
2018-10-08 20:44:22 +00:00
|
|
|
list_replace(&add_list, 0, mpd_song_get_uri(song), lineno);
|
2018-10-07 21:21:00 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
song = mpd_entity_get_song(entity);
|
2018-10-08 20:44:22 +00:00
|
|
|
list_replace(&add_list, i, mpd_song_get_uri(song), lineno);
|
2018-10-07 21:21:00 +00:00
|
|
|
}
|
2018-10-03 22:24:14 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-07 21:21:00 +00:00
|
|
|
lineno++;
|
2018-10-03 22:24:14 +00:00
|
|
|
}
|
2018-09-30 22:18:55 +00:00
|
|
|
mpd_entity_free(entity);
|
2018-10-03 22:24:14 +00:00
|
|
|
}
|
2018-09-30 22:18:55 +00:00
|
|
|
}
|
|
|
|
else if (mympd_state.jukeboxMode == 2) {
|
2018-10-03 22:24:14 +00:00
|
|
|
//add album
|
2018-10-01 19:51:16 +00:00
|
|
|
if (!mpd_search_db_tags(mpd.conn, MPD_TAG_ALBUM)) {
|
2018-10-25 20:03:33 +00:00
|
|
|
LOG_ERROR_AND_RECOVER("mpd_search_db_tags");
|
2018-10-07 21:21:00 +00:00
|
|
|
list_free(&add_list);
|
2018-10-01 19:51:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!mpd_search_commit(mpd.conn)) {
|
2018-10-25 20:03:33 +00:00
|
|
|
LOG_ERROR_AND_RECOVER("mpd_search_commit");
|
2018-10-07 21:21:00 +00:00
|
|
|
list_free(&add_list);
|
2018-10-01 19:51:16 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-10-03 22:24:14 +00:00
|
|
|
while ((pair = mpd_recv_pair_tag(mpd.conn, MPD_TAG_ALBUM )) != NULL) {
|
|
|
|
if (randrange(lineno) < addSongs) {
|
|
|
|
if (nkeep < addSongs) {
|
2018-10-07 21:21:00 +00:00
|
|
|
list_push(&add_list, strdup(pair->value), lineno);
|
|
|
|
nkeep++;
|
2018-10-01 19:51:16 +00:00
|
|
|
}
|
2018-10-03 22:24:14 +00:00
|
|
|
else {
|
|
|
|
i = 0;
|
|
|
|
if (addSongs > 1)
|
|
|
|
i = randrange(addSongs);
|
|
|
|
if (addSongs == 1) {
|
2018-10-08 20:44:22 +00:00
|
|
|
list_replace(&add_list, 0, strdup(pair->value), lineno);
|
2018-10-01 19:51:16 +00:00
|
|
|
}
|
2018-10-03 22:24:14 +00:00
|
|
|
else {
|
2018-10-08 20:44:22 +00:00
|
|
|
list_replace(&add_list, i, strdup(pair->value), lineno);
|
2018-09-30 01:03:33 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-20 16:59:29 +00:00
|
|
|
}
|
2018-10-03 22:24:14 +00:00
|
|
|
lineno++;
|
|
|
|
mpd_return_pair(mpd.conn, pair);
|
|
|
|
}
|
|
|
|
}
|
2018-10-08 20:44:22 +00:00
|
|
|
|
2018-10-03 22:24:14 +00:00
|
|
|
if (nkeep < addSongs) {
|
2018-10-07 21:21:00 +00:00
|
|
|
fprintf(stderr, "Warning: input didn't contain %d entries\n", addSongs);
|
2018-10-03 22:24:14 +00:00
|
|
|
}
|
2018-10-07 21:21:00 +00:00
|
|
|
|
2018-10-08 20:44:22 +00:00
|
|
|
list_shuffle(&add_list);
|
2018-10-08 16:14:07 +00:00
|
|
|
|
2018-10-29 23:33:20 +00:00
|
|
|
nkeep = 0;
|
2018-10-07 21:21:00 +00:00
|
|
|
struct node *current = add_list.list;
|
2018-10-08 16:14:07 +00:00
|
|
|
while (current != NULL) {
|
2018-10-03 22:24:14 +00:00
|
|
|
if (mympd_state.jukeboxMode == 1) {
|
2018-10-07 21:21:00 +00:00
|
|
|
printf("Jukebox adding song: %s\n", current->data);
|
|
|
|
if (!mpd_run_add(mpd.conn, current->data)) {
|
2018-10-03 22:24:14 +00:00
|
|
|
LOG_ERROR_AND_RECOVER("mpd_run_add");
|
2018-09-30 22:18:55 +00:00
|
|
|
}
|
2018-10-29 23:33:20 +00:00
|
|
|
else
|
|
|
|
nkeep++;
|
2018-09-17 22:25:05 +00:00
|
|
|
}
|
2018-10-03 22:24:14 +00:00
|
|
|
else {
|
2018-10-07 21:21:00 +00:00
|
|
|
printf("Jukebox adding album: %s\n", current->data);
|
|
|
|
if (!mpd_send_command(mpd.conn, "searchadd", "Album", current->data, NULL)) {
|
2018-10-03 22:24:14 +00:00
|
|
|
LOG_ERROR_AND_RECOVER("mpd_send_command");
|
|
|
|
return;
|
|
|
|
}
|
2018-10-29 23:33:20 +00:00
|
|
|
else
|
|
|
|
nkeep++;
|
2018-10-03 22:24:14 +00:00
|
|
|
mpd_response_finish(mpd.conn);
|
|
|
|
}
|
2018-10-07 21:21:00 +00:00
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
list_free(&add_list);
|
2018-10-29 23:33:20 +00:00
|
|
|
if (nkeep > 0)
|
|
|
|
mpd_run_play(mpd.conn);
|
|
|
|
else {
|
|
|
|
printf("Error adding song(s), trying again...\n");
|
|
|
|
mympd_jukebox();
|
|
|
|
}
|
2018-10-03 22:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int randrange(int n) {
|
2018-10-08 16:14:07 +00:00
|
|
|
return rand() / (RAND_MAX / (n + 1) + 1);
|
2018-10-03 22:24:14 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 15:24:37 +00:00
|
|
|
int mympd_put_state(char *buffer, int *current_song_id, int *next_song_id, int *last_song_id, unsigned *queue_version, unsigned *queue_length) {
|
2013-11-09 01:07:03 +00:00
|
|
|
struct mpd_status *status;
|
2018-05-31 20:23:57 +00:00
|
|
|
const struct mpd_audio_format *audioformat;
|
2018-06-17 22:42:22 +00:00
|
|
|
int len;
|
|
|
|
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
|
2013-11-09 01:07:03 +00:00
|
|
|
|
2014-02-16 18:46:53 +00:00
|
|
|
status = mpd_run_status(mpd.conn);
|
2013-11-09 01:07:03 +00:00
|
|
|
if (!status) {
|
2018-08-23 09:27:12 +00:00
|
|
|
printf("MPD mpd_run_status: %s\n", mpd_connection_get_error_message(mpd.conn));
|
2014-02-16 18:46:53 +00:00
|
|
|
mpd.conn_state = MPD_FAILURE;
|
2013-11-09 01:07:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-08-17 09:52:22 +00:00
|
|
|
audioformat = mpd_status_get_audio_format(status);
|
2018-08-23 15:24:37 +00:00
|
|
|
int song_id = mpd_status_get_song_id(status);
|
|
|
|
if (*current_song_id != song_id)
|
|
|
|
*last_song_id = *current_song_id;
|
2018-06-17 22:42:22 +00:00
|
|
|
|
2018-08-22 18:42:23 +00:00
|
|
|
len = json_printf(&out, "{type: update_state, data: {"
|
2018-08-21 19:28:13 +00:00
|
|
|
"state: %d, volume: %d, songPos: %d, elapsedTime: %d, "
|
2018-08-15 13:06:45 +00:00
|
|
|
"totalTime: %d, currentSongId: %d, kbitrate: %d, "
|
2018-08-14 14:10:00 +00:00
|
|
|
"audioFormat: { sampleRate: %d, bits: %d, channels: %d}, "
|
2018-08-23 15:24:37 +00:00
|
|
|
"queueLength: %d, nextSongPos: %d, nextSongId: %d, lastSongId: %d, "
|
2018-08-14 14:10:00 +00:00
|
|
|
"queueVersion: %d",
|
2018-06-17 22:42:22 +00:00
|
|
|
mpd_status_get_state(status),
|
|
|
|
mpd_status_get_volume(status),
|
|
|
|
mpd_status_get_song_pos(status),
|
|
|
|
mpd_status_get_elapsed_time(status),
|
|
|
|
mpd_status_get_total_time(status),
|
2018-08-23 15:24:37 +00:00
|
|
|
song_id,
|
2018-06-17 22:42:22 +00:00
|
|
|
mpd_status_get_kbit_rate(status),
|
|
|
|
audioformat ? audioformat->sample_rate : 0,
|
|
|
|
audioformat ? audioformat->bits : 0,
|
|
|
|
audioformat ? audioformat->channels : 0,
|
|
|
|
mpd_status_get_queue_length(status),
|
|
|
|
mpd_status_get_next_song_pos(status),
|
|
|
|
mpd_status_get_next_song_id(status),
|
2018-08-23 15:24:37 +00:00
|
|
|
*last_song_id ? *last_song_id : -1,
|
2018-06-17 22:42:22 +00:00
|
|
|
mpd_status_get_queue_version(status)
|
|
|
|
);
|
|
|
|
|
2018-08-06 18:57:42 +00:00
|
|
|
len += json_printf(&out, "}}");
|
2018-08-23 15:24:37 +00:00
|
|
|
|
|
|
|
*current_song_id = song_id;
|
2018-05-28 18:48:43 +00:00
|
|
|
*next_song_id = mpd_status_get_next_song_id(status);
|
2014-01-17 15:26:26 +00:00
|
|
|
*queue_version = mpd_status_get_queue_version(status);
|
2018-08-21 22:56:17 +00:00
|
|
|
*queue_length = mpd_status_get_queue_length(status);
|
2013-11-09 01:07:03 +00:00
|
|
|
mpd_status_free(status);
|
2018-06-17 22:42:22 +00:00
|
|
|
|
2018-10-14 22:03:56 +00:00
|
|
|
CHECK_RETURN_LEN();
|
2018-06-17 22:42:22 +00:00
|
|
|
return len;
|
2013-11-04 17:18:38 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 19:50:26 +00:00
|
|
|
int mympd_put_volume(char *buffer) {
|
|
|
|
struct mpd_status *status;
|
|
|
|
int len;
|
|
|
|
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
|
|
|
|
|
|
|
|
status = mpd_run_status(mpd.conn);
|
|
|
|
if (!status) {
|
|
|
|
printf("MPD mpd_run_status: %s\n", mpd_connection_get_error_message(mpd.conn));
|
|
|
|
mpd.conn_state = MPD_FAILURE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
len = json_printf(&out, "{type: update_volume, data: {"
|
|
|
|
"volume: %d}}",
|
|
|
|
mpd_status_get_volume(status)
|
|
|
|
);
|
|
|
|
mpd_status_free(status);
|
|
|
|
|
2018-10-14 22:03:56 +00:00
|
|
|
CHECK_RETURN_LEN();
|
2018-10-11 19:50:26 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2018-07-10 23:06:31 +00:00
|
|
|
int mympd_put_welcome(char *buffer) {
|
2018-06-14 20:19:36 +00:00
|
|
|
int len;
|
2018-06-17 22:42:22 +00:00
|
|
|
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
|
|
|
|
|
2018-08-22 00:13:30 +00:00
|
|
|
len = json_printf(&out, "{type: welcome, data: {version: %Q}}", MYMPD_VERSION);
|
2018-06-14 20:19:36 +00:00
|
|
|
|
2018-10-14 22:03:56 +00:00
|
|
|
CHECK_RETURN_LEN();
|
2018-06-14 20:19:36 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2018-10-01 19:13:38 +00:00
|
|
|
bool mympd_state_get(char *name, char *value) {
|
|
|
|
char cfgfile[400];
|
|
|
|
char *line;
|
|
|
|
size_t n = 0;
|
|
|
|
ssize_t read;
|
|
|
|
|
|
|
|
snprintf(cfgfile, 400, "%s/state/%s", config.varlibdir, name);
|
|
|
|
FILE *fp = fopen(cfgfile, "r");
|
|
|
|
if (fp == NULL) {
|
2018-10-07 21:21:00 +00:00
|
|
|
printf("Error opening %s\n", cfgfile);
|
2018-10-01 19:13:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
read = getline(&line, &n, fp);
|
|
|
|
snprintf(value, 400, "%s", line);
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("State %s: %s\n", name, value);
|
|
|
|
#endif
|
|
|
|
fclose(fp);
|
|
|
|
if (read > 0)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-18 21:33:11 +00:00
|
|
|
bool mympd_state_set(const char *name, const char *value) {
|
2018-10-01 19:13:38 +00:00
|
|
|
char tmpfile[400];
|
|
|
|
char cfgfile[400];
|
|
|
|
snprintf(cfgfile, 400, "%s/state/%s", config.varlibdir, name);
|
|
|
|
snprintf(tmpfile, 400, "%s/tmp/%s", config.varlibdir, name);
|
|
|
|
|
|
|
|
FILE *fp = fopen(tmpfile, "w");
|
|
|
|
if (fp == NULL) {
|
2018-10-07 21:21:00 +00:00
|
|
|
printf("Error opening %s\n", tmpfile);
|
2018-10-01 19:13:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
fprintf(fp, value);
|
|
|
|
fclose(fp);
|
|
|
|
rename(tmpfile, cfgfile);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-16 21:38:38 +00:00
|
|
|
int mympd_syscmd(char *buffer, char *cmd, int order) {
|
2018-10-15 22:34:25 +00:00
|
|
|
int len;
|
|
|
|
char filename[400];
|
|
|
|
char *line;
|
|
|
|
size_t n = 0;
|
|
|
|
ssize_t read;
|
|
|
|
|
2018-10-16 21:38:38 +00:00
|
|
|
snprintf(filename, 400, "%s/syscmds/%d%s", config.etcdir, order, cmd);
|
2018-10-15 22:34:25 +00:00
|
|
|
FILE *fp = fopen(filename, "r");
|
|
|
|
if (fp == NULL) {
|
|
|
|
len = snprintf(buffer, MAX_SIZE, "{\"type\": \"error\", \"data\": \"Can't execute cmd %s\"}", cmd);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
read = getline(&line, &n, fp);
|
|
|
|
fclose(fp);
|
2018-10-18 21:33:11 +00:00
|
|
|
if (read > 0) {
|
|
|
|
strtok(line, "\n");
|
|
|
|
system(line);
|
|
|
|
len = snprintf(buffer, MAX_SIZE, "{\"type\": \"result\", \"data\": \"Executed cmd %s\"}", line);
|
|
|
|
} else {
|
|
|
|
len = snprintf(buffer, MAX_SIZE, "{\"type\": \"error\", \"data\": \"Can't execute cmd %s\"}", cmd);
|
|
|
|
}
|
2018-10-15 22:34:25 +00:00
|
|
|
CHECK_RETURN_LEN();
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2018-07-10 23:06:31 +00:00
|
|
|
int mympd_put_settings(char *buffer) {
|
2018-05-29 21:05:34 +00:00
|
|
|
struct mpd_status *status;
|
2018-08-22 12:06:22 +00:00
|
|
|
char *replaygain = strdup("");
|
2018-06-17 22:42:22 +00:00
|
|
|
int len;
|
2018-10-09 21:51:47 +00:00
|
|
|
int nr = 0;
|
2018-06-17 22:42:22 +00:00
|
|
|
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
|
2018-09-17 22:25:05 +00:00
|
|
|
|
2018-05-29 21:05:34 +00:00
|
|
|
status = mpd_run_status(mpd.conn);
|
|
|
|
if (!status) {
|
2018-08-23 09:27:12 +00:00
|
|
|
printf("MPD mpd_run_status: %s\n", mpd_connection_get_error_message(mpd.conn));
|
2018-05-29 21:05:34 +00:00
|
|
|
mpd.conn_state = MPD_FAILURE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-22 12:06:22 +00:00
|
|
|
if (!mpd_send_command(mpd.conn, "replay_gain_status", NULL))
|
|
|
|
RETURN_ERROR_AND_RECOVER("replay_gain_status");
|
2018-05-31 20:23:57 +00:00
|
|
|
struct mpd_pair *pair;
|
|
|
|
while ((pair = mpd_recv_pair(mpd.conn)) != NULL) {
|
2018-08-15 15:23:47 +00:00
|
|
|
replaygain = strdup(pair->value);
|
2018-05-31 20:23:57 +00:00
|
|
|
mpd_return_pair(mpd.conn, pair);
|
|
|
|
}
|
2018-06-21 16:57:40 +00:00
|
|
|
|
2018-08-22 18:42:23 +00:00
|
|
|
len = json_printf(&out, "{type: settings, data: {"
|
2018-08-15 13:06:45 +00:00
|
|
|
"repeat: %d, single: %d, crossfade: %d, consume: %d, random: %d, "
|
2018-10-29 19:41:04 +00:00
|
|
|
"mixrampdb: %f, mixrampdelay: %f, mpdhost: %Q, mpdport: %d, passwort_set: %B, featSyscmds: %B, featPlaylists: %B, featTags: %B, featLibrary: %B, "
|
|
|
|
"featLocalplayer: %B, streamport: %d, streamurl: %Q, featCoverimage: %B, coverimagename: %Q, featStickers: %B, mixramp: %B, featSmartpls: %B, maxElementsPerPage: %d, "
|
2018-09-30 01:03:33 +00:00
|
|
|
"replaygain: %Q, notificationWeb: %B, notificationPage: %B, jukeboxMode: %d, jukeboxPlaylist: %Q, jukeboxQueueLength: %d, "
|
2018-10-09 21:51:47 +00:00
|
|
|
"tags: [",
|
2018-05-29 21:05:34 +00:00
|
|
|
mpd_status_get_repeat(status),
|
|
|
|
mpd_status_get_single(status),
|
|
|
|
mpd_status_get_crossfade(status),
|
|
|
|
mpd_status_get_consume(status),
|
|
|
|
mpd_status_get_random(status),
|
|
|
|
mpd_status_get_mixrampdb(status),
|
|
|
|
mpd_status_get_mixrampdelay(status),
|
2018-08-06 20:51:48 +00:00
|
|
|
config.mpdhost,
|
|
|
|
config.mpdport,
|
2018-08-21 19:28:13 +00:00
|
|
|
config.mpdpass ? "true" : "false",
|
2018-10-29 18:17:04 +00:00
|
|
|
config.syscmds,
|
2018-10-25 20:03:33 +00:00
|
|
|
mpd.feat_playlists,
|
|
|
|
mpd.feat_tags,
|
2018-10-29 19:41:04 +00:00
|
|
|
mpd.feat_library,
|
2018-10-29 18:17:04 +00:00
|
|
|
config.localplayer,
|
|
|
|
config.streamport,
|
|
|
|
config.streamurl,
|
2018-08-06 20:51:48 +00:00
|
|
|
config.coverimage,
|
2018-10-29 19:41:04 +00:00
|
|
|
config.coverimagename,
|
2018-08-21 19:28:13 +00:00
|
|
|
config.stickers,
|
|
|
|
config.mixramp,
|
2018-09-29 23:03:00 +00:00
|
|
|
config.smartpls,
|
2018-10-14 22:03:56 +00:00
|
|
|
config.max_elements_per_page,
|
2018-06-21 16:57:40 +00:00
|
|
|
replaygain,
|
2018-09-17 22:25:05 +00:00
|
|
|
mympd_state.notificationWeb,
|
|
|
|
mympd_state.notificationPage,
|
|
|
|
mympd_state.jukeboxMode,
|
2018-09-20 16:59:29 +00:00
|
|
|
mympd_state.jukeboxPlaylist,
|
2018-10-09 21:51:47 +00:00
|
|
|
mympd_state.jukeboxQueueLength
|
2018-05-29 21:05:34 +00:00
|
|
|
);
|
|
|
|
mpd_status_free(status);
|
2018-08-22 12:06:22 +00:00
|
|
|
free(replaygain);
|
2018-10-09 21:51:47 +00:00
|
|
|
|
2018-10-29 18:17:04 +00:00
|
|
|
nr = 0;
|
2018-10-09 21:51:47 +00:00
|
|
|
struct node *current = mympd_tags.list;
|
|
|
|
while (current != NULL) {
|
2018-10-18 21:55:45 +00:00
|
|
|
if (nr++)
|
2018-10-09 21:51:47 +00:00
|
|
|
len += json_printf(&out, ",");
|
|
|
|
len += json_printf(&out, "%Q", current->data);
|
|
|
|
current = current->next;
|
|
|
|
}
|
2018-10-29 18:17:04 +00:00
|
|
|
len += json_printf(&out, "], searchtags: [");
|
2018-10-15 22:34:25 +00:00
|
|
|
nr = 0;
|
2018-10-29 18:17:04 +00:00
|
|
|
current = mympd_searchtags.list;
|
|
|
|
while (current != NULL) {
|
|
|
|
if (nr++)
|
|
|
|
len += json_printf(&out, ",");
|
|
|
|
len += json_printf(&out, "%Q", current->data);
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
len += json_printf(&out, "], browsetags: [");
|
|
|
|
nr = 0;
|
|
|
|
current = mympd_browsetags.list;
|
2018-10-15 22:34:25 +00:00
|
|
|
while (current != NULL) {
|
2018-10-18 21:55:45 +00:00
|
|
|
if (nr++)
|
2018-10-15 22:34:25 +00:00
|
|
|
len += json_printf(&out, ",");
|
|
|
|
len += json_printf(&out, "%Q", current->data);
|
|
|
|
current = current->next;
|
|
|
|
}
|
2018-10-22 20:03:45 +00:00
|
|
|
len += json_printf(&out, "]");
|
2018-10-29 18:17:04 +00:00
|
|
|
|
|
|
|
if (config.syscmds == true) {
|
|
|
|
len += json_printf(&out, ", syscmds: [");
|
|
|
|
nr = 0;
|
|
|
|
current = syscmds.list;
|
|
|
|
while (current != NULL) {
|
|
|
|
if (nr++)
|
|
|
|
len += json_printf(&out, ",");
|
|
|
|
len += json_printf(&out, "%Q", current->data);
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
len += json_printf(&out, "]");
|
|
|
|
}
|
2018-11-14 23:14:47 +00:00
|
|
|
len += json_printf(&out, ", colsQueueCurrent: %s, colsSearch: %s, colsBrowseDatabase: %s, colsBrowsePlaylistsDetail: %s, "
|
|
|
|
"colsBrowseFilesystem: %s, colsPlayback: %s, colsQueueLastPlayed: %s}}",
|
|
|
|
mympd_state.colsQueueCurrent,
|
2018-11-12 23:48:29 +00:00
|
|
|
mympd_state.colsSearch,
|
|
|
|
mympd_state.colsBrowseDatabase,
|
|
|
|
mympd_state.colsBrowsePlaylistsDetail,
|
|
|
|
mympd_state.colsBrowseFilesystem,
|
|
|
|
mympd_state.colsPlayback,
|
2018-11-14 23:14:47 +00:00
|
|
|
mympd_state.colsQueueLastPlayed
|
2018-11-12 23:48:29 +00:00
|
|
|
);
|
2018-06-17 22:42:22 +00:00
|
|
|
|
2018-10-14 22:03:56 +00:00
|
|
|
CHECK_RETURN_LEN();
|
2018-05-29 21:05:34 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-06 18:57:42 +00:00
|
|
|
int mympd_put_outputs(char *buffer) {
|
2018-06-17 22:42:22 +00:00
|
|
|
struct mpd_output *output;
|
|
|
|
int len;
|
|
|
|
int nr;
|
|
|
|
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
|
2018-06-14 19:56:12 +00:00
|
|
|
|
2018-08-22 00:13:30 +00:00
|
|
|
if (!mpd_send_outputs(mpd.conn))
|
|
|
|
RETURN_ERROR_AND_RECOVER("outputs");
|
|
|
|
|
2018-08-22 18:42:23 +00:00
|
|
|
len = json_printf(&out, "{type: outputs, data: {outputs: [");
|
2018-08-22 00:13:30 +00:00
|
|
|
nr = 0;
|
2018-06-17 22:42:22 +00:00
|
|
|
while ((output = mpd_recv_output(mpd.conn)) != NULL) {
|
2018-10-18 21:55:45 +00:00
|
|
|
if (nr++)
|
2018-08-22 00:13:30 +00:00
|
|
|
len += json_printf(&out, ",");
|
2018-08-06 18:57:42 +00:00
|
|
|
len += json_printf(&out,"{id: %d, name: %Q, state: %d}",
|
2018-06-17 22:42:22 +00:00
|
|
|
mpd_output_get_id(output),
|
2018-08-06 18:57:42 +00:00
|
|
|
mpd_output_get_name(output),
|
|
|
|
mpd_output_get_enabled(output)
|
2018-06-17 22:42:22 +00:00
|
|
|
);
|
|
|
|
mpd_output_free(output);
|
2015-04-28 09:08:21 +00:00
|
|
|
}
|
2018-08-22 00:13:30 +00:00
|
|
|
if (!mpd_response_finish(mpd.conn))
|
|
|
|
LOG_ERROR_AND_RECOVER("outputs");
|
2018-06-14 19:56:12 +00:00
|
|
|
|
2018-06-28 22:51:24 +00:00
|
|
|
len += json_printf(&out,"]}}");
|
2018-06-14 19:56:12 +00:00
|
|
|
|
2018-10-14 22:03:56 +00:00
|
|
|
CHECK_RETURN_LEN();
|
2018-06-17 22:42:22 +00:00
|
|
|
return len;
|
2015-04-28 09:08:21 +00:00
|
|
|
}
|
|
|
|
|
2018-08-22 18:42:23 +00:00
|
|
|
int replacechar(char *str, char orig, char rep) {
|
|
|
|
char *ix = str;
|
|
|
|
int n = 0;
|
2018-10-11 21:30:50 +00:00
|
|
|
while ((ix = strchr(ix, orig)) != NULL) {
|
2018-08-22 18:42:23 +00:00
|
|
|
*ix++ = rep;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2018-07-10 23:06:31 +00:00
|
|
|
int mympd_get_cover(const char *uri, char *cover, int cover_len) {
|
2018-11-05 20:20:18 +00:00
|
|
|
char *orgpath = strdup(uri);
|
|
|
|
char *path = orgpath;
|
2018-06-22 00:01:15 +00:00
|
|
|
int len;
|
2018-10-29 19:41:04 +00:00
|
|
|
|
|
|
|
if (!config.coverimage) {
|
|
|
|
len = snprintf(cover, cover_len, "/assets/coverimage-notavailable.png");
|
|
|
|
}
|
|
|
|
else if (strncasecmp("http:", path, 5) == 0 || strncasecmp("https:", path, 6) == 0) {
|
2018-08-22 18:42:23 +00:00
|
|
|
if(strlen(path) > 8) {
|
|
|
|
if (strncasecmp("http:", path, 5) == 0)
|
|
|
|
path += 7;
|
|
|
|
else if (strncasecmp("https:", path, 6) == 0)
|
|
|
|
path += 8;
|
|
|
|
replacechar(path, '/', '_');
|
|
|
|
replacechar(path, '.', '_');
|
|
|
|
snprintf(cover, cover_len, "%s/pics/%s.png", SRC_PATH, path);
|
2018-10-29 19:41:04 +00:00
|
|
|
if (access(cover, F_OK ) == -1 )
|
2018-08-22 18:42:23 +00:00
|
|
|
len = snprintf(cover, cover_len, "/assets/coverimage-httpstream.png");
|
2018-10-29 19:41:04 +00:00
|
|
|
else
|
2018-08-22 18:42:23 +00:00
|
|
|
len = snprintf(cover, cover_len, "/pics/%s.png", path);
|
2018-10-29 19:41:04 +00:00
|
|
|
} else
|
2018-08-22 18:42:23 +00:00
|
|
|
len = snprintf(cover, cover_len, "/assets/coverimage-httpstream.png");
|
2018-06-22 00:01:15 +00:00
|
|
|
}
|
|
|
|
else {
|
2018-10-29 19:41:04 +00:00
|
|
|
if (mpd.feat_library) {
|
|
|
|
dirname(path);
|
|
|
|
snprintf(cover, cover_len, "%s/library/%s/%s", SRC_PATH, path, config.coverimagename);
|
|
|
|
if (access(cover, F_OK ) == -1 )
|
|
|
|
len = snprintf(cover, cover_len, "/assets/coverimage-notavailable.png");
|
|
|
|
else
|
|
|
|
len = snprintf(cover, cover_len, "/library/%s/%s", path, config.coverimagename);
|
|
|
|
} else
|
2018-08-21 19:28:13 +00:00
|
|
|
len = snprintf(cover, cover_len, "/assets/coverimage-notavailable.png");
|
2018-06-22 00:01:15 +00:00
|
|
|
}
|
2018-11-05 20:20:18 +00:00
|
|
|
free(orgpath);
|
2018-06-22 00:01:15 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2018-07-10 23:06:31 +00:00
|
|
|
int mympd_put_current_song(char *buffer) {
|
2013-11-09 01:07:03 +00:00
|
|
|
struct mpd_song *song;
|
2018-06-18 16:53:30 +00:00
|
|
|
int len;
|
|
|
|
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
|
2018-06-22 00:01:15 +00:00
|
|
|
char cover[500];
|
|
|
|
|
2014-02-16 18:46:53 +00:00
|
|
|
song = mpd_run_current_song(mpd.conn);
|
2018-07-22 19:00:26 +00:00
|
|
|
if (song == NULL) {
|
2018-08-22 18:42:23 +00:00
|
|
|
len = json_printf(&out, "{type: result, data: ok}");
|
2018-07-22 19:00:26 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2018-08-17 09:52:22 +00:00
|
|
|
mympd_get_cover(mpd_song_get_uri(song), cover, 500);
|
|
|
|
|
2018-10-18 21:33:11 +00:00
|
|
|
len = json_printf(&out, "{type: song_change, data: {pos: %d, currentSongId: %d, cover: %Q, ",
|
2018-06-18 16:53:30 +00:00
|
|
|
mpd_song_get_pos(song),
|
|
|
|
mpd.song_id,
|
2018-08-21 19:28:13 +00:00
|
|
|
cover
|
2018-06-18 16:53:30 +00:00
|
|
|
);
|
2018-10-25 20:03:33 +00:00
|
|
|
if (mpd.feat_tags == true)
|
|
|
|
PUT_SONG_TAGS();
|
|
|
|
else
|
|
|
|
PUT_MIN_SONG_TAGS();
|
2018-08-21 19:28:13 +00:00
|
|
|
|
|
|
|
mpd_response_finish(mpd.conn);
|
2018-06-07 22:26:35 +00:00
|
|
|
|
2018-08-21 19:28:13 +00:00
|
|
|
if (config.stickers) {
|
|
|
|
t_sticker *sticker = (t_sticker *) malloc(sizeof(t_sticker));
|
|
|
|
mympd_get_sticker(mpd_song_get_uri(song), sticker);
|
|
|
|
len += json_printf(&out, ", playCount: %d, skipCount: %d, like: %d, lastPlayed: %d",
|
|
|
|
sticker->playCount,
|
|
|
|
sticker->skipCount,
|
|
|
|
sticker->like,
|
|
|
|
sticker->lastPlayed
|
|
|
|
);
|
|
|
|
free(sticker);
|
|
|
|
}
|
|
|
|
len += json_printf(&out, "}}");
|
2013-11-09 01:07:03 +00:00
|
|
|
mpd_song_free(song);
|
|
|
|
|
2018-10-14 22:03:56 +00:00
|
|
|
CHECK_RETURN_LEN();
|
2018-06-18 16:53:30 +00:00
|
|
|
return len;
|
2013-11-04 17:18:38 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 23:06:31 +00:00
|
|
|
int mympd_put_songdetails(char *buffer, char *uri) {
|
2018-07-09 17:28:28 +00:00
|
|
|
struct mpd_entity *entity;
|
|
|
|
const struct mpd_song *song;
|
|
|
|
int len;
|
|
|
|
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
|
|
|
|
char cover[500];
|
|
|
|
|
|
|
|
len = json_printf(&out, "{type: song_details, data: {");
|
2018-10-18 21:55:45 +00:00
|
|
|
if (!mpd_send_list_all_meta(mpd.conn, uri))
|
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_send_list_all_meta");
|
2018-08-17 09:52:22 +00:00
|
|
|
if ((entity = mpd_recv_entity(mpd.conn)) != NULL) {
|
2018-07-09 17:28:28 +00:00
|
|
|
song = mpd_entity_get_song(entity);
|
2018-08-17 09:52:22 +00:00
|
|
|
mympd_get_cover(uri, cover, 500);
|
2018-10-18 21:33:11 +00:00
|
|
|
len += json_printf(&out, "cover: %Q, ", cover);
|
2018-10-25 20:03:33 +00:00
|
|
|
if (mpd.feat_tags == true)
|
|
|
|
PUT_SONG_TAGS();
|
|
|
|
else
|
|
|
|
PUT_MIN_SONG_TAGS();
|
2018-07-09 17:28:28 +00:00
|
|
|
mpd_entity_free(entity);
|
|
|
|
}
|
2018-08-17 09:52:22 +00:00
|
|
|
mpd_response_finish(mpd.conn);
|
|
|
|
|
2018-08-21 19:28:13 +00:00
|
|
|
if (config.stickers) {
|
|
|
|
t_sticker *sticker = (t_sticker *) malloc(sizeof(t_sticker));
|
|
|
|
mympd_get_sticker(uri, sticker);
|
|
|
|
len += json_printf(&out, ", playCount: %d, skipCount: %d, like: %d, lastPlayed: %d",
|
|
|
|
sticker->playCount,
|
|
|
|
sticker->skipCount,
|
|
|
|
sticker->like,
|
|
|
|
sticker->lastPlayed
|
|
|
|
);
|
|
|
|
free(sticker);
|
|
|
|
}
|
2018-07-09 17:28:28 +00:00
|
|
|
len += json_printf(&out, "}}");
|
|
|
|
|
2018-10-14 22:03:56 +00:00
|
|
|
CHECK_RETURN_LEN();
|
2018-07-09 17:28:28 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2018-11-12 23:48:29 +00:00
|
|
|
int mympd_put_last_played_songs(char *buffer, unsigned int offset) {
|
|
|
|
const struct mpd_song *song;
|
|
|
|
struct mpd_entity *entity;
|
|
|
|
int len;
|
|
|
|
unsigned int entity_count = 0;
|
|
|
|
unsigned int entities_returned = 0;
|
|
|
|
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
|
|
|
|
|
|
|
|
len = json_printf(&out, "{type: last_played_songs, data: [");
|
|
|
|
|
|
|
|
struct node *current = last_played.list;
|
|
|
|
while (current != NULL) {
|
|
|
|
entity_count++;
|
|
|
|
if (entity_count > offset && entity_count <= offset + config.max_elements_per_page) {
|
|
|
|
if (entities_returned++)
|
|
|
|
len += json_printf(&out, ",");
|
2018-11-13 23:50:30 +00:00
|
|
|
len += json_printf(&out, "{Pos: %d, LastPlayed: %ld, ", entity_count, current->value);
|
2018-11-12 23:48:29 +00:00
|
|
|
if (!mpd_send_list_all_meta(mpd.conn, current->data))
|
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_send_list_all_meta");
|
|
|
|
if ((entity = mpd_recv_entity(mpd.conn)) != NULL) {
|
|
|
|
song = mpd_entity_get_song(entity);
|
|
|
|
if (mpd.feat_tags == true)
|
|
|
|
PUT_SONG_TAGS();
|
|
|
|
else
|
|
|
|
PUT_MIN_SONG_TAGS();
|
|
|
|
mpd_entity_free(entity);
|
|
|
|
mpd_response_finish(mpd.conn);
|
|
|
|
}
|
|
|
|
len += json_printf(&out, "}");
|
|
|
|
}
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
len += json_printf(&out, "], totalEntities: %d, offset: %d, returnedEntities: %d}",
|
|
|
|
entity_count,
|
|
|
|
offset,
|
|
|
|
entities_returned
|
|
|
|
);
|
|
|
|
|
|
|
|
CHECK_RETURN_LEN();
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-08-27 19:34:21 +00:00
|
|
|
int mympd_put_queue(char *buffer, unsigned int offset, unsigned *queue_version, unsigned *queue_length) {
|
2013-11-09 01:07:03 +00:00
|
|
|
struct mpd_entity *entity;
|
2018-04-04 13:04:25 +00:00
|
|
|
unsigned long totalTime = 0;
|
2018-05-29 21:55:24 +00:00
|
|
|
unsigned long entity_count = 0;
|
|
|
|
unsigned long entities_returned = 0;
|
2018-06-18 16:53:30 +00:00
|
|
|
int len;
|
|
|
|
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
|
2018-08-27 19:34:21 +00:00
|
|
|
struct mpd_status *status = mpd_run_status(mpd.conn);
|
|
|
|
|
|
|
|
if (!status)
|
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_run_status");
|
|
|
|
|
2018-10-14 22:03:56 +00:00
|
|
|
if (!mpd_send_list_queue_range_meta(mpd.conn, offset, offset + config.max_elements_per_page))
|
2014-02-22 00:57:26 +00:00
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_send_list_queue_meta");
|
2018-05-01 21:37:34 +00:00
|
|
|
|
2018-08-22 00:13:30 +00:00
|
|
|
len = json_printf(&out, "{type: queue, data: [");
|
2013-11-04 17:18:38 +00:00
|
|
|
|
2018-06-18 16:53:30 +00:00
|
|
|
while ((entity = mpd_recv_entity(mpd.conn)) != NULL) {
|
2013-11-07 09:09:40 +00:00
|
|
|
const struct mpd_song *song;
|
2018-04-04 13:04:25 +00:00
|
|
|
unsigned int drtn;
|
2018-06-18 16:53:30 +00:00
|
|
|
if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
|
2013-11-09 01:07:03 +00:00
|
|
|
song = mpd_entity_get_song(entity);
|
2018-04-04 13:04:25 +00:00
|
|
|
drtn = mpd_song_get_duration(song);
|
|
|
|
totalTime += drtn;
|
2018-10-18 21:55:45 +00:00
|
|
|
entity_count++;
|
|
|
|
if (entities_returned++)
|
2018-08-22 00:13:30 +00:00
|
|
|
len += json_printf(&out, ",");
|
2018-10-22 20:03:45 +00:00
|
|
|
len += json_printf(&out, "{id: %d, Pos: %d, ",
|
2018-08-21 22:56:17 +00:00
|
|
|
mpd_song_get_id(song),
|
2018-10-18 21:33:11 +00:00
|
|
|
mpd_song_get_pos(song)
|
2018-08-21 22:56:17 +00:00
|
|
|
);
|
2018-10-25 20:03:33 +00:00
|
|
|
if (mpd.feat_tags == true)
|
|
|
|
PUT_SONG_TAGS();
|
|
|
|
else
|
|
|
|
PUT_MIN_SONG_TAGS();
|
2018-10-17 22:04:06 +00:00
|
|
|
len += json_printf(&out, "}");
|
2013-11-09 01:07:03 +00:00
|
|
|
}
|
|
|
|
mpd_entity_free(entity);
|
|
|
|
}
|
|
|
|
|
2018-08-22 00:13:30 +00:00
|
|
|
len += json_printf(&out, "], totalTime: %d, totalEntities: %d, offset: %d, returnedEntities: %d, queue_version: %d}",
|
2018-06-18 16:53:30 +00:00
|
|
|
totalTime,
|
2018-08-27 19:34:21 +00:00
|
|
|
mpd_status_get_queue_length(status),
|
2018-06-18 16:53:30 +00:00
|
|
|
offset,
|
2018-07-15 21:49:57 +00:00
|
|
|
entities_returned,
|
2018-08-27 19:34:21 +00:00
|
|
|
mpd_status_get_queue_version(status)
|
2018-06-18 16:53:30 +00:00
|
|
|
);
|
2018-08-27 19:34:21 +00:00
|
|
|
|
|
|
|
*queue_version = mpd_status_get_queue_version(status);
|
|
|
|
*queue_length = mpd_status_get_queue_length(status);
|
|
|
|
mpd_status_free(status);
|
2018-06-18 16:53:30 +00:00
|
|
|
|
2018-10-14 22:03:56 +00:00
|
|
|
CHECK_RETURN_LEN();
|
2018-06-18 16:53:30 +00:00
|
|
|
return len;
|
2013-11-07 09:09:40 +00:00
|
|
|
}
|
2013-11-04 17:18:38 +00:00
|
|
|
|
2018-07-10 23:06:31 +00:00
|
|
|
int mympd_put_browse(char *buffer, char *path, unsigned int offset, char *filter) {
|
2013-11-09 01:07:03 +00:00
|
|
|
struct mpd_entity *entity;
|
2018-06-03 05:17:30 +00:00
|
|
|
const struct mpd_playlist *pl;
|
2014-02-22 01:11:45 +00:00
|
|
|
unsigned int entity_count = 0;
|
2018-05-29 21:55:24 +00:00
|
|
|
unsigned int entities_returned = 0;
|
2018-06-03 08:52:38 +00:00
|
|
|
const char *entityName;
|
2018-09-29 23:03:00 +00:00
|
|
|
char smartpls_file[400];
|
|
|
|
bool smartpls;
|
2018-06-18 16:53:30 +00:00
|
|
|
int len;
|
|
|
|
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
|
2013-11-09 01:07:03 +00:00
|
|
|
|
2014-02-22 00:57:26 +00:00
|
|
|
if (!mpd_send_list_meta(mpd.conn, path))
|
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_send_list_meta");
|
2014-01-16 17:32:20 +00:00
|
|
|
|
2018-08-22 00:13:30 +00:00
|
|
|
len = json_printf(&out, "{type: browse, data: [");
|
2013-11-07 09:09:40 +00:00
|
|
|
|
2018-10-11 21:30:50 +00:00
|
|
|
while ((entity = mpd_recv_entity(mpd.conn)) != NULL) {
|
2013-11-07 09:09:40 +00:00
|
|
|
const struct mpd_song *song;
|
|
|
|
const struct mpd_directory *dir;
|
2018-10-18 21:55:45 +00:00
|
|
|
entity_count++;
|
2018-10-14 22:03:56 +00:00
|
|
|
if (entity_count > offset && entity_count <= offset + config.max_elements_per_page) {
|
2018-05-29 20:12:11 +00:00
|
|
|
switch (mpd_entity_get_type(entity)) {
|
|
|
|
case MPD_ENTITY_TYPE_UNKNOWN:
|
2018-10-18 21:55:45 +00:00
|
|
|
entity_count--;
|
2018-05-29 20:12:11 +00:00
|
|
|
break;
|
|
|
|
case MPD_ENTITY_TYPE_SONG:
|
|
|
|
song = mpd_entity_get_song(entity);
|
2018-07-09 17:49:55 +00:00
|
|
|
entityName = mympd_get_tag(song, MPD_TAG_TITLE);
|
2018-08-22 00:13:30 +00:00
|
|
|
if (strncmp(filter, "-", 1) == 0 || strncasecmp(filter, entityName, 1) == 0 ||
|
|
|
|
(strncmp(filter, "0", 1) == 0 && isalpha(*entityName) == 0 )
|
2018-06-03 08:52:38 +00:00
|
|
|
) {
|
2018-10-18 21:55:45 +00:00
|
|
|
if (entities_returned++)
|
2018-08-22 00:13:30 +00:00
|
|
|
len += json_printf(&out, ",");
|
2018-10-22 20:03:45 +00:00
|
|
|
len += json_printf(&out, "{Type: song, ");
|
2018-10-25 20:03:33 +00:00
|
|
|
if (mpd.feat_tags == true)
|
|
|
|
PUT_SONG_TAGS();
|
|
|
|
else
|
|
|
|
PUT_MIN_SONG_TAGS();
|
|
|
|
|
2018-10-17 22:04:06 +00:00
|
|
|
len += json_printf(&out, "}");
|
2018-06-03 08:52:38 +00:00
|
|
|
} else {
|
2018-10-18 21:55:45 +00:00
|
|
|
entity_count--;
|
2018-06-03 08:52:38 +00:00
|
|
|
}
|
2018-05-29 20:12:11 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MPD_ENTITY_TYPE_DIRECTORY:
|
2018-06-03 08:52:38 +00:00
|
|
|
dir = mpd_entity_get_directory(entity);
|
|
|
|
entityName = mpd_directory_get_path(dir);
|
|
|
|
char *dirName = strrchr(entityName, '/');
|
2018-08-22 00:13:30 +00:00
|
|
|
|
|
|
|
if (dirName != NULL)
|
2018-10-18 21:55:45 +00:00
|
|
|
dirName++;
|
2018-08-22 00:13:30 +00:00
|
|
|
else
|
2018-06-18 16:53:30 +00:00
|
|
|
dirName = strdup(entityName);
|
2018-08-22 00:13:30 +00:00
|
|
|
|
|
|
|
if (strncmp(filter, "-", 1) == 0 || strncasecmp(filter, dirName, 1) == 0 ||
|
|
|
|
(strncmp(filter, "0", 1) == 0 && isalpha(*dirName) == 0 )
|
2018-06-03 08:55:01 +00:00
|
|
|
) {
|
2018-10-18 21:55:45 +00:00
|
|
|
if (entities_returned++)
|
2018-08-22 00:13:30 +00:00
|
|
|
len += json_printf(&out, ",");
|
2018-10-22 20:03:45 +00:00
|
|
|
len += json_printf(&out, "{Type: dir, uri: %Q, name: %Q}",
|
2018-06-25 22:41:44 +00:00
|
|
|
entityName,
|
|
|
|
dirName
|
2018-06-18 16:53:30 +00:00
|
|
|
);
|
2018-06-03 08:52:38 +00:00
|
|
|
} else {
|
2018-10-18 21:55:45 +00:00
|
|
|
entity_count--;
|
2018-06-03 08:52:38 +00:00
|
|
|
}
|
2018-05-29 20:12:11 +00:00
|
|
|
break;
|
2018-06-03 05:17:30 +00:00
|
|
|
|
2018-05-29 20:12:11 +00:00
|
|
|
case MPD_ENTITY_TYPE_PLAYLIST:
|
2018-06-03 05:17:30 +00:00
|
|
|
pl = mpd_entity_get_playlist(entity);
|
2018-06-03 08:52:38 +00:00
|
|
|
entityName = mpd_playlist_get_path(pl);
|
2018-06-03 10:50:50 +00:00
|
|
|
char *plName = strrchr(entityName, '/');
|
|
|
|
if (plName != NULL) {
|
2018-10-18 21:55:45 +00:00
|
|
|
plName++;
|
2018-06-03 10:50:50 +00:00
|
|
|
} else {
|
2018-07-10 23:06:31 +00:00
|
|
|
plName = strdup(entityName);
|
2018-06-03 10:50:50 +00:00
|
|
|
}
|
2018-08-22 00:13:30 +00:00
|
|
|
if (strncmp(filter, "-", 1) == 0 || strncasecmp(filter, plName, 1) == 0 ||
|
|
|
|
(strncmp(filter, "0", 1) == 0 && isalpha(*plName) == 0 )
|
2018-06-03 08:55:01 +00:00
|
|
|
) {
|
2018-10-18 21:55:45 +00:00
|
|
|
if (entities_returned++)
|
2018-08-22 00:13:30 +00:00
|
|
|
len += json_printf(&out, ",");
|
2018-09-29 23:03:00 +00:00
|
|
|
snprintf(smartpls_file, 400, "%s/smartpls/%s", config.varlibdir, plName);
|
|
|
|
if (access(smartpls_file, F_OK ) != -1)
|
|
|
|
smartpls = true;
|
|
|
|
else
|
|
|
|
smartpls = false;
|
2018-10-22 20:03:45 +00:00
|
|
|
len += json_printf(&out, "{Type: %Q, uri: %Q, name: %Q}",
|
2018-09-29 23:03:00 +00:00
|
|
|
(smartpls == true ? "smartpls" : "plist"),
|
2018-06-25 22:41:44 +00:00
|
|
|
entityName,
|
|
|
|
plName
|
2018-06-18 16:53:30 +00:00
|
|
|
);
|
2018-06-03 08:52:38 +00:00
|
|
|
} else {
|
2018-10-18 21:55:45 +00:00
|
|
|
entity_count--;
|
2018-06-03 08:52:38 +00:00
|
|
|
}
|
2018-05-29 20:12:11 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-11-07 09:09:40 +00:00
|
|
|
}
|
2013-11-09 01:07:03 +00:00
|
|
|
mpd_entity_free(entity);
|
|
|
|
}
|
2013-11-04 17:18:38 +00:00
|
|
|
|
2014-02-16 18:46:53 +00:00
|
|
|
if (mpd_connection_get_error(mpd.conn) != MPD_ERROR_SUCCESS || !mpd_response_finish(mpd.conn)) {
|
2018-08-23 09:27:12 +00:00
|
|
|
printf("MPD mpd_send_list_meta: %s\n", mpd_connection_get_error_message(mpd.conn));
|
2014-02-16 18:46:53 +00:00
|
|
|
mpd.conn_state = MPD_FAILURE;
|
2013-11-07 09:09:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-22 00:13:30 +00:00
|
|
|
len += json_printf(&out, "], totalEntities: %d, offset: %d, returnedEntities: %d, filter: %Q}",
|
2018-06-18 16:53:30 +00:00
|
|
|
entity_count,
|
|
|
|
offset,
|
|
|
|
entities_returned,
|
|
|
|
filter
|
|
|
|
);
|
|
|
|
|
2018-10-14 22:03:56 +00:00
|
|
|
CHECK_RETURN_LEN();
|
2018-06-18 16:53:30 +00:00
|
|
|
return len;
|
2013-11-05 13:59:12 +00:00
|
|
|
}
|
2014-02-16 18:46:53 +00:00
|
|
|
|
2018-07-10 23:06:31 +00:00
|
|
|
int mympd_put_db_tag(char *buffer, unsigned int offset, char *mpdtagtype, char *mpdsearchtagtype, char *searchstr, char *filter) {
|
2018-06-03 16:36:06 +00:00
|
|
|
struct mpd_pair *pair;
|
|
|
|
unsigned long entity_count = 0;
|
|
|
|
unsigned long entities_returned = 0;
|
2018-06-18 16:53:30 +00:00
|
|
|
int len;
|
|
|
|
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
|
2018-06-03 16:36:06 +00:00
|
|
|
|
2018-07-24 22:58:29 +00:00
|
|
|
if (mpd_search_db_tags(mpd.conn, mpd_tag_name_parse(mpdtagtype)) == false)
|
2018-06-03 16:36:06 +00:00
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_search_db_tags");
|
|
|
|
|
|
|
|
if (mpd_tag_name_parse(mpdsearchtagtype) != MPD_TAG_UNKNOWN) {
|
|
|
|
if (mpd_search_add_tag_constraint(mpd.conn, MPD_OPERATOR_DEFAULT, mpd_tag_name_parse(mpdsearchtagtype), searchstr) == false)
|
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_search_add_tag_constraint");
|
|
|
|
}
|
|
|
|
|
2018-07-24 22:58:29 +00:00
|
|
|
if (mpd_search_commit(mpd.conn) == false)
|
2018-06-03 16:36:06 +00:00
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_search_commit");
|
2018-06-04 18:31:05 +00:00
|
|
|
|
2018-08-22 00:13:30 +00:00
|
|
|
len = json_printf(&out, "{type: listDBtags, data: [");
|
2018-10-11 21:30:50 +00:00
|
|
|
while ((pair = mpd_recv_pair_tag(mpd.conn, mpd_tag_name_parse(mpdtagtype))) != NULL) {
|
2018-10-18 21:55:45 +00:00
|
|
|
entity_count++;
|
2018-10-14 22:03:56 +00:00
|
|
|
if (entity_count > offset && entity_count <= offset + config.max_elements_per_page) {
|
2018-09-17 15:54:59 +00:00
|
|
|
if (strcmp(pair->value, "") == 0) {
|
2018-10-18 21:55:45 +00:00
|
|
|
entity_count--;
|
2018-09-17 15:54:59 +00:00
|
|
|
}
|
|
|
|
else if (strncmp(filter, "-", 1) == 0 || strncasecmp(filter, pair->value, 1) == 0 ||
|
2018-08-22 00:13:30 +00:00
|
|
|
(strncmp(filter, "0", 1) == 0 && isalpha(*pair->value) == 0 )
|
2018-06-04 18:31:05 +00:00
|
|
|
) {
|
2018-10-18 21:55:45 +00:00
|
|
|
if (entities_returned++)
|
2018-08-22 00:13:30 +00:00
|
|
|
len += json_printf(&out, ", ");
|
|
|
|
len += json_printf(&out, "{type: %Q, value: %Q}",
|
2018-06-18 16:53:30 +00:00
|
|
|
mpdtagtype,
|
|
|
|
pair->value
|
|
|
|
);
|
2018-06-04 18:31:05 +00:00
|
|
|
} else {
|
2018-10-18 21:55:45 +00:00
|
|
|
entity_count--;
|
2018-06-03 16:36:06 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-04 18:31:05 +00:00
|
|
|
mpd_return_pair(mpd.conn, pair);
|
|
|
|
}
|
2018-06-03 16:36:06 +00:00
|
|
|
|
2018-06-18 16:53:30 +00:00
|
|
|
len += json_printf(&out, "], totalEntities: %d, offset: %d, returnedEntities: %d, "
|
2018-08-22 00:13:30 +00:00
|
|
|
"tagtype: %Q, searchtagtype: %Q, searchstr: %Q, filter: %Q}",
|
2018-06-18 16:53:30 +00:00
|
|
|
entity_count,
|
|
|
|
offset,
|
|
|
|
entities_returned,
|
|
|
|
mpdtagtype,
|
|
|
|
mpdsearchtagtype,
|
|
|
|
searchstr,
|
|
|
|
filter
|
|
|
|
);
|
|
|
|
|
2018-10-14 22:03:56 +00:00
|
|
|
CHECK_RETURN_LEN();
|
2018-06-18 16:53:30 +00:00
|
|
|
return len;
|
2018-06-03 16:36:06 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 20:32:15 +00:00
|
|
|
int mympd_put_songs_in_album(char *buffer, char *album, char *search, char *tag) {
|
2018-06-03 16:36:06 +00:00
|
|
|
struct mpd_song *song;
|
|
|
|
unsigned long entity_count = 0;
|
|
|
|
unsigned long entities_returned = 0;
|
2018-06-18 16:53:30 +00:00
|
|
|
int len;
|
2018-06-22 00:01:15 +00:00
|
|
|
char cover[500];
|
2018-09-11 18:59:22 +00:00
|
|
|
char *albumartist;
|
2018-06-18 16:53:30 +00:00
|
|
|
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
|
2018-06-03 16:36:06 +00:00
|
|
|
|
2018-08-06 21:29:20 +00:00
|
|
|
if (mpd_search_db_songs(mpd.conn, true) == false)
|
2018-06-03 16:36:06 +00:00
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_search_db_songs");
|
|
|
|
|
2018-09-10 20:32:15 +00:00
|
|
|
if (mpd_search_add_tag_constraint(mpd.conn, MPD_OPERATOR_DEFAULT, mpd_tag_name_parse(tag), search) == false)
|
2018-06-03 16:36:06 +00:00
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_search_add_tag_constraint");
|
|
|
|
|
|
|
|
if (mpd_search_add_tag_constraint(mpd.conn, MPD_OPERATOR_DEFAULT, MPD_TAG_ALBUM, album) == false)
|
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_search_add_tag_constraint");
|
|
|
|
|
2018-07-24 22:58:29 +00:00
|
|
|
if (mpd_search_commit(mpd.conn) == false)
|
2018-06-03 16:36:06 +00:00
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_search_commit");
|
|
|
|
else {
|
2018-08-22 00:13:30 +00:00
|
|
|
len = json_printf(&out, "{type: listTitles, data: [");
|
2018-06-03 16:36:06 +00:00
|
|
|
|
2018-10-11 21:30:50 +00:00
|
|
|
while ((song = mpd_recv_song(mpd.conn)) != NULL) {
|
2018-10-18 21:55:45 +00:00
|
|
|
entity_count++;
|
2018-10-14 22:03:56 +00:00
|
|
|
if (entity_count <= config.max_elements_per_page) {
|
2018-10-18 21:55:45 +00:00
|
|
|
if (entities_returned++)
|
2018-08-22 00:13:30 +00:00
|
|
|
len += json_printf(&out, ", ");
|
2018-09-11 18:59:22 +00:00
|
|
|
else {
|
2018-08-22 00:13:30 +00:00
|
|
|
mympd_get_cover(mpd_song_get_uri(song), cover, 500);
|
2018-09-11 18:59:22 +00:00
|
|
|
albumartist = strdup(mympd_get_tag(song, MPD_TAG_ALBUM_ARTIST));
|
|
|
|
}
|
2018-11-04 18:34:46 +00:00
|
|
|
len += json_printf(&out, "{Type: song, ");
|
|
|
|
PUT_SONG_TAGS();
|
|
|
|
len += json_printf(&out, "}");
|
2018-06-03 16:36:06 +00:00
|
|
|
}
|
|
|
|
mpd_song_free(song);
|
|
|
|
}
|
|
|
|
|
2018-10-17 22:04:06 +00:00
|
|
|
len += json_printf(&out, "], totalEntities: %d, returnedEntities: %d, Album: %Q, search: %Q, tag: %Q, cover: %Q, AlbumArtist: %Q}",
|
2018-06-18 16:53:30 +00:00
|
|
|
entity_count,
|
|
|
|
entities_returned,
|
2018-06-22 00:01:15 +00:00
|
|
|
album,
|
2018-09-10 20:32:15 +00:00
|
|
|
search,
|
|
|
|
tag,
|
2018-09-11 18:59:22 +00:00
|
|
|
cover,
|
|
|
|
albumartist
|
2018-06-18 16:53:30 +00:00
|
|
|
);
|
2018-06-03 16:36:06 +00:00
|
|
|
}
|
2018-09-11 18:59:22 +00:00
|
|
|
free(albumartist);
|
2018-06-18 16:53:30 +00:00
|
|
|
|
2018-10-14 22:03:56 +00:00
|
|
|
CHECK_RETURN_LEN();
|
2018-06-18 16:53:30 +00:00
|
|
|
return len;
|
2018-06-03 16:36:06 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 23:06:31 +00:00
|
|
|
int mympd_put_playlists(char *buffer, unsigned int offset, char *filter) {
|
2018-05-28 22:05:56 +00:00
|
|
|
struct mpd_playlist *pl;
|
|
|
|
unsigned int entity_count = 0;
|
2018-05-29 21:55:24 +00:00
|
|
|
unsigned int entities_returned = 0;
|
2018-06-04 18:31:05 +00:00
|
|
|
const char *plpath;
|
2018-06-18 16:53:30 +00:00
|
|
|
int len;
|
2018-09-29 23:03:00 +00:00
|
|
|
bool smartpls;
|
|
|
|
char smartpls_file[400];
|
2018-06-18 16:53:30 +00:00
|
|
|
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
|
2018-05-28 22:05:56 +00:00
|
|
|
|
|
|
|
if (!mpd_send_list_playlists(mpd.conn))
|
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_send_lists_playlists");
|
|
|
|
|
2018-08-22 00:13:30 +00:00
|
|
|
len = json_printf(&out, "{type: playlists, data: [");
|
2018-05-28 22:05:56 +00:00
|
|
|
|
2018-10-11 21:30:50 +00:00
|
|
|
while ((pl = mpd_recv_playlist(mpd.conn)) != NULL) {
|
2018-10-18 21:55:45 +00:00
|
|
|
entity_count++;
|
2018-10-14 22:03:56 +00:00
|
|
|
if (entity_count > offset && entity_count <= offset + config.max_elements_per_page) {
|
2018-06-04 18:31:05 +00:00
|
|
|
plpath = mpd_playlist_get_path(pl);
|
2018-08-21 22:56:17 +00:00
|
|
|
if (strncmp(filter,"-",1) == 0 || strncasecmp(filter, plpath, 1) == 0 ||
|
|
|
|
(strncmp(filter, "0", 1) == 0 && isalpha(*plpath) == 0 )
|
2018-06-04 18:31:05 +00:00
|
|
|
) {
|
2018-10-18 21:55:45 +00:00
|
|
|
if (entities_returned++)
|
2018-08-22 00:13:30 +00:00
|
|
|
len += json_printf(&out, ", ");
|
2018-09-29 23:03:00 +00:00
|
|
|
snprintf(smartpls_file, 400, "%s/smartpls/%s", config.varlibdir, plpath);
|
|
|
|
if (access(smartpls_file, F_OK ) != -1)
|
|
|
|
smartpls = true;
|
|
|
|
else
|
|
|
|
smartpls = false;
|
2018-10-22 22:20:57 +00:00
|
|
|
len += json_printf(&out, "{Type: %Q, uri: %Q, name: %Q, last_modified: %d}",
|
2018-09-29 23:03:00 +00:00
|
|
|
(smartpls == true ? "smartpls" : "plist"),
|
2018-06-25 22:41:44 +00:00
|
|
|
plpath,
|
2018-06-18 16:53:30 +00:00
|
|
|
plpath,
|
|
|
|
mpd_playlist_get_last_modified(pl)
|
|
|
|
);
|
2018-06-04 18:31:05 +00:00
|
|
|
} else {
|
2018-10-18 21:55:45 +00:00
|
|
|
entity_count--;
|
2018-06-04 18:31:05 +00:00
|
|
|
}
|
2018-05-28 22:05:56 +00:00
|
|
|
}
|
|
|
|
mpd_playlist_free(pl);
|
|
|
|
}
|
|
|
|
|
2018-08-06 21:29:20 +00:00
|
|
|
len += json_printf(&out, "], totalEntities: %d, offset: %d, returnedEntities: %d}",
|
2018-06-18 16:53:30 +00:00
|
|
|
entity_count,
|
|
|
|
offset,
|
|
|
|
entities_returned
|
|
|
|
);
|
|
|
|
|
2018-10-14 22:03:56 +00:00
|
|
|
CHECK_RETURN_LEN();
|
2018-06-18 16:53:30 +00:00
|
|
|
return len;
|
2018-05-28 22:05:56 +00:00
|
|
|
}
|
|
|
|
|
2018-07-22 19:00:26 +00:00
|
|
|
int mympd_put_playlist_list(char *buffer, char *uri, unsigned int offset, char *filter) {
|
|
|
|
struct mpd_entity *entity;
|
|
|
|
unsigned int entity_count = 0;
|
|
|
|
unsigned int entities_returned = 0;
|
|
|
|
const char *entityName;
|
2018-09-29 23:03:00 +00:00
|
|
|
char smartpls_file[400];
|
|
|
|
bool smartpls;
|
2018-07-22 19:00:26 +00:00
|
|
|
int len;
|
|
|
|
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
|
|
|
|
|
|
|
|
if (!mpd_send_list_playlist_meta(mpd.conn, uri))
|
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_send_list_meta");
|
|
|
|
|
2018-08-22 00:13:30 +00:00
|
|
|
len = json_printf(&out, "{type: playlist_detail, data: [");
|
2018-07-22 19:00:26 +00:00
|
|
|
|
2018-10-11 21:30:50 +00:00
|
|
|
while ((entity = mpd_recv_entity(mpd.conn)) != NULL) {
|
2018-07-22 19:00:26 +00:00
|
|
|
const struct mpd_song *song;
|
2018-10-18 21:55:45 +00:00
|
|
|
entity_count++;
|
2018-10-14 22:03:56 +00:00
|
|
|
if (entity_count > offset && entity_count <= offset + config.max_elements_per_page) {
|
2018-07-22 19:00:26 +00:00
|
|
|
song = mpd_entity_get_song(entity);
|
|
|
|
entityName = mympd_get_tag(song, MPD_TAG_TITLE);
|
2018-08-21 22:56:17 +00:00
|
|
|
if (strncmp(filter, "-", 1) == 0 || strncasecmp(filter, entityName, 1) == 0 ||
|
2018-08-22 00:13:30 +00:00
|
|
|
(strncmp(filter, "0", 1) == 0 && isalpha(*entityName) == 0 )
|
2018-07-22 19:00:26 +00:00
|
|
|
) {
|
2018-10-18 21:55:45 +00:00
|
|
|
if (entities_returned++)
|
2018-08-22 00:13:30 +00:00
|
|
|
len += json_printf(&out, ",");
|
2018-10-22 22:20:57 +00:00
|
|
|
len += json_printf(&out, "{Type: song, ");
|
2018-10-25 20:03:33 +00:00
|
|
|
if (mpd.feat_tags == true)
|
|
|
|
PUT_SONG_TAGS();
|
|
|
|
else
|
|
|
|
PUT_MIN_SONG_TAGS();
|
2018-10-22 22:20:57 +00:00
|
|
|
len += json_printf(&out, ", Pos: %d", entity_count);
|
2018-10-17 22:04:06 +00:00
|
|
|
len += json_printf(&out, "}");
|
2018-07-22 19:00:26 +00:00
|
|
|
} else {
|
2018-10-18 21:55:45 +00:00
|
|
|
entity_count--;
|
2018-07-22 19:00:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mpd_entity_free(entity);
|
|
|
|
}
|
2018-09-29 23:03:00 +00:00
|
|
|
snprintf(smartpls_file, 400, "%s/smartpls/%s", config.varlibdir, uri);
|
|
|
|
if (access(smartpls_file, F_OK ) != -1)
|
|
|
|
smartpls = true;
|
|
|
|
else
|
|
|
|
smartpls = false;
|
|
|
|
len += json_printf(&out, "], totalEntities: %d, offset: %d, returnedEntities: %d, filter: %Q, uri: %Q, smartpls: %B}",
|
2018-07-22 19:00:26 +00:00
|
|
|
entity_count,
|
|
|
|
offset,
|
|
|
|
entities_returned,
|
|
|
|
filter,
|
2018-09-29 23:03:00 +00:00
|
|
|
uri,
|
|
|
|
smartpls
|
2018-07-22 19:00:26 +00:00
|
|
|
);
|
|
|
|
|
2018-10-14 22:03:56 +00:00
|
|
|
CHECK_RETURN_LEN();
|
2018-07-22 19:00:26 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2018-09-03 18:02:12 +00:00
|
|
|
int mympd_search(char *buffer, char *searchstr, char *filter, char *plist, unsigned int offset) {
|
2014-02-22 00:57:26 +00:00
|
|
|
struct mpd_song *song;
|
2018-05-29 20:12:11 +00:00
|
|
|
unsigned long entity_count = 0;
|
|
|
|
unsigned long entities_returned = 0;
|
2018-09-03 20:01:50 +00:00
|
|
|
int len = 0;
|
2018-06-18 16:53:30 +00:00
|
|
|
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
|
2018-05-29 20:12:11 +00:00
|
|
|
|
2018-09-03 18:02:12 +00:00
|
|
|
if (strcmp(plist, "") == 0) {
|
|
|
|
if (mpd_send_command(mpd.conn, "search", filter, searchstr, NULL) == false)
|
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_search");
|
|
|
|
len = json_printf(&out, "{type: search, data: [");
|
|
|
|
}
|
|
|
|
else if (strcmp(plist, "queue") == 0) {
|
|
|
|
if (mpd_send_command(mpd.conn, "searchadd", filter, searchstr, NULL) == false)
|
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_searchadd");
|
2018-05-29 20:12:11 +00:00
|
|
|
}
|
|
|
|
else {
|
2018-09-03 18:02:12 +00:00
|
|
|
if (mpd_send_command(mpd.conn, "searchaddpl", plist, filter, searchstr, NULL) == false)
|
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_searchaddpl");
|
|
|
|
}
|
2018-11-19 22:37:08 +00:00
|
|
|
|
|
|
|
if (strcmp(plist, "") == 0) {
|
|
|
|
while ((song = mpd_recv_song(mpd.conn)) != NULL) {
|
|
|
|
entity_count++;
|
|
|
|
if (entity_count > offset && entity_count <= offset + config.max_elements_per_page) {
|
|
|
|
if (entities_returned++)
|
|
|
|
len += json_printf(&out, ", ");
|
|
|
|
len += json_printf(&out, "{Type: song, ");
|
|
|
|
if (mpd.feat_tags == true)
|
|
|
|
PUT_SONG_TAGS();
|
|
|
|
else
|
|
|
|
PUT_MIN_SONG_TAGS();
|
|
|
|
len += json_printf(&out, "}");
|
|
|
|
}
|
|
|
|
mpd_song_free(song);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
mpd_response_finish(mpd.conn);
|
|
|
|
|
|
|
|
if (strcmp(plist, "") == 0) {
|
|
|
|
len += json_printf(&out, "], totalEntities: %d, offset: %d, returnedEntities: %d, searchstr: %Q}",
|
|
|
|
entity_count,
|
|
|
|
offset,
|
|
|
|
entities_returned,
|
|
|
|
searchstr
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
len = json_printf(&out, "{type: result, data: ok}");
|
|
|
|
|
|
|
|
CHECK_RETURN_LEN();
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mympd_search_v21(char *buffer, char *searchstr, char *filter, char *plist, unsigned int offset) {
|
|
|
|
struct mpd_song *song;
|
|
|
|
unsigned long entity_count = 0;
|
|
|
|
unsigned long entities_returned = 0;
|
|
|
|
int len = 0;
|
|
|
|
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
|
|
|
|
|
|
|
|
if (strcmp(plist, "") == 0) {
|
|
|
|
if (mpd_search_db_songs(mpd.conn, false) == false)
|
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_search_db_songs");
|
|
|
|
len = json_printf(&out, "{type: search, data: [");
|
|
|
|
}
|
|
|
|
else if (strcmp(plist, "queue") == 0) {
|
|
|
|
if (mpd_search_add_db_songs(mpd.conn, false) == false)
|
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_search_add_db_songs");
|
|
|
|
}
|
|
|
|
else {
|
2018-11-20 23:06:56 +00:00
|
|
|
if (mpd_search_add_db_songs_to_playlist(mpd.conn, plist) == false)
|
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_search_add_db_songs_to_playlist");
|
2018-11-19 22:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(filter, "any") == 0) {
|
|
|
|
if (mpd_search_add_any_tag_constraint(mpd.conn, MPD_OPERATOR_DEFAULT, searchstr) == false)
|
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_search_add_any_tag_constraint");
|
|
|
|
}
|
|
|
|
else if (strcmp(filter, "modified-since") == 0) {
|
|
|
|
char *crap;
|
|
|
|
long timestamp = strtol(searchstr, &crap, 10);
|
|
|
|
if (mpd_search_add_modified_since_constraint(mpd.conn, MPD_OPERATOR_DEFAULT, timestamp) == false)
|
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_search_add_tag_constraint");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (mpd_search_add_tag_constraint(mpd.conn, MPD_OPERATOR_DEFAULT, mpd_tag_name_parse(filter), searchstr) == false)
|
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_search_add_tag_constraint");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mpd_search_commit(mpd.conn) == false)
|
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_search_commit");
|
2014-02-22 00:57:26 +00:00
|
|
|
|
2018-09-03 18:02:12 +00:00
|
|
|
if (strcmp(plist, "") == 0) {
|
|
|
|
while ((song = mpd_recv_song(mpd.conn)) != NULL) {
|
2018-10-18 21:55:45 +00:00
|
|
|
entity_count++;
|
2018-10-14 22:03:56 +00:00
|
|
|
if (entity_count > offset && entity_count <= offset + config.max_elements_per_page) {
|
2018-10-18 21:55:45 +00:00
|
|
|
if (entities_returned++)
|
2018-08-22 00:13:30 +00:00
|
|
|
len += json_printf(&out, ", ");
|
2018-10-22 22:20:57 +00:00
|
|
|
len += json_printf(&out, "{Type: song, ");
|
2018-10-25 20:03:33 +00:00
|
|
|
if (mpd.feat_tags == true)
|
|
|
|
PUT_SONG_TAGS();
|
|
|
|
else
|
|
|
|
PUT_MIN_SONG_TAGS();
|
2018-10-17 22:04:06 +00:00
|
|
|
len += json_printf(&out, "}");
|
2018-05-24 22:21:19 +00:00
|
|
|
}
|
2018-05-27 12:37:50 +00:00
|
|
|
mpd_song_free(song);
|
|
|
|
}
|
2018-09-03 18:02:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
mpd_response_finish(mpd.conn);
|
2018-06-18 16:53:30 +00:00
|
|
|
|
2018-09-03 18:02:12 +00:00
|
|
|
if (strcmp(plist, "") == 0) {
|
|
|
|
len += json_printf(&out, "], totalEntities: %d, offset: %d, returnedEntities: %d, searchstr: %Q}",
|
2018-06-18 16:53:30 +00:00
|
|
|
entity_count,
|
|
|
|
offset,
|
|
|
|
entities_returned,
|
2018-09-03 18:02:12 +00:00
|
|
|
searchstr
|
2018-06-18 16:53:30 +00:00
|
|
|
);
|
2018-09-03 18:02:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
len = json_printf(&out, "{type: result, data: ok}");
|
2018-07-31 22:01:10 +00:00
|
|
|
|
2018-10-14 22:03:56 +00:00
|
|
|
CHECK_RETURN_LEN();
|
2018-07-31 20:46:53 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2018-08-05 22:02:29 +00:00
|
|
|
int mympd_queue_crop(char *buffer) {
|
|
|
|
int len = 0;
|
|
|
|
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
|
|
|
|
struct mpd_status *status = mpd_run_status(mpd.conn);
|
|
|
|
int length = mpd_status_get_queue_length(status) - 1;
|
|
|
|
int playing_song_pos = mpd_status_get_song_pos(status);
|
|
|
|
|
|
|
|
if (length < 0) {
|
2018-08-22 00:13:30 +00:00
|
|
|
len = json_printf(&out, "{type: error, data: %Q}", "A playlist longer than 1 song in length is required to crop.");
|
2018-08-05 22:02:29 +00:00
|
|
|
}
|
|
|
|
else if (mpd_status_get_state(status) == MPD_STATE_PLAY || mpd_status_get_state(status) == MPD_STATE_PAUSE) {
|
2018-10-18 21:55:45 +00:00
|
|
|
playing_song_pos++;
|
2018-08-05 22:02:29 +00:00
|
|
|
if (playing_song_pos < length)
|
|
|
|
mpd_run_delete_range(mpd.conn, playing_song_pos, -1);
|
2018-10-18 21:55:45 +00:00
|
|
|
playing_song_pos--;
|
2018-08-05 22:02:29 +00:00
|
|
|
if (playing_song_pos > 0 )
|
2018-10-18 21:55:45 +00:00
|
|
|
mpd_run_delete_range(mpd.conn, 0, playing_song_pos--);
|
2018-08-06 21:29:20 +00:00
|
|
|
len = json_printf(&out, "{type: result, data: ok}");
|
2018-08-05 22:02:29 +00:00
|
|
|
} else {
|
2018-08-06 21:29:20 +00:00
|
|
|
len = json_printf(&out, "{type: error, data: %Q}", "You need to be playing to crop the playlist");
|
2018-08-05 22:02:29 +00:00
|
|
|
}
|
|
|
|
|
2018-08-21 22:56:17 +00:00
|
|
|
mpd_status_free(status);
|
|
|
|
|
2018-10-14 22:03:56 +00:00
|
|
|
CHECK_RETURN_LEN();
|
2018-08-05 22:02:29 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2018-07-10 23:06:31 +00:00
|
|
|
int mympd_search_queue(char *buffer, char *mpdtagtype, unsigned int offset, char *searchstr) {
|
2018-05-24 22:21:19 +00:00
|
|
|
struct mpd_song *song;
|
2018-05-29 21:55:24 +00:00
|
|
|
unsigned long entity_count = 0;
|
|
|
|
unsigned long entities_returned = 0;
|
2018-06-18 16:53:30 +00:00
|
|
|
int len;
|
2018-08-05 22:02:29 +00:00
|
|
|
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
|
2018-05-24 22:21:19 +00:00
|
|
|
|
2018-07-24 22:58:29 +00:00
|
|
|
if (mpd_search_queue_songs(mpd.conn, false) == false) {
|
2018-05-24 22:21:19 +00:00
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_search_queue_songs");
|
2018-05-27 12:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mpd_tag_name_parse(mpdtagtype) != MPD_TAG_UNKNOWN) {
|
|
|
|
if (mpd_search_add_tag_constraint(mpd.conn, MPD_OPERATOR_DEFAULT, mpd_tag_name_parse(mpdtagtype), searchstr) == false)
|
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_search_add_tag_constraint");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (mpd_search_add_any_tag_constraint(mpd.conn, MPD_OPERATOR_DEFAULT, searchstr) == false)
|
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_search_add_any_tag_constraint");
|
|
|
|
}
|
|
|
|
|
2018-07-24 22:58:29 +00:00
|
|
|
if (mpd_search_commit(mpd.conn) == false)
|
2018-05-24 22:21:19 +00:00
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_search_commit");
|
|
|
|
else {
|
2018-08-22 00:13:30 +00:00
|
|
|
len = json_printf(&out, "{type: queuesearch, data: [");
|
2018-05-24 22:21:19 +00:00
|
|
|
|
2018-09-24 20:55:53 +00:00
|
|
|
while ((song = mpd_recv_song(mpd.conn)) != NULL) {
|
2018-10-18 21:55:45 +00:00
|
|
|
entity_count++;
|
2018-10-14 22:03:56 +00:00
|
|
|
if (entity_count > offset && entity_count <= offset + config.max_elements_per_page) {
|
2018-10-18 21:55:45 +00:00
|
|
|
if (entities_returned++)
|
2018-09-03 18:02:12 +00:00
|
|
|
len += json_printf(&out, ", ");
|
2018-11-15 20:59:38 +00:00
|
|
|
len += json_printf(&out, "{type: song, id: %d, Pos: %d, ",
|
2018-09-03 18:02:12 +00:00
|
|
|
mpd_song_get_id(song),
|
2018-10-18 21:33:11 +00:00
|
|
|
mpd_song_get_pos(song)
|
2018-09-03 18:02:12 +00:00
|
|
|
);
|
2018-10-25 20:03:33 +00:00
|
|
|
if (mpd.feat_tags == true)
|
|
|
|
PUT_SONG_TAGS();
|
|
|
|
else
|
|
|
|
PUT_MIN_SONG_TAGS();
|
2018-10-17 22:04:06 +00:00
|
|
|
len += json_printf(&out, "}");
|
2018-09-03 18:02:12 +00:00
|
|
|
mpd_song_free(song);
|
|
|
|
}
|
2018-05-25 16:12:46 +00:00
|
|
|
}
|
|
|
|
|
2018-08-06 21:29:20 +00:00
|
|
|
len += json_printf(&out, "], totalEntities: %d, offset: %d, returnedEntities: %d, mpdtagtype: %Q}",
|
2018-06-18 16:53:30 +00:00
|
|
|
entity_count,
|
|
|
|
offset,
|
|
|
|
entities_returned,
|
|
|
|
mpdtagtype
|
|
|
|
);
|
2014-02-22 00:57:26 +00:00
|
|
|
}
|
2018-06-18 16:53:30 +00:00
|
|
|
|
2018-10-14 22:03:56 +00:00
|
|
|
CHECK_RETURN_LEN();
|
2018-06-18 16:53:30 +00:00
|
|
|
return len;
|
2013-11-05 13:59:12 +00:00
|
|
|
}
|
2014-02-22 00:57:26 +00:00
|
|
|
|
2018-08-06 21:29:20 +00:00
|
|
|
int mympd_put_stats(char *buffer) {
|
2018-05-27 21:34:39 +00:00
|
|
|
struct mpd_stats *stats = mpd_run_stats(mpd.conn);
|
2018-05-31 23:29:45 +00:00
|
|
|
const unsigned *version = mpd_connection_get_server_version(mpd.conn);
|
2018-06-18 16:53:30 +00:00
|
|
|
int len;
|
2018-08-21 22:56:17 +00:00
|
|
|
char mpd_version[20];
|
2018-11-22 22:15:25 +00:00
|
|
|
char libmpdclient_version[20];
|
2018-06-18 16:53:30 +00:00
|
|
|
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
|
2018-08-21 22:56:17 +00:00
|
|
|
snprintf(mpd_version, 20, "%i.%i.%i", version[0], version[1], version[2]);
|
2018-11-22 22:15:25 +00:00
|
|
|
snprintf(libmpdclient_version, 20, "%i.%i.%i", LIBMPDCLIENT_MAJOR_VERSION, LIBMPDCLIENT_MINOR_VERSION, LIBMPDCLIENT_PATCH_VERSION);
|
|
|
|
|
2018-05-27 21:34:39 +00:00
|
|
|
if (stats == NULL)
|
2018-08-06 21:29:20 +00:00
|
|
|
RETURN_ERROR_AND_RECOVER("mympd_put_stats");
|
2018-08-22 00:13:30 +00:00
|
|
|
len = json_printf(&out, "{type: mpdstats, data: {artists: %d, albums: %d, songs: %d, "
|
2018-11-22 22:15:25 +00:00
|
|
|
"playtime: %d, uptime: %d, dbUpdated: %d, dbPlaytime: %d, mympdVersion: %Q, mpdVersion: %Q, "
|
|
|
|
"libmpdclientVersion: %Q}}",
|
2018-06-18 16:53:30 +00:00
|
|
|
mpd_stats_get_number_of_artists(stats),
|
|
|
|
mpd_stats_get_number_of_albums(stats),
|
|
|
|
mpd_stats_get_number_of_songs(stats),
|
|
|
|
mpd_stats_get_play_time(stats),
|
|
|
|
mpd_stats_get_uptime(stats),
|
|
|
|
mpd_stats_get_db_update_time(stats),
|
|
|
|
mpd_stats_get_db_play_time(stats),
|
|
|
|
MYMPD_VERSION,
|
2018-11-22 22:15:25 +00:00
|
|
|
mpd_version,
|
|
|
|
libmpdclient_version
|
2018-06-18 16:53:30 +00:00
|
|
|
);
|
2018-05-27 21:34:39 +00:00
|
|
|
mpd_stats_free(stats);
|
2018-06-18 16:53:30 +00:00
|
|
|
|
2018-10-14 22:03:56 +00:00
|
|
|
CHECK_RETURN_LEN();
|
2018-06-18 16:53:30 +00:00
|
|
|
return len;
|
2018-05-27 21:34:39 +00:00
|
|
|
}
|
2014-02-22 00:57:26 +00:00
|
|
|
|
2018-07-10 23:06:31 +00:00
|
|
|
void mympd_disconnect() {
|
2014-02-16 18:46:53 +00:00
|
|
|
mpd.conn_state = MPD_DISCONNECT;
|
2018-08-22 00:13:30 +00:00
|
|
|
mympd_idle(NULL, 0);
|
2014-03-18 00:30:01 +00:00
|
|
|
}
|
2018-09-24 20:55:53 +00:00
|
|
|
|
2018-09-30 22:18:55 +00:00
|
|
|
int mympd_smartpls_put(char *buffer, char *playlist) {
|
|
|
|
char pl_file[400];
|
|
|
|
char *smartpltype;
|
|
|
|
char *p_charbuf1, *p_charbuf2;
|
2018-10-08 20:44:22 +00:00
|
|
|
int je, int_buf1;
|
2018-09-30 22:18:55 +00:00
|
|
|
int len = 0;
|
|
|
|
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
|
|
|
|
|
|
|
|
snprintf(pl_file, 400, "%s/smartpls/%s", config.varlibdir, playlist);
|
|
|
|
char *content = json_fread(pl_file);
|
|
|
|
je = json_scanf(content, strlen(content), "{type: %Q }", &smartpltype);
|
|
|
|
if (je == 1) {
|
|
|
|
if (strcmp(smartpltype, "sticker") == 0) {
|
|
|
|
je = json_scanf(content, strlen(content), "{sticker: %Q, maxentries: %d}", &p_charbuf1, &int_buf1);
|
|
|
|
if (je == 2) {
|
2018-10-01 17:07:38 +00:00
|
|
|
len = json_printf(&out, "{type: smartpls, data: {playlist: %Q, type: %Q, sticker: %Q, maxentries: %d}}",
|
|
|
|
playlist,
|
2018-09-30 22:18:55 +00:00
|
|
|
smartpltype,
|
|
|
|
p_charbuf1,
|
|
|
|
int_buf1);
|
|
|
|
free(p_charbuf1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (strcmp(smartpltype, "newest") == 0) {
|
2018-10-08 20:44:22 +00:00
|
|
|
je = json_scanf(content, strlen(content), "{timerange: %d}", &int_buf1);
|
|
|
|
if (je == 1) {
|
|
|
|
len = json_printf(&out, "{type: smartpls, data: {playlist: %Q, type: %Q, timerange: %d}}",
|
2018-10-01 17:07:38 +00:00
|
|
|
playlist,
|
2018-09-30 22:18:55 +00:00
|
|
|
smartpltype,
|
2018-10-08 20:44:22 +00:00
|
|
|
int_buf1);
|
2018-09-30 22:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (strcmp(smartpltype, "search") == 0) {
|
|
|
|
je = json_scanf(content, strlen(content), "{tag: %Q, searchstr: %Q}", &p_charbuf1, &p_charbuf2);
|
|
|
|
if (je == 2) {
|
2018-10-01 17:07:38 +00:00
|
|
|
len = json_printf(&out, "{type: smartpls, data: {playlist: %Q, type: %Q, tag: %Q, searchstr: %Q}}",
|
|
|
|
playlist,
|
2018-09-30 22:18:55 +00:00
|
|
|
smartpltype,
|
|
|
|
p_charbuf1,
|
|
|
|
p_charbuf2);
|
|
|
|
free(p_charbuf1);
|
|
|
|
free(p_charbuf2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(smartpltype);
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mympd_smartpls_save(char *smartpltype, char *playlist, char *tag, char *searchstr, int maxentries, int timerange) {
|
2018-09-26 22:10:51 +00:00
|
|
|
char tmp_file[400];
|
|
|
|
char pl_file[400];
|
2018-09-29 23:03:00 +00:00
|
|
|
snprintf(tmp_file, 400, "%s/tmp/%s", config.varlibdir, playlist);
|
|
|
|
snprintf(pl_file, 400, "%s/smartpls/%s", config.varlibdir, playlist);
|
2018-09-30 22:18:55 +00:00
|
|
|
if (strcmp(smartpltype, "sticker") == 0) {
|
|
|
|
json_fprintf(tmp_file, "{type: %Q, sticker: %Q, maxentries: %d}", smartpltype, tag, maxentries);
|
|
|
|
rename(tmp_file, pl_file);
|
|
|
|
mympd_smartpls_update(playlist, tag, maxentries);
|
|
|
|
}
|
|
|
|
else if (strcmp(smartpltype, "newest") == 0) {
|
2018-10-08 20:44:22 +00:00
|
|
|
json_fprintf(tmp_file, "{type: %Q, timerange: %d}", smartpltype, timerange);
|
2018-09-30 22:18:55 +00:00
|
|
|
rename(tmp_file, pl_file);
|
2018-10-08 20:44:22 +00:00
|
|
|
mympd_smartpls_update_newest(playlist, timerange);
|
2018-09-30 22:18:55 +00:00
|
|
|
}
|
|
|
|
else if (strcmp(smartpltype, "search") == 0) {
|
|
|
|
json_fprintf(tmp_file, "{type: %Q, tag: %Q, searchstr: %Q}", smartpltype, tag, searchstr);
|
|
|
|
rename(tmp_file, pl_file);
|
|
|
|
mympd_smartpls_update_search(playlist, tag, searchstr);
|
|
|
|
}
|
2018-09-26 22:10:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-24 20:55:53 +00:00
|
|
|
int mympd_smartpls_update_all() {
|
2018-09-25 22:49:14 +00:00
|
|
|
DIR *dir;
|
|
|
|
struct dirent *ent;
|
|
|
|
char *smartpltype;
|
|
|
|
char filename[400];
|
2018-09-29 23:03:00 +00:00
|
|
|
char dirname[400];
|
2018-09-25 22:49:14 +00:00
|
|
|
int je;
|
2018-09-26 22:10:51 +00:00
|
|
|
char *p_charbuf1, *p_charbuf2;
|
2018-10-08 20:44:22 +00:00
|
|
|
int int_buf1;
|
2018-09-25 22:49:14 +00:00
|
|
|
|
2018-09-24 22:27:24 +00:00
|
|
|
if (!config.smartpls)
|
2018-09-24 20:55:53 +00:00
|
|
|
return 0;
|
2018-09-25 22:49:14 +00:00
|
|
|
|
2018-09-29 23:03:00 +00:00
|
|
|
snprintf(dirname, 400, "%s/smartpls", config.varlibdir);
|
|
|
|
if ((dir = opendir (dirname)) != NULL) {
|
2018-09-25 22:49:14 +00:00
|
|
|
while ((ent = readdir(dir)) != NULL) {
|
2018-09-26 22:10:51 +00:00
|
|
|
if (strncmp(ent->d_name, ".", 1) == 0)
|
2018-09-25 22:49:14 +00:00
|
|
|
continue;
|
2018-09-29 23:03:00 +00:00
|
|
|
snprintf(filename, 400, "%s/smartpls/%s", config.varlibdir, ent->d_name);
|
2018-09-25 22:49:14 +00:00
|
|
|
char *content = json_fread(filename);
|
|
|
|
je = json_scanf(content, strlen(content), "{type: %Q }", &smartpltype);
|
|
|
|
if (je != 1)
|
|
|
|
continue;
|
|
|
|
if (strcmp(smartpltype, "sticker") == 0) {
|
|
|
|
je = json_scanf(content, strlen(content), "{sticker: %Q, maxentries: %d}", &p_charbuf1, &int_buf1);
|
|
|
|
if (je == 2) {
|
2018-09-30 22:18:55 +00:00
|
|
|
mympd_smartpls_update(ent->d_name, p_charbuf1, int_buf1);
|
2018-09-25 22:49:14 +00:00
|
|
|
free(p_charbuf1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
printf("Can't parse file %s\n", filename);
|
|
|
|
}
|
|
|
|
else if (strcmp(smartpltype, "newest") == 0) {
|
2018-10-08 20:44:22 +00:00
|
|
|
je = json_scanf(content, strlen(content), "{timerange: %d}", &int_buf1);
|
|
|
|
if (je == 1) {
|
|
|
|
mympd_smartpls_update_newest(ent->d_name, int_buf1);
|
2018-09-25 22:49:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
printf("Can't parse file %s\n", filename);
|
|
|
|
}
|
|
|
|
else if (strcmp(smartpltype, "search") == 0) {
|
2018-09-26 22:10:51 +00:00
|
|
|
je = json_scanf(content, strlen(content), "{tag: %Q, searchstr: %Q}", &p_charbuf1, &p_charbuf2);
|
|
|
|
if (je == 2) {
|
|
|
|
mympd_smartpls_update_search(ent->d_name, p_charbuf1, p_charbuf2);
|
|
|
|
free(p_charbuf1);
|
|
|
|
free(p_charbuf2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
printf("Can't parse file %s\n", filename);
|
2018-09-25 22:49:14 +00:00
|
|
|
}
|
|
|
|
free(smartpltype);
|
2018-10-15 22:34:25 +00:00
|
|
|
free(content);
|
2018-09-25 22:49:14 +00:00
|
|
|
}
|
|
|
|
closedir (dir);
|
|
|
|
} else {
|
2018-09-29 23:03:00 +00:00
|
|
|
printf("Can't open dir %s\n", dirname);
|
2018-09-24 22:27:24 +00:00
|
|
|
return 1;
|
2018-09-25 22:49:14 +00:00
|
|
|
}
|
2018-09-24 22:27:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mympd_smartpls_clear(char *playlist) {
|
|
|
|
struct mpd_playlist *pl;
|
|
|
|
const char *plpath;
|
|
|
|
bool exists = false;
|
|
|
|
if (!mpd_send_list_playlists(mpd.conn)) {
|
|
|
|
LOG_ERROR_AND_RECOVER("mpd_send_list_playlists");
|
|
|
|
return 1;
|
|
|
|
}
|
2018-10-11 21:30:50 +00:00
|
|
|
while ((pl = mpd_recv_playlist(mpd.conn)) != NULL) {
|
2018-09-24 22:27:24 +00:00
|
|
|
plpath = mpd_playlist_get_path(pl);
|
|
|
|
if (strcmp(playlist, plpath) == 0)
|
|
|
|
exists = true;
|
|
|
|
mpd_playlist_free(pl);
|
2018-09-25 22:49:14 +00:00
|
|
|
if (exists)
|
|
|
|
break;
|
2018-09-24 22:27:24 +00:00
|
|
|
}
|
2018-09-25 22:49:14 +00:00
|
|
|
mpd_response_finish(mpd.conn);
|
2018-09-24 22:27:24 +00:00
|
|
|
|
|
|
|
if (exists) {
|
|
|
|
if (!mpd_run_rm(mpd.conn, playlist)) {
|
|
|
|
LOG_ERROR_AND_RECOVER("mpd_run_rm");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2018-09-24 20:55:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-26 22:10:51 +00:00
|
|
|
int mympd_smartpls_update_search(char *playlist, char *tag, char *searchstr) {
|
|
|
|
mympd_smartpls_clear(playlist);
|
|
|
|
mympd_search(mpd.buf, searchstr, tag, playlist, 0);
|
|
|
|
printf("Updated %s\n", playlist);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-30 22:18:55 +00:00
|
|
|
int mympd_smartpls_update(char *playlist, char *sticker, int maxentries) {
|
2018-09-24 20:55:53 +00:00
|
|
|
struct mpd_pair *pair;
|
|
|
|
char *uri = NULL;
|
2018-10-18 22:23:58 +00:00
|
|
|
const char *p_value;
|
2018-09-24 20:55:53 +00:00
|
|
|
char *crap;
|
|
|
|
long value;
|
|
|
|
long value_max = 0;
|
2018-09-24 22:27:24 +00:00
|
|
|
long i = 0;
|
2018-10-18 22:23:58 +00:00
|
|
|
unsigned int j;
|
2018-09-24 22:27:24 +00:00
|
|
|
|
2018-09-24 22:44:28 +00:00
|
|
|
if (!mpd_send_sticker_find(mpd.conn, "song", "", sticker)) {
|
|
|
|
LOG_ERROR_AND_RECOVER("mpd_send_sticker_find");
|
|
|
|
return 1;
|
|
|
|
}
|
2018-10-08 20:44:22 +00:00
|
|
|
|
|
|
|
struct list add_list;
|
|
|
|
list_init(&add_list);
|
|
|
|
|
2018-09-24 20:55:53 +00:00
|
|
|
while ((pair = mpd_recv_pair(mpd.conn)) != NULL) {
|
|
|
|
if (strcmp(pair->name, "file") == 0) {
|
|
|
|
uri = strdup(pair->value);
|
|
|
|
}
|
|
|
|
else if (strcmp(pair->name, "sticker") == 0) {
|
2018-10-18 22:23:58 +00:00
|
|
|
p_value = mpd_parse_sticker(pair->value, &j);
|
|
|
|
if (p_value != NULL) {
|
|
|
|
value = strtol(p_value, &crap, 10);
|
|
|
|
if (value > 1)
|
|
|
|
list_push(&add_list, uri, value);
|
|
|
|
if (value > value_max)
|
|
|
|
value_max = value;
|
|
|
|
}
|
2018-09-24 20:55:53 +00:00
|
|
|
}
|
|
|
|
mpd_return_pair(mpd.conn, pair);
|
|
|
|
}
|
|
|
|
mpd_response_finish(mpd.conn);
|
2018-10-08 20:44:22 +00:00
|
|
|
free(uri);
|
2018-09-24 20:55:53 +00:00
|
|
|
|
2018-09-24 22:27:24 +00:00
|
|
|
mympd_smartpls_clear(playlist);
|
|
|
|
|
2018-09-24 20:55:53 +00:00
|
|
|
if (value_max > 2)
|
|
|
|
value_max = value_max / 2;
|
2018-10-08 20:44:22 +00:00
|
|
|
|
|
|
|
struct node *current = add_list.list;
|
|
|
|
while (current != NULL) {
|
|
|
|
if (current->value >= value_max) {
|
|
|
|
if (!mpd_run_playlist_add(mpd.conn, playlist, current->data)) {
|
|
|
|
LOG_ERROR_AND_RECOVER("mpd_run_playlist_add");
|
|
|
|
list_free(&add_list);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
if (i >= maxentries)
|
|
|
|
break;
|
2018-09-24 22:27:24 +00:00
|
|
|
}
|
2018-10-08 20:44:22 +00:00
|
|
|
current = current->next;
|
2018-09-24 22:27:24 +00:00
|
|
|
}
|
2018-10-08 20:44:22 +00:00
|
|
|
list_free(&add_list);
|
2018-09-24 22:27:24 +00:00
|
|
|
printf("Updated %s with %ld songs, minValue: %ld\n", playlist, i, value_max);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-08 20:44:22 +00:00
|
|
|
int mympd_smartpls_update_newest(char *playlist, int timerange) {
|
2018-10-08 16:14:07 +00:00
|
|
|
unsigned long value_max = 0;
|
2018-10-08 20:44:22 +00:00
|
|
|
char searchstr[20];
|
2018-09-24 22:27:24 +00:00
|
|
|
|
2018-10-08 16:14:07 +00:00
|
|
|
struct mpd_stats *stats = mpd_run_stats(mpd.conn);
|
|
|
|
if (stats != NULL) {
|
|
|
|
value_max = mpd_stats_get_db_update_time(stats);
|
|
|
|
mpd_stats_free(stats);
|
2018-09-24 22:44:28 +00:00
|
|
|
}
|
2018-10-08 16:14:07 +00:00
|
|
|
else {
|
|
|
|
LOG_ERROR_AND_RECOVER("mpd_run_stats");
|
2018-09-24 22:27:24 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mympd_smartpls_clear(playlist);
|
2018-09-25 22:49:14 +00:00
|
|
|
value_max -= timerange;
|
2018-10-07 22:21:44 +00:00
|
|
|
if (value_max > 0) {
|
2018-10-08 20:44:22 +00:00
|
|
|
snprintf(searchstr, 20, "%lu", value_max);
|
|
|
|
mympd_search(mpd.buf, searchstr, "modified-since", playlist, 0);
|
|
|
|
printf("Updated %s\n", playlist);
|
2018-09-24 20:55:53 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|