Attempt #46 to understand MIDI data locking.
[ardour2.git] / gtk2_ardour / ui_config.cc
blobb61ef1b5516b0c24e0b408406f21cd6472b1eb39
1 /*
2 Copyright (C) 1999-2006 Paul Davis
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.
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.
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.
20 #include <unistd.h>
21 #include <cstdio> /* for snprintf, grrr */
23 #include <glibmm/miscutils.h>
25 #include "pbd/failed_constructor.h"
26 #include "pbd/xml++.h"
27 #include "pbd/filesystem.h"
28 #include "pbd/file_utils.h"
29 #include "pbd/error.h"
31 #include "ardour/ardour.h"
32 #include "ardour/filesystem_paths.h"
34 #include "ui_config.h"
36 #include "i18n.h"
38 using namespace std;
39 using namespace PBD;
40 using namespace ARDOUR;
42 UIConfiguration::UIConfiguration ()
44 #undef UI_CONFIG_VARIABLE
45 #undef CANVAS_VARIABLE
46 #define UI_CONFIG_VARIABLE(Type,var,name,val) var (name,val),
47 #define CANVAS_VARIABLE(var,name) var (name),
48 #include "ui_config_vars.h"
49 #include "canvas_vars.h"
50 #undef UI_CONFIG_VARIABLE
51 #undef CANVAS_VARIABLE
52 hack(true)
54 load_state();
57 UIConfiguration::~UIConfiguration ()
61 int
62 UIConfiguration::load_defaults ()
64 int found = 0;
66 sys::path default_ui_rc_file;
67 std::string rcfile;
69 if (getenv ("ARDOUR_SAE")) {
70 rcfile = "ardour3_ui_sae.conf";
71 } else {
72 rcfile = "ardour3_ui_default.conf";
75 if (find_file_in_search_path (ardour_search_path() + system_config_search_path(),
76 rcfile, default_ui_rc_file) )
78 XMLTree tree;
79 found = 1;
81 string rcfile = default_ui_rc_file.to_string();
83 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endl;
85 if (!tree.read (rcfile.c_str())) {
86 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
87 return -1;
90 if (set_state (*tree.root(), Stateful::loading_state_version)) {
91 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
92 return -1;
96 return found;
99 int
100 UIConfiguration::load_state ()
102 bool found = false;
104 sys::path default_ui_rc_file;
106 if ( find_file_in_search_path (ardour_search_path() + system_config_search_path(),
107 "ardour3_ui_default.conf", default_ui_rc_file) )
109 XMLTree tree;
110 found = true;
112 string rcfile = default_ui_rc_file.to_string();
114 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endl;
116 if (!tree.read (rcfile.c_str())) {
117 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
118 return -1;
121 if (set_state (*tree.root(), Stateful::loading_state_version)) {
122 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
123 return -1;
127 sys::path user_ui_rc_file;
129 if (find_file_in_search_path (ardour_search_path() + user_config_directory(),
130 "ardour3_ui.conf", user_ui_rc_file))
132 XMLTree tree;
133 found = true;
135 string rcfile = user_ui_rc_file.to_string();
137 info << string_compose (_("Loading user ui configuration file %1"), rcfile) << endmsg;
139 if (!tree.read (rcfile)) {
140 error << string_compose(_("cannot read ui configuration file \"%1\""), rcfile) << endmsg;
141 return -1;
144 if (set_state (*tree.root(), Stateful::loading_state_version)) {
145 error << string_compose(_("user ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
146 return -1;
150 if (!found)
151 error << _("could not find any ui configuration file, canvas will look broken.") << endmsg;
153 pack_canvasvars();
154 return 0;
158 UIConfiguration::save_state()
160 XMLTree tree;
162 try {
163 sys::create_directories (user_config_directory ());
165 catch (const sys::filesystem_error& ex) {
166 error << "Could not create user configuration directory" << endmsg;
167 return -1;
170 sys::path rcfile_path(user_config_directory());
172 rcfile_path /= "ardour3_ui.conf";
173 const string rcfile = rcfile_path.to_string();
175 // this test seems bogus?
176 if (rcfile.length()) {
177 tree.set_root (&get_state());
178 if (!tree.write (rcfile.c_str())){
179 error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
180 return -1;
184 return 0;
187 XMLNode&
188 UIConfiguration::get_state ()
190 XMLNode* root;
191 LocaleGuard lg (X_("POSIX"));
193 root = new XMLNode("Ardour");
195 root->add_child_nocopy (get_variables ("UI"));
196 root->add_child_nocopy (get_variables ("Canvas"));
198 if (_extra_xml) {
199 root->add_child_copy (*_extra_xml);
202 return *root;
205 XMLNode&
206 UIConfiguration::get_variables (std::string which_node)
208 XMLNode* node;
209 LocaleGuard lg (X_("POSIX"));
211 node = new XMLNode(which_node);
213 #undef UI_CONFIG_VARIABLE
214 #undef CANVAS_VARIABLE
215 #define UI_CONFIG_VARIABLE(Type,var,Name,value) if (node->name() == "UI") { var.add_to_node (*node); }
216 #define CANVAS_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
217 #include "ui_config_vars.h"
218 #include "canvas_vars.h"
219 #undef UI_CONFIG_VARIABLE
220 #undef CANVAS_VARIABLE
222 return *node;
226 UIConfiguration::set_state (const XMLNode& root, int /*version*/)
228 if (root.name() != "Ardour") {
229 return -1;
232 XMLNodeList nlist = root.children();
233 XMLNodeConstIterator niter;
234 XMLNode *node;
236 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
238 node = *niter;
239 if (node->name() == "Canvas" || node->name() == "UI") {
240 set_variables (*node);
242 } else if (node->name() == "Extra") {
243 _extra_xml = new XMLNode (*node);
247 return 0;
250 void
251 UIConfiguration::set_variables (const XMLNode& node)
253 #undef UI_CONFIG_VARIABLE
254 #undef CANVAS_VARIABLE
255 #define UI_CONFIG_VARIABLE(Type,var,name,val) \
256 if (var.set_from_node (node)) { \
257 ParameterChanged (name); \
259 #define CANVAS_VARIABLE(var,name) \
260 if (var.set_from_node (node)) { \
261 ParameterChanged (name); \
263 #include "ui_config_vars.h"
264 #include "canvas_vars.h"
265 #undef UI_CONFIG_VARIABLE
266 #undef CANVAS_VARIABLE
269 void
270 UIConfiguration::pack_canvasvars ()
272 #undef CANVAS_VARIABLE
273 #define CANVAS_VARIABLE(var,name) canvas_colors.push_back(&var);
274 #include "canvas_vars.h"
275 #undef CANVAS_VARIABLE