Jackdmp.cpp 22.6 KB
Newer Older
sletz's avatar
sletz committed
1
/*
sletz's avatar
sletz committed
2
Copyright (C) 2001 Paul Davis
3
Copyright (C) 2004-2013 Grame
sletz's avatar
sletz committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

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 <iostream>
#include <assert.h>
23
24
#include <cassert>
#include <csignal>
sletz's avatar
sletz committed
25
26
#include <sys/types.h>
#include <getopt.h>
27
28
#include <cstring>
#include <cstdio>
29
#include <list>
30

31
#include "types.h"
sletz's avatar
sletz committed
32
#include "jack.h"
33
#include "control.h"
sletz's avatar
sletz committed
34
#include "JackConstants.h"
35
#include "JackPlatformPlug.h"
36
37
38
#ifdef __ANDROID__
#include "JackControlAPIAndroid.h"
#endif
sletz's avatar
sletz committed
39

40
#if defined(JACK_DBUS) && defined(__linux__)
41
#include <stdlib.h>
42
#include <dbus/dbus.h>
43
44
45
#include "audio_reserve.h"
#endif

sletz's avatar
sletz committed
46
/*
47
This is a simple port of the old jackdmp.cpp file to use the new jack2 control API. Available options for the server
sletz's avatar
sletz committed
48
49
50
51
are "hard-coded" in the source. A much better approach would be to use the control API to:
- dynamically retrieve available server parameters and then prepare to parse them
- get available drivers and their possible parameters, then prepare to parse them.
*/
sletz's avatar
sletz committed
52

53
#ifdef __APPLE__
54
#include <CoreFoundation/CFNotificationCenter.h>
55
#include <CoreFoundation/CoreFoundation.h>
56

sletz's avatar
sletz committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
static void notify_server_start(const char* server_name)
{
    // Send notification to be used in the JackRouter plugin
    CFStringRef ref = CFStringCreateWithCString(NULL, server_name, kCFStringEncodingMacRoman);
    CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDistributedCenter(),
            CFSTR("com.grame.jackserver.start"),
            ref,
            NULL,
            kCFNotificationDeliverImmediately | kCFNotificationPostToAllSessions);
    CFRelease(ref);
}

static void notify_server_stop(const char* server_name)
{
    // Send notification to be used in the JackRouter plugin
sletz's avatar
sletz committed
72
    CFStringRef ref1 = CFStringCreateWithCString(NULL, server_name, kCFStringEncodingMacRoman);
sletz's avatar
sletz committed
73
74
75
76
77
78
79
    CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDistributedCenter(),
            CFSTR("com.grame.jackserver.stop"),
            ref1,
            NULL,
            kCFNotificationDeliverImmediately | kCFNotificationPostToAllSessions);
    CFRelease(ref1);
}
sletz's avatar
sletz committed
80

sletz's avatar
sletz committed
81
82
83
84
85
86
87
88
#else

static void notify_server_start(const char* server_name)
{}
static void notify_server_stop(const char* server_name)
{}

#endif
sletz's avatar
sletz committed
89
90
91

static void copyright(FILE* file)
{
sletz's avatar
sletz committed
92
    fprintf(file, "jackdmp " VERSION "\n"
sletz's avatar
sletz committed
93
            "Copyright 2001-2005 Paul Davis and others.\n"
Stephane Letz's avatar
Stephane Letz committed
94
            "Copyright 2004-2014 Grame.\n"
sletz's avatar
sletz committed
95
96
97
            "jackdmp comes with ABSOLUTELY NO WARRANTY\n"
            "This is free software, and you are welcome to redistribute it\n"
            "under certain conditions; see the file COPYING for details\n");
sletz's avatar
sletz committed
98
99
}

100
static jackctl_driver_t * jackctl_server_get_driver(jackctl_server_t *server, const char *driver_name)
sletz's avatar
sletz committed
101
{
102
    const JSList * node_ptr = jackctl_server_get_drivers_list(server);
sletz's avatar
sletz committed
103

104
105
    while (node_ptr) {
        if (strcmp(jackctl_driver_get_name((jackctl_driver_t *)node_ptr->data), driver_name) == 0) {
sletz's avatar
sletz committed
106
107
108
109
110
111
            return (jackctl_driver_t *)node_ptr->data;
        }
        node_ptr = jack_slist_next(node_ptr);
    }

    return NULL;
sletz's avatar
sletz committed
112
113
}

114
115
116
117
118
119
120
121
122
123
124
125
126
127
static jackctl_internal_t * jackctl_server_get_internal(jackctl_server_t *server, const char *internal_name)
{
    const JSList * node_ptr = jackctl_server_get_internals_list(server);

    while (node_ptr) {
        if (strcmp(jackctl_internal_get_name((jackctl_internal_t *)node_ptr->data), internal_name) == 0) {
            return (jackctl_internal_t *)node_ptr->data;
        }
        node_ptr = jack_slist_next(node_ptr);
    }

    return NULL;
}

128
129
130
131
132
133
134
135
136
137
138
139
static jackctl_parameter_t * jackctl_get_parameter(const JSList * parameters_list, const char * parameter_name)
{
    while (parameters_list) {
        if (strcmp(jackctl_parameter_get_name((jackctl_parameter_t *)parameters_list->data), parameter_name) == 0) {
            return (jackctl_parameter_t *)parameters_list->data;
        }
        parameters_list = jack_slist_next(parameters_list);
    }

    return NULL;
}

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#ifdef __ANDROID__
static void jackctl_server_switch_master_dummy(jackctl_server_t * server_ctl, char * master_driver_name)
{
    static bool is_dummy_driver = false;
    if(!strcmp(master_driver_name, "dummy")) {
        return;
    }
    jackctl_driver_t * driver_ctr;
    if(is_dummy_driver) {
        is_dummy_driver = false;
        driver_ctr = jackctl_server_get_driver(server_ctl, master_driver_name);
    } else {
        is_dummy_driver = true;
        driver_ctr = jackctl_server_get_driver(server_ctl, "dummy");
    }
    jackctl_server_switch_master(server_ctl, driver_ctr);
}
#endif

159
160
161
162
163
164
165
166
167
168
169
170
171
172
static void print_server_drivers(jackctl_server_t *server, FILE* file)
{
    const JSList * node_ptr = jackctl_server_get_drivers_list(server);

    fprintf(file, "Available backends:\n");
    
    while (node_ptr) {
        jackctl_driver_t* driver = (jackctl_driver_t *)node_ptr->data;
        fprintf(file, "      %s (%s)\n", jackctl_driver_get_name(driver), (jackctl_driver_get_type(driver) == JackMaster) ? "master" : "slave");
        node_ptr = jack_slist_next(node_ptr);
    }
    fprintf(file, "\n");
}

173
174
175
176
177
178
179
180
181
182
183
184
185
186
static void print_server_internals(jackctl_server_t *server, FILE* file)
{
    const JSList * node_ptr = jackctl_server_get_internals_list(server);

    fprintf(file, "Available internals:\n");
    
    while (node_ptr) {
        jackctl_internal_t* internal = (jackctl_internal_t *)node_ptr->data;
        fprintf(file, "      %s\n", jackctl_internal_get_name(internal));
        node_ptr = jack_slist_next(node_ptr);
    }
    fprintf(file, "\n");
}

187
static void usage(FILE* file, jackctl_server_t *server, bool full = true)
188
{
189
190
191
192
193
    jackctl_parameter_t * param;
    const JSList * server_parameters;
    uint32_t i;
    union jackctl_parameter_value value;

194
    fprintf(file, "\n"
195
            "Usage: jackdmp [ --no-realtime OR -r ]\n"
196
197
198
199
200
201
202
203
204
205
            "               [ --realtime OR -R [ --realtime-priority OR -P priority ] ]\n"
            "      (the two previous arguments are mutually exclusive. The default is --realtime)\n"
            "               [ --name OR -n server-name ]\n"
            "               [ --timeout OR -t client-timeout-in-msecs ]\n"
            "               [ --loopback OR -L loopback-port-number ]\n"
            "               [ --port-max OR -p maximum-number-of-ports]\n"
            "               [ --slave-backend OR -X slave-backend-name ]\n"
            "               [ --internal-client OR -I internal-client-name ]\n"
            "               [ --verbose OR -v ]\n"
#ifdef __linux__
206
            "               [ --clocksource OR -c [ h(pet) | s(ystem) ]\n"
207
#endif
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
            "               [ --autoconnect OR -a <modechar>]\n");

    server_parameters = jackctl_server_get_parameters(server);
    param = jackctl_get_parameter(server_parameters, "self-connect-mode");
    fprintf(file,
            "                 where <modechar> is one of:\n");
    for (i = 0; i < jackctl_parameter_get_enum_constraints_count(param); i++)
    {
        value = jackctl_parameter_get_enum_constraint_value(param, i);
        fprintf(file, "                   '%c' - %s", value.c, jackctl_parameter_get_enum_constraint_description(param, i));
        if (value.c == JACK_DEFAULT_SELF_CONNECT_MODE)
        {
            fprintf(file, " (default)");
        }
        fprintf(file, "\n");
    }

    fprintf(file,
226
227
228
229
230
231
232
233
234
            "               [ --replace-registry ]\n"
            "               [ --silent OR -s ]\n"
            "               [ --sync OR -S ]\n"
            "               [ --temporary OR -T ]\n"
            "               [ --version OR -V ]\n"
            "         -d master-backend-name [ ... master-backend args ... ]\n"
            "       jackdmp -d master-backend-name --help\n"
            "             to display options for each master backend\n\n");
    
235
    if (full) {
236
237
238
239
240
        print_server_drivers(server, file);
        print_server_internals(server, file);
    }
}

sletz's avatar
sletz committed
241
242
243
// Prototype to be found in libjackserver
extern "C" void silent_jack_error_callback(const char *desc);

244
int main(int argc, char** argv)
sletz's avatar
sletz committed
245
{
sletz's avatar
sletz committed
246
247
    jackctl_server_t * server_ctl;
    const JSList * server_parameters;
248
    const char* server_name = JACK_DEFAULT_SERVER_NAME;
249
    jackctl_driver_t * master_driver_ctl;
sletz's avatar
sletz committed
250
    jackctl_driver_t * loopback_driver_ctl = NULL;
251
    int replace_registry = 0;
252
    const char *options = "-d:X:I:P:uvshVrRL:STFl:t:mn:p:"
253
        "a:"
254
#ifdef __linux__
255
        "c:"
256
#endif
257
        ;
258

sletz's avatar
sletz committed
259
    struct option long_options[] = {
260
261
262
#ifdef __linux__
                                       { "clock-source", 1, 0, 'c' },
#endif
263
                                       { "loopback-driver", 1, 0, 'L' },
264
265
                                       { "audio-driver", 1, 0, 'd' },
                                       { "midi-driver", 1, 0, 'X' },
266
                                       { "internal-client", 1, 0, 'I' },
sletz's avatar
sletz committed
267
268
269
270
                                       { "verbose", 0, 0, 'v' },
                                       { "help", 0, 0, 'h' },
                                       { "port-max", 1, 0, 'p' },
                                       { "no-mlock", 0, 0, 'm' },
sletz's avatar
sletz committed
271
                                       { "name", 1, 0, 'n' },
sletz's avatar
sletz committed
272
273
                                       { "unlock", 0, 0, 'u' },
                                       { "realtime", 0, 0, 'R' },
274
                                       { "no-realtime", 0, 0, 'r' },
275
                                       { "replace-registry", 0, &replace_registry, 0 },
sletz's avatar
sletz committed
276
277
278
279
280
281
282
                                       { "loopback", 0, 0, 'L' },
                                       { "realtime-priority", 1, 0, 'P' },
                                       { "timeout", 1, 0, 't' },
                                       { "temporary", 0, 0, 'T' },
                                       { "version", 0, 0, 'V' },
                                       { "silent", 0, 0, 's' },
                                       { "sync", 0, 0, 'S' },
283
                                       { "autoconnect", 1, 0, 'a' },
sletz's avatar
sletz committed
284
285
                                       { 0, 0, 0, 0 }
                                   };
286

sletz's avatar
sletz committed
287
    int i,opt = 0;
sletz's avatar
sletz committed
288
    int option_index = 0;
289
290
    char* master_driver_name = NULL;
    char** master_driver_args = NULL;
291
    int master_driver_nargs = 1;
292
    int loopback = 0;
sletz's avatar
sletz committed
293
    bool show_version = false;
294
    jackctl_sigmask_t * sigmask;
sletz's avatar
sletz committed
295
296
    jackctl_parameter_t* param;
    union jackctl_parameter_value value;
sletz's avatar
sletz committed
297

298
299
300
301
    std::list<char*> internals_list;
    std::list<char*> slaves_list;
    std::list<char*>::iterator it;

302
303
304
305
    // Assume that we fail.
    int return_value = -1;
    bool notify_sent = false;

nedko's avatar
nedko committed
306
    copyright(stdout);
307
#if defined(JACK_DBUS) && defined(__linux__)
308
309
310
311
    if (getenv("JACK_NO_AUDIO_RESERVATION"))
        server_ctl = jackctl_server_create(NULL, NULL);
    else
        server_ctl = jackctl_server_create(audio_acquire, audio_release);
312
#else
313
    server_ctl = jackctl_server_create(NULL, NULL);
314
#endif
sletz's avatar
sletz committed
315
    if (server_ctl == NULL) {
sletz's avatar
Cleanup    
sletz committed
316
        fprintf(stderr, "Failed to create server object\n");
sletz's avatar
sletz committed
317
318
        return -1;
    }
319

sletz's avatar
sletz committed
320
    server_parameters = jackctl_server_get_parameters(server_ctl);
321

sletz's avatar
sletz committed
322
    opterr = 0;
323
    while (!master_driver_name &&
sletz's avatar
sletz committed
324
325
326
327
            (opt = getopt_long(argc, argv, options,
                               long_options, &option_index)) != EOF) {
        switch (opt) {

328
        #ifdef __linux__
329
330
331
332
333
334
335
            case 'c':
                param = jackctl_get_parameter(server_parameters, "clock-source");
                if (param != NULL) {
                    if (tolower (optarg[0]) == 'h') {
                        value.ui = JACK_TIMER_HPET;
                        jackctl_parameter_set_value(param, &value);
                    } else if (tolower (optarg[0]) == 'c') {
Adrian Knoth's avatar
Adrian Knoth committed
336
337
338
339
340
                        /* For backwards compatibility with scripts, allow
                         * the user to request the cycle clock on the
                         * command line, but use the system clock instead
                         */
                        value.ui = JACK_TIMER_SYSTEM_CLOCK;
341
342
343
344
345
                        jackctl_parameter_set_value(param, &value);
                    } else if (tolower (optarg[0]) == 's') {
                        value.ui = JACK_TIMER_SYSTEM_CLOCK;
                        jackctl_parameter_set_value(param, &value);
                    } else {
346
                        usage(stdout, server_ctl);
347
                        goto destroy_server;
348
349
350
351
352
                    }
                }
                break;
        #endif

353
354
355
            case 'a':
                param = jackctl_get_parameter(server_parameters, "self-connect-mode");
                if (param != NULL) {
Nedko Arnaudov's avatar
Nedko Arnaudov committed
356
                    bool value_valid = false;
357
                    for (uint32_t k=0; k<jackctl_parameter_get_enum_constraints_count( param ); k++ ) {
Nedko Arnaudov's avatar
Nedko Arnaudov committed
358
359
360
361
362
363
                        value = jackctl_parameter_get_enum_constraint_value( param, k );
                        if( value.c == optarg[0] )
                            value_valid = true;
                    }

                    if( value_valid ) {
Torben Hohn's avatar
Torben Hohn committed
364
                        value.c = optarg[0];
365
366
                        jackctl_parameter_set_value(param, &value);
                    } else {
367
                        usage(stdout, server_ctl);
368
                        goto destroy_server;
369
370
371
                    }
                }
                break;
Torben Hohn's avatar
Torben Hohn committed
372

sletz's avatar
sletz committed
373
            case 'd':
374
                master_driver_name = optarg;
375
                break;
376

377
378
379
            case 'L':
                loopback = atoi(optarg);
                break;
sletz's avatar
sletz committed
380

381
            case 'X':
382
383
384
385
386
                slaves_list.push_back(optarg);
                break;

            case 'I':
                internals_list.push_back(optarg);
sletz's avatar
sletz committed
387
                break;
sletz's avatar
sletz committed
388

389
            case 'p':
sletz's avatar
sletz committed
390
391
392
393
394
                param = jackctl_get_parameter(server_parameters, "port-max");
                if (param != NULL) {
                    value.ui = atoi(optarg);
                    jackctl_parameter_set_value(param, &value);
                }
395
                break;
sletz's avatar
sletz committed
396

397
398
399
400
401
402
            case 'm':
                break;

            case 'u':
                break;

sletz's avatar
sletz committed
403
            case 'v':
sletz's avatar
sletz committed
404
405
406
407
408
                param = jackctl_get_parameter(server_parameters, "verbose");
                if (param != NULL) {
                    value.b = true;
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
409
410
411
412
413
414
415
                break;

            case 's':
                jack_set_error_function(silent_jack_error_callback);
                break;

            case 'S':
sletz's avatar
sletz committed
416
417
418
419
420
                param = jackctl_get_parameter(server_parameters, "sync");
                if (param != NULL) {
                    value.b = true;
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
421
422
423
424
                break;

            case 'n':
                server_name = optarg;
sletz's avatar
sletz committed
425
426
427
428
429
                param = jackctl_get_parameter(server_parameters, "name");
                if (param != NULL) {
                    strncpy(value.str, optarg, JACK_PARAM_STRING_MAX);
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
430
431
432
                break;

            case 'P':
sletz's avatar
sletz committed
433
434
435
436
437
                param = jackctl_get_parameter(server_parameters, "realtime-priority");
                if (param != NULL) {
                    value.i = atoi(optarg);
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
438
                break;
439

sletz's avatar
sletz committed
440
            case 'r':
441
                param = jackctl_get_parameter(server_parameters, "realtime");
sletz's avatar
sletz committed
442
                if (param != NULL) {
443
                    value.b = false;
sletz's avatar
sletz committed
444
445
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
446
447
                break;

sletz's avatar
sletz committed
448
            case 'R':
sletz's avatar
sletz committed
449
450
451
452
453
                param = jackctl_get_parameter(server_parameters, "realtime");
                if (param != NULL) {
                    value.b = true;
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
454
455
456
                break;

            case 'T':
sletz's avatar
sletz committed
457
458
459
460
461
                param = jackctl_get_parameter(server_parameters, "temporary");
                if (param != NULL) {
                    value.b = true;
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
462
463
464
                break;

            case 't':
sletz's avatar
sletz committed
465
466
467
468
469
                param = jackctl_get_parameter(server_parameters, "client-timeout");
                if (param != NULL) {
                    value.i = atoi(optarg);
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
470
471
472
                break;

            case 'V':
sletz's avatar
sletz committed
473
                show_version = true;
sletz's avatar
sletz committed
474
475
476
                break;

            default:
sletz's avatar
sletz committed
477
                fprintf(stderr, "unknown option character %c\n", optopt);
sletz's avatar
sletz committed
478
                /*fallthru*/
sletz's avatar
sletz committed
479

sletz's avatar
sletz committed
480
            case 'h':
481
                usage(stdout, server_ctl);
482
                goto destroy_server;
sletz's avatar
sletz committed
483
484
        }
    }
485

486
487
488
489
490
491
    // Long option with no letter so treated separately
    param = jackctl_get_parameter(server_parameters, "replace-registry");
    if (param != NULL) {
        value.b = replace_registry;
        jackctl_parameter_set_value(param, &value);
    }
492

sletz's avatar
sletz committed
493
    if (show_version) {
sletz's avatar
sletz committed
494
        printf( "jackdmp version " VERSION
nedko's avatar
nedko committed
495
496
497
498
                " tmpdir " jack_server_dir
                " protocol %d"
                "\n", JACK_PROTOCOL_VERSION);
        return -1;
sletz's avatar
sletz committed
499
    }
sletz's avatar
sletz committed
500

501
    if (!master_driver_name) {
502
        usage(stderr, server_ctl, false);
503
        goto destroy_server;
sletz's avatar
sletz committed
504
    }
sletz's avatar
sletz committed
505

506
    // Master driver
507
508
509
    master_driver_ctl = jackctl_server_get_driver(server_ctl, master_driver_name);
    if (master_driver_ctl == NULL) {
        fprintf(stderr, "Unknown driver \"%s\"\n", master_driver_name);
510
        goto destroy_server;
sletz's avatar
sletz committed
511
512
    }

sletz's avatar
sletz committed
513
514
515
516
517
    if (jackctl_driver_get_type(master_driver_ctl) != JackMaster) {
        fprintf(stderr, "Driver \"%s\" is not a master \n", master_driver_name);
        goto destroy_server;
    }

sletz's avatar
sletz committed
518
    if (optind < argc) {
519
        master_driver_nargs = 1 + argc - optind;
sletz's avatar
sletz committed
520
    } else {
521
        master_driver_nargs = 1;
sletz's avatar
sletz committed
522
523
    }

524
    if (master_driver_nargs == 0) {
sletz's avatar
sletz committed
525
        fprintf(stderr, "No driver specified ... hmm. JACK won't do"
sletz's avatar
sletz committed
526
                " anything when run like this.\n");
527
        goto destroy_server;
sletz's avatar
sletz committed
528
529
    }

530
531
    master_driver_args = (char **) malloc(sizeof(char *) * master_driver_nargs);
    master_driver_args[0] = master_driver_name;
sletz's avatar
sletz committed
532

533
534
    for (i = 1; i < master_driver_nargs; i++) {
        master_driver_args[i] = argv[optind++];
sletz's avatar
sletz committed
535
536
    }

537
    if (jackctl_driver_params_parse(master_driver_ctl, master_driver_nargs, master_driver_args)) {
538
        goto destroy_server;
sletz's avatar
sletz committed
539
    }
sletz's avatar
sletz committed
540

541
    // Setup signals
542
    sigmask = jackctl_setup_signals(0);
543

544
545
546
547
    // Open server
    if (! jackctl_server_open(server_ctl, master_driver_ctl)) {
        fprintf(stderr, "Failed to open server\n");
        goto destroy_server;
sletz's avatar
sletz committed
548
    }
sletz's avatar
sletz committed
549

550
    // Slave drivers
551
552
553
554
    for (it = slaves_list.begin(); it != slaves_list.end(); it++) {
        jackctl_driver_t * slave_driver_ctl = jackctl_server_get_driver(server_ctl, *it);
        if (slave_driver_ctl == NULL) {
            fprintf(stderr, "Unknown driver \"%s\"\n", *it);
555
            goto close_server;
556
        }
sletz's avatar
sletz committed
557
558
559
560
        if (jackctl_driver_get_type(slave_driver_ctl) != JackSlave) {
            fprintf(stderr, "Driver \"%s\" is not a slave \n", *it);
            goto close_server;
        }
Stephane Letz's avatar
Stephane Letz committed
561
562
        if (!jackctl_server_add_slave(server_ctl, slave_driver_ctl)) {
            fprintf(stderr, "Driver \"%s\" cannot be loaded\n", *it);
563
            goto close_server;
Stephane Letz's avatar
Stephane Letz committed
564
        }
565
    }
566

567
568
569
    // Loopback driver
    if (loopback > 0) {
        loopback_driver_ctl = jackctl_server_get_driver(server_ctl, "loopback");
570

571
572
573
574
575
576
577
        if (loopback_driver_ctl != NULL) {
            const JSList * loopback_parameters = jackctl_driver_get_parameters(loopback_driver_ctl);
            param = jackctl_get_parameter(loopback_parameters, "channels");
            if (param != NULL) {
                value.ui = loopback;
                jackctl_parameter_set_value(param, &value);
            }
Stephane Letz's avatar
Stephane Letz committed
578
579
            if (!jackctl_server_add_slave(server_ctl, loopback_driver_ctl)) {
                fprintf(stderr, "Driver \"loopback\" cannot be loaded\n");
580
                goto close_server;
Stephane Letz's avatar
Stephane Letz committed
581
            }
sletz's avatar
sletz committed
582
583
584
        } else {
            fprintf(stderr, "Driver \"loopback\" not found\n");
            goto close_server;
585
        }
586
587
588
589
590
591
    }

    // Start the server
    if (!jackctl_server_start(server_ctl)) {
        fprintf(stderr, "Failed to start server\n");
        goto close_server;
592
    }
593

594
    // Internal clients
595
596
597
598
    for (it = internals_list.begin(); it != internals_list.end(); it++) {
        jackctl_internal_t * internal_driver_ctl = jackctl_server_get_internal(server_ctl, *it);
        if (internal_driver_ctl == NULL) {
            fprintf(stderr, "Unknown internal \"%s\"\n", *it);
599
            goto stop_server;
600
        }
Stephane Letz's avatar
Stephane Letz committed
601
602
        if (!jackctl_server_load_internal(server_ctl, internal_driver_ctl)) {
            fprintf(stderr, "Internal client \"%s\" cannot be loaded\n", *it);
603
            goto stop_server;
Stephane Letz's avatar
Stephane Letz committed
604
        }
605
606
    }

sletz's avatar
sletz committed
607
    notify_server_start(server_name);
608
609
    notify_sent = true;
    return_value = 0;
sletz's avatar
sletz committed
610
611

    // Waits for signal
612
613
614
615
616
617
618
619
620
621
622
#ifdef __ANDROID__
    //reserve SIGUSR2 signal for switching master driver
    while(1) {
        int signal = jackctl_wait_signals_and_return(sigmask);
        if (signal == SIGUSR2) {
            jackctl_server_switch_master_dummy(server_ctl, master_driver_name);
        } else {
            break;
        }
    }
#else
623
    jackctl_wait_signals(sigmask);
624
#endif
sletz's avatar
sletz committed
625

626
 stop_server:
sletz's avatar
sletz committed
627
    if (!jackctl_server_stop(server_ctl)) {
sletz's avatar
Cleanup    
sletz committed
628
        fprintf(stderr, "Cannot stop server...\n");
629
    }
sletz's avatar
sletz committed
630

631
 close_server:
sletz's avatar
sletz committed
632
    if (loopback > 0 && loopback_driver_ctl) {
sletz's avatar
sletz committed
633
634
635
636
637
        jackctl_server_remove_slave(server_ctl, loopback_driver_ctl);
    }
    // Slave drivers
    for (it = slaves_list.begin(); it != slaves_list.end(); it++) {
        jackctl_driver_t * slave_driver_ctl = jackctl_server_get_driver(server_ctl, *it);
638
        if (slave_driver_ctl) {
sletz's avatar
sletz committed
639
            jackctl_server_remove_slave(server_ctl, slave_driver_ctl);
640
        }
sletz's avatar
sletz committed
641
    }
sletz's avatar
sletz committed
642

sletz's avatar
sletz committed
643
644
645
    // Internal clients
    for (it = internals_list.begin(); it != internals_list.end(); it++) {
        jackctl_internal_t * internal_driver_ctl = jackctl_server_get_internal(server_ctl, *it);
646
        if (internal_driver_ctl) {
sletz's avatar
sletz committed
647
            jackctl_server_unload_internal(server_ctl, internal_driver_ctl);
648
        }
sletz's avatar
sletz committed
649
    }
650
    jackctl_server_close(server_ctl);
sletz's avatar
sletz committed
651

652
 destroy_server:
sletz's avatar
sletz committed
653
    jackctl_server_destroy(server_ctl);
654
655
656
    if (notify_sent) {
        notify_server_stop(server_name);
    }
657
    return return_value;
sletz's avatar
sletz committed
658
}