- Changed IO's vector<Port*>'s to PortList
[ardour.git] / libs / ardour / 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     $Id$
19 */
20
21 #include <unistd.h>
22 #include <cstdio> /* for snprintf, grrr */
23
24 #include <pbd/failed_constructor.h>
25 #include <pbd/xml++.h>
26
27 #include <ardour/ardour.h>
28 #include <ardour/audio_library.h>
29 #include <ardour/configuration.h>
30 #include <ardour/audio_diskstream.h>
31 #include <ardour/destructive_filesource.h>
32 #include <ardour/control_protocol_manager.h>
33
34 #include "i18n.h"
35
36 using namespace ARDOUR;
37 using namespace std;
38 using namespace PBD;
39
40 /* this is global so that we do not have to indirect through an object pointer
41    to reference it.
42 */
43
44 namespace ARDOUR {
45     float speed_quietning = 0.251189; // -12dB reduction for ffwd or rewind
46 }
47
48 Configuration::Configuration ()
49         :
50 /* construct variables */
51 #undef  CONFIG_VARIABLE
52 #undef  CONFIG_VARIABLE_SPECIAL 
53 #define CONFIG_VARIABLE(Type,var,name,value) var (name,value),
54 #define CONFIG_VARIABLE_SPECIAL(Type,var,name,value,mutator) var (name,value,mutator),
55 #include "ardour/configuration_vars.h"
56 #undef  CONFIG_VARIABLE
57 #undef  CONFIG_VARIABLE_SPECIAL 
58
59         user_configuration (false)
60 {
61         _control_protocol_state = 0;
62 }
63
64 Configuration::~Configuration ()
65 {
66 }
67
68 int
69 Configuration::load_state ()
70 {
71         string rcfile;
72         
73         /* load system configuration first */
74
75         rcfile = find_config_file ("ardour_system.rc");
76
77         if (rcfile.length()) {
78
79                 XMLTree tree;
80
81                 cerr << string_compose (_("loading system configuration file %1"), rcfile) << endl;
82                 
83                 if (!tree.read (rcfile.c_str())) {
84                         error << string_compose(_("Ardour: cannot read system configuration file \"%1\""), rcfile) << endmsg;
85                         return -1;
86                 }
87
88                 if (set_state (*tree.root())) {
89                         error << string_compose(_("Ardour: system configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
90                         return -1;
91                 }
92         }
93
94         /* from this point on, all configuration changes are user driven */
95
96         user_configuration = true;
97
98         /* now load configuration file for user */
99         
100         rcfile = find_config_file ("ardour.rc");
101
102         if (rcfile.length()) {
103
104                 XMLTree tree;
105                 
106                 cerr << string_compose (_("loading user configuration file %1"), rcfile) << endl;
107
108                 if (!tree.read (rcfile)) {
109                         error << string_compose(_("Ardour: cannot read configuration file \"%1\""), rcfile) << endmsg;
110                         return -1;
111                 }
112
113                 if (set_state (*tree.root())) {
114                         error << string_compose(_("Ardour: user configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
115                         return -1;
116                 }
117         }
118
119         return 0;
120 }
121
122 int
123 Configuration::save_state()
124 {
125         XMLTree tree;
126         string rcfile;
127
128         /* Note: this only writes the per-user file, and therefore
129            only saves variables marked as user-set or modified
130         */
131
132         rcfile = get_user_ardour_path ();
133         rcfile += "ardour.rc";
134
135         if (rcfile.length()) {
136                 tree.set_root (&state (true));
137                 if (!tree.write (rcfile.c_str())){
138                         error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
139                         return -1;
140                 }
141         }
142
143         return 0;
144 }
145
146 XMLNode&
147 Configuration::get_state ()
148 {
149         return state (false);
150 }
151
152 XMLNode&
153 Configuration::state (bool user_only)
154 {
155         XMLNode* root = new XMLNode("Ardour");
156         LocaleGuard lg (X_("POSIX"));
157
158         typedef map<string, MidiPortDescriptor*>::const_iterator CI;
159         for(CI m = midi_ports.begin(); m != midi_ports.end(); ++m){
160                 root->add_child_nocopy(m->second->get_state());
161         }
162
163         XMLNode* node = new XMLNode("Config");
164         
165 #undef  CONFIG_VARIABLE
166 #undef  CONFIG_VARIABLE_SPECIAL 
167 #define CONFIG_VARIABLE(type,var,name,value) \
168          if (!user_only || var.is_user()) var.add_to_node (*node);
169 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) \
170          if (!user_only || var.is_user()) var.add_to_node (*node);
171 #include "ardour/configuration_vars.h"
172 #undef  CONFIG_VARIABLE
173 #undef  CONFIG_VARIABLE_SPECIAL 
174
175         root->add_child_nocopy (*node);
176
177         if (_extra_xml) {
178                 root->add_child_copy (*_extra_xml);
179         }
180
181         root->add_child_nocopy (ControlProtocolManager::instance().get_state());
182         root->add_child_nocopy (Library->get_state());
183
184         return *root;
185 }
186
187 int
188 Configuration::set_state (const XMLNode& root)
189 {
190         if (root.name() != "Ardour") {
191                 return -1;
192         }
193
194         XMLNodeList nlist = root.children();
195         XMLNodeConstIterator niter;
196         XMLNode *node;
197
198         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
199
200                 node = *niter;
201
202                 if (node->name() == "MIDI-port") {
203
204                         try {
205                                 pair<string,MidiPortDescriptor*> newpair;
206                                 newpair.second = new MidiPortDescriptor (*node);
207                                 newpair.first = newpair.second->tag;
208                                 midi_ports.insert (newpair);
209                         }
210
211                         catch (failed_constructor& err) {
212                                 warning << _("ill-formed MIDI port specification in ardour rcfile (ignored)") << endmsg;
213                         }
214
215                 } else if (node->name() == "Config") {
216
217 #undef  CONFIG_VARIABLE
218 #undef  CONFIG_VARIABLE_SPECIAL 
219 #define CONFIG_VARIABLE(type,var,name,value) \
220          var.set_from_node (*node); \
221          var.set_is_user (user_configuration);
222 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) \
223          var.set_from_node (*node); \
224          var.set_is_user (user_configuration);
225 #include "ardour/configuration_vars.h"
226 #undef  CONFIG_VARIABLE
227 #undef  CONFIG_VARIABLE_SPECIAL 
228                         
229                 } else if (node->name() == "extra") {
230                         _extra_xml = new XMLNode (*node);
231
232                 } else if (node->name() == ControlProtocolManager::state_node_name) {
233                         _control_protocol_state = new XMLNode (*node);
234                 } else if (node->name() == AudioLibrary::state_node_name) {
235                         Library->set_state (*node);
236                 }
237         }
238
239         Diskstream::set_disk_io_chunk_frames (minimum_disk_io_bytes.get() / sizeof (Sample));
240
241         return 0;
242 }
243
244 Configuration::MidiPortDescriptor::MidiPortDescriptor (const XMLNode& node)
245 {
246         const XMLProperty *prop;
247         bool have_tag = false;
248         bool have_device = false;
249         bool have_type = false;
250         bool have_mode = false;
251
252         if ((prop = node.property ("tag")) != 0) {
253                 tag = prop->value();
254                 have_tag = true;
255         }
256
257         if ((prop = node.property ("device")) != 0) {
258                 device = prop->value();
259                 have_device = true;
260         }
261
262         if ((prop = node.property ("type")) != 0) {
263                 type = prop->value();
264                 have_type = true;
265         }
266
267         if ((prop = node.property ("mode")) != 0) {
268                 mode = prop->value();
269                 have_mode = true;
270         }
271
272         if (!have_tag || !have_device || !have_type || !have_mode) {
273                 throw failed_constructor();
274         }
275 }
276
277 XMLNode&
278 Configuration::MidiPortDescriptor::get_state()
279 {
280         XMLNode* root = new XMLNode("MIDI-port");
281
282         root->add_property("tag", tag);
283         root->add_property("device", device);
284         root->add_property("type", type);
285         root->add_property("mode", mode);
286
287         return *root;
288 }
289