2 Copyright (C) 2006,2007 John Anderson
3 Copyright (C) 2012 Paul Davis
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.
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.
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.
23 #include <glibmm/miscutils.h>
25 #include "pbd/xml++.h"
26 #include "pbd/error.h"
27 #include "pbd/pathscanner.h"
28 #include "pbd/replace_all.h"
30 #include "ardour/filesystem_paths.h"
32 #include "mackie_control_protocol.h"
33 #include "device_profile.h"
37 using namespace Mackie;
39 using namespace ARDOUR;
43 std::map<std::string,DeviceProfile> DeviceProfile::device_profiles;
45 DeviceProfile::DeviceProfile (const string& n)
50 DeviceProfile::~DeviceProfile()
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";
59 system_devprofile_search_path ()
61 bool devprofile_path_defined = false;
62 sys::path spath_env (Glib::getenv (devprofile_env_variable_name, devprofile_path_defined));
64 if (devprofile_path_defined) {
68 SearchPath spath (system_data_search_path());
69 spath.add_subdirectory_to_paths(devprofile_dir_name);
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);
74 if (i == spath.end()) return sys::path();
80 user_devprofile_directory ()
82 sys::path p(user_config_directory());
83 p /= devprofile_dir_name;
89 devprofile_filter (const string &str, void */*arg*/)
91 return (str.length() > strlen(devprofile_suffix) &&
92 str.find (devprofile_suffix) == (str.length() - strlen (devprofile_suffix)));
96 DeviceProfile::reload_device_profiles ()
100 vector<string *> *devprofiles;
102 SearchPath spath (system_devprofile_search_path());
103 spath += user_devprofile_directory ();
105 devprofiles = scanner (spath.to_string(), devprofile_filter, 0, false, true);
106 device_profiles.clear ();
109 error << "No MCP device info files found using " << spath.to_string() << endmsg;
113 if (devprofiles->empty()) {
114 error << "No MCP device info files found using " << spath.to_string() << endmsg;
118 for (vector<string*>::iterator i = devprofiles->begin(); i != devprofiles->end(); ++i) {
119 string fullpath = *(*i);
123 if (!tree.read (fullpath.c_str())) {
127 XMLNode* root = tree.root ();
132 if (dp.set_state (*root, 3000) == 0) { /* version is ignored for now */
133 dp.set_path (fullpath);
134 device_profiles[dp.name()] = dp;
142 DeviceProfile::set_state (const XMLNode& node, int /* version */)
144 const XMLProperty* prop;
145 const XMLNode* child;
147 if (node.name() != "MackieDeviceProfile") {
151 /* name is mandatory */
153 if ((child = node.child ("Name")) == 0 || (prop = child->property ("value")) == 0) {
156 _name = prop->value();
159 if ((child = node.child ("Buttons")) != 0) {
160 XMLNodeConstIterator i;
161 const XMLNodeList& nlist (child->children());
163 for (i = nlist.begin(); i != nlist.end(); ++i) {
165 if ((*i)->name() == "Button") {
167 if ((prop = (*i)->property ("name")) == 0) {
168 error << string_compose ("Button without name in device profile \"%1\" - ignored", _name) << endmsg;
172 int id = Button::name_to_id (prop->value());
174 error << string_compose ("Unknow button ID \"%1\"", prop->value()) << endmsg;
178 Button::ID bid = (Button::ID) id;
180 ButtonActionMap::iterator b = _button_map.find (bid);
182 if (b == _button_map.end()) {
183 b = _button_map.insert (_button_map.end(), std::pair<Button::ID,ButtonActions> (bid, ButtonActions()));
186 if ((prop = (*i)->property ("plain")) != 0) {
187 b->second.plain = prop->value ();
189 if ((prop = (*i)->property ("control")) != 0) {
190 b->second.control = prop->value ();
192 if ((prop = (*i)->property ("shift")) != 0) {
193 b->second.shift = prop->value ();
195 if ((prop = (*i)->property ("option")) != 0) {
196 b->second.option = prop->value ();
198 if ((prop = (*i)->property ("cmdalt")) != 0) {
199 b->second.cmdalt = prop->value ();
201 if ((prop = (*i)->property ("shiftcontrol")) != 0) {
202 b->second.shiftcontrol = prop->value ();
212 DeviceProfile::get_state () const
214 XMLNode* node = new XMLNode ("MackieDeviceProfile");
215 XMLNode* child = new XMLNode ("Name");
217 child->add_property ("value", _name);
218 node->add_child_nocopy (*child);
220 if (_button_map.empty()) {
224 XMLNode* buttons = new XMLNode ("Buttons");
225 node->add_child_nocopy (*buttons);
227 for (ButtonActionMap::const_iterator b = _button_map.begin(); b != _button_map.end(); ++b) {
228 XMLNode* n = new XMLNode ("Button");
230 n->add_property ("name", Button::id_to_name (b->first));
232 if (!b->second.plain.empty()) {
233 n->add_property ("plain", b->second.plain);
235 if (!b->second.control.empty()) {
236 n->add_property ("control", b->second.control);
238 if (!b->second.shift.empty()) {
239 n->add_property ("shift", b->second.shift);
241 if (!b->second.option.empty()) {
242 n->add_property ("option", b->second.option);
244 if (!b->second.cmdalt.empty()) {
245 n->add_property ("cmdalt", b->second.cmdalt);
247 if (!b->second.shiftcontrol.empty()) {
248 n->add_property ("shiftcontrol", b->second.shiftcontrol);
251 buttons->add_child_nocopy (*n);
258 DeviceProfile::get_button_action (Button::ID id, int modifier_state) const
260 ButtonActionMap::const_iterator i = _button_map.find (id);
262 if (i == _button_map.end()) {
266 if (modifier_state == MackieControlProtocol::MODIFIER_CONTROL) {
267 return i->second.control;
268 } else if (modifier_state == MackieControlProtocol::MODIFIER_SHIFT) {
269 return i->second.shift;
270 } else if (modifier_state == MackieControlProtocol::MODIFIER_OPTION) {
271 return i->second.option;
272 } else if (modifier_state == MackieControlProtocol::MODIFIER_CMDALT) {
273 return i->second.cmdalt;
274 } else if (modifier_state == (MackieControlProtocol::MODIFIER_CONTROL|MackieControlProtocol::MODIFIER_SHIFT)) {
275 return i->second.shiftcontrol;
278 return i->second.plain;
282 DeviceProfile::set_button_action (Button::ID id, int modifier_state, const string& act)
284 ButtonActionMap::iterator i = _button_map.find (id);
286 if (i == _button_map.end()) {
287 i = _button_map.insert (std::make_pair (id, ButtonActions())).first;
291 replace_all (action, "<Actions>/", "");
293 if (modifier_state == MackieControlProtocol::MODIFIER_CONTROL) {
294 i->second.control = action;
295 } else if (modifier_state == MackieControlProtocol::MODIFIER_SHIFT) {
296 i->second.shift = action;
297 } else if (modifier_state == MackieControlProtocol::MODIFIER_OPTION) {
298 i->second.option = action;
299 } else if (modifier_state == MackieControlProtocol::MODIFIER_CMDALT) {
300 i->second.cmdalt = action;
301 } else if (modifier_state == (MackieControlProtocol::MODIFIER_CONTROL|MackieControlProtocol::MODIFIER_SHIFT)) {
302 i->second.shiftcontrol = action;
305 if (modifier_state == 0) {
306 i->second.plain = action;
313 DeviceProfile::name() const
319 DeviceProfile::set_path (const string& p)
324 /* XXX copied from libs/ardour/utils.cc */
327 legalize_for_path (const string& str)
329 string::size_type pos;
330 string illegal_chars = "/\\"; /* DOS, POSIX. Yes, we're going to ignore HFS */
336 while ((pos = legal.find_first_of (illegal_chars, pos)) != string::npos) {
337 legal.replace (pos, 1, "_");
341 return string (legal);
346 DeviceProfile::save ()
348 sys::path fullpath = user_devprofile_directory();
350 if (g_mkdir_with_parents (fullpath.to_string().c_str(), 0755) < 0) {
351 error << string_compose(_("Session: cannot create user MCP profile folder \"%1\" (%2)"), fullpath.to_string(), strerror (errno)) << endmsg;
355 fullpath /= legalize_for_path (_name) + ".profile";
358 tree.set_root (&get_state());
360 if (!tree.write (fullpath.to_string())) {
361 error << string_compose ("MCP profile not saved to %1", fullpath.to_string()) << endmsg;