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