fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / rc_configuration.cc
1 /*
2     Copyright (C) 1999-2006 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 <unistd.h>
21 #include <cstdio> /* for snprintf, grrr */
22
23 #include <glib.h>
24 #include "pbd/gstdio_compat.h"
25 #include <glibmm/miscutils.h>
26
27 #include "pbd/xml++.h"
28 #include "pbd/file_utils.h"
29 #include "pbd/replace_all.h"
30
31 #include "ardour/audioengine.h"
32 #include "ardour/control_protocol_manager.h"
33 #include "ardour/diskstream.h"
34 #include "ardour/filesystem_paths.h"
35 #include "ardour/port.h"
36 #include "ardour/rc_configuration.h"
37 #include "ardour/session_metadata.h"
38
39 #include "pbd/i18n.h"
40
41 using namespace ARDOUR;
42 using namespace std;
43 using namespace PBD;
44
45 /* this is global so that we do not have to indirect through an object pointer
46    to reference it.
47 */
48
49 namespace ARDOUR {
50     float speed_quietning = 0.251189; // -12dB reduction for ffwd or rewind
51 }
52
53 static const char* user_config_file_name = "config";
54 static const char* system_config_file_name = "system_config";
55
56 RCConfiguration::RCConfiguration ()
57         :
58 /* construct variables */
59 #undef  CONFIG_VARIABLE
60 #undef  CONFIG_VARIABLE_SPECIAL
61 #define CONFIG_VARIABLE(Type,var,name,value) var (name,value),
62 #define CONFIG_VARIABLE_SPECIAL(Type,var,name,value,mutator) var (name,value,mutator),
63 #include "ardour/rc_configuration_vars.h"
64 #undef  CONFIG_VARIABLE
65 #undef  CONFIG_VARIABLE_SPECIAL
66         _control_protocol_state (0)
67 {
68 }
69
70 RCConfiguration::~RCConfiguration ()
71 {
72         delete _control_protocol_state;
73 }
74
75 int
76 RCConfiguration::load_state ()
77 {
78         std::string rcfile;
79         GStatBuf statbuf;
80
81         /* load system configuration first */
82
83         if (find_file (ardour_config_search_path(), system_config_file_name, rcfile)) {
84
85                 /* stupid XML Parser hates empty files */
86
87                 if (g_stat (rcfile.c_str(), &statbuf)) {
88                         return -1;
89                 }
90
91                 if (statbuf.st_size != 0) {
92                         info << string_compose (_("Loading system configuration file %1"), rcfile) << endmsg;
93
94                         XMLTree tree;
95                         if (!tree.read (rcfile.c_str())) {
96                                 error << string_compose(_("%1: cannot read system configuration file \"%2\""), PROGRAM_NAME, rcfile) << endmsg;
97                                 return -1;
98                         }
99
100                         if (set_state (*tree.root(), Stateful::current_state_version)) {
101                                 error << string_compose(_("%1: system configuration file \"%2\" not loaded successfully."), PROGRAM_NAME, rcfile) << endmsg;
102                                 return -1;
103                         }
104                 } else {
105                         error << string_compose (_("Your system %1 configuration file is empty. This probably means that there was an error installing %1"), PROGRAM_NAME) << endmsg;
106                 }
107         }
108
109         /* now load configuration file for user */
110
111         if (find_file (ardour_config_search_path(), user_config_file_name, rcfile)) {
112
113                 /* stupid XML parser hates empty files */
114
115                 if (g_stat (rcfile.c_str(), &statbuf)) {
116                         return -1;
117                 }
118
119                 if (statbuf.st_size != 0) {
120                         info << string_compose (_("Loading user configuration file %1"), rcfile) << endmsg;
121
122                         XMLTree tree;
123                         if (!tree.read (rcfile)) {
124                                 error << string_compose(_("%1: cannot read configuration file \"%2\""), PROGRAM_NAME, rcfile) << endmsg;
125                                 return -1;
126                         }
127
128                         if (set_state (*tree.root(), Stateful::current_state_version)) {
129                                 error << string_compose(_("%1: user configuration file \"%2\" not loaded successfully."), PROGRAM_NAME, rcfile) << endmsg;
130                                 return -1;
131                         }
132                 } else {
133                         warning << string_compose (_("your %1 configuration file is empty. This is not normal."), PROGRAM_NAME) << endmsg;
134                 }
135         }
136
137         return 0;
138 }
139
140 int
141 RCConfiguration::save_state()
142 {
143         const std::string rcfile = Glib::build_filename (user_config_directory(), user_config_file_name);
144
145         // this test seems bogus?
146         if (!rcfile.empty()) {
147                 XMLTree tree;
148                 tree.set_root (&get_state());
149                 if (!tree.write (rcfile.c_str())){
150                         error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
151                         return -1;
152                 }
153         }
154
155         return 0;
156 }
157
158 void
159 RCConfiguration::add_instant_xml(XMLNode& node)
160 {
161         Stateful::add_instant_xml (node, user_config_directory ());
162 }
163
164 XMLNode*
165 RCConfiguration::instant_xml(const string& node_name)
166 {
167         return Stateful::instant_xml (node_name, user_config_directory ());
168 }
169
170
171 XMLNode&
172 RCConfiguration::get_state ()
173 {
174         XMLNode* root;
175         LocaleGuard lg;
176
177         root = new XMLNode("Ardour");
178
179         root->add_child_nocopy (get_variables ());
180
181         root->add_child_nocopy (SessionMetadata::Metadata()->get_user_state());
182
183         if (_extra_xml) {
184                 root->add_child_copy (*_extra_xml);
185         }
186
187         root->add_child_nocopy (ControlProtocolManager::instance().get_state());
188
189         return *root;
190 }
191
192 XMLNode&
193 RCConfiguration::get_variables ()
194 {
195         XMLNode* node;
196         LocaleGuard lg;
197
198         node = new XMLNode ("Config");
199
200 #undef  CONFIG_VARIABLE
201 #undef  CONFIG_VARIABLE_SPECIAL
202 #define CONFIG_VARIABLE(type,var,Name,value) \
203         var.add_to_node (*node);
204 #define CONFIG_VARIABLE_SPECIAL(type,var,Name,value,mutator) \
205         var.add_to_node (*node);
206 #include "ardour/rc_configuration_vars.h"
207 #undef  CONFIG_VARIABLE
208 #undef  CONFIG_VARIABLE_SPECIAL
209
210         return *node;
211 }
212
213 int
214 RCConfiguration::set_state (const XMLNode& root, int version)
215 {
216         if (root.name() != "Ardour") {
217                 return -1;
218         }
219
220         XMLNodeList nlist = root.children();
221         XMLNodeConstIterator niter;
222         XMLNode *node;
223         LocaleGuard lg;
224
225         Stateful::save_extra_xml (root);
226
227         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
228
229                 node = *niter;
230
231                 if (node->name() == "Config") {
232                         set_variables (*node);
233                 } else if (node->name() == "Metadata") {
234                         SessionMetadata::Metadata()->set_state (*node, version);
235                 } else if (node->name() == ControlProtocolManager::state_node_name) {
236                         _control_protocol_state = new XMLNode (*node);
237                 }
238         }
239
240         Diskstream::set_disk_read_chunk_frames (minimum_disk_read_bytes.get() / sizeof (Sample));
241         Diskstream::set_disk_write_chunk_frames (minimum_disk_write_bytes.get() / sizeof (Sample));
242
243         return 0;
244 }
245
246 void
247 RCConfiguration::set_variables (const XMLNode& node)
248 {
249 #undef  CONFIG_VARIABLE
250 #undef  CONFIG_VARIABLE_SPECIAL
251 #define CONFIG_VARIABLE(type,var,name,value) \
252         if (var.set_from_node (node)) { \
253                 ParameterChanged (name);                  \
254         }
255 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) \
256         if (var.set_from_node (node)) {    \
257                 ParameterChanged (name);                     \
258         }
259
260 #include "ardour/rc_configuration_vars.h"
261 #undef  CONFIG_VARIABLE
262 #undef  CONFIG_VARIABLE_SPECIAL
263
264 }
265 void
266 RCConfiguration::map_parameters (boost::function<void (std::string)>& functor)
267 {
268 #undef  CONFIG_VARIABLE
269 #undef  CONFIG_VARIABLE_SPECIAL
270 #define CONFIG_VARIABLE(type,var,name,value)                 functor (name);
271 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) functor (name);
272 #include "ardour/rc_configuration_vars.h"
273 #undef  CONFIG_VARIABLE
274 #undef  CONFIG_VARIABLE_SPECIAL
275 }
276