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