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/stl_delete.h"
29 #include "pbd/replace_all.h"
31 #include "ardour/filesystem_paths.h"
33 #include "mackie_control_protocol.h"
34 #include "device_profile.h"
38 using namespace Mackie;
40 using namespace ARDOUR;
44 std::map<std::string,DeviceProfile> DeviceProfile::device_profiles;
46 DeviceProfile::DeviceProfile (const string& n)
51 DeviceProfile::~DeviceProfile()
55 static const char * const devprofile_env_variable_name = "ARDOUR_MCP_PATH";
56 static const char* const devprofile_dir_name = "mcp";
57 static const char* const devprofile_suffix = ".profile";
60 devprofile_search_path ()
62 bool devprofile_path_defined = false;
63 std::string spath_env (Glib::getenv (devprofile_env_variable_name, devprofile_path_defined));
65 if (devprofile_path_defined) {
69 Searchpath spath (ardour_data_search_path());
70 spath.add_subdirectory_to_paths(devprofile_dir_name);
76 user_devprofile_directory ()
78 return Glib::build_filename (user_config_directory(), devprofile_dir_name);
82 devprofile_filter (const string &str, void* /*arg*/)
84 return (str.length() > strlen(devprofile_suffix) &&
85 str.find (devprofile_suffix) == (str.length() - strlen (devprofile_suffix)));
89 DeviceProfile::reload_device_profiles ()
93 vector<string *> *devprofiles;
95 Searchpath spath (devprofile_search_path());
97 devprofiles = scanner (spath.to_string(), devprofile_filter, 0, false, true);
98 device_profiles.clear ();
101 error << "No MCP device info files found using " << spath.to_string() << endmsg;
105 if (devprofiles->empty()) {
106 error << "No MCP device info files found using " << spath.to_string() << endmsg;
110 for (vector<string*>::iterator i = devprofiles->begin(); i != devprofiles->end(); ++i) {
111 string fullpath = *(*i);
115 if (!tree.read (fullpath.c_str())) {
119 XMLNode* root = tree.root ();
124 if (dp.set_state (*root, 3000) == 0) { /* version is ignored for now */
125 dp.set_path (fullpath);
126 device_profiles[dp.name()] = dp;
130 vector_delete (devprofiles);
135 DeviceProfile::set_state (const XMLNode& node, int /* version */)
137 const XMLProperty* prop;
138 const XMLNode* child;
140 if (node.name() != "MackieDeviceProfile") {
144 /* name is mandatory */
146 if ((child = node.child ("Name")) == 0 || (prop = child->property ("value")) == 0) {
149 _name = prop->value();
152 if ((child = node.child ("Buttons")) != 0) {
153 XMLNodeConstIterator i;
154 const XMLNodeList& nlist (child->children());
156 for (i = nlist.begin(); i != nlist.end(); ++i) {
158 if ((*i)->name() == "Button") {
160 if ((prop = (*i)->property ("name")) == 0) {
161 error << string_compose ("Button without name in device profile \"%1\" - ignored", _name) << endmsg;
165 int id = Button::name_to_id (prop->value());
167 error << string_compose ("Unknow button ID \"%1\"", prop->value()) << endmsg;
171 Button::ID bid = (Button::ID) id;
173 ButtonActionMap::iterator b = _button_map.find (bid);
175 if (b == _button_map.end()) {
176 b = _button_map.insert (_button_map.end(), std::pair<Button::ID,ButtonActions> (bid, ButtonActions()));
179 if ((prop = (*i)->property ("plain")) != 0) {
180 b->second.plain = prop->value ();
182 if ((prop = (*i)->property ("control")) != 0) {
183 b->second.control = prop->value ();
185 if ((prop = (*i)->property ("shift")) != 0) {
186 b->second.shift = prop->value ();
188 if ((prop = (*i)->property ("option")) != 0) {
189 b->second.option = prop->value ();
191 if ((prop = (*i)->property ("cmdalt")) != 0) {
192 b->second.cmdalt = prop->value ();
194 if ((prop = (*i)->property ("shiftcontrol")) != 0) {
195 b->second.shiftcontrol = prop->value ();
205 DeviceProfile::get_state () const
207 XMLNode* node = new XMLNode ("MackieDeviceProfile");
208 XMLNode* child = new XMLNode ("Name");
210 child->add_property ("value", _name);
211 node->add_child_nocopy (*child);
213 if (_button_map.empty()) {
217 XMLNode* buttons = new XMLNode ("Buttons");
218 node->add_child_nocopy (*buttons);
220 for (ButtonActionMap::const_iterator b = _button_map.begin(); b != _button_map.end(); ++b) {
221 XMLNode* n = new XMLNode ("Button");
223 n->add_property ("name", Button::id_to_name (b->first));
225 if (!b->second.plain.empty()) {
226 n->add_property ("plain", b->second.plain);
228 if (!b->second.control.empty()) {
229 n->add_property ("control", b->second.control);
231 if (!b->second.shift.empty()) {
232 n->add_property ("shift", b->second.shift);
234 if (!b->second.option.empty()) {
235 n->add_property ("option", b->second.option);
237 if (!b->second.cmdalt.empty()) {
238 n->add_property ("cmdalt", b->second.cmdalt);
240 if (!b->second.shiftcontrol.empty()) {
241 n->add_property ("shiftcontrol", b->second.shiftcontrol);
244 buttons->add_child_nocopy (*n);
251 DeviceProfile::get_button_action (Button::ID id, int modifier_state) const
253 ButtonActionMap::const_iterator i = _button_map.find (id);
255 if (i == _button_map.end()) {
259 if (modifier_state == MackieControlProtocol::MODIFIER_CONTROL) {
260 return i->second.control;
261 } else if (modifier_state == MackieControlProtocol::MODIFIER_SHIFT) {
262 return i->second.shift;
263 } else if (modifier_state == MackieControlProtocol::MODIFIER_OPTION) {
264 return i->second.option;
265 } else if (modifier_state == MackieControlProtocol::MODIFIER_CMDALT) {
266 return i->second.cmdalt;
267 } else if (modifier_state == (MackieControlProtocol::MODIFIER_CONTROL|MackieControlProtocol::MODIFIER_SHIFT)) {
268 return i->second.shiftcontrol;
271 return i->second.plain;
275 DeviceProfile::set_button_action (Button::ID id, int modifier_state, const string& act)
277 ButtonActionMap::iterator i = _button_map.find (id);
279 if (i == _button_map.end()) {
280 i = _button_map.insert (std::make_pair (id, ButtonActions())).first;
284 replace_all (action, "<Actions>/", "");
286 if (modifier_state == MackieControlProtocol::MODIFIER_CONTROL) {
287 i->second.control = action;
288 } else if (modifier_state == MackieControlProtocol::MODIFIER_SHIFT) {
289 i->second.shift = action;
290 } else if (modifier_state == MackieControlProtocol::MODIFIER_OPTION) {
291 i->second.option = action;
292 } else if (modifier_state == MackieControlProtocol::MODIFIER_CMDALT) {
293 i->second.cmdalt = action;
294 } else if (modifier_state == (MackieControlProtocol::MODIFIER_CONTROL|MackieControlProtocol::MODIFIER_SHIFT)) {
295 i->second.shiftcontrol = action;
298 if (modifier_state == 0) {
299 i->second.plain = action;
306 DeviceProfile::name() const
312 DeviceProfile::set_path (const string& p)
317 /* XXX copied from libs/ardour/utils.cc */
320 legalize_for_path (const string& str)
322 string::size_type pos;
323 string illegal_chars = "/\\"; /* DOS, POSIX. Yes, we're going to ignore HFS */
329 while ((pos = legal.find_first_of (illegal_chars, pos)) != string::npos) {
330 legal.replace (pos, 1, "_");
334 return string (legal);
339 DeviceProfile::save ()
341 std::string fullpath = user_devprofile_directory();
343 if (g_mkdir_with_parents (fullpath.c_str(), 0755) < 0) {
344 error << string_compose(_("Session: cannot create user MCP profile folder \"%1\" (%2)"), fullpath, strerror (errno)) << endmsg;
348 fullpath = Glib::build_filename (fullpath, legalize_for_path (_name) + ".profile");
351 tree.set_root (&get_state());
353 if (!tree.write (fullpath)) {
354 error << string_compose ("MCP profile not saved to %1", fullpath) << endmsg;