handle deletion of UI objects between the time that a callback is queued with the...
[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         delete _control_protocol_state;
72 }
73
74 int
75 RCConfiguration::load_state ()
76 {
77         bool found = false;
78
79         sys::path system_rc_file;
80         struct stat statbuf;
81
82         /* load system configuration first */
83
84         if (find_file_in_search_path (ardour_search_path() + system_config_search_path(),
85                         "ardour_system.rc", system_rc_file) )
86         {
87                 found = true;
88
89                 string rcfile = system_rc_file.to_string();
90
91                 /* stupid XML Parser hates empty files */
92
93                 if (g_stat (rcfile.c_str(), &statbuf)) {
94                         return -1;
95                 }
96
97                 if (statbuf.st_size != 0) {
98                         info << string_compose (_("Loading system configuration file %1"), rcfile) << endl;
99
100                         XMLTree tree;
101                         if (!tree.read (rcfile.c_str())) {
102                                 error << string_compose(_("%1: cannot read system configuration file \"%2\""), PROGRAM_NAME, rcfile) << endmsg;
103                                 return -1;
104                         }
105
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;
108                                 return -1;
109                         }
110                 } else {
111                         error << string_compose (_("your system %1 configuration file is empty. This probably means that there as an error installing %1"), PROGRAM_NAME) << endmsg;
112                 }
113         }
114
115         /* now load configuration file for user */
116
117         sys::path user_rc_file;
118
119         if (find_file_in_search_path (ardour_search_path() + user_config_directory(),
120                         "ardour.rc", user_rc_file))
121         {
122                 found = true;
123
124                 string rcfile = user_rc_file.to_string();
125
126                 /* stupid XML parser hates empty files */
127
128                 if (g_stat (rcfile.c_str(), &statbuf)) {
129                         return -1;
130                 }
131
132                 if (statbuf.st_size != 0) {
133                         info << string_compose (_("Loading user configuration file %1"), rcfile) << endl;
134
135                         XMLTree tree;
136                         if (!tree.read (rcfile)) {
137                                 error << string_compose(_("%1: cannot read configuration file \"%2\""), PROGRAM_NAME, rcfile) << endmsg;
138                                 return -1;
139                         }
140
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;
143                                 return -1;
144                         }
145                 } else {
146                         warning << string_compose (_("your %1 configuration file is empty. This is not normal."), PROGRAM_NAME) << endmsg;
147                 }
148         }
149
150         if (!found)
151                 error << string_compose (_("%1: could not find configuration file (ardour.rc), canvas will look broken."), PROGRAM_NAME) << endmsg;
152
153         return 0;
154 }
155
156 int
157 RCConfiguration::save_state()
158 {
159         try
160         {
161                 sys::create_directories (user_config_directory ());
162         }
163         catch (const sys::filesystem_error& ex)
164         {
165                 error << "Could not create user configuration directory" << endmsg;
166                 return -1;
167         }
168
169         sys::path rcfile_path(user_config_directory());
170
171         rcfile_path /= "ardour.rc";
172         const string rcfile = rcfile_path.to_string();
173
174         // this test seems bogus?
175         if (!rcfile.empty()) {
176                 XMLTree tree;
177                 tree.set_root (&get_state());
178                 if (!tree.write (rcfile.c_str())){
179                         error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
180                         return -1;
181                 }
182         }
183
184         return 0;
185 }
186
187 void
188 RCConfiguration::add_instant_xml(XMLNode& node)
189 {
190         Stateful::add_instant_xml (node, user_config_directory ());
191 }
192
193 XMLNode*
194 RCConfiguration::instant_xml(const string& node_name)
195 {
196         return Stateful::instant_xml (node_name, user_config_directory ());
197 }
198
199
200 XMLNode&
201 RCConfiguration::get_state ()
202 {
203         XMLNode* root;
204         LocaleGuard lg (X_("POSIX"));
205
206         root = new XMLNode("Ardour");
207
208         const MIDI::Manager::PortList& ports = MIDI::Manager::instance()->get_midi_ports();
209
210         for (MIDI::Manager::PortList::const_iterator i = ports.begin(); i != ports.end(); ++i) {
211                 root->add_child_nocopy((*i)->get_state());
212         }
213
214         root->add_child_nocopy (get_variables ());
215
216         if (_extra_xml) {
217                 root->add_child_copy (*_extra_xml);
218         }
219
220         root->add_child_nocopy (ControlProtocolManager::instance().get_state());
221
222         return *root;
223 }
224
225 XMLNode&
226 RCConfiguration::get_variables ()
227 {
228         XMLNode* node;
229         LocaleGuard lg (X_("POSIX"));
230
231         node = new XMLNode ("Config");
232
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
242
243         return *node;
244 }
245
246 int
247 RCConfiguration::set_state (const XMLNode& root, int /*version*/)
248 {
249         if (root.name() != "Ardour") {
250                 return -1;
251         }
252
253         XMLNodeList nlist = root.children();
254         XMLNodeConstIterator niter;
255         XMLNode *node;
256
257         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
258
259                 node = *niter;
260
261                 if (node->name() == "MIDI-port") {
262
263                         try {
264
265                                 MIDI::Port::Descriptor desc (*node);
266                                 map<string,XMLNode>::iterator x;
267                                 
268                                 if ((x = midi_ports.find (desc.tag)) != midi_ports.end()) {
269                                         warning << string_compose (_("Duplicate MIDI port definition found (tag=\"%1\") - ignored"),
270                                                                    desc.tag) << endmsg;
271                                         continue;
272                                 }
273                                 midi_ports.insert (pair<string,XMLNode>(desc.tag,*node));
274                         }
275
276                         catch (failed_constructor& err) {
277                                 warning << _("ill-formed MIDI port specification in ardour rcfile (ignored)") << endmsg;
278                         }
279
280                 } else if (node->name() == "Config") {
281
282                         set_variables (*node);
283
284                 } else if (node->name() == "Extra") {
285                         _extra_xml = new XMLNode (*node);
286
287                 } else if (node->name() == ControlProtocolManager::state_node_name) {
288                         _control_protocol_state = new XMLNode (*node);
289                 }
290         }
291
292         Diskstream::set_disk_io_chunk_frames (minimum_disk_io_bytes.get() / sizeof (Sample));
293
294         return 0;
295 }
296
297 void
298 RCConfiguration::set_variables (const XMLNode& node)
299 {
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);                  \
305         }
306 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) \
307         if (var.set_from_node (node)) {    \
308                 ParameterChanged (name);                     \
309         }
310
311 #include "ardour/rc_configuration_vars.h"
312 #undef  CONFIG_VARIABLE
313 #undef  CONFIG_VARIABLE_SPECIAL
314
315 }
316 void
317 RCConfiguration::map_parameters (boost::function<void (std::string)>& functor)
318 {
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
326 }