diff options
author | Ben Loftis <ben@harrisonconsoles.com> | 2017-12-07 11:05:57 -0600 |
---|---|---|
committer | Ben Loftis <ben@harrisonconsoles.com> | 2017-12-07 11:08:26 -0600 |
commit | dade19310d5acd83d56ea4c3b720216a49c21e1d (patch) | |
tree | c6816b8598a123cdc18a55a06a5ddf3d7cb3af10 /gtk2_ardour/editor_actions.cc | |
parent | efc858dc8149f3b3456781451ef42dc69eee2d5e (diff) |
Lua scripting: add convenience function Editor::trigger_script_by_name().
Diffstat (limited to 'gtk2_ardour/editor_actions.cc')
-rw-r--r-- | gtk2_ardour/editor_actions.cc | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/gtk2_ardour/editor_actions.cc b/gtk2_ardour/editor_actions.cc index 33fd27f323..864d4aecd0 100644 --- a/gtk2_ardour/editor_actions.cc +++ b/gtk2_ardour/editor_actions.cc @@ -33,11 +33,14 @@ #include "canvas/canvas.h" #include "canvas/pixbuf.h" +#include "LuaBridge/LuaBridge.h" + #include "actions.h" #include "ardour_ui.h" #include "editing.h" #include "editor.h" #include "gui_thread.h" +#include "luainstance.h" #include "main_clock.h" #include "time_axis_view.h" #include "ui_config.h" @@ -746,6 +749,61 @@ Editor::register_actions () } +static void _lua_print (std::string s) { +#ifndef NDEBUG + std::cout << "LuaInstance: " << s << "\n"; +#endif + PBD::info << "LuaInstance: " << s << endmsg; +} + +void +Editor::trigger_script_by_name ( const std::string script_name ) +{ + string script_path; + ARDOUR::LuaScriptList scr = LuaScripting::instance ().scripts(LuaScriptInfo::EditorAction); + for (ARDOUR::LuaScriptList::const_iterator s = scr.begin(); s != scr.end(); ++s) { + + if ( (*s)->name == script_name ) { + script_path = (*s)->path; + + if (!Glib::file_test (script_path, Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR)) { + cerr << "Lua Script action: path to " << script_path << " does not appear to be valid" << endl; + return; + } + + LuaState lua; + lua.Print.connect (&_lua_print); //ToDo + lua.sandbox (true); + lua_State* L = lua.getState(); + LuaInstance::register_classes (L); + LuaBindings::set_session (L, _session); + luabridge::push <PublicEditor *> (L, &PublicEditor::instance()); + lua_setglobal (L, "Editor"); + lua.do_command ("function ardour () end"); + lua.do_file (script_path); + luabridge::LuaRef args (luabridge::newTable (L)); + + //ToDo: args? + // args["how_many"] = count; + + try { + luabridge::LuaRef fn = luabridge::getGlobal (L, "factory"); + if (fn.isFunction()) { + fn (args)(); + } + } catch (luabridge::LuaException const& e) { + cerr << "LuaException:" << e.what () << endl; + } catch (...) { + cerr << "Lua script failed: " << script_path << endl; + } + + continue; //script found; we're done + } + } + + cerr << "Lua script was not found: " << script_name << endl; +} + void Editor::load_bindings () { |