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