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