Lua scripting: add convenience function Editor::trigger_script_by_name().
authorBen Loftis <ben@harrisonconsoles.com>
Thu, 7 Dec 2017 17:05:57 +0000 (11:05 -0600)
committerBen Loftis <ben@harrisonconsoles.com>
Thu, 7 Dec 2017 17:08:26 +0000 (11:08 -0600)
gtk2_ardour/editor.h
gtk2_ardour/editor_actions.cc

index 1331eebfc060067b9255332e8dc61352b9f93369..98b96b65b7f26d2d1f7224d573bef191213f84c7 100644 (file)
@@ -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);
 
index 33fd27f3238ae701f588ec5d9e301f6cf3c063c2..864d4aecd03eb0c923b8fcb7fa11a13a3492cd85 100644 (file)
 #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 ()
 {