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