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