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