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.
19 #include <cairomm/context.h>
21 #include "gtkmm2ext/gui_thread.h"
23 #include "ardour/audioengine.h"
24 #include "ardour/diskstream.h"
25 #include "ardour/plugin_manager.h"
26 #include "ardour/route.h"
27 #include "ardour/session.h"
29 #include "LuaBridge/LuaBridge.h"
31 #include "ardour_ui.h"
32 #include "public_editor.h"
33 #include "region_selection.h"
34 #include "luainstance.h"
35 #include "luasignal.h"
37 #include "time_axis_view.h"
38 #include "selection.h"
39 #include "script_selector.h"
45 #define STATIC(name,c,p) else if (!strcmp(type, #name)) {return name;}
46 #define SESSION(name,c,p) else if (!strcmp(type, #name)) {return name;}
47 #define ENGINE(name,c,p) else if (!strcmp(type, #name)) {return name;}
50 str2luasignal (const std::string &str) {
51 const char* type = str.c_str();
53 # include "luasignal_syms.h"
55 PBD::fatal << string_compose (_("programming error: %1: %2"), "Impossible LuaSignal type", str) << endmsg;
56 abort(); /*NOTREACHED*/
63 #define STATIC(name,c,p) N_(#name),
64 #define SESSION(name,c,p) N_(#name),
65 #define ENGINE(name,c,p) N_(#name),
66 const char *luasignalstr[] = {
67 # include "luasignal_syms.h"
76 ////////////////////////////////////////////////////////////////////////////////
78 #define xstr(s) stringify(s)
79 #define stringify(s) #s
81 using namespace ARDOUR;
84 LuaInstance::register_hooks (lua_State* L)
87 #define ENGINE(name,c,p) .addConst (stringify(name), (LuaSignal::LuaSignal)LuaSignal::name)
88 #define STATIC(name,c,p) .addConst (stringify(name), (LuaSignal::LuaSignal)LuaSignal::name)
89 #define SESSION(name,c,p) .addConst (stringify(name), (LuaSignal::LuaSignal)LuaSignal::name)
90 luabridge::getGlobalNamespace (L)
91 .beginNamespace ("LuaSignal")
92 # include "luasignal_syms.h"
98 luabridge::getGlobalNamespace (L)
99 .beginNamespace ("LuaSignal")
100 .beginStdBitSet <LuaSignal::LAST_SIGNAL> ("Set")
106 LuaInstance::bind_cairo (lua_State* L)
108 /* std::vector<double> for set_dash()
109 * for Windows (DLL, .exe) this needs to be bound in the same memory context as "Cairo".
111 * The std::vector<> argument in set_dash() has a fixed address in ardour.exe, while
112 * the address of the one in libardour.dll is mapped when loading the .dll
114 * see LuaBindings::set_session() for a detailed explanation
116 luabridge::getGlobalNamespace (L)
117 .beginNamespace ("C")
118 .beginStdVector <double> ("DoubleVector")
122 luabridge::getGlobalNamespace (L)
123 .beginNamespace ("Cairo")
124 .beginClass <Cairo::Context> ("Context")
125 .addFunction ("save", &Cairo::Context::save)
126 .addFunction ("restore", &Cairo::Context::restore)
127 .addFunction ("set_operator", &Cairo::Context::set_operator)
128 //.addFunction ("set_source", &Cairo::Context::set_operator) // needs RefPtr
129 .addFunction ("set_source_rgb", &Cairo::Context::set_source_rgb)
130 .addFunction ("set_source_rgba", &Cairo::Context::set_source_rgba)
131 .addFunction ("set_line_width", &Cairo::Context::set_line_width)
132 .addFunction ("set_line_cap", &Cairo::Context::set_line_cap)
133 .addFunction ("set_line_join", &Cairo::Context::set_line_join)
134 .addFunction ("set_dash", (void (Cairo::Context::*)(const std::vector<double>&, double))&Cairo::Context::set_dash)
135 .addFunction ("unset_dash", &Cairo::Context::unset_dash)
136 .addFunction ("translate", &Cairo::Context::translate)
137 .addFunction ("scale", &Cairo::Context::scale)
138 .addFunction ("rotate", &Cairo::Context::rotate)
139 .addFunction ("begin_new_path", &Cairo::Context::begin_new_path)
140 .addFunction ("begin_new_sub_path", &Cairo::Context::begin_new_sub_path)
141 .addFunction ("move_to", &Cairo::Context::move_to)
142 .addFunction ("line_to", &Cairo::Context::line_to)
143 .addFunction ("curve_to", &Cairo::Context::curve_to)
144 .addFunction ("arc", &Cairo::Context::arc)
145 .addFunction ("arc_negative", &Cairo::Context::arc_negative)
146 .addFunction ("rel_move_to", &Cairo::Context::rel_move_to)
147 .addFunction ("rel_line_to", &Cairo::Context::rel_line_to)
148 .addFunction ("rel_curve_to", &Cairo::Context::rel_curve_to)
149 .addFunction ("rectangle", (void (Cairo::Context::*)(double, double, double, double))&Cairo::Context::rectangle)
150 .addFunction ("close_path", &Cairo::Context::close_path)
151 .addFunction ("paint", &Cairo::Context::paint)
152 .addFunction ("paint_with_alpha", &Cairo::Context::paint_with_alpha)
153 .addFunction ("stroke", &Cairo::Context::stroke)
154 .addFunction ("stroke_preserve", &Cairo::Context::stroke_preserve)
155 .addFunction ("fill", &Cairo::Context::fill)
156 .addFunction ("fill_preserve", &Cairo::Context::fill_preserve)
157 .addFunction ("reset_clip", &Cairo::Context::reset_clip)
158 .addFunction ("clip", &Cairo::Context::clip)
159 .addFunction ("clip_preserve", &Cairo::Context::clip_preserve)
160 .addFunction ("set_font_size", &Cairo::Context::set_font_size)
161 .addFunction ("show_text", &Cairo::Context::show_text)
164 // LineCap, LineJoin, Operator
165 .beginNamespace ("LineCap")
166 .addConst ("Butt", CAIRO_LINE_CAP_BUTT)
167 .addConst ("Round", CAIRO_LINE_CAP_ROUND)
168 .addConst ("Square", CAIRO_LINE_CAP_SQUARE)
171 .beginNamespace ("LineJoin")
172 .addConst ("Miter", CAIRO_LINE_JOIN_MITER)
173 .addConst ("Round", CAIRO_LINE_JOIN_ROUND)
174 .addConst ("Bevel", CAIRO_LINE_JOIN_BEVEL)
177 .beginNamespace ("Operator")
178 .addConst ("Clear", CAIRO_OPERATOR_CLEAR)
179 .addConst ("Source", CAIRO_OPERATOR_SOURCE)
180 .addConst ("Over", CAIRO_OPERATOR_OVER)
181 .addConst ("Add", CAIRO_OPERATOR_ADD)
186 /* Lua/cairo bindings operate on Cairo::Context, there is no Cairo::RefPtr wrapper [yet].
187 one can work around this as follows:
190 LuaInstance::register_classes (lua.getState());
192 "function render (ctx)"
193 " ctx:rectangle (0, 0, 100, 100)"
194 " ctx:set_source_rgba (0.1, 1.0, 0.1, 1.0)"
199 Cairo::RefPtr<Cairo::Context> context = get_window ()->create_cairo_context ();
200 Cairo::Context ctx (context->cobj ());
202 luabridge::LuaRef lua_render = luabridge::getGlobal (lua.getState(), "render");
203 lua_render ((Cairo::Context *)&ctx);
210 LuaInstance::register_classes (lua_State* L)
212 LuaBindings::stddef (L);
213 LuaBindings::common (L);
214 LuaBindings::session (L);
215 LuaBindings::osc (L);
220 luabridge::getGlobalNamespace (L)
221 .beginNamespace ("ArdourUI")
223 .beginStdList <ArdourMarker*> ("ArdourMarkerList")
226 .beginClass <ArdourMarker> ("ArdourMarker")
227 .addFunction ("name", &ArdourMarker::name)
228 .addFunction ("position", &ArdourMarker::position)
229 .addFunction ("_type", &ArdourMarker::type)
233 .beginClass <AxisView> ("AxisView")
235 .deriveClass <TimeAxisView, AxisView> ("TimeAxisView")
237 .deriveClass <RouteTimeAxisView, TimeAxisView> ("RouteTimeAxisView")
241 .beginClass <RegionSelection> ("RegionSelection")
242 .addFunction ("clear_all", &RegionSelection::clear_all)
243 .addFunction ("start", &RegionSelection::start)
244 .addFunction ("end_frame", &RegionSelection::end_frame)
245 .addFunction ("n_midi_regions", &RegionSelection::n_midi_regions)
246 .addFunction ("regionlist", &RegionSelection::regionlist) // XXX check windows binding (libardour)
249 .deriveClass <TimeSelection, std::list<ARDOUR::AudioRange> > ("TimeSelection")
250 .addFunction ("start", &TimeSelection::start)
251 .addFunction ("end_frame", &TimeSelection::end_frame)
252 .addFunction ("length", &TimeSelection::length)
255 .deriveClass <MarkerSelection, std::list<ArdourMarker*> > ("MarkerSelection")
258 .beginClass <TrackViewList> ("TrackViewList")
259 .addFunction ("routelist", &TrackViewList::routelist) // XXX check windows binding (libardour)
262 .deriveClass <TrackSelection, TrackViewList> ("TrackSelection")
265 .beginClass <Selection> ("Selection")
266 .addFunction ("clear", &Selection::clear)
267 .addFunction ("clear_all", &Selection::clear_all)
268 .addFunction ("empty", &Selection::empty)
269 .addData ("tracks", &Selection::tracks)
270 .addData ("regions", &Selection::regions)
271 .addData ("time", &Selection::time)
272 .addData ("markers", &Selection::markers)
274 .addData ("lines", &Selection::lines)
275 .addData ("playlists", &Selection::playlists)
276 .addData ("points", &Selection::points)
277 .addData ("midi_regions", &Selection::midi_regions)
278 .addData ("midi_notes", &Selection::midi_notes) // cut buffer only
282 .beginClass <PublicEditor> ("Editor")
283 .addFunction ("snap_type", &PublicEditor::snap_type)
284 .addFunction ("snap_mode", &PublicEditor::snap_mode)
285 .addFunction ("set_snap_mode", &PublicEditor::set_snap_mode)
286 .addFunction ("set_snap_threshold", &PublicEditor::set_snap_threshold)
288 .addFunction ("undo", &PublicEditor::undo)
289 .addFunction ("redo", &PublicEditor::redo)
291 .addFunction ("set_mouse_mode", &PublicEditor::set_mouse_mode)
292 .addFunction ("current_mouse_mode", &PublicEditor::current_mouse_mode)
294 .addFunction ("consider_auditioning", &PublicEditor::consider_auditioning)
296 .addFunction ("new_region_from_selection", &PublicEditor::new_region_from_selection)
297 .addFunction ("separate_region_from_selection", &PublicEditor::separate_region_from_selection)
298 .addFunction ("pixel_to_sample", &PublicEditor::pixel_to_sample)
299 .addFunction ("sample_to_pixel", &PublicEditor::sample_to_pixel)
301 .addFunction ("get_selection", &PublicEditor::get_selection)
302 .addFunction ("get_cut_buffer", &PublicEditor::get_cut_buffer)
303 .addRefFunction ("get_selection_extents", &PublicEditor::get_selection_extents)
305 .addFunction ("play_selection", &PublicEditor::play_selection)
306 .addFunction ("play_with_preroll", &PublicEditor::play_with_preroll)
307 .addFunction ("maybe_locate_with_edit_preroll", &PublicEditor::maybe_locate_with_edit_preroll)
308 .addFunction ("goto_nth_marker", &PublicEditor::goto_nth_marker)
310 .addFunction ("add_location_from_playhead_cursor", &PublicEditor::add_location_from_playhead_cursor)
311 .addFunction ("remove_location_at_playhead_cursor", &PublicEditor::remove_location_at_playhead_cursor)
313 .addFunction ("set_show_measures", &PublicEditor::set_show_measures)
314 .addFunction ("show_measures", &PublicEditor::show_measures)
315 .addFunction ("remove_tracks", &PublicEditor::remove_tracks)
317 .addFunction ("set_loop_range", &PublicEditor::set_loop_range)
318 .addFunction ("set_punch_range", &PublicEditor::set_punch_range)
320 .addFunction ("effective_mouse_mode", &PublicEditor::effective_mouse_mode)
322 .addRefFunction ("do_import", &PublicEditor::do_import)
323 .addRefFunction ("do_embed", &PublicEditor::do_embed)
325 .addFunction ("export_audio", &PublicEditor::export_audio)
326 .addFunction ("stem_export", &PublicEditor::stem_export)
327 .addFunction ("export_selection", &PublicEditor::export_selection)
328 .addFunction ("export_range", &PublicEditor::export_range)
330 .addFunction ("set_zoom_focus", &PublicEditor::set_zoom_focus)
331 .addFunction ("get_zoom_focus", &PublicEditor::get_zoom_focus)
332 .addFunction ("get_current_zoom", &PublicEditor::get_current_zoom)
333 .addFunction ("reset_zoom", &PublicEditor::reset_zoom)
335 #if 0 // These need TimeAxisView* which isn't exposed, yet
336 .addFunction ("playlist_selector", &PublicEditor::playlist_selector)
337 .addFunction ("clear_playlist", &PublicEditor::clear_playlist)
338 .addFunction ("new_playlists", &PublicEditor::new_playlists)
339 .addFunction ("copy_playlists", &PublicEditor::copy_playlists)
340 .addFunction ("clear_playlists", &PublicEditor::clear_playlists)
343 .addFunction ("select_all_tracks", &PublicEditor::select_all_tracks)
344 .addFunction ("deselect_all", &PublicEditor::deselect_all)
346 .addFunction ("set_selected_track", &PublicEditor::set_selected_track)
347 .addFunction ("set_selected_mixer_strip", &PublicEditor::set_selected_mixer_strip)
348 .addFunction ("hide_track_in_display", &PublicEditor::hide_track_in_display)
350 .addFunction ("set_stationary_playhead", &PublicEditor::set_stationary_playhead)
351 .addFunction ("stationary_playhead", &PublicEditor::stationary_playhead)
352 .addFunction ("set_follow_playhead", &PublicEditor::set_follow_playhead)
353 .addFunction ("follow_playhead", &PublicEditor::follow_playhead)
355 .addFunction ("dragging_playhead", &PublicEditor::dragging_playhead)
356 .addFunction ("leftmost_sample", &PublicEditor::leftmost_sample)
357 .addFunction ("current_page_samples", &PublicEditor::current_page_samples)
358 .addFunction ("visible_canvas_height", &PublicEditor::visible_canvas_height)
359 .addFunction ("temporal_zoom_step", &PublicEditor::temporal_zoom_step)
360 //.addFunction ("ensure_time_axis_view_is_visible", &PublicEditor::ensure_time_axis_view_is_visible)
361 .addFunction ("override_visible_track_count", &PublicEditor::override_visible_track_count)
363 .addFunction ("scroll_tracks_down_line", &PublicEditor::scroll_tracks_down_line)
364 .addFunction ("scroll_tracks_up_line", &PublicEditor::scroll_tracks_up_line)
365 .addFunction ("scroll_down_one_track", &PublicEditor::scroll_down_one_track)
366 .addFunction ("scroll_up_one_track", &PublicEditor::scroll_up_one_track)
368 .addFunction ("reset_x_origin", &PublicEditor::reset_x_origin)
369 .addFunction ("get_y_origin", &PublicEditor::get_y_origin)
370 .addFunction ("reset_y_origin", &PublicEditor::reset_y_origin)
372 .addFunction ("remove_last_capture", &PublicEditor::remove_last_capture)
374 .addFunction ("maximise_editing_space", &PublicEditor::maximise_editing_space)
375 .addFunction ("restore_editing_space", &PublicEditor::restore_editing_space)
376 .addFunction ("toggle_meter_updating", &PublicEditor::toggle_meter_updating)
378 //.addFunction ("get_preferred_edit_position", &PublicEditor::get_preferred_edit_position)
379 //.addFunction ("split_regions_at", &PublicEditor::split_regions_at)
381 .addRefFunction ("get_nudge_distance", &PublicEditor::get_nudge_distance)
382 .addFunction ("get_paste_offset", &PublicEditor::get_paste_offset)
383 .addFunction ("get_grid_beat_divisions", &PublicEditor::get_grid_beat_divisions)
384 .addRefFunction ("get_grid_type_as_beats", &PublicEditor::get_grid_type_as_beats)
386 .addFunction ("toggle_ruler_video", &PublicEditor::toggle_ruler_video)
387 .addFunction ("toggle_xjadeo_proc", &PublicEditor::toggle_xjadeo_proc)
388 .addFunction ("get_videotl_bar_height", &PublicEditor::get_videotl_bar_height)
389 .addFunction ("set_video_timeline_height", &PublicEditor::set_video_timeline_height)
392 .addFunction ("get_route_view_by_route_id", &PublicEditor::get_route_view_by_route_id)
393 .addFunction ("get_equivalent_regions", &PublicEditor::get_equivalent_regions)
395 .addFunction ("axis_view_from_route", &PublicEditor::axis_view_from_route)
396 .addFunction ("axis_views_from_routes", &PublicEditor::axis_views_from_routes)
397 .addFunction ("get_track_views", &PublicEditor::get_track_views)
398 .addFunction ("drags", &PublicEditor::drags)
401 .addFunction ("center_screen", &PublicEditor::center_screen)
403 .addFunction ("get_smart_mode", &PublicEditor::get_smart_mode)
404 .addRefFunction ("get_pointer_position", &PublicEditor::get_pointer_position)
406 .addRefFunction ("find_location_from_marker", &PublicEditor::find_location_from_marker)
407 .addFunction ("find_marker_from_location_id", &PublicEditor::find_marker_from_location_id)
408 .addFunction ("mouse_add_new_marker", &PublicEditor::mouse_add_new_marker)
410 .addFunction ("get_regions_at", &PublicEditor::get_regions_at)
411 .addFunction ("get_regions_after", &PublicEditor::get_regions_after)
412 .addFunction ("get_regions_from_selection_and_mouse", &PublicEditor::get_regions_from_selection_and_mouse)
413 .addFunction ("get_regionviews_by_id", &PublicEditor::get_regionviews_by_id)
414 .addFunction ("get_per_region_note_selection", &PublicEditor::get_per_region_note_selection)
418 .addFunction ("mouse_add_new_tempo_event", &PublicEditor::mouse_add_new_tempo_event)
419 .addFunction ("mouse_add_new_meter_event", &PublicEditor::mouse_add_new_meter_event)
420 .addFunction ("edit_tempo_section", &PublicEditor::edit_tempo_section)
421 .addFunction ("edit_meter_section", &PublicEditor::edit_meter_section)
424 .addFunction ("access_action", &PublicEditor::access_action)
428 .beginNamespace ("MarkerType")
429 .addConst ("Mark", ArdourMarker::Type(ArdourMarker::Mark))
430 .addConst ("Tempo", ArdourMarker::Type(ArdourMarker::Tempo))
431 .addConst ("Meter", ArdourMarker::Type(ArdourMarker::Meter))
432 .addConst ("SessionStart", ArdourMarker::Type(ArdourMarker::SessionStart))
433 .addConst ("SessionEnd", ArdourMarker::Type(ArdourMarker::SessionEnd))
434 .addConst ("RangeStart", ArdourMarker::Type(ArdourMarker::RangeStart))
435 .addConst ("RangeEnd", ArdourMarker::Type(ArdourMarker::RangeEnd))
436 .addConst ("LoopStart", ArdourMarker::Type(ArdourMarker::LoopStart))
437 .addConst ("LoopEnd", ArdourMarker::Type(ArdourMarker::LoopEnd))
438 .addConst ("PunchIn", ArdourMarker::Type(ArdourMarker::PunchIn))
439 .addConst ("PunchOut", ArdourMarker::Type(ArdourMarker::PunchOut))
442 .endNamespace (); // end ArdourUI
450 #undef DISPLAYCONTROL
452 #undef IMPORTPOSITION
453 #undef IMPORTDISPOSITION
455 #define ZOOMFOCUS(NAME) .addConst (stringify(NAME), (Editing::ZoomFocus)Editing::NAME)
456 #define SNAPTYPE(NAME) .addConst (stringify(NAME), (Editing::SnapType)Editing::NAME)
457 #define SNAPMODE(NAME) .addConst (stringify(NAME), (Editing::SnapMode)Editing::NAME)
458 #define MOUSEMODE(NAME) .addConst (stringify(NAME), (Editing::MouseMode)Editing::NAME)
459 #define DISPLAYCONTROL(NAME) .addConst (stringify(NAME), (Editing::DisplayControl)Editing::NAME)
460 #define IMPORTMODE(NAME) .addConst (stringify(NAME), (Editing::ImportMode)Editing::NAME)
461 #define IMPORTPOSITION(NAME) .addConst (stringify(NAME), (Editing::ImportPosition)Editing::NAME)
462 #define IMPORTDISPOSITION(NAME) .addConst (stringify(NAME), (Editing::ImportDisposition)Editing::NAME)
463 luabridge::getGlobalNamespace (L)
464 .beginNamespace ("Editing")
465 # include "editing_syms.h"
472 ////////////////////////////////////////////////////////////////////////////////
474 using namespace ARDOUR;
475 using namespace ARDOUR_UI_UTILS;
480 static void _lua_print (std::string s) {
481 std::cout << "LuaInstance: " << s << "\n";
485 LuaInstance* LuaInstance::_instance = 0;
488 LuaInstance::instance ()
491 _instance = new LuaInstance;
497 LuaInstance::LuaInstance ()
500 lua.Print.connect (&_lua_print);
504 LuaScriptParamList args;
507 LuaInstance::~LuaInstance ()
509 delete _lua_call_action;
510 delete _lua_add_action;
511 delete _lua_del_action;
512 delete _lua_get_action;
524 "function ScriptManager ()"
525 " local self = { scripts = {}, instances = {} }"
527 " local remove = function (id)"
528 " self.scripts[id] = nil"
529 " self.instances[id] = nil"
532 " local addinternal = function (i, n, s, f, a)"
533 " assert(type(i) == 'number', 'id must be numeric')"
534 " assert(type(n) == 'string', 'Name must be string')"
535 " assert(type(s) == 'string', 'Script must be string')"
536 " assert(type(f) == 'function', 'Factory is a not a function')"
537 " assert(type(a) == 'table' or type(a) == 'nil', 'Given argument is invalid')"
538 " self.scripts[i] = { ['n'] = n, ['s'] = s, ['f'] = f, ['a'] = a }"
539 " local env = _ENV; env.f = nil env.debug = nil os.exit = nil require = nil dofile = nil loadfile = nil package = nil"
540 " self.instances[i] = load (string.dump(f, true), nil, nil, env)(a)"
543 " local call = function (id)"
544 " if type(self.instances[id]) == 'function' then"
545 " local status, err = pcall (self.instances[id])"
546 " if not status then"
547 " print ('action \"'.. id .. '\": ', err)" // error out
554 " local add = function (i, n, s, b, a)"
555 " assert(type(b) == 'string', 'ByteCode must be string')"
556 " load (b)()" // assigns f
557 " assert(type(f) == 'string', 'Assigned ByteCode must be string')"
558 " addinternal (i, n, s, load(f), a)"
561 " local get = function (id)"
562 " if type(self.scripts[id]) == 'table' then"
563 " return { ['name'] = self.scripts[id]['n'],"
564 " ['script'] = self.scripts[id]['s'],"
565 " ['args'] = self.scripts[id]['a'] }"
570 " local function basic_serialize (o)"
571 " if type(o) == \"number\" then"
572 " return tostring(o)"
574 " return string.format(\"%q\", o)"
578 " local function serialize (name, value)"
579 " local rv = name .. ' = '"
581 " if type(value) == \"number\" or type(value) == \"string\" or type(value) == \"nil\" then"
582 " return rv .. basic_serialize(value) .. ' '"
583 " elseif type(value) == \"table\" then"
585 " for k,v in pairs(value) do"
586 " local fieldname = string.format(\"%s[%s]\", name, basic_serialize(k))"
587 " rv = rv .. serialize(fieldname, v) .. ' '"
588 " collectgarbage()" // string concatenation allocates a new string
591 " elseif type(value) == \"function\" then"
592 " return rv .. string.format(\"%q\", string.dump(value, true))"
594 " error('cannot save a ' .. type(value))"
599 " local save = function ()"
600 " return (serialize('scripts', self.scripts))"
603 " local clear = function ()"
605 " self.instances = {}"
609 " local restore = function (state)"
612 " for i, s in pairs (scripts) do"
613 " addinternal (i, s['n'], s['s'], load(s['f']), s['a'])"
618 " return { call = call, add = add, remove = remove, get = get,"
619 " restore = restore, save = save, clear = clear}"
622 " manager = ScriptManager ()"
623 " ScriptManager = nil"
626 lua_State* L = lua.getState();
629 luabridge::LuaRef lua_mgr = luabridge::getGlobal (L, "manager");
630 lua.do_command ("manager = nil"); // hide it.
631 lua.do_command ("collectgarbage()");
633 _lua_add_action = new luabridge::LuaRef(lua_mgr["add"]);
634 _lua_del_action = new luabridge::LuaRef(lua_mgr["remove"]);
635 _lua_get_action = new luabridge::LuaRef(lua_mgr["get"]);
636 _lua_call_action = new luabridge::LuaRef(lua_mgr["call"]);
637 _lua_save = new luabridge::LuaRef(lua_mgr["save"]);
638 _lua_load = new luabridge::LuaRef(lua_mgr["restore"]);
639 _lua_clear = new luabridge::LuaRef(lua_mgr["clear"]);
641 } catch (luabridge::LuaException const& e) {
642 fatal << string_compose (_("programming error: %1"),
643 X_("Failed to setup Lua action interpreter"))
645 abort(); /*NOTREACHED*/
648 register_classes (L);
650 luabridge::push <PublicEditor *> (L, &PublicEditor::instance());
651 lua_setglobal (L, "Editor");
654 void LuaInstance::set_session (Session* s)
656 SessionHandlePtr::set_session (s);
661 lua_State* L = lua.getState();
662 LuaBindings::set_session (L, _session);
664 for (LuaCallbackMap::iterator i = _callbacks.begin(); i != _callbacks.end(); ++i) {
665 i->second->set_session (s);
670 LuaInstance::session_going_away ()
672 ENSURE_GUI_THREAD (*this, &LuaInstance::session_going_away);
674 for (int i = 0; i < 9; ++i) {
675 ActionChanged (i, ""); /* EMIT SIGNAL */
677 SessionHandlePtr::session_going_away ();
680 lua_State* L = lua.getState();
681 LuaBindings::set_session (L, _session);
682 lua.do_command ("collectgarbage();");
686 LuaInstance::set_state (const XMLNode& node)
688 LocaleGuard lg (X_("C"));
691 if ((child = find_named_node (node, "ActionScript"))) {
692 for (XMLNodeList::const_iterator n = child->children ().begin (); n != child->children ().end (); ++n) {
693 if (!(*n)->is_content ()) { continue; }
695 guchar* buf = g_base64_decode ((*n)->content ().c_str (), &size);
697 (*_lua_load)(std::string ((const char*)buf, size));
698 } catch (luabridge::LuaException const& e) {
699 cerr << "LuaException:" << e.what () << endl;
701 for (int i = 0; i < 9; ++i) {
703 if (lua_action_name (i, name)) {
704 ActionChanged (i, name); /* EMIT SIGNAL */
711 if ((child = find_named_node (node, "ActionHooks"))) {
712 for (XMLNodeList::const_iterator n = child->children ().begin (); n != child->children ().end (); ++n) {
714 LuaCallbackPtr p (new LuaCallback (_session, *(*n)));
715 _callbacks.insert (std::make_pair(p->id(), p));
716 p->drop_callback.connect (_slotcon, MISSING_INVALIDATOR, boost::bind (&LuaInstance::unregister_lua_slot, this, p->id()), gui_context());
717 SlotChanged (p->id(), p->name(), p->signals()); /* EMIT SIGNAL */
718 } catch (luabridge::LuaException const& e) {
719 cerr << "LuaException:" << e.what () << endl;
728 LuaInstance::interactive_add (LuaScriptInfo::ScriptType type, int id)
731 std::vector<std::string> reg;
734 case LuaScriptInfo::EditorAction:
735 reg = lua_action_names ();
736 title = "Add Lua Action";
738 case LuaScriptInfo::EditorHook:
739 reg = lua_slot_names ();
740 title = "Add Lua Callback Hook";
746 LuaScriptInfoPtr spi;
747 ScriptSelector ss (title, type);
749 case Gtk::RESPONSE_ACCEPT:
757 std::string script = "";
760 script = Glib::file_get_contents (spi->path);
761 } catch (Glib::FileError e) {
762 string msg = string_compose (_("Cannot read script '%1': %2"), spi->path, e.what());
763 Gtk::MessageDialog am (msg);
768 LuaScriptParamList lsp = LuaScriptParams::script_params (spi, "action_params");
770 ScriptParameterDialog spd (_("Set Script Parameters"), spi, reg, lsp);
771 switch (spd.run ()) {
772 case Gtk::RESPONSE_ACCEPT:
779 case LuaScriptInfo::EditorAction:
780 return set_lua_action (id, spd.name(), script, lsp);
782 case LuaScriptInfo::EditorHook:
783 return register_lua_slot (spd.name(), script, lsp);
792 LuaInstance::get_action_state ()
794 LocaleGuard lg (X_("C"));
797 luabridge::LuaRef savedstate ((*_lua_save)());
798 saved = savedstate.cast<std::string>();
800 lua.collect_garbage ();
802 gchar* b64 = g_base64_encode ((const guchar*)saved.c_str (), saved.size ());
803 std::string b64s (b64);
806 XMLNode* script_node = new XMLNode (X_("ActionScript"));
807 script_node->add_property (X_("lua"), LUA_VERSION);
808 script_node->add_content (b64s);
814 LuaInstance::get_hook_state ()
816 XMLNode* script_node = new XMLNode (X_("ActionHooks"));
817 for (LuaCallbackMap::const_iterator i = _callbacks.begin(); i != _callbacks.end(); ++i) {
818 script_node->add_child_nocopy (i->second->get_state ());
824 LuaInstance::call_action (const int id)
827 (*_lua_call_action)(id + 1);
828 } catch (luabridge::LuaException const& e) {
829 cerr << "LuaException:" << e.what () << endl;
834 LuaInstance::set_lua_action (
836 const std::string& name,
837 const std::string& script,
838 const LuaScriptParamList& args)
841 lua_State* L = lua.getState();
842 // get bytcode of factory-function in a sandbox
843 // (don't allow scripts to interfere)
844 const std::string& bytecode = LuaScripting::get_factory_bytecode (script);
845 luabridge::LuaRef tbl_arg (luabridge::newTable(L));
846 for (LuaScriptParamList::const_iterator i = args.begin(); i != args.end(); ++i) {
847 if ((*i)->optional && !(*i)->is_set) { continue; }
848 tbl_arg[(*i)->name] = (*i)->value;
850 (*_lua_add_action)(id + 1, name, script, bytecode, tbl_arg);
851 ActionChanged (id, name); /* EMIT SIGNAL */
852 } catch (luabridge::LuaException const& e) {
853 cerr << "LuaException:" << e.what () << endl;
860 LuaInstance::remove_lua_action (const int id)
863 (*_lua_del_action)(id + 1);
864 } catch (luabridge::LuaException const& e) {
865 cerr << "LuaException:" << e.what () << endl;
868 ActionChanged (id, ""); /* EMIT SIGNAL */
873 LuaInstance::lua_action_name (const int id, std::string& rv)
876 luabridge::LuaRef ref ((*_lua_get_action)(id + 1));
880 if (ref["name"].isString()) {
881 rv = ref["name"].cast<std::string>();
885 } catch (luabridge::LuaException const& e) {
886 cerr << "LuaException:" << e.what () << endl;
892 std::vector<std::string>
893 LuaInstance::lua_action_names ()
895 std::vector<std::string> rv;
896 for (int i = 0; i < 9; ++i) {
898 if (lua_action_name (i, name)) {
906 LuaInstance::lua_action (const int id, std::string& name, std::string& script, LuaScriptParamList& args)
909 luabridge::LuaRef ref ((*_lua_get_action)(id + 1));
913 if (!ref["name"].isString()) {
916 if (!ref["script"].isString()) {
919 if (!ref["args"].isTable()) {
922 name = ref["name"].cast<std::string>();
923 script = ref["script"].cast<std::string>();
926 LuaScriptInfoPtr lsi = LuaScripting::script_info (script);
930 args = LuaScriptParams::script_params (lsi, "action_params");
931 luabridge::LuaRef rargs (ref["args"]);
932 LuaScriptParams::ref_to_params (args, &rargs);
934 } catch (luabridge::LuaException const& e) {
935 cerr << "LuaException:" << e.what () << endl;
942 LuaInstance::register_lua_slot (const std::string& name, const std::string& script, const ARDOUR::LuaScriptParamList& args)
944 /* parse script, get ActionHook(s) from script */
949 l.Print.connect (&_lua_print);
951 lua_State* L = l.getState();
953 l.do_command ("function ardour () end");
954 l.do_command (script);
955 luabridge::LuaRef signals = luabridge::getGlobal (L, "signals");
956 if (signals.isFunction()) {
959 } catch (luabridge::LuaException const& e) {
960 cerr << "LuaException:" << e.what () << endl;
964 cerr << "Script registered no hooks." << endl;
968 /* register script w/args, get entry-point / ID */
971 LuaCallbackPtr p (new LuaCallback (_session, name, script, ah, args));
972 _callbacks.insert (std::make_pair(p->id(), p));
973 p->drop_callback.connect (_slotcon, MISSING_INVALIDATOR, boost::bind (&LuaInstance::unregister_lua_slot, this, p->id()), gui_context());
974 SlotChanged (p->id(), p->name(), p->signals()); /* EMIT SIGNAL */
976 } catch (luabridge::LuaException const& e) {
977 cerr << "LuaException:" << e.what () << endl;
983 LuaInstance::unregister_lua_slot (const PBD::ID& id)
985 LuaCallbackMap::iterator i = _callbacks.find (id);
986 if (i != _callbacks.end()) {
987 SlotChanged (id, "", ActionHook()); /* EMIT SIGNAL */
988 _callbacks.erase (i);
995 LuaInstance::lua_slots () const
997 std::vector<PBD::ID> rv;
998 for (LuaCallbackMap::const_iterator i = _callbacks.begin(); i != _callbacks.end(); ++i) {
999 rv.push_back (i->first);
1005 LuaInstance::lua_slot_name (const PBD::ID& id, std::string& name) const
1007 LuaCallbackMap::const_iterator i = _callbacks.find (id);
1008 if (i != _callbacks.end()) {
1009 name = i->second->name();
1015 std::vector<std::string>
1016 LuaInstance::lua_slot_names () const
1018 std::vector<std::string> rv;
1019 std::vector<PBD::ID> ids = lua_slots();
1020 for (std::vector<PBD::ID>::const_iterator i = ids.begin(); i != ids.end(); ++i) {
1022 if (lua_slot_name (*i, name)) {
1023 rv.push_back (name);
1030 LuaInstance::lua_slot (const PBD::ID& id, std::string& name, std::string& script, ActionHook& ah, ARDOUR::LuaScriptParamList& args)
1032 LuaCallbackMap::const_iterator i = _callbacks.find (id);
1033 if (i == _callbacks.end()) {
1034 return false; // error
1036 return i->second->lua_slot (name, script, ah, args);
1039 ///////////////////////////////////////////////////////////////////////////////
1041 LuaCallback::LuaCallback (Session *s,
1042 const std::string& name,
1043 const std::string& script,
1044 const ActionHook& ah,
1045 const ARDOUR::LuaScriptParamList& args)
1046 : SessionHandlePtr (s)
1051 // TODO: allow to reference object (e.g region)
1054 lua_State* L = lua.getState();
1055 luabridge::LuaRef tbl_arg (luabridge::newTable(L));
1056 for (LuaScriptParamList::const_iterator i = args.begin(); i != args.end(); ++i) {
1057 if ((*i)->optional && !(*i)->is_set) { continue; }
1058 tbl_arg[(*i)->name] = (*i)->value;
1062 const std::string& bytecode = LuaScripting::get_factory_bytecode (script);
1063 (*_lua_add)(name, script, bytecode, tbl_arg);
1064 } catch (luabridge::LuaException const& e) {
1065 cerr << "LuaException:" << e.what () << endl;
1066 throw failed_constructor ();
1073 LuaCallback::LuaCallback (Session *s, XMLNode & node)
1074 : SessionHandlePtr (s)
1076 XMLNode* child = NULL;
1077 if (node.name() != X_("LuaCallback")
1078 || !node.property ("signals")
1079 || !node.property ("id")
1080 || !node.property ("name")) {
1081 throw failed_constructor ();
1084 for (XMLNodeList::const_iterator n = node.children ().begin (); n != node.children ().end (); ++n) {
1085 if (!(*n)->is_content ()) { continue; }
1090 throw failed_constructor ();
1095 _id = PBD::ID (node.property ("id")->value ());
1096 _name = node.property ("name")->value ();
1097 _signals = ActionHook (node.property ("signals")->value ());
1100 guchar* buf = g_base64_decode (child->content ().c_str (), &size);
1102 (*_lua_load)(std::string ((const char*)buf, size));
1103 } catch (luabridge::LuaException const& e) {
1104 cerr << "LuaException:" << e.what () << endl;
1111 LuaCallback::~LuaCallback ()
1121 LuaCallback::get_state (void)
1125 luabridge::LuaRef savedstate ((*_lua_save)());
1126 saved = savedstate.cast<std::string>();
1128 lua.collect_garbage ();
1130 gchar* b64 = g_base64_encode ((const guchar*)saved.c_str (), saved.size ());
1131 std::string b64s (b64);
1134 XMLNode* script_node = new XMLNode (X_("LuaCallback"));
1135 script_node->add_property (X_("lua"), LUA_VERSION);
1136 script_node->add_property (X_("id"), _id.to_s ());
1137 script_node->add_property (X_("name"), _name);
1138 script_node->add_property (X_("signals"), _signals.to_string ());
1139 script_node->add_content (b64s);
1140 return *script_node;
1144 LuaCallback::init (void)
1147 lua.Print.connect (&_lua_print);
1151 "function ScriptManager ()"
1152 " local self = { script = {}, instance = {} }"
1154 " local addinternal = function (n, s, f, a)"
1155 " assert(type(n) == 'string', 'Name must be string')"
1156 " assert(type(s) == 'string', 'Script must be string')"
1157 " assert(type(f) == 'function', 'Factory is a not a function')"
1158 " assert(type(a) == 'table' or type(a) == 'nil', 'Given argument is invalid')"
1159 " self.script = { ['n'] = n, ['s'] = s, ['f'] = f, ['a'] = a }"
1160 " local env = _ENV; env.f = nil env.debug = nil os.exit = nil require = nil dofile = nil loadfile = nil package = nil"
1161 " self.instance = load (string.dump(f, true), nil, nil, env)(a)"
1164 " local call = function (...)"
1165 " if type(self.instance) == 'function' then"
1166 " local status, err = pcall (self.instance, ...)"
1167 " if not status then"
1168 " print ('callback \"'.. self.script['n'] .. '\": ', err)" // error out
1169 " self.script = nil"
1170 " self.instance = nil"
1178 " local add = function (n, s, b, a)"
1179 " assert(type(b) == 'string', 'ByteCode must be string')"
1180 " load (b)()" // assigns f
1181 " assert(type(f) == 'string', 'Assigned ByteCode must be string')"
1182 " addinternal (n, s, load(f), a)"
1185 " local get = function ()"
1186 " if type(self.instance) == 'function' and type(self.script['n']) == 'string' then"
1187 " return { ['name'] = self.script['n'],"
1188 " ['script'] = self.script['s'],"
1189 " ['args'] = self.script['a'] }"
1196 " local function basic_serialize (o)"
1197 " if type(o) == \"number\" then"
1198 " return tostring(o)"
1200 " return string.format(\"%q\", o)"
1204 " local function serialize (name, value)"
1205 " local rv = name .. ' = '"
1207 " if type(value) == \"number\" or type(value) == \"string\" or type(value) == \"nil\" then"
1208 " return rv .. basic_serialize(value) .. ' '"
1209 " elseif type(value) == \"table\" then"
1211 " for k,v in pairs(value) do"
1212 " local fieldname = string.format(\"%s[%s]\", name, basic_serialize(k))"
1213 " rv = rv .. serialize(fieldname, v) .. ' '"
1214 " collectgarbage()" // string concatenation allocates a new string
1217 " elseif type(value) == \"function\" then"
1218 " return rv .. string.format(\"%q\", string.dump(value, true))"
1220 " error('cannot save a ' .. type(value))"
1226 " local save = function ()"
1227 " return (serialize('s', self.script))"
1230 " local restore = function (state)"
1233 " addinternal (s['n'], s['s'], load(s['f']), s['a'])"
1236 " return { call = call, add = add, get = get,"
1237 " restore = restore, save = save}"
1240 " manager = ScriptManager ()"
1241 " ScriptManager = nil"
1244 lua_State* L = lua.getState();
1247 luabridge::LuaRef lua_mgr = luabridge::getGlobal (L, "manager");
1248 lua.do_command ("manager = nil"); // hide it.
1249 lua.do_command ("collectgarbage()");
1251 _lua_add = new luabridge::LuaRef(lua_mgr["add"]);
1252 _lua_get = new luabridge::LuaRef(lua_mgr["get"]);
1253 _lua_call = new luabridge::LuaRef(lua_mgr["call"]);
1254 _lua_save = new luabridge::LuaRef(lua_mgr["save"]);
1255 _lua_load = new luabridge::LuaRef(lua_mgr["restore"]);
1257 } catch (luabridge::LuaException const& e) {
1258 fatal << string_compose (_("programming error: %1"),
1259 X_("Failed to setup Lua callback interpreter"))
1261 abort(); /*NOTREACHED*/
1264 LuaInstance::register_classes (L);
1266 luabridge::push <PublicEditor *> (L, &PublicEditor::instance());
1267 lua_setglobal (L, "Editor");
1271 LuaCallback::lua_slot (std::string& name, std::string& script, ActionHook& ah, ARDOUR::LuaScriptParamList& args)
1273 // TODO consolidate w/ LuaInstance::lua_action()
1275 luabridge::LuaRef ref = (*_lua_get)();
1279 if (!ref["name"].isString()) {
1282 if (!ref["script"].isString()) {
1285 if (!ref["args"].isTable()) {
1290 name = ref["name"].cast<std::string> ();
1291 script = ref["script"].cast<std::string> ();
1294 LuaScriptInfoPtr lsi = LuaScripting::script_info (script);
1298 args = LuaScriptParams::script_params (lsi, "action_params");
1299 luabridge::LuaRef rargs (ref["args"]);
1300 LuaScriptParams::ref_to_params (args, &rargs);
1302 } catch (luabridge::LuaException const& e) {
1303 cerr << "LuaException:" << e.what () << endl;
1310 LuaCallback::set_session (ARDOUR::Session *s)
1312 SessionHandlePtr::set_session (s);
1315 lua_State* L = lua.getState();
1316 LuaBindings::set_session (L, _session);
1323 LuaCallback::session_going_away ()
1325 ENSURE_GUI_THREAD (*this, &LuaCallback::session_going_away);
1326 lua.do_command ("collectgarbage();");
1328 SessionHandlePtr::session_going_away ();
1331 drop_callback (); /* EMIT SIGNAL */
1335 LuaCallback::reconnect ()
1337 _connections.drop_connections ();
1338 if ((*_lua_get) ().isNil ()) {
1339 drop_callback (); /* EMIT SIGNAL */
1343 // TODO pass object which emits the signal (e.g region)
1345 // save/load bound objects will be tricky.
1346 // Best idea so far is to save/lookup the PBD::ID
1347 // (either use boost::any indirection or templates for bindable
1348 // object types or a switch statement..)
1350 // _session->route_by_id ()
1351 // _session->track_by_diskstream_id ()
1352 // _session->source_by_id ()
1353 // _session->controllable_by_id ()
1354 // _session->processor_by_id ()
1355 // RegionFactory::region_by_id ()
1357 // TODO loop over objects (if any)
1359 reconnect_object ((void*)0);
1362 template <class T> void
1363 LuaCallback::reconnect_object (T obj)
1365 for (uint32_t i = 0; i < LuaSignal::LAST_SIGNAL; ++i) {
1367 #define ENGINE(n,c,p) else if (i == LuaSignal::n) { connect_ ## p (LuaSignal::n, AudioEngine::instance(), &(AudioEngine::instance()->c)); }
1368 #define SESSION(n,c,p) else if (i == LuaSignal::n) { if (_session) { connect_ ## p (LuaSignal::n, _session, &(_session->c)); } }
1369 #define STATIC(n,c,p) else if (i == LuaSignal::n) { connect_ ## p (LuaSignal::n, obj, c); }
1371 # include "luasignal_syms.h"
1373 PBD::fatal << string_compose (_("programming error: %1: %2"), "Impossible LuaSignal type", i) << endmsg;
1374 abort(); /*NOTREACHED*/
1383 template <typename T, typename S> void
1384 LuaCallback::connect_0 (enum LuaSignal::LuaSignal ls, T ref, S *signal) {
1386 _connections, invalidator (*this),
1387 boost::bind (&LuaCallback::proxy_0<T>, this, ls, ref),
1391 template <typename T, typename C1> void
1392 LuaCallback::connect_1 (enum LuaSignal::LuaSignal ls, T ref, PBD::Signal1<void, C1> *signal) {
1394 _connections, invalidator (*this),
1395 boost::bind (&LuaCallback::proxy_1<T, C1>, this, ls, ref, _1),
1399 template <typename T, typename C1, typename C2> void
1400 LuaCallback::connect_2 (enum LuaSignal::LuaSignal ls, T ref, PBD::Signal2<void, C1, C2> *signal) {
1402 _connections, invalidator (*this),
1403 boost::bind (&LuaCallback::proxy_2<T, C1, C2>, this, ls, ref, _1, _2),
1407 template <typename T> void
1408 LuaCallback::proxy_0 (enum LuaSignal::LuaSignal ls, T ref) {
1411 const luabridge::LuaRef& rv ((*_lua_call)((int)ls, ref));
1412 if (! rv.cast<bool> ()) {
1416 /* destroy LuaRef ^^ first before calling drop_callback() */
1418 drop_callback (); /* EMIT SIGNAL */
1422 template <typename T, typename C1> void
1423 LuaCallback::proxy_1 (enum LuaSignal::LuaSignal ls, T ref, C1 a1) {
1426 const luabridge::LuaRef& rv ((*_lua_call)((int)ls, ref, a1));
1427 if (! rv.cast<bool> ()) {
1432 drop_callback (); /* EMIT SIGNAL */
1436 template <typename T, typename C1, typename C2> void
1437 LuaCallback::proxy_2 (enum LuaSignal::LuaSignal ls, T ref, C1 a1, C2 a2) {
1440 const luabridge::LuaRef& rv ((*_lua_call)((int)ls, ref, a1, a2));
1441 if (! rv.cast<bool> ()) {
1446 drop_callback (); /* EMIT SIGNAL */