Fix a tiny memory-leak when calling vfork
[ardour.git] / libs / ardour / mac_vst_plugin.cc
1 /*
2  * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3  * Copyright (C) 2004 Paul Davis
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include <glibmm/fileutils.h>
21 #include <glibmm/miscutils.h>
22
23 #include "ardour/filesystem_paths.h"
24 #include "ardour/mac_vst_plugin.h"
25 #include "ardour/mac_vst_support.h"
26 #include "ardour/session.h"
27
28 #include "pbd/i18n.h"
29
30 using namespace std;
31 using namespace ARDOUR;
32 using namespace PBD;
33
34 MacVSTPlugin::MacVSTPlugin (AudioEngine& e, Session& session, VSTHandle* h, int unique_id)
35         : VSTPlugin (e, session, h)
36 {
37         /* Instantiate the plugin and return a VSTState* */
38
39         Session::vst_current_loading_id = unique_id;
40         if ((_state = mac_vst_instantiate (_handle, Session::vst_callback, this)) == 0) {
41                 throw failed_constructor ();
42         }
43         open_plugin ();
44         Session::vst_current_loading_id = 0;
45
46         init_plugin ();
47 }
48
49 MacVSTPlugin::MacVSTPlugin (const MacVSTPlugin &other)
50         : VSTPlugin (other)
51 {
52         _handle = other._handle;
53
54         Session::vst_current_loading_id = PBD::atoi (other.unique_id ());
55         if ((_state = mac_vst_instantiate (_handle, Session::vst_callback, this)) == 0) {
56                 throw failed_constructor ();
57         }
58         open_plugin ();
59         Session::vst_current_loading_id = 0;
60
61         XMLNode* root = new XMLNode (other.state_node_name ());
62         other.add_state (root);
63         set_state (*root, Stateful::loading_state_version);
64         delete root;
65
66         init_plugin ();
67 }
68
69 MacVSTPlugin::~MacVSTPlugin ()
70 {
71         mac_vst_close (_state);
72 }
73
74 void
75 MacVSTPlugin::open_plugin ()
76 {
77         VSTPlugin::open_plugin ();
78         _plugin->dispatcher (_plugin, effCanDo, 0, 0, const_cast<char*> ("hasCockosViewAsConfig"), 0.0f);
79 }
80
81 PluginPtr
82 MacVSTPluginInfo::load (Session& session)
83 {
84         try {
85                 PluginPtr plugin;
86
87                 if (Config->get_use_macvst ()) {
88                         VSTHandle* handle;
89
90                         handle = mac_vst_load (path.c_str ());
91
92                         if (handle == NULL) {
93                                 error << string_compose (_("MacVST: cannot load module from \"%1\""), path) << endmsg;
94                         }
95                         else {
96                                 plugin.reset (new MacVSTPlugin (session.engine (), session, handle, PBD::atoi (unique_id)));
97                         }
98                 }
99                 else {
100                         error << _("You asked ardour to not use any MacVST plugins") << endmsg;
101                         return PluginPtr ((Plugin*) 0);
102                 }
103
104                 plugin->set_info (PluginInfoPtr (new MacVSTPluginInfo (*this)));
105                 return plugin;
106         }
107
108         catch (failed_constructor &err) {
109                 return PluginPtr ((Plugin*) 0);
110         }
111 }
112
113 std::vector<Plugin::PresetRecord>
114 MacVSTPluginInfo::get_presets (bool user_only) const
115 {
116         std::vector<Plugin::PresetRecord> p;
117 #ifndef NO_PLUGIN_STATE
118         if (!Config->get_use_macvst ()) {
119                 return p;
120         }
121
122         if (!user_only) {
123                 // TODO - cache, instantiating the plugin can be heavy
124                 /* Built-in presets */
125                 VSTHandle* handle = mac_vst_load (path.c_str ());
126                 Session::vst_current_loading_id = atoi (unique_id);
127                 AEffect* plugin = handle->main_entry (Session::vst_callback);
128                 Session::vst_current_loading_id = 0;
129                 plugin->ptr1 = NULL;
130
131                 plugin->dispatcher (plugin, effOpen, 0, 0, 0, 0); // :(
132                 int const vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, NULL, 0);
133
134                 for (int i = 0; i < plugin->numPrograms; ++i) {
135                         Plugin::PresetRecord r (string_compose (X_("VST:%1:%2"), unique_id, std::setw(4), std::setfill('0'), i), "", false);
136                         if (vst_version >= 2) {
137                                 char buf[256];
138                                 if (plugin->dispatcher (plugin, 29, i, 0, buf, 0) == 1) {
139                                         r.label = buf;
140                                 } else {
141                                         r.label = string_compose (_("Preset %1"), i);
142                                 }
143                         } else {
144                                 r.label = string_compose (_("Preset %1"), i);
145                         }
146                         p.push_back (r);
147                 }
148
149                 plugin->dispatcher (plugin, effMainsChanged, 0, 0, 0, 0);
150                 plugin->dispatcher (plugin, effClose, 0, 0, 0, 0); // :(
151
152                 if (handle->plugincnt) {
153                         handle->plugincnt--;
154                 }
155                 mac_vst_unload (handle);
156         }
157
158         /* user presets */
159         XMLTree* t = new XMLTree;
160         std::string pf = Glib::build_filename (ARDOUR::user_config_directory (), "presets", string_compose ("vst-%1", unique_id));
161         if (Glib::file_test (pf, Glib::FILE_TEST_EXISTS)) {
162                 t->set_filename (pf);
163                 if (t->read ()) { // TODO read names only. skip parsing the actual data
164                         XMLNode* root = t->root ();
165                         for (XMLNodeList::const_iterator i = root->children ().begin (); i != root->children ().end (); ++i) {
166                                 XMLProperty const * uri = (*i)->property (X_("uri"));
167                                 XMLProperty const * label = (*i)->property (X_("label"));
168                                 p.push_back (Plugin::PresetRecord (uri->value (), label->value (), true));
169                         }
170                 }
171         }
172         delete t;
173 #endif
174
175         return p;
176 }
177
178 MacVSTPluginInfo::MacVSTPluginInfo (_VSTInfo* nfo) : VSTPluginInfo (nfo)
179 {
180         type = ARDOUR::MacVST;
181 }