Move MIDI common files in server library on OSX.
[jack2.git] / windows / JackWinNamedPipeClientChannel.cpp
blobf29f7ff57274133e09f48384a2783cdb3a628a4e
1 /*
2 Copyright (C) 2004-2008 Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "JackWinNamedPipeClientChannel.h"
22 #include "JackRequest.h"
23 #include "JackClient.h"
24 #include "JackGlobals.h"
25 #include "JackError.h"
27 namespace Jack
30 JackWinNamedPipeClientChannel::JackWinNamedPipeClientChannel():fThread(this)
32 fClient = NULL;
35 JackWinNamedPipeClientChannel::~JackWinNamedPipeClientChannel()
38 int JackWinNamedPipeClientChannel::ServerCheck(const char* server_name)
40 jack_log("JackWinNamedPipeClientChannel::ServerCheck = %s", server_name);
42 // Connect to server
43 if (fRequestPipe.Connect(jack_server_dir, server_name, 0) < 0) {
44 jack_error("Cannot connect to server pipe");
45 return -1;
46 } else {
47 return 0;
51 int JackWinNamedPipeClientChannel::Open(const char* server_name, const char* name, int uuid, char* name_res, JackClient* obj, jack_options_t options, jack_status_t* status)
53 int result = 0;
54 jack_log("JackWinNamedPipeClientChannel::Open name = %s", name);
57 16/08/07: was called before doing "fRequestPipe.Connect" .... still necessary?
58 if (fNotificationListenPipe.Bind(jack_client_dir, name, 0) < 0) {
59 jack_error("Cannot bind pipe");
60 goto error;
64 if (fRequestPipe.Connect(jack_server_dir, server_name, 0) < 0) {
65 jack_error("Cannot connect to server pipe");
66 goto error;
69 // Check name in server
70 ClientCheck(name, uuid, name_res, JACK_PROTOCOL_VERSION, (int)options, (int*)status, &result);
71 if (result < 0) {
72 jack_error("Client name = %s conflits with another running client", name);
73 goto error;
76 if (fNotificationListenPipe.Bind(jack_client_dir, name_res, 0) < 0) {
77 jack_error("Cannot bind pipe");
78 goto error;
81 fClient = obj;
82 return 0;
84 error:
85 fRequestPipe.Close();
86 fNotificationListenPipe.Close();
87 return -1;
90 void JackWinNamedPipeClientChannel::Close()
92 fRequestPipe.Close();
93 fNotificationListenPipe.Close();
94 // Here the thread will correctly stop when the pipe are closed
95 fThread.Stop();
98 int JackWinNamedPipeClientChannel::Start()
100 jack_log("JackWinNamedPipeClientChannel::Start");
102 To be sure notification thread is started before ClientOpen is called.
104 if (fThread.StartSync() != 0) {
105 jack_error("Cannot start Jack client listener");
106 return -1;
107 } else {
108 return 0;
112 void JackWinNamedPipeClientChannel::Stop()
114 jack_log("JackWinNamedPipeClientChannel::Stop");
115 fThread.Kill(); // Unsafe on WIN32... TODO : solve WIN32 thread Kill issue
118 void JackWinNamedPipeClientChannel::ServerSyncCall(JackRequest* req, JackResult* res, int* result)
120 if (req->Write(&fRequestPipe) < 0) {
121 jack_error("Could not write request type = %ld", req->fType);
122 *result = -1;
123 return ;
126 if (res->Read(&fRequestPipe) < 0) {
127 jack_error("Could not read result type = %ld", req->fType);
128 *result = -1;
129 return ;
132 *result = res->fResult;
135 void JackWinNamedPipeClientChannel::ServerAsyncCall(JackRequest* req, JackResult* res, int* result)
137 if (req->Write(&fRequestPipe) < 0) {
138 jack_error("Could not write request type = %ld", req->fType);
139 *result = -1;
140 } else {
141 *result = 0;
145 void JackWinNamedPipeClientChannel::ClientCheck(const char* name, int uuid, char* name_res, int protocol, int options, int* status, int* result)
147 JackClientCheckRequest req(name, protocol, options, uuid);
148 JackClientCheckResult res;
149 ServerSyncCall(&req, &res, result);
150 *status = res.fStatus;
151 strcpy(name_res, res.fName);
154 void JackWinNamedPipeClientChannel::ClientOpen(const char* name, int pid, int uuid, int* shared_engine, int* shared_client, int* shared_graph, int* result)
156 JackClientOpenRequest req(name, pid, uuid);
157 JackClientOpenResult res;
158 ServerSyncCall(&req, &res, result);
159 *shared_engine = res.fSharedEngine;
160 *shared_client = res.fSharedClient;
161 *shared_graph = res.fSharedGraph;
164 void JackWinNamedPipeClientChannel::ClientClose(int refnum, int* result)
166 JackClientCloseRequest req(refnum);
167 JackResult res;
168 ServerSyncCall(&req, &res, result);
171 void JackWinNamedPipeClientChannel::ClientActivate(int refnum, int is_real_time, int* result)
173 JackActivateRequest req(refnum, is_real_time);
174 JackResult res;
175 ServerSyncCall(&req, &res, result);
178 void JackWinNamedPipeClientChannel::ClientDeactivate(int refnum, int* result)
180 JackDeactivateRequest req(refnum);
181 JackResult res;
182 ServerSyncCall(&req, &res, result);
185 void JackWinNamedPipeClientChannel::PortRegister(int refnum, const char* name, const char* type, unsigned int flags, unsigned int buffer_size, jack_port_id_t* port_index, int* result)
187 JackPortRegisterRequest req(refnum, name, type, flags, buffer_size);
188 JackPortRegisterResult res;
189 ServerSyncCall(&req, &res, result);
190 *port_index = res.fPortIndex;
193 void JackWinNamedPipeClientChannel::PortUnRegister(int refnum, jack_port_id_t port_index, int* result)
195 JackPortUnRegisterRequest req(refnum, port_index);
196 JackResult res;
197 ServerSyncCall(&req, &res, result);
200 void JackWinNamedPipeClientChannel::PortConnect(int refnum, const char* src, const char* dst, int* result)
202 JackPortConnectNameRequest req(refnum, src, dst);
203 JackResult res;
204 ServerSyncCall(&req, &res, result);
207 void JackWinNamedPipeClientChannel::PortDisconnect(int refnum, const char* src, const char* dst, int* result)
209 JackPortDisconnectNameRequest req(refnum, src, dst);
210 JackResult res;
211 ServerSyncCall(&req, &res, result);
214 void JackWinNamedPipeClientChannel::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst, int* result)
216 JackPortConnectRequest req(refnum, src, dst);
217 JackResult res;
218 ServerSyncCall(&req, &res, result);
221 void JackWinNamedPipeClientChannel::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst, int* result)
223 JackPortDisconnectRequest req(refnum, src, dst);
224 JackResult res;
225 ServerSyncCall(&req, &res, result);
228 void JackWinNamedPipeClientChannel::PortRename(int refnum, jack_port_id_t port, const char* name, int* result)
230 JackPortRenameRequest req(refnum, port, name);
231 JackResult res;
232 ServerSyncCall(&req, &res, result);
235 void JackWinNamedPipeClientChannel::SetBufferSize(jack_nframes_t buffer_size, int* result)
237 JackSetBufferSizeRequest req(buffer_size);
238 JackResult res;
239 ServerSyncCall(&req, &res, result);
242 void JackWinNamedPipeClientChannel::SetFreewheel(int onoff, int* result)
244 JackSetFreeWheelRequest req(onoff);
245 JackResult res;
246 ServerSyncCall(&req, &res, result);
249 void JackWinNamedPipeClientChannel::ComputeTotalLatencies(int* result)
251 JackComputeTotalLatenciesRequest req;
252 JackResult res;
253 ServerSyncCall(&req, &res, result);
256 void JackWinNamedPipeClientChannel::SessionNotify(int refnum, const char* target, jack_session_event_type_t type, const char* path, jack_session_command_t** result)
258 JackSessionNotifyRequest req(refnum, path, type, target);
259 JackSessionNotifyResult res;
260 int intresult;
261 ServerSyncCall(&req, &res, &intresult);
263 jack_session_command_t* session_command = (jack_session_command_t *)malloc(sizeof(jack_session_command_t) * (res.fCommandList.size() + 1));
264 int i = 0;
266 for (std::list<JackSessionCommand>::iterator ci=res.fCommandList.begin(); ci!=res.fCommandList.end(); ci++) {
267 session_command[i].uuid = strdup( ci->fUUID );
268 session_command[i].client_name = strdup( ci->fClientName );
269 session_command[i].command = strdup( ci->fCommand );
270 session_command[i].flags = ci->fFlags;
271 i += 1;
274 session_command[i].uuid = NULL;
275 session_command[i].client_name = NULL;
276 session_command[i].command = NULL;
277 session_command[i].flags = (jack_session_flags_t)0;
279 *result = session_command;
282 void JackWinNamedPipeClientChannel::SessionReply(int refnum, int* result)
284 JackSessionReplyRequest req(refnum);
285 JackResult res;
286 ServerSyncCall(&req, &res, result);
289 void JackWinNamedPipeClientChannel::GetUUIDForClientName(int refnum, const char* client_name, char* uuid_res, int* result)
291 JackGetUUIDRequest req(client_name);
292 JackUUIDResult res;
293 ServerSyncCall(&req, &res, result);
294 strncpy(uuid_res, res.fUUID, JACK_UUID_SIZE);
297 void JackWinNamedPipeClientChannel::GetClientNameForUUID(int refnum, const char* uuid, char* name_res, int* result)
299 JackGetClientNameRequest req(uuid);
300 JackClientNameResult res;
301 ServerSyncCall(&req, &res, result);
302 strncpy(name_res, res.fName, JACK_CLIENT_NAME_SIZE);
305 void JackWinNamedPipeClientChannel::ClientHasSessionCallback(const char* client_name, int* result)
307 JackClientHasSessionCallbackRequest req(client_name);
308 JackResult res;
309 ServerSyncCall(&req, &res, result);
312 void JackWinNamedPipeClientChannel::ReserveClientName(int refnum, const char* client_name, const char* uuid, int* result)
314 JackReserveNameRequest req(refnum, client_name, uuid);
315 JackResult res;
316 ServerSyncCall(&req, &res, result);
319 void JackWinNamedPipeClientChannel::ReleaseTimebase(int refnum, int* result)
321 JackReleaseTimebaseRequest req(refnum);
322 JackResult res;
323 ServerSyncCall(&req, &res, result);
326 void JackWinNamedPipeClientChannel::SetTimebaseCallback(int refnum, int conditional, int* result)
328 JackSetTimebaseCallbackRequest req(refnum, conditional);
329 JackResult res;
330 ServerSyncCall(&req, &res, result);
333 void JackWinNamedPipeClientChannel::GetInternalClientName(int refnum, int int_ref, char* name_res, int* result)
335 JackGetInternalClientNameRequest req(refnum, int_ref);
336 JackGetInternalClientNameResult res;
337 ServerSyncCall(&req, &res, result);
338 strcpy(name_res, res.fName);
341 void JackWinNamedPipeClientChannel::InternalClientHandle(int refnum, const char* client_name, int* status, int* int_ref, int* result)
343 JackInternalClientHandleRequest req(refnum, client_name);
344 JackInternalClientHandleResult res;
345 ServerSyncCall(&req, &res, result);
346 *int_ref = res.fIntRefNum;
347 *status = res.fStatus;
350 void JackWinNamedPipeClientChannel::InternalClientLoad(int refnum, const char* client_name, const char* so_name, const char* objet_data, int options, int* status, int* int_ref, int uuid, int* result)
352 JackInternalClientLoadRequest req(refnum, client_name, so_name, objet_data, options, uuid);
353 JackInternalClientLoadResult res;
354 ServerSyncCall(&req, &res, result);
355 *int_ref = res.fIntRefNum;
356 *status = res.fStatus;
359 void JackWinNamedPipeClientChannel::InternalClientUnload(int refnum, int int_ref, int* status, int* result)
361 JackInternalClientUnloadRequest req(refnum, int_ref);
362 JackInternalClientUnloadResult res;
363 ServerSyncCall(&req, &res, result);
364 *status = res.fStatus;
367 bool JackWinNamedPipeClientChannel::Init()
369 jack_log("JackWinNamedPipeClientChannel::Init");
371 if (!fNotificationListenPipe.Accept()) {
372 jack_error("JackWinNamedPipeClientChannel: cannot establish notification pipe");
373 return false;
374 } else {
375 return true;
379 bool JackWinNamedPipeClientChannel::Execute()
381 JackClientNotification event;
382 JackResult res;
384 if (event.Read(&fNotificationListenPipe) < 0) {
385 jack_error("JackWinNamedPipeClientChannel read fail");
386 goto error;
389 res.fResult = fClient->ClientNotify(event.fRefNum, event.fName, event.fNotify, event.fSync, event.fMessage, event.fValue1, event.fValue2);
391 if (event.fSync) {
392 if (res.Write(&fNotificationListenPipe) < 0) {
393 jack_error("JackWinNamedPipeClientChannel write fail");
394 goto error;
397 return true;
399 error:
400 // Close the pipes, server wont be able to create them otherwise.
401 fNotificationListenPipe.Close();
402 fRequestPipe.Close();
403 fClient->ShutDown();
404 return false;
407 } // end of namespace