ardour
rc_configuration.cc
Go to the documentation of this file.
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/file_utils.h"
29 
30 #include "ardour/audioengine.h"
32 #include "ardour/diskstream.h"
34 #include "ardour/port.h"
37 
38 #include "i18n.h"
39 
40 using namespace ARDOUR;
41 using namespace std;
42 using namespace PBD;
43 
44 /* this is global so that we do not have to indirect through an object pointer
45  to reference it.
46 */
47 
48 namespace ARDOUR {
49  float speed_quietning = 0.251189; // -12dB reduction for ffwd or rewind
50 }
51 
52 static const char* user_config_file_name = "config";
53 static const char* system_config_file_name = "system_config";
54 
56  :
57 /* construct variables */
58 #undef CONFIG_VARIABLE
60 #define CONFIG_VARIABLE(Type,var,name,value) var (name,value),
61 #define CONFIG_VARIABLE_SPECIAL(Type,var,name,value,mutator) var (name,value,mutator),
62 #include "ardour/rc_configuration_vars.h"
63 #undef CONFIG_VARIABLE
65  _control_protocol_state (0)
66 {
67 }
68 
70 {
72 }
73 
74 int
76 {
77  std::string rcfile;
78  GStatBuf statbuf;
79 
80  /* load system configuration first */
81 
83 
84  /* stupid XML Parser hates empty files */
85 
86  if (g_stat (rcfile.c_str(), &statbuf)) {
87  return -1;
88  }
89 
90  if (statbuf.st_size != 0) {
91  info << string_compose (_("Loading system configuration file %1"), rcfile) << endl;
92 
93  XMLTree tree;
94  if (!tree.read (rcfile.c_str())) {
95  error << string_compose(_("%1: cannot read system configuration file \"%2\""), PROGRAM_NAME, rcfile) << endmsg;
96  return -1;
97  }
98 
100  error << string_compose(_("%1: system configuration file \"%2\" not loaded successfully."), PROGRAM_NAME, rcfile) << endmsg;
101  return -1;
102  }
103  } else {
104  error << string_compose (_("Your system %1 configuration file is empty. This probably means that there was an error installing %1"), PROGRAM_NAME) << endmsg;
105  }
106  }
107 
108  /* now load configuration file for user */
109 
111 
112  /* stupid XML parser hates empty files */
113 
114  if (g_stat (rcfile.c_str(), &statbuf)) {
115  return -1;
116  }
117 
118  if (statbuf.st_size != 0) {
119  info << string_compose (_("Loading user configuration file %1"), rcfile) << endl;
120 
121  XMLTree tree;
122  if (!tree.read (rcfile)) {
123  error << string_compose(_("%1: cannot read configuration file \"%2\""), PROGRAM_NAME, rcfile) << endmsg;
124  return -1;
125  }
126 
128  error << string_compose(_("%1: user configuration file \"%2\" not loaded successfully."), PROGRAM_NAME, rcfile) << endmsg;
129  return -1;
130  }
131  } else {
132  warning << string_compose (_("your %1 configuration file is empty. This is not normal."), PROGRAM_NAME) << endmsg;
133  }
134  }
135 
136  return 0;
137 }
138 
139 int
141 {
142  const std::string rcfile = Glib::build_filename (user_config_directory(), user_config_file_name);
143 
144  // this test seems bogus?
145  if (!rcfile.empty()) {
146  XMLTree tree;
147  tree.set_root (&get_state());
148  if (!tree.write (rcfile.c_str())){
149  error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
150  return -1;
151  }
152  }
153 
154  return 0;
155 }
156 
157 void
159 {
161 }
162 
163 XMLNode*
164 RCConfiguration::instant_xml(const string& node_name)
165 {
166  return Stateful::instant_xml (node_name, user_config_directory ());
167 }
168 
169 
170 XMLNode&
172 {
173  XMLNode* root;
174  LocaleGuard lg (X_("C"));
175 
176  root = new XMLNode("Ardour");
177 
178  root->add_child_nocopy (get_variables ());
179 
180  root->add_child_nocopy (SessionMetadata::Metadata()->get_user_state());
181 
182  if (_extra_xml) {
183  root->add_child_copy (*_extra_xml);
184  }
185 
187 
188  return *root;
189 }
190 
191 XMLNode&
193 {
194  XMLNode* node;
195  LocaleGuard lg (X_("C"));
196 
197  node = new XMLNode ("Config");
198 
199 #undef CONFIG_VARIABLE
200 #undef CONFIG_VARIABLE_SPECIAL
201 #define CONFIG_VARIABLE(type,var,Name,value) \
202  var.add_to_node (*node);
203 #define CONFIG_VARIABLE_SPECIAL(type,var,Name,value,mutator) \
204  var.add_to_node (*node);
206 #undef CONFIG_VARIABLE
207 #undef CONFIG_VARIABLE_SPECIAL
208 
209  return *node;
210 }
211 
212 int
213 RCConfiguration::set_state (const XMLNode& root, int version)
214 {
215  if (root.name() != "Ardour") {
216  return -1;
217  }
218 
219  XMLNodeList nlist = root.children();
220  XMLNodeConstIterator niter;
221  XMLNode *node;
222 
224 
225  for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
226 
227  node = *niter;
228 
229  if (node->name() == "Config") {
230  set_variables (*node);
231  } else if (node->name() == "Metadata") {
232  SessionMetadata::Metadata()->set_state (*node, version);
233  } else if (node->name() == ControlProtocolManager::state_node_name) {
234  _control_protocol_state = new XMLNode (*node);
235  }
236  }
237 
238  Diskstream::set_disk_read_chunk_frames (minimum_disk_read_bytes.get() / sizeof (Sample));
240 
241  return 0;
242 }
243 
244 void
246 {
247 #undef CONFIG_VARIABLE
248 #undef CONFIG_VARIABLE_SPECIAL
249 #define CONFIG_VARIABLE(type,var,name,value) \
250  if (var.set_from_node (node)) { \
251  ParameterChanged (name); \
252  }
253 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) \
254  if (var.set_from_node (node)) { \
255  ParameterChanged (name); \
256  }
257 
259 #undef CONFIG_VARIABLE
260 #undef CONFIG_VARIABLE_SPECIAL
261 
262 }
263 void
264 RCConfiguration::map_parameters (boost::function<void (std::string)>& functor)
265 {
266 #undef CONFIG_VARIABLE
267 #undef CONFIG_VARIABLE_SPECIAL
268 #define CONFIG_VARIABLE(type,var,name,value) functor (name);
269 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) functor (name);
271 #undef CONFIG_VARIABLE
272 #undef CONFIG_VARIABLE_SPECIAL
273 }
274 
void set_variables(XMLNode const &)
#define CONFIG_VARIABLE(Type, var, name, value)
#define CONFIG_VARIABLE_SPECIAL(Type, var, name, value, mutator)
static const char * user_config_file_name
LIBARDOUR_API gain_t speed_quietning
void save_extra_xml(const XMLNode &)
Definition: stateful.cc:94
bool write() const
Definition: xml++.cc:147
int set_state(XMLNode const &, int version)
int set_state(const XMLNode &, int version_num)
const std::string & name() const
Definition: xml++.h:104
XMLNode * add_child_copy(const XMLNode &)
Definition: xml++.cc:363
Definition: Beats.hpp:239
LIBPBD_API Transmitter error
LIBPBD_API Transmitter warning
static ControlProtocolManager & instance()
const XMLNodeList & children(const std::string &str=std::string()) const
Definition: xml++.cc:329
static void set_disk_read_chunk_frames(framecnt_t n)
Definition: diskstream.h:143
std::ostream & endmsg(std::ostream &ostr)
Definition: transmitter.h:71
static void set_disk_write_chunk_frames(framecnt_t n)
Definition: diskstream.h:144
XMLNode * instant_xml(const std::string &str)
bool find_file(const Searchpath &search_path, const string &filename, std::string &result)
Definition: file_utils.cc:187
Definition: xml++.h:55
std::list< XMLNode * > XMLNodeList
Definition: xml++.h:44
#define _(Text)
Definition: i18n.h:11
XMLNode * _extra_xml
Definition: stateful.h:109
LIBARDOUR_API std::string user_config_directory(int version=-1)
#define X_(Text)
Definition: i18n.h:13
static int current_state_version
Definition: stateful.h:89
float Sample
Definition: types.h:54
static const char * system_config_file_name
XMLNode * set_root(XMLNode *n)
Definition: xml++.h:63
XMLNode * root() const
Definition: xml++.h:62
Definition: amp.h:29
bool read()
Definition: xml++.h:71
static const std::string state_node_name
LIBARDOUR_API PBD::Searchpath ardour_config_search_path()
static SessionMetadata * Metadata()
LIBPBD_API Transmitter info
void add_instant_xml(XMLNode &, const std::string &directory_path)
Definition: stateful.cc:111
void map_parameters(boost::function< void(std::string)> &)
const char * name
void add_child_nocopy(XMLNode &)
Definition: xml++.cc:357
Definition: xml++.h:95
minimum_disk_write_bytes
Definition: debug.h:30
XMLNode * instant_xml(const std::string &str, const std::string &directory_path)
Definition: stateful.cc:151
void add_instant_xml(XMLNode &)
XMLNodeList::const_iterator XMLNodeConstIterator
Definition: xml++.h:49
std::string string_compose(const std::string &fmt, const T1 &o1)
Definition: compose.h:208