strip X specific from keyboard.cc; fix up many buttons to avoid prelight (mostly...
[ardour.git] / libs / ardour / globals.cc
1 /*
2     Copyright (C) 2000 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id: globals.cc 935 2006-09-29 21:39:39Z paul $
19 */
20
21 #include <cstdio> // Needed so that libraptor (included in lrdf) won't complain
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <locale.h>
27
28 #ifdef VST_SUPPORT
29 #include <fst.h>
30 #endif
31
32 #include <lrdf.h>
33
34 #include <pbd/error.h>
35 #include <pbd/id.h>
36 #include <pbd/strsplit.h>
37
38 #include <midi++/port.h>
39 #include <midi++/port_request.h>
40 #include <midi++/manager.h>
41 #include <midi++/mmc.h>
42
43 #include <ardour/ardour.h>
44 #include <ardour/audio_library.h>
45 #include <ardour/configuration.h>
46 #include <ardour/plugin_manager.h>
47 #include <ardour/audiosource.h>
48 #include <ardour/utils.h>
49 #include <ardour/session.h>
50 #include <ardour/control_protocol_manager.h>
51
52 #ifdef HAVE_LIBLO
53 #include <ardour/osc.h>
54 #endif
55
56 #include <ardour/mix.h>
57
58 #if defined (__APPLE__)
59        #include <Carbon/Carbon.h> // For Gestalt
60 #endif
61        
62 #include "i18n.h"
63
64 ARDOUR::Configuration* ARDOUR::Config = 0;
65 ARDOUR::AudioLibrary* ARDOUR::Library = 0;
66
67 #ifdef HAVE_LIBLO
68 ARDOUR::OSC* ARDOUR::osc = 0;
69 #endif
70
71 using namespace ARDOUR;
72 using namespace std;
73 using namespace PBD;
74
75 MIDI::Port *default_mmc_port = 0;
76 MIDI::Port *default_mtc_port = 0;
77 MIDI::Port *default_midi_port = 0;
78
79 Change ARDOUR::StartChanged = ARDOUR::new_change ();
80 Change ARDOUR::LengthChanged = ARDOUR::new_change ();
81 Change ARDOUR::PositionChanged = ARDOUR::new_change ();
82 Change ARDOUR::NameChanged = ARDOUR::new_change ();
83 Change ARDOUR::BoundsChanged = Change (0); // see init(), below
84
85 #ifdef HAVE_LIBLO
86 static int
87 setup_osc ()
88 {
89         /* no real cost to creating this object, and it avoids
90            conditionals anywhere that uses it 
91         */
92         
93         osc = new OSC (Config->get_osc_port());
94         
95         if (Config->get_use_osc ()) {
96                 return osc->start ();
97         } else {
98                 return 0;
99         }
100 }
101 #endif
102
103 static int 
104 setup_midi ()
105 {
106         std::map<string,Configuration::MidiPortDescriptor*>::iterator i;
107         int nports;
108
109         if ((nports = Config->midi_ports.size()) == 0) {
110                 warning << _("no MIDI ports specified: no MMC or MTC control possible") << endmsg;
111                 return 0;
112         }
113
114         for (i = Config->midi_ports.begin(); i != Config->midi_ports.end(); ++i) {
115                 Configuration::MidiPortDescriptor* port_descriptor;
116
117                 port_descriptor = (*i).second;
118
119                 MIDI::PortRequest request (port_descriptor->device, 
120                                            port_descriptor->tag, 
121                                            port_descriptor->mode, 
122                                            port_descriptor->type);
123
124                 if (request.status != MIDI::PortRequest::OK) {
125                         error << string_compose(_("MIDI port specifications for \"%1\" are not understandable."), port_descriptor->tag) << endmsg;
126                         continue;
127                 }
128                 
129                 MIDI::Manager::instance()->add_port (request);
130
131                 nports++;
132         }
133
134         if (nports > 1) {
135
136                 /* More than one port, so try using specific names for each port */
137
138                 map<string,Configuration::MidiPortDescriptor *>::iterator i;
139
140                 if (Config->get_mmc_port_name() != N_("default")) {
141                         default_mmc_port =  MIDI::Manager::instance()->port (Config->get_mmc_port_name());
142                 } 
143
144                 if (Config->get_mtc_port_name() != N_("default")) {
145                         default_mtc_port =  MIDI::Manager::instance()->port (Config->get_mtc_port_name());
146                 } 
147
148                 if (Config->get_midi_port_name() != N_("default")) {
149                         default_midi_port =  MIDI::Manager::instance()->port (Config->get_midi_port_name());
150                 } 
151                 
152                 /* If that didn't work, just use the first listed port */
153
154                 if (default_mmc_port == 0) {
155                         default_mmc_port = MIDI::Manager::instance()->port (0);
156                 }
157
158                 if (default_mtc_port == 0) {
159                         default_mtc_port = MIDI::Manager::instance()->port (0);
160                 }
161
162                 if (default_midi_port == 0) {
163                         default_midi_port = MIDI::Manager::instance()->port (0);
164                 }
165                 
166         } else {
167
168                 /* Only one port described, so use it for both MTC and MMC */
169
170                 default_mmc_port = MIDI::Manager::instance()->port (0);
171                 default_mtc_port = default_mmc_port;
172                 default_midi_port = default_mmc_port;
173         }
174
175         if (default_mmc_port == 0) {
176                 warning << string_compose (_("No MMC control (MIDI port \"%1\" not available)"), Config->get_mmc_port_name()) 
177                         << endmsg;
178                 return 0;
179         } 
180
181         if (default_mtc_port == 0) {
182                 warning << string_compose (_("No MTC support (MIDI port \"%1\" not available)"), Config->get_mtc_port_name()) 
183                         << endmsg;
184         }
185
186         if (default_midi_port == 0) {
187                 warning << string_compose (_("No MIDI parameter support (MIDI port \"%1\" not available)"), Config->get_midi_port_name()) 
188                         << endmsg;
189         }
190
191         return 0;
192 }
193
194 void
195 setup_hardware_optimization (bool try_optimization)
196 {
197         bool generic_mix_functions = true;
198
199
200         if (try_optimization) {
201
202 #if defined (ARCH_X86) && defined (BUILD_SSE_OPTIMIZATIONS)
203         
204                 unsigned long use_sse = 0;
205
206 #ifndef USE_X86_64_ASM
207                 asm (
208                                  "mov $1, %%eax\n"
209                                  "pushl %%ebx\n"
210                                  "cpuid\n"
211                                  "movl %%edx, %0\n"
212                                  "popl %%ebx\n"
213                              : "=r" (use_sse)
214                              : 
215                          : "%eax", "%ecx", "%edx", "memory");
216
217 #else
218
219                 asm (
220                                  "pushq %%rbx\n"
221                                  "movq $1, %%rax\n"
222                                  "cpuid\n"
223                                  "movq %%rdx, %0\n"
224                                  "popq %%rbx\n"
225                              : "=r" (use_sse)
226                              : 
227                          : "%rax", "%rcx", "%rdx", "memory");
228
229 #endif /* USE_X86_64_ASM */
230                 use_sse &= (1 << 25); // bit 25 = SSE support
231                 
232                 if (use_sse) {
233                         info << "Using SSE optimized routines" << endmsg;
234         
235                         // SSE SET
236                         Session::compute_peak           = x86_sse_compute_peak;
237                         Session::apply_gain_to_buffer   = x86_sse_apply_gain_to_buffer;
238                         Session::mix_buffers_with_gain  = x86_sse_mix_buffers_with_gain;
239                         Session::mix_buffers_no_gain    = x86_sse_mix_buffers_no_gain;
240
241                         generic_mix_functions = false;
242
243                 }
244
245 #elif defined (__APPLE__) && defined (BUILD_VECLIB_OPTIMIZATIONS)
246                 long sysVersion = 0;
247
248                 if (noErr != Gestalt(gestaltSystemVersion, &sysVersion))
249                         sysVersion = 0;
250
251                 if (sysVersion >= 0x00001040) { // Tiger at least
252                         Session::compute_peak           = veclib_compute_peak;
253                         Session::apply_gain_to_buffer   = veclib_apply_gain_to_buffer;
254                         Session::mix_buffers_with_gain  = veclib_mix_buffers_with_gain;
255                         Session::mix_buffers_no_gain    = veclib_mix_buffers_no_gain;
256
257                         generic_mix_functions = false;
258
259                         info << "Apple VecLib H/W specific optimizations in use" << endmsg;
260                 }
261 #endif
262         }
263
264         if (generic_mix_functions) {
265
266                 Session::compute_peak                   = compute_peak;
267                 Session::apply_gain_to_buffer   = apply_gain_to_buffer;
268                 Session::mix_buffers_with_gain  = mix_buffers_with_gain;
269                 Session::mix_buffers_no_gain    = mix_buffers_no_gain;
270                 
271                 info << "No H/W specific optimizations in use" << endmsg;
272         }
273 }
274
275 int
276 ARDOUR::init (bool use_vst, bool try_optimization)
277 {
278         extern void setup_enum_writer ();
279
280         (void) bindtextdomain(PACKAGE, LOCALEDIR);
281
282         setup_enum_writer ();
283
284         lrdf_init();
285         Library = new AudioLibrary;
286
287         Config = new Configuration;
288
289         if (Config->load_state ()) {
290                 return -1;
291         }
292
293         Config->set_use_vst (use_vst);
294
295         if (setup_midi ()) {
296                 return -1;
297         }
298     
299 #ifdef HAVE_LIBLO
300         if (setup_osc ()) {
301                 return -1;
302         }
303 #endif
304
305 #ifdef VST_SUPPORT
306         if (Config->get_use_vst() && fst_init ()) {
307                 return -1;
308         }
309 #endif
310
311         setup_hardware_optimization (try_optimization);
312
313         /* singleton - first object is "it" */
314         new PluginManager ();
315         
316         /* singleton - first object is "it" */
317         new ControlProtocolManager ();
318         ControlProtocolManager::instance().discover_control_protocols (Session::control_protocol_path());
319
320         XMLNode* node;
321         if ((node = Config->control_protocol_state()) != 0) {
322                 ControlProtocolManager::instance().set_state (*node);
323         }
324         
325         BoundsChanged = Change (StartChanged|PositionChanged|LengthChanged);
326
327         return 0;
328 }
329
330 int
331 ARDOUR::cleanup ()
332 {
333         delete Library;
334         lrdf_cleanup ();
335         delete &ControlProtocolManager::instance();
336         return 0;
337 }
338
339
340 microseconds_t
341 ARDOUR::get_microseconds ()
342 {
343         /* XXX need JACK to export its functionality */
344
345         struct timeval now;
346         gettimeofday (&now, 0);
347         return now.tv_sec * 1000000ULL + now.tv_usec;
348 }
349
350 ARDOUR::Change
351 ARDOUR::new_change ()
352 {
353         Change c;
354         static uint32_t change_bit = 1;
355
356         /* catch out-of-range */
357         if (!change_bit)
358         {
359                 fatal << _("programming error: ")
360                         << "change_bit out of range in ARDOUR::new_change()"
361                         << endmsg;
362                 /*NOTREACHED*/
363         }
364
365         c = Change (change_bit);
366         change_bit <<= 1;       // if it shifts too far, change_bit == 0
367
368         return c;
369 }
370
371 string
372 ARDOUR::get_ardour_revision ()
373 {
374         return "$Rev$";
375 }
376
377 string
378 ARDOUR::get_user_ardour_path ()
379 {
380         string path;
381         char* envvar;
382         
383         if ((envvar = getenv ("HOME")) == 0 || strlen (envvar) == 0) {
384                 return "/";
385         }
386                 
387         path = envvar;
388         path += "/.ardour2/";
389
390         /* create it if necessary */
391
392         if (g_mkdir_with_parents (path.c_str (), 0755)) {
393                 throw exception ();
394         }
395
396         return path;
397 }
398
399 string
400 ARDOUR::get_system_data_path ()
401 {
402         string path;
403
404         char *envvar;
405
406         if ((envvar = getenv ("ARDOUR_DATA_PATH")) != 0) {
407                 path = envvar;
408         } else {
409                 path += DATA_DIR;
410                 path += "/ardour2/";
411         }
412         
413         return path;
414 }
415
416 string
417 ARDOUR::get_system_module_path ()
418 {
419         string path;
420         char *envvar;
421
422         if ((envvar = getenv ("ARDOUR_MODULE_PATH")) != 0) {
423                 path = envvar;
424         } else {
425                 path += MODULE_DIR;
426                 path += "/ardour2/";
427         }
428         
429         return path;
430 }
431
432 static string
433 find_file (string name, string dir, string subdir = "")
434 {
435         string path;
436         char* envvar = getenv("ARDOUR_PATH");
437
438         /* 1st attempt: any directory in ARDOUR_PATH */
439         
440         if (envvar != 0) {
441
442                 vector<string> split_path;
443         
444                 split (envvar, split_path, ':');
445                 
446                 for (vector<string>::iterator i = split_path.begin(); i != split_path.end(); ++i) {
447                         path = *i;
448                         path += "/" + name;
449                         if (access (path.c_str(), R_OK) == 0) {
450                                 // cerr << "Using file " << path << " found in ARDOUR_PATH." << endl;
451                                 return path;
452                         }
453                 }
454         }
455
456         /* 2nd attempt: ~/.ardour/ */
457
458         path = get_user_ardour_path();
459                 
460         if (subdir.length()) {
461                 path += subdir + "/";
462         }
463                 
464         path += name;
465         if (access (path.c_str(), R_OK) == 0) {
466                 return path;
467         }
468
469         /* 3rd attempt: dir/... */
470         
471         path = dir;
472         path += "/ardour2/";
473         
474         if (subdir.length()) {
475                 path += subdir + "/";
476         }
477         
478         path += name;
479         
480         if (access (path.c_str(), R_OK) == 0) {
481                 return path;
482         }
483
484         return "";
485 }
486
487 string
488 ARDOUR::find_config_file (string name)
489 {
490         char* envvar;
491
492         if ((envvar = getenv("ARDOUR_CONFIG_PATH")) == 0) {
493                 envvar = CONFIG_DIR;
494         }
495
496         return find_file (name, envvar);
497 }
498
499 string
500 ARDOUR::find_data_file (string name, string subdir)
501 {
502         char* envvar;
503         if ((envvar = getenv("ARDOUR_DATA_PATH")) == 0) {
504                 envvar = DATA_DIR;
505         }
506
507         return find_file (name, envvar, subdir);
508 }
509
510 ARDOUR::LocaleGuard::LocaleGuard (const char* str)
511 {
512         old = strdup (setlocale (LC_NUMERIC, NULL));
513         if (strcmp (old, str)) {
514                 setlocale (LC_NUMERIC, str);
515         } 
516 }
517
518 ARDOUR::LocaleGuard::~LocaleGuard ()
519 {
520         setlocale (LC_NUMERIC, old);
521         free ((char*)old);
522 }
523
524 ARDOUR::OverlapType
525 ARDOUR::coverage (nframes_t sa, nframes_t ea, 
526                   nframes_t sb, nframes_t eb)
527 {
528         /* OverlapType returned reflects how the second (B)
529            range overlaps the first (A).
530
531            The diagrams show various relative placements
532            of A and B for each OverlapType.
533
534            Notes:
535               Internal: the start points cannot coincide
536               External: the start and end points can coincide
537               Start: end points can coincide
538               End: start points can coincide
539
540            XXX Logically, Internal should disallow end
541            point equality.
542         */
543
544         /*
545              |--------------------|   A
546                   |------|            B
547                 |-----------------|   B
548
549
550              "B is internal to A"               
551
552         */
553 #ifdef OLD_COVERAGE
554         if ((sb >= sa) && (eb <= ea)) {
555 #else
556         if ((sb > sa) && (eb <= ea)) {
557 #endif
558                 return OverlapInternal;
559         }
560
561         /*
562              |--------------------|   A
563            ----|                      B
564            -----------------------|   B
565            --|                        B
566            
567              "B overlaps the start of A"
568
569         */
570
571         if ((eb >= sa) && (eb <= ea)) {
572                 return OverlapStart;
573         }
574         /* 
575              |---------------------|  A
576                    |----------------- B
577              |----------------------- B    
578                                    |- B
579
580             "B overlaps the end of A"                              
581
582         */
583         if ((sb >= sa) && (sb <= ea)) {
584                 return OverlapEnd;
585         }
586         /*
587              |--------------------|     A
588            --------------------------  B   
589              |-----------------------  B
590             ----------------------|    B
591              |--------------------|    B
592
593
594            "B overlaps all of A"
595         */
596         if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
597                 return OverlapExternal;
598         }
599
600         return OverlapNone;
601 }
602
603 /* not sure where to put these */
604
605 template<class T>
606 std::istream& int_to_type (std::istream& o, T& hf) {
607         int val;
608         o >> val;
609         hf = (T) val;
610         return o;
611 }
612
613 std::istream& operator>>(std::istream& o, HeaderFormat& var) { return int_to_type<HeaderFormat> (o, var); }
614 std::istream& operator>>(std::istream& o, SampleFormat& var) { return int_to_type<SampleFormat> (o, var); }
615 std::istream& operator>>(std::istream& o, AutoConnectOption& var) { return int_to_type<AutoConnectOption> (o, var); }
616 std::istream& operator>>(std::istream& o, MonitorModel& var) { return int_to_type<MonitorModel> (o, var); }
617 std::istream& operator>>(std::istream& o, EditMode& var) { return int_to_type<EditMode> (o, var); }
618 std::istream& operator>>(std::istream& o, SoloModel& var) { return int_to_type<SoloModel> (o, var); }
619 std::istream& operator>>(std::istream& o, LayerModel& var) { return int_to_type<LayerModel> (o, var); }
620 std::istream& operator>>(std::istream& o, CrossfadeModel& var) { return int_to_type<CrossfadeModel> (o, var); }
621 std::istream& operator>>(std::istream& o, SlaveSource& var) { return int_to_type<SlaveSource> (o, var); }
622 std::istream& operator>>(std::istream& o, ShuttleBehaviour& var) { return int_to_type<ShuttleBehaviour> (o, var); }
623 std::istream& operator>>(std::istream& o, ShuttleUnits& var) { return int_to_type<ShuttleUnits> (o, var); }
624 std::istream& operator>>(std::istream& o, SmpteFormat& var) { return int_to_type<SmpteFormat> (o, var); }
625