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 ()
63 LuaScripting::~LuaScripting ()
65 if (getenv ("ARDOUR_RUNNING_UNDER_VALGRIND")) {
66 // don't bother, just exit quickly.
77 LuaScripting::refresh (bool run_scan)
79 Glib::Threads::Mutex::Lock lm (_lock);
100 bool operator () (LuaScriptInfoPtr a, LuaScriptInfoPtr b) {
101 return a->name < b->name;
106 LuaScripting::script_info (const std::string &script) {
107 return scan_script ("", script);
111 LuaScripting::scan ()
113 Glib::Threads::Mutex::Lock lm (_lock);
115 #define CLEAR_OR_NEW(LIST) \
116 if (LIST) { LIST->clear (); } else { LIST = new LuaScriptList (); }
118 CLEAR_OR_NEW (_sl_dsp)
119 CLEAR_OR_NEW (_sl_session)
120 CLEAR_OR_NEW (_sl_hook)
121 CLEAR_OR_NEW (_sl_action)
122 CLEAR_OR_NEW (_sl_snippet)
126 vector<string> luascripts;
127 find_files_matching_pattern (luascripts, lua_search_path (), "*.lua");
129 for (vector<string>::iterator i = luascripts.begin(); i != luascripts.end (); ++i) {
130 LuaScriptInfoPtr lsi = scan_script (*i);
132 PBD::info << string_compose (_("Script '%1' has no valid descriptor."), *i) << endmsg;
136 case LuaScriptInfo::DSP:
137 _sl_dsp->push_back(lsi);
139 case LuaScriptInfo::Session:
140 _sl_session->push_back(lsi);
142 case LuaScriptInfo::EditorHook:
143 _sl_hook->push_back(lsi);
145 case LuaScriptInfo::EditorAction:
146 _sl_action->push_back(lsi);
148 case LuaScriptInfo::Snippet:
149 _sl_snippet->push_back(lsi);
156 std::sort (_sl_dsp->begin(), _sl_dsp->end(), ScriptSorter());
157 std::sort (_sl_session->begin(), _sl_session->end(), ScriptSorter());
158 std::sort (_sl_hook->begin(), _sl_hook->end(), ScriptSorter());
159 std::sort (_sl_action->begin(), _sl_action->end(), ScriptSorter());
160 std::sort (_sl_snippet->begin(), _sl_snippet->end(), ScriptSorter());
162 scripts_changed (); /* EMIT SIGNAL */
166 LuaScripting::lua_print (std::string s) {
167 PBD::info << "Lua: " << s << "\n";
171 LuaScripting::scan_script (const std::string &fn, const std::string &sc)
174 if (!(fn.empty() ^ sc.empty())){
175 // give either file OR script
177 return LuaScriptInfoPtr();
180 lua_State* L = lua.getState();
181 lua.Print.connect (&LuaScripting::lua_print);
183 lua.do_command ("io = nil;");
187 "function ardour (entry)"
188 " ardourluainfo['type'] = assert(entry['type'])"
189 " ardourluainfo['name'] = assert(entry['name'])"
190 " ardourluainfo['category'] = entry['category'] or 'Unknown'"
191 " ardourluainfo['author'] = entry['author'] or 'Unknown'"
192 " ardourluainfo['license'] = entry['license'] or ''"
193 " ardourluainfo['description'] = entry['description'] or ''"
200 err = lua.do_command (sc);
202 err = lua.do_file (fn);
206 cerr << "failed to load lua script\n";
208 return LuaScriptInfoPtr();
210 } catch (...) { // luabridge::LuaException
212 cerr << "failed to parse lua script\n";
214 return LuaScriptInfoPtr();
216 luabridge::LuaRef nfo = luabridge::getGlobal (L, "ardourluainfo");
217 if (nfo.type() != LUA_TTABLE) {
219 cerr << "failed to get ardour{} table from script\n";
221 return LuaScriptInfoPtr();
224 if (nfo["name"].type() != LUA_TSTRING || nfo["type"].type() != LUA_TSTRING) {
226 cerr << "script-type or script-name is not a string\n";
228 return LuaScriptInfoPtr();
231 std::string name = nfo["name"].cast<std::string>();
232 LuaScriptInfo::ScriptType type = LuaScriptInfo::str2type (nfo["type"].cast<std::string>());
234 if (name.empty() || type == LuaScriptInfo::Invalid) {
236 cerr << "invalid script-type or missing script name\n";
238 return LuaScriptInfoPtr();
246 sha1_write (&s, (const uint8_t *) sc.c_str(), sc.size ());
249 std::string script = Glib::file_get_contents (fn);
250 sha1_write (&s, (const uint8_t *) script.c_str(), script.size ());
251 } catch (Glib::FileError err) {
252 return LuaScriptInfoPtr();
255 sha1_result_hash (&s, hash);
258 LuaScriptInfoPtr lsi (new LuaScriptInfo (type, name, fn, hash));
260 for (luabridge::Iterator i(nfo); !i.isNil (); ++i) {
261 if (!i.key().isString() || !i.value().isString()) {
262 return LuaScriptInfoPtr();
264 std::string key = i.key().tostring();
265 std::string val = i.value().tostring();
267 if (key == "author") { lsi->author = val; }
268 if (key == "license") { lsi->license = val; }
269 if (key == "description") { lsi->description = val; }
270 if (key == "category") { lsi->category = val; }
276 LuaScripting::scripts (LuaScriptInfo::ScriptType type) {
278 if (!_sl_dsp || !_sl_session || !_sl_hook || !_sl_action || !_sl_snippet) {
283 case LuaScriptInfo::DSP:
286 case LuaScriptInfo::Session:
289 case LuaScriptInfo::EditorHook:
292 case LuaScriptInfo::EditorAction:
295 case LuaScriptInfo::Snippet:
301 return _empty_script_info; // make some compilers happy
306 LuaScriptInfo::type2str (const ScriptType t) {
308 case LuaScriptInfo::DSP: return "DSP";
309 case LuaScriptInfo::Session: return "Session";
310 case LuaScriptInfo::EditorHook: return "EditorHook";
311 case LuaScriptInfo::EditorAction: return "EditorAction";
312 case LuaScriptInfo::Snippet: return "Snippet";
313 default: return "Invalid";
317 LuaScriptInfo::ScriptType
318 LuaScriptInfo::str2type (const std::string& str) {
319 const char* type = str.c_str();
320 if (!strcasecmp (type, "DSP")) {return LuaScriptInfo::DSP;}
321 if (!strcasecmp (type, "Session")) {return LuaScriptInfo::Session;}
322 if (!strcasecmp (type, "EditorHook")) {return LuaScriptInfo::EditorHook;}
323 if (!strcasecmp (type, "EditorAction")) {return LuaScriptInfo::EditorAction;}
324 if (!strcasecmp (type, "Snippet")) {return LuaScriptInfo::Snippet;}
325 return LuaScriptInfo::Invalid;
329 LuaScriptParams::script_params (LuaScriptInfoPtr lsi, const std::string &pname)
332 return LuaScriptParams::script_params (lsi->path, pname);
336 LuaScriptParams::script_params (const std::string& s, const std::string &pname, bool file)
338 LuaScriptParamList rv;
341 lua_State* L = lua.getState();
342 lua.do_command ("io = nil;");
343 lua.do_command ("function ardour () end");
351 } catch (luabridge::LuaException const& e) {
355 luabridge::LuaRef lua_params = luabridge::getGlobal (L, pname.c_str());
356 if (lua_params.isFunction ()) {
357 luabridge::LuaRef params = lua_params ();
358 if (params.isTable ()) {
359 for (luabridge::Iterator i (params); !i.isNil (); ++i) {
360 if (!i.key ().isString ()) { continue; }
361 if (!i.value ().isTable ()) { continue; }
362 if (!i.value ()["title"].isString ()) { continue; }
364 std::string name = i.key ().cast<std::string> ();
365 std::string title = i.value ()["title"].cast<std::string> ();
367 bool optional = false;
369 if (i.value ()["default"].isString ()) {
370 dflt = i.value ()["default"].cast<std::string> ();
372 if (i.value ()["optional"].isBoolean ()) {
373 optional = i.value ()["optional"].cast<bool> ();
375 LuaScriptParamPtr lsspp (new LuaScriptParam(name, title, dflt, optional));
376 rv.push_back (lsspp);
384 LuaScriptParams::params_to_ref (luabridge::LuaRef *tbl_args, const LuaScriptParamList& args)
386 assert (tbl_args && (*tbl_args).isTable ());
387 for (LuaScriptParamList::const_iterator i = args.begin(); i != args.end(); ++i) {
388 if ((*i)->optional && !(*i)->is_set) { continue; }
389 (*tbl_args)[(*i)->name] = (*i)->value;
394 LuaScriptParams::ref_to_params (LuaScriptParamList& args, luabridge::LuaRef *tbl_ref)
396 assert (tbl_ref && (*tbl_ref).isTable ());
397 for (luabridge::Iterator i (*tbl_ref); !i.isNil (); ++i) {
398 if (!i.key ().isString ()) { assert(0); continue; }
399 std::string name = i.key ().cast<std::string> ();
400 std::string value = i.value ().cast<std::string> ();
401 for (LuaScriptParamList::const_iterator ii = args.begin(); ii != args.end(); ++ii) {
402 if ((*ii)->name == name) {
403 (*ii)->value = value;
411 LuaScripting::try_compile (const std::string& script, const LuaScriptParamList& args)
413 const std::string& bytecode = get_factory_bytecode (script);
414 if (bytecode.empty()) {
418 l.Print.connect (&LuaScripting::lua_print);
419 lua_State* L = l.getState();
422 " function checkfactory (b, a)"
423 " assert(type(b) == 'string', 'ByteCode must be string')"
424 " load(b)()" // assigns f
425 " assert(type(f) == 'string', 'Assigned ByteCode must be string')"
426 " local factory = load(f)"
427 " assert(type(factory) == 'function', 'Factory is a not a function')"
428 " local env = _ENV; env.f = nil env.debug = nil os.exit = nil"
429 " load (string.dump(factory, true), nil, nil, env)(a)"
434 luabridge::LuaRef lua_test = luabridge::getGlobal (L, "checkfactory");
435 l.do_command ("checkfactory = nil"); // hide it.
436 l.do_command ("collectgarbage()");
438 luabridge::LuaRef tbl_arg (luabridge::newTable(L));
439 LuaScriptParams::params_to_ref (&tbl_arg, args);
440 lua_test (bytecode, tbl_arg);
442 } catch (luabridge::LuaException const& e) {
444 cerr << e.what() << "\n";
446 lua_print (e.what());
453 LuaScripting::get_factory_bytecode (const std::string& script)
456 l.Print.connect (&LuaScripting::lua_print);
457 lua_State* L = l.getState();
460 " function ardour () end"
462 " function dump_function (f)"
463 " assert(type(f) == 'function', 'Factory is a not a function')"
464 " return string.format(\"f = %q\", string.dump(f, true))"
469 luabridge::LuaRef lua_dump = luabridge::getGlobal (L, "dump_function");
470 l.do_command ("dump_function = nil"); // hide it
471 l.do_command (script); // register "factory"
472 luabridge::LuaRef lua_factory = luabridge::getGlobal (L, "factory");
474 if (lua_factory.isFunction()) {
475 return (lua_dump(lua_factory)).cast<std::string> ();
477 } catch (luabridge::LuaException const& e) { }
482 LuaScripting::user_script_dir ()
484 std::string dir = Glib::build_filename (user_config_directory(), lua_dir_name);
485 g_mkdir_with_parents (dir.c_str(), 0744);