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