2012-04-17 16:41:31 -04:00
|
|
|
/*
|
|
|
|
Copyright (C) 2006,2007 John Anderson
|
|
|
|
Copyright (C) 2012 Paul Davis
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
2012-04-17 23:24:59 -04:00
|
|
|
#include <cerrno>
|
2012-04-17 16:41:31 -04:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <glibmm/miscutils.h>
|
|
|
|
|
|
|
|
#include "pbd/xml++.h"
|
|
|
|
#include "pbd/error.h"
|
2014-06-17 07:41:29 -04:00
|
|
|
#include "pbd/file_utils.h"
|
2014-06-14 22:09:48 -04:00
|
|
|
#include "pbd/stl_delete.h"
|
2012-04-17 23:24:59 -04:00
|
|
|
#include "pbd/replace_all.h"
|
2012-04-17 16:41:31 -04:00
|
|
|
|
|
|
|
#include "ardour/filesystem_paths.h"
|
|
|
|
|
|
|
|
#include "mackie_control_protocol.h"
|
|
|
|
#include "device_profile.h"
|
|
|
|
|
2016-07-14 14:44:52 -04:00
|
|
|
#include "pbd/i18n.h"
|
2012-04-17 16:41:31 -04:00
|
|
|
|
|
|
|
using namespace PBD;
|
|
|
|
using namespace ARDOUR;
|
2015-04-15 20:37:20 -04:00
|
|
|
using namespace ArdourSurface;
|
|
|
|
using namespace Mackie;
|
|
|
|
|
2012-04-17 16:41:31 -04:00
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
std::map<std::string,DeviceProfile> DeviceProfile::device_profiles;
|
2016-02-04 11:23:54 -05:00
|
|
|
const std::string DeviceProfile::edited_indicator (" (edited)");
|
|
|
|
const std::string DeviceProfile::default_profile_name ("User");
|
2012-04-17 16:41:31 -04:00
|
|
|
|
|
|
|
DeviceProfile::DeviceProfile (const string& n)
|
|
|
|
: _name (n)
|
2016-02-04 11:23:54 -05:00
|
|
|
, edited (false)
|
2012-04-17 16:41:31 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceProfile::~DeviceProfile()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char * const devprofile_env_variable_name = "ARDOUR_MCP_PATH";
|
|
|
|
static const char* const devprofile_dir_name = "mcp";
|
|
|
|
static const char* const devprofile_suffix = ".profile";
|
|
|
|
|
2013-08-15 06:04:08 -04:00
|
|
|
static Searchpath
|
2012-05-18 13:24:06 -04:00
|
|
|
devprofile_search_path ()
|
2012-04-17 16:41:31 -04:00
|
|
|
{
|
|
|
|
bool devprofile_path_defined = false;
|
2012-06-23 01:06:54 -04:00
|
|
|
std::string spath_env (Glib::getenv (devprofile_env_variable_name, devprofile_path_defined));
|
2012-04-17 16:41:31 -04:00
|
|
|
|
|
|
|
if (devprofile_path_defined) {
|
|
|
|
return spath_env;
|
|
|
|
}
|
|
|
|
|
2013-08-15 06:04:08 -04:00
|
|
|
Searchpath spath (ardour_data_search_path());
|
2012-04-17 16:41:31 -04:00
|
|
|
spath.add_subdirectory_to_paths(devprofile_dir_name);
|
|
|
|
|
2012-05-05 16:29:46 -04:00
|
|
|
return spath;
|
2012-04-17 16:41:31 -04:00
|
|
|
}
|
|
|
|
|
2012-06-23 01:09:44 -04:00
|
|
|
static std::string
|
2012-04-17 16:41:31 -04:00
|
|
|
user_devprofile_directory ()
|
|
|
|
{
|
2012-06-23 01:09:44 -04:00
|
|
|
return Glib::build_filename (user_config_directory(), devprofile_dir_name);
|
2012-04-17 16:41:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2013-09-05 05:03:07 -04:00
|
|
|
devprofile_filter (const string &str, void* /*arg*/)
|
2012-04-17 16:41:31 -04:00
|
|
|
{
|
|
|
|
return (str.length() > strlen(devprofile_suffix) &&
|
|
|
|
str.find (devprofile_suffix) == (str.length() - strlen (devprofile_suffix)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DeviceProfile::reload_device_profiles ()
|
|
|
|
{
|
|
|
|
vector<string> s;
|
2014-06-16 06:39:45 -04:00
|
|
|
vector<string> devprofiles;
|
2013-08-15 06:04:08 -04:00
|
|
|
Searchpath spath (devprofile_search_path());
|
2012-04-17 16:41:31 -04:00
|
|
|
|
2014-06-19 04:26:17 -04:00
|
|
|
find_files_matching_filter (devprofiles, spath, devprofile_filter, 0, false, true);
|
2012-04-17 16:41:31 -04:00
|
|
|
device_profiles.clear ();
|
|
|
|
|
2014-06-16 06:39:45 -04:00
|
|
|
if (devprofiles.empty()) {
|
2012-04-17 16:41:31 -04:00
|
|
|
error << "No MCP device info files found using " << spath.to_string() << endmsg;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-16 06:39:45 -04:00
|
|
|
for (vector<string>::iterator i = devprofiles.begin(); i != devprofiles.end(); ++i) {
|
|
|
|
string fullpath = *i;
|
2015-10-03 16:06:34 -04:00
|
|
|
DeviceProfile dp; // has to be initial every loop or info from last added.
|
2012-04-17 16:41:31 -04:00
|
|
|
|
|
|
|
XMLTree tree;
|
|
|
|
|
|
|
|
if (!tree.read (fullpath.c_str())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLNode* root = tree.root ();
|
|
|
|
if (!root) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dp.set_state (*root, 3000) == 0) { /* version is ignored for now */
|
2012-04-17 23:24:59 -04:00
|
|
|
dp.set_path (fullpath);
|
2012-04-17 16:41:31 -04:00
|
|
|
device_profiles[dp.name()] = dp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
DeviceProfile::set_state (const XMLNode& node, int /* version */)
|
|
|
|
{
|
|
|
|
const XMLProperty* prop;
|
|
|
|
const XMLNode* child;
|
|
|
|
|
|
|
|
if (node.name() != "MackieDeviceProfile") {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* name is mandatory */
|
2015-10-04 14:51:05 -04:00
|
|
|
|
2012-04-17 16:41:31 -04:00
|
|
|
if ((child = node.child ("Name")) == 0 || (prop = child->property ("value")) == 0) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
_name = prop->value();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((child = node.child ("Buttons")) != 0) {
|
|
|
|
XMLNodeConstIterator i;
|
|
|
|
const XMLNodeList& nlist (child->children());
|
|
|
|
|
|
|
|
for (i = nlist.begin(); i != nlist.end(); ++i) {
|
|
|
|
|
|
|
|
if ((*i)->name() == "Button") {
|
|
|
|
|
|
|
|
if ((prop = (*i)->property ("name")) == 0) {
|
|
|
|
error << string_compose ("Button without name in device profile \"%1\" - ignored", _name) << endmsg;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int id = Button::name_to_id (prop->value());
|
|
|
|
if (id < 0) {
|
2015-10-02 07:29:51 -04:00
|
|
|
error << string_compose ("Unknown button ID \"%1\"", prop->value()) << endmsg;
|
2012-04-17 16:41:31 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Button::ID bid = (Button::ID) id;
|
|
|
|
|
|
|
|
ButtonActionMap::iterator b = _button_map.find (bid);
|
|
|
|
|
|
|
|
if (b == _button_map.end()) {
|
|
|
|
b = _button_map.insert (_button_map.end(), std::pair<Button::ID,ButtonActions> (bid, ButtonActions()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((prop = (*i)->property ("plain")) != 0) {
|
|
|
|
b->second.plain = prop->value ();
|
|
|
|
}
|
|
|
|
if ((prop = (*i)->property ("control")) != 0) {
|
|
|
|
b->second.control = prop->value ();
|
|
|
|
}
|
|
|
|
if ((prop = (*i)->property ("shift")) != 0) {
|
|
|
|
b->second.shift = prop->value ();
|
|
|
|
}
|
|
|
|
if ((prop = (*i)->property ("option")) != 0) {
|
|
|
|
b->second.option = prop->value ();
|
|
|
|
}
|
|
|
|
if ((prop = (*i)->property ("cmdalt")) != 0) {
|
|
|
|
b->second.cmdalt = prop->value ();
|
|
|
|
}
|
|
|
|
if ((prop = (*i)->property ("shiftcontrol")) != 0) {
|
|
|
|
b->second.shiftcontrol = prop->value ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-04 11:23:54 -05:00
|
|
|
edited = false;
|
|
|
|
|
2012-04-17 16:41:31 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLNode&
|
|
|
|
DeviceProfile::get_state () const
|
|
|
|
{
|
|
|
|
XMLNode* node = new XMLNode ("MackieDeviceProfile");
|
2012-04-17 23:24:59 -04:00
|
|
|
XMLNode* child = new XMLNode ("Name");
|
2012-04-17 16:41:31 -04:00
|
|
|
|
2016-02-04 11:23:54 -05:00
|
|
|
child->add_property ("value", name());
|
2012-04-17 23:24:59 -04:00
|
|
|
node->add_child_nocopy (*child);
|
2012-04-17 16:41:31 -04:00
|
|
|
|
|
|
|
if (_button_map.empty()) {
|
|
|
|
return *node;
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLNode* buttons = new XMLNode ("Buttons");
|
|
|
|
node->add_child_nocopy (*buttons);
|
|
|
|
|
|
|
|
for (ButtonActionMap::const_iterator b = _button_map.begin(); b != _button_map.end(); ++b) {
|
|
|
|
XMLNode* n = new XMLNode ("Button");
|
|
|
|
|
2012-04-17 23:24:59 -04:00
|
|
|
n->add_property ("name", Button::id_to_name (b->first));
|
2012-04-17 16:41:31 -04:00
|
|
|
|
|
|
|
if (!b->second.plain.empty()) {
|
|
|
|
n->add_property ("plain", b->second.plain);
|
|
|
|
}
|
|
|
|
if (!b->second.control.empty()) {
|
|
|
|
n->add_property ("control", b->second.control);
|
|
|
|
}
|
|
|
|
if (!b->second.shift.empty()) {
|
|
|
|
n->add_property ("shift", b->second.shift);
|
|
|
|
}
|
|
|
|
if (!b->second.option.empty()) {
|
|
|
|
n->add_property ("option", b->second.option);
|
|
|
|
}
|
|
|
|
if (!b->second.cmdalt.empty()) {
|
|
|
|
n->add_property ("cmdalt", b->second.cmdalt);
|
|
|
|
}
|
|
|
|
if (!b->second.shiftcontrol.empty()) {
|
|
|
|
n->add_property ("shiftcontrol", b->second.shiftcontrol);
|
|
|
|
}
|
|
|
|
|
|
|
|
buttons->add_child_nocopy (*n);
|
|
|
|
}
|
|
|
|
|
|
|
|
return *node;
|
|
|
|
}
|
|
|
|
|
|
|
|
string
|
|
|
|
DeviceProfile::get_button_action (Button::ID id, int modifier_state) const
|
|
|
|
{
|
|
|
|
ButtonActionMap::const_iterator i = _button_map.find (id);
|
|
|
|
|
|
|
|
if (i == _button_map.end()) {
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (modifier_state == MackieControlProtocol::MODIFIER_CONTROL) {
|
|
|
|
return i->second.control;
|
|
|
|
} else if (modifier_state == MackieControlProtocol::MODIFIER_SHIFT) {
|
|
|
|
return i->second.shift;
|
|
|
|
} else if (modifier_state == MackieControlProtocol::MODIFIER_OPTION) {
|
|
|
|
return i->second.option;
|
|
|
|
} else if (modifier_state == MackieControlProtocol::MODIFIER_CMDALT) {
|
|
|
|
return i->second.cmdalt;
|
|
|
|
} else if (modifier_state == (MackieControlProtocol::MODIFIER_CONTROL|MackieControlProtocol::MODIFIER_SHIFT)) {
|
|
|
|
return i->second.shiftcontrol;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i->second.plain;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-04-17 23:24:59 -04:00
|
|
|
DeviceProfile::set_button_action (Button::ID id, int modifier_state, const string& act)
|
2012-04-17 16:41:31 -04:00
|
|
|
{
|
|
|
|
ButtonActionMap::iterator i = _button_map.find (id);
|
|
|
|
|
|
|
|
if (i == _button_map.end()) {
|
2012-04-25 17:21:36 -04:00
|
|
|
i = _button_map.insert (std::make_pair (id, ButtonActions())).first;
|
2012-04-17 16:41:31 -04:00
|
|
|
}
|
|
|
|
|
2012-04-17 23:24:59 -04:00
|
|
|
string action (act);
|
|
|
|
replace_all (action, "<Actions>/", "");
|
|
|
|
|
2012-04-17 16:41:31 -04:00
|
|
|
if (modifier_state == MackieControlProtocol::MODIFIER_CONTROL) {
|
|
|
|
i->second.control = action;
|
|
|
|
} else if (modifier_state == MackieControlProtocol::MODIFIER_SHIFT) {
|
|
|
|
i->second.shift = action;
|
|
|
|
} else if (modifier_state == MackieControlProtocol::MODIFIER_OPTION) {
|
|
|
|
i->second.option = action;
|
|
|
|
} else if (modifier_state == MackieControlProtocol::MODIFIER_CMDALT) {
|
|
|
|
i->second.cmdalt = action;
|
|
|
|
} else if (modifier_state == (MackieControlProtocol::MODIFIER_CONTROL|MackieControlProtocol::MODIFIER_SHIFT)) {
|
|
|
|
i->second.shiftcontrol = action;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (modifier_state == 0) {
|
|
|
|
i->second.plain = action;
|
|
|
|
}
|
2012-04-17 23:24:59 -04:00
|
|
|
|
2016-02-04 11:23:54 -05:00
|
|
|
edited = true;
|
|
|
|
|
2012-04-17 23:24:59 -04:00
|
|
|
save ();
|
2012-04-17 16:41:31 -04:00
|
|
|
}
|
|
|
|
|
2016-02-04 11:23:54 -05:00
|
|
|
string
|
|
|
|
DeviceProfile::name_when_edited (string const& base)
|
|
|
|
{
|
|
|
|
return string_compose ("%1 %2", base, edited_indicator);
|
|
|
|
}
|
|
|
|
|
|
|
|
string
|
2012-04-17 16:41:31 -04:00
|
|
|
DeviceProfile::name() const
|
|
|
|
{
|
2016-02-04 11:23:54 -05:00
|
|
|
if (edited) {
|
|
|
|
if (_name.find (edited_indicator) == string::npos) {
|
|
|
|
/* modify name to included edited indicator */
|
|
|
|
return name_when_edited (_name);
|
|
|
|
} else {
|
|
|
|
/* name already contains edited indicator */
|
|
|
|
return _name;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return _name;
|
|
|
|
}
|
2012-04-17 16:41:31 -04:00
|
|
|
}
|
2012-04-17 23:24:59 -04:00
|
|
|
|
|
|
|
void
|
|
|
|
DeviceProfile::set_path (const string& p)
|
|
|
|
{
|
|
|
|
_path = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX copied from libs/ardour/utils.cc */
|
|
|
|
|
|
|
|
static string
|
|
|
|
legalize_for_path (const string& str)
|
|
|
|
{
|
|
|
|
string::size_type pos;
|
|
|
|
string illegal_chars = "/\\"; /* DOS, POSIX. Yes, we're going to ignore HFS */
|
|
|
|
string legal;
|
|
|
|
|
|
|
|
legal = str;
|
|
|
|
pos = 0;
|
|
|
|
|
|
|
|
while ((pos = legal.find_first_of (illegal_chars, pos)) != string::npos) {
|
|
|
|
legal.replace (pos, 1, "_");
|
|
|
|
pos += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return string (legal);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
DeviceProfile::save ()
|
|
|
|
{
|
2012-06-23 01:09:44 -04:00
|
|
|
std::string fullpath = user_devprofile_directory();
|
2012-04-17 23:24:59 -04:00
|
|
|
|
2012-06-23 01:09:44 -04:00
|
|
|
if (g_mkdir_with_parents (fullpath.c_str(), 0755) < 0) {
|
|
|
|
error << string_compose(_("Session: cannot create user MCP profile folder \"%1\" (%2)"), fullpath, strerror (errno)) << endmsg;
|
2012-04-17 23:24:59 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-04 11:23:54 -05:00
|
|
|
fullpath = Glib::build_filename (fullpath, string_compose ("%1%2", legalize_for_path (name()), devprofile_suffix));
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2012-04-17 23:24:59 -04:00
|
|
|
XMLTree tree;
|
|
|
|
tree.set_root (&get_state());
|
2012-04-25 17:21:36 -04:00
|
|
|
|
2012-06-23 01:09:44 -04:00
|
|
|
if (!tree.write (fullpath)) {
|
|
|
|
error << string_compose ("MCP profile not saved to %1", fullpath) << endmsg;
|
2012-04-17 23:24:59 -04:00
|
|
|
}
|
|
|
|
}
|