From 11ec2346a2eedf3ff477637519ad3b30bbec15cb Mon Sep 17 00:00:00 2001 From: Joe Date: Thu, 14 Jul 2022 15:02:25 +0200 Subject: Not bad --- src/c_defines.h | 2 +- src/c_mpdview.c | 40 +++++++++++++++++++++++++++++----------- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/c_defines.h b/src/c_defines.h index 7a32d9b..9f303fb 100644 --- a/src/c_defines.h +++ b/src/c_defines.h @@ -72,6 +72,6 @@ enum ret_e { #define OPTSTRING "hvV" -#define DEF_MUSIC_DIR "/var/mpd/music" +#define DEF_MUSIC_DIR "/home/jozan/mu" #endif /* __C_DEFINES_H__ */ diff --git a/src/c_mpdview.c b/src/c_mpdview.c index 9456e7b..1fd0d98 100644 --- a/src/c_mpdview.c +++ b/src/c_mpdview.c @@ -45,10 +45,10 @@ * This is the entrypoint of the program. */ -#include #include #include +#include #include #include #include @@ -59,6 +59,9 @@ #include "c_defines.h" #include "c_mpdview.h" + +static bool_t doexit = FALSE; + static struct mpdview_s * c_create_view(bool_t verbose) { @@ -157,9 +160,8 @@ static void c_get_current_dir(struct mpdview_s* v) { ptr_t ptr; - size_t len; + const size_t len = strlen(v->music_dir); - len = strlen(v->current_dir); strlcpy(v->current_dir, v->music_dir, PATH_MAX); v->current_dir[len + 1] = 0x00; v->current_dir[len] = '/'; @@ -172,6 +174,13 @@ c_get_current_dir(struct mpdview_s* v) *ptr = 0x00; } +static void +c_signal(int signal) +{ + (void)signal; + doexit = TRUE; +} + int main (int argc, @@ -179,7 +188,6 @@ main { struct mpdview_s* view; char c; - bool_t doexit; bool_t verbose; verbose = FALSE; @@ -205,14 +213,8 @@ main if (view == NULL) { return (EXIT_FAILURE); } - doexit = FALSE; - while ( - doexit == FALSE && - mpd_run_idle_mask(view->conn, MPD_IDLE_PLAYER) != 0 - ) { - } c_get_music_dir(view); - if (c_connect_mpd(view) != RET_OK) { + if (c_connect_mpd(view) == RET_ERR) { c_destroy_mpdview(&view); return (EXIT_FAILURE); } @@ -220,6 +222,7 @@ main c_destroy_mpdview(&view); return (EXIT_FAILURE); } + signal(SIGINT, c_signal); if (view->state == MPD_STATE_PLAY || view->state == MPD_STATE_PAUSE) { if (c_get_mpd_song(view) == RET_ERR) { c_destroy_mpdview(&view); @@ -230,6 +233,21 @@ main dprintf(STDOUT_FILENO, "%s\n", view->current_dir); } } + while ( + doexit == FALSE && + mpd_run_idle_mask(view->conn, MPD_IDLE_PLAYER) != 0 + ) { + if (view->state == MPD_STATE_PLAY || view->state == MPD_STATE_PAUSE) { + if (c_get_mpd_song(view) == RET_ERR) { + c_destroy_mpdview(&view); + return (EXIT_FAILURE); + } + c_get_current_dir(view); + if (view->verbose == TRUE) { + dprintf(STDOUT_FILENO, "%s\n", view->current_dir); + } + } + } c_destroy_mpdview(&view); return (EXIT_SUCCESS); } -- cgit v1.2.3