Merge branch 'master' into audioengine
[ardour.git] / libs / ardour / rc_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 #include <glibmm/miscutils.h>
26
27 #include "pbd/xml++.h"
28 #include "pbd/file_utils.h"
29
30 #include "ardour/control_protocol_manager.h"
31 #include "ardour/diskstream.h"
32 #include "ardour/filesystem_paths.h"
33 #include "ardour/rc_configuration.h"
34 #include "ardour/session_metadata.h"
35 #include "ardour/midiport_manager.h"
36
37 #include "i18n.h"
38
39 using namespace ARDOUR;
40 using namespace std;
41 using namespace PBD;
42
43 /* this is global so that we do not have to indirect through an object pointer
44    to reference it.
45 */
46
47 namespace ARDOUR {
48     float speed_quietning = 0.251189; // -12dB reduction for ffwd or rewind
49 }
50
51 RCConfiguration::RCConfiguration ()
52         :
53 /* construct variables */
54 #undef  CONFIG_VARIABLE
55 #undef  CONFIG_VARIABLE_SPECIAL
56 #define CONFIG_VARIABLE(Type,var,name,value) var (name,value),
57 #define CONFIG_VARIABLE_SPECIAL(Type,var,name,value,mutator) var (name,value,mutator),
58 #include "ardour/rc_configuration_vars.h"
59 #undef  CONFIG_VARIABLE
60 #undef  CONFIG_VARIABLE_SPECIAL
61         _control_protocol_state (0)
62 {
63 }
64
65
66 RCConfiguration::~RCConfiguration ()
67 {
68         for (list<XMLNode*>::iterator i = _midi_port_states.begin(); i != _midi_port_states.end(); ++i) {
69                 delete *i;
70         }
71
72         delete _control_protocol_state;
73 }
74
75 int
76 RCConfiguration::load_state ()
77 {
78         std::string rcfile;
79         struct stat statbuf;
80
81         /* load system configuration first */
82
83         if (find_file_in_search_path (ardour_config_search_path(), "ardour_system.rc", rcfile)) {
84
85                 /* stupid XML Parser hates empty files */
86
87                 if (g_stat (rcfile.c_str(), &statbuf)) {
88                         return -1;
89                 }
90
91                 if (statbuf.st_size != 0) {
92                         info << string_compose (_("Loading system configuration file %1"), rcfile) << endl;
93
94                         XMLTree tree;
95                         if (!tree.read (rcfile.c_str())) {
96                                 error << string_compose(_("%1: cannot read system configuration file \"%2\""), PROGRAM_NAME, rcfile) << endmsg;
97                                 return -1;
98                         }
99
100                         if (set_state (*tree.root(), Stateful::current_state_version)) {
101                                 error << string_compose(_("%1: system configuration file \"%2\" not loaded successfully."), PROGRAM_NAME, rcfile) << endmsg;
102                                 return -1;
103                         }
104                 } else {
105                         error << string_compose (_("Your system %1 configuration file is empty. This probably means that there was an error installing %1"), PROGRAM_NAME) << endmsg;
106                 }
107         }
108
109         /* now load configuration file for user */
110
111         if (find_file_in_search_path (ardour_config_search_path(), "ardour.rc", rcfile)) {
112
113                 /* stupid XML parser hates empty files */
114
115                 if (g_stat (rcfile.c_str(), &statbuf)) {
116                         return -1;
117                 }
118
119                 if (statbuf.st_size != 0) {
120                         info << string_compose (_("Loading user configuration file %1"), rcfile) << endl;
121
122                         XMLTree tree;
123                         if (!tree.read (rcfile)) {
124                                 error << string_compose(_("%1: cannot read configuration file \"%2\""), PROGRAM_NAME, rcfile) << endmsg;
125                                 return -1;
126                         }
127
128                         if (set_state (*tree.root(), Stateful::current_state_version)) {
129                                 error << string_compose(_("%1: user configuration file \"%2\" not loaded successfully."), PROGRAM_NAME, rcfile) << endmsg;
130                                 return -1;
131                         }
132                 } else {
133                         warning << string_compose (_("your %1 configuration file is empty. This is not normal."), PROGRAM_NAME) << endmsg;
134                 }
135         }
136
137         return 0;
138 }
139
140 int
141 RCConfiguration::save_state()
142 {
143         const std::string rcfile = Glib::build_filename (user_config_directory(), "ardour.rc");
144
145         // this test seems bogus?
146         if (!rcfile.empty()) {
147                 XMLTree tree;
148                 tree.set_root (&get_state());
149                 if (!tree.write (rcfile.c_str())){
150                         error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
151                         return -1;
152                 }
153         }
154
155         return 0;
156 }
157
158 void
159 RCConfiguration::add_instant_xml(XMLNode& node)
160 {
161         Stateful::add_instant_xml (node, user_config_directory ());
162 }
163
164 XMLNode*
165 RCConfiguration::instant_xml(const string& node_name)
166 {
167         return Stateful::instant_xml (node_name, user_config_directory ());
168 }
169
170
171 XMLNode&
172 RCConfiguration::get_state ()
173 {
174         XMLNode* root;
175         LocaleGuard lg (X_("POSIX"));
176
177         root = new XMLNode("Ardour");
178
179         /* XXX 
180          * GET STATE OF MIDI::Port HERE
181          */
182 #if 0
183         MidiPortManager* mm = MidiPortManager::instance();
184
185         if (mm) {
186                 boost::shared_ptr<const MidiPortManager::PortList> ports = mm->get_midi_ports();
187
188                 for (MidiPortManager::PortList::const_iterator i = ports->begin(); i != ports->end(); ++i) {
189                         // root->add_child_nocopy ((*i)->get_state());
190                 }
191         }
192 #endif
193
194         root->add_child_nocopy (get_variables ());
195
196         root->add_child_nocopy (SessionMetadata::Metadata()->get_user_state());
197
198         if (_extra_xml) {
199                 root->add_child_copy (*_extra_xml);
200         }
201
202         root->add_child_nocopy (ControlProtocolManager::instance().get_state());
203
204         return *root;
205 }
206
207 XMLNode&
208 RCConfiguration::get_variables ()
209 {
210         XMLNode* node;
211         LocaleGuard lg (X_("POSIX"));
212
213         node = new XMLNode ("Config");
214
215 #undef  CONFIG_VARIABLE
216 #undef  CONFIG_VARIABLE_SPECIAL
217 #define CONFIG_VARIABLE(type,var,Name,value) \
218         var.add_to_node (*node);
219 #define CONFIG_VARIABLE_SPECIAL(type,var,Name,value,mutator) \
220         var.add_to_node (*node);
221 #include "ardour/rc_configuration_vars.h"
222 #undef  CONFIG_VARIABLE
223 #undef  CONFIG_VARIABLE_SPECIAL
224
225         return *node;
226 }
227
228 int
229 RCConfiguration::set_state (const XMLNode& root, int version)
230 {
231         if (root.name() != "Ardour") {
232                 return -1;
233         }
234
235         XMLNodeList nlist = root.children();
236         XMLNodeConstIterator niter;
237         XMLNode *node;
238
239         for (list<XMLNode*>::iterator i = _midi_port_states.begin(); i != _midi_port_states.end(); ++i) {
240                 delete *i;
241         }
242
243         _midi_port_states.clear ();
244
245         Stateful::save_extra_xml (root);
246
247         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
248
249                 node = *niter;
250
251                 if (node->name() == "Config") {
252                         set_variables (*node);
253                 } else if (node->name() == "Metadata") {
254                         SessionMetadata::Metadata()->set_state (*node, version);
255                 } else if (node->name() == ControlProtocolManager::state_node_name) {
256                         _control_protocol_state = new XMLNode (*node);
257                 } else if (node->name() == MIDI::Port::state_node_name) {
258                         _midi_port_states.push_back (new XMLNode (*node));
259                 }
260         }
261
262         Diskstream::set_disk_io_chunk_frames (minimum_disk_io_bytes.get() / sizeof (Sample));
263
264         return 0;
265 }
266
267 void
268 RCConfiguration::set_variables (const XMLNode& node)
269 {
270 #undef  CONFIG_VARIABLE
271 #undef  CONFIG_VARIABLE_SPECIAL
272 #define CONFIG_VARIABLE(type,var,name,value) \
273         if (var.set_from_node (node)) { \
274                 ParameterChanged (name);                  \
275         }
276 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) \
277         if (var.set_from_node (node)) {    \
278                 ParameterChanged (name);                     \
279         }
280
281 #include "ardour/rc_configuration_vars.h"
282 #undef  CONFIG_VARIABLE
283 #undef  CONFIG_VARIABLE_SPECIAL
284
285 }
286 void
287 RCConfiguration::map_parameters (boost::function<void (std::string)>& functor)
288 {
289 #undef  CONFIG_VARIABLE
290 #undef  CONFIG_VARIABLE_SPECIAL
291 #define CONFIG_VARIABLE(type,var,name,value)                 functor (name);
292 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) functor (name);
293 #include "ardour/rc_configuration_vars.h"
294 #undef  CONFIG_VARIABLE
295 #undef  CONFIG_VARIABLE_SPECIAL
296 }