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