/* * ======================== * ===== =============== * ====== ================ * ====== ================ * ====== ==== ==== == * ====== === == = = * ====== === = == = * = === === = == ==== * = === === = == = = * == ===== ==== == * ======================== * * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 2022 Joe * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the organization nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY JOE ''AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL JOE BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * mpdview: src/c_mpdview.c * Tue, 12 Jul 2022 21:36:26 +0200 * Joe * * This is the entrypoint of the program. */ #include #include #include #include #include #include #include #include #include #include #include "c_defines.h" #include "c_mpd.h" #include "c_mpdview.h" #include "c_view.h" static bool_t doexit = FALSE; static void c_get_music_dir(struct mpdview_s* v) { strlcpy(v->music_dir, DEF_MUSIC_DIR, PATH_MAX); } static void c_get_current_dir(struct mpdview_s* v) { ptr_t ptr; const size_t len = strlen(v->music_dir); strlcpy(v->current_dir, v->music_dir, PATH_MAX); v->current_dir[len + 1] = 0x00; v->current_dir[len] = '/'; strlcat(v->current_dir, mpd_song_get_uri(v->song), PATH_MAX); ptr = v->current_dir; ptr += strlen(v->current_dir); while (*ptr != '/' && ptr > v->music_dir) { ptr--; } *ptr = 0x00; } static void c_signal(int signal) { (void)signal; doexit = TRUE; } int main (int argc, char* const argv[]) { struct mpdview_s* view; char c; bool_t verbose; verbose = FALSE; c = 0; while (c != -1) { c = getopt(argc, argv, OPTSTRING); if (c == 'h') { /* TODO: usage(); */ dprintf(STDOUT_FILENO, "help\n"); return (EXIT_SUCCESS); } else if (c == 'v') { verbose = TRUE; } else if (c == 'V') { dprintf(STDOUT_FILENO, "%s %s\n", PROGNAME, VERSION); return (EXIT_SUCCESS); } else if (c == '?') { /* TODO: usage(); */ return (EXIT_FAILURE); } } view = c_create_view(verbose); if (view == NULL) { return (EXIT_FAILURE); } c_get_music_dir(view); if (c_connect_mpd(view) == RET_ERR) { c_destroy_mpdview(&view); return (EXIT_FAILURE); } signal(SIGINT, c_signal); if (c_get_mpd_status(view) == RET_ERR) { c_destroy_mpdview(&view); return (EXIT_FAILURE); } 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); } } while ( doexit == FALSE && mpd_run_idle_mask(view->conn, MPD_IDLE_PLAYER) != 0 ) { if (c_get_mpd_status(view) == RET_ERR) { c_destroy_mpdview(&view); return (EXIT_FAILURE); } 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); }