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