Cleanup
[jack2.git] / common / JackLibClient.cpp
blobc853706b83defe188e1952957e66974f91263082
1 /*
2 Copyright (C) 2004-2006 Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 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 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "JackLibClient.h"
21 #include "JackTime.h"
22 #include "JackLibGlobals.h"
23 #include "JackGlobals.h"
24 #include "JackChannel.h"
26 namespace Jack
29 // Used for external C API (JackAPI.cpp)
30 JackGraphManager* GetGraphManager()
32 assert(JackLibGlobals::fGlobals->fGraphManager);
33 return JackLibGlobals::fGlobals->fGraphManager;
36 JackEngineControl* GetEngineControl()
38 assert(JackLibGlobals::fGlobals->fEngineControl);
39 return JackLibGlobals::fGlobals->fEngineControl;
42 JackSynchro** GetSynchroTable()
44 assert(JackLibGlobals::fGlobals);
45 return JackLibGlobals::fGlobals->fSynchroTable;
48 //-------------------
49 // Client management
50 //-------------------
52 JackLibClient::JackLibClient(JackSynchro** table): JackClient(table)
54 JackLog("JackLibClient::JackLibClient table = %x\n", table);
55 fChannel = JackGlobals::MakeClientChannel();
58 JackLibClient::~JackLibClient()
60 JackLog("JackLibClient::~JackLibClient\n");
61 delete fChannel;
64 int JackLibClient::Open(const char* name)
66 int shared_engine, shared_client, shared_graph, result;
67 JackLog("JackLibClient::Open %s\n", name);
69 // Open server/client channel
70 if (fChannel->Open(name, this) < 0) {
71 jack_error("Cannot connect to the server");
72 goto error;
75 // Start receiving notifications
76 if (fChannel->Start() < 0) {
77 jack_error("Cannot start channel");
78 goto error;
81 // Require new client
82 fChannel->ClientOpen(name, &shared_engine, &shared_client, &shared_graph, &result);
83 if (result < 0) {
84 jack_error("Cannot open %s client", name);
85 goto error;
88 try {
89 // Map shared memory segments
90 JackLibGlobals::fGlobals->fEngineControl = shared_engine;
91 JackLibGlobals::fGlobals->fGraphManager = shared_graph;
92 fClientControl = shared_client;
93 jack_verbose = GetEngineControl()->fVerbose;
94 } catch (int n) {
95 jack_error("Map shared memory segments exception %d", n);
96 goto error;
99 SetupDriverSync(false);
101 /* TODO : solve WIN32 thread Kill issue
102 #ifndef WIN32
103 // Connect shared synchro : the synchro must be usable in I/O mode when several clients live in the same process
104 if (!fSynchroTable[fClientControl->fRefNum]->Connect(name)) {
105 jack_error("Cannot ConnectSemaphore %s client", name);
106 goto error;
108 #endif
110 // Connect shared synchro : the synchro must be usable in I/O mode when several clients live in the same process
111 if (!fSynchroTable[fClientControl->fRefNum]->Connect(name)) {
112 jack_error("Cannot ConnectSemaphore %s client", name);
113 goto error;
116 JackLog("JackLibClient::Open name = %s refnum = %ld\n", name, fClientControl->fRefNum);
118 return 0;
120 error:
121 fChannel->Stop();
122 fChannel->Close();
123 return -1;
126 // Notifications received from the server
127 // TODO this should be done once for all clients in the process, when a shared notification channel
128 // will be shared by all clients...
129 int JackLibClient::ClientNotifyImp(int refnum, const char* name, int notify, int sync, int value)
131 int res = 0;
133 // Done all time
134 switch (notify) {
136 case JackNotifyChannelInterface::kAddClient:
137 JackLog("JackClient::AddClient name = %s, ref = %ld \n", name, refnum);
138 // the synchro must be usable in I/O mode when several clients live in the same process
139 res = fSynchroTable[refnum]->Connect(name) ? 0 : -1;
140 break;
142 case JackNotifyChannelInterface::kRemoveClient:
143 JackLog("JackClient::RemoveClient name = %s, ref = %ld \n", name, refnum);
144 if (strcmp(GetClientControl()->fName, name) != 0)
145 res = fSynchroTable[refnum]->Disconnect() ? 0 : -1;
146 break;
149 return res;
152 JackGraphManager* JackLibClient::GetGraphManager() const
154 assert(JackLibGlobals::fGlobals->fGraphManager);
155 return JackLibGlobals::fGlobals->fGraphManager;
158 JackEngineControl* JackLibClient::GetEngineControl() const
160 assert(JackLibGlobals::fGlobals->fEngineControl);
161 return JackLibGlobals::fGlobals->fEngineControl;
164 JackClientControl* JackLibClient::GetClientControl() const
166 return fClientControl;
169 } // end of namespace