2013-10-29 21:38:29 +00:00
|
|
|
/*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MPlayer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <libavutil/md5.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "talloc.h"
|
|
|
|
|
|
|
|
#include "osdep/io.h"
|
|
|
|
|
2014-06-10 20:09:27 +00:00
|
|
|
#include "common/global.h"
|
2014-06-11 00:04:02 +00:00
|
|
|
#include "common/encode.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2014-07-01 21:10:38 +00:00
|
|
|
#include "misc/ctype.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/path.h"
|
|
|
|
#include "options/m_config.h"
|
|
|
|
#include "options/parse_configfile.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/playlist.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/options.h"
|
|
|
|
#include "options/m_property.h"
|
2013-10-29 21:38:29 +00:00
|
|
|
|
|
|
|
#include "stream/stream.h"
|
|
|
|
|
2013-12-17 00:08:53 +00:00
|
|
|
#include "core.h"
|
2013-10-29 21:38:29 +00:00
|
|
|
#include "command.h"
|
|
|
|
|
2014-06-26 15:56:47 +00:00
|
|
|
static void load_all_cfgfiles(struct MPContext *mpctx, char *section,
|
|
|
|
char *filename)
|
|
|
|
{
|
2014-06-26 17:13:37 +00:00
|
|
|
char **cf = mp_find_all_config_files(NULL, mpctx->global, filename);
|
2014-06-26 15:56:47 +00:00
|
|
|
for (int i = 0; cf && cf[i]; i++)
|
|
|
|
m_config_parse_config_file(mpctx->mconfig, cf[i], section, 0);
|
2014-06-26 17:13:37 +00:00
|
|
|
talloc_free(cf);
|
2014-06-26 15:56:47 +00:00
|
|
|
}
|
|
|
|
|
2014-04-19 20:05:17 +00:00
|
|
|
#define SECT_ENCODE "encoding"
|
|
|
|
|
2014-06-26 15:56:47 +00:00
|
|
|
void mp_parse_cfgfiles(struct MPContext *mpctx)
|
2013-10-29 21:38:29 +00:00
|
|
|
{
|
|
|
|
struct MPOpts *opts = mpctx->opts;
|
|
|
|
if (!opts->load_config)
|
2014-06-26 15:56:47 +00:00
|
|
|
return;
|
2013-12-28 15:32:49 +00:00
|
|
|
|
2014-06-26 16:12:28 +00:00
|
|
|
mp_mk_config_dir(mpctx->global, "");
|
|
|
|
|
2013-12-28 15:32:49 +00:00
|
|
|
m_config_t *conf = mpctx->mconfig;
|
2014-04-19 20:05:17 +00:00
|
|
|
char *section = NULL;
|
2014-06-11 00:04:02 +00:00
|
|
|
bool encoding = opts->encode_opts &&
|
|
|
|
opts->encode_opts->file && opts->encode_opts->file[0];
|
2014-04-19 20:05:17 +00:00
|
|
|
// In encoding mode, we don't want to apply normal config options.
|
|
|
|
// So we "divert" normal options into a separate section, and the diverted
|
|
|
|
// section is never used - unless maybe it's explicitly referenced from an
|
|
|
|
// encoding profile.
|
|
|
|
if (encoding)
|
|
|
|
section = "playback-default";
|
2013-12-28 15:32:49 +00:00
|
|
|
|
2014-01-01 18:25:52 +00:00
|
|
|
// The #if is a stupid hack to avoid errors if libavfilter is not available.
|
2014-03-16 09:25:05 +00:00
|
|
|
#if HAVE_LIBAVFILTER && HAVE_ENCODING
|
2014-06-26 17:13:37 +00:00
|
|
|
char *cf = mp_find_config_file(NULL, mpctx->global, "encoding-profiles.conf");
|
|
|
|
if (cf)
|
|
|
|
m_config_parse_config_file(mpctx->mconfig, cf, SECT_ENCODE, 0);
|
|
|
|
talloc_free(cf);
|
2014-01-01 18:25:52 +00:00
|
|
|
#endif
|
|
|
|
|
2014-06-26 15:56:47 +00:00
|
|
|
load_all_cfgfiles(mpctx, section, "config");
|
|
|
|
load_all_cfgfiles(mpctx, section, "mpv.conf");
|
2013-12-28 15:32:49 +00:00
|
|
|
|
2014-04-19 20:05:17 +00:00
|
|
|
if (encoding)
|
|
|
|
m_config_set_profile(conf, m_config_add_profile(conf, SECT_ENCODE), 0);
|
2013-10-29 21:38:29 +00:00
|
|
|
}
|
|
|
|
|
2013-12-21 19:45:19 +00:00
|
|
|
static int try_load_config(struct MPContext *mpctx, const char *file, int flags)
|
2013-10-29 21:38:29 +00:00
|
|
|
{
|
|
|
|
if (!mp_path_exists(file))
|
|
|
|
return 0;
|
2013-12-21 19:45:19 +00:00
|
|
|
MP_INFO(mpctx, "Loading config '%s'\n", file);
|
2014-04-19 20:05:17 +00:00
|
|
|
m_config_parse_config_file(mpctx->mconfig, file, NULL, flags);
|
2013-10-29 21:38:29 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-12-22 22:08:31 +00:00
|
|
|
// Set options file-local, and don't set them if the user set them via the
|
|
|
|
// command line.
|
|
|
|
#define FILE_LOCAL_FLAGS (M_SETOPT_BACKUP | M_SETOPT_PRESERVE_CMDLINE)
|
|
|
|
|
|
|
|
static void mp_load_per_file_config(struct MPContext *mpctx)
|
2013-10-29 21:38:29 +00:00
|
|
|
{
|
2013-12-21 19:45:19 +00:00
|
|
|
struct MPOpts *opts = mpctx->opts;
|
2013-10-29 21:38:29 +00:00
|
|
|
char *confpath;
|
2013-12-22 22:13:59 +00:00
|
|
|
char cfg[512];
|
2013-12-21 19:45:19 +00:00
|
|
|
const char *file = mpctx->filename;
|
2013-10-29 21:38:29 +00:00
|
|
|
|
2013-12-22 22:13:59 +00:00
|
|
|
if (snprintf(cfg, sizeof(cfg), "%s.conf", file) >= sizeof(cfg)) {
|
2014-05-10 14:08:07 +00:00
|
|
|
MP_VERBOSE(mpctx, "Filename is too long, "
|
|
|
|
"can not load file or directory specific config files\n");
|
2013-10-29 21:38:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-22 22:13:59 +00:00
|
|
|
char *name = mp_basename(cfg);
|
|
|
|
|
2013-12-21 19:45:19 +00:00
|
|
|
if (opts->use_filedir_conf) {
|
2013-12-22 22:13:59 +00:00
|
|
|
bstr dir = mp_dirname(cfg);
|
|
|
|
char *dircfg = mp_path_join(NULL, dir, bstr0("mpv.conf"));
|
2013-12-21 19:45:19 +00:00
|
|
|
try_load_config(mpctx, dircfg, FILE_LOCAL_FLAGS);
|
2013-12-22 22:13:59 +00:00
|
|
|
talloc_free(dircfg);
|
2013-10-29 21:38:29 +00:00
|
|
|
|
2013-12-21 19:45:19 +00:00
|
|
|
if (try_load_config(mpctx, cfg, FILE_LOCAL_FLAGS))
|
2013-10-29 21:38:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-18 23:55:40 +00:00
|
|
|
if ((confpath = mp_find_config_file(NULL, mpctx->global, name))) {
|
2013-12-21 19:45:19 +00:00
|
|
|
try_load_config(mpctx, confpath, FILE_LOCAL_FLAGS);
|
2013-10-29 21:38:29 +00:00
|
|
|
|
|
|
|
talloc_free(confpath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-22 22:08:31 +00:00
|
|
|
static void mp_auto_load_profile(struct MPContext *mpctx, char *category,
|
|
|
|
bstr item)
|
|
|
|
{
|
|
|
|
if (!item.len)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char t[512];
|
|
|
|
snprintf(t, sizeof(t), "%s.%.*s", category, BSTR_P(item));
|
|
|
|
m_profile_t *p = m_config_get_profile0(mpctx->mconfig, t);
|
|
|
|
if (p) {
|
|
|
|
MP_INFO(mpctx, "Auto-loading profile '%s'\n", t);
|
|
|
|
m_config_set_profile(mpctx->mconfig, p, FILE_LOCAL_FLAGS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mp_load_auto_profiles(struct MPContext *mpctx)
|
|
|
|
{
|
|
|
|
struct MPOpts *opts = mpctx->opts;
|
|
|
|
|
|
|
|
mp_auto_load_profile(mpctx, "protocol",
|
|
|
|
mp_split_proto(bstr0(mpctx->filename), NULL));
|
|
|
|
mp_auto_load_profile(mpctx, "extension",
|
|
|
|
bstr0(mp_splitext(mpctx->filename, NULL)));
|
|
|
|
|
|
|
|
mp_load_per_file_config(mpctx);
|
|
|
|
|
|
|
|
if (opts->vo.video_driver_list)
|
|
|
|
mp_auto_load_profile(mpctx, "vo", bstr0(opts->vo.video_driver_list[0].name));
|
|
|
|
if (opts->audio_driver_list)
|
|
|
|
mp_auto_load_profile(mpctx, "ao", bstr0(opts->audio_driver_list[0].name));
|
|
|
|
}
|
|
|
|
|
2013-10-29 21:38:29 +00:00
|
|
|
#define MP_WATCH_LATER_CONF "watch_later"
|
|
|
|
|
2013-12-21 19:45:19 +00:00
|
|
|
static char *mp_get_playback_resume_config_filename(struct mpv_global *global,
|
|
|
|
const char *fname)
|
2013-10-29 21:38:29 +00:00
|
|
|
{
|
2014-07-14 23:49:02 +00:00
|
|
|
struct MPOpts *opts = global->opts;
|
2013-10-29 21:38:29 +00:00
|
|
|
char *res = NULL;
|
|
|
|
void *tmp = talloc_new(NULL);
|
|
|
|
const char *realpath = fname;
|
|
|
|
bstr bfname = bstr0(fname);
|
|
|
|
if (!mp_is_url(bfname)) {
|
|
|
|
char *cwd = mp_getcwd(tmp);
|
|
|
|
if (!cwd)
|
|
|
|
goto exit;
|
|
|
|
realpath = mp_path_join(tmp, bstr0(cwd), bstr0(fname));
|
|
|
|
}
|
2014-07-14 23:49:02 +00:00
|
|
|
if (bstr_startswith0(bfname, "dvd://"))
|
|
|
|
realpath = talloc_asprintf(tmp, "%s - %s", realpath, opts->dvd_device);
|
|
|
|
if (bstr_startswith0(bfname, "br://") || bstr_startswith0(bfname, "bd://") ||
|
|
|
|
bstr_startswith0(bfname, "bluray://"))
|
|
|
|
realpath = talloc_asprintf(tmp, "%s - %s", realpath, opts->bluray_device);
|
2013-10-29 21:38:29 +00:00
|
|
|
uint8_t md5[16];
|
|
|
|
av_md5_sum(md5, realpath, strlen(realpath));
|
|
|
|
char *conf = talloc_strdup(tmp, "");
|
|
|
|
for (int i = 0; i < 16; i++)
|
|
|
|
conf = talloc_asprintf_append(conf, "%02X", md5[i]);
|
|
|
|
|
2014-06-18 23:55:40 +00:00
|
|
|
res = talloc_asprintf(tmp, MP_WATCH_LATER_CONF "/%s", conf);
|
|
|
|
res = mp_find_config_file(NULL, global, res);
|
2013-10-29 21:38:29 +00:00
|
|
|
|
2014-06-18 23:55:40 +00:00
|
|
|
if (!res) {
|
|
|
|
res = mp_find_config_file(tmp, global, MP_WATCH_LATER_CONF);
|
2014-09-05 13:09:12 +00:00
|
|
|
if (res)
|
|
|
|
res = talloc_asprintf(NULL, "%s/%s", res, conf);
|
2014-06-18 23:55:40 +00:00
|
|
|
}
|
2013-10-29 21:38:29 +00:00
|
|
|
|
|
|
|
exit:
|
|
|
|
talloc_free(tmp);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-06-10 21:56:05 +00:00
|
|
|
static const char *const backup_properties[] = {
|
2014-02-25 21:51:55 +00:00
|
|
|
"options/osd-level",
|
2013-10-29 21:38:29 +00:00
|
|
|
//"loop",
|
2014-02-25 21:51:55 +00:00
|
|
|
"options/speed",
|
|
|
|
"options/edition",
|
|
|
|
"options/pause",
|
2013-10-29 21:38:29 +00:00
|
|
|
"volume-restore-data",
|
2014-02-25 21:51:55 +00:00
|
|
|
"options/audio-delay",
|
2013-10-29 21:38:29 +00:00
|
|
|
//"balance",
|
2014-02-25 21:51:55 +00:00
|
|
|
"options/fullscreen",
|
|
|
|
"options/colormatrix",
|
|
|
|
"options/colormatrix-input-range",
|
|
|
|
"options/colormatrix-output-range",
|
|
|
|
"options/ontop",
|
|
|
|
"options/border",
|
|
|
|
"options/gamma",
|
|
|
|
"options/brightness",
|
|
|
|
"options/contrast",
|
|
|
|
"options/saturation",
|
|
|
|
"options/hue",
|
|
|
|
"options/deinterlace",
|
|
|
|
"options/vf",
|
|
|
|
"options/af",
|
|
|
|
"options/panscan",
|
|
|
|
"options/aid",
|
|
|
|
"options/vid",
|
|
|
|
"options/sid",
|
|
|
|
"options/sub-delay",
|
|
|
|
"options/sub-pos",
|
|
|
|
"options/sub-visibility",
|
|
|
|
"options/sub-scale",
|
|
|
|
"options/ass-use-margins",
|
|
|
|
"options/ass-vsfilter-aspect-compat",
|
|
|
|
"options/ass-style-override",
|
2013-10-29 21:38:29 +00:00
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2014-02-25 21:34:32 +00:00
|
|
|
// Used to retrieve default settings, which should not be stored in the
|
|
|
|
// resume config. Uses backup_properties[] meaning/order of values.
|
|
|
|
// This explicitly includes values set by config files and command line.
|
|
|
|
void mp_get_resume_defaults(struct MPContext *mpctx)
|
|
|
|
{
|
|
|
|
char **list =
|
|
|
|
talloc_zero_array(mpctx, char*, MP_ARRAY_SIZE(backup_properties));
|
|
|
|
for (int i = 0; backup_properties[i]; i++) {
|
|
|
|
const char *pname = backup_properties[i];
|
|
|
|
char *val = NULL;
|
2014-02-25 21:51:55 +00:00
|
|
|
int r = mp_property_do(pname, M_PROPERTY_GET_STRING, &val, mpctx);
|
2014-02-25 21:34:32 +00:00
|
|
|
if (r == M_PROPERTY_OK)
|
|
|
|
list[i] = talloc_steal(list, val);
|
|
|
|
}
|
|
|
|
mpctx->resume_defaults = list;
|
|
|
|
}
|
|
|
|
|
2013-10-29 21:38:29 +00:00
|
|
|
// Should follow what parser-cfg.c does/needs
|
|
|
|
static bool needs_config_quoting(const char *s)
|
|
|
|
{
|
2014-10-16 13:40:19 +00:00
|
|
|
if (s[0] == '%')
|
|
|
|
return true;
|
2013-10-29 21:38:29 +00:00
|
|
|
for (int i = 0; s && s[i]; i++) {
|
|
|
|
unsigned char c = s[i];
|
2014-07-01 21:10:38 +00:00
|
|
|
if (!mp_isprint(c) || mp_isspace(c) || c == '#' || c == '\'' || c == '"')
|
2013-10-29 21:38:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mp_write_watch_later_conf(struct MPContext *mpctx)
|
|
|
|
{
|
|
|
|
char *filename = mpctx->filename;
|
2014-06-26 17:13:37 +00:00
|
|
|
char *conffile = NULL;
|
2013-10-29 21:38:29 +00:00
|
|
|
if (!filename)
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
double pos = get_current_time(mpctx);
|
|
|
|
if (pos == MP_NOPTS_VALUE)
|
|
|
|
goto exit;
|
|
|
|
|
2013-12-21 19:45:19 +00:00
|
|
|
mp_mk_config_dir(mpctx->global, MP_WATCH_LATER_CONF);
|
2013-10-29 21:38:29 +00:00
|
|
|
|
2014-09-16 16:23:01 +00:00
|
|
|
conffile = mp_get_playback_resume_config_filename(mpctx->global, filename);
|
2013-10-29 21:38:29 +00:00
|
|
|
if (!conffile)
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
MP_INFO(mpctx, "Saving state.\n");
|
|
|
|
|
|
|
|
FILE *file = fopen(conffile, "wb");
|
|
|
|
if (!file)
|
|
|
|
goto exit;
|
2014-09-16 16:23:01 +00:00
|
|
|
if (mpctx->opts->write_filename_in_watch_later_config) {
|
|
|
|
char write_name[1024] = {0};
|
|
|
|
for (int n = 0; filename[n] && n < sizeof(write_name) - 1; n++)
|
|
|
|
write_name[n] = (unsigned char)filename[n] < 32 ? '_' : filename[n];
|
|
|
|
fprintf(file, "# %s\n", write_name);
|
|
|
|
}
|
2013-10-29 21:38:29 +00:00
|
|
|
fprintf(file, "start=%f\n", pos);
|
|
|
|
for (int i = 0; backup_properties[i]; i++) {
|
|
|
|
const char *pname = backup_properties[i];
|
|
|
|
char *val = NULL;
|
|
|
|
int r = mp_property_do(pname, M_PROPERTY_GET_STRING, &val, mpctx);
|
|
|
|
if (r == M_PROPERTY_OK) {
|
2014-02-25 21:51:55 +00:00
|
|
|
if (strncmp(pname, "options/", 8) == 0)
|
|
|
|
pname += 8;
|
2014-02-25 21:34:32 +00:00
|
|
|
// Only store it if it's different from the initial value.
|
|
|
|
char *prev = mpctx->resume_defaults[i];
|
|
|
|
if (!prev || strcmp(prev, val) != 0) {
|
|
|
|
if (needs_config_quoting(val)) {
|
|
|
|
// e.g. '%6%STRING'
|
|
|
|
fprintf(file, "%s=%%%d%%%s\n", pname, (int)strlen(val), val);
|
|
|
|
} else {
|
|
|
|
fprintf(file, "%s=%s\n", pname, val);
|
|
|
|
}
|
2013-10-29 21:38:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
talloc_free(val);
|
|
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
exit:
|
2014-06-26 17:13:37 +00:00
|
|
|
talloc_free(conffile);
|
2013-10-29 21:38:29 +00:00
|
|
|
}
|
|
|
|
|
2013-12-21 19:45:19 +00:00
|
|
|
void mp_load_playback_resume(struct MPContext *mpctx, const char *file)
|
2013-10-29 21:38:29 +00:00
|
|
|
{
|
2013-12-21 19:45:19 +00:00
|
|
|
char *fname = mp_get_playback_resume_config_filename(mpctx->global, file);
|
2013-10-29 21:38:29 +00:00
|
|
|
if (fname && mp_path_exists(fname)) {
|
|
|
|
// Never apply the saved start position to following files
|
2013-12-21 19:45:19 +00:00
|
|
|
m_config_backup_opt(mpctx->mconfig, "start");
|
|
|
|
MP_INFO(mpctx, "Resuming playback. This behavior can "
|
2013-10-29 21:38:29 +00:00
|
|
|
"be disabled with --no-resume-playback.\n");
|
2013-12-21 19:45:19 +00:00
|
|
|
try_load_config(mpctx, fname, M_SETOPT_PRESERVE_CMDLINE);
|
2013-10-29 21:38:29 +00:00
|
|
|
unlink(fname);
|
|
|
|
}
|
|
|
|
talloc_free(fname);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the first file that has a resume config.
|
|
|
|
// Compared to hashing the playlist file or contents and managing separate
|
|
|
|
// resume file for them, this is simpler, and also has the nice property
|
|
|
|
// that appending to a playlist doesn't interfere with resuming (especially
|
|
|
|
// if the playlist comes from the command line).
|
2013-12-21 19:45:19 +00:00
|
|
|
struct playlist_entry *mp_check_playlist_resume(struct MPContext *mpctx,
|
|
|
|
struct playlist *playlist)
|
2013-10-29 21:38:29 +00:00
|
|
|
{
|
2013-12-21 19:45:19 +00:00
|
|
|
if (!mpctx->opts->position_resume)
|
2013-10-29 21:38:29 +00:00
|
|
|
return NULL;
|
|
|
|
for (struct playlist_entry *e = playlist->first; e; e = e->next) {
|
2013-12-21 19:45:19 +00:00
|
|
|
char *conf = mp_get_playback_resume_config_filename(mpctx->global,
|
|
|
|
e->filename);
|
2013-10-29 21:38:29 +00:00
|
|
|
bool exists = conf && mp_path_exists(conf);
|
|
|
|
talloc_free(conf);
|
|
|
|
if (exists)
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|