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