2004-07-23 16:35:20 +00: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-30 23:24:23 +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.
|
2004-07-23 16:35:20 +00:00
|
|
|
*/
|
2007-08-28 11:20:24 +00:00
|
|
|
|
2012-12-09 14:05:21 +00:00
|
|
|
#include <assert.h>
|
2007-08-28 11:20:24 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2011-02-23 14:18:09 +00:00
|
|
|
#include <stdbool.h>
|
2012-02-03 07:05:11 +00: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 17:37:29 +00:00
|
|
|
#include <errno.h>
|
2010-02-18 09:29:05 +00:00
|
|
|
#include "config.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "core/mp_msg.h"
|
|
|
|
#include "core/path.h"
|
2012-12-09 14:05:21 +00:00
|
|
|
#include "talloc.h"
|
|
|
|
#include "osdep/io.h"
|
2007-08-28 11:20:24 +00:00
|
|
|
|
2012-12-09 14:05:21 +00:00
|
|
|
#if defined(__MINGW32__)
|
2007-08-29 09:31:44 +00:00
|
|
|
#include <windows.h>
|
2009-01-30 23:29:39 +00:00
|
|
|
#elif defined(__CYGWIN__)
|
2008-10-13 14:55:01 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <sys/cygwin.h>
|
2008-02-24 12:41:51 +00:00
|
|
|
#endif
|
|
|
|
|
2012-12-09 14:05:21 +00:00
|
|
|
#ifdef CONFIG_MACOSX_BUNDLE
|
|
|
|
#include "osdep/macosx_bundle.h"
|
|
|
|
#endif
|
2011-02-23 14:18:09 +00:00
|
|
|
|
2010-03-10 10:00:59 +00:00
|
|
|
|
2012-12-09 14:05:21 +00:00
|
|
|
typedef char *(*lookup_fun)(const char *);
|
|
|
|
static const lookup_fun config_lookup_functions[] = {
|
|
|
|
mp_find_user_config_file,
|
|
|
|
#ifdef CONFIG_MACOSX_BUNDLE
|
|
|
|
get_bundled_path,
|
|
|
|
#endif
|
|
|
|
mp_find_global_config_file,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
char *mp_find_config_file(const char *filename)
|
2012-12-10 20:18:46 +00:00
|
|
|
{
|
2012-12-09 14:05:21 +00:00
|
|
|
for (int i = 0; config_lookup_functions[i] != NULL; i++) {
|
|
|
|
char *path = config_lookup_functions[i](filename);
|
|
|
|
if (!path) continue;
|
|
|
|
|
|
|
|
if (mp_path_exists(path))
|
|
|
|
return path;
|
|
|
|
|
|
|
|
talloc_free(path);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *mp_find_user_config_file(const char *filename)
|
|
|
|
{
|
|
|
|
char *homedir = NULL, *buff = NULL;
|
2007-08-29 09:43:00 +00:00
|
|
|
#ifdef __MINGW32__
|
2012-12-09 14:05:21 +00:00
|
|
|
static char *config_dir = "mpv";
|
2003-04-18 18:17:05 +00:00
|
|
|
#else
|
2012-12-09 14:05:21 +00:00
|
|
|
static char *config_dir = ".mpv";
|
2012-04-28 15:37:01 +00:00
|
|
|
#endif
|
|
|
|
#if defined(__MINGW32__) || defined(__CYGWIN__)
|
2013-05-22 15:31:38 +00:00
|
|
|
char *temp = NULL;
|
2012-12-10 20:18:46 +00:00
|
|
|
char exedir[260];
|
2013-05-22 15:31:38 +00:00
|
|
|
/* Hack to get fonts etc. loaded outside of Cygwin environment. */
|
|
|
|
int i, imax = 0;
|
|
|
|
int len = (int)GetModuleFileNameA(NULL, exedir, 260);
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
if (exedir[i] == '\\') {
|
|
|
|
exedir[i] = '/';
|
|
|
|
imax = i;
|
|
|
|
}
|
|
|
|
exedir[imax] = '\0';
|
|
|
|
|
|
|
|
if (filename)
|
|
|
|
temp = mp_path_join(NULL, bstr0(exedir), bstr0(filename));
|
|
|
|
|
|
|
|
if (temp && mp_path_exists(temp) && !mp_path_isdir(temp)) {
|
|
|
|
homedir = exedir;
|
|
|
|
config_dir = "";
|
|
|
|
}
|
|
|
|
else
|
2003-04-18 18:17:05 +00:00
|
|
|
#endif
|
2012-12-09 14:05:21 +00:00
|
|
|
if ((homedir = getenv("MPV_HOME")) != NULL) {
|
2012-12-10 20:18:46 +00:00
|
|
|
config_dir = "";
|
2012-12-09 14:05:21 +00:00
|
|
|
} else if ((homedir = getenv("HOME")) == NULL) {
|
2007-08-29 09:43:00 +00:00
|
|
|
#if defined(__MINGW32__) || defined(__CYGWIN__)
|
2012-12-10 20:18:46 +00:00
|
|
|
homedir = exedir;
|
2003-04-18 18:17:05 +00:00
|
|
|
#else
|
2012-12-10 20:18:46 +00:00
|
|
|
return NULL;
|
2007-08-29 09:43:00 +00:00
|
|
|
#endif
|
2012-12-10 20:18:46 +00:00
|
|
|
}
|
2013-05-22 15:31:38 +00:00
|
|
|
#if defined(__MINGW32__) || defined(__CYGWIN__)
|
|
|
|
talloc_free(temp);
|
|
|
|
#endif
|
2005-04-13 11:46:35 +00:00
|
|
|
|
2012-12-09 14:05:21 +00:00
|
|
|
if (filename) {
|
|
|
|
char * temp = mp_path_join(NULL, bstr0(homedir), bstr0(config_dir));
|
|
|
|
buff = mp_path_join(NULL, bstr0(temp), bstr0(filename));
|
|
|
|
talloc_free(temp);
|
|
|
|
} else {
|
|
|
|
buff = mp_path_join(NULL, bstr0(homedir), bstr0(config_dir));
|
2012-12-10 20:18:46 +00:00
|
|
|
}
|
2012-12-09 14:05:21 +00:00
|
|
|
|
2012-12-10 20:18:46 +00:00
|
|
|
mp_msg(MSGT_GLOBAL, MSGL_V, "get_path('%s') -> '%s'\n", filename, buff);
|
|
|
|
return buff;
|
2001-10-30 17:04:59 +00:00
|
|
|
}
|
2005-10-16 19:14:09 +00:00
|
|
|
|
2012-12-09 14:05:21 +00:00
|
|
|
char *mp_find_global_config_file(const char *filename)
|
|
|
|
{
|
|
|
|
if (filename) {
|
|
|
|
return mp_path_join(NULL, bstr0(MPLAYER_CONFDIR), bstr0(filename));
|
|
|
|
} else {
|
|
|
|
return talloc_strdup(NULL, MPLAYER_CONFDIR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-23 14:18:09 +00:00
|
|
|
char *mp_basename(const char *path)
|
2010-11-16 21:06:52 +00: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 14:18:09 +00:00
|
|
|
return s ? s + 1 : (char *)path;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct bstr mp_dirname(const char *path)
|
|
|
|
{
|
2012-12-10 20:18:46 +00:00
|
|
|
struct bstr ret = {
|
|
|
|
(uint8_t *)path, mp_basename(path) - path
|
|
|
|
};
|
2011-02-23 14:18:09 +00:00
|
|
|
if (ret.len == 0)
|
2012-07-28 21:47:42 +00:00
|
|
|
return bstr0(".");
|
2011-02-23 14:18:09 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 20:18:46 +00:00
|
|
|
|| p1.len == 2 && endchar1 == ':'; // "X:" only
|
2011-02-23 14:18:09 +00: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 21:06:52 +00:00
|
|
|
}
|
2012-02-03 07:05:11 +00: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 17:37:29 +00: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 07:05:11 +00: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);
|
|
|
|
}
|