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