fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / windows_vst_plugin.cc
1 /*
2     Copyright (C) 2004 Paul Davis
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
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "fst.h"
21
22 #include <glibmm/fileutils.h>
23 #include <glibmm/miscutils.h>
24
25 #include "ardour/filesystem_paths.h"
26 #include "ardour/windows_vst_plugin.h"
27 #include "ardour/session.h"
28
29 #include "pbd/i18n.h"
30
31 using namespace std;
32 using namespace ARDOUR;
33 using namespace PBD;
34
35 WindowsVSTPlugin::WindowsVSTPlugin (AudioEngine& e, Session& session, VSTHandle* h, int unique_id)
36         : VSTPlugin (e, session, h)
37 {
38         Session::vst_current_loading_id = unique_id;
39         if ((_state = fst_instantiate (_handle, Session::vst_callback, this)) == 0) {
40                 throw failed_constructor();
41         }
42         Session::vst_current_loading_id = 0;
43
44         set_plugin (_state->plugin);
45 }
46
47 WindowsVSTPlugin::WindowsVSTPlugin (const WindowsVSTPlugin &other)
48         : VSTPlugin (other)
49 {
50         _handle = other._handle;
51
52         Session::vst_current_loading_id = PBD::atoi(other.unique_id());
53         if ((_state = fst_instantiate (_handle, Session::vst_callback, this)) == 0) {
54                 throw failed_constructor();
55         }
56         Session::vst_current_loading_id = 0;
57
58         _plugin = _state->plugin;
59 }
60
61 WindowsVSTPlugin::~WindowsVSTPlugin ()
62 {
63         deactivate ();
64         fst_close (_state);
65 }
66
67 PluginPtr
68 WindowsVSTPluginInfo::load (Session& session)
69 {
70         try {
71                 PluginPtr plugin;
72
73                 if (Config->get_use_windows_vst ()) {
74                         VSTHandle* handle;
75
76                         handle = fst_load(path.c_str());
77
78                         if (!handle) {
79                                 error << string_compose(_("VST: cannot load module from \"%1\""), path) << endmsg;
80                         } else {
81                                 plugin.reset (new WindowsVSTPlugin (session.engine(), session, handle, PBD::atoi(unique_id)));
82                         }
83                 } else {
84                         error << _("You asked ardour to not use any VST plugins") << endmsg;
85                         return PluginPtr ((Plugin*) 0);
86                 }
87
88                 plugin->set_info(PluginInfoPtr(new WindowsVSTPluginInfo(*this)));
89                 return plugin;
90         }
91
92         catch (failed_constructor &err) {
93                 return PluginPtr ((Plugin*) 0);
94         }
95 }
96
97 std::vector<Plugin::PresetRecord>
98 WindowsVSTPluginInfo::get_presets (bool user_only) const
99 {
100         std::vector<Plugin::PresetRecord> p;
101 #ifndef NO_PLUGIN_STATE
102         if (!Config->get_use_lxvst()) {
103                 return p;
104         }
105
106         if (!user_only) {
107                 // TODO cache and load factory-preset names
108         }
109
110         /* user presets */
111         XMLTree* t = new XMLTree;
112         std::string pf = Glib::build_filename (ARDOUR::user_config_directory (), "presets", string_compose ("vst-%1", unique_id));
113         if (Glib::file_test (pf, Glib::FILE_TEST_EXISTS)) {
114                 t->set_filename (pf);
115                 if (t->read ()) {
116                         XMLNode* root = t->root ();
117                         for (XMLNodeList::const_iterator i = root->children().begin(); i != root->children().end(); ++i) {
118                                 XMLProperty const * uri = (*i)->property (X_("uri"));
119                                 XMLProperty const * label = (*i)->property (X_("label"));
120                                 p.push_back (Plugin::PresetRecord (uri->value(), label->value(), true));
121                         }
122                 }
123         }
124         delete t;
125 #endif
126
127         return p;
128 }
129
130 WindowsVSTPluginInfo::WindowsVSTPluginInfo()
131 {
132        type = ARDOUR::Windows_VST;
133 }
134