Large nasty commit in the form of a 5000 line patch chock-full of completely
[ardour.git] / libs / ardour / globals.cc
index 238055089e057742476244b7c2a2533a413bd559..f3e6e1b94797f1adea7c5d2c5a54d6aa4437aed3 100644 (file)
@@ -1,6 +1,5 @@
 /*
     Copyright (C) 2000 Paul Davis 
-    Copyright (C) 2005 Sampo Savolainen
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -33,6 +32,7 @@
 #include <lrdf.h>
 
 #include <pbd/error.h>
+#include <pbd/strsplit.h>
 
 #include <midi++/port.h>
 #include <midi++/port_request.h>
 #include <ardour/audio_library.h>
 #include <ardour/configuration.h>
 #include <ardour/plugin_manager.h>
-#include <ardour/source.h>
+#include <ardour/audiosource.h>
 #include <ardour/utils.h>
 #include <ardour/session.h>
+#include <ardour/control_protocol_manager.h>
+#include <ardour/audioengine.h>
+
+#ifdef HAVE_LIBLO
+#include <ardour/osc.h>
+#endif
 
 #include <ardour/mix.h>
 
 ARDOUR::Configuration* ARDOUR::Config = 0;
 ARDOUR::AudioLibrary* ARDOUR::Library = 0;
 
+#ifdef HAVE_LIBLO
+ARDOUR::OSC* ARDOUR::osc = 0;
+#endif
+
 using namespace ARDOUR;
 using namespace std;
+using namespace PBD;
 
 MIDI::Port *default_mmc_port = 0;
 MIDI::Port *default_mtc_port = 0;
@@ -71,9 +82,26 @@ Change ARDOUR::PositionChanged = ARDOUR::new_change ();
 Change ARDOUR::NameChanged = ARDOUR::new_change ();
 Change ARDOUR::BoundsChanged = Change (0); // see init(), below
 
+#ifdef HAVE_LIBLO
+static int
+setup_osc ()
+{
+       /* no real cost to creating this object, and it avoids
+          conditionals anywhere that uses it 
+       */
+       
+       osc = new OSC (Config->get_osc_port());
+       
+       if (Config->get_use_osc ()) {
+               return osc->start ();
+       } else {
+               return 0;
+       }
+}
+#endif
 
 static int 
-setup_midi ()
+setup_midi (AudioEngine& engine        )
 {
        std::map<string,Configuration::MidiPortDescriptor*>::iterator i;
        int nports;
@@ -83,6 +111,8 @@ setup_midi ()
                return 0;
        }
 
+       MIDI::Manager::instance()->set_api_data(engine.jack());
+
        for (i = Config->midi_ports.begin(); i != Config->midi_ports.end(); ++i) {
                Configuration::MidiPortDescriptor* port_descriptor;
 
@@ -162,7 +192,7 @@ setup_midi ()
 }
 
 int
-ARDOUR::init (AudioEngine& engine, bool use_vst, bool try_optimization, void (*sighandler)(int,siginfo_t*,void*))
+ARDOUR::init (AudioEngine& engine, bool use_vst, bool try_optimization)
 {
         bool generic_mix_functions = true;
 
@@ -176,12 +206,18 @@ ARDOUR::init (AudioEngine& engine, bool use_vst, bool try_optimization, void (*s
 
        Config->set_use_vst (use_vst);
 
-       if (setup_midi ()) {
+       if (setup_midi (engine)) {
                return -1;
        }
+    
+#ifdef HAVE_LIBLO
+       if (setup_osc ()) {
+               return -1;
+       }
+#endif
 
 #ifdef VST_SUPPORT
-       if (Config->get_use_vst() && fst_init (sighandler)) {
+       if (Config->get_use_vst() && fst_init ()) {
                return -1;
        }
 #endif
@@ -192,6 +228,7 @@ ARDOUR::init (AudioEngine& engine, bool use_vst, bool try_optimization, void (*s
        
                unsigned int use_sse = 0;
 
+#ifndef USE_X86_64_ASM
                asm volatile (
                                 "mov $1, %%eax\n"
                                 "pushl %%ebx\n"
@@ -202,7 +239,21 @@ ARDOUR::init (AudioEngine& engine, bool use_vst, bool try_optimization, void (*s
                             : "=m" (use_sse)
                             : 
                         : "%eax", "%ecx", "%edx", "memory");
+#else
+
+               asm volatile (
+                                "movq $1, %%rax\n"
+                                "pushq %%rbx\n"
+                                "cpuid\n"
+                                "popq %%rbx\n"
+                                "andq $33554432, %%rdx\n"
+                                "movq %%rdx, %0\n"
+                            : "=m" (use_sse)
+                            : 
+                        : "%rax", "%rcx", "%rdx", "memory");
 
+#endif /* USE_X86_64_ASM */
+               
                if (use_sse) {
                        cerr << "Enabling SSE optimized routines" << endl;
        
@@ -251,6 +302,15 @@ ARDOUR::init (AudioEngine& engine, bool use_vst, bool try_optimization, void (*s
        /* singleton - first object is "it" */
        new PluginManager (engine);
        
+       /* singleton - first object is "it" */
+       new ControlProtocolManager ();
+       ControlProtocolManager::instance().discover_control_protocols (Session::control_protocol_path());
+
+       XMLNode* node;
+       if ((node = Config->control_protocol_state()) != 0) {
+               ControlProtocolManager::instance().set_state (*node);
+       }
+       
        BoundsChanged = Change (StartChanged|PositionChanged|LengthChanged);
 
        return 0;
@@ -261,6 +321,7 @@ ARDOUR::cleanup ()
 {
        delete Library;
        lrdf_cleanup ();
+       delete &ControlProtocolManager::instance();
        return 0;
 }
 
@@ -276,44 +337,109 @@ ARDOUR::new_change ()
        Change c;
        static uint32_t change_bit = 1;
 
-       /* XXX catch out-of-range */
+       /* catch out-of-range */
+       if (!change_bit)
+       {
+               fatal << _("programming error: ")
+                       << "change_bit out of range in ARDOUR::new_change()"
+                       << endmsg;
+               /*NOTREACHED*/
+       }
 
        c = Change (change_bit);
-       change_bit <<= 1;
+       change_bit <<= 1;       // if it shifts too far, change_bit == 0
 
        return c;
 }
 
-static string
-find_file (string name, string dir)
+string
+ARDOUR::get_user_ardour_path ()
 {
        string path;
+       char* envvar;
+       
+       if ((envvar = getenv ("HOME")) == 0 || strlen (envvar) == 0) {
+               return "/";
+       }
+               
+       path = envvar;
+       path += "/.ardour2/";
 
-       /* stop A: ~/.ardour/... */
+       /* create it if necessary */
 
-       path = getenv ("HOME");
+       mkdir (path.c_str (), 0755);
 
-       if (path.length()) {
-               
-               path += "/.ardour/";
+       return path;
+}
 
-               /* try to ensure that the directory exists.
-                  failure doesn't mean much here.
-               */
+string
+ARDOUR::get_system_data_path ()
+{
+       string path;
 
-               mkdir (path.c_str(), 0755);
+       path += DATA_DIR;
+       path += "/ardour2/";
+       
+       return path;
+}
 
-               path += name;
+string
+ARDOUR::get_system_module_path ()
+{
+       string path;
 
-               if (access (path.c_str(), R_OK) == 0) {
-                       return path;
+       path += MODULE_DIR;
+       path += "/ardour2/";
+       
+       return path;
+}
+
+static string
+find_file (string name, string dir, string subdir = "")
+{
+       string path;
+       char* envvar = getenv("ARDOUR_PATH");
+
+       /* 1st attempt: any directory in ARDOUR_PATH */
+       
+       if (envvar != 0) {
+
+               vector<string> split_path;
+       
+               split (envvar, split_path, ':');
+               
+               for (vector<string>::iterator i = split_path.begin(); i != split_path.end(); ++i) {
+                       path = *i;
+                       path += "/" + name;
+                       if (access (path.c_str(), R_OK) == 0) {
+                               // cerr << "Using file " << path << " found in ARDOUR_PATH." << endl;
+                               return path;
+                       }
                }
        }
 
-       /* stop B: dir/... */
+       /* 2nd attempt: ~/.ardour/ */
 
+       path = get_user_ardour_path();
+               
+       if (subdir.length()) {
+               path += subdir + "/";
+       }
+               
+       path += name;
+       if (access (path.c_str(), R_OK) == 0) {
+               return path;
+       }
+
+       /* 3rd attempt: dir/... */
+       
        path = dir;
-       path += "/ardour/";
+       path += "/ardour2/";
+       
+       if (subdir.length()) {
+               path += subdir + "/";
+       }
+       
        path += name;
        
        if (access (path.c_str(), R_OK) == 0) {
@@ -327,6 +453,7 @@ string
 ARDOUR::find_config_file (string name)
 {
        char* envvar;
+
        if ((envvar = getenv("ARDOUR_CONFIG_PATH")) == 0) {
                envvar = CONFIG_DIR;
        }
@@ -335,14 +462,14 @@ ARDOUR::find_config_file (string name)
 }
 
 string
-ARDOUR::find_data_file (string name)
+ARDOUR::find_data_file (string name, string subdir)
 {
        char* envvar;
        if ((envvar = getenv("ARDOUR_DATA_PATH")) == 0) {
                envvar = DATA_DIR;
        }
 
-       return find_file (name, envvar);
+       return find_file (name, envvar, subdir);
 }
 
 ARDOUR::LocaleGuard::LocaleGuard (const char* str)
@@ -438,3 +565,18 @@ ARDOUR::coverage (jack_nframes_t sa, jack_nframes_t ea,
        return OverlapNone;
 }
 
+/* not sure where to put these */
+
+std::istream& operator>>(std::istream& o, HeaderFormat& hf) {
+       int val;
+       o >> val;
+       hf = (HeaderFormat) val;
+       return o;
+}
+
+std::istream& operator>>(std::istream& o, SampleFormat& sf) {
+       int val;
+       o >> val;
+       sf = (SampleFormat) val;
+       return o;
+}