add 'available' interface to the AudioBackendInfo
[ardour.git] / libs / backends / jack / jack_api.cc
1 /*
2     Copyright (C) 2013 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
20 #include "jack_connection.h"
21 #include "jack_audiobackend.h"
22
23 using namespace ARDOUR;
24
25 static boost::shared_ptr<JACKAudioBackend> backend;
26 static boost::shared_ptr<JackConnection> jack_connection;
27
28 static boost::shared_ptr<AudioBackend> backend_factory (AudioEngine& ae);
29 static int  instantiate (const std::string& arg1, const std::string& arg2);
30 static int  deinstantiate ();
31 static bool already_configured ();
32 static bool available ();
33
34 static ARDOUR::AudioBackendInfo _descriptor = {
35         "JACK",
36         instantiate,
37         deinstantiate,
38         backend_factory,
39         already_configured,
40         available
41 };
42
43 static boost::shared_ptr<AudioBackend>
44 backend_factory (AudioEngine& ae)
45 {
46         if (!jack_connection) {
47                 return boost::shared_ptr<AudioBackend>();
48         }
49
50         if (!backend) {
51                 backend.reset (new JACKAudioBackend (ae, _descriptor, jack_connection));
52         }
53
54         return backend;
55 }
56
57 static int
58 instantiate (const std::string& arg1, const std::string& arg2)
59 {
60         try {
61                 jack_connection.reset (new JackConnection (arg1, arg2));
62         } catch (...) {
63                 return -1;
64         }
65
66         return 0;
67 }
68
69 static int 
70 deinstantiate ()
71 {
72         backend.reset ();
73         jack_connection.reset ();
74
75         return 0;
76 }
77
78 static bool
79 already_configured ()
80 {
81         return !JackConnection::in_control ();
82 }
83
84 static bool
85 available ()
86 {
87         return have_libjack() ? false : true;
88 }
89
90 extern "C" ARDOURBACKEND_API ARDOUR::AudioBackendInfo* descriptor() { return &_descriptor; }
91