JackLibClient.cpp 4.93 KB
Newer Older
sletz's avatar
sletz committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Copyright (C) 2004-2006 Grame  

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.

*/

#include "JackLibClient.h"
#include "JackTime.h"
#include "JackLibGlobals.h"
#include "JackGlobals.h"
#include "JackChannel.h"

namespace Jack
{

// Used for external C API (JackAPI.cpp)
JackGraphManager* GetGraphManager()
{
    assert(JackLibGlobals::fGlobals->fGraphManager);
    return JackLibGlobals::fGlobals->fGraphManager;
}

JackEngineControl* GetEngineControl()
{
    assert(JackLibGlobals::fGlobals->fEngineControl);
    return JackLibGlobals::fGlobals->fEngineControl;
}

JackSynchro** GetSynchroTable()
{
    assert(JackLibGlobals::fGlobals);
    return JackLibGlobals::fGlobals->fSynchroTable;
}

//-------------------
// Client management
//-------------------

JackLibClient::JackLibClient(JackSynchro** table): JackClient(table)
{
    JackLog("JackLibClient::JackLibClient table = %x\n", table);
    fChannel = JackGlobals::MakeClientChannel();
}

JackLibClient::~JackLibClient()
{
    JackLog("JackLibClient::~JackLibClient\n");
    delete fChannel;
}

64
int JackLibClient::Open(const char* name, jack_options_t options, jack_status_t* status)
sletz's avatar
sletz committed
65
{
sletz's avatar
sletz committed
66
    int shared_engine, shared_client, shared_graph, result;
sletz's avatar
sletz committed
67
    JackLog("JackLibClient::Open %s\n", name);
68
	
sletz's avatar
sletz committed
69
    // Open server/client channel
70
71
	char name_res[JACK_CLIENT_NAME_SIZE]; 
    if (fChannel->Open(name, name_res, this, options, status) < 0) {
sletz's avatar
sletz committed
72
73
74
75
76
77
78
79
80
81
82
        jack_error("Cannot connect to the server");
        goto error;
    }

    // Start receiving notifications
    if (fChannel->Start() < 0) {
        jack_error("Cannot start channel");
        goto error;
    }

    // Require new client
83
    fChannel->ClientOpen(name_res, &shared_engine, &shared_client, &shared_graph, &result);
sletz's avatar
sletz committed
84
    if (result < 0) {
85
        jack_error("Cannot open %s client", name_res);
sletz's avatar
sletz committed
86
87
88
89
90
91
        goto error;
    }

    try {
        // Map shared memory segments
        JackLibGlobals::fGlobals->fEngineControl = shared_engine;
sletz's avatar
sletz committed
92
        JackLibGlobals::fGlobals->fGraphManager = shared_graph;
sletz's avatar
sletz committed
93
        fClientControl = shared_client;
sletz's avatar
sletz committed
94
        jack_verbose = GetEngineControl()->fVerbose;
sletz's avatar
sletz committed
95
96
97
98
99
100
101
    } catch (int n) {
        jack_error("Map shared memory segments exception %d", n);
        goto error;
    }

    SetupDriverSync(false);

102
/* TODO : solve WIN32 thread Kill issue
103
104
#ifndef WIN32
    // Connect shared synchro : the synchro must be usable in I/O mode when several clients live in the same process    
105
	if (!fSynchroTable[fClientControl->fRefNum]->Connect(name)) {
sletz's avatar
sletz committed
106
107
        jack_error("Cannot ConnectSemaphore %s client", name);
        goto error;
108
109
    }
#endif
110
111
112
113
114
115
*/
	// Connect shared synchro : the synchro must be usable in I/O mode when several clients live in the same process    
	if (!fSynchroTable[fClientControl->fRefNum]->Connect(name)) {
        jack_error("Cannot ConnectSemaphore %s client", name);
        goto error;
    }
sletz's avatar
sletz committed
116

117
    JackLog("JackLibClient::Open name = %s refnum = %ld\n", name, fClientControl->fRefNum);
118
	
sletz's avatar
sletz committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
    return 0;

error:
    fChannel->Stop();
    fChannel->Close();
    return -1;
}

// Notifications received from the server
// TODO this should be done once for all clients in the process, when a shared notification channel
// will be shared by all clients...
int JackLibClient::ClientNotifyImp(int refnum, const char* name, int notify, int sync, int value)
{
    int res = 0;

    // Done all time
    switch (notify) {

137
        case kAddClient:
sletz's avatar
sletz committed
138
139
140
141
142
            JackLog("JackClient::AddClient name = %s, ref = %ld \n", name, refnum);
            // the synchro must be usable in I/O mode when several clients live in the same process
            res = fSynchroTable[refnum]->Connect(name) ? 0 : -1;
            break;

143
        case kRemoveClient:
sletz's avatar
sletz committed
144
145
146
147
            JackLog("JackClient::RemoveClient name = %s, ref = %ld \n", name, refnum);
            if (strcmp(GetClientControl()->fName, name) != 0)
                res = fSynchroTable[refnum]->Disconnect() ? 0 : -1;
            break;
148
	}
sletz's avatar
sletz committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173

    return res;
}

JackGraphManager* JackLibClient::GetGraphManager() const
{
    assert(JackLibGlobals::fGlobals->fGraphManager);
    return JackLibGlobals::fGlobals->fGraphManager;
}

JackEngineControl* JackLibClient::GetEngineControl() const
{
    assert(JackLibGlobals::fGlobals->fEngineControl);
    return JackLibGlobals::fGlobals->fEngineControl;
}

JackClientControl* JackLibClient::GetClientControl() const
{
    return fClientControl;
}

} // end of namespace