1 /*
2 * Copyright (C) 1999-2018 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
4 * Copyright (C) 2009-2012 David Robillard <d@drobilla.net>
5 * Copyright (C) 2012-2016 Tim Mayberry <mojofunk@gmail.com>
6 * Copyright (C) 2015-2019 Robin Gareus <robin@gareus.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include <unistd.h>
24 #include <cstdio> /* for snprintf, grrr */
25
26 #include <glib.h>
27 #include "pbd/gstdio_compat.h"
28 #include <glibmm/miscutils.h>
29
30 #include "pbd/xml++.h"
31 #include "pbd/file_utils.h"
32 #include "pbd/replace_all.h"
33
34 #include "ardour/audioengine.h"
35 #include "ardour/disk_reader.h"
36 #include "ardour/disk_writer.h"
37 #include "ardour/control_protocol_manager.h"
38 #include "ardour/filename_extensions.h"
39 #include "ardour/filesystem_paths.h"
40 #include "ardour/port.h"
41 #include "ardour/rc_configuration.h"
42 #include "ardour/session_metadata.h"
43 #include "ardour/transport_master_manager.h"
44 #include "ardour/types_convert.h"
45
46 #include "pbd/i18n.h"
47
48 using namespace ARDOUR;
49 using namespace std;
50 using namespace PBD;
51
52 /* this is global so that we do not have to indirect through an object pointer
53 to reference it.
54 */
55
56 namespace ARDOUR {
57 float speed_quietning = 0.251189; // -12dB reduction for ffwd or rewind
58 }
59
60 static const char* user_config_file_name = "config";
61 static const char* system_config_file_name = "system_config";
62
RCConfiguration()63 RCConfiguration::RCConfiguration ()
64 :
65 /* construct variables */
66 #undef CONFIG_VARIABLE
67 #undef CONFIG_VARIABLE_SPECIAL
68 #define CONFIG_VARIABLE(Type,var,name,value) var (name,value),
69 #define CONFIG_VARIABLE_SPECIAL(Type,var,name,value,mutator) var (name,value,mutator),
70 #include "ardour/rc_configuration_vars.h"
71 #undef CONFIG_VARIABLE
72 #undef CONFIG_VARIABLE_SPECIAL
73 _control_protocol_state (0)
74 , _transport_master_state (0)
75 {
76 }
77
~RCConfiguration()78 RCConfiguration::~RCConfiguration ()
79 {
80 delete _control_protocol_state;
81 delete _transport_master_state;
82 }
83
84 int
load_state()85 RCConfiguration::load_state ()
86 {
87 std::string rcfile;
88 GStatBuf statbuf;
89
90 /* load system configuration first */
91
92 if (find_file (ardour_config_search_path(), system_config_file_name, rcfile)) {
93
94 /* stupid XML Parser hates empty files */
95
96 if (g_stat (rcfile.c_str(), &statbuf)) {
97 return -1;
98 }
99
100 if (statbuf.st_size != 0) {
101 info << string_compose (_("Loading system configuration file %1"), rcfile) << endmsg;
102
103 XMLTree tree;
104 if (!tree.read (rcfile.c_str())) {
105 error << string_compose(_("%1: cannot read system configuration file \"%2\""), PROGRAM_NAME, rcfile) << endmsg;
106 return -1;
107 }
108
109 if (set_state (*tree.root(), Stateful::current_state_version)) {
110 error << string_compose(_("%1: system configuration file \"%2\" not loaded successfully."), PROGRAM_NAME, rcfile) << endmsg;
111 return -1;
112 }
113 } else {
114 error << string_compose (_("Your system %1 configuration file is empty. This probably means that there was an error installing %1"), PROGRAM_NAME) << endmsg;
115 }
116 }
117
118 /* now load configuration file for user */
119
120 if (find_file (ardour_config_search_path(), user_config_file_name, rcfile)) {
121
122 /* stupid XML parser hates empty files */
123
124 if (g_stat (rcfile.c_str(), &statbuf)) {
125 return -1;
126 }
127
128 if (statbuf.st_size != 0) {
129 info << string_compose (_("Loading user configuration file %1"), rcfile) << endmsg;
130
131 XMLTree tree;
132 if (!tree.read (rcfile)) {
133 error << string_compose(_("%1: cannot read configuration file \"%2\""), PROGRAM_NAME, rcfile) << endmsg;
134 return -1;
135 }
136
137 if (set_state (*tree.root(), Stateful::current_state_version)) {
138 error << string_compose(_("%1: user configuration file \"%2\" not loaded successfully."), PROGRAM_NAME, rcfile) << endmsg;
139 return -1;
140 }
141 } else {
142 warning << string_compose (_("your %1 configuration file is empty. This is not normal."), PROGRAM_NAME) << endmsg;
143 }
144 }
145
146 return 0;
147 }
148
149 int
save_state()150 RCConfiguration::save_state()
151 {
152 const std::string rcfile = Glib::build_filename (user_config_directory(), user_config_file_name);
153 const std::string tmpfile = rcfile + temp_suffix;
154
155 XMLTree tree;
156 tree.set_root (&get_state());
157 if (!tree.write (tmpfile.c_str())){
158 error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
159 if (g_remove (tmpfile.c_str()) != 0) {
160 error << string_compose(_("Could not remove temporary config file at path \"%1\" (%2)"), tmpfile, g_strerror (errno)) << endmsg;
161 }
162 return -1;
163 }
164
165 if (::g_rename (tmpfile.c_str(), rcfile.c_str()) != 0) {
166 error << string_compose (_("Could not rename temporary config file %1 to %2 (%3)"), tmpfile, rcfile, g_strerror(errno)) << endmsg;
167 if (g_remove (tmpfile.c_str()) != 0) {
168 error << string_compose(_("Could not remove temporary config file at path \"%1\" (%2)"), tmpfile, g_strerror (errno)) << endmsg;
169 }
170 return -1;
171 }
172
173 return 0;
174 }
175
176 void
add_instant_xml(XMLNode & node)177 RCConfiguration::add_instant_xml(XMLNode& node)
178 {
179 Stateful::add_instant_xml (node, user_config_directory ());
180 }
181
182 XMLNode*
instant_xml(const string & node_name)183 RCConfiguration::instant_xml(const string& node_name)
184 {
185 return Stateful::instant_xml (node_name, user_config_directory ());
186 }
187
188
189 XMLNode&
get_state()190 RCConfiguration::get_state ()
191 {
192 XMLNode* root;
193
194 root = new XMLNode("Ardour");
195
196 root->add_child_nocopy (get_variables ());
197
198 root->add_child_nocopy (SessionMetadata::Metadata()->get_user_state());
199
200 if (_extra_xml) {
201 root->add_child_copy (*_extra_xml);
202 }
203
204 root->add_child_nocopy (ControlProtocolManager::instance().get_state());
205
206 if (TransportMasterManager::exists()) {
207 root->add_child_nocopy (TransportMasterManager::instance().get_state());
208 }
209
210 return *root;
211 }
212
213 XMLNode&
get_variables()214 RCConfiguration::get_variables ()
215 {
216 XMLNode* node;
217
218 node = new XMLNode ("Config");
219
220 #undef CONFIG_VARIABLE
221 #undef CONFIG_VARIABLE_SPECIAL
222 #define CONFIG_VARIABLE(type,var,Name,value) \
223 var.add_to_node (*node);
224 #define CONFIG_VARIABLE_SPECIAL(type,var,Name,value,mutator) \
225 var.add_to_node (*node);
226 #include "ardour/rc_configuration_vars.h"
227 #undef CONFIG_VARIABLE
228 #undef CONFIG_VARIABLE_SPECIAL
229
230 return *node;
231 }
232
233 int
set_state(const XMLNode & root,int version)234 RCConfiguration::set_state (const XMLNode& root, int version)
235 {
236 if (root.name() != "Ardour") {
237 return -1;
238 }
239
240 XMLNodeList nlist = root.children();
241 XMLNodeConstIterator niter;
242 XMLNode *node;
243
244 Stateful::save_extra_xml (root);
245
246 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
247
248 node = *niter;
249
250 if (node->name() == "Config") {
251 set_variables (*node);
252 } else if (node->name() == "Metadata") {
253 SessionMetadata::Metadata()->set_state (*node, version);
254 } else if (node->name() == ControlProtocolManager::state_node_name) {
255 _control_protocol_state = new XMLNode (*node);
256 } else if (node->name() == TransportMasterManager::state_node_name) {
257 _transport_master_state = new XMLNode (*node);
258 }
259 }
260
261 DiskReader::set_chunk_samples (minimum_disk_read_bytes.get() / sizeof (Sample));
262 DiskWriter::set_chunk_samples (minimum_disk_write_bytes.get() / sizeof (Sample));
263
264 return 0;
265 }
266
267 void
set_variables(const XMLNode & node)268 RCConfiguration::set_variables (const XMLNode& node)
269 {
270 #undef CONFIG_VARIABLE
271 #undef CONFIG_VARIABLE_SPECIAL
272 #define CONFIG_VARIABLE(type,var,name,value) \
273 if (var.set_from_node (node)) { \
274 ParameterChanged (name); \
275 }
276
277 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) \
278 if (var.set_from_node (node)) { \
279 ParameterChanged (name); \
280 }
281
282 #include "ardour/rc_configuration_vars.h"
283 #undef CONFIG_VARIABLE
284 #undef CONFIG_VARIABLE_SPECIAL
285
286 }
287 void
map_parameters(boost::function<void (std::string)> & functor)288 RCConfiguration::map_parameters (boost::function<void (std::string)>& functor)
289 {
290 #undef CONFIG_VARIABLE
291 #undef CONFIG_VARIABLE_SPECIAL
292 #define CONFIG_VARIABLE(type,var,name,value) functor (name);
293 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) functor (name);
294 #include "ardour/rc_configuration_vars.h"
295 #undef CONFIG_VARIABLE
296 #undef CONFIG_VARIABLE_SPECIAL
297 }
298
299