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