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