tweak MCP search paths
[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/replace_all.h"
29
30 #include "ardour/filesystem_paths.h"
31
32 #include "mackie_control_protocol.h"
33 #include "device_profile.h"
34
35 #include "i18n.h"
36
37 using namespace Mackie;
38 using namespace PBD;
39 using namespace ARDOUR;
40 using std::string;
41 using std::vector;
42
43 std::map<std::string,DeviceProfile> DeviceProfile::device_profiles;
44
45 DeviceProfile::DeviceProfile (const string& n)
46         : _name (n)
47 {
48 }
49
50 DeviceProfile::~DeviceProfile()
51 {
52 }
53
54 static const char * const devprofile_env_variable_name = "ARDOUR_MCP_PATH";
55 static const char* const devprofile_dir_name = "mcp";
56 static const char* const devprofile_suffix = ".profile";
57
58 static SearchPath
59 system_devprofile_search_path ()
60 {
61         bool devprofile_path_defined = false;
62         sys::path spath_env (Glib::getenv (devprofile_env_variable_name, devprofile_path_defined));
63
64         if (devprofile_path_defined) {
65                 return spath_env;
66         }
67
68         SearchPath spath (system_data_search_path());
69         spath.add_subdirectory_to_paths(devprofile_dir_name);
70
71         return spath;
72 }
73
74 static sys::path
75 user_devprofile_directory ()
76 {
77         sys::path p(user_config_directory());
78         p /= devprofile_dir_name;
79
80         return p;
81 }
82
83 static bool
84 devprofile_filter (const string &str, void */*arg*/)
85 {
86         return (str.length() > strlen(devprofile_suffix) &&
87                 str.find (devprofile_suffix) == (str.length() - strlen (devprofile_suffix)));
88 }
89
90 void
91 DeviceProfile::reload_device_profiles ()
92 {
93         DeviceProfile dp;
94         vector<string> s;
95         vector<string *> *devprofiles;
96         PathScanner scanner;
97         SearchPath spath (system_devprofile_search_path());
98         spath += user_devprofile_directory ();
99
100         devprofiles = scanner (spath.to_string(), devprofile_filter, 0, false, true);
101         device_profiles.clear ();
102
103         if (!devprofiles) {
104                 error << "No MCP device info files found using " << spath.to_string() << endmsg;
105                 return;
106         }
107
108         if (devprofiles->empty()) {
109                 error << "No MCP device info files found using " << spath.to_string() << endmsg;
110                 return;
111         }
112
113         for (vector<string*>::iterator i = devprofiles->begin(); i != devprofiles->end(); ++i) {
114                 string fullpath = *(*i);
115
116                 XMLTree tree;
117
118                 if (!tree.read (fullpath.c_str())) {
119                         continue;
120                 }
121
122                 XMLNode* root = tree.root ();
123                 if (!root) {
124                         continue;
125                 }
126
127                 if (dp.set_state (*root, 3000) == 0) { /* version is ignored for now */
128                         dp.set_path (fullpath);
129                         device_profiles[dp.name()] = dp;
130                 }
131         }
132
133         delete devprofiles;
134 }
135
136 int
137 DeviceProfile::set_state (const XMLNode& node, int /* version */)
138 {
139         const XMLProperty* prop;
140         const XMLNode* child;
141
142         if (node.name() != "MackieDeviceProfile") {
143                 return -1;
144         }
145
146         /* name is mandatory */
147  
148         if ((child = node.child ("Name")) == 0 || (prop = child->property ("value")) == 0) {
149                 return -1;
150         } else {
151                 _name = prop->value();
152         }
153
154         if ((child = node.child ("Buttons")) != 0) {
155                 XMLNodeConstIterator i;
156                 const XMLNodeList& nlist (child->children());
157
158                 for (i = nlist.begin(); i != nlist.end(); ++i) {
159
160                         if ((*i)->name() == "Button") {
161
162                                 if ((prop = (*i)->property ("name")) == 0) {
163                                         error << string_compose ("Button without name in device profile \"%1\" - ignored", _name) << endmsg;
164                                         continue;
165                                 }
166
167                                 int id = Button::name_to_id (prop->value());
168                                 if (id < 0) {
169                                         error << string_compose ("Unknow button ID \"%1\"", prop->value()) << endmsg;
170                                         continue;
171                                 }
172
173                                 Button::ID bid = (Button::ID) id;
174
175                                 ButtonActionMap::iterator b = _button_map.find (bid);
176
177                                 if (b == _button_map.end()) {
178                                         b = _button_map.insert (_button_map.end(), std::pair<Button::ID,ButtonActions> (bid, ButtonActions()));
179                                 }
180
181                                 if ((prop = (*i)->property ("plain")) != 0) {
182                                         b->second.plain = prop->value ();
183                                 }
184                                 if ((prop = (*i)->property ("control")) != 0) {
185                                         b->second.control = prop->value ();
186                                 }
187                                 if ((prop = (*i)->property ("shift")) != 0) {
188                                         b->second.shift = prop->value ();
189                                 }
190                                 if ((prop = (*i)->property ("option")) != 0) {
191                                         b->second.option = prop->value ();
192                                 }
193                                 if ((prop = (*i)->property ("cmdalt")) != 0) {
194                                         b->second.cmdalt = prop->value ();
195                                 }
196                                 if ((prop = (*i)->property ("shiftcontrol")) != 0) {
197                                         b->second.shiftcontrol = prop->value ();
198                                 }
199                         }
200                 }
201         }
202
203         return 0;
204 }
205
206 XMLNode&
207 DeviceProfile::get_state () const
208 {
209         XMLNode* node = new XMLNode ("MackieDeviceProfile");
210         XMLNode* child = new XMLNode ("Name");
211
212         child->add_property ("value", _name);
213         node->add_child_nocopy (*child);
214
215         if (_button_map.empty()) {
216                 return *node;
217         }
218
219         XMLNode* buttons = new XMLNode ("Buttons");
220         node->add_child_nocopy (*buttons);
221
222         for (ButtonActionMap::const_iterator b = _button_map.begin(); b != _button_map.end(); ++b) {
223                 XMLNode* n = new XMLNode ("Button");
224
225                 n->add_property ("name", Button::id_to_name (b->first));
226
227                 if (!b->second.plain.empty()) {
228                         n->add_property ("plain", b->second.plain);
229                 }
230                 if (!b->second.control.empty()) {
231                         n->add_property ("control", b->second.control);
232                 }
233                 if (!b->second.shift.empty()) {
234                         n->add_property ("shift", b->second.shift);
235                 }
236                 if (!b->second.option.empty()) {
237                         n->add_property ("option", b->second.option);
238                 }
239                 if (!b->second.cmdalt.empty()) {
240                         n->add_property ("cmdalt", b->second.cmdalt);
241                 }
242                 if (!b->second.shiftcontrol.empty()) {
243                         n->add_property ("shiftcontrol", b->second.shiftcontrol);
244                 }
245
246                 buttons->add_child_nocopy (*n);
247         }
248
249         return *node;
250 }
251
252 string
253 DeviceProfile::get_button_action (Button::ID id, int modifier_state) const
254 {
255         ButtonActionMap::const_iterator i = _button_map.find (id);
256
257         if (i == _button_map.end()) {
258                 return string();
259         }
260
261         if (modifier_state == MackieControlProtocol::MODIFIER_CONTROL) {
262                 return i->second.control;
263         } else if (modifier_state == MackieControlProtocol::MODIFIER_SHIFT) {
264                 return i->second.shift;
265         } else if (modifier_state == MackieControlProtocol::MODIFIER_OPTION) {
266                 return i->second.option;
267         } else if (modifier_state == MackieControlProtocol::MODIFIER_CMDALT) {
268                 return i->second.cmdalt;
269         } else if (modifier_state == (MackieControlProtocol::MODIFIER_CONTROL|MackieControlProtocol::MODIFIER_SHIFT)) {
270                 return i->second.shiftcontrol;
271         }
272
273         return i->second.plain;
274 }
275
276 void
277 DeviceProfile::set_button_action (Button::ID id, int modifier_state, const string& act)
278 {
279         ButtonActionMap::iterator i = _button_map.find (id);
280
281         if (i == _button_map.end()) {
282                 i = _button_map.insert (std::make_pair (id, ButtonActions())).first;
283         }
284
285         string action (act);
286         replace_all (action, "<Actions>/", "");
287
288         if (modifier_state == MackieControlProtocol::MODIFIER_CONTROL) {
289                 i->second.control = action;
290         } else if (modifier_state == MackieControlProtocol::MODIFIER_SHIFT) {
291                 i->second.shift = action;
292         } else if (modifier_state == MackieControlProtocol::MODIFIER_OPTION) {
293                 i->second.option = action;
294         } else if (modifier_state == MackieControlProtocol::MODIFIER_CMDALT) {
295                 i->second.cmdalt = action;
296         } else if (modifier_state == (MackieControlProtocol::MODIFIER_CONTROL|MackieControlProtocol::MODIFIER_SHIFT)) {
297                 i->second.shiftcontrol = action;
298         }
299
300         if (modifier_state == 0) {
301                 i->second.plain = action;
302         }
303
304         save ();
305 }
306
307 const string&
308 DeviceProfile::name() const
309 {
310         return _name;
311 }
312
313 void
314 DeviceProfile::set_path (const string& p)
315 {
316         _path = p;
317 }
318
319 /* XXX copied from libs/ardour/utils.cc */
320
321 static string
322 legalize_for_path (const string& str)
323 {
324         string::size_type pos;
325         string illegal_chars = "/\\"; /* DOS, POSIX. Yes, we're going to ignore HFS */
326         string legal;
327
328         legal = str;
329         pos = 0;
330
331         while ((pos = legal.find_first_of (illegal_chars, pos)) != string::npos) {
332                 legal.replace (pos, 1, "_");
333                 pos += 1;
334         }
335
336         return string (legal);
337 }
338
339
340 void
341 DeviceProfile::save ()
342 {
343         sys::path fullpath = user_devprofile_directory();
344
345         if (g_mkdir_with_parents (fullpath.to_string().c_str(), 0755) < 0) {
346                 error << string_compose(_("Session: cannot create user MCP profile folder \"%1\" (%2)"), fullpath.to_string(), strerror (errno)) << endmsg;
347                 return;
348         }
349
350         fullpath /= legalize_for_path (_name) + ".profile";
351         
352         XMLTree tree;
353         tree.set_root (&get_state());
354
355         if (!tree.write (fullpath.to_string())) {
356                 error << string_compose ("MCP profile not saved to %1", fullpath.to_string()) << endmsg;
357         }
358 }
359