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