1
0
mirror of https://github.com/SuperBFG7/ympd synced 2026-06-04 03:32:14 +00:00

Feat: track mpd status changes with idle protocol

This commit is contained in:
jcorporation
2018-08-14 16:10:00 +02:00
parent 15afb2a70e
commit dd5db4f8a3
4 changed files with 99 additions and 82 deletions
+12 -16
View File
@@ -75,14 +75,10 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg) {
if (cmd_id == -1)
cmd_id = get_cmd_id("MPD_API_UNKNOWN");
mpd_send_noidle(mpd.conn);
mpd_send_noidle(mpd.conn);
// mympd_parse_idle(mg_mgr);
mpd_response_finish(mpd.conn);
/*
enum mpd_idle idle_event;
mpd_connection_set_timeout(mpd.conn, 1);
idle_event = mpd_recv_idle(mpd.conn, false);
*/
mpd_connection_set_timeout(mpd.conn, mpd.timeout);
// mpd_connection_set_timeout(mpd.conn, mpd.timeout);
switch(cmd_id) {
case MPD_API_UNKNOWN:
@@ -515,7 +511,7 @@ void mympd_poll(struct mg_mgr *s, int timeout) {
switch (mpd.conn_state) {
case MPD_DISCONNECTED:
/* Try to connect */
fprintf(stdout, "MPD Connecting to %s: %d\n", config.mpdhost, config.mpdport);
fprintf(stdout, "MPD Connecting to %s:%d\n", config.mpdhost, config.mpdport);
mpd.conn = mpd_connection_new(config.mpdhost, config.mpdport, mpd.timeout);
if (mpd.conn == NULL) {
fprintf(stderr, "Out of memory.");
@@ -585,9 +581,9 @@ void mympd_notify(struct mg_mgr *s) {
}
void mympd_parse_idle(struct mg_mgr *s) {
mpd_connection_set_timeout(mpd.conn, 60);
// mpd_connection_set_timeout(mpd.conn, 60);
enum mpd_idle idle_bitmask = mpd_recv_idle(mpd.conn, false);
mpd_connection_set_timeout(mpd.conn, mpd.timeout);
// mpd_connection_set_timeout(mpd.conn, mpd.timeout);
int len;
for (unsigned j = 0;; ++j) {
@@ -668,11 +664,11 @@ int mympd_put_state(char *buffer, int *current_song_id, int *next_song_id, unsig
}
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",
"state:%d, volume:%d, songPos: %d, elapsedTime: %d, "
"totalTime:%d, currentSongId: %d, kbitrate: %d, "
"audioFormat: { sampleRate: %d, bits: %d, channels: %d}, "
"queueLength: %d, nextSongPos: %d, nextSongId: %d, "
"queueVersion: %d",
mpd_status_get_state(status),
mpd_status_get_volume(status),
mpd_status_get_song_pos(status),
@@ -841,7 +837,7 @@ int mympd_put_current_song(char *buffer) {
mympd_get_cover(mpd_song_get_uri(song),cover,500);
len = json_printf(&out,"{type: song_change, data: { pos: %d, title: %Q, "
"artist: %Q, album: %Q, uri: %Q, currentsongid: %d, albumartist: %Q, "
"artist: %Q, album: %Q, uri: %Q, currentSongId: %d, albumartist: %Q, "
"duration: %d, cover: %Q}}",
mpd_song_get_pos(song),
mympd_get_tag(song, MPD_TAG_TITLE),
+16 -3
View File
@@ -28,6 +28,7 @@
#include <unistd.h>
#include <sys/time.h>
#include <pwd.h>
#include <pthread.h>
#include "../dist/src/mongoose/mongoose.h"
#include "../dist/src/inih/ini.h"
@@ -37,6 +38,7 @@
static sig_atomic_t s_signal_received = 0;
static struct mg_serve_http_opts s_http_server_opts;
char s_redirect[250];
struct mg_mgr mgr;
static void signal_handler(int sig_num) {
signal(sig_num, signal_handler); // Reinstantiate signal handler
@@ -145,8 +147,14 @@ static int inihandler(void* user, const char* section, const char* name, const c
return 1;
}
void mympd_poll_loop() {
while (s_signal_received == 0) {
mympd_poll(&mgr, 200);
}
}
int main(int argc, char **argv) {
struct mg_mgr mgr;
struct mg_connection *nc;
struct mg_connection *nc_http;
struct mg_bind_opts bind_opts;
@@ -254,10 +262,15 @@ int main(int argc, char **argv) {
printf("myMPD started on http port %s\n", config.webport);
if (config.ssl == true)
printf("myMPD started on ssl port %s\n", config.sslport);
//Create thread for mpd idle connection
//pthread_t tid;
//pthread_create(&tid, NULL, &mympd_poll_loop, NULL);
//Main loop for http handling
while (s_signal_received == 0) {
mympd_poll(&mgr, 60);
mg_mgr_poll(&mgr, 60);
mg_mgr_poll(&mgr, 100);
mympd_poll(&mgr, 1);
}
mg_mgr_free(&mgr);
mympd_disconnect();