weak/runtime jack linking: load libjack dynamically at runtime
[ardour.git] / libs / backends / jack / jack_connection.cc
index 00b6feb5e1dc6b22bc746eb7df40f128796543aa..201769070b20980af1be15da9cac6940c0d26139 100644 (file)
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
 */
+#include <iostream>
 
 #include <boost/scoped_ptr.hpp>
-#include <jack/session.h>
+
+#include <glibmm/timer.h>
 
 #include "pbd/epa.h"
 
@@ -35,6 +37,8 @@ using std::vector;
 using std::cerr;
 using std::endl;
 
+bool JackConnection::_in_control = false;
+
 static void jack_halted_callback (void* arg)
 {
        JackConnection* jc = static_cast<JackConnection*> (arg);
@@ -53,16 +57,9 @@ JackConnection::JackConnection (const std::string& arg1, const std::string& arg2
        , _client_name (arg1)
        , session_uuid (arg2)
 {
-}
-
-JackConnection::~JackConnection ()
-{
-       close ();
-}
+       /* See if the server is already up 
+        */
 
-bool
-JackConnection::server_running ()
-{
         EnvironmentalProtectionAgency* global_epa = EnvironmentalProtectionAgency::get_global_epa ();
         boost::scoped_ptr<EnvironmentalProtectionAgency> current_epa;
 
@@ -77,14 +74,19 @@ JackConnection::server_running ()
         }
 
        jack_status_t status;
-       jack_client_t* c = jack_client_open ("ardourprobe", JackNoStartServer, &status);
+       jack_client_t* c = jack_client_open1 ("ardourprobe", JackNoStartServer, &status);
 
        if (status == 0) {
                jack_client_close (c);
-               return true;
+               _in_control = false;
+       } else {
+               _in_control = true;
        }
+}
 
-       return false;
+JackConnection::~JackConnection ()
+{
+       close ();
 }
 
 int
@@ -112,7 +114,7 @@ JackConnection::open ()
        get_jack_server_dir_paths (dirs);
        set_path_env_for_jack_autostart (dirs);
 
-       if ((_jack = jack_client_open (_client_name.c_str(), JackSessionID, &status, session_uuid.c_str())) == 0) {
+       if ((_jack = jack_client_open2 (_client_name.c_str(), JackSessionID, &status, session_uuid.c_str())) == 0) {
                return -1;
        }
 
@@ -142,7 +144,12 @@ JackConnection::close ()
        if (_priv_jack) {       
                int ret = jack_client_close (_priv_jack);
                _jack = 0;
+
+               /* If we started JACK, it will be closing down */
+               Glib::usleep (500000);
+
                Disconnected (""); /* EMIT SIGNAL */
+
                return ret;
        }