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