2 * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "pbd/error.h"
23 #include "pbd/file_utils.h"
24 #include "pbd/compose.h"
26 #include "ardour/directory_names.h"
27 #include "ardour/filesystem_paths.h"
28 #include "ardour/luascripting.h"
29 #include "ardour/lua_script_params.h"
30 #include "ardour/search_paths.h"
32 #include "lua/luastate.h"
33 #include "LuaBridge/LuaBridge.h"
37 using namespace ARDOUR;
41 LuaScripting* LuaScripting::_instance = 0;
44 LuaScripting::instance ()
47 _instance = new LuaScripting;
52 LuaScripting::LuaScripting ()
62 LuaScripting::~LuaScripting ()
64 if (getenv ("ARDOUR_RUNNING_UNDER_VALGRIND")) {
65 // don't bother, just exit quickly.
76 LuaScripting::refresh (bool run_scan)
78 Glib::Threads::Mutex::Lock lm (_lock);
99 bool operator () (LuaScriptInfoPtr a, LuaScriptInfoPtr b) {
100 return a->name < b->name;
105 LuaScripting::script_info (const std::string &script) {
106 return scan_script ("", script);
110 LuaScripting::scan ()
112 Glib::Threads::Mutex::Lock lm (_lock);
114 #define CLEAR_OR_NEW(LIST) \
115 if (LIST) { LIST->clear (); } else { LIST = new LuaScriptList (); }
117 CLEAR_OR_NEW (_sl_dsp)
118 CLEAR_OR_NEW (_sl_session)
119 CLEAR_OR_NEW (_sl_hook)
120 CLEAR_OR_NEW (_sl_action)
121 CLEAR_OR_NEW (_sl_snippet)
125 vector<string> luascripts;
126 find_files_matching_pattern (luascripts, lua_search_path (), "*.lua");
128 for (vector<string>::iterator i = luascripts.begin(); i != luascripts.end (); ++i) {
129 LuaScriptInfoPtr lsi = scan_script (*i);
131 PBD::info << string_compose (_("Script '%1' has no valid descriptor."), *i) << endmsg;
135 case LuaScriptInfo::DSP:
136 _sl_dsp->push_back(lsi);
138 case LuaScriptInfo::Session:
139 _sl_session->push_back(lsi);
141 case LuaScriptInfo::EditorHook:
142 _sl_hook->push_back(lsi);
144 case LuaScriptInfo::EditorAction:
145 _sl_action->push_back(lsi);
147 case LuaScriptInfo::Snippet:
148 _sl_snippet->push_back(lsi);
155 std::sort (_sl_dsp->begin(), _sl_dsp->end(), ScriptSorter());
156 std::sort (_sl_session->begin(), _sl_session->end(), ScriptSorter());
157 std::sort (_sl_hook->begin(), _sl_hook->end(), ScriptSorter());
158 std::sort (_sl_action->begin(), _sl_action->end(), ScriptSorter());
159 std::sort (_sl_snippet->begin(), _sl_snippet->end(), ScriptSorter());
161 scripts_changed (); /* EMIT SIGNAL */
165 LuaScripting::lua_print (std::string s) {
166 PBD::info << "Lua: " << s << "\n";
170 LuaScripting::scan_script (const std::string &fn, const std::string &sc)
173 if (!(fn.empty() ^ sc.empty())){
174 // give either file OR script
176 return LuaScriptInfoPtr();
179 lua_State* L = lua.getState();
180 lua.Print.connect (&LuaScripting::lua_print);
182 lua.do_command ("io = nil;");
186 "function ardour (entry)"
187 " ardourluainfo['type'] = assert(entry['type'])"
188 " ardourluainfo['name'] = assert(entry['name'])"
189 " ardourluainfo['author'] = entry['author'] or 'Unknown'"
190 " ardourluainfo['license'] = entry['license'] or ''"
191 " ardourluainfo['description'] = entry['description'] or ''"
198 err = lua.do_command (sc);
200 err = lua.do_file (fn);
204 cerr << "failed to load lua script\n";
206 return LuaScriptInfoPtr();
208 } catch (...) { // luabridge::LuaException
210 cerr << "failed to parse lua script\n";
212 return LuaScriptInfoPtr();
214 luabridge::LuaRef nfo = luabridge::getGlobal (L, "ardourluainfo");
215 if (nfo.type() != LUA_TTABLE) {
217 cerr << "failed to get ardour{} table from script\n";
219 return LuaScriptInfoPtr();
222 if (nfo["name"].type() != LUA_TSTRING || nfo["type"].type() != LUA_TSTRING) {
224 cerr << "script-type or script-name is not a string\n";
226 return LuaScriptInfoPtr();
229 std::string name = nfo["name"].cast<std::string>();
230 LuaScriptInfo::ScriptType type = LuaScriptInfo::str2type (nfo["type"].cast<std::string>());
232 if (name.empty() || type == LuaScriptInfo::Invalid) {
234 cerr << "invalid script-type or missing script name\n";
236 return LuaScriptInfoPtr();
239 LuaScriptInfoPtr lsi (new LuaScriptInfo (type, name, fn));
241 for (luabridge::Iterator i(nfo); !i.isNil (); ++i) {
242 if (!i.key().isString() || !i.value().isString()) {
243 return LuaScriptInfoPtr();
245 std::string key = i.key().tostring();
246 std::string val = i.value().tostring();
248 if (key == "author") { lsi->author = val; }
249 if (key == "license") { lsi->license = val; }
250 if (key == "description") { lsi->description = val; }
257 LuaScripting::scripts (LuaScriptInfo::ScriptType type) {
259 if (!_sl_dsp || !_sl_session || !_sl_hook || !_sl_action || !_sl_snippet) {
264 case LuaScriptInfo::DSP:
267 case LuaScriptInfo::Session:
270 case LuaScriptInfo::EditorHook:
273 case LuaScriptInfo::EditorAction:
276 case LuaScriptInfo::Snippet:
282 return _empty_script_info; // make some compilers happy
287 LuaScriptInfo::type2str (const ScriptType t) {
289 case LuaScriptInfo::DSP: return "DSP";
290 case LuaScriptInfo::Session: return "Session";
291 case LuaScriptInfo::EditorHook: return "EditorHook";
292 case LuaScriptInfo::EditorAction: return "EditorAction";
293 case LuaScriptInfo::Snippet: return "Snippet";
294 default: return "Invalid";
298 LuaScriptInfo::ScriptType
299 LuaScriptInfo::str2type (const std::string& str) {
300 const char* type = str.c_str();
301 if (!strcasecmp (type, "DSP")) {return LuaScriptInfo::DSP;}
302 if (!strcasecmp (type, "Session")) {return LuaScriptInfo::Session;}
303 if (!strcasecmp (type, "EditorHook")) {return LuaScriptInfo::EditorHook;}
304 if (!strcasecmp (type, "EditorAction")) {return LuaScriptInfo::EditorAction;}
305 if (!strcasecmp (type, "Snippet")) {return LuaScriptInfo::Snippet;}
306 return LuaScriptInfo::Invalid;
310 LuaScriptParams::script_params (LuaScriptInfoPtr lsi, const std::string &pname)
313 return LuaScriptParams::script_params (lsi->path, pname);
317 LuaScriptParams::script_params (const std::string& s, const std::string &pname, bool file)
319 LuaScriptParamList rv;
322 lua_State* L = lua.getState();
323 lua.do_command ("io = nil;");
324 lua.do_command ("function ardour () end");
332 } catch (luabridge::LuaException const& e) {
336 luabridge::LuaRef lua_params = luabridge::getGlobal (L, pname.c_str());
337 if (lua_params.isFunction ()) {
338 luabridge::LuaRef params = lua_params ();
339 if (params.isTable ()) {
340 for (luabridge::Iterator i (params); !i.isNil (); ++i) {
341 if (!i.key ().isString ()) { continue; }
342 if (!i.value ().isTable ()) { continue; }
343 if (!i.value ()["title"].isString ()) { continue; }
345 std::string name = i.key ().cast<std::string> ();
346 std::string title = i.value ()["title"].cast<std::string> ();
348 bool optional = false;
350 if (i.value ()["default"].isString ()) {
351 dflt = i.value ()["default"].cast<std::string> ();
353 if (i.value ()["optional"].isBoolean ()) {
354 optional = i.value ()["optional"].cast<bool> ();
356 LuaScriptParamPtr lsspp (new LuaScriptParam(name, title, dflt, optional));
357 rv.push_back (lsspp);
365 LuaScriptParams::params_to_ref (luabridge::LuaRef *tbl_args, const LuaScriptParamList& args)
367 assert (tbl_args && (*tbl_args).isTable ());
368 for (LuaScriptParamList::const_iterator i = args.begin(); i != args.end(); ++i) {
369 if ((*i)->optional && !(*i)->is_set) { continue; }
370 (*tbl_args)[(*i)->name] = (*i)->value;
375 LuaScriptParams::ref_to_params (LuaScriptParamList& args, luabridge::LuaRef *tbl_ref)
377 assert (tbl_ref && (*tbl_ref).isTable ());
378 for (luabridge::Iterator i (*tbl_ref); !i.isNil (); ++i) {
379 if (!i.key ().isString ()) { assert(0); continue; }
380 std::string name = i.key ().cast<std::string> ();
381 std::string value = i.value ().cast<std::string> ();
382 for (LuaScriptParamList::const_iterator ii = args.begin(); ii != args.end(); ++ii) {
383 if ((*ii)->name == name) {
384 (*ii)->value = value;
392 LuaScripting::try_compile (const std::string& script, const LuaScriptParamList& args)
394 const std::string& bytecode = get_factory_bytecode (script);
395 if (bytecode.empty()) {
399 l.Print.connect (&LuaScripting::lua_print);
400 lua_State* L = l.getState();
403 " function checkfactory (b, a)"
404 " assert(type(b) == 'string', 'ByteCode must be string')"
405 " load(b)()" // assigns f
406 " assert(type(f) == 'string', 'Assigned ByteCode must be string')"
407 " local factory = load(f)"
408 " assert(type(factory) == 'function', 'Factory is a not a function')"
409 " local env = _ENV; env.f = nil env.debug = nil os.exit = nil"
410 " load (string.dump(factory, true), nil, nil, env)(a)"
415 luabridge::LuaRef lua_test = luabridge::getGlobal (L, "checkfactory");
416 l.do_command ("checkfactory = nil"); // hide it.
417 l.do_command ("collectgarbage()");
419 luabridge::LuaRef tbl_arg (luabridge::newTable(L));
420 LuaScriptParams::params_to_ref (&tbl_arg, args);
421 lua_test (bytecode, tbl_arg);
423 } catch (luabridge::LuaException const& e) {
425 cerr << e.what() << "\n";
427 lua_print (e.what());
434 LuaScripting::get_factory_bytecode (const std::string& script)
437 l.Print.connect (&LuaScripting::lua_print);
438 lua_State* L = l.getState();
441 " function ardour () end"
443 " function dump_function (f)"
444 " assert(type(f) == 'function', 'Factory is a not a function')"
445 " return string.format(\"f = %q\", string.dump(f, true))"
450 luabridge::LuaRef lua_dump = luabridge::getGlobal (L, "dump_function");
451 l.do_command ("dump_function = nil"); // hide it
452 l.do_command (script); // register "factory"
453 luabridge::LuaRef lua_factory = luabridge::getGlobal (L, "factory");
455 if (lua_factory.isFunction()) {
456 return (lua_dump(lua_factory)).cast<std::string> ();
458 } catch (luabridge::LuaException const& e) { }
463 LuaScripting::user_script_dir ()
465 std::string dir = Glib::build_filename (user_config_directory(), lua_dir_name);
466 g_mkdir_with_parents (dir.c_str(), 0744);