2004-07-23 18:35:20 +02:00
|
|
|
/*
|
|
|
|
* Get path to config dir/file.
|
|
|
|
*
|
|
|
|
* Return Values:
|
|
|
|
* Returns the pointer to the ALLOCATED buffer containing the
|
|
|
|
* zero terminated path string. This buffer has to be FREED
|
|
|
|
* by the caller.
|
2010-01-31 00:24:23 +01: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.
|
2004-07-23 18:35:20 +02:00
|
|
|
*/
|
2007-08-28 13:20:24 +02:00
|
|
|
|
2012-12-09 15:05:21 +01:00
|
|
|
#include <assert.h>
|
2007-08-28 13:20:24 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2011-02-23 15:18:09 +01:00
|
|
|
#include <stdbool.h>
|
2012-02-03 08:05:11 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
core: add playback resume feature (manual/opt-in)
A "watch later" command is now mapped to Shift+Q. This quits the player
and stores the playback state in a config file in ~/.mpv/watch_later/.
When calling the player with the same file again, playback is resumed
at that time position.
It's also possible to make mpv save playback state always on quit with
the --save-position-on-quit option. Likewise, resuming can be disabled
with the --no-resume-playback option.
This also attempts to save some playback parameters, like fullscreen
state or track selection. This will unconditionally override config
settings and command line options (which is probably not what you would
expect, but in general nobody will really care about this). Some things
are not backed up, because that would cause various problems. Additional
subtitle files, video filters, etc. are not stored because that would be
too hard and fragile. Volume/mute state are not stored because it would
mess up if the system mixer is used, or if the system mixer was
readjusted in the meantime.
Basically, the tradeoff between perfect state restoration and
complexity/fragility makes it not worth to attempt to implement
it perfectly, even if the result is a little bit inconsistent.
2013-05-05 19:37:29 +02:00
|
|
|
#include <errno.h>
|
2014-02-14 13:52:59 +01:00
|
|
|
|
2010-02-18 10:29:05 +01:00
|
|
|
#include "config.h"
|
2014-02-14 13:52:59 +01:00
|
|
|
|
2013-12-21 20:45:19 +01:00
|
|
|
#include "common/global.h"
|
2013-12-17 02:39:45 +01:00
|
|
|
#include "common/msg.h"
|
2014-02-14 13:52:59 +01:00
|
|
|
#include "options/options.h"
|
2013-12-17 02:02:25 +01:00
|
|
|
#include "options/path.h"
|
2012-12-09 15:05:21 +01:00
|
|
|
#include "talloc.h"
|
|
|
|
#include "osdep/io.h"
|
2013-09-18 18:42:18 +02:00
|
|
|
#include "osdep/path.h"
|
2008-02-24 13:41:51 +01:00
|
|
|
|
2013-12-21 20:45:19 +01:00
|
|
|
typedef char *(*lookup_fun)(void *tctx, struct mpv_global *global, const char *);
|
2012-12-09 15:05:21 +01:00
|
|
|
static const lookup_fun config_lookup_functions[] = {
|
|
|
|
mp_find_user_config_file,
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_COCOA
|
2013-09-18 19:26:26 +02:00
|
|
|
mp_get_macosx_bundled_path,
|
2012-12-09 15:05:21 +01:00
|
|
|
#endif
|
|
|
|
mp_find_global_config_file,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2014-02-25 21:19:18 +01:00
|
|
|
#define STRNULL(s) ((s) ? (s) : "(NULL)")
|
|
|
|
|
2013-12-21 20:45:19 +01:00
|
|
|
char *mp_find_config_file(void *talloc_ctx, struct mpv_global *global,
|
|
|
|
const char *filename)
|
2012-12-10 21:18:46 +01:00
|
|
|
{
|
2014-02-14 13:52:59 +01:00
|
|
|
struct MPOpts *opts = global->opts;
|
2014-02-25 21:04:04 +01:00
|
|
|
|
2014-02-25 21:19:18 +01:00
|
|
|
char *res = NULL;
|
|
|
|
if (opts->load_config) {
|
|
|
|
if (opts->force_configdir && opts->force_configdir[0]) {
|
|
|
|
// Always force the local config dir.
|
|
|
|
res = mp_find_user_config_file(talloc_ctx, global, filename);
|
|
|
|
} else {
|
|
|
|
for (int i = 0; config_lookup_functions[i] != NULL; i++) {
|
|
|
|
res = config_lookup_functions[i](talloc_ctx, global, filename);
|
|
|
|
if (!res)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (mp_path_exists(res))
|
|
|
|
break;
|
|
|
|
|
|
|
|
talloc_free(res);
|
|
|
|
res = NULL;
|
|
|
|
}
|
|
|
|
}
|
2012-12-09 15:05:21 +01:00
|
|
|
}
|
2014-02-25 21:19:18 +01:00
|
|
|
MP_VERBOSE(global, "any config path: '%s' -> '%s'\n", STRNULL(filename),
|
|
|
|
STRNULL(res));
|
|
|
|
return res;
|
2012-12-09 15:05:21 +01:00
|
|
|
}
|
|
|
|
|
2013-12-21 20:45:19 +01:00
|
|
|
char *mp_find_user_config_file(void *talloc_ctx, struct mpv_global *global,
|
|
|
|
const char *filename)
|
2012-12-09 15:05:21 +01:00
|
|
|
{
|
2014-02-14 13:52:59 +01:00
|
|
|
struct MPOpts *opts = global->opts;
|
2014-02-25 21:04:04 +01:00
|
|
|
|
2014-02-25 21:19:18 +01:00
|
|
|
char *res = NULL;
|
|
|
|
if (opts->load_config) {
|
|
|
|
if (opts->force_configdir && opts->force_configdir[0]) {
|
|
|
|
res = mp_path_join(talloc_ctx, bstr0(opts->force_configdir),
|
|
|
|
bstr0(filename));
|
|
|
|
} else {
|
|
|
|
char *homedir = getenv("MPV_HOME");
|
|
|
|
char *configdir = NULL;
|
2013-05-22 17:31:38 +02:00
|
|
|
|
2014-02-25 21:19:18 +01:00
|
|
|
if (!homedir) {
|
2013-09-18 18:42:18 +02:00
|
|
|
#ifdef _WIN32
|
2014-02-25 21:19:18 +01:00
|
|
|
res = talloc_steal(talloc_ctx, mp_get_win_config_path(filename));
|
2007-08-29 11:43:00 +02:00
|
|
|
#endif
|
2014-02-25 21:19:18 +01:00
|
|
|
homedir = getenv("HOME");
|
|
|
|
configdir = ".mpv";
|
|
|
|
}
|
2005-04-13 13:46:35 +02:00
|
|
|
|
2014-02-25 21:19:18 +01:00
|
|
|
if (!res && homedir) {
|
|
|
|
char *temp = mp_path_join(NULL, bstr0(homedir), bstr0(configdir));
|
|
|
|
res = mp_path_join(talloc_ctx, bstr0(temp), bstr0(filename));
|
|
|
|
talloc_free(temp);
|
|
|
|
}
|
|
|
|
}
|
2012-12-10 21:18:46 +01:00
|
|
|
}
|
2012-12-09 15:05:21 +01:00
|
|
|
|
2014-02-25 21:19:18 +01:00
|
|
|
MP_VERBOSE(global, "user config path: '%s' -> '%s'\n", STRNULL(filename),
|
|
|
|
STRNULL(res));
|
|
|
|
return res;
|
2001-10-30 18:04:59 +01:00
|
|
|
}
|
2005-10-16 21:14:09 +02:00
|
|
|
|
2013-12-21 20:45:19 +01:00
|
|
|
char *mp_find_global_config_file(void *talloc_ctx, struct mpv_global *global,
|
|
|
|
const char *filename)
|
2012-12-09 15:05:21 +01:00
|
|
|
{
|
2014-02-25 21:04:04 +01:00
|
|
|
struct MPOpts *opts = global->opts;
|
2014-02-25 21:19:18 +01:00
|
|
|
char *res = NULL;
|
|
|
|
|
2014-02-28 21:30:29 +01:00
|
|
|
if (opts->load_config && !(opts->force_configdir && opts->force_configdir[0]))
|
2014-02-25 21:19:18 +01:00
|
|
|
{
|
|
|
|
if (filename) {
|
|
|
|
res = mp_path_join(talloc_ctx, bstr0(MPLAYER_CONFDIR), bstr0(filename));
|
|
|
|
} else {
|
|
|
|
res = talloc_strdup(talloc_ctx, MPLAYER_CONFDIR);
|
|
|
|
}
|
2012-12-09 15:05:21 +01:00
|
|
|
}
|
2014-02-25 21:19:18 +01:00
|
|
|
|
|
|
|
MP_VERBOSE(global, "global config path: '%s' -> '%s'\n", STRNULL(filename),
|
|
|
|
STRNULL(res));
|
|
|
|
return res;
|
2012-12-09 15:05:21 +01:00
|
|
|
}
|
|
|
|
|
2013-12-21 20:45:19 +01:00
|
|
|
char *mp_get_user_path(void *talloc_ctx, struct mpv_global *global,
|
|
|
|
const char *path)
|
2013-12-14 19:50:00 +01:00
|
|
|
{
|
|
|
|
if (!path)
|
|
|
|
return NULL;
|
2014-02-25 21:19:18 +01:00
|
|
|
char *res = NULL;
|
2013-12-14 19:50:00 +01:00
|
|
|
bstr bpath = bstr0(path);
|
|
|
|
if (bstr_eatstart0(&bpath, "~")) {
|
|
|
|
// parse to "~" <prefix> "/" <rest>
|
|
|
|
bstr prefix, rest;
|
|
|
|
if (bstr_split_tok(bpath, "/", &prefix, &rest)) {
|
|
|
|
const char *rest0 = rest.start; // ok in this case
|
2013-12-21 20:45:19 +01:00
|
|
|
if (bstr_equals0(prefix, "~")) {
|
|
|
|
res = mp_find_user_config_file(talloc_ctx, global, rest0);
|
|
|
|
} else if (bstr_equals0(prefix, "")) {
|
2013-12-14 19:50:00 +01:00
|
|
|
res = mp_path_join(talloc_ctx, bstr0(getenv("HOME")), rest);
|
2013-12-21 20:45:19 +01:00
|
|
|
}
|
2013-12-14 19:50:00 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-25 21:19:18 +01:00
|
|
|
if (!res)
|
|
|
|
res = talloc_strdup(talloc_ctx, path);
|
|
|
|
MP_VERBOSE(global, "user path: '%s' -> '%s'\n", path, res);
|
|
|
|
return res;
|
2013-12-14 19:50:00 +01:00
|
|
|
}
|
|
|
|
|
2011-02-23 15:18:09 +01:00
|
|
|
char *mp_basename(const char *path)
|
2010-11-16 22:06:52 +01:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
#if HAVE_DOS_PATHS
|
|
|
|
s = strrchr(path, '\\');
|
|
|
|
if (s)
|
|
|
|
path = s + 1;
|
|
|
|
s = strrchr(path, ':');
|
|
|
|
if (s)
|
|
|
|
path = s + 1;
|
|
|
|
#endif
|
|
|
|
s = strrchr(path, '/');
|
2011-02-23 15:18:09 +01:00
|
|
|
return s ? s + 1 : (char *)path;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct bstr mp_dirname(const char *path)
|
|
|
|
{
|
2012-12-10 21:18:46 +01:00
|
|
|
struct bstr ret = {
|
|
|
|
(uint8_t *)path, mp_basename(path) - path
|
|
|
|
};
|
2011-02-23 15:18:09 +01:00
|
|
|
if (ret.len == 0)
|
2012-07-28 23:47:42 +02:00
|
|
|
return bstr0(".");
|
2011-02-23 15:18:09 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-07-08 20:34:26 +02:00
|
|
|
char *mp_splitext(const char *path, bstr *root)
|
|
|
|
{
|
|
|
|
assert(path);
|
|
|
|
const char *split = strrchr(path, '.');
|
2013-12-22 23:04:19 +01:00
|
|
|
if (!split || !split[1] || strchr(split, '/'))
|
|
|
|
return NULL;
|
2013-07-08 20:34:26 +02:00
|
|
|
if (root)
|
2013-12-22 23:04:19 +01:00
|
|
|
*root = (bstr){(char *)path, split - path};
|
|
|
|
return (char *)split + 1;
|
2013-07-08 20:34:26 +02:00
|
|
|
}
|
|
|
|
|
2011-02-23 15:18:09 +01:00
|
|
|
char *mp_path_join(void *talloc_ctx, struct bstr p1, struct bstr p2)
|
|
|
|
{
|
|
|
|
if (p1.len == 0)
|
|
|
|
return bstrdup0(talloc_ctx, p2);
|
|
|
|
if (p2.len == 0)
|
|
|
|
return bstrdup0(talloc_ctx, p1);
|
|
|
|
|
|
|
|
#if HAVE_DOS_PATHS
|
|
|
|
if (p2.len >= 2 && p2.start[1] == ':'
|
|
|
|
|| p2.start[0] == '\\' || p2.start[0] == '/')
|
|
|
|
#else
|
|
|
|
if (p2.start[0] == '/')
|
|
|
|
#endif
|
|
|
|
return bstrdup0(talloc_ctx, p2); // absolute path
|
|
|
|
|
|
|
|
bool have_separator;
|
|
|
|
int endchar1 = p1.start[p1.len - 1];
|
|
|
|
#if HAVE_DOS_PATHS
|
|
|
|
have_separator = endchar1 == '/' || endchar1 == '\\'
|
2012-12-10 21:18:46 +01:00
|
|
|
|| p1.len == 2 && endchar1 == ':'; // "X:" only
|
2011-02-23 15:18:09 +01:00
|
|
|
#else
|
|
|
|
have_separator = endchar1 == '/';
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return talloc_asprintf(talloc_ctx, "%.*s%s%.*s", BSTR_P(p1),
|
|
|
|
have_separator ? "" : "/", BSTR_P(p2));
|
2010-11-16 22:06:52 +01:00
|
|
|
}
|
2012-02-03 08:05:11 +01:00
|
|
|
|
core: add playback resume feature (manual/opt-in)
A "watch later" command is now mapped to Shift+Q. This quits the player
and stores the playback state in a config file in ~/.mpv/watch_later/.
When calling the player with the same file again, playback is resumed
at that time position.
It's also possible to make mpv save playback state always on quit with
the --save-position-on-quit option. Likewise, resuming can be disabled
with the --no-resume-playback option.
This also attempts to save some playback parameters, like fullscreen
state or track selection. This will unconditionally override config
settings and command line options (which is probably not what you would
expect, but in general nobody will really care about this). Some things
are not backed up, because that would cause various problems. Additional
subtitle files, video filters, etc. are not stored because that would be
too hard and fragile. Volume/mute state are not stored because it would
mess up if the system mixer is used, or if the system mixer was
readjusted in the meantime.
Basically, the tradeoff between perfect state restoration and
complexity/fragility makes it not worth to attempt to implement
it perfectly, even if the result is a little bit inconsistent.
2013-05-05 19:37:29 +02:00
|
|
|
char *mp_getcwd(void *talloc_ctx)
|
|
|
|
{
|
|
|
|
char *wd = talloc_array(talloc_ctx, char, 20);
|
|
|
|
while (getcwd(wd, talloc_get_size(wd)) == NULL) {
|
|
|
|
if (errno != ERANGE) {
|
|
|
|
talloc_free(wd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
wd = talloc_realloc(talloc_ctx, wd, char, talloc_get_size(wd) * 2);
|
|
|
|
}
|
|
|
|
return wd;
|
|
|
|
}
|
|
|
|
|
2012-02-03 08:05:11 +01:00
|
|
|
bool mp_path_exists(const char *path)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
return mp_stat(path, &st) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mp_path_isdir(const char *path)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
return mp_stat(path, &st) == 0 && S_ISDIR(st.st_mode);
|
|
|
|
}
|
2013-09-04 14:04:35 +02:00
|
|
|
|
|
|
|
// Return false if it's considered a normal local filesystem path.
|
|
|
|
bool mp_is_url(bstr path)
|
|
|
|
{
|
2013-09-04 16:28:02 +02:00
|
|
|
int proto = bstr_find0(path, "://");
|
2013-12-22 23:05:12 +01:00
|
|
|
if (proto < 1)
|
2013-09-04 16:28:02 +02:00
|
|
|
return false;
|
|
|
|
// The protocol part must be alphanumeric, otherwise it's not an URL.
|
|
|
|
for (int i = 0; i < proto; i++) {
|
|
|
|
unsigned char c = path.start[i];
|
|
|
|
if (!(c >= 'a' && c <= 'z') && !(c >= 'A' && c <= 'Z') &&
|
|
|
|
!(c >= '0' && c <= '9') && c != '_')
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2013-09-04 14:04:35 +02:00
|
|
|
}
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2013-12-22 23:05:37 +01:00
|
|
|
// Return the protocol part of path, e.g. "http" if path is "http://...".
|
|
|
|
// On success, out_url (if not NULL) is set to the part after the "://".
|
|
|
|
bstr mp_split_proto(bstr path, bstr *out_url)
|
|
|
|
{
|
|
|
|
if (!mp_is_url(path))
|
|
|
|
return (bstr){0};
|
|
|
|
bstr r;
|
|
|
|
bstr_split_tok(path, "://", &r, out_url ? out_url : &(bstr){0});
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-12-21 20:45:19 +01:00
|
|
|
void mp_mk_config_dir(struct mpv_global *global, char *subdir)
|
2013-10-29 22:38:29 +01:00
|
|
|
{
|
|
|
|
void *tmp = talloc_new(NULL);
|
2013-12-21 20:45:19 +01:00
|
|
|
char *confdir = mp_find_user_config_file(tmp, global, "");
|
2013-10-29 22:38:29 +01:00
|
|
|
if (confdir) {
|
|
|
|
if (subdir)
|
|
|
|
confdir = mp_path_join(tmp, bstr0(confdir), bstr0(subdir));
|
|
|
|
mkdir(confdir, 0777);
|
|
|
|
}
|
|
|
|
talloc_free(tmp);
|
|
|
|
}
|