display results of a bounce in the region list
[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     $Id$
19 */
20
21 #include <unistd.h>
22 #include <cstdio> /* for snprintf, grrr */
23
24 #include <pbd/failed_constructor.h>
25 #include <pbd/xml++.h>
26
27 #include <ardour/ardour.h>
28 #include <ardour/audio_library.h>
29 #include <ardour/configuration.h>
30 #include <ardour/audio_diskstream.h>
31 #include <ardour/destructive_filesource.h>
32 #include <ardour/control_protocol_manager.h>
33
34 #include "i18n.h"
35
36 using namespace ARDOUR;
37 using namespace std;
38 using namespace PBD;
39
40 /* this is global so that we do not have to indirect through an object pointer
41    to reference it.
42 */
43
44 namespace ARDOUR {
45     float speed_quietning = 0.251189; // -12dB reduction for ffwd or rewind
46 }
47
48 Configuration::Configuration ()
49         :
50 /* construct variables */
51 #undef  CONFIG_VARIABLE
52 #undef  CONFIG_VARIABLE_SPECIAL 
53 #define CONFIG_VARIABLE(Type,var,name,value) var (name,value),
54 #define CONFIG_VARIABLE_SPECIAL(Type,var,name,value,mutator) var (name,value,mutator),
55 #include "ardour/configuration_vars.h"
56 #undef  CONFIG_VARIABLE
57 #undef  CONFIG_VARIABLE_SPECIAL 
58
59         current_owner (ConfigVariableBase::Default)
60 {
61         _control_protocol_state = 0;
62 }
63
64 Configuration::~Configuration ()
65 {
66 }
67
68 void
69 Configuration::set_current_owner (ConfigVariableBase::Owner owner)
70 {
71         current_owner = owner;
72 }
73
74 int
75 Configuration::load_state ()
76 {
77         string rcfile;
78         
79         /* load system configuration first */
80
81         rcfile = find_config_file ("ardour_system.rc");
82
83         if (rcfile.length()) {
84
85                 XMLTree tree;
86
87                 cerr << string_compose (_("loading system configuration file %1"), rcfile) << endl;
88                 
89                 if (!tree.read (rcfile.c_str())) {
90                         error << string_compose(_("Ardour: cannot read system configuration file \"%1\""), rcfile) << endmsg;
91                         return -1;
92                 }
93
94                 current_owner = ConfigVariableBase::System;
95
96                 if (set_state (*tree.root())) {
97                         error << string_compose(_("Ardour: system configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
98                         return -1;
99                 }
100         }
101
102
103         /* now load configuration file for user */
104         
105         rcfile = find_config_file ("ardour.rc");
106
107         if (rcfile.length()) {
108
109                 XMLTree tree;
110                 
111                 cerr << string_compose (_("loading user configuration file %1"), rcfile) << endl;
112
113                 if (!tree.read (rcfile)) {
114                         error << string_compose(_("Ardour: cannot read configuration file \"%1\""), rcfile) << endmsg;
115                         return -1;
116                 }
117
118                 current_owner = ConfigVariableBase::Config;
119
120                 if (set_state (*tree.root())) {
121                         error << string_compose(_("Ardour: user configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
122                         return -1;
123                 }
124         }
125
126         return 0;
127 }
128
129 int
130 Configuration::save_state()
131 {
132         XMLTree tree;
133         string rcfile;
134
135         rcfile = get_user_ardour_path ();
136         rcfile += "ardour.rc";
137
138         if (rcfile.length()) {
139                 tree.set_root (&get_state());
140                 if (!tree.write (rcfile.c_str())){
141                         error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
142                         return -1;
143                 }
144         }
145
146         return 0;
147 }
148
149 bool
150 Configuration::save_config_options_predicate (ConfigVariableBase::Owner owner)
151 {
152         /* only save things that were in the config file to start with */
153         return owner & ConfigVariableBase::Config;
154 }
155
156 XMLNode&
157 Configuration::get_state ()
158 {
159         XMLNode* root;
160         LocaleGuard lg (X_("POSIX"));
161
162         root = new XMLNode("Ardour");
163         typedef map<string, MidiPortDescriptor*>::const_iterator CI;
164         for(CI m = midi_ports.begin(); m != midi_ports.end(); ++m){
165                 root->add_child_nocopy(m->second->get_state());
166         }
167         
168         root->add_child_nocopy (get_variables (sigc::mem_fun (*this, &Configuration::save_config_options_predicate)));
169         
170         if (_extra_xml) {
171                 root->add_child_copy (*_extra_xml);
172         }
173         
174         root->add_child_nocopy (ControlProtocolManager::instance().get_state());
175         root->add_child_nocopy (Library->get_state());
176         
177         return *root;
178 }
179
180 XMLNode&
181 Configuration::get_variables (sigc::slot<bool,ConfigVariableBase::Owner> predicate)
182 {
183         XMLNode* node;
184         LocaleGuard lg (X_("POSIX"));
185
186         node = new XMLNode("Config");
187
188 #undef  CONFIG_VARIABLE
189 #undef  CONFIG_VARIABLE_SPECIAL 
190 #define CONFIG_VARIABLE(type,var,name,value) \
191          if (predicate (var.owner())) { var.add_to_node (*node); }
192 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) \
193          if (predicate (var.owner())) { var.add_to_node (*node); }
194 #include "ardour/configuration_vars.h"
195 #undef  CONFIG_VARIABLE
196 #undef  CONFIG_VARIABLE_SPECIAL 
197         
198         return *node;
199 }
200
201 int
202 Configuration::set_state (const XMLNode& root)
203 {
204         if (root.name() != "Ardour") {
205                 return -1;
206         }
207
208         XMLNodeList nlist = root.children();
209         XMLNodeConstIterator niter;
210         XMLNode *node;
211
212         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
213
214                 node = *niter;
215
216                 if (node->name() == "MIDI-port") {
217
218                         try {
219                                 pair<string,MidiPortDescriptor*> newpair;
220                                 newpair.second = new MidiPortDescriptor (*node);
221                                 newpair.first = newpair.second->tag;
222                                 midi_ports.insert (newpair);
223                         }
224
225                         catch (failed_constructor& err) {
226                                 warning << _("ill-formed MIDI port specification in ardour rcfile (ignored)") << endmsg;
227                         }
228
229                 } else if (node->name() == "Config") {
230                         
231                         set_variables (*node, ConfigVariableBase::Config);
232                         
233                 } else if (node->name() == "extra") {
234                         _extra_xml = new XMLNode (*node);
235
236                 } else if (node->name() == ControlProtocolManager::state_node_name) {
237                         _control_protocol_state = new XMLNode (*node);
238                 } else if (node->name() == AudioLibrary::state_node_name) {
239                         Library->set_state (*node);
240                 }
241         }
242
243         Diskstream::set_disk_io_chunk_frames (minimum_disk_io_bytes.get() / sizeof (Sample));
244
245         return 0;
246 }
247
248 void
249 Configuration::set_variables (const XMLNode& node, ConfigVariableBase::Owner owner)
250 {
251 #undef  CONFIG_VARIABLE
252 #undef  CONFIG_VARIABLE_SPECIAL 
253 #define CONFIG_VARIABLE(type,var,name,value) \
254          if (var.set_from_node (node, owner)) { \
255                  ParameterChanged (name); \
256          }
257 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) \
258          if (var.set_from_node (node, owner)) { \
259                  ParameterChanged (name); \
260          }
261 #include "ardour/configuration_vars.h"
262 #undef  CONFIG_VARIABLE
263 #undef  CONFIG_VARIABLE_SPECIAL 
264
265 }
266
267 Configuration::MidiPortDescriptor::MidiPortDescriptor (const XMLNode& node)
268 {
269         const XMLProperty *prop;
270         bool have_tag = false;
271         bool have_device = false;
272         bool have_type = false;
273         bool have_mode = false;
274
275         if ((prop = node.property ("tag")) != 0) {
276                 tag = prop->value();
277                 have_tag = true;
278         }
279
280         if ((prop = node.property ("device")) != 0) {
281                 device = prop->value();
282                 have_device = true;
283         }
284
285         if ((prop = node.property ("type")) != 0) {
286                 type = prop->value();
287                 have_type = true;
288         }
289
290         if ((prop = node.property ("mode")) != 0) {
291                 mode = prop->value();
292                 have_mode = true;
293         }
294
295         if (!have_tag || !have_device || !have_type || !have_mode) {
296                 throw failed_constructor();
297         }
298 }
299
300 XMLNode&
301 Configuration::MidiPortDescriptor::get_state()
302 {
303         XMLNode* root = new XMLNode("MIDI-port");
304
305         root->add_property("tag", tag);
306         root->add_property("device", device);
307         root->add_property("type", type);
308         root->add_property("mode", mode);
309
310         return *root;
311 }
312
313 void
314 Configuration::map_parameters (sigc::slot<void,const char*> theSlot)
315 {
316 #undef  CONFIG_VARIABLE
317 #undef  CONFIG_VARIABLE_SPECIAL 
318 #define CONFIG_VARIABLE(type,var,name,value)                 theSlot (name);
319 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) theSlot (name);
320 #include "ardour/configuration_vars.h"
321 #undef  CONFIG_VARIABLE
322 #undef  CONFIG_VARIABLE_SPECIAL 
323 }