remove debug output
[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         open_plugin ();
43         Session::vst_current_loading_id = 0;
44
45         init_plugin ();
46 }
47
48 WindowsVSTPlugin::WindowsVSTPlugin (const WindowsVSTPlugin &other)
49         : VSTPlugin (other)
50 {
51         _handle = other._handle;
52
53         Session::vst_current_loading_id = PBD::atoi(other.unique_id());
54         if ((_state = fst_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 WindowsVSTPlugin::~WindowsVSTPlugin ()
69 {
70         deactivate ();
71         fst_close (_state);
72 }
73
74 PluginPtr
75 WindowsVSTPluginInfo::load (Session& session)
76 {
77         try {
78                 PluginPtr plugin;
79
80                 if (Config->get_use_windows_vst ()) {
81                         VSTHandle* handle;
82
83                         handle = fst_load(path.c_str());
84
85                         if (!handle) {
86                                 error << string_compose(_("VST: cannot load module from \"%1\""), path) << endmsg;
87                         } else {
88                                 plugin.reset (new WindowsVSTPlugin (session.engine(), session, handle, PBD::atoi(unique_id)));
89                         }
90                 } else {
91                         error << _("You asked ardour to not use any VST plugins") << endmsg;
92                         return PluginPtr ((Plugin*) 0);
93                 }
94
95                 plugin->set_info(PluginInfoPtr(new WindowsVSTPluginInfo(*this)));
96                 return plugin;
97         }
98
99         catch (failed_constructor &err) {
100                 return PluginPtr ((Plugin*) 0);
101         }
102 }
103
104 std::vector<Plugin::PresetRecord>
105 WindowsVSTPluginInfo::get_presets (bool user_only) const
106 {
107         std::vector<Plugin::PresetRecord> p;
108 #ifndef NO_PLUGIN_STATE
109         if (!Config->get_use_lxvst()) {
110                 return p;
111         }
112
113         if (!user_only) {
114                 // TODO cache and load factory-preset names
115         }
116
117         /* user presets */
118         XMLTree* t = new XMLTree;
119         std::string pf = Glib::build_filename (ARDOUR::user_config_directory (), "presets", string_compose ("vst-%1", unique_id));
120         if (Glib::file_test (pf, Glib::FILE_TEST_EXISTS)) {
121                 t->set_filename (pf);
122                 if (t->read ()) {
123                         XMLNode* root = t->root ();
124                         for (XMLNodeList::const_iterator i = root->children().begin(); i != root->children().end(); ++i) {
125                                 XMLProperty const * uri = (*i)->property (X_("uri"));
126                                 XMLProperty const * label = (*i)->property (X_("label"));
127                                 p.push_back (Plugin::PresetRecord (uri->value(), label->value(), true));
128                         }
129                 }
130         }
131         delete t;
132 #endif
133
134         return p;
135 }
136
137 WindowsVSTPluginInfo::WindowsVSTPluginInfo()
138 {
139        type = ARDOUR::Windows_VST;
140 }
141