globally remove all trailing whitespace from ardour code base.
[ardour.git] / libs / backends / jack / 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 #include <iostream>
20
21 #include <boost/scoped_ptr.hpp>
22
23 #include <glibmm/timer.h>
24
25 #include "pbd/epa.h"
26
27 #include "jack_connection.h"
28 #include "jack_utils.h"
29
30 #define GET_PRIVATE_JACK_POINTER(j)  jack_client_t* _priv_jack = (jack_client_t*) (j); if (!_priv_jack) { return; }
31 #define GET_PRIVATE_JACK_POINTER_RET(j,r) jack_client_t* _priv_jack = (jack_client_t*) (j); if (!_priv_jack) { return r; }
32
33 using namespace ARDOUR;
34 using namespace PBD;
35 using std::string;
36 using std::vector;
37 using std::cerr;
38 using std::endl;
39
40 bool JackConnection::_in_control = false;
41
42 static void jack_halted_callback (void* arg)
43 {
44         JackConnection* jc = static_cast<JackConnection*> (arg);
45         jc->halted_callback ();
46 }
47
48 static void jack_halted_info_callback (jack_status_t code, const char* reason, void* arg)
49 {
50         JackConnection* jc = static_cast<JackConnection*> (arg);
51         jc->halted_info_callback (code, reason);
52 }
53
54
55 JackConnection::JackConnection (const std::string& arg1, const std::string& arg2)
56         : _jack (0)
57         , _client_name (arg1)
58         , session_uuid (arg2)
59         , _probed_buffer_size (0)
60         , _probed_sample_rate (0)
61 {
62         /* See if the server is already up
63          */
64
65         EnvironmentalProtectionAgency* global_epa = EnvironmentalProtectionAgency::get_global_epa ();
66         boost::scoped_ptr<EnvironmentalProtectionAgency> current_epa;
67
68         /* revert all environment settings back to whatever they were when
69          * ardour started, because ardour's startup script may have reset
70          * something in ways that interfere with finding/starting JACK.
71          */
72
73         if (global_epa) {
74                 current_epa.reset (new EnvironmentalProtectionAgency(true)); /* will restore settings when we leave scope */
75                 global_epa->restore ();
76         }
77
78         jack_status_t status;
79         jack_client_t* c = jack_client_open ("ardourprobe", JackNoStartServer, &status);
80
81         if (status == 0) {
82                 _probed_buffer_size = jack_get_buffer_size(c);
83                 _probed_sample_rate = jack_get_sample_rate(c);
84                 jack_client_close (c);
85                 _in_control = false;
86         } else {
87                 _in_control = true;
88         }
89 }
90
91 JackConnection::~JackConnection ()
92 {
93         close ();
94 }
95
96 int
97 JackConnection::open ()
98 {
99         EnvironmentalProtectionAgency* global_epa = EnvironmentalProtectionAgency::get_global_epa ();
100         boost::scoped_ptr<EnvironmentalProtectionAgency> current_epa;
101         jack_status_t status;
102
103         close ();
104
105         /* revert all environment settings back to whatever they were when ardour started
106          */
107
108         if (global_epa) {
109                 current_epa.reset (new EnvironmentalProtectionAgency(true)); /* will restore settings when we leave scope */
110                 global_epa->restore ();
111         }
112
113         /* ensure that PATH or equivalent includes likely locations of the JACK
114          * server, in case the user's default does not.
115          */
116
117         vector<string> dirs;
118         get_jack_server_dir_paths (dirs);
119         set_path_env_for_jack_autostart (dirs);
120
121         if ((_jack = jack_client_open (_client_name.c_str(), JackSessionID, &status, session_uuid.c_str())) == 0) {
122                 return -1;
123         }
124
125         if (status & JackNameNotUnique) {
126                 _client_name = jack_get_client_name (_jack);
127         }
128
129         /* attach halted handler */
130
131         if (jack_on_info_shutdown) {
132                 jack_on_info_shutdown (_jack, jack_halted_info_callback, this);
133         } else {
134                 jack_on_shutdown (_jack, jack_halted_callback, this);
135         }
136
137
138         Connected(); /* EMIT SIGNAL */
139
140         return 0;
141 }
142
143 int
144 JackConnection::close ()
145 {
146         GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
147
148         if (_priv_jack) {       
149                 int ret = jack_client_close (_priv_jack);
150                 _jack = 0;
151
152                 /* If we started JACK, it will be closing down */
153                 Glib::usleep (500000);
154
155                 Disconnected (""); /* EMIT SIGNAL */
156
157                 return ret;
158         }
159
160         return 0;
161 }
162
163 void
164 JackConnection::halted_callback ()
165 {
166         _jack = 0;
167         std::cerr << "JACK HALTED\n";
168         Disconnected ("");
169 }
170
171 void
172 JackConnection::halted_info_callback (jack_status_t /*status*/, const char* reason)
173 {
174         _jack = 0;
175         std::cerr << "JACK HALTED: " << reason << std::endl;
176         Disconnected (reason);
177 }
178
179