1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
/******************************************************************************
Copyright (C) 2017 by Hugh Bailey <jim@obsproject.com>
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#pragma once
/* ---------------------------- */
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4100)
#pragma warning(disable : 4189)
#pragma warning(disable : 4244)
#pragma warning(disable : 4267)
#endif
#define SWIG_TYPE_TABLE obslua
#include "swig/swigluarun.h"
#ifdef _MSC_VER
#pragma warning(pop)
#endif
/* ---------------------------- */
#include <util/threading.h>
#include <util/base.h>
#include <util/bmem.h>
#include "obs-scripting-internal.h"
#include "obs-scripting-callback.h"
#define do_log(level, format, ...) \
blog(level, "[Lua] " format, ##__VA_ARGS__)
#define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
#define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
#define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
/* ------------------------------------------------------------ */
struct obs_lua_script;
struct lua_obs_callback;
extern THREAD_LOCAL struct lua_obs_callback *current_lua_cb;
extern THREAD_LOCAL struct obs_lua_script *current_lua_script;
/* ------------------------------------------------------------ */
struct lua_obs_callback;
struct obs_lua_script {
obs_script_t base;
struct dstr dir;
struct dstr log_chunk;
pthread_mutex_t mutex;
lua_State *script;
struct script_callback *first_callback;
int update;
int get_properties;
int save;
int tick;
struct obs_lua_script *next_tick;
struct obs_lua_script **p_prev_next_tick;
bool defined_sources;
};
#define lock_callback() \
struct obs_lua_script *__last_script = current_lua_script; \
struct lua_obs_callback *__last_callback = current_lua_cb; \
current_lua_cb = cb; \
current_lua_script = (struct obs_lua_script *)cb->base.script; \
pthread_mutex_lock(¤t_lua_script->mutex);
#define unlock_callback() \
pthread_mutex_unlock(¤t_lua_script->mutex); \
current_lua_script = __last_script; \
current_lua_cb = __last_callback;
/* ------------------------------------------------ */
struct lua_obs_callback {
struct script_callback base;
lua_State *script;
int reg_idx;
};
static inline struct lua_obs_callback *add_lua_obs_callback_extra(
lua_State *script,
int stack_idx,
size_t extra_size)
{
struct obs_lua_script *data = current_lua_script;
struct lua_obs_callback *cb = add_script_callback(
&data->first_callback,
(obs_script_t *)data,
sizeof(*cb) + extra_size);
lua_pushvalue(script, stack_idx);
cb->reg_idx = luaL_ref(script, LUA_REGISTRYINDEX);
cb->script = script;
return cb;
}
static inline struct lua_obs_callback *add_lua_obs_callback(
lua_State *script, int stack_idx)
{
return add_lua_obs_callback_extra(script, stack_idx, 0);
}
static inline void *lua_obs_callback_extra_data(struct lua_obs_callback *cb)
{
return (void*)&cb[1];
}
static inline struct obs_lua_script *lua_obs_callback_script(
struct lua_obs_callback *cb)
{
return (struct obs_lua_script *)cb->base.script;
}
static inline struct lua_obs_callback *find_next_lua_obs_callback(
lua_State *script, struct lua_obs_callback *cb, int stack_idx)
{
struct obs_lua_script *data = current_lua_script;
cb = cb ? (struct lua_obs_callback *)cb->base.next
: (struct lua_obs_callback *)data->first_callback;
while (cb) {
lua_rawgeti(script, LUA_REGISTRYINDEX, cb->reg_idx);
bool match = lua_rawequal(script, -1, stack_idx);
lua_pop(script, 1);
if (match)
break;
cb = (struct lua_obs_callback *)cb->base.next;
}
return cb;
}
static inline struct lua_obs_callback *find_lua_obs_callback(
lua_State *script, int stack_idx)
{
return find_next_lua_obs_callback(script, NULL, stack_idx);
}
static inline void remove_lua_obs_callback(struct lua_obs_callback *cb)
{
remove_script_callback(&cb->base);
luaL_unref(cb->script, LUA_REGISTRYINDEX, cb->reg_idx);
}
static inline void just_free_lua_obs_callback(struct lua_obs_callback *cb)
{
just_free_script_callback(&cb->base);
}
static inline void free_lua_obs_callback(struct lua_obs_callback *cb)
{
free_script_callback(&cb->base);
}
/* ------------------------------------------------ */
static int is_ptr(lua_State *script, int idx)
{
return lua_isuserdata(script, idx) || lua_isnil(script, idx);
}
static int is_table(lua_State *script, int idx)
{
return lua_istable(script, idx);
}
static int is_function(lua_State *script, int idx)
{
return lua_isfunction(script, idx);
}
typedef int (*param_cb)(lua_State *script, int idx);
static inline bool verify_args1_(lua_State *script,
param_cb param1_check,
const char *func)
{
if (lua_gettop(script) != 1) {
warn("Wrong number of parameters for %s", func);
return false;
}
if (!param1_check(script, 1)) {
warn("Wrong parameter type for parameter %d of %s", 1, func);
return false;
}
return true;
}
#define verify_args1(script, param1_check) \
verify_args1_(script, param1_check, __FUNCTION__)
static inline bool call_func_(lua_State *script,
int reg_idx, int args, int rets,
const char *func, const char *display_name)
{
if (reg_idx == LUA_REFNIL)
return false;
struct obs_lua_script *data = current_lua_script;
lua_rawgeti(script, LUA_REGISTRYINDEX, reg_idx);
lua_insert(script, -1 - args);
if (lua_pcall(script, args, rets, 0) != 0) {
script_warn(&data->base, "Failed to call %s for %s: %s", func,
display_name,
lua_tostring(script, -1));
lua_pop(script, 1);
return false;
}
return true;
}
bool ls_get_libobs_obj_(lua_State * script,
const char *type,
int lua_idx,
void * libobs_out,
const char *id,
const char *func,
int line);
bool ls_push_libobs_obj_(lua_State * script,
const char *type,
void * libobs_in,
bool ownership,
const char *id,
const char *func,
int line);
extern void add_lua_source_functions(lua_State *script);
|