The great audio processing overhaul.
[ardour.git] / libs / ardour / 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 <glib/gstdio.h> /* for g_stat() */
25 #include <glibmm/miscutils.h>
26
27 #include "pbd/failed_constructor.h"
28 #include "pbd/xml++.h"
29 #include "pbd/filesystem.h"
30 #include "pbd/file_utils.h"
31
32 #include "midi++/manager.h"
33
34 #include "ardour/ardour.h"
35 #include "ardour/configuration.h"
36 #include "ardour/audio_diskstream.h"
37 #include "ardour/control_protocol_manager.h"
38 #include "ardour/filesystem_paths.h"
39
40 #include "i18n.h"
41
42 using namespace ARDOUR;
43 using namespace std;
44 using namespace PBD;
45
46 /* this is global so that we do not have to indirect through an object pointer
47    to reference it.
48 */
49
50 namespace ARDOUR {
51     float speed_quietning = 0.251189; // -12dB reduction for ffwd or rewind
52 }
53
54 Configuration::Configuration ()
55         :
56 /* construct variables */
57 #undef  CONFIG_VARIABLE
58 #undef  CONFIG_VARIABLE_SPECIAL 
59 #define CONFIG_VARIABLE(Type,var,name,value) var (name,value),
60 #define CONFIG_VARIABLE_SPECIAL(Type,var,name,value,mutator) var (name,value,mutator),
61 #include "ardour/configuration_vars.h"
62 #undef  CONFIG_VARIABLE
63 #undef  CONFIG_VARIABLE_SPECIAL
64
65
66         current_owner (ConfigVariableBase::Default)
67 {
68         _control_protocol_state = 0;
69 }
70
71 Configuration::~Configuration ()
72 {
73 }
74
75 void
76 Configuration::set_current_owner (ConfigVariableBase::Owner owner)
77 {
78         current_owner = owner;
79 }
80
81 int
82 Configuration::load_state ()
83 {
84         bool found = false;
85
86         sys::path system_rc_file;
87         struct stat statbuf;
88
89         /* load system configuration first */
90         
91         if (find_file_in_search_path (ardour_search_path() + system_config_search_path(),
92                         "ardour_system.rc", system_rc_file) )
93         {
94                 XMLTree tree;
95                 found = true;
96
97                 string rcfile = system_rc_file.to_string();
98
99                 /* stupid XML Parser hates empty files */
100                 
101                 if (g_stat (rcfile.c_str(), &statbuf)) {
102                         return -1;
103                 }
104
105                 if (statbuf.st_size != 0) {
106                         cerr << string_compose (_("Loading system configuration file %1"), rcfile) << endl;
107                         
108                         if (!tree.read (rcfile.c_str())) {
109                                 error << string_compose(_("Ardour: cannot read system configuration file \"%1\""), rcfile) << endmsg;
110                                 return -1;
111                         }
112                         
113                         current_owner = ConfigVariableBase::System;
114                         
115                         if (set_state (*tree.root())) {
116                                 error << string_compose(_("Ardour: system configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
117                                 return -1;
118                         }
119                 } else {
120                         error << _("your system Ardour configuration file is empty. This probably means that there as an error installing Ardour") << endmsg;
121                 }
122         }
123
124         /* now load configuration file for user */
125
126         sys::path user_rc_file;
127
128         if (find_file_in_search_path (ardour_search_path() + user_config_directory(),
129                         "ardour.rc", user_rc_file))
130         {
131                 XMLTree tree;
132                 found = true;
133
134                 string rcfile = user_rc_file.to_string();
135
136                 /* stupid XML parser hates empty files */
137
138                 if (g_stat (rcfile.c_str(), &statbuf)) {
139                         return -1;
140                 }
141
142                 if (statbuf.st_size != 0) {
143                         cerr << string_compose (_("Loading user configuration file %1"), rcfile) << endl;
144                         
145                         if (!tree.read (rcfile)) {
146                                 error << string_compose(_("Ardour: cannot read configuration file \"%1\""), rcfile) << endmsg;
147                                 return -1;
148                         }
149                         
150                         current_owner = ConfigVariableBase::Config;
151                         
152                         if (set_state (*tree.root())) {
153                                 error << string_compose(_("Ardour: user configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
154                                 return -1;
155                         }
156                 } else {
157                         warning << _("your Ardour configuration file is empty. This is not normal.") << endmsg;
158                 }       
159         }
160
161         if (!found)
162                 error << "Ardour: could not find configuration file (ardour.rc), canvas will look broken." << endmsg;
163
164         return 0;
165 }
166
167 int
168 Configuration::save_state()
169 {
170         XMLTree tree;
171
172         try
173         {
174                 sys::create_directories (user_config_directory ());
175         }
176         catch (const sys::filesystem_error& ex)
177         {
178                 error << "Could not create user configuration directory" << endmsg;
179                 return -1;
180         }
181         
182         sys::path rcfile_path(user_config_directory());
183
184         rcfile_path /= "ardour.rc";
185         const string rcfile = rcfile_path.to_string();
186
187         // this test seems bogus?
188         if (rcfile.length()) {
189                 tree.set_root (&get_state());
190                 if (!tree.write (rcfile.c_str())){
191                         error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
192                         return -1;
193                 }
194         }
195
196         return 0;
197 }
198
199 void
200 Configuration::add_instant_xml(XMLNode& node)
201 {
202         Stateful::add_instant_xml (node, user_config_directory ());
203 }
204
205 XMLNode*
206 Configuration::instant_xml(const string& node_name)
207 {
208         return Stateful::instant_xml (node_name, user_config_directory ());
209 }
210
211
212 bool
213 Configuration::save_config_options_predicate (ConfigVariableBase::Owner owner)
214 {
215         /* only save things that were in the config file to start with */
216         return owner & ConfigVariableBase::Config;
217 }
218
219 XMLNode&
220 Configuration::get_state ()
221 {
222         XMLNode* root;
223         LocaleGuard lg (X_("POSIX"));
224
225         root = new XMLNode("Ardour");
226
227         MIDI::Manager::PortMap::const_iterator i;
228         const MIDI::Manager::PortMap& ports = MIDI::Manager::instance()->get_midi_ports();
229         
230         for (i = ports.begin(); i != ports.end(); ++i) {
231                 root->add_child_nocopy(i->second->get_state());
232         }
233         
234         root->add_child_nocopy (get_variables (sigc::mem_fun (*this, &Configuration::save_config_options_predicate), "Config"));
235         
236         if (_extra_xml) {
237                 root->add_child_copy (*_extra_xml);
238         }
239         
240         root->add_child_nocopy (ControlProtocolManager::instance().get_state());
241         
242         return *root;
243 }
244
245 XMLNode&
246 Configuration::get_variables (sigc::slot<bool,ConfigVariableBase::Owner> predicate, std::string which_node)
247 {
248         XMLNode* node;
249         LocaleGuard lg (X_("POSIX"));
250
251         node = new XMLNode(which_node);
252
253 #undef  CONFIG_VARIABLE
254 #undef  CONFIG_VARIABLE_SPECIAL 
255 #define CONFIG_VARIABLE(type,var,Name,value) \
256          if (node->name() == "Config") { if (predicate (var.owner())) { var.add_to_node (*node); }}
257 #define CONFIG_VARIABLE_SPECIAL(type,var,Name,value,mutator) \
258          if (node->name() == "Config") { if (predicate (var.owner())) { var.add_to_node (*node); }}
259 #include "ardour/configuration_vars.h"
260 #undef  CONFIG_VARIABLE
261 #undef  CONFIG_VARIABLE_SPECIAL 
262
263         return *node;
264 }
265
266 int
267 Configuration::set_state (const XMLNode& root)
268 {
269         if (root.name() != "Ardour") {
270                 return -1;
271         }
272
273         XMLNodeList nlist = root.children();
274         XMLNodeConstIterator niter;
275         XMLNode *node;
276
277         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
278
279                 node = *niter;
280
281                 if (node->name() == "MIDI-port") {
282
283                         try {
284
285                                 MIDI::Port::Descriptor desc (*node);
286                                 map<string,XMLNode>::iterator x;
287                                 if ((x = midi_ports.find (desc.tag)) != midi_ports.end()) {
288                                         midi_ports.erase (x);
289                                 }
290                                 midi_ports.insert (pair<string,XMLNode>(desc.tag,*node));
291                         }
292
293                         catch (failed_constructor& err) {
294                                 warning << _("ill-formed MIDI port specification in ardour rcfile (ignored)") << endmsg;
295                         }
296
297                 } else if (node->name() == "Config") {
298                         
299                         set_variables (*node, ConfigVariableBase::Config);
300                         
301                 } else if (node->name() == "Extra") {
302                         _extra_xml = new XMLNode (*node);
303
304                 } else if (node->name() == ControlProtocolManager::state_node_name) {
305                         _control_protocol_state = new XMLNode (*node);
306                 }
307         }
308
309         Diskstream::set_disk_io_chunk_frames (minimum_disk_io_bytes.get() / sizeof (Sample));
310
311         return 0;
312 }
313
314 void
315 Configuration::set_variables (const XMLNode& node, ConfigVariableBase::Owner owner)
316 {
317 #undef  CONFIG_VARIABLE
318 #undef  CONFIG_VARIABLE_SPECIAL 
319 #define CONFIG_VARIABLE(type,var,name,value) \
320          if (var.set_from_node (node, owner)) { \
321                  ParameterChanged (name); \
322          }
323 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) \
324          if (var.set_from_node (node, owner)) { \
325                  ParameterChanged (name); \
326          }
327
328 #include "ardour/configuration_vars.h"
329 #undef  CONFIG_VARIABLE
330 #undef  CONFIG_VARIABLE_SPECIAL
331         
332 }
333 void
334 Configuration::map_parameters (sigc::slot<void,const char*> theSlot)
335 {
336 #undef  CONFIG_VARIABLE
337 #undef  CONFIG_VARIABLE_SPECIAL 
338 #define CONFIG_VARIABLE(type,var,name,value)                 theSlot (name);
339 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) theSlot (name);
340 #include "ardour/configuration_vars.h"
341 #undef  CONFIG_VARIABLE
342 #undef  CONFIG_VARIABLE_SPECIAL 
343 }
344
345 bool ConfigVariableBase::show_stores = false;
346
347 void
348 ConfigVariableBase::set_show_stored_values (bool yn)
349 {
350         show_stores = yn;
351 }
352
353 void
354 ConfigVariableBase::show_stored_value (const string& str)
355 {
356         if (show_stores) {
357                 cerr << "Config variable " << _name << " stored as " << str << endl;
358         }
359 }
360
361 void
362 ConfigVariableBase::notify ()
363 {
364         // placeholder for any debugging desired when a config variable is modified
365 }
366
367 void
368 ConfigVariableBase::miss ()
369 {
370         // placeholder for any debugging desired when a config variable 
371         // is set but to the same value as it already has
372 }
373