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