Use PBD string conversion functions in PBD::ConfigurationVariable
[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 "pbd/gstdio_compat.h"
25 #include <glibmm/miscutils.h>
26
27 #include "pbd/xml++.h"
28 #include "pbd/file_utils.h"
29 #include "pbd/replace_all.h"
30
31 #include "ardour/audioengine.h"
32 #include "ardour/control_protocol_manager.h"
33 #include "ardour/diskstream.h"
34 #include "ardour/filesystem_paths.h"
35 #include "ardour/port.h"
36 #include "ardour/rc_configuration.h"
37 #include "ardour/session_metadata.h"
38 #include "ardour/types_convert.h"
39
40 #include "pbd/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 static const char* user_config_file_name = "config";
55 static const char* system_config_file_name = "system_config";
56
57 RCConfiguration::RCConfiguration ()
58         :
59 /* construct variables */
60 #undef  CONFIG_VARIABLE
61 #undef  CONFIG_VARIABLE_SPECIAL
62 #define CONFIG_VARIABLE(Type,var,name,value) var (name,value),
63 #define CONFIG_VARIABLE_SPECIAL(Type,var,name,value,mutator) var (name,value,mutator),
64 #include "ardour/rc_configuration_vars.h"
65 #undef  CONFIG_VARIABLE
66 #undef  CONFIG_VARIABLE_SPECIAL
67         _control_protocol_state (0)
68 {
69 }
70
71 RCConfiguration::~RCConfiguration ()
72 {
73         delete _control_protocol_state;
74 }
75
76 int
77 RCConfiguration::load_state ()
78 {
79         std::string rcfile;
80         GStatBuf statbuf;
81
82         /* load system configuration first */
83
84         if (find_file (ardour_config_search_path(), system_config_file_name, rcfile)) {
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                         info << string_compose (_("Loading system configuration file %1"), rcfile) << endmsg;
94
95                         XMLTree tree;
96                         if (!tree.read (rcfile.c_str())) {
97                                 error << string_compose(_("%1: cannot read system configuration file \"%2\""), PROGRAM_NAME, rcfile) << endmsg;
98                                 return -1;
99                         }
100
101                         if (set_state (*tree.root(), Stateful::current_state_version)) {
102                                 error << string_compose(_("%1: system configuration file \"%2\" not loaded successfully."), PROGRAM_NAME, rcfile) << endmsg;
103                                 return -1;
104                         }
105                 } else {
106                         error << string_compose (_("Your system %1 configuration file is empty. This probably means that there was an error installing %1"), PROGRAM_NAME) << endmsg;
107                 }
108         }
109
110         /* now load configuration file for user */
111
112         if (find_file (ardour_config_search_path(), user_config_file_name, rcfile)) {
113
114                 /* stupid XML parser hates empty files */
115
116                 if (g_stat (rcfile.c_str(), &statbuf)) {
117                         return -1;
118                 }
119
120                 if (statbuf.st_size != 0) {
121                         info << string_compose (_("Loading user configuration file %1"), rcfile) << endmsg;
122
123                         XMLTree tree;
124                         if (!tree.read (rcfile)) {
125                                 error << string_compose(_("%1: cannot read configuration file \"%2\""), PROGRAM_NAME, rcfile) << endmsg;
126                                 return -1;
127                         }
128
129                         if (set_state (*tree.root(), Stateful::current_state_version)) {
130                                 error << string_compose(_("%1: user configuration file \"%2\" not loaded successfully."), PROGRAM_NAME, rcfile) << endmsg;
131                                 return -1;
132                         }
133                 } else {
134                         warning << string_compose (_("your %1 configuration file is empty. This is not normal."), PROGRAM_NAME) << endmsg;
135                 }
136         }
137
138         return 0;
139 }
140
141 int
142 RCConfiguration::save_state()
143 {
144         const std::string rcfile = Glib::build_filename (user_config_directory(), user_config_file_name);
145
146         // this test seems bogus?
147         if (!rcfile.empty()) {
148                 XMLTree tree;
149                 tree.set_root (&get_state());
150                 if (!tree.write (rcfile.c_str())){
151                         error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
152                         return -1;
153                 }
154         }
155
156         return 0;
157 }
158
159 void
160 RCConfiguration::add_instant_xml(XMLNode& node)
161 {
162         Stateful::add_instant_xml (node, user_config_directory ());
163 }
164
165 XMLNode*
166 RCConfiguration::instant_xml(const string& node_name)
167 {
168         return Stateful::instant_xml (node_name, user_config_directory ());
169 }
170
171
172 XMLNode&
173 RCConfiguration::get_state ()
174 {
175         XMLNode* root;
176         LocaleGuard lg;
177
178         root = new XMLNode("Ardour");
179
180         root->add_child_nocopy (get_variables ());
181
182         root->add_child_nocopy (SessionMetadata::Metadata()->get_user_state());
183
184         if (_extra_xml) {
185                 root->add_child_copy (*_extra_xml);
186         }
187
188         root->add_child_nocopy (ControlProtocolManager::instance().get_state());
189
190         return *root;
191 }
192
193 XMLNode&
194 RCConfiguration::get_variables ()
195 {
196         XMLNode* node;
197         LocaleGuard lg;
198
199         node = new XMLNode ("Config");
200
201 #undef  CONFIG_VARIABLE
202 #undef  CONFIG_VARIABLE_SPECIAL
203 #define CONFIG_VARIABLE(type,var,Name,value) \
204         var.add_to_node (*node);
205 #define CONFIG_VARIABLE_SPECIAL(type,var,Name,value,mutator) \
206         var.add_to_node (*node);
207 #include "ardour/rc_configuration_vars.h"
208 #undef  CONFIG_VARIABLE
209 #undef  CONFIG_VARIABLE_SPECIAL
210
211         return *node;
212 }
213
214 int
215 RCConfiguration::set_state (const XMLNode& root, int version)
216 {
217         if (root.name() != "Ardour") {
218                 return -1;
219         }
220
221         XMLNodeList nlist = root.children();
222         XMLNodeConstIterator niter;
223         XMLNode *node;
224         LocaleGuard lg;
225
226         Stateful::save_extra_xml (root);
227
228         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
229
230                 node = *niter;
231
232                 if (node->name() == "Config") {
233                         set_variables (*node);
234                 } else if (node->name() == "Metadata") {
235                         SessionMetadata::Metadata()->set_state (*node, version);
236                 } else if (node->name() == ControlProtocolManager::state_node_name) {
237                         _control_protocol_state = new XMLNode (*node);
238                 }
239         }
240
241         Diskstream::set_disk_read_chunk_frames (minimum_disk_read_bytes.get() / sizeof (Sample));
242         Diskstream::set_disk_write_chunk_frames (minimum_disk_write_bytes.get() / sizeof (Sample));
243
244         return 0;
245 }
246
247 void
248 RCConfiguration::set_variables (const XMLNode& node)
249 {
250 #undef  CONFIG_VARIABLE
251 #undef  CONFIG_VARIABLE_SPECIAL
252 #define CONFIG_VARIABLE(type,var,name,value) \
253         if (var.set_from_node (node)) { \
254                 ParameterChanged (name);                  \
255         }
256 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) \
257         if (var.set_from_node (node)) {    \
258                 ParameterChanged (name);                     \
259         }
260
261 #include "ardour/rc_configuration_vars.h"
262 #undef  CONFIG_VARIABLE
263 #undef  CONFIG_VARIABLE_SPECIAL
264
265 }
266 void
267 RCConfiguration::map_parameters (boost::function<void (std::string)>& functor)
268 {
269 #undef  CONFIG_VARIABLE
270 #undef  CONFIG_VARIABLE_SPECIAL
271 #define CONFIG_VARIABLE(type,var,name,value)                 functor (name);
272 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) functor (name);
273 #include "ardour/rc_configuration_vars.h"
274 #undef  CONFIG_VARIABLE
275 #undef  CONFIG_VARIABLE_SPECIAL
276 }
277