From 0f31cbea48a8f21b66cf200ab0e1e6b639513e96 Mon Sep 17 00:00:00 2001 From: jcorporation Date: Wed, 15 Aug 2018 18:06:12 +0200 Subject: [PATCH] Feat: add lastPlayed sticker to songs --- src/mpd_client.c | 11 ++++++++--- src/mpd_client.h | 2 ++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/mpd_client.c b/src/mpd_client.c index 5235bd4..2323f8f 100644 --- a/src/mpd_client.c +++ b/src/mpd_client.c @@ -557,6 +557,7 @@ void mympd_poll(struct mg_mgr *s, int timeout) { fprintf(stderr, "MPD connection failed.\n"); snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"disconnected\"}"); mympd_notify(s); + case MPD_DISCONNECT: case MPD_RECONNECT: if (mpd.conn != NULL) @@ -584,7 +585,7 @@ char* mympd_get_song_uri_from_song_id(int song_id) { if (song_id > -1) { song = mpd_run_get_queue_song_id(mpd.conn, song_id); if (song) { - str = (char *)mpd_song_get_uri(song); + str = strdup(mpd_song_get_uri(song)); mpd_song_free(song); } } @@ -592,8 +593,7 @@ char* mympd_get_song_uri_from_song_id(int song_id) { } void mympd_count_song_id(int song_id, char *name, int value) { - char *uri = mympd_get_song_uri_from_song_id(song_id); - mympd_count_song_uri(uri, name, value); + mympd_count_song_uri(mympd_get_song_uri_from_song_id(song_id), name, value); } void mympd_count_song_uri(const char *uri, char *name, int value) { @@ -624,6 +624,10 @@ void mympd_like_song_uri(const char *uri, int value) { mpd_run_sticker_set(mpd.conn, "song", uri, "like", v); } +void mympd_last_played_song_id(int song_id) { + mympd_last_played_song_uri(mympd_get_song_uri_from_song_id(song_id)); +} + void mympd_last_played_song_uri(const char *uri) { char v[20]; snprintf(v, 19, "%lu", time(NULL)); @@ -656,6 +660,7 @@ void mympd_parse_idle(struct mg_mgr *s) { case MPD_IDLE_PLAYER: len = mympd_put_state(mpd.buf, &mpd.song_id, &mpd.next_song_id, &mpd.queue_version); mympd_count_song_id(mpd.song_id, "playCount", 1); + mympd_last_played_song_id(mpd.song_id); break; case MPD_IDLE_MIXER: len = mympd_put_state(mpd.buf, &mpd.song_id, &mpd.next_song_id, &mpd.queue_version); diff --git a/src/mpd_client.h b/src/mpd_client.h index 33fd195..b8b514a 100644 --- a/src/mpd_client.h +++ b/src/mpd_client.h @@ -150,6 +150,8 @@ void mympd_notify(struct mg_mgr *s); void mympd_count_song_id(int song_id, char *name, int value); void mympd_count_song_uri(const char *uri, char *name, int value); void mympd_like_song_uri(const char *uri, int value); +void mympd_last_played_song_uri(const char *uri); +void mympd_last_played_song_id(int song_id); int mympd_put_state(char *buffer, int *current_song_id, int *next_song_id, unsigned *queue_version); int mympd_put_outputs(char *buffer); int mympd_put_current_song(char *buffer);