summaryrefslogtreecommitdiff
path: root/libs/ardour/jack_connection.cc
blob: 57950b0e17086004dfcdd8cd6453e3ab5ae4b918 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
    Copyright (C) 2013 Paul Davis

    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
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <boost/scoped_ptr.hpp>
#include <jack/session.h>

#include "pbd/epa.h"

#include "ardour/jack_connection.h"
#include "ardour/jack_utils.h"

#define GET_PRIVATE_JACK_POINTER(j)  jack_client_t* _priv_jack = (jack_client_t*) (j); if (!_priv_jack) { return; }
#define GET_PRIVATE_JACK_POINTER_RET(j,r) jack_client_t* _priv_jack = (jack_client_t*) (j); if (!_priv_jack) { return r; }

using namespace ARDOUR;
using namespace PBD;
using std::string;
using std::vector;

static void jack_halted_callback (void* arg)
{
	JackConnection* jc = static_cast<JackConnection*> (arg);
	jc->halted_callback ();
}

static void jack_halted_info_callback (jack_status_t code, const char* reason, void* arg)
{
	JackConnection* jc = static_cast<JackConnection*> (arg);
	jc->halted_info_callback (code, reason);
}


JackConnection::JackConnection (const std::string& arg1, const std::string& arg2)
	: _jack (0)
	, _client_name (arg1)
	, session_uuid (arg2)
{
}

JackConnection::~JackConnection ()
{
	close ();
}

bool
JackConnection::server_running ()
{
        EnvironmentalProtectionAgency* global_epa = EnvironmentalProtectionAgency::get_global_epa ();
        boost::scoped_ptr<EnvironmentalProtectionAgency> current_epa;

        /* revert all environment settings back to whatever they were when
	 * ardour started, because ardour's startup script may have reset
	 * something in ways that interfere with finding/starting JACK.
         */

        if (global_epa) {
                current_epa.reset (new EnvironmentalProtectionAgency(true)); /* will restore settings when we leave scope */
                global_epa->restore ();
        }

	jack_status_t status;
	jack_client_t* c = jack_client_open ("ardourprobe", JackNoStartServer, &status);

	if (status == 0) {
		jack_client_close (c);
		return true;
	}

	return false;
}

int
JackConnection::open ()
{
        EnvironmentalProtectionAgency* global_epa = EnvironmentalProtectionAgency::get_global_epa ();
        boost::scoped_ptr<EnvironmentalProtectionAgency> current_epa;
	jack_status_t status;

	close ();

        /* revert all environment settings back to whatever they were when ardour started
         */

        if (global_epa) {
                current_epa.reset (new EnvironmentalProtectionAgency(true)); /* will restore settings when we leave scope */
                global_epa->restore ();
        }

	/* ensure that PATH or equivalent includes likely locations of the JACK
	 * server, in case the user's default does not.
	 */

	vector<string> dirs;
	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) {
		return -1;
	}

	if (status & JackNameNotUnique) {
		_client_name = jack_get_client_name (_jack);
	}

	/* attach halted handler */

        if (jack_on_info_shutdown) {
                jack_on_info_shutdown (_jack, jack_halted_info_callback, this);
        } else {
                jack_on_shutdown (_jack, jack_halted_callback, this);
        }

	return 0;
}

int
JackConnection::close ()
{
	GET_PRIVATE_JACK_POINTER_RET (_jack, -1);

	if (_priv_jack) {
		return jack_client_close (_priv_jack);
	}

	return 0;
}

void
JackConnection::halted_callback ()
{
	_jack = 0;
	Disconnected ("");
}

void
JackConnection::halted_info_callback (jack_status_t /*status*/, const char* reason)
{
	_jack = 0;
	Disconnected (reason);
}