2014-05-12 23:14:07 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
Relicense some non-MPlayer source files to LGPL 2.1 or later
This covers source files which were added in mplayer2 and mpv times
only, and where all code is covered by LGPL relicensing agreements.
There are probably more files to which this applies, but I'm being
conservative here.
A file named ao_sdl.c exists in MPlayer too, but the mpv one is a
complete rewrite, and was added some time after the original ao_sdl.c
was removed. The same applies to vo_sdl.c, for which the SDL2 API is
radically different in addition (MPlayer supports SDL 1.2 only).
common.c contains only code written by me. But common.h is a strange
case: although it originally was named mp_common.h and exists in MPlayer
too, by now it contains only definitions written by uau and me. The
exceptions are the CONTROL_ defines - thus not changing the license of
common.h yet.
codec_tags.c contained once large tables generated from MPlayer's
codecs.conf, but all of these tables were removed.
From demux_playlist.c I'm removing a code fragment from someone who was
not asked; this probably could be done later (see commit 15dccc37).
misc.c is a bit complicated to reason about (it was split off mplayer.c
and thus contains random functions out of this file), but actually all
functions have been added post-MPlayer. Except get_relative_time(),
which was written by uau, but looks similar to 3 different versions of
something similar in each of the Unix/win32/OSX timer source files. I'm
not sure what that means in regards to copyright, so I've just moved it
into another still-GPL source file for now.
screenshot.c once had some minor parts of MPlayer's vf_screenshot.c, but
they're all gone.
2016-01-19 17:36:06 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2014-05-12 23:14:07 +00:00
|
|
|
*
|
|
|
|
* mpv 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
|
Relicense some non-MPlayer source files to LGPL 2.1 or later
This covers source files which were added in mplayer2 and mpv times
only, and where all code is covered by LGPL relicensing agreements.
There are probably more files to which this applies, but I'm being
conservative here.
A file named ao_sdl.c exists in MPlayer too, but the mpv one is a
complete rewrite, and was added some time after the original ao_sdl.c
was removed. The same applies to vo_sdl.c, for which the SDL2 API is
radically different in addition (MPlayer supports SDL 1.2 only).
common.c contains only code written by me. But common.h is a strange
case: although it originally was named mp_common.h and exists in MPlayer
too, by now it contains only definitions written by uau and me. The
exceptions are the CONTROL_ defines - thus not changing the license of
common.h yet.
codec_tags.c contained once large tables generated from MPlayer's
codecs.conf, but all of these tables were removed.
From demux_playlist.c I'm removing a code fragment from someone who was
not asked; this probably could be done later (see commit 15dccc37).
misc.c is a bit complicated to reason about (it was split off mplayer.c
and thus contains random functions out of this file), but actually all
functions have been added post-MPlayer. Except get_relative_time(),
which was written by uau, but looks similar to 3 different versions of
something similar in each of the Unix/win32/OSX timer source files. I'm
not sure what that means in regards to copyright, so I've just moved it
into another still-GPL source file for now.
screenshot.c once had some minor parts of MPlayer's vf_screenshot.c, but
they're all gone.
2016-01-19 17:36:06 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2014-05-12 23:14:07 +00:00
|
|
|
*
|
Relicense some non-MPlayer source files to LGPL 2.1 or later
This covers source files which were added in mplayer2 and mpv times
only, and where all code is covered by LGPL relicensing agreements.
There are probably more files to which this applies, but I'm being
conservative here.
A file named ao_sdl.c exists in MPlayer too, but the mpv one is a
complete rewrite, and was added some time after the original ao_sdl.c
was removed. The same applies to vo_sdl.c, for which the SDL2 API is
radically different in addition (MPlayer supports SDL 1.2 only).
common.c contains only code written by me. But common.h is a strange
case: although it originally was named mp_common.h and exists in MPlayer
too, by now it contains only definitions written by uau and me. The
exceptions are the CONTROL_ defines - thus not changing the license of
common.h yet.
codec_tags.c contained once large tables generated from MPlayer's
codecs.conf, but all of these tables were removed.
From demux_playlist.c I'm removing a code fragment from someone who was
not asked; this probably could be done later (see commit 15dccc37).
misc.c is a bit complicated to reason about (it was split off mplayer.c
and thus contains random functions out of this file), but actually all
functions have been added post-MPlayer. Except get_relative_time(),
which was written by uau, but looks similar to 3 different versions of
something similar in each of the Unix/win32/OSX timer source files. I'm
not sure what that means in regards to copyright, so I've just moved it
into another still-GPL source file for now.
screenshot.c once had some minor parts of MPlayer's vf_screenshot.c, but
they're all gone.
2016-01-19 17:36:06 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2014-05-12 23:14:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
2014-07-10 06:28:03 +00:00
|
|
|
#include <strings.h>
|
2014-05-12 23:14:07 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "osdep/io.h"
|
2014-10-19 21:32:34 +00:00
|
|
|
#include "osdep/threads.h"
|
2014-05-12 23:14:07 +00:00
|
|
|
|
|
|
|
#include "common/common.h"
|
|
|
|
#include "common/msg.h"
|
|
|
|
#include "options/path.h"
|
2014-08-29 10:09:04 +00:00
|
|
|
#include "misc/bstr.h"
|
2014-05-12 23:14:07 +00:00
|
|
|
#include "core.h"
|
|
|
|
#include "client.h"
|
|
|
|
#include "libmpv/client.h"
|
|
|
|
|
|
|
|
extern const struct mp_scripting mp_scripting_lua;
|
|
|
|
|
2014-06-10 21:56:05 +00:00
|
|
|
static const struct mp_scripting *const scripting_backends[] = {
|
2014-05-12 23:14:07 +00:00
|
|
|
#if HAVE_LUA
|
|
|
|
&mp_scripting_lua,
|
|
|
|
#endif
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static char *script_name_from_filename(void *talloc_ctx, const char *fname)
|
|
|
|
{
|
|
|
|
fname = mp_basename(fname);
|
|
|
|
if (fname[0] == '@')
|
|
|
|
fname += 1;
|
|
|
|
char *name = talloc_strdup(talloc_ctx, fname);
|
|
|
|
// Drop file extension
|
|
|
|
char *dot = strrchr(name, '.');
|
|
|
|
if (dot)
|
|
|
|
*dot = '\0';
|
|
|
|
// Turn it into a safe identifier - this is used with e.g. dispatching
|
|
|
|
// input via: "send scriptname ..."
|
|
|
|
for (int n = 0; name[n]; n++) {
|
|
|
|
char c = name[n];
|
|
|
|
if (!(c >= 'A' && c <= 'Z') && !(c >= 'a' && c <= 'z') &&
|
|
|
|
!(c >= '0' && c <= '9'))
|
|
|
|
name[n] = '_';
|
|
|
|
}
|
|
|
|
return talloc_asprintf(talloc_ctx, "%s", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct thread_arg {
|
2014-09-06 15:02:47 +00:00
|
|
|
struct mp_log *log;
|
2014-05-12 23:14:07 +00:00
|
|
|
const struct mp_scripting *backend;
|
|
|
|
mpv_handle *client;
|
|
|
|
const char *fname;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void *script_thread(void *p)
|
|
|
|
{
|
|
|
|
pthread_detach(pthread_self());
|
|
|
|
|
|
|
|
struct thread_arg *arg = p;
|
|
|
|
|
2014-10-19 21:32:34 +00:00
|
|
|
char name[90];
|
2014-10-19 22:17:11 +00:00
|
|
|
snprintf(name, sizeof(name), "lua (%s)", mpv_client_name(arg->client));
|
2014-10-19 21:32:34 +00:00
|
|
|
mpthread_set_name(name);
|
|
|
|
|
2014-05-12 23:14:07 +00:00
|
|
|
if (arg->backend->load(arg->client, arg->fname) < 0)
|
2014-09-06 15:02:47 +00:00
|
|
|
MP_ERR(arg, "Could not load script %s\n", arg->fname);
|
2014-05-12 23:14:07 +00:00
|
|
|
|
2014-09-06 15:02:47 +00:00
|
|
|
MP_VERBOSE(arg, "Exiting...\n");
|
2014-05-12 23:14:07 +00:00
|
|
|
|
2014-06-07 18:25:48 +00:00
|
|
|
mpv_detach_destroy(arg->client);
|
2014-05-12 23:14:07 +00:00
|
|
|
talloc_free(arg);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-09-06 15:02:47 +00:00
|
|
|
static void wait_loaded(struct MPContext *mpctx)
|
|
|
|
{
|
2014-10-06 19:20:38 +00:00
|
|
|
while (!mp_clients_all_initialized(mpctx))
|
|
|
|
mp_idle(mpctx);
|
2016-09-16 12:24:15 +00:00
|
|
|
mp_wakeup_core(mpctx); // avoid lost wakeups during waiting
|
2014-09-06 15:02:47 +00:00
|
|
|
}
|
|
|
|
|
2016-09-22 18:43:33 +00:00
|
|
|
int mp_load_script(struct MPContext *mpctx, const char *fname)
|
2014-05-12 23:14:07 +00:00
|
|
|
{
|
|
|
|
char *ext = mp_splitext(fname, NULL);
|
|
|
|
const struct mp_scripting *backend = NULL;
|
|
|
|
for (int n = 0; scripting_backends[n]; n++) {
|
|
|
|
const struct mp_scripting *b = scripting_backends[n];
|
|
|
|
if (ext && strcasecmp(ext, b->file_ext) == 0) {
|
|
|
|
backend = b;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!backend) {
|
2014-06-03 22:33:17 +00:00
|
|
|
MP_VERBOSE(mpctx, "Can't load unknown script: %s\n", fname);
|
2016-09-22 18:43:33 +00:00
|
|
|
return -1;
|
2014-05-12 23:14:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct thread_arg *arg = talloc_ptrtype(NULL, arg);
|
|
|
|
char *name = script_name_from_filename(arg, fname);
|
|
|
|
*arg = (struct thread_arg){
|
|
|
|
.fname = talloc_strdup(arg, fname),
|
|
|
|
.backend = backend,
|
|
|
|
// Create the client before creating the thread; otherwise a race
|
|
|
|
// condition could happen, where MPContext is destroyed while the
|
|
|
|
// thread tries to create the client.
|
|
|
|
.client = mp_new_client(mpctx->clients, name),
|
|
|
|
};
|
|
|
|
if (!arg->client) {
|
|
|
|
talloc_free(arg);
|
2016-09-22 18:43:33 +00:00
|
|
|
return -1;
|
2014-05-12 23:14:07 +00:00
|
|
|
}
|
2014-09-06 15:02:47 +00:00
|
|
|
arg->log = mp_client_get_log(arg->client);
|
|
|
|
|
|
|
|
MP_VERBOSE(arg, "Loading script %s...\n", fname);
|
2014-05-12 23:14:07 +00:00
|
|
|
|
|
|
|
pthread_t thread;
|
2015-04-30 19:57:08 +00:00
|
|
|
if (pthread_create(&thread, NULL, script_thread, arg)) {
|
|
|
|
mpv_detach_destroy(arg->client);
|
2014-05-12 23:14:07 +00:00
|
|
|
talloc_free(arg);
|
2016-09-22 18:43:33 +00:00
|
|
|
return -1;
|
2015-04-30 19:57:08 +00:00
|
|
|
}
|
2014-05-12 23:14:07 +00:00
|
|
|
|
2014-09-06 15:02:47 +00:00
|
|
|
wait_loaded(mpctx);
|
|
|
|
MP_VERBOSE(mpctx, "Done loading %s.\n", fname);
|
|
|
|
|
2016-09-22 18:43:33 +00:00
|
|
|
return 0;
|
2014-05-12 23:14:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int compare_filename(const void *pa, const void *pb)
|
|
|
|
{
|
|
|
|
char *a = (char *)pa;
|
|
|
|
char *b = (char *)pb;
|
|
|
|
return strcmp(a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char **list_script_files(void *talloc_ctx, char *path)
|
|
|
|
{
|
|
|
|
char **files = NULL;
|
|
|
|
int count = 0;
|
|
|
|
DIR *dp = opendir(path);
|
|
|
|
if (!dp)
|
|
|
|
return NULL;
|
|
|
|
struct dirent *ep;
|
|
|
|
while ((ep = readdir(dp))) {
|
2015-05-09 13:26:47 +00:00
|
|
|
char *fname = mp_path_join(talloc_ctx, path, ep->d_name);
|
2014-05-12 23:14:07 +00:00
|
|
|
struct stat s;
|
2014-10-17 19:46:08 +00:00
|
|
|
if (!stat(fname, &s) && S_ISREG(s.st_mode))
|
2014-05-12 23:14:07 +00:00
|
|
|
MP_TARRAY_APPEND(talloc_ctx, files, count, fname);
|
|
|
|
}
|
|
|
|
closedir(dp);
|
2014-08-27 22:12:43 +00:00
|
|
|
if (files)
|
|
|
|
qsort(files, count, sizeof(char *), compare_filename);
|
2014-05-12 23:14:07 +00:00
|
|
|
MP_TARRAY_APPEND(talloc_ctx, files, count, NULL);
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
2016-09-19 22:59:54 +00:00
|
|
|
static void load_builtin_script(struct MPContext *mpctx, bool enable,
|
|
|
|
const char *fname)
|
|
|
|
{
|
|
|
|
void *tmp = talloc_new(NULL);
|
|
|
|
// (The name doesn't have to match if there were conflicts with other
|
|
|
|
// scripts, so this is on best-effort basis.)
|
|
|
|
char *name = script_name_from_filename(tmp, fname);
|
|
|
|
if (enable != mp_client_exists(mpctx, name)) {
|
|
|
|
if (enable) {
|
|
|
|
mp_load_script(mpctx, fname);
|
|
|
|
} else {
|
|
|
|
// Try to unload it by sending a shutdown event. Wait until it has
|
|
|
|
// terminated, or re-enabling the script could be racy (because it'd
|
|
|
|
// recognize a still-terminating script as "loaded").
|
|
|
|
while (mp_client_exists(mpctx, name)) {
|
|
|
|
if (mp_client_send_event(mpctx, name, MPV_EVENT_SHUTDOWN, NULL) < 0)
|
|
|
|
break;
|
|
|
|
mp_idle(mpctx);
|
|
|
|
}
|
|
|
|
mp_wakeup_core(mpctx); // avoid lost wakeups during waiting
|
|
|
|
}
|
|
|
|
}
|
|
|
|
talloc_free(tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mp_load_builtin_scripts(struct MPContext *mpctx)
|
|
|
|
{
|
|
|
|
load_builtin_script(mpctx, mpctx->opts->lua_load_osc, "@osc.lua");
|
|
|
|
load_builtin_script(mpctx, mpctx->opts->lua_load_ytdl, "@ytdl_hook.lua");
|
|
|
|
}
|
|
|
|
|
2014-05-12 23:14:07 +00:00
|
|
|
void mp_load_scripts(struct MPContext *mpctx)
|
|
|
|
{
|
|
|
|
// Load scripts from options
|
2016-09-19 22:59:54 +00:00
|
|
|
mp_load_builtin_scripts(mpctx);
|
2014-12-14 23:31:30 +00:00
|
|
|
char **files = mpctx->opts->script_files;
|
2014-05-12 23:14:07 +00:00
|
|
|
for (int n = 0; files && files[n]; n++) {
|
|
|
|
if (files[n][0])
|
|
|
|
mp_load_script(mpctx, files[n]);
|
|
|
|
}
|
|
|
|
if (!mpctx->opts->auto_load_scripts)
|
|
|
|
return;
|
2014-06-18 23:55:40 +00:00
|
|
|
|
2014-12-14 23:31:30 +00:00
|
|
|
// Load all scripts
|
2014-05-12 23:14:07 +00:00
|
|
|
void *tmp = talloc_new(NULL);
|
2015-05-09 13:04:05 +00:00
|
|
|
char **scriptsdir = mp_find_all_config_files(tmp, mpctx->global, "scripts");
|
|
|
|
for (int i = 0; scriptsdir && scriptsdir[i]; i++) {
|
|
|
|
files = list_script_files(tmp, scriptsdir[i]);
|
|
|
|
for (int n = 0; files && files[n]; n++)
|
|
|
|
mp_load_script(mpctx, files[n]);
|
2014-05-12 23:14:07 +00:00
|
|
|
}
|
|
|
|
talloc_free(tmp);
|
|
|
|
}
|