Fix broken whitespace. I'd apologize for the compile times if it was my fault :D
[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/failed_constructor.h"
28 #include "pbd/xml++.h"
29 #include "pbd/filesystem.h"
30 #include "pbd/file_utils.h"
31
32 #include "midi++/manager.h"
33
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"
39
40 #include "i18n.h"
41
42 using namespace ARDOUR;
43 using namespace std;
44 using namespace PBD;
45
46 /* this is global so that we do not have to indirect through an object pointer
47    to reference it.
48 */
49
50 namespace ARDOUR {
51     float speed_quietning = 0.251189; // -12dB reduction for ffwd or rewind
52 }
53
54 RCConfiguration::RCConfiguration ()
55         :
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)
65 {
66 }
67
68
69 RCConfiguration::~RCConfiguration ()
70 {
71         for (list<XMLNode*>::iterator i = _midi_port_states.begin(); i != _midi_port_states.end(); ++i) {
72                 delete *i;
73         }
74
75         delete _control_protocol_state;
76 }
77
78 int
79 RCConfiguration::load_state ()
80 {
81         bool found = false;
82
83         sys::path system_rc_file;
84         struct stat statbuf;
85
86         /* load system configuration first */
87
88         if (find_file_in_search_path (ardour_search_path() + system_config_search_path(),
89                         "ardour_system.rc", system_rc_file) )
90         {
91                 found = true;
92
93                 string rcfile = system_rc_file.to_string();
94
95                 /* stupid XML Parser hates empty files */
96
97                 if (g_stat (rcfile.c_str(), &statbuf)) {
98                         return -1;
99                 }
100
101                 if (statbuf.st_size != 0) {
102                         info << string_compose (_("Loading system configuration file %1"), rcfile) << endl;
103
104                         XMLTree tree;
105                         if (!tree.read (rcfile.c_str())) {
106                                 error << string_compose(_("%1: cannot read system configuration file \"%2\""), PROGRAM_NAME, rcfile) << endmsg;
107                                 return -1;
108                         }
109
110                         if (set_state (*tree.root(), Stateful::current_state_version)) {
111                                 error << string_compose(_("%1: system configuration file \"%2\" not loaded successfully."), PROGRAM_NAME, rcfile) << endmsg;
112                                 return -1;
113                         }
114                 } else {
115                         error << string_compose (_("your system %1 configuration file is empty. This probably means that there as an error installing %1"), PROGRAM_NAME) << endmsg;
116                 }
117         }
118
119         /* now load configuration file for user */
120
121         sys::path user_rc_file;
122
123         if (find_file_in_search_path (ardour_search_path() + user_config_directory(),
124                         "ardour.rc", user_rc_file))
125         {
126                 found = true;
127
128                 string rcfile = user_rc_file.to_string();
129
130                 /* stupid XML parser hates empty files */
131
132                 if (g_stat (rcfile.c_str(), &statbuf)) {
133                         return -1;
134                 }
135
136                 if (statbuf.st_size != 0) {
137                         info << string_compose (_("Loading user configuration file %1"), rcfile) << endl;
138
139                         XMLTree tree;
140                         if (!tree.read (rcfile)) {
141                                 error << string_compose(_("%1: cannot read configuration file \"%2\""), PROGRAM_NAME, rcfile) << endmsg;
142                                 return -1;
143                         }
144
145                         if (set_state (*tree.root(), Stateful::current_state_version)) {
146                                 error << string_compose(_("%1: user configuration file \"%2\" not loaded successfully."), PROGRAM_NAME, rcfile) << endmsg;
147                                 return -1;
148                         }
149                 } else {
150                         warning << string_compose (_("your %1 configuration file is empty. This is not normal."), PROGRAM_NAME) << endmsg;
151                 }
152         }
153
154         if (!found)
155                 error << string_compose (_("%1: could not find configuration file (ardour.rc), canvas will look broken."), PROGRAM_NAME) << endmsg;
156
157         return 0;
158 }
159
160 int
161 RCConfiguration::save_state()
162 {
163         try
164         {
165                 sys::create_directories (user_config_directory ());
166         }
167         catch (const sys::filesystem_error& ex)
168         {
169                 error << "Could not create user configuration directory" << endmsg;
170                 return -1;
171         }
172
173         sys::path rcfile_path(user_config_directory());
174
175         rcfile_path /= "ardour.rc";
176         const string rcfile = rcfile_path.to_string();
177
178         // this test seems bogus?
179         if (!rcfile.empty()) {
180                 XMLTree tree;
181                 tree.set_root (&get_state());
182                 if (!tree.write (rcfile.c_str())){
183                         error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
184                         return -1;
185                 }
186         }
187
188         return 0;
189 }
190
191 void
192 RCConfiguration::add_instant_xml(XMLNode& node)
193 {
194         Stateful::add_instant_xml (node, user_config_directory ());
195 }
196
197 XMLNode*
198 RCConfiguration::instant_xml(const string& node_name)
199 {
200         return Stateful::instant_xml (node_name, user_config_directory ());
201 }
202
203
204 XMLNode&
205 RCConfiguration::get_state ()
206 {
207         XMLNode* root;
208         LocaleGuard lg (X_("POSIX"));
209
210         root = new XMLNode("Ardour");
211
212         MIDI::Manager* mm = MIDI::Manager::instance();
213
214         if (mm) {
215                 const MIDI::Manager::PortList& ports = mm->get_midi_ports();
216
217                 for (MIDI::Manager::PortList::const_iterator i = ports.begin(); i != ports.end(); ++i) {
218                         root->add_child_nocopy((*i)->get_state());
219                 }
220         }
221
222         root->add_child_nocopy (get_variables ());
223
224         if (_extra_xml) {
225                 root->add_child_copy (*_extra_xml);
226         }
227
228         root->add_child_nocopy (ControlProtocolManager::instance().get_state());
229
230         return *root;
231 }
232
233 XMLNode&
234 RCConfiguration::get_variables ()
235 {
236         XMLNode* node;
237         LocaleGuard lg (X_("POSIX"));
238
239         node = new XMLNode ("Config");
240
241 #undef  CONFIG_VARIABLE
242 #undef  CONFIG_VARIABLE_SPECIAL
243 #define CONFIG_VARIABLE(type,var,Name,value) \
244         var.add_to_node (*node);
245 #define CONFIG_VARIABLE_SPECIAL(type,var,Name,value,mutator) \
246         var.add_to_node (*node);
247 #include "ardour/rc_configuration_vars.h"
248 #undef  CONFIG_VARIABLE
249 #undef  CONFIG_VARIABLE_SPECIAL
250
251         return *node;
252 }
253
254 int
255 RCConfiguration::set_state (const XMLNode& root, int /*version*/)
256 {
257         if (root.name() != "Ardour") {
258                 return -1;
259         }
260
261         XMLNodeList nlist = root.children();
262         XMLNodeConstIterator niter;
263         XMLNode *node;
264
265         for (list<XMLNode*>::iterator i = _midi_port_states.begin(); i != _midi_port_states.end(); ++i) {
266                 delete *i;
267         }
268
269         _midi_port_states.clear ();
270
271         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
272
273                 node = *niter;
274
275                 if (node->name() == "Config") {
276                         set_variables (*node);
277                 } else if (node->name() == "Extra") {
278                         _extra_xml = new XMLNode (*node);
279                 } else if (node->name() == ControlProtocolManager::state_node_name) {
280                         _control_protocol_state = new XMLNode (*node);
281                 } else if (node->name() == MIDI::Port::state_node_name) {
282                         _midi_port_states.push_back (new XMLNode (*node));
283                 }
284         }
285
286         Diskstream::set_disk_io_chunk_frames (minimum_disk_io_bytes.get() / sizeof (Sample));
287
288         return 0;
289 }
290
291 void
292 RCConfiguration::set_variables (const XMLNode& node)
293 {
294 #undef  CONFIG_VARIABLE
295 #undef  CONFIG_VARIABLE_SPECIAL
296 #define CONFIG_VARIABLE(type,var,name,value) \
297         if (var.set_from_node (node)) { \
298                 ParameterChanged (name);                  \
299         }
300 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) \
301         if (var.set_from_node (node)) {    \
302                 ParameterChanged (name);                     \
303         }
304
305 #include "ardour/rc_configuration_vars.h"
306 #undef  CONFIG_VARIABLE
307 #undef  CONFIG_VARIABLE_SPECIAL
308
309 }
310 void
311 RCConfiguration::map_parameters (boost::function<void (std::string)>& functor)
312 {
313 #undef  CONFIG_VARIABLE
314 #undef  CONFIG_VARIABLE_SPECIAL
315 #define CONFIG_VARIABLE(type,var,name,value)                 functor (name);
316 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) functor (name);
317 #include "ardour/rc_configuration_vars.h"
318 #undef  CONFIG_VARIABLE
319 #undef  CONFIG_VARIABLE_SPECIAL
320 }