remove old mcp config params, use new one
[ardour.git] / libs / surfaces / mackie / device_info.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 <cstring>
21 #include <glibmm/miscutils.h>
22
23 #include "pbd/xml++.h"
24 #include "pbd/error.h"
25 #include "pbd/pathscanner.h"
26
27 #include "ardour/filesystem_paths.h"
28
29 #include "device_info.h"
30
31 #include "i18n.h"
32
33 using namespace Mackie;
34 using namespace PBD;
35 using namespace ARDOUR;
36 using std::string;
37 using std::vector;
38
39 std::map<std::string,DeviceInfo> DeviceInfo::device_info;
40
41 DeviceInfo::DeviceInfo()
42         : _strip_cnt (8)
43         , _extenders (0)
44         , _has_two_character_display (true)
45         , _has_master_fader (true)
46         , _has_segmented_display (false)
47         , _has_timecode_display (true)
48         , _has_global_controls (true)
49         , _has_jog_wheel (true)
50         , _has_touch_sense_faders (true)
51         , _name (X_("Mackie Control Universal Pro"))
52 {
53         
54 }
55
56 DeviceInfo::~DeviceInfo()
57 {
58 }
59
60 int
61 DeviceInfo::set_state (const XMLNode& node, int /* version */)
62 {
63         const XMLProperty* prop;
64         const XMLNode* child;
65
66         if (node.name() != "MackieProtocolDevice") {
67                 return -1;
68         }
69
70         /* name is mandatory */
71  
72         if ((child = node.child ("Name")) != 0) {
73                 if ((prop = child->property ("value")) != 0) {
74                         _name = prop->value();
75                 } else {
76                         return -1;
77                 }
78         }
79
80         if ((child = node.child ("Strips")) != 0) {
81                 if ((prop = child->property ("value")) != 0) {
82                         if ((_strip_cnt = atoi (prop->value())) == 0) {
83                                 _strip_cnt = 8;
84                         }
85                 }
86         }
87
88         if ((child = node.child ("Extenders")) != 0) {
89                 if ((prop = child->property ("value")) != 0) {
90                         if ((_extenders = atoi (prop->value())) == 0) {
91                                 _extenders = 0;
92                         }
93                 }
94         }
95
96         if ((child = node.child ("TwoCharacterDisplay")) != 0) {
97                 if ((prop = child->property ("value")) != 0) {
98                         _has_two_character_display = string_is_affirmative (prop->value());
99                 }
100         }
101
102         if ((child = node.child ("MasterFader")) != 0) {
103                 if ((prop = child->property ("value")) != 0) {
104                         _has_master_fader = string_is_affirmative (prop->value());
105                 }
106         }
107
108         if ((child = node.child ("DisplaySegments")) != 0) {
109                 if ((prop = child->property ("value")) != 0) {
110                         _has_segmented_display = string_is_affirmative (prop->value());
111                 }
112         }
113
114         if ((child = node.child ("TimecodeDisplay")) != 0) {
115                 if ((prop = child->property ("value")) != 0) {
116                         _has_timecode_display = string_is_affirmative (prop->value());
117                 }
118         }
119
120         if ((child = node.child ("GlobalControls")) != 0) {
121                 if ((prop = child->property ("value")) != 0) {
122                         _has_global_controls = string_is_affirmative (prop->value());
123                 }
124         }
125
126         if ((child = node.child ("JogWheel")) != 0) {
127                 if ((prop = child->property ("value")) != 0) {
128                         _has_jog_wheel = string_is_affirmative (prop->value());
129                 }
130         }
131
132         if ((child = node.child ("TouchSenseFaders")) != 0) {
133                 if ((prop = child->property ("value")) != 0) {
134                         _has_touch_sense_faders = string_is_affirmative (prop->value());
135                 }
136         }
137
138         return 0;
139 }
140
141 const string&
142 DeviceInfo::name() const
143 {
144         return _name;
145 }
146
147 uint32_t
148 DeviceInfo::strip_cnt() const
149 {
150         return _strip_cnt;
151 }
152
153 uint32_t
154 DeviceInfo::extenders() const
155 {
156         return _extenders;
157 }
158
159 bool
160 DeviceInfo::has_master_fader() const
161 {
162         return _has_master_fader;
163 }
164
165 bool
166 DeviceInfo::has_two_character_display() const
167 {
168         return _has_two_character_display;
169 }
170
171 bool
172 DeviceInfo::has_segmented_display() const
173 {
174         return _has_segmented_display;
175 }
176
177 bool
178 DeviceInfo::has_timecode_display () const
179 {
180         return _has_timecode_display;
181 }
182
183 bool
184 DeviceInfo::has_global_controls () const
185 {
186         return _has_global_controls;
187 }
188
189 bool
190 DeviceInfo::has_jog_wheel () const
191 {
192         return _has_jog_wheel;
193 }
194
195 bool
196 DeviceInfo::has_touch_sense_faders () const
197 {
198         return _has_touch_sense_faders;
199 }
200
201 static const char * const devinfo_env_variable_name = "ARDOUR_MCP_DEVINFO_PATH";
202 static const char* const devinfo_dir_name = "mcp_devices";
203 static const char* const devinfo_suffix = ".xml";
204
205 static sys::path
206 system_devinfo_search_path ()
207 {
208         bool devinfo_path_defined = false;
209         sys::path spath_env (Glib::getenv (devinfo_env_variable_name, devinfo_path_defined));
210
211         if (devinfo_path_defined) {
212                 return spath_env;
213         }
214
215         SearchPath spath (system_data_search_path());
216         spath.add_subdirectory_to_paths(devinfo_dir_name);
217
218         // just return the first directory in the search path that exists
219         SearchPath::const_iterator i = std::find_if(spath.begin(), spath.end(), sys::exists);
220
221         if (i == spath.end()) return sys::path();
222
223         return *i;
224 }
225
226 static sys::path
227 user_devinfo_directory ()
228 {
229         sys::path p(user_config_directory());
230         p /= devinfo_dir_name;
231
232         return p;
233 }
234
235 static bool
236 devinfo_filter (const string &str, void */*arg*/)
237 {
238         return (str.length() > strlen(devinfo_suffix) &&
239                 str.find (devinfo_suffix) == (str.length() - strlen (devinfo_suffix)));
240 }
241
242 void
243 DeviceInfo::reload_device_info ()
244 {
245         DeviceInfo di;
246         vector<string> s;
247         vector<string *> *devinfos;
248         PathScanner scanner;
249         SearchPath spath (system_devinfo_search_path());
250         spath += user_devinfo_directory ();
251
252         devinfos = scanner (spath.to_string(), devinfo_filter, 0, false, true);
253         device_info.clear ();
254
255         if (!devinfos) {
256                 error << "No MCP device info files found using " << spath.to_string() << endmsg;
257                 std::cerr << "No MCP device info files found using " << spath.to_string() << std::endl;
258                 return;
259         }
260
261         if (devinfos->empty()) {
262                 error << "No MCP device info files found using " << spath.to_string() << endmsg;
263                 std::cerr << "No MCP device info files found using " << spath.to_string() << std::endl;
264                 return;
265         }
266
267         for (vector<string*>::iterator i = devinfos->begin(); i != devinfos->end(); ++i) {
268                 string fullpath = *(*i);
269
270                 XMLTree tree;
271
272
273                 if (!tree.read (fullpath.c_str())) {
274                         continue;
275                 }
276
277                 XMLNode* root = tree.root ();
278                 if (!root) {
279                         continue;
280                 }
281
282                 if (di.set_state (*root, 3000) == 0) { /* version is ignored for now */
283                         device_info[di.name()] = di;
284                         std::cerr << di << '\n';
285                 }
286         }
287
288         delete devinfos;
289 }
290
291 std::ostream& operator<< (std::ostream& os, const Mackie::DeviceInfo& di)
292 {
293         os << di.name() << ' ' 
294            << di.strip_cnt() << ' '
295            << di.extenders() << ' '
296                 ;
297         return os;
298 }