655477e5589bdc77238604a446427396597a6303
[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 #include <pbd/filesystem.h>
26
27 #include <ardour/ardour.h>
28 #include <ardour/configuration.h>
29 #include <ardour/audio_diskstream.h>
30 #include <ardour/control_protocol_manager.h>
31 #include <ardour/filesystem_paths.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 #undef  CANVAS_VARIABLE
59 #define CANVAS_VARIABLE(var,name) var (name),  // <-- is this really so bad?
60 #include "ardour/canvas_vars.h"
61 #undef  CANVAS_VARIABLE
62
63         current_owner (ConfigVariableBase::Default)
64 {
65         _control_protocol_state = 0;
66 }
67
68 Configuration::~Configuration ()
69 {
70 }
71
72 void
73 Configuration::set_current_owner (ConfigVariableBase::Owner owner)
74 {
75         current_owner = owner;
76 }
77
78 int
79 Configuration::load_state ()
80 {
81         bool found = false;
82
83         string rcfile;
84         
85         /* load system configuration first */
86
87         rcfile = find_config_file ("ardour_system.rc");
88
89         if (rcfile.length()) {
90
91                 XMLTree tree;
92                 found = true;
93
94                 cerr << string_compose (_("loading system configuration file %1"), rcfile) << endl;
95                 
96                 if (!tree.read (rcfile.c_str())) {
97                         error << string_compose(_("Ardour: cannot read system configuration file \"%1\""), rcfile) << endmsg;
98                         return -1;
99                 }
100
101                 current_owner = ConfigVariableBase::System;
102
103                 if (set_state (*tree.root())) {
104                         error << string_compose(_("Ardour: system configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
105                         return -1;
106                 }
107         }
108
109
110         /* now load configuration file for user */
111         
112         rcfile = find_config_file ("ardour.rc");
113
114         if (rcfile.length()) {
115
116                 XMLTree tree;
117                 found = true;
118                 
119                 cerr << string_compose (_("loading user configuration file %1"), rcfile) << endl;
120
121                 if (!tree.read (rcfile)) {
122                         error << string_compose(_("Ardour: cannot read configuration file \"%1\""), rcfile) << endmsg;
123                         return -1;
124                 }
125
126                 current_owner = ConfigVariableBase::Config;
127
128                 if (set_state (*tree.root())) {
129                         error << string_compose(_("Ardour: user configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
130                         return -1;
131                 }
132         }
133
134         if (!found)
135                 error << "Ardour: could not find configuration file (ardour.rc), canvas will look broken." << endmsg;
136
137         pack_canvasvars();
138         return 0;
139 }
140
141 int
142 Configuration::save_state()
143 {
144         XMLTree tree;
145         string rcfile;
146
147         rcfile = get_user_ardour_path ();
148         rcfile += "ardour.rc";
149
150         if (rcfile.length()) {
151                 tree.set_root (&get_state());
152                 if (!tree.write (rcfile.c_str())){
153                         error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
154                         return -1;
155                 }
156         }
157
158         return 0;
159 }
160
161 void
162 Configuration::add_instant_xml(XMLNode& node)
163 {
164         Stateful::add_instant_xml (node, user_config_directory ());
165 }
166
167 XMLNode*
168 Configuration::instant_xml(const string& node_name)
169 {
170         return Stateful::instant_xml (node_name, user_config_directory ());
171 }
172
173
174 bool
175 Configuration::save_config_options_predicate (ConfigVariableBase::Owner owner)
176 {
177         /* only save things that were in the config file to start with */
178         return owner & ConfigVariableBase::Config;
179 }
180
181 XMLNode&
182 Configuration::get_state ()
183 {
184         XMLNode* root;
185         LocaleGuard lg (X_("POSIX"));
186
187         root = new XMLNode("Ardour");
188         typedef map<string, MidiPortDescriptor*>::const_iterator CI;
189         for(CI m = midi_ports.begin(); m != midi_ports.end(); ++m){
190                 root->add_child_nocopy(m->second->get_state());
191         }
192         
193         root->add_child_nocopy (get_variables (sigc::mem_fun (*this, &Configuration::save_config_options_predicate), "Config"));
194         root->add_child_nocopy (get_variables (sigc::mem_fun (*this, &Configuration::save_config_options_predicate), "Canvas"));
195         
196         if (_extra_xml) {
197                 root->add_child_copy (*_extra_xml);
198         }
199         
200         root->add_child_nocopy (ControlProtocolManager::instance().get_state());
201         
202         return *root;
203 }
204
205 XMLNode&
206 Configuration::get_variables (sigc::slot<bool,ConfigVariableBase::Owner> predicate, std::string which_node)
207 {
208         XMLNode* node;
209         LocaleGuard lg (X_("POSIX"));
210
211         node = new XMLNode(which_node);
212
213 #undef  CONFIG_VARIABLE
214 #undef  CONFIG_VARIABLE_SPECIAL 
215 #define CONFIG_VARIABLE(type,var,Name,value) \
216          if (node->name() == "Config") { if (predicate (var.owner())) { var.add_to_node (*node); }}
217 #define CONFIG_VARIABLE_SPECIAL(type,var,Name,value,mutator) \
218          if (node->name() == "Config") { if (predicate (var.owner())) { var.add_to_node (*node); }}
219 #include "ardour/configuration_vars.h"
220 #undef  CONFIG_VARIABLE
221 #undef  CONFIG_VARIABLE_SPECIAL 
222
223 #undef  CANVAS_VARIABLE
224 #define CANVAS_VARIABLE(var,Name) if (node->name() == "Canvas") { if (predicate (ConfigVariableBase::Config)) { var.add_to_node (*node); }}
225 #include "ardour/canvas_vars.h"
226 #undef  CANVAS_VARIABLE
227
228         return *node;
229 }
230
231 int
232 Configuration::set_state (const XMLNode& root)
233 {
234         if (root.name() != "Ardour") {
235                 return -1;
236         }
237
238         XMLNodeList nlist = root.children();
239         XMLNodeConstIterator niter;
240         XMLNode *node;
241
242         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
243
244                 node = *niter;
245
246                 if (node->name() == "MIDI-port") {
247
248                         try {
249                                 pair<string,MidiPortDescriptor*> newpair;
250                                 newpair.second = new MidiPortDescriptor (*node);
251                                 newpair.first = newpair.second->tag;
252                                 midi_ports.insert (newpair);
253                         }
254
255                         catch (failed_constructor& err) {
256                                 warning << _("ill-formed MIDI port specification in ardour rcfile (ignored)") << endmsg;
257                         }
258
259                 } else if (node->name() == "Config" || node->name() == "Canvas" ) {
260                         
261                         set_variables (*node, ConfigVariableBase::Config);
262                         
263                 } else if (node->name() == "extra") {
264                         _extra_xml = new XMLNode (*node);
265
266                 } else if (node->name() == ControlProtocolManager::state_node_name) {
267                         _control_protocol_state = new XMLNode (*node);
268                 }
269         }
270
271         Diskstream::set_disk_io_chunk_frames (minimum_disk_io_bytes.get() / sizeof (Sample));
272
273         return 0;
274 }
275
276 void
277 Configuration::set_variables (const XMLNode& node, ConfigVariableBase::Owner owner)
278 {
279 #undef  CONFIG_VARIABLE
280 #undef  CONFIG_VARIABLE_SPECIAL 
281 #define CONFIG_VARIABLE(type,var,name,value) \
282          if (var.set_from_node (node, owner)) { \
283                  ParameterChanged (name); \
284          }
285 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) \
286          if (var.set_from_node (node, owner)) { \
287                  ParameterChanged (name); \
288          }
289
290 #include "ardour/configuration_vars.h"
291 #undef  CONFIG_VARIABLE
292 #undef  CONFIG_VARIABLE_SPECIAL 
293
294 #undef  CANVAS_VARIABLE
295 #define CANVAS_VARIABLE(var,name) \
296          if (var.set_from_node (node, owner)) { \
297                  ParameterChanged (name); \
298                  }
299 #include "ardour/canvas_vars.h"
300 #undef  CANVAS_VARIABLE
301         
302 }
303
304 void
305 Configuration::pack_canvasvars ()
306 {
307 #undef  CANVAS_VARIABLE
308 #define CANVAS_VARIABLE(var,name) canvas_colors.push_back(&var); 
309 #include "ardour/canvas_vars.h"
310 #undef  CANVAS_VARIABLE
311         cerr << "Configuration::pack_canvasvars () called, canvas_colors.size() = " << canvas_colors.size() << endl;
312         
313 }
314
315 Configuration::MidiPortDescriptor::MidiPortDescriptor (const XMLNode& node)
316 {
317         const XMLProperty *prop;
318         bool have_tag = false;
319         bool have_device = false;
320         bool have_type = false;
321         bool have_mode = false;
322
323         if ((prop = node.property ("tag")) != 0) {
324                 tag = prop->value();
325                 have_tag = true;
326         }
327
328         if ((prop = node.property ("device")) != 0) {
329                 device = prop->value();
330                 have_device = true;
331         }
332
333         if ((prop = node.property ("type")) != 0) {
334                 type = prop->value();
335                 have_type = true;
336         }
337
338         if ((prop = node.property ("mode")) != 0) {
339                 mode = prop->value();
340                 have_mode = true;
341         }
342
343         if (!have_tag || !have_device || !have_type || !have_mode) {
344                 throw failed_constructor();
345         }
346 }
347
348 XMLNode&
349 Configuration::MidiPortDescriptor::get_state()
350 {
351         XMLNode* root = new XMLNode("MIDI-port");
352
353         root->add_property("tag", tag);
354         root->add_property("device", device);
355         root->add_property("type", type);
356         root->add_property("mode", mode);
357
358         return *root;
359 }
360
361 void
362 Configuration::map_parameters (sigc::slot<void,const char*> theSlot)
363 {
364 #undef  CONFIG_VARIABLE
365 #undef  CONFIG_VARIABLE_SPECIAL 
366 #define CONFIG_VARIABLE(type,var,name,value)                 theSlot (name);
367 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) theSlot (name);
368 #include "ardour/configuration_vars.h"
369 #undef  CONFIG_VARIABLE
370 #undef  CONFIG_VARIABLE_SPECIAL 
371 }
372
373 bool ConfigVariableBase::show_stores = false;
374
375 void
376 ConfigVariableBase::set_show_stored_values (bool yn)
377 {
378         show_stores = yn;
379 }
380
381 void
382 ConfigVariableBase::show_stored_value (const string& str)
383 {
384         if (show_stores) {
385                 cerr << "Config variable " << _name << " stored as " << str << endl;
386         }
387 }
388
389 void
390 ConfigVariableBase::notify ()
391 {
392         // placeholder for any debugging desired when a config variable is modified
393 }
394
395 void
396 ConfigVariableBase::miss ()
397 {
398         // placeholder for any debugging desired when a config variable 
399         // is set but to the same value as it already has
400 }
401