JackGraphManager.cpp 28 KB
Newer Older
sletz's avatar
sletz committed
1
2
/*
Copyright (C) 2001 Paul Davis
sletz's avatar
sletz committed
3
Copyright (C) 2004-2008 Grame
sletz's avatar
sletz committed
4
5

This program is free software; you can redistribute it and/or modify
sletz's avatar
sletz committed
6
7
8
9
10
11
12
13
14
15
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
sletz's avatar
sletz committed
16
along with this program; if not, write to the Free Software
sletz's avatar
sletz committed
17
18
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

sletz's avatar
sletz committed
19
20
21
22
*/

#include "JackGraphManager.h"
#include "JackConstants.h"
sletz's avatar
sletz committed
23
#include "JackError.h"
sletz's avatar
sletz committed
24
25
26
27
28
29
30
31
32
#include <assert.h>
#include <stdlib.h>
#include <algorithm>
#include <regex.h>

namespace Jack
{


33
static void AssertBufferSize(jack_nframes_t buffer_size)
sletz's avatar
sletz committed
34
{
35
    if (buffer_size > BUFFER_SIZE_MAX) {
sletz's avatar
sletz committed
36
        jack_log("JackGraphManager::AssertBufferSize frames = %ld", buffer_size);
37
        assert(buffer_size <= BUFFER_SIZE_MAX);
sletz's avatar
sletz committed
38
39
    }
}
sletz's avatar
sletz committed
40

sletz's avatar
sletz committed
41
42
43
44
45
46
47
void JackGraphManager::AssertPort(jack_port_id_t port_index)
{
    if (port_index >= fPortMax) {
        jack_log("JackGraphManager::AssertPort port_index = %ld", port_index);
        assert(port_index < fPortMax);
    }
}
sletz's avatar
sletz committed
48

sletz's avatar
sletz committed
49
50
51
52
53
54
JackGraphManager* JackGraphManager::Allocate(int port_max)
{
    // Using "Placement" new
    void* shared_ptr = JackShmMem::operator new(sizeof(JackGraphManager) + port_max * sizeof(JackPort));
    return new(shared_ptr) JackGraphManager(port_max);
}
sletz's avatar
sletz committed
55

sletz's avatar
sletz committed
56
57
58
59
60
61
void JackGraphManager::Destroy(JackGraphManager* manager)
{
    // "Placement" new was used
    manager->~JackGraphManager();
    JackShmMem::operator delete(manager);
}
sletz's avatar
sletz committed
62
63

JackGraphManager::JackGraphManager(int port_max)
sletz's avatar
sletz committed
64
65
{
    assert(port_max <= PORT_NUM_MAX);
sletz's avatar
sletz committed
66

sletz's avatar
sletz committed
67
68
69
    for (int i = 0; i < port_max; i++) {
        fPortArray[i].Release();
    }
sletz's avatar
sletz committed
70

sletz's avatar
sletz committed
71
72
    fPortMax = port_max;
}
sletz's avatar
sletz committed
73

sletz's avatar
sletz committed
74
75
76
77
78
79
JackPort* JackGraphManager::GetPort(jack_port_id_t port_index)
{
    AssertPort(port_index);
    return &fPortArray[port_index];
}

80
jack_default_audio_sample_t* JackGraphManager::GetBuffer(jack_port_id_t port_index)
sletz's avatar
sletz committed
81
82
83
84
85
{
    return fPortArray[port_index].GetBuffer();
}

// Server
86
87
88
89
90
91
void JackGraphManager::InitRefNum(int refnum)
{
    JackConnectionManager* manager = WriteNextStateStart();
    manager->InitRefNum(refnum);
    WriteNextStateStop();
}
sletz's avatar
sletz committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116

// RT
void JackGraphManager::RunCurrentGraph()
{
    JackConnectionManager* manager = ReadCurrentState();
    manager->ResetGraph(fClientTiming);
}

// RT
bool JackGraphManager::RunNextGraph()
{
    bool res;
    JackConnectionManager* manager = TrySwitchState(&res);
    manager->ResetGraph(fClientTiming);
    return res;
}

// RT
bool JackGraphManager::IsFinishedGraph()
{
    JackConnectionManager* manager = ReadCurrentState();
    return (manager->GetActivation(FREEWHEEL_DRIVER_REFNUM) == 0);
}

// RT
117
int JackGraphManager::ResumeRefNum(JackClientControl* control, JackSynchro* table)
sletz's avatar
sletz committed
118
119
120
121
122
123
{
    JackConnectionManager* manager = ReadCurrentState();
    return manager->ResumeRefNum(control, table, fClientTiming);
}

// RT
124
int JackGraphManager::SuspendRefNum(JackClientControl* control, JackSynchro* table, long usec)
sletz's avatar
sletz committed
125
126
127
128
129
{
    JackConnectionManager* manager = ReadCurrentState();
    return manager->SuspendRefNum(control, table, fClientTiming, usec);
}

130
131
132
133
134
135
136
void JackGraphManager::TopologicalSort(std::vector<jack_int_t>& sorted)
{
    UInt16 cur_index;
    UInt16 next_index;

    do {
        cur_index = GetCurrentIndex();
sletz's avatar
sletz committed
137
138
        sorted.clear();
        ReadCurrentState()->TopologicalSort(sorted);
139
140
141
142
        next_index = GetCurrentIndex();
    } while (cur_index != next_index); // Until a coherent state has been read
}

sletz's avatar
sletz committed
143
144
145
146
147
// Server
void JackGraphManager::DirectConnect(int ref1, int ref2)
{
    JackConnectionManager* manager = WriteNextStateStart();
    manager->DirectConnect(ref1, ref2);
sletz's avatar
sletz committed
148
    jack_log("JackGraphManager::ConnectRefNum cur_index = %ld ref1 = %ld ref2 = %ld", CurIndex(fCounter), ref1, ref2);
sletz's avatar
sletz committed
149
150
151
152
153
154
155
156
    WriteNextStateStop();
}

// Server
void JackGraphManager::DirectDisconnect(int ref1, int ref2)
{
    JackConnectionManager* manager = WriteNextStateStart();
    manager->DirectDisconnect(ref1, ref2);
sletz's avatar
sletz committed
157
    jack_log("JackGraphManager::DisconnectRefNum cur_index = %ld ref1 = %ld ref2 = %ld", CurIndex(fCounter), ref1, ref2);
sletz's avatar
sletz committed
158
159
160
161
162
163
164
165
166
167
168
    WriteNextStateStop();
}

// Server
bool JackGraphManager::IsDirectConnection(int ref1, int ref2)
{
    JackConnectionManager* manager = ReadCurrentState();
    return manager->IsDirectConnection(ref1, ref2);
}

// RT
169
void* JackGraphManager::GetBuffer(jack_port_id_t port_index, jack_nframes_t buffer_size, bool nulled)
sletz's avatar
sletz committed
170
171
{
    AssertPort(port_index);
172
    AssertBufferSize(buffer_size);
sletz's avatar
sletz committed
173
174
175
176

    JackConnectionManager* manager = ReadCurrentState();
    JackPort* port = GetPort(port_index);

177
    // This happens when a port has just been unregistered and is still used by the RT code
sletz's avatar
sletz committed
178
    if (!port->IsUsed()) {
sletz's avatar
sletz committed
179
        jack_log("JackGraphManager::GetBuffer : port = %ld is released state", port_index);
sletz's avatar
sletz committed
180
181
182
        return GetBuffer(0); // port_index 0 is not used
    }

183
184
    jack_int_t len = manager->Connections(port_index);

sletz's avatar
sletz committed
185
186
    // Output port
    if (port->fFlags & JackPortIsOutput) {
187
188
189
190
191
192
        if (port->fTied != NO_PORT) {
            return GetBuffer(port->fTied, buffer_size);
        } else {
            return (len == 0 && nulled) ? NULL : GetBuffer(port_index);
        }
     }
sletz's avatar
sletz committed
193

194
    // No connections : return a zero-filled buffer
sletz's avatar
sletz committed
195
    if (len == 0) {
sletz's avatar
sletz committed
196
        port->ClearBuffer(buffer_size);
197
        return  (nulled) ? NULL : port->GetBuffer();
sletz's avatar
sletz committed
198

199
    // One connection
sletz's avatar
sletz committed
200
    } else if (len == 1) {
201
        jack_port_id_t src_index = manager->GetPort(port_index, 0);
sletz's avatar
sletz committed
202

203
204
205
206
207
208
209
210
211
212
        // Ports in same client : copy the buffer
        if (GetPort(src_index)->GetRefNum() == port->GetRefNum()) {
            void* buffers[1];
            buffers[0] = GetBuffer(src_index, buffer_size);
            port->MixBuffers(buffers, 1, buffer_size);
            return port->GetBuffer();
        // Otherwise, use zero-copy mode, just pass the buffer of the connected (output) port.
        } else {
            return GetBuffer(src_index, buffer_size);
        }
sletz's avatar
sletz committed
213

214
    // Multiple connections : mix all buffers
sletz's avatar
sletz committed
215
216
    } else {

sletz's avatar
sletz committed
217
        const jack_int_t* connections = manager->GetConnections(port_index);
218
        void* buffers[CONNECTION_NUM_FOR_PORT];
sletz's avatar
sletz committed
219
220
221
        jack_port_id_t src_index;
        int i;

222
        for (i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((src_index = connections[i]) != EMPTY); i++) {
sletz's avatar
sletz committed
223
            AssertPort(src_index);
sletz's avatar
sletz committed
224
            buffers[i] = GetBuffer(src_index, buffer_size);
sletz's avatar
sletz committed
225
        }
sletz's avatar
sletz committed
226
227

        port->MixBuffers(buffers, i, buffer_size);
sletz's avatar
sletz committed
228
        return port->GetBuffer();
sletz's avatar
sletz committed
229
230
231
    }
}

232
// Server
sletz's avatar
sletz committed
233
234
235
236
237
238
int JackGraphManager::RequestMonitor(jack_port_id_t port_index, bool onoff) // Client
{
    AssertPort(port_index);
    JackPort* port = GetPort(port_index);

    /**
sletz's avatar
sletz committed
239
    jackd.h
sletz's avatar
sletz committed
240
        * If @ref JackPortCanMonitor is set for this @a port, turn input
sletz's avatar
sletz committed
241
        * monitoring on or off. Otherwise, do nothing.
sletz's avatar
sletz committed
242

sletz's avatar
sletz committed
243
244
245
246
247
248
249
250
251
     if (!(fFlags & JackPortCanMonitor))
    	return -1;
    */

    port->RequestMonitor(onoff);

    const jack_int_t* connections = ReadCurrentState()->GetConnections(port_index);
    if ((port->fFlags & JackPortIsOutput) == 0) { // ?? Taken from jack, why not (port->fFlags  & JackPortIsInput) ?
        jack_port_id_t src_index;
252
        for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((src_index = connections[i]) != EMPTY); i++) {
sletz's avatar
sletz committed
253
254
255
256
257
258
259
260
            // XXX much worse things will happen if there is a feedback loop !!!
            RequestMonitor(src_index, onoff);
        }
    }

    return 0;
}

261
262
// Client
jack_nframes_t JackGraphManager::ComputeTotalLatencyAux(jack_port_id_t port_index, jack_port_id_t src_port_index, JackConnectionManager* manager, int hop_count)
sletz's avatar
sletz committed
263
{
264
    const jack_int_t* connections = ReadCurrentState()->GetConnections(port_index);
sletz's avatar
sletz committed
265
266
267
268
    jack_nframes_t max_latency = 0;
    jack_port_id_t dst_index;

    if (hop_count > 8)
sletz's avatar
sletz committed
269
        return GetPort(port_index)->GetLatency();
sletz's avatar
sletz committed
270

271
    for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((dst_index = connections[i]) != EMPTY); i++) {
sletz's avatar
sletz committed
272
        if (src_port_index != dst_index) {
sletz's avatar
sletz committed
273
            AssertPort(dst_index);
sletz's avatar
sletz committed
274
275
276
            JackPort* dst_port = GetPort(dst_index);
            jack_nframes_t this_latency = (dst_port->fFlags & JackPortIsTerminal)
                                          ? dst_port->GetLatency()
277
                                          : ComputeTotalLatencyAux(dst_index, port_index, manager, hop_count + 1);
278
            max_latency = ((max_latency > this_latency) ? max_latency : this_latency);
sletz's avatar
sletz committed
279
280
        }
    }
sletz's avatar
sletz committed
281

sletz's avatar
sletz committed
282
    return max_latency + GetPort(port_index)->GetLatency();
sletz's avatar
sletz committed
283
284
}

285
286
// Client
int JackGraphManager::ComputeTotalLatency(jack_port_id_t port_index)
sletz's avatar
sletz committed
287
288
289
{
    UInt16 cur_index;
    UInt16 next_index;
sletz's avatar
sletz committed
290
    JackPort* port = GetPort(port_index);
sletz's avatar
sletz committed
291
    AssertPort(port_index);
sletz's avatar
sletz committed
292

sletz's avatar
sletz committed
293
294
    do {
        cur_index = GetCurrentIndex();
295
        port->fTotalLatency = ComputeTotalLatencyAux(port_index, port_index, ReadCurrentState(), 0);
sletz's avatar
sletz committed
296
297
        next_index = GetCurrentIndex();
    } while (cur_index != next_index); // Until a coherent state has been read
sletz's avatar
sletz committed
298

sletz's avatar
sletz committed
299
    jack_log("JackGraphManager::GetTotalLatency port_index = %ld total latency = %ld", port_index, port->fTotalLatency);
sletz's avatar
sletz committed
300
    return 0;
301
}
sletz's avatar
sletz committed
302

303
304
305
// Client
int JackGraphManager::ComputeTotalLatencies()
{
sletz's avatar
sletz committed
306
    jack_port_id_t port_index;
sletz's avatar
sletz committed
307
    for (port_index = FIRST_AVAILABLE_PORT; port_index < fPortMax; port_index++) {
308
        JackPort* port = GetPort(port_index);
sletz's avatar
sletz committed
309
310
311
312
        if (port->IsUsed())
            ComputeTotalLatency(port_index);
    }
    return 0;
sletz's avatar
sletz committed
313
314
}

315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
void JackGraphManager::RecalculateLatencyAux(jack_port_id_t port_index, jack_latency_callback_mode_t mode)
{
    const jack_int_t* connections = ReadCurrentState()->GetConnections(port_index);
    JackPort* port = GetPort(port_index);
    jack_latency_range_t latency = { UINT32_MAX, 0 };
    jack_port_id_t dst_index;

    for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((dst_index = connections[i]) != EMPTY); i++) {
        AssertPort(dst_index);
        JackPort* dst_port = GetPort(dst_index);
        jack_latency_range_t other_latency;

        dst_port->GetLatencyRange(mode, &other_latency);

        if (other_latency.max > latency.max)
			latency.max = other_latency.max;
		if (other_latency.min < latency.min)
			latency.min = other_latency.min;
    }

    if (latency.min == UINT32_MAX)
		latency.min = 0;

	port->SetLatencyRange(mode, &latency);
}

void JackGraphManager::RecalculateLatency(jack_port_id_t port_index, jack_latency_callback_mode_t mode)
{
    UInt16 cur_index;
    UInt16 next_index;

    do {
        cur_index = GetCurrentIndex();
        RecalculateLatencyAux(port_index, mode);
        next_index = GetCurrentIndex();
    } while (cur_index != next_index); // Until a coherent state has been read

    jack_log("JackGraphManager::RecalculateLatency port_index = %ld", port_index);
}

sletz's avatar
Cleanup    
sletz committed
355
356
357
// Server
void JackGraphManager::SetBufferSize(jack_nframes_t buffer_size)
{
358
    jack_log("JackGraphManager::SetBufferSize size = %ld", buffer_size);
sletz's avatar
sletz committed
359
360

    jack_port_id_t port_index;
sletz's avatar
sletz committed
361
    for (port_index = FIRST_AVAILABLE_PORT; port_index < fPortMax; port_index++) {
sletz's avatar
Cleanup    
sletz committed
362
363
        JackPort* port = GetPort(port_index);
        if (port->IsUsed())
364
            port->ClearBuffer(buffer_size);
sletz's avatar
Cleanup    
sletz committed
365
366
367
    }
}

sletz's avatar
sletz committed
368
// Server
sletz's avatar
sletz committed
369
jack_port_id_t JackGraphManager::AllocatePortAux(int refnum, const char* port_name, const char* port_type, JackPortFlags flags)
sletz's avatar
sletz committed
370
371
372
{
    jack_port_id_t port_index;

sletz's avatar
Cleanup    
sletz committed
373
    // Available ports start at FIRST_AVAILABLE_PORT (= 1), otherwise a port_index of 0 is "seen" as a NULL port by the external API...
sletz's avatar
sletz committed
374
    for (port_index = FIRST_AVAILABLE_PORT; port_index < fPortMax; port_index++) {
sletz's avatar
sletz committed
375
376
        JackPort* port = GetPort(port_index);
        if (!port->IsUsed()) {
sletz's avatar
sletz committed
377
            jack_log("JackGraphManager::AllocatePortAux port_index = %ld name = %s type = %s", port_index, port_name, port_type);
sletz's avatar
sletz committed
378
            if (!port->Allocate(refnum, port_name, port_type, flags))
sletz's avatar
sletz committed
379
                return NO_PORT;
sletz's avatar
sletz committed
380
381
382
383
            break;
        }
    }

sletz's avatar
sletz committed
384
    return (port_index < fPortMax) ? port_index : NO_PORT;
sletz's avatar
sletz committed
385
386
387
}

// Server
388
jack_port_id_t JackGraphManager::AllocatePort(int refnum, const char* port_name, const char* port_type, JackPortFlags flags, jack_nframes_t buffer_size)
sletz's avatar
sletz committed
389
390
{
    JackConnectionManager* manager = WriteNextStateStart();
sletz's avatar
sletz committed
391
392
    jack_port_id_t port_index = AllocatePortAux(refnum, port_name, port_type, flags);

sletz's avatar
sletz committed
393
    if (port_index != NO_PORT) {
sletz's avatar
sletz committed
394
395
        JackPort* port = GetPort(port_index);
        assert(port);
396
        port->ClearBuffer(buffer_size);
sletz's avatar
sletz committed
397

sletz's avatar
sletz committed
398
399
400
401
402
403
        int res;
        if (flags & JackPortIsOutput) {
            res = manager->AddOutputPort(refnum, port_index);
        } else {
            res = manager->AddInputPort(refnum, port_index);
        }
sletz's avatar
sletz committed
404
        // Insertion failure
sletz's avatar
sletz committed
405
406
407
408
409
410
411
412
413
414
415
        if (res < 0) {
            port->Release();
            port_index = NO_PORT;
        }
    }

    WriteNextStateStop();
    return port_index;
}

// Server
416
int JackGraphManager::ReleasePort(int refnum, jack_port_id_t port_index)
sletz's avatar
sletz committed
417
418
419
420
421
422
423
424
425
426
427
428
429
{
    JackConnectionManager* manager = WriteNextStateStart();
    JackPort* port = GetPort(port_index);
    int res;

    if (port->fFlags & JackPortIsOutput) {
        DisconnectAllOutput(port_index);
        res = manager->RemoveOutputPort(refnum, port_index);
    } else {
        DisconnectAllInput(port_index);
        res = manager->RemoveInputPort(refnum, port_index);
    }

sletz's avatar
sletz committed
430
    port->Release();
sletz's avatar
sletz committed
431
432
433
434
    WriteNextStateStop();
    return res;
}

435
436
void JackGraphManager::GetInputPorts(int refnum, jack_int_t* res)
{
sletz's avatar
sletz committed
437
438
439
440
    JackConnectionManager* manager = WriteNextStateStart();
    const jack_int_t* input = manager->GetInputPorts(refnum);
    memcpy(res, input, sizeof(jack_int_t) * PORT_NUM_FOR_CLIENT);
    WriteNextStateStop();
441
442
443
444
}

void JackGraphManager::GetOutputPorts(int refnum, jack_int_t* res)
{
sletz's avatar
sletz committed
445
446
447
448
    JackConnectionManager* manager = WriteNextStateStart();
    const jack_int_t* output = manager->GetOutputPorts(refnum);
    memcpy(res, output, sizeof(jack_int_t) * PORT_NUM_FOR_CLIENT);
    WriteNextStateStop();
449
450
}

sletz's avatar
sletz committed
451
452
453
// Server
void JackGraphManager::RemoveAllPorts(int refnum)
{
sletz's avatar
sletz committed
454
    jack_log("JackGraphManager::RemoveAllPorts ref = %ld", refnum);
sletz's avatar
sletz committed
455
456
457
    JackConnectionManager* manager = WriteNextStateStart();
    jack_port_id_t port_index;

458
    // Warning : ReleasePort shift port to left, thus we always remove the first port until the "input" table is empty
sletz's avatar
sletz committed
459
460
    const jack_int_t* input = manager->GetInputPorts(refnum);
    while ((port_index = input[0]) != EMPTY) {
461
462
463
464
        int res = ReleasePort(refnum, port_index);
        if (res < 0) {
            jack_error("JackGraphManager::RemoveAllPorts failure ref = %ld port_index = %ld", refnum, port_index);
            assert(true);
sletz's avatar
sletz committed
465
            break;
466
        }
sletz's avatar
sletz committed
467
468
    }

469
    // Warning : ReleasePort shift port to left, thus we always remove the first port until the "output" table is empty
sletz's avatar
sletz committed
470
471
    const jack_int_t* output = manager->GetOutputPorts(refnum);
    while ((port_index = output[0]) != EMPTY) {
472
473
474
475
        int res = ReleasePort(refnum, port_index);
        if (res < 0) {
            jack_error("JackGraphManager::RemoveAllPorts failure ref = %ld port_index = %ld", refnum, port_index);
            assert(true);
sletz's avatar
sletz committed
476
            break;
sletz's avatar
sletz committed
477
        }
sletz's avatar
sletz committed
478
479
480
481
482
483
484
485
486
    }

    WriteNextStateStop();
}

// Server
void JackGraphManager::DisconnectAllPorts(int refnum)
{
    int i;
sletz's avatar
sletz committed
487
    jack_log("JackGraphManager::DisconnectAllPorts ref = %ld", refnum);
sletz's avatar
sletz committed
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
    JackConnectionManager* manager = WriteNextStateStart();

    const jack_int_t* input = manager->GetInputPorts(refnum);
    for (i = 0; i < PORT_NUM_FOR_CLIENT && input[i] != EMPTY ; i++) {
        DisconnectAllInput(input[i]);
    }

    const jack_int_t* output = manager->GetOutputPorts(refnum);
    for (i = 0; i < PORT_NUM_FOR_CLIENT && output[i] != EMPTY; i++) {
        DisconnectAllOutput(output[i]);
    }

    WriteNextStateStop();
}

// Server
void JackGraphManager::DisconnectAllInput(jack_port_id_t port_index)
{
506
    jack_log("JackGraphManager::DisconnectAllInput port_index = %ld", port_index);
sletz's avatar
sletz committed
507
508
    JackConnectionManager* manager = WriteNextStateStart();

sletz's avatar
sletz committed
509
    for (unsigned int i = 0; i < fPortMax; i++) {
sletz's avatar
sletz committed
510
        if (manager->IsConnected(i, port_index)) {
sletz's avatar
sletz committed
511
            jack_log("JackGraphManager::Disconnect i = %ld  port_index = %ld", i, port_index);
sletz's avatar
sletz committed
512
513
514
515
516
517
518
519
520
            Disconnect(i, port_index);
        }
    }
    WriteNextStateStop();
}

// Server
void JackGraphManager::DisconnectAllOutput(jack_port_id_t port_index)
{
sletz's avatar
sletz committed
521
    jack_log("JackGraphManager::DisconnectAllOutput port_index = %ld ", port_index);
sletz's avatar
sletz committed
522
523
524
    JackConnectionManager* manager = WriteNextStateStart();

    const jack_int_t* connections = manager->GetConnections(port_index);
sletz's avatar
sletz committed
525
526
    while (connections[0] != EMPTY) {
        Disconnect(port_index, connections[0]); // Warning : Disconnect shift port to left
sletz's avatar
sletz committed
527
528
529
530
531
532
533
534
535
536
537
    }
    WriteNextStateStop();
}

// Server
int JackGraphManager::DisconnectAll(jack_port_id_t port_index)
{
    AssertPort(port_index);

    JackPort* port = GetPort(port_index);
    if (port->fFlags & JackPortIsOutput) {
sletz's avatar
sletz committed
538
        DisconnectAllOutput(port_index);
sletz's avatar
sletz committed
539
    } else {
sletz's avatar
sletz committed
540
        DisconnectAllInput(port_index);
sletz's avatar
sletz committed
541
542
543
544
    }
    return 0;
}

sletz's avatar
sletz committed
545
546
547
// Server
void JackGraphManager::GetConnections(jack_port_id_t port_index, jack_int_t* res)
{
sletz's avatar
sletz committed
548
549
    JackConnectionManager* manager = WriteNextStateStart();
    const jack_int_t* connections = manager->GetConnections(port_index);
550
    memcpy(res, connections, sizeof(jack_int_t) * CONNECTION_NUM_FOR_PORT);
sletz's avatar
sletz committed
551
    WriteNextStateStop();
sletz's avatar
sletz committed
552
553
}

554
555
556
// Server
void JackGraphManager::Activate(int refnum)
{
sletz's avatar
sletz committed
557
    DirectConnect(FREEWHEEL_DRIVER_REFNUM, refnum);
558
559
560
561
562
563
564
565
566
567
568
    DirectConnect(refnum, FREEWHEEL_DRIVER_REFNUM);
}

/*
	Disconnection from the FW must be done in last otherwise an intermediate "unconnected"
	(thus unactivated) state may happen where the client is still checked for its end.
*/

// Server
void JackGraphManager::Deactivate(int refnum)
{
sletz's avatar
sletz committed
569
    // Disconnect only when needed
570
571
572
    if (IsDirectConnection(refnum, FREEWHEEL_DRIVER_REFNUM)) {
        DirectDisconnect(refnum, FREEWHEEL_DRIVER_REFNUM);
    } else {
573
        jack_log("JackServer::Deactivate client = %ld was not activated", refnum);
574
575
    }

sletz's avatar
sletz committed
576
    // Disconnect only when needed
577
578
579
    if (IsDirectConnection(FREEWHEEL_DRIVER_REFNUM, refnum)) {
        DirectDisconnect(FREEWHEEL_DRIVER_REFNUM, refnum);
    } else {
580
        jack_log("JackServer::Deactivate client = %ld was not activated", refnum);
581
582
583
    }
}

sletz's avatar
sletz committed
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
// Server
int JackGraphManager::GetInputRefNum(jack_port_id_t port_index)
{
    AssertPort(port_index);
    JackConnectionManager* manager = WriteNextStateStart();
    int res = manager->GetInputRefNum(port_index);
    WriteNextStateStop();
    return res;
}

// Server
int JackGraphManager::GetOutputRefNum(jack_port_id_t port_index)
{
    AssertPort(port_index);
    JackConnectionManager* manager = WriteNextStateStart();
    int res = manager->GetOutputRefNum(port_index);
    WriteNextStateStop();
    return res;
}

int JackGraphManager::Connect(jack_port_id_t port_src, jack_port_id_t port_dst)
{
    JackConnectionManager* manager = WriteNextStateStart();
sletz's avatar
sletz committed
607
    jack_log("JackGraphManager::Connect port_src = %ld port_dst = %ld", port_src, port_dst);
sletz's avatar
sletz committed
608
609
    JackPort* src = GetPort(port_src);
    JackPort* dst = GetPort(port_dst);
sletz's avatar
sletz committed
610
611
    int res = 0;

sletz's avatar
sletz committed
612
613
    if (!src->fInUse || !dst->fInUse) {
        if (!src->fInUse)
614
            jack_error("JackGraphManager::Connect port_src = %ld not used name = %s", port_src, GetPort(port_src)->fName);
sletz's avatar
sletz committed
615
        if (!dst->fInUse)
616
            jack_error("JackGraphManager::Connect port_dst = %ld not used name = %s", port_dst, GetPort(port_dst)->fName);
sletz's avatar
sletz committed
617
618
619
        res = -1;
        goto end;
    }
sletz's avatar
sletz committed
620
    if (src->fTypeId != dst->fTypeId) {
621
        jack_error("JackGraphManager::Connect different port types port_src = %ld port_dst = %ld", port_src, port_dst);
sletz's avatar
sletz committed
622
623
        res = -1;
        goto end;
sletz's avatar
sletz committed
624
    }
sletz's avatar
sletz committed
625
626
627
628
629
630
631
632
633
634
635
    if (manager->IsConnected(port_src, port_dst)) {
        jack_error("JackGraphManager::Connect already connected port_src = %ld port_dst = %ld", port_src, port_dst);
        res = EEXIST;
        goto end;
    }

    res = manager->Connect(port_src, port_dst);
    if (res < 0) {
        jack_error("JackGraphManager::Connect failed port_src = %ld port_dst = %ld", port_src, port_dst);
        goto end;
    }
636
    res = manager->Connect(port_dst, port_src);
sletz's avatar
sletz committed
637
    if (res < 0) {
638
        jack_error("JackGraphManager::Connect failed port_dst = %ld port_src = %ld", port_dst, port_src);
sletz's avatar
sletz committed
639
640
641
642
        goto end;
    }

    if (manager->IsLoopPath(port_src, port_dst)) {
sletz's avatar
sletz committed
643
        jack_log("JackGraphManager::Connect: LOOP detected");
sletz's avatar
sletz committed
644
645
646
647
648
649
650
651
652
653
654
655
656
657
        manager->IncFeedbackConnection(port_src, port_dst);
    } else {
        manager->IncDirectConnection(port_src, port_dst);
    }

end:
    WriteNextStateStop();
    return res;
}

// Server
int JackGraphManager::Disconnect(jack_port_id_t port_src, jack_port_id_t port_dst)
{
    JackConnectionManager* manager = WriteNextStateStart();
sletz's avatar
sletz committed
658
    jack_log("JackGraphManager::Disconnect port_src = %ld port_dst = %ld", port_src, port_dst);
sletz's avatar
sletz committed
659
    bool in_use_src = GetPort(port_src)->fInUse;
660
    bool in_use_dst = GetPort(port_dst)->fInUse;
sletz's avatar
sletz committed
661
662
663
664
    int res = 0;

    if (!in_use_src || !in_use_dst) {
        if (!in_use_src)
665
            jack_error("JackGraphManager::Disconnect: port_src = %ld not used name = %s", port_src, GetPort(port_src)->fName);
sletz's avatar
sletz committed
666
        if (!in_use_dst)
667
            jack_error("JackGraphManager::Disconnect: port_src = %ld not used name = %s", port_dst, GetPort(port_dst)->fName);
sletz's avatar
sletz committed
668
669
670
671
672
673
674
675
676
        res = -1;
        goto end;
    }
    if (!manager->IsConnected(port_src, port_dst)) {
        jack_error("JackGraphManager::Disconnect not connected port_src = %ld port_dst = %ld", port_src, port_dst);
        res = -1;
        goto end;
    }

677
    res = manager->Disconnect(port_src, port_dst);
sletz's avatar
sletz committed
678
679
680
681
    if (res < 0) {
        jack_error("JackGraphManager::Disconnect failed port_src = %ld port_dst = %ld", port_src, port_dst);
        goto end;
    }
682
    res = manager->Disconnect(port_dst, port_src);
sletz's avatar
sletz committed
683
    if (res < 0) {
684
        jack_error("JackGraphManager::Disconnect failed port_dst = %ld port_src = %ld", port_dst, port_src);
sletz's avatar
sletz committed
685
686
687
688
        goto end;
    }

    if (manager->IsFeedbackConnection(port_src, port_dst)) {
sletz's avatar
sletz committed
689
        jack_log("JackGraphManager::Disconnect: FEEDBACK removed");
sletz's avatar
sletz committed
690
691
692
693
694
695
696
697
698
699
700
        manager->DecFeedbackConnection(port_src, port_dst);
    } else {
        manager->DecDirectConnection(port_src, port_dst);
    }

end:
    WriteNextStateStop();
    return res;
}

// Client
701
int JackGraphManager::IsConnected(jack_port_id_t port_src, jack_port_id_t port_dst)
sletz's avatar
sletz committed
702
703
{
    JackConnectionManager* manager = ReadCurrentState();
sletz's avatar
sletz committed
704
    return manager->IsConnected(port_src, port_dst);
sletz's avatar
sletz committed
705
706
707
708
709
710
711
712
}

// Server
int JackGraphManager::CheckPorts(jack_port_id_t port_src, jack_port_id_t port_dst)
{
    JackPort* src = GetPort(port_src);
    JackPort* dst = GetPort(port_dst);

713
    if ((dst->fFlags & JackPortIsInput) == 0) {
sletz's avatar
sletz committed
714
715
716
717
        jack_error("Destination port in attempted (dis)connection of %s and %s is not an input port", src->fName, dst->fName);
        return -1;
    }

718
    if ((src->fFlags & JackPortIsOutput) == 0) {
sletz's avatar
sletz committed
719
720
721
722
723
724
725
        jack_error("Source port in attempted (dis)connection of %s and %s is not an output port", src->fName, dst->fName);
        return -1;
    }

    return 0;
}

726
int JackGraphManager::GetTwoPorts(const char* src_name, const char* dst_name, jack_port_id_t* port_src, jack_port_id_t* port_dst)
sletz's avatar
sletz committed
727
{
sletz's avatar
sletz committed
728
    jack_log("JackGraphManager::CheckConnect src_name = %s dst_name = %s", src_name, dst_name);
sletz's avatar
sletz committed
729
730
731
732
733
734
735
736
737
738
739

    if ((*port_src = GetPort(src_name)) == NO_PORT) {
        jack_error("Unknown source port in attempted (dis)connection src_name [%s] dst_name [%s]", src_name, dst_name);
        return -1;
    }

    if ((*port_dst = GetPort(dst_name)) == NO_PORT) {
        jack_error("Unknown destination port in attempted (dis)connection src_name [%s] dst_name [%s]", src_name, dst_name);
        return -1;
    }

740
    return 0;
sletz's avatar
sletz committed
741
742
743
744
745
}

// Client : port array
jack_port_id_t JackGraphManager::GetPort(const char* name)
{
sletz's avatar
sletz committed
746
    for (unsigned int i = 0; i < fPortMax; i++) {
sletz's avatar
sletz committed
747
        JackPort* port = GetPort(i);
748
        if (port->IsUsed() && port->NameEquals(name))
sletz's avatar
sletz committed
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
            return i;
    }
    return NO_PORT;
}

/*!
\brief Get the connection port name array.
*/

// Client
void JackGraphManager::GetConnectionsAux(JackConnectionManager* manager, const char** res, jack_port_id_t port_index)
{
    const jack_int_t* connections = manager->GetConnections(port_index);
    jack_int_t index;
    int i;
sletz's avatar
sletz committed
764

765
    // Cleanup connection array
766
    memset(res, 0, sizeof(char*) * CONNECTION_NUM_FOR_PORT);
sletz's avatar
sletz committed
767

sletz's avatar
sletz committed
768
    for (i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((index = connections[i]) != EMPTY); i++) {
sletz's avatar
sletz committed
769
770
771
772
773
774
775
776
777
778
779
780
        JackPort* port = GetPort(index);
        res[i] = port->fName;
    }

    res[i] = NULL;
}

/*
	Use the state returned by ReadCurrentState and check that the state was not changed during the read operation.
	The operation is lock-free since there is no intermediate state in the write operation that could cause the
	read to loop forever.
*/
781
782

// Client
sletz's avatar
sletz committed
783
784
const char** JackGraphManager::GetConnections(jack_port_id_t port_index)
{
785
    const char** res = (const char**)malloc(sizeof(char*) * CONNECTION_NUM_FOR_PORT);
786
    UInt16 cur_index, next_index;
sletz's avatar
sletz committed
787

788
789
    if (!res)
        return NULL;
sletz's avatar
sletz committed
790
791
792
793
794
795
796

    do {
        cur_index = GetCurrentIndex();
        GetConnectionsAux(ReadCurrentState(), res, port_index);
        next_index = GetCurrentIndex();
    } while (cur_index != next_index); // Until a coherent state has been read

797
    if (res[0]) {	// At least one connection
798
        return res;
799
    } else {		// Empty array, should return NULL
800
801
802
        free(res);
        return NULL;
    }
sletz's avatar
sletz committed
803
804
805
}

// Client
806
void JackGraphManager::GetPortsAux(const char** matching_ports, const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
sletz's avatar
sletz committed
807
{
808
809
    int match_cnt = 0;
    regex_t port_regex, type_regex;
sletz's avatar
sletz committed
810

sletz's avatar
sletz committed
811
812
813
814
815
816
817
    if (port_name_pattern && port_name_pattern[0]) {
        regcomp(&port_regex, port_name_pattern, REG_EXTENDED | REG_NOSUB);
    }
    if (type_name_pattern && type_name_pattern[0]) {
        regcomp(&type_regex, type_name_pattern, REG_EXTENDED | REG_NOSUB);
    }

818
    // Cleanup port array
sletz's avatar
sletz committed
819
    memset(matching_ports, 0, sizeof(char*) * fPortMax);
sletz's avatar
sletz committed
820

sletz's avatar
sletz committed
821
    for (unsigned int i = 0; i < fPortMax; i++) {
822
        bool matching = true;
sletz's avatar
sletz committed
823
        JackPort* port = GetPort(i);
sletz's avatar
sletz committed
824
825
826
827
828
829
830
831
832
833

        if (port->IsUsed()) {

            if (flags) {
                if ((port->fFlags & flags) != flags) {
                    matching = false;
                }
            }

            if (matching && port_name_pattern && port_name_pattern[0]) {
834
                if (regexec(&port_regex, port->GetName(), 0, NULL, 0)) {
sletz's avatar
sletz committed
835
836
837
                    matching = false;
                }
            }
sletz's avatar
sletz committed
838
            if (matching && type_name_pattern && type_name_pattern[0]) {
839
                if (regexec(&type_regex, port->GetType(), 0, NULL, 0)) {
sletz's avatar
sletz committed
840
841
842
843
844
845
846
847
848
849
850
851
                    matching = false;
                }
            }

            if (matching) {
                matching_ports[match_cnt++] = port->fName;
            }
        }
    }

    matching_ports[match_cnt] = 0;

852
853
854
855
856
857
    if (port_name_pattern && port_name_pattern[0]) {
        regfree(&port_regex);
    }
    if (type_name_pattern && type_name_pattern[0]) {
        regfree(&type_regex);
    }
sletz's avatar
sletz committed
858
859
860
861
862
863
864
865
866
867
}

// Client
/*
	Check that the state was not changed during the read operation.
	The operation is lock-free since there is no intermediate state in the write operation that could cause the
	read to loop forever.
*/
const char** JackGraphManager::GetPorts(const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
{
sletz's avatar
sletz committed
868
    const char** res = (const char**)malloc(sizeof(char*) * fPortMax);
869
    UInt16 cur_index, next_index;
sletz's avatar
sletz committed
870

871
872
    if (!res)
        return NULL;
sletz's avatar
sletz committed
873

sletz's avatar
sletz committed
874
875
    do {
        cur_index = GetCurrentIndex();
876
        GetPortsAux(res, port_name_pattern, type_name_pattern, flags);
sletz's avatar
sletz committed
877
        next_index = GetCurrentIndex();
sletz's avatar
sletz committed
878
    } while (cur_index != next_index);  // Until a coherent state has been read
sletz's avatar
sletz committed
879

880
    if (res[0]) {    // At least one port
881
882
        return res;
    } else {
883
        free(res);   // Empty array, should return NULL
884
885
        return NULL;
    }
sletz's avatar
sletz committed
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
}

// Server
void JackGraphManager::Save(JackConnectionManager* dst)
{
    JackConnectionManager* manager = WriteNextStateStart();
    memcpy(dst, manager, sizeof(JackConnectionManager));
    WriteNextStateStop();
}

// Server
void JackGraphManager::Restore(JackConnectionManager* src)
{
    JackConnectionManager* manager = WriteNextStateStart();
    memcpy(manager, src, sizeof(JackConnectionManager));
    WriteNextStateStop();
}

} // end of namespace