NO-OP: whitespace/comments
[ardour.git] / libs / ardour / windows_vst_plugin.cc
1 /*
2  * Copyright (C) 2011 Carl Hetherington <carl@carlh.net>
3  * Copyright (C) 2014-2018 Robin Gareus <robin@gareus.org>
4  * Copyright (C) 2016 Paul Davis <paul@linuxaudiosystems.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include "fst.h"
22
23 #include <glibmm/fileutils.h>
24 #include <glibmm/miscutils.h>
25
26 #include "ardour/filesystem_paths.h"
27 #include "ardour/windows_vst_plugin.h"
28 #include "ardour/session.h"
29
30 #include "pbd/i18n.h"
31
32 using namespace std;
33 using namespace ARDOUR;
34 using namespace PBD;
35
36 WindowsVSTPlugin::WindowsVSTPlugin (AudioEngine& e, Session& session, VSTHandle* h, int unique_id)
37         : VSTPlugin (e, session, h)
38 {
39         Session::vst_current_loading_id = unique_id;
40         if ((_state = fst_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 WindowsVSTPlugin::WindowsVSTPlugin (const WindowsVSTPlugin &other)
50         : VSTPlugin (other)
51 {
52         _handle = other._handle;
53
54         Session::vst_current_loading_id = PBD::atoi(other.unique_id());
55         if ((_state = fst_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 WindowsVSTPlugin::~WindowsVSTPlugin ()
70 {
71         deactivate ();
72         fst_close (_state);
73 }
74
75 PluginPtr
76 WindowsVSTPluginInfo::load (Session& session)
77 {
78         try {
79                 PluginPtr plugin;
80
81                 if (Config->get_use_windows_vst ()) {
82                         VSTHandle* handle;
83
84                         handle = fst_load(path.c_str());
85
86                         if (!handle) {
87                                 error << string_compose(_("VST: cannot load module from \"%1\""), path) << endmsg;
88                         } else {
89                                 plugin.reset (new WindowsVSTPlugin (session.engine(), session, handle, PBD::atoi(unique_id)));
90                         }
91                 } else {
92                         error << _("You asked ardour to not use any VST plugins") << endmsg;
93                         return PluginPtr ((Plugin*) 0);
94                 }
95
96                 plugin->set_info(PluginInfoPtr(new WindowsVSTPluginInfo(*this)));
97                 return plugin;
98         }
99
100         catch (failed_constructor &err) {
101                 return PluginPtr ((Plugin*) 0);
102         }
103 }
104
105 std::vector<Plugin::PresetRecord>
106 WindowsVSTPluginInfo::get_presets (bool user_only) const
107 {
108         std::vector<Plugin::PresetRecord> p;
109
110         if (!Config->get_use_lxvst()) {
111                 return p;
112         }
113
114         if (!user_only) {
115                 // TODO cache and load factory-preset names
116         }
117
118         /* user presets */
119         XMLTree* t = new XMLTree;
120         std::string pf = Glib::build_filename (ARDOUR::user_config_directory (), "presets", string_compose ("vst-%1", unique_id));
121         if (Glib::file_test (pf, Glib::FILE_TEST_EXISTS)) {
122                 t->set_filename (pf);
123                 if (t->read ()) {
124                         XMLNode* root = t->root ();
125                         for (XMLNodeList::const_iterator i = root->children().begin(); i != root->children().end(); ++i) {
126                                 XMLProperty const * uri = (*i)->property (X_("uri"));
127                                 XMLProperty const * label = (*i)->property (X_("label"));
128                                 p.push_back (Plugin::PresetRecord (uri->value(), label->value(), true));
129                         }
130                 }
131         }
132         delete t;
133         return p;
134 }
135
136 WindowsVSTPluginInfo::WindowsVSTPluginInfo (_VSTInfo* nfo) : VSTPluginInfo (nfo)
137 {
138         type = ARDOUR::Windows_VST;
139 }
140