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