rework check for old configuration files
[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
19 #ifdef WAF_BUILD
20 #include "libardour-config.h"
21 #endif
22
23 #ifdef interface
24 #undef interface
25 #endif
26
27 #include <cstdio> // Needed so that libraptor (included in lrdf) won't complain
28 #include <cstdlib>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <sys/time.h>
32 #ifndef PLATFORM_WINDOWS
33 #include <sys/resource.h>
34 #endif
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <errno.h>
38 #include <time.h>
39
40 #ifdef PLATFORM_WINDOWS
41 #include <windows.h> // for LARGE_INTEGER
42 #endif
43
44 #ifdef WINDOWS_VST_SUPPORT
45 #include <fst.h>
46 #endif
47
48 #ifdef LXVST_SUPPORT
49 #include "ardour/linux_vst_support.h"
50 #endif
51
52 #ifdef AUDIOUNIT_SUPPORT
53 #include "ardour/audio_unit.h"
54 #endif
55
56 #if defined(__SSE__) || defined(USE_XMMINTRIN)
57 #include <xmmintrin.h>
58 #endif
59
60 #ifdef check
61 #undef check /* stupid Apple and their un-namespaced, generic Carbon macros */
62 #endif 
63
64 #include <glibmm/fileutils.h>
65 #include <glibmm/miscutils.h>
66
67 #ifdef HAVE_LRDF
68 #include <lrdf.h>
69 #endif
70
71 #include "pbd/cpus.h"
72 #include "pbd/error.h"
73 #include "pbd/id.h"
74 #include "pbd/pbd.h"
75 #include "pbd/strsplit.h"
76 #include "pbd/fpu.h"
77 #include "pbd/file_utils.h"
78 #include "pbd/enumwriter.h"
79 #include "pbd/basename.h"
80
81 #include "midi++/port.h"
82 #include "midi++/mmc.h"
83
84 #include "ardour/analyser.h"
85 #include "ardour/audio_library.h"
86 #include "ardour/audio_backend.h"
87 #include "ardour/audioengine.h"
88 #include "ardour/audioplaylist.h"
89 #include "ardour/audioregion.h"
90 #include "ardour/buffer_manager.h"
91 #include "ardour/control_protocol_manager.h"
92 #include "ardour/directory_names.h"
93 #include "ardour/event_type_map.h"
94 #include "ardour/filesystem_paths.h"
95 #include "ardour/midi_region.h"
96 #include "ardour/midiport_manager.h"
97 #include "ardour/mix.h"
98 #include "ardour/operations.h"
99 #include "ardour/panner_manager.h"
100 #include "ardour/plugin_manager.h"
101 #include "ardour/process_thread.h"
102 #include "ardour/profile.h"
103 #include "ardour/rc_configuration.h"
104 #include "ardour/region.h"
105 #include "ardour/route_group.h"
106 #include "ardour/runtime_functions.h"
107 #include "ardour/session_event.h"
108 #include "ardour/source_factory.h"
109 #include "ardour/uri_map.h"
110
111 #include "audiographer/routines.h"
112
113 #if defined (__APPLE__)
114        #include <Carbon/Carbon.h> // For Gestalt
115 #endif
116
117 #include "i18n.h"
118
119 ARDOUR::RCConfiguration* ARDOUR::Config = 0;
120 ARDOUR::RuntimeProfile* ARDOUR::Profile = 0;
121 ARDOUR::AudioLibrary* ARDOUR::Library = 0;
122
123 using namespace ARDOUR;
124 using namespace std;
125 using namespace PBD;
126
127 bool libardour_initialized = false;
128
129 compute_peak_t          ARDOUR::compute_peak = 0;
130 find_peaks_t            ARDOUR::find_peaks = 0;
131 apply_gain_to_buffer_t  ARDOUR::apply_gain_to_buffer = 0;
132 mix_buffers_with_gain_t ARDOUR::mix_buffers_with_gain = 0;
133 mix_buffers_no_gain_t   ARDOUR::mix_buffers_no_gain = 0;
134
135 PBD::Signal1<void,std::string> ARDOUR::BootMessage;
136 PBD::Signal3<void,std::string,std::string,bool> ARDOUR::PluginScanMessage;
137 PBD::Signal1<void,int> ARDOUR::PluginScanTimeout;
138 PBD::Signal0<void> ARDOUR::GUIIdle;
139 PBD::Signal3<bool,std::string,std::string,int> ARDOUR::CopyConfigurationFiles;
140
141 static bool have_old_configuration_files = false;
142
143 namespace ARDOUR {
144 extern void setup_enum_writer ();
145 }
146
147 /* this is useful for quite a few things that want to check
148    if any bounds-related property has changed
149 */
150 PBD::PropertyChange ARDOUR::bounds_change;
151
152 void
153 setup_hardware_optimization (bool try_optimization)
154 {
155         bool generic_mix_functions = true;
156
157         if (try_optimization) {
158
159                 FPU fpu;
160
161 #if defined (ARCH_X86) && defined (BUILD_SSE_OPTIMIZATIONS)
162
163                 if (fpu.has_sse()) {
164
165                         info << "Using SSE optimized routines" << endmsg;
166
167                         // SSE SET
168                         compute_peak          = x86_sse_compute_peak;
169                         find_peaks            = x86_sse_find_peaks;
170                         apply_gain_to_buffer  = x86_sse_apply_gain_to_buffer;
171                         mix_buffers_with_gain = x86_sse_mix_buffers_with_gain;
172                         mix_buffers_no_gain   = x86_sse_mix_buffers_no_gain;
173
174                         generic_mix_functions = false;
175
176                 }
177
178 #elif defined (__APPLE__) && defined (BUILD_VECLIB_OPTIMIZATIONS)
179                 SInt32 sysVersion = 0;
180
181                 if (noErr != Gestalt(gestaltSystemVersion, &sysVersion))
182                         sysVersion = 0;
183
184                 if (sysVersion >= 0x00001040) { // Tiger at least
185                         compute_peak           = veclib_compute_peak;
186                         find_peaks             = veclib_find_peaks;
187                         apply_gain_to_buffer   = veclib_apply_gain_to_buffer;
188                         mix_buffers_with_gain  = veclib_mix_buffers_with_gain;
189                         mix_buffers_no_gain    = veclib_mix_buffers_no_gain;
190
191                         generic_mix_functions = false;
192
193                         info << "Apple VecLib H/W specific optimizations in use" << endmsg;
194                 }
195 #endif
196
197                 /* consider FPU denormal handling to be "h/w optimization" */
198
199                 setup_fpu ();
200         }
201
202         if (generic_mix_functions) {
203
204                 compute_peak          = default_compute_peak;
205                 find_peaks            = default_find_peaks;
206                 apply_gain_to_buffer  = default_apply_gain_to_buffer;
207                 mix_buffers_with_gain = default_mix_buffers_with_gain;
208                 mix_buffers_no_gain   = default_mix_buffers_no_gain;
209
210                 info << "No H/W specific optimizations in use" << endmsg;
211         }
212
213         AudioGrapher::Routines::override_compute_peak (compute_peak);
214         AudioGrapher::Routines::override_apply_gain_to_buffer (apply_gain_to_buffer);
215 }
216
217 static void
218 lotsa_files_please ()
219 {
220 #ifndef PLATFORM_WINDOWS
221         struct rlimit rl;
222
223         if (getrlimit (RLIMIT_NOFILE, &rl) == 0) {
224
225 #ifdef __APPLE__
226                 /* See the COMPATIBILITY note on the Apple setrlimit() man page */
227                 rl.rlim_cur = min ((rlim_t) OPEN_MAX, rl.rlim_max);
228 #else
229                 rl.rlim_cur = rl.rlim_max;
230 #endif
231
232                 if (setrlimit (RLIMIT_NOFILE, &rl) != 0) {
233                         if (rl.rlim_cur == RLIM_INFINITY) {
234                                 error << _("Could not set system open files limit to \"unlimited\"") << endmsg;
235                         } else {
236                                 error << string_compose (_("Could not set system open files limit to %1"), rl.rlim_cur) << endmsg;
237                         }
238                 } else {
239                         if (rl.rlim_cur != RLIM_INFINITY) {
240                                 info << string_compose (_("Your system is configured to limit %1 to only %2 open files"), PROGRAM_NAME, rl.rlim_cur) << endmsg;
241                         }
242                 }
243         } else {
244                 error << string_compose (_("Could not get system open files limit (%1)"), strerror (errno)) << endmsg;
245         }
246 #endif
247 }
248
249 static int
250 copy_configuration_files (string const & old_dir, string const & new_dir, int old_version)
251 {
252         string old_name;
253         string new_name;
254
255         /* ensure target directory exists */
256
257         if (g_mkdir_with_parents (new_dir.c_str(), 0755)) {
258                 return -1;
259         }
260         
261         if (old_version == 3) {
262         
263                 old_name = Glib::build_filename (old_dir, X_("recent"));
264                 new_name = Glib::build_filename (new_dir, X_("recent"));
265
266                 copy_file (old_name, new_name);
267
268                 old_name = Glib::build_filename (old_dir, X_("sfdb"));
269                 new_name = Glib::build_filename (new_dir, X_("sfdb"));
270
271                 copy_file (old_name, new_name);
272
273                 /* can only copy ardour.rc - UI config is not compatible */
274
275                 old_name = Glib::build_filename (old_dir, X_("ardour.rc"));
276                 new_name = Glib::build_filename (new_dir, X_("config"));
277
278                 copy_file (old_name, new_name);
279
280                 /* copy templates and route templates */
281
282                 old_name = Glib::build_filename (old_dir, X_("templates"));
283                 new_name = Glib::build_filename (new_dir, X_("templates"));
284
285                 copy_recurse (old_name, new_name);
286
287                 old_name = Glib::build_filename (old_dir, X_("route_templates"));
288                 new_name = Glib::build_filename (new_dir, X_("route_templates"));
289
290                 copy_recurse (old_name, new_name);
291
292                 /* presets */
293
294                 old_name = Glib::build_filename (old_dir, X_("presets"));
295                 new_name = Glib::build_filename (new_dir, X_("presets"));
296                 
297                 copy_recurse (old_name, new_name);
298
299                 /* presets */
300
301                 old_name = Glib::build_filename (old_dir, X_("plugin_statuses"));
302                 new_name = Glib::build_filename (new_dir, X_("plugin_statuses"));
303
304                 copy_file (old_name, new_name);
305                 
306                 /* export formats */
307
308                 old_name = Glib::build_filename (old_dir, export_formats_dir_name);
309                 new_name = Glib::build_filename (new_dir, export_formats_dir_name);
310                 
311                 vector<string> export_formats;
312                 g_mkdir_with_parents (Glib::build_filename (new_dir, export_formats_dir_name).c_str(), 0755);
313                 find_files_matching_pattern (export_formats, old_name, X_("*.format"));
314                 for (vector<string>::iterator i = export_formats.begin(); i != export_formats.end(); ++i) {
315                         std::string from = *i;
316                         std::string to = Glib::build_filename (new_name, Glib::path_get_basename (*i));
317                         copy_file (from, to);
318                 }
319         }
320
321         return 0;
322 }
323
324 void
325 ARDOUR::check_for_old_configuration_files ()
326 {
327         int current_version = atoi (X_(PROGRAM_VERSION));
328         
329         if (current_version <= 1) {
330                 return;
331         }
332
333         int old_version = current_version - 1;
334
335         string old_config_dir = user_config_directory (old_version);
336         /* pass in the current version explicitly to avoid creation */
337         string current_config_dir = user_config_directory (current_version);
338
339         if (!Glib::file_test (current_config_dir, Glib::FILE_TEST_IS_DIR)) {
340                 if (Glib::file_test (old_config_dir, Glib::FILE_TEST_IS_DIR)) {
341                         have_old_configuration_files = true;
342                 }
343         }
344 }
345
346 int
347 ARDOUR::handle_old_configuration_files (boost::function<bool (std::string const&, std::string const&, int)> ui_handler)
348 {
349         if (have_old_configuration_files) {
350                 int current_version = atoi (X_(PROGRAM_VERSION));
351                 assert (current_version > 1); // established in check_for_old_configuration_files ()
352                 int old_version = current_version - 1;
353                 string old_config_dir = user_config_directory (old_version);
354                 string current_config_dir = user_config_directory (current_version);
355
356                 if (ui_handler (old_config_dir, current_config_dir, old_version)) {
357                         copy_configuration_files (old_config_dir, current_config_dir, old_version);
358                         return 1;
359                 }
360         }
361         return 0;
362 }
363
364 bool
365 ARDOUR::init (bool use_windows_vst, bool try_optimization, const char* localedir)
366 {
367         if (libardour_initialized) {
368                 return true;
369         }
370
371         if (!PBD::init()) return false;
372
373 #ifdef ENABLE_NLS
374         (void) bindtextdomain(PACKAGE, localedir);
375         (void) bind_textdomain_codeset (PACKAGE, "UTF-8");
376 #endif
377
378         SessionEvent::init_event_pool ();
379
380         Operations::make_operations_quarks ();
381         SessionObject::make_property_quarks ();
382         Region::make_property_quarks ();
383         MidiRegion::make_property_quarks ();
384         AudioRegion::make_property_quarks ();
385         RouteGroup::make_property_quarks ();
386         Playlist::make_property_quarks ();
387         AudioPlaylist::make_property_quarks ();
388
389         /* this is a useful ready to use PropertyChange that many
390            things need to check. This avoids having to compose
391            it every time we want to check for any of the relevant
392            property changes.
393         */
394
395         bounds_change.add (ARDOUR::Properties::start);
396         bounds_change.add (ARDOUR::Properties::position);
397         bounds_change.add (ARDOUR::Properties::length);
398
399         /* provide a state version for the few cases that need it and are not
400            driven by reading state from disk (e.g. undo/redo)
401         */
402
403         Stateful::current_state_version = CURRENT_SESSION_FILE_VERSION;
404
405         ARDOUR::setup_enum_writer ();
406
407         // allow ardour the absolute maximum number of open files
408         lotsa_files_please ();
409
410 #ifdef HAVE_LRDF
411         lrdf_init();
412 #endif
413         Library = new AudioLibrary;
414
415         BootMessage (_("Loading configuration"));
416
417         Config = new RCConfiguration;
418
419         if (Config->load_state ()) {
420                 return false;
421         }
422
423         Config->set_use_windows_vst (use_windows_vst);
424 #ifdef LXVST_SUPPORT
425         Config->set_use_lxvst(true);
426 #endif
427
428         Profile = new RuntimeProfile;
429
430
431 #ifdef WINDOWS_VST_SUPPORT
432         if (Config->get_use_windows_vst() && fst_init (0)) {
433                 return false;
434         }
435 #endif
436
437 #ifdef LXVST_SUPPORT
438         if (Config->get_use_lxvst() && vstfx_init (0)) {
439                 return false;
440         }
441 #endif
442
443 #ifdef AUDIOUNIT_SUPPORT
444         AUPluginInfo::load_cached_info ();
445 #endif
446
447         setup_hardware_optimization (try_optimization);
448
449         SourceFactory::init ();
450         Analyser::init ();
451
452         /* singletons - first object is "it" */
453         (void) PluginManager::instance();
454 #ifdef LV2_SUPPORT
455         (void) URIMap::instance();
456 #endif
457         (void) EventTypeMap::instance();
458
459         ProcessThread::init ();
460         /* the + 4 is a bit of a handwave. i don't actually know
461            how many more per-thread buffer sets we need above
462            the h/w concurrency, but its definitely > 1 more.
463         */
464         BufferManager::init (hardware_concurrency() + 4); 
465
466         PannerManager::instance().discover_panners();
467
468         ARDOUR::AudioEngine::create ();
469
470         libardour_initialized = true;
471
472         return true;
473 }
474
475 void
476 ARDOUR::init_post_engine ()
477 {
478         ControlProtocolManager::instance().discover_control_protocols ();
479
480         XMLNode* node;
481         if ((node = Config->control_protocol_state()) != 0) {
482                 ControlProtocolManager::instance().set_state (*node, Stateful::loading_state_version);
483         }
484
485         /* find plugins */
486
487         ARDOUR::PluginManager::instance().refresh (!Config->get_discover_vst_on_start());
488 }
489
490 void
491 ARDOUR::cleanup () 
492 {
493         if (!libardour_initialized) {
494                 return;
495         }
496
497         ARDOUR::AudioEngine::destroy ();
498
499         delete Library;
500 #ifdef HAVE_LRDF
501         lrdf_cleanup ();
502 #endif
503         delete &ControlProtocolManager::instance();
504 #ifdef WINDOWS_VST_SUPPORT
505         fst_exit ();
506 #endif
507
508 #ifdef LXVST_SUPPORT
509         vstfx_exit();
510 #endif
511         delete &PluginManager::instance();
512         delete Config;
513         PBD::cleanup ();
514
515         return;
516 }
517
518 void
519 ARDOUR::find_bindings_files (map<string,string>& files)
520 {
521         vector<std::string> found;
522         Searchpath spath = ardour_config_search_path();
523
524         if (getenv ("ARDOUR_SAE")) {
525                 find_files_matching_pattern (found, spath, "*SAE-*.bindings");
526         } else {
527                 find_files_matching_pattern (found, spath, "*.bindings");
528         }
529
530         if (found.empty()) {
531                 return;
532         }
533
534         for (vector<std::string>::iterator x = found.begin(); x != found.end(); ++x) {
535                 std::string path(*x);
536                 pair<string,string> namepath;
537                 namepath.second = path;
538                 namepath.first = PBD::basename_nosuffix (path);
539                 files.insert (namepath);
540         }
541 }
542
543 bool
544 ARDOUR::no_auto_connect()
545 {
546         return getenv ("ARDOUR_NO_AUTOCONNECT") != 0;
547 }
548
549 void
550 ARDOUR::setup_fpu ()
551 {
552
553         if (getenv ("ARDOUR_RUNNING_UNDER_VALGRIND")) {
554                 // valgrind doesn't understand this assembler stuff
555                 // September 10th, 2007
556                 return;
557         }
558
559 #if defined(ARCH_X86) && defined(USE_XMMINTRIN)
560
561         int MXCSR;
562         FPU fpu;
563
564         /* XXX use real code to determine if the processor supports
565            DenormalsAreZero and FlushToZero
566         */
567
568         if (!fpu.has_flush_to_zero() && !fpu.has_denormals_are_zero()) {
569                 return;
570         }
571
572         MXCSR  = _mm_getcsr();
573
574 #ifdef DEBUG_DENORMAL_EXCEPTION
575         /* This will raise a FP exception if a denormal is detected */
576         MXCSR &= ~_MM_MASK_DENORM;
577 #endif  
578
579         switch (Config->get_denormal_model()) {
580         case DenormalNone:
581                 MXCSR &= ~(_MM_FLUSH_ZERO_ON | 0x40);
582                 break;
583
584         case DenormalFTZ:
585                 if (fpu.has_flush_to_zero()) {
586                         MXCSR |= _MM_FLUSH_ZERO_ON;
587                 }
588                 break;
589
590         case DenormalDAZ:
591                 MXCSR &= ~_MM_FLUSH_ZERO_ON;
592                 if (fpu.has_denormals_are_zero()) {
593                         MXCSR |= 0x40;
594                 }
595                 break;
596
597         case DenormalFTZDAZ:
598                 if (fpu.has_flush_to_zero()) {
599                         if (fpu.has_denormals_are_zero()) {
600                                 MXCSR |= _MM_FLUSH_ZERO_ON | 0x40;
601                         } else {
602                                 MXCSR |= _MM_FLUSH_ZERO_ON;
603                         }
604                 }
605                 break;
606         }
607
608         _mm_setcsr (MXCSR);
609
610 #endif
611 }
612
613 /* this can be changed to modify the translation behaviour for
614    cases where the user has never expressed a preference.
615 */
616 static const bool translate_by_default = true;
617
618 string
619 ARDOUR::translation_enable_path ()
620 {
621         return Glib::build_filename (user_config_directory(), ".translate");
622 }
623
624 bool
625 ARDOUR::translations_are_enabled ()
626 {
627         int fd = ::open (ARDOUR::translation_enable_path().c_str(), O_RDONLY);
628
629         if (fd < 0) {
630                 return translate_by_default;
631         }
632
633         char c;
634         bool ret = false;
635
636         if (::read (fd, &c, 1) == 1 && c == '1') {
637                 ret = true;
638         }
639
640         ::close (fd);
641
642         return ret;
643 }
644
645 bool
646 ARDOUR::set_translations_enabled (bool yn)
647 {
648         string i18n_enabler = ARDOUR::translation_enable_path();
649         int fd = ::open (i18n_enabler.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0644);
650
651         if (fd < 0) {
652                 return false;
653         }
654         
655         char c;
656         
657         if (yn) {
658                 c = '1';
659         } else {
660                 c = '0';
661         }
662         
663         (void) ::write (fd, &c, 1);
664         (void) ::close (fd);
665
666         return true;
667 }
668
669
670 vector<SyncSource>
671 ARDOUR::get_available_sync_options ()
672 {
673         vector<SyncSource> ret;
674
675         boost::shared_ptr<AudioBackend> backend = AudioEngine::instance()->current_backend();
676         if (backend && backend->name() == "JACK") {
677                 ret.push_back (Engine);
678         }
679
680         ret.push_back (MTC);
681         ret.push_back (MIDIClock);
682         ret.push_back (LTC);
683
684         return ret;
685 }
686
687 /** Return a monotonic value for the number of microseconds that have elapsed
688  * since an arbitrary zero origin.
689  */
690
691 #ifdef __MACH__
692 /* Thanks Apple for not implementing this basic SUSv2, POSIX.1-2001 function
693  */
694 #include <mach/mach_time.h>
695 #define CLOCK_REALTIME 0
696 #define CLOCK_MONOTONIC 0
697 int 
698 clock_gettime (int /*clk_id*/, struct timespec *t)
699 {
700         static bool initialized = false;
701         static mach_timebase_info_data_t timebase;
702         if (!initialized) {
703                 mach_timebase_info(&timebase);
704                 initialized = true;
705         }
706         uint64_t time;
707         time = mach_absolute_time();
708         double nseconds = ((double)time * (double)timebase.numer)/((double)timebase.denom);
709         double seconds = ((double)time * (double)timebase.numer)/((double)timebase.denom * 1e9);
710         t->tv_sec = seconds;
711         t->tv_nsec = nseconds;
712         return 0;
713 }
714 #endif
715  
716 microseconds_t
717 ARDOUR::get_microseconds ()
718 {
719 #ifdef PLATFORM_WINDOWS
720         microseconds_t ret = 0;
721         LARGE_INTEGER freq, time;
722
723         if (QueryPerformanceFrequency(&freq))
724                 if (QueryPerformanceCounter(&time))
725                         ret = (microseconds_t)((time.QuadPart * 1000000) / freq.QuadPart);
726
727         return ret;
728 #else
729         struct timespec ts;
730         if (clock_gettime (CLOCK_MONOTONIC, &ts) != 0) {
731                 /* EEEK! */
732                 return 0;
733         }
734         return (microseconds_t) ts.tv_sec * 1000000 + (ts.tv_nsec/1000);
735 #endif
736 }
737
738 /** Return the number of bits per sample for a given sample format.
739  *
740  * This is closely related to sndfile_data_width() but does NOT
741  * return a "magic" value to differentiate between 32 bit integer
742  * and 32 bit floating point values.
743  */
744
745 int
746 ARDOUR::format_data_width (ARDOUR::SampleFormat format)
747 {
748
749
750
751         switch (format) {
752         case ARDOUR::FormatInt16:
753                 return 16;
754         case ARDOUR::FormatInt24:
755                 return 24;
756         default:
757                 return 32;
758         }
759 }