JackServerAPI.cpp 6.44 KB
Newer Older
sletz's avatar
sletz committed
1
2
/*
Copyright (C) 2001-2003 Paul Davis
sletz's avatar
sletz committed
3
Copyright (C) 2004-2008 Grame
sletz's avatar
sletz committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

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.

*/
sletz's avatar
Cleanup    
sletz committed
20

21
#include "JackSystemDeps.h"
sletz's avatar
sletz committed
22
#include "JackGraphManager.h"
sletz's avatar
sletz committed
23
#include "JackInternalClient.h"
sletz's avatar
sletz committed
24
25
26
#include "JackServer.h"
#include "JackDebugClient.h"
#include "JackServerGlobals.h"
27
#include "JackTools.h"
28
#include "JackCompilerDeps.h"
sletz's avatar
sletz committed
29
#include "JackLockedEngine.h"
sletz's avatar
sletz committed
30
31
32
33
34
35

#ifdef __cplusplus
extern "C"
{
#endif

sletz's avatar
sletz committed
36
37
38
39
    jack_client_t * jack_client_new_aux (const char *client_name,
            jack_options_t options,
            jack_status_t *status);
    jack_client_t * jack_client_open_aux (const char *client_name,
40
            jack_options_t options,
41
            jack_status_t *status, va_list ap);
sletz's avatar
sletz committed
42
43
44
45
    EXPORT jack_client_t * jack_client_open (const char *client_name,
            jack_options_t options,
            jack_status_t *status, ...);
    EXPORT int jack_client_close (jack_client_t *client);
46
    EXPORT int jack_get_client_pid (const char *name);
sletz's avatar
sletz committed
47
48
49
50
51
52
53

#ifdef __cplusplus
}
#endif

using namespace Jack;

sletz's avatar
sletz committed
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
jack_client_t* jack_client_new_aux(const char* client_name, jack_options_t options, jack_status_t* status)
{
    jack_varargs_t va;		/* variable arguments */
    jack_status_t my_status;
    JackClient* client;

    if (client_name == NULL) {
        jack_error("jack_client_new called with a NULL client_name");
        return NULL;
    }

    jack_log("jack_client_new %s", client_name);
 
    if (status == NULL)			/* no status from caller? */
        status = &my_status;	/* use local status word */
    *status = (jack_status_t)0;

    /* validate parameters */
    if ((options & ~JackOpenOptions)) {
        int my_status1 = *status | (JackFailure | JackInvalidOption);
        *status = (jack_status_t)my_status1;
        return NULL;
    }

    /* parse variable arguments */
    jack_varargs_init(&va);
 
    if (!JackServerGlobals::Init()) { // jack server initialisation
        int my_status1 = (JackFailure | JackServerError);
        *status = (jack_status_t)my_status1;
        return NULL;
    }
 
    if (JACK_DEBUG) {
        client = new JackDebugClient(new JackInternalClient(JackServerGlobals::fInstance, GetSynchroTable())); // Debug mode
    } else {
        client = new JackInternalClient(JackServerGlobals::fInstance, GetSynchroTable());
    }

    int res = client->Open(va.server_name, client_name, options, status);
    if (res < 0) {
        delete client;
        JackServerGlobals::Destroy(); // jack server destruction
        int my_status1 = (JackFailure | JackServerError);
        *status = (jack_status_t)my_status1;
        return NULL;
    } else {
        return (jack_client_t*)client;
    }
}

jack_client_t* jack_client_open_aux(const char* client_name, jack_options_t options, jack_status_t* status, va_list ap)
sletz's avatar
sletz committed
106
107
108
{
    jack_varargs_t va;		/* variable arguments */
    jack_status_t my_status;
sletz's avatar
sletz committed
109
    JackClient* client;
110

111
    if (client_name == NULL) {
sletz's avatar
sletz committed
112
113
114
        jack_error("jack_client_open called with a NULL client_name");
        return NULL;
    }
115

116
117
    jack_log("jack_client_open %s", client_name);
 
sletz's avatar
sletz committed
118
119
120
121
122
123
124
125
126
127
128
129
    if (status == NULL)			/* no status from caller? */
        status = &my_status;	/* use local status word */
    *status = (jack_status_t)0;

    /* validate parameters */
    if ((options & ~JackOpenOptions)) {
        int my_status1 = *status | (JackFailure | JackInvalidOption);
        *status = (jack_status_t)my_status1;
        return NULL;
    }

    /* parse variable arguments */
sletz's avatar
sletz committed
130
    if (ap) {
131
        jack_varargs_parse(options, ap, &va);
sletz's avatar
sletz committed
132
    } else {
133
        jack_varargs_init(&va);
sletz's avatar
sletz committed
134
    }
135

136
137
138
139
    if (!JackServerGlobals::Init()) { // jack server initialisation
        int my_status1 = (JackFailure | JackServerError);
        *status = (jack_status_t)my_status1;
        return NULL;
sletz's avatar
sletz committed
140
    }
141
 
sletz's avatar
sletz committed
142
    if (JACK_DEBUG) {
143
        client = new JackDebugClient(new JackInternalClient(JackServerGlobals::fInstance, GetSynchroTable())); // Debug mode
sletz's avatar
sletz committed
144
    } else {
145
        client = new JackInternalClient(JackServerGlobals::fInstance, GetSynchroTable());
sletz's avatar
sletz committed
146
    }
sletz's avatar
sletz committed
147

148
    int res = client->Open(va.server_name, client_name, options, status);
sletz's avatar
sletz committed
149
150
    if (res < 0) {
        delete client;
151
        JackServerGlobals::Destroy(); // jack server destruction
sletz's avatar
sletz committed
152
        int my_status1 = (JackFailure | JackServerError);
153
        *status = (jack_status_t)my_status1;
sletz's avatar
sletz committed
154
155
156
157
158
159
        return NULL;
    } else {
        return (jack_client_t*)client;
    }
}

160
161
EXPORT jack_client_t* jack_client_open(const char* ext_client_name, jack_options_t options, jack_status_t* status, ...)
{
sletz's avatar
sletz committed
162
163
164
#ifdef __CLIENTDEBUG__
    JackGlobals::CheckContext("jack_client_open");
#endif
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
    try {
        assert(JackGlobals::fOpenMutex);
        JackGlobals::fOpenMutex->Lock();
        va_list ap;
        va_start(ap, status);
        jack_client_t* res = jack_client_open_aux(ext_client_name, options, status, ap);
        va_end(ap);
        JackGlobals::fOpenMutex->Unlock();
        return res;
    } catch(std::bad_alloc& e) {
        jack_error("Memory allocation error...");
        return NULL;
    } catch (...) {
        jack_error("Unknown error...");
        return NULL;
    }
181
182
}

sletz's avatar
sletz committed
183
184
EXPORT int jack_client_close(jack_client_t* ext_client)
{
sletz's avatar
sletz committed
185
186
187
#ifdef __CLIENTDEBUG__
    JackGlobals::CheckContext("jack_client_close");
#endif    
188
189
    assert(JackGlobals::fOpenMutex);
    JackGlobals::fOpenMutex->Lock();
190
    int res = -1;
sletz's avatar
sletz committed
191
    jack_log("jack_client_close");
sletz's avatar
sletz committed
192
193
194
    JackClient* client = (JackClient*)ext_client;
    if (client == NULL) {
        jack_error("jack_client_close called with a NULL client");
195
    } else {
196
        res = client->Close();
sletz's avatar
sletz committed
197
        delete client;
198
        JackServerGlobals::Destroy();	// jack server destruction
199
        jack_log("jack_client_close res = %d", res);
sletz's avatar
sletz committed
200
    }
201
    JackGlobals::fOpenMutex->Unlock();
202
    return res;
sletz's avatar
sletz committed
203
204
}

205
206
EXPORT int jack_get_client_pid(const char *name)
{
207
208
    return (JackServerGlobals::fInstance != NULL) 
        ? JackServerGlobals::fInstance->GetEngine()->GetClientPID(name)
209
210
211
        : 0;
}