more stuff compiles
[ardour.git] / libs / ardour / jack_connection.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 <boost/scoped_ptr.hpp>
21 #include <jack/session.h>
22
23 #include "pbd/epa.h"
24
25 #include "ardour/jack_connection.h"
26
27 #define GET_PRIVATE_JACK_POINTER(j)  jack_client_t* _priv_jack = (jack_client_t*) (j); if (!_priv_jack) { return; }
28 #define GET_PRIVATE_JACK_POINTER_RET(j,r) jack_client_t* _priv_jack = (jack_client_t*) (j); if (!_priv_jack) { return r; }
29
30 using namespace ARDOUR;
31 using namespace PBD;
32
33 static void jack_halted_callback (void* arg)
34 {
35         JackConnection* jc = static_cast<JackConnection*> (arg);
36         jc->halted_callback ();
37 }
38
39 static void jack_halted_info_callback (jack_status_t code, const char* reason, void* arg)
40 {
41         JackConnection* jc = static_cast<JackConnection*> (arg);
42         jc->halted_info_callback (code, reason);
43 }
44
45
46 JackConnection::JackConnection (const std::string& arg1, const std::string& arg2)
47         : _jack (0)
48         , _client_name (arg1)
49         , session_uuid (arg2)
50 {
51 }
52
53 JackConnection::~JackConnection ()
54 {
55         close ();
56 }
57
58 int
59 JackConnection::open ()
60 {
61         EnvironmentalProtectionAgency* global_epa = EnvironmentalProtectionAgency::get_global_epa ();
62         boost::scoped_ptr<EnvironmentalProtectionAgency> current_epa;
63         jack_status_t status;
64
65         close ();
66
67         /* revert all environment settings back to whatever they were when ardour started
68          */
69
70         if (global_epa) {
71                 current_epa.reset (new EnvironmentalProtectionAgency(true)); /* will restore settings when we leave scope */
72                 global_epa->restore ();
73         }
74
75         if ((_jack = jack_client_open (_client_name.c_str(), JackSessionID, &status, session_uuid.c_str())) == 0) {
76                 return -1;
77         }
78
79         if (status & JackNameNotUnique) {
80                 _client_name = jack_get_client_name (_jack);
81         }
82
83         /* attach halted handler */
84
85         if (jack_on_info_shutdown) {
86                 jack_on_info_shutdown (_jack, jack_halted_info_callback, this);
87         } else {
88                 jack_on_shutdown (_jack, jack_halted_callback, this);
89         }
90
91         return 0;
92 }
93
94 int
95 JackConnection::close ()
96 {
97         GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
98
99         if (_priv_jack) {
100                 return jack_client_close (_priv_jack);
101         }
102
103         return 0;
104 }
105
106 void
107 JackConnection::halted_callback ()
108 {
109         _jack = 0;
110         Disconnected ("");
111 }
112
113 void
114 JackConnection::halted_info_callback (jack_status_t /*status*/, const char* reason)
115 {
116         _jack = 0;
117         Disconnected (reason);
118 }
119
120