screenshot: move message showing to common code

The screenshot command has this weird behavior that it shows messages
both on terminal and OSD by default, but that a command prefix can be
used to disable the OSD message.

Move this mechanism to common code, and make this available to other
commands too (although as of this commit only the screenshot commands
use it).

This gets rid of the weird screenshot_ctx.osd field too, which was sort
of set on a command, and sometimes inconsistently restored after the
command.
This commit is contained in:
wm4 2019-07-07 18:00:19 +02:00
parent b55b9cb98c
commit c43846f2cb
3 changed files with 39 additions and 46 deletions

View File

@ -4575,6 +4575,24 @@ void run_command(struct MPContext *mpctx, struct mp_cmd *cmd,
}
}
// When a command shows a message. status is the level (e.g. MSGL_INFO), and
// msg+vararg is as in printf (don't include a trailing "\n").
void mp_cmd_msg(struct mp_cmd_ctx *cmd, int status, const char *msg, ...)
{
va_list ap;
char *s;
va_start(ap, msg);
s = talloc_vasprintf(NULL, msg, ap);
va_end(ap);
MP_MSG(cmd->mpctx, status, "%s\n", s);
if (cmd->msg_osd && status <= MSGL_INFO)
set_osd_msg(cmd->mpctx, 1, cmd->mpctx->opts->osd_duration, "%s", s);
talloc_free(s);
}
static void cmd_seek(void *p)
{
struct mp_cmd_ctx *cmd = p;

View File

@ -70,6 +70,8 @@ void run_command(struct MPContext *mpctx, struct mp_cmd *cmd,
void (*on_completion)(struct mp_cmd_ctx *cmd),
void *on_completion_priv);
void mp_cmd_ctx_complete(struct mp_cmd_ctx *cmd);
PRINTF_ATTRIBUTE(3, 4)
void mp_cmd_msg(struct mp_cmd_ctx *cmd, int status, const char *msg, ...);
char *mp_property_expand_string(struct MPContext *mpctx, const char *str);
char *mp_property_expand_escaped_string(struct MPContext *mpctx, const char *str);
void property_print_help(struct MPContext *mpctx);

View File

@ -49,8 +49,6 @@
typedef struct screenshot_ctx {
struct MPContext *mpctx;
bool osd;
// Command to repeat in each-frame mode.
struct mp_cmd *each_frame;
@ -66,26 +64,6 @@ void screenshot_init(struct MPContext *mpctx)
};
}
static void screenshot_msg(screenshot_ctx *ctx, int status, const char *msg,
...) PRINTF_ATTRIBUTE(3,4);
static void screenshot_msg(screenshot_ctx *ctx, int status, const char *msg,
...)
{
va_list ap;
char *s;
va_start(ap, msg);
s = talloc_vasprintf(NULL, msg, ap);
va_end(ap);
MP_MSG(ctx->mpctx, status, "%s\n", s);
if (ctx->osd && status <= MSGL_INFO)
set_osd_msg(ctx->mpctx, 1, ctx->mpctx->opts->osd_duration, "%s", s);
talloc_free(s);
}
static char *stripext(void *talloc_ctx, const char *s)
{
const char *end = strrchr(s, '.');
@ -94,14 +72,14 @@ static char *stripext(void *talloc_ctx, const char *s)
return talloc_asprintf(talloc_ctx, "%.*s", (int)(end - s), s);
}
static bool write_screenshot(struct MPContext *mpctx, struct mp_image *img,
static bool write_screenshot(struct mp_cmd_ctx *cmd, struct mp_image *img,
const char *filename, struct image_writer_opts *opts)
{
screenshot_ctx *ctx = mpctx->screenshot_ctx;
struct MPContext *mpctx = cmd->mpctx;
struct image_writer_opts *gopts = mpctx->opts->screenshot_image_opts;
struct image_writer_opts opts_copy = opts ? *opts : *gopts;
screenshot_msg(ctx, MSGL_V, "Starting screenshot: '%s'", filename);
mp_cmd_msg(cmd, MSGL_V, "Starting screenshot: '%s'", filename);
mp_core_unlock(mpctx);
@ -110,9 +88,9 @@ static bool write_screenshot(struct MPContext *mpctx, struct mp_image *img,
mp_core_lock(mpctx);
if (ok) {
screenshot_msg(ctx, MSGL_INFO, "Screenshot: '%s'", filename);
mp_cmd_msg(cmd, MSGL_INFO, "Screenshot: '%s'", filename);
} else {
screenshot_msg(ctx, MSGL_ERR, "Error writing screenshot!");
mp_cmd_msg(cmd, MSGL_ERR, "Error writing screenshot!");
}
return ok;
}
@ -290,8 +268,11 @@ error_exit:
return NULL;
}
static char *gen_fname(screenshot_ctx *ctx, const char *file_ext)
static char *gen_fname(struct mp_cmd_ctx *cmd, const char *file_ext)
{
struct MPContext *mpctx = cmd->mpctx;
screenshot_ctx *ctx = mpctx->screenshot_ctx;
int sequence = 0;
for (;;) {
int prev_sequence = sequence;
@ -302,9 +283,9 @@ static char *gen_fname(screenshot_ctx *ctx, const char *file_ext)
&ctx->frameno);
if (!fname) {
screenshot_msg(ctx, MSGL_ERR, "Invalid screenshot filename "
"template! Fix or remove the --screenshot-template "
"option.");
mp_cmd_msg(cmd, MSGL_ERR, "Invalid screenshot filename "
"template! Fix or remove the --screenshot-template "
"option.");
return NULL;
}
@ -328,8 +309,8 @@ static char *gen_fname(screenshot_ctx *ctx, const char *file_ext)
return fname;
if (sequence == prev_sequence) {
screenshot_msg(ctx, MSGL_ERR, "Can't save screenshot, file '%s' "
"already exists!", fname);
mp_cmd_msg(cmd, MSGL_ERR, "Can't save screenshot, file '%s' "
"already exists!", fname);
talloc_free(fname);
return NULL;
}
@ -441,11 +422,7 @@ void cmd_screenshot_to_file(void *p)
struct MPContext *mpctx = cmd->mpctx;
const char *filename = cmd->args[0].v.s;
int mode = cmd->args[1].v.i;
bool osd = cmd->msg_osd;
screenshot_ctx *ctx = mpctx->screenshot_ctx;
struct image_writer_opts opts = *mpctx->opts->screenshot_image_opts;
bool old_osd = ctx->osd;
ctx->osd = osd;
char *ext = mp_splitext(filename, NULL);
int format = image_writer_format_from_ext(ext);
@ -453,13 +430,12 @@ void cmd_screenshot_to_file(void *p)
opts.format = format;
bool high_depth = image_writer_high_depth(&opts);
struct mp_image *image = screenshot_get(mpctx, mode, high_depth);
ctx->osd = old_osd;
if (!image) {
screenshot_msg(ctx, MSGL_ERR, "Taking screenshot failed.");
mp_cmd_msg(cmd, MSGL_ERR, "Taking screenshot failed.");
cmd->success = false;
return;
}
cmd->success = write_screenshot(mpctx, image, filename, &opts);
cmd->success = write_screenshot(cmd, image, filename, &opts);
talloc_free(image);
}
@ -470,7 +446,6 @@ void cmd_screenshot(void *p)
int mode = cmd->args[0].v.i & 3;
bool each_frame_toggle = (cmd->args[0].v.i | cmd->args[1].v.i) & 8;
bool each_frame_mode = cmd->args[0].v.i & 16;
bool osd = cmd->msg_osd;
screenshot_ctx *ctx = mpctx->screenshot_ctx;
@ -492,20 +467,18 @@ void cmd_screenshot(void *p)
cmd->success = false;
ctx->osd = osd;
struct image_writer_opts *opts = mpctx->opts->screenshot_image_opts;
bool high_depth = image_writer_high_depth(opts);
struct mp_image *image = screenshot_get(mpctx, mode, high_depth);
if (image) {
char *filename = gen_fname(ctx, image_writer_file_ext(opts));
char *filename = gen_fname(cmd, image_writer_file_ext(opts));
if (filename)
cmd->success = write_screenshot(mpctx, image, filename, NULL);
cmd->success = write_screenshot(cmd, image, filename, NULL);
talloc_free(filename);
} else {
screenshot_msg(ctx, MSGL_ERR, "Taking screenshot failed.");
mp_cmd_msg(cmd, MSGL_ERR, "Taking screenshot failed.");
}
talloc_free(image);