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>
|
2013-11-04 17:18:38 +00:00
|
|
|
#include <mpd/client.h>
|
2017-03-24 10:01:24 +00:00
|
|
|
#include <mpd/message.h>
|
2013-11-04 17:18:38 +00:00
|
|
|
|
|
|
|
#include "mpd_client.h"
|
2014-02-04 16:58:10 +00:00
|
|
|
#include "config.h"
|
2018-06-14 22:00:54 +00:00
|
|
|
#include "frozen/frozen.h"
|
2013-11-04 17:18:38 +00:00
|
|
|
|
2015-04-28 09:08:21 +00:00
|
|
|
/* forward declaration */
|
2018-06-14 22:00:54 +00:00
|
|
|
static int mympd_notify_callback(struct mg_connection *c, const char *param);
|
2015-04-28 09:08:21 +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-06-12 00:07:03 +00:00
|
|
|
static inline enum mpd_cmd_ids get_cmd_id(const char *cmd)
|
2013-11-04 17:18:38 +00:00
|
|
|
{
|
2014-02-16 18:46:53 +00:00
|
|
|
for(int 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-06-18 19:55:54 +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-07-18 22:29:19 +00:00
|
|
|
int je, int_buf, int_rc;
|
2018-06-14 22:57:57 +00:00
|
|
|
float float_buf;
|
2018-06-18 19:55:54 +00:00
|
|
|
char *p_charbuf1, *p_charbuf2;
|
2018-06-21 16:57:40 +00:00
|
|
|
struct mympd_state { int a; int b; } state = { .a = 0, .b = 0 };
|
2018-07-02 19:40:56 +00:00
|
|
|
enum mpd_cmd_ids cmd_id;
|
2018-06-21 16:57:40 +00:00
|
|
|
|
2018-06-14 22:57:57 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stdout,"Got request: %s\n",msg.p);
|
|
|
|
#endif
|
|
|
|
|
2018-07-02 19:40:56 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{cmd:%Q}", &cmd);
|
|
|
|
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-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-06-21 16:57:40 +00:00
|
|
|
case MPD_API_SET_SETTINGS:
|
|
|
|
json_scanf(msg.p, msg.len, "{ data: { notificationWeb: %d, notificationPage: %d} }", &state.a, &state.b);
|
2018-06-24 18:05:38 +00:00
|
|
|
char tmpfile[200];
|
2018-07-15 22:51:36 +00:00
|
|
|
snprintf(tmpfile,200,"%s.tmp", mpd.statefile);
|
2018-06-24 18:05:38 +00:00
|
|
|
json_fprintf(tmpfile, "{ notificationWeb: %d, notificationPage: %d}", state.a, state.b);
|
|
|
|
rename(tmpfile,mpd.statefile);
|
2018-06-21 16:57:40 +00:00
|
|
|
|
2018-06-19 22:43:36 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{ data: { random:%u } }", &uint_buf1);
|
|
|
|
if (je == 1)
|
|
|
|
mpd_run_random(mpd.conn, uint_buf1);
|
|
|
|
|
|
|
|
je = json_scanf(msg.p, msg.len, "{ data: { repeat:%u } }", &uint_buf1);
|
|
|
|
if (je == 1)
|
|
|
|
mpd_run_repeat(mpd.conn, uint_buf1);
|
|
|
|
|
|
|
|
je = json_scanf(msg.p, msg.len, "{ data: { consume:%u } }", &uint_buf1);
|
|
|
|
if (je == 1)
|
|
|
|
mpd_run_consume(mpd.conn, uint_buf1);
|
|
|
|
|
|
|
|
je = json_scanf(msg.p, msg.len, "{ data: { single:%u } }", &uint_buf1);
|
|
|
|
if (je == 1)
|
|
|
|
mpd_run_single(mpd.conn, uint_buf1);
|
|
|
|
|
|
|
|
je = json_scanf(msg.p, msg.len, "{ data: { crossfade:%u } }", &uint_buf1);
|
|
|
|
if (je == 1)
|
|
|
|
mpd_run_crossfade(mpd.conn, uint_buf1);
|
|
|
|
|
|
|
|
je = json_scanf(msg.p, msg.len, "{ data: { mixrampdb:%f } }", &float_buf);
|
|
|
|
if (je == 1)
|
|
|
|
mpd_run_mixrampdb(mpd.conn, float_buf);
|
|
|
|
|
|
|
|
je = json_scanf(msg.p, msg.len, "{ data: { mixrampdelay:%f } }", &float_buf);
|
|
|
|
if (je == 1)
|
|
|
|
mpd_run_mixrampdelay(mpd.conn, float_buf);
|
|
|
|
|
|
|
|
je = json_scanf(msg.p, msg.len, "{ data: { replaygain:%Q } }", &p_charbuf1);
|
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-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 22:57:57 +00:00
|
|
|
case MPD_API_GET_ARTISTALBUMTITLES:
|
|
|
|
je = json_scanf(msg.p, msg.len, "{ data: { albumartist:%Q, album:%Q } }", &p_charbuf1, &p_charbuf2);
|
2018-06-26 21:25:52 +00:00
|
|
|
if (je == 2) {
|
|
|
|
n = mympd_put_songs_in_album(mpd.buf, p_charbuf1, p_charbuf2);
|
|
|
|
free(p_charbuf1);
|
|
|
|
free(p_charbuf2);
|
2018-07-16 22:33:55 +00:00
|
|
|
}
|
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;
|
2014-02-16 18:46:53 +00:00
|
|
|
case MPD_API_UPDATE_DB:
|
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;
|
2014-02-16 18:46:53 +00:00
|
|
|
case MPD_API_SET_PAUSE:
|
|
|
|
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;
|
|
|
|
case MPD_API_SET_PREV:
|
|
|
|
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;
|
|
|
|
case MPD_API_SET_NEXT:
|
|
|
|
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;
|
|
|
|
case MPD_API_SET_PLAY:
|
|
|
|
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;
|
|
|
|
case MPD_API_SET_STOP:
|
|
|
|
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;
|
|
|
|
case MPD_API_RM_ALL:
|
|
|
|
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;
|
|
|
|
case MPD_API_RM_TRACK:
|
2018-06-18 19:55:54 +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;
|
2017-04-08 09:25:40 +00:00
|
|
|
case MPD_API_RM_RANGE:
|
2018-06-18 19:55:54 +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;
|
2015-09-04 14:43:16 +00:00
|
|
|
case MPD_API_MOVE_TRACK:
|
2018-07-22 23:07:25 +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-07-26 21:48:03 +00:00
|
|
|
uint_buf1 --;
|
|
|
|
uint_buf2 --;
|
|
|
|
if (uint_buf1 < uint_buf2)
|
|
|
|
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:
|
|
|
|
je = json_scanf(msg.p, msg.len, "{ data: { plist: %Q, from:%u, to:%u } }", &p_charbuf1, &uint_buf1, &uint_buf2);
|
|
|
|
if (je == 3) {
|
|
|
|
uint_buf1 --;
|
|
|
|
uint_buf2 --;
|
|
|
|
if (uint_buf1 < uint_buf2)
|
|
|
|
uint_buf2 --;
|
|
|
|
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;
|
2014-02-16 18:46:53 +00:00
|
|
|
case MPD_API_PLAY_TRACK:
|
2018-06-18 19:55:54 +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-06-14 19:56:12 +00:00
|
|
|
case MPD_API_GET_OUTPUTNAMES:
|
2018-06-14 22:00:54 +00:00
|
|
|
n = mympd_put_outputnames(mpd.buf);
|
2015-04-28 09:08:21 +00:00
|
|
|
break;
|
|
|
|
case MPD_API_TOGGLE_OUTPUT:
|
2018-06-18 19:55:54 +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;
|
2014-02-16 18:46:53 +00:00
|
|
|
case MPD_API_SET_VOLUME:
|
2018-06-18 19:55:54 +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;
|
|
|
|
case MPD_API_SET_SEEK:
|
2018-06-18 19:55:54 +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;
|
2014-02-22 01:11:45 +00:00
|
|
|
case MPD_API_GET_QUEUE:
|
2018-06-18 19:55:54 +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-06-18 19:55:54 +00:00
|
|
|
n = mympd_put_queue(mpd.buf, uint_buf1);
|
2018-07-16 22:33:55 +00:00
|
|
|
}
|
2014-02-22 01:11:45 +00:00
|
|
|
break;
|
2018-06-11 17:33:11 +00:00
|
|
|
case MPD_API_GET_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;
|
|
|
|
case MPD_API_GET_SONGDETAILS:
|
|
|
|
je = json_scanf(msg.p, msg.len, "{ data: { uri:%Q } }", &p_charbuf1);
|
|
|
|
if (je == 1) {
|
|
|
|
n = mympd_put_songdetails(mpd.buf, p_charbuf1);
|
|
|
|
free(p_charbuf1);
|
|
|
|
}
|
2018-06-11 17:33:11 +00:00
|
|
|
break;
|
2018-06-03 16:36:06 +00:00
|
|
|
case MPD_API_GET_ARTISTS:
|
2018-06-18 19:55:54 +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-07-09 17:28:28 +00:00
|
|
|
n = mympd_put_db_tag(mpd.buf, uint_buf1, "AlbumArtist", "", "", p_charbuf1);
|
2018-06-26 21:25:52 +00:00
|
|
|
free(p_charbuf1);
|
|
|
|
}
|
2018-06-03 16:36:06 +00:00
|
|
|
break;
|
|
|
|
case MPD_API_GET_ARTISTALBUMS:
|
2018-06-18 19:55:54 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{ data: { offset:%u, filter:%Q, albumartist:%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_db_tag(mpd.buf, uint_buf1, "Album", "AlbumArtist", p_charbuf2, p_charbuf1);
|
2018-06-26 21:25:52 +00:00
|
|
|
free(p_charbuf1);
|
|
|
|
free(p_charbuf2);
|
|
|
|
}
|
2018-06-03 16:36:06 +00:00
|
|
|
break;
|
2018-07-22 19:00:26 +00:00
|
|
|
case MPD_API_PLAYLIST_RENAME:
|
|
|
|
je = json_scanf(msg.p, msg.len, "{ data: { from:%Q, to:%Q } }", &p_charbuf1, &p_charbuf2);
|
|
|
|
if (je == 2) {
|
|
|
|
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);
|
|
|
|
free(p_charbuf1);
|
|
|
|
free(p_charbuf2);
|
|
|
|
}
|
|
|
|
break;
|
2018-05-28 22:05:56 +00:00
|
|
|
case MPD_API_GET_PLAYLISTS:
|
2018-06-18 19:55:54 +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-07-22 19:00:26 +00:00
|
|
|
case MPD_API_GET_PLAYLIST_LIST:
|
|
|
|
je = json_scanf(msg.p, msg.len, "{ data: { uri: %Q, offset:%u, filter:%Q } }", &p_charbuf1, &uint_buf1, &p_charbuf2);
|
|
|
|
if (je == 3) {
|
|
|
|
n = mympd_put_playlist_list(mpd.buf, p_charbuf1, uint_buf1, p_charbuf2);
|
|
|
|
free(p_charbuf1);
|
|
|
|
free(p_charbuf2);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MPD_API_ADD_TO_PLAYLIST:
|
|
|
|
je = json_scanf(msg.p, msg.len, "{ data: { plist:%Q, uri:%Q } }", &p_charbuf1, &p_charbuf2);
|
|
|
|
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:
|
|
|
|
je = json_scanf(msg.p, msg.len, "{ data: { uri:%Q } }", &p_charbuf1);
|
|
|
|
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;
|
|
|
|
case MPD_API_RM_PLAYLIST_TRACK:
|
|
|
|
je = json_scanf(msg.p, msg.len, "{ data: { uri:%Q, track:%u } }", &p_charbuf1, &uint_buf1);
|
|
|
|
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-06-14 19:56:12 +00:00
|
|
|
case MPD_API_GET_FILESYSTEM:
|
2018-06-18 19:55:54 +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-07-05 19:34:16 +00:00
|
|
|
case MPD_API_ADD_TRACK_AFTER:
|
2018-07-18 22:29:19 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{ data: { uri:%Q, to:%d } }", &p_charbuf1, &int_buf);
|
2018-07-05 19:34:16 +00:00
|
|
|
if (je == 2) {
|
2018-07-18 22:29:19 +00:00
|
|
|
int_rc = mpd_run_add_id_to(mpd.conn, p_charbuf1, int_buf);
|
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;
|
|
|
|
case MPD_API_REPLACE_TRACK:
|
|
|
|
je = json_scanf(msg.p, msg.len, "{ data: { uri:%Q } }", &p_charbuf1);
|
|
|
|
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;
|
2014-02-16 18:46:53 +00:00
|
|
|
case MPD_API_ADD_TRACK:
|
2018-06-18 19:55:54 +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;
|
|
|
|
case MPD_API_ADD_PLAY_TRACK:
|
2018-06-18 19:55:54 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{ data: { uri:%Q } }", &p_charbuf1);
|
|
|
|
if (je == 1) {
|
|
|
|
int_buf = mpd_run_add_id(mpd.conn, p_charbuf1);
|
2018-07-24 22:58:29 +00:00
|
|
|
if (int_buf != -1)
|
2018-06-18 19:55:54 +00:00
|
|
|
mpd_run_play_id(mpd.conn, int_buf);
|
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-07-05 19:34:16 +00:00
|
|
|
case MPD_API_REPLACE_PLAYLIST:
|
|
|
|
je = json_scanf(msg.p, msg.len, "{ data: { plist:%Q } }", &p_charbuf1);
|
|
|
|
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;
|
2014-02-22 00:57:26 +00:00
|
|
|
case MPD_API_ADD_PLAYLIST:
|
2018-06-18 19:55:54 +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;
|
2015-09-02 17:24:52 +00:00
|
|
|
case MPD_API_SAVE_QUEUE:
|
2018-06-18 19:55:54 +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-05-24 22:21:19 +00:00
|
|
|
case MPD_API_SEARCH_QUEUE:
|
2018-06-18 19:55:54 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{ data: { offset:%u, mpdtag:%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-05-31 23:05:49 +00:00
|
|
|
case MPD_API_SEARCH_ADD:
|
2018-07-05 20:04:25 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{ data: { filter:%Q, searchstr:%Q } }", &p_charbuf1, &p_charbuf2);
|
2018-06-26 21:25:52 +00:00
|
|
|
if (je == 2) {
|
2018-06-18 19:55:54 +00:00
|
|
|
n = mympd_search_add(mpd.buf, p_charbuf1, p_charbuf2);
|
2018-06-26 21:25:52 +00:00
|
|
|
free(p_charbuf1);
|
|
|
|
free(p_charbuf2);
|
|
|
|
}
|
2018-05-31 23:05:49 +00:00
|
|
|
break;
|
2014-02-22 00:57:26 +00:00
|
|
|
case MPD_API_SEARCH:
|
2018-06-18 19:55:54 +00:00
|
|
|
je = json_scanf(msg.p, msg.len, "{ data: { offset:%u, mpdtag:%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(mpd.buf, p_charbuf1, uint_buf1, p_charbuf2);
|
2018-06-26 21:25:52 +00:00
|
|
|
free(p_charbuf1);
|
|
|
|
free(p_charbuf2);
|
|
|
|
}
|
2014-02-22 00:57:26 +00:00
|
|
|
break;
|
2018-05-24 19:36:40 +00:00
|
|
|
case MPD_API_SEND_SHUFFLE:
|
|
|
|
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;
|
2017-03-18 12:31:26 +00:00
|
|
|
case MPD_API_SEND_MESSAGE:
|
2018-06-18 19:55:54 +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-05-29 20:12:11 +00:00
|
|
|
case MPD_API_RM_PLAYLIST:
|
2018-07-22 19:48:13 +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_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-06-21 16:57:40 +00:00
|
|
|
case MPD_API_GET_SETTINGS:
|
2018-05-29 21:05:34 +00:00
|
|
|
n = mympd_put_settings(mpd.buf);
|
2014-02-16 18:46:53 +00:00
|
|
|
break;
|
2018-05-27 21:34:39 +00:00
|
|
|
case MPD_API_GET_STATS:
|
|
|
|
n = mympd_get_stats(mpd.buf);
|
|
|
|
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-07-16 22:33:55 +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-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
|
|
|
|
fprintf(stdout,"Send websocket response:\n %s\n",mpd.buf);
|
|
|
|
#endif
|
|
|
|
mg_send_websocket_frame(nc, WEBSOCKET_OP_TEXT, mpd.buf, n);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stdout,"Send http response:\n %s\n",mpd.buf);
|
|
|
|
#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-07-10 23:06:31 +00:00
|
|
|
int mympd_close_handler(struct mg_connection *c) {
|
2014-02-16 18:46:53 +00:00
|
|
|
/* Cleanup session data */
|
2018-07-10 23:06:31 +00:00
|
|
|
if (c->user_data)
|
2018-06-12 01:09:24 +00:00
|
|
|
free(c->user_data);
|
2013-11-09 01:07:03 +00:00
|
|
|
return 0;
|
2013-11-04 17:18:38 +00:00
|
|
|
}
|
|
|
|
|
2018-06-14 22:00:54 +00:00
|
|
|
static int mympd_notify_callback(struct mg_connection *c, const char *param) {
|
2014-02-16 18:46:53 +00:00
|
|
|
size_t n;
|
2018-07-10 23:06:31 +00:00
|
|
|
if (!is_websocket(c))
|
2018-06-12 00:07:03 +00:00
|
|
|
return 0;
|
2014-02-16 18:46:53 +00:00
|
|
|
|
2018-07-10 23:06:31 +00:00
|
|
|
if (param) {
|
2014-02-16 18:46:53 +00:00
|
|
|
/* error message? */
|
2018-06-12 23:29:58 +00:00
|
|
|
n=snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"error\",\"data\":\"%s\"}",param);
|
2018-06-14 15:28:10 +00:00
|
|
|
#ifdef DEBUG
|
2018-06-12 23:29:58 +00:00
|
|
|
fprintf(stdout,"Error in mpd_notify_callback: %s\n",param);
|
2018-06-14 15:28:10 +00:00
|
|
|
#endif
|
2018-06-12 23:29:58 +00:00
|
|
|
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, n);
|
2018-06-12 00:07:03 +00:00
|
|
|
return 0;
|
2014-02-16 18:46:53 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 22:58:29 +00:00
|
|
|
if (!c->user_data)
|
2018-06-12 01:09:24 +00:00
|
|
|
c->user_data = calloc(1, sizeof(struct t_mpd_client_session));
|
2014-02-16 18:46:53 +00:00
|
|
|
|
2018-06-12 01:09:24 +00:00
|
|
|
struct t_mpd_client_session *s = (struct t_mpd_client_session *)c->user_data;
|
2014-02-16 18:46:53 +00:00
|
|
|
|
2018-07-24 22:58:29 +00:00
|
|
|
if (mpd.conn_state != MPD_CONNECTED) {
|
2018-06-12 23:29:58 +00:00
|
|
|
n=snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"disconnected\"}");
|
2018-06-14 15:28:10 +00:00
|
|
|
#ifdef DEBUG
|
2018-06-12 23:29:58 +00:00
|
|
|
fprintf(stdout,"Notify: disconnected\n");
|
2018-06-14 15:28:10 +00:00
|
|
|
#endif
|
2018-06-12 23:29:58 +00:00
|
|
|
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, n);
|
2014-02-16 18:46:53 +00:00
|
|
|
}
|
2018-07-10 23:06:31 +00:00
|
|
|
else {
|
2018-06-14 15:28:10 +00:00
|
|
|
#ifdef DEBUG
|
2018-06-12 23:29:58 +00:00
|
|
|
fprintf(stdout,"Notify: %s\n",mpd.buf);
|
2018-06-14 15:28:10 +00:00
|
|
|
#endif
|
2018-06-18 21:24:35 +00:00
|
|
|
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, strlen(mpd.buf));
|
2018-06-12 00:07:03 +00:00
|
|
|
|
2018-07-24 22:58:29 +00:00
|
|
|
if (s->song_id != mpd.song_id) {
|
2018-06-14 22:00:54 +00:00
|
|
|
n=mympd_put_current_song(mpd.buf);
|
2018-06-14 15:28:10 +00:00
|
|
|
#ifdef DEBUG
|
2018-06-12 23:29:58 +00:00
|
|
|
fprintf(stdout,"Notify: %s\n",mpd.buf);
|
2018-06-14 15:28:10 +00:00
|
|
|
#endif
|
2018-06-12 23:29:58 +00:00
|
|
|
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, n);
|
2018-06-11 17:33:11 +00:00
|
|
|
s->song_id = mpd.song_id;
|
2014-02-16 18:46:53 +00:00
|
|
|
}
|
2018-06-11 17:33:11 +00:00
|
|
|
|
2018-07-24 22:58:29 +00:00
|
|
|
if (s->queue_version != mpd.queue_version) {
|
2018-06-12 23:29:58 +00:00
|
|
|
n=snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"update_queue\"}");
|
2018-06-14 15:28:10 +00:00
|
|
|
#ifdef DEBUG
|
2018-06-12 23:29:58 +00:00
|
|
|
fprintf(stdout,"Notify: update_queue\n");
|
2018-06-14 15:28:10 +00:00
|
|
|
#endif
|
2018-06-12 23:29:58 +00:00
|
|
|
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, n);
|
2018-06-11 17:33:11 +00:00
|
|
|
s->queue_version = mpd.queue_version;
|
2014-02-16 18:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-12 00:07:03 +00:00
|
|
|
return 0;
|
2014-02-16 18:46:53 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 23:06:31 +00:00
|
|
|
void mympd_poll(struct mg_mgr *s) {
|
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 */
|
2014-02-16 18:46:53 +00:00
|
|
|
fprintf(stdout, "MPD Connecting to %s:%d\n", mpd.host, mpd.port);
|
|
|
|
mpd.conn = mpd_connection_new(mpd.host, mpd.port, 3000);
|
|
|
|
if (mpd.conn == NULL) {
|
|
|
|
fprintf(stderr, "Out of memory.");
|
|
|
|
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) {
|
|
|
|
fprintf(stderr, "MPD connection: %s\n", mpd_connection_get_error_message(mpd.conn));
|
2018-07-10 23:06:31 +00:00
|
|
|
for (struct mg_connection *c = mg_next(s, NULL); c != NULL; c = mg_next(s, c)) {
|
2018-06-14 22:00:54 +00:00
|
|
|
mympd_notify_callback(c, mpd_connection_get_error_message(mpd.conn));
|
2015-04-21 03:11:03 +00:00
|
|
|
}
|
2014-02-16 18:46:53 +00:00
|
|
|
mpd.conn_state = MPD_FAILURE;
|
2013-11-09 01:07:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-24 22:58:29 +00:00
|
|
|
if (mpd.password && !mpd_run_password(mpd.conn, mpd.password)) {
|
2014-02-16 18:46:53 +00:00
|
|
|
fprintf(stderr, "MPD connection: %s\n", mpd_connection_get_error_message(mpd.conn));
|
2018-07-10 23:06:31 +00:00
|
|
|
for (struct mg_connection *c = mg_next(s, NULL); c != NULL; c = mg_next(s, c)) {
|
2018-06-14 22:00:54 +00:00
|
|
|
mympd_notify_callback(c, mpd_connection_get_error_message(mpd.conn));
|
2015-04-21 03:11:03 +00:00
|
|
|
}
|
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
|
|
|
fprintf(stderr, "MPD connected.\n");
|
2014-10-19 18:27:11 +00:00
|
|
|
mpd_connection_set_timeout(mpd.conn, 10000);
|
2014-02-16 18:46:53 +00:00
|
|
|
mpd.conn_state = MPD_CONNECTED;
|
2013-11-09 01:07:03 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MPD_FAILURE:
|
2014-02-16 18:46:53 +00:00
|
|
|
fprintf(stderr, "MPD connection failed.\n");
|
2013-11-09 01:07:03 +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-06-14 22:00:54 +00:00
|
|
|
mpd.buf_size = mympd_put_state(mpd.buf, &mpd.song_id, &mpd.next_song_id, &mpd.queue_version);
|
2018-07-10 23:06:31 +00:00
|
|
|
for (struct mg_connection *c = mg_next(s, NULL); c != NULL; c = mg_next(s, c)) {
|
2018-06-14 22:00:54 +00:00
|
|
|
mympd_notify_callback(c, NULL);
|
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-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));
|
|
|
|
else
|
|
|
|
str = "-";
|
2014-11-20 19:17:16 +00:00
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2018-07-10 23:06:31 +00:00
|
|
|
int mympd_put_state(char *buffer, int *current_song_id, int *next_song_id, unsigned *queue_version) {
|
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
|
|
|
struct mpd_output *output;
|
|
|
|
int len;
|
|
|
|
int nr;
|
|
|
|
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) {
|
2014-02-16 18:46:53 +00:00
|
|
|
fprintf(stderr, "MPD mpd_run_status: %s\n", mpd_connection_get_error_message(mpd.conn));
|
|
|
|
mpd.conn_state = MPD_FAILURE;
|
2013-11-09 01:07:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-05-31 23:05:49 +00:00
|
|
|
if (status) {
|
|
|
|
audioformat = mpd_status_get_audio_format(status);
|
|
|
|
}
|
2018-06-17 22:42:22 +00:00
|
|
|
|
|
|
|
len = json_printf(&out,"{type:state, data:{"
|
|
|
|
"state:%d, volume:%d, songpos: %d, elapsedTime: %d, "
|
|
|
|
"totalTime:%d, currentsongid: %d, kbitrate: %d, "
|
|
|
|
"audioformat: { sample_rate: %d, bits: %d, channels: %d}, "
|
|
|
|
"queue_length: %d, nextsongpos: %d, nextsongid: %d, "
|
|
|
|
"queue_version: %d",
|
|
|
|
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),
|
|
|
|
mpd_status_get_song_id(status),
|
|
|
|
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),
|
|
|
|
mpd_status_get_queue_version(status)
|
|
|
|
);
|
|
|
|
|
2018-06-28 22:51:24 +00:00
|
|
|
len += json_printf(&out, ",outputs: [");
|
2018-06-14 19:56:12 +00:00
|
|
|
|
|
|
|
mpd_send_outputs(mpd.conn);
|
2018-06-17 22:42:22 +00:00
|
|
|
nr=0;
|
|
|
|
while ((output = mpd_recv_output(mpd.conn)) != NULL) {
|
|
|
|
if (nr++) len += json_printf(&out, ",");
|
2018-06-28 22:51:24 +00:00
|
|
|
len += json_printf(&out, "{id: %d, state: %d}",
|
2018-06-17 22:42:22 +00:00
|
|
|
mpd_output_get_id(output),
|
|
|
|
mpd_output_get_enabled(output)
|
|
|
|
);
|
|
|
|
mpd_output_free(output);
|
2018-06-14 19:56:12 +00:00
|
|
|
}
|
|
|
|
if (!mpd_response_finish(mpd.conn)) {
|
|
|
|
fprintf(stderr, "MPD outputs: %s\n", mpd_connection_get_error_message(mpd.conn));
|
|
|
|
mpd_connection_clear_error(mpd.conn);
|
|
|
|
}
|
|
|
|
|
2018-06-28 22:51:24 +00:00
|
|
|
len += json_printf(&out, "]}}");
|
2013-11-09 01:07:03 +00:00
|
|
|
|
2014-01-17 15:26:26 +00:00
|
|
|
*current_song_id = mpd_status_get_song_id(status);
|
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);
|
2013-11-09 01:07:03 +00:00
|
|
|
mpd_status_free(status);
|
2018-06-17 22:42:22 +00:00
|
|
|
|
|
|
|
if (len > MAX_SIZE) fprintf(stderr,"Buffer truncated\n");
|
|
|
|
return len;
|
2013-11-04 17:18:38 +00:00
|
|
|
}
|
|
|
|
|
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-07-18 22:38:46 +00:00
|
|
|
len = json_printf(&out, "{type: welcome, data: { version: %Q}}", MYMPD_VERSION);
|
2018-06-14 20:19:36 +00:00
|
|
|
|
2018-06-17 22:42:22 +00:00
|
|
|
if (len > MAX_SIZE) fprintf(stderr,"Buffer truncated\n");
|
2018-06-14 20:19:36 +00:00
|
|
|
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-05-31 20:23:57 +00:00
|
|
|
char *replaygain;
|
2018-06-17 22:42:22 +00:00
|
|
|
int len;
|
2018-06-21 21:29:49 +00:00
|
|
|
int je;
|
2018-06-17 22:42:22 +00:00
|
|
|
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
|
2018-06-21 16:57:40 +00:00
|
|
|
struct mympd_state { int a; int b; } state = { .a = 0, .b = 0 };
|
2018-07-24 22:58:29 +00:00
|
|
|
if (access( mpd.statefile, F_OK ) != -1 ) {
|
2018-06-21 16:57:40 +00:00
|
|
|
char *content = json_fread(mpd.statefile);
|
2018-06-21 21:29:49 +00:00
|
|
|
je = json_scanf(content, strlen(content), "{notificationWeb: %d, notificationPage: %d}", &state.a, &state.b);
|
|
|
|
if (je != 2) {
|
|
|
|
state.a=0;
|
|
|
|
state.b=0;
|
|
|
|
}
|
2018-06-21 16:57:40 +00:00
|
|
|
} else {
|
|
|
|
state.a=0;
|
|
|
|
state.b=0;
|
|
|
|
}
|
2018-05-29 21:05:34 +00:00
|
|
|
|
|
|
|
status = mpd_run_status(mpd.conn);
|
|
|
|
if (!status) {
|
|
|
|
fprintf(stderr, "MPD mpd_run_status: %s\n", mpd_connection_get_error_message(mpd.conn));
|
|
|
|
mpd.conn_state = MPD_FAILURE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-31 20:23:57 +00:00
|
|
|
mpd_send_command(mpd.conn, "replay_gain_status", NULL);
|
|
|
|
struct mpd_pair *pair;
|
|
|
|
while ((pair = mpd_recv_pair(mpd.conn)) != NULL) {
|
|
|
|
replaygain=strdup(pair->value);
|
|
|
|
mpd_return_pair(mpd.conn, pair);
|
|
|
|
}
|
2018-06-21 16:57:40 +00:00
|
|
|
|
2018-06-17 22:42:22 +00:00
|
|
|
len = json_printf(&out,
|
|
|
|
"{type:settings, data:{"
|
|
|
|
"repeat:%d, single:%d, crossfade:%d, consume:%d, random:%d, "
|
|
|
|
"mixrampdb: %f, mixrampdelay: %f, mpdhost : %Q, mpdport: %d, passwort_set: %B, "
|
2018-06-21 16:57:40 +00:00
|
|
|
"streamport: %d, coverimage: %Q, max_elements_per_page: %d, replaygain: %Q,"
|
|
|
|
"notificationWeb: %d, notificationPage: %d"
|
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-05-29 21:55:24 +00:00
|
|
|
mpd.host, mpd.port,
|
|
|
|
mpd.password ? "true" : "false",
|
|
|
|
streamport, coverimage,
|
2018-05-31 20:23:57 +00:00
|
|
|
MAX_ELEMENTS_PER_PAGE,
|
2018-06-21 16:57:40 +00:00
|
|
|
replaygain,
|
|
|
|
state.a,
|
|
|
|
state.b
|
2018-05-29 21:05:34 +00:00
|
|
|
);
|
|
|
|
mpd_status_free(status);
|
2018-06-17 22:42:22 +00:00
|
|
|
|
|
|
|
if (len > MAX_SIZE) fprintf(stderr,"Buffer truncated\n");
|
2018-05-29 21:05:34 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-10 23:06:31 +00:00
|
|
|
int mympd_put_outputnames(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-06-28 22:51:24 +00:00
|
|
|
len = json_printf(&out,"{type: outputnames, data: { outputs: [");
|
2018-06-14 19:56:12 +00:00
|
|
|
|
2018-06-17 22:42:22 +00:00
|
|
|
mpd_send_outputs(mpd.conn);
|
|
|
|
nr=0;
|
|
|
|
while ((output = mpd_recv_output(mpd.conn)) != NULL) {
|
|
|
|
if (nr++) len += json_printf(&out, ",");
|
2018-06-28 22:51:24 +00:00
|
|
|
len += json_printf(&out,"{id: %d, name: %Q}",
|
2018-06-17 22:42:22 +00:00
|
|
|
mpd_output_get_id(output),
|
|
|
|
mpd_output_get_name(output)
|
|
|
|
);
|
|
|
|
mpd_output_free(output);
|
2015-04-28 09:08:21 +00:00
|
|
|
}
|
2015-05-01 22:07:37 +00:00
|
|
|
if (!mpd_response_finish(mpd.conn)) {
|
2015-05-02 12:36:02 +00:00
|
|
|
fprintf(stderr, "MPD outputs: %s\n", mpd_connection_get_error_message(mpd.conn));
|
2015-05-01 22:07:37 +00:00
|
|
|
mpd_connection_clear_error(mpd.conn);
|
|
|
|
}
|
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-06-17 22:42:22 +00:00
|
|
|
if (len > MAX_SIZE) fprintf(stderr,"Buffer truncated\n");
|
|
|
|
return len;
|
2015-04-28 09:08:21 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 23:06:31 +00:00
|
|
|
int mympd_get_cover(const char *uri, char *cover, int cover_len) {
|
2018-06-22 00:01:15 +00:00
|
|
|
char *path=strdup(uri);
|
|
|
|
int len;
|
|
|
|
if (strncasecmp("http:",path,5) == 0 ) {
|
|
|
|
len=snprintf(cover,cover_len,"/assets/coverimage-httpstream.png");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dirname(path);
|
|
|
|
snprintf(cover,cover_len,"%s/library/%s/%s",SRC_PATH,path,coverimage);
|
|
|
|
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,coverimage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
len = json_printf(&out,"{type: result, data: ok}");
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2018-06-22 00:01:15 +00:00
|
|
|
mympd_get_cover(mpd_song_get_uri(song),cover,500);
|
2013-11-09 01:07:03 +00:00
|
|
|
|
2018-06-18 16:53:30 +00:00
|
|
|
len = json_printf(&out,"{type: song_change, data: { pos: %d, title: %Q, "
|
|
|
|
"artist: %Q, album: %Q, uri: %Q, currentsongid: %d, albumartist: %Q, "
|
2018-06-22 00:01:15 +00:00
|
|
|
"duration: %d, cover: %Q }}",
|
2018-06-18 16:53:30 +00:00
|
|
|
mpd_song_get_pos(song),
|
2018-07-09 17:49:55 +00:00
|
|
|
mympd_get_tag(song, MPD_TAG_TITLE),
|
|
|
|
mympd_get_tag(song, MPD_TAG_ARTIST),
|
|
|
|
mympd_get_tag(song, MPD_TAG_ALBUM),
|
2018-06-18 16:53:30 +00:00
|
|
|
mpd_song_get_uri(song),
|
|
|
|
mpd.song_id,
|
2018-07-09 17:49:55 +00:00
|
|
|
mympd_get_tag(song, MPD_TAG_ALBUM_ARTIST),
|
2018-06-22 00:01:15 +00:00
|
|
|
mpd_song_get_duration(song),
|
|
|
|
cover
|
2018-06-18 16:53:30 +00:00
|
|
|
);
|
2018-06-07 22:26:35 +00:00
|
|
|
|
2013-11-09 01:07:03 +00:00
|
|
|
mpd_song_free(song);
|
2014-02-16 18:46:53 +00:00
|
|
|
mpd_response_finish(mpd.conn);
|
2013-11-09 01:07:03 +00:00
|
|
|
|
2018-06-18 16:53:30 +00:00
|
|
|
if (len > MAX_SIZE) fprintf(stderr,"Buffer truncated\n");
|
|
|
|
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: {");
|
|
|
|
mpd_send_list_all_meta(mpd.conn, uri);
|
|
|
|
while ((entity = mpd_recv_entity(mpd.conn)) != NULL) {
|
|
|
|
song = mpd_entity_get_song(entity);
|
|
|
|
mympd_get_cover(mpd_song_get_uri(song),cover,500);
|
2018-07-09 17:49:55 +00:00
|
|
|
len += json_printf(&out, "duration: %d, artist: %Q, album: %Q, title: %Q, albumartist: %Q, cover: %Q, uri: %Q, genre: %Q, track: %Q, date: %Q",
|
2018-07-09 17:28:28 +00:00
|
|
|
mpd_song_get_duration(song),
|
2018-07-09 17:49:55 +00:00
|
|
|
mympd_get_tag(song, MPD_TAG_ARTIST),
|
|
|
|
mympd_get_tag(song, MPD_TAG_ALBUM),
|
|
|
|
mympd_get_tag(song, MPD_TAG_TITLE),
|
|
|
|
mympd_get_tag(song, MPD_TAG_ALBUM_ARTIST),
|
2018-07-09 17:28:28 +00:00
|
|
|
cover,
|
|
|
|
uri,
|
2018-07-09 17:49:55 +00:00
|
|
|
mympd_get_tag(song, MPD_TAG_GENRE),
|
|
|
|
mympd_get_tag(song, MPD_TAG_TRACK),
|
|
|
|
mympd_get_tag(song, MPD_TAG_DATE)
|
2018-07-09 17:28:28 +00:00
|
|
|
);
|
|
|
|
mpd_entity_free(entity);
|
|
|
|
}
|
|
|
|
len += json_printf(&out, "}}");
|
|
|
|
|
|
|
|
if (len > MAX_SIZE) fprintf(stderr,"Buffer truncated\n");
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2018-07-10 23:06:31 +00:00
|
|
|
int mympd_put_queue(char *buffer, unsigned int offset) {
|
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);
|
2013-11-04 17:18:38 +00:00
|
|
|
|
2018-05-01 21:37:34 +00:00
|
|
|
if (!mpd_send_list_queue_range_meta(mpd.conn, 0, -1))
|
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-06-18 16:53: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-05-29 21:55:24 +00:00
|
|
|
entity_count ++;
|
2018-06-18 16:53:30 +00:00
|
|
|
if (entity_count > offset && entity_count <= offset+MAX_ELEMENTS_PER_PAGE) {
|
|
|
|
if (entities_returned ++) len += json_printf(&out,",");
|
2018-07-09 17:28:28 +00:00
|
|
|
len += json_printf(&out, "{ id: %d, pos: %d, duration: %d, artist: %Q, album: %Q, title: %Q, uri: %Q }",
|
2018-06-18 16:53:30 +00:00
|
|
|
mpd_song_get_id(song),
|
|
|
|
mpd_song_get_pos(song),
|
|
|
|
mpd_song_get_duration(song),
|
2018-07-09 17:49:55 +00:00
|
|
|
mympd_get_tag(song, MPD_TAG_ARTIST),
|
|
|
|
mympd_get_tag(song, MPD_TAG_ALBUM),
|
|
|
|
mympd_get_tag(song, MPD_TAG_TITLE),
|
2018-07-09 17:28:28 +00:00
|
|
|
mpd_song_get_uri(song)
|
2018-06-18 16:53:30 +00:00
|
|
|
);
|
2018-05-01 21:37:34 +00:00
|
|
|
}
|
2013-11-09 01:07:03 +00:00
|
|
|
}
|
|
|
|
mpd_entity_free(entity);
|
|
|
|
}
|
|
|
|
|
2018-07-15 21:49:57 +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,
|
|
|
|
entity_count,
|
|
|
|
offset,
|
2018-07-15 21:49:57 +00:00
|
|
|
entities_returned,
|
|
|
|
mpd.queue_version
|
2018-06-18 16:53:30 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (len > MAX_SIZE) fprintf(stderr,"Buffer truncated\n");
|
|
|
|
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-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-06-18 16:53:30 +00:00
|
|
|
len = json_printf(&out, "{ type: browse, data: [ ");
|
2013-11-07 09:09:40 +00:00
|
|
|
|
2014-02-16 18:46:53 +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-05-29 21:55:24 +00:00
|
|
|
entity_count ++;
|
2018-07-24 22:58:29 +00:00
|
|
|
if (entity_count > offset && entity_count <= offset+MAX_ELEMENTS_PER_PAGE) {
|
2018-05-29 20:12:11 +00:00
|
|
|
switch (mpd_entity_get_type(entity)) {
|
|
|
|
case MPD_ENTITY_TYPE_UNKNOWN:
|
2018-06-03 08:52:38 +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-06-10 21:15:33 +00:00
|
|
|
if (strncmp(filter,"-",1) == 0 || strncasecmp(filter,entityName,1) == 0 ||
|
2018-06-03 08:52:38 +00:00
|
|
|
( strncmp(filter,"0",1) == 0 && isalpha(*entityName) == 0 )
|
|
|
|
) {
|
2018-06-18 16:53:30 +00:00
|
|
|
if (entities_returned ++) len += json_printf(&out,",");
|
2018-06-25 22:41:44 +00:00
|
|
|
len += json_printf(&out, "{type: song, uri: %Q, album: %Q, artist: %Q, duration: %d, title: %Q, name: %Q }",
|
2018-06-18 16:53:30 +00:00
|
|
|
mpd_song_get_uri(song),
|
2018-07-09 17:49:55 +00:00
|
|
|
mympd_get_tag(song, MPD_TAG_ALBUM),
|
|
|
|
mympd_get_tag(song, MPD_TAG_ARTIST),
|
2018-06-18 16:53:30 +00:00
|
|
|
mpd_song_get_duration(song),
|
2018-06-25 22:41:44 +00:00
|
|
|
entityName,
|
2018-06-18 16:53:30 +00:00
|
|
|
entityName
|
|
|
|
);
|
2018-06-03 08:52:38 +00:00
|
|
|
} else {
|
|
|
|
entity_count --;
|
|
|
|
}
|
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, '/');
|
|
|
|
if (dirName != NULL) {
|
|
|
|
dirName ++;
|
|
|
|
} else {
|
2018-06-18 16:53:30 +00:00
|
|
|
dirName = strdup(entityName);
|
2018-06-03 08:52:38 +00:00
|
|
|
}
|
2018-06-10 21:15:33 +00:00
|
|
|
if (strncmp(filter,"-",1) == 0 || strncasecmp(filter,dirName,1) == 0 ||
|
2018-06-03 08:55:01 +00:00
|
|
|
( strncmp(filter,"0",1) == 0 && isalpha(*dirName) == 0 )
|
|
|
|
) {
|
2018-06-18 16:53:30 +00:00
|
|
|
if (entities_returned ++) len += json_printf(&out,",");
|
2018-06-25 22:41:44 +00:00
|
|
|
len += json_printf(&out, "{type: dir, uri: %Q, name: %Q }",
|
|
|
|
entityName,
|
|
|
|
dirName
|
2018-06-18 16:53:30 +00:00
|
|
|
);
|
2018-06-03 08:52:38 +00:00
|
|
|
} else {
|
|
|
|
entity_count --;
|
|
|
|
}
|
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) {
|
|
|
|
plName ++;
|
|
|
|
} else {
|
2018-07-10 23:06:31 +00:00
|
|
|
plName = strdup(entityName);
|
2018-06-03 10:50:50 +00:00
|
|
|
}
|
2018-06-10 21:15:33 +00:00
|
|
|
if (strncmp(filter,"-",1) == 0 || strncasecmp(filter,plName,1) == 0 ||
|
2018-06-03 10:50:50 +00:00
|
|
|
( strncmp(filter,"0",1) == 0 && isalpha(*plName) == 0 )
|
2018-06-03 08:55:01 +00:00
|
|
|
) {
|
2018-06-18 16:53:30 +00:00
|
|
|
if (entities_returned ++) len += json_printf(&out,",");
|
2018-06-25 22:41:44 +00:00
|
|
|
len += json_printf(&out, "{ type: plist, uri: %Q, name: %Q }",
|
|
|
|
entityName,
|
|
|
|
plName
|
2018-06-18 16:53:30 +00:00
|
|
|
);
|
2018-06-03 08:52:38 +00:00
|
|
|
} else {
|
|
|
|
entity_count --;
|
|
|
|
}
|
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)) {
|
|
|
|
fprintf(stderr, "MPD mpd_send_list_meta: %s\n", mpd_connection_get_error_message(mpd.conn));
|
|
|
|
mpd.conn_state = MPD_FAILURE;
|
2013-11-07 09:09:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-18 16:53:30 +00:00
|
|
|
len += json_printf(&out, "], totalEntities: %d, offset: %d, returnedEntities: %d, filter: %Q }",
|
|
|
|
entity_count,
|
|
|
|
offset,
|
|
|
|
entities_returned,
|
|
|
|
filter
|
|
|
|
);
|
|
|
|
|
|
|
|
if (len > MAX_SIZE) fprintf(stderr,"Buffer truncated\n");
|
|
|
|
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-06-18 16:53:30 +00:00
|
|
|
len = json_printf(&out, "{type: listDBtags, data: [ ");
|
2018-06-04 18:31:05 +00:00
|
|
|
while((pair = mpd_recv_pair_tag(mpd.conn, mpd_tag_name_parse(mpdtagtype))) != NULL) {
|
|
|
|
entity_count ++;
|
2018-07-24 22:58:29 +00:00
|
|
|
if (entity_count > offset && entity_count <= offset+MAX_ELEMENTS_PER_PAGE) {
|
2018-06-10 21:15:33 +00:00
|
|
|
if (strncmp(filter,"-",1) == 0 || strncasecmp(filter,pair->value,1) == 0 ||
|
2018-06-04 18:31:05 +00:00
|
|
|
( strncmp(filter,"0",1) == 0 && isalpha(*pair->value) == 0 )
|
|
|
|
) {
|
2018-06-18 16:53:30 +00:00
|
|
|
if (entities_returned ++) len += json_printf(&out,", ");
|
|
|
|
len += json_printf(&out, "{type: %Q, value: %Q }",
|
|
|
|
mpdtagtype,
|
|
|
|
pair->value
|
|
|
|
);
|
2018-06-04 18:31:05 +00:00
|
|
|
} else {
|
|
|
|
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, "
|
|
|
|
"tagtype: %Q, searchtagtype: %Q, searchstr: %Q, filter: %Q }",
|
|
|
|
entity_count,
|
|
|
|
offset,
|
|
|
|
entities_returned,
|
|
|
|
mpdtagtype,
|
|
|
|
mpdsearchtagtype,
|
|
|
|
searchstr,
|
|
|
|
filter
|
|
|
|
);
|
|
|
|
|
|
|
|
if (len > MAX_SIZE) fprintf(stderr,"Buffer truncated\n");
|
|
|
|
return len;
|
2018-06-03 16:36:06 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 23:06:31 +00:00
|
|
|
int mympd_put_songs_in_album(char *buffer, char *albumartist, char *album) {
|
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-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-07-24 22:58:29 +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");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mpd_search_add_tag_constraint(mpd.conn, MPD_OPERATOR_DEFAULT, MPD_TAG_ALBUM_ARTIST, albumartist) == false)
|
|
|
|
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-06-18 16:53:30 +00:00
|
|
|
len = json_printf(&out, "{ type: listTitles, data: [ ");
|
2018-06-03 16:36:06 +00:00
|
|
|
|
|
|
|
while((song = mpd_recv_song(mpd.conn)) != NULL) {
|
|
|
|
entity_count ++;
|
2018-07-24 22:58:29 +00:00
|
|
|
if (entity_count <= MAX_ELEMENTS_PER_PAGE) {
|
2018-06-18 16:53:30 +00:00
|
|
|
if (entities_returned ++) len += json_printf(&out, ", ");
|
2018-06-22 00:01:15 +00:00
|
|
|
else mympd_get_cover(mpd_song_get_uri(song),cover,500);
|
2018-06-18 16:53:30 +00:00
|
|
|
len += json_printf(&out, "{ type: song, uri: %Q, duration: %d, title: %Q, track: %Q }",
|
|
|
|
mpd_song_get_uri(song),
|
|
|
|
mpd_song_get_duration(song),
|
2018-07-09 17:49:55 +00:00
|
|
|
mympd_get_tag(song, MPD_TAG_TITLE),
|
|
|
|
mympd_get_tag(song, MPD_TAG_TRACK)
|
2018-06-18 16:53:30 +00:00
|
|
|
);
|
2018-06-03 16:36:06 +00:00
|
|
|
}
|
|
|
|
mpd_song_free(song);
|
|
|
|
}
|
|
|
|
|
2018-06-22 00:01:15 +00:00
|
|
|
len += json_printf(&out, "], totalEntities: %d, returnedEntities: %d, albumartist: %Q, album: %Q, cover: %Q }",
|
2018-06-18 16:53:30 +00:00
|
|
|
entity_count,
|
|
|
|
entities_returned,
|
|
|
|
albumartist,
|
2018-06-22 00:01:15 +00:00
|
|
|
album,
|
|
|
|
cover
|
2018-06-18 16:53:30 +00:00
|
|
|
);
|
2018-06-03 16:36:06 +00:00
|
|
|
}
|
2018-06-18 16:53:30 +00:00
|
|
|
|
|
|
|
if (len > MAX_SIZE) fprintf(stderr,"Buffer truncated\n");
|
|
|
|
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;
|
|
|
|
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-06-18 16:53:30 +00:00
|
|
|
len = json_printf(&out, "{ type: playlists, data: [ ");
|
2018-05-28 22:05:56 +00:00
|
|
|
|
|
|
|
while((pl = mpd_recv_playlist(mpd.conn)) != NULL) {
|
2018-05-29 21:55:24 +00:00
|
|
|
entity_count ++;
|
2018-07-24 22:58:29 +00:00
|
|
|
if (entity_count > offset && entity_count <= offset+MAX_ELEMENTS_PER_PAGE) {
|
2018-06-04 18:31:05 +00:00
|
|
|
plpath = mpd_playlist_get_path(pl);
|
2018-06-10 21:15:33 +00:00
|
|
|
if (strncmp(filter,"-",1) == 0 || strncasecmp(filter,plpath,1) == 0 ||
|
2018-06-04 18:31:05 +00:00
|
|
|
( strncmp(filter,"0",1) == 0 && isalpha(*plpath) == 0 )
|
|
|
|
) {
|
2018-06-18 16:53:30 +00:00
|
|
|
if (entities_returned ++) len += json_printf(&out, ", ");
|
2018-06-25 22:41:44 +00:00
|
|
|
len += json_printf(&out, "{ type: plist, uri: %Q, name: %Q, last_modified: %d }",
|
|
|
|
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 {
|
|
|
|
entity_count --;
|
|
|
|
}
|
2018-05-28 22:05:56 +00:00
|
|
|
}
|
|
|
|
mpd_playlist_free(pl);
|
|
|
|
}
|
|
|
|
|
2018-06-03 16:36:06 +00:00
|
|
|
if (mpd_connection_get_error(mpd.conn) != MPD_ERROR_SUCCESS || !mpd_response_finish(mpd.conn))
|
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_send_list_playlists");
|
|
|
|
|
2018-06-18 16:53:30 +00:00
|
|
|
len += json_printf(&out, "], totalEntities: %d, offset: %d, returnedEntities: %d }",
|
|
|
|
entity_count,
|
|
|
|
offset,
|
|
|
|
entities_returned
|
|
|
|
);
|
|
|
|
|
|
|
|
if (len > MAX_SIZE) fprintf(stderr,"Buffer truncated\n");
|
|
|
|
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;
|
|
|
|
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");
|
|
|
|
|
|
|
|
len = json_printf(&out, "{ type: playlist_detail, data: [ ");
|
|
|
|
|
|
|
|
while((entity = mpd_recv_entity(mpd.conn)) != NULL) {
|
|
|
|
const struct mpd_song *song;
|
|
|
|
entity_count ++;
|
|
|
|
if (entity_count > offset && entity_count <= offset+MAX_ELEMENTS_PER_PAGE) {
|
|
|
|
song = mpd_entity_get_song(entity);
|
|
|
|
entityName = mympd_get_tag(song, MPD_TAG_TITLE);
|
|
|
|
if (strncmp(filter,"-",1) == 0 || strncasecmp(filter,entityName,1) == 0 ||
|
|
|
|
( strncmp(filter,"0",1) == 0 && isalpha(*entityName) == 0 )
|
|
|
|
) {
|
|
|
|
if (entities_returned ++) len += json_printf(&out,",");
|
|
|
|
len += json_printf(&out, "{type: song, uri: %Q, album: %Q, artist: %Q, duration: %d, title: %Q, name: %Q }",
|
|
|
|
mpd_song_get_uri(song),
|
2018-07-26 21:48:03 +00:00
|
|
|
mympd_get_tag(song, MPD_TAG_ALBUM),
|
|
|
|
mympd_get_tag(song, MPD_TAG_ARTIST),
|
|
|
|
mpd_song_get_duration(song),
|
|
|
|
entityName,
|
|
|
|
entityName
|
2018-07-22 19:00:26 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
entity_count --;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mpd_entity_free(entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
len += json_printf(&out, "], totalEntities: %d, offset: %d, returnedEntities: %d, filter: %Q, uri: %Q }",
|
|
|
|
entity_count,
|
|
|
|
offset,
|
|
|
|
entities_returned,
|
|
|
|
filter,
|
|
|
|
uri
|
|
|
|
);
|
|
|
|
|
|
|
|
if (len > MAX_SIZE)
|
|
|
|
fprintf(stderr,"Buffer truncated\n");
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2018-07-10 23:06:31 +00:00
|
|
|
int mympd_search(char *buffer, char *mpdtagtype, unsigned int offset, char *searchstr) {
|
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-06-18 16:53:30 +00:00
|
|
|
int len;
|
|
|
|
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
|
2014-02-22 00:57:26 +00:00
|
|
|
|
2018-07-24 22:58:29 +00:00
|
|
|
if (mpd_search_db_songs(mpd.conn, false) == false)
|
2018-05-31 23:05:49 +00:00
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_search_db_songs");
|
2018-05-29 20:12:11 +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)
|
2014-02-22 00:57:26 +00:00
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_search_commit");
|
|
|
|
else {
|
2018-06-18 16:53:30 +00:00
|
|
|
len = json_printf(&out, "{ type: search, data: [ ");
|
2014-02-22 00:57:26 +00:00
|
|
|
|
|
|
|
while((song = mpd_recv_song(mpd.conn)) != NULL) {
|
2018-05-29 20:12:11 +00:00
|
|
|
entity_count ++;
|
2018-07-24 22:58:29 +00:00
|
|
|
if (entity_count > offset && entity_count <= offset+MAX_ELEMENTS_PER_PAGE) {
|
2018-06-18 16:53:30 +00:00
|
|
|
if (entities_returned ++) len += json_printf(&out, ", ");
|
2018-06-25 22:41:44 +00:00
|
|
|
len += json_printf(&out, "{ type: song, uri: %Q, album: %Q, artist: %Q, duration: %d, title: %Q, name: %Q }",
|
2018-06-18 16:53:30 +00:00
|
|
|
mpd_song_get_uri(song),
|
2018-07-09 17:49:55 +00:00
|
|
|
mympd_get_tag(song, MPD_TAG_ALBUM),
|
|
|
|
mympd_get_tag(song, MPD_TAG_ARTIST),
|
2018-06-18 16:53:30 +00:00
|
|
|
mpd_song_get_duration(song),
|
2018-07-09 17:49:55 +00:00
|
|
|
mympd_get_tag(song, MPD_TAG_TITLE),
|
|
|
|
mympd_get_tag(song, MPD_TAG_TITLE)
|
2018-06-18 16:53:30 +00:00
|
|
|
);
|
2018-05-24 22:21:19 +00:00
|
|
|
}
|
2018-05-27 12:37:50 +00:00
|
|
|
mpd_song_free(song);
|
|
|
|
}
|
2018-06-18 16:53:30 +00:00
|
|
|
|
|
|
|
len += json_printf(&out, "], totalEntities: %d, offset: %d, returnedEntities: %d, mpdtagtype: %Q }",
|
|
|
|
entity_count,
|
|
|
|
offset,
|
|
|
|
entities_returned,
|
|
|
|
mpdtagtype
|
|
|
|
);
|
2018-05-24 22:21:19 +00:00
|
|
|
}
|
2018-06-18 16:53:30 +00:00
|
|
|
|
2018-07-22 19:00:26 +00:00
|
|
|
if (len > MAX_SIZE)
|
|
|
|
fprintf(stderr,"Buffer truncated\n");
|
2018-06-18 16:53:30 +00:00
|
|
|
return len;
|
2018-05-24 22:21:19 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 23:06:31 +00:00
|
|
|
int mympd_search_add(char *buffer,char *mpdtagtype, char *searchstr) {
|
2018-05-31 23:05:49 +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);
|
|
|
|
len = 0;
|
2018-05-31 23:05:49 +00:00
|
|
|
|
2018-07-24 22:58:29 +00:00
|
|
|
if (mpd_search_add_db_songs(mpd.conn, false) == false) {
|
2018-05-31 23:05:49 +00:00
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_search_add_db_songs");
|
|
|
|
}
|
|
|
|
|
|
|
|
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-31 23:05:49 +00:00
|
|
|
RETURN_ERROR_AND_RECOVER("mpd_search_commit");
|
|
|
|
|
|
|
|
while((song = mpd_recv_song(mpd.conn)) != NULL) {
|
|
|
|
mpd_song_free(song);
|
|
|
|
}
|
|
|
|
|
2018-07-22 19:00:26 +00:00
|
|
|
if (len > MAX_SIZE)
|
|
|
|
fprintf(stderr,"Buffer truncated\n");
|
2018-06-18 16:53:30 +00:00
|
|
|
return len;
|
2018-05-31 23:05:49 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
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-06-18 16:53:30 +00:00
|
|
|
len = json_printf(&out, "{ type: queuesearch, data: [ ");
|
2018-05-24 22:21:19 +00:00
|
|
|
|
|
|
|
while((song = mpd_recv_song(mpd.conn)) != NULL) {
|
2018-05-29 21:55:24 +00:00
|
|
|
entity_count ++;
|
2018-07-24 22:58:29 +00:00
|
|
|
if (entity_count > offset && entity_count <= offset+MAX_ELEMENTS_PER_PAGE) {
|
2018-06-18 16:53:30 +00:00
|
|
|
if (entities_returned ++) len += json_printf(&out, ", ");
|
|
|
|
len += json_printf(&out, "{ type: song, id: %d, pos: %d, album: %Q, artist: %Q, duration: %d, title: %Q }",
|
|
|
|
mpd_song_get_id(song),
|
|
|
|
mpd_song_get_pos(song),
|
2018-07-09 17:49:55 +00:00
|
|
|
mympd_get_tag(song, MPD_TAG_ALBUM),
|
|
|
|
mympd_get_tag(song, MPD_TAG_ARTIST),
|
2018-06-18 16:53:30 +00:00
|
|
|
mpd_song_get_duration(song),
|
2018-07-09 17:49:55 +00:00
|
|
|
mympd_get_tag(song, MPD_TAG_TITLE)
|
2018-06-18 16:53:30 +00:00
|
|
|
);
|
2014-02-22 00:57:26 +00:00
|
|
|
mpd_song_free(song);
|
2018-05-25 16:12:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-18 16:53:30 +00:00
|
|
|
len += json_printf(&out, "], totalEntities: %d, offset: %d, returnedEntities: %d, mpdtagtype: %Q }",
|
|
|
|
entity_count,
|
|
|
|
offset,
|
|
|
|
entities_returned,
|
|
|
|
mpdtagtype
|
|
|
|
);
|
2014-02-22 00:57:26 +00:00
|
|
|
}
|
2018-06-18 16:53:30 +00:00
|
|
|
|
|
|
|
if (len > MAX_SIZE) fprintf(stderr,"Buffer truncated\n");
|
|
|
|
return len;
|
2013-11-05 13:59:12 +00:00
|
|
|
}
|
2014-02-22 00:57:26 +00:00
|
|
|
|
2018-07-10 23:06:31 +00:00
|
|
|
int mympd_get_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);
|
|
|
|
char mpd_version[20];
|
2018-06-18 16:53:30 +00:00
|
|
|
int len;
|
|
|
|
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
|
2018-05-31 23:29:45 +00:00
|
|
|
|
|
|
|
snprintf(mpd_version,20,"%i.%i.%i", version[0], version[1], version[2]);
|
|
|
|
|
2018-05-27 21:34:39 +00:00
|
|
|
if (stats == NULL)
|
|
|
|
RETURN_ERROR_AND_RECOVER("mympd_get_stats");
|
2018-06-18 16:53:30 +00:00
|
|
|
len = json_printf(&out, "{ type: mpdstats, data: { artists: %d, albums: %d, songs: %d, "
|
2018-06-24 18:59:32 +00:00
|
|
|
"playtime: %d, uptime: %d, dbupdated: %d, dbplaytime: %d, mympd_version: %Q, mpd_version: %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,
|
|
|
|
mpd_version
|
|
|
|
);
|
2018-05-27 21:34:39 +00:00
|
|
|
mpd_stats_free(stats);
|
2018-06-18 16:53:30 +00:00
|
|
|
|
|
|
|
if (len > MAX_SIZE) fprintf(stderr,"Buffer truncated\n");
|
|
|
|
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-06-14 22:00:54 +00:00
|
|
|
mympd_poll(NULL);
|
2014-03-18 00:30:01 +00:00
|
|
|
}
|