fix pathscanner / stl_vector related memory leaks
[ardour.git] / libs / surfaces / mackie / device_profile.cc
1 /*
2         Copyright (C) 2006,2007 John Anderson
3         Copyright (C) 2012 Paul Davis
4
5         This program is free software; you can redistribute it and/or modify
6         it under the terms of the GNU General Public License as published by
7         the Free Software Foundation; either version 2 of the License, or
8         (at your option) any later version.
9
10         This program is distributed in the hope that it will be useful,
11         but WITHOUT ANY WARRANTY; without even the implied warranty of
12         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13         GNU General Public License for more details.
14
15         You should have received a copy of the GNU General Public License
16         along with this program; if not, write to the Free Software
17         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <cerrno>
21 #include <cstdlib>
22 #include <cstring>
23 #include <glibmm/miscutils.h>
24
25 #include "pbd/xml++.h"
26 #include "pbd/error.h"
27 #include "pbd/pathscanner.h"
28 #include "pbd/stl_delete.h"
29 #include "pbd/replace_all.h"
30
31 #include "ardour/filesystem_paths.h"
32
33 #include "mackie_control_protocol.h"
34 #include "device_profile.h"
35
36 #include "i18n.h"
37
38 using namespace Mackie;
39 using namespace PBD;
40 using namespace ARDOUR;
41 using std::string;
42 using std::vector;
43
44 std::map<std::string,DeviceProfile> DeviceProfile::device_profiles;
45
46 DeviceProfile::DeviceProfile (const string& n)
47         : _name (n)
48 {
49 }
50
51 DeviceProfile::~DeviceProfile()
52 {
53 }
54
55 static const char * const devprofile_env_variable_name = "ARDOUR_MCP_PATH";
56 static const char* const devprofile_dir_name = "mcp";
57 static const char* const devprofile_suffix = ".profile";
58
59 static Searchpath
60 devprofile_search_path ()
61 {
62         bool devprofile_path_defined = false;
63         std::string spath_env (Glib::getenv (devprofile_env_variable_name, devprofile_path_defined));
64
65         if (devprofile_path_defined) {
66                 return spath_env;
67         }
68
69         Searchpath spath (ardour_data_search_path());
70         spath.add_subdirectory_to_paths(devprofile_dir_name);
71
72         return spath;
73 }
74
75 static std::string
76 user_devprofile_directory ()
77 {
78         return Glib::build_filename (user_config_directory(), devprofile_dir_name);
79 }
80
81 static bool
82 devprofile_filter (const string &str, void* /*arg*/)
83 {
84         return (str.length() > strlen(devprofile_suffix) &&
85                 str.find (devprofile_suffix) == (str.length() - strlen (devprofile_suffix)));
86 }
87
88 void
89 DeviceProfile::reload_device_profiles ()
90 {
91         DeviceProfile dp;
92         vector<string> s;
93         vector<string *> *devprofiles;
94         PathScanner scanner;
95         Searchpath spath (devprofile_search_path());
96
97         devprofiles = scanner (spath.to_string(), devprofile_filter, 0, false, true);
98         device_profiles.clear ();
99
100         if (!devprofiles) {
101                 error << "No MCP device info files found using " << spath.to_string() << endmsg;
102                 return;
103         }
104
105         if (devprofiles->empty()) {
106                 error << "No MCP device info files found using " << spath.to_string() << endmsg;
107                 return;
108         }
109
110         for (vector<string*>::iterator i = devprofiles->begin(); i != devprofiles->end(); ++i) {
111                 string fullpath = *(*i);
112
113                 XMLTree tree;
114
115                 if (!tree.read (fullpath.c_str())) {
116                         continue;
117                 }
118
119                 XMLNode* root = tree.root ();
120                 if (!root) {
121                         continue;
122                 }
123
124                 if (dp.set_state (*root, 3000) == 0) { /* version is ignored for now */
125                         dp.set_path (fullpath);
126                         device_profiles[dp.name()] = dp;
127                 }
128         }
129
130         vector_delete (devprofiles);
131         delete devprofiles;
132 }
133
134 int
135 DeviceProfile::set_state (const XMLNode& node, int /* version */)
136 {
137         const XMLProperty* prop;
138         const XMLNode* child;
139
140         if (node.name() != "MackieDeviceProfile") {
141                 return -1;
142         }
143
144         /* name is mandatory */
145  
146         if ((child = node.child ("Name")) == 0 || (prop = child->property ("value")) == 0) {
147                 return -1;
148         } else {
149                 _name = prop->value();
150         }
151
152         if ((child = node.child ("Buttons")) != 0) {
153                 XMLNodeConstIterator i;
154                 const XMLNodeList& nlist (child->children());
155
156                 for (i = nlist.begin(); i != nlist.end(); ++i) {
157
158                         if ((*i)->name() == "Button") {
159
160                                 if ((prop = (*i)->property ("name")) == 0) {
161                                         error << string_compose ("Button without name in device profile \"%1\" - ignored", _name) << endmsg;
162                                         continue;
163                                 }
164
165                                 int id = Button::name_to_id (prop->value());
166                                 if (id < 0) {
167                                         error << string_compose ("Unknow button ID \"%1\"", prop->value()) << endmsg;
168                                         continue;
169                                 }
170
171                                 Button::ID bid = (Button::ID) id;
172
173                                 ButtonActionMap::iterator b = _button_map.find (bid);
174
175                                 if (b == _button_map.end()) {
176                                         b = _button_map.insert (_button_map.end(), std::pair<Button::ID,ButtonActions> (bid, ButtonActions()));
177                                 }
178
179                                 if ((prop = (*i)->property ("plain")) != 0) {
180                                         b->second.plain = prop->value ();
181                                 }
182                                 if ((prop = (*i)->property ("control")) != 0) {
183                                         b->second.control = prop->value ();
184                                 }
185                                 if ((prop = (*i)->property ("shift")) != 0) {
186                                         b->second.shift = prop->value ();
187                                 }
188                                 if ((prop = (*i)->property ("option")) != 0) {
189                                         b->second.option = prop->value ();
190                                 }
191                                 if ((prop = (*i)->property ("cmdalt")) != 0) {
192                                         b->second.cmdalt = prop->value ();
193                                 }
194                                 if ((prop = (*i)->property ("shiftcontrol")) != 0) {
195                                         b->second.shiftcontrol = prop->value ();
196                                 }
197                         }
198                 }
199         }
200
201         return 0;
202 }
203
204 XMLNode&
205 DeviceProfile::get_state () const
206 {
207         XMLNode* node = new XMLNode ("MackieDeviceProfile");
208         XMLNode* child = new XMLNode ("Name");
209
210         child->add_property ("value", _name);
211         node->add_child_nocopy (*child);
212
213         if (_button_map.empty()) {
214                 return *node;
215         }
216
217         XMLNode* buttons = new XMLNode ("Buttons");
218         node->add_child_nocopy (*buttons);
219
220         for (ButtonActionMap::const_iterator b = _button_map.begin(); b != _button_map.end(); ++b) {
221                 XMLNode* n = new XMLNode ("Button");
222
223                 n->add_property ("name", Button::id_to_name (b->first));
224
225                 if (!b->second.plain.empty()) {
226                         n->add_property ("plain", b->second.plain);
227                 }
228                 if (!b->second.control.empty()) {
229                         n->add_property ("control", b->second.control);
230                 }
231                 if (!b->second.shift.empty()) {
232                         n->add_property ("shift", b->second.shift);
233                 }
234                 if (!b->second.option.empty()) {
235                         n->add_property ("option", b->second.option);
236                 }
237                 if (!b->second.cmdalt.empty()) {
238                         n->add_property ("cmdalt", b->second.cmdalt);
239                 }
240                 if (!b->second.shiftcontrol.empty()) {
241                         n->add_property ("shiftcontrol", b->second.shiftcontrol);
242                 }
243
244                 buttons->add_child_nocopy (*n);
245         }
246
247         return *node;
248 }
249
250 string
251 DeviceProfile::get_button_action (Button::ID id, int modifier_state) const
252 {
253         ButtonActionMap::const_iterator i = _button_map.find (id);
254
255         if (i == _button_map.end()) {
256                 return string();
257         }
258
259         if (modifier_state == MackieControlProtocol::MODIFIER_CONTROL) {
260                 return i->second.control;
261         } else if (modifier_state == MackieControlProtocol::MODIFIER_SHIFT) {
262                 return i->second.shift;
263         } else if (modifier_state == MackieControlProtocol::MODIFIER_OPTION) {
264                 return i->second.option;
265         } else if (modifier_state == MackieControlProtocol::MODIFIER_CMDALT) {
266                 return i->second.cmdalt;
267         } else if (modifier_state == (MackieControlProtocol::MODIFIER_CONTROL|MackieControlProtocol::MODIFIER_SHIFT)) {
268                 return i->second.shiftcontrol;
269         }
270
271         return i->second.plain;
272 }
273
274 void
275 DeviceProfile::set_button_action (Button::ID id, int modifier_state, const string& act)
276 {
277         ButtonActionMap::iterator i = _button_map.find (id);
278
279         if (i == _button_map.end()) {
280                 i = _button_map.insert (std::make_pair (id, ButtonActions())).first;
281         }
282
283         string action (act);
284         replace_all (action, "<Actions>/", "");
285
286         if (modifier_state == MackieControlProtocol::MODIFIER_CONTROL) {
287                 i->second.control = action;
288         } else if (modifier_state == MackieControlProtocol::MODIFIER_SHIFT) {
289                 i->second.shift = action;
290         } else if (modifier_state == MackieControlProtocol::MODIFIER_OPTION) {
291                 i->second.option = action;
292         } else if (modifier_state == MackieControlProtocol::MODIFIER_CMDALT) {
293                 i->second.cmdalt = action;
294         } else if (modifier_state == (MackieControlProtocol::MODIFIER_CONTROL|MackieControlProtocol::MODIFIER_SHIFT)) {
295                 i->second.shiftcontrol = action;
296         }
297
298         if (modifier_state == 0) {
299                 i->second.plain = action;
300         }
301
302         save ();
303 }
304
305 const string&
306 DeviceProfile::name() const
307 {
308         return _name;
309 }
310
311 void
312 DeviceProfile::set_path (const string& p)
313 {
314         _path = p;
315 }
316
317 /* XXX copied from libs/ardour/utils.cc */
318
319 static string
320 legalize_for_path (const string& str)
321 {
322         string::size_type pos;
323         string illegal_chars = "/\\"; /* DOS, POSIX. Yes, we're going to ignore HFS */
324         string legal;
325
326         legal = str;
327         pos = 0;
328
329         while ((pos = legal.find_first_of (illegal_chars, pos)) != string::npos) {
330                 legal.replace (pos, 1, "_");
331                 pos += 1;
332         }
333
334         return string (legal);
335 }
336
337
338 void
339 DeviceProfile::save ()
340 {
341         std::string fullpath = user_devprofile_directory();
342
343         if (g_mkdir_with_parents (fullpath.c_str(), 0755) < 0) {
344                 error << string_compose(_("Session: cannot create user MCP profile folder \"%1\" (%2)"), fullpath, strerror (errno)) << endmsg;
345                 return;
346         }
347
348         fullpath = Glib::build_filename (fullpath, legalize_for_path (_name) + ".profile");
349         
350         XMLTree tree;
351         tree.set_root (&get_state());
352
353         if (!tree.write (fullpath)) {
354                 error << string_compose ("MCP profile not saved to %1", fullpath) << endmsg;
355         }
356 }
357