From b2073d08b0913aa7eaa4c2e6c59e880104b3c346 Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 9 Mar 2018 04:07:34 +0100 Subject: player: shuffle around CLI exit code handling Remove the weird prepare_exit_cplayer() function, and fold the contents into mpv_main() and mp_initialize(). --- player/client.c | 2 +- player/main.c | 100 ++++++++++++++++++++++++-------------------------------- 2 files changed, 44 insertions(+), 58 deletions(-) diff --git a/player/client.c b/player/client.c index a8b90082cc..21c6807ad3 100644 --- a/player/client.c +++ b/player/client.c @@ -543,7 +543,7 @@ int mpv_initialize(mpv_handle *ctx) int res = 0; void *args[2] = {ctx->mpctx, &res}; mp_dispatch_run(ctx->mpctx->dispatch, doinit, args); - return res < 0 ? MPV_ERROR_INVALID_PARAMETER : 0; + return res == 0 ? 0 : MPV_ERROR_INVALID_PARAMETER; } // set ev->data to a new copy of the original data diff --git a/player/main.c b/player/main.c index 5a3fe4c1b7..66ae538d4a 100644 --- a/player/main.c +++ b/player/main.c @@ -206,48 +206,6 @@ void mp_destroy(struct MPContext *mpctx) talloc_free(mpctx); } -static int prepare_exit_cplayer(struct MPContext *mpctx, enum exit_reason how) -{ - int rc = 0; - const char *reason = NULL; - - if (how == EXIT_ERROR) { - reason = "Fatal error"; - rc = 1; - } else if (how == EXIT_NORMAL) { - if (mpctx->stop_play == PT_QUIT) { - reason = "Quit"; - rc = 0; - } else if (mpctx->files_played) { - if (mpctx->files_errored || mpctx->files_broken) { - reason = "Some errors happened"; - rc = 3; - } else { - reason = "End of file"; - rc = 0; - } - } else if (mpctx->files_broken && !mpctx->files_errored) { - reason = "Errors when loading file"; - rc = 2; - } else if (mpctx->files_errored) { - reason = "Interrupted by error"; - rc = 2; - } else { - reason = "No files played"; - rc = 0; - } - } - - if (reason) - MP_INFO(mpctx, "\nExiting... (%s)\n", reason); - - if (mpctx->has_quit_custom_rc) - rc = mpctx->quit_custom_rc; - - mp_destroy(mpctx); - return rc; -} - static bool handle_help_options(struct MPContext *mpctx) { struct MPOpts *opts = mpctx->opts; @@ -370,7 +328,7 @@ struct MPContext *mp_create(void) // Some of the initializations depend on the options, and can't be changed or // undone later. // If options is not NULL, apply them as command line player arguments. -// Returns: <0 on error, 0 on success. +// Returns: 0 on success, -1 on error, 1 if exiting normally (e.g. help). int mp_initialize(struct MPContext *mpctx, char **options) { struct MPOpts *opts = mpctx->opts; @@ -398,7 +356,7 @@ int mp_initialize(struct MPContext *mpctx, char **options) int r = m_config_parse_mp_command_line(mpctx->mconfig, mpctx->playlist, mpctx->global, options); if (r < 0) - return r == M_OPT_EXIT ? -2 : -1; + return r == M_OPT_EXIT ? 1 : -1; } if (opts->operation_mode == 1) { @@ -421,7 +379,7 @@ int mp_initialize(struct MPContext *mpctx, char **options) mp_option_change_callback(mpctx, NULL, UPDATE_OPTS_MASK); if (handle_help_options(mpctx)) - return -2; + return 1; // help if (!print_libav_versions(mp_null_log, 0)) { // Using mismatched libraries can be legitimate, but even then it's @@ -438,8 +396,12 @@ int mp_initialize(struct MPContext *mpctx, char **options) return -1; } - if (!mpctx->playlist->first && !opts->player_idle_mode) - return -3; + if (!mpctx->playlist->first && !opts->player_idle_mode) { + // nothing to play + mp_print_version(mpctx->log, true); + MP_INFO(mpctx, "%s", mp_help_text); + return 1; + } MP_STATS(mpctx, "start init"); @@ -482,16 +444,40 @@ int mpv_main(int argc, char *argv[]) char **options = argv && argv[0] ? argv + 1 : NULL; // skips program name int r = mp_initialize(mpctx, options); - if (r == -2) // help - return prepare_exit_cplayer(mpctx, EXIT_NONE); - if (r == -3) { // nothing to play - mp_print_version(mpctx->log, true); - MP_INFO(mpctx, "%s", mp_help_text); - return prepare_exit_cplayer(mpctx, EXIT_NONE); + if (r == 0) + mp_play_files(mpctx); + + int rc = 0; + const char *reason = NULL; + if (r < 0) { + reason = "Fatal error"; + rc = 1; + } else if (r > 0) { + // nothing + } else if (mpctx->stop_play == PT_QUIT) { + reason = "Quit"; + } else if (mpctx->files_played) { + if (mpctx->files_errored || mpctx->files_broken) { + reason = "Some errors happened"; + rc = 3; + } else { + reason = "End of file"; + } + } else if (mpctx->files_broken && !mpctx->files_errored) { + reason = "Errors when loading file"; + rc = 2; + } else if (mpctx->files_errored) { + reason = "Interrupted by error"; + rc = 2; + } else { + reason = "No files played"; } - if (r < 0) // another error - return prepare_exit_cplayer(mpctx, EXIT_ERROR); - mp_play_files(mpctx); - return prepare_exit_cplayer(mpctx, EXIT_NORMAL); + if (reason) + MP_INFO(mpctx, "\nExiting... (%s)\n", reason); + if (mpctx->has_quit_custom_rc) + rc = mpctx->quit_custom_rc; + + mp_destroy(mpctx); + return rc; } -- cgit v1.2.3