0
0
mirror of https://github.com/mpv-player/mpv.git synced 2024-09-20 03:52:22 +02:00

player: make --terminal freetly settable at runtime

So client API users don't have to care about whether to set this before
or after mpv_initialize().

We still don't enable terminal at any point before mpv_initialize(),
because reasons.

This also subtly changes some behavior how terminal options are applied
while parsing. This essentially reverts the behavior as it was reported
in issue #2588. Originally, I was hoping to get rid of the pre-parse
option pass, but it seems this is absolutely not possible due to the way
config and command line parsing are entangled. Command line options take
priority over configfile options, so they have to be applied later - but
we also want to apply logging and terminal options as specified on the
command-line, but _before_ parsing the config files. It has to be this
way to see config file error messages on the terminal, or to hide them
if --no-terminal is used. libmpv considerations also factor into this.
This commit is contained in:
wm4 2016-09-19 19:54:54 +02:00
parent 75fe626aa6
commit b62634c051
6 changed files with 35 additions and 28 deletions

View File

@ -256,7 +256,7 @@ const m_option_t mp_opts[] = {
OPT_FLAG("quiet", quiet, 0), OPT_FLAG("quiet", quiet, 0),
OPT_FLAG_STORE("really-quiet", verbose, OPT_FLAG_STORE("really-quiet", verbose,
CONF_GLOBAL | CONF_PRE_PARSE | M_OPT_NOPROP, -10), CONF_GLOBAL | CONF_PRE_PARSE | M_OPT_NOPROP, -10),
OPT_FLAG("terminal", use_terminal, CONF_GLOBAL | CONF_PRE_PARSE | UPDATE_TERM), OPT_FLAG("terminal", use_terminal, CONF_PRE_PARSE | UPDATE_TERM),
OPT_GENERAL(char**, "msg-level", msg_levels, CONF_PRE_PARSE | UPDATE_TERM, OPT_GENERAL(char**, "msg-level", msg_levels, CONF_PRE_PARSE | UPDATE_TERM,
.type = &m_option_type_msglevels), .type = &m_option_type_msglevels),
OPT_STRING("dump-stats", dump_stats, UPDATE_TERM | CONF_PRE_PARSE), OPT_STRING("dump-stats", dump_stats, UPDATE_TERM | CONF_PRE_PARSE),
@ -650,7 +650,7 @@ const m_option_t mp_opts[] = {
{"once", 1}, {"once", 1},
{"yes", 2})), {"yes", 2})),
OPT_FLAG("input-terminal", consolecontrols, CONF_GLOBAL), OPT_FLAG("input-terminal", consolecontrols, UPDATE_TERM),
OPT_STRING("input-file", input_file, M_OPT_FILE), OPT_STRING("input-file", input_file, M_OPT_FILE),
OPT_STRING("input-ipc-server", ipc_path, M_OPT_FILE | M_OPT_FIXED), OPT_STRING("input-ipc-server", ipc_path, M_OPT_FILE | M_OPT_FIXED),

View File

@ -410,11 +410,9 @@ static void *terminal_thread(void *ptr)
void terminal_setup_getch(struct input_ctx *ictx) void terminal_setup_getch(struct input_ctx *ictx)
{ {
if (!getch2_enabled) if (!getch2_enabled || input_ctx)
return; return;
assert(!input_ctx); // already setup
if (mp_make_wakeup_pipe(death_pipe) < 0) if (mp_make_wakeup_pipe(death_pipe) < 0)
return; return;

View File

@ -136,7 +136,8 @@ static void *input_thread_fn(void *ptr)
void terminal_setup_getch(struct input_ctx *ictx) void terminal_setup_getch(struct input_ctx *ictx)
{ {
assert(!running); if (running)
return;
HANDLE in = GetStdHandle(STD_INPUT_HANDLE); HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
if (GetNumberOfConsoleInputEvents(in, &(DWORD){0})) { if (GetNumberOfConsoleInputEvents(in, &(DWORD){0})) {

View File

@ -5631,7 +5631,7 @@ void mp_option_change_callback(void *ctx, struct m_config_option *co, int flags)
struct MPContext *mpctx = ctx; struct MPContext *mpctx = ctx;
if (flags & UPDATE_TERM) if (flags & UPDATE_TERM)
mp_msg_update_msglevels(mpctx->global); mp_update_logging(mpctx);
if (flags & UPDATE_RENDERER) { if (flags & UPDATE_RENDERER) {
if (mpctx->video_out) if (mpctx->video_out)

View File

@ -487,6 +487,7 @@ struct MPContext *mp_create(void);
void mp_destroy(struct MPContext *mpctx); void mp_destroy(struct MPContext *mpctx);
void mp_print_version(struct mp_log *log, int always); void mp_print_version(struct mp_log *log, int always);
void wakeup_playloop(void *ctx); void wakeup_playloop(void *ctx);
void mp_update_logging(struct MPContext *mpctx);
// misc.c // misc.c
double rel_time_to_abs(struct MPContext *mpctx, struct m_rel_time t); double rel_time_to_abs(struct MPContext *mpctx, struct m_rel_time t);

View File

@ -116,11 +116,23 @@ static bool cas_terminal_owner(struct MPContext *old, struct MPContext *new)
return r; return r;
} }
static void update_logging(struct MPContext *mpctx) void mp_update_logging(struct MPContext *mpctx)
{ {
mp_msg_update_msglevels(mpctx->global); mp_msg_update_msglevels(mpctx->global);
if (mpctx->opts->use_terminal && cas_terminal_owner(NULL, mpctx))
bool enable = mpctx->opts->use_terminal;
bool enabled = cas_terminal_owner(mpctx, mpctx);
if (enable != enabled) {
if (enable && cas_terminal_owner(NULL, mpctx)) {
terminal_init(); terminal_init();
} else if (!enable) {
terminal_uninit();
cas_terminal_owner(mpctx, NULL);
}
}
if (cas_terminal_owner(mpctx, mpctx) && mpctx->opts->consolecontrols)
terminal_setup_getch(mpctx->input);
} }
void mp_print_version(struct mp_log *log, int always) void mp_print_version(struct mp_log *log, int always)
@ -356,12 +368,6 @@ struct MPContext *mp_create(void)
mp_input_set_cancel(mpctx->input, mpctx->playback_abort); mp_input_set_cancel(mpctx->input, mpctx->playback_abort);
mpctx->mconfig->option_set_callback = mp_on_set_option;
mpctx->mconfig->option_set_callback_cb = mpctx;
mpctx->mconfig->option_change_callback = mp_option_change_callback;
mpctx->mconfig->option_change_callback_ctx = mpctx;
return mpctx; return mpctx;
} }
@ -376,32 +382,39 @@ int mp_initialize(struct MPContext *mpctx, char **options)
assert(!mpctx->initialized); assert(!mpctx->initialized);
if (options) {
// Preparse the command line, so we can init the terminal early. // Preparse the command line, so we can init the terminal early.
if (options)
m_config_preparse_command_line(mpctx->mconfig, mpctx->global, options); m_config_preparse_command_line(mpctx->mconfig, mpctx->global, options);
update_logging(mpctx); mp_update_logging(mpctx);
if (options) {
MP_VERBOSE(mpctx, "Command line options:"); MP_VERBOSE(mpctx, "Command line options:");
for (int i = 0; options[i]; i++) for (int i = 0; options[i]; i++)
MP_VERBOSE(mpctx, " '%s'", options[i]); MP_VERBOSE(mpctx, " '%s'", options[i]);
MP_VERBOSE(mpctx, "\n"); MP_VERBOSE(mpctx, "\n");
} }
update_logging(mpctx);
mp_print_version(mpctx->log, false); mp_print_version(mpctx->log, false);
mp_parse_cfgfiles(mpctx); mp_parse_cfgfiles(mpctx);
update_logging(mpctx);
if (options) { if (options) {
int r = m_config_parse_mp_command_line(mpctx->mconfig, mpctx->playlist, int r = m_config_parse_mp_command_line(mpctx->mconfig, mpctx->playlist,
mpctx->global, options); mpctx->global, options);
if (r < 0) if (r < 0)
return r == M_OPT_EXIT ? -2 : -1; return r == M_OPT_EXIT ? -2 : -1;
update_logging(mpctx);
} }
// From this point on, all mpctx members are initialized.
mpctx->initialized = true;
mpctx->mconfig->option_set_callback = mp_on_set_option;
mpctx->mconfig->option_set_callback_cb = mpctx;
mpctx->mconfig->option_change_callback = mp_option_change_callback;
mpctx->mconfig->option_change_callback_ctx = mpctx;
// Run all update handlers.
mp_option_change_callback(mpctx, NULL, UPDATE_OPTS_MASK);
if (handle_help_options(mpctx)) if (handle_help_options(mpctx))
return -2; return -2;
@ -448,18 +461,12 @@ int mp_initialize(struct MPContext *mpctx, char **options)
MP_WARN(mpctx, "There will be no OSD and no text subtitles.\n"); MP_WARN(mpctx, "There will be no OSD and no text subtitles.\n");
#endif #endif
// From this point on, all mpctx members are initialized.
mpctx->initialized = true;
mp_get_resume_defaults(mpctx); mp_get_resume_defaults(mpctx);
// Lua user scripts (etc.) can call arbitrary functions. Load them at a point // Lua user scripts (etc.) can call arbitrary functions. Load them at a point
// where this is safe. // where this is safe.
mp_load_scripts(mpctx); mp_load_scripts(mpctx);
if (opts->consolecontrols && cas_terminal_owner(mpctx, mpctx))
terminal_setup_getch(mpctx->input);
if (opts->force_vo == 2 && handle_force_window(mpctx, false) < 0) if (opts->force_vo == 2 && handle_force_window(mpctx, false) < 0)
return -1; return -1;