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