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