From dade19310d5acd83d56ea4c3b720216a49c21e1d Mon Sep 17 00:00:00 2001 From: Ben Loftis Date: Thu, 7 Dec 2017 11:05:57 -0600 Subject: Lua scripting: add convenience function Editor::trigger_script_by_name(). --- gtk2_ardour/editor.h | 1 + gtk2_ardour/editor_actions.cc | 58 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/gtk2_ardour/editor.h b/gtk2_ardour/editor.h index 1331eebfc0..98b96b65b7 100644 --- a/gtk2_ardour/editor.h +++ b/gtk2_ardour/editor.h @@ -658,6 +658,7 @@ private: gint really_remove_marker (ARDOUR::Location* loc); void goto_nth_marker (int nth); void trigger_script (int nth); + void trigger_script_by_name ( const std::string script_name ); void toggle_marker_lines (); void set_marker_line_visibility (bool); 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 (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 () { -- cgit v1.2.3