Bogus commit to comment commit 12689:
[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 <cstdlib>
21 #include <cstring>
22 #include <glibmm/miscutils.h>
23
24 #include "pbd/xml++.h"
25 #include "pbd/error.h"
26 #include "pbd/pathscanner.h"
27
28 #include "ardour/filesystem_paths.h"
29
30 #include "device_info.h"
31
32 #include "i18n.h"
33
34 using namespace Mackie;
35 using namespace PBD;
36 using namespace ARDOUR;
37 using std::string;
38 using std::vector;
39
40 std::map<std::string,DeviceInfo> DeviceInfo::device_info;
41
42 DeviceInfo::DeviceInfo()
43         : _strip_cnt (8)
44         , _extenders (0)
45         , _has_two_character_display (true)
46         , _has_master_fader (true)
47         , _has_timecode_display (true)
48         , _has_global_controls (true)
49         , _has_jog_wheel (true)
50         , _has_touch_sense_faders (true)
51         , _uses_logic_control_buttons (false)
52         , _uses_ipmidi (false)
53         , _no_handshake (false)
54         , _has_meters (true)
55         , _name (X_("Mackie Control Universal Pro"))
56 {
57         mackie_control_buttons ();
58 }
59
60 DeviceInfo::~DeviceInfo()
61 {
62 }
63
64 GlobalButtonInfo&
65 DeviceInfo::get_global_button(Button::ID id)
66 {
67         GlobalButtonsInfo::iterator it;
68
69         it = _global_buttons.find (id);
70
71         return it->second;
72 }
73
74 std::string&
75 DeviceInfo::get_global_button_name(Button::ID id)
76 {
77         GlobalButtonsInfo::iterator it;
78         
79         it = _global_buttons.find (id);
80         if (it == _global_buttons.end ()) {
81                 _global_button_name = "";
82                 return _global_button_name;
83         } else {
84                 return it->second.label;
85         }
86 }
87
88 void
89 DeviceInfo::mackie_control_buttons ()
90 {
91         _global_buttons.clear ();
92         shared_buttons ();
93         
94         _global_buttons[Button::UserA] = GlobalButtonInfo ("Rear Panel User Switch 1", "user", 0x66);
95         _global_buttons[Button::UserB] = GlobalButtonInfo ("Rear Panel User Switch 2", "user", 0x67);
96         
97         //TODO Implement "rear panel external control": a connection for a resistive 
98         //TODO element expression pedal . Message: 0xb0 0x2e 0xVV where 0xVV = external 
99         //TODO controller position value (0x00 to 0x7f)
100         
101         _strip_buttons[Button::RecEnable] = StripButtonInfo (0x0, "Rec");
102 }
103
104 void
105 DeviceInfo::logic_control_buttons ()
106 {
107         _global_buttons.clear ();
108         shared_buttons ();
109         
110         _global_buttons[Button::UserA] = GlobalButtonInfo ("User Switch A", "user", 0x66);
111         _global_buttons[Button::UserB] = GlobalButtonInfo ("User Switch B", "user", 0x67);
112
113         _strip_buttons[Button::RecEnable] = StripButtonInfo (0x0, "Rec/Rdy");
114 }
115
116 void
117 DeviceInfo::shared_buttons ()
118 {
119         _global_buttons[Button::Track] = GlobalButtonInfo ("Track", "assignment", 0x28);
120         _global_buttons[Button::Send] = GlobalButtonInfo ("Send", "assignment", 0x29);
121         _global_buttons[Button::Pan] = GlobalButtonInfo ("Pan/Surround", "assignment", 0x2a);
122         _global_buttons[Button::Plugin] = GlobalButtonInfo ("Plugin", "assignment", 0x2b);
123         _global_buttons[Button::Eq] = GlobalButtonInfo ("Eq", "assignment", 0x2c);
124         _global_buttons[Button::Dyn] = GlobalButtonInfo ("Instrument", "assignment", 0x2d);
125         
126         _global_buttons[Button::Left] = GlobalButtonInfo ("Bank Left", "bank", 0x2e);
127         _global_buttons[Button::Right] = GlobalButtonInfo ("Bank Right", "bank", 0x2f);
128         _global_buttons[Button::ChannelLeft] = GlobalButtonInfo ("Channel Left", "bank", 0x30);
129         _global_buttons[Button::ChannelRight] = GlobalButtonInfo ("Channel Right", "bank", 0x31);
130         _global_buttons[Button::Flip] = GlobalButtonInfo ("Flip", "assignment", 0x32);
131         _global_buttons[Button::View] = GlobalButtonInfo ("Global View", "global view", 0x33);
132
133         _global_buttons[Button::NameValue] = GlobalButtonInfo ("Name/Value", "display", 0x34);
134         _global_buttons[Button::TimecodeBeats] = GlobalButtonInfo ("Timecode/Beats", "display", 0x35);
135         
136         _global_buttons[Button::F1] = GlobalButtonInfo ("F1", "function select", 0x36);
137         _global_buttons[Button::F2] = GlobalButtonInfo ("F2", "function select", 0x37);
138         _global_buttons[Button::F3] = GlobalButtonInfo ("F3", "function select", 0x38);
139         _global_buttons[Button::F4] = GlobalButtonInfo ("F4", "function select", 0x39);
140         _global_buttons[Button::F5] = GlobalButtonInfo ("F5", "function select", 0x3a);
141         _global_buttons[Button::F6] = GlobalButtonInfo ("F6", "function select", 0x3b);
142         _global_buttons[Button::F7] = GlobalButtonInfo ("F7", "function select", 0x3c);
143         _global_buttons[Button::F8] = GlobalButtonInfo ("F8", "function select", 0x3d);
144         
145         _global_buttons[Button::MidiTracks] = GlobalButtonInfo ("MIDI Tracks", "global view", 0x3e);
146         _global_buttons[Button::Inputs] = GlobalButtonInfo ("Inputs", "global view", 0x3f);
147         _global_buttons[Button::AudioTracks] = GlobalButtonInfo ("Audio Tracks", "global view", 0x40);
148         _global_buttons[Button::AudioInstruments] = GlobalButtonInfo ("Audio Instruments", "global view", 0x41);
149         _global_buttons[Button::Aux] = GlobalButtonInfo ("Aux", "global view", 0x42);
150         _global_buttons[Button::Busses] = GlobalButtonInfo ("Busses", "global view", 0x43);
151         _global_buttons[Button::Outputs] = GlobalButtonInfo ("Outputs", "global view", 0x44);
152         _global_buttons[Button::User] = GlobalButtonInfo ("User", "global view", 0x45);
153         
154         _global_buttons[Button::Shift] = GlobalButtonInfo ("Shift", "modifiers", 0x46);
155         _global_buttons[Button::Option] = GlobalButtonInfo ("Option", "modifiers", 0x47);
156         _global_buttons[Button::Ctrl] = GlobalButtonInfo ("Ctrl", "modifiers", 0x48);
157         _global_buttons[Button::CmdAlt] = GlobalButtonInfo ("Cmd/Alt", "modifiers", 0x49);
158         
159         _global_buttons[Button::Read] = GlobalButtonInfo ("Read/Off", "automation", 0x4a);
160         _global_buttons[Button::Write] = GlobalButtonInfo ("Write", "automation", 0x4b);
161         _global_buttons[Button::Trim] = GlobalButtonInfo ("Trim", "automation", 0x4c);
162         _global_buttons[Button::Touch] = GlobalButtonInfo ("Touch", "automation", 0x4d);
163         _global_buttons[Button::Latch] = GlobalButtonInfo ("Latch", "automation", 0x4e);
164         _global_buttons[Button::Grp] = GlobalButtonInfo ("Group", "automation", 0x4f);
165         
166         _global_buttons[Button::Save] = GlobalButtonInfo ("Save", "utilities", 0x50);
167         _global_buttons[Button::Undo] = GlobalButtonInfo ("Undo", "utilities", 0x51);
168         _global_buttons[Button::Cancel] = GlobalButtonInfo ("Cancel", "utilities", 0x52);
169         _global_buttons[Button::Enter] = GlobalButtonInfo ("Enter", "utilities", 0x53);
170
171         _global_buttons[Button::Marker] = GlobalButtonInfo ("Marker", "transport", 0x54);
172         _global_buttons[Button::Nudge] = GlobalButtonInfo ("Nudge", "transport", 0x55);
173         _global_buttons[Button::Loop] = GlobalButtonInfo ("Cycle", "transport", 0x56);
174         _global_buttons[Button::Drop] = GlobalButtonInfo ("Drop", "transport", 0x57);
175         _global_buttons[Button::Replace] = GlobalButtonInfo ("Replace", "transport", 0x58);
176         _global_buttons[Button::Click] = GlobalButtonInfo ("Click", "transport", 0x59);
177         _global_buttons[Button::ClearSolo] = GlobalButtonInfo ("Solo", "transport", 0x5a);
178         
179         _global_buttons[Button::Rewind] = GlobalButtonInfo ("Rewind", "transport", 0x5b);
180         _global_buttons[Button::Ffwd] = GlobalButtonInfo ("Fast Fwd", "transport", 0x5c);
181         _global_buttons[Button::Stop] = GlobalButtonInfo ("Stop", "transport", 0x5d);
182         _global_buttons[Button::Play] = GlobalButtonInfo ("Play", "transport", 0x5e);
183         _global_buttons[Button::Record] = GlobalButtonInfo ("Record", "transport", 0x5f);
184         
185         _global_buttons[Button::CursorUp] = GlobalButtonInfo ("Cursor Up", "cursor", 0x60);
186         _global_buttons[Button::CursorDown] = GlobalButtonInfo ("Cursor Down", "cursor", 0x61);
187         _global_buttons[Button::CursorLeft] = GlobalButtonInfo ("Cursor Left", "cursor", 0x62);
188         _global_buttons[Button::CursorRight] = GlobalButtonInfo ("Cursor Right", "cursor", 0x63);
189         _global_buttons[Button::Zoom] = GlobalButtonInfo ("Zoom", "cursor", 0x64);
190         _global_buttons[Button::Scrub] = GlobalButtonInfo ("Scrub", "cursor", 0x65);
191
192         _strip_buttons[Button::Solo] = StripButtonInfo (0x08, "Solo");
193         _strip_buttons[Button::Mute] = StripButtonInfo (0x10, "Mute");
194         _strip_buttons[Button::Select] = StripButtonInfo (0x18, "Select");
195         _strip_buttons[Button::VSelect] = StripButtonInfo (0x20, "V-Select");
196
197         _strip_buttons[Button::FaderTouch] = StripButtonInfo (0x68, "Fader Touch");
198
199         _global_buttons[Button::MasterFaderTouch] = GlobalButtonInfo ("Master Fader Touch", "master", 0x70);
200 }
201
202 int
203 DeviceInfo::set_state (const XMLNode& node, int /* version */)
204 {
205         const XMLProperty* prop;
206         const XMLNode* child;
207
208         if (node.name() != "MackieProtocolDevice") {
209                 return -1;
210         }
211
212         /* name is mandatory */
213         if ((child = node.child ("Name")) != 0) {
214                 if ((prop = child->property ("value")) != 0) {
215                         _name = prop->value();
216                 } else {
217                         return -1;
218                 }
219         }
220
221         /* strip count is mandatory */
222         if ((child = node.child ("Strips")) != 0) {
223                 if ((prop = child->property ("value")) != 0) {
224                         if ((_strip_cnt = atoi (prop->value())) == 0) {
225                                 _strip_cnt = 8;
226                         }
227                 }
228         } else {
229                 return -1;
230         }
231
232         if ((child = node.child ("Extenders")) != 0) {
233                 if ((prop = child->property ("value")) != 0) {
234                         if ((_extenders = atoi (prop->value())) == 0) {
235                                 _extenders = 0;
236                         }
237                 }
238         }
239
240         if ((child = node.child ("TwoCharacterDisplay")) != 0) {
241                 if ((prop = child->property ("value")) != 0) {
242                         _has_two_character_display = string_is_affirmative (prop->value());
243                 }
244         }
245
246         if ((child = node.child ("MasterFader")) != 0) {
247                 if ((prop = child->property ("value")) != 0) {
248                         _has_master_fader = string_is_affirmative (prop->value());
249                 }
250         }
251
252         if ((child = node.child ("TimecodeDisplay")) != 0) {
253                 if ((prop = child->property ("value")) != 0) {
254                         _has_timecode_display = string_is_affirmative (prop->value());
255                 }
256         } else {
257                 _has_timecode_display = false;
258         }
259
260         if ((child = node.child ("GlobalControls")) != 0) {
261                 if ((prop = child->property ("value")) != 0) {
262                         _has_global_controls = string_is_affirmative (prop->value());
263                 }
264         } else {
265                 _has_global_controls = false;
266         }
267
268         if ((child = node.child ("JogWheel")) != 0) {
269                 if ((prop = child->property ("value")) != 0) {
270                         _has_jog_wheel = string_is_affirmative (prop->value());
271                 }
272         } else {
273                 _has_jog_wheel = false;
274         }
275
276         if ((child = node.child ("TouchSenseFaders")) != 0) {
277                 if ((prop = child->property ("value")) != 0) {
278                         _has_touch_sense_faders = string_is_affirmative (prop->value());
279                 }
280         } else {
281                 _has_touch_sense_faders = false;
282         }
283
284         if ((child = node.child ("UsesIPMIDI")) != 0) {
285                 if ((prop = child->property ("value")) != 0) {
286                         _uses_ipmidi = string_is_affirmative (prop->value());
287                 }
288         } else {
289                 _uses_ipmidi = false;
290         }
291
292         if ((child = node.child ("NoHandShake")) != 0) {
293                 if ((prop = child->property ("value")) != 0) {
294                         _no_handshake = string_is_affirmative (prop->value());
295                 }
296         } else {
297                 _no_handshake = false;
298         }
299
300         if ((child = node.child ("HasMeters")) != 0) {
301                 if ((prop = child->property ("value")) != 0) {
302                         _has_meters = string_is_affirmative (prop->value());
303                 }
304         } else {
305                 _has_meters = true;
306         }
307
308         if ((child = node.child ("LogicControlButtons")) != 0) {
309                 if ((prop = child->property ("value")) != 0) {
310                         _uses_logic_control_buttons = string_is_affirmative (prop->value());
311
312                         if (_uses_logic_control_buttons) {
313                                 logic_control_buttons();
314                         } else {
315                                 mackie_control_buttons ();
316                         }
317                 }
318         }
319
320         if ((child = node.child ("Buttons")) != 0) {
321                 XMLNodeConstIterator i;
322                 const XMLNodeList& nlist (child->children());
323
324                 for (i = nlist.begin(); i != nlist.end(); ++i) {
325                         if ((*i)->name() == "GlobalButton") {
326                                 if ((prop = (*i)->property ("name")) != 0) {
327                                         int id = Button::name_to_id (prop->value());
328                                         if (id >= 0) {
329                                                 Button::ID bid = (Button::ID) id;
330                                                 if ((prop = (*i)->property ("id")) != 0) {
331                                                         int val = strtol (prop->value().c_str(), 0, 0);
332                                                         std::map<Button::ID,GlobalButtonInfo>::iterator b = _global_buttons.find (bid);
333                                                         if (b != _global_buttons.end()) {
334                                                                 b->second.id = val;
335                                                                 
336                                                                 if ((prop = (*i)->property ("label")) != 0) {
337                                                                         b->second.label = prop->value();
338                                                                 }
339                                                         }
340                                                 }
341                                         }
342                                         
343                                 }
344                                 
345                         } else if ((*i)->name() == "StripButton") {
346                                 if ((prop = (*i)->property ("name")) != 0) {
347                                         int id = Button::name_to_id (prop->value());
348                                         if (id >= 0) {
349                                                 Button::ID bid = (Button::ID) id;
350                                                 if ((prop = (*i)->property ("baseid")) != 0) {
351                                                         int val = strtol (prop->value().c_str(), 0, 0);
352                                                         std::map<Button::ID,StripButtonInfo>::iterator b = _strip_buttons.find (bid);
353                                                         if (b != _strip_buttons.end()) {
354                                                                 b->second.base_id = val;
355                                                         }
356                                                 }
357                                         }
358                                         
359                                 }
360                                 
361                         }
362                 }
363         }
364
365         return 0;
366 }
367
368 const string&
369 DeviceInfo::name() const
370 {
371         return _name;
372 }
373
374 uint32_t
375 DeviceInfo::strip_cnt() const
376 {
377         return _strip_cnt;
378 }
379
380 uint32_t
381 DeviceInfo::extenders() const
382 {
383         return _extenders;
384 }
385
386 bool
387 DeviceInfo::has_master_fader() const
388 {
389         return _has_master_fader;
390 }
391
392 bool
393 DeviceInfo::has_meters() const
394 {
395         return _has_meters;
396 }
397
398 bool
399 DeviceInfo::has_two_character_display() const
400 {
401         return _has_two_character_display;
402 }
403
404 bool
405 DeviceInfo::has_timecode_display () const
406 {
407         return _has_timecode_display;
408 }
409
410 bool
411 DeviceInfo::uses_ipmidi () const
412 {
413         return _uses_ipmidi;
414 }
415
416 bool
417 DeviceInfo::has_global_controls () const
418 {
419         return _has_global_controls;
420 }
421
422 bool
423 DeviceInfo::has_jog_wheel () const
424 {
425         return _has_jog_wheel;
426 }
427
428 bool
429 DeviceInfo::no_handshake () const
430 {
431         return _no_handshake;
432 }
433
434 bool
435 DeviceInfo::has_touch_sense_faders () const
436 {
437         return _has_touch_sense_faders;
438 }
439
440 static const char * const devinfo_env_variable_name = "ARDOUR_MCP_PATH";
441 static const char* const devinfo_dir_name = "mcp";
442 static const char* const devinfo_suffix = ".device";
443
444 static SearchPath
445 devinfo_search_path ()
446 {
447         bool devinfo_path_defined = false;
448         sys::path spath_env (Glib::getenv (devinfo_env_variable_name, devinfo_path_defined));
449
450         if (devinfo_path_defined) {
451                 return spath_env;
452         }
453
454         SearchPath spath (ardour_data_search_path());
455         spath.add_subdirectory_to_paths(devinfo_dir_name);
456
457         return spath;
458 }
459
460 static bool
461 devinfo_filter (const string &str, void */*arg*/)
462 {
463         return (str.length() > strlen(devinfo_suffix) &&
464                 str.find (devinfo_suffix) == (str.length() - strlen (devinfo_suffix)));
465 }
466
467 void
468 DeviceInfo::reload_device_info ()
469 {
470         DeviceInfo di;
471         vector<string> s;
472         vector<string *> *devinfos;
473         PathScanner scanner;
474         SearchPath spath (devinfo_search_path());
475
476         devinfos = scanner (spath.to_string(), devinfo_filter, 0, false, true);
477         device_info.clear ();
478
479         if (!devinfos) {
480                 error << "No MCP device info files found using " << spath.to_string() << endmsg;
481                 std::cerr << "No MCP device info files found using " << spath.to_string() << std::endl;
482                 return;
483         }
484
485         if (devinfos->empty()) {
486                 error << "No MCP device info files found using " << spath.to_string() << endmsg;
487                 std::cerr << "No MCP device info files found using " << spath.to_string() << std::endl;
488                 return;
489         }
490
491         for (vector<string*>::iterator i = devinfos->begin(); i != devinfos->end(); ++i) {
492                 string fullpath = *(*i);
493
494                 XMLTree tree;
495
496
497                 if (!tree.read (fullpath.c_str())) {
498                         continue;
499                 }
500
501                 XMLNode* root = tree.root ();
502                 if (!root) {
503                         continue;
504                 }
505
506                 if (di.set_state (*root, 3000) == 0) { /* version is ignored for now */
507                         device_info[di.name()] = di;
508                 }
509         }
510
511         delete devinfos;
512 }
513
514 std::ostream& operator<< (std::ostream& os, const Mackie::DeviceInfo& di)
515 {
516         os << di.name() << ' ' 
517            << di.strip_cnt() << ' '
518            << di.extenders() << ' '
519                 ;
520         return os;
521 }