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