Init engine fields, cleanup.
[jack2.git] / common / JackLibClient.cpp
blob14dbc733f40f9766385d9eaa817d9df003138a7b
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.
20 #include "JackLibClient.h"
21 #include "JackTime.h"
22 #include "JackLibGlobals.h"
23 #include "JackGlobals.h"
24 #include "JackPlatformPlug.h"
25 #include "JackTools.h"
27 namespace Jack
30 // Used for external C API (JackAPI.cpp)
31 JackGraphManager* GetGraphManager()
33 if (JackLibGlobals::fGlobals) {
34 return JackLibGlobals::fGlobals->fGraphManager;
35 } else {
36 return NULL;
40 JackEngineControl* GetEngineControl()
42 if (JackLibGlobals::fGlobals) {
43 return JackLibGlobals::fGlobals->fEngineControl;
44 } else {
45 return NULL;
49 JackSynchro* GetSynchroTable()
51 return (JackLibGlobals::fGlobals ? JackLibGlobals::fGlobals->fSynchroTable : 0);
54 //-------------------
55 // Client management
56 //-------------------
58 JackLibClient::JackLibClient(JackSynchro* table): JackClient(table)
60 jack_log("JackLibClient::JackLibClient table = %x", table);
61 fChannel = new JackClientChannel();
64 JackLibClient::~JackLibClient()
66 jack_log("JackLibClient::~JackLibClient");
67 delete fChannel;
70 int JackLibClient::Open(const char* server_name, const char* name, int uuid, jack_options_t options, jack_status_t* status)
72 int shared_engine, shared_client, shared_graph, result;
73 jack_log("JackLibClient::Open name = %s", name);
75 strncpy(fServerName, server_name, sizeof(fServerName));
77 // Open server/client channel
78 char name_res[JACK_CLIENT_NAME_SIZE + 1];
79 if (fChannel->Open(server_name, name, name_res, this, options, status) < 0) {
80 jack_error("Cannot connect to the server");
81 goto error;
84 // Start receiving notifications
85 if (fChannel->Start() < 0) {
86 jack_error("Cannot start channel");
87 goto error;
90 // Require new client
91 fChannel->ClientOpen(name_res, JackTools::GetPID(), uuid, &shared_engine, &shared_client, &shared_graph, &result);
92 if (result < 0) {
93 jack_error("Cannot open %s client", name_res);
94 goto error;
97 try {
98 // Map shared memory segments
99 JackLibGlobals::fGlobals->fEngineControl.SetShmIndex(shared_engine, fServerName);
100 JackLibGlobals::fGlobals->fGraphManager.SetShmIndex(shared_graph, fServerName);
101 fClientControl.SetShmIndex(shared_client, fServerName);
102 JackGlobals::fVerbose = GetEngineControl()->fVerbose;
103 } catch (int n) {
104 jack_error("Map shared memory segments exception %d", n);
105 goto error;
106 } catch (...) {
107 jack_error("Unknown error...");
108 goto error;
111 SetupDriverSync(false);
113 // Connect shared synchro : the synchro must be usable in I/O mode when several clients live in the same process
114 if (!fSynchroTable[GetClientControl()->fRefNum].Connect(name_res, fServerName)) {
115 jack_error("Cannot ConnectSemaphore %s client", name_res);
116 goto error;
119 JackGlobals::fClientTable[GetClientControl()->fRefNum] = this;
120 JackGlobals::fServerRunning = true;
121 SetClockSource(GetEngineControl()->fClockSource);
122 jack_log("JackLibClient::Open name = %s refnum = %ld", name_res, GetClientControl()->fRefNum);
123 return 0;
125 error:
126 fChannel->Stop();
127 fChannel->Close();
128 return -1;
131 // Notifications received from the server
132 // TODO this should be done once for all clients in the process, when a shared notification channel
133 // will be shared by all clients...
134 int JackLibClient::ClientNotifyImp(int refnum, const char* name, int notify, int sync, const char* message, int value1, int value2)
136 int res = 0;
138 // Done all time
139 switch (notify) {
141 case kAddClient:
142 jack_log("JackClient::AddClient name = %s, ref = %ld ", name, refnum);
143 // the synchro must be usable in I/O mode when several clients live in the same process
144 res = fSynchroTable[refnum].Connect(name, fServerName) ? 0 : -1;
145 break;
147 case kRemoveClient:
148 jack_log("JackClient::RemoveClient name = %s, ref = %ld ", name, refnum);
149 if (strcmp(GetClientControl()->fName, name) != 0)
150 res = fSynchroTable[refnum].Disconnect() ? 0 : -1;
151 break;
154 return res;
157 JackGraphManager* JackLibClient::GetGraphManager() const
159 assert(JackLibGlobals::fGlobals->fGraphManager);
160 return JackLibGlobals::fGlobals->fGraphManager;
163 JackEngineControl* JackLibClient::GetEngineControl() const
165 assert(JackLibGlobals::fGlobals->fEngineControl);
166 return JackLibGlobals::fGlobals->fEngineControl;
169 JackClientControl* JackLibClient::GetClientControl() const
171 return fClientControl;
174 } // end of namespace