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"
38 using namespace ARDOUR;
42 LuaScripting* LuaScripting::_instance = 0;
45 LuaScripting::instance ()
48 _instance = new LuaScripting;
53 LuaScripting::LuaScripting ()
65 LuaScripting::~LuaScripting ()
67 if (getenv ("ARDOUR_RUNNING_UNDER_VALGRIND")) {
68 // don't bother, just exit quickly.
81 LuaScripting::refresh (bool run_scan)
83 Glib::Threads::Mutex::Lock lm (_lock);
107 struct ScriptSorter {
108 bool operator () (LuaScriptInfoPtr a, LuaScriptInfoPtr b) {
109 return a->name < b->name;
114 LuaScripting::script_info (const std::string &script) {
115 return scan_script ("", script);
119 LuaScripting::scan ()
121 Glib::Threads::Mutex::Lock lm (_lock);
123 #define CLEAR_OR_NEW(LIST) \
124 if (LIST) { LIST->clear (); } else { LIST = new LuaScriptList (); }
126 CLEAR_OR_NEW (_sl_dsp)
127 CLEAR_OR_NEW (_sl_session)
128 CLEAR_OR_NEW (_sl_hook)
129 CLEAR_OR_NEW (_sl_action)
130 CLEAR_OR_NEW (_sl_snippet)
131 CLEAR_OR_NEW (_sl_setup)
132 CLEAR_OR_NEW (_sl_tracks)
136 vector<string> luascripts;
137 find_files_matching_pattern (luascripts, lua_search_path (), "*.lua");
139 for (vector<string>::iterator i = luascripts.begin(); i != luascripts.end (); ++i) {
140 LuaScriptInfoPtr lsi = scan_script (*i);
142 PBD::info << string_compose (_("Script '%1' has no valid descriptor."), *i) << endmsg;
146 case LuaScriptInfo::DSP:
147 _sl_dsp->push_back(lsi);
149 case LuaScriptInfo::Session:
150 _sl_session->push_back(lsi);
152 case LuaScriptInfo::EditorHook:
153 _sl_hook->push_back(lsi);
155 case LuaScriptInfo::EditorAction:
156 _sl_action->push_back(lsi);
158 case LuaScriptInfo::Snippet:
159 _sl_snippet->push_back(lsi);
161 case LuaScriptInfo::SessionInit:
162 _sl_setup->push_back(lsi);
169 std::sort (_sl_dsp->begin(), _sl_dsp->end(), ScriptSorter());
170 std::sort (_sl_session->begin(), _sl_session->end(), ScriptSorter());
171 std::sort (_sl_hook->begin(), _sl_hook->end(), ScriptSorter());
172 std::sort (_sl_action->begin(), _sl_action->end(), ScriptSorter());
173 std::sort (_sl_snippet->begin(), _sl_snippet->end(), ScriptSorter());
174 std::sort (_sl_setup->begin(), _sl_setup->end(), ScriptSorter());
175 std::sort (_sl_tracks->begin(), _sl_tracks->end(), ScriptSorter());
177 scripts_changed (); /* EMIT SIGNAL */
181 LuaScripting::lua_print (std::string s) {
182 PBD::info << "Lua: " << s << "\n";
186 LuaScripting::scan_script (const std::string &fn, const std::string &sc)
189 if (!(fn.empty() ^ sc.empty())){
190 // give either file OR script
192 return LuaScriptInfoPtr();
195 lua_State* L = lua.getState();
196 lua.Print.connect (&LuaScripting::lua_print);
201 "function ardour (entry)"
202 " ardourluainfo['type'] = assert(entry['type'])"
203 " ardourluainfo['name'] = assert(entry['name'])"
204 " ardourluainfo['category'] = entry['category'] or 'Unknown'"
205 " ardourluainfo['author'] = entry['author'] or 'Unknown'"
206 " ardourluainfo['license'] = entry['license'] or ''"
207 " ardourluainfo['description'] = entry['description'] or ''"
214 err = lua.do_command (sc);
216 err = lua.do_file (fn);
220 cerr << "failed to load lua script\n";
222 return LuaScriptInfoPtr();
224 } catch (...) { // luabridge::LuaException
226 cerr << "failed to parse lua script\n";
228 return LuaScriptInfoPtr();
230 luabridge::LuaRef nfo = luabridge::getGlobal (L, "ardourluainfo");
231 if (nfo.type() != LUA_TTABLE) {
233 cerr << "failed to get ardour{} table from script\n";
235 return LuaScriptInfoPtr();
238 if (nfo["name"].type() != LUA_TSTRING || nfo["type"].type() != LUA_TSTRING) {
240 cerr << "script-type or script-name is not a string\n";
242 return LuaScriptInfoPtr();
245 std::string name = nfo["name"].cast<std::string>();
246 LuaScriptInfo::ScriptType type = LuaScriptInfo::str2type (nfo["type"].cast<std::string>());
248 if (name.empty() || type == LuaScriptInfo::Invalid) {
250 cerr << "invalid script-type or missing script name\n";
252 return LuaScriptInfoPtr();
260 sha1_write (&s, (const uint8_t *) sc.c_str(), sc.size ());
263 std::string script = Glib::file_get_contents (fn);
264 sha1_write (&s, (const uint8_t *) script.c_str(), script.size ());
265 } catch (Glib::FileError err) {
266 return LuaScriptInfoPtr();
269 sha1_result_hash (&s, hash);
272 LuaScriptInfoPtr lsi (new LuaScriptInfo (type, name, fn, hash));
274 for (luabridge::Iterator i(nfo); !i.isNil (); ++i) {
275 if (!i.key().isString() || !i.value().isString()) {
276 return LuaScriptInfoPtr();
278 std::string key = i.key().tostring();
279 std::string val = i.value().tostring();
281 if (key == "author") { lsi->author = val; }
282 if (key == "license") { lsi->license = val; }
283 if (key == "description") { lsi->description = val; }
284 if (key == "category") { lsi->category = val; }
288 if (type == LuaScriptInfo::EditorAction) {
290 luabridge::LuaRef lua_rs = luabridge::getGlobal (L, "route_setup");
291 if (lua_rs.isFunction ()) {
292 lsi->subtype |= LuaScriptInfo::RouteSetup;
295 luabridge::LuaRef lua_ss = luabridge::getGlobal (L, "session_setup");
296 if (lua_ss.isFunction ()) {
298 if (lua_ss () == true) {
299 lsi->subtype |= LuaScriptInfo::SessionSetup;
310 LuaScripting::scripts (LuaScriptInfo::ScriptType type) {
312 if (!_sl_dsp || !_sl_session || !_sl_hook || !_sl_action || !_sl_snippet || ! _sl_setup || ! _sl_tracks) {
317 case LuaScriptInfo::DSP:
320 case LuaScriptInfo::Session:
323 case LuaScriptInfo::EditorHook:
326 case LuaScriptInfo::EditorAction:
329 case LuaScriptInfo::Snippet:
332 case LuaScriptInfo::SessionInit:
338 return _empty_script_info; // make some compilers happy
343 LuaScriptInfo::type2str (const ScriptType t) {
345 case LuaScriptInfo::DSP: return "DSP";
346 case LuaScriptInfo::Session: return "Session";
347 case LuaScriptInfo::EditorHook: return "EditorHook";
348 case LuaScriptInfo::EditorAction: return "EditorAction";
349 case LuaScriptInfo::Snippet: return "Snippet";
350 case LuaScriptInfo::SessionInit: return "SessionInit";
351 default: return "Invalid";
355 LuaScriptInfo::ScriptType
356 LuaScriptInfo::str2type (const std::string& str) {
357 const char* type = str.c_str();
358 if (!strcasecmp (type, "DSP")) {return LuaScriptInfo::DSP;}
359 if (!strcasecmp (type, "Session")) {return LuaScriptInfo::Session;}
360 if (!strcasecmp (type, "EditorHook")) {return LuaScriptInfo::EditorHook;}
361 if (!strcasecmp (type, "EditorAction")) {return LuaScriptInfo::EditorAction;}
362 if (!strcasecmp (type, "Snippet")) {return LuaScriptInfo::Snippet;}
363 if (!strcasecmp (type, "SessionInit")) {return LuaScriptInfo::SessionInit;}
364 return LuaScriptInfo::Invalid;
368 LuaScriptParams::script_params (const LuaScriptInfoPtr& lsi, const std::string &pname)
371 return LuaScriptParams::script_params (lsi->path, pname);
375 LuaScriptParams::script_params (const std::string& s, const std::string &pname, bool file)
378 return LuaScriptParams::script_params (lua, s, pname, file);
382 LuaScriptParams::script_params (LuaState& lua, const std::string& s, const std::string &pname, bool file)
384 LuaScriptParamList rv;
386 lua_State* L = lua.getState();
388 lua.do_command ("function ardour () end");
400 luabridge::LuaRef lua_params = luabridge::getGlobal (L, pname.c_str());
401 if (lua_params.isFunction ()) {
402 luabridge::LuaRef params = lua_params ();
403 if (params.isTable ()) {
404 for (luabridge::Iterator i (params); !i.isNil (); ++i) {
405 if (!i.key ().isString ()) { continue; }
406 if (!i.value ().isTable ()) { continue; }
407 if (!i.value ()["title"].isString ()) { continue; }
409 std::string name = i.key ().cast<std::string> ();
410 std::string title = i.value ()["title"].cast<std::string> ();
412 bool optional = false;
413 bool preseeded = false;
415 if (i.value ()["default"].isString ()) {
416 dflt = i.value ()["default"].cast<std::string> ();
418 if (i.value ()["optional"].isBoolean ()) {
419 optional = i.value ()["optional"].cast<bool> ();
421 if (i.value ()["preseeded"].isBoolean ()) {
422 preseeded = i.value ()["preseeded"].cast<bool> ();
424 LuaScriptParamPtr lsspp (new LuaScriptParam(name, title, dflt, optional, preseeded));
425 rv.push_back (lsspp);
433 LuaScriptParams::params_to_ref (luabridge::LuaRef *tbl_args, const LuaScriptParamList& args)
435 assert (tbl_args && (*tbl_args).isTable ());
436 for (LuaScriptParamList::const_iterator i = args.begin(); i != args.end(); ++i) {
437 if ((*i)->optional && !(*i)->is_set) { continue; }
438 (*tbl_args)[(*i)->name] = (*i)->value;
443 LuaScriptParams::ref_to_params (LuaScriptParamList& args, luabridge::LuaRef *tbl_ref)
445 assert (tbl_ref && (*tbl_ref).isTable ());
446 for (luabridge::Iterator i (*tbl_ref); !i.isNil (); ++i) {
447 if (!i.key ().isString ()) { assert(0); continue; }
448 std::string name = i.key ().cast<std::string> ();
449 std::string value = i.value ().cast<std::string> ();
450 for (LuaScriptParamList::const_iterator ii = args.begin(); ii != args.end(); ++ii) {
451 if ((*ii)->name == name) {
452 (*ii)->value = value;
460 LuaScripting::try_compile (const std::string& script, const LuaScriptParamList& args)
462 const std::string& bytecode = get_factory_bytecode (script);
463 if (bytecode.empty()) {
467 l.Print.connect (&LuaScripting::lua_print);
469 lua_State* L = l.getState();
472 " function checkfactory (b, a)"
473 " assert(type(b) == 'string', 'ByteCode must be string')"
474 " load(b)()" // assigns f
475 " assert(type(f) == 'string', 'Assigned ByteCode must be string')"
476 " local factory = load(f)"
477 " assert(type(factory) == 'function', 'Factory is a not a function')"
478 " local env = _ENV; env.f = nil env.os = nil env.io = nil"
479 " load (string.dump(factory, true), nil, nil, env)(a)"
484 luabridge::LuaRef lua_test = luabridge::getGlobal (L, "checkfactory");
485 l.do_command ("checkfactory = nil"); // hide it.
486 l.do_command ("collectgarbage()");
488 luabridge::LuaRef tbl_arg (luabridge::newTable(L));
489 LuaScriptParams::params_to_ref (&tbl_arg, args);
490 lua_test (bytecode, tbl_arg);
492 } catch (luabridge::LuaException const& e) {
494 cerr << e.what() << "\n";
496 lua_print (e.what());
503 LuaScripting::get_factory_bytecode (const std::string& script, const std::string& ffn, const std::string& fp)
506 l.Print.connect (&LuaScripting::lua_print);
508 lua_State* L = l.getState();
511 " function ardour () end"
513 " function dump_function (f)"
514 " assert(type(f) == 'function', 'Factory is a not a function')"
515 " return string.format(\"" + fp + " = %q\", string.dump(f, true))"
520 luabridge::LuaRef lua_dump = luabridge::getGlobal (L, "dump_function");
521 l.do_command ("dump_function = nil"); // hide it
522 l.do_command (script); // register "factory"
523 luabridge::LuaRef lua_factory = luabridge::getGlobal (L, ffn.c_str());
525 if (lua_factory.isFunction()) {
526 return (lua_dump(lua_factory)).cast<std::string> ();
533 LuaScripting::user_script_dir ()
535 std::string dir = Glib::build_filename (user_config_directory(), lua_dir_name);
536 g_mkdir_with_parents (dir.c_str(), 0744);