2 Copyright (C) 1999-2006 Paul Davis
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.
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.
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.
21 #include <cstdio> /* for snprintf, grrr */
24 #include <glib/gstdio.h> /* for g_stat() */
25 #include <glibmm/miscutils.h>
27 #include "pbd/failed_constructor.h"
28 #include "pbd/xml++.h"
29 #include "pbd/filesystem.h"
30 #include "pbd/file_utils.h"
32 #include "midi++/manager.h"
34 #include "ardour/ardour.h"
35 #include "ardour/rc_configuration.h"
36 #include "ardour/audio_diskstream.h"
37 #include "ardour/control_protocol_manager.h"
38 #include "ardour/filesystem_paths.h"
42 using namespace ARDOUR;
46 /* this is global so that we do not have to indirect through an object pointer
51 float speed_quietning = 0.251189; // -12dB reduction for ffwd or rewind
54 RCConfiguration::RCConfiguration ()
56 /* construct variables */
57 #undef CONFIG_VARIABLE
58 #undef CONFIG_VARIABLE_SPECIAL
59 #define CONFIG_VARIABLE(Type,var,name,value) var (name,value),
60 #define CONFIG_VARIABLE_SPECIAL(Type,var,name,value,mutator) var (name,value,mutator),
61 #include "ardour/rc_configuration_vars.h"
62 #undef CONFIG_VARIABLE
63 #undef CONFIG_VARIABLE_SPECIAL
64 _control_protocol_state (0)
69 RCConfiguration::~RCConfiguration ()
71 delete _control_protocol_state;
75 RCConfiguration::load_state ()
79 sys::path system_rc_file;
82 /* load system configuration first */
84 if (find_file_in_search_path (ardour_search_path() + system_config_search_path(),
85 "ardour_system.rc", system_rc_file) )
89 string rcfile = system_rc_file.to_string();
91 /* stupid XML Parser hates empty files */
93 if (g_stat (rcfile.c_str(), &statbuf)) {
97 if (statbuf.st_size != 0) {
98 info << string_compose (_("Loading system configuration file %1"), rcfile) << endl;
101 if (!tree.read (rcfile.c_str())) {
102 error << string_compose(_("%1: cannot read system configuration file \"%2\""), PROGRAM_NAME, rcfile) << endmsg;
106 if (set_state (*tree.root(), Stateful::current_state_version)) {
107 error << string_compose(_("%1: system configuration file \"%2\" not loaded successfully."), PROGRAM_NAME, rcfile) << endmsg;
111 error << string_compose (_("your system %1 configuration file is empty. This probably means that there as an error installing %1"), PROGRAM_NAME) << endmsg;
115 /* now load configuration file for user */
117 sys::path user_rc_file;
119 if (find_file_in_search_path (ardour_search_path() + user_config_directory(),
120 "ardour.rc", user_rc_file))
124 string rcfile = user_rc_file.to_string();
126 /* stupid XML parser hates empty files */
128 if (g_stat (rcfile.c_str(), &statbuf)) {
132 if (statbuf.st_size != 0) {
133 info << string_compose (_("Loading user configuration file %1"), rcfile) << endl;
136 if (!tree.read (rcfile)) {
137 error << string_compose(_("%1: cannot read configuration file \"%2\""), PROGRAM_NAME, rcfile) << endmsg;
141 if (set_state (*tree.root(), Stateful::current_state_version)) {
142 error << string_compose(_("%1: user configuration file \"%2\" not loaded successfully."), PROGRAM_NAME, rcfile) << endmsg;
146 warning << string_compose (_("your %1 configuration file is empty. This is not normal."), PROGRAM_NAME) << endmsg;
151 error << string_compose (_("%1: could not find configuration file (ardour.rc), canvas will look broken."), PROGRAM_NAME) << endmsg;
157 RCConfiguration::save_state()
161 sys::create_directories (user_config_directory ());
163 catch (const sys::filesystem_error& ex)
165 error << "Could not create user configuration directory" << endmsg;
169 sys::path rcfile_path(user_config_directory());
171 rcfile_path /= "ardour.rc";
172 const string rcfile = rcfile_path.to_string();
174 // this test seems bogus?
175 if (!rcfile.empty()) {
177 tree.set_root (&get_state());
178 if (!tree.write (rcfile.c_str())){
179 error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
188 RCConfiguration::add_instant_xml(XMLNode& node)
190 Stateful::add_instant_xml (node, user_config_directory ());
194 RCConfiguration::instant_xml(const string& node_name)
196 return Stateful::instant_xml (node_name, user_config_directory ());
201 RCConfiguration::get_state ()
204 LocaleGuard lg (X_("POSIX"));
206 root = new XMLNode("Ardour");
208 const MIDI::Manager::PortList& ports = MIDI::Manager::instance()->get_midi_ports();
210 for (MIDI::Manager::PortList::const_iterator i = ports.begin(); i != ports.end(); ++i) {
211 root->add_child_nocopy((*i)->get_state());
214 root->add_child_nocopy (get_variables ());
217 root->add_child_copy (*_extra_xml);
220 root->add_child_nocopy (ControlProtocolManager::instance().get_state());
226 RCConfiguration::get_variables ()
229 LocaleGuard lg (X_("POSIX"));
231 node = new XMLNode ("Config");
233 #undef CONFIG_VARIABLE
234 #undef CONFIG_VARIABLE_SPECIAL
235 #define CONFIG_VARIABLE(type,var,Name,value) \
236 var.add_to_node (*node);
237 #define CONFIG_VARIABLE_SPECIAL(type,var,Name,value,mutator) \
238 var.add_to_node (*node);
239 #include "ardour/rc_configuration_vars.h"
240 #undef CONFIG_VARIABLE
241 #undef CONFIG_VARIABLE_SPECIAL
247 RCConfiguration::set_state (const XMLNode& root, int /*version*/)
249 if (root.name() != "Ardour") {
253 XMLNodeList nlist = root.children();
254 XMLNodeConstIterator niter;
257 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
261 if (node->name() == "MIDI-port") {
265 MIDI::Port::Descriptor desc (*node);
266 map<string,XMLNode>::iterator x;
268 if ((x = midi_ports.find (desc.tag)) != midi_ports.end()) {
269 warning << string_compose (_("Duplicate MIDI port definition found (tag=\"%1\") - ignored"),
273 midi_ports.insert (pair<string,XMLNode>(desc.tag,*node));
276 catch (failed_constructor& err) {
277 warning << _("ill-formed MIDI port specification in ardour rcfile (ignored)") << endmsg;
280 } else if (node->name() == "Config") {
282 set_variables (*node);
284 } else if (node->name() == "Extra") {
285 _extra_xml = new XMLNode (*node);
287 } else if (node->name() == ControlProtocolManager::state_node_name) {
288 _control_protocol_state = new XMLNode (*node);
292 Diskstream::set_disk_io_chunk_frames (minimum_disk_io_bytes.get() / sizeof (Sample));
298 RCConfiguration::set_variables (const XMLNode& node)
300 #undef CONFIG_VARIABLE
301 #undef CONFIG_VARIABLE_SPECIAL
302 #define CONFIG_VARIABLE(type,var,name,value) \
303 if (var.set_from_node (node)) { \
304 ParameterChanged (name); \
306 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) \
307 if (var.set_from_node (node)) { \
308 ParameterChanged (name); \
311 #include "ardour/rc_configuration_vars.h"
312 #undef CONFIG_VARIABLE
313 #undef CONFIG_VARIABLE_SPECIAL
317 RCConfiguration::map_parameters (boost::function<void (std::string)>& functor)
319 #undef CONFIG_VARIABLE
320 #undef CONFIG_VARIABLE_SPECIAL
321 #define CONFIG_VARIABLE(type,var,name,value) functor (name);
322 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) functor (name);
323 #include "ardour/rc_configuration_vars.h"
324 #undef CONFIG_VARIABLE
325 #undef CONFIG_VARIABLE_SPECIAL