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