From 8437356b6c615ac93f49fe122a591a229722dbe8 Mon Sep 17 00:00:00 2001 From: wm4 Date: Thu, 6 Feb 2014 16:49:50 +0100 Subject: [PATCH] options: add --no-terminal switch Mostly useful for internal reasons. This code will be enabled by default if mpv is started via the client API. --- DOCS/man/en/options.rst | 7 +++++++ common/msg.c | 34 +++++++++++++++++++++------------- options/options.c | 2 ++ options/options.h | 1 + player/loadfile.c | 4 ++-- player/main.c | 30 +++++++++++++++++------------- player/osd.c | 6 ++++++ player/playloop.c | 6 ++++-- 8 files changed, 60 insertions(+), 30 deletions(-) diff --git a/DOCS/man/en/options.rst b/DOCS/man/en/options.rst index 46586e69cf..44fe7ff6ca 100644 --- a/DOCS/man/en/options.rst +++ b/DOCS/man/en/options.rst @@ -2411,6 +2411,13 @@ OPTIONS Default: ``[-+-]``. +``--no-terminal``, ``--terminal`` + Disable any use of the terminal and stdin/stdout/stderr. This completely + silences any message output. + + Unlike ``--really-quiet``, this disables input and terminal initialization + as well. + ``--title=`` Set the window title. Properties are expanded on playback start. (See `Property Expansion`_.) diff --git a/common/msg.c b/common/msg.c index 7ac3d1b665..9b93a3fb7a 100644 --- a/common/msg.c +++ b/common/msg.c @@ -45,6 +45,7 @@ struct mp_log_root { struct mpv_global *global; // --- protected by mp_msg_lock char *msglevels; + bool use_terminal; // make accesses to stderr/stdout bool smode; // slave mode compatibility glue bool module; bool termosd; // use terminal control codes for status line @@ -98,18 +99,22 @@ static bool match_mod(const char *name, bstr mod) static void update_loglevel(struct mp_log *log) { pthread_mutex_lock(&mp_msg_lock); - log->level = MSGL_STATUS + log->root->verbose; // default log level - // Stupid exception for the remains of -identify - if (match_mod(log->verbose_prefix, bstr0("identify"))) - log->level = -1; - bstr s = bstr0(log->root->msglevels); - bstr mod; - int level; - while (mp_msg_split_msglevel(&s, &mod, &level) > 0) { - if (match_mod(log->verbose_prefix, mod)) - log->level = level; + log->level = -1; + log->terminal_level = -1; + if (log->root->use_terminal) { + log->level = MSGL_STATUS + log->root->verbose; // default log level + // Stupid exception for the remains of -identify + if (match_mod(log->verbose_prefix, bstr0("identify"))) + log->level = -1; + bstr s = bstr0(log->root->msglevels); + bstr mod; + int level; + while (mp_msg_split_msglevel(&s, &mod, &level) > 0) { + if (match_mod(log->verbose_prefix, mod)) + log->level = level; + } + log->terminal_level = log->root->use_terminal ? log->level : -1; } - log->terminal_level = log->level; for (int n = 0; n < log->root->num_buffers; n++) log->level = MPMAX(log->level, log->root->buffers[n]->level); log->reload_counter = log->root->reload_counter; @@ -372,8 +377,11 @@ void mp_msg_update_msglevels(struct mpv_global *global) root->verbose = opts->verbose; root->module = opts->msg_module; root->smode = opts->msg_identify; - root->color = opts->msg_color && isatty(fileno(stdout)); - root->termosd = !opts->slave_mode && isatty(fileno(stderr)); + root->use_terminal = opts->use_terminal; + if (root->use_terminal) { + root->color = opts->msg_color && isatty(fileno(stdout)); + root->termosd = !opts->slave_mode && isatty(fileno(stderr)); + } talloc_free(root->msglevels); root->msglevels = talloc_strdup(root, global->opts->msglevels); diff --git a/options/options.c b/options/options.c index 1eaccb8063..700415a443 100644 --- a/options/options.c +++ b/options/options.c @@ -226,6 +226,7 @@ const m_option_t mp_opts[] = { // ------------------------- common options -------------------- OPT_FLAG("quiet", quiet, CONF_GLOBAL), OPT_FLAG_STORE("really-quiet", verbose, CONF_GLOBAL | CONF_PRE_PARSE, -10), + OPT_FLAG("terminal", use_terminal, CONF_GLOBAL | CONF_PRE_PARSE), OPT_GENERAL(char*, "msglevel", msglevels, CONF_GLOBAL|CONF_PRE_PARSE, .type = &m_option_type_msglevels), OPT_FLAG("msgcolor", msg_color, CONF_GLOBAL | CONF_PRE_PARSE), @@ -649,6 +650,7 @@ const m_option_t mp_opts[] = { }; const struct MPOpts mp_default_opts = { + .use_terminal = 1, .msg_color = 1, .audio_driver_list = NULL, .audio_decoders = "-spdif:*", // never select spdif by default diff --git a/options/options.h b/options/options.h index e328ccd25c..0b8ea0bded 100644 --- a/options/options.h +++ b/options/options.h @@ -44,6 +44,7 @@ typedef struct mp_vo_opts { } mp_vo_opts; typedef struct MPOpts { + int use_terminal; char *msglevels; int verbose; int msg_identify; diff --git a/player/loadfile.c b/player/loadfile.c index 9ba5a00758..662a714ec0 100644 --- a/player/loadfile.c +++ b/player/loadfile.c @@ -1097,7 +1097,7 @@ static void play_current_file(struct MPContext *mpctx) load_per_file_options(mpctx->mconfig, mpctx->playlist->current->params, mpctx->playlist->current->num_params); - if (!opts->consolecontrols) + if (opts->use_terminal && !opts->consolecontrols) getch2_disable(); #if HAVE_LIBASS @@ -1393,7 +1393,7 @@ terminate_playback: // don't jump here after ao/vo/getch initialization! if (mpctx->stop_play != PT_RESTART) m_config_restore_backups(mpctx->mconfig); - if (opts->consolecontrols) + if (opts->use_terminal && opts->consolecontrols) getch2_enable(); mpctx->filename = NULL; diff --git a/player/main.c b/player/main.c index d1c1017414..1709776fa7 100644 --- a/player/main.c +++ b/player/main.c @@ -149,7 +149,8 @@ static MP_NORETURN void exit_player(struct MPContext *mpctx, mpctx->ass_library = NULL; #endif - getch2_disable(); + if (mpctx->opts->use_terminal) + getch2_disable(); uninit_libav(mpctx->global); if (how != EXIT_NONE) { @@ -288,8 +289,6 @@ static void osdep_preinit(int *p_argc, char ***p_argv) pSetSearchPathMode(BASE_SEARCH_PATH_ENABLE_SAFE_SEARCHMODE); #endif - terminal_init(); - mp_time_init(); } @@ -342,18 +341,21 @@ static int mpv_main(int argc, char *argv[]) char *verbose_env = getenv("MPV_VERBOSE"); if (verbose_env) opts->verbose = atoi(verbose_env); + + // Preparse the command line + m_config_preparse_command_line(mpctx->mconfig, mpctx->global, argc, argv); + mp_msg_update_msglevels(mpctx->global); + if (opts->use_terminal) + terminal_init(); + init_libav(mpctx->global); GetCpuCaps(&gCpuCaps); screenshot_init(mpctx); mpctx->mixer = mixer_init(mpctx, mpctx->global); command_init(mpctx); - // Preparse the command line - m_config_preparse_command_line(mpctx->mconfig, mpctx->global, argc, argv); - mp_msg_update_msglevels(mpctx->global); - mp_print_version(mpctx->log, false); if (!mp_parse_cfgfiles(mpctx)) @@ -413,13 +415,15 @@ static int mpv_main(int argc, char *argv[]) } #endif - if (mpctx->opts->slave_mode) - terminal_setup_stdin_cmd_input(mpctx->input); - else if (mpctx->opts->consolecontrols) - terminal_setup_getch(mpctx->input); + if (opts->use_terminal) { + if (mpctx->opts->slave_mode) + terminal_setup_stdin_cmd_input(mpctx->input); + else if (mpctx->opts->consolecontrols) + terminal_setup_getch(mpctx->input); - if (opts->consolecontrols) - getch2_enable(); + if (opts->consolecontrols) + getch2_enable(); + } #if HAVE_LIBASS mpctx->ass_log = mp_log_new(mpctx, mpctx->global->log, "!libass"); diff --git a/player/osd.c b/player/osd.c index 1f15e3ddc7..abfe826a81 100644 --- a/player/osd.c +++ b/player/osd.c @@ -70,6 +70,9 @@ static void term_osd_update(struct MPContext *mpctx) int num_parts = 0; char *parts[3] = {0}; + if (!mpctx->opts->use_terminal) + return; + if (mpctx->term_osd_subs && mpctx->term_osd_subs[0]) parts[num_parts++] = mpctx->term_osd_subs; if (mpctx->term_osd_text && mpctx->term_osd_text[0]) @@ -147,6 +150,9 @@ void print_status(struct MPContext *mpctx) update_window_title(mpctx, false); + if (!opts->use_terminal) + return; + if (opts->quiet || !(mpctx->initialized_flags & INITIALIZED_PLAYBACK)) { term_osd_set_status(mpctx, ""); return; diff --git a/player/playloop.c b/player/playloop.c index 4faa3ba01d..e28d387801 100644 --- a/player/playloop.c +++ b/player/playloop.c @@ -1300,7 +1300,8 @@ void run_playloop(struct MPContext *mpctx) execute_queued_seek(mpctx); - getch2_poll(); + if (mpctx->opts->use_terminal) + getch2_poll(); } // Waiting for the slave master to send us a new file to play. @@ -1330,6 +1331,7 @@ void idle_loop(struct MPContext *mpctx) run_command(mpctx, cmd); mp_cmd_free(cmd); mp_flush_events(mpctx); - getch2_poll(); + if (mpctx->opts->use_terminal) + getch2_poll(); } }