Compiles on Windows again.
[jack2.git] / common / JackServerAPI.cpp
blobe8e6c021b59b0c8b54e706ba37b603fa8405ca72
1 /*
2 Copyright (C) 2001-2003 Paul Davis
3 Copyright (C) 2004-2008 Grame
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "JackSystemDeps.h"
22 #include "JackGraphManager.h"
23 #include "JackInternalClient.h"
24 #include "JackServer.h"
25 #include "JackDebugClient.h"
26 #include "JackServerGlobals.h"
27 #include "JackTools.h"
28 #include "JackCompilerDeps.h"
29 #include "JackLockedEngine.h"
31 #ifdef __cplusplus
32 extern "C"
34 #endif
36 EXPORT jack_client_t * jack_client_open_aux (const char *client_name,
37 jack_options_t options,
38 jack_status_t *status, va_list ap);
39 EXPORT jack_client_t * jack_client_open (const char *client_name,
40 jack_options_t options,
41 jack_status_t *status, ...);
42 EXPORT int jack_client_close (jack_client_t *client);
43 EXPORT int jack_get_client_pid (const char *name);
45 #ifdef __cplusplus
47 #endif
49 using namespace Jack;
51 EXPORT jack_client_t* jack_client_open_aux(const char* client_name, jack_options_t options, jack_status_t* status, va_list ap)
53 jack_varargs_t va; /* variable arguments */
54 jack_status_t my_status;
55 JackClient* client;
57 if (client_name == NULL) {
58 jack_error("jack_client_open called with a NULL client_name");
59 return NULL;
62 jack_log("jack_client_open %s", client_name);
64 if (status == NULL) /* no status from caller? */
65 status = &my_status; /* use local status word */
66 *status = (jack_status_t)0;
68 /* validate parameters */
69 if ((options & ~JackOpenOptions)) {
70 int my_status1 = *status | (JackFailure | JackInvalidOption);
71 *status = (jack_status_t)my_status1;
72 return NULL;
75 /* parse variable arguments */
76 if (ap) {
77 jack_varargs_parse(options, ap, &va);
78 } else {
79 jack_varargs_init(&va);
82 if (!JackServerGlobals::Init()) { // jack server initialisation
83 int my_status1 = (JackFailure | JackServerError);
84 *status = (jack_status_t)my_status1;
85 return NULL;
88 if (JACK_DEBUG) {
89 client = new JackDebugClient(new JackInternalClient(JackServerGlobals::fInstance, GetSynchroTable())); // Debug mode
90 } else {
91 client = new JackInternalClient(JackServerGlobals::fInstance, GetSynchroTable());
94 int res = client->Open(va.server_name, client_name, options, status);
95 if (res < 0) {
96 delete client;
97 JackServerGlobals::Destroy(); // jack server destruction
98 int my_status1 = (JackFailure | JackServerError);
99 *status = (jack_status_t)my_status1;
100 return NULL;
101 } else {
102 return (jack_client_t*)client;
106 EXPORT jack_client_t* jack_client_open(const char* ext_client_name, jack_options_t options, jack_status_t* status, ...)
108 try {
109 assert(JackGlobals::fOpenMutex);
110 JackGlobals::fOpenMutex->Lock();
111 va_list ap;
112 va_start(ap, status);
113 jack_client_t* res = jack_client_open_aux(ext_client_name, options, status, ap);
114 va_end(ap);
115 JackGlobals::fOpenMutex->Unlock();
116 return res;
117 } catch(std::bad_alloc& e) {
118 jack_error("Memory allocation error...");
119 return NULL;
120 } catch (...) {
121 jack_error("Unknown error...");
122 return NULL;
126 EXPORT int jack_client_close(jack_client_t* ext_client)
128 assert(JackGlobals::fOpenMutex);
129 JackGlobals::fOpenMutex->Lock();
130 int res = -1;
131 jack_log("jack_client_close");
132 JackClient* client = (JackClient*)ext_client;
133 if (client == NULL) {
134 jack_error("jack_client_close called with a NULL client");
135 } else {
136 res = client->Close();
137 delete client;
138 JackServerGlobals::Destroy(); // jack server destruction
139 jack_log("jack_client_close res = %d", res);
141 JackGlobals::fOpenMutex->Unlock();
142 return res;
145 EXPORT int jack_get_client_pid(const char *name)
147 return (JackServerGlobals::fInstance != NULL)
148 ? JackServerGlobals::fInstance->GetEngine()->GetClientPID(name)
149 : 0;