LV2: don't leave active plugins deactivated after latency-compute-run
[ardour.git] / libs / ardour / lv2_plugin.cc
index f5dcc264101b33e456a093ca1e670a4162b2ebc6..deff882d70ac409670744dc910428afc264bd843 100644 (file)
 #include <cstdlib>
 #include <cstring>
 
-#include <giomm/file.h>
+#include <glib/gstdio.h>
 #include <glib/gprintf.h>
 #include <glibmm.h>
 
 #include <boost/utility.hpp>
 
-#include "pbd/pathscanner.h"
+#include "pbd/file_utils.h"
+#include "pbd/stl_delete.h"
 #include "pbd/compose.h"
 #include "pbd/error.h"
 #include "pbd/xml++.h"
@@ -47,7 +48,7 @@
 #include "ardour/types.h"
 #include "ardour/utils.h"
 #include "ardour/worker.h"
-#include "ardour/lv2_bundled_search_path.h"
+#include "ardour/search_paths.h"
 
 #include "i18n.h"
 #include <locale.h>
@@ -406,7 +407,7 @@ LV2Plugin::init(const void* c_plugin, framecnt_t rate)
                throw failed_constructor();
        }
 
-#ifdef HAVE_NEW_LILV
+#ifdef HAVE_LILV_0_16_0
        // Load default state
        LilvState* state = lilv_state_new_from_world(
                _world.world, _uri_map.urid_map(), lilv_plugin_get_uri(_impl->plugin));
@@ -880,27 +881,6 @@ LV2Plugin::lv2_state_make_path(LV2_State_Make_Path_Handle handle,
        return g_strndup(abs_path.c_str(), abs_path.length());
 }
 
-static void
-remove_directory(const std::string& path)
-{
-       if (!Glib::file_test(path, Glib::FILE_TEST_IS_DIR)) {
-               warning << string_compose("\"%1\" is not a directory", path) << endmsg;
-               return;
-       }
-
-       Glib::RefPtr<Gio::File>           dir = Gio::File::create_for_path(path);
-       Glib::RefPtr<Gio::FileEnumerator> e   = dir->enumerate_children();
-       Glib::RefPtr<Gio::FileInfo>       fi;
-       while ((fi = e->next_file())) {
-               if (fi->get_type() == Gio::FILE_TYPE_DIRECTORY) {
-                       remove_directory(fi->get_name());
-               } else {
-                       dir->get_child(fi->get_name())->remove();
-               }
-       }
-       dir->remove();
-}
-
 void
 LV2Plugin::add_state(XMLNode* root) const
 {
@@ -952,7 +932,7 @@ LV2Plugin::add_state(XMLNode* root) const
                } else {
                        // State is identical, decrement version and nuke directory
                        lilv_state_free(state);
-                       remove_directory(new_dir);
+                       PBD::remove_directory(new_dir);
                        --_state_version;
                }
 
@@ -1026,6 +1006,7 @@ LV2Plugin::load_preset(PresetRecord r)
        if (state) {
                lilv_state_restore(state, _impl->instance, set_port_value, this, 0, NULL);
                lilv_state_free(state);
+               Plugin::load_preset(r);
        }
 
        lilv_node_free(pset);
@@ -1093,7 +1074,18 @@ LV2Plugin::do_save_preset(string name)
 
        lilv_state_free(state);
 
-       return Glib::filename_to_uri(Glib::build_filename(bundle, file_name));
+       std::string uri = Glib::filename_to_uri(Glib::build_filename(bundle, file_name));
+       LilvNode *node_bundle = lilv_new_uri(_world.world, Glib::filename_to_uri(Glib::build_filename(bundle, "/")).c_str());
+       LilvNode *node_preset = lilv_new_uri(_world.world, uri.c_str());
+#ifdef HAVE_LILV_0_19_2
+       lilv_world_unload_resource(_world.world, node_preset);
+       lilv_world_unload_bundle(_world.world, node_bundle);
+#endif
+       lilv_world_load_bundle(_world.world, node_bundle);
+       lilv_world_load_resource(_world.world, node_preset);
+       lilv_node_free(node_bundle);
+       lilv_node_free(node_preset);
+       return uri;
 }
 
 void
@@ -1106,7 +1098,7 @@ LV2Plugin::do_remove_preset(string name)
                        name + ".ttl"
                )
        );
-       unlink(preset_file.c_str());
+       ::g_unlink(preset_file.c_str());
 }
 
 bool
@@ -1134,16 +1126,16 @@ LV2Plugin::write_to(RingBuffer<uint8_t>* dest,
                     uint32_t             size,
                     const uint8_t*       body)
 {
-       const uint32_t buf_size = sizeof(UIMessage) + size;
-       uint8_t        buf[buf_size];
+       const uint32_t  buf_size = sizeof(UIMessage) + size;
+       vector<uint8_t> buf(buf_size);
 
-       UIMessage* msg = (UIMessage*)buf;
+       UIMessage* msg = (UIMessage*)&buf[0];
        msg->index    = index;
        msg->protocol = protocol;
        msg->size     = size;
        memcpy(msg + 1, body, size);
 
-       return (dest->write(buf, buf_size) == buf_size);
+       return (dest->write(&buf[0], buf_size) == buf_size);
 }
 
 bool
@@ -1162,10 +1154,14 @@ LV2Plugin::write_from_ui(uint32_t       index,
                 *  e.g 48kSPS / 128fpp -> audio-periods = 375 Hz
                 *  ui-periods = 25 Hz (SuperRapidScreenUpdate)
                 *  default minimumSize = 32K (see LV2Plugin::allocate_atom_event_buffers()
-                *  -> 15 * 32K
-                * it is safe to overflow (but the plugin state may be inconsistent).
+                *
+                * it is NOT safe to overflow (msg.size will be misinterpreted)
                 */
-               rbs = max((size_t) 32768 * 6, rbs);
+               uint32_t bufsiz = 32768;
+               if (_atom_ev_buffers && _atom_ev_buffers[0]) {
+                       bufsiz =  lv2_evbuf_get_capacity(_atom_ev_buffers[0]);
+               }
+               rbs = max((size_t) bufsiz * 8, rbs);
                _from_ui = new RingBuffer<uint8_t>(rbs);
        }
 
@@ -1194,8 +1190,12 @@ LV2Plugin::enable_ui_emmission()
 {
        if (!_to_ui) {
                /* see note in LV2Plugin::write_from_ui() */
+               uint32_t bufsiz = 32768;
+               if (_atom_ev_buffers && _atom_ev_buffers[0]) {
+                       bufsiz =  lv2_evbuf_get_capacity(_atom_ev_buffers[0]);
+               }
                size_t rbs = _session.engine().raw_buffer_size(DataType::MIDI) * NBUFS;
-               rbs = max((size_t) 32768 * 8, rbs);
+               rbs = max((size_t) bufsiz * 8, rbs);
                _to_ui = new RingBuffer<uint8_t>(rbs);
        }
 }
@@ -1214,13 +1214,13 @@ LV2Plugin::emit_to_ui(void* controller, UIMessageSink sink)
                        error << "Error reading from Plugin=>UI RingBuffer" << endmsg;
                        break;
                }
-               uint8_t body[msg.size];
-               if (_to_ui->read(body, msg.size) != msg.size) {
+               vector<uint8_t> body(msg.size);
+               if (_to_ui->read(&body[0], msg.size) != msg.size) {
                        error << "Error reading from Plugin=>UI RingBuffer" << endmsg;
                        break;
                }
 
-               sink(controller, msg.index, msg.size, msg.protocol, body);
+               sink(controller, msg.index, msg.size, msg.protocol, &body[0]);
 
                read_space -= sizeof(msg) + msg.size;
        }
@@ -1508,7 +1508,7 @@ LV2Plugin::allocate_atom_event_buffers()
                return;
        }
 
-       DEBUG_TRACE(DEBUG::LV2, string_compose("allocate %1 atom_ev_buffers\n", total_atom_buffers));
+       DEBUG_TRACE(DEBUG::LV2, string_compose("allocate %1 atom_ev_buffers of %d bytes\n", total_atom_buffers, minimumSize));
        _atom_ev_buffers = (LV2_Evbuf**) malloc((total_atom_buffers + 1) * sizeof(LV2_Evbuf*));
        for (int i = 0; i < total_atom_buffers; ++i ) {
                _atom_ev_buffers[i] = lv2_evbuf_new(minimumSize, LV2_EVBUF_ATOM,
@@ -1681,6 +1681,9 @@ LV2Plugin::connect_and_run(BufferSet& bufs,
                                }
                        } else if (!valid) {
                                // Nothing we understand or care about, connect to scratch
+                               // see note for midi-buffer size above
+                               scratch_bufs.ensure_lv2_bufsize((flags & PORT_INPUT),
+                                               0, _port_minimumSize[port_index]);
                                _ev_buffers[port_index] = scratch_bufs.get_lv2_midi(
                                        (flags & PORT_INPUT), 0, (flags & PORT_EVENT));
                        }
@@ -1700,15 +1703,15 @@ LV2Plugin::connect_and_run(BufferSet& bufs,
                                error << "Error reading from UI=>Plugin RingBuffer" << endmsg;
                                break;
                        }
-                       uint8_t body[msg.size];
-                       if (_from_ui->read(body, msg.size) != msg.size) {
+                       vector<uint8_t> body(msg.size);
+                       if (_from_ui->read(&body[0], msg.size) != msg.size) {
                                error << "Error reading from UI=>Plugin RingBuffer" << endmsg;
                                break;
                        }
                        if (msg.protocol == urids.atom_eventTransfer) {
                                LV2_Evbuf*            buf  = _ev_buffers[msg.index];
                                LV2_Evbuf_Iterator    i    = lv2_evbuf_end(buf);
-                               const LV2_Atom* const atom = (const LV2_Atom*)body;
+                               const LV2_Atom* const atom = (const LV2_Atom*)&body[0];
                                if (!lv2_evbuf_write(&i, nframes, 0, atom->type, atom->size,
                                                (const uint8_t*)(atom + 1))) {
                                        error << "Failed to write data to LV2 event buffer\n";
@@ -1885,6 +1888,7 @@ LV2Plugin::latency_compute_run()
 
        // Run the plugin so that it can set its latency parameter
 
+       bool was_activated = _was_activated;
        activate();
 
        uint32_t port_index = 0;
@@ -1915,6 +1919,9 @@ LV2Plugin::latency_compute_run()
 
        run(bufsize);
        deactivate();
+       if (was_activated) {
+               activate();
+       }
 }
 
 const LilvPort*
@@ -2014,21 +2021,19 @@ LV2World::load_bundled_plugins()
 {
        if (!_bundle_checked) {
                cout << "Scanning folders for bundled LV2s: " << ARDOUR::lv2_bundled_search_path().to_string() << endl;
-               PathScanner scanner;
-               vector<string *> *plugin_objects = scanner (ARDOUR::lv2_bundled_search_path().to_string(), lv2_filter, 0, true, true);
-               if (plugin_objects) {
-                       for ( vector<string *>::iterator x = plugin_objects->begin(); x != plugin_objects->end (); ++x) {
-#ifdef WINDOWS
-                               string uri = "file:///" + **x + "/";
+
+               vector<string> plugin_objects;
+               find_paths_matching_filter (plugin_objects, ARDOUR::lv2_bundled_search_path(), lv2_filter, 0, true, true, true);
+               for ( vector<string>::iterator x = plugin_objects.begin(); x != plugin_objects.end (); ++x) {
+#ifdef PLATFORM_WINDOWS
+                       string uri = "file:///" + *x + "/";
 #else
-                               string uri = "file://" + **x + "/";
+                       string uri = "file://" + *x + "/";
 #endif
-                               LilvNode *node = lilv_new_uri(world, uri.c_str());
-                               lilv_world_load_bundle(world, node);
-                               lilv_node_free(node);
-                       }
+                       LilvNode *node = lilv_new_uri(world, uri.c_str());
+                       lilv_world_load_bundle(world, node);
+                       lilv_node_free(node);
                }
-               delete (plugin_objects);
 
                _bundle_checked = true;
        }
@@ -2070,7 +2075,9 @@ LV2PluginInfo::discover()
        PluginInfoList*    plugs   = new PluginInfoList;
        const LilvPlugins* plugins = lilv_world_get_all_plugins(_world.world);
 
-       info << "LV2: Discovering " << lilv_plugins_size(plugins) << " plugins" << endmsg;
+       if (!Config->get_show_plugin_scan_window()) {
+               info << "LV2: Discovering " << lilv_plugins_size(plugins) << " plugins" << endmsg;
+       }
 
        LILV_FOREACH(plugins, i, plugins) {
                const LilvPlugin* p = lilv_plugins_get(plugins, i);
@@ -2088,6 +2095,7 @@ LV2PluginInfo::discover()
 
                info->name = string(lilv_node_as_string(name));
                lilv_node_free(name);
+               ARDOUR::PluginScanMessage(_("LV2"), info->name, false);
 
                const LilvPluginClass* pclass = lilv_plugin_get_class(p);
                const LilvNode*        label  = lilv_plugin_class_get_label(pclass);